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>
57 MODULE_DESCRIPTION("ISDN4Linux: PCMCIA client driver for Elsa PCM cards");
58 MODULE_AUTHOR("Klaus Lichtenwalder");
59 MODULE_LICENSE("Dual MPL/GPL");
62 All the PCMCIA modules use PCMCIA_DEBUG to control debugging. If
63 you do not define PCMCIA_DEBUG at all, all the debug code will be
64 left out. If you compile with PCMCIA_DEBUG=0, the debug code will
65 be present but disabled -- but it can then be enabled for specific
66 modules at load time with a 'pc_debug=#' option to insmod.
70 static int pc_debug
= PCMCIA_DEBUG
;
71 MODULE_PARM(pc_debug
, "i");
72 #define DEBUG(n, args...) if (pc_debug>(n)) printk(KERN_DEBUG args);
73 static char *version
=
74 "elsa_cs.c $Revision: 1.1.2.2 $ $Date: 2001/09/23 22:24:47 $ (K.Lichtenwalder)";
76 #define DEBUG(n, args...)
79 /*====================================================================*/
81 /* Parameters that can be set with 'insmod' */
83 /* Bit map of interrupts to choose from, the old way */
84 /* This means pick from 15, 14, 12, 11, 10, 9, 7, 5, 4, 3 */
85 static u_long irq_mask
= 0xdeb8;
87 /* Newer, simpler way of listing specific interrupts */
88 static int irq_list
[4] = { -1 };
90 MODULE_PARM(irq_mask
, "i");
91 MODULE_PARM(irq_list
, "1-4i");
93 static int protocol
= 2; /* EURO-ISDN Default */
94 MODULE_PARM(protocol
, "i");
96 extern int elsa_init_pcmcia(int, int, int*, int);
98 /*====================================================================*/
101 The event() function is this driver's Card Services event handler.
102 It will be called by Card Services when an appropriate card status
103 event is received. The config() and release() entry points are
104 used to configure or release a socket, in response to card insertion
105 and ejection events. They are invoked from the elsa_cs event
109 static void elsa_cs_config(dev_link_t
*link
);
110 static void elsa_cs_release(u_long arg
);
111 static int elsa_cs_event(event_t event
, int priority
,
112 event_callback_args_t
*args
);
115 The attach() and detach() entry points are used to create and destroy
116 "instances" of the driver, where each instance represents everything
117 needed to manage one actual PCMCIA card.
120 static dev_link_t
*elsa_cs_attach(void);
121 static void elsa_cs_detach(dev_link_t
*);
124 The dev_info variable is the "key" that is used to match up this
125 device driver with appropriate cards, through the card configuration
129 static dev_info_t dev_info
= "elsa_cs";
132 A linked list of "instances" of the elsa_cs device. Each actual
133 PCMCIA card corresponds to one device instance, and is described
134 by one dev_link_t structure (defined in ds.h).
136 You may not want to use a linked list for this -- for example, the
137 memory card driver uses an array of dev_link_t pointers, where minor
138 device numbers are used to derive the corresponding array index.
141 static dev_link_t
*dev_list
= NULL
;
144 A dev_link_t structure has fields for most things that are needed
145 to keep track of a socket, but there will usually be some device
146 specific information that also needs to be kept track of. The
147 'priv' pointer in a dev_link_t structure can be used to point to
148 a device-specific private data structure, like this.
150 To simplify the data structure handling, we actually include the
151 dev_link_t structure in the device's private data structure.
153 A driver needs to provide a dev_node_t structure for each device
154 on a card. In some cases, there is only one device per card (for
155 example, ethernet cards, modems). In other cases, there may be
156 many actual or logical devices (SCSI adapters, memory cards with
157 multiple partitions). The dev_node_t structures need to be kept
158 in a linked list starting at the 'dev' field of a dev_link_t
159 structure. We allocate them in the card's private data structure,
160 because they generally shouldn't be allocated dynamically.
161 In this case, we also provide a flag to indicate if a device is
162 "stopped" due to a power management event, or card ejection. The
163 device IO routines can use a flag like this to throttle IO to a
164 card that is not ready to accept it.
167 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
;
191 void elsa_interrupt(int, void *, struct pt_regs
*);
193 DEBUG(0, "elsa_cs_attach()\n");
195 /* Allocate space for private device-specific data */
196 local
= kmalloc(sizeof(local_info_t
), GFP_KERNEL
);
197 if (!local
) return NULL
;
198 memset(local
, 0, sizeof(local_info_t
));
199 link
= &local
->link
; link
->priv
= local
;
201 /* Initialize the dev_link_t structure */
202 link
->release
.function
= &elsa_cs_release
;
203 link
->release
.data
= (u_long
)link
;
205 /* Interrupt setup */
206 link
->irq
.Attributes
= IRQ_TYPE_DYNAMIC_SHARING
|IRQ_FIRST_SHARED
;
207 link
->irq
.IRQInfo1
= IRQ_INFO2_VALID
|IRQ_LEVEL_ID
|IRQ_SHARE_ID
;
208 if (irq_list
[0] == -1)
209 link
->irq
.IRQInfo2
= irq_mask
;
211 for (i
= 0; i
< 4; i
++)
212 link
->irq
.IRQInfo2
|= 1 << irq_list
[i
];
213 link
->irq
.Handler
= NULL
;
216 General socket configuration defaults can go here. In this
217 client, we assume very little, and rely on the CIS for almost
218 everything. In most clients, many details (i.e., number, sizes,
219 and attributes of IO windows) are fixed by the nature of the
220 device, and can be hard-wired here.
222 link
->io
.NumPorts1
= 8;
223 link
->io
.Attributes1
= IO_DATA_PATH_WIDTH_AUTO
;
224 link
->io
.IOAddrLines
= 3;
226 link
->conf
.Attributes
= CONF_ENABLE_IRQ
;
228 link
->conf
.IntType
= INT_MEMORY_AND_IO
;
230 /* Register with Card Services */
231 link
->next
= dev_list
;
233 client_reg
.dev_info
= &dev_info
;
234 client_reg
.Attributes
= INFO_IO_CLIENT
| INFO_CARD_SHARE
;
235 client_reg
.EventMask
=
236 CS_EVENT_CARD_INSERTION
| CS_EVENT_CARD_REMOVAL
|
237 CS_EVENT_RESET_PHYSICAL
| CS_EVENT_CARD_RESET
|
238 CS_EVENT_PM_SUSPEND
| CS_EVENT_PM_RESUME
;
239 client_reg
.event_handler
= &elsa_cs_event
;
240 client_reg
.Version
= 0x0210;
241 client_reg
.event_callback_args
.client_data
= link
;
242 ret
= CardServices(RegisterClient
, &link
->handle
, &client_reg
);
243 if (ret
!= CS_SUCCESS
) {
244 cs_error(link
->handle
, RegisterClient
, ret
);
245 elsa_cs_detach(link
);
250 } /* elsa_cs_attach */
252 /*======================================================================
254 This deletes a driver "instance". The device is de-registered
255 with Card Services. If it has been released, all local data
256 structures are freed. Otherwise, the structures will be freed
257 when the device is released.
259 ======================================================================*/
261 static void elsa_cs_detach(dev_link_t
*link
)
264 local_info_t
*info
= link
->priv
;
267 DEBUG(0, "elsa_cs_detach(0x%p)\n", link
);
269 /* Locate device structure */
270 for (linkp
= &dev_list
; *linkp
; linkp
= &(*linkp
)->next
)
271 if (*linkp
== link
) break;
275 del_timer(&link
->release
);
276 if (link
->state
& DEV_CONFIG
)
277 elsa_cs_release((u_long
)link
);
280 If the device is currently configured and active, we won't
281 actually delete it yet. Instead, it is marked so that when
282 the release() function is called, that will trigger a proper
285 if (link
->state
& DEV_CONFIG
) {
286 DEBUG(0, "elsa_cs: detach postponed, '%s' "
287 "still locked\n", link
->dev
->dev_name
);
288 link
->state
|= DEV_STALE_LINK
;
292 /* Break the link with Card Services */
294 ret
= CardServices(DeregisterClient
, link
->handle
);
295 if (ret
!= CS_SUCCESS
)
296 cs_error(link
->handle
, DeregisterClient
, ret
);
299 /* Unlink device structure and free it */
303 } /* elsa_cs_detach */
305 /*======================================================================
307 elsa_cs_config() is scheduled to run after a CARD_INSERTION event
308 is received, to configure the PCMCIA socket, and to make the
309 device available to the system.
311 ======================================================================*/
312 static int get_tuple(int fn
, client_handle_t handle
, tuple_t
*tuple
,
316 i
= CardServices(fn
, handle
, tuple
);
317 if (i
!= CS_SUCCESS
) return i
;
318 i
= CardServices(GetTupleData
, handle
, tuple
);
319 if (i
!= CS_SUCCESS
) return i
;
320 return CardServices(ParseTuple
, handle
, tuple
, parse
);
323 #define first_tuple(a, b, c) get_tuple(GetFirstTuple, a, b, c)
324 #define next_tuple(a, b, c) get_tuple(GetNextTuple, a, b, c)
326 static void elsa_cs_config(dev_link_t
*link
)
328 client_handle_t handle
;
334 cistpl_cftable_entry_t
*cf
= &parse
.cftable_entry
;
336 DEBUG(0, "elsa_config(0x%p)\n", link
);
337 handle
= link
->handle
;
341 This reads the card's CONFIG tuple to find its configuration
344 tuple
.DesiredTuple
= CISTPL_CONFIG
;
345 tuple
.TupleData
= (cisdata_t
*)buf
;
346 tuple
.TupleDataMax
= 255;
347 tuple
.TupleOffset
= 0;
348 tuple
.Attributes
= 0;
349 i
= first_tuple(handle
, &tuple
, &parse
);
350 if (i
!= CS_SUCCESS
) {
351 last_fn
= ParseTuple
;
354 link
->conf
.ConfigBase
= parse
.config
.base
;
355 link
->conf
.Present
= parse
.config
.rmask
[0];
358 link
->state
|= DEV_CONFIG
;
360 tuple
.TupleData
= (cisdata_t
*)buf
;
361 tuple
.TupleOffset
= 0; tuple
.TupleDataMax
= 255;
362 tuple
.Attributes
= 0;
363 tuple
.DesiredTuple
= CISTPL_CFTABLE_ENTRY
;
364 i
= first_tuple(handle
, &tuple
, &parse
);
365 while (i
== CS_SUCCESS
) {
366 if ( (cf
->io
.nwin
> 0) && cf
->io
.win
[0].base
) {
367 printk(KERN_INFO
"(elsa_cs: looks like the 96 model)\n");
368 link
->conf
.ConfigIndex
= cf
->index
;
369 link
->io
.BasePort1
= cf
->io
.win
[0].base
;
370 i
= CardServices(RequestIO
, link
->handle
, &link
->io
);
371 if (i
== CS_SUCCESS
) break;
373 printk(KERN_INFO
"(elsa_cs: looks like the 97 model)\n");
374 link
->conf
.ConfigIndex
= cf
->index
;
375 for (i
= 0, j
= 0x2f0; j
> 0x100; j
-= 0x10) {
376 link
->io
.BasePort1
= j
;
377 i
= CardServices(RequestIO
, link
->handle
, &link
->io
);
378 if (i
== CS_SUCCESS
) break;
382 i
= next_tuple(handle
, &tuple
, &parse
);
385 if (i
!= CS_SUCCESS
) {
390 i
= CardServices(RequestIRQ
, link
->handle
, &link
->irq
);
391 if (i
!= CS_SUCCESS
) {
392 link
->irq
.AssignedIRQ
= 0;
393 last_fn
= RequestIRQ
;
397 i
= CardServices(RequestConfiguration
, link
->handle
, &link
->conf
);
398 if (i
!= CS_SUCCESS
) {
399 last_fn
= RequestConfiguration
;
403 /* At this point, the dev_node_t structure(s) should be
404 initialized and arranged in a linked list at link->dev. *//* */
405 sprintf(dev
->node
.dev_name
, "elsa");
406 dev
->node
.major
= dev
->node
.minor
= 0x0;
408 link
->dev
= &dev
->node
;
410 /* Finally, report what we've done */
411 printk(KERN_INFO
"%s: index 0x%02x: Vcc %d.%d",
412 dev
->node
.dev_name
, link
->conf
.ConfigIndex
,
413 link
->conf
.Vcc
/10, link
->conf
.Vcc
%10);
415 printk(", Vpp %d.%d", link
->conf
.Vpp1
/10, link
->conf
.Vpp1
%10);
416 if (link
->conf
.Attributes
& CONF_ENABLE_IRQ
)
417 printk(", irq %d", link
->irq
.AssignedIRQ
);
418 if (link
->io
.NumPorts1
)
419 printk(", io 0x%04x-0x%04x", link
->io
.BasePort1
,
420 link
->io
.BasePort1
+link
->io
.NumPorts1
-1);
421 if (link
->io
.NumPorts2
)
422 printk(" & 0x%04x-0x%04x", link
->io
.BasePort2
,
423 link
->io
.BasePort2
+link
->io
.NumPorts2
-1);
426 link
->state
&= ~DEV_CONFIG_PENDING
;
428 elsa_init_pcmcia(link
->io
.BasePort1
, link
->irq
.AssignedIRQ
,
429 &(((local_info_t
*)link
->priv
)->busy
),
433 cs_error(link
->handle
, last_fn
, i
);
434 elsa_cs_release((u_long
)link
);
435 } /* elsa_cs_config */
437 /*======================================================================
439 After a card is removed, elsa_cs_release() will unregister the net
440 device, and release the PCMCIA configuration. If the device is
441 still open, this will be postponed until it is closed.
443 ======================================================================*/
445 static void elsa_cs_release(u_long arg
)
447 dev_link_t
*link
= (dev_link_t
*)arg
;
449 DEBUG(0, "elsa_cs_release(0x%p)\n", link
);
452 If the device is currently in use, we won't release until it
453 is actually closed, because until then, we can't be sure that
454 no one will try to access the device or its data structures.
457 DEBUG(1, "elsa_cs: release postponed, '%s' "
458 "still open\n", link
->dev
->dev_name
);
459 link
->state
|= DEV_STALE_CONFIG
;
463 /* Unlink the device chain */
466 /* Don't bother checking to see if these succeed or not */
468 CardServices(ReleaseWindow
, link
->win
);
469 CardServices(ReleaseConfiguration
, link
->handle
);
470 CardServices(ReleaseIO
, link
->handle
, &link
->io
);
471 CardServices(ReleaseIRQ
, link
->handle
, &link
->irq
);
472 link
->state
&= ~DEV_CONFIG
;
474 if (link
->state
& DEV_STALE_LINK
)
475 elsa_cs_detach(link
);
477 } /* elsa_cs_release */
479 /*======================================================================
481 The card status event handler. Mostly, this schedules other
482 stuff to run after an event is received. A CARD_REMOVAL event
483 also sets some flags to discourage the net drivers from trying
484 to talk to the card any more.
486 When a CARD_REMOVAL event is received, we immediately set a flag
487 to block future accesses to this device. All the functions that
488 actually access the device should check this flag to make sure
489 the card is still present.
491 ======================================================================*/
493 static int elsa_cs_event(event_t event
, int priority
,
494 event_callback_args_t
*args
)
496 dev_link_t
*link
= args
->client_data
;
497 local_info_t
*dev
= link
->priv
;
499 DEBUG(1, "elsa_cs_event(%d)\n", event
);
502 case CS_EVENT_CARD_REMOVAL
:
503 link
->state
&= ~DEV_PRESENT
;
504 if (link
->state
& DEV_CONFIG
) {
505 ((local_info_t
*)link
->priv
)->busy
= 1;
506 mod_timer(&link
->release
, jiffies
+ HZ
/20);
509 case CS_EVENT_CARD_INSERTION
:
510 link
->state
|= DEV_PRESENT
| DEV_CONFIG_PENDING
;
511 elsa_cs_config(link
);
513 case CS_EVENT_PM_SUSPEND
:
514 link
->state
|= DEV_SUSPEND
;
515 /* Fall through... */
516 case CS_EVENT_RESET_PHYSICAL
:
517 /* Mark the device as stopped, to block IO until later */
519 if (link
->state
& DEV_CONFIG
)
520 CardServices(ReleaseConfiguration
, link
->handle
);
522 case CS_EVENT_PM_RESUME
:
523 link
->state
&= ~DEV_SUSPEND
;
524 /* Fall through... */
525 case CS_EVENT_CARD_RESET
:
526 if (link
->state
& DEV_CONFIG
)
527 CardServices(RequestConfiguration
, link
->handle
, &link
->conf
);
532 } /* elsa_cs_event */
534 static struct pcmcia_driver elsa_cs_driver
= {
535 .owner
= THIS_MODULE
,
539 .attach
= elsa_cs_attach
,
540 .detach
= elsa_cs_detach
,
543 static int __init
init_elsa_cs(void)
545 return pcmcia_register_driver(&elsa_cs_driver
);
548 static void __exit
exit_elsa_cs(void)
550 pcmcia_unregister_driver(&elsa_cs_driver
);
552 /* XXX: this really needs to move into generic code.. */
553 while (dev_list
!= NULL
)
554 elsa_cs_detach(dev_list
);
557 module_init(init_elsa_cs
);
558 module_exit(exit_elsa_cs
);