File: /usr/src/linux/drivers/isdn/hisax/elsa_cs.c

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