comedi_pcmcia.c 7.1 KB

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