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);
274 /* Break the link with Card Services */
276 ret = pcmcia_deregister_client(link->handle);
277 if (ret != CS_SUCCESS)
278 cs_error(link->handle, DeregisterClient, ret);
281 /* Unlink device structure and free it */
285 } /* elsa_cs_detach */
287 /*======================================================================
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.
293 ======================================================================*/
294 static int get_tuple(client_handle_t handle, tuple_t *tuple,
297 int i = pcmcia_get_tuple_data(handle, tuple);
298 if (i != CS_SUCCESS) return i;
299 return pcmcia_parse_tuple(handle, tuple, parse);
302 static int first_tuple(client_handle_t handle, tuple_t *tuple,
305 int i = pcmcia_get_first_tuple(handle, tuple);
306 if (i != CS_SUCCESS) return i;
307 return get_tuple(handle, tuple, parse);
310 static int next_tuple(client_handle_t handle, tuple_t *tuple,
313 int i = pcmcia_get_next_tuple(handle, tuple);
314 if (i != CS_SUCCESS) return i;
315 return get_tuple(handle, tuple, parse);
318 static void elsa_cs_config(dev_link_t *link)
320 client_handle_t handle;
326 cistpl_cftable_entry_t *cf = &parse.cftable_entry;
329 DEBUG(0, "elsa_config(0x%p)\n", link);
330 handle = link->handle;
334 This reads the card's CONFIG tuple to find its configuration
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;
347 link->conf.ConfigBase = parse.config.base;
348 link->conf.Present = parse.config.rmask[0];
351 link->state |= DEV_CONFIG;
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;
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;
375 i = next_tuple(handle, &tuple, &parse);
378 if (i != CS_SUCCESS) {
383 i = pcmcia_request_irq(link->handle, &link->irq);
384 if (i != CS_SUCCESS) {
385 link->irq.AssignedIRQ = 0;
386 last_fn = RequestIRQ;
390 i = pcmcia_request_configuration(link->handle, &link->conf);
391 if (i != CS_SUCCESS) {
392 last_fn = RequestConfiguration;
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;
401 link->dev = &dev->node;
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);
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);
419 link->state &= ~DEV_CONFIG_PENDING;
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;
426 i = hisax_init_pcmcia(link, &(((local_info_t*)link->priv)->busy), &icard);
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);
432 ((local_info_t*)link->priv)->cardnr = i;
436 cs_error(link->handle, last_fn, i);
437 elsa_cs_release(link);
438 } /* elsa_cs_config */
440 /*======================================================================
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.
446 ======================================================================*/
448 static void elsa_cs_release(dev_link_t *link)
450 local_info_t *local = link->priv;
452 DEBUG(0, "elsa_cs_release(0x%p)\n", link);
455 if (local->cardnr >= 0) {
456 /* no unregister function with hisax */
457 HiSax_closecard(local->cardnr);
460 /* Unlink the device chain */
463 /* Don't bother checking to see if these succeed or not */
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 */
472 /*======================================================================
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.
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.
484 ======================================================================*/
486 static int elsa_cs_event(event_t event, int priority,
487 event_callback_args_t *args)
489 dev_link_t *link = args->client_data;
490 local_info_t *dev = link->priv;
492 DEBUG(1, "elsa_cs_event(%d)\n", 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);
502 case CS_EVENT_CARD_INSERTION:
503 link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
504 elsa_cs_config(link);
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 */
512 if (link->state & DEV_CONFIG)
513 pcmcia_release_configuration(link->handle);
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);
525 } /* elsa_cs_event */
527 static struct pcmcia_driver elsa_cs_driver = {
528 .owner = THIS_MODULE,
532 .attach = elsa_cs_attach,
533 .detach = elsa_cs_detach,
536 static int __init init_elsa_cs(void)
538 return pcmcia_register_driver(&elsa_cs_driver);
541 static void __exit exit_elsa_cs(void)
543 pcmcia_unregister_driver(&elsa_cs_driver);
545 /* XXX: this really needs to move into generic code.. */
546 while (dev_list != NULL)
547 elsa_cs_detach(dev_list);
550 module_init(init_elsa_cs);
551 module_exit(exit_elsa_cs);