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