blob: 6d963f9a09ce6318309b800626630dd3e0070d96 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
34/*====================================================================*/
35
36/* Parameters that can be set with 'insmod' */
37
38static int isdnprot = 2;
39
40module_param(isdnprot, int, 0);
41
42/*====================================================================*/
43
44/*
45 The event() function is this driver's Card Services event handler.
46 It will be called by Card Services when an appropriate card status
47 event is received. The config() and release() entry points are
48 used to configure or release a socket, in response to card insertion
49 and ejection events. They are invoked from the skeleton event
50 handler.
51*/
52
Dominik Brodowski15b99ac2006-03-31 17:26:06 +020053static int avma1cs_config(struct pcmcia_device *link);
Dominik Brodowskifba395e2006-03-31 17:21:06 +020054static void avma1cs_release(struct pcmcia_device *link);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56/*
57 The attach() and detach() entry points are used to create and destroy
58 "instances" of the driver, where each instance represents everything
59 needed to manage one actual PCMCIA card.
60*/
61
Dominik Brodowskicc3b4862005-11-14 21:23:14 +010062static void avma1cs_detach(struct pcmcia_device *p_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65/*
66 A linked list of "instances" of the skeleton device. Each actual
67 PCMCIA card corresponds to one device instance, and is described
Dominik Brodowskifba395e2006-03-31 17:21:06 +020068 by one struct pcmcia_device structure (defined in ds.h).
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70 You may not want to use a linked list for this -- for example, the
Dominik Brodowskifba395e2006-03-31 17:21:06 +020071 memory card driver uses an array of struct pcmcia_device pointers, where minor
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 device numbers are used to derive the corresponding array index.
73*/
74
Linus Torvalds1da177e2005-04-16 15:20:36 -070075/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 A driver needs to provide a dev_node_t structure for each device
77 on a card. In some cases, there is only one device per card (for
78 example, ethernet cards, modems). In other cases, there may be
79 many actual or logical devices (SCSI adapters, memory cards with
80 multiple partitions). The dev_node_t structures need to be kept
Dominik Brodowskifba395e2006-03-31 17:21:06 +020081 in a linked list starting at the 'dev' field of a struct pcmcia_device
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 structure. We allocate them in the card's private data structure,
83 because they generally can't be allocated dynamically.
84*/
85
86typedef struct local_info_t {
87 dev_node_t node;
88} local_info_t;
89
90/*======================================================================
91
92 avma1cs_attach() creates an "instance" of the driver, allocating
93 local data structures for one device. The device is registered
94 with Card Services.
95
96 The dev_link structure is initialized, but we don't actually
97 configure the card at this point -- we wait until we receive a
98 card insertion event.
99
100======================================================================*/
101
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200102static int avma1cs_probe(struct pcmcia_device *p_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 local_info_t *local;
Dominik Brodowskif8cfa612005-11-14 21:25:51 +0100105
Dominik Brodowskie773cfe2009-10-24 15:50:13 +0200106 dev_dbg(&p_dev->dev, "avma1cs_attach()\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 /* Allocate space for private device-specific data */
Burman Yan41f96932006-12-08 02:39:35 -0800109 local = kzalloc(sizeof(local_info_t), GFP_KERNEL);
Dominik Brodowskifd238232006-03-05 10:45:09 +0100110 if (!local)
Dominik Brodowskif8cfa612005-11-14 21:25:51 +0100111 return -ENOMEM;
Dominik Brodowskifd238232006-03-05 10:45:09 +0100112
Dominik Brodowskifd238232006-03-05 10:45:09 +0100113 p_dev->priv = local;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
115 /* The io structure describes IO port mapping */
Dominik Brodowskifd238232006-03-05 10:45:09 +0100116 p_dev->io.NumPorts1 = 16;
117 p_dev->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
118 p_dev->io.NumPorts2 = 16;
119 p_dev->io.Attributes2 = IO_DATA_PATH_WIDTH_16;
120 p_dev->io.IOAddrLines = 5;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
122 /* Interrupt setup */
Dominik Brodowskifd238232006-03-05 10:45:09 +0100123 p_dev->irq.Attributes = IRQ_TYPE_EXCLUSIVE;
124 p_dev->irq.Attributes = IRQ_TYPE_DYNAMIC_SHARING|IRQ_FIRST_SHARED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Dominik Brodowskifd238232006-03-05 10:45:09 +0100126 p_dev->irq.IRQInfo1 = IRQ_LEVEL_ID;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
128 /* General socket configuration */
Dominik Brodowskifd238232006-03-05 10:45:09 +0100129 p_dev->conf.Attributes = CONF_ENABLE_IRQ;
130 p_dev->conf.IntType = INT_MEMORY_AND_IO;
131 p_dev->conf.ConfigIndex = 1;
132 p_dev->conf.Present = PRESENT_OPTION;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200134 return avma1cs_config(p_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135} /* avma1cs_attach */
136
137/*======================================================================
138
139 This deletes a driver "instance". The device is de-registered
140 with Card Services. If it has been released, all local data
141 structures are freed. Otherwise, the structures will be freed
142 when the device is released.
143
144======================================================================*/
145
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200146static void avma1cs_detach(struct pcmcia_device *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147{
Dominik Brodowskie773cfe2009-10-24 15:50:13 +0200148 dev_dbg(&link->dev, "avma1cs_detach(0x%p)\n", link);
Dominik Brodowskie2d40962006-03-02 00:09:29 +0100149 avma1cs_release(link);
150 kfree(link->priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151} /* avma1cs_detach */
152
153/*======================================================================
154
155 avma1cs_config() is scheduled to run after a CARD_INSERTION event
156 is received, to configure the PCMCIA socket, and to make the
157 ethernet device available to the system.
158
159======================================================================*/
160
Dominik Brodowski5fcd4da2008-07-29 08:38:55 +0200161static int avma1cs_configcheck(struct pcmcia_device *p_dev,
Dominik Brodowski8e2fc392008-08-02 15:30:31 +0200162 cistpl_cftable_entry_t *cf,
163 cistpl_cftable_entry_t *dflt,
Dominik Brodowskiad913c12008-08-02 16:12:00 +0200164 unsigned int vcc,
Dominik Brodowski8e2fc392008-08-02 15:30:31 +0200165 void *priv_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166{
Dominik Brodowski5fcd4da2008-07-29 08:38:55 +0200167 if (cf->io.nwin <= 0)
168 return -ENODEV;
169
Dominik Brodowski5fcd4da2008-07-29 08:38:55 +0200170 p_dev->io.BasePort1 = cf->io.win[0].base;
171 p_dev->io.NumPorts1 = cf->io.win[0].len;
172 p_dev->io.NumPorts2 = 0;
173 printk(KERN_INFO "avma1_cs: testing i/o %#x-%#x\n",
174 p_dev->io.BasePort1,
175 p_dev->io.BasePort1+p_dev->io.NumPorts1-1);
176 return pcmcia_request_io(p_dev, &p_dev->io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177}
178
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200180static int avma1cs_config(struct pcmcia_device *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 local_info_t *dev;
183 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 char devname[128];
185 IsdnCard_t icard;
186 int busy = 0;
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200187
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 dev = link->priv;
189
Dominik Brodowskie773cfe2009-10-24 15:50:13 +0200190 dev_dbg(&link->dev, "avma1cs_config(0x%p)\n", link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
Dominik Brodowski5fcd4da2008-07-29 08:38:55 +0200192 devname[0] = 0;
193 if (link->prod_id[1])
194 strlcpy(devname, link->prod_id[1], sizeof(devname));
195
196 if (pcmcia_loop_config(link, avma1cs_configcheck, NULL))
197 return -ENODEV;
198
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 /*
201 * allocate an interrupt line
202 */
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200203 i = pcmcia_request_irq(link, &link->irq);
Dominik Brodowski4c89e882008-08-03 10:07:45 +0200204 if (i != 0) {
Dominik Brodowski50db3fd2006-01-15 10:05:19 +0100205 /* undo */
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200206 pcmcia_disable_device(link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 break;
208 }
Dominik Brodowski50db3fd2006-01-15 10:05:19 +0100209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 /*
211 * configure the PCMCIA socket
212 */
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200213 i = pcmcia_request_configuration(link, &link->conf);
Dominik Brodowski4c89e882008-08-03 10:07:45 +0200214 if (i != 0) {
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200215 pcmcia_disable_device(link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 break;
217 }
218
219 } while (0);
220
221 /* At this point, the dev_node_t structure(s) should be
222 initialized and arranged in a linked list at link->dev. */
223
224 strcpy(dev->node.dev_name, "A1");
225 dev->node.major = 45;
226 dev->node.minor = 0;
Dominik Brodowskifd238232006-03-05 10:45:09 +0100227 link->dev_node = &dev->node;
Dominik Brodowskie2d40962006-03-02 00:09:29 +0100228
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 /* If any step failed, release any partially configured state */
230 if (i != 0) {
231 avma1cs_release(link);
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200232 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 }
234
235 printk(KERN_NOTICE "avma1_cs: checking at i/o %#x, irq %d\n",
236 link->io.BasePort1, link->irq.AssignedIRQ);
237
238 icard.para[0] = link->irq.AssignedIRQ;
239 icard.para[1] = link->io.BasePort1;
240 icard.protocol = isdnprot;
241 icard.typ = ISDN_CTYPE_A1_PCMCIA;
242
243 i = hisax_init_pcmcia(link, &busy, &icard);
244 if (i < 0) {
245 printk(KERN_ERR "avma1_cs: failed to initialize AVM A1 PCMCIA %d at i/o %#x\n", i, link->io.BasePort1);
246 avma1cs_release(link);
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200247 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 }
249 dev->node.minor = i;
250
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200251 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252} /* avma1cs_config */
253
254/*======================================================================
255
256 After a card is removed, avma1cs_release() will unregister the net
257 device, and release the PCMCIA configuration. If the device is
258 still open, this will be postponed until it is closed.
259
260======================================================================*/
261
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200262static void avma1cs_release(struct pcmcia_device *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263{
Dominik Brodowski5f2a71f2006-01-15 09:32:39 +0100264 local_info_t *local = link->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
Dominik Brodowskie773cfe2009-10-24 15:50:13 +0200266 dev_dbg(&link->dev, "avma1cs_release(0x%p)\n", link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
Dominik Brodowski5f2a71f2006-01-15 09:32:39 +0100268 /* now unregister function with hisax */
269 HiSax_closecard(local->node.minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200271 pcmcia_disable_device(link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272} /* avma1cs_release */
273
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
Dominik Brodowskic594c122005-06-27 16:28:34 -0700275static struct pcmcia_device_id avma1cs_ids[] = {
276 PCMCIA_DEVICE_PROD_ID12("AVM", "ISDN A", 0x95d42008, 0xadc9d4bb),
277 PCMCIA_DEVICE_PROD_ID12("ISDN", "CARD", 0x8d9761c8, 0x01c5aa7b),
278 PCMCIA_DEVICE_NULL
279};
280MODULE_DEVICE_TABLE(pcmcia, avma1cs_ids);
281
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282static struct pcmcia_driver avma1cs_driver = {
283 .owner = THIS_MODULE,
284 .drv = {
285 .name = "avma1_cs",
286 },
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200287 .probe = avma1cs_probe,
Dominik Brodowskicc3b4862005-11-14 21:23:14 +0100288 .remove = avma1cs_detach,
Dominik Brodowskic594c122005-06-27 16:28:34 -0700289 .id_table = avma1cs_ids,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290};
Dominik Brodowski8661bb52006-03-02 00:02:33 +0100291
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292/*====================================================================*/
293
294static int __init init_avma1_cs(void)
295{
296 return(pcmcia_register_driver(&avma1cs_driver));
297}
298
299static void __exit exit_avma1_cs(void)
300{
301 pcmcia_unregister_driver(&avma1cs_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302}
303
304module_init(init_avma1_cs);
305module_exit(exit_avma1_cs);