hysdn_init.c 8.2 KB

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