[PATCH] pcmcia: remove dev_list from drivers
[linux-2.6/linux-2.6-openrd.git] / drivers / isdn / hisax / avma1_cs.c
blobb6ea653f881e75d9598752fcb028a37835a7d92b
1 /*
2 * PCMCIA client driver for AVM A1 / Fritz!PCMCIA
4 * Author Carsten Paeth
5 * Copyright 1998-2001 by Carsten Paeth <calle@calle.in-berlin.de>
6 *
7 * This software may be used and distributed according to the terms
8 * of the GNU General Public License, incorporated herein by reference.
12 #include <linux/module.h>
15 #include <linux/kernel.h>
16 #include <linux/init.h>
17 #include <linux/sched.h>
18 #include <linux/ptrace.h>
19 #include <linux/slab.h>
20 #include <linux/string.h>
21 #include <asm/io.h>
22 #include <asm/system.h>
24 #include <pcmcia/cs_types.h>
25 #include <pcmcia/cs.h>
26 #include <pcmcia/cistpl.h>
27 #include <pcmcia/ds.h>
28 #include "hisax_cfg.h"
30 MODULE_DESCRIPTION("ISDN4Linux: PCMCIA client driver for AVM A1/Fritz!PCMCIA cards");
31 MODULE_AUTHOR("Carsten Paeth");
32 MODULE_LICENSE("GPL");
35 All the PCMCIA modules use PCMCIA_DEBUG to control debugging. If
36 you do not define PCMCIA_DEBUG at all, all the debug code will be
37 left out. If you compile with PCMCIA_DEBUG=0, the debug code will
38 be present but disabled -- but it can then be enabled for specific
39 modules at load time with a 'pc_debug=#' option to insmod.
41 #ifdef PCMCIA_DEBUG
42 static int pc_debug = PCMCIA_DEBUG;
43 module_param(pc_debug, int, 0);
44 #define DEBUG(n, args...) if (pc_debug>(n)) printk(KERN_DEBUG args);
45 static char *version =
46 "avma1_cs.c 1.00 1998/01/23 10:00:00 (Carsten Paeth)";
47 #else
48 #define DEBUG(n, args...)
49 #endif
51 /*====================================================================*/
53 /* Parameters that can be set with 'insmod' */
55 static int isdnprot = 2;
57 module_param(isdnprot, int, 0);
59 /*====================================================================*/
62 The event() function is this driver's Card Services event handler.
63 It will be called by Card Services when an appropriate card status
64 event is received. The config() and release() entry points are
65 used to configure or release a socket, in response to card insertion
66 and ejection events. They are invoked from the skeleton event
67 handler.
70 static void avma1cs_config(dev_link_t *link);
71 static void avma1cs_release(dev_link_t *link);
72 static int avma1cs_event(event_t event, int priority,
73 event_callback_args_t *args);
76 The attach() and detach() entry points are used to create and destroy
77 "instances" of the driver, where each instance represents everything
78 needed to manage one actual PCMCIA card.
81 static dev_link_t *avma1cs_attach(void);
82 static void avma1cs_detach(struct pcmcia_device *p_dev);
85 The dev_info variable is the "key" that is used to match up this
86 device driver with appropriate cards, through the card configuration
87 database.
90 static dev_info_t dev_info = "avma1_cs";
93 A linked list of "instances" of the skeleton device. Each actual
94 PCMCIA card corresponds to one device instance, and is described
95 by one dev_link_t structure (defined in ds.h).
97 You may not want to use a linked list for this -- for example, the
98 memory card driver uses an array of dev_link_t pointers, where minor
99 device numbers are used to derive the corresponding array index.
103 A driver needs to provide a dev_node_t structure for each device
104 on a card. In some cases, there is only one device per card (for
105 example, ethernet cards, modems). In other cases, there may be
106 many actual or logical devices (SCSI adapters, memory cards with
107 multiple partitions). The dev_node_t structures need to be kept
108 in a linked list starting at the 'dev' field of a dev_link_t
109 structure. We allocate them in the card's private data structure,
110 because they generally can't be allocated dynamically.
113 typedef struct local_info_t {
114 dev_node_t node;
115 } local_info_t;
117 /*======================================================================
119 avma1cs_attach() creates an "instance" of the driver, allocating
120 local data structures for one device. The device is registered
121 with Card Services.
123 The dev_link structure is initialized, but we don't actually
124 configure the card at this point -- we wait until we receive a
125 card insertion event.
127 ======================================================================*/
129 static dev_link_t *avma1cs_attach(void)
131 client_reg_t client_reg;
132 dev_link_t *link;
133 local_info_t *local;
134 int ret;
136 DEBUG(0, "avma1cs_attach()\n");
138 /* Initialize the dev_link_t structure */
139 link = kmalloc(sizeof(struct dev_link_t), GFP_KERNEL);
140 if (!link)
141 return NULL;
142 memset(link, 0, sizeof(struct dev_link_t));
144 /* Allocate space for private device-specific data */
145 local = kmalloc(sizeof(local_info_t), GFP_KERNEL);
146 if (!local) {
147 kfree(link);
148 return NULL;
150 memset(local, 0, sizeof(local_info_t));
151 link->priv = local;
153 /* The io structure describes IO port mapping */
154 link->io.NumPorts1 = 16;
155 link->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
156 link->io.NumPorts2 = 16;
157 link->io.Attributes2 = IO_DATA_PATH_WIDTH_16;
158 link->io.IOAddrLines = 5;
160 /* Interrupt setup */
161 link->irq.Attributes = IRQ_TYPE_EXCLUSIVE;
162 link->irq.Attributes = IRQ_TYPE_DYNAMIC_SHARING|IRQ_FIRST_SHARED;
164 link->irq.IRQInfo1 = IRQ_LEVEL_ID;
166 /* General socket configuration */
167 link->conf.Attributes = CONF_ENABLE_IRQ;
168 link->conf.Vcc = 50;
169 link->conf.IntType = INT_MEMORY_AND_IO;
170 link->conf.ConfigIndex = 1;
171 link->conf.Present = PRESENT_OPTION;
173 /* Register with Card Services */
174 link->next = NULL;
175 client_reg.dev_info = &dev_info;
176 client_reg.Version = 0x0210;
177 client_reg.event_callback_args.client_data = link;
178 ret = pcmcia_register_client(&link->handle, &client_reg);
179 if (ret != 0) {
180 cs_error(link->handle, RegisterClient, ret);
181 avma1cs_detach(link->handle);
182 return NULL;
185 return link;
186 } /* avma1cs_attach */
188 /*======================================================================
190 This deletes a driver "instance". The device is de-registered
191 with Card Services. If it has been released, all local data
192 structures are freed. Otherwise, the structures will be freed
193 when the device is released.
195 ======================================================================*/
197 static void avma1cs_detach(struct pcmcia_device *p_dev)
199 dev_link_t *link = dev_to_instance(p_dev);
201 DEBUG(0, "avma1cs_detach(0x%p)\n", link);
203 if (link->state & DEV_CONFIG)
204 avma1cs_release(link);
206 kfree(link->priv);
207 kfree(link);
208 } /* avma1cs_detach */
210 /*======================================================================
212 avma1cs_config() is scheduled to run after a CARD_INSERTION event
213 is received, to configure the PCMCIA socket, and to make the
214 ethernet device available to the system.
216 ======================================================================*/
218 static int get_tuple(client_handle_t handle, tuple_t *tuple,
219 cisparse_t *parse)
221 int i = pcmcia_get_tuple_data(handle, tuple);
222 if (i != CS_SUCCESS) return i;
223 return pcmcia_parse_tuple(handle, tuple, parse);
226 static int first_tuple(client_handle_t handle, tuple_t *tuple,
227 cisparse_t *parse)
229 int i = pcmcia_get_first_tuple(handle, tuple);
230 if (i != CS_SUCCESS) return i;
231 return get_tuple(handle, tuple, parse);
234 static int next_tuple(client_handle_t handle, tuple_t *tuple,
235 cisparse_t *parse)
237 int i = pcmcia_get_next_tuple(handle, tuple);
238 if (i != CS_SUCCESS) return i;
239 return get_tuple(handle, tuple, parse);
242 static void avma1cs_config(dev_link_t *link)
244 client_handle_t handle;
245 tuple_t tuple;
246 cisparse_t parse;
247 cistpl_cftable_entry_t *cf = &parse.cftable_entry;
248 local_info_t *dev;
249 int i;
250 u_char buf[64];
251 char devname[128];
252 IsdnCard_t icard;
253 int busy = 0;
255 handle = link->handle;
256 dev = link->priv;
258 DEBUG(0, "avma1cs_config(0x%p)\n", link);
261 This reads the card's CONFIG tuple to find its configuration
262 registers.
264 do {
265 tuple.DesiredTuple = CISTPL_CONFIG;
266 i = pcmcia_get_first_tuple(handle, &tuple);
267 if (i != CS_SUCCESS) break;
268 tuple.TupleData = buf;
269 tuple.TupleDataMax = 64;
270 tuple.TupleOffset = 0;
271 i = pcmcia_get_tuple_data(handle, &tuple);
272 if (i != CS_SUCCESS) break;
273 i = pcmcia_parse_tuple(handle, &tuple, &parse);
274 if (i != CS_SUCCESS) break;
275 link->conf.ConfigBase = parse.config.base;
276 } while (0);
277 if (i != CS_SUCCESS) {
278 cs_error(link->handle, ParseTuple, i);
279 link->state &= ~DEV_CONFIG_PENDING;
280 return;
283 /* Configure card */
284 link->state |= DEV_CONFIG;
286 do {
288 tuple.Attributes = 0;
289 tuple.TupleData = buf;
290 tuple.TupleDataMax = 254;
291 tuple.TupleOffset = 0;
292 tuple.DesiredTuple = CISTPL_VERS_1;
294 devname[0] = 0;
295 if( !first_tuple(handle, &tuple, &parse) && parse.version_1.ns > 1 ) {
296 strlcpy(devname,parse.version_1.str + parse.version_1.ofs[1],
297 sizeof(devname));
300 * find IO port
302 tuple.TupleData = (cisdata_t *)buf;
303 tuple.TupleOffset = 0; tuple.TupleDataMax = 255;
304 tuple.Attributes = 0;
305 tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
306 i = first_tuple(handle, &tuple, &parse);
307 while (i == CS_SUCCESS) {
308 if (cf->io.nwin > 0) {
309 link->conf.ConfigIndex = cf->index;
310 link->io.BasePort1 = cf->io.win[0].base;
311 link->io.NumPorts1 = cf->io.win[0].len;
312 link->io.NumPorts2 = 0;
313 printk(KERN_INFO "avma1_cs: testing i/o %#x-%#x\n",
314 link->io.BasePort1,
315 link->io.BasePort1+link->io.NumPorts1 - 1);
316 i = pcmcia_request_io(link->handle, &link->io);
317 if (i == CS_SUCCESS) goto found_port;
319 i = next_tuple(handle, &tuple, &parse);
322 found_port:
323 if (i != CS_SUCCESS) {
324 cs_error(link->handle, RequestIO, i);
325 break;
329 * allocate an interrupt line
331 i = pcmcia_request_irq(link->handle, &link->irq);
332 if (i != CS_SUCCESS) {
333 cs_error(link->handle, RequestIRQ, i);
334 pcmcia_release_io(link->handle, &link->io);
335 break;
339 * configure the PCMCIA socket
341 i = pcmcia_request_configuration(link->handle, &link->conf);
342 if (i != CS_SUCCESS) {
343 cs_error(link->handle, RequestConfiguration, i);
344 pcmcia_release_io(link->handle, &link->io);
345 pcmcia_release_irq(link->handle, &link->irq);
346 break;
349 } while (0);
351 /* At this point, the dev_node_t structure(s) should be
352 initialized and arranged in a linked list at link->dev. */
354 strcpy(dev->node.dev_name, "A1");
355 dev->node.major = 45;
356 dev->node.minor = 0;
357 link->dev = &dev->node;
359 link->state &= ~DEV_CONFIG_PENDING;
360 /* If any step failed, release any partially configured state */
361 if (i != 0) {
362 avma1cs_release(link);
363 return;
366 printk(KERN_NOTICE "avma1_cs: checking at i/o %#x, irq %d\n",
367 link->io.BasePort1, link->irq.AssignedIRQ);
369 icard.para[0] = link->irq.AssignedIRQ;
370 icard.para[1] = link->io.BasePort1;
371 icard.protocol = isdnprot;
372 icard.typ = ISDN_CTYPE_A1_PCMCIA;
374 i = hisax_init_pcmcia(link, &busy, &icard);
375 if (i < 0) {
376 printk(KERN_ERR "avma1_cs: failed to initialize AVM A1 PCMCIA %d at i/o %#x\n", i, link->io.BasePort1);
377 avma1cs_release(link);
378 return;
380 dev->node.minor = i;
382 } /* avma1cs_config */
384 /*======================================================================
386 After a card is removed, avma1cs_release() will unregister the net
387 device, and release the PCMCIA configuration. If the device is
388 still open, this will be postponed until it is closed.
390 ======================================================================*/
392 static void avma1cs_release(dev_link_t *link)
394 local_info_t *local = link->priv;
396 DEBUG(0, "avma1cs_release(0x%p)\n", link);
398 /* no unregister function with hisax */
399 HiSax_closecard(local->node.minor);
401 /* Unlink the device chain */
402 link->dev = NULL;
404 /* Don't bother checking to see if these succeed or not */
405 pcmcia_release_configuration(link->handle);
406 pcmcia_release_io(link->handle, &link->io);
407 pcmcia_release_irq(link->handle, &link->irq);
408 link->state &= ~DEV_CONFIG;
409 } /* avma1cs_release */
411 static int avma1cs_suspend(struct pcmcia_device *dev)
413 dev_link_t *link = dev_to_instance(dev);
415 link->state |= DEV_SUSPEND;
416 if (link->state & DEV_CONFIG)
417 pcmcia_release_configuration(link->handle);
419 return 0;
422 static int avma1cs_resume(struct pcmcia_device *dev)
424 dev_link_t *link = dev_to_instance(dev);
426 link->state &= ~DEV_SUSPEND;
427 if (link->state & DEV_CONFIG)
428 pcmcia_request_configuration(link->handle, &link->conf);
430 return 0;
433 /*======================================================================
435 The card status event handler. Mostly, this schedules other
436 stuff to run after an event is received. A CARD_REMOVAL event
437 also sets some flags to discourage the net drivers from trying
438 to talk to the card any more.
440 When a CARD_REMOVAL event is received, we immediately set a flag
441 to block future accesses to this device. All the functions that
442 actually access the device should check this flag to make sure
443 the card is still present.
445 ======================================================================*/
447 static int avma1cs_event(event_t event, int priority,
448 event_callback_args_t *args)
450 dev_link_t *link = args->client_data;
452 DEBUG(1, "avma1cs_event(0x%06x)\n", event);
454 switch (event) {
455 case CS_EVENT_CARD_INSERTION:
456 link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
457 avma1cs_config(link);
458 break;
460 return 0;
461 } /* avma1cs_event */
463 static struct pcmcia_device_id avma1cs_ids[] = {
464 PCMCIA_DEVICE_PROD_ID12("AVM", "ISDN A", 0x95d42008, 0xadc9d4bb),
465 PCMCIA_DEVICE_PROD_ID12("ISDN", "CARD", 0x8d9761c8, 0x01c5aa7b),
466 PCMCIA_DEVICE_NULL
468 MODULE_DEVICE_TABLE(pcmcia, avma1cs_ids);
470 static struct pcmcia_driver avma1cs_driver = {
471 .owner = THIS_MODULE,
472 .drv = {
473 .name = "avma1_cs",
475 .attach = avma1cs_attach,
476 .event = avma1cs_event,
477 .remove = avma1cs_detach,
478 .id_table = avma1cs_ids,
479 .suspend = avma1cs_suspend,
480 .resume = avma1cs_resume,
483 /*====================================================================*/
485 static int __init init_avma1_cs(void)
487 return(pcmcia_register_driver(&avma1cs_driver));
490 static void __exit exit_avma1_cs(void)
492 pcmcia_unregister_driver(&avma1cs_driver);
495 module_init(init_avma1_cs);
496 module_exit(exit_avma1_cs);