hysdn_init.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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,
  21. PCI_ANY_ID, PCI_SUBDEVICE_ID_HYPERCOPE_METRO, 0, 0, BD_METRO },
  22. { PCI_VENDOR_ID_HYPERCOPE, PCI_DEVICE_ID_HYPERCOPE_PLX,
  23. PCI_ANY_ID, PCI_SUBDEVICE_ID_HYPERCOPE_CHAMP2, 0, 0, BD_CHAMP2 },
  24. { PCI_VENDOR_ID_HYPERCOPE, PCI_DEVICE_ID_HYPERCOPE_PLX,
  25. PCI_ANY_ID, PCI_SUBDEVICE_ID_HYPERCOPE_ERGO, 0, 0, BD_ERGO },
  26. { PCI_VENDOR_ID_HYPERCOPE, PCI_DEVICE_ID_HYPERCOPE_PLX,
  27. PCI_ANY_ID, PCI_SUBDEVICE_ID_HYPERCOPE_OLD_ERGO, 0, 0, BD_ERGO },
  28. { } /* Terminating entry */
  29. };
  30. MODULE_DEVICE_TABLE(pci, hysdn_pci_tbl);
  31. MODULE_DESCRIPTION("ISDN4Linux: Driver for HYSDN cards");
  32. MODULE_AUTHOR("Werner Cornelius");
  33. MODULE_LICENSE("GPL");
  34. static char *hysdn_init_revision = "$Revision: 1.6.6.6 $";
  35. static int cardmax; /* number of found cards */
  36. hysdn_card *card_root = NULL; /* pointer to first card */
  37. static hysdn_card *card_last = NULL; /* pointer to first card */
  38. /****************************************************************************/
  39. /* The module startup and shutdown code. Only compiled when used as module. */
  40. /* Using the driver as module is always advisable, because the booting */
  41. /* image becomes smaller and the driver code is only loaded when needed. */
  42. /* Additionally newer versions may be activated without rebooting. */
  43. /****************************************************************************/
  44. /******************************************************/
  45. /* extract revision number from string for log output */
  46. /******************************************************/
  47. char *
  48. hysdn_getrev(const char *revision)
  49. {
  50. char *rev;
  51. char *p;
  52. if ((p = strchr(revision, ':'))) {
  53. rev = p + 2;
  54. p = strchr(rev, '$');
  55. *--p = 0;
  56. } else
  57. rev = "???";
  58. return rev;
  59. }
  60. /****************************************************************************/
  61. /* init_module is called once when the module is loaded to do all necessary */
  62. /* things like autodetect... */
  63. /* If the return value of this function is 0 the init has been successful */
  64. /* and the module is added to the list in /proc/modules, otherwise an error */
  65. /* is assumed and the module will not be kept in memory. */
  66. /****************************************************************************/
  67. static int __devinit hysdn_pci_init_one(struct pci_dev *akt_pcidev,
  68. const struct pci_device_id *ent)
  69. {
  70. hysdn_card *card;
  71. int rc;
  72. rc = pci_enable_device(akt_pcidev);
  73. if (rc)
  74. return rc;
  75. if (!(card = kzalloc(sizeof(hysdn_card), GFP_KERNEL))) {
  76. printk(KERN_ERR "HYSDN: unable to alloc device mem \n");
  77. rc = -ENOMEM;
  78. goto err_out;
  79. }
  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. card->brdtype = ent->driver_data;
  93. if (ergo_inithardware(card)) {
  94. printk(KERN_WARNING "HYSDN: card at io 0x%04x already in use\n", card->iobase);
  95. rc = -EBUSY;
  96. goto err_out_card;
  97. }
  98. cardmax++;
  99. card->next = NULL; /*end of chain */
  100. if (card_last)
  101. card_last->next = card; /* pointer to next card */
  102. else
  103. card_root = card;
  104. card_last = card; /* new chain end */
  105. pci_set_drvdata(akt_pcidev, card);
  106. return 0;
  107. err_out_card:
  108. kfree(card);
  109. err_out:
  110. pci_disable_device(akt_pcidev);
  111. return rc;
  112. }
  113. static void __devexit hysdn_pci_remove_one(struct pci_dev *akt_pcidev)
  114. {
  115. hysdn_card *card = pci_get_drvdata(akt_pcidev);
  116. pci_set_drvdata(akt_pcidev, NULL);
  117. if (card->stopcard)
  118. card->stopcard(card);
  119. #ifdef CONFIG_HYSDN_CAPI
  120. hycapi_capi_release(card);
  121. #endif
  122. if (card->releasehardware)
  123. card->releasehardware(card); /* free all hardware resources */
  124. if (card == card_root) {
  125. card_root = card_root->next;
  126. if (!card_root)
  127. card_last = NULL;
  128. } else {
  129. hysdn_card *tmp = card_root;
  130. while (tmp) {
  131. if (tmp->next == card)
  132. tmp->next = card->next;
  133. card_last = tmp;
  134. tmp = tmp->next;
  135. }
  136. }
  137. kfree(card);
  138. pci_disable_device(akt_pcidev);
  139. }
  140. static struct pci_driver hysdn_pci_driver = {
  141. .name = "hysdn",
  142. .id_table = hysdn_pci_tbl,
  143. .probe = hysdn_pci_init_one,
  144. .remove = __devexit_p(hysdn_pci_remove_one),
  145. };
  146. static int hysdn_have_procfs;
  147. static int __init
  148. hysdn_init(void)
  149. {
  150. char tmp[50];
  151. int rc;
  152. strcpy(tmp, hysdn_init_revision);
  153. printk(KERN_NOTICE "HYSDN: module Rev: %s loaded\n", hysdn_getrev(tmp));
  154. strcpy(tmp, hysdn_net_revision);
  155. printk(KERN_NOTICE "HYSDN: network interface Rev: %s \n", hysdn_getrev(tmp));
  156. rc = pci_register_driver(&hysdn_pci_driver);
  157. if (rc)
  158. return rc;
  159. printk(KERN_INFO "HYSDN: %d card(s) found.\n", cardmax);
  160. if (!hysdn_procconf_init())
  161. hysdn_have_procfs = 1;
  162. #ifdef CONFIG_HYSDN_CAPI
  163. if(cardmax > 0) {
  164. if(hycapi_init()) {
  165. printk(KERN_ERR "HYCAPI: init failed\n");
  166. if (hysdn_have_procfs)
  167. hysdn_procconf_release();
  168. pci_unregister_driver(&hysdn_pci_driver);
  169. return -ESPIPE;
  170. }
  171. }
  172. #endif /* CONFIG_HYSDN_CAPI */
  173. return 0; /* no error */
  174. } /* init_module */
  175. /***********************************************************************/
  176. /* cleanup_module is called when the module is released by the kernel. */
  177. /* The routine is only called if init_module has been successful and */
  178. /* the module counter has a value of 0. Otherwise this function will */
  179. /* not be called. This function must release all resources still allo- */
  180. /* cated as after the return from this function the module code will */
  181. /* be removed from memory. */
  182. /***********************************************************************/
  183. static void __exit
  184. hysdn_exit(void)
  185. {
  186. if (hysdn_have_procfs)
  187. hysdn_procconf_release();
  188. pci_unregister_driver(&hysdn_pci_driver);
  189. #ifdef CONFIG_HYSDN_CAPI
  190. hycapi_cleanup();
  191. #endif /* CONFIG_HYSDN_CAPI */
  192. printk(KERN_NOTICE "HYSDN: module unloaded\n");
  193. } /* cleanup_module */
  194. module_init(hysdn_init);
  195. module_exit(hysdn_exit);