ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / isdn / tpam / tpam_main.c
1 /* $Id: tpam_main.c,v 1.1.2.3 2001/09/23 22:25:03 kai Exp $
2  *
3  * Turbo PAM ISDN driver for Linux. (Kernel Driver - main routines)
4  *
5  * Copyright 2001 Stelian Pop <stelian.pop@fr.alcove.com>, AlcĂ´ve
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  * For all support questions please contact: <support@auvertech.fr>
11  *
12  */
13
14 #include <linux/module.h>
15 #include <linux/pci.h>
16 #include <linux/sched.h>
17 #include <linux/interrupt.h>
18
19 #include <linux/init.h>
20 #include <asm/io.h>
21
22 #include "tpam.h"
23
24 /* Local functions prototypes */
25 static int __devinit tpam_probe(struct pci_dev *, const struct pci_device_id *);
26 static void __devexit tpam_unregister_card(tpam_card *);
27 static void __devexit tpam_remove(struct pci_dev *);
28 static int __init tpam_init(void);
29 static void __exit tpam_exit(void);
30
31 /* List of boards */
32 static tpam_card *cards; /* = NULL; */
33 /* Number of cards */
34 static int cards_num;
35 /* Configurable id of the driver */
36 static char *id = "tpam\0\0\0\0\0\0\0\0\0\0\0\0";
37
38 MODULE_DESCRIPTION("ISDN4Linux: Driver for TurboPAM ISDN cards");
39 MODULE_AUTHOR("Stelian Pop");
40 MODULE_LICENSE("GPL");
41 MODULE_PARM_DESC(id,"ID-String of the driver");
42 MODULE_PARM(id,"s");
43
44 /*
45  * Finds a board by its driver ID.
46  *
47  *      driverId: driver ID (as referenced by the IDSN link layer)
48  *
49  * Return: the tpam_card structure if found, NULL on error.
50  */
51 tpam_card *tpam_findcard(int driverid) {
52         tpam_card *p = cards;
53
54         while (p) {
55                 if (p->id == driverid)
56                         return p;
57                 p = p->next;
58         }
59         return NULL;
60 }
61
62 /*
63  * Finds a channel number by its ncoid.
64  *
65  *      card: the board
66  *      ncoid: the NCO id
67  *
68  * Return: the channel number if found, TPAM_CHANNEL_INVALID if not.
69  */
70 u32 tpam_findchannel(tpam_card *card, u32 ncoid) {
71         int i;
72
73         for (i = 0; i < TPAM_NBCHANNEL; ++i)
74                 if (card->channels[i].ncoid == ncoid)
75                         return card->channels[i].num;
76         return TPAM_CHANNEL_INVALID;
77 }
78
79 /*
80  * Initializes and registers a new TurboPAM card.
81  *
82  *      dev: the PCI device
83  *      num: the board number
84  *
85  * Return: 0 if OK, <0 if error
86  */
87 static int __devinit tpam_probe(struct pci_dev *dev, const struct pci_device_id *pci_id) {
88         tpam_card *card, *c;
89         int i;
90
91         /* allocate memory for the board structure */
92         if (!(card = (tpam_card *)kmalloc(sizeof(tpam_card), GFP_KERNEL))) {
93                 printk(KERN_ERR "TurboPAM: tpam_register_card: "
94                        "kmalloc failed!\n");
95                 return -ENOMEM;
96         }
97
98         memset((char *)card, 0, sizeof(tpam_card));
99
100         card->irq = dev->irq;
101         card->lock = SPIN_LOCK_UNLOCKED;
102         sprintf(card->interface.id, "%s%d", id, cards_num);
103
104         /* request interrupt */
105         if (request_irq(card->irq, &tpam_irq, SA_INTERRUPT | SA_SHIRQ, 
106                         card->interface.id, card)) {
107                 printk(KERN_ERR "TurboPAM: tpam_register_card: "
108                        "could not request irq %d\n", card->irq);
109                 kfree(card);
110                 return -EIO;
111         }
112
113         /* remap board memory */
114         if (!(card->bar0 = (unsigned long) ioremap(pci_resource_start(dev, 0),
115                                                    0x800000))) {
116                 printk(KERN_ERR "TurboPAM: tpam_register_card: "
117                        "unable to remap bar0\n");
118                 free_irq(card->irq, card);
119                 kfree(card);
120                 return -EIO;
121         }
122
123         /* reset the board */
124         readl(card->bar0 + TPAM_RESETPAM_REGISTER);
125
126         /* initialisation magic :-( */
127         copy_to_pam_dword(card, (void *)0x01800008, 0x00000030);
128         copy_to_pam_dword(card, (void *)0x01800010, 0x00000030);
129         copy_to_pam_dword(card, (void *)0x01800014, 0x42240822);
130         copy_to_pam_dword(card, (void *)0x01800018, 0x07114000);
131         copy_to_pam_dword(card, (void *)0x0180001c, 0x00000400);
132         copy_to_pam_dword(card, (void *)0x01840070, 0x00000010);
133
134         /* fill the ISDN link layer structure */
135         card->interface.owner = THIS_MODULE;
136         card->interface.channels = TPAM_NBCHANNEL;
137         card->interface.maxbufsize = TPAM_MAXBUFSIZE;
138         card->interface.features = 
139                 ISDN_FEATURE_P_EURO |
140                 ISDN_FEATURE_L2_HDLC |
141                 ISDN_FEATURE_L2_MODEM |
142                 ISDN_FEATURE_L3_TRANS;
143         card->interface.hl_hdrlen = 0;
144         card->interface.command = tpam_command;
145         card->interface.writebuf_skb = tpam_writebuf_skb;
146         card->interface.writecmd = NULL;
147         card->interface.readstat = NULL;
148
149         /* register wrt the ISDN link layer */
150         if (!register_isdn(&card->interface)) {
151                 printk(KERN_ERR "TurboPAM: tpam_register_card: "
152                        "unable to register %s\n", card->interface.id);
153                 free_irq(card->irq, card);
154                 iounmap((void *)card->bar0);
155                 kfree(card);
156                 return -EIO;
157         }
158         card->id = card->interface.channels;
159
160         /* initialize all channels */
161         for (i = 0; i < TPAM_NBCHANNEL; ++i) {
162                 card->channels[i].num = i;
163                 card->channels[i].card = card;
164                 card->channels[i].ncoid = TPAM_NCOID_INVALID;
165                 card->channels[i].hdlc = 0;
166                 card->channels[i].realhdlc = 0;
167                 card->channels[i].hdlcshift = 0;
168                 skb_queue_head_init(&card->channels[i].sendq);
169         }
170
171         /* initialize the rest of board structure */
172         card->channels_used = 0;
173         card->channels_tested = 0;
174         card->running = 0;
175         card->busy = 0;
176         card->roundrobin = 0;
177         card->loopmode = 0;
178         skb_queue_head_init(&card->sendq);
179         skb_queue_head_init(&card->recvq);
180         INIT_WORK(&card->recv_tq, (void *) (void *) tpam_recv_tq, card);
181         INIT_WORK(&card->send_tq, (void *) (void *) tpam_send_tq, card);
182
183         /* add the board at the end of the list of boards */
184         card->next = NULL;
185         if (cards) {
186                 c = cards;
187                 while (c->next)
188                         c = c->next;
189                 c->next = card;
190         }
191         else
192                 cards = card;
193
194         ++cards_num;
195         pci_set_drvdata(dev, card);
196
197         return 0;
198 }
199
200 /*
201  * Unregisters a TurboPAM board by releasing all its ressources (irq, mem etc).
202  *
203  *      card: the board.
204  */
205 static void __devexit tpam_unregister_card(tpam_card *card) {
206         isdn_ctrl cmd;
207
208         /* prevent the ISDN link layer that the driver will be unloaded */
209         cmd.command = ISDN_STAT_UNLOAD;
210         cmd.driver = card->id;
211         (* card->interface.statcallb)(&cmd);
212
213         /* release interrupt */
214         free_irq(card->irq, card);
215
216         /* release mapped memory */
217         iounmap((void *)card->bar0);
218 }
219
220 /*
221  * Stops the driver.
222  */
223 static void __devexit tpam_remove(struct pci_dev *pcidev) {
224         tpam_card *card = pci_get_drvdata(pcidev);
225         tpam_card *c;
226
227         /* remove from the list of cards */
228         if (card == cards)
229                 cards = cards->next;
230         else {
231                 c = cards;
232                 while (c->next != card) 
233                         c = c->next;
234                 c->next = c->next->next;
235         }
236         
237         /* unregister each board */
238         tpam_unregister_card(card);
239         
240         /* and free the board structure itself */
241         kfree(card);
242 }
243
244 static struct pci_device_id tpam_pci_tbl[] = {
245         { PCI_VENDOR_ID_XILINX, PCI_DEVICE_ID_TURBOPAM,
246           PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
247         { }
248 };
249
250 MODULE_DEVICE_TABLE(pci, tpam_pci_tbl);
251
252 static struct pci_driver tpam_driver = {
253         .name           = "tpam",
254         .id_table       = tpam_pci_tbl,
255         .probe          = tpam_probe,
256         .remove         = __devexit_p(tpam_remove),
257 };
258
259 static int __init tpam_init(void) {
260         int ret;
261         
262         ret = pci_module_init(&tpam_driver);
263         if (ret)
264                 return ret;
265         printk(KERN_INFO "TurboPAM: %d card%s found, driver loaded.\n", 
266                cards_num, (cards_num > 1) ? "s" : "");
267         return 0;
268 }
269
270 static void __exit tpam_exit(void) {
271         pci_unregister_driver(&tpam_driver);
272         printk(KERN_INFO "TurboPAM: driver unloaded\n");
273 }
274
275 /* Module entry points */
276 module_init(tpam_init);
277 module_exit(tpam_exit);
278