[PATCH] pcmcia: embed dev_link_t into struct pcmcia_device
[linux-2.6/suspend2-2.6.18.git] / drivers / isdn / hisax / avma1_cs.c
blobf7143fe1a2e7aefb26200d2126ef9ccf0f47bd55
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);
74 The attach() and detach() entry points are used to create and destroy
75 "instances" of the driver, where each instance represents everything
76 needed to manage one actual PCMCIA card.
79 static void avma1cs_detach(struct pcmcia_device *p_dev);
83 A linked list of "instances" of the skeleton device. Each actual
84 PCMCIA card corresponds to one device instance, and is described
85 by one dev_link_t structure (defined in ds.h).
87 You may not want to use a linked list for this -- for example, the
88 memory card driver uses an array of dev_link_t pointers, where minor
89 device numbers are used to derive the corresponding array index.
93 A driver needs to provide a dev_node_t structure for each device
94 on a card. In some cases, there is only one device per card (for
95 example, ethernet cards, modems). In other cases, there may be
96 many actual or logical devices (SCSI adapters, memory cards with
97 multiple partitions). The dev_node_t structures need to be kept
98 in a linked list starting at the 'dev' field of a dev_link_t
99 structure. We allocate them in the card's private data structure,
100 because they generally can't be allocated dynamically.
103 typedef struct local_info_t {
104 dev_node_t node;
105 } local_info_t;
107 /*======================================================================
109 avma1cs_attach() creates an "instance" of the driver, allocating
110 local data structures for one device. The device is registered
111 with Card Services.
113 The dev_link structure is initialized, but we don't actually
114 configure the card at this point -- we wait until we receive a
115 card insertion event.
117 ======================================================================*/
119 static int avma1cs_attach(struct pcmcia_device *p_dev)
121 local_info_t *local;
123 DEBUG(0, "avma1cs_attach()\n");
125 /* Allocate space for private device-specific data */
126 local = kmalloc(sizeof(local_info_t), GFP_KERNEL);
127 if (!local)
128 return -ENOMEM;
130 memset(local, 0, sizeof(local_info_t));
131 p_dev->priv = local;
133 /* The io structure describes IO port mapping */
134 p_dev->io.NumPorts1 = 16;
135 p_dev->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
136 p_dev->io.NumPorts2 = 16;
137 p_dev->io.Attributes2 = IO_DATA_PATH_WIDTH_16;
138 p_dev->io.IOAddrLines = 5;
140 /* Interrupt setup */
141 p_dev->irq.Attributes = IRQ_TYPE_EXCLUSIVE;
142 p_dev->irq.Attributes = IRQ_TYPE_DYNAMIC_SHARING|IRQ_FIRST_SHARED;
144 p_dev->irq.IRQInfo1 = IRQ_LEVEL_ID;
146 /* General socket configuration */
147 p_dev->conf.Attributes = CONF_ENABLE_IRQ;
148 p_dev->conf.IntType = INT_MEMORY_AND_IO;
149 p_dev->conf.ConfigIndex = 1;
150 p_dev->conf.Present = PRESENT_OPTION;
152 p_dev->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
153 avma1cs_config(p_dev);
155 return 0;
156 } /* avma1cs_attach */
158 /*======================================================================
160 This deletes a driver "instance". The device is de-registered
161 with Card Services. If it has been released, all local data
162 structures are freed. Otherwise, the structures will be freed
163 when the device is released.
165 ======================================================================*/
167 static void avma1cs_detach(struct pcmcia_device *p_dev)
169 dev_link_t *link = dev_to_instance(p_dev);
171 DEBUG(0, "avma1cs_detach(0x%p)\n", link);
173 if (link->state & DEV_CONFIG)
174 avma1cs_release(link);
176 kfree(link->priv);
177 } /* avma1cs_detach */
179 /*======================================================================
181 avma1cs_config() is scheduled to run after a CARD_INSERTION event
182 is received, to configure the PCMCIA socket, and to make the
183 ethernet device available to the system.
185 ======================================================================*/
187 static int get_tuple(client_handle_t handle, tuple_t *tuple,
188 cisparse_t *parse)
190 int i = pcmcia_get_tuple_data(handle, tuple);
191 if (i != CS_SUCCESS) return i;
192 return pcmcia_parse_tuple(handle, tuple, parse);
195 static int first_tuple(client_handle_t handle, tuple_t *tuple,
196 cisparse_t *parse)
198 int i = pcmcia_get_first_tuple(handle, tuple);
199 if (i != CS_SUCCESS) return i;
200 return get_tuple(handle, tuple, parse);
203 static int next_tuple(client_handle_t handle, tuple_t *tuple,
204 cisparse_t *parse)
206 int i = pcmcia_get_next_tuple(handle, tuple);
207 if (i != CS_SUCCESS) return i;
208 return get_tuple(handle, tuple, parse);
211 static void avma1cs_config(dev_link_t *link)
213 client_handle_t handle;
214 tuple_t tuple;
215 cisparse_t parse;
216 cistpl_cftable_entry_t *cf = &parse.cftable_entry;
217 local_info_t *dev;
218 int i;
219 u_char buf[64];
220 char devname[128];
221 IsdnCard_t icard;
222 int busy = 0;
224 handle = link->handle;
225 dev = link->priv;
227 DEBUG(0, "avma1cs_config(0x%p)\n", link);
230 This reads the card's CONFIG tuple to find its configuration
231 registers.
233 do {
234 tuple.DesiredTuple = CISTPL_CONFIG;
235 i = pcmcia_get_first_tuple(handle, &tuple);
236 if (i != CS_SUCCESS) break;
237 tuple.TupleData = buf;
238 tuple.TupleDataMax = 64;
239 tuple.TupleOffset = 0;
240 i = pcmcia_get_tuple_data(handle, &tuple);
241 if (i != CS_SUCCESS) break;
242 i = pcmcia_parse_tuple(handle, &tuple, &parse);
243 if (i != CS_SUCCESS) break;
244 link->conf.ConfigBase = parse.config.base;
245 } while (0);
246 if (i != CS_SUCCESS) {
247 cs_error(link->handle, ParseTuple, i);
248 link->state &= ~DEV_CONFIG_PENDING;
249 return;
252 /* Configure card */
253 link->state |= DEV_CONFIG;
255 do {
257 tuple.Attributes = 0;
258 tuple.TupleData = buf;
259 tuple.TupleDataMax = 254;
260 tuple.TupleOffset = 0;
261 tuple.DesiredTuple = CISTPL_VERS_1;
263 devname[0] = 0;
264 if( !first_tuple(handle, &tuple, &parse) && parse.version_1.ns > 1 ) {
265 strlcpy(devname,parse.version_1.str + parse.version_1.ofs[1],
266 sizeof(devname));
269 * find IO port
271 tuple.TupleData = (cisdata_t *)buf;
272 tuple.TupleOffset = 0; tuple.TupleDataMax = 255;
273 tuple.Attributes = 0;
274 tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
275 i = first_tuple(handle, &tuple, &parse);
276 while (i == CS_SUCCESS) {
277 if (cf->io.nwin > 0) {
278 link->conf.ConfigIndex = cf->index;
279 link->io.BasePort1 = cf->io.win[0].base;
280 link->io.NumPorts1 = cf->io.win[0].len;
281 link->io.NumPorts2 = 0;
282 printk(KERN_INFO "avma1_cs: testing i/o %#x-%#x\n",
283 link->io.BasePort1,
284 link->io.BasePort1+link->io.NumPorts1 - 1);
285 i = pcmcia_request_io(link->handle, &link->io);
286 if (i == CS_SUCCESS) goto found_port;
288 i = next_tuple(handle, &tuple, &parse);
291 found_port:
292 if (i != CS_SUCCESS) {
293 cs_error(link->handle, RequestIO, i);
294 break;
298 * allocate an interrupt line
300 i = pcmcia_request_irq(link->handle, &link->irq);
301 if (i != CS_SUCCESS) {
302 cs_error(link->handle, RequestIRQ, i);
303 /* undo */
304 pcmcia_disable_device(link->handle);
305 break;
309 * configure the PCMCIA socket
311 i = pcmcia_request_configuration(link->handle, &link->conf);
312 if (i != CS_SUCCESS) {
313 cs_error(link->handle, RequestConfiguration, i);
314 pcmcia_disable_device(link->handle);
315 break;
318 } while (0);
320 /* At this point, the dev_node_t structure(s) should be
321 initialized and arranged in a linked list at link->dev. */
323 strcpy(dev->node.dev_name, "A1");
324 dev->node.major = 45;
325 dev->node.minor = 0;
326 link->dev_node = &dev->node;
328 link->state &= ~DEV_CONFIG_PENDING;
329 /* If any step failed, release any partially configured state */
330 if (i != 0) {
331 avma1cs_release(link);
332 return;
335 printk(KERN_NOTICE "avma1_cs: checking at i/o %#x, irq %d\n",
336 link->io.BasePort1, link->irq.AssignedIRQ);
338 icard.para[0] = link->irq.AssignedIRQ;
339 icard.para[1] = link->io.BasePort1;
340 icard.protocol = isdnprot;
341 icard.typ = ISDN_CTYPE_A1_PCMCIA;
343 i = hisax_init_pcmcia(link, &busy, &icard);
344 if (i < 0) {
345 printk(KERN_ERR "avma1_cs: failed to initialize AVM A1 PCMCIA %d at i/o %#x\n", i, link->io.BasePort1);
346 avma1cs_release(link);
347 return;
349 dev->node.minor = i;
351 } /* avma1cs_config */
353 /*======================================================================
355 After a card is removed, avma1cs_release() will unregister the net
356 device, and release the PCMCIA configuration. If the device is
357 still open, this will be postponed until it is closed.
359 ======================================================================*/
361 static void avma1cs_release(dev_link_t *link)
363 local_info_t *local = link->priv;
365 DEBUG(0, "avma1cs_release(0x%p)\n", link);
367 /* now unregister function with hisax */
368 HiSax_closecard(local->node.minor);
370 pcmcia_disable_device(link->handle);
371 } /* avma1cs_release */
374 static struct pcmcia_device_id avma1cs_ids[] = {
375 PCMCIA_DEVICE_PROD_ID12("AVM", "ISDN A", 0x95d42008, 0xadc9d4bb),
376 PCMCIA_DEVICE_PROD_ID12("ISDN", "CARD", 0x8d9761c8, 0x01c5aa7b),
377 PCMCIA_DEVICE_NULL
379 MODULE_DEVICE_TABLE(pcmcia, avma1cs_ids);
381 static struct pcmcia_driver avma1cs_driver = {
382 .owner = THIS_MODULE,
383 .drv = {
384 .name = "avma1_cs",
386 .probe = avma1cs_attach,
387 .remove = avma1cs_detach,
388 .id_table = avma1cs_ids,
391 /*====================================================================*/
393 static int __init init_avma1_cs(void)
395 return(pcmcia_register_driver(&avma1cs_driver));
398 static void __exit exit_avma1_cs(void)
400 pcmcia_unregister_driver(&avma1cs_driver);
403 module_init(init_avma1_cs);
404 module_exit(exit_avma1_cs);