3036831f5ad57dd9499701257a7cf53ad79852ac
[linux-flexiantxendom0-3.2.10.git] / drivers / isdn / avmb1 / b1isa.c
1 /*
2  * $Id: b1isa.c,v 1.10.6.2 2001/02/16 16:43:23 kai Exp $
3  * 
4  * Module for AVM B1 ISA-card.
5  * 
6  * (c) Copyright 1999 by Carsten Paeth (calle@calle.in-berlin.de)
7  * 
8  * $Log: b1isa.c,v $
9  * Revision 1.10.6.2  2001/02/16 16:43:23  kai
10  * Changes from -ac16, little bug fixes, typos and the like
11  *
12  * Revision 1.10.6.1  2001/02/13 11:43:29  kai
13  * more compatility changes for 2.2.19
14  *
15  * Revision 1.10  2000/11/23 20:45:14  kai
16  * fixed module_init/exit stuff
17  * Note: compiled-in kernel doesn't work pre 2.2.18 anymore.
18  *
19  * Revision 1.9  2000/11/01 14:05:02  calle
20  * - use module_init/module_exit from linux/init.h.
21  * - all static struct variables are initialized with "membername:" now.
22  * - avm_cs.c, let it work with newer pcmcia-cs.
23  *
24  * Revision 1.8  2000/04/03 13:29:24  calle
25  * make Tim Waugh happy (module unload races in 2.3.99-pre3).
26  * no real problem there, but now it is much cleaner ...
27  *
28  * Revision 1.7  2000/02/02 18:36:03  calle
29  * - Modules are now locked while init_module is running
30  * - fixed problem with memory mapping if address is not aligned
31  *
32  * Revision 1.6  2000/01/25 14:37:39  calle
33  * new message after successful detection including card revision and
34  * used resources.
35  *
36  * Revision 1.5  1999/11/05 16:38:01  calle
37  * Cleanups before kernel 2.4:
38  * - Changed all messages to use card->name or driver->name instead of
39  *   constant string.
40  * - Moved some data from struct avmcard into new struct avmctrl_info.
41  *   Changed all lowlevel capi driver to match the new structur.
42  *
43  * Revision 1.4  1999/08/22 20:26:24  calle
44  * backported changes from kernel 2.3.14:
45  * - several #include "config.h" gone, others come.
46  * - "struct device" changed to "struct net_device" in 2.3.14, added a
47  *   define in isdn_compat.h for older kernel versions.
48  *
49  * Revision 1.3  1999/07/09 15:05:40  keil
50  * compat.h is now isdn_compat.h
51  *
52  * Revision 1.2  1999/07/05 15:09:49  calle
53  * - renamed "appl_release" to "appl_released".
54  * - version und profile data now cleared on controller reset
55  * - extended /proc interface, to allow driver and controller specific
56  *   informations to include by driver hackers.
57  *
58  * Revision 1.1  1999/07/01 15:26:27  calle
59  * complete new version (I love it):
60  * + new hardware independed "capi_driver" interface that will make it easy to:
61  *   - support other controllers with CAPI-2.0 (i.e. USB Controller)
62  *   - write a CAPI-2.0 for the passive cards
63  *   - support serial link CAPI-2.0 boxes.
64  * + wrote "capi_driver" for all supported cards.
65  * + "capi_driver" (supported cards) now have to be configured with
66  *   make menuconfig, in the past all supported cards where included
67  *   at once.
68  * + new and better informations in /proc/capi/
69  * + new ioctl to switch trace of capi messages per controller
70  *   using "avmcapictrl trace [contr] on|off|...."
71  * + complete testcircle with all supported cards and also the
72  *   PCMCIA cards (now patch for pcmcia-cs-3.0.13 needed) done.
73  *
74  *
75  */
76
77 #include <linux/module.h>
78 #include <linux/kernel.h>
79 #include <linux/skbuff.h>
80 #include <linux/delay.h>
81 #include <linux/mm.h>
82 #include <linux/interrupt.h>
83 #include <linux/ioport.h>
84 #include <linux/capi.h>
85 #include <linux/init.h>
86 #include <asm/io.h>
87 #include "capicmd.h"
88 #include "capiutil.h"
89 #include "capilli.h"
90 #include "avmcard.h"
91
92 static char *revision = "$Revision: 1.10.6.2 $";
93
94 /* ------------------------------------------------------------- */
95
96 MODULE_AUTHOR("Carsten Paeth <calle@calle.in-berlin.de>");
97
98 /* ------------------------------------------------------------- */
99
100 static void b1isa_interrupt(int interrupt, void *devptr, struct pt_regs *regs)
101 {
102         avmcard *card;
103
104         card = (avmcard *) devptr;
105
106         if (!card) {
107                 printk(KERN_WARNING "b1_interrupt: wrong device\n");
108                 return;
109         }
110         if (card->interrupt) {
111                 printk(KERN_ERR "b1_interrupt: reentering interrupt hander (%s)\n", card->name);
112                 return;
113         }
114
115         card->interrupt = 1;
116
117         b1_handle_interrupt(card);
118
119         card->interrupt = 0;
120 }
121 /* ------------------------------------------------------------- */
122
123 static struct capi_driver_interface *di;
124
125 /* ------------------------------------------------------------- */
126
127 static void b1isa_remove_ctr(struct capi_ctr *ctrl)
128 {
129         avmctrl_info *cinfo = (avmctrl_info *)(ctrl->driverdata);
130         avmcard *card = cinfo->card;
131         unsigned int port = card->port;
132
133         b1_reset(port);
134         b1_reset(port);
135
136         di->detach_ctr(ctrl);
137         free_irq(card->irq, card);
138         release_region(card->port, AVMB1_PORTLEN);
139         kfree(card->ctrlinfo);
140         kfree(card);
141
142         MOD_DEC_USE_COUNT;
143 }
144
145 /* ------------------------------------------------------------- */
146
147 static int b1isa_add_card(struct capi_driver *driver, struct capicardparams *p)
148 {
149         avmctrl_info *cinfo;
150         avmcard *card;
151         int retval;
152
153         MOD_INC_USE_COUNT;
154
155         card = (avmcard *) kmalloc(sizeof(avmcard), GFP_ATOMIC);
156
157         if (!card) {
158                 printk(KERN_WARNING "b1isa: no memory.\n");
159                 MOD_DEC_USE_COUNT;
160                 return -ENOMEM;
161         }
162         memset(card, 0, sizeof(avmcard));
163         cinfo = (avmctrl_info *) kmalloc(sizeof(avmctrl_info), GFP_ATOMIC);
164         if (!cinfo) {
165                 printk(KERN_WARNING "b1isa: no memory.\n");
166                 kfree(card);
167                 MOD_DEC_USE_COUNT;
168                 return -ENOMEM;
169         }
170         memset(cinfo, 0, sizeof(avmctrl_info));
171         card->ctrlinfo = cinfo;
172         cinfo->card = card;
173         sprintf(card->name, "b1isa-%x", p->port);
174         card->port = p->port;
175         card->irq = p->irq;
176         card->cardtype = avm_b1isa;
177
178         if (check_region(card->port, AVMB1_PORTLEN)) {
179                 printk(KERN_WARNING
180                        "b1isa: ports 0x%03x-0x%03x in use.\n",
181                        card->port, card->port + AVMB1_PORTLEN);
182                 kfree(card->ctrlinfo);
183                 kfree(card);
184                 MOD_DEC_USE_COUNT;
185                 return -EBUSY;
186         }
187         if (b1_irq_table[card->irq & 0xf] == 0) {
188                 printk(KERN_WARNING "b1isa: irq %d not valid.\n", card->irq);
189                 kfree(card->ctrlinfo);
190                 kfree(card);
191                 MOD_DEC_USE_COUNT;
192                 return -EINVAL;
193         }
194         if (   card->port != 0x150 && card->port != 0x250
195             && card->port != 0x300 && card->port != 0x340) {
196                 printk(KERN_WARNING "b1isa: illegal port 0x%x.\n", card->port);
197                 kfree(card->ctrlinfo);
198                 kfree(card);
199                 MOD_DEC_USE_COUNT;
200                 return -EINVAL;
201         }
202         b1_reset(card->port);
203         if ((retval = b1_detect(card->port, card->cardtype)) != 0) {
204                 printk(KERN_NOTICE "b1isa: NO card at 0x%x (%d)\n",
205                                         card->port, retval);
206                 kfree(card->ctrlinfo);
207                 kfree(card);
208                 MOD_DEC_USE_COUNT;
209                 return -EIO;
210         }
211         b1_reset(card->port);
212         b1_getrevision(card);
213
214         request_region(p->port, AVMB1_PORTLEN, card->name);
215
216         retval = request_irq(card->irq, b1isa_interrupt, 0, card->name, card);
217         if (retval) {
218                 printk(KERN_ERR "b1isa: unable to get IRQ %d.\n", card->irq);
219                 release_region(card->port, AVMB1_PORTLEN);
220                 kfree(card->ctrlinfo);
221                 kfree(card);
222                 MOD_DEC_USE_COUNT;
223                 return -EBUSY;
224         }
225
226         cinfo->capi_ctrl = di->attach_ctr(driver, card->name, cinfo);
227         if (!cinfo->capi_ctrl) {
228                 printk(KERN_ERR "b1isa: attach controller failed.\n");
229                 free_irq(card->irq, card);
230                 release_region(card->port, AVMB1_PORTLEN);
231                 kfree(card->ctrlinfo);
232                 kfree(card);
233                 MOD_DEC_USE_COUNT;
234                 return -EBUSY;
235         }
236
237         printk(KERN_INFO
238                 "%s: AVM B1 ISA at i/o %#x, irq %d, revision %d\n",
239                 driver->name, card->port, card->irq, card->revision);
240
241         return 0;
242 }
243
244 static char *b1isa_procinfo(struct capi_ctr *ctrl)
245 {
246         avmctrl_info *cinfo = (avmctrl_info *)(ctrl->driverdata);
247
248         if (!cinfo)
249                 return "";
250         sprintf(cinfo->infobuf, "%s %s 0x%x %d r%d",
251                 cinfo->cardname[0] ? cinfo->cardname : "-",
252                 cinfo->version[VER_DRIVER] ? cinfo->version[VER_DRIVER] : "-",
253                 cinfo->card ? cinfo->card->port : 0x0,
254                 cinfo->card ? cinfo->card->irq : 0,
255                 cinfo->card ? cinfo->card->revision : 0
256                 );
257         return cinfo->infobuf;
258 }
259
260 /* ------------------------------------------------------------- */
261
262 static struct capi_driver b1isa_driver = {
263     name: "b1isa",
264     revision: "0.0",
265     load_firmware: b1_load_firmware,
266     reset_ctr: b1_reset_ctr,
267     remove_ctr: b1isa_remove_ctr,
268     register_appl: b1_register_appl,
269     release_appl: b1_release_appl,
270     send_message: b1_send_message,
271
272     procinfo: b1isa_procinfo,
273     ctr_read_proc: b1ctl_read_proc,
274     driver_read_proc: 0,        /* use standard driver_read_proc */
275
276     add_card: b1isa_add_card,
277 };
278
279 static int __init b1isa_init(void)
280 {
281         struct capi_driver *driver = &b1isa_driver;
282         char *p;
283         int retval = 0;
284
285         MOD_INC_USE_COUNT;
286
287         if ((p = strchr(revision, ':'))) {
288                 strncpy(driver->revision, p + 1, sizeof(driver->revision));
289                 p = strchr(driver->revision, '$');
290                 *p = 0;
291         } 
292
293         printk(KERN_INFO "%s: revision %s\n", driver->name, driver->revision);
294
295         di = attach_capi_driver(driver);
296
297         if (!di) {
298                 printk(KERN_ERR "%s: failed to attach capi_driver\n",
299                                 driver->name);
300                 retval = -EIO;
301         }
302         MOD_DEC_USE_COUNT;
303         return retval;
304 }
305
306 static void __exit b1isa_exit(void)
307 {
308     detach_capi_driver(&b1isa_driver);
309 }
310
311 module_init(b1isa_init);
312 module_exit(b1isa_exit);