Merge to Fedora kernel-2.6.18-1.2224_FC5 patched with stable patch-2.6.18.1-vs2.0...
[linux-2.6.git] / drivers / isdn / hysdn / hysdn_init.c
1 /* $Id: hysdn_init.c,v 1.6.6.6 2001/09/23 22:24:54 kai Exp $
2  *
3  * Linux driver for HYSDN cards, init functions.
4  *
5  * Author    Werner Cornelius (werner@titro.de) for Hypercope GmbH
6  * Copyright 1999 by Werner Cornelius (werner@titro.de)
7  *
8  * This software may be used and distributed according to the terms
9  * of the GNU General Public License, incorporated herein by reference.
10  *
11  */
12
13 #include <linux/module.h>
14 #include <linux/init.h>
15 #include <linux/poll.h>
16 #include <linux/vmalloc.h>
17 #include <linux/slab.h>
18 #include <linux/pci.h>
19
20 #include "hysdn_defs.h"
21
22 static struct pci_device_id hysdn_pci_tbl[] = {
23         {PCI_VENDOR_ID_HYPERCOPE, PCI_DEVICE_ID_HYPERCOPE_PLX, PCI_ANY_ID, PCI_SUBDEVICE_ID_HYPERCOPE_METRO},
24         {PCI_VENDOR_ID_HYPERCOPE, PCI_DEVICE_ID_HYPERCOPE_PLX, PCI_ANY_ID, PCI_SUBDEVICE_ID_HYPERCOPE_CHAMP2},
25         {PCI_VENDOR_ID_HYPERCOPE, PCI_DEVICE_ID_HYPERCOPE_PLX, PCI_ANY_ID, PCI_SUBDEVICE_ID_HYPERCOPE_ERGO},
26         {PCI_VENDOR_ID_HYPERCOPE, PCI_DEVICE_ID_HYPERCOPE_PLX, PCI_ANY_ID, PCI_SUBDEVICE_ID_HYPERCOPE_OLD_ERGO},
27         { }                             /* Terminating entry */
28 };
29 MODULE_DEVICE_TABLE(pci, hysdn_pci_tbl);
30 MODULE_DESCRIPTION("ISDN4Linux: Driver for HYSDN cards");
31 MODULE_AUTHOR("Werner Cornelius");
32 MODULE_LICENSE("GPL");
33
34 static char *hysdn_init_revision = "$Revision: 1.6.6.6 $";
35 static int cardmax;             /* number of found cards */
36 hysdn_card *card_root = NULL;   /* pointer to first card */
37
38 /**********************************************/
39 /* table assigning PCI-sub ids to board types */
40 /* the last entry contains all 0              */
41 /**********************************************/
42 static struct {
43         unsigned short subid;           /* PCI sub id */
44         unsigned char cardtyp;          /* card type assigned */
45 } pci_subid_map[] = {
46
47         {
48                 PCI_SUBDEVICE_ID_HYPERCOPE_METRO, BD_METRO
49         },
50         {
51                 PCI_SUBDEVICE_ID_HYPERCOPE_CHAMP2, BD_CHAMP2
52         },
53         {
54                 PCI_SUBDEVICE_ID_HYPERCOPE_ERGO, BD_ERGO
55         },
56         {
57                 PCI_SUBDEVICE_ID_HYPERCOPE_OLD_ERGO, BD_ERGO
58         },
59         {
60                 0, 0
61         }                       /* terminating entry */
62 };
63
64
65 /*********************************************************************/
66 /* search_cards searches for available cards in the pci config data. */
67 /* If a card is found, the card structure is allocated and the cards */
68 /* ressources are reserved. cardmax is incremented.                  */
69 /*********************************************************************/
70 static void
71 search_cards(void)
72 {
73         struct pci_dev *akt_pcidev = NULL;
74         hysdn_card *card, *card_last;
75         int i;
76
77         card_root = NULL;
78         card_last = NULL;
79         while ((akt_pcidev = pci_find_device(PCI_VENDOR_ID_HYPERCOPE, PCI_DEVICE_ID_HYPERCOPE_PLX,
80                                              akt_pcidev)) != NULL) {
81                 if (pci_enable_device(akt_pcidev))
82                         continue;
83
84                 if (!(card = kmalloc(sizeof(hysdn_card), GFP_KERNEL))) {
85                         printk(KERN_ERR "HYSDN: unable to alloc device mem \n");
86                         return;
87                 }
88                 memset(card, 0, sizeof(hysdn_card));
89                 card->myid = cardmax;   /* set own id */
90                 card->bus = akt_pcidev->bus->number;
91                 card->devfn = akt_pcidev->devfn;        /* slot + function */
92                 card->subsysid = akt_pcidev->subsystem_device;
93                 card->irq = akt_pcidev->irq;
94                 card->iobase = pci_resource_start(akt_pcidev, PCI_REG_PLX_IO_BASE);
95                 card->plxbase = pci_resource_start(akt_pcidev, PCI_REG_PLX_MEM_BASE);
96                 card->membase = pci_resource_start(akt_pcidev, PCI_REG_MEMORY_BASE);
97                 card->brdtype = BD_NONE;        /* unknown */
98                 card->debug_flags = DEF_DEB_FLAGS;      /* set default debug */
99                 card->faxchans = 0;     /* default no fax channels */
100                 card->bchans = 2;       /* and 2 b-channels */
101                 for (i = 0; pci_subid_map[i].subid; i++)
102                         if (pci_subid_map[i].subid == card->subsysid) {
103                                 card->brdtype = pci_subid_map[i].cardtyp;
104                                 break;
105                         }
106                 if (card->brdtype != BD_NONE) {
107                         if (ergo_inithardware(card)) {
108                                 printk(KERN_WARNING "HYSDN: card at io 0x%04x already in use\n", card->iobase);
109                                 kfree(card);
110                                 continue;
111                         }
112                 } else {
113                         printk(KERN_WARNING "HYSDN: unknown card id 0x%04x\n", card->subsysid);
114                         kfree(card);    /* release mem */
115                         continue;
116                 }
117                 cardmax++;
118                 card->next = NULL;      /*end of chain */
119                 if (card_last)
120                         card_last->next = card;         /* pointer to next card */
121                 else
122                         card_root = card;
123                 card_last = card;       /* new chain end */
124         }                       /* device found */
125 }                               /* search_cards */
126
127 /************************************************************************************/
128 /* free_resources frees the acquired PCI resources and returns the allocated memory */
129 /************************************************************************************/
130 static void
131 free_resources(void)
132 {
133         hysdn_card *card;
134
135         while (card_root) {
136                 card = card_root;
137                 if (card->releasehardware)
138                         card->releasehardware(card);    /* free all hardware resources */
139                 card_root = card_root->next;    /* remove card from chain */
140                 kfree(card);    /* return mem */
141
142         }                       /* while card_root */
143 }                               /* free_resources */
144
145 /**************************************************************************/
146 /* stop_cards disables (hardware resets) all cards and disables interrupt */
147 /**************************************************************************/
148 static void
149 stop_cards(void)
150 {
151         hysdn_card *card;
152
153         card = card_root;       /* first in chain */
154         while (card) {
155                 if (card->stopcard)
156                         card->stopcard(card);
157                 card = card->next;      /* remove card from chain */
158         }                       /* while card */
159 }                               /* stop_cards */
160
161
162 /****************************************************************************/
163 /* The module startup and shutdown code. Only compiled when used as module. */
164 /* Using the driver as module is always advisable, because the booting      */
165 /* image becomes smaller and the driver code is only loaded when needed.    */
166 /* Additionally newer versions may be activated without rebooting.          */
167 /****************************************************************************/
168
169 /******************************************************/
170 /* extract revision number from string for log output */
171 /******************************************************/
172 char *
173 hysdn_getrev(const char *revision)
174 {
175         char *rev;
176         char *p;
177
178         if ((p = strchr(revision, ':'))) {
179                 rev = p + 2;
180                 p = strchr(rev, '$');
181                 *--p = 0;
182         } else
183                 rev = "???";
184         return rev;
185 }
186
187
188 /****************************************************************************/
189 /* init_module is called once when the module is loaded to do all necessary */
190 /* things like autodetect...                                                */
191 /* If the return value of this function is 0 the init has been successful   */
192 /* and the module is added to the list in /proc/modules, otherwise an error */
193 /* is assumed and the module will not be kept in memory.                    */
194 /****************************************************************************/
195 static int __init
196 hysdn_init(void)
197 {
198         char tmp[50];
199
200         strcpy(tmp, hysdn_init_revision);
201         printk(KERN_NOTICE "HYSDN: module Rev: %s loaded\n", hysdn_getrev(tmp));
202         strcpy(tmp, hysdn_net_revision);
203         printk(KERN_NOTICE "HYSDN: network interface Rev: %s \n", hysdn_getrev(tmp));
204         search_cards();
205         printk(KERN_INFO "HYSDN: %d card(s) found.\n", cardmax);
206
207         if (hysdn_procconf_init()) {
208                 free_resources();       /* proc file_sys not created */
209                 return (-1);
210         }
211 #ifdef CONFIG_HYSDN_CAPI
212         if(cardmax > 0) {
213                 if(hycapi_init()) {
214                         printk(KERN_ERR "HYCAPI: init failed\n");
215                         return(-1);
216                 }
217         }
218 #endif /* CONFIG_HYSDN_CAPI */
219         return (0);             /* no error */
220 }                               /* init_module */
221
222
223 /***********************************************************************/
224 /* cleanup_module is called when the module is released by the kernel. */
225 /* The routine is only called if init_module has been successful and   */
226 /* the module counter has a value of 0. Otherwise this function will   */
227 /* not be called. This function must release all resources still allo- */
228 /* cated as after the return from this function the module code will   */
229 /* be removed from memory.                                             */
230 /***********************************************************************/
231 static void __exit
232 hysdn_exit(void)
233 {
234 #ifdef CONFIG_HYSDN_CAPI
235         hysdn_card *card;
236 #endif /* CONFIG_HYSDN_CAPI */
237         stop_cards();
238 #ifdef CONFIG_HYSDN_CAPI
239         card = card_root;       /* first in chain */
240         while (card) {
241                 hycapi_capi_release(card);
242                 card = card->next;      /* remove card from chain */
243         }                       /* while card */
244         hycapi_cleanup();
245 #endif /* CONFIG_HYSDN_CAPI */
246         hysdn_procconf_release();
247         free_resources();
248         printk(KERN_NOTICE "HYSDN: module unloaded\n");
249 }                               /* cleanup_module */
250
251 module_init(hysdn_init);
252 module_exit(hysdn_exit);