comedi_pcmcia.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * comedi_pcmcia.c
  4. * Comedi PCMCIA driver specific functions.
  5. *
  6. * COMEDI - Linux Control and Measurement Device Interface
  7. * Copyright (C) 1997-2000 David A. Schleef <ds@schleef.org>
  8. */
  9. #include <linux/module.h>
  10. #include <linux/kernel.h>
  11. #include "comedi_pcmcia.h"
  12. /**
  13. * comedi_to_pcmcia_dev() - Return PCMCIA device attached to COMEDI device
  14. * @dev: COMEDI device.
  15. *
  16. * Assuming @dev->hw_dev is non-%NULL, it is assumed to be pointing to a
  17. * a &struct device embedded in a &struct pcmcia_device.
  18. *
  19. * Return: Attached PCMCIA device if @dev->hw_dev is non-%NULL.
  20. * Return %NULL if @dev->hw_dev is %NULL.
  21. */
  22. struct pcmcia_device *comedi_to_pcmcia_dev(struct comedi_device *dev)
  23. {
  24. return dev->hw_dev ? to_pcmcia_dev(dev->hw_dev) : NULL;
  25. }
  26. EXPORT_SYMBOL_GPL(comedi_to_pcmcia_dev);
  27. static int comedi_pcmcia_conf_check(struct pcmcia_device *link,
  28. void *priv_data)
  29. {
  30. if (link->config_index == 0)
  31. return -EINVAL;
  32. return pcmcia_request_io(link);
  33. }
  34. /**
  35. * comedi_pcmcia_enable() - Request the regions and enable the PCMCIA device
  36. * @dev: COMEDI device.
  37. * @conf_check: Optional callback to check each configuration option of the
  38. * PCMCIA device and request I/O regions.
  39. *
  40. * Assuming @dev->hw_dev is non-%NULL, it is assumed to be pointing to a a
  41. * &struct device embedded in a &struct pcmcia_device. The comedi PCMCIA
  42. * driver needs to set the 'config_flags' member in the &struct pcmcia_device,
  43. * as appropriate for that driver, before calling this function in order to
  44. * allow pcmcia_loop_config() to do its internal autoconfiguration.
  45. *
  46. * If @conf_check is %NULL it is set to a default function. If is
  47. * passed to pcmcia_loop_config() and should return %0 if the configuration
  48. * is valid and I/O regions requested successfully, otherwise it should return
  49. * a negative error value. The default function returns -%EINVAL if the
  50. * 'config_index' member is %0, otherwise it calls pcmcia_request_io() and
  51. * returns the result.
  52. *
  53. * If the above configuration check passes, pcmcia_enable_device() is called
  54. * to set up and activate the PCMCIA device.
  55. *
  56. * If this function returns an error, comedi_pcmcia_disable() should be called
  57. * to release requested resources.
  58. *
  59. * Return:
  60. * 0 on success,
  61. * -%ENODEV id @dev->hw_dev is %NULL,
  62. * a negative error number from pcmcia_loop_config() if it fails,
  63. * or a negative error number from pcmcia_enable_device() if it fails.
  64. */
  65. int comedi_pcmcia_enable(struct comedi_device *dev,
  66. int (*conf_check)(struct pcmcia_device *p_dev,
  67. void *priv_data))
  68. {
  69. struct pcmcia_device *link = comedi_to_pcmcia_dev(dev);
  70. int ret;
  71. if (!link)
  72. return -ENODEV;
  73. if (!conf_check)
  74. conf_check = comedi_pcmcia_conf_check;
  75. ret = pcmcia_loop_config(link, conf_check, NULL);
  76. if (ret)
  77. return ret;
  78. return pcmcia_enable_device(link);
  79. }
  80. EXPORT_SYMBOL_GPL(comedi_pcmcia_enable);
  81. /**
  82. * comedi_pcmcia_disable() - Disable the PCMCIA device and release the regions
  83. * @dev: COMEDI device.
  84. *
  85. * Assuming @dev->hw_dev is non-%NULL, it is assumed to be pointing to a
  86. * a &struct device embedded in a &struct pcmcia_device. Call
  87. * pcmcia_disable_device() to disable and clean up the PCMCIA device.
  88. */
  89. void comedi_pcmcia_disable(struct comedi_device *dev)
  90. {
  91. struct pcmcia_device *link = comedi_to_pcmcia_dev(dev);
  92. if (link)
  93. pcmcia_disable_device(link);
  94. }
  95. EXPORT_SYMBOL_GPL(comedi_pcmcia_disable);
  96. /**
  97. * comedi_pcmcia_auto_config() - Configure/probe a PCMCIA COMEDI device
  98. * @link: PCMCIA device.
  99. * @driver: Registered COMEDI driver.
  100. *
  101. * Typically called from the pcmcia_driver (*probe) function. Auto-configure
  102. * a COMEDI device, using a pointer to the &struct device embedded in *@link
  103. * as the hardware device. The @driver's "auto_attach" handler may call
  104. * comedi_to_pcmcia_dev() on the passed in COMEDI device to recover @link.
  105. *
  106. * Return: The result of calling comedi_auto_config() (0 on success, or a
  107. * negative error number on failure).
  108. */
  109. int comedi_pcmcia_auto_config(struct pcmcia_device *link,
  110. struct comedi_driver *driver)
  111. {
  112. return comedi_auto_config(&link->dev, driver, 0);
  113. }
  114. EXPORT_SYMBOL_GPL(comedi_pcmcia_auto_config);
  115. /**
  116. * comedi_pcmcia_auto_unconfig() - Unconfigure/remove a PCMCIA COMEDI device
  117. * @link: PCMCIA device.
  118. *
  119. * Typically called from the pcmcia_driver (*remove) function.
  120. * Auto-unconfigure a COMEDI device attached to this PCMCIA device, using a
  121. * pointer to the &struct device embedded in *@link as the hardware device.
  122. * The COMEDI driver's "detach" handler will be called during unconfiguration
  123. * of the COMEDI device.
  124. *
  125. * Note that the COMEDI device may have already been unconfigured using the
  126. * %COMEDI_DEVCONFIG ioctl, in which case this attempt to unconfigure it
  127. * again should be ignored.
  128. */
  129. void comedi_pcmcia_auto_unconfig(struct pcmcia_device *link)
  130. {
  131. comedi_auto_unconfig(&link->dev);
  132. }
  133. EXPORT_SYMBOL_GPL(comedi_pcmcia_auto_unconfig);
  134. /**
  135. * comedi_pcmcia_driver_register() - Register a PCMCIA COMEDI driver
  136. * @comedi_driver: COMEDI driver to be registered.
  137. * @pcmcia_driver: PCMCIA driver to be registered.
  138. *
  139. * This function is used for the module_init() of PCMCIA COMEDI driver modules
  140. * to register the COMEDI driver and the PCMCIA driver. Do not call it
  141. * directly, use the module_comedi_pcmcia_driver() helper macro instead.
  142. *
  143. * Return: 0 on success, or a negative error number on failure.
  144. */
  145. int comedi_pcmcia_driver_register(struct comedi_driver *comedi_driver,
  146. struct pcmcia_driver *pcmcia_driver)
  147. {
  148. int ret;
  149. ret = comedi_driver_register(comedi_driver);
  150. if (ret < 0)
  151. return ret;
  152. ret = pcmcia_register_driver(pcmcia_driver);
  153. if (ret < 0) {
  154. comedi_driver_unregister(comedi_driver);
  155. return ret;
  156. }
  157. return 0;
  158. }
  159. EXPORT_SYMBOL_GPL(comedi_pcmcia_driver_register);
  160. /**
  161. * comedi_pcmcia_driver_unregister() - Unregister a PCMCIA COMEDI driver
  162. * @comedi_driver: COMEDI driver to be registered.
  163. * @pcmcia_driver: PCMCIA driver to be registered.
  164. *
  165. * This function is called from the module_exit() of PCMCIA COMEDI driver
  166. * modules to unregister the PCMCIA driver and the COMEDI driver. Do not call
  167. * it directly, use the module_comedi_pcmcia_driver() helper macro instead.
  168. */
  169. void comedi_pcmcia_driver_unregister(struct comedi_driver *comedi_driver,
  170. struct pcmcia_driver *pcmcia_driver)
  171. {
  172. pcmcia_unregister_driver(pcmcia_driver);
  173. comedi_driver_unregister(comedi_driver);
  174. }
  175. EXPORT_SYMBOL_GPL(comedi_pcmcia_driver_unregister);
  176. static int __init comedi_pcmcia_init(void)
  177. {
  178. return 0;
  179. }
  180. module_init(comedi_pcmcia_init);
  181. static void __exit comedi_pcmcia_exit(void)
  182. {
  183. }
  184. module_exit(comedi_pcmcia_exit);
  185. MODULE_AUTHOR("https://www.comedi.org");
  186. MODULE_DESCRIPTION("Comedi PCMCIA interface module");
  187. MODULE_LICENSE("GPL");