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