[PATCH] isdn: replace kmalloc+memset with kzalloc
[deliverable/linux.git] / drivers / isdn / hisax / avma1_cs.c
CommitLineData
1da177e4
LT
1/*
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
1da177e4
LT
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"
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
15b99ac1 70static int avma1cs_config(struct pcmcia_device *link);
fba395ee 71static void avma1cs_release(struct pcmcia_device *link);
1da177e4
LT
72
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
cc3b4866 79static void avma1cs_detach(struct pcmcia_device *p_dev);
1da177e4 80
1da177e4
LT
81
82/*
83 A linked list of "instances" of the skeleton device. Each actual
84 PCMCIA card corresponds to one device instance, and is described
fba395ee 85 by one struct pcmcia_device structure (defined in ds.h).
1da177e4
LT
86
87 You may not want to use a linked list for this -- for example, the
fba395ee 88 memory card driver uses an array of struct pcmcia_device pointers, where minor
1da177e4
LT
89 device numbers are used to derive the corresponding array index.
90*/
91
1da177e4 92/*
1da177e4
LT
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
fba395ee 98 in a linked list starting at the 'dev' field of a struct pcmcia_device
1da177e4
LT
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
15b99ac1 119static int avma1cs_probe(struct pcmcia_device *p_dev)
1da177e4 120{
1da177e4 121 local_info_t *local;
f8cfa618 122
1da177e4
LT
123 DEBUG(0, "avma1cs_attach()\n");
124
1da177e4 125 /* Allocate space for private device-specific data */
41f96935 126 local = kzalloc(sizeof(local_info_t), GFP_KERNEL);
fd238232 127 if (!local)
f8cfa618 128 return -ENOMEM;
fd238232 129
fd238232 130 p_dev->priv = local;
1da177e4
LT
131
132 /* The io structure describes IO port mapping */
fd238232
DB
133 p_dev->io.NumPorts1 = 16;
134 p_dev->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
135 p_dev->io.NumPorts2 = 16;
136 p_dev->io.Attributes2 = IO_DATA_PATH_WIDTH_16;
137 p_dev->io.IOAddrLines = 5;
1da177e4
LT
138
139 /* Interrupt setup */
fd238232
DB
140 p_dev->irq.Attributes = IRQ_TYPE_EXCLUSIVE;
141 p_dev->irq.Attributes = IRQ_TYPE_DYNAMIC_SHARING|IRQ_FIRST_SHARED;
1da177e4 142
fd238232 143 p_dev->irq.IRQInfo1 = IRQ_LEVEL_ID;
1da177e4
LT
144
145 /* General socket configuration */
fd238232
DB
146 p_dev->conf.Attributes = CONF_ENABLE_IRQ;
147 p_dev->conf.IntType = INT_MEMORY_AND_IO;
148 p_dev->conf.ConfigIndex = 1;
149 p_dev->conf.Present = PRESENT_OPTION;
f8cfa618 150
15b99ac1 151 return avma1cs_config(p_dev);
1da177e4
LT
152} /* avma1cs_attach */
153
154/*======================================================================
155
156 This deletes a driver "instance". The device is de-registered
157 with Card Services. If it has been released, all local data
158 structures are freed. Otherwise, the structures will be freed
159 when the device is released.
160
161======================================================================*/
162
fba395ee 163static void avma1cs_detach(struct pcmcia_device *link)
1da177e4 164{
e2d40963
DB
165 DEBUG(0, "avma1cs_detach(0x%p)\n", link);
166 avma1cs_release(link);
167 kfree(link->priv);
1da177e4
LT
168} /* avma1cs_detach */
169
170/*======================================================================
171
172 avma1cs_config() is scheduled to run after a CARD_INSERTION event
173 is received, to configure the PCMCIA socket, and to make the
174 ethernet device available to the system.
175
176======================================================================*/
177
fba395ee 178static int get_tuple(struct pcmcia_device *handle, tuple_t *tuple,
1da177e4
LT
179 cisparse_t *parse)
180{
181 int i = pcmcia_get_tuple_data(handle, tuple);
182 if (i != CS_SUCCESS) return i;
183 return pcmcia_parse_tuple(handle, tuple, parse);
184}
185
fba395ee 186static int first_tuple(struct pcmcia_device *handle, tuple_t *tuple,
1da177e4
LT
187 cisparse_t *parse)
188{
189 int i = pcmcia_get_first_tuple(handle, tuple);
190 if (i != CS_SUCCESS) return i;
191 return get_tuple(handle, tuple, parse);
192}
193
fba395ee 194static int next_tuple(struct pcmcia_device *handle, tuple_t *tuple,
1da177e4
LT
195 cisparse_t *parse)
196{
197 int i = pcmcia_get_next_tuple(handle, tuple);
198 if (i != CS_SUCCESS) return i;
199 return get_tuple(handle, tuple, parse);
200}
201
15b99ac1 202static int avma1cs_config(struct pcmcia_device *link)
1da177e4 203{
1da177e4
LT
204 tuple_t tuple;
205 cisparse_t parse;
206 cistpl_cftable_entry_t *cf = &parse.cftable_entry;
207 local_info_t *dev;
208 int i;
209 u_char buf[64];
210 char devname[128];
211 IsdnCard_t icard;
212 int busy = 0;
fba395ee 213
1da177e4
LT
214 dev = link->priv;
215
216 DEBUG(0, "avma1cs_config(0x%p)\n", link);
217
1da177e4 218 do {
1da177e4 219 devname[0] = 0;
a9606fd3
DB
220 if (link->prod_id[1])
221 strlcpy(devname, link->prod_id[1], sizeof(devname));
222
1da177e4
LT
223 /*
224 * find IO port
225 */
226 tuple.TupleData = (cisdata_t *)buf;
227 tuple.TupleOffset = 0; tuple.TupleDataMax = 255;
228 tuple.Attributes = 0;
229 tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
fba395ee 230 i = first_tuple(link, &tuple, &parse);
1da177e4
LT
231 while (i == CS_SUCCESS) {
232 if (cf->io.nwin > 0) {
233 link->conf.ConfigIndex = cf->index;
234 link->io.BasePort1 = cf->io.win[0].base;
235 link->io.NumPorts1 = cf->io.win[0].len;
236 link->io.NumPorts2 = 0;
237 printk(KERN_INFO "avma1_cs: testing i/o %#x-%#x\n",
238 link->io.BasePort1,
239 link->io.BasePort1+link->io.NumPorts1 - 1);
fba395ee 240 i = pcmcia_request_io(link, &link->io);
1da177e4
LT
241 if (i == CS_SUCCESS) goto found_port;
242 }
fba395ee 243 i = next_tuple(link, &tuple, &parse);
1da177e4
LT
244 }
245
246found_port:
247 if (i != CS_SUCCESS) {
fba395ee 248 cs_error(link, RequestIO, i);
1da177e4
LT
249 break;
250 }
251
252 /*
253 * allocate an interrupt line
254 */
fba395ee 255 i = pcmcia_request_irq(link, &link->irq);
1da177e4 256 if (i != CS_SUCCESS) {
fba395ee 257 cs_error(link, RequestIRQ, i);
50db3fdb 258 /* undo */
fba395ee 259 pcmcia_disable_device(link);
1da177e4
LT
260 break;
261 }
50db3fdb 262
1da177e4
LT
263 /*
264 * configure the PCMCIA socket
265 */
fba395ee 266 i = pcmcia_request_configuration(link, &link->conf);
1da177e4 267 if (i != CS_SUCCESS) {
fba395ee
DB
268 cs_error(link, RequestConfiguration, i);
269 pcmcia_disable_device(link);
1da177e4
LT
270 break;
271 }
272
273 } while (0);
274
275 /* At this point, the dev_node_t structure(s) should be
276 initialized and arranged in a linked list at link->dev. */
277
278 strcpy(dev->node.dev_name, "A1");
279 dev->node.major = 45;
280 dev->node.minor = 0;
fd238232 281 link->dev_node = &dev->node;
e2d40963 282
1da177e4
LT
283 /* If any step failed, release any partially configured state */
284 if (i != 0) {
285 avma1cs_release(link);
15b99ac1 286 return -ENODEV;
1da177e4
LT
287 }
288
289 printk(KERN_NOTICE "avma1_cs: checking at i/o %#x, irq %d\n",
290 link->io.BasePort1, link->irq.AssignedIRQ);
291
292 icard.para[0] = link->irq.AssignedIRQ;
293 icard.para[1] = link->io.BasePort1;
294 icard.protocol = isdnprot;
295 icard.typ = ISDN_CTYPE_A1_PCMCIA;
296
297 i = hisax_init_pcmcia(link, &busy, &icard);
298 if (i < 0) {
299 printk(KERN_ERR "avma1_cs: failed to initialize AVM A1 PCMCIA %d at i/o %#x\n", i, link->io.BasePort1);
300 avma1cs_release(link);
15b99ac1 301 return -ENODEV;
1da177e4
LT
302 }
303 dev->node.minor = i;
304
15b99ac1 305 return 0;
1da177e4
LT
306} /* avma1cs_config */
307
308/*======================================================================
309
310 After a card is removed, avma1cs_release() will unregister the net
311 device, and release the PCMCIA configuration. If the device is
312 still open, this will be postponed until it is closed.
313
314======================================================================*/
315
fba395ee 316static void avma1cs_release(struct pcmcia_device *link)
1da177e4 317{
5f2a71fc 318 local_info_t *local = link->priv;
1da177e4 319
5f2a71fc 320 DEBUG(0, "avma1cs_release(0x%p)\n", link);
1da177e4 321
5f2a71fc
DB
322 /* now unregister function with hisax */
323 HiSax_closecard(local->node.minor);
1da177e4 324
fba395ee 325 pcmcia_disable_device(link);
1da177e4
LT
326} /* avma1cs_release */
327
1da177e4 328
c594c12c
DB
329static struct pcmcia_device_id avma1cs_ids[] = {
330 PCMCIA_DEVICE_PROD_ID12("AVM", "ISDN A", 0x95d42008, 0xadc9d4bb),
331 PCMCIA_DEVICE_PROD_ID12("ISDN", "CARD", 0x8d9761c8, 0x01c5aa7b),
332 PCMCIA_DEVICE_NULL
333};
334MODULE_DEVICE_TABLE(pcmcia, avma1cs_ids);
335
1da177e4
LT
336static struct pcmcia_driver avma1cs_driver = {
337 .owner = THIS_MODULE,
338 .drv = {
339 .name = "avma1_cs",
340 },
15b99ac1 341 .probe = avma1cs_probe,
cc3b4866 342 .remove = avma1cs_detach,
c594c12c 343 .id_table = avma1cs_ids,
1da177e4 344};
8661bb5b 345
1da177e4
LT
346/*====================================================================*/
347
348static int __init init_avma1_cs(void)
349{
350 return(pcmcia_register_driver(&avma1cs_driver));
351}
352
353static void __exit exit_avma1_cs(void)
354{
355 pcmcia_unregister_driver(&avma1cs_driver);
1da177e4
LT
356}
357
358module_init(init_avma1_cs);
359module_exit(exit_avma1_cs);
This page took 0.199657 seconds and 5 git commands to generate.