patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / drivers / isdn / hardware / avm / avm_cs.c
1 /* $Id: avm_cs.c,v 1.4.6.3 2001/09/23 22:24:33 kai Exp $
2  *
3  * A PCMCIA client driver for AVM B1/M1/M2
4  *
5  * Copyright 1999 by Carsten Paeth <calle@calle.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 #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>
22 #include <asm/io.h>
23 #include <asm/system.h>
24
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>
32
33 #include <linux/skbuff.h>
34 #include <linux/capi.h>
35 #include <linux/b1lli.h>
36 #include <linux/b1pcmcia.h>
37
38 /*====================================================================*/
39
40 MODULE_DESCRIPTION("CAPI4Linux: PCMCIA client driver for AVM B1/M1/M2");
41 MODULE_AUTHOR("Carsten Paeth");
42 MODULE_LICENSE("GPL");
43
44 /*====================================================================*/
45
46 /* Parameters that can be set with 'insmod' */
47
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 };
51
52 MODULE_PARM(irq_list, "1-10i");
53
54 /*====================================================================*/
55
56 /*
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
62    handler.
63 */
64
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);
69
70 /*
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.
74 */
75
76 static dev_link_t *avmcs_attach(void);
77 static void avmcs_detach(dev_link_t *);
78
79 /*
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
82    database.
83 */
84
85 static dev_info_t dev_info = "avm_cs";
86
87 /*
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).
91
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.
95 */
96
97 static dev_link_t *dev_list = NULL;
98
99 /*
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.
105
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.
114 */
115    
116 typedef struct local_info_t {
117     dev_node_t  node;
118 } local_info_t;
119
120 /*======================================================================
121
122     avmcs_attach() creates an "instance" of the driver, allocating
123     local data structures for one device.  The device is registered
124     with Card Services.
125
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.
129     
130 ======================================================================*/
131
132 static dev_link_t *avmcs_attach(void)
133 {
134     client_reg_t client_reg;
135     dev_link_t *link;
136     local_info_t *local;
137     int ret, i;
138     
139     /* Initialize the dev_link_t structure */
140     link = kmalloc(sizeof(struct dev_link_t), GFP_KERNEL);
141     if (!link)
142         goto err;
143     memset(link, 0, sizeof(struct dev_link_t));
144
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;
149
150     /* Interrupt setup */
151     link->irq.Attributes = IRQ_TYPE_EXCLUSIVE;
152     link->irq.Attributes = IRQ_TYPE_DYNAMIC_SHARING|IRQ_FIRST_SHARED;
153
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];
158     } else {
159             for (i = 0; i < 10 && default_irq_list[i] > 0; i++)
160                link->irq.IRQInfo2 |= 1 << default_irq_list[i];
161     }
162     
163     /* General socket configuration */
164     link->conf.Attributes = CONF_ENABLE_IRQ;
165     link->conf.Vcc = 50;
166     link->conf.IntType = INT_MEMORY_AND_IO;
167     link->conf.ConfigIndex = 1;
168     link->conf.Present = PRESENT_OPTION;
169
170     /* Allocate space for private device-specific data */
171     local = kmalloc(sizeof(local_info_t), GFP_KERNEL);
172     if (!local)
173         goto err_kfree;
174     memset(local, 0, sizeof(local_info_t));
175     link->priv = local;
176     
177     /* Register with Card Services */
178     link->next = dev_list;
179     dev_list = link;
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);
190     if (ret != 0) {
191         cs_error(link->handle, RegisterClient, ret);
192         avmcs_detach(link);
193         goto err;
194     }
195     return link;
196
197  err_kfree:
198     kfree(link);
199  err:
200     return NULL;
201 } /* avmcs_attach */
202
203 /*======================================================================
204
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.
209
210 ======================================================================*/
211
212 static void avmcs_detach(dev_link_t *link)
213 {
214     dev_link_t **linkp;
215
216     /* Locate device structure */
217     for (linkp = &dev_list; *linkp; linkp = &(*linkp)->next)
218         if (*linkp == link) break;
219     if (*linkp == NULL)
220         return;
221
222     /*
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
226        detach().
227     */
228     if (link->state & DEV_CONFIG) {
229         link->state |= DEV_STALE_LINK;
230         return;
231     }
232
233     /* Break the link with Card Services */
234     if (link->handle)
235         pcmcia_deregister_client(link->handle);
236     
237     /* Unlink device structure, free pieces */
238     *linkp = link->next;
239     if (link->priv) {
240         kfree(link->priv);
241     }
242     kfree(link);
243     
244 } /* avmcs_detach */
245
246 /*======================================================================
247
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.
251     
252 ======================================================================*/
253
254 static int get_tuple(client_handle_t handle, tuple_t *tuple,
255                      cisparse_t *parse)
256 {
257     int i = pcmcia_get_tuple_data(handle, tuple);
258     if (i != CS_SUCCESS) return i;
259     return pcmcia_parse_tuple(handle, tuple, parse);
260 }
261
262 static int first_tuple(client_handle_t handle, tuple_t *tuple,
263                      cisparse_t *parse)
264 {
265     int i = pcmcia_get_first_tuple(handle, tuple);
266     if (i != CS_SUCCESS) return i;
267     return get_tuple(handle, tuple, parse);
268 }
269
270 static int next_tuple(client_handle_t handle, tuple_t *tuple,
271                      cisparse_t *parse)
272 {
273     int i = pcmcia_get_next_tuple(handle, tuple);
274     if (i != CS_SUCCESS) return i;
275     return get_tuple(handle, tuple, parse);
276 }
277
278 static void avmcs_config(dev_link_t *link)
279 {
280     client_handle_t handle;
281     tuple_t tuple;
282     cisparse_t parse;
283     cistpl_cftable_entry_t *cf = &parse.cftable_entry;
284     local_info_t *dev;
285     int i;
286     u_char buf[64];
287     char devname[128];
288     int cardtype;
289     int (*addcard)(unsigned int port, unsigned irq);
290     
291     handle = link->handle;
292     dev = link->priv;
293
294     /*
295        This reads the card's CONFIG tuple to find its configuration
296        registers.
297     */
298     do {
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;
310     } while (0);
311     if (i != CS_SUCCESS) {
312         cs_error(link->handle, ParseTuple, i);
313         link->state &= ~DEV_CONFIG_PENDING;
314         return;
315     }
316     
317     /* Configure card */
318     link->state |= DEV_CONFIG;
319
320     do {
321
322         tuple.Attributes = 0;
323         tuple.TupleData = buf;
324         tuple.TupleDataMax = 254;
325         tuple.TupleOffset = 0;
326         tuple.DesiredTuple = CISTPL_VERS_1;
327
328         devname[0] = 0;
329         if( !first_tuple(handle, &tuple, &parse) && parse.version_1.ns > 1 ) {
330             strlcpy(devname,parse.version_1.str + parse.version_1.ofs[1], 
331                         sizeof(devname));
332         }
333         /*
334          * find IO port
335          */
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",
348                         link->io.BasePort1,
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;
352             }
353             i = next_tuple(handle, &tuple, &parse);
354         }
355
356 found_port:
357         if (i != CS_SUCCESS) {
358             cs_error(link->handle, RequestIO, i);
359             break;
360         }
361         
362         /*
363          * allocate an interrupt line
364          */
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);
369             break;
370         }
371         
372         /*
373          * configure the PCMCIA socket
374           */
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);
380             break;
381         }
382
383     } while (0);
384
385     /* At this point, the dev_node_t structure(s) should be
386        initialized and arranged in a linked list at link->dev. */
387
388     if (devname[0]) {
389         char *s = strrchr(devname, ' ');
390         if (!s)
391            s = devname;
392         else s++;
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;
398         } else {
399            cardtype = AVM_CARDTYPE_B1;
400         }
401     } else {
402         strcpy(dev->node.dev_name, "b1");
403         cardtype = AVM_CARDTYPE_B1;
404     }
405
406     dev->node.major = 64;
407     dev->node.minor = 0;
408     link->dev = &dev->node;
409     
410     link->state &= ~DEV_CONFIG_PENDING;
411     /* If any step failed, release any partially configured state */
412     if (i != 0) {
413         avmcs_release(link);
414         return;
415     }
416
417
418     switch (cardtype) {
419         case AVM_CARDTYPE_M1: addcard = b1pcmcia_addcard_m1; break;
420         case AVM_CARDTYPE_M2: addcard = b1pcmcia_addcard_m2; break;
421         default:
422         case AVM_CARDTYPE_B1: addcard = b1pcmcia_addcard_b1; break;
423     }
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);
427         avmcs_release(link);
428         return;
429     }
430     dev->node.minor = i;
431
432 } /* avmcs_config */
433
434 /*======================================================================
435
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.
439     
440 ======================================================================*/
441
442 static void avmcs_release(dev_link_t *link)
443 {
444     b1pcmcia_delcard(link->io.BasePort1, link->irq.AssignedIRQ);
445
446     /* Unlink the device chain */
447     link->dev = NULL;
448     
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;
454     
455     if (link->state & DEV_STALE_LINK)
456         avmcs_detach(link);
457     
458 } /* avmcs_release */
459
460 /*======================================================================
461
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.
466
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.
471     
472 ======================================================================*/
473
474 static int avmcs_event(event_t event, int priority,
475                           event_callback_args_t *args)
476 {
477     dev_link_t *link = args->client_data;
478
479     switch (event) {
480     case CS_EVENT_CARD_REMOVAL:
481         link->state &= ~DEV_PRESENT;
482         if (link->state & DEV_CONFIG)
483                 avmcs_release(link);
484         break;
485     case CS_EVENT_CARD_INSERTION:
486         link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
487         avmcs_config(link);
488         break;
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);
495         break;
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);
502         break;
503     }
504     return 0;
505 } /* avmcs_event */
506
507 static struct pcmcia_driver avmcs_driver = {
508         .owner  = THIS_MODULE,
509         .drv    = {
510                 .name   = "avm_cs",
511         },
512         .attach = avmcs_attach,
513         .detach = avmcs_detach,
514 };
515
516 static int __init avmcs_init(void)
517 {
518         return pcmcia_register_driver(&avmcs_driver);
519 }
520
521 static void __exit avmcs_exit(void)
522 {
523         pcmcia_unregister_driver(&avmcs_driver);
524
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);
530         }
531 }
532
533 module_init(avmcs_init);
534 module_exit(avmcs_exit);