VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[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     /* Break the link with Card Services */
275     if (link->handle) {
276         ret = pcmcia_deregister_client(link->handle);
277         if (ret != CS_SUCCESS)
278             cs_error(link->handle, DeregisterClient, ret);
279     }
280
281     /* Unlink device structure and free it */
282     *linkp = link->next;
283     kfree(info);
284
285 } /* elsa_cs_detach */
286
287 /*======================================================================
288
289     elsa_cs_config() is scheduled to run after a CARD_INSERTION event
290     is received, to configure the PCMCIA socket, and to make the
291     device available to the system.
292
293 ======================================================================*/
294 static int get_tuple(client_handle_t handle, tuple_t *tuple,
295                      cisparse_t *parse)
296 {
297     int i = pcmcia_get_tuple_data(handle, tuple);
298     if (i != CS_SUCCESS) return i;
299     return pcmcia_parse_tuple(handle, tuple, parse);
300 }
301
302 static int first_tuple(client_handle_t handle, tuple_t *tuple,
303                      cisparse_t *parse)
304 {
305     int i = pcmcia_get_first_tuple(handle, tuple);
306     if (i != CS_SUCCESS) return i;
307     return get_tuple(handle, tuple, parse);
308 }
309
310 static int next_tuple(client_handle_t handle, tuple_t *tuple,
311                      cisparse_t *parse)
312 {
313     int i = pcmcia_get_next_tuple(handle, tuple);
314     if (i != CS_SUCCESS) return i;
315     return get_tuple(handle, tuple, parse);
316 }
317
318 static void elsa_cs_config(dev_link_t *link)
319 {
320     client_handle_t handle;
321     tuple_t tuple;
322     cisparse_t parse;
323     local_info_t *dev;
324     int i, j, last_fn;
325     u_short buf[128];
326     cistpl_cftable_entry_t *cf = &parse.cftable_entry;
327     IsdnCard_t icard;
328
329     DEBUG(0, "elsa_config(0x%p)\n", link);
330     handle = link->handle;
331     dev = link->priv;
332
333     /*
334        This reads the card's CONFIG tuple to find its configuration
335        registers.
336     */
337     tuple.DesiredTuple = CISTPL_CONFIG;
338     tuple.TupleData = (cisdata_t *)buf;
339     tuple.TupleDataMax = 255;
340     tuple.TupleOffset = 0;
341     tuple.Attributes = 0;
342     i = first_tuple(handle, &tuple, &parse);
343     if (i != CS_SUCCESS) {
344         last_fn = ParseTuple;
345         goto cs_failed;
346     }
347     link->conf.ConfigBase = parse.config.base;
348     link->conf.Present = parse.config.rmask[0];
349
350     /* Configure card */
351     link->state |= DEV_CONFIG;
352
353     tuple.TupleData = (cisdata_t *)buf;
354     tuple.TupleOffset = 0; tuple.TupleDataMax = 255;
355     tuple.Attributes = 0;
356     tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
357     i = first_tuple(handle, &tuple, &parse);
358     while (i == CS_SUCCESS) {
359         if ( (cf->io.nwin > 0) && cf->io.win[0].base) {
360             printk(KERN_INFO "(elsa_cs: looks like the 96 model)\n");
361             link->conf.ConfigIndex = cf->index;
362             link->io.BasePort1 = cf->io.win[0].base;
363             i = pcmcia_request_io(link->handle, &link->io);
364             if (i == CS_SUCCESS) break;
365         } else {
366           printk(KERN_INFO "(elsa_cs: looks like the 97 model)\n");
367           link->conf.ConfigIndex = cf->index;
368           for (i = 0, j = 0x2f0; j > 0x100; j -= 0x10) {
369             link->io.BasePort1 = j;
370             i = pcmcia_request_io(link->handle, &link->io);
371             if (i == CS_SUCCESS) break;
372           }
373           break;
374         }
375         i = next_tuple(handle, &tuple, &parse);
376     }
377
378     if (i != CS_SUCCESS) {
379         last_fn = RequestIO;
380         goto cs_failed;
381     }
382
383     i = pcmcia_request_irq(link->handle, &link->irq);
384     if (i != CS_SUCCESS) {
385         link->irq.AssignedIRQ = 0;
386         last_fn = RequestIRQ;
387         goto cs_failed;
388     }
389
390     i = pcmcia_request_configuration(link->handle, &link->conf);
391     if (i != CS_SUCCESS) {
392       last_fn = RequestConfiguration;
393       goto cs_failed;
394     }
395
396     /* At this point, the dev_node_t structure(s) should be
397        initialized and arranged in a linked list at link->dev. *//*  */
398     sprintf(dev->node.dev_name, "elsa");
399     dev->node.major = dev->node.minor = 0x0;
400
401     link->dev = &dev->node;
402
403     /* Finally, report what we've done */
404     printk(KERN_INFO "%s: index 0x%02x: Vcc %d.%d",
405            dev->node.dev_name, link->conf.ConfigIndex,
406            link->conf.Vcc/10, link->conf.Vcc%10);
407     if (link->conf.Vpp1)
408         printk(", Vpp %d.%d", link->conf.Vpp1/10, link->conf.Vpp1%10);
409     if (link->conf.Attributes & CONF_ENABLE_IRQ)
410         printk(", irq %d", link->irq.AssignedIRQ);
411     if (link->io.NumPorts1)
412         printk(", io 0x%04x-0x%04x", link->io.BasePort1,
413                link->io.BasePort1+link->io.NumPorts1-1);
414     if (link->io.NumPorts2)
415         printk(" & 0x%04x-0x%04x", link->io.BasePort2,
416                link->io.BasePort2+link->io.NumPorts2-1);
417     printk("\n");
418
419     link->state &= ~DEV_CONFIG_PENDING;
420
421     icard.para[0] = link->irq.AssignedIRQ;
422     icard.para[1] = link->io.BasePort1;
423     icard.protocol = protocol;
424     icard.typ = ISDN_CTYPE_ELSA_PCMCIA;
425     
426     i = hisax_init_pcmcia(link, &(((local_info_t*)link->priv)->busy), &icard);
427     if (i < 0) {
428         printk(KERN_ERR "elsa_cs: failed to initialize Elsa PCMCIA %d at i/o %#x\n",
429                 i, link->io.BasePort1);
430         elsa_cs_release(link);
431     } else
432         ((local_info_t*)link->priv)->cardnr = i;
433
434     return;
435 cs_failed:
436     cs_error(link->handle, last_fn, i);
437     elsa_cs_release(link);
438 } /* elsa_cs_config */
439
440 /*======================================================================
441
442     After a card is removed, elsa_cs_release() will unregister the net
443     device, and release the PCMCIA configuration.  If the device is
444     still open, this will be postponed until it is closed.
445
446 ======================================================================*/
447
448 static void elsa_cs_release(dev_link_t *link)
449 {
450     local_info_t *local = link->priv;
451
452     DEBUG(0, "elsa_cs_release(0x%p)\n", link);
453
454     if (local) {
455         if (local->cardnr >= 0) {
456             /* no unregister function with hisax */
457             HiSax_closecard(local->cardnr);
458         }
459     }
460     /* Unlink the device chain */
461     link->dev = NULL;
462
463     /* Don't bother checking to see if these succeed or not */
464     if (link->win)
465         pcmcia_release_window(link->win);
466     pcmcia_release_configuration(link->handle);
467     pcmcia_release_io(link->handle, &link->io);
468     pcmcia_release_irq(link->handle, &link->irq);
469     link->state &= ~DEV_CONFIG;
470 } /* elsa_cs_release */
471
472 /*======================================================================
473
474     The card status event handler.  Mostly, this schedules other
475     stuff to run after an event is received.  A CARD_REMOVAL event
476     also sets some flags to discourage the net drivers from trying
477     to talk to the card any more.
478
479     When a CARD_REMOVAL event is received, we immediately set a flag
480     to block future accesses to this device.  All the functions that
481     actually access the device should check this flag to make sure
482     the card is still present.
483
484 ======================================================================*/
485
486 static int elsa_cs_event(event_t event, int priority,
487                           event_callback_args_t *args)
488 {
489     dev_link_t *link = args->client_data;
490     local_info_t *dev = link->priv;
491
492     DEBUG(1, "elsa_cs_event(%d)\n", event);
493
494     switch (event) {
495     case CS_EVENT_CARD_REMOVAL:
496         link->state &= ~DEV_PRESENT;
497         if (link->state & DEV_CONFIG) {
498             ((local_info_t*)link->priv)->busy = 1;
499             elsa_cs_release(link);
500         }
501         break;
502     case CS_EVENT_CARD_INSERTION:
503         link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
504         elsa_cs_config(link);
505         break;
506     case CS_EVENT_PM_SUSPEND:
507         link->state |= DEV_SUSPEND;
508         /* Fall through... */
509     case CS_EVENT_RESET_PHYSICAL:
510         /* Mark the device as stopped, to block IO until later */
511         dev->busy = 1;
512         if (link->state & DEV_CONFIG)
513             pcmcia_release_configuration(link->handle);
514         break;
515     case CS_EVENT_PM_RESUME:
516         link->state &= ~DEV_SUSPEND;
517         /* Fall through... */
518     case CS_EVENT_CARD_RESET:
519         if (link->state & DEV_CONFIG)
520             pcmcia_request_configuration(link->handle, &link->conf);
521         dev->busy = 0;
522         break;
523     }
524     return 0;
525 } /* elsa_cs_event */
526
527 static struct pcmcia_driver elsa_cs_driver = {
528         .owner          = THIS_MODULE,
529         .drv            = {
530                 .name   = "elsa_cs",
531         },
532         .attach         = elsa_cs_attach,
533         .detach         = elsa_cs_detach,
534 };
535
536 static int __init init_elsa_cs(void)
537 {
538         return pcmcia_register_driver(&elsa_cs_driver);
539 }
540
541 static void __exit exit_elsa_cs(void)
542 {
543         pcmcia_unregister_driver(&elsa_cs_driver);
544
545         /* XXX: this really needs to move into generic code.. */
546         while (dev_list != NULL)
547                 elsa_cs_detach(dev_list);
548 }
549
550 module_init(init_elsa_cs);
551 module_exit(exit_elsa_cs);