blob: 23560c897ec361d59526ffa1d36fad6264ddadcb [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/ptrace.h>
18#include <linux/slab.h>
19#include <linux/string.h>
20#include <asm/io.h>
21#include <asm/system.h>
22
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <pcmcia/cs_types.h>
24#include <pcmcia/cs.h>
25#include <pcmcia/cistpl.h>
26#include <pcmcia/ds.h>
27#include "hisax_cfg.h"
28
29MODULE_DESCRIPTION("ISDN4Linux: PCMCIA client driver for AVM A1/Fritz!PCMCIA cards");
30MODULE_AUTHOR("Carsten Paeth");
31MODULE_LICENSE("GPL");
32
33/*
34 All the PCMCIA modules use PCMCIA_DEBUG to control debugging. If
35 you do not define PCMCIA_DEBUG at all, all the debug code will be
36 left out. If you compile with PCMCIA_DEBUG=0, the debug code will
37 be present but disabled -- but it can then be enabled for specific
38 modules at load time with a 'pc_debug=#' option to insmod.
39*/
40#ifdef PCMCIA_DEBUG
41static int pc_debug = PCMCIA_DEBUG;
42module_param(pc_debug, int, 0);
43#define DEBUG(n, args...) if (pc_debug>(n)) printk(KERN_DEBUG args);
44static char *version =
45"avma1_cs.c 1.00 1998/01/23 10:00:00 (Carsten Paeth)";
46#else
47#define DEBUG(n, args...)
48#endif
49
50/*====================================================================*/
51
52/* Parameters that can be set with 'insmod' */
53
54static int isdnprot = 2;
55
56module_param(isdnprot, int, 0);
57
58/*====================================================================*/
59
60/*
61 The event() function is this driver's Card Services event handler.
62 It will be called by Card Services when an appropriate card status
63 event is received. The config() and release() entry points are
64 used to configure or release a socket, in response to card insertion
65 and ejection events. They are invoked from the skeleton event
66 handler.
67*/
68
Dominik Brodowski15b99ac2006-03-31 17:26:06 +020069static int avma1cs_config(struct pcmcia_device *link);
Dominik Brodowskifba395e2006-03-31 17:21:06 +020070static void avma1cs_release(struct pcmcia_device *link);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72/*
73 The attach() and detach() entry points are used to create and destroy
74 "instances" of the driver, where each instance represents everything
75 needed to manage one actual PCMCIA card.
76*/
77
Dominik Brodowskicc3b4862005-11-14 21:23:14 +010078static void avma1cs_detach(struct pcmcia_device *p_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81/*
82 A linked list of "instances" of the skeleton device. Each actual
83 PCMCIA card corresponds to one device instance, and is described
Dominik Brodowskifba395e2006-03-31 17:21:06 +020084 by one struct pcmcia_device structure (defined in ds.h).
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
86 You may not want to use a linked list for this -- for example, the
Dominik Brodowskifba395e2006-03-31 17:21:06 +020087 memory card driver uses an array of struct pcmcia_device pointers, where minor
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 device numbers are used to derive the corresponding array index.
89*/
90
Linus Torvalds1da177e2005-04-16 15:20:36 -070091/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 A driver needs to provide a dev_node_t structure for each device
93 on a card. In some cases, there is only one device per card (for
94 example, ethernet cards, modems). In other cases, there may be
95 many actual or logical devices (SCSI adapters, memory cards with
96 multiple partitions). The dev_node_t structures need to be kept
Dominik Brodowskifba395e2006-03-31 17:21:06 +020097 in a linked list starting at the 'dev' field of a struct pcmcia_device
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 structure. We allocate them in the card's private data structure,
99 because they generally can't be allocated dynamically.
100*/
101
102typedef struct local_info_t {
103 dev_node_t node;
104} local_info_t;
105
106/*======================================================================
107
108 avma1cs_attach() creates an "instance" of the driver, allocating
109 local data structures for one device. The device is registered
110 with Card Services.
111
112 The dev_link structure is initialized, but we don't actually
113 configure the card at this point -- we wait until we receive a
114 card insertion event.
115
116======================================================================*/
117
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200118static int avma1cs_probe(struct pcmcia_device *p_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 local_info_t *local;
Dominik Brodowskif8cfa612005-11-14 21:25:51 +0100121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 DEBUG(0, "avma1cs_attach()\n");
123
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 /* Allocate space for private device-specific data */
Burman Yan41f96932006-12-08 02:39:35 -0800125 local = kzalloc(sizeof(local_info_t), GFP_KERNEL);
Dominik Brodowskifd238232006-03-05 10:45:09 +0100126 if (!local)
Dominik Brodowskif8cfa612005-11-14 21:25:51 +0100127 return -ENOMEM;
Dominik Brodowskifd238232006-03-05 10:45:09 +0100128
Dominik Brodowskifd238232006-03-05 10:45:09 +0100129 p_dev->priv = local;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
131 /* The io structure describes IO port mapping */
Dominik Brodowskifd238232006-03-05 10:45:09 +0100132 p_dev->io.NumPorts1 = 16;
133 p_dev->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
134 p_dev->io.NumPorts2 = 16;
135 p_dev->io.Attributes2 = IO_DATA_PATH_WIDTH_16;
136 p_dev->io.IOAddrLines = 5;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
138 /* Interrupt setup */
Dominik Brodowskifd238232006-03-05 10:45:09 +0100139 p_dev->irq.Attributes = IRQ_TYPE_EXCLUSIVE;
140 p_dev->irq.Attributes = IRQ_TYPE_DYNAMIC_SHARING|IRQ_FIRST_SHARED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Dominik Brodowskifd238232006-03-05 10:45:09 +0100142 p_dev->irq.IRQInfo1 = IRQ_LEVEL_ID;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
144 /* General socket configuration */
Dominik Brodowskifd238232006-03-05 10:45:09 +0100145 p_dev->conf.Attributes = CONF_ENABLE_IRQ;
146 p_dev->conf.IntType = INT_MEMORY_AND_IO;
147 p_dev->conf.ConfigIndex = 1;
148 p_dev->conf.Present = PRESENT_OPTION;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200150 return avma1cs_config(p_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151} /* avma1cs_attach */
152
153/*======================================================================
154
155 This deletes a driver "instance". The device is de-registered
156 with Card Services. If it has been released, all local data
157 structures are freed. Otherwise, the structures will be freed
158 when the device is released.
159
160======================================================================*/
161
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200162static void avma1cs_detach(struct pcmcia_device *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163{
Dominik Brodowskie2d40962006-03-02 00:09:29 +0100164 DEBUG(0, "avma1cs_detach(0x%p)\n", link);
165 avma1cs_release(link);
166 kfree(link->priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167} /* avma1cs_detach */
168
169/*======================================================================
170
171 avma1cs_config() is scheduled to run after a CARD_INSERTION event
172 is received, to configure the PCMCIA socket, and to make the
173 ethernet device available to the system.
174
175======================================================================*/
176
Dominik Brodowski5fcd4da2008-07-29 08:38:55 +0200177static int avma1cs_configcheck(struct pcmcia_device *p_dev,
Dominik Brodowski8e2fc392008-08-02 15:30:31 +0200178 cistpl_cftable_entry_t *cf,
179 cistpl_cftable_entry_t *dflt,
Dominik Brodowskiad913c12008-08-02 16:12:00 +0200180 unsigned int vcc,
Dominik Brodowski8e2fc392008-08-02 15:30:31 +0200181 void *priv_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182{
Dominik Brodowski5fcd4da2008-07-29 08:38:55 +0200183 if (cf->io.nwin <= 0)
184 return -ENODEV;
185
Dominik Brodowski5fcd4da2008-07-29 08:38:55 +0200186 p_dev->io.BasePort1 = cf->io.win[0].base;
187 p_dev->io.NumPorts1 = cf->io.win[0].len;
188 p_dev->io.NumPorts2 = 0;
189 printk(KERN_INFO "avma1_cs: testing i/o %#x-%#x\n",
190 p_dev->io.BasePort1,
191 p_dev->io.BasePort1+p_dev->io.NumPorts1-1);
192 return pcmcia_request_io(p_dev, &p_dev->io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193}
194
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200196static int avma1cs_config(struct pcmcia_device *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 local_info_t *dev;
199 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 char devname[128];
201 IsdnCard_t icard;
202 int busy = 0;
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200203
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 dev = link->priv;
205
206 DEBUG(0, "avma1cs_config(0x%p)\n", link);
207
Dominik Brodowski5fcd4da2008-07-29 08:38:55 +0200208 devname[0] = 0;
209 if (link->prod_id[1])
210 strlcpy(devname, link->prod_id[1], sizeof(devname));
211
212 if (pcmcia_loop_config(link, avma1cs_configcheck, NULL))
213 return -ENODEV;
214
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 /*
217 * allocate an interrupt line
218 */
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200219 i = pcmcia_request_irq(link, &link->irq);
Dominik Brodowski4c89e882008-08-03 10:07:45 +0200220 if (i != 0) {
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200221 cs_error(link, RequestIRQ, i);
Dominik Brodowski50db3fd2006-01-15 10:05:19 +0100222 /* undo */
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200223 pcmcia_disable_device(link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 break;
225 }
Dominik Brodowski50db3fd2006-01-15 10:05:19 +0100226
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 /*
228 * configure the PCMCIA socket
229 */
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200230 i = pcmcia_request_configuration(link, &link->conf);
Dominik Brodowski4c89e882008-08-03 10:07:45 +0200231 if (i != 0) {
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200232 cs_error(link, RequestConfiguration, i);
233 pcmcia_disable_device(link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 break;
235 }
236
237 } while (0);
238
239 /* At this point, the dev_node_t structure(s) should be
240 initialized and arranged in a linked list at link->dev. */
241
242 strcpy(dev->node.dev_name, "A1");
243 dev->node.major = 45;
244 dev->node.minor = 0;
Dominik Brodowskifd238232006-03-05 10:45:09 +0100245 link->dev_node = &dev->node;
Dominik Brodowskie2d40962006-03-02 00:09:29 +0100246
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 /* If any step failed, release any partially configured state */
248 if (i != 0) {
249 avma1cs_release(link);
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200250 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 }
252
253 printk(KERN_NOTICE "avma1_cs: checking at i/o %#x, irq %d\n",
254 link->io.BasePort1, link->irq.AssignedIRQ);
255
256 icard.para[0] = link->irq.AssignedIRQ;
257 icard.para[1] = link->io.BasePort1;
258 icard.protocol = isdnprot;
259 icard.typ = ISDN_CTYPE_A1_PCMCIA;
260
261 i = hisax_init_pcmcia(link, &busy, &icard);
262 if (i < 0) {
263 printk(KERN_ERR "avma1_cs: failed to initialize AVM A1 PCMCIA %d at i/o %#x\n", i, link->io.BasePort1);
264 avma1cs_release(link);
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200265 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 }
267 dev->node.minor = i;
268
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200269 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270} /* avma1cs_config */
271
272/*======================================================================
273
274 After a card is removed, avma1cs_release() will unregister the net
275 device, and release the PCMCIA configuration. If the device is
276 still open, this will be postponed until it is closed.
277
278======================================================================*/
279
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200280static void avma1cs_release(struct pcmcia_device *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281{
Dominik Brodowski5f2a71f2006-01-15 09:32:39 +0100282 local_info_t *local = link->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
Dominik Brodowski5f2a71f2006-01-15 09:32:39 +0100284 DEBUG(0, "avma1cs_release(0x%p)\n", link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
Dominik Brodowski5f2a71f2006-01-15 09:32:39 +0100286 /* now unregister function with hisax */
287 HiSax_closecard(local->node.minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200289 pcmcia_disable_device(link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290} /* avma1cs_release */
291
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
Dominik Brodowskic594c122005-06-27 16:28:34 -0700293static struct pcmcia_device_id avma1cs_ids[] = {
294 PCMCIA_DEVICE_PROD_ID12("AVM", "ISDN A", 0x95d42008, 0xadc9d4bb),
295 PCMCIA_DEVICE_PROD_ID12("ISDN", "CARD", 0x8d9761c8, 0x01c5aa7b),
296 PCMCIA_DEVICE_NULL
297};
298MODULE_DEVICE_TABLE(pcmcia, avma1cs_ids);
299
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300static struct pcmcia_driver avma1cs_driver = {
301 .owner = THIS_MODULE,
302 .drv = {
303 .name = "avma1_cs",
304 },
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200305 .probe = avma1cs_probe,
Dominik Brodowskicc3b4862005-11-14 21:23:14 +0100306 .remove = avma1cs_detach,
Dominik Brodowskic594c122005-06-27 16:28:34 -0700307 .id_table = avma1cs_ids,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308};
Dominik Brodowski8661bb52006-03-02 00:02:33 +0100309
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310/*====================================================================*/
311
312static int __init init_avma1_cs(void)
313{
314 return(pcmcia_register_driver(&avma1cs_driver));
315}
316
317static void __exit exit_avma1_cs(void)
318{
319 pcmcia_unregister_driver(&avma1cs_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320}
321
322module_init(init_avma1_cs);
323module_exit(exit_avma1_cs);