hysdn_init.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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 = kzalloc(sizeof(hysdn_card), GFP_KERNEL))) {
  75. printk(KERN_ERR "HYSDN: unable to alloc device mem \n");
  76. return;
  77. }
  78. card->myid = cardmax; /* set own id */
  79. card->bus = akt_pcidev->bus->number;
  80. card->devfn = akt_pcidev->devfn; /* slot + function */
  81. card->subsysid = akt_pcidev->subsystem_device;
  82. card->irq = akt_pcidev->irq;
  83. card->iobase = pci_resource_start(akt_pcidev, PCI_REG_PLX_IO_BASE);
  84. card->plxbase = pci_resource_start(akt_pcidev, PCI_REG_PLX_MEM_BASE);
  85. card->membase = pci_resource_start(akt_pcidev, PCI_REG_MEMORY_BASE);
  86. card->brdtype = BD_NONE; /* unknown */
  87. card->debug_flags = DEF_DEB_FLAGS; /* set default debug */
  88. card->faxchans = 0; /* default no fax channels */
  89. card->bchans = 2; /* and 2 b-channels */
  90. for (i = 0; pci_subid_map[i].subid; i++)
  91. if (pci_subid_map[i].subid == card->subsysid) {
  92. card->brdtype = pci_subid_map[i].cardtyp;
  93. break;
  94. }
  95. if (card->brdtype != BD_NONE) {
  96. if (ergo_inithardware(card)) {
  97. printk(KERN_WARNING "HYSDN: card at io 0x%04x already in use\n", card->iobase);
  98. kfree(card);
  99. continue;
  100. }
  101. } else {
  102. printk(KERN_WARNING "HYSDN: unknown card id 0x%04x\n", card->subsysid);
  103. kfree(card); /* release mem */
  104. continue;
  105. }
  106. cardmax++;
  107. card->next = NULL; /*end of chain */
  108. if (card_last)
  109. card_last->next = card; /* pointer to next card */
  110. else
  111. card_root = card;
  112. card_last = card; /* new chain end */
  113. } /* device found */
  114. } /* search_cards */
  115. /************************************************************************************/
  116. /* free_resources frees the acquired PCI resources and returns the allocated memory */
  117. /************************************************************************************/
  118. static void
  119. free_resources(void)
  120. {
  121. hysdn_card *card;
  122. while (card_root) {
  123. card = card_root;
  124. if (card->releasehardware)
  125. card->releasehardware(card); /* free all hardware resources */
  126. card_root = card_root->next; /* remove card from chain */
  127. kfree(card); /* return mem */
  128. } /* while card_root */
  129. } /* free_resources */
  130. /**************************************************************************/
  131. /* stop_cards disables (hardware resets) all cards and disables interrupt */
  132. /**************************************************************************/
  133. static void
  134. stop_cards(void)
  135. {
  136. hysdn_card *card;
  137. card = card_root; /* first in chain */
  138. while (card) {
  139. if (card->stopcard)
  140. card->stopcard(card);
  141. card = card->next; /* remove card from chain */
  142. } /* while card */
  143. } /* stop_cards */
  144. /****************************************************************************/
  145. /* The module startup and shutdown code. Only compiled when used as module. */
  146. /* Using the driver as module is always advisable, because the booting */
  147. /* image becomes smaller and the driver code is only loaded when needed. */
  148. /* Additionally newer versions may be activated without rebooting. */
  149. /****************************************************************************/
  150. /******************************************************/
  151. /* extract revision number from string for log output */
  152. /******************************************************/
  153. char *
  154. hysdn_getrev(const char *revision)
  155. {
  156. char *rev;
  157. char *p;
  158. if ((p = strchr(revision, ':'))) {
  159. rev = p + 2;
  160. p = strchr(rev, '$');
  161. *--p = 0;
  162. } else
  163. rev = "???";
  164. return rev;
  165. }
  166. /****************************************************************************/
  167. /* init_module is called once when the module is loaded to do all necessary */
  168. /* things like autodetect... */
  169. /* If the return value of this function is 0 the init has been successful */
  170. /* and the module is added to the list in /proc/modules, otherwise an error */
  171. /* is assumed and the module will not be kept in memory. */
  172. /****************************************************************************/
  173. static int __init
  174. hysdn_init(void)
  175. {
  176. char tmp[50];
  177. strcpy(tmp, hysdn_init_revision);
  178. printk(KERN_NOTICE "HYSDN: module Rev: %s loaded\n", hysdn_getrev(tmp));
  179. strcpy(tmp, hysdn_net_revision);
  180. printk(KERN_NOTICE "HYSDN: network interface Rev: %s \n", hysdn_getrev(tmp));
  181. search_cards();
  182. printk(KERN_INFO "HYSDN: %d card(s) found.\n", cardmax);
  183. if (hysdn_procconf_init()) {
  184. free_resources(); /* proc file_sys not created */
  185. return (-1);
  186. }
  187. #ifdef CONFIG_HYSDN_CAPI
  188. if(cardmax > 0) {
  189. if(hycapi_init()) {
  190. printk(KERN_ERR "HYCAPI: init failed\n");
  191. return(-1);
  192. }
  193. }
  194. #endif /* CONFIG_HYSDN_CAPI */
  195. return (0); /* no error */
  196. } /* init_module */
  197. /***********************************************************************/
  198. /* cleanup_module is called when the module is released by the kernel. */
  199. /* The routine is only called if init_module has been successful and */
  200. /* the module counter has a value of 0. Otherwise this function will */
  201. /* not be called. This function must release all resources still allo- */
  202. /* cated as after the return from this function the module code will */
  203. /* be removed from memory. */
  204. /***********************************************************************/
  205. static void __exit
  206. hysdn_exit(void)
  207. {
  208. #ifdef CONFIG_HYSDN_CAPI
  209. hysdn_card *card;
  210. #endif /* CONFIG_HYSDN_CAPI */
  211. stop_cards();
  212. #ifdef CONFIG_HYSDN_CAPI
  213. card = card_root; /* first in chain */
  214. while (card) {
  215. hycapi_capi_release(card);
  216. card = card->next; /* remove card from chain */
  217. } /* while card */
  218. hycapi_cleanup();
  219. #endif /* CONFIG_HYSDN_CAPI */
  220. hysdn_procconf_release();
  221. free_resources();
  222. printk(KERN_NOTICE "HYSDN: module unloaded\n");
  223. } /* cleanup_module */
  224. module_init(hysdn_init);
  225. module_exit(hysdn_exit);