162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0+
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * comedi_usb.c
462306a36Sopenharmony_ci * Comedi USB driver specific functions.
562306a36Sopenharmony_ci *
662306a36Sopenharmony_ci * COMEDI - Linux Control and Measurement Device Interface
762306a36Sopenharmony_ci * Copyright (C) 1997-2000 David A. Schleef <ds@schleef.org>
862306a36Sopenharmony_ci */
962306a36Sopenharmony_ci
1062306a36Sopenharmony_ci#include <linux/module.h>
1162306a36Sopenharmony_ci#include <linux/comedi/comedi_usb.h>
1262306a36Sopenharmony_ci
1362306a36Sopenharmony_ci/**
1462306a36Sopenharmony_ci * comedi_to_usb_interface() - Return USB interface attached to COMEDI device
1562306a36Sopenharmony_ci * @dev: COMEDI device.
1662306a36Sopenharmony_ci *
1762306a36Sopenharmony_ci * Assuming @dev->hw_dev is non-%NULL, it is assumed to be pointing to a
1862306a36Sopenharmony_ci * a &struct device embedded in a &struct usb_interface.
1962306a36Sopenharmony_ci *
2062306a36Sopenharmony_ci * Return: Attached USB interface if @dev->hw_dev is non-%NULL.
2162306a36Sopenharmony_ci * Return %NULL if @dev->hw_dev is %NULL.
2262306a36Sopenharmony_ci */
2362306a36Sopenharmony_cistruct usb_interface *comedi_to_usb_interface(struct comedi_device *dev)
2462306a36Sopenharmony_ci{
2562306a36Sopenharmony_ci	return dev->hw_dev ? to_usb_interface(dev->hw_dev) : NULL;
2662306a36Sopenharmony_ci}
2762306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(comedi_to_usb_interface);
2862306a36Sopenharmony_ci
2962306a36Sopenharmony_ci/**
3062306a36Sopenharmony_ci * comedi_to_usb_dev() - Return USB device attached to COMEDI device
3162306a36Sopenharmony_ci * @dev: COMEDI device.
3262306a36Sopenharmony_ci *
3362306a36Sopenharmony_ci * Assuming @dev->hw_dev is non-%NULL, it is assumed to be pointing to a
3462306a36Sopenharmony_ci * a &struct device embedded in a &struct usb_interface.
3562306a36Sopenharmony_ci *
3662306a36Sopenharmony_ci * Return: USB device to which the USB interface belongs if @dev->hw_dev is
3762306a36Sopenharmony_ci * non-%NULL.  Return %NULL if @dev->hw_dev is %NULL.
3862306a36Sopenharmony_ci */
3962306a36Sopenharmony_cistruct usb_device *comedi_to_usb_dev(struct comedi_device *dev)
4062306a36Sopenharmony_ci{
4162306a36Sopenharmony_ci	struct usb_interface *intf = comedi_to_usb_interface(dev);
4262306a36Sopenharmony_ci
4362306a36Sopenharmony_ci	return intf ? interface_to_usbdev(intf) : NULL;
4462306a36Sopenharmony_ci}
4562306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(comedi_to_usb_dev);
4662306a36Sopenharmony_ci
4762306a36Sopenharmony_ci/**
4862306a36Sopenharmony_ci * comedi_usb_auto_config() - Configure/probe a USB COMEDI driver
4962306a36Sopenharmony_ci * @intf: USB interface.
5062306a36Sopenharmony_ci * @driver: Registered COMEDI driver.
5162306a36Sopenharmony_ci * @context: Driver specific data, passed to comedi_auto_config().
5262306a36Sopenharmony_ci *
5362306a36Sopenharmony_ci * Typically called from the usb_driver (*probe) function.  Auto-configure a
5462306a36Sopenharmony_ci * COMEDI device, using a pointer to the &struct device embedded in *@intf as
5562306a36Sopenharmony_ci * the hardware device.  The @context value gets passed through to @driver's
5662306a36Sopenharmony_ci * "auto_attach" handler.  The "auto_attach" handler may call
5762306a36Sopenharmony_ci * comedi_to_usb_interface() on the passed in COMEDI device to recover @intf.
5862306a36Sopenharmony_ci *
5962306a36Sopenharmony_ci * Return: The result of calling comedi_auto_config() (%0 on success, or
6062306a36Sopenharmony_ci * a negative error number on failure).
6162306a36Sopenharmony_ci */
6262306a36Sopenharmony_ciint comedi_usb_auto_config(struct usb_interface *intf,
6362306a36Sopenharmony_ci			   struct comedi_driver *driver,
6462306a36Sopenharmony_ci			   unsigned long context)
6562306a36Sopenharmony_ci{
6662306a36Sopenharmony_ci	return comedi_auto_config(&intf->dev, driver, context);
6762306a36Sopenharmony_ci}
6862306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(comedi_usb_auto_config);
6962306a36Sopenharmony_ci
7062306a36Sopenharmony_ci/**
7162306a36Sopenharmony_ci * comedi_usb_auto_unconfig() - Unconfigure/disconnect a USB COMEDI device
7262306a36Sopenharmony_ci * @intf: USB interface.
7362306a36Sopenharmony_ci *
7462306a36Sopenharmony_ci * Typically called from the usb_driver (*disconnect) function.
7562306a36Sopenharmony_ci * Auto-unconfigure a COMEDI device attached to this USB interface, using a
7662306a36Sopenharmony_ci * pointer to the &struct device embedded in *@intf as the hardware device.
7762306a36Sopenharmony_ci * The COMEDI driver's "detach" handler will be called during unconfiguration
7862306a36Sopenharmony_ci * of the COMEDI device.
7962306a36Sopenharmony_ci *
8062306a36Sopenharmony_ci * Note that the COMEDI device may have already been unconfigured using the
8162306a36Sopenharmony_ci * %COMEDI_DEVCONFIG ioctl, in which case this attempt to unconfigure it
8262306a36Sopenharmony_ci * again should be ignored.
8362306a36Sopenharmony_ci */
8462306a36Sopenharmony_civoid comedi_usb_auto_unconfig(struct usb_interface *intf)
8562306a36Sopenharmony_ci{
8662306a36Sopenharmony_ci	comedi_auto_unconfig(&intf->dev);
8762306a36Sopenharmony_ci}
8862306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(comedi_usb_auto_unconfig);
8962306a36Sopenharmony_ci
9062306a36Sopenharmony_ci/**
9162306a36Sopenharmony_ci * comedi_usb_driver_register() - Register a USB COMEDI driver
9262306a36Sopenharmony_ci * @comedi_driver: COMEDI driver to be registered.
9362306a36Sopenharmony_ci * @usb_driver: USB driver to be registered.
9462306a36Sopenharmony_ci *
9562306a36Sopenharmony_ci * This function is called from the module_init() of USB COMEDI driver modules
9662306a36Sopenharmony_ci * to register the COMEDI driver and the USB driver.  Do not call it directly,
9762306a36Sopenharmony_ci * use the module_comedi_usb_driver() helper macro instead.
9862306a36Sopenharmony_ci *
9962306a36Sopenharmony_ci * Return: %0 on success, or a negative error number on failure.
10062306a36Sopenharmony_ci */
10162306a36Sopenharmony_ciint comedi_usb_driver_register(struct comedi_driver *comedi_driver,
10262306a36Sopenharmony_ci			       struct usb_driver *usb_driver)
10362306a36Sopenharmony_ci{
10462306a36Sopenharmony_ci	int ret;
10562306a36Sopenharmony_ci
10662306a36Sopenharmony_ci	ret = comedi_driver_register(comedi_driver);
10762306a36Sopenharmony_ci	if (ret < 0)
10862306a36Sopenharmony_ci		return ret;
10962306a36Sopenharmony_ci
11062306a36Sopenharmony_ci	ret = usb_register(usb_driver);
11162306a36Sopenharmony_ci	if (ret < 0) {
11262306a36Sopenharmony_ci		comedi_driver_unregister(comedi_driver);
11362306a36Sopenharmony_ci		return ret;
11462306a36Sopenharmony_ci	}
11562306a36Sopenharmony_ci
11662306a36Sopenharmony_ci	return 0;
11762306a36Sopenharmony_ci}
11862306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(comedi_usb_driver_register);
11962306a36Sopenharmony_ci
12062306a36Sopenharmony_ci/**
12162306a36Sopenharmony_ci * comedi_usb_driver_unregister() - Unregister a USB COMEDI driver
12262306a36Sopenharmony_ci * @comedi_driver: COMEDI driver to be registered.
12362306a36Sopenharmony_ci * @usb_driver: USB driver to be registered.
12462306a36Sopenharmony_ci *
12562306a36Sopenharmony_ci * This function is called from the module_exit() of USB COMEDI driver modules
12662306a36Sopenharmony_ci * to unregister the USB driver and the COMEDI driver.  Do not call it
12762306a36Sopenharmony_ci * directly, use the module_comedi_usb_driver() helper macro instead.
12862306a36Sopenharmony_ci */
12962306a36Sopenharmony_civoid comedi_usb_driver_unregister(struct comedi_driver *comedi_driver,
13062306a36Sopenharmony_ci				  struct usb_driver *usb_driver)
13162306a36Sopenharmony_ci{
13262306a36Sopenharmony_ci	usb_deregister(usb_driver);
13362306a36Sopenharmony_ci	comedi_driver_unregister(comedi_driver);
13462306a36Sopenharmony_ci}
13562306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(comedi_usb_driver_unregister);
13662306a36Sopenharmony_ci
13762306a36Sopenharmony_cistatic int __init comedi_usb_init(void)
13862306a36Sopenharmony_ci{
13962306a36Sopenharmony_ci	return 0;
14062306a36Sopenharmony_ci}
14162306a36Sopenharmony_cimodule_init(comedi_usb_init);
14262306a36Sopenharmony_ci
14362306a36Sopenharmony_cistatic void __exit comedi_usb_exit(void)
14462306a36Sopenharmony_ci{
14562306a36Sopenharmony_ci}
14662306a36Sopenharmony_cimodule_exit(comedi_usb_exit);
14762306a36Sopenharmony_ci
14862306a36Sopenharmony_ciMODULE_AUTHOR("https://www.comedi.org");
14962306a36Sopenharmony_ciMODULE_DESCRIPTION("Comedi USB interface module");
15062306a36Sopenharmony_ciMODULE_LICENSE("GPL");
151