1 /* $Id: avm_cs.c,v 1.4.6.3 2001/09/23 22:24:33 kai Exp $
3 * A PCMCIA client driver for AVM B1/M1/M2
5 * Copyright 1999 by Carsten Paeth <calle@calle.de>
7 * This software may be used and distributed according to the terms
8 * of the GNU General Public License, incorporated herein by reference.
12 #include <linux/module.h>
13 #include <linux/kernel.h>
14 #include <linux/init.h>
15 #include <linux/sched.h>
16 #include <linux/ptrace.h>
17 #include <linux/slab.h>
18 #include <linux/string.h>
19 #include <linux/tty.h>
20 #include <linux/serial.h>
21 #include <linux/major.h>
23 #include <asm/system.h>
25 #include <pcmcia/version.h>
26 #include <pcmcia/cs_types.h>
27 #include <pcmcia/cs.h>
28 #include <pcmcia/cistpl.h>
29 #include <pcmcia/ciscode.h>
30 #include <pcmcia/ds.h>
31 #include <pcmcia/cisreg.h>
33 #include <linux/skbuff.h>
34 #include <linux/capi.h>
35 #include <linux/b1lli.h>
36 #include <linux/b1pcmcia.h>
38 /*====================================================================*/
40 MODULE_DESCRIPTION("CAPI4Linux: PCMCIA client driver for AVM B1/M1/M2");
41 MODULE_AUTHOR("Carsten Paeth");
42 MODULE_LICENSE("GPL");
44 /*====================================================================*/
46 /* Parameters that can be set with 'insmod' */
48 /* This means pick from 15, 12, 11, 10, 9, 7, 5, 4, and 3 */
49 static int default_irq_list[10] = { 15, 12, 11, 10, 9, 7, 5, 4, 3, -1 };
50 static int irq_list[10] = { -1 };
52 MODULE_PARM(irq_list, "1-10i");
54 /*====================================================================*/
57 The event() function is this driver's Card Services event handler.
58 It will be called by Card Services when an appropriate card status
59 event is received. The config() and release() entry points are
60 used to configure or release a socket, in response to card insertion
61 and ejection events. They are invoked from the skeleton event
65 static void avmcs_config(dev_link_t *link);
66 static void avmcs_release(dev_link_t *link);
67 static int avmcs_event(event_t event, int priority,
68 event_callback_args_t *args);
71 The attach() and detach() entry points are used to create and destroy
72 "instances" of the driver, where each instance represents everything
73 needed to manage one actual PCMCIA card.
76 static dev_link_t *avmcs_attach(void);
77 static void avmcs_detach(dev_link_t *);
80 The dev_info variable is the "key" that is used to match up this
81 device driver with appropriate cards, through the card configuration
85 static dev_info_t dev_info = "avm_cs";
88 A linked list of "instances" of the skeleton device. Each actual
89 PCMCIA card corresponds to one device instance, and is described
90 by one dev_link_t structure (defined in ds.h).
92 You may not want to use a linked list for this -- for example, the
93 memory card driver uses an array of dev_link_t pointers, where minor
94 device numbers are used to derive the corresponding array index.
97 static dev_link_t *dev_list = NULL;
100 A dev_link_t structure has fields for most things that are needed
101 to keep track of a socket, but there will usually be some device
102 specific information that also needs to be kept track of. The
103 'priv' pointer in a dev_link_t structure can be used to point to
104 a device-specific private data structure, like this.
106 A driver needs to provide a dev_node_t structure for each device
107 on a card. In some cases, there is only one device per card (for
108 example, ethernet cards, modems). In other cases, there may be
109 many actual or logical devices (SCSI adapters, memory cards with
110 multiple partitions). The dev_node_t structures need to be kept
111 in a linked list starting at the 'dev' field of a dev_link_t
112 structure. We allocate them in the card's private data structure,
113 because they generally can't be allocated dynamically.
116 typedef struct local_info_t {
120 /*======================================================================
122 avmcs_attach() creates an "instance" of the driver, allocating
123 local data structures for one device. The device is registered
126 The dev_link structure is initialized, but we don't actually
127 configure the card at this point -- we wait until we receive a
128 card insertion event.
130 ======================================================================*/
132 static dev_link_t *avmcs_attach(void)
134 client_reg_t client_reg;
139 /* Initialize the dev_link_t structure */
140 link = kmalloc(sizeof(struct dev_link_t), GFP_KERNEL);
143 memset(link, 0, sizeof(struct dev_link_t));
145 /* The io structure describes IO port mapping */
146 link->io.NumPorts1 = 16;
147 link->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
148 link->io.NumPorts2 = 0;
150 /* Interrupt setup */
151 link->irq.Attributes = IRQ_TYPE_EXCLUSIVE;
152 link->irq.Attributes = IRQ_TYPE_DYNAMIC_SHARING|IRQ_FIRST_SHARED;
154 link->irq.IRQInfo1 = IRQ_INFO2_VALID|IRQ_LEVEL_ID;
155 if (irq_list[0] != -1) {
156 for (i = 0; i < 10 && irq_list[i] > 0; i++)
157 link->irq.IRQInfo2 |= 1 << irq_list[i];
159 for (i = 0; i < 10 && default_irq_list[i] > 0; i++)
160 link->irq.IRQInfo2 |= 1 << default_irq_list[i];
163 /* General socket configuration */
164 link->conf.Attributes = CONF_ENABLE_IRQ;
166 link->conf.IntType = INT_MEMORY_AND_IO;
167 link->conf.ConfigIndex = 1;
168 link->conf.Present = PRESENT_OPTION;
170 /* Allocate space for private device-specific data */
171 local = kmalloc(sizeof(local_info_t), GFP_KERNEL);
174 memset(local, 0, sizeof(local_info_t));
177 /* Register with Card Services */
178 link->next = dev_list;
180 client_reg.dev_info = &dev_info;
181 client_reg.Attributes = INFO_IO_CLIENT | INFO_CARD_SHARE;
182 client_reg.EventMask =
183 CS_EVENT_CARD_INSERTION | CS_EVENT_CARD_REMOVAL |
184 CS_EVENT_RESET_PHYSICAL | CS_EVENT_CARD_RESET |
185 CS_EVENT_PM_SUSPEND | CS_EVENT_PM_RESUME;
186 client_reg.event_handler = &avmcs_event;
187 client_reg.Version = 0x0210;
188 client_reg.event_callback_args.client_data = link;
189 ret = pcmcia_register_client(&link->handle, &client_reg);
191 cs_error(link->handle, RegisterClient, ret);
203 /*======================================================================
205 This deletes a driver "instance". The device is de-registered
206 with Card Services. If it has been released, all local data
207 structures are freed. Otherwise, the structures will be freed
208 when the device is released.
210 ======================================================================*/
212 static void avmcs_detach(dev_link_t *link)
216 /* Locate device structure */
217 for (linkp = &dev_list; *linkp; linkp = &(*linkp)->next)
218 if (*linkp == link) break;
223 If the device is currently configured and active, we won't
224 actually delete it yet. Instead, it is marked so that when
225 the release() function is called, that will trigger a proper
228 if (link->state & DEV_CONFIG) {
229 link->state |= DEV_STALE_LINK;
233 /* Break the link with Card Services */
235 pcmcia_deregister_client(link->handle);
237 /* Unlink device structure, free pieces */
246 /*======================================================================
248 avmcs_config() is scheduled to run after a CARD_INSERTION event
249 is received, to configure the PCMCIA socket, and to make the
250 ethernet device available to the system.
252 ======================================================================*/
254 static int get_tuple(client_handle_t handle, tuple_t *tuple,
257 int i = pcmcia_get_tuple_data(handle, tuple);
258 if (i != CS_SUCCESS) return i;
259 return pcmcia_parse_tuple(handle, tuple, parse);
262 static int first_tuple(client_handle_t handle, tuple_t *tuple,
265 int i = pcmcia_get_first_tuple(handle, tuple);
266 if (i != CS_SUCCESS) return i;
267 return get_tuple(handle, tuple, parse);
270 static int next_tuple(client_handle_t handle, tuple_t *tuple,
273 int i = pcmcia_get_next_tuple(handle, tuple);
274 if (i != CS_SUCCESS) return i;
275 return get_tuple(handle, tuple, parse);
278 static void avmcs_config(dev_link_t *link)
280 client_handle_t handle;
283 cistpl_cftable_entry_t *cf = &parse.cftable_entry;
289 int (*addcard)(unsigned int port, unsigned irq);
291 handle = link->handle;
295 This reads the card's CONFIG tuple to find its configuration
299 tuple.DesiredTuple = CISTPL_CONFIG;
300 i = pcmcia_get_first_tuple(handle, &tuple);
301 if (i != CS_SUCCESS) break;
302 tuple.TupleData = buf;
303 tuple.TupleDataMax = 64;
304 tuple.TupleOffset = 0;
305 i = pcmcia_get_tuple_data(handle, &tuple);
306 if (i != CS_SUCCESS) break;
307 i = pcmcia_parse_tuple(handle, &tuple, &parse);
308 if (i != CS_SUCCESS) break;
309 link->conf.ConfigBase = parse.config.base;
311 if (i != CS_SUCCESS) {
312 cs_error(link->handle, ParseTuple, i);
313 link->state &= ~DEV_CONFIG_PENDING;
318 link->state |= DEV_CONFIG;
322 tuple.Attributes = 0;
323 tuple.TupleData = buf;
324 tuple.TupleDataMax = 254;
325 tuple.TupleOffset = 0;
326 tuple.DesiredTuple = CISTPL_VERS_1;
329 if( !first_tuple(handle, &tuple, &parse) && parse.version_1.ns > 1 ) {
330 strlcpy(devname,parse.version_1.str + parse.version_1.ofs[1],
336 tuple.TupleData = (cisdata_t *)buf;
337 tuple.TupleOffset = 0; tuple.TupleDataMax = 255;
338 tuple.Attributes = 0;
339 tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
340 i = first_tuple(handle, &tuple, &parse);
341 while (i == CS_SUCCESS) {
342 if (cf->io.nwin > 0) {
343 link->conf.ConfigIndex = cf->index;
344 link->io.BasePort1 = cf->io.win[0].base;
345 link->io.NumPorts1 = cf->io.win[0].len;
346 link->io.NumPorts2 = 0;
347 printk(KERN_INFO "avm_cs: testing i/o %#x-%#x\n",
349 link->io.BasePort1+link->io.NumPorts1-1);
350 i = pcmcia_request_io(link->handle, &link->io);
351 if (i == CS_SUCCESS) goto found_port;
353 i = next_tuple(handle, &tuple, &parse);
357 if (i != CS_SUCCESS) {
358 cs_error(link->handle, RequestIO, i);
363 * allocate an interrupt line
365 i = pcmcia_request_irq(link->handle, &link->irq);
366 if (i != CS_SUCCESS) {
367 cs_error(link->handle, RequestIRQ, i);
368 pcmcia_release_io(link->handle, &link->io);
373 * configure the PCMCIA socket
375 i = pcmcia_request_configuration(link->handle, &link->conf);
376 if (i != CS_SUCCESS) {
377 cs_error(link->handle, RequestConfiguration, i);
378 pcmcia_release_io(link->handle, &link->io);
379 pcmcia_release_irq(link->handle, &link->irq);
385 /* At this point, the dev_node_t structure(s) should be
386 initialized and arranged in a linked list at link->dev. */
389 char *s = strrchr(devname, ' ');
393 strcpy(dev->node.dev_name, s);
394 if (strcmp("M1", s) == 0) {
395 cardtype = AVM_CARDTYPE_M1;
396 } else if (strcmp("M2", s) == 0) {
397 cardtype = AVM_CARDTYPE_M2;
399 cardtype = AVM_CARDTYPE_B1;
402 strcpy(dev->node.dev_name, "b1");
403 cardtype = AVM_CARDTYPE_B1;
406 dev->node.major = 64;
408 link->dev = &dev->node;
410 link->state &= ~DEV_CONFIG_PENDING;
411 /* If any step failed, release any partially configured state */
419 case AVM_CARDTYPE_M1: addcard = b1pcmcia_addcard_m1; break;
420 case AVM_CARDTYPE_M2: addcard = b1pcmcia_addcard_m2; break;
422 case AVM_CARDTYPE_B1: addcard = b1pcmcia_addcard_b1; break;
424 if ((i = (*addcard)(link->io.BasePort1, link->irq.AssignedIRQ)) < 0) {
425 printk(KERN_ERR "avm_cs: failed to add AVM-%s-Controller at i/o %#x, irq %d\n",
426 dev->node.dev_name, link->io.BasePort1, link->irq.AssignedIRQ);
434 /*======================================================================
436 After a card is removed, avmcs_release() will unregister the net
437 device, and release the PCMCIA configuration. If the device is
438 still open, this will be postponed until it is closed.
440 ======================================================================*/
442 static void avmcs_release(dev_link_t *link)
444 b1pcmcia_delcard(link->io.BasePort1, link->irq.AssignedIRQ);
446 /* Unlink the device chain */
449 /* Don't bother checking to see if these succeed or not */
450 pcmcia_release_configuration(link->handle);
451 pcmcia_release_io(link->handle, &link->io);
452 pcmcia_release_irq(link->handle, &link->irq);
453 link->state &= ~DEV_CONFIG;
455 if (link->state & DEV_STALE_LINK)
458 } /* avmcs_release */
460 /*======================================================================
462 The card status event handler. Mostly, this schedules other
463 stuff to run after an event is received. A CARD_REMOVAL event
464 also sets some flags to discourage the net drivers from trying
465 to talk to the card any more.
467 When a CARD_REMOVAL event is received, we immediately set a flag
468 to block future accesses to this device. All the functions that
469 actually access the device should check this flag to make sure
470 the card is still present.
472 ======================================================================*/
474 static int avmcs_event(event_t event, int priority,
475 event_callback_args_t *args)
477 dev_link_t *link = args->client_data;
480 case CS_EVENT_CARD_REMOVAL:
481 link->state &= ~DEV_PRESENT;
482 if (link->state & DEV_CONFIG)
485 case CS_EVENT_CARD_INSERTION:
486 link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
489 case CS_EVENT_PM_SUSPEND:
490 link->state |= DEV_SUSPEND;
491 /* Fall through... */
492 case CS_EVENT_RESET_PHYSICAL:
493 if (link->state & DEV_CONFIG)
494 pcmcia_release_configuration(link->handle);
496 case CS_EVENT_PM_RESUME:
497 link->state &= ~DEV_SUSPEND;
498 /* Fall through... */
499 case CS_EVENT_CARD_RESET:
500 if (link->state & DEV_CONFIG)
501 pcmcia_request_configuration(link->handle, &link->conf);
507 static struct pcmcia_driver avmcs_driver = {
508 .owner = THIS_MODULE,
512 .attach = avmcs_attach,
513 .detach = avmcs_detach,
516 static int __init avmcs_init(void)
518 return pcmcia_register_driver(&avmcs_driver);
521 static void __exit avmcs_exit(void)
523 pcmcia_unregister_driver(&avmcs_driver);
525 /* XXX: this really needs to move into generic code.. */
526 while (dev_list != NULL) {
527 if (dev_list->state & DEV_CONFIG)
528 avmcs_release(dev_list);
529 avmcs_detach(dev_list);
533 module_init(avmcs_init);
534 module_exit(avmcs_exit);