vserver 2.0 rc7
[linux-2.6.git] / drivers / isdn / hardware / avm / b1pci.c
1 /* $Id: b1pci.c,v 1.1.2.2 2004/01/16 21:09:27 keil Exp $
2  * 
3  * Module for AVM B1 PCI-card.
4  * 
5  * Copyright 1999 by Carsten Paeth <calle@calle.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/config.h>
13 #include <linux/module.h>
14 #include <linux/kernel.h>
15 #include <linux/skbuff.h>
16 #include <linux/delay.h>
17 #include <linux/mm.h>
18 #include <linux/interrupt.h>
19 #include <linux/ioport.h>
20 #include <linux/pci.h>
21 #include <linux/capi.h>
22 #include <asm/io.h>
23 #include <linux/init.h>
24 #include <linux/isdn/capicmd.h>
25 #include <linux/isdn/capiutil.h>
26 #include <linux/isdn/capilli.h>
27 #include "avmcard.h"
28
29 /* ------------------------------------------------------------- */
30
31 static char *revision = "$Revision: 1.1.2.2 $";
32
33 /* ------------------------------------------------------------- */
34
35 static struct pci_device_id b1pci_pci_tbl[] = {
36         { PCI_VENDOR_ID_AVM, PCI_DEVICE_ID_AVM_B1, PCI_ANY_ID, PCI_ANY_ID },
37         { }                             /* Terminating entry */
38 };
39
40 MODULE_DEVICE_TABLE(pci, b1pci_pci_tbl);
41 MODULE_DESCRIPTION("CAPI4Linux: Driver for AVM B1 PCI card");
42 MODULE_AUTHOR("Carsten Paeth");
43 MODULE_LICENSE("GPL");
44
45 /* ------------------------------------------------------------- */
46
47 static char *b1pci_procinfo(struct capi_ctr *ctrl)
48 {
49         avmctrl_info *cinfo = (avmctrl_info *)(ctrl->driverdata);
50
51         if (!cinfo)
52                 return "";
53         sprintf(cinfo->infobuf, "%s %s 0x%x %d r%d",
54                 cinfo->cardname[0] ? cinfo->cardname : "-",
55                 cinfo->version[VER_DRIVER] ? cinfo->version[VER_DRIVER] : "-",
56                 cinfo->card ? cinfo->card->port : 0x0,
57                 cinfo->card ? cinfo->card->irq : 0,
58                 cinfo->card ? cinfo->card->revision : 0
59                 );
60         return cinfo->infobuf;
61 }
62
63 /* ------------------------------------------------------------- */
64
65 static int b1pci_probe(struct capicardparams *p, struct pci_dev *pdev)
66 {
67         avmcard *card;
68         avmctrl_info *cinfo;
69         int retval;
70
71         card = b1_alloc_card(1);
72         if (!card) {
73                 printk(KERN_WARNING "b1pci: no memory.\n");
74                 retval = -ENOMEM;
75                 goto err;
76         }
77
78         cinfo = card->ctrlinfo;
79         sprintf(card->name, "b1pci-%x", p->port);
80         card->port = p->port;
81         card->irq = p->irq;
82         card->cardtype = avm_b1pci;
83         
84         if (!request_region(card->port, AVMB1_PORTLEN, card->name)) {
85                 printk(KERN_WARNING "b1pci: ports 0x%03x-0x%03x in use.\n",
86                        card->port, card->port + AVMB1_PORTLEN);
87                 retval = -EBUSY;
88                 goto err_free;
89         }
90         b1_reset(card->port);
91         retval = b1_detect(card->port, card->cardtype);
92         if (retval) {
93                 printk(KERN_NOTICE "b1pci: NO card at 0x%x (%d)\n",
94                        card->port, retval);
95                 retval = -ENODEV;
96                 goto err_release_region;
97         }
98         b1_reset(card->port);
99         b1_getrevision(card);
100         
101         retval = request_irq(card->irq, b1_interrupt, SA_SHIRQ, card->name, card);
102         if (retval) {
103                 printk(KERN_ERR "b1pci: unable to get IRQ %d.\n", card->irq);
104                 retval = -EBUSY;
105                 goto err_release_region;
106         }
107         
108         cinfo->capi_ctrl.driver_name   = "b1pci";
109         cinfo->capi_ctrl.driverdata    = cinfo;
110         cinfo->capi_ctrl.register_appl = b1_register_appl;
111         cinfo->capi_ctrl.release_appl  = b1_release_appl;
112         cinfo->capi_ctrl.send_message  = b1_send_message;
113         cinfo->capi_ctrl.load_firmware = b1_load_firmware;
114         cinfo->capi_ctrl.reset_ctr     = b1_reset_ctr;
115         cinfo->capi_ctrl.procinfo      = b1pci_procinfo;
116         cinfo->capi_ctrl.ctr_read_proc = b1ctl_read_proc;
117         strcpy(cinfo->capi_ctrl.name, card->name);
118         cinfo->capi_ctrl.owner         = THIS_MODULE;
119
120         retval = attach_capi_ctr(&cinfo->capi_ctrl);
121         if (retval) {
122                 printk(KERN_ERR "b1pci: attach controller failed.\n");
123                 goto err_free_irq;
124         }
125
126         if (card->revision >= 4) {
127                 printk(KERN_INFO "b1pci: AVM B1 PCI V4 at i/o %#x, irq %d, revision %d (no dma)\n",
128                        card->port, card->irq, card->revision);
129         } else {
130                 printk(KERN_INFO "b1pci: AVM B1 PCI at i/o %#x, irq %d, revision %d\n",
131                        card->port, card->irq, card->revision);
132         }
133
134         pci_set_drvdata(pdev, card);
135         return 0;
136
137  err_free_irq:
138         free_irq(card->irq, card);
139  err_release_region:
140         release_region(card->port, AVMB1_PORTLEN);
141  err_free:
142         b1_free_card(card);
143  err:
144         return retval;
145 }
146
147 static void b1pci_remove(struct pci_dev *pdev)
148 {
149         avmcard *card = pci_get_drvdata(pdev);
150         avmctrl_info *cinfo = card->ctrlinfo;
151         unsigned int port = card->port;
152
153         b1_reset(port);
154         b1_reset(port);
155
156         detach_capi_ctr(&cinfo->capi_ctrl);
157         free_irq(card->irq, card);
158         release_region(card->port, AVMB1_PORTLEN);
159         b1_free_card(card);
160 }
161
162 #ifdef CONFIG_ISDN_DRV_AVMB1_B1PCIV4
163 /* ------------------------------------------------------------- */
164
165 static char *b1pciv4_procinfo(struct capi_ctr *ctrl)
166 {
167         avmctrl_info *cinfo = (avmctrl_info *)(ctrl->driverdata);
168
169         if (!cinfo)
170                 return "";
171         sprintf(cinfo->infobuf, "%s %s 0x%x %d 0x%lx r%d",
172                 cinfo->cardname[0] ? cinfo->cardname : "-",
173                 cinfo->version[VER_DRIVER] ? cinfo->version[VER_DRIVER] : "-",
174                 cinfo->card ? cinfo->card->port : 0x0,
175                 cinfo->card ? cinfo->card->irq : 0,
176                 cinfo->card ? cinfo->card->membase : 0,
177                 cinfo->card ? cinfo->card->revision : 0
178                 );
179         return cinfo->infobuf;
180 }
181
182 /* ------------------------------------------------------------- */
183
184 static int b1pciv4_probe(struct capicardparams *p, struct pci_dev *pdev)
185 {
186         avmcard *card;
187         avmctrl_info *cinfo;
188         int retval;
189
190         card = b1_alloc_card(1);
191         if (!card) {
192                 printk(KERN_WARNING "b1pci: no memory.\n");
193                 retval = -ENOMEM;
194                 goto err;
195         }
196
197         card->dma = avmcard_dma_alloc("b1pci", pdev, 2048+128, 2048+128);
198         if (!card->dma) {
199                 printk(KERN_WARNING "b1pci: dma alloc.\n");
200                 retval = -ENOMEM;
201                 goto err_free;
202         }
203
204         cinfo = card->ctrlinfo;
205         sprintf(card->name, "b1pciv4-%x", p->port);
206         card->port = p->port;
207         card->irq = p->irq;
208         card->membase = p->membase;
209         card->cardtype = avm_b1pci;
210
211         if (!request_region(card->port, AVMB1_PORTLEN, card->name)) {
212                 printk(KERN_WARNING "b1pci: ports 0x%03x-0x%03x in use.\n",
213                        card->port, card->port + AVMB1_PORTLEN);
214                 retval = -EBUSY;
215                 goto err_free_dma;
216         }
217
218         card->mbase = ioremap(card->membase, 64);
219         if (!card->mbase) {
220                 printk(KERN_NOTICE "b1pci: can't remap memory at 0x%lx\n",
221                        card->membase);
222                 retval = -ENOMEM;
223                 goto err_release_region;
224         }
225
226         b1dma_reset(card);
227
228         retval = b1pciv4_detect(card);
229         if (retval) {
230                 printk(KERN_NOTICE "b1pci: NO card at 0x%x (%d)\n",
231                        card->port, retval);
232                 retval = -ENODEV;
233                 goto err_unmap;
234         }
235         b1dma_reset(card);
236         b1_getrevision(card);
237
238         retval = request_irq(card->irq, b1dma_interrupt, SA_SHIRQ, card->name, card);
239         if (retval) {
240                 printk(KERN_ERR "b1pci: unable to get IRQ %d.\n",
241                        card->irq);
242                 retval = -EBUSY;
243                 goto err_unmap;
244         }
245
246         cinfo->capi_ctrl.owner         = THIS_MODULE;
247         cinfo->capi_ctrl.driver_name   = "b1pciv4";
248         cinfo->capi_ctrl.driverdata    = cinfo;
249         cinfo->capi_ctrl.register_appl = b1dma_register_appl;
250         cinfo->capi_ctrl.release_appl  = b1dma_release_appl;
251         cinfo->capi_ctrl.send_message  = b1dma_send_message;
252         cinfo->capi_ctrl.load_firmware = b1dma_load_firmware;
253         cinfo->capi_ctrl.reset_ctr     = b1dma_reset_ctr;
254         cinfo->capi_ctrl.procinfo      = b1pciv4_procinfo;
255         cinfo->capi_ctrl.ctr_read_proc = b1dmactl_read_proc;
256         strcpy(cinfo->capi_ctrl.name, card->name);
257
258         retval = attach_capi_ctr(&cinfo->capi_ctrl);
259         if (retval) {
260                 printk(KERN_ERR "b1pci: attach controller failed.\n");
261                 goto err_free_irq;
262         }
263         card->cardnr = cinfo->capi_ctrl.cnr;
264
265         printk(KERN_INFO "b1pci: AVM B1 PCI V4 at i/o %#x, irq %d, mem %#lx, revision %d (dma)\n",
266                card->port, card->irq, card->membase, card->revision);
267
268         pci_set_drvdata(pdev, card);
269         return 0;
270
271  err_free_irq:
272         free_irq(card->irq, card);
273  err_unmap:
274         iounmap(card->mbase);
275  err_release_region:
276         release_region(card->port, AVMB1_PORTLEN);
277  err_free_dma:
278         avmcard_dma_free(card->dma);
279  err_free:
280         b1_free_card(card);
281  err:
282         return retval;
283
284 }
285
286 static void b1pciv4_remove(struct pci_dev *pdev)
287 {
288         avmcard *card = pci_get_drvdata(pdev);
289         avmctrl_info *cinfo = card->ctrlinfo;
290
291         b1dma_reset(card);
292
293         detach_capi_ctr(&cinfo->capi_ctrl);
294         free_irq(card->irq, card);
295         iounmap(card->mbase);
296         release_region(card->port, AVMB1_PORTLEN);
297         avmcard_dma_free(card->dma);
298         b1_free_card(card);
299 }
300
301 #endif /* CONFIG_ISDN_DRV_AVMB1_B1PCIV4 */
302
303 static int __devinit b1pci_pci_probe(struct pci_dev *pdev,
304                                      const struct pci_device_id *ent)
305 {
306         struct capicardparams param;
307         int retval;
308
309         if (pci_enable_device(pdev) < 0) {
310                 printk(KERN_ERR "b1pci: failed to enable AVM-B1\n");
311                 return -ENODEV;
312         }
313         param.irq = pdev->irq;
314
315         if (pci_resource_start(pdev, 2)) { /* B1 PCI V4 */
316 #ifdef CONFIG_ISDN_DRV_AVMB1_B1PCIV4
317                 pci_set_master(pdev);
318 #endif
319                 param.membase = pci_resource_start(pdev, 0);
320                 param.port = pci_resource_start(pdev, 2);
321
322                 printk(KERN_INFO "b1pci: PCI BIOS reports AVM-B1 V4 at i/o %#x, irq %d, mem %#x\n",
323                        param.port, param.irq, param.membase);
324 #ifdef CONFIG_ISDN_DRV_AVMB1_B1PCIV4
325                 retval = b1pciv4_probe(&param, pdev);
326 #else
327                 retval = b1pci_probe(&param, pdev);
328 #endif
329                 if (retval != 0) {
330                         printk(KERN_ERR "b1pci: no AVM-B1 V4 at i/o %#x, irq %d, mem %#x detected\n",
331                                param.port, param.irq, param.membase);
332                 }
333         } else {
334                 param.membase = 0;
335                 param.port = pci_resource_start(pdev, 1);
336
337                 printk(KERN_INFO "b1pci: PCI BIOS reports AVM-B1 at i/o %#x, irq %d\n",
338                        param.port, param.irq);
339                 retval = b1pci_probe(&param, pdev);
340                 if (retval != 0) {
341                         printk(KERN_ERR "b1pci: no AVM-B1 at i/o %#x, irq %d detected\n",
342                                param.port, param.irq);
343                 }
344         }
345         return retval;
346 }
347
348 static void __devexit b1pci_pci_remove(struct pci_dev *pdev)
349 {
350 #ifdef CONFIG_ISDN_DRV_AVMB1_B1PCIV4
351         avmcard *card = pci_get_drvdata(pdev);
352
353         if (card->dma)
354                 b1pciv4_remove(pdev);
355         else
356                 b1pci_remove(pdev);
357 #else
358         b1pci_remove(pdev);
359 #endif
360 }
361
362 static struct pci_driver b1pci_pci_driver = {
363         .name           = "b1pci",
364         .id_table       = b1pci_pci_tbl,
365         .probe          = b1pci_pci_probe,
366         .remove         = __devexit_p(b1pci_pci_remove),
367 };
368
369 static struct capi_driver capi_driver_b1pci = {
370         .name           = "b1pci",
371         .revision       = "1.0",
372 };
373 #ifdef CONFIG_ISDN_DRV_AVMB1_B1PCIV4
374 static struct capi_driver capi_driver_b1pciv4 = {
375         .name           = "b1pciv4",
376         .revision       = "1.0",
377 };
378 #endif
379
380 static int __init b1pci_init(void)
381 {
382         char *p;
383         char rev[32];
384         int err;
385
386         if ((p = strchr(revision, ':')) != 0 && p[1]) {
387                 strlcpy(rev, p + 2, 32);
388                 if ((p = strchr(rev, '$')) != 0 && p > rev)
389                    *(p-1) = 0;
390         } else
391                 strcpy(rev, "1.0");
392
393
394         err = pci_register_driver(&b1pci_pci_driver);
395         if (!err) {
396                 strlcpy(capi_driver_b1pci.revision, rev, 32);
397                 register_capi_driver(&capi_driver_b1pci);
398 #ifdef CONFIG_ISDN_DRV_AVMB1_B1PCIV4
399                 strlcpy(capi_driver_b1pciv4.revision, rev, 32);
400                 register_capi_driver(&capi_driver_b1pciv4);
401 #endif
402                 printk(KERN_INFO "b1pci: revision %s\n", rev);
403         }
404         return err;
405 }
406
407 static void __exit b1pci_exit(void)
408 {
409         unregister_capi_driver(&capi_driver_b1pci);
410 #ifdef CONFIG_ISDN_DRV_AVMB1_B1PCIV4
411         unregister_capi_driver(&capi_driver_b1pciv4);
412 #endif
413         pci_unregister_driver(&b1pci_pci_driver);
414 }
415
416 module_init(b1pci_init);
417 module_exit(b1pci_exit);