comedi_pci.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. * comedi_pci.c
  3. * Comedi PCI driver specific functions.
  4. *
  5. * COMEDI - Linux Control and Measurement Device Interface
  6. * Copyright (C) 1997-2000 David A. Schleef <ds@schleef.org>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. */
  18. #include <linux/module.h>
  19. #include <linux/interrupt.h>
  20. #include "comedi_pci.h"
  21. /**
  22. * comedi_to_pci_dev() - Return PCI device attached to COMEDI device
  23. * @dev: COMEDI device.
  24. *
  25. * Assuming @dev->hw_dev is non-%NULL, it is assumed to be pointing to a
  26. * a &struct device embedded in a &struct pci_dev.
  27. *
  28. * Return: Attached PCI device if @dev->hw_dev is non-%NULL.
  29. * Return %NULL if @dev->hw_dev is %NULL.
  30. */
  31. struct pci_dev *comedi_to_pci_dev(struct comedi_device *dev)
  32. {
  33. return dev->hw_dev ? to_pci_dev(dev->hw_dev) : NULL;
  34. }
  35. EXPORT_SYMBOL_GPL(comedi_to_pci_dev);
  36. /**
  37. * comedi_pci_enable() - Enable the PCI device and request the regions
  38. * @dev: COMEDI device.
  39. *
  40. * Assuming @dev->hw_dev is non-%NULL, it is assumed to be pointing to a
  41. * a &struct device embedded in a &struct pci_dev. Enable the PCI device
  42. * and request its regions. Set @dev->ioenabled to %true if successful,
  43. * otherwise undo what was done.
  44. *
  45. * Calls to comedi_pci_enable() and comedi_pci_disable() cannot be nested.
  46. *
  47. * Return:
  48. * 0 on success,
  49. * -%ENODEV if @dev->hw_dev is %NULL,
  50. * -%EBUSY if regions busy,
  51. * or some negative error number if failed to enable PCI device.
  52. *
  53. */
  54. int comedi_pci_enable(struct comedi_device *dev)
  55. {
  56. struct pci_dev *pcidev = comedi_to_pci_dev(dev);
  57. int rc;
  58. if (!pcidev)
  59. return -ENODEV;
  60. rc = pci_enable_device(pcidev);
  61. if (rc < 0)
  62. return rc;
  63. rc = pci_request_regions(pcidev, dev->board_name);
  64. if (rc < 0)
  65. pci_disable_device(pcidev);
  66. else
  67. dev->ioenabled = true;
  68. return rc;
  69. }
  70. EXPORT_SYMBOL_GPL(comedi_pci_enable);
  71. /**
  72. * comedi_pci_disable() - Release the regions and disable the PCI device
  73. * @dev: COMEDI device.
  74. *
  75. * Assuming @dev->hw_dev is non-%NULL, it is assumed to be pointing to a
  76. * a &struct device embedded in a &struct pci_dev. If the earlier call
  77. * to comedi_pci_enable() was successful, release the PCI device's regions
  78. * and disable it. Reset @dev->ioenabled back to %false.
  79. */
  80. void comedi_pci_disable(struct comedi_device *dev)
  81. {
  82. struct pci_dev *pcidev = comedi_to_pci_dev(dev);
  83. if (pcidev && dev->ioenabled) {
  84. pci_release_regions(pcidev);
  85. pci_disable_device(pcidev);
  86. }
  87. dev->ioenabled = false;
  88. }
  89. EXPORT_SYMBOL_GPL(comedi_pci_disable);
  90. /**
  91. * comedi_pci_detach() - A generic "detach" handler for PCI COMEDI drivers
  92. * @dev: COMEDI device.
  93. *
  94. * COMEDI drivers for PCI devices that need no special clean-up of private data
  95. * and have no ioremapped regions other than that pointed to by @dev->mmio may
  96. * use this function as its "detach" handler called by the COMEDI core when a
  97. * COMEDI device is being detached from the low-level driver. It may be also
  98. * called from a more specific "detach" handler that does additional clean-up.
  99. *
  100. * Free the IRQ if @dev->irq is non-zero, iounmap @dev->mmio if it is
  101. * non-%NULL, and call comedi_pci_disable() to release the PCI device's regions
  102. * and disable it.
  103. */
  104. void comedi_pci_detach(struct comedi_device *dev)
  105. {
  106. struct pci_dev *pcidev = comedi_to_pci_dev(dev);
  107. if (!pcidev || !dev->ioenabled)
  108. return;
  109. if (dev->irq) {
  110. free_irq(dev->irq, dev);
  111. dev->irq = 0;
  112. }
  113. if (dev->mmio) {
  114. iounmap(dev->mmio);
  115. dev->mmio = NULL;
  116. }
  117. comedi_pci_disable(dev);
  118. }
  119. EXPORT_SYMBOL_GPL(comedi_pci_detach);
  120. /**
  121. * comedi_pci_auto_config() - Configure/probe a PCI COMEDI device
  122. * @pcidev: PCI device.
  123. * @driver: Registered COMEDI driver.
  124. * @context: Driver specific data, passed to comedi_auto_config().
  125. *
  126. * Typically called from the pci_driver (*probe) function. Auto-configure
  127. * a COMEDI device, using the &struct device embedded in *@pcidev as the
  128. * hardware device. The @context value gets passed through to @driver's
  129. * "auto_attach" handler. The "auto_attach" handler may call
  130. * comedi_to_pci_dev() on the passed in COMEDI device to recover @pcidev.
  131. *
  132. * Return: The result of calling comedi_auto_config() (0 on success, or
  133. * a negative error number on failure).
  134. */
  135. int comedi_pci_auto_config(struct pci_dev *pcidev,
  136. struct comedi_driver *driver,
  137. unsigned long context)
  138. {
  139. return comedi_auto_config(&pcidev->dev, driver, context);
  140. }
  141. EXPORT_SYMBOL_GPL(comedi_pci_auto_config);
  142. /**
  143. * comedi_pci_auto_unconfig() - Unconfigure/remove a PCI COMEDI device
  144. * @pcidev: PCI device.
  145. *
  146. * Typically called from the pci_driver (*remove) function. Auto-unconfigure
  147. * a COMEDI device attached to this PCI device, using a pointer to the
  148. * &struct device embedded in *@pcidev as the hardware device. The COMEDI
  149. * driver's "detach" handler will be called during unconfiguration of the
  150. * COMEDI device.
  151. *
  152. * Note that the COMEDI device may have already been unconfigured using the
  153. * %COMEDI_DEVCONFIG ioctl, in which case this attempt to unconfigure it
  154. * again should be ignored.
  155. */
  156. void comedi_pci_auto_unconfig(struct pci_dev *pcidev)
  157. {
  158. comedi_auto_unconfig(&pcidev->dev);
  159. }
  160. EXPORT_SYMBOL_GPL(comedi_pci_auto_unconfig);
  161. /**
  162. * comedi_pci_driver_register() - Register a PCI COMEDI driver
  163. * @comedi_driver: COMEDI driver to be registered.
  164. * @pci_driver: PCI driver to be registered.
  165. *
  166. * This function is called from the module_init() of PCI COMEDI driver modules
  167. * to register the COMEDI driver and the PCI driver. Do not call it directly,
  168. * use the module_comedi_pci_driver() helper macro instead.
  169. *
  170. * Return: 0 on success, or a negative error number on failure.
  171. */
  172. int comedi_pci_driver_register(struct comedi_driver *comedi_driver,
  173. struct pci_driver *pci_driver)
  174. {
  175. int ret;
  176. ret = comedi_driver_register(comedi_driver);
  177. if (ret < 0)
  178. return ret;
  179. ret = pci_register_driver(pci_driver);
  180. if (ret < 0) {
  181. comedi_driver_unregister(comedi_driver);
  182. return ret;
  183. }
  184. return 0;
  185. }
  186. EXPORT_SYMBOL_GPL(comedi_pci_driver_register);
  187. /**
  188. * comedi_pci_driver_unregister() - Unregister a PCI COMEDI driver
  189. * @comedi_driver: COMEDI driver to be unregistered.
  190. * @pci_driver: PCI driver to be unregistered.
  191. *
  192. * This function is called from the module_exit() of PCI COMEDI driver modules
  193. * to unregister the PCI driver and the COMEDI driver. Do not call it
  194. * directly, use the module_comedi_pci_driver() helper macro instead.
  195. */
  196. void comedi_pci_driver_unregister(struct comedi_driver *comedi_driver,
  197. struct pci_driver *pci_driver)
  198. {
  199. pci_unregister_driver(pci_driver);
  200. comedi_driver_unregister(comedi_driver);
  201. }
  202. EXPORT_SYMBOL_GPL(comedi_pci_driver_unregister);
  203. static int __init comedi_pci_init(void)
  204. {
  205. return 0;
  206. }
  207. module_init(comedi_pci_init);
  208. static void __exit comedi_pci_exit(void)
  209. {
  210. }
  211. module_exit(comedi_pci_exit);
  212. MODULE_AUTHOR("http://www.comedi.org");
  213. MODULE_DESCRIPTION("Comedi PCI interface module");
  214. MODULE_LICENSE("GPL");