103270634SSteven Haigh /* 203270634SSteven Haigh * adutux - driver for ADU devices from Ontrak Control Systems 303270634SSteven Haigh * This is an experimental driver. Use at your own risk. 403270634SSteven Haigh * This driver is not supported by Ontrak Control Systems. 503270634SSteven Haigh * 603270634SSteven Haigh * Copyright (c) 2003 John Homppi (SCO, leave this notice here) 703270634SSteven Haigh * 803270634SSteven Haigh * This program is free software; you can redistribute it and/or 903270634SSteven Haigh * modify it under the terms of the GNU General Public License as 1003270634SSteven Haigh * published by the Free Software Foundation; either version 2 of 1103270634SSteven Haigh * the License, or (at your option) any later version. 1203270634SSteven Haigh * 1303270634SSteven Haigh * derived from the Lego USB Tower driver 0.56: 1403270634SSteven Haigh * Copyright (c) 2003 David Glance <davidgsf@sourceforge.net> 1503270634SSteven Haigh * 2001 Juergen Stuber <stuber@loria.fr> 1603270634SSteven Haigh * that was derived from USB Skeleton driver - 0.5 1703270634SSteven Haigh * Copyright (c) 2001 Greg Kroah-Hartman (greg@kroah.com) 1803270634SSteven Haigh * 1903270634SSteven Haigh */ 2003270634SSteven Haigh 2103270634SSteven Haigh #include <linux/kernel.h> 2203270634SSteven Haigh #include <linux/errno.h> 2303270634SSteven Haigh #include <linux/init.h> 2403270634SSteven Haigh #include <linux/slab.h> 2503270634SSteven Haigh #include <linux/module.h> 2603270634SSteven Haigh #include <linux/usb.h> 278293c568SMatthias Kaehlcke #include <linux/mutex.h> 2840cf4833SLisa Nguyen #include <linux/uaccess.h> 2903270634SSteven Haigh 3003270634SSteven Haigh #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) \ 41f45ba776SJoe Perches printk(KERN_DEBUG "%s: " format "\n", __FILE__, ##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 */ 5933b9e162SNémeth Márton static const 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 135f45ba776SJoe Perches printk(KERN_DEBUG "%s: %s - length = %d, data = ", 136f45ba776SJoe Perches __FILE__, 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 150441b62c1SHarvey Harrison dbg(2, " %s : enter", __func__); 15103270634SSteven Haigh 15203270634SSteven Haigh if (dev->udev == NULL) { 153441b62c1SHarvey 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: 175441b62c1SHarvey Harrison dbg(2, " %s : leave", __func__); 17603270634SSteven Haigh } 17703270634SSteven Haigh 17803270634SSteven Haigh static void adu_delete(struct adu_device *dev) 17903270634SSteven Haigh { 180441b62c1SHarvey 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 191441b62c1SHarvey 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 199441b62c1SHarvey Harrison dbg(4, " %s : enter, status %d", __func__, status); 200441b62c1SHarvey 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", 209441b62c1SHarvey 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 < 21629cc8897SKuninori Morimoto (4 * usb_endpoint_maxp(dev->interrupt_in_endpoint)) - 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; 223441b62c1SHarvey Harrison dbg(2, " %s reading %d ", __func__, 22403270634SSteven Haigh urb->actual_length); 22503270634SSteven Haigh } else { 226441b62c1SHarvey 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); 235441b62c1SHarvey Harrison adu_debug_data(5, __func__, urb->actual_length, 23603270634SSteven Haigh urb->transfer_buffer); 237441b62c1SHarvey 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 245441b62c1SHarvey Harrison dbg(4, " %s : enter, status %d", __func__, status); 246441b62c1SHarvey 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", 252441b62c1SHarvey 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 263441b62c1SHarvey Harrison adu_debug_data(5, __func__, urb->actual_length, 26403270634SSteven Haigh urb->transfer_buffer); 265441b62c1SHarvey 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 275441b62c1SHarvey Harrison dbg(2, "%s : enter", __func__); 27603270634SSteven Haigh 27703270634SSteven Haigh subminor = iminor(inode); 27803270634SSteven Haigh 279e77c4e6aSLisa Nguyen retval = mutex_lock_interruptible(&adutux_mutex); 280e77c4e6aSLisa Nguyen if (retval) { 281441b62c1SHarvey Harrison dbg(2, "%s : mutex lock failed", __func__); 282f08812d5SPete Zaitcev goto exit_no_lock; 283f08812d5SPete Zaitcev } 284f08812d5SPete Zaitcev 28503270634SSteven Haigh interface = usb_find_interface(&adu_driver, subminor); 28603270634SSteven Haigh if (!interface) { 287fd3f1917SGreg Kroah-Hartman printk(KERN_ERR "adutux: %s - error, can't find device for " 288fd3f1917SGreg Kroah-Hartman "minor %d\n", __func__, subminor); 28903270634SSteven Haigh retval = -ENODEV; 29003270634SSteven Haigh goto exit_no_device; 29103270634SSteven Haigh } 29203270634SSteven Haigh 29303270634SSteven Haigh dev = usb_get_intfdata(interface); 294f08812d5SPete Zaitcev if (!dev || !dev->udev) { 29503270634SSteven Haigh retval = -ENODEV; 29603270634SSteven Haigh goto exit_no_device; 29703270634SSteven Haigh } 29803270634SSteven Haigh 299f08812d5SPete Zaitcev /* check that nobody else is using the device */ 300f08812d5SPete Zaitcev if (dev->open_count) { 301f08812d5SPete Zaitcev retval = -EBUSY; 30203270634SSteven Haigh goto exit_no_device; 30303270634SSteven Haigh } 30403270634SSteven Haigh 30503270634SSteven Haigh ++dev->open_count; 306441b62c1SHarvey Harrison dbg(2, "%s : open count %d", __func__, dev->open_count); 30703270634SSteven Haigh 30803270634SSteven Haigh /* save device in the file's private structure */ 30903270634SSteven Haigh file->private_data = dev; 31003270634SSteven Haigh 31103270634SSteven Haigh /* initialize in direction */ 31203270634SSteven Haigh dev->read_buffer_length = 0; 31303270634SSteven Haigh 31403270634SSteven Haigh /* fixup first read by having urb waiting for it */ 31503270634SSteven Haigh usb_fill_int_urb(dev->interrupt_in_urb, dev->udev, 31603270634SSteven Haigh usb_rcvintpipe(dev->udev, 31703270634SSteven Haigh dev->interrupt_in_endpoint->bEndpointAddress), 31803270634SSteven Haigh dev->interrupt_in_buffer, 31929cc8897SKuninori Morimoto usb_endpoint_maxp(dev->interrupt_in_endpoint), 32003270634SSteven Haigh adu_interrupt_in_callback, dev, 32103270634SSteven Haigh dev->interrupt_in_endpoint->bInterval); 32203270634SSteven Haigh dev->read_urb_finished = 0; 323f08812d5SPete Zaitcev if (usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL)) 324f08812d5SPete Zaitcev dev->read_urb_finished = 1; 325f08812d5SPete Zaitcev /* we ignore failure */ 326f08812d5SPete Zaitcev /* end of fixup for first read */ 327f08812d5SPete Zaitcev 328f08812d5SPete Zaitcev /* initialize out direction */ 329f08812d5SPete Zaitcev dev->out_urb_finished = 1; 330f08812d5SPete Zaitcev 331f08812d5SPete Zaitcev retval = 0; 33203270634SSteven Haigh 33303270634SSteven Haigh exit_no_device: 334f08812d5SPete Zaitcev mutex_unlock(&adutux_mutex); 335f08812d5SPete Zaitcev exit_no_lock: 336441b62c1SHarvey Harrison dbg(2, "%s : leave, return value %d ", __func__, retval); 33703270634SSteven Haigh return retval; 33803270634SSteven Haigh } 33903270634SSteven Haigh 340f08812d5SPete Zaitcev static void adu_release_internal(struct adu_device *dev) 34103270634SSteven Haigh { 342441b62c1SHarvey Harrison dbg(2, " %s : enter", __func__); 34303270634SSteven Haigh 34403270634SSteven Haigh /* decrement our usage count for the device */ 34503270634SSteven Haigh --dev->open_count; 346441b62c1SHarvey Harrison dbg(2, " %s : open count %d", __func__, dev->open_count); 34703270634SSteven Haigh if (dev->open_count <= 0) { 34803270634SSteven Haigh adu_abort_transfers(dev); 34903270634SSteven Haigh dev->open_count = 0; 35003270634SSteven Haigh } 35103270634SSteven Haigh 352441b62c1SHarvey Harrison dbg(2, " %s : leave", __func__); 35303270634SSteven Haigh } 35403270634SSteven Haigh 35503270634SSteven Haigh static int adu_release(struct inode *inode, struct file *file) 35603270634SSteven Haigh { 357f08812d5SPete Zaitcev struct adu_device *dev; 35803270634SSteven Haigh int retval = 0; 35903270634SSteven Haigh 360441b62c1SHarvey Harrison dbg(2, " %s : enter", __func__); 36103270634SSteven Haigh 36203270634SSteven Haigh if (file == NULL) { 363441b62c1SHarvey Harrison dbg(1, " %s : file is NULL", __func__); 36403270634SSteven Haigh retval = -ENODEV; 36503270634SSteven Haigh goto exit; 36603270634SSteven Haigh } 36703270634SSteven Haigh 36803270634SSteven Haigh dev = file->private_data; 36903270634SSteven Haigh if (dev == NULL) { 370441b62c1SHarvey Harrison dbg(1, " %s : object is NULL", __func__); 37103270634SSteven Haigh retval = -ENODEV; 37203270634SSteven Haigh goto exit; 37303270634SSteven Haigh } 37403270634SSteven Haigh 375f08812d5SPete Zaitcev mutex_lock(&adutux_mutex); /* not interruptible */ 37603270634SSteven Haigh 37703270634SSteven Haigh if (dev->open_count <= 0) { 378441b62c1SHarvey Harrison dbg(1, " %s : device not opened", __func__); 37903270634SSteven Haigh retval = -ENODEV; 38046c9844cSJiri Slaby goto unlock; 38103270634SSteven Haigh } 38203270634SSteven Haigh 383f08812d5SPete Zaitcev adu_release_internal(dev); 384d4ead16fSAlan Stern if (dev->udev == NULL) { 385d4ead16fSAlan Stern /* the device was unplugged before the file was released */ 386f08812d5SPete Zaitcev if (!dev->open_count) /* ... and we're the last user */ 387d4ead16fSAlan Stern adu_delete(dev); 388d4ead16fSAlan Stern } 38946c9844cSJiri Slaby unlock: 390f08812d5SPete Zaitcev mutex_unlock(&adutux_mutex); 39146c9844cSJiri Slaby exit: 392441b62c1SHarvey Harrison dbg(2, " %s : leave, return value %d", __func__, retval); 39303270634SSteven Haigh return retval; 39403270634SSteven Haigh } 39503270634SSteven Haigh 39603270634SSteven Haigh static ssize_t adu_read(struct file *file, __user char *buffer, size_t count, 39703270634SSteven Haigh loff_t *ppos) 39803270634SSteven Haigh { 39903270634SSteven Haigh struct adu_device *dev; 40003270634SSteven Haigh size_t bytes_read = 0; 40103270634SSteven Haigh size_t bytes_to_read = count; 40203270634SSteven Haigh int i; 40303270634SSteven Haigh int retval = 0; 40403270634SSteven Haigh int timeout = 0; 40503270634SSteven Haigh int should_submit = 0; 40603270634SSteven Haigh unsigned long flags; 40703270634SSteven Haigh DECLARE_WAITQUEUE(wait, current); 40803270634SSteven Haigh 409441b62c1SHarvey Harrison dbg(2, " %s : enter, count = %Zd, file=%p", __func__, count, file); 41003270634SSteven Haigh 41103270634SSteven Haigh dev = file->private_data; 412441b62c1SHarvey Harrison dbg(2, " %s : dev=%p", __func__, dev); 413f08812d5SPete Zaitcev 4148293c568SMatthias Kaehlcke if (mutex_lock_interruptible(&dev->mtx)) 41503270634SSteven Haigh return -ERESTARTSYS; 41603270634SSteven Haigh 41703270634SSteven Haigh /* verify that the device wasn't unplugged */ 418f08812d5SPete Zaitcev if (dev->udev == NULL) { 41903270634SSteven Haigh retval = -ENODEV; 420fd3f1917SGreg Kroah-Hartman printk(KERN_ERR "adutux: No device or device unplugged %d\n", 421fd3f1917SGreg Kroah-Hartman retval); 42203270634SSteven Haigh goto exit; 42303270634SSteven Haigh } 42403270634SSteven Haigh 42503270634SSteven Haigh /* verify that some data was requested */ 42603270634SSteven Haigh if (count == 0) { 427441b62c1SHarvey Harrison dbg(1, " %s : read request of 0 bytes", __func__); 42803270634SSteven Haigh goto exit; 42903270634SSteven Haigh } 43003270634SSteven Haigh 43103270634SSteven Haigh timeout = COMMAND_TIMEOUT; 432441b62c1SHarvey Harrison dbg(2, " %s : about to start looping", __func__); 43303270634SSteven Haigh while (bytes_to_read) { 43403270634SSteven Haigh int data_in_secondary = dev->secondary_tail - dev->secondary_head; 43503270634SSteven Haigh dbg(2, " %s : while, data_in_secondary=%d, status=%d", 436441b62c1SHarvey Harrison __func__, data_in_secondary, 43703270634SSteven Haigh dev->interrupt_in_urb->status); 43803270634SSteven Haigh 43903270634SSteven Haigh if (data_in_secondary) { 44003270634SSteven Haigh /* drain secondary buffer */ 44103270634SSteven Haigh int amount = bytes_to_read < data_in_secondary ? bytes_to_read : data_in_secondary; 44203270634SSteven Haigh i = copy_to_user(buffer, dev->read_buffer_secondary+dev->secondary_head, amount); 4431865a9c3SKulikov Vasiliy if (i) { 44403270634SSteven Haigh retval = -EFAULT; 44503270634SSteven Haigh goto exit; 44603270634SSteven Haigh } 44703270634SSteven Haigh dev->secondary_head += (amount - i); 44803270634SSteven Haigh bytes_read += (amount - i); 44903270634SSteven Haigh bytes_to_read -= (amount - i); 45003270634SSteven Haigh if (i) { 45103270634SSteven Haigh retval = bytes_read ? bytes_read : -EFAULT; 45203270634SSteven Haigh goto exit; 45303270634SSteven Haigh } 45403270634SSteven Haigh } else { 45503270634SSteven Haigh /* we check the primary buffer */ 45603270634SSteven Haigh spin_lock_irqsave (&dev->buflock, flags); 45703270634SSteven Haigh if (dev->read_buffer_length) { 45803270634SSteven Haigh /* we secure access to the primary */ 45903270634SSteven Haigh char *tmp; 46003270634SSteven Haigh dbg(2, " %s : swap, read_buffer_length = %d", 461441b62c1SHarvey Harrison __func__, dev->read_buffer_length); 46203270634SSteven Haigh tmp = dev->read_buffer_secondary; 46303270634SSteven Haigh dev->read_buffer_secondary = dev->read_buffer_primary; 46403270634SSteven Haigh dev->read_buffer_primary = tmp; 46503270634SSteven Haigh dev->secondary_head = 0; 46603270634SSteven Haigh dev->secondary_tail = dev->read_buffer_length; 46703270634SSteven Haigh dev->read_buffer_length = 0; 46803270634SSteven Haigh spin_unlock_irqrestore(&dev->buflock, flags); 46903270634SSteven Haigh /* we have a free buffer so use it */ 47003270634SSteven Haigh should_submit = 1; 47103270634SSteven Haigh } else { 47203270634SSteven Haigh /* even the primary was empty - we may need to do IO */ 473f08812d5SPete Zaitcev if (!dev->read_urb_finished) { 47403270634SSteven Haigh /* somebody is doing IO */ 47503270634SSteven Haigh spin_unlock_irqrestore(&dev->buflock, flags); 476441b62c1SHarvey Harrison dbg(2, " %s : submitted already", __func__); 47703270634SSteven Haigh } else { 47803270634SSteven Haigh /* we must initiate input */ 479441b62c1SHarvey Harrison dbg(2, " %s : initiate input", __func__); 48003270634SSteven Haigh dev->read_urb_finished = 0; 481f08812d5SPete Zaitcev spin_unlock_irqrestore(&dev->buflock, flags); 48203270634SSteven Haigh 48303270634SSteven Haigh usb_fill_int_urb(dev->interrupt_in_urb, dev->udev, 48403270634SSteven Haigh usb_rcvintpipe(dev->udev, 48503270634SSteven Haigh dev->interrupt_in_endpoint->bEndpointAddress), 48603270634SSteven Haigh dev->interrupt_in_buffer, 48729cc8897SKuninori Morimoto usb_endpoint_maxp(dev->interrupt_in_endpoint), 48803270634SSteven Haigh adu_interrupt_in_callback, 48903270634SSteven Haigh dev, 49003270634SSteven Haigh dev->interrupt_in_endpoint->bInterval); 491f08812d5SPete Zaitcev retval = usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL); 492f08812d5SPete Zaitcev if (retval) { 493f08812d5SPete Zaitcev dev->read_urb_finished = 1; 49403270634SSteven Haigh if (retval == -ENOMEM) { 49503270634SSteven Haigh retval = bytes_read ? bytes_read : -ENOMEM; 49603270634SSteven Haigh } 497441b62c1SHarvey Harrison dbg(2, " %s : submit failed", __func__); 49803270634SSteven Haigh goto exit; 49903270634SSteven Haigh } 50003270634SSteven Haigh } 50103270634SSteven Haigh 50203270634SSteven Haigh /* we wait for I/O to complete */ 50303270634SSteven Haigh set_current_state(TASK_INTERRUPTIBLE); 50403270634SSteven Haigh add_wait_queue(&dev->read_wait, &wait); 505f08812d5SPete Zaitcev spin_lock_irqsave(&dev->buflock, flags); 506f08812d5SPete Zaitcev if (!dev->read_urb_finished) { 507f08812d5SPete Zaitcev spin_unlock_irqrestore(&dev->buflock, flags); 50803270634SSteven Haigh timeout = schedule_timeout(COMMAND_TIMEOUT); 509f08812d5SPete Zaitcev } else { 510f08812d5SPete Zaitcev spin_unlock_irqrestore(&dev->buflock, flags); 51103270634SSteven Haigh set_current_state(TASK_RUNNING); 512f08812d5SPete Zaitcev } 51303270634SSteven Haigh remove_wait_queue(&dev->read_wait, &wait); 51403270634SSteven Haigh 51503270634SSteven Haigh if (timeout <= 0) { 516441b62c1SHarvey Harrison dbg(2, " %s : timeout", __func__); 51703270634SSteven Haigh retval = bytes_read ? bytes_read : -ETIMEDOUT; 51803270634SSteven Haigh goto exit; 51903270634SSteven Haigh } 52003270634SSteven Haigh 52103270634SSteven Haigh if (signal_pending(current)) { 522441b62c1SHarvey Harrison dbg(2, " %s : signal pending", __func__); 52303270634SSteven Haigh retval = bytes_read ? bytes_read : -EINTR; 52403270634SSteven Haigh goto exit; 52503270634SSteven Haigh } 52603270634SSteven Haigh } 52703270634SSteven Haigh } 52803270634SSteven Haigh } 52903270634SSteven Haigh 53003270634SSteven Haigh retval = bytes_read; 53103270634SSteven Haigh /* if the primary buffer is empty then use it */ 532f08812d5SPete Zaitcev spin_lock_irqsave(&dev->buflock, flags); 533f08812d5SPete Zaitcev if (should_submit && dev->read_urb_finished) { 534f08812d5SPete Zaitcev dev->read_urb_finished = 0; 535f08812d5SPete Zaitcev spin_unlock_irqrestore(&dev->buflock, flags); 53603270634SSteven Haigh usb_fill_int_urb(dev->interrupt_in_urb, dev->udev, 53703270634SSteven Haigh usb_rcvintpipe(dev->udev, 53803270634SSteven Haigh dev->interrupt_in_endpoint->bEndpointAddress), 53903270634SSteven Haigh dev->interrupt_in_buffer, 54029cc8897SKuninori Morimoto usb_endpoint_maxp(dev->interrupt_in_endpoint), 54103270634SSteven Haigh adu_interrupt_in_callback, 54203270634SSteven Haigh dev, 54303270634SSteven Haigh dev->interrupt_in_endpoint->bInterval); 544f08812d5SPete Zaitcev if (usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL) != 0) 545f08812d5SPete Zaitcev dev->read_urb_finished = 1; 54603270634SSteven Haigh /* we ignore failure */ 547f08812d5SPete Zaitcev } else { 548f08812d5SPete Zaitcev spin_unlock_irqrestore(&dev->buflock, flags); 54903270634SSteven Haigh } 55003270634SSteven Haigh 55103270634SSteven Haigh exit: 55203270634SSteven Haigh /* unlock the device */ 5538293c568SMatthias Kaehlcke mutex_unlock(&dev->mtx); 55403270634SSteven Haigh 555441b62c1SHarvey Harrison dbg(2, " %s : leave, return value %d", __func__, retval); 55603270634SSteven Haigh return retval; 55703270634SSteven Haigh } 55803270634SSteven Haigh 55903270634SSteven Haigh static ssize_t adu_write(struct file *file, const __user char *buffer, 56003270634SSteven Haigh size_t count, loff_t *ppos) 56103270634SSteven Haigh { 562f08812d5SPete Zaitcev DECLARE_WAITQUEUE(waita, current); 56303270634SSteven Haigh struct adu_device *dev; 56403270634SSteven Haigh size_t bytes_written = 0; 56503270634SSteven Haigh size_t bytes_to_write; 56603270634SSteven Haigh size_t buffer_size; 567f08812d5SPete Zaitcev unsigned long flags; 568ebc3ac14SOliver Neukum int retval; 56903270634SSteven Haigh 570441b62c1SHarvey Harrison dbg(2, " %s : enter, count = %Zd", __func__, count); 57103270634SSteven Haigh 57203270634SSteven Haigh dev = file->private_data; 57303270634SSteven Haigh 5748293c568SMatthias Kaehlcke retval = mutex_lock_interruptible(&dev->mtx); 575ebc3ac14SOliver Neukum if (retval) 576ebc3ac14SOliver Neukum goto exit_nolock; 57703270634SSteven Haigh 57803270634SSteven Haigh /* verify that the device wasn't unplugged */ 579f08812d5SPete Zaitcev if (dev->udev == NULL) { 58003270634SSteven Haigh retval = -ENODEV; 581fd3f1917SGreg Kroah-Hartman printk(KERN_ERR "adutux: No device or device unplugged %d\n", 582fd3f1917SGreg Kroah-Hartman retval); 58303270634SSteven Haigh goto exit; 58403270634SSteven Haigh } 58503270634SSteven Haigh 58603270634SSteven Haigh /* verify that we actually have some data to write */ 58703270634SSteven Haigh if (count == 0) { 588441b62c1SHarvey Harrison dbg(1, " %s : write request of 0 bytes", __func__); 58903270634SSteven Haigh goto exit; 59003270634SSteven Haigh } 59103270634SSteven Haigh 59203270634SSteven Haigh while (count > 0) { 593f08812d5SPete Zaitcev add_wait_queue(&dev->write_wait, &waita); 594f08812d5SPete Zaitcev set_current_state(TASK_INTERRUPTIBLE); 595f08812d5SPete Zaitcev spin_lock_irqsave(&dev->buflock, flags); 596f08812d5SPete Zaitcev if (!dev->out_urb_finished) { 597f08812d5SPete Zaitcev spin_unlock_irqrestore(&dev->buflock, flags); 59803270634SSteven Haigh 599f08812d5SPete Zaitcev mutex_unlock(&dev->mtx); 60003270634SSteven Haigh if (signal_pending(current)) { 601441b62c1SHarvey Harrison dbg(1, " %s : interrupted", __func__); 602f08812d5SPete Zaitcev set_current_state(TASK_RUNNING); 60303270634SSteven Haigh retval = -EINTR; 604f08812d5SPete Zaitcev goto exit_onqueue; 60503270634SSteven Haigh } 606f08812d5SPete Zaitcev if (schedule_timeout(COMMAND_TIMEOUT) == 0) { 607441b62c1SHarvey Harrison dbg(1, "%s - command timed out.", __func__); 608f08812d5SPete Zaitcev retval = -ETIMEDOUT; 609f08812d5SPete Zaitcev goto exit_onqueue; 610f08812d5SPete Zaitcev } 611f08812d5SPete Zaitcev remove_wait_queue(&dev->write_wait, &waita); 6128293c568SMatthias Kaehlcke retval = mutex_lock_interruptible(&dev->mtx); 613ebc3ac14SOliver Neukum if (retval) { 614ebc3ac14SOliver Neukum retval = bytes_written ? bytes_written : retval; 615ebc3ac14SOliver Neukum goto exit_nolock; 616ebc3ac14SOliver Neukum } 61703270634SSteven Haigh 618441b62c1SHarvey Harrison dbg(4, " %s : in progress, count = %Zd", __func__, count); 61903270634SSteven Haigh } else { 620f08812d5SPete Zaitcev spin_unlock_irqrestore(&dev->buflock, flags); 621f08812d5SPete Zaitcev set_current_state(TASK_RUNNING); 622f08812d5SPete Zaitcev remove_wait_queue(&dev->write_wait, &waita); 623441b62c1SHarvey Harrison dbg(4, " %s : sending, count = %Zd", __func__, count); 62403270634SSteven Haigh 62503270634SSteven Haigh /* write the data into interrupt_out_buffer from userspace */ 62629cc8897SKuninori Morimoto buffer_size = usb_endpoint_maxp(dev->interrupt_out_endpoint); 62703270634SSteven Haigh bytes_to_write = count > buffer_size ? buffer_size : count; 62803270634SSteven Haigh dbg(4, " %s : buffer_size = %Zd, count = %Zd, bytes_to_write = %Zd", 629441b62c1SHarvey Harrison __func__, buffer_size, count, bytes_to_write); 63003270634SSteven Haigh 63103270634SSteven Haigh if (copy_from_user(dev->interrupt_out_buffer, buffer, bytes_to_write) != 0) { 63203270634SSteven Haigh retval = -EFAULT; 63303270634SSteven Haigh goto exit; 63403270634SSteven Haigh } 63503270634SSteven Haigh 63603270634SSteven Haigh /* send off the urb */ 63703270634SSteven Haigh usb_fill_int_urb( 63803270634SSteven Haigh dev->interrupt_out_urb, 63903270634SSteven Haigh dev->udev, 64003270634SSteven Haigh usb_sndintpipe(dev->udev, dev->interrupt_out_endpoint->bEndpointAddress), 64103270634SSteven Haigh dev->interrupt_out_buffer, 64203270634SSteven Haigh bytes_to_write, 64303270634SSteven Haigh adu_interrupt_out_callback, 64403270634SSteven Haigh dev, 645f08812d5SPete Zaitcev dev->interrupt_out_endpoint->bInterval); 64603270634SSteven Haigh dev->interrupt_out_urb->actual_length = bytes_to_write; 647f08812d5SPete Zaitcev dev->out_urb_finished = 0; 64803270634SSteven Haigh retval = usb_submit_urb(dev->interrupt_out_urb, GFP_KERNEL); 64903270634SSteven Haigh if (retval < 0) { 650f08812d5SPete Zaitcev dev->out_urb_finished = 1; 651fd3f1917SGreg Kroah-Hartman dev_err(&dev->udev->dev, "Couldn't submit " 652fd3f1917SGreg Kroah-Hartman "interrupt_out_urb %d\n", retval); 65303270634SSteven Haigh goto exit; 65403270634SSteven Haigh } 65503270634SSteven Haigh 65603270634SSteven Haigh buffer += bytes_to_write; 65703270634SSteven Haigh count -= bytes_to_write; 65803270634SSteven Haigh 65903270634SSteven Haigh bytes_written += bytes_to_write; 66003270634SSteven Haigh } 66103270634SSteven Haigh } 662f08812d5SPete Zaitcev mutex_unlock(&dev->mtx); 663f08812d5SPete Zaitcev return bytes_written; 66403270634SSteven Haigh 66503270634SSteven Haigh exit: 6668293c568SMatthias Kaehlcke mutex_unlock(&dev->mtx); 667ebc3ac14SOliver Neukum exit_nolock: 668441b62c1SHarvey Harrison dbg(2, " %s : leave, return value %d", __func__, retval); 669f08812d5SPete Zaitcev return retval; 67003270634SSteven Haigh 671f08812d5SPete Zaitcev exit_onqueue: 672f08812d5SPete Zaitcev remove_wait_queue(&dev->write_wait, &waita); 67303270634SSteven Haigh return retval; 67403270634SSteven Haigh } 67503270634SSteven Haigh 67603270634SSteven Haigh /* file operations needed when we register this driver */ 67700977a59SArjan van de Ven static const struct file_operations adu_fops = { 67803270634SSteven Haigh .owner = THIS_MODULE, 67903270634SSteven Haigh .read = adu_read, 68003270634SSteven Haigh .write = adu_write, 68103270634SSteven Haigh .open = adu_open, 68203270634SSteven Haigh .release = adu_release, 6836038f373SArnd Bergmann .llseek = noop_llseek, 68403270634SSteven Haigh }; 68503270634SSteven Haigh 68603270634SSteven Haigh /* 68703270634SSteven Haigh * usb class driver info in order to get a minor number from the usb core, 68803270634SSteven Haigh * and to have the device registered with devfs and the driver core 68903270634SSteven Haigh */ 69003270634SSteven Haigh static struct usb_class_driver adu_class = { 69103270634SSteven Haigh .name = "usb/adutux%d", 69203270634SSteven Haigh .fops = &adu_fops, 69303270634SSteven Haigh .minor_base = ADU_MINOR_BASE, 69403270634SSteven Haigh }; 69503270634SSteven Haigh 69603270634SSteven Haigh /** 69703270634SSteven Haigh * adu_probe 69803270634SSteven Haigh * 69903270634SSteven Haigh * Called by the usb core when a new device is connected that it thinks 70003270634SSteven Haigh * this driver might be interested in. 70103270634SSteven Haigh */ 70203270634SSteven Haigh static int adu_probe(struct usb_interface *interface, 70303270634SSteven Haigh const struct usb_device_id *id) 70403270634SSteven Haigh { 70503270634SSteven Haigh struct usb_device *udev = interface_to_usbdev(interface); 70603270634SSteven Haigh struct adu_device *dev = NULL; 70703270634SSteven Haigh struct usb_host_interface *iface_desc; 70803270634SSteven Haigh struct usb_endpoint_descriptor *endpoint; 70903270634SSteven Haigh int retval = -ENODEV; 71003270634SSteven Haigh int in_end_size; 71103270634SSteven Haigh int out_end_size; 71203270634SSteven Haigh int i; 71303270634SSteven Haigh 714441b62c1SHarvey Harrison dbg(2, " %s : enter", __func__); 71503270634SSteven Haigh 71603270634SSteven Haigh if (udev == NULL) { 71703270634SSteven Haigh dev_err(&interface->dev, "udev is NULL.\n"); 71803270634SSteven Haigh goto exit; 71903270634SSteven Haigh } 72003270634SSteven Haigh 721b595076aSUwe Kleine-König /* allocate memory for our device state and initialize it */ 72203270634SSteven Haigh dev = kzalloc(sizeof(struct adu_device), GFP_KERNEL); 72303270634SSteven Haigh if (dev == NULL) { 72403270634SSteven Haigh dev_err(&interface->dev, "Out of memory\n"); 72503270634SSteven Haigh retval = -ENOMEM; 72603270634SSteven Haigh goto exit; 72703270634SSteven Haigh } 72803270634SSteven Haigh 7298293c568SMatthias Kaehlcke mutex_init(&dev->mtx); 73003270634SSteven Haigh spin_lock_init(&dev->buflock); 73103270634SSteven Haigh dev->udev = udev; 73203270634SSteven Haigh init_waitqueue_head(&dev->read_wait); 73303270634SSteven Haigh init_waitqueue_head(&dev->write_wait); 73403270634SSteven Haigh 73503270634SSteven Haigh iface_desc = &interface->altsetting[0]; 73603270634SSteven Haigh 73703270634SSteven Haigh /* set up the endpoint information */ 73803270634SSteven Haigh for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) { 73903270634SSteven Haigh endpoint = &iface_desc->endpoint[i].desc; 74003270634SSteven Haigh 74103270634SSteven Haigh if (usb_endpoint_is_int_in(endpoint)) 74203270634SSteven Haigh dev->interrupt_in_endpoint = endpoint; 74303270634SSteven Haigh 74403270634SSteven Haigh if (usb_endpoint_is_int_out(endpoint)) 74503270634SSteven Haigh dev->interrupt_out_endpoint = endpoint; 74603270634SSteven Haigh } 74703270634SSteven Haigh if (dev->interrupt_in_endpoint == NULL) { 74803270634SSteven Haigh dev_err(&interface->dev, "interrupt in endpoint not found\n"); 74903270634SSteven Haigh goto error; 75003270634SSteven Haigh } 75103270634SSteven Haigh if (dev->interrupt_out_endpoint == NULL) { 75203270634SSteven Haigh dev_err(&interface->dev, "interrupt out endpoint not found\n"); 75303270634SSteven Haigh goto error; 75403270634SSteven Haigh } 75503270634SSteven Haigh 75629cc8897SKuninori Morimoto in_end_size = usb_endpoint_maxp(dev->interrupt_in_endpoint); 75729cc8897SKuninori Morimoto out_end_size = usb_endpoint_maxp(dev->interrupt_out_endpoint); 75803270634SSteven Haigh 75903270634SSteven Haigh dev->read_buffer_primary = kmalloc((4 * in_end_size), GFP_KERNEL); 76003270634SSteven Haigh if (!dev->read_buffer_primary) { 76103270634SSteven Haigh dev_err(&interface->dev, "Couldn't allocate read_buffer_primary\n"); 76203270634SSteven Haigh retval = -ENOMEM; 76303270634SSteven Haigh goto error; 76403270634SSteven Haigh } 76503270634SSteven Haigh 76603270634SSteven Haigh /* debug code prime the buffer */ 76703270634SSteven Haigh memset(dev->read_buffer_primary, 'a', in_end_size); 76803270634SSteven Haigh memset(dev->read_buffer_primary + in_end_size, 'b', in_end_size); 76903270634SSteven Haigh memset(dev->read_buffer_primary + (2 * in_end_size), 'c', in_end_size); 77003270634SSteven Haigh memset(dev->read_buffer_primary + (3 * in_end_size), 'd', in_end_size); 77103270634SSteven Haigh 77203270634SSteven Haigh dev->read_buffer_secondary = kmalloc((4 * in_end_size), GFP_KERNEL); 77303270634SSteven Haigh if (!dev->read_buffer_secondary) { 77403270634SSteven Haigh dev_err(&interface->dev, "Couldn't allocate read_buffer_secondary\n"); 77503270634SSteven Haigh retval = -ENOMEM; 77603270634SSteven Haigh goto error; 77703270634SSteven Haigh } 77803270634SSteven Haigh 77903270634SSteven Haigh /* debug code prime the buffer */ 78003270634SSteven Haigh memset(dev->read_buffer_secondary, 'e', in_end_size); 78103270634SSteven Haigh memset(dev->read_buffer_secondary + in_end_size, 'f', in_end_size); 78203270634SSteven Haigh memset(dev->read_buffer_secondary + (2 * in_end_size), 'g', in_end_size); 78303270634SSteven Haigh memset(dev->read_buffer_secondary + (3 * in_end_size), 'h', in_end_size); 78403270634SSteven Haigh 78503270634SSteven Haigh dev->interrupt_in_buffer = kmalloc(in_end_size, GFP_KERNEL); 78603270634SSteven Haigh if (!dev->interrupt_in_buffer) { 78703270634SSteven Haigh dev_err(&interface->dev, "Couldn't allocate interrupt_in_buffer\n"); 78803270634SSteven Haigh goto error; 78903270634SSteven Haigh } 79003270634SSteven Haigh 79103270634SSteven Haigh /* debug code prime the buffer */ 79203270634SSteven Haigh memset(dev->interrupt_in_buffer, 'i', in_end_size); 79303270634SSteven Haigh 79403270634SSteven Haigh dev->interrupt_in_urb = usb_alloc_urb(0, GFP_KERNEL); 79503270634SSteven Haigh if (!dev->interrupt_in_urb) { 79603270634SSteven Haigh dev_err(&interface->dev, "Couldn't allocate interrupt_in_urb\n"); 79703270634SSteven Haigh goto error; 79803270634SSteven Haigh } 79903270634SSteven Haigh dev->interrupt_out_buffer = kmalloc(out_end_size, GFP_KERNEL); 80003270634SSteven Haigh if (!dev->interrupt_out_buffer) { 80103270634SSteven Haigh dev_err(&interface->dev, "Couldn't allocate interrupt_out_buffer\n"); 80203270634SSteven Haigh goto error; 80303270634SSteven Haigh } 80403270634SSteven Haigh dev->interrupt_out_urb = usb_alloc_urb(0, GFP_KERNEL); 80503270634SSteven Haigh if (!dev->interrupt_out_urb) { 80603270634SSteven Haigh dev_err(&interface->dev, "Couldn't allocate interrupt_out_urb\n"); 80703270634SSteven Haigh goto error; 80803270634SSteven Haigh } 80903270634SSteven Haigh 81003270634SSteven Haigh if (!usb_string(udev, udev->descriptor.iSerialNumber, dev->serial_number, 81103270634SSteven Haigh sizeof(dev->serial_number))) { 81203270634SSteven Haigh dev_err(&interface->dev, "Could not retrieve serial number\n"); 81303270634SSteven Haigh goto error; 81403270634SSteven Haigh } 815441b62c1SHarvey Harrison dbg(2, " %s : serial_number=%s", __func__, dev->serial_number); 81603270634SSteven Haigh 81703270634SSteven Haigh /* we can register the device now, as it is ready */ 81803270634SSteven Haigh usb_set_intfdata(interface, dev); 81903270634SSteven Haigh 82003270634SSteven Haigh retval = usb_register_dev(interface, &adu_class); 82103270634SSteven Haigh 82203270634SSteven Haigh if (retval) { 82303270634SSteven Haigh /* something prevented us from registering this driver */ 82403270634SSteven Haigh dev_err(&interface->dev, "Not able to get a minor for this device.\n"); 82503270634SSteven Haigh usb_set_intfdata(interface, NULL); 82603270634SSteven Haigh goto error; 82703270634SSteven Haigh } 82803270634SSteven Haigh 82903270634SSteven Haigh dev->minor = interface->minor; 83003270634SSteven Haigh 83103270634SSteven Haigh /* let the user know what node this device is now attached to */ 832898eb71cSJoe Perches dev_info(&interface->dev, "ADU%d %s now attached to /dev/usb/adutux%d\n", 83303270634SSteven Haigh udev->descriptor.idProduct, dev->serial_number, 83403270634SSteven Haigh (dev->minor - ADU_MINOR_BASE)); 83503270634SSteven Haigh exit: 836441b62c1SHarvey Harrison dbg(2, " %s : leave, return value %p (dev)", __func__, dev); 83703270634SSteven Haigh 83803270634SSteven Haigh return retval; 83903270634SSteven Haigh 84003270634SSteven Haigh error: 84103270634SSteven Haigh adu_delete(dev); 84203270634SSteven Haigh return retval; 84303270634SSteven Haigh } 84403270634SSteven Haigh 84503270634SSteven Haigh /** 84603270634SSteven Haigh * adu_disconnect 84703270634SSteven Haigh * 84803270634SSteven Haigh * Called by the usb core when the device is removed from the system. 84903270634SSteven Haigh */ 85003270634SSteven Haigh static void adu_disconnect(struct usb_interface *interface) 85103270634SSteven Haigh { 85203270634SSteven Haigh struct adu_device *dev; 85303270634SSteven Haigh int minor; 85403270634SSteven Haigh 855441b62c1SHarvey Harrison dbg(2, " %s : enter", __func__); 85603270634SSteven Haigh 85703270634SSteven Haigh dev = usb_get_intfdata(interface); 85803270634SSteven Haigh 8598293c568SMatthias Kaehlcke mutex_lock(&dev->mtx); /* not interruptible */ 860f08812d5SPete Zaitcev dev->udev = NULL; /* poison */ 861f08812d5SPete Zaitcev minor = dev->minor; 862f08812d5SPete Zaitcev usb_deregister_dev(interface, &adu_class); 863f08812d5SPete Zaitcev mutex_unlock(&dev->mtx); 864f08812d5SPete Zaitcev 865f08812d5SPete Zaitcev mutex_lock(&adutux_mutex); 866f08812d5SPete Zaitcev usb_set_intfdata(interface, NULL); 867d4ead16fSAlan Stern 86803270634SSteven Haigh /* if the device is not opened, then we clean up right now */ 869441b62c1SHarvey Harrison dbg(2, " %s : open count %d", __func__, dev->open_count); 870f08812d5SPete Zaitcev if (!dev->open_count) 87103270634SSteven Haigh adu_delete(dev); 872f08812d5SPete Zaitcev 873f08812d5SPete Zaitcev mutex_unlock(&adutux_mutex); 87403270634SSteven Haigh 875898eb71cSJoe Perches dev_info(&interface->dev, "ADU device adutux%d now disconnected\n", 87603270634SSteven Haigh (minor - ADU_MINOR_BASE)); 87703270634SSteven Haigh 878441b62c1SHarvey Harrison dbg(2, " %s : leave", __func__); 87903270634SSteven Haigh } 88003270634SSteven Haigh 88103270634SSteven Haigh /* usb specific object needed to register this driver with the usb subsystem */ 88203270634SSteven Haigh static struct usb_driver adu_driver = { 88303270634SSteven Haigh .name = "adutux", 88403270634SSteven Haigh .probe = adu_probe, 88503270634SSteven Haigh .disconnect = adu_disconnect, 88603270634SSteven Haigh .id_table = device_table, 88703270634SSteven Haigh }; 88803270634SSteven Haigh 88965db4305SGreg Kroah-Hartman module_usb_driver(adu_driver); 89003270634SSteven Haigh 89103270634SSteven Haigh MODULE_AUTHOR(DRIVER_AUTHOR); 89203270634SSteven Haigh MODULE_DESCRIPTION(DRIVER_DESC); 89303270634SSteven Haigh MODULE_LICENSE("GPL"); 894