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 279f08812d5SPete Zaitcev if ((retval = mutex_lock_interruptible(&adutux_mutex))) { 280441b62c1SHarvey 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) { 286fd3f1917SGreg Kroah-Hartman printk(KERN_ERR "adutux: %s - error, can't find device for " 287fd3f1917SGreg Kroah-Hartman "minor %d\n", __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; 305441b62c1SHarvey 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, 31829cc8897SKuninori Morimoto usb_endpoint_maxp(dev->interrupt_in_endpoint), 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: 335441b62c1SHarvey 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 { 341441b62c1SHarvey Harrison dbg(2," %s : enter", __func__); 34203270634SSteven Haigh 34303270634SSteven Haigh /* decrement our usage count for the device */ 34403270634SSteven Haigh --dev->open_count; 345441b62c1SHarvey 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 351441b62c1SHarvey 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 359441b62c1SHarvey Harrison dbg(2," %s : enter", __func__); 36003270634SSteven Haigh 36103270634SSteven Haigh if (file == NULL) { 362441b62c1SHarvey 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) { 369441b62c1SHarvey 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) { 377441b62c1SHarvey Harrison dbg(1," %s : device not opened", __func__); 37803270634SSteven Haigh retval = -ENODEV; 37946c9844cSJiri Slaby goto unlock; 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 } 38846c9844cSJiri Slaby unlock: 389f08812d5SPete Zaitcev mutex_unlock(&adutux_mutex); 39046c9844cSJiri Slaby exit: 391441b62c1SHarvey 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 408441b62c1SHarvey Harrison dbg(2," %s : enter, count = %Zd, file=%p", __func__, count, file); 40903270634SSteven Haigh 41003270634SSteven Haigh dev = file->private_data; 411441b62c1SHarvey 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; 419fd3f1917SGreg Kroah-Hartman printk(KERN_ERR "adutux: No device or device unplugged %d\n", 420fd3f1917SGreg Kroah-Hartman retval); 42103270634SSteven Haigh goto exit; 42203270634SSteven Haigh } 42303270634SSteven Haigh 42403270634SSteven Haigh /* verify that some data was requested */ 42503270634SSteven Haigh if (count == 0) { 426441b62c1SHarvey Harrison dbg(1," %s : read request of 0 bytes", __func__); 42703270634SSteven Haigh goto exit; 42803270634SSteven Haigh } 42903270634SSteven Haigh 43003270634SSteven Haigh timeout = COMMAND_TIMEOUT; 431441b62c1SHarvey Harrison dbg(2," %s : about to start looping", __func__); 43203270634SSteven Haigh while (bytes_to_read) { 43303270634SSteven Haigh int data_in_secondary = dev->secondary_tail - dev->secondary_head; 43403270634SSteven Haigh dbg(2," %s : while, data_in_secondary=%d, status=%d", 435441b62c1SHarvey Harrison __func__, data_in_secondary, 43603270634SSteven Haigh dev->interrupt_in_urb->status); 43703270634SSteven Haigh 43803270634SSteven Haigh if (data_in_secondary) { 43903270634SSteven Haigh /* drain secondary buffer */ 44003270634SSteven Haigh int amount = bytes_to_read < data_in_secondary ? bytes_to_read : data_in_secondary; 44103270634SSteven Haigh i = copy_to_user(buffer, dev->read_buffer_secondary+dev->secondary_head, amount); 4421865a9c3SKulikov Vasiliy if (i) { 44303270634SSteven Haigh retval = -EFAULT; 44403270634SSteven Haigh goto exit; 44503270634SSteven Haigh } 44603270634SSteven Haigh dev->secondary_head += (amount - i); 44703270634SSteven Haigh bytes_read += (amount - i); 44803270634SSteven Haigh bytes_to_read -= (amount - i); 44903270634SSteven Haigh if (i) { 45003270634SSteven Haigh retval = bytes_read ? bytes_read : -EFAULT; 45103270634SSteven Haigh goto exit; 45203270634SSteven Haigh } 45303270634SSteven Haigh } else { 45403270634SSteven Haigh /* we check the primary buffer */ 45503270634SSteven Haigh spin_lock_irqsave (&dev->buflock, flags); 45603270634SSteven Haigh if (dev->read_buffer_length) { 45703270634SSteven Haigh /* we secure access to the primary */ 45803270634SSteven Haigh char *tmp; 45903270634SSteven Haigh dbg(2," %s : swap, read_buffer_length = %d", 460441b62c1SHarvey Harrison __func__, dev->read_buffer_length); 46103270634SSteven Haigh tmp = dev->read_buffer_secondary; 46203270634SSteven Haigh dev->read_buffer_secondary = dev->read_buffer_primary; 46303270634SSteven Haigh dev->read_buffer_primary = tmp; 46403270634SSteven Haigh dev->secondary_head = 0; 46503270634SSteven Haigh dev->secondary_tail = dev->read_buffer_length; 46603270634SSteven Haigh dev->read_buffer_length = 0; 46703270634SSteven Haigh spin_unlock_irqrestore(&dev->buflock, flags); 46803270634SSteven Haigh /* we have a free buffer so use it */ 46903270634SSteven Haigh should_submit = 1; 47003270634SSteven Haigh } else { 47103270634SSteven Haigh /* even the primary was empty - we may need to do IO */ 472f08812d5SPete Zaitcev if (!dev->read_urb_finished) { 47303270634SSteven Haigh /* somebody is doing IO */ 47403270634SSteven Haigh spin_unlock_irqrestore(&dev->buflock, flags); 475441b62c1SHarvey Harrison dbg(2," %s : submitted already", __func__); 47603270634SSteven Haigh } else { 47703270634SSteven Haigh /* we must initiate input */ 478441b62c1SHarvey Harrison dbg(2," %s : initiate input", __func__); 47903270634SSteven Haigh dev->read_urb_finished = 0; 480f08812d5SPete Zaitcev spin_unlock_irqrestore(&dev->buflock, flags); 48103270634SSteven Haigh 48203270634SSteven Haigh usb_fill_int_urb(dev->interrupt_in_urb,dev->udev, 48303270634SSteven Haigh usb_rcvintpipe(dev->udev, 48403270634SSteven Haigh dev->interrupt_in_endpoint->bEndpointAddress), 48503270634SSteven Haigh dev->interrupt_in_buffer, 48629cc8897SKuninori Morimoto usb_endpoint_maxp(dev->interrupt_in_endpoint), 48703270634SSteven Haigh adu_interrupt_in_callback, 48803270634SSteven Haigh dev, 48903270634SSteven Haigh dev->interrupt_in_endpoint->bInterval); 490f08812d5SPete Zaitcev retval = usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL); 491f08812d5SPete Zaitcev if (retval) { 492f08812d5SPete Zaitcev dev->read_urb_finished = 1; 49303270634SSteven Haigh if (retval == -ENOMEM) { 49403270634SSteven Haigh retval = bytes_read ? bytes_read : -ENOMEM; 49503270634SSteven Haigh } 496441b62c1SHarvey Harrison dbg(2," %s : submit failed", __func__); 49703270634SSteven Haigh goto exit; 49803270634SSteven Haigh } 49903270634SSteven Haigh } 50003270634SSteven Haigh 50103270634SSteven Haigh /* we wait for I/O to complete */ 50203270634SSteven Haigh set_current_state(TASK_INTERRUPTIBLE); 50303270634SSteven Haigh add_wait_queue(&dev->read_wait, &wait); 504f08812d5SPete Zaitcev spin_lock_irqsave(&dev->buflock, flags); 505f08812d5SPete Zaitcev if (!dev->read_urb_finished) { 506f08812d5SPete Zaitcev spin_unlock_irqrestore(&dev->buflock, flags); 50703270634SSteven Haigh timeout = schedule_timeout(COMMAND_TIMEOUT); 508f08812d5SPete Zaitcev } else { 509f08812d5SPete Zaitcev spin_unlock_irqrestore(&dev->buflock, flags); 51003270634SSteven Haigh set_current_state(TASK_RUNNING); 511f08812d5SPete Zaitcev } 51203270634SSteven Haigh remove_wait_queue(&dev->read_wait, &wait); 51303270634SSteven Haigh 51403270634SSteven Haigh if (timeout <= 0) { 515441b62c1SHarvey Harrison dbg(2," %s : timeout", __func__); 51603270634SSteven Haigh retval = bytes_read ? bytes_read : -ETIMEDOUT; 51703270634SSteven Haigh goto exit; 51803270634SSteven Haigh } 51903270634SSteven Haigh 52003270634SSteven Haigh if (signal_pending(current)) { 521441b62c1SHarvey Harrison dbg(2," %s : signal pending", __func__); 52203270634SSteven Haigh retval = bytes_read ? bytes_read : -EINTR; 52303270634SSteven Haigh goto exit; 52403270634SSteven Haigh } 52503270634SSteven Haigh } 52603270634SSteven Haigh } 52703270634SSteven Haigh } 52803270634SSteven Haigh 52903270634SSteven Haigh retval = bytes_read; 53003270634SSteven Haigh /* if the primary buffer is empty then use it */ 531f08812d5SPete Zaitcev spin_lock_irqsave(&dev->buflock, flags); 532f08812d5SPete Zaitcev if (should_submit && dev->read_urb_finished) { 533f08812d5SPete Zaitcev dev->read_urb_finished = 0; 534f08812d5SPete Zaitcev spin_unlock_irqrestore(&dev->buflock, flags); 53503270634SSteven Haigh usb_fill_int_urb(dev->interrupt_in_urb,dev->udev, 53603270634SSteven Haigh usb_rcvintpipe(dev->udev, 53703270634SSteven Haigh dev->interrupt_in_endpoint->bEndpointAddress), 53803270634SSteven Haigh dev->interrupt_in_buffer, 53929cc8897SKuninori Morimoto usb_endpoint_maxp(dev->interrupt_in_endpoint), 54003270634SSteven Haigh adu_interrupt_in_callback, 54103270634SSteven Haigh dev, 54203270634SSteven Haigh dev->interrupt_in_endpoint->bInterval); 543f08812d5SPete Zaitcev if (usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL) != 0) 544f08812d5SPete Zaitcev dev->read_urb_finished = 1; 54503270634SSteven Haigh /* we ignore failure */ 546f08812d5SPete Zaitcev } else { 547f08812d5SPete Zaitcev spin_unlock_irqrestore(&dev->buflock, flags); 54803270634SSteven Haigh } 54903270634SSteven Haigh 55003270634SSteven Haigh exit: 55103270634SSteven Haigh /* unlock the device */ 5528293c568SMatthias Kaehlcke mutex_unlock(&dev->mtx); 55303270634SSteven Haigh 554441b62c1SHarvey Harrison dbg(2," %s : leave, return value %d", __func__, retval); 55503270634SSteven Haigh return retval; 55603270634SSteven Haigh } 55703270634SSteven Haigh 55803270634SSteven Haigh static ssize_t adu_write(struct file *file, const __user char *buffer, 55903270634SSteven Haigh size_t count, loff_t *ppos) 56003270634SSteven Haigh { 561f08812d5SPete Zaitcev DECLARE_WAITQUEUE(waita, current); 56203270634SSteven Haigh struct adu_device *dev; 56303270634SSteven Haigh size_t bytes_written = 0; 56403270634SSteven Haigh size_t bytes_to_write; 56503270634SSteven Haigh size_t buffer_size; 566f08812d5SPete Zaitcev unsigned long flags; 567ebc3ac14SOliver Neukum int retval; 56803270634SSteven Haigh 569441b62c1SHarvey Harrison dbg(2," %s : enter, count = %Zd", __func__, count); 57003270634SSteven Haigh 57103270634SSteven Haigh dev = file->private_data; 57203270634SSteven Haigh 5738293c568SMatthias Kaehlcke retval = mutex_lock_interruptible(&dev->mtx); 574ebc3ac14SOliver Neukum if (retval) 575ebc3ac14SOliver Neukum goto exit_nolock; 57603270634SSteven Haigh 57703270634SSteven Haigh /* verify that the device wasn't unplugged */ 578f08812d5SPete Zaitcev if (dev->udev == NULL) { 57903270634SSteven Haigh retval = -ENODEV; 580fd3f1917SGreg Kroah-Hartman printk(KERN_ERR "adutux: No device or device unplugged %d\n", 581fd3f1917SGreg Kroah-Hartman retval); 58203270634SSteven Haigh goto exit; 58303270634SSteven Haigh } 58403270634SSteven Haigh 58503270634SSteven Haigh /* verify that we actually have some data to write */ 58603270634SSteven Haigh if (count == 0) { 587441b62c1SHarvey Harrison dbg(1," %s : write request of 0 bytes", __func__); 58803270634SSteven Haigh goto exit; 58903270634SSteven Haigh } 59003270634SSteven Haigh 59103270634SSteven Haigh while (count > 0) { 592f08812d5SPete Zaitcev add_wait_queue(&dev->write_wait, &waita); 593f08812d5SPete Zaitcev set_current_state(TASK_INTERRUPTIBLE); 594f08812d5SPete Zaitcev spin_lock_irqsave(&dev->buflock, flags); 595f08812d5SPete Zaitcev if (!dev->out_urb_finished) { 596f08812d5SPete Zaitcev spin_unlock_irqrestore(&dev->buflock, flags); 59703270634SSteven Haigh 598f08812d5SPete Zaitcev mutex_unlock(&dev->mtx); 59903270634SSteven Haigh if (signal_pending(current)) { 600441b62c1SHarvey Harrison dbg(1," %s : interrupted", __func__); 601f08812d5SPete Zaitcev set_current_state(TASK_RUNNING); 60203270634SSteven Haigh retval = -EINTR; 603f08812d5SPete Zaitcev goto exit_onqueue; 60403270634SSteven Haigh } 605f08812d5SPete Zaitcev if (schedule_timeout(COMMAND_TIMEOUT) == 0) { 606441b62c1SHarvey Harrison dbg(1, "%s - command timed out.", __func__); 607f08812d5SPete Zaitcev retval = -ETIMEDOUT; 608f08812d5SPete Zaitcev goto exit_onqueue; 609f08812d5SPete Zaitcev } 610f08812d5SPete Zaitcev remove_wait_queue(&dev->write_wait, &waita); 6118293c568SMatthias Kaehlcke retval = mutex_lock_interruptible(&dev->mtx); 612ebc3ac14SOliver Neukum if (retval) { 613ebc3ac14SOliver Neukum retval = bytes_written ? bytes_written : retval; 614ebc3ac14SOliver Neukum goto exit_nolock; 615ebc3ac14SOliver Neukum } 61603270634SSteven Haigh 617441b62c1SHarvey Harrison dbg(4," %s : in progress, count = %Zd", __func__, count); 61803270634SSteven Haigh } else { 619f08812d5SPete Zaitcev spin_unlock_irqrestore(&dev->buflock, flags); 620f08812d5SPete Zaitcev set_current_state(TASK_RUNNING); 621f08812d5SPete Zaitcev remove_wait_queue(&dev->write_wait, &waita); 622441b62c1SHarvey Harrison dbg(4," %s : sending, count = %Zd", __func__, count); 62303270634SSteven Haigh 62403270634SSteven Haigh /* write the data into interrupt_out_buffer from userspace */ 62529cc8897SKuninori Morimoto buffer_size = usb_endpoint_maxp(dev->interrupt_out_endpoint); 62603270634SSteven Haigh bytes_to_write = count > buffer_size ? buffer_size : count; 62703270634SSteven Haigh dbg(4," %s : buffer_size = %Zd, count = %Zd, bytes_to_write = %Zd", 628441b62c1SHarvey Harrison __func__, buffer_size, count, bytes_to_write); 62903270634SSteven Haigh 63003270634SSteven Haigh if (copy_from_user(dev->interrupt_out_buffer, buffer, bytes_to_write) != 0) { 63103270634SSteven Haigh retval = -EFAULT; 63203270634SSteven Haigh goto exit; 63303270634SSteven Haigh } 63403270634SSteven Haigh 63503270634SSteven Haigh /* send off the urb */ 63603270634SSteven Haigh usb_fill_int_urb( 63703270634SSteven Haigh dev->interrupt_out_urb, 63803270634SSteven Haigh dev->udev, 63903270634SSteven Haigh usb_sndintpipe(dev->udev, dev->interrupt_out_endpoint->bEndpointAddress), 64003270634SSteven Haigh dev->interrupt_out_buffer, 64103270634SSteven Haigh bytes_to_write, 64203270634SSteven Haigh adu_interrupt_out_callback, 64303270634SSteven Haigh dev, 644f08812d5SPete Zaitcev dev->interrupt_out_endpoint->bInterval); 64503270634SSteven Haigh dev->interrupt_out_urb->actual_length = bytes_to_write; 646f08812d5SPete Zaitcev dev->out_urb_finished = 0; 64703270634SSteven Haigh retval = usb_submit_urb(dev->interrupt_out_urb, GFP_KERNEL); 64803270634SSteven Haigh if (retval < 0) { 649f08812d5SPete Zaitcev dev->out_urb_finished = 1; 650fd3f1917SGreg Kroah-Hartman dev_err(&dev->udev->dev, "Couldn't submit " 651fd3f1917SGreg Kroah-Hartman "interrupt_out_urb %d\n", retval); 65203270634SSteven Haigh goto exit; 65303270634SSteven Haigh } 65403270634SSteven Haigh 65503270634SSteven Haigh buffer += bytes_to_write; 65603270634SSteven Haigh count -= bytes_to_write; 65703270634SSteven Haigh 65803270634SSteven Haigh bytes_written += bytes_to_write; 65903270634SSteven Haigh } 66003270634SSteven Haigh } 661f08812d5SPete Zaitcev mutex_unlock(&dev->mtx); 662f08812d5SPete Zaitcev return bytes_written; 66303270634SSteven Haigh 66403270634SSteven Haigh exit: 6658293c568SMatthias Kaehlcke mutex_unlock(&dev->mtx); 666ebc3ac14SOliver Neukum exit_nolock: 667441b62c1SHarvey Harrison dbg(2," %s : leave, return value %d", __func__, retval); 668f08812d5SPete Zaitcev return retval; 66903270634SSteven Haigh 670f08812d5SPete Zaitcev exit_onqueue: 671f08812d5SPete Zaitcev remove_wait_queue(&dev->write_wait, &waita); 67203270634SSteven Haigh return retval; 67303270634SSteven Haigh } 67403270634SSteven Haigh 67503270634SSteven Haigh /* file operations needed when we register this driver */ 67600977a59SArjan van de Ven static const struct file_operations adu_fops = { 67703270634SSteven Haigh .owner = THIS_MODULE, 67803270634SSteven Haigh .read = adu_read, 67903270634SSteven Haigh .write = adu_write, 68003270634SSteven Haigh .open = adu_open, 68103270634SSteven Haigh .release = adu_release, 6826038f373SArnd Bergmann .llseek = noop_llseek, 68303270634SSteven Haigh }; 68403270634SSteven Haigh 68503270634SSteven Haigh /* 68603270634SSteven Haigh * usb class driver info in order to get a minor number from the usb core, 68703270634SSteven Haigh * and to have the device registered with devfs and the driver core 68803270634SSteven Haigh */ 68903270634SSteven Haigh static struct usb_class_driver adu_class = { 69003270634SSteven Haigh .name = "usb/adutux%d", 69103270634SSteven Haigh .fops = &adu_fops, 69203270634SSteven Haigh .minor_base = ADU_MINOR_BASE, 69303270634SSteven Haigh }; 69403270634SSteven Haigh 69503270634SSteven Haigh /** 69603270634SSteven Haigh * adu_probe 69703270634SSteven Haigh * 69803270634SSteven Haigh * Called by the usb core when a new device is connected that it thinks 69903270634SSteven Haigh * this driver might be interested in. 70003270634SSteven Haigh */ 70103270634SSteven Haigh static int adu_probe(struct usb_interface *interface, 70203270634SSteven Haigh const struct usb_device_id *id) 70303270634SSteven Haigh { 70403270634SSteven Haigh struct usb_device *udev = interface_to_usbdev(interface); 70503270634SSteven Haigh struct adu_device *dev = NULL; 70603270634SSteven Haigh struct usb_host_interface *iface_desc; 70703270634SSteven Haigh struct usb_endpoint_descriptor *endpoint; 70803270634SSteven Haigh int retval = -ENODEV; 70903270634SSteven Haigh int in_end_size; 71003270634SSteven Haigh int out_end_size; 71103270634SSteven Haigh int i; 71203270634SSteven Haigh 713441b62c1SHarvey Harrison dbg(2," %s : enter", __func__); 71403270634SSteven Haigh 71503270634SSteven Haigh if (udev == NULL) { 71603270634SSteven Haigh dev_err(&interface->dev, "udev is NULL.\n"); 71703270634SSteven Haigh goto exit; 71803270634SSteven Haigh } 71903270634SSteven Haigh 720b595076aSUwe Kleine-König /* allocate memory for our device state and initialize it */ 72103270634SSteven Haigh dev = kzalloc(sizeof(struct adu_device), GFP_KERNEL); 72203270634SSteven Haigh if (dev == NULL) { 72303270634SSteven Haigh dev_err(&interface->dev, "Out of memory\n"); 72403270634SSteven Haigh retval = -ENOMEM; 72503270634SSteven Haigh goto exit; 72603270634SSteven Haigh } 72703270634SSteven Haigh 7288293c568SMatthias Kaehlcke mutex_init(&dev->mtx); 72903270634SSteven Haigh spin_lock_init(&dev->buflock); 73003270634SSteven Haigh dev->udev = udev; 73103270634SSteven Haigh init_waitqueue_head(&dev->read_wait); 73203270634SSteven Haigh init_waitqueue_head(&dev->write_wait); 73303270634SSteven Haigh 73403270634SSteven Haigh iface_desc = &interface->altsetting[0]; 73503270634SSteven Haigh 73603270634SSteven Haigh /* set up the endpoint information */ 73703270634SSteven Haigh for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) { 73803270634SSteven Haigh endpoint = &iface_desc->endpoint[i].desc; 73903270634SSteven Haigh 74003270634SSteven Haigh if (usb_endpoint_is_int_in(endpoint)) 74103270634SSteven Haigh dev->interrupt_in_endpoint = endpoint; 74203270634SSteven Haigh 74303270634SSteven Haigh if (usb_endpoint_is_int_out(endpoint)) 74403270634SSteven Haigh dev->interrupt_out_endpoint = endpoint; 74503270634SSteven Haigh } 74603270634SSteven Haigh if (dev->interrupt_in_endpoint == NULL) { 74703270634SSteven Haigh dev_err(&interface->dev, "interrupt in endpoint not found\n"); 74803270634SSteven Haigh goto error; 74903270634SSteven Haigh } 75003270634SSteven Haigh if (dev->interrupt_out_endpoint == NULL) { 75103270634SSteven Haigh dev_err(&interface->dev, "interrupt out endpoint not found\n"); 75203270634SSteven Haigh goto error; 75303270634SSteven Haigh } 75403270634SSteven Haigh 75529cc8897SKuninori Morimoto in_end_size = usb_endpoint_maxp(dev->interrupt_in_endpoint); 75629cc8897SKuninori Morimoto out_end_size = usb_endpoint_maxp(dev->interrupt_out_endpoint); 75703270634SSteven Haigh 75803270634SSteven Haigh dev->read_buffer_primary = kmalloc((4 * in_end_size), GFP_KERNEL); 75903270634SSteven Haigh if (!dev->read_buffer_primary) { 76003270634SSteven Haigh dev_err(&interface->dev, "Couldn't allocate read_buffer_primary\n"); 76103270634SSteven Haigh retval = -ENOMEM; 76203270634SSteven Haigh goto error; 76303270634SSteven Haigh } 76403270634SSteven Haigh 76503270634SSteven Haigh /* debug code prime the buffer */ 76603270634SSteven Haigh memset(dev->read_buffer_primary, 'a', in_end_size); 76703270634SSteven Haigh memset(dev->read_buffer_primary + in_end_size, 'b', in_end_size); 76803270634SSteven Haigh memset(dev->read_buffer_primary + (2 * in_end_size), 'c', in_end_size); 76903270634SSteven Haigh memset(dev->read_buffer_primary + (3 * in_end_size), 'd', in_end_size); 77003270634SSteven Haigh 77103270634SSteven Haigh dev->read_buffer_secondary = kmalloc((4 * in_end_size), GFP_KERNEL); 77203270634SSteven Haigh if (!dev->read_buffer_secondary) { 77303270634SSteven Haigh dev_err(&interface->dev, "Couldn't allocate read_buffer_secondary\n"); 77403270634SSteven Haigh retval = -ENOMEM; 77503270634SSteven Haigh goto error; 77603270634SSteven Haigh } 77703270634SSteven Haigh 77803270634SSteven Haigh /* debug code prime the buffer */ 77903270634SSteven Haigh memset(dev->read_buffer_secondary, 'e', in_end_size); 78003270634SSteven Haigh memset(dev->read_buffer_secondary + in_end_size, 'f', in_end_size); 78103270634SSteven Haigh memset(dev->read_buffer_secondary + (2 * in_end_size), 'g', in_end_size); 78203270634SSteven Haigh memset(dev->read_buffer_secondary + (3 * in_end_size), 'h', in_end_size); 78303270634SSteven Haigh 78403270634SSteven Haigh dev->interrupt_in_buffer = kmalloc(in_end_size, GFP_KERNEL); 78503270634SSteven Haigh if (!dev->interrupt_in_buffer) { 78603270634SSteven Haigh dev_err(&interface->dev, "Couldn't allocate interrupt_in_buffer\n"); 78703270634SSteven Haigh goto error; 78803270634SSteven Haigh } 78903270634SSteven Haigh 79003270634SSteven Haigh /* debug code prime the buffer */ 79103270634SSteven Haigh memset(dev->interrupt_in_buffer, 'i', in_end_size); 79203270634SSteven Haigh 79303270634SSteven Haigh dev->interrupt_in_urb = usb_alloc_urb(0, GFP_KERNEL); 79403270634SSteven Haigh if (!dev->interrupt_in_urb) { 79503270634SSteven Haigh dev_err(&interface->dev, "Couldn't allocate interrupt_in_urb\n"); 79603270634SSteven Haigh goto error; 79703270634SSteven Haigh } 79803270634SSteven Haigh dev->interrupt_out_buffer = kmalloc(out_end_size, GFP_KERNEL); 79903270634SSteven Haigh if (!dev->interrupt_out_buffer) { 80003270634SSteven Haigh dev_err(&interface->dev, "Couldn't allocate interrupt_out_buffer\n"); 80103270634SSteven Haigh goto error; 80203270634SSteven Haigh } 80303270634SSteven Haigh dev->interrupt_out_urb = usb_alloc_urb(0, GFP_KERNEL); 80403270634SSteven Haigh if (!dev->interrupt_out_urb) { 80503270634SSteven Haigh dev_err(&interface->dev, "Couldn't allocate interrupt_out_urb\n"); 80603270634SSteven Haigh goto error; 80703270634SSteven Haigh } 80803270634SSteven Haigh 80903270634SSteven Haigh if (!usb_string(udev, udev->descriptor.iSerialNumber, dev->serial_number, 81003270634SSteven Haigh sizeof(dev->serial_number))) { 81103270634SSteven Haigh dev_err(&interface->dev, "Could not retrieve serial number\n"); 81203270634SSteven Haigh goto error; 81303270634SSteven Haigh } 814441b62c1SHarvey Harrison dbg(2," %s : serial_number=%s", __func__, dev->serial_number); 81503270634SSteven Haigh 81603270634SSteven Haigh /* we can register the device now, as it is ready */ 81703270634SSteven Haigh usb_set_intfdata(interface, dev); 81803270634SSteven Haigh 81903270634SSteven Haigh retval = usb_register_dev(interface, &adu_class); 82003270634SSteven Haigh 82103270634SSteven Haigh if (retval) { 82203270634SSteven Haigh /* something prevented us from registering this driver */ 82303270634SSteven Haigh dev_err(&interface->dev, "Not able to get a minor for this device.\n"); 82403270634SSteven Haigh usb_set_intfdata(interface, NULL); 82503270634SSteven Haigh goto error; 82603270634SSteven Haigh } 82703270634SSteven Haigh 82803270634SSteven Haigh dev->minor = interface->minor; 82903270634SSteven Haigh 83003270634SSteven Haigh /* let the user know what node this device is now attached to */ 831898eb71cSJoe Perches dev_info(&interface->dev, "ADU%d %s now attached to /dev/usb/adutux%d\n", 83203270634SSteven Haigh udev->descriptor.idProduct, dev->serial_number, 83303270634SSteven Haigh (dev->minor - ADU_MINOR_BASE)); 83403270634SSteven Haigh exit: 835441b62c1SHarvey Harrison dbg(2," %s : leave, return value %p (dev)", __func__, dev); 83603270634SSteven Haigh 83703270634SSteven Haigh return retval; 83803270634SSteven Haigh 83903270634SSteven Haigh error: 84003270634SSteven Haigh adu_delete(dev); 84103270634SSteven Haigh return retval; 84203270634SSteven Haigh } 84303270634SSteven Haigh 84403270634SSteven Haigh /** 84503270634SSteven Haigh * adu_disconnect 84603270634SSteven Haigh * 84703270634SSteven Haigh * Called by the usb core when the device is removed from the system. 84803270634SSteven Haigh */ 84903270634SSteven Haigh static void adu_disconnect(struct usb_interface *interface) 85003270634SSteven Haigh { 85103270634SSteven Haigh struct adu_device *dev; 85203270634SSteven Haigh int minor; 85303270634SSteven Haigh 854441b62c1SHarvey Harrison dbg(2," %s : enter", __func__); 85503270634SSteven Haigh 85603270634SSteven Haigh dev = usb_get_intfdata(interface); 85703270634SSteven Haigh 8588293c568SMatthias Kaehlcke mutex_lock(&dev->mtx); /* not interruptible */ 859f08812d5SPete Zaitcev dev->udev = NULL; /* poison */ 860f08812d5SPete Zaitcev minor = dev->minor; 861f08812d5SPete Zaitcev usb_deregister_dev(interface, &adu_class); 862f08812d5SPete Zaitcev mutex_unlock(&dev->mtx); 863f08812d5SPete Zaitcev 864f08812d5SPete Zaitcev mutex_lock(&adutux_mutex); 865f08812d5SPete Zaitcev usb_set_intfdata(interface, NULL); 866d4ead16fSAlan Stern 86703270634SSteven Haigh /* if the device is not opened, then we clean up right now */ 868441b62c1SHarvey Harrison dbg(2," %s : open count %d", __func__, dev->open_count); 869f08812d5SPete Zaitcev if (!dev->open_count) 87003270634SSteven Haigh adu_delete(dev); 871f08812d5SPete Zaitcev 872f08812d5SPete Zaitcev mutex_unlock(&adutux_mutex); 87303270634SSteven Haigh 874898eb71cSJoe Perches dev_info(&interface->dev, "ADU device adutux%d now disconnected\n", 87503270634SSteven Haigh (minor - ADU_MINOR_BASE)); 87603270634SSteven Haigh 877441b62c1SHarvey Harrison dbg(2," %s : leave", __func__); 87803270634SSteven Haigh } 87903270634SSteven Haigh 88003270634SSteven Haigh /* usb specific object needed to register this driver with the usb subsystem */ 88103270634SSteven Haigh static struct usb_driver adu_driver = { 88203270634SSteven Haigh .name = "adutux", 88303270634SSteven Haigh .probe = adu_probe, 88403270634SSteven Haigh .disconnect = adu_disconnect, 88503270634SSteven Haigh .id_table = device_table, 88603270634SSteven Haigh }; 88703270634SSteven Haigh 88865db4305SGreg Kroah-Hartman module_usb_driver(adu_driver); 88903270634SSteven Haigh 89003270634SSteven Haigh MODULE_AUTHOR(DRIVER_AUTHOR); 89103270634SSteven Haigh MODULE_DESCRIPTION(DRIVER_DESC); 89203270634SSteven Haigh MODULE_LICENSE("GPL"); 893