blob: 9e01748a176e95c2726c2842ff102bca5fd3c4eb [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* $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
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/module.h>
14#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#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
22static 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};
29MODULE_DEVICE_TABLE(pci, hysdn_pci_tbl);
30MODULE_DESCRIPTION("ISDN4Linux: Driver for HYSDN cards");
31MODULE_AUTHOR("Werner Cornelius");
32MODULE_LICENSE("GPL");
33
34static char *hysdn_init_revision = "$Revision: 1.6.6.6 $";
Adrian Bunkaade0e82005-06-28 20:44:56 -070035static int cardmax; /* number of found cards */
Linus Torvalds1da177e2005-04-16 15:20:36 -070036hysdn_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/**********************************************/
42static struct {
Andrew Mortonc721bcc2006-03-25 03:07:04 -080043 unsigned short subid; /* PCI sub id */
44 unsigned char cardtyp; /* card type assigned */
Linus Torvalds1da177e2005-04-16 15:20:36 -070045} 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/*********************************************************************/
70static void
71search_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
Burman Yan41f96932006-12-08 02:39:35 -080084 if (!(card = kzalloc(sizeof(hysdn_card), GFP_KERNEL))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 printk(KERN_ERR "HYSDN: unable to alloc device mem \n");
86 return;
87 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 card->myid = cardmax; /* set own id */
89 card->bus = akt_pcidev->bus->number;
90 card->devfn = akt_pcidev->devfn; /* slot + function */
91 card->subsysid = akt_pcidev->subsystem_device;
92 card->irq = akt_pcidev->irq;
93 card->iobase = pci_resource_start(akt_pcidev, PCI_REG_PLX_IO_BASE);
94 card->plxbase = pci_resource_start(akt_pcidev, PCI_REG_PLX_MEM_BASE);
95 card->membase = pci_resource_start(akt_pcidev, PCI_REG_MEMORY_BASE);
96 card->brdtype = BD_NONE; /* unknown */
97 card->debug_flags = DEF_DEB_FLAGS; /* set default debug */
98 card->faxchans = 0; /* default no fax channels */
99 card->bchans = 2; /* and 2 b-channels */
100 for (i = 0; pci_subid_map[i].subid; i++)
101 if (pci_subid_map[i].subid == card->subsysid) {
102 card->brdtype = pci_subid_map[i].cardtyp;
103 break;
104 }
105 if (card->brdtype != BD_NONE) {
106 if (ergo_inithardware(card)) {
107 printk(KERN_WARNING "HYSDN: card at io 0x%04x already in use\n", card->iobase);
108 kfree(card);
109 continue;
110 }
111 } else {
112 printk(KERN_WARNING "HYSDN: unknown card id 0x%04x\n", card->subsysid);
113 kfree(card); /* release mem */
114 continue;
115 }
116 cardmax++;
117 card->next = NULL; /*end of chain */
118 if (card_last)
119 card_last->next = card; /* pointer to next card */
120 else
121 card_root = card;
122 card_last = card; /* new chain end */
123 } /* device found */
124} /* search_cards */
125
126/************************************************************************************/
127/* free_resources frees the acquired PCI resources and returns the allocated memory */
128/************************************************************************************/
129static void
130free_resources(void)
131{
132 hysdn_card *card;
133
134 while (card_root) {
135 card = card_root;
136 if (card->releasehardware)
137 card->releasehardware(card); /* free all hardware resources */
138 card_root = card_root->next; /* remove card from chain */
139 kfree(card); /* return mem */
140
141 } /* while card_root */
142} /* free_resources */
143
144/**************************************************************************/
145/* stop_cards disables (hardware resets) all cards and disables interrupt */
146/**************************************************************************/
147static void
148stop_cards(void)
149{
150 hysdn_card *card;
151
152 card = card_root; /* first in chain */
153 while (card) {
154 if (card->stopcard)
155 card->stopcard(card);
156 card = card->next; /* remove card from chain */
157 } /* while card */
158} /* stop_cards */
159
160
161/****************************************************************************/
162/* The module startup and shutdown code. Only compiled when used as module. */
163/* Using the driver as module is always advisable, because the booting */
164/* image becomes smaller and the driver code is only loaded when needed. */
165/* Additionally newer versions may be activated without rebooting. */
166/****************************************************************************/
167
168/******************************************************/
169/* extract revision number from string for log output */
170/******************************************************/
171char *
172hysdn_getrev(const char *revision)
173{
174 char *rev;
175 char *p;
176
177 if ((p = strchr(revision, ':'))) {
178 rev = p + 2;
179 p = strchr(rev, '$');
180 *--p = 0;
181 } else
182 rev = "???";
183 return rev;
184}
185
186
187/****************************************************************************/
188/* init_module is called once when the module is loaded to do all necessary */
189/* things like autodetect... */
190/* If the return value of this function is 0 the init has been successful */
191/* and the module is added to the list in /proc/modules, otherwise an error */
192/* is assumed and the module will not be kept in memory. */
193/****************************************************************************/
194static int __init
195hysdn_init(void)
196{
197 char tmp[50];
198
199 strcpy(tmp, hysdn_init_revision);
200 printk(KERN_NOTICE "HYSDN: module Rev: %s loaded\n", hysdn_getrev(tmp));
201 strcpy(tmp, hysdn_net_revision);
202 printk(KERN_NOTICE "HYSDN: network interface Rev: %s \n", hysdn_getrev(tmp));
203 search_cards();
204 printk(KERN_INFO "HYSDN: %d card(s) found.\n", cardmax);
205
206 if (hysdn_procconf_init()) {
207 free_resources(); /* proc file_sys not created */
208 return (-1);
209 }
210#ifdef CONFIG_HYSDN_CAPI
211 if(cardmax > 0) {
212 if(hycapi_init()) {
213 printk(KERN_ERR "HYCAPI: init failed\n");
214 return(-1);
215 }
216 }
217#endif /* CONFIG_HYSDN_CAPI */
218 return (0); /* no error */
219} /* init_module */
220
221
222/***********************************************************************/
223/* cleanup_module is called when the module is released by the kernel. */
224/* The routine is only called if init_module has been successful and */
225/* the module counter has a value of 0. Otherwise this function will */
226/* not be called. This function must release all resources still allo- */
227/* cated as after the return from this function the module code will */
228/* be removed from memory. */
229/***********************************************************************/
230static void __exit
231hysdn_exit(void)
232{
233#ifdef CONFIG_HYSDN_CAPI
234 hysdn_card *card;
235#endif /* CONFIG_HYSDN_CAPI */
236 stop_cards();
237#ifdef CONFIG_HYSDN_CAPI
238 card = card_root; /* first in chain */
239 while (card) {
240 hycapi_capi_release(card);
241 card = card->next; /* remove card from chain */
242 } /* while card */
243 hycapi_cleanup();
244#endif /* CONFIG_HYSDN_CAPI */
245 hysdn_procconf_release();
246 free_resources();
247 printk(KERN_NOTICE "HYSDN: module unloaded\n");
248} /* cleanup_module */
249
250module_init(hysdn_init);
251module_exit(hysdn_exit);