hysdn_init.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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 int cardmax; /* number of found cards */
  35. hysdn_card *card_root = NULL; /* pointer to first card */
  36. static hysdn_card *card_last = NULL; /* pointer to first card */
  37. /****************************************************************************/
  38. /* The module startup and shutdown code. Only compiled when used as module. */
  39. /* Using the driver as module is always advisable, because the booting */
  40. /* image becomes smaller and the driver code is only loaded when needed. */
  41. /* Additionally newer versions may be activated without rebooting. */
  42. /****************************************************************************/
  43. /****************************************************************************/
  44. /* init_module is called once when the module is loaded to do all necessary */
  45. /* things like autodetect... */
  46. /* If the return value of this function is 0 the init has been successful */
  47. /* and the module is added to the list in /proc/modules, otherwise an error */
  48. /* is assumed and the module will not be kept in memory. */
  49. /****************************************************************************/
  50. static int hysdn_pci_init_one(struct pci_dev *akt_pcidev,
  51. const struct pci_device_id *ent)
  52. {
  53. hysdn_card *card;
  54. int rc;
  55. rc = pci_enable_device(akt_pcidev);
  56. if (rc)
  57. return rc;
  58. if (!(card = kzalloc(sizeof(hysdn_card), GFP_KERNEL))) {
  59. printk(KERN_ERR "HYSDN: unable to alloc device mem \n");
  60. rc = -ENOMEM;
  61. goto err_out;
  62. }
  63. card->myid = cardmax; /* set own id */
  64. card->bus = akt_pcidev->bus->number;
  65. card->devfn = akt_pcidev->devfn; /* slot + function */
  66. card->subsysid = akt_pcidev->subsystem_device;
  67. card->irq = akt_pcidev->irq;
  68. card->iobase = pci_resource_start(akt_pcidev, PCI_REG_PLX_IO_BASE);
  69. card->plxbase = pci_resource_start(akt_pcidev, PCI_REG_PLX_MEM_BASE);
  70. card->membase = pci_resource_start(akt_pcidev, PCI_REG_MEMORY_BASE);
  71. card->brdtype = BD_NONE; /* unknown */
  72. card->debug_flags = DEF_DEB_FLAGS; /* set default debug */
  73. card->faxchans = 0; /* default no fax channels */
  74. card->bchans = 2; /* and 2 b-channels */
  75. card->brdtype = ent->driver_data;
  76. if (ergo_inithardware(card)) {
  77. printk(KERN_WARNING "HYSDN: card at io 0x%04x already in use\n", card->iobase);
  78. rc = -EBUSY;
  79. goto err_out_card;
  80. }
  81. cardmax++;
  82. card->next = NULL; /*end of chain */
  83. if (card_last)
  84. card_last->next = card; /* pointer to next card */
  85. else
  86. card_root = card;
  87. card_last = card; /* new chain end */
  88. pci_set_drvdata(akt_pcidev, card);
  89. return 0;
  90. err_out_card:
  91. kfree(card);
  92. err_out:
  93. pci_disable_device(akt_pcidev);
  94. return rc;
  95. }
  96. static void hysdn_pci_remove_one(struct pci_dev *akt_pcidev)
  97. {
  98. hysdn_card *card = pci_get_drvdata(akt_pcidev);
  99. pci_set_drvdata(akt_pcidev, NULL);
  100. if (card->stopcard)
  101. card->stopcard(card);
  102. #ifdef CONFIG_HYSDN_CAPI
  103. hycapi_capi_release(card);
  104. #endif
  105. if (card->releasehardware)
  106. card->releasehardware(card); /* free all hardware resources */
  107. if (card == card_root) {
  108. card_root = card_root->next;
  109. if (!card_root)
  110. card_last = NULL;
  111. } else {
  112. hysdn_card *tmp = card_root;
  113. while (tmp) {
  114. if (tmp->next == card)
  115. tmp->next = card->next;
  116. card_last = tmp;
  117. tmp = tmp->next;
  118. }
  119. }
  120. kfree(card);
  121. pci_disable_device(akt_pcidev);
  122. }
  123. static struct pci_driver hysdn_pci_driver = {
  124. .name = "hysdn",
  125. .id_table = hysdn_pci_tbl,
  126. .probe = hysdn_pci_init_one,
  127. .remove = hysdn_pci_remove_one,
  128. };
  129. static int hysdn_have_procfs;
  130. static int __init
  131. hysdn_init(void)
  132. {
  133. int rc;
  134. printk(KERN_NOTICE "HYSDN: module loaded\n");
  135. rc = pci_register_driver(&hysdn_pci_driver);
  136. if (rc)
  137. return rc;
  138. printk(KERN_INFO "HYSDN: %d card(s) found.\n", cardmax);
  139. if (!hysdn_procconf_init())
  140. hysdn_have_procfs = 1;
  141. #ifdef CONFIG_HYSDN_CAPI
  142. if (cardmax > 0) {
  143. if (hycapi_init()) {
  144. printk(KERN_ERR "HYCAPI: init failed\n");
  145. if (hysdn_have_procfs)
  146. hysdn_procconf_release();
  147. pci_unregister_driver(&hysdn_pci_driver);
  148. return -ESPIPE;
  149. }
  150. }
  151. #endif /* CONFIG_HYSDN_CAPI */
  152. return 0; /* no error */
  153. } /* init_module */
  154. /***********************************************************************/
  155. /* cleanup_module is called when the module is released by the kernel. */
  156. /* The routine is only called if init_module has been successful and */
  157. /* the module counter has a value of 0. Otherwise this function will */
  158. /* not be called. This function must release all resources still allo- */
  159. /* cated as after the return from this function the module code will */
  160. /* be removed from memory. */
  161. /***********************************************************************/
  162. static void __exit
  163. hysdn_exit(void)
  164. {
  165. if (hysdn_have_procfs)
  166. hysdn_procconf_release();
  167. pci_unregister_driver(&hysdn_pci_driver);
  168. #ifdef CONFIG_HYSDN_CAPI
  169. hycapi_cleanup();
  170. #endif /* CONFIG_HYSDN_CAPI */
  171. printk(KERN_NOTICE "HYSDN: module unloaded\n");
  172. } /* cleanup_module */
  173. module_init(hysdn_init);
  174. module_exit(hysdn_exit);