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> 2803270634SSteven Haigh #include <asm/uaccess.h> 2903270634SSteven Haigh 3003270634SSteven Haigh #ifdef CONFIG_USB_DEBUG 3103270634SSteven Haigh static int debug = 5; 3203270634SSteven Haigh #else 3303270634SSteven Haigh static int debug = 1; 3403270634SSteven Haigh #endif 3503270634SSteven Haigh 3603270634SSteven Haigh /* Use our own dbg macro */ 3703270634SSteven Haigh #undef dbg 3803270634SSteven Haigh #define dbg(lvl, format, arg...) \ 3903270634SSteven Haigh do { \ 4003270634SSteven Haigh if (debug >= lvl) \ 4103270634SSteven Haigh printk(KERN_DEBUG __FILE__ " : " format " \n", ## arg); \ 4203270634SSteven Haigh } while (0) 4303270634SSteven Haigh 4403270634SSteven Haigh 4503270634SSteven Haigh /* Version Information */ 4603270634SSteven Haigh #define DRIVER_VERSION "v0.0.13" 4703270634SSteven Haigh #define DRIVER_AUTHOR "John Homppi" 4803270634SSteven Haigh #define DRIVER_DESC "adutux (see www.ontrak.net)" 4903270634SSteven Haigh 5003270634SSteven Haigh /* Module parameters */ 5103270634SSteven Haigh module_param(debug, int, S_IRUGO | S_IWUSR); 5203270634SSteven Haigh MODULE_PARM_DESC(debug, "Debug enabled or not"); 5303270634SSteven Haigh 5403270634SSteven Haigh /* Define these values to match your device */ 5503270634SSteven Haigh #define ADU_VENDOR_ID 0x0a07 5603270634SSteven Haigh #define ADU_PRODUCT_ID 0x0064 5703270634SSteven Haigh 5803270634SSteven Haigh /* table of devices that work with this driver */ 5903270634SSteven Haigh static struct usb_device_id device_table [] = { 6003270634SSteven Haigh { USB_DEVICE(ADU_VENDOR_ID, ADU_PRODUCT_ID) }, /* ADU100 */ 6103270634SSteven Haigh { USB_DEVICE(ADU_VENDOR_ID, ADU_PRODUCT_ID+20) }, /* ADU120 */ 6203270634SSteven Haigh { USB_DEVICE(ADU_VENDOR_ID, ADU_PRODUCT_ID+30) }, /* ADU130 */ 6303270634SSteven Haigh { USB_DEVICE(ADU_VENDOR_ID, ADU_PRODUCT_ID+100) }, /* ADU200 */ 6403270634SSteven Haigh { USB_DEVICE(ADU_VENDOR_ID, ADU_PRODUCT_ID+108) }, /* ADU208 */ 6503270634SSteven Haigh { USB_DEVICE(ADU_VENDOR_ID, ADU_PRODUCT_ID+118) }, /* ADU218 */ 6603270634SSteven Haigh { }/* Terminating entry */ 6703270634SSteven Haigh }; 6803270634SSteven Haigh 6903270634SSteven Haigh MODULE_DEVICE_TABLE(usb, device_table); 7003270634SSteven Haigh 7103270634SSteven Haigh #ifdef CONFIG_USB_DYNAMIC_MINORS 7203270634SSteven Haigh #define ADU_MINOR_BASE 0 7303270634SSteven Haigh #else 7403270634SSteven Haigh #define ADU_MINOR_BASE 67 7503270634SSteven Haigh #endif 7603270634SSteven Haigh 7703270634SSteven Haigh /* we can have up to this number of device plugged in at once */ 7803270634SSteven Haigh #define MAX_DEVICES 16 7903270634SSteven Haigh 8003270634SSteven Haigh #define COMMAND_TIMEOUT (2*HZ) /* 60 second timeout for a command */ 8103270634SSteven Haigh 82f08812d5SPete Zaitcev /* 83f08812d5SPete Zaitcev * The locking scheme is a vanilla 3-lock: 84f08812d5SPete Zaitcev * adu_device.buflock: A spinlock, covers what IRQs touch. 85f08812d5SPete Zaitcev * adutux_mutex: A Static lock to cover open_count. It would also cover 86f08812d5SPete Zaitcev * any globals, but we don't have them in 2.6. 87f08812d5SPete Zaitcev * adu_device.mtx: A mutex to hold across sleepers like copy_from_user. 88f08812d5SPete Zaitcev * It covers all of adu_device, except the open_count 89f08812d5SPete Zaitcev * and what .buflock covers. 90f08812d5SPete Zaitcev */ 91f08812d5SPete Zaitcev 9203270634SSteven Haigh /* Structure to hold all of our device specific stuff */ 9303270634SSteven Haigh struct adu_device { 94f08812d5SPete Zaitcev struct mutex mtx; 9503270634SSteven Haigh struct usb_device* udev; /* save off the usb device pointer */ 9603270634SSteven Haigh struct usb_interface* interface; 97f08812d5SPete Zaitcev unsigned int minor; /* the starting minor number for this device */ 9803270634SSteven Haigh char serial_number[8]; 9903270634SSteven Haigh 10003270634SSteven Haigh int open_count; /* number of times this port has been opened */ 10103270634SSteven Haigh 10203270634SSteven Haigh char* read_buffer_primary; 10303270634SSteven Haigh int read_buffer_length; 10403270634SSteven Haigh char* read_buffer_secondary; 10503270634SSteven Haigh int secondary_head; 10603270634SSteven Haigh int secondary_tail; 10703270634SSteven Haigh spinlock_t buflock; 10803270634SSteven Haigh 10903270634SSteven Haigh wait_queue_head_t read_wait; 11003270634SSteven Haigh wait_queue_head_t write_wait; 11103270634SSteven Haigh 11203270634SSteven Haigh char* interrupt_in_buffer; 11303270634SSteven Haigh struct usb_endpoint_descriptor* interrupt_in_endpoint; 11403270634SSteven Haigh struct urb* interrupt_in_urb; 11503270634SSteven Haigh int read_urb_finished; 11603270634SSteven Haigh 11703270634SSteven Haigh char* interrupt_out_buffer; 11803270634SSteven Haigh struct usb_endpoint_descriptor* interrupt_out_endpoint; 11903270634SSteven Haigh struct urb* interrupt_out_urb; 120f08812d5SPete Zaitcev int out_urb_finished; 12103270634SSteven Haigh }; 12203270634SSteven Haigh 123f08812d5SPete Zaitcev static DEFINE_MUTEX(adutux_mutex); 124f08812d5SPete Zaitcev 12503270634SSteven Haigh static struct usb_driver adu_driver; 12603270634SSteven Haigh 12703270634SSteven Haigh static void adu_debug_data(int level, const char *function, int size, 12803270634SSteven Haigh const unsigned char *data) 12903270634SSteven Haigh { 13003270634SSteven Haigh int i; 13103270634SSteven Haigh 13203270634SSteven Haigh if (debug < level) 13303270634SSteven Haigh return; 13403270634SSteven Haigh 13503270634SSteven Haigh printk(KERN_DEBUG __FILE__": %s - length = %d, data = ", 13603270634SSteven Haigh function, size); 13703270634SSteven Haigh for (i = 0; i < size; ++i) 13803270634SSteven Haigh printk("%.2x ", data[i]); 13903270634SSteven Haigh printk("\n"); 14003270634SSteven Haigh } 14103270634SSteven Haigh 14203270634SSteven Haigh /** 14303270634SSteven Haigh * adu_abort_transfers 14403270634SSteven Haigh * aborts transfers and frees associated data structures 14503270634SSteven Haigh */ 14603270634SSteven Haigh static void adu_abort_transfers(struct adu_device *dev) 14703270634SSteven Haigh { 148f08812d5SPete Zaitcev unsigned long flags; 14903270634SSteven Haigh 150*441b62c1SHarvey Harrison dbg(2," %s : enter", __func__); 15103270634SSteven Haigh 15203270634SSteven Haigh if (dev->udev == NULL) { 153*441b62c1SHarvey Harrison dbg(1," %s : udev is null", __func__); 15403270634SSteven Haigh goto exit; 15503270634SSteven Haigh } 15603270634SSteven Haigh 15703270634SSteven Haigh /* shutdown transfer */ 158f08812d5SPete Zaitcev 159f08812d5SPete Zaitcev /* XXX Anchor these instead */ 160f08812d5SPete Zaitcev spin_lock_irqsave(&dev->buflock, flags); 161f08812d5SPete Zaitcev if (!dev->read_urb_finished) { 162f08812d5SPete Zaitcev spin_unlock_irqrestore(&dev->buflock, flags); 163f08812d5SPete Zaitcev usb_kill_urb(dev->interrupt_in_urb); 164f08812d5SPete Zaitcev } else 165f08812d5SPete Zaitcev spin_unlock_irqrestore(&dev->buflock, flags); 166f08812d5SPete Zaitcev 167f08812d5SPete Zaitcev spin_lock_irqsave(&dev->buflock, flags); 168f08812d5SPete Zaitcev if (!dev->out_urb_finished) { 169f08812d5SPete Zaitcev spin_unlock_irqrestore(&dev->buflock, flags); 170f08812d5SPete Zaitcev usb_kill_urb(dev->interrupt_out_urb); 171f08812d5SPete Zaitcev } else 172f08812d5SPete Zaitcev spin_unlock_irqrestore(&dev->buflock, flags); 17303270634SSteven Haigh 17403270634SSteven Haigh exit: 175*441b62c1SHarvey Harrison dbg(2," %s : leave", __func__); 17603270634SSteven Haigh } 17703270634SSteven Haigh 17803270634SSteven Haigh static void adu_delete(struct adu_device *dev) 17903270634SSteven Haigh { 180*441b62c1SHarvey Harrison dbg(2, "%s enter", __func__); 18103270634SSteven Haigh 18203270634SSteven Haigh /* free data structures */ 18303270634SSteven Haigh usb_free_urb(dev->interrupt_in_urb); 18403270634SSteven Haigh usb_free_urb(dev->interrupt_out_urb); 18503270634SSteven Haigh kfree(dev->read_buffer_primary); 18603270634SSteven Haigh kfree(dev->read_buffer_secondary); 18703270634SSteven Haigh kfree(dev->interrupt_in_buffer); 18803270634SSteven Haigh kfree(dev->interrupt_out_buffer); 18903270634SSteven Haigh kfree(dev); 19003270634SSteven Haigh 191*441b62c1SHarvey Harrison dbg(2, "%s : leave", __func__); 19203270634SSteven Haigh } 19303270634SSteven Haigh 1947d12e780SDavid Howells static void adu_interrupt_in_callback(struct urb *urb) 19503270634SSteven Haigh { 19603270634SSteven Haigh struct adu_device *dev = urb->context; 19724497a00SGreg Kroah-Hartman int status = urb->status; 19803270634SSteven Haigh 199*441b62c1SHarvey Harrison dbg(4," %s : enter, status %d", __func__, status); 200*441b62c1SHarvey Harrison adu_debug_data(5, __func__, urb->actual_length, 20103270634SSteven Haigh urb->transfer_buffer); 20203270634SSteven Haigh 20303270634SSteven Haigh spin_lock(&dev->buflock); 20403270634SSteven Haigh 20524497a00SGreg Kroah-Hartman if (status != 0) { 206f6c1ceaaSOliver Neukum if ((status != -ENOENT) && (status != -ECONNRESET) && 207f6c1ceaaSOliver Neukum (status != -ESHUTDOWN)) { 20803270634SSteven Haigh dbg(1," %s : nonzero status received: %d", 209*441b62c1SHarvey Harrison __func__, status); 21003270634SSteven Haigh } 21103270634SSteven Haigh goto exit; 21203270634SSteven Haigh } 21303270634SSteven Haigh 21403270634SSteven Haigh if (urb->actual_length > 0 && dev->interrupt_in_buffer[0] != 0x00) { 21503270634SSteven Haigh if (dev->read_buffer_length < 21603270634SSteven Haigh (4 * le16_to_cpu(dev->interrupt_in_endpoint->wMaxPacketSize)) - 21703270634SSteven Haigh (urb->actual_length)) { 21803270634SSteven Haigh memcpy (dev->read_buffer_primary + 21903270634SSteven Haigh dev->read_buffer_length, 22003270634SSteven Haigh dev->interrupt_in_buffer, urb->actual_length); 22103270634SSteven Haigh 22203270634SSteven Haigh dev->read_buffer_length += urb->actual_length; 223*441b62c1SHarvey Harrison dbg(2," %s reading %d ", __func__, 22403270634SSteven Haigh urb->actual_length); 22503270634SSteven Haigh } else { 226*441b62c1SHarvey Harrison dbg(1," %s : read_buffer overflow", __func__); 22703270634SSteven Haigh } 22803270634SSteven Haigh } 22903270634SSteven Haigh 23003270634SSteven Haigh exit: 23103270634SSteven Haigh dev->read_urb_finished = 1; 23203270634SSteven Haigh spin_unlock(&dev->buflock); 23303270634SSteven Haigh /* always wake up so we recover from errors */ 23403270634SSteven Haigh wake_up_interruptible(&dev->read_wait); 235*441b62c1SHarvey Harrison adu_debug_data(5, __func__, urb->actual_length, 23603270634SSteven Haigh urb->transfer_buffer); 237*441b62c1SHarvey Harrison dbg(4," %s : leave, status %d", __func__, status); 23803270634SSteven Haigh } 23903270634SSteven Haigh 2407d12e780SDavid Howells static void adu_interrupt_out_callback(struct urb *urb) 24103270634SSteven Haigh { 24203270634SSteven Haigh struct adu_device *dev = urb->context; 24324497a00SGreg Kroah-Hartman int status = urb->status; 24403270634SSteven Haigh 245*441b62c1SHarvey Harrison dbg(4," %s : enter, status %d", __func__, status); 246*441b62c1SHarvey Harrison adu_debug_data(5,__func__, urb->actual_length, urb->transfer_buffer); 24703270634SSteven Haigh 24824497a00SGreg Kroah-Hartman if (status != 0) { 24924497a00SGreg Kroah-Hartman if ((status != -ENOENT) && 25024497a00SGreg Kroah-Hartman (status != -ECONNRESET)) { 25103270634SSteven Haigh dbg(1, " %s :nonzero status received: %d", 252*441b62c1SHarvey Harrison __func__, status); 25303270634SSteven Haigh } 25403270634SSteven Haigh goto exit; 25503270634SSteven Haigh } 25603270634SSteven Haigh 257f08812d5SPete Zaitcev spin_lock(&dev->buflock); 258f08812d5SPete Zaitcev dev->out_urb_finished = 1; 259f08812d5SPete Zaitcev wake_up(&dev->write_wait); 260f08812d5SPete Zaitcev spin_unlock(&dev->buflock); 26103270634SSteven Haigh exit: 26203270634SSteven Haigh 263*441b62c1SHarvey Harrison adu_debug_data(5, __func__, urb->actual_length, 26403270634SSteven Haigh urb->transfer_buffer); 265*441b62c1SHarvey Harrison dbg(4," %s : leave, status %d", __func__, status); 26603270634SSteven Haigh } 26703270634SSteven Haigh 26803270634SSteven Haigh static int adu_open(struct inode *inode, struct file *file) 26903270634SSteven Haigh { 27003270634SSteven Haigh struct adu_device *dev = NULL; 27103270634SSteven Haigh struct usb_interface *interface; 27203270634SSteven Haigh int subminor; 273f08812d5SPete Zaitcev int retval; 27403270634SSteven Haigh 275*441b62c1SHarvey Harrison dbg(2,"%s : enter", __func__); 27603270634SSteven Haigh 27703270634SSteven Haigh subminor = iminor(inode); 27803270634SSteven Haigh 279f08812d5SPete Zaitcev if ((retval = mutex_lock_interruptible(&adutux_mutex))) { 280*441b62c1SHarvey Harrison dbg(2, "%s : mutex lock failed", __func__); 281f08812d5SPete Zaitcev goto exit_no_lock; 282f08812d5SPete Zaitcev } 283f08812d5SPete Zaitcev 28403270634SSteven Haigh interface = usb_find_interface(&adu_driver, subminor); 28503270634SSteven Haigh if (!interface) { 28603270634SSteven Haigh err("%s - error, can't find device for minor %d", 287*441b62c1SHarvey Harrison __func__, subminor); 28803270634SSteven Haigh retval = -ENODEV; 28903270634SSteven Haigh goto exit_no_device; 29003270634SSteven Haigh } 29103270634SSteven Haigh 29203270634SSteven Haigh dev = usb_get_intfdata(interface); 293f08812d5SPete Zaitcev if (!dev || !dev->udev) { 29403270634SSteven Haigh retval = -ENODEV; 29503270634SSteven Haigh goto exit_no_device; 29603270634SSteven Haigh } 29703270634SSteven Haigh 298f08812d5SPete Zaitcev /* check that nobody else is using the device */ 299f08812d5SPete Zaitcev if (dev->open_count) { 300f08812d5SPete Zaitcev retval = -EBUSY; 30103270634SSteven Haigh goto exit_no_device; 30203270634SSteven Haigh } 30303270634SSteven Haigh 30403270634SSteven Haigh ++dev->open_count; 305*441b62c1SHarvey Harrison dbg(2,"%s : open count %d", __func__, dev->open_count); 30603270634SSteven Haigh 30703270634SSteven Haigh /* save device in the file's private structure */ 30803270634SSteven Haigh file->private_data = dev; 30903270634SSteven Haigh 31003270634SSteven Haigh /* initialize in direction */ 31103270634SSteven Haigh dev->read_buffer_length = 0; 31203270634SSteven Haigh 31303270634SSteven Haigh /* fixup first read by having urb waiting for it */ 31403270634SSteven Haigh usb_fill_int_urb(dev->interrupt_in_urb,dev->udev, 31503270634SSteven Haigh usb_rcvintpipe(dev->udev, 31603270634SSteven Haigh dev->interrupt_in_endpoint->bEndpointAddress), 31703270634SSteven Haigh dev->interrupt_in_buffer, 31803270634SSteven Haigh le16_to_cpu(dev->interrupt_in_endpoint->wMaxPacketSize), 31903270634SSteven Haigh adu_interrupt_in_callback, dev, 32003270634SSteven Haigh dev->interrupt_in_endpoint->bInterval); 32103270634SSteven Haigh dev->read_urb_finished = 0; 322f08812d5SPete Zaitcev if (usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL)) 323f08812d5SPete Zaitcev dev->read_urb_finished = 1; 324f08812d5SPete Zaitcev /* we ignore failure */ 325f08812d5SPete Zaitcev /* end of fixup for first read */ 326f08812d5SPete Zaitcev 327f08812d5SPete Zaitcev /* initialize out direction */ 328f08812d5SPete Zaitcev dev->out_urb_finished = 1; 329f08812d5SPete Zaitcev 330f08812d5SPete Zaitcev retval = 0; 33103270634SSteven Haigh 33203270634SSteven Haigh exit_no_device: 333f08812d5SPete Zaitcev mutex_unlock(&adutux_mutex); 334f08812d5SPete Zaitcev exit_no_lock: 335*441b62c1SHarvey Harrison dbg(2,"%s : leave, return value %d ", __func__, retval); 33603270634SSteven Haigh return retval; 33703270634SSteven Haigh } 33803270634SSteven Haigh 339f08812d5SPete Zaitcev static void adu_release_internal(struct adu_device *dev) 34003270634SSteven Haigh { 341*441b62c1SHarvey Harrison dbg(2," %s : enter", __func__); 34203270634SSteven Haigh 34303270634SSteven Haigh /* decrement our usage count for the device */ 34403270634SSteven Haigh --dev->open_count; 345*441b62c1SHarvey Harrison dbg(2," %s : open count %d", __func__, dev->open_count); 34603270634SSteven Haigh if (dev->open_count <= 0) { 34703270634SSteven Haigh adu_abort_transfers(dev); 34803270634SSteven Haigh dev->open_count = 0; 34903270634SSteven Haigh } 35003270634SSteven Haigh 351*441b62c1SHarvey Harrison dbg(2," %s : leave", __func__); 35203270634SSteven Haigh } 35303270634SSteven Haigh 35403270634SSteven Haigh static int adu_release(struct inode *inode, struct file *file) 35503270634SSteven Haigh { 356f08812d5SPete Zaitcev struct adu_device *dev; 35703270634SSteven Haigh int retval = 0; 35803270634SSteven Haigh 359*441b62c1SHarvey Harrison dbg(2," %s : enter", __func__); 36003270634SSteven Haigh 36103270634SSteven Haigh if (file == NULL) { 362*441b62c1SHarvey Harrison dbg(1," %s : file is NULL", __func__); 36303270634SSteven Haigh retval = -ENODEV; 36403270634SSteven Haigh goto exit; 36503270634SSteven Haigh } 36603270634SSteven Haigh 36703270634SSteven Haigh dev = file->private_data; 36803270634SSteven Haigh if (dev == NULL) { 369*441b62c1SHarvey Harrison dbg(1," %s : object is NULL", __func__); 37003270634SSteven Haigh retval = -ENODEV; 37103270634SSteven Haigh goto exit; 37203270634SSteven Haigh } 37303270634SSteven Haigh 374f08812d5SPete Zaitcev mutex_lock(&adutux_mutex); /* not interruptible */ 37503270634SSteven Haigh 37603270634SSteven Haigh if (dev->open_count <= 0) { 377*441b62c1SHarvey Harrison dbg(1," %s : device not opened", __func__); 37803270634SSteven Haigh retval = -ENODEV; 37903270634SSteven Haigh goto exit; 38003270634SSteven Haigh } 38103270634SSteven Haigh 382f08812d5SPete Zaitcev adu_release_internal(dev); 383d4ead16fSAlan Stern if (dev->udev == NULL) { 384d4ead16fSAlan Stern /* the device was unplugged before the file was released */ 385f08812d5SPete Zaitcev if (!dev->open_count) /* ... and we're the last user */ 386d4ead16fSAlan Stern adu_delete(dev); 387d4ead16fSAlan Stern } 38803270634SSteven Haigh 38903270634SSteven Haigh exit: 390f08812d5SPete Zaitcev mutex_unlock(&adutux_mutex); 391*441b62c1SHarvey Harrison dbg(2," %s : leave, return value %d", __func__, retval); 39203270634SSteven Haigh return retval; 39303270634SSteven Haigh } 39403270634SSteven Haigh 39503270634SSteven Haigh static ssize_t adu_read(struct file *file, __user char *buffer, size_t count, 39603270634SSteven Haigh loff_t *ppos) 39703270634SSteven Haigh { 39803270634SSteven Haigh struct adu_device *dev; 39903270634SSteven Haigh size_t bytes_read = 0; 40003270634SSteven Haigh size_t bytes_to_read = count; 40103270634SSteven Haigh int i; 40203270634SSteven Haigh int retval = 0; 40303270634SSteven Haigh int timeout = 0; 40403270634SSteven Haigh int should_submit = 0; 40503270634SSteven Haigh unsigned long flags; 40603270634SSteven Haigh DECLARE_WAITQUEUE(wait, current); 40703270634SSteven Haigh 408*441b62c1SHarvey Harrison dbg(2," %s : enter, count = %Zd, file=%p", __func__, count, file); 40903270634SSteven Haigh 41003270634SSteven Haigh dev = file->private_data; 411*441b62c1SHarvey Harrison dbg(2," %s : dev=%p", __func__, dev); 412f08812d5SPete Zaitcev 4138293c568SMatthias Kaehlcke if (mutex_lock_interruptible(&dev->mtx)) 41403270634SSteven Haigh return -ERESTARTSYS; 41503270634SSteven Haigh 41603270634SSteven Haigh /* verify that the device wasn't unplugged */ 417f08812d5SPete Zaitcev if (dev->udev == NULL) { 41803270634SSteven Haigh retval = -ENODEV; 41903270634SSteven Haigh err("No device or device unplugged %d", retval); 42003270634SSteven Haigh goto exit; 42103270634SSteven Haigh } 42203270634SSteven Haigh 42303270634SSteven Haigh /* verify that some data was requested */ 42403270634SSteven Haigh if (count == 0) { 425*441b62c1SHarvey Harrison dbg(1," %s : read request of 0 bytes", __func__); 42603270634SSteven Haigh goto exit; 42703270634SSteven Haigh } 42803270634SSteven Haigh 42903270634SSteven Haigh timeout = COMMAND_TIMEOUT; 430*441b62c1SHarvey Harrison dbg(2," %s : about to start looping", __func__); 43103270634SSteven Haigh while (bytes_to_read) { 43203270634SSteven Haigh int data_in_secondary = dev->secondary_tail - dev->secondary_head; 43303270634SSteven Haigh dbg(2," %s : while, data_in_secondary=%d, status=%d", 434*441b62c1SHarvey Harrison __func__, data_in_secondary, 43503270634SSteven Haigh dev->interrupt_in_urb->status); 43603270634SSteven Haigh 43703270634SSteven Haigh if (data_in_secondary) { 43803270634SSteven Haigh /* drain secondary buffer */ 43903270634SSteven Haigh int amount = bytes_to_read < data_in_secondary ? bytes_to_read : data_in_secondary; 44003270634SSteven Haigh i = copy_to_user(buffer, dev->read_buffer_secondary+dev->secondary_head, amount); 44103270634SSteven Haigh if (i < 0) { 44203270634SSteven Haigh retval = -EFAULT; 44303270634SSteven Haigh goto exit; 44403270634SSteven Haigh } 44503270634SSteven Haigh dev->secondary_head += (amount - i); 44603270634SSteven Haigh bytes_read += (amount - i); 44703270634SSteven Haigh bytes_to_read -= (amount - i); 44803270634SSteven Haigh if (i) { 44903270634SSteven Haigh retval = bytes_read ? bytes_read : -EFAULT; 45003270634SSteven Haigh goto exit; 45103270634SSteven Haigh } 45203270634SSteven Haigh } else { 45303270634SSteven Haigh /* we check the primary buffer */ 45403270634SSteven Haigh spin_lock_irqsave (&dev->buflock, flags); 45503270634SSteven Haigh if (dev->read_buffer_length) { 45603270634SSteven Haigh /* we secure access to the primary */ 45703270634SSteven Haigh char *tmp; 45803270634SSteven Haigh dbg(2," %s : swap, read_buffer_length = %d", 459*441b62c1SHarvey Harrison __func__, dev->read_buffer_length); 46003270634SSteven Haigh tmp = dev->read_buffer_secondary; 46103270634SSteven Haigh dev->read_buffer_secondary = dev->read_buffer_primary; 46203270634SSteven Haigh dev->read_buffer_primary = tmp; 46303270634SSteven Haigh dev->secondary_head = 0; 46403270634SSteven Haigh dev->secondary_tail = dev->read_buffer_length; 46503270634SSteven Haigh dev->read_buffer_length = 0; 46603270634SSteven Haigh spin_unlock_irqrestore(&dev->buflock, flags); 46703270634SSteven Haigh /* we have a free buffer so use it */ 46803270634SSteven Haigh should_submit = 1; 46903270634SSteven Haigh } else { 47003270634SSteven Haigh /* even the primary was empty - we may need to do IO */ 471f08812d5SPete Zaitcev if (!dev->read_urb_finished) { 47203270634SSteven Haigh /* somebody is doing IO */ 47303270634SSteven Haigh spin_unlock_irqrestore(&dev->buflock, flags); 474*441b62c1SHarvey Harrison dbg(2," %s : submitted already", __func__); 47503270634SSteven Haigh } else { 47603270634SSteven Haigh /* we must initiate input */ 477*441b62c1SHarvey Harrison dbg(2," %s : initiate input", __func__); 47803270634SSteven Haigh dev->read_urb_finished = 0; 479f08812d5SPete Zaitcev spin_unlock_irqrestore(&dev->buflock, flags); 48003270634SSteven Haigh 48103270634SSteven Haigh usb_fill_int_urb(dev->interrupt_in_urb,dev->udev, 48203270634SSteven Haigh usb_rcvintpipe(dev->udev, 48303270634SSteven Haigh dev->interrupt_in_endpoint->bEndpointAddress), 48403270634SSteven Haigh dev->interrupt_in_buffer, 48503270634SSteven Haigh le16_to_cpu(dev->interrupt_in_endpoint->wMaxPacketSize), 48603270634SSteven Haigh adu_interrupt_in_callback, 48703270634SSteven Haigh dev, 48803270634SSteven Haigh dev->interrupt_in_endpoint->bInterval); 489f08812d5SPete Zaitcev retval = usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL); 490f08812d5SPete Zaitcev if (retval) { 491f08812d5SPete Zaitcev dev->read_urb_finished = 1; 49203270634SSteven Haigh if (retval == -ENOMEM) { 49303270634SSteven Haigh retval = bytes_read ? bytes_read : -ENOMEM; 49403270634SSteven Haigh } 495*441b62c1SHarvey Harrison dbg(2," %s : submit failed", __func__); 49603270634SSteven Haigh goto exit; 49703270634SSteven Haigh } 49803270634SSteven Haigh } 49903270634SSteven Haigh 50003270634SSteven Haigh /* we wait for I/O to complete */ 50103270634SSteven Haigh set_current_state(TASK_INTERRUPTIBLE); 50203270634SSteven Haigh add_wait_queue(&dev->read_wait, &wait); 503f08812d5SPete Zaitcev spin_lock_irqsave(&dev->buflock, flags); 504f08812d5SPete Zaitcev if (!dev->read_urb_finished) { 505f08812d5SPete Zaitcev spin_unlock_irqrestore(&dev->buflock, flags); 50603270634SSteven Haigh timeout = schedule_timeout(COMMAND_TIMEOUT); 507f08812d5SPete Zaitcev } else { 508f08812d5SPete Zaitcev spin_unlock_irqrestore(&dev->buflock, flags); 50903270634SSteven Haigh set_current_state(TASK_RUNNING); 510f08812d5SPete Zaitcev } 51103270634SSteven Haigh remove_wait_queue(&dev->read_wait, &wait); 51203270634SSteven Haigh 51303270634SSteven Haigh if (timeout <= 0) { 514*441b62c1SHarvey Harrison dbg(2," %s : timeout", __func__); 51503270634SSteven Haigh retval = bytes_read ? bytes_read : -ETIMEDOUT; 51603270634SSteven Haigh goto exit; 51703270634SSteven Haigh } 51803270634SSteven Haigh 51903270634SSteven Haigh if (signal_pending(current)) { 520*441b62c1SHarvey Harrison dbg(2," %s : signal pending", __func__); 52103270634SSteven Haigh retval = bytes_read ? bytes_read : -EINTR; 52203270634SSteven Haigh goto exit; 52303270634SSteven Haigh } 52403270634SSteven Haigh } 52503270634SSteven Haigh } 52603270634SSteven Haigh } 52703270634SSteven Haigh 52803270634SSteven Haigh retval = bytes_read; 52903270634SSteven Haigh /* if the primary buffer is empty then use it */ 530f08812d5SPete Zaitcev spin_lock_irqsave(&dev->buflock, flags); 531f08812d5SPete Zaitcev if (should_submit && dev->read_urb_finished) { 532f08812d5SPete Zaitcev dev->read_urb_finished = 0; 533f08812d5SPete Zaitcev spin_unlock_irqrestore(&dev->buflock, flags); 53403270634SSteven Haigh usb_fill_int_urb(dev->interrupt_in_urb,dev->udev, 53503270634SSteven Haigh usb_rcvintpipe(dev->udev, 53603270634SSteven Haigh dev->interrupt_in_endpoint->bEndpointAddress), 53703270634SSteven Haigh dev->interrupt_in_buffer, 53803270634SSteven Haigh le16_to_cpu(dev->interrupt_in_endpoint->wMaxPacketSize), 53903270634SSteven Haigh adu_interrupt_in_callback, 54003270634SSteven Haigh dev, 54103270634SSteven Haigh dev->interrupt_in_endpoint->bInterval); 542f08812d5SPete Zaitcev if (usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL) != 0) 543f08812d5SPete Zaitcev dev->read_urb_finished = 1; 54403270634SSteven Haigh /* we ignore failure */ 545f08812d5SPete Zaitcev } else { 546f08812d5SPete Zaitcev spin_unlock_irqrestore(&dev->buflock, flags); 54703270634SSteven Haigh } 54803270634SSteven Haigh 54903270634SSteven Haigh exit: 55003270634SSteven Haigh /* unlock the device */ 5518293c568SMatthias Kaehlcke mutex_unlock(&dev->mtx); 55203270634SSteven Haigh 553*441b62c1SHarvey Harrison dbg(2," %s : leave, return value %d", __func__, retval); 55403270634SSteven Haigh return retval; 55503270634SSteven Haigh } 55603270634SSteven Haigh 55703270634SSteven Haigh static ssize_t adu_write(struct file *file, const __user char *buffer, 55803270634SSteven Haigh size_t count, loff_t *ppos) 55903270634SSteven Haigh { 560f08812d5SPete Zaitcev DECLARE_WAITQUEUE(waita, current); 56103270634SSteven Haigh struct adu_device *dev; 56203270634SSteven Haigh size_t bytes_written = 0; 56303270634SSteven Haigh size_t bytes_to_write; 56403270634SSteven Haigh size_t buffer_size; 565f08812d5SPete Zaitcev unsigned long flags; 566ebc3ac14SOliver Neukum int retval; 56703270634SSteven Haigh 568*441b62c1SHarvey Harrison dbg(2," %s : enter, count = %Zd", __func__, count); 56903270634SSteven Haigh 57003270634SSteven Haigh dev = file->private_data; 57103270634SSteven Haigh 5728293c568SMatthias Kaehlcke retval = mutex_lock_interruptible(&dev->mtx); 573ebc3ac14SOliver Neukum if (retval) 574ebc3ac14SOliver Neukum goto exit_nolock; 57503270634SSteven Haigh 57603270634SSteven Haigh /* verify that the device wasn't unplugged */ 577f08812d5SPete Zaitcev if (dev->udev == NULL) { 57803270634SSteven Haigh retval = -ENODEV; 57903270634SSteven Haigh err("No device or device unplugged %d", retval); 58003270634SSteven Haigh goto exit; 58103270634SSteven Haigh } 58203270634SSteven Haigh 58303270634SSteven Haigh /* verify that we actually have some data to write */ 58403270634SSteven Haigh if (count == 0) { 585*441b62c1SHarvey Harrison dbg(1," %s : write request of 0 bytes", __func__); 58603270634SSteven Haigh goto exit; 58703270634SSteven Haigh } 58803270634SSteven Haigh 58903270634SSteven Haigh while (count > 0) { 590f08812d5SPete Zaitcev add_wait_queue(&dev->write_wait, &waita); 591f08812d5SPete Zaitcev set_current_state(TASK_INTERRUPTIBLE); 592f08812d5SPete Zaitcev spin_lock_irqsave(&dev->buflock, flags); 593f08812d5SPete Zaitcev if (!dev->out_urb_finished) { 594f08812d5SPete Zaitcev spin_unlock_irqrestore(&dev->buflock, flags); 59503270634SSteven Haigh 596f08812d5SPete Zaitcev mutex_unlock(&dev->mtx); 59703270634SSteven Haigh if (signal_pending(current)) { 598*441b62c1SHarvey Harrison dbg(1," %s : interrupted", __func__); 599f08812d5SPete Zaitcev set_current_state(TASK_RUNNING); 60003270634SSteven Haigh retval = -EINTR; 601f08812d5SPete Zaitcev goto exit_onqueue; 60203270634SSteven Haigh } 603f08812d5SPete Zaitcev if (schedule_timeout(COMMAND_TIMEOUT) == 0) { 604*441b62c1SHarvey Harrison dbg(1, "%s - command timed out.", __func__); 605f08812d5SPete Zaitcev retval = -ETIMEDOUT; 606f08812d5SPete Zaitcev goto exit_onqueue; 607f08812d5SPete Zaitcev } 608f08812d5SPete Zaitcev remove_wait_queue(&dev->write_wait, &waita); 6098293c568SMatthias Kaehlcke retval = mutex_lock_interruptible(&dev->mtx); 610ebc3ac14SOliver Neukum if (retval) { 611ebc3ac14SOliver Neukum retval = bytes_written ? bytes_written : retval; 612ebc3ac14SOliver Neukum goto exit_nolock; 613ebc3ac14SOliver Neukum } 61403270634SSteven Haigh 615*441b62c1SHarvey Harrison dbg(4," %s : in progress, count = %Zd", __func__, count); 61603270634SSteven Haigh } else { 617f08812d5SPete Zaitcev spin_unlock_irqrestore(&dev->buflock, flags); 618f08812d5SPete Zaitcev set_current_state(TASK_RUNNING); 619f08812d5SPete Zaitcev remove_wait_queue(&dev->write_wait, &waita); 620*441b62c1SHarvey Harrison dbg(4," %s : sending, count = %Zd", __func__, count); 62103270634SSteven Haigh 62203270634SSteven Haigh /* write the data into interrupt_out_buffer from userspace */ 62303270634SSteven Haigh buffer_size = le16_to_cpu(dev->interrupt_out_endpoint->wMaxPacketSize); 62403270634SSteven Haigh bytes_to_write = count > buffer_size ? buffer_size : count; 62503270634SSteven Haigh dbg(4," %s : buffer_size = %Zd, count = %Zd, bytes_to_write = %Zd", 626*441b62c1SHarvey Harrison __func__, buffer_size, count, bytes_to_write); 62703270634SSteven Haigh 62803270634SSteven Haigh if (copy_from_user(dev->interrupt_out_buffer, buffer, bytes_to_write) != 0) { 62903270634SSteven Haigh retval = -EFAULT; 63003270634SSteven Haigh goto exit; 63103270634SSteven Haigh } 63203270634SSteven Haigh 63303270634SSteven Haigh /* send off the urb */ 63403270634SSteven Haigh usb_fill_int_urb( 63503270634SSteven Haigh dev->interrupt_out_urb, 63603270634SSteven Haigh dev->udev, 63703270634SSteven Haigh usb_sndintpipe(dev->udev, dev->interrupt_out_endpoint->bEndpointAddress), 63803270634SSteven Haigh dev->interrupt_out_buffer, 63903270634SSteven Haigh bytes_to_write, 64003270634SSteven Haigh adu_interrupt_out_callback, 64103270634SSteven Haigh dev, 642f08812d5SPete Zaitcev dev->interrupt_out_endpoint->bInterval); 64303270634SSteven Haigh dev->interrupt_out_urb->actual_length = bytes_to_write; 644f08812d5SPete Zaitcev dev->out_urb_finished = 0; 64503270634SSteven Haigh retval = usb_submit_urb(dev->interrupt_out_urb, GFP_KERNEL); 64603270634SSteven Haigh if (retval < 0) { 647f08812d5SPete Zaitcev dev->out_urb_finished = 1; 64803270634SSteven Haigh err("Couldn't submit interrupt_out_urb %d", retval); 64903270634SSteven Haigh goto exit; 65003270634SSteven Haigh } 65103270634SSteven Haigh 65203270634SSteven Haigh buffer += bytes_to_write; 65303270634SSteven Haigh count -= bytes_to_write; 65403270634SSteven Haigh 65503270634SSteven Haigh bytes_written += bytes_to_write; 65603270634SSteven Haigh } 65703270634SSteven Haigh } 658f08812d5SPete Zaitcev mutex_unlock(&dev->mtx); 659f08812d5SPete Zaitcev return bytes_written; 66003270634SSteven Haigh 66103270634SSteven Haigh exit: 6628293c568SMatthias Kaehlcke mutex_unlock(&dev->mtx); 663ebc3ac14SOliver Neukum exit_nolock: 664*441b62c1SHarvey Harrison dbg(2," %s : leave, return value %d", __func__, retval); 665f08812d5SPete Zaitcev return retval; 66603270634SSteven Haigh 667f08812d5SPete Zaitcev exit_onqueue: 668f08812d5SPete Zaitcev remove_wait_queue(&dev->write_wait, &waita); 66903270634SSteven Haigh return retval; 67003270634SSteven Haigh } 67103270634SSteven Haigh 67203270634SSteven Haigh /* file operations needed when we register this driver */ 67300977a59SArjan van de Ven static const struct file_operations adu_fops = { 67403270634SSteven Haigh .owner = THIS_MODULE, 67503270634SSteven Haigh .read = adu_read, 67603270634SSteven Haigh .write = adu_write, 67703270634SSteven Haigh .open = adu_open, 67803270634SSteven Haigh .release = adu_release, 67903270634SSteven Haigh }; 68003270634SSteven Haigh 68103270634SSteven Haigh /* 68203270634SSteven Haigh * usb class driver info in order to get a minor number from the usb core, 68303270634SSteven Haigh * and to have the device registered with devfs and the driver core 68403270634SSteven Haigh */ 68503270634SSteven Haigh static struct usb_class_driver adu_class = { 68603270634SSteven Haigh .name = "usb/adutux%d", 68703270634SSteven Haigh .fops = &adu_fops, 68803270634SSteven Haigh .minor_base = ADU_MINOR_BASE, 68903270634SSteven Haigh }; 69003270634SSteven Haigh 69103270634SSteven Haigh /** 69203270634SSteven Haigh * adu_probe 69303270634SSteven Haigh * 69403270634SSteven Haigh * Called by the usb core when a new device is connected that it thinks 69503270634SSteven Haigh * this driver might be interested in. 69603270634SSteven Haigh */ 69703270634SSteven Haigh static int adu_probe(struct usb_interface *interface, 69803270634SSteven Haigh const struct usb_device_id *id) 69903270634SSteven Haigh { 70003270634SSteven Haigh struct usb_device *udev = interface_to_usbdev(interface); 70103270634SSteven Haigh struct adu_device *dev = NULL; 70203270634SSteven Haigh struct usb_host_interface *iface_desc; 70303270634SSteven Haigh struct usb_endpoint_descriptor *endpoint; 70403270634SSteven Haigh int retval = -ENODEV; 70503270634SSteven Haigh int in_end_size; 70603270634SSteven Haigh int out_end_size; 70703270634SSteven Haigh int i; 70803270634SSteven Haigh 709*441b62c1SHarvey Harrison dbg(2," %s : enter", __func__); 71003270634SSteven Haigh 71103270634SSteven Haigh if (udev == NULL) { 71203270634SSteven Haigh dev_err(&interface->dev, "udev is NULL.\n"); 71303270634SSteven Haigh goto exit; 71403270634SSteven Haigh } 71503270634SSteven Haigh 71603270634SSteven Haigh /* allocate memory for our device state and intialize it */ 71703270634SSteven Haigh dev = kzalloc(sizeof(struct adu_device), GFP_KERNEL); 71803270634SSteven Haigh if (dev == NULL) { 71903270634SSteven Haigh dev_err(&interface->dev, "Out of memory\n"); 72003270634SSteven Haigh retval = -ENOMEM; 72103270634SSteven Haigh goto exit; 72203270634SSteven Haigh } 72303270634SSteven Haigh 7248293c568SMatthias Kaehlcke mutex_init(&dev->mtx); 72503270634SSteven Haigh spin_lock_init(&dev->buflock); 72603270634SSteven Haigh dev->udev = udev; 72703270634SSteven Haigh init_waitqueue_head(&dev->read_wait); 72803270634SSteven Haigh init_waitqueue_head(&dev->write_wait); 72903270634SSteven Haigh 73003270634SSteven Haigh iface_desc = &interface->altsetting[0]; 73103270634SSteven Haigh 73203270634SSteven Haigh /* set up the endpoint information */ 73303270634SSteven Haigh for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) { 73403270634SSteven Haigh endpoint = &iface_desc->endpoint[i].desc; 73503270634SSteven Haigh 73603270634SSteven Haigh if (usb_endpoint_is_int_in(endpoint)) 73703270634SSteven Haigh dev->interrupt_in_endpoint = endpoint; 73803270634SSteven Haigh 73903270634SSteven Haigh if (usb_endpoint_is_int_out(endpoint)) 74003270634SSteven Haigh dev->interrupt_out_endpoint = endpoint; 74103270634SSteven Haigh } 74203270634SSteven Haigh if (dev->interrupt_in_endpoint == NULL) { 74303270634SSteven Haigh dev_err(&interface->dev, "interrupt in endpoint not found\n"); 74403270634SSteven Haigh goto error; 74503270634SSteven Haigh } 74603270634SSteven Haigh if (dev->interrupt_out_endpoint == NULL) { 74703270634SSteven Haigh dev_err(&interface->dev, "interrupt out endpoint not found\n"); 74803270634SSteven Haigh goto error; 74903270634SSteven Haigh } 75003270634SSteven Haigh 75103270634SSteven Haigh in_end_size = le16_to_cpu(dev->interrupt_in_endpoint->wMaxPacketSize); 75203270634SSteven Haigh out_end_size = le16_to_cpu(dev->interrupt_out_endpoint->wMaxPacketSize); 75303270634SSteven Haigh 75403270634SSteven Haigh dev->read_buffer_primary = kmalloc((4 * in_end_size), GFP_KERNEL); 75503270634SSteven Haigh if (!dev->read_buffer_primary) { 75603270634SSteven Haigh dev_err(&interface->dev, "Couldn't allocate read_buffer_primary\n"); 75703270634SSteven Haigh retval = -ENOMEM; 75803270634SSteven Haigh goto error; 75903270634SSteven Haigh } 76003270634SSteven Haigh 76103270634SSteven Haigh /* debug code prime the buffer */ 76203270634SSteven Haigh memset(dev->read_buffer_primary, 'a', in_end_size); 76303270634SSteven Haigh memset(dev->read_buffer_primary + in_end_size, 'b', in_end_size); 76403270634SSteven Haigh memset(dev->read_buffer_primary + (2 * in_end_size), 'c', in_end_size); 76503270634SSteven Haigh memset(dev->read_buffer_primary + (3 * in_end_size), 'd', in_end_size); 76603270634SSteven Haigh 76703270634SSteven Haigh dev->read_buffer_secondary = kmalloc((4 * in_end_size), GFP_KERNEL); 76803270634SSteven Haigh if (!dev->read_buffer_secondary) { 76903270634SSteven Haigh dev_err(&interface->dev, "Couldn't allocate read_buffer_secondary\n"); 77003270634SSteven Haigh retval = -ENOMEM; 77103270634SSteven Haigh goto error; 77203270634SSteven Haigh } 77303270634SSteven Haigh 77403270634SSteven Haigh /* debug code prime the buffer */ 77503270634SSteven Haigh memset(dev->read_buffer_secondary, 'e', in_end_size); 77603270634SSteven Haigh memset(dev->read_buffer_secondary + in_end_size, 'f', in_end_size); 77703270634SSteven Haigh memset(dev->read_buffer_secondary + (2 * in_end_size), 'g', in_end_size); 77803270634SSteven Haigh memset(dev->read_buffer_secondary + (3 * in_end_size), 'h', in_end_size); 77903270634SSteven Haigh 78003270634SSteven Haigh dev->interrupt_in_buffer = kmalloc(in_end_size, GFP_KERNEL); 78103270634SSteven Haigh if (!dev->interrupt_in_buffer) { 78203270634SSteven Haigh dev_err(&interface->dev, "Couldn't allocate interrupt_in_buffer\n"); 78303270634SSteven Haigh goto error; 78403270634SSteven Haigh } 78503270634SSteven Haigh 78603270634SSteven Haigh /* debug code prime the buffer */ 78703270634SSteven Haigh memset(dev->interrupt_in_buffer, 'i', in_end_size); 78803270634SSteven Haigh 78903270634SSteven Haigh dev->interrupt_in_urb = usb_alloc_urb(0, GFP_KERNEL); 79003270634SSteven Haigh if (!dev->interrupt_in_urb) { 79103270634SSteven Haigh dev_err(&interface->dev, "Couldn't allocate interrupt_in_urb\n"); 79203270634SSteven Haigh goto error; 79303270634SSteven Haigh } 79403270634SSteven Haigh dev->interrupt_out_buffer = kmalloc(out_end_size, GFP_KERNEL); 79503270634SSteven Haigh if (!dev->interrupt_out_buffer) { 79603270634SSteven Haigh dev_err(&interface->dev, "Couldn't allocate interrupt_out_buffer\n"); 79703270634SSteven Haigh goto error; 79803270634SSteven Haigh } 79903270634SSteven Haigh dev->interrupt_out_urb = usb_alloc_urb(0, GFP_KERNEL); 80003270634SSteven Haigh if (!dev->interrupt_out_urb) { 80103270634SSteven Haigh dev_err(&interface->dev, "Couldn't allocate interrupt_out_urb\n"); 80203270634SSteven Haigh goto error; 80303270634SSteven Haigh } 80403270634SSteven Haigh 80503270634SSteven Haigh if (!usb_string(udev, udev->descriptor.iSerialNumber, dev->serial_number, 80603270634SSteven Haigh sizeof(dev->serial_number))) { 80703270634SSteven Haigh dev_err(&interface->dev, "Could not retrieve serial number\n"); 80803270634SSteven Haigh goto error; 80903270634SSteven Haigh } 810*441b62c1SHarvey Harrison dbg(2," %s : serial_number=%s", __func__, dev->serial_number); 81103270634SSteven Haigh 81203270634SSteven Haigh /* we can register the device now, as it is ready */ 81303270634SSteven Haigh usb_set_intfdata(interface, dev); 81403270634SSteven Haigh 81503270634SSteven Haigh retval = usb_register_dev(interface, &adu_class); 81603270634SSteven Haigh 81703270634SSteven Haigh if (retval) { 81803270634SSteven Haigh /* something prevented us from registering this driver */ 81903270634SSteven Haigh dev_err(&interface->dev, "Not able to get a minor for this device.\n"); 82003270634SSteven Haigh usb_set_intfdata(interface, NULL); 82103270634SSteven Haigh goto error; 82203270634SSteven Haigh } 82303270634SSteven Haigh 82403270634SSteven Haigh dev->minor = interface->minor; 82503270634SSteven Haigh 82603270634SSteven Haigh /* let the user know what node this device is now attached to */ 827898eb71cSJoe Perches dev_info(&interface->dev, "ADU%d %s now attached to /dev/usb/adutux%d\n", 82803270634SSteven Haigh udev->descriptor.idProduct, dev->serial_number, 82903270634SSteven Haigh (dev->minor - ADU_MINOR_BASE)); 83003270634SSteven Haigh exit: 831*441b62c1SHarvey Harrison dbg(2," %s : leave, return value %p (dev)", __func__, dev); 83203270634SSteven Haigh 83303270634SSteven Haigh return retval; 83403270634SSteven Haigh 83503270634SSteven Haigh error: 83603270634SSteven Haigh adu_delete(dev); 83703270634SSteven Haigh return retval; 83803270634SSteven Haigh } 83903270634SSteven Haigh 84003270634SSteven Haigh /** 84103270634SSteven Haigh * adu_disconnect 84203270634SSteven Haigh * 84303270634SSteven Haigh * Called by the usb core when the device is removed from the system. 84403270634SSteven Haigh */ 84503270634SSteven Haigh static void adu_disconnect(struct usb_interface *interface) 84603270634SSteven Haigh { 84703270634SSteven Haigh struct adu_device *dev; 84803270634SSteven Haigh int minor; 84903270634SSteven Haigh 850*441b62c1SHarvey Harrison dbg(2," %s : enter", __func__); 85103270634SSteven Haigh 85203270634SSteven Haigh dev = usb_get_intfdata(interface); 85303270634SSteven Haigh 8548293c568SMatthias Kaehlcke mutex_lock(&dev->mtx); /* not interruptible */ 855f08812d5SPete Zaitcev dev->udev = NULL; /* poison */ 856f08812d5SPete Zaitcev minor = dev->minor; 857f08812d5SPete Zaitcev usb_deregister_dev(interface, &adu_class); 858f08812d5SPete Zaitcev mutex_unlock(&dev->mtx); 859f08812d5SPete Zaitcev 860f08812d5SPete Zaitcev mutex_lock(&adutux_mutex); 861f08812d5SPete Zaitcev usb_set_intfdata(interface, NULL); 862d4ead16fSAlan Stern 86303270634SSteven Haigh /* if the device is not opened, then we clean up right now */ 864*441b62c1SHarvey Harrison dbg(2," %s : open count %d", __func__, dev->open_count); 865f08812d5SPete Zaitcev if (!dev->open_count) 86603270634SSteven Haigh adu_delete(dev); 867f08812d5SPete Zaitcev 868f08812d5SPete Zaitcev mutex_unlock(&adutux_mutex); 86903270634SSteven Haigh 870898eb71cSJoe Perches dev_info(&interface->dev, "ADU device adutux%d now disconnected\n", 87103270634SSteven Haigh (minor - ADU_MINOR_BASE)); 87203270634SSteven Haigh 873*441b62c1SHarvey Harrison dbg(2," %s : leave", __func__); 87403270634SSteven Haigh } 87503270634SSteven Haigh 87603270634SSteven Haigh /* usb specific object needed to register this driver with the usb subsystem */ 87703270634SSteven Haigh static struct usb_driver adu_driver = { 87803270634SSteven Haigh .name = "adutux", 87903270634SSteven Haigh .probe = adu_probe, 88003270634SSteven Haigh .disconnect = adu_disconnect, 88103270634SSteven Haigh .id_table = device_table, 88203270634SSteven Haigh }; 88303270634SSteven Haigh 88403270634SSteven Haigh static int __init adu_init(void) 88503270634SSteven Haigh { 88603270634SSteven Haigh int result; 88703270634SSteven Haigh 888*441b62c1SHarvey Harrison dbg(2," %s : enter", __func__); 88903270634SSteven Haigh 89003270634SSteven Haigh /* register this driver with the USB subsystem */ 89103270634SSteven Haigh result = usb_register(&adu_driver); 89203270634SSteven Haigh if (result < 0) { 89303270634SSteven Haigh err("usb_register failed for the "__FILE__" driver. " 89403270634SSteven Haigh "Error number %d", result); 89503270634SSteven Haigh goto exit; 89603270634SSteven Haigh } 89703270634SSteven Haigh 89803270634SSteven Haigh info("adutux " DRIVER_DESC " " DRIVER_VERSION); 89903270634SSteven Haigh info("adutux is an experimental driver. Use at your own risk"); 90003270634SSteven Haigh 90103270634SSteven Haigh exit: 902*441b62c1SHarvey Harrison dbg(2," %s : leave, return value %d", __func__, result); 90303270634SSteven Haigh 90403270634SSteven Haigh return result; 90503270634SSteven Haigh } 90603270634SSteven Haigh 90703270634SSteven Haigh static void __exit adu_exit(void) 90803270634SSteven Haigh { 909*441b62c1SHarvey Harrison dbg(2," %s : enter", __func__); 91003270634SSteven Haigh /* deregister this driver with the USB subsystem */ 91103270634SSteven Haigh usb_deregister(&adu_driver); 912*441b62c1SHarvey Harrison dbg(2," %s : leave", __func__); 91303270634SSteven Haigh } 91403270634SSteven Haigh 91503270634SSteven Haigh module_init(adu_init); 91603270634SSteven Haigh module_exit(adu_exit); 91703270634SSteven Haigh 91803270634SSteven Haigh MODULE_AUTHOR(DRIVER_AUTHOR); 91903270634SSteven Haigh MODULE_DESCRIPTION(DRIVER_DESC); 92003270634SSteven Haigh MODULE_LICENSE("GPL"); 921