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 2103270634SSteven Haigh #include <linux/kernel.h> 2203270634SSteven Haigh #include <linux/errno.h> 2303270634SSteven Haigh #include <linux/init.h> 2403270634SSteven Haigh #include <linux/slab.h> 2503270634SSteven Haigh #include <linux/module.h> 2603270634SSteven Haigh #include <linux/usb.h> 278293c568SMatthias Kaehlcke #include <linux/mutex.h> 2840cf4833SLisa Nguyen #include <linux/uaccess.h> 2903270634SSteven Haigh 3003270634SSteven Haigh /* Version Information */ 3103270634SSteven Haigh #define DRIVER_VERSION "v0.0.13" 3203270634SSteven Haigh #define DRIVER_AUTHOR "John Homppi" 3303270634SSteven Haigh #define DRIVER_DESC "adutux (see www.ontrak.net)" 3403270634SSteven Haigh 3503270634SSteven Haigh /* Define these values to match your device */ 3603270634SSteven Haigh #define ADU_VENDOR_ID 0x0a07 3703270634SSteven Haigh #define ADU_PRODUCT_ID 0x0064 3803270634SSteven Haigh 3903270634SSteven Haigh /* table of devices that work with this driver */ 4033b9e162SNémeth Márton static const struct usb_device_id device_table[] = { 4103270634SSteven Haigh { USB_DEVICE(ADU_VENDOR_ID, ADU_PRODUCT_ID) }, /* ADU100 */ 4203270634SSteven Haigh { USB_DEVICE(ADU_VENDOR_ID, ADU_PRODUCT_ID+20) }, /* ADU120 */ 4303270634SSteven Haigh { USB_DEVICE(ADU_VENDOR_ID, ADU_PRODUCT_ID+30) }, /* ADU130 */ 4403270634SSteven Haigh { USB_DEVICE(ADU_VENDOR_ID, ADU_PRODUCT_ID+100) }, /* ADU200 */ 4503270634SSteven Haigh { USB_DEVICE(ADU_VENDOR_ID, ADU_PRODUCT_ID+108) }, /* ADU208 */ 4603270634SSteven Haigh { USB_DEVICE(ADU_VENDOR_ID, ADU_PRODUCT_ID+118) }, /* ADU218 */ 4703270634SSteven Haigh { } /* Terminating entry */ 4803270634SSteven Haigh }; 4903270634SSteven Haigh 5003270634SSteven Haigh MODULE_DEVICE_TABLE(usb, device_table); 5103270634SSteven Haigh 5203270634SSteven Haigh #ifdef CONFIG_USB_DYNAMIC_MINORS 5303270634SSteven Haigh #define ADU_MINOR_BASE 0 5403270634SSteven Haigh #else 5503270634SSteven Haigh #define ADU_MINOR_BASE 67 5603270634SSteven Haigh #endif 5703270634SSteven Haigh 5803270634SSteven Haigh /* we can have up to this number of device plugged in at once */ 5903270634SSteven Haigh #define MAX_DEVICES 16 6003270634SSteven Haigh 6103270634SSteven Haigh #define COMMAND_TIMEOUT (2*HZ) /* 60 second timeout for a command */ 6203270634SSteven Haigh 63f08812d5SPete Zaitcev /* 64f08812d5SPete Zaitcev * The locking scheme is a vanilla 3-lock: 65f08812d5SPete Zaitcev * adu_device.buflock: A spinlock, covers what IRQs touch. 66f08812d5SPete Zaitcev * adutux_mutex: A Static lock to cover open_count. It would also cover 67f08812d5SPete Zaitcev * any globals, but we don't have them in 2.6. 68f08812d5SPete Zaitcev * adu_device.mtx: A mutex to hold across sleepers like copy_from_user. 69f08812d5SPete Zaitcev * It covers all of adu_device, except the open_count 70f08812d5SPete Zaitcev * and what .buflock covers. 71f08812d5SPete Zaitcev */ 72f08812d5SPete Zaitcev 7303270634SSteven Haigh /* Structure to hold all of our device specific stuff */ 7403270634SSteven Haigh struct adu_device { 75f08812d5SPete Zaitcev struct mutex mtx; 7603270634SSteven Haigh struct usb_device *udev; /* save off the usb device pointer */ 7703270634SSteven Haigh struct usb_interface *interface; 78f08812d5SPete Zaitcev unsigned int minor; /* the starting minor number for this device */ 7903270634SSteven Haigh char serial_number[8]; 8003270634SSteven Haigh 8103270634SSteven Haigh int open_count; /* number of times this port has been opened */ 8203270634SSteven Haigh 8303270634SSteven Haigh char *read_buffer_primary; 8403270634SSteven Haigh int read_buffer_length; 8503270634SSteven Haigh char *read_buffer_secondary; 8603270634SSteven Haigh int secondary_head; 8703270634SSteven Haigh int secondary_tail; 8803270634SSteven Haigh spinlock_t buflock; 8903270634SSteven Haigh 9003270634SSteven Haigh wait_queue_head_t read_wait; 9103270634SSteven Haigh wait_queue_head_t write_wait; 9203270634SSteven Haigh 9303270634SSteven Haigh char *interrupt_in_buffer; 9403270634SSteven Haigh struct usb_endpoint_descriptor *interrupt_in_endpoint; 9503270634SSteven Haigh struct urb *interrupt_in_urb; 9603270634SSteven Haigh int read_urb_finished; 9703270634SSteven Haigh 9803270634SSteven Haigh char *interrupt_out_buffer; 9903270634SSteven Haigh struct usb_endpoint_descriptor *interrupt_out_endpoint; 10003270634SSteven Haigh struct urb *interrupt_out_urb; 101f08812d5SPete Zaitcev int out_urb_finished; 10203270634SSteven Haigh }; 10303270634SSteven Haigh 104f08812d5SPete Zaitcev static DEFINE_MUTEX(adutux_mutex); 105f08812d5SPete Zaitcev 10603270634SSteven Haigh static struct usb_driver adu_driver; 10703270634SSteven Haigh 108*1ef37c60SGreg Kroah-Hartman static inline void adu_debug_data(struct device *dev, const char *function, 109*1ef37c60SGreg Kroah-Hartman int size, const unsigned char *data) 11003270634SSteven Haigh { 111*1ef37c60SGreg Kroah-Hartman dev_dbg(dev, "%s - length = %d, data = %*ph\n", 112*1ef37c60SGreg Kroah-Hartman function, size, size, data); 11303270634SSteven Haigh } 11403270634SSteven Haigh 11503270634SSteven Haigh /** 11603270634SSteven Haigh * adu_abort_transfers 11703270634SSteven Haigh * aborts transfers and frees associated data structures 11803270634SSteven Haigh */ 11903270634SSteven Haigh static void adu_abort_transfers(struct adu_device *dev) 12003270634SSteven Haigh { 121f08812d5SPete Zaitcev unsigned long flags; 12203270634SSteven Haigh 12366d4bc30SGreg Kroah-Hartman if (dev->udev == NULL) 1246e42a158SGreg Kroah-Hartman return; 12503270634SSteven Haigh 12603270634SSteven Haigh /* shutdown transfer */ 127f08812d5SPete Zaitcev 128f08812d5SPete Zaitcev /* XXX Anchor these instead */ 129f08812d5SPete Zaitcev spin_lock_irqsave(&dev->buflock, flags); 130f08812d5SPete Zaitcev if (!dev->read_urb_finished) { 131f08812d5SPete Zaitcev spin_unlock_irqrestore(&dev->buflock, flags); 132f08812d5SPete Zaitcev usb_kill_urb(dev->interrupt_in_urb); 133f08812d5SPete Zaitcev } else 134f08812d5SPete Zaitcev spin_unlock_irqrestore(&dev->buflock, flags); 135f08812d5SPete Zaitcev 136f08812d5SPete Zaitcev spin_lock_irqsave(&dev->buflock, flags); 137f08812d5SPete Zaitcev if (!dev->out_urb_finished) { 138f08812d5SPete Zaitcev spin_unlock_irqrestore(&dev->buflock, flags); 139f08812d5SPete Zaitcev usb_kill_urb(dev->interrupt_out_urb); 140f08812d5SPete Zaitcev } else 141f08812d5SPete Zaitcev spin_unlock_irqrestore(&dev->buflock, flags); 14203270634SSteven Haigh } 14303270634SSteven Haigh 14403270634SSteven Haigh static void adu_delete(struct adu_device *dev) 14503270634SSteven Haigh { 14603270634SSteven Haigh /* free data structures */ 14703270634SSteven Haigh usb_free_urb(dev->interrupt_in_urb); 14803270634SSteven Haigh usb_free_urb(dev->interrupt_out_urb); 14903270634SSteven Haigh kfree(dev->read_buffer_primary); 15003270634SSteven Haigh kfree(dev->read_buffer_secondary); 15103270634SSteven Haigh kfree(dev->interrupt_in_buffer); 15203270634SSteven Haigh kfree(dev->interrupt_out_buffer); 15303270634SSteven Haigh kfree(dev); 15403270634SSteven Haigh } 15503270634SSteven Haigh 1567d12e780SDavid Howells static void adu_interrupt_in_callback(struct urb *urb) 15703270634SSteven Haigh { 15803270634SSteven Haigh struct adu_device *dev = urb->context; 15924497a00SGreg Kroah-Hartman int status = urb->status; 16003270634SSteven Haigh 161*1ef37c60SGreg Kroah-Hartman adu_debug_data(&dev->udev->dev, __func__, 162*1ef37c60SGreg Kroah-Hartman urb->actual_length, urb->transfer_buffer); 16303270634SSteven Haigh 16403270634SSteven Haigh spin_lock(&dev->buflock); 16503270634SSteven Haigh 16624497a00SGreg Kroah-Hartman if (status != 0) { 167f6c1ceaaSOliver Neukum if ((status != -ENOENT) && (status != -ECONNRESET) && 168f6c1ceaaSOliver Neukum (status != -ESHUTDOWN)) { 16966d4bc30SGreg Kroah-Hartman dev_dbg(&dev->udev->dev, 17066d4bc30SGreg Kroah-Hartman "%s : nonzero status received: %d\n", 171441b62c1SHarvey Harrison __func__, status); 17203270634SSteven Haigh } 17303270634SSteven Haigh goto exit; 17403270634SSteven Haigh } 17503270634SSteven Haigh 17603270634SSteven Haigh if (urb->actual_length > 0 && dev->interrupt_in_buffer[0] != 0x00) { 17703270634SSteven Haigh if (dev->read_buffer_length < 17829cc8897SKuninori Morimoto (4 * usb_endpoint_maxp(dev->interrupt_in_endpoint)) - 17903270634SSteven Haigh (urb->actual_length)) { 18003270634SSteven Haigh memcpy (dev->read_buffer_primary + 18103270634SSteven Haigh dev->read_buffer_length, 18203270634SSteven Haigh dev->interrupt_in_buffer, urb->actual_length); 18303270634SSteven Haigh 18403270634SSteven Haigh dev->read_buffer_length += urb->actual_length; 18566d4bc30SGreg Kroah-Hartman dev_dbg(&dev->udev->dev,"%s reading %d\n", __func__, 18603270634SSteven Haigh urb->actual_length); 18703270634SSteven Haigh } else { 18866d4bc30SGreg Kroah-Hartman dev_dbg(&dev->udev->dev,"%s : read_buffer overflow\n", 18966d4bc30SGreg Kroah-Hartman __func__); 19003270634SSteven Haigh } 19103270634SSteven Haigh } 19203270634SSteven Haigh 19303270634SSteven Haigh exit: 19403270634SSteven Haigh dev->read_urb_finished = 1; 19503270634SSteven Haigh spin_unlock(&dev->buflock); 19603270634SSteven Haigh /* always wake up so we recover from errors */ 19703270634SSteven Haigh wake_up_interruptible(&dev->read_wait); 19803270634SSteven Haigh } 19903270634SSteven Haigh 2007d12e780SDavid Howells static void adu_interrupt_out_callback(struct urb *urb) 20103270634SSteven Haigh { 20203270634SSteven Haigh struct adu_device *dev = urb->context; 20324497a00SGreg Kroah-Hartman int status = urb->status; 20403270634SSteven Haigh 205*1ef37c60SGreg Kroah-Hartman adu_debug_data(&dev->udev->dev, __func__, 206*1ef37c60SGreg Kroah-Hartman urb->actual_length, urb->transfer_buffer); 20703270634SSteven Haigh 20824497a00SGreg Kroah-Hartman if (status != 0) { 20924497a00SGreg Kroah-Hartman if ((status != -ENOENT) && 21024497a00SGreg Kroah-Hartman (status != -ECONNRESET)) { 21166d4bc30SGreg Kroah-Hartman dev_dbg(&dev->udev->dev, 21266d4bc30SGreg Kroah-Hartman "%s :nonzero status received: %d\n", __func__, 21366d4bc30SGreg Kroah-Hartman status); 21403270634SSteven Haigh } 215*1ef37c60SGreg Kroah-Hartman return; 21603270634SSteven Haigh } 21703270634SSteven Haigh 218f08812d5SPete Zaitcev spin_lock(&dev->buflock); 219f08812d5SPete Zaitcev dev->out_urb_finished = 1; 220f08812d5SPete Zaitcev wake_up(&dev->write_wait); 221f08812d5SPete Zaitcev spin_unlock(&dev->buflock); 22203270634SSteven Haigh } 22303270634SSteven Haigh 22403270634SSteven Haigh static int adu_open(struct inode *inode, struct file *file) 22503270634SSteven Haigh { 22603270634SSteven Haigh struct adu_device *dev = NULL; 22703270634SSteven Haigh struct usb_interface *interface; 22803270634SSteven Haigh int subminor; 229f08812d5SPete Zaitcev int retval; 23003270634SSteven Haigh 23103270634SSteven Haigh subminor = iminor(inode); 23203270634SSteven Haigh 233e77c4e6aSLisa Nguyen retval = mutex_lock_interruptible(&adutux_mutex); 23466d4bc30SGreg Kroah-Hartman if (retval) 235f08812d5SPete Zaitcev goto exit_no_lock; 236f08812d5SPete Zaitcev 23703270634SSteven Haigh interface = usb_find_interface(&adu_driver, subminor); 23803270634SSteven Haigh if (!interface) { 239fd3f1917SGreg Kroah-Hartman printk(KERN_ERR "adutux: %s - error, can't find device for " 240fd3f1917SGreg Kroah-Hartman "minor %d\n", __func__, subminor); 24103270634SSteven Haigh retval = -ENODEV; 24203270634SSteven Haigh goto exit_no_device; 24303270634SSteven Haigh } 24403270634SSteven Haigh 24503270634SSteven Haigh dev = usb_get_intfdata(interface); 246f08812d5SPete Zaitcev if (!dev || !dev->udev) { 24703270634SSteven Haigh retval = -ENODEV; 24803270634SSteven Haigh goto exit_no_device; 24903270634SSteven Haigh } 25003270634SSteven Haigh 251f08812d5SPete Zaitcev /* check that nobody else is using the device */ 252f08812d5SPete Zaitcev if (dev->open_count) { 253f08812d5SPete Zaitcev retval = -EBUSY; 25403270634SSteven Haigh goto exit_no_device; 25503270634SSteven Haigh } 25603270634SSteven Haigh 25703270634SSteven Haigh ++dev->open_count; 25866d4bc30SGreg Kroah-Hartman dev_dbg(&dev->udev->dev, "%s: open count %d\n", __func__, 25966d4bc30SGreg Kroah-Hartman dev->open_count); 26003270634SSteven Haigh 26103270634SSteven Haigh /* save device in the file's private structure */ 26203270634SSteven Haigh file->private_data = dev; 26303270634SSteven Haigh 26403270634SSteven Haigh /* initialize in direction */ 26503270634SSteven Haigh dev->read_buffer_length = 0; 26603270634SSteven Haigh 26703270634SSteven Haigh /* fixup first read by having urb waiting for it */ 26803270634SSteven Haigh usb_fill_int_urb(dev->interrupt_in_urb, dev->udev, 26903270634SSteven Haigh usb_rcvintpipe(dev->udev, 27003270634SSteven Haigh dev->interrupt_in_endpoint->bEndpointAddress), 27103270634SSteven Haigh dev->interrupt_in_buffer, 27229cc8897SKuninori Morimoto usb_endpoint_maxp(dev->interrupt_in_endpoint), 27303270634SSteven Haigh adu_interrupt_in_callback, dev, 27403270634SSteven Haigh dev->interrupt_in_endpoint->bInterval); 27503270634SSteven Haigh dev->read_urb_finished = 0; 276f08812d5SPete Zaitcev if (usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL)) 277f08812d5SPete Zaitcev dev->read_urb_finished = 1; 278f08812d5SPete Zaitcev /* we ignore failure */ 279f08812d5SPete Zaitcev /* end of fixup for first read */ 280f08812d5SPete Zaitcev 281f08812d5SPete Zaitcev /* initialize out direction */ 282f08812d5SPete Zaitcev dev->out_urb_finished = 1; 283f08812d5SPete Zaitcev 284f08812d5SPete Zaitcev retval = 0; 28503270634SSteven Haigh 28603270634SSteven Haigh exit_no_device: 287f08812d5SPete Zaitcev mutex_unlock(&adutux_mutex); 288f08812d5SPete Zaitcev exit_no_lock: 28903270634SSteven Haigh return retval; 29003270634SSteven Haigh } 29103270634SSteven Haigh 292f08812d5SPete Zaitcev static void adu_release_internal(struct adu_device *dev) 29303270634SSteven Haigh { 29403270634SSteven Haigh /* decrement our usage count for the device */ 29503270634SSteven Haigh --dev->open_count; 29666d4bc30SGreg Kroah-Hartman dev_dbg(&dev->udev->dev, "%s : open count %d\n", __func__, 29766d4bc30SGreg Kroah-Hartman dev->open_count); 29803270634SSteven Haigh if (dev->open_count <= 0) { 29903270634SSteven Haigh adu_abort_transfers(dev); 30003270634SSteven Haigh dev->open_count = 0; 30103270634SSteven Haigh } 30203270634SSteven Haigh } 30303270634SSteven Haigh 30403270634SSteven Haigh static int adu_release(struct inode *inode, struct file *file) 30503270634SSteven Haigh { 306f08812d5SPete Zaitcev struct adu_device *dev; 30703270634SSteven Haigh int retval = 0; 30803270634SSteven Haigh 30903270634SSteven Haigh if (file == NULL) { 31003270634SSteven Haigh retval = -ENODEV; 31103270634SSteven Haigh goto exit; 31203270634SSteven Haigh } 31303270634SSteven Haigh 31403270634SSteven Haigh dev = file->private_data; 31503270634SSteven Haigh if (dev == NULL) { 31603270634SSteven Haigh retval = -ENODEV; 31703270634SSteven Haigh goto exit; 31803270634SSteven Haigh } 31903270634SSteven Haigh 320f08812d5SPete Zaitcev mutex_lock(&adutux_mutex); /* not interruptible */ 32103270634SSteven Haigh 32203270634SSteven Haigh if (dev->open_count <= 0) { 32366d4bc30SGreg Kroah-Hartman dev_dbg(&dev->udev->dev, "%s : device not opened\n", __func__); 32403270634SSteven Haigh retval = -ENODEV; 32546c9844cSJiri Slaby goto unlock; 32603270634SSteven Haigh } 32703270634SSteven Haigh 328f08812d5SPete Zaitcev adu_release_internal(dev); 329d4ead16fSAlan Stern if (dev->udev == NULL) { 330d4ead16fSAlan Stern /* the device was unplugged before the file was released */ 331f08812d5SPete Zaitcev if (!dev->open_count) /* ... and we're the last user */ 332d4ead16fSAlan Stern adu_delete(dev); 333d4ead16fSAlan Stern } 33446c9844cSJiri Slaby unlock: 335f08812d5SPete Zaitcev mutex_unlock(&adutux_mutex); 33646c9844cSJiri Slaby exit: 33703270634SSteven Haigh return retval; 33803270634SSteven Haigh } 33903270634SSteven Haigh 34003270634SSteven Haigh static ssize_t adu_read(struct file *file, __user char *buffer, size_t count, 34103270634SSteven Haigh loff_t *ppos) 34203270634SSteven Haigh { 34303270634SSteven Haigh struct adu_device *dev; 34403270634SSteven Haigh size_t bytes_read = 0; 34503270634SSteven Haigh size_t bytes_to_read = count; 34603270634SSteven Haigh int i; 34703270634SSteven Haigh int retval = 0; 34803270634SSteven Haigh int timeout = 0; 34903270634SSteven Haigh int should_submit = 0; 35003270634SSteven Haigh unsigned long flags; 35103270634SSteven Haigh DECLARE_WAITQUEUE(wait, current); 35203270634SSteven Haigh 35303270634SSteven Haigh dev = file->private_data; 3548293c568SMatthias Kaehlcke if (mutex_lock_interruptible(&dev->mtx)) 35503270634SSteven Haigh return -ERESTARTSYS; 35603270634SSteven Haigh 35703270634SSteven Haigh /* verify that the device wasn't unplugged */ 358f08812d5SPete Zaitcev if (dev->udev == NULL) { 35903270634SSteven Haigh retval = -ENODEV; 360fd3f1917SGreg Kroah-Hartman printk(KERN_ERR "adutux: No device or device unplugged %d\n", 361fd3f1917SGreg Kroah-Hartman retval); 36203270634SSteven Haigh goto exit; 36303270634SSteven Haigh } 36403270634SSteven Haigh 36503270634SSteven Haigh /* verify that some data was requested */ 36603270634SSteven Haigh if (count == 0) { 36766d4bc30SGreg Kroah-Hartman dev_dbg(&dev->udev->dev, "%s : read request of 0 bytes\n", 36866d4bc30SGreg Kroah-Hartman __func__); 36903270634SSteven Haigh goto exit; 37003270634SSteven Haigh } 37103270634SSteven Haigh 37203270634SSteven Haigh timeout = COMMAND_TIMEOUT; 37366d4bc30SGreg Kroah-Hartman dev_dbg(&dev->udev->dev, "%s : about to start looping\n", __func__); 37403270634SSteven Haigh while (bytes_to_read) { 37503270634SSteven Haigh int data_in_secondary = dev->secondary_tail - dev->secondary_head; 37666d4bc30SGreg Kroah-Hartman dev_dbg(&dev->udev->dev, 37766d4bc30SGreg Kroah-Hartman "%s : while, data_in_secondary=%d, status=%d\n", 378441b62c1SHarvey Harrison __func__, data_in_secondary, 37903270634SSteven Haigh dev->interrupt_in_urb->status); 38003270634SSteven Haigh 38103270634SSteven Haigh if (data_in_secondary) { 38203270634SSteven Haigh /* drain secondary buffer */ 38303270634SSteven Haigh int amount = bytes_to_read < data_in_secondary ? bytes_to_read : data_in_secondary; 38403270634SSteven Haigh i = copy_to_user(buffer, dev->read_buffer_secondary+dev->secondary_head, amount); 3851865a9c3SKulikov Vasiliy if (i) { 38603270634SSteven Haigh retval = -EFAULT; 38703270634SSteven Haigh goto exit; 38803270634SSteven Haigh } 38903270634SSteven Haigh dev->secondary_head += (amount - i); 39003270634SSteven Haigh bytes_read += (amount - i); 39103270634SSteven Haigh bytes_to_read -= (amount - i); 39203270634SSteven Haigh if (i) { 39303270634SSteven Haigh retval = bytes_read ? bytes_read : -EFAULT; 39403270634SSteven Haigh goto exit; 39503270634SSteven Haigh } 39603270634SSteven Haigh } else { 39703270634SSteven Haigh /* we check the primary buffer */ 39803270634SSteven Haigh spin_lock_irqsave (&dev->buflock, flags); 39903270634SSteven Haigh if (dev->read_buffer_length) { 40003270634SSteven Haigh /* we secure access to the primary */ 40103270634SSteven Haigh char *tmp; 40266d4bc30SGreg Kroah-Hartman dev_dbg(&dev->udev->dev, 40366d4bc30SGreg Kroah-Hartman "%s : swap, read_buffer_length = %d\n", 404441b62c1SHarvey Harrison __func__, dev->read_buffer_length); 40503270634SSteven Haigh tmp = dev->read_buffer_secondary; 40603270634SSteven Haigh dev->read_buffer_secondary = dev->read_buffer_primary; 40703270634SSteven Haigh dev->read_buffer_primary = tmp; 40803270634SSteven Haigh dev->secondary_head = 0; 40903270634SSteven Haigh dev->secondary_tail = dev->read_buffer_length; 41003270634SSteven Haigh dev->read_buffer_length = 0; 41103270634SSteven Haigh spin_unlock_irqrestore(&dev->buflock, flags); 41203270634SSteven Haigh /* we have a free buffer so use it */ 41303270634SSteven Haigh should_submit = 1; 41403270634SSteven Haigh } else { 41503270634SSteven Haigh /* even the primary was empty - we may need to do IO */ 416f08812d5SPete Zaitcev if (!dev->read_urb_finished) { 41703270634SSteven Haigh /* somebody is doing IO */ 41803270634SSteven Haigh spin_unlock_irqrestore(&dev->buflock, flags); 41966d4bc30SGreg Kroah-Hartman dev_dbg(&dev->udev->dev, 42066d4bc30SGreg Kroah-Hartman "%s : submitted already\n", 42166d4bc30SGreg Kroah-Hartman __func__); 42203270634SSteven Haigh } else { 42303270634SSteven Haigh /* we must initiate input */ 42466d4bc30SGreg Kroah-Hartman dev_dbg(&dev->udev->dev, 42566d4bc30SGreg Kroah-Hartman "%s : initiate input\n", 42666d4bc30SGreg Kroah-Hartman __func__); 42703270634SSteven Haigh dev->read_urb_finished = 0; 428f08812d5SPete Zaitcev spin_unlock_irqrestore(&dev->buflock, flags); 42903270634SSteven Haigh 43003270634SSteven Haigh usb_fill_int_urb(dev->interrupt_in_urb, dev->udev, 43103270634SSteven Haigh usb_rcvintpipe(dev->udev, 43203270634SSteven Haigh dev->interrupt_in_endpoint->bEndpointAddress), 43303270634SSteven Haigh dev->interrupt_in_buffer, 43429cc8897SKuninori Morimoto usb_endpoint_maxp(dev->interrupt_in_endpoint), 43503270634SSteven Haigh adu_interrupt_in_callback, 43603270634SSteven Haigh dev, 43703270634SSteven Haigh dev->interrupt_in_endpoint->bInterval); 438f08812d5SPete Zaitcev retval = usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL); 439f08812d5SPete Zaitcev if (retval) { 440f08812d5SPete Zaitcev dev->read_urb_finished = 1; 44103270634SSteven Haigh if (retval == -ENOMEM) { 44203270634SSteven Haigh retval = bytes_read ? bytes_read : -ENOMEM; 44303270634SSteven Haigh } 44466d4bc30SGreg Kroah-Hartman dev_dbg(&dev->udev->dev, 44566d4bc30SGreg Kroah-Hartman "%s : submit failed\n", 44666d4bc30SGreg Kroah-Hartman __func__); 44703270634SSteven Haigh goto exit; 44803270634SSteven Haigh } 44903270634SSteven Haigh } 45003270634SSteven Haigh 45103270634SSteven Haigh /* we wait for I/O to complete */ 45203270634SSteven Haigh set_current_state(TASK_INTERRUPTIBLE); 45303270634SSteven Haigh add_wait_queue(&dev->read_wait, &wait); 454f08812d5SPete Zaitcev spin_lock_irqsave(&dev->buflock, flags); 455f08812d5SPete Zaitcev if (!dev->read_urb_finished) { 456f08812d5SPete Zaitcev spin_unlock_irqrestore(&dev->buflock, flags); 45703270634SSteven Haigh timeout = schedule_timeout(COMMAND_TIMEOUT); 458f08812d5SPete Zaitcev } else { 459f08812d5SPete Zaitcev spin_unlock_irqrestore(&dev->buflock, flags); 46003270634SSteven Haigh set_current_state(TASK_RUNNING); 461f08812d5SPete Zaitcev } 46203270634SSteven Haigh remove_wait_queue(&dev->read_wait, &wait); 46303270634SSteven Haigh 46403270634SSteven Haigh if (timeout <= 0) { 46566d4bc30SGreg Kroah-Hartman dev_dbg(&dev->udev->dev, 46666d4bc30SGreg Kroah-Hartman "%s : timeout\n", __func__); 46703270634SSteven Haigh retval = bytes_read ? bytes_read : -ETIMEDOUT; 46803270634SSteven Haigh goto exit; 46903270634SSteven Haigh } 47003270634SSteven Haigh 47103270634SSteven Haigh if (signal_pending(current)) { 47266d4bc30SGreg Kroah-Hartman dev_dbg(&dev->udev->dev, 47366d4bc30SGreg Kroah-Hartman "%s : signal pending\n", 47466d4bc30SGreg Kroah-Hartman __func__); 47503270634SSteven Haigh retval = bytes_read ? bytes_read : -EINTR; 47603270634SSteven Haigh goto exit; 47703270634SSteven Haigh } 47803270634SSteven Haigh } 47903270634SSteven Haigh } 48003270634SSteven Haigh } 48103270634SSteven Haigh 48203270634SSteven Haigh retval = bytes_read; 48303270634SSteven Haigh /* if the primary buffer is empty then use it */ 484f08812d5SPete Zaitcev spin_lock_irqsave(&dev->buflock, flags); 485f08812d5SPete Zaitcev if (should_submit && dev->read_urb_finished) { 486f08812d5SPete Zaitcev dev->read_urb_finished = 0; 487f08812d5SPete Zaitcev spin_unlock_irqrestore(&dev->buflock, flags); 48803270634SSteven Haigh usb_fill_int_urb(dev->interrupt_in_urb, dev->udev, 48903270634SSteven Haigh usb_rcvintpipe(dev->udev, 49003270634SSteven Haigh dev->interrupt_in_endpoint->bEndpointAddress), 49103270634SSteven Haigh dev->interrupt_in_buffer, 49229cc8897SKuninori Morimoto usb_endpoint_maxp(dev->interrupt_in_endpoint), 49303270634SSteven Haigh adu_interrupt_in_callback, 49403270634SSteven Haigh dev, 49503270634SSteven Haigh dev->interrupt_in_endpoint->bInterval); 496f08812d5SPete Zaitcev if (usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL) != 0) 497f08812d5SPete Zaitcev dev->read_urb_finished = 1; 49803270634SSteven Haigh /* we ignore failure */ 499f08812d5SPete Zaitcev } else { 500f08812d5SPete Zaitcev spin_unlock_irqrestore(&dev->buflock, flags); 50103270634SSteven Haigh } 50203270634SSteven Haigh 50303270634SSteven Haigh exit: 50403270634SSteven Haigh /* unlock the device */ 5058293c568SMatthias Kaehlcke mutex_unlock(&dev->mtx); 50603270634SSteven Haigh 50703270634SSteven Haigh return retval; 50803270634SSteven Haigh } 50903270634SSteven Haigh 51003270634SSteven Haigh static ssize_t adu_write(struct file *file, const __user char *buffer, 51103270634SSteven Haigh size_t count, loff_t *ppos) 51203270634SSteven Haigh { 513f08812d5SPete Zaitcev DECLARE_WAITQUEUE(waita, current); 51403270634SSteven Haigh struct adu_device *dev; 51503270634SSteven Haigh size_t bytes_written = 0; 51603270634SSteven Haigh size_t bytes_to_write; 51703270634SSteven Haigh size_t buffer_size; 518f08812d5SPete Zaitcev unsigned long flags; 519ebc3ac14SOliver Neukum int retval; 52003270634SSteven Haigh 52103270634SSteven Haigh dev = file->private_data; 52203270634SSteven Haigh 5238293c568SMatthias Kaehlcke retval = mutex_lock_interruptible(&dev->mtx); 524ebc3ac14SOliver Neukum if (retval) 525ebc3ac14SOliver Neukum goto exit_nolock; 52603270634SSteven Haigh 52703270634SSteven Haigh /* verify that the device wasn't unplugged */ 528f08812d5SPete Zaitcev if (dev->udev == NULL) { 52903270634SSteven Haigh retval = -ENODEV; 530fd3f1917SGreg Kroah-Hartman printk(KERN_ERR "adutux: No device or device unplugged %d\n", 531fd3f1917SGreg Kroah-Hartman retval); 53203270634SSteven Haigh goto exit; 53303270634SSteven Haigh } 53403270634SSteven Haigh 53503270634SSteven Haigh /* verify that we actually have some data to write */ 53603270634SSteven Haigh if (count == 0) { 53766d4bc30SGreg Kroah-Hartman dev_dbg(&dev->udev->dev, "%s : write request of 0 bytes\n", 53866d4bc30SGreg Kroah-Hartman __func__); 53903270634SSteven Haigh goto exit; 54003270634SSteven Haigh } 54103270634SSteven Haigh 54203270634SSteven Haigh while (count > 0) { 543f08812d5SPete Zaitcev add_wait_queue(&dev->write_wait, &waita); 544f08812d5SPete Zaitcev set_current_state(TASK_INTERRUPTIBLE); 545f08812d5SPete Zaitcev spin_lock_irqsave(&dev->buflock, flags); 546f08812d5SPete Zaitcev if (!dev->out_urb_finished) { 547f08812d5SPete Zaitcev spin_unlock_irqrestore(&dev->buflock, flags); 54803270634SSteven Haigh 549f08812d5SPete Zaitcev mutex_unlock(&dev->mtx); 55003270634SSteven Haigh if (signal_pending(current)) { 55166d4bc30SGreg Kroah-Hartman dev_dbg(&dev->udev->dev, "%s : interrupted\n", 55266d4bc30SGreg Kroah-Hartman __func__); 553f08812d5SPete Zaitcev set_current_state(TASK_RUNNING); 55403270634SSteven Haigh retval = -EINTR; 555f08812d5SPete Zaitcev goto exit_onqueue; 55603270634SSteven Haigh } 557f08812d5SPete Zaitcev if (schedule_timeout(COMMAND_TIMEOUT) == 0) { 55866d4bc30SGreg Kroah-Hartman dev_dbg(&dev->udev->dev, 55966d4bc30SGreg Kroah-Hartman "%s - command timed out.\n", __func__); 560f08812d5SPete Zaitcev retval = -ETIMEDOUT; 561f08812d5SPete Zaitcev goto exit_onqueue; 562f08812d5SPete Zaitcev } 563f08812d5SPete Zaitcev remove_wait_queue(&dev->write_wait, &waita); 5648293c568SMatthias Kaehlcke retval = mutex_lock_interruptible(&dev->mtx); 565ebc3ac14SOliver Neukum if (retval) { 566ebc3ac14SOliver Neukum retval = bytes_written ? bytes_written : retval; 567ebc3ac14SOliver Neukum goto exit_nolock; 568ebc3ac14SOliver Neukum } 56903270634SSteven Haigh 57066d4bc30SGreg Kroah-Hartman dev_dbg(&dev->udev->dev, 57166d4bc30SGreg Kroah-Hartman "%s : in progress, count = %Zd\n", 57266d4bc30SGreg Kroah-Hartman __func__, count); 57303270634SSteven Haigh } else { 574f08812d5SPete Zaitcev spin_unlock_irqrestore(&dev->buflock, flags); 575f08812d5SPete Zaitcev set_current_state(TASK_RUNNING); 576f08812d5SPete Zaitcev remove_wait_queue(&dev->write_wait, &waita); 57766d4bc30SGreg Kroah-Hartman dev_dbg(&dev->udev->dev, "%s : sending, count = %Zd\n", 57866d4bc30SGreg Kroah-Hartman __func__, count); 57903270634SSteven Haigh 58003270634SSteven Haigh /* write the data into interrupt_out_buffer from userspace */ 58129cc8897SKuninori Morimoto buffer_size = usb_endpoint_maxp(dev->interrupt_out_endpoint); 58203270634SSteven Haigh bytes_to_write = count > buffer_size ? buffer_size : count; 58366d4bc30SGreg Kroah-Hartman dev_dbg(&dev->udev->dev, 58466d4bc30SGreg Kroah-Hartman "%s : buffer_size = %Zd, count = %Zd, bytes_to_write = %Zd\n", 585441b62c1SHarvey Harrison __func__, buffer_size, count, bytes_to_write); 58603270634SSteven Haigh 58703270634SSteven Haigh if (copy_from_user(dev->interrupt_out_buffer, buffer, bytes_to_write) != 0) { 58803270634SSteven Haigh retval = -EFAULT; 58903270634SSteven Haigh goto exit; 59003270634SSteven Haigh } 59103270634SSteven Haigh 59203270634SSteven Haigh /* send off the urb */ 59303270634SSteven Haigh usb_fill_int_urb( 59403270634SSteven Haigh dev->interrupt_out_urb, 59503270634SSteven Haigh dev->udev, 59603270634SSteven Haigh usb_sndintpipe(dev->udev, dev->interrupt_out_endpoint->bEndpointAddress), 59703270634SSteven Haigh dev->interrupt_out_buffer, 59803270634SSteven Haigh bytes_to_write, 59903270634SSteven Haigh adu_interrupt_out_callback, 60003270634SSteven Haigh dev, 601f08812d5SPete Zaitcev dev->interrupt_out_endpoint->bInterval); 60203270634SSteven Haigh dev->interrupt_out_urb->actual_length = bytes_to_write; 603f08812d5SPete Zaitcev dev->out_urb_finished = 0; 60403270634SSteven Haigh retval = usb_submit_urb(dev->interrupt_out_urb, GFP_KERNEL); 60503270634SSteven Haigh if (retval < 0) { 606f08812d5SPete Zaitcev dev->out_urb_finished = 1; 607fd3f1917SGreg Kroah-Hartman dev_err(&dev->udev->dev, "Couldn't submit " 608fd3f1917SGreg Kroah-Hartman "interrupt_out_urb %d\n", retval); 60903270634SSteven Haigh goto exit; 61003270634SSteven Haigh } 61103270634SSteven Haigh 61203270634SSteven Haigh buffer += bytes_to_write; 61303270634SSteven Haigh count -= bytes_to_write; 61403270634SSteven Haigh 61503270634SSteven Haigh bytes_written += bytes_to_write; 61603270634SSteven Haigh } 61703270634SSteven Haigh } 618f08812d5SPete Zaitcev mutex_unlock(&dev->mtx); 619f08812d5SPete Zaitcev return bytes_written; 62003270634SSteven Haigh 62103270634SSteven Haigh exit: 6228293c568SMatthias Kaehlcke mutex_unlock(&dev->mtx); 623ebc3ac14SOliver Neukum exit_nolock: 624f08812d5SPete Zaitcev return retval; 62503270634SSteven Haigh 626f08812d5SPete Zaitcev exit_onqueue: 627f08812d5SPete Zaitcev remove_wait_queue(&dev->write_wait, &waita); 62803270634SSteven Haigh return retval; 62903270634SSteven Haigh } 63003270634SSteven Haigh 63103270634SSteven Haigh /* file operations needed when we register this driver */ 63200977a59SArjan van de Ven static const struct file_operations adu_fops = { 63303270634SSteven Haigh .owner = THIS_MODULE, 63403270634SSteven Haigh .read = adu_read, 63503270634SSteven Haigh .write = adu_write, 63603270634SSteven Haigh .open = adu_open, 63703270634SSteven Haigh .release = adu_release, 6386038f373SArnd Bergmann .llseek = noop_llseek, 63903270634SSteven Haigh }; 64003270634SSteven Haigh 64103270634SSteven Haigh /* 64203270634SSteven Haigh * usb class driver info in order to get a minor number from the usb core, 64303270634SSteven Haigh * and to have the device registered with devfs and the driver core 64403270634SSteven Haigh */ 64503270634SSteven Haigh static struct usb_class_driver adu_class = { 64603270634SSteven Haigh .name = "usb/adutux%d", 64703270634SSteven Haigh .fops = &adu_fops, 64803270634SSteven Haigh .minor_base = ADU_MINOR_BASE, 64903270634SSteven Haigh }; 65003270634SSteven Haigh 65103270634SSteven Haigh /** 65203270634SSteven Haigh * adu_probe 65303270634SSteven Haigh * 65403270634SSteven Haigh * Called by the usb core when a new device is connected that it thinks 65503270634SSteven Haigh * this driver might be interested in. 65603270634SSteven Haigh */ 65703270634SSteven Haigh static int adu_probe(struct usb_interface *interface, 65803270634SSteven Haigh const struct usb_device_id *id) 65903270634SSteven Haigh { 66003270634SSteven Haigh struct usb_device *udev = interface_to_usbdev(interface); 66103270634SSteven Haigh struct adu_device *dev = NULL; 66203270634SSteven Haigh struct usb_host_interface *iface_desc; 66303270634SSteven Haigh struct usb_endpoint_descriptor *endpoint; 66403270634SSteven Haigh int retval = -ENODEV; 66503270634SSteven Haigh int in_end_size; 66603270634SSteven Haigh int out_end_size; 66703270634SSteven Haigh int i; 66803270634SSteven Haigh 66903270634SSteven Haigh if (udev == NULL) { 67003270634SSteven Haigh dev_err(&interface->dev, "udev is NULL.\n"); 67103270634SSteven Haigh goto exit; 67203270634SSteven Haigh } 67303270634SSteven Haigh 674b595076aSUwe Kleine-König /* allocate memory for our device state and initialize it */ 67503270634SSteven Haigh dev = kzalloc(sizeof(struct adu_device), GFP_KERNEL); 67603270634SSteven Haigh if (dev == NULL) { 67703270634SSteven Haigh dev_err(&interface->dev, "Out of memory\n"); 67803270634SSteven Haigh retval = -ENOMEM; 67903270634SSteven Haigh goto exit; 68003270634SSteven Haigh } 68103270634SSteven Haigh 6828293c568SMatthias Kaehlcke mutex_init(&dev->mtx); 68303270634SSteven Haigh spin_lock_init(&dev->buflock); 68403270634SSteven Haigh dev->udev = udev; 68503270634SSteven Haigh init_waitqueue_head(&dev->read_wait); 68603270634SSteven Haigh init_waitqueue_head(&dev->write_wait); 68703270634SSteven Haigh 68803270634SSteven Haigh iface_desc = &interface->altsetting[0]; 68903270634SSteven Haigh 69003270634SSteven Haigh /* set up the endpoint information */ 69103270634SSteven Haigh for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) { 69203270634SSteven Haigh endpoint = &iface_desc->endpoint[i].desc; 69303270634SSteven Haigh 69403270634SSteven Haigh if (usb_endpoint_is_int_in(endpoint)) 69503270634SSteven Haigh dev->interrupt_in_endpoint = endpoint; 69603270634SSteven Haigh 69703270634SSteven Haigh if (usb_endpoint_is_int_out(endpoint)) 69803270634SSteven Haigh dev->interrupt_out_endpoint = endpoint; 69903270634SSteven Haigh } 70003270634SSteven Haigh if (dev->interrupt_in_endpoint == NULL) { 70103270634SSteven Haigh dev_err(&interface->dev, "interrupt in endpoint not found\n"); 70203270634SSteven Haigh goto error; 70303270634SSteven Haigh } 70403270634SSteven Haigh if (dev->interrupt_out_endpoint == NULL) { 70503270634SSteven Haigh dev_err(&interface->dev, "interrupt out endpoint not found\n"); 70603270634SSteven Haigh goto error; 70703270634SSteven Haigh } 70803270634SSteven Haigh 70929cc8897SKuninori Morimoto in_end_size = usb_endpoint_maxp(dev->interrupt_in_endpoint); 71029cc8897SKuninori Morimoto out_end_size = usb_endpoint_maxp(dev->interrupt_out_endpoint); 71103270634SSteven Haigh 71203270634SSteven Haigh dev->read_buffer_primary = kmalloc((4 * in_end_size), GFP_KERNEL); 71303270634SSteven Haigh if (!dev->read_buffer_primary) { 71403270634SSteven Haigh dev_err(&interface->dev, "Couldn't allocate read_buffer_primary\n"); 71503270634SSteven Haigh retval = -ENOMEM; 71603270634SSteven Haigh goto error; 71703270634SSteven Haigh } 71803270634SSteven Haigh 71903270634SSteven Haigh /* debug code prime the buffer */ 72003270634SSteven Haigh memset(dev->read_buffer_primary, 'a', in_end_size); 72103270634SSteven Haigh memset(dev->read_buffer_primary + in_end_size, 'b', in_end_size); 72203270634SSteven Haigh memset(dev->read_buffer_primary + (2 * in_end_size), 'c', in_end_size); 72303270634SSteven Haigh memset(dev->read_buffer_primary + (3 * in_end_size), 'd', in_end_size); 72403270634SSteven Haigh 72503270634SSteven Haigh dev->read_buffer_secondary = kmalloc((4 * in_end_size), GFP_KERNEL); 72603270634SSteven Haigh if (!dev->read_buffer_secondary) { 72703270634SSteven Haigh dev_err(&interface->dev, "Couldn't allocate read_buffer_secondary\n"); 72803270634SSteven Haigh retval = -ENOMEM; 72903270634SSteven Haigh goto error; 73003270634SSteven Haigh } 73103270634SSteven Haigh 73203270634SSteven Haigh /* debug code prime the buffer */ 73303270634SSteven Haigh memset(dev->read_buffer_secondary, 'e', in_end_size); 73403270634SSteven Haigh memset(dev->read_buffer_secondary + in_end_size, 'f', in_end_size); 73503270634SSteven Haigh memset(dev->read_buffer_secondary + (2 * in_end_size), 'g', in_end_size); 73603270634SSteven Haigh memset(dev->read_buffer_secondary + (3 * in_end_size), 'h', in_end_size); 73703270634SSteven Haigh 73803270634SSteven Haigh dev->interrupt_in_buffer = kmalloc(in_end_size, GFP_KERNEL); 73903270634SSteven Haigh if (!dev->interrupt_in_buffer) { 74003270634SSteven Haigh dev_err(&interface->dev, "Couldn't allocate interrupt_in_buffer\n"); 74103270634SSteven Haigh goto error; 74203270634SSteven Haigh } 74303270634SSteven Haigh 74403270634SSteven Haigh /* debug code prime the buffer */ 74503270634SSteven Haigh memset(dev->interrupt_in_buffer, 'i', in_end_size); 74603270634SSteven Haigh 74703270634SSteven Haigh dev->interrupt_in_urb = usb_alloc_urb(0, GFP_KERNEL); 74803270634SSteven Haigh if (!dev->interrupt_in_urb) { 74903270634SSteven Haigh dev_err(&interface->dev, "Couldn't allocate interrupt_in_urb\n"); 75003270634SSteven Haigh goto error; 75103270634SSteven Haigh } 75203270634SSteven Haigh dev->interrupt_out_buffer = kmalloc(out_end_size, GFP_KERNEL); 75303270634SSteven Haigh if (!dev->interrupt_out_buffer) { 75403270634SSteven Haigh dev_err(&interface->dev, "Couldn't allocate interrupt_out_buffer\n"); 75503270634SSteven Haigh goto error; 75603270634SSteven Haigh } 75703270634SSteven Haigh dev->interrupt_out_urb = usb_alloc_urb(0, GFP_KERNEL); 75803270634SSteven Haigh if (!dev->interrupt_out_urb) { 75903270634SSteven Haigh dev_err(&interface->dev, "Couldn't allocate interrupt_out_urb\n"); 76003270634SSteven Haigh goto error; 76103270634SSteven Haigh } 76203270634SSteven Haigh 76303270634SSteven Haigh if (!usb_string(udev, udev->descriptor.iSerialNumber, dev->serial_number, 76403270634SSteven Haigh sizeof(dev->serial_number))) { 76503270634SSteven Haigh dev_err(&interface->dev, "Could not retrieve serial number\n"); 76603270634SSteven Haigh goto error; 76703270634SSteven Haigh } 76866d4bc30SGreg Kroah-Hartman dev_dbg(&interface->dev,"serial_number=%s", dev->serial_number); 76903270634SSteven Haigh 77003270634SSteven Haigh /* we can register the device now, as it is ready */ 77103270634SSteven Haigh usb_set_intfdata(interface, dev); 77203270634SSteven Haigh 77303270634SSteven Haigh retval = usb_register_dev(interface, &adu_class); 77403270634SSteven Haigh 77503270634SSteven Haigh if (retval) { 77603270634SSteven Haigh /* something prevented us from registering this driver */ 77703270634SSteven Haigh dev_err(&interface->dev, "Not able to get a minor for this device.\n"); 77803270634SSteven Haigh usb_set_intfdata(interface, NULL); 77903270634SSteven Haigh goto error; 78003270634SSteven Haigh } 78103270634SSteven Haigh 78203270634SSteven Haigh dev->minor = interface->minor; 78303270634SSteven Haigh 78403270634SSteven Haigh /* let the user know what node this device is now attached to */ 785898eb71cSJoe Perches dev_info(&interface->dev, "ADU%d %s now attached to /dev/usb/adutux%d\n", 78603270634SSteven Haigh udev->descriptor.idProduct, dev->serial_number, 78703270634SSteven Haigh (dev->minor - ADU_MINOR_BASE)); 78803270634SSteven Haigh exit: 78903270634SSteven Haigh return retval; 79003270634SSteven Haigh 79103270634SSteven Haigh error: 79203270634SSteven Haigh adu_delete(dev); 79303270634SSteven Haigh return retval; 79403270634SSteven Haigh } 79503270634SSteven Haigh 79603270634SSteven Haigh /** 79703270634SSteven Haigh * adu_disconnect 79803270634SSteven Haigh * 79903270634SSteven Haigh * Called by the usb core when the device is removed from the system. 80003270634SSteven Haigh */ 80103270634SSteven Haigh static void adu_disconnect(struct usb_interface *interface) 80203270634SSteven Haigh { 80303270634SSteven Haigh struct adu_device *dev; 80403270634SSteven Haigh int minor; 80503270634SSteven Haigh 80603270634SSteven Haigh dev = usb_get_intfdata(interface); 80703270634SSteven Haigh 8088293c568SMatthias Kaehlcke mutex_lock(&dev->mtx); /* not interruptible */ 809f08812d5SPete Zaitcev dev->udev = NULL; /* poison */ 810f08812d5SPete Zaitcev minor = dev->minor; 811f08812d5SPete Zaitcev usb_deregister_dev(interface, &adu_class); 812f08812d5SPete Zaitcev mutex_unlock(&dev->mtx); 813f08812d5SPete Zaitcev 814f08812d5SPete Zaitcev mutex_lock(&adutux_mutex); 815f08812d5SPete Zaitcev usb_set_intfdata(interface, NULL); 816d4ead16fSAlan Stern 81703270634SSteven Haigh /* if the device is not opened, then we clean up right now */ 81866d4bc30SGreg Kroah-Hartman dev_dbg(&dev->udev->dev, "%s : open count %d\n", 81966d4bc30SGreg Kroah-Hartman __func__, dev->open_count); 820f08812d5SPete Zaitcev if (!dev->open_count) 82103270634SSteven Haigh adu_delete(dev); 822f08812d5SPete Zaitcev 823f08812d5SPete Zaitcev mutex_unlock(&adutux_mutex); 82403270634SSteven Haigh 825898eb71cSJoe Perches dev_info(&interface->dev, "ADU device adutux%d now disconnected\n", 82603270634SSteven Haigh (minor - ADU_MINOR_BASE)); 82703270634SSteven Haigh } 82803270634SSteven Haigh 82903270634SSteven Haigh /* usb specific object needed to register this driver with the usb subsystem */ 83003270634SSteven Haigh static struct usb_driver adu_driver = { 83103270634SSteven Haigh .name = "adutux", 83203270634SSteven Haigh .probe = adu_probe, 83303270634SSteven Haigh .disconnect = adu_disconnect, 83403270634SSteven Haigh .id_table = device_table, 83503270634SSteven Haigh }; 83603270634SSteven Haigh 83765db4305SGreg Kroah-Hartman module_usb_driver(adu_driver); 83803270634SSteven Haigh 83903270634SSteven Haigh MODULE_AUTHOR(DRIVER_AUTHOR); 84003270634SSteven Haigh MODULE_DESCRIPTION(DRIVER_DESC); 84103270634SSteven Haigh MODULE_LICENSE("GPL"); 842