1 /*======================================================================
3 An elsa_cs PCMCIA client driver
5 This driver is for the Elsa PCM ISDN Cards, i.e. the MicroLink
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/
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.
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.
22 Modifications from dummy_cs.c are Copyright (C) 1999-2001 Klaus
23 Lichtenwalder <Lichtenwalder@ACM.org>. All Rights Reserved.
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.
36 ======================================================================*/
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>
48 #include <asm/system.h>
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"
58 MODULE_DESCRIPTION("ISDN4Linux: PCMCIA client driver for Elsa PCM cards");
59 MODULE_AUTHOR("Klaus Lichtenwalder");
60 MODULE_LICENSE("Dual MPL/GPL");
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.
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)";
77 #define DEBUG(n, args...)
80 /*====================================================================*/
82 /* Parameters that can be set with 'insmod' */
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;
88 /* Newer, simpler way of listing specific interrupts */
89 static int irq_list[4] = { -1 };
91 MODULE_PARM(irq_mask, "i");
92 MODULE_PARM(irq_list, "1-4i");
94 static int protocol = 2; /* EURO-ISDN Default */
95 MODULE_PARM(protocol, "i");
97 /*====================================================================*/
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
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);
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.
119 static dev_link_t *elsa_cs_attach(void);
120 static void elsa_cs_detach(dev_link_t *);
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
128 static dev_info_t dev_info = "elsa_cs";
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).
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.
140 static dev_link_t *dev_list = NULL;
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.
149 To simplify the data structure handling, we actually include the
150 dev_link_t structure in the device's private data structure.
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.
166 typedef struct local_info_t {
173 /*======================================================================
175 elsa_cs_attach() creates an "instance" of the driver, allocatingx
176 local data structures for one device. The device is registered
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.
183 ======================================================================*/
185 static dev_link_t *elsa_cs_attach(void)
187 client_reg_t client_reg;
192 DEBUG(0, "elsa_cs_attach()\n");
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));
199 link = &local->link; link->priv = local;
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;
207 for (i = 0; i < 4; i++)
208 link->irq.IRQInfo2 |= 1 << irq_list[i];
209 link->irq.Handler = NULL;
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.
218 link->io.NumPorts1 = 8;
219 link->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO;
220 link->io.IOAddrLines = 3;
222 link->conf.Attributes = CONF_ENABLE_IRQ;
224 link->conf.IntType = INT_MEMORY_AND_IO;
226 /* Register with Card Services */
227 link->next = dev_list;
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);
246 } /* elsa_cs_attach */
248 /*======================================================================
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.
255 ======================================================================*/
257 static void elsa_cs_detach(dev_link_t *link)
260 local_info_t *info = link->priv;
263 DEBUG(0, "elsa_cs_detach(0x%p)\n", link);
265 /* Locate device structure */
266 for (linkp = &dev_list; *linkp; linkp = &(*linkp)->next)
267 if (*linkp == link) break;
271 if (link->state & DEV_CONFIG)
272 elsa_cs_release(link);
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
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;
287 /* Break the link with Card Services */
289 ret = pcmcia_deregister_client(link->handle);
290 if (ret != CS_SUCCESS)
291 cs_error(link->handle, DeregisterClient, ret);
294 /* Unlink device structure and free it */
298 } /* elsa_cs_detach */
300 /*======================================================================
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.
306 ======================================================================*/
307 static int get_tuple(client_handle_t handle, tuple_t *tuple,
310 int i = pcmcia_get_tuple_data(handle, tuple);
311 if (i != CS_SUCCESS) return i;
312 return pcmcia_parse_tuple(handle, tuple, parse);
315 static int first_tuple(client_handle_t handle, tuple_t *tuple,
318 int i = pcmcia_get_first_tuple(handle, tuple);
319 if (i != CS_SUCCESS) return i;
320 return get_tuple(handle, tuple, parse);
323 static int next_tuple(client_handle_t handle, tuple_t *tuple,
326 int i = pcmcia_get_next_tuple(handle, tuple);
327 if (i != CS_SUCCESS) return i;
328 return get_tuple(handle, tuple, parse);
331 static void elsa_cs_config(dev_link_t *link)
333 client_handle_t handle;
339 cistpl_cftable_entry_t *cf = &parse.cftable_entry;
342 DEBUG(0, "elsa_config(0x%p)\n", link);
343 handle = link->handle;
347 This reads the card's CONFIG tuple to find its configuration
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;
360 link->conf.ConfigBase = parse.config.base;
361 link->conf.Present = parse.config.rmask[0];
364 link->state |= DEV_CONFIG;
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;
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;
388 i = next_tuple(handle, &tuple, &parse);
391 if (i != CS_SUCCESS) {
396 i = pcmcia_request_irq(link->handle, &link->irq);
397 if (i != CS_SUCCESS) {
398 link->irq.AssignedIRQ = 0;
399 last_fn = RequestIRQ;
403 i = pcmcia_request_configuration(link->handle, &link->conf);
404 if (i != CS_SUCCESS) {
405 last_fn = RequestConfiguration;
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;
414 link->dev = &dev->node;
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);
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);
432 link->state &= ~DEV_CONFIG_PENDING;
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;
439 i = hisax_init_pcmcia(link, &(((local_info_t*)link->priv)->busy), &icard);
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);
445 ((local_info_t*)link->priv)->cardnr = i;
449 cs_error(link->handle, last_fn, i);
450 elsa_cs_release(link);
451 } /* elsa_cs_config */
453 /*======================================================================
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.
459 ======================================================================*/
461 static void elsa_cs_release(dev_link_t *link)
463 local_info_t *local = link->priv;
465 DEBUG(0, "elsa_cs_release(0x%p)\n", link);
468 if (local->cardnr >= 0) {
469 /* no unregister function with hisax */
470 HiSax_closecard(local->cardnr);
473 /* Unlink the device chain */
476 /* Don't bother checking to see if these succeed or not */
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;
484 if (link->state & DEV_STALE_LINK)
485 elsa_cs_detach(link);
487 } /* elsa_cs_release */
489 /*======================================================================
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.
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.
501 ======================================================================*/
503 static int elsa_cs_event(event_t event, int priority,
504 event_callback_args_t *args)
506 dev_link_t *link = args->client_data;
507 local_info_t *dev = link->priv;
509 DEBUG(1, "elsa_cs_event(%d)\n", 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);
519 case CS_EVENT_CARD_INSERTION:
520 link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
521 elsa_cs_config(link);
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 */
529 if (link->state & DEV_CONFIG)
530 pcmcia_release_configuration(link->handle);
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);
542 } /* elsa_cs_event */
544 static struct pcmcia_driver elsa_cs_driver = {
545 .owner = THIS_MODULE,
549 .attach = elsa_cs_attach,
550 .detach = elsa_cs_detach,
553 static int __init init_elsa_cs(void)
555 return pcmcia_register_driver(&elsa_cs_driver);
558 static void __exit exit_elsa_cs(void)
560 pcmcia_unregister_driver(&elsa_cs_driver);
562 /* XXX: this really needs to move into generic code.. */
563 while (dev_list != NULL)
564 elsa_cs_detach(dev_list);
567 module_init(init_elsa_cs);
568 module_exit(exit_elsa_cs);