hysdn_init.c
上传用户:lgb322
上传日期:2013-02-24
资源大小:30529k
文件大小:8k
源码类别:

嵌入式Linux

开发平台:

Unix_Linux

  1. /* $Id: hysdn_init.c,v 1.1.4.1 2001/11/20 14:19:37 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[] __initdata = {
  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.1.4.1 $";
  33. 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 usen", card->iobase);
  101. kfree(card);
  102. continue;
  103. }
  104. } else {
  105. printk(KERN_WARNING "HYSDN: unknown card id 0x%04xn", 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 loadedn", hysdn_getrev(tmp));
  182. strcpy(tmp, hysdn_net_revision);
  183. printk(KERN_NOTICE "HYSDN: network interface Rev: %s n", hysdn_getrev(tmp));
  184. if (!pci_present()) {
  185. printk(KERN_ERR "HYSDN: no PCI bus present, module not loadedn");
  186. return (-1);
  187. }
  188. search_cards();
  189. printk(KERN_INFO "HYSDN: %d card(s) found.n", cardmax);
  190. if (hysdn_procconf_init()) {
  191. free_resources(); /* proc file_sys not created */
  192. return (-1);
  193. }
  194. #ifdef CONFIG_HYSDN_CAPI
  195. if(cardmax > 0) {
  196. if(hycapi_init()) {
  197. printk(KERN_ERR "HYCAPI: init failedn");
  198. return(-1);
  199. }
  200. }
  201. #endif /* CONFIG_HYSDN_CAPI */
  202. return (0); /* no error */
  203. } /* init_module */
  204. /***********************************************************************/
  205. /* cleanup_module is called when the module is released by the kernel. */
  206. /* The routine is only called if init_module has been successful and   */
  207. /* the module counter has a value of 0. Otherwise this function will   */
  208. /* not be called. This function must release all resources still allo- */
  209. /* cated as after the return from this function the module code will   */
  210. /* be removed from memory.                                             */
  211. /***********************************************************************/
  212. static void __exit
  213. hysdn_exit(void)
  214. {
  215. #ifdef CONFIG_HYSDN_CAPI
  216. hysdn_card *card;
  217. #endif /* CONFIG_HYSDN_CAPI */
  218. stop_cards();
  219. #ifdef CONFIG_HYSDN_CAPI
  220. card = card_root; /* first in chain */
  221. while (card) {
  222. hycapi_capi_release(card);
  223. card = card->next; /* remove card from chain */
  224. } /* while card */
  225. hycapi_cleanup();
  226. #endif /* CONFIG_HYSDN_CAPI */
  227. hysdn_procconf_release();
  228. free_resources();
  229. printk(KERN_NOTICE "HYSDN: module unloadedn");
  230. } /* cleanup_module */
  231. module_init(hysdn_init);
  232. module_exit(hysdn_exit);