103270634SSteven Haigh /* 203270634SSteven Haigh * adutux - driver for ADU devices from Ontrak Control Systems 303270634SSteven Haigh * This is an experimental driver. Use at your own risk. 403270634SSteven Haigh * This driver is not supported by Ontrak Control Systems. 503270634SSteven Haigh * 603270634SSteven Haigh * Copyright (c) 2003 John Homppi (SCO, leave this notice here) 703270634SSteven Haigh * 803270634SSteven Haigh * This program is free software; you can redistribute it and/or 903270634SSteven Haigh * modify it under the terms of the GNU General Public License as 1003270634SSteven Haigh * published by the Free Software Foundation; either version 2 of 1103270634SSteven Haigh * the License, or (at your option) any later version. 1203270634SSteven Haigh * 1303270634SSteven Haigh * derived from the Lego USB Tower driver 0.56: 1403270634SSteven Haigh * Copyright (c) 2003 David Glance <davidgsf@sourceforge.net> 1503270634SSteven Haigh * 2001 Juergen Stuber <stuber@loria.fr> 1603270634SSteven Haigh * that was derived from USB Skeleton driver - 0.5 1703270634SSteven Haigh * Copyright (c) 2001 Greg Kroah-Hartman (greg@kroah.com) 1803270634SSteven Haigh * 1903270634SSteven Haigh */ 2003270634SSteven Haigh 2128f47c34SGreg Kroah-Hartman #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 2228f47c34SGreg Kroah-Hartman 2303270634SSteven Haigh #include <linux/kernel.h> 24174cd4b1SIngo Molnar #include <linux/sched/signal.h> 2503270634SSteven Haigh #include <linux/errno.h> 2603270634SSteven Haigh #include <linux/slab.h> 2703270634SSteven Haigh #include <linux/module.h> 2803270634SSteven Haigh #include <linux/usb.h> 298293c568SMatthias Kaehlcke #include <linux/mutex.h> 3040cf4833SLisa Nguyen #include <linux/uaccess.h> 3103270634SSteven Haigh 3203270634SSteven Haigh /* Version Information */ 3303270634SSteven Haigh #define DRIVER_VERSION "v0.0.13" 3403270634SSteven Haigh #define DRIVER_AUTHOR "John Homppi" 3503270634SSteven Haigh #define DRIVER_DESC "adutux (see www.ontrak.net)" 3603270634SSteven Haigh 3703270634SSteven Haigh /* Define these values to match your device */ 3803270634SSteven Haigh #define ADU_VENDOR_ID 0x0a07 3903270634SSteven Haigh #define ADU_PRODUCT_ID 0x0064 4003270634SSteven Haigh 4103270634SSteven Haigh /* table of devices that work with this driver */ 4233b9e162SNémeth Márton static const struct usb_device_id device_table[] = { 4303270634SSteven Haigh { USB_DEVICE(ADU_VENDOR_ID, ADU_PRODUCT_ID) }, /* ADU100 */ 4403270634SSteven Haigh { USB_DEVICE(ADU_VENDOR_ID, ADU_PRODUCT_ID+20) }, /* ADU120 */ 4503270634SSteven Haigh { USB_DEVICE(ADU_VENDOR_ID, ADU_PRODUCT_ID+30) }, /* ADU130 */ 4603270634SSteven Haigh { USB_DEVICE(ADU_VENDOR_ID, ADU_PRODUCT_ID+100) }, /* ADU200 */ 4703270634SSteven Haigh { USB_DEVICE(ADU_VENDOR_ID, ADU_PRODUCT_ID+108) }, /* ADU208 */ 4803270634SSteven Haigh { USB_DEVICE(ADU_VENDOR_ID, ADU_PRODUCT_ID+118) }, /* ADU218 */ 4903270634SSteven Haigh { } /* Terminating entry */ 5003270634SSteven Haigh }; 5103270634SSteven Haigh 5203270634SSteven Haigh MODULE_DEVICE_TABLE(usb, device_table); 5303270634SSteven Haigh 5403270634SSteven Haigh #ifdef CONFIG_USB_DYNAMIC_MINORS 5503270634SSteven Haigh #define ADU_MINOR_BASE 0 5603270634SSteven Haigh #else 5703270634SSteven Haigh #define ADU_MINOR_BASE 67 5803270634SSteven Haigh #endif 5903270634SSteven Haigh 6003270634SSteven Haigh /* we can have up to this number of device plugged in at once */ 6103270634SSteven Haigh #define MAX_DEVICES 16 6203270634SSteven Haigh 6303270634SSteven Haigh #define COMMAND_TIMEOUT (2*HZ) /* 60 second timeout for a command */ 6403270634SSteven Haigh 65f08812d5SPete Zaitcev /* 66f08812d5SPete Zaitcev * The locking scheme is a vanilla 3-lock: 67f08812d5SPete Zaitcev * adu_device.buflock: A spinlock, covers what IRQs touch. 68f08812d5SPete Zaitcev * adutux_mutex: A Static lock to cover open_count. It would also cover 69f08812d5SPete Zaitcev * any globals, but we don't have them in 2.6. 70f08812d5SPete Zaitcev * adu_device.mtx: A mutex to hold across sleepers like copy_from_user. 71f08812d5SPete Zaitcev * It covers all of adu_device, except the open_count 72f08812d5SPete Zaitcev * and what .buflock covers. 73f08812d5SPete Zaitcev */ 74f08812d5SPete Zaitcev 7503270634SSteven Haigh /* Structure to hold all of our device specific stuff */ 7603270634SSteven Haigh struct adu_device { 77f08812d5SPete Zaitcev struct mutex mtx; 7803270634SSteven Haigh struct usb_device *udev; /* save off the usb device pointer */ 7903270634SSteven Haigh struct usb_interface *interface; 80f08812d5SPete Zaitcev unsigned int minor; /* the starting minor number for this device */ 8103270634SSteven Haigh char serial_number[8]; 8203270634SSteven Haigh 8303270634SSteven Haigh int open_count; /* number of times this port has been opened */ 8403270634SSteven Haigh 8503270634SSteven Haigh char *read_buffer_primary; 8603270634SSteven Haigh int read_buffer_length; 8703270634SSteven Haigh char *read_buffer_secondary; 8803270634SSteven Haigh int secondary_head; 8903270634SSteven Haigh int secondary_tail; 9003270634SSteven Haigh spinlock_t buflock; 9103270634SSteven Haigh 9203270634SSteven Haigh wait_queue_head_t read_wait; 9303270634SSteven Haigh wait_queue_head_t write_wait; 9403270634SSteven Haigh 9503270634SSteven Haigh char *interrupt_in_buffer; 9603270634SSteven Haigh struct usb_endpoint_descriptor *interrupt_in_endpoint; 9703270634SSteven Haigh struct urb *interrupt_in_urb; 9803270634SSteven Haigh int read_urb_finished; 9903270634SSteven Haigh 10003270634SSteven Haigh char *interrupt_out_buffer; 10103270634SSteven Haigh struct usb_endpoint_descriptor *interrupt_out_endpoint; 10203270634SSteven Haigh struct urb *interrupt_out_urb; 103f08812d5SPete Zaitcev int out_urb_finished; 10403270634SSteven Haigh }; 10503270634SSteven Haigh 106f08812d5SPete Zaitcev static DEFINE_MUTEX(adutux_mutex); 107f08812d5SPete Zaitcev 10803270634SSteven Haigh static struct usb_driver adu_driver; 10903270634SSteven Haigh 1101ef37c60SGreg Kroah-Hartman static inline void adu_debug_data(struct device *dev, const char *function, 1111ef37c60SGreg Kroah-Hartman int size, const unsigned char *data) 11203270634SSteven Haigh { 1131ef37c60SGreg Kroah-Hartman dev_dbg(dev, "%s - length = %d, data = %*ph\n", 1141ef37c60SGreg Kroah-Hartman function, size, size, data); 11503270634SSteven Haigh } 11603270634SSteven Haigh 11703270634SSteven Haigh /** 11803270634SSteven Haigh * adu_abort_transfers 11903270634SSteven Haigh * aborts transfers and frees associated data structures 12003270634SSteven Haigh */ 12103270634SSteven Haigh static void adu_abort_transfers(struct adu_device *dev) 12203270634SSteven Haigh { 123f08812d5SPete Zaitcev unsigned long flags; 12403270634SSteven Haigh 12566d4bc30SGreg Kroah-Hartman if (dev->udev == NULL) 1266e42a158SGreg Kroah-Hartman return; 12703270634SSteven Haigh 12803270634SSteven Haigh /* shutdown transfer */ 129f08812d5SPete Zaitcev 130f08812d5SPete Zaitcev /* XXX Anchor these instead */ 131f08812d5SPete Zaitcev spin_lock_irqsave(&dev->buflock, flags); 132f08812d5SPete Zaitcev if (!dev->read_urb_finished) { 133f08812d5SPete Zaitcev spin_unlock_irqrestore(&dev->buflock, flags); 134f08812d5SPete Zaitcev usb_kill_urb(dev->interrupt_in_urb); 135f08812d5SPete Zaitcev } else 136f08812d5SPete Zaitcev spin_unlock_irqrestore(&dev->buflock, flags); 137f08812d5SPete Zaitcev 138f08812d5SPete Zaitcev spin_lock_irqsave(&dev->buflock, flags); 139f08812d5SPete Zaitcev if (!dev->out_urb_finished) { 140f08812d5SPete Zaitcev spin_unlock_irqrestore(&dev->buflock, flags); 141f08812d5SPete Zaitcev usb_kill_urb(dev->interrupt_out_urb); 142f08812d5SPete Zaitcev } else 143f08812d5SPete Zaitcev spin_unlock_irqrestore(&dev->buflock, flags); 14403270634SSteven Haigh } 14503270634SSteven Haigh 14603270634SSteven Haigh static void adu_delete(struct adu_device *dev) 14703270634SSteven Haigh { 14803270634SSteven Haigh /* free data structures */ 14903270634SSteven Haigh usb_free_urb(dev->interrupt_in_urb); 15003270634SSteven Haigh usb_free_urb(dev->interrupt_out_urb); 15103270634SSteven Haigh kfree(dev->read_buffer_primary); 15203270634SSteven Haigh kfree(dev->read_buffer_secondary); 15303270634SSteven Haigh kfree(dev->interrupt_in_buffer); 15403270634SSteven Haigh kfree(dev->interrupt_out_buffer); 15503270634SSteven Haigh kfree(dev); 15603270634SSteven Haigh } 15703270634SSteven Haigh 1587d12e780SDavid Howells static void adu_interrupt_in_callback(struct urb *urb) 15903270634SSteven Haigh { 16003270634SSteven Haigh struct adu_device *dev = urb->context; 16124497a00SGreg Kroah-Hartman int status = urb->status; 16203270634SSteven Haigh 1631ef37c60SGreg Kroah-Hartman adu_debug_data(&dev->udev->dev, __func__, 1641ef37c60SGreg Kroah-Hartman urb->actual_length, urb->transfer_buffer); 16503270634SSteven Haigh 16603270634SSteven Haigh spin_lock(&dev->buflock); 16703270634SSteven Haigh 16824497a00SGreg Kroah-Hartman if (status != 0) { 169f6c1ceaaSOliver Neukum if ((status != -ENOENT) && (status != -ECONNRESET) && 170f6c1ceaaSOliver Neukum (status != -ESHUTDOWN)) { 17166d4bc30SGreg Kroah-Hartman dev_dbg(&dev->udev->dev, 17266d4bc30SGreg Kroah-Hartman "%s : nonzero status received: %d\n", 173441b62c1SHarvey Harrison __func__, status); 17403270634SSteven Haigh } 17503270634SSteven Haigh goto exit; 17603270634SSteven Haigh } 17703270634SSteven Haigh 17803270634SSteven Haigh if (urb->actual_length > 0 && dev->interrupt_in_buffer[0] != 0x00) { 17903270634SSteven Haigh if (dev->read_buffer_length < 18029cc8897SKuninori Morimoto (4 * usb_endpoint_maxp(dev->interrupt_in_endpoint)) - 18103270634SSteven Haigh (urb->actual_length)) { 18203270634SSteven Haigh memcpy (dev->read_buffer_primary + 18303270634SSteven Haigh dev->read_buffer_length, 18403270634SSteven Haigh dev->interrupt_in_buffer, urb->actual_length); 18503270634SSteven Haigh 18603270634SSteven Haigh dev->read_buffer_length += urb->actual_length; 18766d4bc30SGreg Kroah-Hartman dev_dbg(&dev->udev->dev,"%s reading %d\n", __func__, 18803270634SSteven Haigh urb->actual_length); 18903270634SSteven Haigh } else { 19066d4bc30SGreg Kroah-Hartman dev_dbg(&dev->udev->dev,"%s : read_buffer overflow\n", 19166d4bc30SGreg Kroah-Hartman __func__); 19203270634SSteven Haigh } 19303270634SSteven Haigh } 19403270634SSteven Haigh 19503270634SSteven Haigh exit: 19603270634SSteven Haigh dev->read_urb_finished = 1; 19703270634SSteven Haigh spin_unlock(&dev->buflock); 19803270634SSteven Haigh /* always wake up so we recover from errors */ 19903270634SSteven Haigh wake_up_interruptible(&dev->read_wait); 20003270634SSteven Haigh } 20103270634SSteven Haigh 2027d12e780SDavid Howells static void adu_interrupt_out_callback(struct urb *urb) 20303270634SSteven Haigh { 20403270634SSteven Haigh struct adu_device *dev = urb->context; 20524497a00SGreg Kroah-Hartman int status = urb->status; 20603270634SSteven Haigh 2071ef37c60SGreg Kroah-Hartman adu_debug_data(&dev->udev->dev, __func__, 2081ef37c60SGreg Kroah-Hartman urb->actual_length, urb->transfer_buffer); 20903270634SSteven Haigh 21024497a00SGreg Kroah-Hartman if (status != 0) { 21124497a00SGreg Kroah-Hartman if ((status != -ENOENT) && 21224497a00SGreg Kroah-Hartman (status != -ECONNRESET)) { 21366d4bc30SGreg Kroah-Hartman dev_dbg(&dev->udev->dev, 21466d4bc30SGreg Kroah-Hartman "%s :nonzero status received: %d\n", __func__, 21566d4bc30SGreg Kroah-Hartman status); 21603270634SSteven Haigh } 2171ef37c60SGreg Kroah-Hartman return; 21803270634SSteven Haigh } 21903270634SSteven Haigh 220f08812d5SPete Zaitcev spin_lock(&dev->buflock); 221f08812d5SPete Zaitcev dev->out_urb_finished = 1; 222f08812d5SPete Zaitcev wake_up(&dev->write_wait); 223f08812d5SPete Zaitcev spin_unlock(&dev->buflock); 22403270634SSteven Haigh } 22503270634SSteven Haigh 22603270634SSteven Haigh static int adu_open(struct inode *inode, struct file *file) 22703270634SSteven Haigh { 22803270634SSteven Haigh struct adu_device *dev = NULL; 22903270634SSteven Haigh struct usb_interface *interface; 23003270634SSteven Haigh int subminor; 231f08812d5SPete Zaitcev int retval; 23203270634SSteven Haigh 23303270634SSteven Haigh subminor = iminor(inode); 23403270634SSteven Haigh 235e77c4e6aSLisa Nguyen retval = mutex_lock_interruptible(&adutux_mutex); 23666d4bc30SGreg Kroah-Hartman if (retval) 237f08812d5SPete Zaitcev goto exit_no_lock; 238f08812d5SPete Zaitcev 23903270634SSteven Haigh interface = usb_find_interface(&adu_driver, subminor); 24003270634SSteven Haigh if (!interface) { 24128f47c34SGreg Kroah-Hartman pr_err("%s - error, can't find device for minor %d\n", 24228f47c34SGreg Kroah-Hartman __func__, subminor); 24303270634SSteven Haigh retval = -ENODEV; 24403270634SSteven Haigh goto exit_no_device; 24503270634SSteven Haigh } 24603270634SSteven Haigh 24703270634SSteven Haigh dev = usb_get_intfdata(interface); 248f08812d5SPete Zaitcev if (!dev || !dev->udev) { 24903270634SSteven Haigh retval = -ENODEV; 25003270634SSteven Haigh goto exit_no_device; 25103270634SSteven Haigh } 25203270634SSteven Haigh 253f08812d5SPete Zaitcev /* check that nobody else is using the device */ 254f08812d5SPete Zaitcev if (dev->open_count) { 255f08812d5SPete Zaitcev retval = -EBUSY; 25603270634SSteven Haigh goto exit_no_device; 25703270634SSteven Haigh } 25803270634SSteven Haigh 25903270634SSteven Haigh ++dev->open_count; 26066d4bc30SGreg Kroah-Hartman dev_dbg(&dev->udev->dev, "%s: open count %d\n", __func__, 26166d4bc30SGreg Kroah-Hartman dev->open_count); 26203270634SSteven Haigh 26303270634SSteven Haigh /* save device in the file's private structure */ 26403270634SSteven Haigh file->private_data = dev; 26503270634SSteven Haigh 26603270634SSteven Haigh /* initialize in direction */ 26703270634SSteven Haigh dev->read_buffer_length = 0; 26803270634SSteven Haigh 26903270634SSteven Haigh /* fixup first read by having urb waiting for it */ 27003270634SSteven Haigh usb_fill_int_urb(dev->interrupt_in_urb, dev->udev, 27103270634SSteven Haigh usb_rcvintpipe(dev->udev, 27203270634SSteven Haigh dev->interrupt_in_endpoint->bEndpointAddress), 27303270634SSteven Haigh dev->interrupt_in_buffer, 27429cc8897SKuninori Morimoto usb_endpoint_maxp(dev->interrupt_in_endpoint), 27503270634SSteven Haigh adu_interrupt_in_callback, dev, 27603270634SSteven Haigh dev->interrupt_in_endpoint->bInterval); 27703270634SSteven Haigh dev->read_urb_finished = 0; 278f08812d5SPete Zaitcev if (usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL)) 279f08812d5SPete Zaitcev dev->read_urb_finished = 1; 280f08812d5SPete Zaitcev /* we ignore failure */ 281f08812d5SPete Zaitcev /* end of fixup for first read */ 282f08812d5SPete Zaitcev 283f08812d5SPete Zaitcev /* initialize out direction */ 284f08812d5SPete Zaitcev dev->out_urb_finished = 1; 285f08812d5SPete Zaitcev 286f08812d5SPete Zaitcev retval = 0; 28703270634SSteven Haigh 28803270634SSteven Haigh exit_no_device: 289f08812d5SPete Zaitcev mutex_unlock(&adutux_mutex); 290f08812d5SPete Zaitcev exit_no_lock: 29103270634SSteven Haigh return retval; 29203270634SSteven Haigh } 29303270634SSteven Haigh 294f08812d5SPete Zaitcev static void adu_release_internal(struct adu_device *dev) 29503270634SSteven Haigh { 29603270634SSteven Haigh /* decrement our usage count for the device */ 29703270634SSteven Haigh --dev->open_count; 29866d4bc30SGreg Kroah-Hartman dev_dbg(&dev->udev->dev, "%s : open count %d\n", __func__, 29966d4bc30SGreg Kroah-Hartman dev->open_count); 30003270634SSteven Haigh if (dev->open_count <= 0) { 30103270634SSteven Haigh adu_abort_transfers(dev); 30203270634SSteven Haigh dev->open_count = 0; 30303270634SSteven Haigh } 30403270634SSteven Haigh } 30503270634SSteven Haigh 30603270634SSteven Haigh static int adu_release(struct inode *inode, struct file *file) 30703270634SSteven Haigh { 308f08812d5SPete Zaitcev struct adu_device *dev; 30903270634SSteven Haigh int retval = 0; 31003270634SSteven Haigh 31103270634SSteven Haigh if (file == NULL) { 31203270634SSteven Haigh retval = -ENODEV; 31303270634SSteven Haigh goto exit; 31403270634SSteven Haigh } 31503270634SSteven Haigh 31603270634SSteven Haigh dev = file->private_data; 31703270634SSteven Haigh if (dev == NULL) { 31803270634SSteven Haigh retval = -ENODEV; 31903270634SSteven Haigh goto exit; 32003270634SSteven Haigh } 32103270634SSteven Haigh 322f08812d5SPete Zaitcev mutex_lock(&adutux_mutex); /* not interruptible */ 32303270634SSteven Haigh 32403270634SSteven Haigh if (dev->open_count <= 0) { 32566d4bc30SGreg Kroah-Hartman dev_dbg(&dev->udev->dev, "%s : device not opened\n", __func__); 32603270634SSteven Haigh retval = -ENODEV; 32746c9844cSJiri Slaby goto unlock; 32803270634SSteven Haigh } 32903270634SSteven Haigh 330f08812d5SPete Zaitcev adu_release_internal(dev); 331d4ead16fSAlan Stern if (dev->udev == NULL) { 332d4ead16fSAlan Stern /* the device was unplugged before the file was released */ 333f08812d5SPete Zaitcev if (!dev->open_count) /* ... and we're the last user */ 334d4ead16fSAlan Stern adu_delete(dev); 335d4ead16fSAlan Stern } 33646c9844cSJiri Slaby unlock: 337f08812d5SPete Zaitcev mutex_unlock(&adutux_mutex); 33846c9844cSJiri Slaby exit: 33903270634SSteven Haigh return retval; 34003270634SSteven Haigh } 34103270634SSteven Haigh 34203270634SSteven Haigh static ssize_t adu_read(struct file *file, __user char *buffer, size_t count, 34303270634SSteven Haigh loff_t *ppos) 34403270634SSteven Haigh { 34503270634SSteven Haigh struct adu_device *dev; 34603270634SSteven Haigh size_t bytes_read = 0; 34703270634SSteven Haigh size_t bytes_to_read = count; 34803270634SSteven Haigh int i; 34903270634SSteven Haigh int retval = 0; 35003270634SSteven Haigh int timeout = 0; 35103270634SSteven Haigh int should_submit = 0; 35203270634SSteven Haigh unsigned long flags; 35303270634SSteven Haigh DECLARE_WAITQUEUE(wait, current); 35403270634SSteven Haigh 35503270634SSteven Haigh dev = file->private_data; 3568293c568SMatthias Kaehlcke if (mutex_lock_interruptible(&dev->mtx)) 35703270634SSteven Haigh return -ERESTARTSYS; 35803270634SSteven Haigh 35903270634SSteven Haigh /* verify that the device wasn't unplugged */ 360f08812d5SPete Zaitcev if (dev->udev == NULL) { 36103270634SSteven Haigh retval = -ENODEV; 36228f47c34SGreg Kroah-Hartman pr_err("No device or device unplugged %d\n", retval); 36303270634SSteven Haigh goto exit; 36403270634SSteven Haigh } 36503270634SSteven Haigh 36603270634SSteven Haigh /* verify that some data was requested */ 36703270634SSteven Haigh if (count == 0) { 36866d4bc30SGreg Kroah-Hartman dev_dbg(&dev->udev->dev, "%s : read request of 0 bytes\n", 36966d4bc30SGreg Kroah-Hartman __func__); 37003270634SSteven Haigh goto exit; 37103270634SSteven Haigh } 37203270634SSteven Haigh 37303270634SSteven Haigh timeout = COMMAND_TIMEOUT; 37466d4bc30SGreg Kroah-Hartman dev_dbg(&dev->udev->dev, "%s : about to start looping\n", __func__); 37503270634SSteven Haigh while (bytes_to_read) { 37603270634SSteven Haigh int data_in_secondary = dev->secondary_tail - dev->secondary_head; 37766d4bc30SGreg Kroah-Hartman dev_dbg(&dev->udev->dev, 37866d4bc30SGreg Kroah-Hartman "%s : while, data_in_secondary=%d, status=%d\n", 379441b62c1SHarvey Harrison __func__, data_in_secondary, 38003270634SSteven Haigh dev->interrupt_in_urb->status); 38103270634SSteven Haigh 38203270634SSteven Haigh if (data_in_secondary) { 38303270634SSteven Haigh /* drain secondary buffer */ 38403270634SSteven Haigh int amount = bytes_to_read < data_in_secondary ? bytes_to_read : data_in_secondary; 38503270634SSteven Haigh i = copy_to_user(buffer, dev->read_buffer_secondary+dev->secondary_head, amount); 3861865a9c3SKulikov Vasiliy if (i) { 38703270634SSteven Haigh retval = -EFAULT; 38803270634SSteven Haigh goto exit; 38903270634SSteven Haigh } 39003270634SSteven Haigh dev->secondary_head += (amount - i); 39103270634SSteven Haigh bytes_read += (amount - i); 39203270634SSteven Haigh bytes_to_read -= (amount - i); 39303270634SSteven Haigh } else { 39403270634SSteven Haigh /* we check the primary buffer */ 39503270634SSteven Haigh spin_lock_irqsave (&dev->buflock, flags); 39603270634SSteven Haigh if (dev->read_buffer_length) { 39703270634SSteven Haigh /* we secure access to the primary */ 39803270634SSteven Haigh char *tmp; 39966d4bc30SGreg Kroah-Hartman dev_dbg(&dev->udev->dev, 40066d4bc30SGreg Kroah-Hartman "%s : swap, read_buffer_length = %d\n", 401441b62c1SHarvey Harrison __func__, dev->read_buffer_length); 40203270634SSteven Haigh tmp = dev->read_buffer_secondary; 40303270634SSteven Haigh dev->read_buffer_secondary = dev->read_buffer_primary; 40403270634SSteven Haigh dev->read_buffer_primary = tmp; 40503270634SSteven Haigh dev->secondary_head = 0; 40603270634SSteven Haigh dev->secondary_tail = dev->read_buffer_length; 40703270634SSteven Haigh dev->read_buffer_length = 0; 40803270634SSteven Haigh spin_unlock_irqrestore(&dev->buflock, flags); 40903270634SSteven Haigh /* we have a free buffer so use it */ 41003270634SSteven Haigh should_submit = 1; 41103270634SSteven Haigh } else { 41203270634SSteven Haigh /* even the primary was empty - we may need to do IO */ 413f08812d5SPete Zaitcev if (!dev->read_urb_finished) { 41403270634SSteven Haigh /* somebody is doing IO */ 41503270634SSteven Haigh spin_unlock_irqrestore(&dev->buflock, flags); 41666d4bc30SGreg Kroah-Hartman dev_dbg(&dev->udev->dev, 41766d4bc30SGreg Kroah-Hartman "%s : submitted already\n", 41866d4bc30SGreg Kroah-Hartman __func__); 41903270634SSteven Haigh } else { 42003270634SSteven Haigh /* we must initiate input */ 42166d4bc30SGreg Kroah-Hartman dev_dbg(&dev->udev->dev, 42266d4bc30SGreg Kroah-Hartman "%s : initiate input\n", 42366d4bc30SGreg Kroah-Hartman __func__); 42403270634SSteven Haigh dev->read_urb_finished = 0; 425f08812d5SPete Zaitcev spin_unlock_irqrestore(&dev->buflock, flags); 42603270634SSteven Haigh 42703270634SSteven Haigh usb_fill_int_urb(dev->interrupt_in_urb, dev->udev, 42803270634SSteven Haigh usb_rcvintpipe(dev->udev, 42903270634SSteven Haigh dev->interrupt_in_endpoint->bEndpointAddress), 43003270634SSteven Haigh dev->interrupt_in_buffer, 43129cc8897SKuninori Morimoto usb_endpoint_maxp(dev->interrupt_in_endpoint), 43203270634SSteven Haigh adu_interrupt_in_callback, 43303270634SSteven Haigh dev, 43403270634SSteven Haigh dev->interrupt_in_endpoint->bInterval); 435f08812d5SPete Zaitcev retval = usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL); 436f08812d5SPete Zaitcev if (retval) { 437f08812d5SPete Zaitcev dev->read_urb_finished = 1; 43803270634SSteven Haigh if (retval == -ENOMEM) { 43903270634SSteven Haigh retval = bytes_read ? bytes_read : -ENOMEM; 44003270634SSteven Haigh } 44166d4bc30SGreg Kroah-Hartman dev_dbg(&dev->udev->dev, 44266d4bc30SGreg Kroah-Hartman "%s : submit failed\n", 44366d4bc30SGreg Kroah-Hartman __func__); 44403270634SSteven Haigh goto exit; 44503270634SSteven Haigh } 44603270634SSteven Haigh } 44703270634SSteven Haigh 44803270634SSteven Haigh /* we wait for I/O to complete */ 44903270634SSteven Haigh set_current_state(TASK_INTERRUPTIBLE); 45003270634SSteven Haigh add_wait_queue(&dev->read_wait, &wait); 451f08812d5SPete Zaitcev spin_lock_irqsave(&dev->buflock, flags); 452f08812d5SPete Zaitcev if (!dev->read_urb_finished) { 453f08812d5SPete Zaitcev spin_unlock_irqrestore(&dev->buflock, flags); 45403270634SSteven Haigh timeout = schedule_timeout(COMMAND_TIMEOUT); 455f08812d5SPete Zaitcev } else { 456f08812d5SPete Zaitcev spin_unlock_irqrestore(&dev->buflock, flags); 45703270634SSteven Haigh set_current_state(TASK_RUNNING); 458f08812d5SPete Zaitcev } 45903270634SSteven Haigh remove_wait_queue(&dev->read_wait, &wait); 46003270634SSteven Haigh 46103270634SSteven Haigh if (timeout <= 0) { 46266d4bc30SGreg Kroah-Hartman dev_dbg(&dev->udev->dev, 46366d4bc30SGreg Kroah-Hartman "%s : timeout\n", __func__); 46403270634SSteven Haigh retval = bytes_read ? bytes_read : -ETIMEDOUT; 46503270634SSteven Haigh goto exit; 46603270634SSteven Haigh } 46703270634SSteven Haigh 46803270634SSteven Haigh if (signal_pending(current)) { 46966d4bc30SGreg Kroah-Hartman dev_dbg(&dev->udev->dev, 47066d4bc30SGreg Kroah-Hartman "%s : signal pending\n", 47166d4bc30SGreg Kroah-Hartman __func__); 47203270634SSteven Haigh retval = bytes_read ? bytes_read : -EINTR; 47303270634SSteven Haigh goto exit; 47403270634SSteven Haigh } 47503270634SSteven Haigh } 47603270634SSteven Haigh } 47703270634SSteven Haigh } 47803270634SSteven Haigh 47903270634SSteven Haigh retval = bytes_read; 48003270634SSteven Haigh /* if the primary buffer is empty then use it */ 481f08812d5SPete Zaitcev spin_lock_irqsave(&dev->buflock, flags); 482f08812d5SPete Zaitcev if (should_submit && dev->read_urb_finished) { 483f08812d5SPete Zaitcev dev->read_urb_finished = 0; 484f08812d5SPete Zaitcev spin_unlock_irqrestore(&dev->buflock, flags); 48503270634SSteven Haigh usb_fill_int_urb(dev->interrupt_in_urb, dev->udev, 48603270634SSteven Haigh usb_rcvintpipe(dev->udev, 48703270634SSteven Haigh dev->interrupt_in_endpoint->bEndpointAddress), 48803270634SSteven Haigh dev->interrupt_in_buffer, 48929cc8897SKuninori Morimoto usb_endpoint_maxp(dev->interrupt_in_endpoint), 49003270634SSteven Haigh adu_interrupt_in_callback, 49103270634SSteven Haigh dev, 49203270634SSteven Haigh dev->interrupt_in_endpoint->bInterval); 493f08812d5SPete Zaitcev if (usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL) != 0) 494f08812d5SPete Zaitcev dev->read_urb_finished = 1; 49503270634SSteven Haigh /* we ignore failure */ 496f08812d5SPete Zaitcev } else { 497f08812d5SPete Zaitcev spin_unlock_irqrestore(&dev->buflock, flags); 49803270634SSteven Haigh } 49903270634SSteven Haigh 50003270634SSteven Haigh exit: 50103270634SSteven Haigh /* unlock the device */ 5028293c568SMatthias Kaehlcke mutex_unlock(&dev->mtx); 50303270634SSteven Haigh 50403270634SSteven Haigh return retval; 50503270634SSteven Haigh } 50603270634SSteven Haigh 50703270634SSteven Haigh static ssize_t adu_write(struct file *file, const __user char *buffer, 50803270634SSteven Haigh size_t count, loff_t *ppos) 50903270634SSteven Haigh { 510f08812d5SPete Zaitcev DECLARE_WAITQUEUE(waita, current); 51103270634SSteven Haigh struct adu_device *dev; 51203270634SSteven Haigh size_t bytes_written = 0; 51303270634SSteven Haigh size_t bytes_to_write; 51403270634SSteven Haigh size_t buffer_size; 515f08812d5SPete Zaitcev unsigned long flags; 516ebc3ac14SOliver Neukum int retval; 51703270634SSteven Haigh 51803270634SSteven Haigh dev = file->private_data; 51903270634SSteven Haigh 5208293c568SMatthias Kaehlcke retval = mutex_lock_interruptible(&dev->mtx); 521ebc3ac14SOliver Neukum if (retval) 522ebc3ac14SOliver Neukum goto exit_nolock; 52303270634SSteven Haigh 52403270634SSteven Haigh /* verify that the device wasn't unplugged */ 525f08812d5SPete Zaitcev if (dev->udev == NULL) { 52603270634SSteven Haigh retval = -ENODEV; 52728f47c34SGreg Kroah-Hartman pr_err("No device or device unplugged %d\n", retval); 52803270634SSteven Haigh goto exit; 52903270634SSteven Haigh } 53003270634SSteven Haigh 53103270634SSteven Haigh /* verify that we actually have some data to write */ 53203270634SSteven Haigh if (count == 0) { 53366d4bc30SGreg Kroah-Hartman dev_dbg(&dev->udev->dev, "%s : write request of 0 bytes\n", 53466d4bc30SGreg Kroah-Hartman __func__); 53503270634SSteven Haigh goto exit; 53603270634SSteven Haigh } 53703270634SSteven Haigh 53803270634SSteven Haigh while (count > 0) { 539f08812d5SPete Zaitcev add_wait_queue(&dev->write_wait, &waita); 540f08812d5SPete Zaitcev set_current_state(TASK_INTERRUPTIBLE); 541f08812d5SPete Zaitcev spin_lock_irqsave(&dev->buflock, flags); 542f08812d5SPete Zaitcev if (!dev->out_urb_finished) { 543f08812d5SPete Zaitcev spin_unlock_irqrestore(&dev->buflock, flags); 54403270634SSteven Haigh 545f08812d5SPete Zaitcev mutex_unlock(&dev->mtx); 54603270634SSteven Haigh if (signal_pending(current)) { 54766d4bc30SGreg Kroah-Hartman dev_dbg(&dev->udev->dev, "%s : interrupted\n", 54866d4bc30SGreg Kroah-Hartman __func__); 549f08812d5SPete Zaitcev set_current_state(TASK_RUNNING); 55003270634SSteven Haigh retval = -EINTR; 551f08812d5SPete Zaitcev goto exit_onqueue; 55203270634SSteven Haigh } 553f08812d5SPete Zaitcev if (schedule_timeout(COMMAND_TIMEOUT) == 0) { 55466d4bc30SGreg Kroah-Hartman dev_dbg(&dev->udev->dev, 55566d4bc30SGreg Kroah-Hartman "%s - command timed out.\n", __func__); 556f08812d5SPete Zaitcev retval = -ETIMEDOUT; 557f08812d5SPete Zaitcev goto exit_onqueue; 558f08812d5SPete Zaitcev } 559f08812d5SPete Zaitcev remove_wait_queue(&dev->write_wait, &waita); 5608293c568SMatthias Kaehlcke retval = mutex_lock_interruptible(&dev->mtx); 561ebc3ac14SOliver Neukum if (retval) { 562ebc3ac14SOliver Neukum retval = bytes_written ? bytes_written : retval; 563ebc3ac14SOliver Neukum goto exit_nolock; 564ebc3ac14SOliver Neukum } 56503270634SSteven Haigh 56666d4bc30SGreg Kroah-Hartman dev_dbg(&dev->udev->dev, 5675b5e0928SAlexey Dobriyan "%s : in progress, count = %zd\n", 56866d4bc30SGreg Kroah-Hartman __func__, count); 56903270634SSteven Haigh } else { 570f08812d5SPete Zaitcev spin_unlock_irqrestore(&dev->buflock, flags); 571f08812d5SPete Zaitcev set_current_state(TASK_RUNNING); 572f08812d5SPete Zaitcev remove_wait_queue(&dev->write_wait, &waita); 5735b5e0928SAlexey Dobriyan dev_dbg(&dev->udev->dev, "%s : sending, count = %zd\n", 57466d4bc30SGreg Kroah-Hartman __func__, count); 57503270634SSteven Haigh 57603270634SSteven Haigh /* write the data into interrupt_out_buffer from userspace */ 57729cc8897SKuninori Morimoto buffer_size = usb_endpoint_maxp(dev->interrupt_out_endpoint); 57803270634SSteven Haigh bytes_to_write = count > buffer_size ? buffer_size : count; 57966d4bc30SGreg Kroah-Hartman dev_dbg(&dev->udev->dev, 5805b5e0928SAlexey Dobriyan "%s : buffer_size = %zd, count = %zd, bytes_to_write = %zd\n", 581441b62c1SHarvey Harrison __func__, buffer_size, count, bytes_to_write); 58203270634SSteven Haigh 58303270634SSteven Haigh if (copy_from_user(dev->interrupt_out_buffer, buffer, bytes_to_write) != 0) { 58403270634SSteven Haigh retval = -EFAULT; 58503270634SSteven Haigh goto exit; 58603270634SSteven Haigh } 58703270634SSteven Haigh 58803270634SSteven Haigh /* send off the urb */ 58903270634SSteven Haigh usb_fill_int_urb( 59003270634SSteven Haigh dev->interrupt_out_urb, 59103270634SSteven Haigh dev->udev, 59203270634SSteven Haigh usb_sndintpipe(dev->udev, dev->interrupt_out_endpoint->bEndpointAddress), 59303270634SSteven Haigh dev->interrupt_out_buffer, 59403270634SSteven Haigh bytes_to_write, 59503270634SSteven Haigh adu_interrupt_out_callback, 59603270634SSteven Haigh dev, 597f08812d5SPete Zaitcev dev->interrupt_out_endpoint->bInterval); 59803270634SSteven Haigh dev->interrupt_out_urb->actual_length = bytes_to_write; 599f08812d5SPete Zaitcev dev->out_urb_finished = 0; 60003270634SSteven Haigh retval = usb_submit_urb(dev->interrupt_out_urb, GFP_KERNEL); 60103270634SSteven Haigh if (retval < 0) { 602f08812d5SPete Zaitcev dev->out_urb_finished = 1; 603fd3f1917SGreg Kroah-Hartman dev_err(&dev->udev->dev, "Couldn't submit " 604fd3f1917SGreg Kroah-Hartman "interrupt_out_urb %d\n", retval); 60503270634SSteven Haigh goto exit; 60603270634SSteven Haigh } 60703270634SSteven Haigh 60803270634SSteven Haigh buffer += bytes_to_write; 60903270634SSteven Haigh count -= bytes_to_write; 61003270634SSteven Haigh 61103270634SSteven Haigh bytes_written += bytes_to_write; 61203270634SSteven Haigh } 61303270634SSteven Haigh } 614f08812d5SPete Zaitcev mutex_unlock(&dev->mtx); 615f08812d5SPete Zaitcev return bytes_written; 61603270634SSteven Haigh 61703270634SSteven Haigh exit: 6188293c568SMatthias Kaehlcke mutex_unlock(&dev->mtx); 619ebc3ac14SOliver Neukum exit_nolock: 620f08812d5SPete Zaitcev return retval; 62103270634SSteven Haigh 622f08812d5SPete Zaitcev exit_onqueue: 623f08812d5SPete Zaitcev remove_wait_queue(&dev->write_wait, &waita); 62403270634SSteven Haigh return retval; 62503270634SSteven Haigh } 62603270634SSteven Haigh 62703270634SSteven Haigh /* file operations needed when we register this driver */ 62800977a59SArjan van de Ven static const struct file_operations adu_fops = { 62903270634SSteven Haigh .owner = THIS_MODULE, 63003270634SSteven Haigh .read = adu_read, 63103270634SSteven Haigh .write = adu_write, 63203270634SSteven Haigh .open = adu_open, 63303270634SSteven Haigh .release = adu_release, 6346038f373SArnd Bergmann .llseek = noop_llseek, 63503270634SSteven Haigh }; 63603270634SSteven Haigh 63703270634SSteven Haigh /* 63803270634SSteven Haigh * usb class driver info in order to get a minor number from the usb core, 63903270634SSteven Haigh * and to have the device registered with devfs and the driver core 64003270634SSteven Haigh */ 64103270634SSteven Haigh static struct usb_class_driver adu_class = { 64203270634SSteven Haigh .name = "usb/adutux%d", 64303270634SSteven Haigh .fops = &adu_fops, 64403270634SSteven Haigh .minor_base = ADU_MINOR_BASE, 64503270634SSteven Haigh }; 64603270634SSteven Haigh 64703270634SSteven Haigh /** 64803270634SSteven Haigh * adu_probe 64903270634SSteven Haigh * 65003270634SSteven Haigh * Called by the usb core when a new device is connected that it thinks 65103270634SSteven Haigh * this driver might be interested in. 65203270634SSteven Haigh */ 65303270634SSteven Haigh static int adu_probe(struct usb_interface *interface, 65403270634SSteven Haigh const struct usb_device_id *id) 65503270634SSteven Haigh { 65603270634SSteven Haigh struct usb_device *udev = interface_to_usbdev(interface); 65703270634SSteven Haigh struct adu_device *dev = NULL; 65803270634SSteven Haigh struct usb_host_interface *iface_desc; 65903270634SSteven Haigh struct usb_endpoint_descriptor *endpoint; 66003270634SSteven Haigh int retval = -ENODEV; 66103270634SSteven Haigh int in_end_size; 66203270634SSteven Haigh int out_end_size; 66303270634SSteven Haigh int i; 66403270634SSteven Haigh 66503270634SSteven Haigh if (udev == NULL) { 66603270634SSteven Haigh dev_err(&interface->dev, "udev is NULL.\n"); 66703270634SSteven Haigh goto exit; 66803270634SSteven Haigh } 66903270634SSteven Haigh 670b595076aSUwe Kleine-König /* allocate memory for our device state and initialize it */ 67103270634SSteven Haigh dev = kzalloc(sizeof(struct adu_device), GFP_KERNEL); 672a02b55c8SWolfram Sang if (!dev) { 67303270634SSteven Haigh retval = -ENOMEM; 67403270634SSteven Haigh goto exit; 67503270634SSteven Haigh } 67603270634SSteven Haigh 6778293c568SMatthias Kaehlcke mutex_init(&dev->mtx); 67803270634SSteven Haigh spin_lock_init(&dev->buflock); 67903270634SSteven Haigh dev->udev = udev; 68003270634SSteven Haigh init_waitqueue_head(&dev->read_wait); 68103270634SSteven Haigh init_waitqueue_head(&dev->write_wait); 68203270634SSteven Haigh 68303270634SSteven Haigh iface_desc = &interface->altsetting[0]; 68403270634SSteven Haigh 68503270634SSteven Haigh /* set up the endpoint information */ 68603270634SSteven Haigh for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) { 68703270634SSteven Haigh endpoint = &iface_desc->endpoint[i].desc; 68803270634SSteven Haigh 68903270634SSteven Haigh if (usb_endpoint_is_int_in(endpoint)) 69003270634SSteven Haigh dev->interrupt_in_endpoint = endpoint; 69103270634SSteven Haigh 69203270634SSteven Haigh if (usb_endpoint_is_int_out(endpoint)) 69303270634SSteven Haigh dev->interrupt_out_endpoint = endpoint; 69403270634SSteven Haigh } 69503270634SSteven Haigh if (dev->interrupt_in_endpoint == NULL) { 69603270634SSteven Haigh dev_err(&interface->dev, "interrupt in endpoint not found\n"); 69703270634SSteven Haigh goto error; 69803270634SSteven Haigh } 69903270634SSteven Haigh if (dev->interrupt_out_endpoint == NULL) { 70003270634SSteven Haigh dev_err(&interface->dev, "interrupt out endpoint not found\n"); 70103270634SSteven Haigh goto error; 70203270634SSteven Haigh } 70303270634SSteven Haigh 70429cc8897SKuninori Morimoto in_end_size = usb_endpoint_maxp(dev->interrupt_in_endpoint); 70529cc8897SKuninori Morimoto out_end_size = usb_endpoint_maxp(dev->interrupt_out_endpoint); 70603270634SSteven Haigh 70703270634SSteven Haigh dev->read_buffer_primary = kmalloc((4 * in_end_size), GFP_KERNEL); 70803270634SSteven Haigh if (!dev->read_buffer_primary) { 70903270634SSteven Haigh retval = -ENOMEM; 71003270634SSteven Haigh goto error; 71103270634SSteven Haigh } 71203270634SSteven Haigh 71303270634SSteven Haigh /* debug code prime the buffer */ 71403270634SSteven Haigh memset(dev->read_buffer_primary, 'a', in_end_size); 71503270634SSteven Haigh memset(dev->read_buffer_primary + in_end_size, 'b', in_end_size); 71603270634SSteven Haigh memset(dev->read_buffer_primary + (2 * in_end_size), 'c', in_end_size); 71703270634SSteven Haigh memset(dev->read_buffer_primary + (3 * in_end_size), 'd', in_end_size); 71803270634SSteven Haigh 71903270634SSteven Haigh dev->read_buffer_secondary = kmalloc((4 * in_end_size), GFP_KERNEL); 72003270634SSteven Haigh if (!dev->read_buffer_secondary) { 72103270634SSteven Haigh retval = -ENOMEM; 72203270634SSteven Haigh goto error; 72303270634SSteven Haigh } 72403270634SSteven Haigh 72503270634SSteven Haigh /* debug code prime the buffer */ 72603270634SSteven Haigh memset(dev->read_buffer_secondary, 'e', in_end_size); 72703270634SSteven Haigh memset(dev->read_buffer_secondary + in_end_size, 'f', in_end_size); 72803270634SSteven Haigh memset(dev->read_buffer_secondary + (2 * in_end_size), 'g', in_end_size); 72903270634SSteven Haigh memset(dev->read_buffer_secondary + (3 * in_end_size), 'h', in_end_size); 73003270634SSteven Haigh 73103270634SSteven Haigh dev->interrupt_in_buffer = kmalloc(in_end_size, GFP_KERNEL); 732a02b55c8SWolfram Sang if (!dev->interrupt_in_buffer) 73303270634SSteven Haigh goto error; 73403270634SSteven Haigh 73503270634SSteven Haigh /* debug code prime the buffer */ 73603270634SSteven Haigh memset(dev->interrupt_in_buffer, 'i', in_end_size); 73703270634SSteven Haigh 73803270634SSteven Haigh dev->interrupt_in_urb = usb_alloc_urb(0, GFP_KERNEL); 73971574a55SWolfram Sang if (!dev->interrupt_in_urb) 74003270634SSteven Haigh goto error; 74103270634SSteven Haigh dev->interrupt_out_buffer = kmalloc(out_end_size, GFP_KERNEL); 742a02b55c8SWolfram Sang if (!dev->interrupt_out_buffer) 74303270634SSteven Haigh goto error; 74403270634SSteven Haigh dev->interrupt_out_urb = usb_alloc_urb(0, GFP_KERNEL); 74571574a55SWolfram Sang if (!dev->interrupt_out_urb) 74603270634SSteven Haigh goto error; 74703270634SSteven Haigh 74803270634SSteven Haigh if (!usb_string(udev, udev->descriptor.iSerialNumber, dev->serial_number, 74903270634SSteven Haigh sizeof(dev->serial_number))) { 75003270634SSteven Haigh dev_err(&interface->dev, "Could not retrieve serial number\n"); 75103270634SSteven Haigh goto error; 75203270634SSteven Haigh } 75366d4bc30SGreg Kroah-Hartman dev_dbg(&interface->dev,"serial_number=%s", dev->serial_number); 75403270634SSteven Haigh 75503270634SSteven Haigh /* we can register the device now, as it is ready */ 75603270634SSteven Haigh usb_set_intfdata(interface, dev); 75703270634SSteven Haigh 75803270634SSteven Haigh retval = usb_register_dev(interface, &adu_class); 75903270634SSteven Haigh 76003270634SSteven Haigh if (retval) { 76103270634SSteven Haigh /* something prevented us from registering this driver */ 76203270634SSteven Haigh dev_err(&interface->dev, "Not able to get a minor for this device.\n"); 76303270634SSteven Haigh usb_set_intfdata(interface, NULL); 76403270634SSteven Haigh goto error; 76503270634SSteven Haigh } 76603270634SSteven Haigh 76703270634SSteven Haigh dev->minor = interface->minor; 76803270634SSteven Haigh 76903270634SSteven Haigh /* let the user know what node this device is now attached to */ 770898eb71cSJoe Perches dev_info(&interface->dev, "ADU%d %s now attached to /dev/usb/adutux%d\n", 771d482b9d5SJohan Hovold le16_to_cpu(udev->descriptor.idProduct), dev->serial_number, 77203270634SSteven Haigh (dev->minor - ADU_MINOR_BASE)); 77303270634SSteven Haigh exit: 77403270634SSteven Haigh return retval; 77503270634SSteven Haigh 77603270634SSteven Haigh error: 77703270634SSteven Haigh adu_delete(dev); 77803270634SSteven Haigh return retval; 77903270634SSteven Haigh } 78003270634SSteven Haigh 78103270634SSteven Haigh /** 78203270634SSteven Haigh * adu_disconnect 78303270634SSteven Haigh * 78403270634SSteven Haigh * Called by the usb core when the device is removed from the system. 78503270634SSteven Haigh */ 78603270634SSteven Haigh static void adu_disconnect(struct usb_interface *interface) 78703270634SSteven Haigh { 78803270634SSteven Haigh struct adu_device *dev; 78903270634SSteven Haigh int minor; 79003270634SSteven Haigh 79103270634SSteven Haigh dev = usb_get_intfdata(interface); 79203270634SSteven Haigh 7938293c568SMatthias Kaehlcke mutex_lock(&dev->mtx); /* not interruptible */ 794f08812d5SPete Zaitcev dev->udev = NULL; /* poison */ 795f08812d5SPete Zaitcev minor = dev->minor; 796f08812d5SPete Zaitcev usb_deregister_dev(interface, &adu_class); 797f08812d5SPete Zaitcev mutex_unlock(&dev->mtx); 798f08812d5SPete Zaitcev 799f08812d5SPete Zaitcev mutex_lock(&adutux_mutex); 800f08812d5SPete Zaitcev usb_set_intfdata(interface, NULL); 801d4ead16fSAlan Stern 80203270634SSteven Haigh /* if the device is not opened, then we clean up right now */ 803f08812d5SPete Zaitcev if (!dev->open_count) 80403270634SSteven Haigh adu_delete(dev); 805f08812d5SPete Zaitcev 806f08812d5SPete Zaitcev mutex_unlock(&adutux_mutex); 80703270634SSteven Haigh } 80803270634SSteven Haigh 80903270634SSteven Haigh /* usb specific object needed to register this driver with the usb subsystem */ 81003270634SSteven Haigh static struct usb_driver adu_driver = { 81103270634SSteven Haigh .name = "adutux", 81203270634SSteven Haigh .probe = adu_probe, 81303270634SSteven Haigh .disconnect = adu_disconnect, 81403270634SSteven Haigh .id_table = device_table, 81503270634SSteven Haigh }; 81603270634SSteven Haigh 81765db4305SGreg Kroah-Hartman module_usb_driver(adu_driver); 81803270634SSteven Haigh 81903270634SSteven Haigh MODULE_AUTHOR(DRIVER_AUTHOR); 82003270634SSteven Haigh MODULE_DESCRIPTION(DRIVER_DESC); 82103270634SSteven Haigh MODULE_LICENSE("GPL"); 822