]> err.no Git - linux-2.6/blob - drivers/isdn/hisax/avma1_cs.c
[PATCH] pcmcia: move event handler
[linux-2.6] / drivers / isdn / hisax / avma1_cs.c
1 /*
2  * PCMCIA client driver for AVM A1 / Fritz!PCMCIA
3  *
4  * Author       Carsten Paeth
5  * Copyright    1998-2001 by Carsten Paeth <calle@calle.in-berlin.de>
6  * 
7  * This software may be used and distributed according to the terms
8  * of the GNU General Public License, incorporated herein by reference.
9  *
10  */
11
12 #include <linux/module.h>
13
14
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>
21 #include <asm/io.h>
22 #include <asm/system.h>
23
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"
30
31 MODULE_DESCRIPTION("ISDN4Linux: PCMCIA client driver for AVM A1/Fritz!PCMCIA cards");
32 MODULE_AUTHOR("Carsten Paeth");
33 MODULE_LICENSE("GPL");
34
35 /*
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.
41 */
42 #ifdef PCMCIA_DEBUG
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)";
48 #else
49 #define DEBUG(n, args...)
50 #endif
51
52 /*====================================================================*/
53
54 /* Parameters that can be set with 'insmod' */
55
56 static int isdnprot = 2;
57
58 module_param(isdnprot, int, 0);
59
60 /*====================================================================*/
61
62 /*
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
68    handler.
69 */
70
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);
75
76 /*
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.
80 */
81
82 static dev_link_t *avma1cs_attach(void);
83 static void avma1cs_detach(dev_link_t *);
84
85 /*
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
88    database.
89 */
90
91 static dev_info_t dev_info = "avma1_cs";
92
93 /*
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).
97
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.
101 */
102
103 static dev_link_t *dev_list = NULL;
104
105 /*
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.
111
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.
120 */
121    
122 typedef struct local_info_t {
123     dev_node_t  node;
124 } local_info_t;
125
126 /*======================================================================
127
128     avma1cs_attach() creates an "instance" of the driver, allocating
129     local data structures for one device.  The device is registered
130     with Card Services.
131
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.
135     
136 ======================================================================*/
137
138 static dev_link_t *avma1cs_attach(void)
139 {
140     client_reg_t client_reg;
141     dev_link_t *link;
142     local_info_t *local;
143     int ret;
144     
145     DEBUG(0, "avma1cs_attach()\n");
146
147     /* Initialize the dev_link_t structure */
148     link = kmalloc(sizeof(struct dev_link_t), GFP_KERNEL);
149     if (!link)
150         return NULL;
151     memset(link, 0, sizeof(struct dev_link_t));
152
153     /* Allocate space for private device-specific data */
154     local = kmalloc(sizeof(local_info_t), GFP_KERNEL);
155     if (!local) {
156         kfree(link);
157         return NULL;
158     }
159     memset(local, 0, sizeof(local_info_t));
160     link->priv = local;
161
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;
168
169     /* Interrupt setup */
170     link->irq.Attributes = IRQ_TYPE_EXCLUSIVE;
171     link->irq.Attributes = IRQ_TYPE_DYNAMIC_SHARING|IRQ_FIRST_SHARED;
172
173     link->irq.IRQInfo1 = IRQ_LEVEL_ID;
174
175     /* General socket configuration */
176     link->conf.Attributes = CONF_ENABLE_IRQ;
177     link->conf.Vcc = 50;
178     link->conf.IntType = INT_MEMORY_AND_IO;
179     link->conf.ConfigIndex = 1;
180     link->conf.Present = PRESENT_OPTION;
181
182     /* Register with Card Services */
183     link->next = dev_list;
184     dev_list = link;
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);
189     if (ret != 0) {
190         cs_error(link->handle, RegisterClient, ret);
191         avma1cs_detach(link);
192         return NULL;
193     }
194
195     return link;
196 } /* avma1cs_attach */
197
198 /*======================================================================
199
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.
204
205 ======================================================================*/
206
207 static void avma1cs_detach(dev_link_t *link)
208 {
209     dev_link_t **linkp;
210
211     DEBUG(0, "avma1cs_detach(0x%p)\n", link);
212     
213     /* Locate device structure */
214     for (linkp = &dev_list; *linkp; linkp = &(*linkp)->next)
215         if (*linkp == link) break;
216     if (*linkp == NULL)
217         return;
218
219     /*
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
223        detach().
224     */
225     if (link->state & DEV_CONFIG) {
226 #ifdef PCMCIA_DEBUG
227         printk(KERN_DEBUG "avma1_cs: detach postponed, '%s' "
228                "still locked\n", link->dev->dev_name);
229 #endif
230         link->state |= DEV_STALE_LINK;
231         return;
232     }
233
234     /* Break the link with Card Services */
235     if (link->handle)
236         pcmcia_deregister_client(link->handle);
237     
238     /* Unlink device structure, free pieces */
239     *linkp = link->next;
240     if (link->priv) {
241         kfree(link->priv);
242     }
243     kfree(link);
244     
245 } /* avma1cs_detach */
246
247 /*======================================================================
248
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.
252     
253 ======================================================================*/
254
255 static int get_tuple(client_handle_t handle, tuple_t *tuple,
256                      cisparse_t *parse)
257 {
258     int i = pcmcia_get_tuple_data(handle, tuple);
259     if (i != CS_SUCCESS) return i;
260     return pcmcia_parse_tuple(handle, tuple, parse);
261 }
262
263 static int first_tuple(client_handle_t handle, tuple_t *tuple,
264                      cisparse_t *parse)
265 {
266     int i = pcmcia_get_first_tuple(handle, tuple);
267     if (i != CS_SUCCESS) return i;
268     return get_tuple(handle, tuple, parse);
269 }
270
271 static int next_tuple(client_handle_t handle, tuple_t *tuple,
272                      cisparse_t *parse)
273 {
274     int i = pcmcia_get_next_tuple(handle, tuple);
275     if (i != CS_SUCCESS) return i;
276     return get_tuple(handle, tuple, parse);
277 }
278
279 static void avma1cs_config(dev_link_t *link)
280 {
281     client_handle_t handle;
282     tuple_t tuple;
283     cisparse_t parse;
284     cistpl_cftable_entry_t *cf = &parse.cftable_entry;
285     local_info_t *dev;
286     int i;
287     u_char buf[64];
288     char devname[128];
289     IsdnCard_t  icard;
290     int busy = 0;
291     
292     handle = link->handle;
293     dev = link->priv;
294
295     DEBUG(0, "avma1cs_config(0x%p)\n", link);
296
297     /*
298        This reads the card's CONFIG tuple to find its configuration
299        registers.
300     */
301     do {
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;
313     } while (0);
314     if (i != CS_SUCCESS) {
315         cs_error(link->handle, ParseTuple, i);
316         link->state &= ~DEV_CONFIG_PENDING;
317         return;
318     }
319     
320     /* Configure card */
321     link->state |= DEV_CONFIG;
322
323     do {
324
325         tuple.Attributes = 0;
326         tuple.TupleData = buf;
327         tuple.TupleDataMax = 254;
328         tuple.TupleOffset = 0;
329         tuple.DesiredTuple = CISTPL_VERS_1;
330
331         devname[0] = 0;
332         if( !first_tuple(handle, &tuple, &parse) && parse.version_1.ns > 1 ) {
333             strlcpy(devname,parse.version_1.str + parse.version_1.ofs[1], 
334                         sizeof(devname));
335         }
336         /*
337          * find IO port
338          */
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",
351                         link->io.BasePort1,
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;
355             }
356             i = next_tuple(handle, &tuple, &parse);
357         }
358
359 found_port:
360         if (i != CS_SUCCESS) {
361             cs_error(link->handle, RequestIO, i);
362             break;
363         }
364         
365         /*
366          * allocate an interrupt line
367          */
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);
372             break;
373         }
374         
375         /*
376          * configure the PCMCIA socket
377          */
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);
383             break;
384         }
385
386     } while (0);
387
388     /* At this point, the dev_node_t structure(s) should be
389        initialized and arranged in a linked list at link->dev. */
390
391     strcpy(dev->node.dev_name, "A1");
392     dev->node.major = 45;
393     dev->node.minor = 0;
394     link->dev = &dev->node;
395     
396     link->state &= ~DEV_CONFIG_PENDING;
397     /* If any step failed, release any partially configured state */
398     if (i != 0) {
399         avma1cs_release(link);
400         return;
401     }
402
403     printk(KERN_NOTICE "avma1_cs: checking at i/o %#x, irq %d\n",
404                                 link->io.BasePort1, link->irq.AssignedIRQ);
405
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;
410     
411     i = hisax_init_pcmcia(link, &busy, &icard);
412     if (i < 0) {
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);
415         return;
416     }
417     dev->node.minor = i;
418
419 } /* avma1cs_config */
420
421 /*======================================================================
422
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.
426     
427 ======================================================================*/
428
429 static void avma1cs_release(dev_link_t *link)
430 {
431     local_info_t *local = link->priv;
432
433     DEBUG(0, "avma1cs_release(0x%p)\n", link);
434
435     /* no unregister function with hisax */
436     HiSax_closecard(local->node.minor);
437
438     /* Unlink the device chain */
439     link->dev = NULL;
440     
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;
446     
447     if (link->state & DEV_STALE_LINK)
448         avma1cs_detach(link);
449 } /* avma1cs_release */
450
451 /*======================================================================
452
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.
457
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.
462     
463 ======================================================================*/
464
465 static int avma1cs_event(event_t event, int priority,
466                           event_callback_args_t *args)
467 {
468     dev_link_t *link = args->client_data;
469
470     DEBUG(1, "avma1cs_event(0x%06x)\n", event);
471     
472     switch (event) {
473         case CS_EVENT_CARD_REMOVAL:
474             if (link->state & DEV_CONFIG)
475                 avma1cs_release(link);
476             break;
477         case CS_EVENT_CARD_INSERTION:
478             link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
479             avma1cs_config(link);
480             break;
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);
487             break;
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);
494             break;
495     }
496     return 0;
497 } /* avma1cs_event */
498
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),
502         PCMCIA_DEVICE_NULL
503 };
504 MODULE_DEVICE_TABLE(pcmcia, avma1cs_ids);
505
506 static struct pcmcia_driver avma1cs_driver = {
507         .owner          = THIS_MODULE,
508         .drv            = {
509                 .name   = "avma1_cs",
510         },
511         .attach         = avma1cs_attach,
512         .event          = avma1cs_event,
513         .detach         = avma1cs_detach,
514         .id_table       = avma1cs_ids,
515 };
516  
517 /*====================================================================*/
518
519 static int __init init_avma1_cs(void)
520 {
521         return(pcmcia_register_driver(&avma1cs_driver));
522 }
523
524 static void __exit exit_avma1_cs(void)
525 {
526         pcmcia_unregister_driver(&avma1cs_driver);
527         BUG_ON(dev_list != NULL);
528 }
529
530 module_init(init_avma1_cs);
531 module_exit(exit_avma1_cs);