blob: 9c946d40b8944b56ae3ba7d99e16492623934ee0 [file] [log] [blame]
H Hartley Sweetenabac8b52013-01-30 15:21:49 -07001/*
2 * comedi_usb.c
3 * Comedi USB 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.
H Hartley Sweetenabac8b52013-01-30 15:21:49 -070017 */
18
Ian Abbottba9ac252014-10-31 17:47:38 +000019#include <linux/module.h>
H Hartley Sweetenabac8b52013-01-30 15:21:49 -070020
Ian Abbott08f6b952015-01-27 17:49:02 +000021#include "comedi_usb.h"
H Hartley Sweetenabac8b52013-01-30 15:21:49 -070022
23/**
Ian Abbottddf837e2015-09-30 17:37:13 +010024 * comedi_to_usb_interface() - Return USB interface attached to COMEDI device
25 * @dev: COMEDI device.
26 *
27 * Assuming @dev->hw_dev is non-%NULL, it is assumed to be pointing to a
28 * a &struct device embedded in a &struct usb_interface.
29 *
30 * Return: Attached USB interface if @dev->hw_dev is non-%NULL.
31 * Return %NULL if @dev->hw_dev is %NULL.
H Hartley Sweetenabac8b52013-01-30 15:21:49 -070032 */
33struct usb_interface *comedi_to_usb_interface(struct comedi_device *dev)
34{
35 return dev->hw_dev ? to_usb_interface(dev->hw_dev) : NULL;
36}
37EXPORT_SYMBOL_GPL(comedi_to_usb_interface);
38
39/**
Ian Abbottddf837e2015-09-30 17:37:13 +010040 * comedi_to_usb_dev() - Return USB device attached to COMEDI device
41 * @dev: COMEDI device.
42 *
43 * Assuming @dev->hw_dev is non-%NULL, it is assumed to be pointing to a
44 * a &struct device embedded in a &struct usb_interface.
45 *
46 * Return: USB device to which the USB interface belongs if @dev->hw_dev is
47 * non-%NULL. Return %NULL if @dev->hw_dev is %NULL.
H Hartley Sweeten61dd1492013-05-20 14:20:02 -070048 */
49struct usb_device *comedi_to_usb_dev(struct comedi_device *dev)
50{
51 struct usb_interface *intf = comedi_to_usb_interface(dev);
52
53 return intf ? interface_to_usbdev(intf) : NULL;
54}
55EXPORT_SYMBOL_GPL(comedi_to_usb_dev);
56
57/**
Ian Abbottddf837e2015-09-30 17:37:13 +010058 * comedi_usb_auto_config() - Configure/probe a USB COMEDI driver
59 * @intf: USB interface.
60 * @driver: Registered COMEDI driver.
61 * @context: Driver specific data, passed to comedi_auto_config().
H Hartley Sweetenabac8b52013-01-30 15:21:49 -070062 *
Ian Abbottddf837e2015-09-30 17:37:13 +010063 * Typically called from the usb_driver (*probe) function. Auto-configure a
64 * COMEDI device, using a pointer to the &struct device embedded in *@intf as
65 * the hardware device. The @context value gets passed through to @driver's
66 * "auto_attach" handler. The "auto_attach" handler may call
67 * comedi_to_usb_interface() on the passed in COMEDI device to recover @intf.
68 *
69 * Return: The result of calling comedi_auto_config() (%0 on success, or
70 * a negative error number on failure).
H Hartley Sweetenabac8b52013-01-30 15:21:49 -070071 */
72int comedi_usb_auto_config(struct usb_interface *intf,
H Hartley Sweeten55ab4f62013-02-05 17:23:40 -070073 struct comedi_driver *driver,
74 unsigned long context)
H Hartley Sweetenabac8b52013-01-30 15:21:49 -070075{
H Hartley Sweeten55ab4f62013-02-05 17:23:40 -070076 return comedi_auto_config(&intf->dev, driver, context);
H Hartley Sweetenabac8b52013-01-30 15:21:49 -070077}
78EXPORT_SYMBOL_GPL(comedi_usb_auto_config);
79
80/**
Ian Abbottddf837e2015-09-30 17:37:13 +010081 * comedi_usb_auto_unconfig() - Unconfigure/disconnect a USB COMEDI device
82 * @intf: USB interface.
H Hartley Sweetenabac8b52013-01-30 15:21:49 -070083 *
84 * Typically called from the usb_driver (*disconnect) function.
Ian Abbottddf837e2015-09-30 17:37:13 +010085 * Auto-unconfigure a COMEDI device attached to this USB interface, using a
86 * pointer to the &struct device embedded in *@intf as the hardware device.
87 * The COMEDI driver's "detach" handler will be called during unconfiguration
88 * of the COMEDI device.
89 *
90 * Note that the COMEDI device may have already been unconfigured using the
91 * %COMEDI_DEVCONFIG ioctl, in which case this attempt to unconfigure it
92 * again should be ignored.
H Hartley Sweetenabac8b52013-01-30 15:21:49 -070093 */
94void comedi_usb_auto_unconfig(struct usb_interface *intf)
95{
96 comedi_auto_unconfig(&intf->dev);
97}
98EXPORT_SYMBOL_GPL(comedi_usb_auto_unconfig);
99
100/**
Ian Abbottddf837e2015-09-30 17:37:13 +0100101 * comedi_usb_driver_register() - Register a USB COMEDI driver
102 * @comedi_driver: COMEDI driver to be registered.
103 * @usb_driver: USB driver to be registered.
H Hartley Sweetenabac8b52013-01-30 15:21:49 -0700104 *
Ian Abbottddf837e2015-09-30 17:37:13 +0100105 * This function is called from the module_init() of USB COMEDI driver modules
106 * to register the COMEDI driver and the USB driver. Do not call it directly,
107 * use the module_comedi_usb_driver() helper macro instead.
108 *
109 * Return: %0 on success, or a negative error number on failure.
H Hartley Sweetenabac8b52013-01-30 15:21:49 -0700110 */
111int comedi_usb_driver_register(struct comedi_driver *comedi_driver,
112 struct usb_driver *usb_driver)
113{
114 int ret;
115
116 ret = comedi_driver_register(comedi_driver);
117 if (ret < 0)
118 return ret;
119
120 ret = usb_register(usb_driver);
121 if (ret < 0) {
122 comedi_driver_unregister(comedi_driver);
123 return ret;
124 }
125
126 return 0;
127}
128EXPORT_SYMBOL_GPL(comedi_usb_driver_register);
129
130/**
Ian Abbottddf837e2015-09-30 17:37:13 +0100131 * comedi_usb_driver_unregister() - Unregister a USB COMEDI driver
132 * @comedi_driver: COMEDI driver to be registered.
133 * @usb_driver: USB driver to be registered.
H Hartley Sweetenabac8b52013-01-30 15:21:49 -0700134 *
Ian Abbottddf837e2015-09-30 17:37:13 +0100135 * This function is called from the module_exit() of USB COMEDI driver modules
136 * to unregister the USB driver and the COMEDI driver. Do not call it
137 * directly, use the module_comedi_usb_driver() helper macro instead.
H Hartley Sweetenabac8b52013-01-30 15:21:49 -0700138 */
139void comedi_usb_driver_unregister(struct comedi_driver *comedi_driver,
140 struct usb_driver *usb_driver)
141{
142 usb_deregister(usb_driver);
143 comedi_driver_unregister(comedi_driver);
144}
145EXPORT_SYMBOL_GPL(comedi_usb_driver_unregister);
Ian Abbottba9ac252014-10-31 17:47:38 +0000146
147static int __init comedi_usb_init(void)
148{
149 return 0;
150}
151module_init(comedi_usb_init);
152
153static void __exit comedi_usb_exit(void)
154{
155}
156module_exit(comedi_usb_exit);
157
158MODULE_AUTHOR("http://www.comedi.org");
159MODULE_DESCRIPTION("Comedi USB interface module");
160MODULE_LICENSE("GPL");