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