hysdn_init.c 8.2 KB

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