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