hysdn_init.c 8.3 KB

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