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> 2703270634SSteven Haigh #include <asm/uaccess.h> 2803270634SSteven Haigh 2903270634SSteven Haigh #ifdef CONFIG_USB_DEBUG 3003270634SSteven Haigh static int debug = 5; 3103270634SSteven Haigh #else 3203270634SSteven Haigh static int debug = 1; 3303270634SSteven Haigh #endif 3403270634SSteven Haigh 3503270634SSteven Haigh /* Use our own dbg macro */ 3603270634SSteven Haigh #undef dbg 3703270634SSteven Haigh #define dbg(lvl, format, arg...) \ 3803270634SSteven Haigh do { \ 3903270634SSteven Haigh if (debug >= lvl) \ 4003270634SSteven Haigh printk(KERN_DEBUG __FILE__ " : " format " \n", ## arg); \ 4103270634SSteven Haigh } while (0) 4203270634SSteven Haigh 4303270634SSteven Haigh 4403270634SSteven Haigh /* Version Information */ 4503270634SSteven Haigh #define DRIVER_VERSION "v0.0.13" 4603270634SSteven Haigh #define DRIVER_AUTHOR "John Homppi" 4703270634SSteven Haigh #define DRIVER_DESC "adutux (see www.ontrak.net)" 4803270634SSteven Haigh 4903270634SSteven Haigh /* Module parameters */ 5003270634SSteven Haigh module_param(debug, int, S_IRUGO | S_IWUSR); 5103270634SSteven Haigh MODULE_PARM_DESC(debug, "Debug enabled or not"); 5203270634SSteven Haigh 5303270634SSteven Haigh /* Define these values to match your device */ 5403270634SSteven Haigh #define ADU_VENDOR_ID 0x0a07 5503270634SSteven Haigh #define ADU_PRODUCT_ID 0x0064 5603270634SSteven Haigh 5703270634SSteven Haigh /* table of devices that work with this driver */ 5803270634SSteven Haigh static struct usb_device_id device_table [] = { 5903270634SSteven Haigh { USB_DEVICE(ADU_VENDOR_ID, ADU_PRODUCT_ID) }, /* ADU100 */ 6003270634SSteven Haigh { USB_DEVICE(ADU_VENDOR_ID, ADU_PRODUCT_ID+20) }, /* ADU120 */ 6103270634SSteven Haigh { USB_DEVICE(ADU_VENDOR_ID, ADU_PRODUCT_ID+30) }, /* ADU130 */ 6203270634SSteven Haigh { USB_DEVICE(ADU_VENDOR_ID, ADU_PRODUCT_ID+100) }, /* ADU200 */ 6303270634SSteven Haigh { USB_DEVICE(ADU_VENDOR_ID, ADU_PRODUCT_ID+108) }, /* ADU208 */ 6403270634SSteven Haigh { USB_DEVICE(ADU_VENDOR_ID, ADU_PRODUCT_ID+118) }, /* ADU218 */ 6503270634SSteven Haigh { }/* Terminating entry */ 6603270634SSteven Haigh }; 6703270634SSteven Haigh 6803270634SSteven Haigh MODULE_DEVICE_TABLE(usb, device_table); 6903270634SSteven Haigh 7003270634SSteven Haigh #ifdef CONFIG_USB_DYNAMIC_MINORS 7103270634SSteven Haigh #define ADU_MINOR_BASE 0 7203270634SSteven Haigh #else 7303270634SSteven Haigh #define ADU_MINOR_BASE 67 7403270634SSteven Haigh #endif 7503270634SSteven Haigh 7603270634SSteven Haigh /* we can have up to this number of device plugged in at once */ 7703270634SSteven Haigh #define MAX_DEVICES 16 7803270634SSteven Haigh 7903270634SSteven Haigh #define COMMAND_TIMEOUT (2*HZ) /* 60 second timeout for a command */ 8003270634SSteven Haigh 8103270634SSteven Haigh /* Structure to hold all of our device specific stuff */ 8203270634SSteven Haigh struct adu_device { 8303270634SSteven Haigh struct semaphore sem; /* locks this structure */ 8403270634SSteven Haigh struct usb_device* udev; /* save off the usb device pointer */ 8503270634SSteven Haigh struct usb_interface* interface; 8603270634SSteven Haigh unsigned char minor; /* the starting minor number for this device */ 8703270634SSteven Haigh char serial_number[8]; 8803270634SSteven Haigh 8903270634SSteven Haigh int open_count; /* number of times this port has been opened */ 9003270634SSteven Haigh 9103270634SSteven Haigh char* read_buffer_primary; 9203270634SSteven Haigh int read_buffer_length; 9303270634SSteven Haigh char* read_buffer_secondary; 9403270634SSteven Haigh int secondary_head; 9503270634SSteven Haigh int secondary_tail; 9603270634SSteven Haigh spinlock_t buflock; 9703270634SSteven Haigh 9803270634SSteven Haigh wait_queue_head_t read_wait; 9903270634SSteven Haigh wait_queue_head_t write_wait; 10003270634SSteven Haigh 10103270634SSteven Haigh char* interrupt_in_buffer; 10203270634SSteven Haigh struct usb_endpoint_descriptor* interrupt_in_endpoint; 10303270634SSteven Haigh struct urb* interrupt_in_urb; 10403270634SSteven Haigh int read_urb_finished; 10503270634SSteven Haigh 10603270634SSteven Haigh char* interrupt_out_buffer; 10703270634SSteven Haigh struct usb_endpoint_descriptor* interrupt_out_endpoint; 10803270634SSteven Haigh struct urb* interrupt_out_urb; 10903270634SSteven Haigh }; 11003270634SSteven Haigh 11103270634SSteven Haigh /* prevent races between open() and disconnect */ 11203270634SSteven Haigh static DEFINE_MUTEX(disconnect_mutex); 11303270634SSteven Haigh static struct usb_driver adu_driver; 11403270634SSteven Haigh 11503270634SSteven Haigh static void adu_debug_data(int level, const char *function, int size, 11603270634SSteven Haigh const unsigned char *data) 11703270634SSteven Haigh { 11803270634SSteven Haigh int i; 11903270634SSteven Haigh 12003270634SSteven Haigh if (debug < level) 12103270634SSteven Haigh return; 12203270634SSteven Haigh 12303270634SSteven Haigh printk(KERN_DEBUG __FILE__": %s - length = %d, data = ", 12403270634SSteven Haigh function, size); 12503270634SSteven Haigh for (i = 0; i < size; ++i) 12603270634SSteven Haigh printk("%.2x ", data[i]); 12703270634SSteven Haigh printk("\n"); 12803270634SSteven Haigh } 12903270634SSteven Haigh 13003270634SSteven Haigh /** 13103270634SSteven Haigh * adu_abort_transfers 13203270634SSteven Haigh * aborts transfers and frees associated data structures 13303270634SSteven Haigh */ 13403270634SSteven Haigh static void adu_abort_transfers(struct adu_device *dev) 13503270634SSteven Haigh { 13603270634SSteven Haigh dbg(2," %s : enter", __FUNCTION__); 13703270634SSteven Haigh 13803270634SSteven Haigh if (dev == NULL) { 13903270634SSteven Haigh dbg(1," %s : dev is null", __FUNCTION__); 14003270634SSteven Haigh goto exit; 14103270634SSteven Haigh } 14203270634SSteven Haigh 14303270634SSteven Haigh if (dev->udev == NULL) { 14403270634SSteven Haigh dbg(1," %s : udev is null", __FUNCTION__); 14503270634SSteven Haigh goto exit; 14603270634SSteven Haigh } 14703270634SSteven Haigh 14803270634SSteven Haigh dbg(2," %s : udev state %d", __FUNCTION__, dev->udev->state); 14903270634SSteven Haigh if (dev->udev->state == USB_STATE_NOTATTACHED) { 15003270634SSteven Haigh dbg(1," %s : udev is not attached", __FUNCTION__); 15103270634SSteven Haigh goto exit; 15203270634SSteven Haigh } 15303270634SSteven Haigh 15403270634SSteven Haigh /* shutdown transfer */ 15503270634SSteven Haigh usb_unlink_urb(dev->interrupt_in_urb); 15603270634SSteven Haigh usb_unlink_urb(dev->interrupt_out_urb); 15703270634SSteven Haigh 15803270634SSteven Haigh exit: 15903270634SSteven Haigh dbg(2," %s : leave", __FUNCTION__); 16003270634SSteven Haigh } 16103270634SSteven Haigh 16203270634SSteven Haigh static void adu_delete(struct adu_device *dev) 16303270634SSteven Haigh { 16403270634SSteven Haigh dbg(2, "%s enter", __FUNCTION__); 16503270634SSteven Haigh 16603270634SSteven Haigh adu_abort_transfers(dev); 16703270634SSteven Haigh 16803270634SSteven Haigh /* free data structures */ 16903270634SSteven Haigh usb_free_urb(dev->interrupt_in_urb); 17003270634SSteven Haigh usb_free_urb(dev->interrupt_out_urb); 17103270634SSteven Haigh kfree(dev->read_buffer_primary); 17203270634SSteven Haigh kfree(dev->read_buffer_secondary); 17303270634SSteven Haigh kfree(dev->interrupt_in_buffer); 17403270634SSteven Haigh kfree(dev->interrupt_out_buffer); 17503270634SSteven Haigh kfree(dev); 17603270634SSteven Haigh 17703270634SSteven Haigh dbg(2, "%s : leave", __FUNCTION__); 17803270634SSteven Haigh } 17903270634SSteven Haigh 1807d12e780SDavid Howells static void adu_interrupt_in_callback(struct urb *urb) 18103270634SSteven Haigh { 18203270634SSteven Haigh struct adu_device *dev = urb->context; 18303270634SSteven Haigh 18403270634SSteven Haigh dbg(4," %s : enter, status %d", __FUNCTION__, urb->status); 18503270634SSteven Haigh adu_debug_data(5, __FUNCTION__, urb->actual_length, 18603270634SSteven Haigh urb->transfer_buffer); 18703270634SSteven Haigh 18803270634SSteven Haigh spin_lock(&dev->buflock); 18903270634SSteven Haigh 19003270634SSteven Haigh if (urb->status != 0) { 19103270634SSteven Haigh if ((urb->status != -ENOENT) && (urb->status != -ECONNRESET)) { 19203270634SSteven Haigh dbg(1," %s : nonzero status received: %d", 19303270634SSteven Haigh __FUNCTION__, urb->status); 19403270634SSteven Haigh } 19503270634SSteven Haigh goto exit; 19603270634SSteven Haigh } 19703270634SSteven Haigh 19803270634SSteven Haigh if (urb->actual_length > 0 && dev->interrupt_in_buffer[0] != 0x00) { 19903270634SSteven Haigh if (dev->read_buffer_length < 20003270634SSteven Haigh (4 * le16_to_cpu(dev->interrupt_in_endpoint->wMaxPacketSize)) - 20103270634SSteven Haigh (urb->actual_length)) { 20203270634SSteven Haigh memcpy (dev->read_buffer_primary + 20303270634SSteven Haigh dev->read_buffer_length, 20403270634SSteven Haigh dev->interrupt_in_buffer, urb->actual_length); 20503270634SSteven Haigh 20603270634SSteven Haigh dev->read_buffer_length += urb->actual_length; 20703270634SSteven Haigh dbg(2," %s reading %d ", __FUNCTION__, 20803270634SSteven Haigh urb->actual_length); 20903270634SSteven Haigh } else { 21003270634SSteven Haigh dbg(1," %s : read_buffer overflow", __FUNCTION__); 21103270634SSteven Haigh } 21203270634SSteven Haigh } 21303270634SSteven Haigh 21403270634SSteven Haigh exit: 21503270634SSteven Haigh dev->read_urb_finished = 1; 21603270634SSteven Haigh spin_unlock(&dev->buflock); 21703270634SSteven Haigh /* always wake up so we recover from errors */ 21803270634SSteven Haigh wake_up_interruptible(&dev->read_wait); 21903270634SSteven Haigh adu_debug_data(5, __FUNCTION__, urb->actual_length, 22003270634SSteven Haigh urb->transfer_buffer); 22103270634SSteven Haigh dbg(4," %s : leave, status %d", __FUNCTION__, urb->status); 22203270634SSteven Haigh } 22303270634SSteven Haigh 2247d12e780SDavid Howells static void adu_interrupt_out_callback(struct urb *urb) 22503270634SSteven Haigh { 22603270634SSteven Haigh struct adu_device *dev = urb->context; 22703270634SSteven Haigh 22803270634SSteven Haigh dbg(4," %s : enter, status %d", __FUNCTION__, urb->status); 22903270634SSteven Haigh adu_debug_data(5,__FUNCTION__, urb->actual_length, urb->transfer_buffer); 23003270634SSteven Haigh 23103270634SSteven Haigh if (urb->status != 0) { 23203270634SSteven Haigh if ((urb->status != -ENOENT) && 23303270634SSteven Haigh (urb->status != -ECONNRESET)) { 23403270634SSteven Haigh dbg(1, " %s :nonzero status received: %d", 23503270634SSteven Haigh __FUNCTION__, urb->status); 23603270634SSteven Haigh } 23703270634SSteven Haigh goto exit; 23803270634SSteven Haigh } 23903270634SSteven Haigh 24003270634SSteven Haigh wake_up_interruptible(&dev->write_wait); 24103270634SSteven Haigh exit: 24203270634SSteven Haigh 24303270634SSteven Haigh adu_debug_data(5, __FUNCTION__, urb->actual_length, 24403270634SSteven Haigh urb->transfer_buffer); 24503270634SSteven Haigh dbg(4," %s : leave, status %d", __FUNCTION__, urb->status); 24603270634SSteven Haigh } 24703270634SSteven Haigh 24803270634SSteven Haigh static int adu_open(struct inode *inode, struct file *file) 24903270634SSteven Haigh { 25003270634SSteven Haigh struct adu_device *dev = NULL; 25103270634SSteven Haigh struct usb_interface *interface; 25203270634SSteven Haigh int subminor; 25303270634SSteven Haigh int retval = 0; 25403270634SSteven Haigh 25503270634SSteven Haigh dbg(2,"%s : enter", __FUNCTION__); 25603270634SSteven Haigh 25703270634SSteven Haigh subminor = iminor(inode); 25803270634SSteven Haigh 25903270634SSteven Haigh mutex_lock(&disconnect_mutex); 26003270634SSteven Haigh 26103270634SSteven Haigh interface = usb_find_interface(&adu_driver, subminor); 26203270634SSteven Haigh if (!interface) { 26303270634SSteven Haigh err("%s - error, can't find device for minor %d", 26403270634SSteven Haigh __FUNCTION__, subminor); 26503270634SSteven Haigh retval = -ENODEV; 26603270634SSteven Haigh goto exit_no_device; 26703270634SSteven Haigh } 26803270634SSteven Haigh 26903270634SSteven Haigh dev = usb_get_intfdata(interface); 27003270634SSteven Haigh if (!dev) { 27103270634SSteven Haigh retval = -ENODEV; 27203270634SSteven Haigh goto exit_no_device; 27303270634SSteven Haigh } 27403270634SSteven Haigh 27503270634SSteven Haigh /* lock this device */ 27603270634SSteven Haigh if ((retval = down_interruptible(&dev->sem))) { 27703270634SSteven Haigh dbg(2, "%s : sem down failed", __FUNCTION__); 27803270634SSteven Haigh goto exit_no_device; 27903270634SSteven Haigh } 28003270634SSteven Haigh 28103270634SSteven Haigh /* increment our usage count for the device */ 28203270634SSteven Haigh ++dev->open_count; 28303270634SSteven Haigh dbg(2,"%s : open count %d", __FUNCTION__, dev->open_count); 28403270634SSteven Haigh 28503270634SSteven Haigh /* save device in the file's private structure */ 28603270634SSteven Haigh file->private_data = dev; 28703270634SSteven Haigh 288ebc3ac14SOliver Neukum if (dev->open_count == 1) { 28903270634SSteven Haigh /* initialize in direction */ 29003270634SSteven Haigh dev->read_buffer_length = 0; 29103270634SSteven Haigh 29203270634SSteven Haigh /* fixup first read by having urb waiting for it */ 29303270634SSteven Haigh usb_fill_int_urb(dev->interrupt_in_urb,dev->udev, 29403270634SSteven Haigh usb_rcvintpipe(dev->udev, 29503270634SSteven Haigh dev->interrupt_in_endpoint->bEndpointAddress), 29603270634SSteven Haigh dev->interrupt_in_buffer, 29703270634SSteven Haigh le16_to_cpu(dev->interrupt_in_endpoint->wMaxPacketSize), 29803270634SSteven Haigh adu_interrupt_in_callback, dev, 29903270634SSteven Haigh dev->interrupt_in_endpoint->bInterval); 30003270634SSteven Haigh /* dev->interrupt_in_urb->transfer_flags |= URB_ASYNC_UNLINK; */ 30103270634SSteven Haigh dev->read_urb_finished = 0; 302ebc3ac14SOliver Neukum retval = usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL); 303ebc3ac14SOliver Neukum if (retval) 304ebc3ac14SOliver Neukum --dev->open_count; 305ebc3ac14SOliver Neukum } 30603270634SSteven Haigh up(&dev->sem); 30703270634SSteven Haigh 30803270634SSteven Haigh exit_no_device: 30903270634SSteven Haigh mutex_unlock(&disconnect_mutex); 31003270634SSteven Haigh dbg(2,"%s : leave, return value %d ", __FUNCTION__, retval); 31103270634SSteven Haigh 31203270634SSteven Haigh return retval; 31303270634SSteven Haigh } 31403270634SSteven Haigh 31503270634SSteven Haigh static int adu_release_internal(struct adu_device *dev) 31603270634SSteven Haigh { 31703270634SSteven Haigh int retval = 0; 31803270634SSteven Haigh 31903270634SSteven Haigh dbg(2," %s : enter", __FUNCTION__); 32003270634SSteven Haigh 32103270634SSteven Haigh if (dev->udev == NULL) { 32203270634SSteven Haigh /* the device was unplugged before the file was released */ 32303270634SSteven Haigh adu_delete(dev); 32403270634SSteven Haigh goto exit; 32503270634SSteven Haigh } 32603270634SSteven Haigh 32703270634SSteven Haigh /* decrement our usage count for the device */ 32803270634SSteven Haigh --dev->open_count; 32903270634SSteven Haigh dbg(2," %s : open count %d", __FUNCTION__, dev->open_count); 33003270634SSteven Haigh if (dev->open_count <= 0) { 33103270634SSteven Haigh adu_abort_transfers(dev); 33203270634SSteven Haigh dev->open_count = 0; 33303270634SSteven Haigh } 33403270634SSteven Haigh 33503270634SSteven Haigh exit: 33603270634SSteven Haigh dbg(2," %s : leave", __FUNCTION__); 33703270634SSteven Haigh return retval; 33803270634SSteven Haigh } 33903270634SSteven Haigh 34003270634SSteven Haigh static int adu_release(struct inode *inode, struct file *file) 34103270634SSteven Haigh { 34203270634SSteven Haigh struct adu_device *dev = NULL; 34303270634SSteven Haigh int retval = 0; 34403270634SSteven Haigh 34503270634SSteven Haigh dbg(2," %s : enter", __FUNCTION__); 34603270634SSteven Haigh 34703270634SSteven Haigh if (file == NULL) { 34803270634SSteven Haigh dbg(1," %s : file is NULL", __FUNCTION__); 34903270634SSteven Haigh retval = -ENODEV; 35003270634SSteven Haigh goto exit; 35103270634SSteven Haigh } 35203270634SSteven Haigh 35303270634SSteven Haigh dev = file->private_data; 35403270634SSteven Haigh 35503270634SSteven Haigh if (dev == NULL) { 35603270634SSteven Haigh dbg(1," %s : object is NULL", __FUNCTION__); 35703270634SSteven Haigh retval = -ENODEV; 35803270634SSteven Haigh goto exit; 35903270634SSteven Haigh } 36003270634SSteven Haigh 36103270634SSteven Haigh /* lock our device */ 36203270634SSteven Haigh down(&dev->sem); /* not interruptible */ 36303270634SSteven Haigh 36403270634SSteven Haigh if (dev->open_count <= 0) { 36503270634SSteven Haigh dbg(1," %s : device not opened", __FUNCTION__); 36603270634SSteven Haigh retval = -ENODEV; 36703270634SSteven Haigh goto exit; 36803270634SSteven Haigh } 36903270634SSteven Haigh 37003270634SSteven Haigh /* do the work */ 37103270634SSteven Haigh retval = adu_release_internal(dev); 37203270634SSteven Haigh 37303270634SSteven Haigh exit: 374a65dc301SEric Sesterhenn if (dev) 37503270634SSteven Haigh up(&dev->sem); 37603270634SSteven Haigh dbg(2," %s : leave, return value %d", __FUNCTION__, retval); 37703270634SSteven Haigh return retval; 37803270634SSteven Haigh } 37903270634SSteven Haigh 38003270634SSteven Haigh static ssize_t adu_read(struct file *file, __user char *buffer, size_t count, 38103270634SSteven Haigh loff_t *ppos) 38203270634SSteven Haigh { 38303270634SSteven Haigh struct adu_device *dev; 38403270634SSteven Haigh size_t bytes_read = 0; 38503270634SSteven Haigh size_t bytes_to_read = count; 38603270634SSteven Haigh int i; 38703270634SSteven Haigh int retval = 0; 38803270634SSteven Haigh int timeout = 0; 38903270634SSteven Haigh int should_submit = 0; 39003270634SSteven Haigh unsigned long flags; 39103270634SSteven Haigh DECLARE_WAITQUEUE(wait, current); 39203270634SSteven Haigh 39303270634SSteven Haigh dbg(2," %s : enter, count = %Zd, file=%p", __FUNCTION__, count, file); 39403270634SSteven Haigh 39503270634SSteven Haigh dev = file->private_data; 39603270634SSteven Haigh dbg(2," %s : dev=%p", __FUNCTION__, dev); 39703270634SSteven Haigh /* lock this object */ 39803270634SSteven Haigh if (down_interruptible(&dev->sem)) 39903270634SSteven Haigh return -ERESTARTSYS; 40003270634SSteven Haigh 40103270634SSteven Haigh /* verify that the device wasn't unplugged */ 40203270634SSteven Haigh if (dev->udev == NULL || dev->minor == 0) { 40303270634SSteven Haigh retval = -ENODEV; 40403270634SSteven Haigh err("No device or device unplugged %d", retval); 40503270634SSteven Haigh goto exit; 40603270634SSteven Haigh } 40703270634SSteven Haigh 40803270634SSteven Haigh /* verify that some data was requested */ 40903270634SSteven Haigh if (count == 0) { 41003270634SSteven Haigh dbg(1," %s : read request of 0 bytes", __FUNCTION__); 41103270634SSteven Haigh goto exit; 41203270634SSteven Haigh } 41303270634SSteven Haigh 41403270634SSteven Haigh timeout = COMMAND_TIMEOUT; 41503270634SSteven Haigh dbg(2," %s : about to start looping", __FUNCTION__); 41603270634SSteven Haigh while (bytes_to_read) { 41703270634SSteven Haigh int data_in_secondary = dev->secondary_tail - dev->secondary_head; 41803270634SSteven Haigh dbg(2," %s : while, data_in_secondary=%d, status=%d", 41903270634SSteven Haigh __FUNCTION__, data_in_secondary, 42003270634SSteven Haigh dev->interrupt_in_urb->status); 42103270634SSteven Haigh 42203270634SSteven Haigh if (data_in_secondary) { 42303270634SSteven Haigh /* drain secondary buffer */ 42403270634SSteven Haigh int amount = bytes_to_read < data_in_secondary ? bytes_to_read : data_in_secondary; 42503270634SSteven Haigh i = copy_to_user(buffer, dev->read_buffer_secondary+dev->secondary_head, amount); 42603270634SSteven Haigh if (i < 0) { 42703270634SSteven Haigh retval = -EFAULT; 42803270634SSteven Haigh goto exit; 42903270634SSteven Haigh } 43003270634SSteven Haigh dev->secondary_head += (amount - i); 43103270634SSteven Haigh bytes_read += (amount - i); 43203270634SSteven Haigh bytes_to_read -= (amount - i); 43303270634SSteven Haigh if (i) { 43403270634SSteven Haigh retval = bytes_read ? bytes_read : -EFAULT; 43503270634SSteven Haigh goto exit; 43603270634SSteven Haigh } 43703270634SSteven Haigh } else { 43803270634SSteven Haigh /* we check the primary buffer */ 43903270634SSteven Haigh spin_lock_irqsave (&dev->buflock, flags); 44003270634SSteven Haigh if (dev->read_buffer_length) { 44103270634SSteven Haigh /* we secure access to the primary */ 44203270634SSteven Haigh char *tmp; 44303270634SSteven Haigh dbg(2," %s : swap, read_buffer_length = %d", 44403270634SSteven Haigh __FUNCTION__, dev->read_buffer_length); 44503270634SSteven Haigh tmp = dev->read_buffer_secondary; 44603270634SSteven Haigh dev->read_buffer_secondary = dev->read_buffer_primary; 44703270634SSteven Haigh dev->read_buffer_primary = tmp; 44803270634SSteven Haigh dev->secondary_head = 0; 44903270634SSteven Haigh dev->secondary_tail = dev->read_buffer_length; 45003270634SSteven Haigh dev->read_buffer_length = 0; 45103270634SSteven Haigh spin_unlock_irqrestore(&dev->buflock, flags); 45203270634SSteven Haigh /* we have a free buffer so use it */ 45303270634SSteven Haigh should_submit = 1; 45403270634SSteven Haigh } else { 45503270634SSteven Haigh /* even the primary was empty - we may need to do IO */ 45603270634SSteven Haigh if (dev->interrupt_in_urb->status == -EINPROGRESS) { 45703270634SSteven Haigh /* somebody is doing IO */ 45803270634SSteven Haigh spin_unlock_irqrestore(&dev->buflock, flags); 45903270634SSteven Haigh dbg(2," %s : submitted already", __FUNCTION__); 46003270634SSteven Haigh } else { 46103270634SSteven Haigh /* we must initiate input */ 46203270634SSteven Haigh dbg(2," %s : initiate input", __FUNCTION__); 46303270634SSteven Haigh dev->read_urb_finished = 0; 46403270634SSteven Haigh 46503270634SSteven Haigh usb_fill_int_urb(dev->interrupt_in_urb,dev->udev, 46603270634SSteven Haigh usb_rcvintpipe(dev->udev, 46703270634SSteven Haigh dev->interrupt_in_endpoint->bEndpointAddress), 46803270634SSteven Haigh dev->interrupt_in_buffer, 46903270634SSteven Haigh le16_to_cpu(dev->interrupt_in_endpoint->wMaxPacketSize), 47003270634SSteven Haigh adu_interrupt_in_callback, 47103270634SSteven Haigh dev, 47203270634SSteven Haigh dev->interrupt_in_endpoint->bInterval); 473ebc3ac14SOliver Neukum retval = usb_submit_urb(dev->interrupt_in_urb, GFP_ATOMIC); 47403270634SSteven Haigh if (!retval) { 47503270634SSteven Haigh spin_unlock_irqrestore(&dev->buflock, flags); 47603270634SSteven Haigh dbg(2," %s : submitted OK", __FUNCTION__); 47703270634SSteven Haigh } else { 47803270634SSteven Haigh if (retval == -ENOMEM) { 47903270634SSteven Haigh retval = bytes_read ? bytes_read : -ENOMEM; 48003270634SSteven Haigh } 48103270634SSteven Haigh spin_unlock_irqrestore(&dev->buflock, flags); 48203270634SSteven Haigh dbg(2," %s : submit failed", __FUNCTION__); 48303270634SSteven Haigh goto exit; 48403270634SSteven Haigh } 48503270634SSteven Haigh } 48603270634SSteven Haigh 48703270634SSteven Haigh /* we wait for I/O to complete */ 48803270634SSteven Haigh set_current_state(TASK_INTERRUPTIBLE); 48903270634SSteven Haigh add_wait_queue(&dev->read_wait, &wait); 49003270634SSteven Haigh if (!dev->read_urb_finished) 49103270634SSteven Haigh timeout = schedule_timeout(COMMAND_TIMEOUT); 49203270634SSteven Haigh else 49303270634SSteven Haigh set_current_state(TASK_RUNNING); 49403270634SSteven Haigh remove_wait_queue(&dev->read_wait, &wait); 49503270634SSteven Haigh 49603270634SSteven Haigh if (timeout <= 0) { 49703270634SSteven Haigh dbg(2," %s : timeout", __FUNCTION__); 49803270634SSteven Haigh retval = bytes_read ? bytes_read : -ETIMEDOUT; 49903270634SSteven Haigh goto exit; 50003270634SSteven Haigh } 50103270634SSteven Haigh 50203270634SSteven Haigh if (signal_pending(current)) { 50303270634SSteven Haigh dbg(2," %s : signal pending", __FUNCTION__); 50403270634SSteven Haigh retval = bytes_read ? bytes_read : -EINTR; 50503270634SSteven Haigh goto exit; 50603270634SSteven Haigh } 50703270634SSteven Haigh } 50803270634SSteven Haigh } 50903270634SSteven Haigh } 51003270634SSteven Haigh 51103270634SSteven Haigh retval = bytes_read; 51203270634SSteven Haigh /* if the primary buffer is empty then use it */ 51303270634SSteven Haigh if (should_submit && !dev->interrupt_in_urb->status==-EINPROGRESS) { 51403270634SSteven Haigh usb_fill_int_urb(dev->interrupt_in_urb,dev->udev, 51503270634SSteven Haigh usb_rcvintpipe(dev->udev, 51603270634SSteven Haigh dev->interrupt_in_endpoint->bEndpointAddress), 51703270634SSteven Haigh dev->interrupt_in_buffer, 51803270634SSteven Haigh le16_to_cpu(dev->interrupt_in_endpoint->wMaxPacketSize), 51903270634SSteven Haigh adu_interrupt_in_callback, 52003270634SSteven Haigh dev, 52103270634SSteven Haigh dev->interrupt_in_endpoint->bInterval); 52203270634SSteven Haigh /* dev->interrupt_in_urb->transfer_flags |= URB_ASYNC_UNLINK; */ 52303270634SSteven Haigh dev->read_urb_finished = 0; 52403270634SSteven Haigh usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL); 52503270634SSteven Haigh /* we ignore failure */ 52603270634SSteven Haigh } 52703270634SSteven Haigh 52803270634SSteven Haigh exit: 52903270634SSteven Haigh /* unlock the device */ 53003270634SSteven Haigh up(&dev->sem); 53103270634SSteven Haigh 53203270634SSteven Haigh dbg(2," %s : leave, return value %d", __FUNCTION__, retval); 53303270634SSteven Haigh return retval; 53403270634SSteven Haigh } 53503270634SSteven Haigh 53603270634SSteven Haigh static ssize_t adu_write(struct file *file, const __user char *buffer, 53703270634SSteven Haigh size_t count, loff_t *ppos) 53803270634SSteven Haigh { 53903270634SSteven Haigh struct adu_device *dev; 54003270634SSteven Haigh size_t bytes_written = 0; 54103270634SSteven Haigh size_t bytes_to_write; 54203270634SSteven Haigh size_t buffer_size; 543ebc3ac14SOliver Neukum int retval; 54403270634SSteven Haigh int timeout = 0; 54503270634SSteven Haigh 54603270634SSteven Haigh dbg(2," %s : enter, count = %Zd", __FUNCTION__, count); 54703270634SSteven Haigh 54803270634SSteven Haigh dev = file->private_data; 54903270634SSteven Haigh 55003270634SSteven Haigh /* lock this object */ 551ebc3ac14SOliver Neukum retval = down_interruptible(&dev->sem); 552ebc3ac14SOliver Neukum if (retval) 553ebc3ac14SOliver Neukum goto exit_nolock; 55403270634SSteven Haigh 55503270634SSteven Haigh /* verify that the device wasn't unplugged */ 55603270634SSteven Haigh if (dev->udev == NULL || dev->minor == 0) { 55703270634SSteven Haigh retval = -ENODEV; 55803270634SSteven Haigh err("No device or device unplugged %d", retval); 55903270634SSteven Haigh goto exit; 56003270634SSteven Haigh } 56103270634SSteven Haigh 56203270634SSteven Haigh /* verify that we actually have some data to write */ 56303270634SSteven Haigh if (count == 0) { 56403270634SSteven Haigh dbg(1," %s : write request of 0 bytes", __FUNCTION__); 56503270634SSteven Haigh goto exit; 56603270634SSteven Haigh } 56703270634SSteven Haigh 56803270634SSteven Haigh 56903270634SSteven Haigh while (count > 0) { 57003270634SSteven Haigh if (dev->interrupt_out_urb->status == -EINPROGRESS) { 57103270634SSteven Haigh timeout = COMMAND_TIMEOUT; 57203270634SSteven Haigh 57303270634SSteven Haigh while (timeout > 0) { 57403270634SSteven Haigh if (signal_pending(current)) { 57503270634SSteven Haigh dbg(1," %s : interrupted", __FUNCTION__); 57603270634SSteven Haigh retval = -EINTR; 57703270634SSteven Haigh goto exit; 57803270634SSteven Haigh } 57903270634SSteven Haigh up(&dev->sem); 58003270634SSteven Haigh timeout = interruptible_sleep_on_timeout(&dev->write_wait, timeout); 581ebc3ac14SOliver Neukum retval = down_interruptible(&dev->sem); 582ebc3ac14SOliver Neukum if (retval) { 583ebc3ac14SOliver Neukum retval = bytes_written ? bytes_written : retval; 584ebc3ac14SOliver Neukum goto exit_nolock; 585ebc3ac14SOliver Neukum } 58603270634SSteven Haigh if (timeout > 0) { 58703270634SSteven Haigh break; 58803270634SSteven Haigh } 58903270634SSteven Haigh dbg(1," %s : interrupted timeout: %d", __FUNCTION__, timeout); 59003270634SSteven Haigh } 59103270634SSteven Haigh 59203270634SSteven Haigh 59303270634SSteven Haigh dbg(1," %s : final timeout: %d", __FUNCTION__, timeout); 59403270634SSteven Haigh 59503270634SSteven Haigh if (timeout == 0) { 59603270634SSteven Haigh dbg(1, "%s - command timed out.", __FUNCTION__); 59703270634SSteven Haigh retval = -ETIMEDOUT; 59803270634SSteven Haigh goto exit; 59903270634SSteven Haigh } 60003270634SSteven Haigh 60103270634SSteven Haigh dbg(4," %s : in progress, count = %Zd", __FUNCTION__, count); 60203270634SSteven Haigh 60303270634SSteven Haigh } else { 60403270634SSteven Haigh dbg(4," %s : sending, count = %Zd", __FUNCTION__, count); 60503270634SSteven Haigh 60603270634SSteven Haigh /* write the data into interrupt_out_buffer from userspace */ 60703270634SSteven Haigh buffer_size = le16_to_cpu(dev->interrupt_out_endpoint->wMaxPacketSize); 60803270634SSteven Haigh bytes_to_write = count > buffer_size ? buffer_size : count; 60903270634SSteven Haigh dbg(4," %s : buffer_size = %Zd, count = %Zd, bytes_to_write = %Zd", 61003270634SSteven Haigh __FUNCTION__, buffer_size, count, bytes_to_write); 61103270634SSteven Haigh 61203270634SSteven Haigh if (copy_from_user(dev->interrupt_out_buffer, buffer, bytes_to_write) != 0) { 61303270634SSteven Haigh retval = -EFAULT; 61403270634SSteven Haigh goto exit; 61503270634SSteven Haigh } 61603270634SSteven Haigh 61703270634SSteven Haigh /* send off the urb */ 61803270634SSteven Haigh usb_fill_int_urb( 61903270634SSteven Haigh dev->interrupt_out_urb, 62003270634SSteven Haigh dev->udev, 62103270634SSteven Haigh usb_sndintpipe(dev->udev, dev->interrupt_out_endpoint->bEndpointAddress), 62203270634SSteven Haigh dev->interrupt_out_buffer, 62303270634SSteven Haigh bytes_to_write, 62403270634SSteven Haigh adu_interrupt_out_callback, 62503270634SSteven Haigh dev, 62603270634SSteven Haigh dev->interrupt_in_endpoint->bInterval); 62703270634SSteven Haigh /* dev->interrupt_in_urb->transfer_flags |= URB_ASYNC_UNLINK; */ 62803270634SSteven Haigh dev->interrupt_out_urb->actual_length = bytes_to_write; 62903270634SSteven Haigh retval = usb_submit_urb(dev->interrupt_out_urb, GFP_KERNEL); 63003270634SSteven Haigh if (retval < 0) { 63103270634SSteven Haigh err("Couldn't submit interrupt_out_urb %d", retval); 63203270634SSteven Haigh goto exit; 63303270634SSteven Haigh } 63403270634SSteven Haigh 63503270634SSteven Haigh buffer += bytes_to_write; 63603270634SSteven Haigh count -= bytes_to_write; 63703270634SSteven Haigh 63803270634SSteven Haigh bytes_written += bytes_to_write; 63903270634SSteven Haigh } 64003270634SSteven Haigh } 64103270634SSteven Haigh 64203270634SSteven Haigh retval = bytes_written; 64303270634SSteven Haigh 64403270634SSteven Haigh exit: 64503270634SSteven Haigh /* unlock the device */ 64603270634SSteven Haigh up(&dev->sem); 647ebc3ac14SOliver Neukum exit_nolock: 64803270634SSteven Haigh 64903270634SSteven Haigh dbg(2," %s : leave, return value %d", __FUNCTION__, retval); 65003270634SSteven Haigh 65103270634SSteven Haigh return retval; 65203270634SSteven Haigh } 65303270634SSteven Haigh 65403270634SSteven Haigh /* file operations needed when we register this driver */ 65500977a59SArjan van de Ven static const struct file_operations adu_fops = { 65603270634SSteven Haigh .owner = THIS_MODULE, 65703270634SSteven Haigh .read = adu_read, 65803270634SSteven Haigh .write = adu_write, 65903270634SSteven Haigh .open = adu_open, 66003270634SSteven Haigh .release = adu_release, 66103270634SSteven Haigh }; 66203270634SSteven Haigh 66303270634SSteven Haigh /* 66403270634SSteven Haigh * usb class driver info in order to get a minor number from the usb core, 66503270634SSteven Haigh * and to have the device registered with devfs and the driver core 66603270634SSteven Haigh */ 66703270634SSteven Haigh static struct usb_class_driver adu_class = { 66803270634SSteven Haigh .name = "usb/adutux%d", 66903270634SSteven Haigh .fops = &adu_fops, 67003270634SSteven Haigh .minor_base = ADU_MINOR_BASE, 67103270634SSteven Haigh }; 67203270634SSteven Haigh 67303270634SSteven Haigh /** 67403270634SSteven Haigh * adu_probe 67503270634SSteven Haigh * 67603270634SSteven Haigh * Called by the usb core when a new device is connected that it thinks 67703270634SSteven Haigh * this driver might be interested in. 67803270634SSteven Haigh */ 67903270634SSteven Haigh static int adu_probe(struct usb_interface *interface, 68003270634SSteven Haigh const struct usb_device_id *id) 68103270634SSteven Haigh { 68203270634SSteven Haigh struct usb_device *udev = interface_to_usbdev(interface); 68303270634SSteven Haigh struct adu_device *dev = NULL; 68403270634SSteven Haigh struct usb_host_interface *iface_desc; 68503270634SSteven Haigh struct usb_endpoint_descriptor *endpoint; 68603270634SSteven Haigh int retval = -ENODEV; 68703270634SSteven Haigh int in_end_size; 68803270634SSteven Haigh int out_end_size; 68903270634SSteven Haigh int i; 69003270634SSteven Haigh 69103270634SSteven Haigh dbg(2," %s : enter", __FUNCTION__); 69203270634SSteven Haigh 69303270634SSteven Haigh if (udev == NULL) { 69403270634SSteven Haigh dev_err(&interface->dev, "udev is NULL.\n"); 69503270634SSteven Haigh goto exit; 69603270634SSteven Haigh } 69703270634SSteven Haigh 69803270634SSteven Haigh /* allocate memory for our device state and intialize it */ 69903270634SSteven Haigh dev = kzalloc(sizeof(struct adu_device), GFP_KERNEL); 70003270634SSteven Haigh if (dev == NULL) { 70103270634SSteven Haigh dev_err(&interface->dev, "Out of memory\n"); 70203270634SSteven Haigh retval = -ENOMEM; 70303270634SSteven Haigh goto exit; 70403270634SSteven Haigh } 70503270634SSteven Haigh 70603270634SSteven Haigh init_MUTEX(&dev->sem); 70703270634SSteven Haigh spin_lock_init(&dev->buflock); 70803270634SSteven Haigh dev->udev = udev; 70903270634SSteven Haigh init_waitqueue_head(&dev->read_wait); 71003270634SSteven Haigh init_waitqueue_head(&dev->write_wait); 71103270634SSteven Haigh 71203270634SSteven Haigh iface_desc = &interface->altsetting[0]; 71303270634SSteven Haigh 71403270634SSteven Haigh /* set up the endpoint information */ 71503270634SSteven Haigh for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) { 71603270634SSteven Haigh endpoint = &iface_desc->endpoint[i].desc; 71703270634SSteven Haigh 71803270634SSteven Haigh if (usb_endpoint_is_int_in(endpoint)) 71903270634SSteven Haigh dev->interrupt_in_endpoint = endpoint; 72003270634SSteven Haigh 72103270634SSteven Haigh if (usb_endpoint_is_int_out(endpoint)) 72203270634SSteven Haigh dev->interrupt_out_endpoint = endpoint; 72303270634SSteven Haigh } 72403270634SSteven Haigh if (dev->interrupt_in_endpoint == NULL) { 72503270634SSteven Haigh dev_err(&interface->dev, "interrupt in endpoint not found\n"); 72603270634SSteven Haigh goto error; 72703270634SSteven Haigh } 72803270634SSteven Haigh if (dev->interrupt_out_endpoint == NULL) { 72903270634SSteven Haigh dev_err(&interface->dev, "interrupt out endpoint not found\n"); 73003270634SSteven Haigh goto error; 73103270634SSteven Haigh } 73203270634SSteven Haigh 73303270634SSteven Haigh in_end_size = le16_to_cpu(dev->interrupt_in_endpoint->wMaxPacketSize); 73403270634SSteven Haigh out_end_size = le16_to_cpu(dev->interrupt_out_endpoint->wMaxPacketSize); 73503270634SSteven Haigh 73603270634SSteven Haigh dev->read_buffer_primary = kmalloc((4 * in_end_size), GFP_KERNEL); 73703270634SSteven Haigh if (!dev->read_buffer_primary) { 73803270634SSteven Haigh dev_err(&interface->dev, "Couldn't allocate read_buffer_primary\n"); 73903270634SSteven Haigh retval = -ENOMEM; 74003270634SSteven Haigh goto error; 74103270634SSteven Haigh } 74203270634SSteven Haigh 74303270634SSteven Haigh /* debug code prime the buffer */ 74403270634SSteven Haigh memset(dev->read_buffer_primary, 'a', in_end_size); 74503270634SSteven Haigh memset(dev->read_buffer_primary + in_end_size, 'b', in_end_size); 74603270634SSteven Haigh memset(dev->read_buffer_primary + (2 * in_end_size), 'c', in_end_size); 74703270634SSteven Haigh memset(dev->read_buffer_primary + (3 * in_end_size), 'd', in_end_size); 74803270634SSteven Haigh 74903270634SSteven Haigh dev->read_buffer_secondary = kmalloc((4 * in_end_size), GFP_KERNEL); 75003270634SSteven Haigh if (!dev->read_buffer_secondary) { 75103270634SSteven Haigh dev_err(&interface->dev, "Couldn't allocate read_buffer_secondary\n"); 75203270634SSteven Haigh retval = -ENOMEM; 75303270634SSteven Haigh goto error; 75403270634SSteven Haigh } 75503270634SSteven Haigh 75603270634SSteven Haigh /* debug code prime the buffer */ 75703270634SSteven Haigh memset(dev->read_buffer_secondary, 'e', in_end_size); 75803270634SSteven Haigh memset(dev->read_buffer_secondary + in_end_size, 'f', in_end_size); 75903270634SSteven Haigh memset(dev->read_buffer_secondary + (2 * in_end_size), 'g', in_end_size); 76003270634SSteven Haigh memset(dev->read_buffer_secondary + (3 * in_end_size), 'h', in_end_size); 76103270634SSteven Haigh 76203270634SSteven Haigh dev->interrupt_in_buffer = kmalloc(in_end_size, GFP_KERNEL); 76303270634SSteven Haigh if (!dev->interrupt_in_buffer) { 76403270634SSteven Haigh dev_err(&interface->dev, "Couldn't allocate interrupt_in_buffer\n"); 76503270634SSteven Haigh goto error; 76603270634SSteven Haigh } 76703270634SSteven Haigh 76803270634SSteven Haigh /* debug code prime the buffer */ 76903270634SSteven Haigh memset(dev->interrupt_in_buffer, 'i', in_end_size); 77003270634SSteven Haigh 77103270634SSteven Haigh dev->interrupt_in_urb = usb_alloc_urb(0, GFP_KERNEL); 77203270634SSteven Haigh if (!dev->interrupt_in_urb) { 77303270634SSteven Haigh dev_err(&interface->dev, "Couldn't allocate interrupt_in_urb\n"); 77403270634SSteven Haigh goto error; 77503270634SSteven Haigh } 77603270634SSteven Haigh dev->interrupt_out_buffer = kmalloc(out_end_size, GFP_KERNEL); 77703270634SSteven Haigh if (!dev->interrupt_out_buffer) { 77803270634SSteven Haigh dev_err(&interface->dev, "Couldn't allocate interrupt_out_buffer\n"); 77903270634SSteven Haigh goto error; 78003270634SSteven Haigh } 78103270634SSteven Haigh dev->interrupt_out_urb = usb_alloc_urb(0, GFP_KERNEL); 78203270634SSteven Haigh if (!dev->interrupt_out_urb) { 78303270634SSteven Haigh dev_err(&interface->dev, "Couldn't allocate interrupt_out_urb\n"); 78403270634SSteven Haigh goto error; 78503270634SSteven Haigh } 78603270634SSteven Haigh 78703270634SSteven Haigh if (!usb_string(udev, udev->descriptor.iSerialNumber, dev->serial_number, 78803270634SSteven Haigh sizeof(dev->serial_number))) { 78903270634SSteven Haigh dev_err(&interface->dev, "Could not retrieve serial number\n"); 79003270634SSteven Haigh goto error; 79103270634SSteven Haigh } 79203270634SSteven Haigh dbg(2," %s : serial_number=%s", __FUNCTION__, dev->serial_number); 79303270634SSteven Haigh 79403270634SSteven Haigh /* we can register the device now, as it is ready */ 79503270634SSteven Haigh usb_set_intfdata(interface, dev); 79603270634SSteven Haigh 79703270634SSteven Haigh retval = usb_register_dev(interface, &adu_class); 79803270634SSteven Haigh 79903270634SSteven Haigh if (retval) { 80003270634SSteven Haigh /* something prevented us from registering this driver */ 80103270634SSteven Haigh dev_err(&interface->dev, "Not able to get a minor for this device.\n"); 80203270634SSteven Haigh usb_set_intfdata(interface, NULL); 80303270634SSteven Haigh goto error; 80403270634SSteven Haigh } 80503270634SSteven Haigh 80603270634SSteven Haigh dev->minor = interface->minor; 80703270634SSteven Haigh 80803270634SSteven Haigh /* let the user know what node this device is now attached to */ 80903270634SSteven Haigh dev_info(&interface->dev, "ADU%d %s now attached to /dev/usb/adutux%d", 81003270634SSteven Haigh udev->descriptor.idProduct, dev->serial_number, 81103270634SSteven Haigh (dev->minor - ADU_MINOR_BASE)); 81203270634SSteven Haigh exit: 81303270634SSteven Haigh dbg(2," %s : leave, return value %p (dev)", __FUNCTION__, dev); 81403270634SSteven Haigh 81503270634SSteven Haigh return retval; 81603270634SSteven Haigh 81703270634SSteven Haigh error: 81803270634SSteven Haigh adu_delete(dev); 81903270634SSteven Haigh return retval; 82003270634SSteven Haigh } 82103270634SSteven Haigh 82203270634SSteven Haigh /** 82303270634SSteven Haigh * adu_disconnect 82403270634SSteven Haigh * 82503270634SSteven Haigh * Called by the usb core when the device is removed from the system. 82603270634SSteven Haigh */ 82703270634SSteven Haigh static void adu_disconnect(struct usb_interface *interface) 82803270634SSteven Haigh { 82903270634SSteven Haigh struct adu_device *dev; 83003270634SSteven Haigh int minor; 83103270634SSteven Haigh 83203270634SSteven Haigh dbg(2," %s : enter", __FUNCTION__); 83303270634SSteven Haigh 83403270634SSteven Haigh mutex_lock(&disconnect_mutex); /* not interruptible */ 83503270634SSteven Haigh 83603270634SSteven Haigh dev = usb_get_intfdata(interface); 83703270634SSteven Haigh usb_set_intfdata(interface, NULL); 83803270634SSteven Haigh 83903270634SSteven Haigh down(&dev->sem); /* not interruptible */ 84003270634SSteven Haigh 84103270634SSteven Haigh minor = dev->minor; 84203270634SSteven Haigh 84303270634SSteven Haigh /* give back our minor */ 84403270634SSteven Haigh usb_deregister_dev(interface, &adu_class); 84503270634SSteven Haigh dev->minor = 0; 84603270634SSteven Haigh 84703270634SSteven Haigh /* if the device is not opened, then we clean up right now */ 84803270634SSteven Haigh dbg(2," %s : open count %d", __FUNCTION__, dev->open_count); 84903270634SSteven Haigh if (!dev->open_count) { 85003270634SSteven Haigh up(&dev->sem); 85103270634SSteven Haigh adu_delete(dev); 85203270634SSteven Haigh } else { 85303270634SSteven Haigh dev->udev = NULL; 85403270634SSteven Haigh up(&dev->sem); 85503270634SSteven Haigh } 85603270634SSteven Haigh 85703270634SSteven Haigh mutex_unlock(&disconnect_mutex); 85803270634SSteven Haigh 85903270634SSteven Haigh dev_info(&interface->dev, "ADU device adutux%d now disconnected", 86003270634SSteven Haigh (minor - ADU_MINOR_BASE)); 86103270634SSteven Haigh 86203270634SSteven Haigh dbg(2," %s : leave", __FUNCTION__); 86303270634SSteven Haigh } 86403270634SSteven Haigh 86503270634SSteven Haigh /* usb specific object needed to register this driver with the usb subsystem */ 86603270634SSteven Haigh static struct usb_driver adu_driver = { 86703270634SSteven Haigh .name = "adutux", 86803270634SSteven Haigh .probe = adu_probe, 86903270634SSteven Haigh .disconnect = adu_disconnect, 87003270634SSteven Haigh .id_table = device_table, 87103270634SSteven Haigh }; 87203270634SSteven Haigh 87303270634SSteven Haigh static int __init adu_init(void) 87403270634SSteven Haigh { 87503270634SSteven Haigh int result; 87603270634SSteven Haigh 87703270634SSteven Haigh dbg(2," %s : enter", __FUNCTION__); 87803270634SSteven Haigh 87903270634SSteven Haigh /* register this driver with the USB subsystem */ 88003270634SSteven Haigh result = usb_register(&adu_driver); 88103270634SSteven Haigh if (result < 0) { 88203270634SSteven Haigh err("usb_register failed for the "__FILE__" driver. " 88303270634SSteven Haigh "Error number %d", result); 88403270634SSteven Haigh goto exit; 88503270634SSteven Haigh } 88603270634SSteven Haigh 88703270634SSteven Haigh info("adutux " DRIVER_DESC " " DRIVER_VERSION); 88803270634SSteven Haigh info("adutux is an experimental driver. Use at your own risk"); 88903270634SSteven Haigh 89003270634SSteven Haigh exit: 89103270634SSteven Haigh dbg(2," %s : leave, return value %d", __FUNCTION__, result); 89203270634SSteven Haigh 89303270634SSteven Haigh return result; 89403270634SSteven Haigh } 89503270634SSteven Haigh 89603270634SSteven Haigh static void __exit adu_exit(void) 89703270634SSteven Haigh { 89803270634SSteven Haigh dbg(2," %s : enter", __FUNCTION__); 89903270634SSteven Haigh /* deregister this driver with the USB subsystem */ 90003270634SSteven Haigh usb_deregister(&adu_driver); 90103270634SSteven Haigh dbg(2," %s : leave", __FUNCTION__); 90203270634SSteven Haigh } 90303270634SSteven Haigh 90403270634SSteven Haigh module_init(adu_init); 90503270634SSteven Haigh module_exit(adu_exit); 90603270634SSteven Haigh 90703270634SSteven Haigh MODULE_AUTHOR(DRIVER_AUTHOR); 90803270634SSteven Haigh MODULE_DESCRIPTION(DRIVER_DESC); 90903270634SSteven Haigh MODULE_LICENSE("GPL"); 910