blob: ac28e3278ad91567ff710d572afce06b7ecfb31c [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
Dominik Brodowski15b99ac2006-03-31 17:26:06 +020070static int avma1cs_config(struct pcmcia_device *link);
Dominik Brodowskifba395e2006-03-31 17:21:06 +020071static void avma1cs_release(struct pcmcia_device *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
Dominik Brodowskifba395e2006-03-31 17:21:06 +020085 by one struct pcmcia_device structure (defined in ds.h).
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
87 You may not want to use a linked list for this -- for example, the
Dominik Brodowskifba395e2006-03-31 17:21:06 +020088 memory card driver uses an array of struct pcmcia_device pointers, where minor
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 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
Dominik Brodowskifba395e2006-03-31 17:21:06 +020098 in a linked list starting at the 'dev' field of a struct pcmcia_device
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 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 Brodowski15b99ac2006-03-31 17:26:06 +0200119static int avma1cs_probe(struct pcmcia_device *p_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 local_info_t *local;
Dominik Brodowskif8cfa612005-11-14 21:25:51 +0100122
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 DEBUG(0, "avma1cs_attach()\n");
124
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 /* Allocate space for private device-specific data */
126 local = kmalloc(sizeof(local_info_t), GFP_KERNEL);
Dominik Brodowskifd238232006-03-05 10:45:09 +0100127 if (!local)
Dominik Brodowskif8cfa612005-11-14 21:25:51 +0100128 return -ENOMEM;
Dominik Brodowskifd238232006-03-05 10:45:09 +0100129
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 memset(local, 0, sizeof(local_info_t));
Dominik Brodowskifd238232006-03-05 10:45:09 +0100131 p_dev->priv = local;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
133 /* The io structure describes IO port mapping */
Dominik Brodowskifd238232006-03-05 10:45:09 +0100134 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
140 /* Interrupt setup */
Dominik Brodowskifd238232006-03-05 10:45:09 +0100141 p_dev->irq.Attributes = IRQ_TYPE_EXCLUSIVE;
142 p_dev->irq.Attributes = IRQ_TYPE_DYNAMIC_SHARING|IRQ_FIRST_SHARED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
Dominik Brodowskifd238232006-03-05 10:45:09 +0100144 p_dev->irq.IRQInfo1 = IRQ_LEVEL_ID;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
146 /* General socket configuration */
Dominik Brodowskifd238232006-03-05 10:45:09 +0100147 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200152 return avma1cs_config(p_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153} /* avma1cs_attach */
154
155/*======================================================================
156
157 This deletes a driver "instance". The device is de-registered
158 with Card Services. If it has been released, all local data
159 structures are freed. Otherwise, the structures will be freed
160 when the device is released.
161
162======================================================================*/
163
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200164static void avma1cs_detach(struct pcmcia_device *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165{
Dominik Brodowskie2d40962006-03-02 00:09:29 +0100166 DEBUG(0, "avma1cs_detach(0x%p)\n", link);
167 avma1cs_release(link);
168 kfree(link->priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169} /* avma1cs_detach */
170
171/*======================================================================
172
173 avma1cs_config() is scheduled to run after a CARD_INSERTION event
174 is received, to configure the PCMCIA socket, and to make the
175 ethernet device available to the system.
176
177======================================================================*/
178
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200179static int get_tuple(struct pcmcia_device *handle, tuple_t *tuple,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 cisparse_t *parse)
181{
182 int i = pcmcia_get_tuple_data(handle, tuple);
183 if (i != CS_SUCCESS) return i;
184 return pcmcia_parse_tuple(handle, tuple, parse);
185}
186
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200187static int first_tuple(struct pcmcia_device *handle, tuple_t *tuple,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 cisparse_t *parse)
189{
190 int i = pcmcia_get_first_tuple(handle, tuple);
191 if (i != CS_SUCCESS) return i;
192 return get_tuple(handle, tuple, parse);
193}
194
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200195static int next_tuple(struct pcmcia_device *handle, tuple_t *tuple,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 cisparse_t *parse)
197{
198 int i = pcmcia_get_next_tuple(handle, tuple);
199 if (i != CS_SUCCESS) return i;
200 return get_tuple(handle, tuple, parse);
201}
202
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200203static int avma1cs_config(struct pcmcia_device *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 tuple_t tuple;
206 cisparse_t parse;
207 cistpl_cftable_entry_t *cf = &parse.cftable_entry;
208 local_info_t *dev;
209 int i;
210 u_char buf[64];
211 char devname[128];
212 IsdnCard_t icard;
213 int busy = 0;
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200214
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 dev = link->priv;
216
217 DEBUG(0, "avma1cs_config(0x%p)\n", link);
218
219 /*
220 This reads the card's CONFIG tuple to find its configuration
221 registers.
222 */
223 do {
224 tuple.DesiredTuple = CISTPL_CONFIG;
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200225 i = pcmcia_get_first_tuple(link, &tuple);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 if (i != CS_SUCCESS) break;
227 tuple.TupleData = buf;
228 tuple.TupleDataMax = 64;
229 tuple.TupleOffset = 0;
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200230 i = pcmcia_get_tuple_data(link, &tuple);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 if (i != CS_SUCCESS) break;
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200232 i = pcmcia_parse_tuple(link, &tuple, &parse);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 if (i != CS_SUCCESS) break;
234 link->conf.ConfigBase = parse.config.base;
235 } while (0);
236 if (i != CS_SUCCESS) {
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200237 cs_error(link, ParseTuple, i);
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200238 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
241 do {
242
243 tuple.Attributes = 0;
244 tuple.TupleData = buf;
245 tuple.TupleDataMax = 254;
246 tuple.TupleOffset = 0;
247 tuple.DesiredTuple = CISTPL_VERS_1;
248
249 devname[0] = 0;
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200250 if( !first_tuple(link, &tuple, &parse) && parse.version_1.ns > 1 ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 strlcpy(devname,parse.version_1.str + parse.version_1.ofs[1],
252 sizeof(devname));
253 }
254 /*
255 * find IO port
256 */
257 tuple.TupleData = (cisdata_t *)buf;
258 tuple.TupleOffset = 0; tuple.TupleDataMax = 255;
259 tuple.Attributes = 0;
260 tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200261 i = first_tuple(link, &tuple, &parse);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 while (i == CS_SUCCESS) {
263 if (cf->io.nwin > 0) {
264 link->conf.ConfigIndex = cf->index;
265 link->io.BasePort1 = cf->io.win[0].base;
266 link->io.NumPorts1 = cf->io.win[0].len;
267 link->io.NumPorts2 = 0;
268 printk(KERN_INFO "avma1_cs: testing i/o %#x-%#x\n",
269 link->io.BasePort1,
270 link->io.BasePort1+link->io.NumPorts1 - 1);
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200271 i = pcmcia_request_io(link, &link->io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 if (i == CS_SUCCESS) goto found_port;
273 }
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200274 i = next_tuple(link, &tuple, &parse);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 }
276
277found_port:
278 if (i != CS_SUCCESS) {
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200279 cs_error(link, RequestIO, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 break;
281 }
282
283 /*
284 * allocate an interrupt line
285 */
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200286 i = pcmcia_request_irq(link, &link->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 if (i != CS_SUCCESS) {
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200288 cs_error(link, RequestIRQ, i);
Dominik Brodowski50db3fd2006-01-15 10:05:19 +0100289 /* undo */
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200290 pcmcia_disable_device(link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 break;
292 }
Dominik Brodowski50db3fd2006-01-15 10:05:19 +0100293
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 /*
295 * configure the PCMCIA socket
296 */
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200297 i = pcmcia_request_configuration(link, &link->conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 if (i != CS_SUCCESS) {
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200299 cs_error(link, RequestConfiguration, i);
300 pcmcia_disable_device(link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 break;
302 }
303
304 } while (0);
305
306 /* At this point, the dev_node_t structure(s) should be
307 initialized and arranged in a linked list at link->dev. */
308
309 strcpy(dev->node.dev_name, "A1");
310 dev->node.major = 45;
311 dev->node.minor = 0;
Dominik Brodowskifd238232006-03-05 10:45:09 +0100312 link->dev_node = &dev->node;
Dominik Brodowskie2d40962006-03-02 00:09:29 +0100313
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 /* If any step failed, release any partially configured state */
315 if (i != 0) {
316 avma1cs_release(link);
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200317 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 }
319
320 printk(KERN_NOTICE "avma1_cs: checking at i/o %#x, irq %d\n",
321 link->io.BasePort1, link->irq.AssignedIRQ);
322
323 icard.para[0] = link->irq.AssignedIRQ;
324 icard.para[1] = link->io.BasePort1;
325 icard.protocol = isdnprot;
326 icard.typ = ISDN_CTYPE_A1_PCMCIA;
327
328 i = hisax_init_pcmcia(link, &busy, &icard);
329 if (i < 0) {
330 printk(KERN_ERR "avma1_cs: failed to initialize AVM A1 PCMCIA %d at i/o %#x\n", i, link->io.BasePort1);
331 avma1cs_release(link);
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200332 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 }
334 dev->node.minor = i;
335
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200336 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337} /* avma1cs_config */
338
339/*======================================================================
340
341 After a card is removed, avma1cs_release() will unregister the net
342 device, and release the PCMCIA configuration. If the device is
343 still open, this will be postponed until it is closed.
344
345======================================================================*/
346
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200347static void avma1cs_release(struct pcmcia_device *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348{
Dominik Brodowski5f2a71f2006-01-15 09:32:39 +0100349 local_info_t *local = link->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
Dominik Brodowski5f2a71f2006-01-15 09:32:39 +0100351 DEBUG(0, "avma1cs_release(0x%p)\n", link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
Dominik Brodowski5f2a71f2006-01-15 09:32:39 +0100353 /* now unregister function with hisax */
354 HiSax_closecard(local->node.minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200356 pcmcia_disable_device(link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357} /* avma1cs_release */
358
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
Dominik Brodowskic594c122005-06-27 16:28:34 -0700360static struct pcmcia_device_id avma1cs_ids[] = {
361 PCMCIA_DEVICE_PROD_ID12("AVM", "ISDN A", 0x95d42008, 0xadc9d4bb),
362 PCMCIA_DEVICE_PROD_ID12("ISDN", "CARD", 0x8d9761c8, 0x01c5aa7b),
363 PCMCIA_DEVICE_NULL
364};
365MODULE_DEVICE_TABLE(pcmcia, avma1cs_ids);
366
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367static struct pcmcia_driver avma1cs_driver = {
368 .owner = THIS_MODULE,
369 .drv = {
370 .name = "avma1_cs",
371 },
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200372 .probe = avma1cs_probe,
Dominik Brodowskicc3b4862005-11-14 21:23:14 +0100373 .remove = avma1cs_detach,
Dominik Brodowskic594c122005-06-27 16:28:34 -0700374 .id_table = avma1cs_ids,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375};
Dominik Brodowski8661bb52006-03-02 00:02:33 +0100376
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377/*====================================================================*/
378
379static int __init init_avma1_cs(void)
380{
381 return(pcmcia_register_driver(&avma1cs_driver));
382}
383
384static void __exit exit_avma1_cs(void)
385{
386 pcmcia_unregister_driver(&avma1cs_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387}
388
389module_init(init_avma1_cs);
390module_exit(exit_avma1_cs);