Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/roland...
[linux-2.6] / drivers / isdn / hardware / avm / t1pci.c
1 /* $Id: t1pci.c,v 1.1.2.2 2004/01/16 21:09:27 keil Exp $
2  * 
3  * Module for AVM T1 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/module.h>
13 #include <linux/kernel.h>
14 #include <linux/skbuff.h>
15 #include <linux/delay.h>
16 #include <linux/mm.h>
17 #include <linux/interrupt.h>
18 #include <linux/ioport.h>
19 #include <linux/pci.h>
20 #include <linux/capi.h>
21 #include <linux/init.h>
22 #include <asm/io.h>
23 #include <linux/isdn/capicmd.h>
24 #include <linux/isdn/capiutil.h>
25 #include <linux/isdn/capilli.h>
26 #include "avmcard.h"
27
28 #undef CONFIG_T1PCI_DEBUG
29 #undef CONFIG_T1PCI_POLLDEBUG
30
31 /* ------------------------------------------------------------- */
32 static char *revision = "$Revision: 1.1.2.2 $";
33 /* ------------------------------------------------------------- */
34
35 static struct pci_device_id t1pci_pci_tbl[] = {
36         { PCI_VENDOR_ID_AVM, PCI_DEVICE_ID_AVM_T1, PCI_ANY_ID, PCI_ANY_ID },
37         { }                             /* Terminating entry */
38 };
39
40 MODULE_DEVICE_TABLE(pci, t1pci_pci_tbl);
41 MODULE_DESCRIPTION("CAPI4Linux: Driver for AVM T1 PCI card");
42 MODULE_AUTHOR("Carsten Paeth");
43 MODULE_LICENSE("GPL");
44
45 /* ------------------------------------------------------------- */
46
47 static char *t1pci_procinfo(struct capi_ctr *ctrl);
48
49 static int t1pci_add_card(struct capicardparams *p, struct pci_dev *pdev)
50 {
51         avmcard *card;
52         avmctrl_info *cinfo;
53         int retval;
54
55         card = b1_alloc_card(1);
56         if (!card) {
57                 printk(KERN_WARNING "t1pci: no memory.\n");
58                 retval = -ENOMEM;
59                 goto err;
60         }
61
62         card->dma = avmcard_dma_alloc("t1pci", pdev, 2048+128, 2048+128);
63         if (!card->dma) {
64                 printk(KERN_WARNING "t1pci: no memory.\n");
65                 retval = -ENOMEM;
66                 goto err_free;
67         }
68
69         cinfo = card->ctrlinfo;
70         sprintf(card->name, "t1pci-%x", p->port);
71         card->port = p->port;
72         card->irq = p->irq;
73         card->membase = p->membase;
74         card->cardtype = avm_t1pci;
75
76         if (!request_region(card->port, AVMB1_PORTLEN, card->name)) {
77                 printk(KERN_WARNING "t1pci: ports 0x%03x-0x%03x in use.\n",
78                        card->port, card->port + AVMB1_PORTLEN);
79                 retval = -EBUSY;
80                 goto err_free_dma;
81         }
82
83         card->mbase = ioremap(card->membase, 64);
84         if (!card->mbase) {
85                 printk(KERN_NOTICE "t1pci: can't remap memory at 0x%lx\n",
86                        card->membase);
87                 retval = -EIO;
88                 goto err_release_region;
89         }
90
91         b1dma_reset(card);
92
93         retval = t1pci_detect(card);
94         if (retval != 0) {
95                 if (retval < 6)
96                         printk(KERN_NOTICE "t1pci: NO card at 0x%x (%d)\n",
97                                card->port, retval);
98                 else
99                         printk(KERN_NOTICE "t1pci: card at 0x%x, but cable not connected or T1 has no power (%d)\n",
100                                card->port, retval);
101                 retval = -EIO;
102                 goto err_unmap;
103         }
104         b1dma_reset(card);
105
106         retval = request_irq(card->irq, b1dma_interrupt, IRQF_SHARED, card->name, card);
107         if (retval) {
108                 printk(KERN_ERR "t1pci: unable to get IRQ %d.\n", card->irq);
109                 retval = -EBUSY;
110                 goto err_unmap;
111         }
112
113         cinfo->capi_ctrl.owner         = THIS_MODULE;
114         cinfo->capi_ctrl.driver_name   = "t1pci";
115         cinfo->capi_ctrl.driverdata    = cinfo;
116         cinfo->capi_ctrl.register_appl = b1dma_register_appl;
117         cinfo->capi_ctrl.release_appl  = b1dma_release_appl;
118         cinfo->capi_ctrl.send_message  = b1dma_send_message;
119         cinfo->capi_ctrl.load_firmware = b1dma_load_firmware;
120         cinfo->capi_ctrl.reset_ctr     = b1dma_reset_ctr;
121         cinfo->capi_ctrl.procinfo      = t1pci_procinfo;
122         cinfo->capi_ctrl.ctr_read_proc = b1dmactl_read_proc;
123         strcpy(cinfo->capi_ctrl.name, card->name);
124
125         retval = attach_capi_ctr(&cinfo->capi_ctrl);
126         if (retval) {
127                 printk(KERN_ERR "t1pci: attach controller failed.\n");
128                 retval = -EBUSY;
129                 goto err_free_irq;
130         }
131         card->cardnr = cinfo->capi_ctrl.cnr;
132
133         printk(KERN_INFO "t1pci: AVM T1 PCI at i/o %#x, irq %d, mem %#lx\n",
134                card->port, card->irq, card->membase);
135
136         pci_set_drvdata(pdev, card);
137         return 0;
138
139  err_free_irq:
140         free_irq(card->irq, card);
141  err_unmap:
142         iounmap(card->mbase);
143  err_release_region:
144         release_region(card->port, AVMB1_PORTLEN);
145  err_free_dma:
146         avmcard_dma_free(card->dma);
147  err_free:
148         b1_free_card(card);
149  err:
150         return retval;
151 }
152
153 /* ------------------------------------------------------------- */
154
155 static void t1pci_remove(struct pci_dev *pdev)
156 {
157         avmcard *card = pci_get_drvdata(pdev);
158         avmctrl_info *cinfo = card->ctrlinfo;
159
160         b1dma_reset(card);
161
162         detach_capi_ctr(&cinfo->capi_ctrl);
163         free_irq(card->irq, card);
164         iounmap(card->mbase);
165         release_region(card->port, AVMB1_PORTLEN);
166         avmcard_dma_free(card->dma);
167         b1_free_card(card);
168 }
169
170 /* ------------------------------------------------------------- */
171
172 static char *t1pci_procinfo(struct capi_ctr *ctrl)
173 {
174         avmctrl_info *cinfo = (avmctrl_info *)(ctrl->driverdata);
175
176         if (!cinfo)
177                 return "";
178         sprintf(cinfo->infobuf, "%s %s 0x%x %d 0x%lx",
179                 cinfo->cardname[0] ? cinfo->cardname : "-",
180                 cinfo->version[VER_DRIVER] ? cinfo->version[VER_DRIVER] : "-",
181                 cinfo->card ? cinfo->card->port : 0x0,
182                 cinfo->card ? cinfo->card->irq : 0,
183                 cinfo->card ? cinfo->card->membase : 0
184                 );
185         return cinfo->infobuf;
186 }
187
188 /* ------------------------------------------------------------- */
189
190 static int __devinit t1pci_probe(struct pci_dev *dev,
191                                  const struct pci_device_id *ent)
192 {
193         struct capicardparams param;
194         int retval;
195
196         if (pci_enable_device(dev) < 0) {
197                 printk(KERN_ERR "t1pci: failed to enable AVM-T1-PCI\n");
198                 return -ENODEV;
199         }
200         pci_set_master(dev);
201
202         param.port = pci_resource_start(dev, 1);
203         param.irq = dev->irq;
204         param.membase = pci_resource_start(dev, 0);
205
206         printk(KERN_INFO "t1pci: PCI BIOS reports AVM-T1-PCI at i/o %#x, irq %d, mem %#x\n",
207                param.port, param.irq, param.membase);
208
209         retval = t1pci_add_card(&param, dev);
210         if (retval != 0) {
211                 printk(KERN_ERR "t1pci: no AVM-T1-PCI at i/o %#x, irq %d detected, mem %#x\n",
212                        param.port, param.irq, param.membase);
213                 return -ENODEV;
214         }
215         return 0;
216 }
217
218 static struct pci_driver t1pci_pci_driver = {
219        .name           = "t1pci",
220        .id_table       = t1pci_pci_tbl,
221        .probe          = t1pci_probe,
222        .remove         = t1pci_remove,
223 };
224
225 static struct capi_driver capi_driver_t1pci = {
226         .name           = "t1pci",
227         .revision       = "1.0",
228 };
229
230 static int __init t1pci_init(void)
231 {
232         char *p;
233         char rev[32];
234         int err;
235
236         if ((p = strchr(revision, ':')) != 0 && p[1]) {
237                 strlcpy(rev, p + 2, 32);
238                 if ((p = strchr(rev, '$')) != 0 && p > rev)
239                    *(p-1) = 0;
240         } else
241                 strcpy(rev, "1.0");
242
243         err = pci_register_driver(&t1pci_pci_driver);
244         if (!err) {
245                 strlcpy(capi_driver_t1pci.revision, rev, 32);
246                 register_capi_driver(&capi_driver_t1pci);
247                 printk(KERN_INFO "t1pci: revision %s\n", rev);
248         }
249         return err;
250 }
251
252 static void __exit t1pci_exit(void)
253 {
254         unregister_capi_driver(&capi_driver_t1pci);
255         pci_unregister_driver(&t1pci_pci_driver);
256 }
257
258 module_init(t1pci_init);
259 module_exit(t1pci_exit);