comedi_usb.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * comedi_usb.c
  4. * Comedi USB 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 "comedi_usb.h"
  11. /**
  12. * comedi_to_usb_interface() - Return USB interface attached to COMEDI device
  13. * @dev: COMEDI device.
  14. *
  15. * Assuming @dev->hw_dev is non-%NULL, it is assumed to be pointing to a
  16. * a &struct device embedded in a &struct usb_interface.
  17. *
  18. * Return: Attached USB interface if @dev->hw_dev is non-%NULL.
  19. * Return %NULL if @dev->hw_dev is %NULL.
  20. */
  21. struct usb_interface *comedi_to_usb_interface(struct comedi_device *dev)
  22. {
  23. return dev->hw_dev ? to_usb_interface(dev->hw_dev) : NULL;
  24. }
  25. EXPORT_SYMBOL_GPL(comedi_to_usb_interface);
  26. /**
  27. * comedi_to_usb_dev() - Return USB device attached to COMEDI device
  28. * @dev: COMEDI device.
  29. *
  30. * Assuming @dev->hw_dev is non-%NULL, it is assumed to be pointing to a
  31. * a &struct device embedded in a &struct usb_interface.
  32. *
  33. * Return: USB device to which the USB interface belongs if @dev->hw_dev is
  34. * non-%NULL. Return %NULL if @dev->hw_dev is %NULL.
  35. */
  36. struct usb_device *comedi_to_usb_dev(struct comedi_device *dev)
  37. {
  38. struct usb_interface *intf = comedi_to_usb_interface(dev);
  39. return intf ? interface_to_usbdev(intf) : NULL;
  40. }
  41. EXPORT_SYMBOL_GPL(comedi_to_usb_dev);
  42. /**
  43. * comedi_usb_auto_config() - Configure/probe a USB COMEDI driver
  44. * @intf: USB interface.
  45. * @driver: Registered COMEDI driver.
  46. * @context: Driver specific data, passed to comedi_auto_config().
  47. *
  48. * Typically called from the usb_driver (*probe) function. Auto-configure a
  49. * COMEDI device, using a pointer to the &struct device embedded in *@intf as
  50. * the hardware device. The @context value gets passed through to @driver's
  51. * "auto_attach" handler. The "auto_attach" handler may call
  52. * comedi_to_usb_interface() on the passed in COMEDI device to recover @intf.
  53. *
  54. * Return: The result of calling comedi_auto_config() (%0 on success, or
  55. * a negative error number on failure).
  56. */
  57. int comedi_usb_auto_config(struct usb_interface *intf,
  58. struct comedi_driver *driver,
  59. unsigned long context)
  60. {
  61. return comedi_auto_config(&intf->dev, driver, context);
  62. }
  63. EXPORT_SYMBOL_GPL(comedi_usb_auto_config);
  64. /**
  65. * comedi_usb_auto_unconfig() - Unconfigure/disconnect a USB COMEDI device
  66. * @intf: USB interface.
  67. *
  68. * Typically called from the usb_driver (*disconnect) function.
  69. * Auto-unconfigure a COMEDI device attached to this USB interface, using a
  70. * pointer to the &struct device embedded in *@intf as the hardware device.
  71. * The COMEDI driver's "detach" handler will be called during unconfiguration
  72. * of the COMEDI device.
  73. *
  74. * Note that the COMEDI device may have already been unconfigured using the
  75. * %COMEDI_DEVCONFIG ioctl, in which case this attempt to unconfigure it
  76. * again should be ignored.
  77. */
  78. void comedi_usb_auto_unconfig(struct usb_interface *intf)
  79. {
  80. comedi_auto_unconfig(&intf->dev);
  81. }
  82. EXPORT_SYMBOL_GPL(comedi_usb_auto_unconfig);
  83. /**
  84. * comedi_usb_driver_register() - Register a USB COMEDI driver
  85. * @comedi_driver: COMEDI driver to be registered.
  86. * @usb_driver: USB driver to be registered.
  87. *
  88. * This function is called from the module_init() of USB COMEDI driver modules
  89. * to register the COMEDI driver and the USB driver. Do not call it directly,
  90. * use the module_comedi_usb_driver() helper macro instead.
  91. *
  92. * Return: %0 on success, or a negative error number on failure.
  93. */
  94. int comedi_usb_driver_register(struct comedi_driver *comedi_driver,
  95. struct usb_driver *usb_driver)
  96. {
  97. int ret;
  98. ret = comedi_driver_register(comedi_driver);
  99. if (ret < 0)
  100. return ret;
  101. ret = usb_register(usb_driver);
  102. if (ret < 0) {
  103. comedi_driver_unregister(comedi_driver);
  104. return ret;
  105. }
  106. return 0;
  107. }
  108. EXPORT_SYMBOL_GPL(comedi_usb_driver_register);
  109. /**
  110. * comedi_usb_driver_unregister() - Unregister a USB COMEDI driver
  111. * @comedi_driver: COMEDI driver to be registered.
  112. * @usb_driver: USB driver to be registered.
  113. *
  114. * This function is called from the module_exit() of USB COMEDI driver modules
  115. * to unregister the USB driver and the COMEDI driver. Do not call it
  116. * directly, use the module_comedi_usb_driver() helper macro instead.
  117. */
  118. void comedi_usb_driver_unregister(struct comedi_driver *comedi_driver,
  119. struct usb_driver *usb_driver)
  120. {
  121. usb_deregister(usb_driver);
  122. comedi_driver_unregister(comedi_driver);
  123. }
  124. EXPORT_SYMBOL_GPL(comedi_usb_driver_unregister);
  125. static int __init comedi_usb_init(void)
  126. {
  127. return 0;
  128. }
  129. module_init(comedi_usb_init);
  130. static void __exit comedi_usb_exit(void)
  131. {
  132. }
  133. module_exit(comedi_usb_exit);
  134. MODULE_AUTHOR("https://www.comedi.org");
  135. MODULE_DESCRIPTION("Comedi USB interface module");
  136. MODULE_LICENSE("GPL");