1 /*======================================================================
3 An elsa_cs PCMCIA client driver
5 This driver is for the Elsa PCM ISDN Cards, i.e. the MicroLink
8 The contents of this file are subject to the Mozilla Public
9 License Version 1.1 (the "License"); you may not use this file
10 except in compliance with the License. You may obtain a copy of
11 the License at http://www.mozilla.org/MPL/
13 Software distributed under the License is distributed on an "AS
14 IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
15 implied. See the License for the specific language governing
16 rights and limitations under the License.
18 The initial developer of the original code is David A. Hinds
19 <dahinds@users.sourceforge.net>. Portions created by David A. Hinds
20 are Copyright (C) 1999 David A. Hinds. All Rights Reserved.
22 Modifications from dummy_cs.c are Copyright (C) 1999-2001 Klaus
23 Lichtenwalder <Lichtenwalder@ACM.org>. All Rights Reserved.
25 Alternatively, the contents of this file may be used under the
26 terms of the GNU General Public License version 2 (the "GPL"), in
27 which case the provisions of the GPL are applicable instead of the
28 above. If you wish to allow the use of your version of this file
29 only under the terms of the GPL and not to allow others to use
30 your version of this file under the MPL, indicate your decision
31 by deleting the provisions above and replace them with the notice
32 and other provisions required by the GPL. If you do not delete
33 the provisions above, a recipient may use your version of this
34 file under either the MPL or the GPL.
36 ======================================================================*/
38 #include <linux/module.h>
39 #include <linux/kernel.h>
40 #include <linux/init.h>
41 #include <linux/sched.h>
42 #include <linux/ptrace.h>
43 #include <linux/slab.h>
44 #include <linux/string.h>
45 #include <linux/timer.h>
46 #include <linux/ioport.h>
48 #include <asm/system.h>
50 #include <pcmcia/version.h>
51 #include <pcmcia/cs_types.h>
52 #include <pcmcia/cs.h>
53 #include <pcmcia/cistpl.h>
54 #include <pcmcia/cisreg.h>
55 #include <pcmcia/ds.h>
56 #include <pcmcia/bus_ops.h>
58 MODULE_DESCRIPTION("ISDN4Linux: PCMCIA client driver for Elsa PCM cards");
59 MODULE_AUTHOR("Klaus Lichtenwalder");
60 MODULE_LICENSE("Dual MPL/GPL");
63 All the PCMCIA modules use PCMCIA_DEBUG to control debugging. If
64 you do not define PCMCIA_DEBUG at all, all the debug code will be
65 left out. If you compile with PCMCIA_DEBUG=0, the debug code will
66 be present but disabled -- but it can then be enabled for specific
67 modules at load time with a 'pc_debug=#' option to insmod.
71 static int pc_debug = PCMCIA_DEBUG;
72 MODULE_PARM(pc_debug, "i");
73 #define DEBUG(n, args...) if (pc_debug>(n)) printk(KERN_DEBUG args);
74 static char *version =
75 "elsa_cs.c $Revision: 1.1.4.1 $ $Date: 2001/11/20 14:19:35 $ (K.Lichtenwalder)";
77 #define DEBUG(n, args...)
80 /*====================================================================*/
82 /* Parameters that can be set with 'insmod' */
84 /* Bit map of interrupts to choose from, the old way */
85 /* This means pick from 15, 14, 12, 11, 10, 9, 7, 5, 4, 3 */
86 static u_long irq_mask = 0xdeb8;
88 /* Newer, simpler way of listing specific interrupts */
89 static int irq_list[4] = { -1 };
91 MODULE_PARM(irq_mask, "i");
92 MODULE_PARM(irq_list, "1-4i");
94 static int protocol = 2; /* EURO-ISDN Default */
95 MODULE_PARM(protocol, "i");
97 extern int elsa_init_pcmcia(int, int, int*, int);
99 /*====================================================================*/
102 The event() function is this driver's Card Services event handler.
103 It will be called by Card Services when an appropriate card status
104 event is received. The config() and release() entry points are
105 used to configure or release a socket, in response to card insertion
106 and ejection events. They are invoked from the elsa_cs event
110 static void elsa_cs_config(dev_link_t *link);
111 static void elsa_cs_release(u_long arg);
112 static int elsa_cs_event(event_t event, int priority,
113 event_callback_args_t *args);
116 The attach() and detach() entry points are used to create and destroy
117 "instances" of the driver, where each instance represents everything
118 needed to manage one actual PCMCIA card.
121 static dev_link_t *elsa_cs_attach(void);
122 static void elsa_cs_detach(dev_link_t *);
125 The dev_info variable is the "key" that is used to match up this
126 device driver with appropriate cards, through the card configuration
130 static dev_info_t dev_info = "elsa_cs";
133 A linked list of "instances" of the elsa_cs device. Each actual
134 PCMCIA card corresponds to one device instance, and is described
135 by one dev_link_t structure (defined in ds.h).
137 You may not want to use a linked list for this -- for example, the
138 memory card driver uses an array of dev_link_t pointers, where minor
139 device numbers are used to derive the corresponding array index.
142 static dev_link_t *dev_list = NULL;
145 A dev_link_t structure has fields for most things that are needed
146 to keep track of a socket, but there will usually be some device
147 specific information that also needs to be kept track of. The
148 'priv' pointer in a dev_link_t structure can be used to point to
149 a device-specific private data structure, like this.
151 To simplify the data structure handling, we actually include the
152 dev_link_t structure in the device's private data structure.
154 A driver needs to provide a dev_node_t structure for each device
155 on a card. In some cases, there is only one device per card (for
156 example, ethernet cards, modems). In other cases, there may be
157 many actual or logical devices (SCSI adapters, memory cards with
158 multiple partitions). The dev_node_t structures need to be kept
159 in a linked list starting at the 'dev' field of a dev_link_t
160 structure. We allocate them in the card's private data structure,
161 because they generally shouldn't be allocated dynamically.
162 In this case, we also provide a flag to indicate if a device is
163 "stopped" due to a power management event, or card ejection. The
164 device IO routines can use a flag like this to throttle IO to a
165 card that is not ready to accept it.
167 The bus_operations pointer is used on platforms for which we need
168 to use special socket-specific versions of normal IO primitives
169 (inb, outb, readb, writeb, etc) for card IO.
172 typedef struct local_info_t {
176 struct bus_operations *bus;
179 /*====================================================================*/
181 static void cs_error(client_handle_t handle, int func, int ret)
183 error_info_t err = { func, ret};
184 CardServices(ReportError, handle, &err);
187 /*======================================================================
189 elsa_cs_attach() creates an "instance" of the driver, allocatingx
190 local data structures for one device. The device is registered
193 The dev_link structure is initialized, but we don't actually
194 configure the card at this point -- we wait until we receive a
195 card insertion event.
197 ======================================================================*/
199 static dev_link_t *elsa_cs_attach(void)
201 client_reg_t client_reg;
205 void elsa_interrupt(int, void *, struct pt_regs *);
207 DEBUG(0, "elsa_cs_attach()\n");
209 /* Allocate space for private device-specific data */
210 local = kmalloc(sizeof(local_info_t), GFP_KERNEL);
211 if (!local) return NULL;
212 memset(local, 0, sizeof(local_info_t));
213 link = &local->link; link->priv = local;
215 /* Initialize the dev_link_t structure */
216 link->release.function = &elsa_cs_release;
217 link->release.data = (u_long)link;
219 /* Interrupt setup */
220 link->irq.Attributes = IRQ_TYPE_DYNAMIC_SHARING|IRQ_FIRST_SHARED;
221 link->irq.IRQInfo1 = IRQ_INFO2_VALID|IRQ_LEVEL_ID|IRQ_SHARE_ID;
222 if (irq_list[0] == -1)
223 link->irq.IRQInfo2 = irq_mask;
225 for (i = 0; i < 4; i++)
226 link->irq.IRQInfo2 |= 1 << irq_list[i];
227 link->irq.Handler = NULL;
230 General socket configuration defaults can go here. In this
231 client, we assume very little, and rely on the CIS for almost
232 everything. In most clients, many details (i.e., number, sizes,
233 and attributes of IO windows) are fixed by the nature of the
234 device, and can be hard-wired here.
236 link->io.NumPorts1 = 8;
237 link->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO;
238 link->io.IOAddrLines = 3;
240 link->conf.Attributes = CONF_ENABLE_IRQ;
242 link->conf.IntType = INT_MEMORY_AND_IO;
244 /* Register with Card Services */
245 link->next = dev_list;
247 client_reg.dev_info = &dev_info;
248 client_reg.Attributes = INFO_IO_CLIENT | INFO_CARD_SHARE;
249 client_reg.EventMask =
250 CS_EVENT_CARD_INSERTION | CS_EVENT_CARD_REMOVAL |
251 CS_EVENT_RESET_PHYSICAL | CS_EVENT_CARD_RESET |
252 CS_EVENT_PM_SUSPEND | CS_EVENT_PM_RESUME;
253 client_reg.event_handler = &elsa_cs_event;
254 client_reg.Version = 0x0210;
255 client_reg.event_callback_args.client_data = link;
256 ret = CardServices(RegisterClient, &link->handle, &client_reg);
257 if (ret != CS_SUCCESS) {
258 cs_error(link->handle, RegisterClient, ret);
259 elsa_cs_detach(link);
264 } /* elsa_cs_attach */
266 /*======================================================================
268 This deletes a driver "instance". The device is de-registered
269 with Card Services. If it has been released, all local data
270 structures are freed. Otherwise, the structures will be freed
271 when the device is released.
273 ======================================================================*/
275 static void elsa_cs_detach(dev_link_t *link)
278 local_info_t *info = link->priv;
281 DEBUG(0, "elsa_cs_detach(0x%p)\n", link);
283 /* Locate device structure */
284 for (linkp = &dev_list; *linkp; linkp = &(*linkp)->next)
285 if (*linkp == link) break;
289 del_timer(&link->release);
290 if (link->state & DEV_CONFIG)
291 elsa_cs_release((u_long)link);
294 If the device is currently configured and active, we won't
295 actually delete it yet. Instead, it is marked so that when
296 the release() function is called, that will trigger a proper
299 if (link->state & DEV_CONFIG) {
300 DEBUG(0, "elsa_cs: detach postponed, '%s' "
301 "still locked\n", link->dev->dev_name);
302 link->state |= DEV_STALE_LINK;
306 /* Break the link with Card Services */
308 ret = CardServices(DeregisterClient, link->handle);
309 if (ret != CS_SUCCESS)
310 cs_error(link->handle, DeregisterClient, ret);
313 /* Unlink device structure and free it */
317 } /* elsa_cs_detach */
319 /*======================================================================
321 elsa_cs_config() is scheduled to run after a CARD_INSERTION event
322 is received, to configure the PCMCIA socket, and to make the
323 device available to the system.
325 ======================================================================*/
326 static int get_tuple(int fn, client_handle_t handle, tuple_t *tuple,
330 i = CardServices(fn, handle, tuple);
331 if (i != CS_SUCCESS) return i;
332 i = CardServices(GetTupleData, handle, tuple);
333 if (i != CS_SUCCESS) return i;
334 return CardServices(ParseTuple, handle, tuple, parse);
337 #define first_tuple(a, b, c) get_tuple(GetFirstTuple, a, b, c)
338 #define next_tuple(a, b, c) get_tuple(GetNextTuple, a, b, c)
340 static void elsa_cs_config(dev_link_t *link)
342 client_handle_t handle;
348 cistpl_cftable_entry_t *cf = &parse.cftable_entry;
350 DEBUG(0, "elsa_config(0x%p)\n", link);
351 handle = link->handle;
355 This reads the card's CONFIG tuple to find its configuration
358 tuple.DesiredTuple = CISTPL_CONFIG;
359 tuple.TupleData = (cisdata_t *)buf;
360 tuple.TupleDataMax = 255;
361 tuple.TupleOffset = 0;
362 tuple.Attributes = 0;
363 i = first_tuple(handle, &tuple, &parse);
364 if (i != CS_SUCCESS) {
365 last_fn = ParseTuple;
368 link->conf.ConfigBase = parse.config.base;
369 link->conf.Present = parse.config.rmask[0];
372 link->state |= DEV_CONFIG;
374 tuple.TupleData = (cisdata_t *)buf;
375 tuple.TupleOffset = 0; tuple.TupleDataMax = 255;
376 tuple.Attributes = 0;
377 tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
378 i = first_tuple(handle, &tuple, &parse);
379 while (i == CS_SUCCESS) {
380 if ( (cf->io.nwin > 0) && cf->io.win[0].base) {
381 printk(KERN_INFO "(elsa_cs: looks like the 96 model)\n");
382 link->conf.ConfigIndex = cf->index;
383 link->io.BasePort1 = cf->io.win[0].base;
384 i = CardServices(RequestIO, link->handle, &link->io);
385 if (i == CS_SUCCESS) break;
387 printk(KERN_INFO "(elsa_cs: looks like the 97 model)\n");
388 link->conf.ConfigIndex = cf->index;
389 for (i = 0, j = 0x2f0; j > 0x100; j -= 0x10) {
390 link->io.BasePort1 = j;
391 i = CardServices(RequestIO, link->handle, &link->io);
392 if (i == CS_SUCCESS) break;
396 i = next_tuple(handle, &tuple, &parse);
399 if (i != CS_SUCCESS) {
404 i = CardServices(RequestIRQ, link->handle, &link->irq);
405 if (i != CS_SUCCESS) {
406 link->irq.AssignedIRQ = 0;
407 last_fn = RequestIRQ;
411 i = CardServices(RequestConfiguration, link->handle, &link->conf);
412 if (i != CS_SUCCESS) {
413 last_fn = RequestConfiguration;
417 /* At this point, the dev_node_t structure(s) should be
418 initialized and arranged in a linked list at link->dev. *//* */
419 sprintf(dev->node.dev_name, "elsa");
420 dev->node.major = dev->node.minor = 0x0;
422 link->dev = &dev->node;
424 /* Finally, report what we've done */
425 printk(KERN_INFO "%s: index 0x%02x: Vcc %d.%d",
426 dev->node.dev_name, link->conf.ConfigIndex,
427 link->conf.Vcc/10, link->conf.Vcc%10);
429 printk(", Vpp %d.%d", link->conf.Vpp1/10, link->conf.Vpp1%10);
430 if (link->conf.Attributes & CONF_ENABLE_IRQ)
431 printk(", irq %d", link->irq.AssignedIRQ);
432 if (link->io.NumPorts1)
433 printk(", io 0x%04x-0x%04x", link->io.BasePort1,
434 link->io.BasePort1+link->io.NumPorts1-1);
435 if (link->io.NumPorts2)
436 printk(" & 0x%04x-0x%04x", link->io.BasePort2,
437 link->io.BasePort2+link->io.NumPorts2-1);
440 link->state &= ~DEV_CONFIG_PENDING;
442 elsa_init_pcmcia(link->io.BasePort1, link->irq.AssignedIRQ,
443 &(((local_info_t*)link->priv)->busy),
447 cs_error(link->handle, last_fn, i);
448 elsa_cs_release((u_long)link);
449 } /* elsa_cs_config */
451 /*======================================================================
453 After a card is removed, elsa_cs_release() will unregister the net
454 device, and release the PCMCIA configuration. If the device is
455 still open, this will be postponed until it is closed.
457 ======================================================================*/
459 static void elsa_cs_release(u_long arg)
461 dev_link_t *link = (dev_link_t *)arg;
463 DEBUG(0, "elsa_cs_release(0x%p)\n", link);
466 If the device is currently in use, we won't release until it
467 is actually closed, because until then, we can't be sure that
468 no one will try to access the device or its data structures.
471 DEBUG(1, "elsa_cs: release postponed, '%s' "
472 "still open\n", link->dev->dev_name);
473 link->state |= DEV_STALE_CONFIG;
477 /* Unlink the device chain */
480 /* Don't bother checking to see if these succeed or not */
482 CardServices(ReleaseWindow, link->win);
483 CardServices(ReleaseConfiguration, link->handle);
484 CardServices(ReleaseIO, link->handle, &link->io);
485 CardServices(ReleaseIRQ, link->handle, &link->irq);
486 link->state &= ~DEV_CONFIG;
488 if (link->state & DEV_STALE_LINK)
489 elsa_cs_detach(link);
491 } /* elsa_cs_release */
493 /*======================================================================
495 The card status event handler. Mostly, this schedules other
496 stuff to run after an event is received. A CARD_REMOVAL event
497 also sets some flags to discourage the net drivers from trying
498 to talk to the card any more.
500 When a CARD_REMOVAL event is received, we immediately set a flag
501 to block future accesses to this device. All the functions that
502 actually access the device should check this flag to make sure
503 the card is still present.
505 ======================================================================*/
507 static int elsa_cs_event(event_t event, int priority,
508 event_callback_args_t *args)
510 dev_link_t *link = args->client_data;
511 local_info_t *dev = link->priv;
513 DEBUG(1, "elsa_cs_event(%d)\n", event);
516 case CS_EVENT_CARD_REMOVAL:
517 link->state &= ~DEV_PRESENT;
518 if (link->state & DEV_CONFIG) {
519 ((local_info_t*)link->priv)->busy = 1;
520 mod_timer(&link->release, jiffies + HZ/20);
523 case CS_EVENT_CARD_INSERTION:
524 link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
525 dev->bus = args->bus;
526 elsa_cs_config(link);
528 case CS_EVENT_PM_SUSPEND:
529 link->state |= DEV_SUSPEND;
530 /* Fall through... */
531 case CS_EVENT_RESET_PHYSICAL:
532 /* Mark the device as stopped, to block IO until later */
534 if (link->state & DEV_CONFIG)
535 CardServices(ReleaseConfiguration, link->handle);
537 case CS_EVENT_PM_RESUME:
538 link->state &= ~DEV_SUSPEND;
539 /* Fall through... */
540 case CS_EVENT_CARD_RESET:
541 if (link->state & DEV_CONFIG)
542 CardServices(RequestConfiguration, link->handle, &link->conf);
547 } /* elsa_cs_event */
549 /*====================================================================*/
551 static int __init init_elsa_cs(void)
554 DEBUG(0, "%s\n", version);
555 CardServices(GetCardServicesInfo, &serv);
556 if (serv.Revision != CS_RELEASE_CODE) {
557 printk(KERN_NOTICE "elsa_cs: Card Services release "
558 "does not match!\n");
561 register_pccard_driver(&dev_info, &elsa_cs_attach, &elsa_cs_detach);
565 static void __exit exit_elsa_cs(void)
567 DEBUG(0, "elsa_cs: unloading\n");
568 unregister_pccard_driver(&dev_info);
569 while (dev_list != NULL)
570 elsa_cs_detach(dev_list);
573 module_init(init_elsa_cs);
574 module_exit(exit_elsa_cs);