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