blob: 729c2de0bc1d270fdb9ced9e299ca96e115e63b9 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * PCMCIA client driver for AVM A1 / Fritz!PCMCIA
3 *
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.
9 *
10 */
11
12#include <linux/module.h>
13
14
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>
23
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#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"
29
30MODULE_DESCRIPTION("ISDN4Linux: PCMCIA client driver for AVM A1/Fritz!PCMCIA cards");
31MODULE_AUTHOR("Carsten Paeth");
32MODULE_LICENSE("GPL");
33
34/*
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.
40*/
41#ifdef PCMCIA_DEBUG
42static int pc_debug = PCMCIA_DEBUG;
43module_param(pc_debug, int, 0);
44#define DEBUG(n, args...) if (pc_debug>(n)) printk(KERN_DEBUG args);
45static 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
50
51/*====================================================================*/
52
53/* Parameters that can be set with 'insmod' */
54
55static int isdnprot = 2;
56
57module_param(isdnprot, int, 0);
58
59/*====================================================================*/
60
61/*
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.
68*/
69
70static void avma1cs_config(dev_link_t *link);
71static void avma1cs_release(dev_link_t *link);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
73/*
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.
77*/
78
Dominik Brodowskicc3b4862005-11-14 21:23:14 +010079static void avma1cs_detach(struct pcmcia_device *p_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
82/*
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).
86
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.
90*/
91
Linus Torvalds1da177e2005-04-16 15:20:36 -070092/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 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.
101*/
102
103typedef struct local_info_t {
104 dev_node_t node;
105} local_info_t;
106
107/*======================================================================
108
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.
112
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.
116
117======================================================================*/
118
Dominik Brodowskif8cfa612005-11-14 21:25:51 +0100119static int avma1cs_attach(struct pcmcia_device *p_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 dev_link_t *link;
122 local_info_t *local;
Dominik Brodowskif8cfa612005-11-14 21:25:51 +0100123
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 DEBUG(0, "avma1cs_attach()\n");
125
126 /* Initialize the dev_link_t structure */
127 link = kmalloc(sizeof(struct dev_link_t), GFP_KERNEL);
128 if (!link)
Dominik Brodowskif8cfa612005-11-14 21:25:51 +0100129 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 memset(link, 0, sizeof(struct dev_link_t));
131
132 /* Allocate space for private device-specific data */
133 local = kmalloc(sizeof(local_info_t), GFP_KERNEL);
134 if (!local) {
135 kfree(link);
Dominik Brodowskif8cfa612005-11-14 21:25:51 +0100136 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 }
138 memset(local, 0, sizeof(local_info_t));
139 link->priv = local;
140
141 /* The io structure describes IO port mapping */
142 link->io.NumPorts1 = 16;
143 link->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
144 link->io.NumPorts2 = 16;
145 link->io.Attributes2 = IO_DATA_PATH_WIDTH_16;
146 link->io.IOAddrLines = 5;
147
148 /* Interrupt setup */
149 link->irq.Attributes = IRQ_TYPE_EXCLUSIVE;
150 link->irq.Attributes = IRQ_TYPE_DYNAMIC_SHARING|IRQ_FIRST_SHARED;
151
152 link->irq.IRQInfo1 = IRQ_LEVEL_ID;
153
154 /* General socket configuration */
155 link->conf.Attributes = CONF_ENABLE_IRQ;
156 link->conf.Vcc = 50;
157 link->conf.IntType = INT_MEMORY_AND_IO;
158 link->conf.ConfigIndex = 1;
159 link->conf.Present = PRESENT_OPTION;
160
Dominik Brodowskif8cfa612005-11-14 21:25:51 +0100161 link->handle = p_dev;
162 p_dev->instance = link;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
Dominik Brodowskif8cfa612005-11-14 21:25:51 +0100164 link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
165 avma1cs_config(link);
166
167 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168} /* avma1cs_attach */
169
170/*======================================================================
171
172 This deletes a driver "instance". The device is de-registered
173 with Card Services. If it has been released, all local data
174 structures are freed. Otherwise, the structures will be freed
175 when the device is released.
176
177======================================================================*/
178
Dominik Brodowskicc3b4862005-11-14 21:23:14 +0100179static void avma1cs_detach(struct pcmcia_device *p_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180{
Dominik Brodowskicc3b4862005-11-14 21:23:14 +0100181 dev_link_t *link = dev_to_instance(p_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
183 DEBUG(0, "avma1cs_detach(0x%p)\n", link);
Dominik Brodowskicc3b4862005-11-14 21:23:14 +0100184
Dominik Brodowskicc3b4862005-11-14 21:23:14 +0100185 if (link->state & DEV_CONFIG)
186 avma1cs_release(link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
Jesper Juhl3c7208f2005-11-07 01:01:29 -0800188 kfree(link->priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 kfree(link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190} /* avma1cs_detach */
191
192/*======================================================================
193
194 avma1cs_config() is scheduled to run after a CARD_INSERTION event
195 is received, to configure the PCMCIA socket, and to make the
196 ethernet device available to the system.
197
198======================================================================*/
199
200static int get_tuple(client_handle_t handle, tuple_t *tuple,
201 cisparse_t *parse)
202{
203 int i = pcmcia_get_tuple_data(handle, tuple);
204 if (i != CS_SUCCESS) return i;
205 return pcmcia_parse_tuple(handle, tuple, parse);
206}
207
208static int first_tuple(client_handle_t handle, tuple_t *tuple,
209 cisparse_t *parse)
210{
211 int i = pcmcia_get_first_tuple(handle, tuple);
212 if (i != CS_SUCCESS) return i;
213 return get_tuple(handle, tuple, parse);
214}
215
216static int next_tuple(client_handle_t handle, tuple_t *tuple,
217 cisparse_t *parse)
218{
219 int i = pcmcia_get_next_tuple(handle, tuple);
220 if (i != CS_SUCCESS) return i;
221 return get_tuple(handle, tuple, parse);
222}
223
224static void avma1cs_config(dev_link_t *link)
225{
226 client_handle_t handle;
227 tuple_t tuple;
228 cisparse_t parse;
229 cistpl_cftable_entry_t *cf = &parse.cftable_entry;
230 local_info_t *dev;
231 int i;
232 u_char buf[64];
233 char devname[128];
234 IsdnCard_t icard;
235 int busy = 0;
236
237 handle = link->handle;
238 dev = link->priv;
239
240 DEBUG(0, "avma1cs_config(0x%p)\n", link);
241
242 /*
243 This reads the card's CONFIG tuple to find its configuration
244 registers.
245 */
246 do {
247 tuple.DesiredTuple = CISTPL_CONFIG;
248 i = pcmcia_get_first_tuple(handle, &tuple);
249 if (i != CS_SUCCESS) break;
250 tuple.TupleData = buf;
251 tuple.TupleDataMax = 64;
252 tuple.TupleOffset = 0;
253 i = pcmcia_get_tuple_data(handle, &tuple);
254 if (i != CS_SUCCESS) break;
255 i = pcmcia_parse_tuple(handle, &tuple, &parse);
256 if (i != CS_SUCCESS) break;
257 link->conf.ConfigBase = parse.config.base;
258 } while (0);
259 if (i != CS_SUCCESS) {
260 cs_error(link->handle, ParseTuple, i);
261 link->state &= ~DEV_CONFIG_PENDING;
262 return;
263 }
264
265 /* Configure card */
266 link->state |= DEV_CONFIG;
267
268 do {
269
270 tuple.Attributes = 0;
271 tuple.TupleData = buf;
272 tuple.TupleDataMax = 254;
273 tuple.TupleOffset = 0;
274 tuple.DesiredTuple = CISTPL_VERS_1;
275
276 devname[0] = 0;
277 if( !first_tuple(handle, &tuple, &parse) && parse.version_1.ns > 1 ) {
278 strlcpy(devname,parse.version_1.str + parse.version_1.ofs[1],
279 sizeof(devname));
280 }
281 /*
282 * find IO port
283 */
284 tuple.TupleData = (cisdata_t *)buf;
285 tuple.TupleOffset = 0; tuple.TupleDataMax = 255;
286 tuple.Attributes = 0;
287 tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
288 i = first_tuple(handle, &tuple, &parse);
289 while (i == CS_SUCCESS) {
290 if (cf->io.nwin > 0) {
291 link->conf.ConfigIndex = cf->index;
292 link->io.BasePort1 = cf->io.win[0].base;
293 link->io.NumPorts1 = cf->io.win[0].len;
294 link->io.NumPorts2 = 0;
295 printk(KERN_INFO "avma1_cs: testing i/o %#x-%#x\n",
296 link->io.BasePort1,
297 link->io.BasePort1+link->io.NumPorts1 - 1);
298 i = pcmcia_request_io(link->handle, &link->io);
299 if (i == CS_SUCCESS) goto found_port;
300 }
301 i = next_tuple(handle, &tuple, &parse);
302 }
303
304found_port:
305 if (i != CS_SUCCESS) {
306 cs_error(link->handle, RequestIO, i);
307 break;
308 }
309
310 /*
311 * allocate an interrupt line
312 */
313 i = pcmcia_request_irq(link->handle, &link->irq);
314 if (i != CS_SUCCESS) {
315 cs_error(link->handle, RequestIRQ, i);
316 pcmcia_release_io(link->handle, &link->io);
317 break;
318 }
319
320 /*
321 * configure the PCMCIA socket
322 */
323 i = pcmcia_request_configuration(link->handle, &link->conf);
324 if (i != CS_SUCCESS) {
325 cs_error(link->handle, RequestConfiguration, i);
326 pcmcia_release_io(link->handle, &link->io);
327 pcmcia_release_irq(link->handle, &link->irq);
328 break;
329 }
330
331 } while (0);
332
333 /* At this point, the dev_node_t structure(s) should be
334 initialized and arranged in a linked list at link->dev. */
335
336 strcpy(dev->node.dev_name, "A1");
337 dev->node.major = 45;
338 dev->node.minor = 0;
339 link->dev = &dev->node;
340
341 link->state &= ~DEV_CONFIG_PENDING;
342 /* If any step failed, release any partially configured state */
343 if (i != 0) {
344 avma1cs_release(link);
345 return;
346 }
347
348 printk(KERN_NOTICE "avma1_cs: checking at i/o %#x, irq %d\n",
349 link->io.BasePort1, link->irq.AssignedIRQ);
350
351 icard.para[0] = link->irq.AssignedIRQ;
352 icard.para[1] = link->io.BasePort1;
353 icard.protocol = isdnprot;
354 icard.typ = ISDN_CTYPE_A1_PCMCIA;
355
356 i = hisax_init_pcmcia(link, &busy, &icard);
357 if (i < 0) {
358 printk(KERN_ERR "avma1_cs: failed to initialize AVM A1 PCMCIA %d at i/o %#x\n", i, link->io.BasePort1);
359 avma1cs_release(link);
360 return;
361 }
362 dev->node.minor = i;
363
364} /* avma1cs_config */
365
366/*======================================================================
367
368 After a card is removed, avma1cs_release() will unregister the net
369 device, and release the PCMCIA configuration. If the device is
370 still open, this will be postponed until it is closed.
371
372======================================================================*/
373
374static void avma1cs_release(dev_link_t *link)
375{
Dominik Brodowski5f2a71f2006-01-15 09:32:39 +0100376 local_info_t *local = link->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
Dominik Brodowski5f2a71f2006-01-15 09:32:39 +0100378 DEBUG(0, "avma1cs_release(0x%p)\n", link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
Dominik Brodowski5f2a71f2006-01-15 09:32:39 +0100380 /* now unregister function with hisax */
381 HiSax_closecard(local->node.minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
Dominik Brodowski5f2a71f2006-01-15 09:32:39 +0100383 pcmcia_disable_device(link->handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384} /* avma1cs_release */
385
Dominik Brodowski98e4c282005-11-14 21:21:18 +0100386static int avma1cs_suspend(struct pcmcia_device *dev)
387{
388 dev_link_t *link = dev_to_instance(dev);
389
390 link->state |= DEV_SUSPEND;
391 if (link->state & DEV_CONFIG)
392 pcmcia_release_configuration(link->handle);
393
394 return 0;
395}
396
397static int avma1cs_resume(struct pcmcia_device *dev)
398{
399 dev_link_t *link = dev_to_instance(dev);
400
401 link->state &= ~DEV_SUSPEND;
402 if (link->state & DEV_CONFIG)
403 pcmcia_request_configuration(link->handle, &link->conf);
404
405 return 0;
406}
407
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
Dominik Brodowskic594c122005-06-27 16:28:34 -0700409static struct pcmcia_device_id avma1cs_ids[] = {
410 PCMCIA_DEVICE_PROD_ID12("AVM", "ISDN A", 0x95d42008, 0xadc9d4bb),
411 PCMCIA_DEVICE_PROD_ID12("ISDN", "CARD", 0x8d9761c8, 0x01c5aa7b),
412 PCMCIA_DEVICE_NULL
413};
414MODULE_DEVICE_TABLE(pcmcia, avma1cs_ids);
415
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416static struct pcmcia_driver avma1cs_driver = {
417 .owner = THIS_MODULE,
418 .drv = {
419 .name = "avma1_cs",
420 },
Dominik Brodowskif8cfa612005-11-14 21:25:51 +0100421 .probe = avma1cs_attach,
Dominik Brodowskicc3b4862005-11-14 21:23:14 +0100422 .remove = avma1cs_detach,
Dominik Brodowskic594c122005-06-27 16:28:34 -0700423 .id_table = avma1cs_ids,
Dominik Brodowski98e4c282005-11-14 21:21:18 +0100424 .suspend = avma1cs_suspend,
425 .resume = avma1cs_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426};
427
428/*====================================================================*/
429
430static int __init init_avma1_cs(void)
431{
432 return(pcmcia_register_driver(&avma1cs_driver));
433}
434
435static void __exit exit_avma1_cs(void)
436{
437 pcmcia_unregister_driver(&avma1cs_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438}
439
440module_init(init_avma1_cs);
441module_exit(exit_avma1_cs);