blob: d11ea148ebf8bf85e447a8df4630b89c29b76a3c [file] [log] [blame]
Greg Kroah-Hartmane184e2b2017-11-07 14:58:43 +01001// SPDX-License-Identifier: GPL-2.0+
H Hartley Sweetenabac8b52013-01-30 15:21:49 -07002/*
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 Sweetenabac8b52013-01-30 15:21:49 -07008 */
9
Ian Abbottba9ac252014-10-31 17:47:38 +000010#include <linux/module.h>
Ian Abbottdf0e68c12021-11-17 12:05:59 +000011#include <linux/comedi/comedi_usb.h>
H Hartley Sweetenabac8b52013-01-30 15:21:49 -070012
13/**
Ian Abbottddf837e2015-09-30 17:37:13 +010014 * comedi_to_usb_interface() - Return USB interface attached to COMEDI device
15 * @dev: COMEDI device.
16 *
17 * Assuming @dev->hw_dev is non-%NULL, it is assumed to be pointing to a
18 * a &struct device embedded in a &struct usb_interface.
19 *
20 * Return: Attached USB interface if @dev->hw_dev is non-%NULL.
21 * Return %NULL if @dev->hw_dev is %NULL.
H Hartley Sweetenabac8b52013-01-30 15:21:49 -070022 */
23struct usb_interface *comedi_to_usb_interface(struct comedi_device *dev)
24{
25 return dev->hw_dev ? to_usb_interface(dev->hw_dev) : NULL;
26}
27EXPORT_SYMBOL_GPL(comedi_to_usb_interface);
28
29/**
Ian Abbottddf837e2015-09-30 17:37:13 +010030 * comedi_to_usb_dev() - Return USB device attached to COMEDI device
31 * @dev: COMEDI device.
32 *
33 * Assuming @dev->hw_dev is non-%NULL, it is assumed to be pointing to a
34 * a &struct device embedded in a &struct usb_interface.
35 *
36 * Return: USB device to which the USB interface belongs if @dev->hw_dev is
37 * non-%NULL. Return %NULL if @dev->hw_dev is %NULL.
H Hartley Sweeten61dd1492013-05-20 14:20:02 -070038 */
39struct usb_device *comedi_to_usb_dev(struct comedi_device *dev)
40{
41 struct usb_interface *intf = comedi_to_usb_interface(dev);
42
43 return intf ? interface_to_usbdev(intf) : NULL;
44}
45EXPORT_SYMBOL_GPL(comedi_to_usb_dev);
46
47/**
Ian Abbottddf837e2015-09-30 17:37:13 +010048 * comedi_usb_auto_config() - Configure/probe a USB COMEDI driver
49 * @intf: USB interface.
50 * @driver: Registered COMEDI driver.
51 * @context: Driver specific data, passed to comedi_auto_config().
H Hartley Sweetenabac8b52013-01-30 15:21:49 -070052 *
Ian Abbottddf837e2015-09-30 17:37:13 +010053 * Typically called from the usb_driver (*probe) function. Auto-configure a
54 * COMEDI device, using a pointer to the &struct device embedded in *@intf as
55 * the hardware device. The @context value gets passed through to @driver's
56 * "auto_attach" handler. The "auto_attach" handler may call
57 * comedi_to_usb_interface() on the passed in COMEDI device to recover @intf.
58 *
59 * Return: The result of calling comedi_auto_config() (%0 on success, or
60 * a negative error number on failure).
H Hartley Sweetenabac8b52013-01-30 15:21:49 -070061 */
62int comedi_usb_auto_config(struct usb_interface *intf,
H Hartley Sweeten55ab4f62013-02-05 17:23:40 -070063 struct comedi_driver *driver,
64 unsigned long context)
H Hartley Sweetenabac8b52013-01-30 15:21:49 -070065{
H Hartley Sweeten55ab4f62013-02-05 17:23:40 -070066 return comedi_auto_config(&intf->dev, driver, context);
H Hartley Sweetenabac8b52013-01-30 15:21:49 -070067}
68EXPORT_SYMBOL_GPL(comedi_usb_auto_config);
69
70/**
Ian Abbottddf837e2015-09-30 17:37:13 +010071 * comedi_usb_auto_unconfig() - Unconfigure/disconnect a USB COMEDI device
72 * @intf: USB interface.
H Hartley Sweetenabac8b52013-01-30 15:21:49 -070073 *
74 * Typically called from the usb_driver (*disconnect) function.
Ian Abbottddf837e2015-09-30 17:37:13 +010075 * Auto-unconfigure a COMEDI device attached to this USB interface, using a
76 * pointer to the &struct device embedded in *@intf as the hardware device.
77 * The COMEDI driver's "detach" handler will be called during unconfiguration
78 * of the COMEDI device.
79 *
80 * Note that the COMEDI device may have already been unconfigured using the
81 * %COMEDI_DEVCONFIG ioctl, in which case this attempt to unconfigure it
82 * again should be ignored.
H Hartley Sweetenabac8b52013-01-30 15:21:49 -070083 */
84void comedi_usb_auto_unconfig(struct usb_interface *intf)
85{
86 comedi_auto_unconfig(&intf->dev);
87}
88EXPORT_SYMBOL_GPL(comedi_usb_auto_unconfig);
89
90/**
Ian Abbottddf837e2015-09-30 17:37:13 +010091 * comedi_usb_driver_register() - Register a USB COMEDI driver
92 * @comedi_driver: COMEDI driver to be registered.
93 * @usb_driver: USB driver to be registered.
H Hartley Sweetenabac8b52013-01-30 15:21:49 -070094 *
Ian Abbottddf837e2015-09-30 17:37:13 +010095 * This function is called from the module_init() of USB COMEDI driver modules
96 * to register the COMEDI driver and the USB driver. Do not call it directly,
97 * use the module_comedi_usb_driver() helper macro instead.
98 *
99 * Return: %0 on success, or a negative error number on failure.
H Hartley Sweetenabac8b52013-01-30 15:21:49 -0700100 */
101int comedi_usb_driver_register(struct comedi_driver *comedi_driver,
102 struct usb_driver *usb_driver)
103{
104 int ret;
105
106 ret = comedi_driver_register(comedi_driver);
107 if (ret < 0)
108 return ret;
109
110 ret = usb_register(usb_driver);
111 if (ret < 0) {
112 comedi_driver_unregister(comedi_driver);
113 return ret;
114 }
115
116 return 0;
117}
118EXPORT_SYMBOL_GPL(comedi_usb_driver_register);
119
120/**
Ian Abbottddf837e2015-09-30 17:37:13 +0100121 * comedi_usb_driver_unregister() - Unregister a USB COMEDI driver
122 * @comedi_driver: COMEDI driver to be registered.
123 * @usb_driver: USB driver to be registered.
H Hartley Sweetenabac8b52013-01-30 15:21:49 -0700124 *
Ian Abbottddf837e2015-09-30 17:37:13 +0100125 * This function is called from the module_exit() of USB COMEDI driver modules
126 * to unregister the USB driver and the COMEDI driver. Do not call it
127 * directly, use the module_comedi_usb_driver() helper macro instead.
H Hartley Sweetenabac8b52013-01-30 15:21:49 -0700128 */
129void comedi_usb_driver_unregister(struct comedi_driver *comedi_driver,
130 struct usb_driver *usb_driver)
131{
132 usb_deregister(usb_driver);
133 comedi_driver_unregister(comedi_driver);
134}
135EXPORT_SYMBOL_GPL(comedi_usb_driver_unregister);
Ian Abbottba9ac252014-10-31 17:47:38 +0000136
137static int __init comedi_usb_init(void)
138{
139 return 0;
140}
141module_init(comedi_usb_init);
142
143static void __exit comedi_usb_exit(void)
144{
145}
146module_exit(comedi_usb_exit);
147
Alexander A. Klimov13d8b1f2020-07-23 21:40:53 +0200148MODULE_AUTHOR("https://www.comedi.org");
Ian Abbottba9ac252014-10-31 17:47:38 +0000149MODULE_DESCRIPTION("Comedi USB interface module");
150MODULE_LICENSE("GPL");