2 * PCMCIA client driver for AVM A1 / Fritz!PCMCIA
5 * Copyright 1998-2001 by Carsten Paeth <calle@calle.in-berlin.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>
15 #include <linux/kernel.h>
16 #include <linux/init.h>
17 #include <linux/sched.h>
18 #include <linux/ptrace.h>
19 #include <linux/slab.h>
20 #include <linux/string.h>
22 #include <asm/system.h>
24 #include <pcmcia/version.h>
25 #include <pcmcia/cs_types.h>
26 #include <pcmcia/cs.h>
27 #include <pcmcia/cistpl.h>
28 #include <pcmcia/ds.h>
29 #include "hisax_cfg.h"
31 MODULE_DESCRIPTION("ISDN4Linux: PCMCIA client driver for AVM A1/Fritz!PCMCIA cards");
32 MODULE_AUTHOR("Carsten Paeth");
33 MODULE_LICENSE("GPL");
36 All the PCMCIA modules use PCMCIA_DEBUG to control debugging. If
37 you do not define PCMCIA_DEBUG at all, all the debug code will be
38 left out. If you compile with PCMCIA_DEBUG=0, the debug code will
39 be present but disabled -- but it can then be enabled for specific
40 modules at load time with a 'pc_debug=#' option to insmod.
43 static int pc_debug = PCMCIA_DEBUG;
44 module_param(pc_debug, int, 0);
45 #define DEBUG(n, args...) if (pc_debug>(n)) printk(KERN_DEBUG args);
46 static char *version =
47 "avma1_cs.c 1.00 1998/01/23 10:00:00 (Carsten Paeth)";
49 #define DEBUG(n, args...)
52 /*====================================================================*/
54 /* Parameters that can be set with 'insmod' */
56 static int isdnprot = 2;
58 module_param(isdnprot, int, 0);
60 /*====================================================================*/
63 The event() function is this driver's Card Services event handler.
64 It will be called by Card Services when an appropriate card status
65 event is received. The config() and release() entry points are
66 used to configure or release a socket, in response to card insertion
67 and ejection events. They are invoked from the skeleton event
71 static void avma1cs_config(dev_link_t *link);
72 static void avma1cs_release(dev_link_t *link);
73 static int avma1cs_event(event_t event, int priority,
74 event_callback_args_t *args);
77 The attach() and detach() entry points are used to create and destroy
78 "instances" of the driver, where each instance represents everything
79 needed to manage one actual PCMCIA card.
82 static dev_link_t *avma1cs_attach(void);
83 static void avma1cs_detach(dev_link_t *);
86 The dev_info variable is the "key" that is used to match up this
87 device driver with appropriate cards, through the card configuration
91 static dev_info_t dev_info = "avma1_cs";
94 A linked list of "instances" of the skeleton device. Each actual
95 PCMCIA card corresponds to one device instance, and is described
96 by one dev_link_t structure (defined in ds.h).
98 You may not want to use a linked list for this -- for example, the
99 memory card driver uses an array of dev_link_t pointers, where minor
100 device numbers are used to derive the corresponding array index.
103 static dev_link_t *dev_list = NULL;
106 A dev_link_t structure has fields for most things that are needed
107 to keep track of a socket, but there will usually be some device
108 specific information that also needs to be kept track of. The
109 'priv' pointer in a dev_link_t structure can be used to point to
110 a device-specific private data structure, like this.
112 A driver needs to provide a dev_node_t structure for each device
113 on a card. In some cases, there is only one device per card (for
114 example, ethernet cards, modems). In other cases, there may be
115 many actual or logical devices (SCSI adapters, memory cards with
116 multiple partitions). The dev_node_t structures need to be kept
117 in a linked list starting at the 'dev' field of a dev_link_t
118 structure. We allocate them in the card's private data structure,
119 because they generally can't be allocated dynamically.
122 typedef struct local_info_t {
126 /*======================================================================
128 avma1cs_attach() creates an "instance" of the driver, allocating
129 local data structures for one device. The device is registered
132 The dev_link structure is initialized, but we don't actually
133 configure the card at this point -- we wait until we receive a
134 card insertion event.
136 ======================================================================*/
138 static dev_link_t *avma1cs_attach(void)
140 client_reg_t client_reg;
145 DEBUG(0, "avma1cs_attach()\n");
147 /* Initialize the dev_link_t structure */
148 link = kmalloc(sizeof(struct dev_link_t), GFP_KERNEL);
151 memset(link, 0, sizeof(struct dev_link_t));
153 /* Allocate space for private device-specific data */
154 local = kmalloc(sizeof(local_info_t), GFP_KERNEL);
159 memset(local, 0, sizeof(local_info_t));
162 /* The io structure describes IO port mapping */
163 link->io.NumPorts1 = 16;
164 link->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
165 link->io.NumPorts2 = 16;
166 link->io.Attributes2 = IO_DATA_PATH_WIDTH_16;
167 link->io.IOAddrLines = 5;
169 /* Interrupt setup */
170 link->irq.Attributes = IRQ_TYPE_EXCLUSIVE;
171 link->irq.Attributes = IRQ_TYPE_DYNAMIC_SHARING|IRQ_FIRST_SHARED;
173 link->irq.IRQInfo1 = IRQ_LEVEL_ID;
175 /* General socket configuration */
176 link->conf.Attributes = CONF_ENABLE_IRQ;
178 link->conf.IntType = INT_MEMORY_AND_IO;
179 link->conf.ConfigIndex = 1;
180 link->conf.Present = PRESENT_OPTION;
182 /* Register with Card Services */
183 link->next = dev_list;
185 client_reg.dev_info = &dev_info;
186 client_reg.Version = 0x0210;
187 client_reg.event_callback_args.client_data = link;
188 ret = pcmcia_register_client(&link->handle, &client_reg);
190 cs_error(link->handle, RegisterClient, ret);
191 avma1cs_detach(link);
196 } /* avma1cs_attach */
198 /*======================================================================
200 This deletes a driver "instance". The device is de-registered
201 with Card Services. If it has been released, all local data
202 structures are freed. Otherwise, the structures will be freed
203 when the device is released.
205 ======================================================================*/
207 static void avma1cs_detach(dev_link_t *link)
211 DEBUG(0, "avma1cs_detach(0x%p)\n", link);
213 /* Locate device structure */
214 for (linkp = &dev_list; *linkp; linkp = &(*linkp)->next)
215 if (*linkp == link) break;
220 If the device is currently configured and active, we won't
221 actually delete it yet. Instead, it is marked so that when
222 the release() function is called, that will trigger a proper
225 if (link->state & DEV_CONFIG) {
227 printk(KERN_DEBUG "avma1_cs: detach postponed, '%s' "
228 "still locked\n", link->dev->dev_name);
230 link->state |= DEV_STALE_LINK;
234 /* Break the link with Card Services */
236 pcmcia_deregister_client(link->handle);
238 /* Unlink device structure, free pieces */
245 } /* avma1cs_detach */
247 /*======================================================================
249 avma1cs_config() is scheduled to run after a CARD_INSERTION event
250 is received, to configure the PCMCIA socket, and to make the
251 ethernet device available to the system.
253 ======================================================================*/
255 static int get_tuple(client_handle_t handle, tuple_t *tuple,
258 int i = pcmcia_get_tuple_data(handle, tuple);
259 if (i != CS_SUCCESS) return i;
260 return pcmcia_parse_tuple(handle, tuple, parse);
263 static int first_tuple(client_handle_t handle, tuple_t *tuple,
266 int i = pcmcia_get_first_tuple(handle, tuple);
267 if (i != CS_SUCCESS) return i;
268 return get_tuple(handle, tuple, parse);
271 static int next_tuple(client_handle_t handle, tuple_t *tuple,
274 int i = pcmcia_get_next_tuple(handle, tuple);
275 if (i != CS_SUCCESS) return i;
276 return get_tuple(handle, tuple, parse);
279 static void avma1cs_config(dev_link_t *link)
281 client_handle_t handle;
284 cistpl_cftable_entry_t *cf = &parse.cftable_entry;
292 handle = link->handle;
295 DEBUG(0, "avma1cs_config(0x%p)\n", link);
298 This reads the card's CONFIG tuple to find its configuration
302 tuple.DesiredTuple = CISTPL_CONFIG;
303 i = pcmcia_get_first_tuple(handle, &tuple);
304 if (i != CS_SUCCESS) break;
305 tuple.TupleData = buf;
306 tuple.TupleDataMax = 64;
307 tuple.TupleOffset = 0;
308 i = pcmcia_get_tuple_data(handle, &tuple);
309 if (i != CS_SUCCESS) break;
310 i = pcmcia_parse_tuple(handle, &tuple, &parse);
311 if (i != CS_SUCCESS) break;
312 link->conf.ConfigBase = parse.config.base;
314 if (i != CS_SUCCESS) {
315 cs_error(link->handle, ParseTuple, i);
316 link->state &= ~DEV_CONFIG_PENDING;
321 link->state |= DEV_CONFIG;
325 tuple.Attributes = 0;
326 tuple.TupleData = buf;
327 tuple.TupleDataMax = 254;
328 tuple.TupleOffset = 0;
329 tuple.DesiredTuple = CISTPL_VERS_1;
332 if( !first_tuple(handle, &tuple, &parse) && parse.version_1.ns > 1 ) {
333 strlcpy(devname,parse.version_1.str + parse.version_1.ofs[1],
339 tuple.TupleData = (cisdata_t *)buf;
340 tuple.TupleOffset = 0; tuple.TupleDataMax = 255;
341 tuple.Attributes = 0;
342 tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
343 i = first_tuple(handle, &tuple, &parse);
344 while (i == CS_SUCCESS) {
345 if (cf->io.nwin > 0) {
346 link->conf.ConfigIndex = cf->index;
347 link->io.BasePort1 = cf->io.win[0].base;
348 link->io.NumPorts1 = cf->io.win[0].len;
349 link->io.NumPorts2 = 0;
350 printk(KERN_INFO "avma1_cs: testing i/o %#x-%#x\n",
352 link->io.BasePort1+link->io.NumPorts1 - 1);
353 i = pcmcia_request_io(link->handle, &link->io);
354 if (i == CS_SUCCESS) goto found_port;
356 i = next_tuple(handle, &tuple, &parse);
360 if (i != CS_SUCCESS) {
361 cs_error(link->handle, RequestIO, i);
366 * allocate an interrupt line
368 i = pcmcia_request_irq(link->handle, &link->irq);
369 if (i != CS_SUCCESS) {
370 cs_error(link->handle, RequestIRQ, i);
371 pcmcia_release_io(link->handle, &link->io);
376 * configure the PCMCIA socket
378 i = pcmcia_request_configuration(link->handle, &link->conf);
379 if (i != CS_SUCCESS) {
380 cs_error(link->handle, RequestConfiguration, i);
381 pcmcia_release_io(link->handle, &link->io);
382 pcmcia_release_irq(link->handle, &link->irq);
388 /* At this point, the dev_node_t structure(s) should be
389 initialized and arranged in a linked list at link->dev. */
391 strcpy(dev->node.dev_name, "A1");
392 dev->node.major = 45;
394 link->dev = &dev->node;
396 link->state &= ~DEV_CONFIG_PENDING;
397 /* If any step failed, release any partially configured state */
399 avma1cs_release(link);
403 printk(KERN_NOTICE "avma1_cs: checking at i/o %#x, irq %d\n",
404 link->io.BasePort1, link->irq.AssignedIRQ);
406 icard.para[0] = link->irq.AssignedIRQ;
407 icard.para[1] = link->io.BasePort1;
408 icard.protocol = isdnprot;
409 icard.typ = ISDN_CTYPE_A1_PCMCIA;
411 i = hisax_init_pcmcia(link, &busy, &icard);
413 printk(KERN_ERR "avma1_cs: failed to initialize AVM A1 PCMCIA %d at i/o %#x\n", i, link->io.BasePort1);
414 avma1cs_release(link);
419 } /* avma1cs_config */
421 /*======================================================================
423 After a card is removed, avma1cs_release() will unregister the net
424 device, and release the PCMCIA configuration. If the device is
425 still open, this will be postponed until it is closed.
427 ======================================================================*/
429 static void avma1cs_release(dev_link_t *link)
431 local_info_t *local = link->priv;
433 DEBUG(0, "avma1cs_release(0x%p)\n", link);
435 /* no unregister function with hisax */
436 HiSax_closecard(local->node.minor);
438 /* Unlink the device chain */
441 /* Don't bother checking to see if these succeed or not */
442 pcmcia_release_configuration(link->handle);
443 pcmcia_release_io(link->handle, &link->io);
444 pcmcia_release_irq(link->handle, &link->irq);
445 link->state &= ~DEV_CONFIG;
447 if (link->state & DEV_STALE_LINK)
448 avma1cs_detach(link);
449 } /* avma1cs_release */
451 /*======================================================================
453 The card status event handler. Mostly, this schedules other
454 stuff to run after an event is received. A CARD_REMOVAL event
455 also sets some flags to discourage the net drivers from trying
456 to talk to the card any more.
458 When a CARD_REMOVAL event is received, we immediately set a flag
459 to block future accesses to this device. All the functions that
460 actually access the device should check this flag to make sure
461 the card is still present.
463 ======================================================================*/
465 static int avma1cs_event(event_t event, int priority,
466 event_callback_args_t *args)
468 dev_link_t *link = args->client_data;
470 DEBUG(1, "avma1cs_event(0x%06x)\n", event);
473 case CS_EVENT_CARD_REMOVAL:
474 if (link->state & DEV_CONFIG)
475 avma1cs_release(link);
477 case CS_EVENT_CARD_INSERTION:
478 link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
479 avma1cs_config(link);
481 case CS_EVENT_PM_SUSPEND:
482 link->state |= DEV_SUSPEND;
483 /* Fall through... */
484 case CS_EVENT_RESET_PHYSICAL:
485 if (link->state & DEV_CONFIG)
486 pcmcia_release_configuration(link->handle);
488 case CS_EVENT_PM_RESUME:
489 link->state &= ~DEV_SUSPEND;
490 /* Fall through... */
491 case CS_EVENT_CARD_RESET:
492 if (link->state & DEV_CONFIG)
493 pcmcia_request_configuration(link->handle, &link->conf);
497 } /* avma1cs_event */
499 static struct pcmcia_device_id avma1cs_ids[] = {
500 PCMCIA_DEVICE_PROD_ID12("AVM", "ISDN A", 0x95d42008, 0xadc9d4bb),
501 PCMCIA_DEVICE_PROD_ID12("ISDN", "CARD", 0x8d9761c8, 0x01c5aa7b),
504 MODULE_DEVICE_TABLE(pcmcia, avma1cs_ids);
506 static struct pcmcia_driver avma1cs_driver = {
507 .owner = THIS_MODULE,
511 .attach = avma1cs_attach,
512 .event = avma1cs_event,
513 .detach = avma1cs_detach,
514 .id_table = avma1cs_ids,
517 /*====================================================================*/
519 static int __init init_avma1_cs(void)
521 return(pcmcia_register_driver(&avma1cs_driver));
524 static void __exit exit_avma1_cs(void)
526 pcmcia_unregister_driver(&avma1cs_driver);
527 BUG_ON(dev_list != NULL);
530 module_init(init_avma1_cs);
531 module_exit(exit_avma1_cs);