18ffdff6aSGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0+
28ffdff6aSGreg Kroah-Hartman /*
38ffdff6aSGreg Kroah-Hartman * comedi_usb.c
48ffdff6aSGreg Kroah-Hartman * Comedi USB driver specific functions.
58ffdff6aSGreg Kroah-Hartman *
68ffdff6aSGreg Kroah-Hartman * COMEDI - Linux Control and Measurement Device Interface
78ffdff6aSGreg Kroah-Hartman * Copyright (C) 1997-2000 David A. Schleef <ds@schleef.org>
88ffdff6aSGreg Kroah-Hartman */
98ffdff6aSGreg Kroah-Hartman
108ffdff6aSGreg Kroah-Hartman #include <linux/module.h>
11*df0e68c1SIan Abbott #include <linux/comedi/comedi_usb.h>
128ffdff6aSGreg Kroah-Hartman
138ffdff6aSGreg Kroah-Hartman /**
148ffdff6aSGreg Kroah-Hartman * comedi_to_usb_interface() - Return USB interface attached to COMEDI device
158ffdff6aSGreg Kroah-Hartman * @dev: COMEDI device.
168ffdff6aSGreg Kroah-Hartman *
178ffdff6aSGreg Kroah-Hartman * Assuming @dev->hw_dev is non-%NULL, it is assumed to be pointing to a
188ffdff6aSGreg Kroah-Hartman * a &struct device embedded in a &struct usb_interface.
198ffdff6aSGreg Kroah-Hartman *
208ffdff6aSGreg Kroah-Hartman * Return: Attached USB interface if @dev->hw_dev is non-%NULL.
218ffdff6aSGreg Kroah-Hartman * Return %NULL if @dev->hw_dev is %NULL.
228ffdff6aSGreg Kroah-Hartman */
comedi_to_usb_interface(struct comedi_device * dev)238ffdff6aSGreg Kroah-Hartman struct usb_interface *comedi_to_usb_interface(struct comedi_device *dev)
248ffdff6aSGreg Kroah-Hartman {
258ffdff6aSGreg Kroah-Hartman return dev->hw_dev ? to_usb_interface(dev->hw_dev) : NULL;
268ffdff6aSGreg Kroah-Hartman }
278ffdff6aSGreg Kroah-Hartman EXPORT_SYMBOL_GPL(comedi_to_usb_interface);
288ffdff6aSGreg Kroah-Hartman
298ffdff6aSGreg Kroah-Hartman /**
308ffdff6aSGreg Kroah-Hartman * comedi_to_usb_dev() - Return USB device attached to COMEDI device
318ffdff6aSGreg Kroah-Hartman * @dev: COMEDI device.
328ffdff6aSGreg Kroah-Hartman *
338ffdff6aSGreg Kroah-Hartman * Assuming @dev->hw_dev is non-%NULL, it is assumed to be pointing to a
348ffdff6aSGreg Kroah-Hartman * a &struct device embedded in a &struct usb_interface.
358ffdff6aSGreg Kroah-Hartman *
368ffdff6aSGreg Kroah-Hartman * Return: USB device to which the USB interface belongs if @dev->hw_dev is
378ffdff6aSGreg Kroah-Hartman * non-%NULL. Return %NULL if @dev->hw_dev is %NULL.
388ffdff6aSGreg Kroah-Hartman */
comedi_to_usb_dev(struct comedi_device * dev)398ffdff6aSGreg Kroah-Hartman struct usb_device *comedi_to_usb_dev(struct comedi_device *dev)
408ffdff6aSGreg Kroah-Hartman {
418ffdff6aSGreg Kroah-Hartman struct usb_interface *intf = comedi_to_usb_interface(dev);
428ffdff6aSGreg Kroah-Hartman
438ffdff6aSGreg Kroah-Hartman return intf ? interface_to_usbdev(intf) : NULL;
448ffdff6aSGreg Kroah-Hartman }
458ffdff6aSGreg Kroah-Hartman EXPORT_SYMBOL_GPL(comedi_to_usb_dev);
468ffdff6aSGreg Kroah-Hartman
478ffdff6aSGreg Kroah-Hartman /**
488ffdff6aSGreg Kroah-Hartman * comedi_usb_auto_config() - Configure/probe a USB COMEDI driver
498ffdff6aSGreg Kroah-Hartman * @intf: USB interface.
508ffdff6aSGreg Kroah-Hartman * @driver: Registered COMEDI driver.
518ffdff6aSGreg Kroah-Hartman * @context: Driver specific data, passed to comedi_auto_config().
528ffdff6aSGreg Kroah-Hartman *
538ffdff6aSGreg Kroah-Hartman * Typically called from the usb_driver (*probe) function. Auto-configure a
548ffdff6aSGreg Kroah-Hartman * COMEDI device, using a pointer to the &struct device embedded in *@intf as
558ffdff6aSGreg Kroah-Hartman * the hardware device. The @context value gets passed through to @driver's
568ffdff6aSGreg Kroah-Hartman * "auto_attach" handler. The "auto_attach" handler may call
578ffdff6aSGreg Kroah-Hartman * comedi_to_usb_interface() on the passed in COMEDI device to recover @intf.
588ffdff6aSGreg Kroah-Hartman *
598ffdff6aSGreg Kroah-Hartman * Return: The result of calling comedi_auto_config() (%0 on success, or
608ffdff6aSGreg Kroah-Hartman * a negative error number on failure).
618ffdff6aSGreg Kroah-Hartman */
comedi_usb_auto_config(struct usb_interface * intf,struct comedi_driver * driver,unsigned long context)628ffdff6aSGreg Kroah-Hartman int comedi_usb_auto_config(struct usb_interface *intf,
638ffdff6aSGreg Kroah-Hartman struct comedi_driver *driver,
648ffdff6aSGreg Kroah-Hartman unsigned long context)
658ffdff6aSGreg Kroah-Hartman {
668ffdff6aSGreg Kroah-Hartman return comedi_auto_config(&intf->dev, driver, context);
678ffdff6aSGreg Kroah-Hartman }
688ffdff6aSGreg Kroah-Hartman EXPORT_SYMBOL_GPL(comedi_usb_auto_config);
698ffdff6aSGreg Kroah-Hartman
708ffdff6aSGreg Kroah-Hartman /**
718ffdff6aSGreg Kroah-Hartman * comedi_usb_auto_unconfig() - Unconfigure/disconnect a USB COMEDI device
728ffdff6aSGreg Kroah-Hartman * @intf: USB interface.
738ffdff6aSGreg Kroah-Hartman *
748ffdff6aSGreg Kroah-Hartman * Typically called from the usb_driver (*disconnect) function.
758ffdff6aSGreg Kroah-Hartman * Auto-unconfigure a COMEDI device attached to this USB interface, using a
768ffdff6aSGreg Kroah-Hartman * pointer to the &struct device embedded in *@intf as the hardware device.
778ffdff6aSGreg Kroah-Hartman * The COMEDI driver's "detach" handler will be called during unconfiguration
788ffdff6aSGreg Kroah-Hartman * of the COMEDI device.
798ffdff6aSGreg Kroah-Hartman *
808ffdff6aSGreg Kroah-Hartman * Note that the COMEDI device may have already been unconfigured using the
818ffdff6aSGreg Kroah-Hartman * %COMEDI_DEVCONFIG ioctl, in which case this attempt to unconfigure it
828ffdff6aSGreg Kroah-Hartman * again should be ignored.
838ffdff6aSGreg Kroah-Hartman */
comedi_usb_auto_unconfig(struct usb_interface * intf)848ffdff6aSGreg Kroah-Hartman void comedi_usb_auto_unconfig(struct usb_interface *intf)
858ffdff6aSGreg Kroah-Hartman {
868ffdff6aSGreg Kroah-Hartman comedi_auto_unconfig(&intf->dev);
878ffdff6aSGreg Kroah-Hartman }
888ffdff6aSGreg Kroah-Hartman EXPORT_SYMBOL_GPL(comedi_usb_auto_unconfig);
898ffdff6aSGreg Kroah-Hartman
908ffdff6aSGreg Kroah-Hartman /**
918ffdff6aSGreg Kroah-Hartman * comedi_usb_driver_register() - Register a USB COMEDI driver
928ffdff6aSGreg Kroah-Hartman * @comedi_driver: COMEDI driver to be registered.
938ffdff6aSGreg Kroah-Hartman * @usb_driver: USB driver to be registered.
948ffdff6aSGreg Kroah-Hartman *
958ffdff6aSGreg Kroah-Hartman * This function is called from the module_init() of USB COMEDI driver modules
968ffdff6aSGreg Kroah-Hartman * to register the COMEDI driver and the USB driver. Do not call it directly,
978ffdff6aSGreg Kroah-Hartman * use the module_comedi_usb_driver() helper macro instead.
988ffdff6aSGreg Kroah-Hartman *
998ffdff6aSGreg Kroah-Hartman * Return: %0 on success, or a negative error number on failure.
1008ffdff6aSGreg Kroah-Hartman */
comedi_usb_driver_register(struct comedi_driver * comedi_driver,struct usb_driver * usb_driver)1018ffdff6aSGreg Kroah-Hartman int comedi_usb_driver_register(struct comedi_driver *comedi_driver,
1028ffdff6aSGreg Kroah-Hartman struct usb_driver *usb_driver)
1038ffdff6aSGreg Kroah-Hartman {
1048ffdff6aSGreg Kroah-Hartman int ret;
1058ffdff6aSGreg Kroah-Hartman
1068ffdff6aSGreg Kroah-Hartman ret = comedi_driver_register(comedi_driver);
1078ffdff6aSGreg Kroah-Hartman if (ret < 0)
1088ffdff6aSGreg Kroah-Hartman return ret;
1098ffdff6aSGreg Kroah-Hartman
1108ffdff6aSGreg Kroah-Hartman ret = usb_register(usb_driver);
1118ffdff6aSGreg Kroah-Hartman if (ret < 0) {
1128ffdff6aSGreg Kroah-Hartman comedi_driver_unregister(comedi_driver);
1138ffdff6aSGreg Kroah-Hartman return ret;
1148ffdff6aSGreg Kroah-Hartman }
1158ffdff6aSGreg Kroah-Hartman
1168ffdff6aSGreg Kroah-Hartman return 0;
1178ffdff6aSGreg Kroah-Hartman }
1188ffdff6aSGreg Kroah-Hartman EXPORT_SYMBOL_GPL(comedi_usb_driver_register);
1198ffdff6aSGreg Kroah-Hartman
1208ffdff6aSGreg Kroah-Hartman /**
1218ffdff6aSGreg Kroah-Hartman * comedi_usb_driver_unregister() - Unregister a USB COMEDI driver
1228ffdff6aSGreg Kroah-Hartman * @comedi_driver: COMEDI driver to be registered.
1238ffdff6aSGreg Kroah-Hartman * @usb_driver: USB driver to be registered.
1248ffdff6aSGreg Kroah-Hartman *
1258ffdff6aSGreg Kroah-Hartman * This function is called from the module_exit() of USB COMEDI driver modules
1268ffdff6aSGreg Kroah-Hartman * to unregister the USB driver and the COMEDI driver. Do not call it
1278ffdff6aSGreg Kroah-Hartman * directly, use the module_comedi_usb_driver() helper macro instead.
1288ffdff6aSGreg Kroah-Hartman */
comedi_usb_driver_unregister(struct comedi_driver * comedi_driver,struct usb_driver * usb_driver)1298ffdff6aSGreg Kroah-Hartman void comedi_usb_driver_unregister(struct comedi_driver *comedi_driver,
1308ffdff6aSGreg Kroah-Hartman struct usb_driver *usb_driver)
1318ffdff6aSGreg Kroah-Hartman {
1328ffdff6aSGreg Kroah-Hartman usb_deregister(usb_driver);
1338ffdff6aSGreg Kroah-Hartman comedi_driver_unregister(comedi_driver);
1348ffdff6aSGreg Kroah-Hartman }
1358ffdff6aSGreg Kroah-Hartman EXPORT_SYMBOL_GPL(comedi_usb_driver_unregister);
1368ffdff6aSGreg Kroah-Hartman
comedi_usb_init(void)1378ffdff6aSGreg Kroah-Hartman static int __init comedi_usb_init(void)
1388ffdff6aSGreg Kroah-Hartman {
1398ffdff6aSGreg Kroah-Hartman return 0;
1408ffdff6aSGreg Kroah-Hartman }
1418ffdff6aSGreg Kroah-Hartman module_init(comedi_usb_init);
1428ffdff6aSGreg Kroah-Hartman
comedi_usb_exit(void)1438ffdff6aSGreg Kroah-Hartman static void __exit comedi_usb_exit(void)
1448ffdff6aSGreg Kroah-Hartman {
1458ffdff6aSGreg Kroah-Hartman }
1468ffdff6aSGreg Kroah-Hartman module_exit(comedi_usb_exit);
1478ffdff6aSGreg Kroah-Hartman
1488ffdff6aSGreg Kroah-Hartman MODULE_AUTHOR("https://www.comedi.org");
1498ffdff6aSGreg Kroah-Hartman MODULE_DESCRIPTION("Comedi USB interface module");
1508ffdff6aSGreg Kroah-Hartman MODULE_LICENSE("GPL");
151