Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 1 | /* |
| 2 | * adutux - driver for ADU devices from Ontrak Control Systems |
| 3 | * This is an experimental driver. Use at your own risk. |
| 4 | * This driver is not supported by Ontrak Control Systems. |
| 5 | * |
| 6 | * Copyright (c) 2003 John Homppi (SCO, leave this notice here) |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public License as |
| 10 | * published by the Free Software Foundation; either version 2 of |
| 11 | * the License, or (at your option) any later version. |
| 12 | * |
| 13 | * derived from the Lego USB Tower driver 0.56: |
| 14 | * Copyright (c) 2003 David Glance <davidgsf@sourceforge.net> |
| 15 | * 2001 Juergen Stuber <stuber@loria.fr> |
| 16 | * that was derived from USB Skeleton driver - 0.5 |
| 17 | * Copyright (c) 2001 Greg Kroah-Hartman (greg@kroah.com) |
| 18 | * |
| 19 | */ |
| 20 | |
| 21 | #include <linux/kernel.h> |
| 22 | #include <linux/errno.h> |
| 23 | #include <linux/init.h> |
| 24 | #include <linux/slab.h> |
| 25 | #include <linux/module.h> |
| 26 | #include <linux/usb.h> |
Matthias Kaehlcke | 8293c56 | 2007-07-13 21:28:31 +0200 | [diff] [blame] | 27 | #include <linux/mutex.h> |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 28 | #include <asm/uaccess.h> |
| 29 | |
| 30 | #ifdef CONFIG_USB_DEBUG |
| 31 | static int debug = 5; |
| 32 | #else |
| 33 | static int debug = 1; |
| 34 | #endif |
| 35 | |
| 36 | /* Use our own dbg macro */ |
| 37 | #undef dbg |
| 38 | #define dbg(lvl, format, arg...) \ |
| 39 | do { \ |
| 40 | if (debug >= lvl) \ |
| 41 | printk(KERN_DEBUG __FILE__ " : " format " \n", ## arg); \ |
| 42 | } while (0) |
| 43 | |
| 44 | |
| 45 | /* Version Information */ |
| 46 | #define DRIVER_VERSION "v0.0.13" |
| 47 | #define DRIVER_AUTHOR "John Homppi" |
| 48 | #define DRIVER_DESC "adutux (see www.ontrak.net)" |
| 49 | |
| 50 | /* Module parameters */ |
| 51 | module_param(debug, int, S_IRUGO | S_IWUSR); |
| 52 | MODULE_PARM_DESC(debug, "Debug enabled or not"); |
| 53 | |
| 54 | /* Define these values to match your device */ |
| 55 | #define ADU_VENDOR_ID 0x0a07 |
| 56 | #define ADU_PRODUCT_ID 0x0064 |
| 57 | |
| 58 | /* table of devices that work with this driver */ |
| 59 | static struct usb_device_id device_table [] = { |
| 60 | { USB_DEVICE(ADU_VENDOR_ID, ADU_PRODUCT_ID) }, /* ADU100 */ |
| 61 | { USB_DEVICE(ADU_VENDOR_ID, ADU_PRODUCT_ID+20) }, /* ADU120 */ |
| 62 | { USB_DEVICE(ADU_VENDOR_ID, ADU_PRODUCT_ID+30) }, /* ADU130 */ |
| 63 | { USB_DEVICE(ADU_VENDOR_ID, ADU_PRODUCT_ID+100) }, /* ADU200 */ |
| 64 | { USB_DEVICE(ADU_VENDOR_ID, ADU_PRODUCT_ID+108) }, /* ADU208 */ |
| 65 | { USB_DEVICE(ADU_VENDOR_ID, ADU_PRODUCT_ID+118) }, /* ADU218 */ |
| 66 | { }/* Terminating entry */ |
| 67 | }; |
| 68 | |
| 69 | MODULE_DEVICE_TABLE(usb, device_table); |
| 70 | |
| 71 | #ifdef CONFIG_USB_DYNAMIC_MINORS |
| 72 | #define ADU_MINOR_BASE 0 |
| 73 | #else |
| 74 | #define ADU_MINOR_BASE 67 |
| 75 | #endif |
| 76 | |
| 77 | /* we can have up to this number of device plugged in at once */ |
| 78 | #define MAX_DEVICES 16 |
| 79 | |
| 80 | #define COMMAND_TIMEOUT (2*HZ) /* 60 second timeout for a command */ |
| 81 | |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 82 | /* |
| 83 | * The locking scheme is a vanilla 3-lock: |
| 84 | * adu_device.buflock: A spinlock, covers what IRQs touch. |
| 85 | * adutux_mutex: A Static lock to cover open_count. It would also cover |
| 86 | * any globals, but we don't have them in 2.6. |
| 87 | * adu_device.mtx: A mutex to hold across sleepers like copy_from_user. |
| 88 | * It covers all of adu_device, except the open_count |
| 89 | * and what .buflock covers. |
| 90 | */ |
| 91 | |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 92 | /* Structure to hold all of our device specific stuff */ |
| 93 | struct adu_device { |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 94 | struct mutex mtx; |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 95 | struct usb_device* udev; /* save off the usb device pointer */ |
| 96 | struct usb_interface* interface; |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 97 | unsigned int minor; /* the starting minor number for this device */ |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 98 | char serial_number[8]; |
| 99 | |
| 100 | int open_count; /* number of times this port has been opened */ |
| 101 | |
| 102 | char* read_buffer_primary; |
| 103 | int read_buffer_length; |
| 104 | char* read_buffer_secondary; |
| 105 | int secondary_head; |
| 106 | int secondary_tail; |
| 107 | spinlock_t buflock; |
| 108 | |
| 109 | wait_queue_head_t read_wait; |
| 110 | wait_queue_head_t write_wait; |
| 111 | |
| 112 | char* interrupt_in_buffer; |
| 113 | struct usb_endpoint_descriptor* interrupt_in_endpoint; |
| 114 | struct urb* interrupt_in_urb; |
| 115 | int read_urb_finished; |
| 116 | |
| 117 | char* interrupt_out_buffer; |
| 118 | struct usb_endpoint_descriptor* interrupt_out_endpoint; |
| 119 | struct urb* interrupt_out_urb; |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 120 | int out_urb_finished; |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 121 | }; |
| 122 | |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 123 | static DEFINE_MUTEX(adutux_mutex); |
| 124 | |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 125 | static struct usb_driver adu_driver; |
| 126 | |
| 127 | static void adu_debug_data(int level, const char *function, int size, |
| 128 | const unsigned char *data) |
| 129 | { |
| 130 | int i; |
| 131 | |
| 132 | if (debug < level) |
| 133 | return; |
| 134 | |
| 135 | printk(KERN_DEBUG __FILE__": %s - length = %d, data = ", |
| 136 | function, size); |
| 137 | for (i = 0; i < size; ++i) |
| 138 | printk("%.2x ", data[i]); |
| 139 | printk("\n"); |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * adu_abort_transfers |
| 144 | * aborts transfers and frees associated data structures |
| 145 | */ |
| 146 | static void adu_abort_transfers(struct adu_device *dev) |
| 147 | { |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 148 | unsigned long flags; |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 149 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 150 | dbg(2," %s : enter", __func__); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 151 | |
| 152 | if (dev->udev == NULL) { |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 153 | dbg(1," %s : udev is null", __func__); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 154 | goto exit; |
| 155 | } |
| 156 | |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 157 | /* shutdown transfer */ |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 158 | |
| 159 | /* XXX Anchor these instead */ |
| 160 | spin_lock_irqsave(&dev->buflock, flags); |
| 161 | if (!dev->read_urb_finished) { |
| 162 | spin_unlock_irqrestore(&dev->buflock, flags); |
| 163 | usb_kill_urb(dev->interrupt_in_urb); |
| 164 | } else |
| 165 | spin_unlock_irqrestore(&dev->buflock, flags); |
| 166 | |
| 167 | spin_lock_irqsave(&dev->buflock, flags); |
| 168 | if (!dev->out_urb_finished) { |
| 169 | spin_unlock_irqrestore(&dev->buflock, flags); |
| 170 | usb_kill_urb(dev->interrupt_out_urb); |
| 171 | } else |
| 172 | spin_unlock_irqrestore(&dev->buflock, flags); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 173 | |
| 174 | exit: |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 175 | dbg(2," %s : leave", __func__); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | static void adu_delete(struct adu_device *dev) |
| 179 | { |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 180 | dbg(2, "%s enter", __func__); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 181 | |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 182 | /* free data structures */ |
| 183 | usb_free_urb(dev->interrupt_in_urb); |
| 184 | usb_free_urb(dev->interrupt_out_urb); |
| 185 | kfree(dev->read_buffer_primary); |
| 186 | kfree(dev->read_buffer_secondary); |
| 187 | kfree(dev->interrupt_in_buffer); |
| 188 | kfree(dev->interrupt_out_buffer); |
| 189 | kfree(dev); |
| 190 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 191 | dbg(2, "%s : leave", __func__); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 192 | } |
| 193 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 194 | static void adu_interrupt_in_callback(struct urb *urb) |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 195 | { |
| 196 | struct adu_device *dev = urb->context; |
Greg Kroah-Hartman | 24497a0 | 2007-07-18 10:58:02 -0700 | [diff] [blame] | 197 | int status = urb->status; |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 198 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 199 | dbg(4," %s : enter, status %d", __func__, status); |
| 200 | adu_debug_data(5, __func__, urb->actual_length, |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 201 | urb->transfer_buffer); |
| 202 | |
| 203 | spin_lock(&dev->buflock); |
| 204 | |
Greg Kroah-Hartman | 24497a0 | 2007-07-18 10:58:02 -0700 | [diff] [blame] | 205 | if (status != 0) { |
Oliver Neukum | f6c1cea | 2007-08-16 16:02:08 +0200 | [diff] [blame] | 206 | if ((status != -ENOENT) && (status != -ECONNRESET) && |
| 207 | (status != -ESHUTDOWN)) { |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 208 | dbg(1," %s : nonzero status received: %d", |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 209 | __func__, status); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 210 | } |
| 211 | goto exit; |
| 212 | } |
| 213 | |
| 214 | if (urb->actual_length > 0 && dev->interrupt_in_buffer[0] != 0x00) { |
| 215 | if (dev->read_buffer_length < |
| 216 | (4 * le16_to_cpu(dev->interrupt_in_endpoint->wMaxPacketSize)) - |
| 217 | (urb->actual_length)) { |
| 218 | memcpy (dev->read_buffer_primary + |
| 219 | dev->read_buffer_length, |
| 220 | dev->interrupt_in_buffer, urb->actual_length); |
| 221 | |
| 222 | dev->read_buffer_length += urb->actual_length; |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 223 | dbg(2," %s reading %d ", __func__, |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 224 | urb->actual_length); |
| 225 | } else { |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 226 | dbg(1," %s : read_buffer overflow", __func__); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 227 | } |
| 228 | } |
| 229 | |
| 230 | exit: |
| 231 | dev->read_urb_finished = 1; |
| 232 | spin_unlock(&dev->buflock); |
| 233 | /* always wake up so we recover from errors */ |
| 234 | wake_up_interruptible(&dev->read_wait); |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 235 | adu_debug_data(5, __func__, urb->actual_length, |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 236 | urb->transfer_buffer); |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 237 | dbg(4," %s : leave, status %d", __func__, status); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 238 | } |
| 239 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 240 | static void adu_interrupt_out_callback(struct urb *urb) |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 241 | { |
| 242 | struct adu_device *dev = urb->context; |
Greg Kroah-Hartman | 24497a0 | 2007-07-18 10:58:02 -0700 | [diff] [blame] | 243 | int status = urb->status; |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 244 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 245 | dbg(4," %s : enter, status %d", __func__, status); |
| 246 | adu_debug_data(5,__func__, urb->actual_length, urb->transfer_buffer); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 247 | |
Greg Kroah-Hartman | 24497a0 | 2007-07-18 10:58:02 -0700 | [diff] [blame] | 248 | if (status != 0) { |
| 249 | if ((status != -ENOENT) && |
| 250 | (status != -ECONNRESET)) { |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 251 | dbg(1, " %s :nonzero status received: %d", |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 252 | __func__, status); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 253 | } |
| 254 | goto exit; |
| 255 | } |
| 256 | |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 257 | spin_lock(&dev->buflock); |
| 258 | dev->out_urb_finished = 1; |
| 259 | wake_up(&dev->write_wait); |
| 260 | spin_unlock(&dev->buflock); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 261 | exit: |
| 262 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 263 | adu_debug_data(5, __func__, urb->actual_length, |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 264 | urb->transfer_buffer); |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 265 | dbg(4," %s : leave, status %d", __func__, status); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | static int adu_open(struct inode *inode, struct file *file) |
| 269 | { |
| 270 | struct adu_device *dev = NULL; |
| 271 | struct usb_interface *interface; |
| 272 | int subminor; |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 273 | int retval; |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 274 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 275 | dbg(2,"%s : enter", __func__); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 276 | |
| 277 | subminor = iminor(inode); |
| 278 | |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 279 | if ((retval = mutex_lock_interruptible(&adutux_mutex))) { |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 280 | dbg(2, "%s : mutex lock failed", __func__); |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 281 | goto exit_no_lock; |
| 282 | } |
| 283 | |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 284 | interface = usb_find_interface(&adu_driver, subminor); |
| 285 | if (!interface) { |
| 286 | err("%s - error, can't find device for minor %d", |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 287 | __func__, subminor); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 288 | retval = -ENODEV; |
| 289 | goto exit_no_device; |
| 290 | } |
| 291 | |
| 292 | dev = usb_get_intfdata(interface); |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 293 | if (!dev || !dev->udev) { |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 294 | retval = -ENODEV; |
| 295 | goto exit_no_device; |
| 296 | } |
| 297 | |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 298 | /* check that nobody else is using the device */ |
| 299 | if (dev->open_count) { |
| 300 | retval = -EBUSY; |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 301 | goto exit_no_device; |
| 302 | } |
| 303 | |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 304 | ++dev->open_count; |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 305 | dbg(2,"%s : open count %d", __func__, dev->open_count); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 306 | |
| 307 | /* save device in the file's private structure */ |
| 308 | file->private_data = dev; |
| 309 | |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 310 | /* initialize in direction */ |
| 311 | dev->read_buffer_length = 0; |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 312 | |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 313 | /* fixup first read by having urb waiting for it */ |
| 314 | usb_fill_int_urb(dev->interrupt_in_urb,dev->udev, |
| 315 | usb_rcvintpipe(dev->udev, |
| 316 | dev->interrupt_in_endpoint->bEndpointAddress), |
| 317 | dev->interrupt_in_buffer, |
| 318 | le16_to_cpu(dev->interrupt_in_endpoint->wMaxPacketSize), |
| 319 | adu_interrupt_in_callback, dev, |
| 320 | dev->interrupt_in_endpoint->bInterval); |
| 321 | dev->read_urb_finished = 0; |
| 322 | if (usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL)) |
| 323 | dev->read_urb_finished = 1; |
| 324 | /* we ignore failure */ |
| 325 | /* end of fixup for first read */ |
| 326 | |
| 327 | /* initialize out direction */ |
| 328 | dev->out_urb_finished = 1; |
| 329 | |
| 330 | retval = 0; |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 331 | |
| 332 | exit_no_device: |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 333 | mutex_unlock(&adutux_mutex); |
| 334 | exit_no_lock: |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 335 | dbg(2,"%s : leave, return value %d ", __func__, retval); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 336 | return retval; |
| 337 | } |
| 338 | |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 339 | static void adu_release_internal(struct adu_device *dev) |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 340 | { |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 341 | dbg(2," %s : enter", __func__); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 342 | |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 343 | /* decrement our usage count for the device */ |
| 344 | --dev->open_count; |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 345 | dbg(2," %s : open count %d", __func__, dev->open_count); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 346 | if (dev->open_count <= 0) { |
| 347 | adu_abort_transfers(dev); |
| 348 | dev->open_count = 0; |
| 349 | } |
| 350 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 351 | dbg(2," %s : leave", __func__); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | static int adu_release(struct inode *inode, struct file *file) |
| 355 | { |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 356 | struct adu_device *dev; |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 357 | int retval = 0; |
| 358 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 359 | dbg(2," %s : enter", __func__); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 360 | |
| 361 | if (file == NULL) { |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 362 | dbg(1," %s : file is NULL", __func__); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 363 | retval = -ENODEV; |
| 364 | goto exit; |
| 365 | } |
| 366 | |
| 367 | dev = file->private_data; |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 368 | if (dev == NULL) { |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 369 | dbg(1," %s : object is NULL", __func__); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 370 | retval = -ENODEV; |
| 371 | goto exit; |
| 372 | } |
| 373 | |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 374 | mutex_lock(&adutux_mutex); /* not interruptible */ |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 375 | |
| 376 | if (dev->open_count <= 0) { |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 377 | dbg(1," %s : device not opened", __func__); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 378 | retval = -ENODEV; |
| 379 | goto exit; |
| 380 | } |
| 381 | |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 382 | adu_release_internal(dev); |
Alan Stern | d4ead16 | 2007-05-22 11:46:41 -0400 | [diff] [blame] | 383 | if (dev->udev == NULL) { |
| 384 | /* the device was unplugged before the file was released */ |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 385 | if (!dev->open_count) /* ... and we're the last user */ |
| 386 | adu_delete(dev); |
Alan Stern | d4ead16 | 2007-05-22 11:46:41 -0400 | [diff] [blame] | 387 | } |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 388 | |
| 389 | exit: |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 390 | mutex_unlock(&adutux_mutex); |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 391 | dbg(2," %s : leave, return value %d", __func__, retval); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 392 | return retval; |
| 393 | } |
| 394 | |
| 395 | static ssize_t adu_read(struct file *file, __user char *buffer, size_t count, |
| 396 | loff_t *ppos) |
| 397 | { |
| 398 | struct adu_device *dev; |
| 399 | size_t bytes_read = 0; |
| 400 | size_t bytes_to_read = count; |
| 401 | int i; |
| 402 | int retval = 0; |
| 403 | int timeout = 0; |
| 404 | int should_submit = 0; |
| 405 | unsigned long flags; |
| 406 | DECLARE_WAITQUEUE(wait, current); |
| 407 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 408 | dbg(2," %s : enter, count = %Zd, file=%p", __func__, count, file); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 409 | |
| 410 | dev = file->private_data; |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 411 | dbg(2," %s : dev=%p", __func__, dev); |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 412 | |
Matthias Kaehlcke | 8293c56 | 2007-07-13 21:28:31 +0200 | [diff] [blame] | 413 | if (mutex_lock_interruptible(&dev->mtx)) |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 414 | return -ERESTARTSYS; |
| 415 | |
| 416 | /* verify that the device wasn't unplugged */ |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 417 | if (dev->udev == NULL) { |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 418 | retval = -ENODEV; |
| 419 | err("No device or device unplugged %d", retval); |
| 420 | goto exit; |
| 421 | } |
| 422 | |
| 423 | /* verify that some data was requested */ |
| 424 | if (count == 0) { |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 425 | dbg(1," %s : read request of 0 bytes", __func__); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 426 | goto exit; |
| 427 | } |
| 428 | |
| 429 | timeout = COMMAND_TIMEOUT; |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 430 | dbg(2," %s : about to start looping", __func__); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 431 | while (bytes_to_read) { |
| 432 | int data_in_secondary = dev->secondary_tail - dev->secondary_head; |
| 433 | dbg(2," %s : while, data_in_secondary=%d, status=%d", |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 434 | __func__, data_in_secondary, |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 435 | dev->interrupt_in_urb->status); |
| 436 | |
| 437 | if (data_in_secondary) { |
| 438 | /* drain secondary buffer */ |
| 439 | int amount = bytes_to_read < data_in_secondary ? bytes_to_read : data_in_secondary; |
| 440 | i = copy_to_user(buffer, dev->read_buffer_secondary+dev->secondary_head, amount); |
| 441 | if (i < 0) { |
| 442 | retval = -EFAULT; |
| 443 | goto exit; |
| 444 | } |
| 445 | dev->secondary_head += (amount - i); |
| 446 | bytes_read += (amount - i); |
| 447 | bytes_to_read -= (amount - i); |
| 448 | if (i) { |
| 449 | retval = bytes_read ? bytes_read : -EFAULT; |
| 450 | goto exit; |
| 451 | } |
| 452 | } else { |
| 453 | /* we check the primary buffer */ |
| 454 | spin_lock_irqsave (&dev->buflock, flags); |
| 455 | if (dev->read_buffer_length) { |
| 456 | /* we secure access to the primary */ |
| 457 | char *tmp; |
| 458 | dbg(2," %s : swap, read_buffer_length = %d", |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 459 | __func__, dev->read_buffer_length); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 460 | tmp = dev->read_buffer_secondary; |
| 461 | dev->read_buffer_secondary = dev->read_buffer_primary; |
| 462 | dev->read_buffer_primary = tmp; |
| 463 | dev->secondary_head = 0; |
| 464 | dev->secondary_tail = dev->read_buffer_length; |
| 465 | dev->read_buffer_length = 0; |
| 466 | spin_unlock_irqrestore(&dev->buflock, flags); |
| 467 | /* we have a free buffer so use it */ |
| 468 | should_submit = 1; |
| 469 | } else { |
| 470 | /* even the primary was empty - we may need to do IO */ |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 471 | if (!dev->read_urb_finished) { |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 472 | /* somebody is doing IO */ |
| 473 | spin_unlock_irqrestore(&dev->buflock, flags); |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 474 | dbg(2," %s : submitted already", __func__); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 475 | } else { |
| 476 | /* we must initiate input */ |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 477 | dbg(2," %s : initiate input", __func__); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 478 | dev->read_urb_finished = 0; |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 479 | spin_unlock_irqrestore(&dev->buflock, flags); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 480 | |
| 481 | usb_fill_int_urb(dev->interrupt_in_urb,dev->udev, |
| 482 | usb_rcvintpipe(dev->udev, |
| 483 | dev->interrupt_in_endpoint->bEndpointAddress), |
| 484 | dev->interrupt_in_buffer, |
| 485 | le16_to_cpu(dev->interrupt_in_endpoint->wMaxPacketSize), |
| 486 | adu_interrupt_in_callback, |
| 487 | dev, |
| 488 | dev->interrupt_in_endpoint->bInterval); |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 489 | retval = usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL); |
| 490 | if (retval) { |
| 491 | dev->read_urb_finished = 1; |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 492 | if (retval == -ENOMEM) { |
| 493 | retval = bytes_read ? bytes_read : -ENOMEM; |
| 494 | } |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 495 | dbg(2," %s : submit failed", __func__); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 496 | goto exit; |
| 497 | } |
| 498 | } |
| 499 | |
| 500 | /* we wait for I/O to complete */ |
| 501 | set_current_state(TASK_INTERRUPTIBLE); |
| 502 | add_wait_queue(&dev->read_wait, &wait); |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 503 | spin_lock_irqsave(&dev->buflock, flags); |
| 504 | if (!dev->read_urb_finished) { |
| 505 | spin_unlock_irqrestore(&dev->buflock, flags); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 506 | timeout = schedule_timeout(COMMAND_TIMEOUT); |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 507 | } else { |
| 508 | spin_unlock_irqrestore(&dev->buflock, flags); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 509 | set_current_state(TASK_RUNNING); |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 510 | } |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 511 | remove_wait_queue(&dev->read_wait, &wait); |
| 512 | |
| 513 | if (timeout <= 0) { |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 514 | dbg(2," %s : timeout", __func__); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 515 | retval = bytes_read ? bytes_read : -ETIMEDOUT; |
| 516 | goto exit; |
| 517 | } |
| 518 | |
| 519 | if (signal_pending(current)) { |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 520 | dbg(2," %s : signal pending", __func__); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 521 | retval = bytes_read ? bytes_read : -EINTR; |
| 522 | goto exit; |
| 523 | } |
| 524 | } |
| 525 | } |
| 526 | } |
| 527 | |
| 528 | retval = bytes_read; |
| 529 | /* if the primary buffer is empty then use it */ |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 530 | spin_lock_irqsave(&dev->buflock, flags); |
| 531 | if (should_submit && dev->read_urb_finished) { |
| 532 | dev->read_urb_finished = 0; |
| 533 | spin_unlock_irqrestore(&dev->buflock, flags); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 534 | usb_fill_int_urb(dev->interrupt_in_urb,dev->udev, |
| 535 | usb_rcvintpipe(dev->udev, |
| 536 | dev->interrupt_in_endpoint->bEndpointAddress), |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 537 | dev->interrupt_in_buffer, |
| 538 | le16_to_cpu(dev->interrupt_in_endpoint->wMaxPacketSize), |
| 539 | adu_interrupt_in_callback, |
| 540 | dev, |
| 541 | dev->interrupt_in_endpoint->bInterval); |
| 542 | if (usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL) != 0) |
| 543 | dev->read_urb_finished = 1; |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 544 | /* we ignore failure */ |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 545 | } else { |
| 546 | spin_unlock_irqrestore(&dev->buflock, flags); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 547 | } |
| 548 | |
| 549 | exit: |
| 550 | /* unlock the device */ |
Matthias Kaehlcke | 8293c56 | 2007-07-13 21:28:31 +0200 | [diff] [blame] | 551 | mutex_unlock(&dev->mtx); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 552 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 553 | dbg(2," %s : leave, return value %d", __func__, retval); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 554 | return retval; |
| 555 | } |
| 556 | |
| 557 | static ssize_t adu_write(struct file *file, const __user char *buffer, |
| 558 | size_t count, loff_t *ppos) |
| 559 | { |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 560 | DECLARE_WAITQUEUE(waita, current); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 561 | struct adu_device *dev; |
| 562 | size_t bytes_written = 0; |
| 563 | size_t bytes_to_write; |
| 564 | size_t buffer_size; |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 565 | unsigned long flags; |
Oliver Neukum | ebc3ac1 | 2007-04-02 15:16:36 +0200 | [diff] [blame] | 566 | int retval; |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 567 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 568 | dbg(2," %s : enter, count = %Zd", __func__, count); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 569 | |
| 570 | dev = file->private_data; |
| 571 | |
Matthias Kaehlcke | 8293c56 | 2007-07-13 21:28:31 +0200 | [diff] [blame] | 572 | retval = mutex_lock_interruptible(&dev->mtx); |
Oliver Neukum | ebc3ac1 | 2007-04-02 15:16:36 +0200 | [diff] [blame] | 573 | if (retval) |
| 574 | goto exit_nolock; |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 575 | |
| 576 | /* verify that the device wasn't unplugged */ |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 577 | if (dev->udev == NULL) { |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 578 | retval = -ENODEV; |
| 579 | err("No device or device unplugged %d", retval); |
| 580 | goto exit; |
| 581 | } |
| 582 | |
| 583 | /* verify that we actually have some data to write */ |
| 584 | if (count == 0) { |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 585 | dbg(1," %s : write request of 0 bytes", __func__); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 586 | goto exit; |
| 587 | } |
| 588 | |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 589 | while (count > 0) { |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 590 | add_wait_queue(&dev->write_wait, &waita); |
| 591 | set_current_state(TASK_INTERRUPTIBLE); |
| 592 | spin_lock_irqsave(&dev->buflock, flags); |
| 593 | if (!dev->out_urb_finished) { |
| 594 | spin_unlock_irqrestore(&dev->buflock, flags); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 595 | |
Matthias Kaehlcke | 8293c56 | 2007-07-13 21:28:31 +0200 | [diff] [blame] | 596 | mutex_unlock(&dev->mtx); |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 597 | if (signal_pending(current)) { |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 598 | dbg(1," %s : interrupted", __func__); |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 599 | set_current_state(TASK_RUNNING); |
| 600 | retval = -EINTR; |
| 601 | goto exit_onqueue; |
| 602 | } |
| 603 | if (schedule_timeout(COMMAND_TIMEOUT) == 0) { |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 604 | dbg(1, "%s - command timed out.", __func__); |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 605 | retval = -ETIMEDOUT; |
| 606 | goto exit_onqueue; |
| 607 | } |
| 608 | remove_wait_queue(&dev->write_wait, &waita); |
Matthias Kaehlcke | 8293c56 | 2007-07-13 21:28:31 +0200 | [diff] [blame] | 609 | retval = mutex_lock_interruptible(&dev->mtx); |
Oliver Neukum | ebc3ac1 | 2007-04-02 15:16:36 +0200 | [diff] [blame] | 610 | if (retval) { |
| 611 | retval = bytes_written ? bytes_written : retval; |
| 612 | goto exit_nolock; |
| 613 | } |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 614 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 615 | dbg(4," %s : in progress, count = %Zd", __func__, count); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 616 | } else { |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 617 | spin_unlock_irqrestore(&dev->buflock, flags); |
| 618 | set_current_state(TASK_RUNNING); |
| 619 | remove_wait_queue(&dev->write_wait, &waita); |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 620 | dbg(4," %s : sending, count = %Zd", __func__, count); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 621 | |
| 622 | /* write the data into interrupt_out_buffer from userspace */ |
| 623 | buffer_size = le16_to_cpu(dev->interrupt_out_endpoint->wMaxPacketSize); |
| 624 | bytes_to_write = count > buffer_size ? buffer_size : count; |
| 625 | dbg(4," %s : buffer_size = %Zd, count = %Zd, bytes_to_write = %Zd", |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 626 | __func__, buffer_size, count, bytes_to_write); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 627 | |
| 628 | if (copy_from_user(dev->interrupt_out_buffer, buffer, bytes_to_write) != 0) { |
| 629 | retval = -EFAULT; |
| 630 | goto exit; |
| 631 | } |
| 632 | |
| 633 | /* send off the urb */ |
| 634 | usb_fill_int_urb( |
| 635 | dev->interrupt_out_urb, |
| 636 | dev->udev, |
| 637 | usb_sndintpipe(dev->udev, dev->interrupt_out_endpoint->bEndpointAddress), |
| 638 | dev->interrupt_out_buffer, |
| 639 | bytes_to_write, |
| 640 | adu_interrupt_out_callback, |
| 641 | dev, |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 642 | dev->interrupt_out_endpoint->bInterval); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 643 | dev->interrupt_out_urb->actual_length = bytes_to_write; |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 644 | dev->out_urb_finished = 0; |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 645 | retval = usb_submit_urb(dev->interrupt_out_urb, GFP_KERNEL); |
| 646 | if (retval < 0) { |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 647 | dev->out_urb_finished = 1; |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 648 | err("Couldn't submit interrupt_out_urb %d", retval); |
| 649 | goto exit; |
| 650 | } |
| 651 | |
| 652 | buffer += bytes_to_write; |
| 653 | count -= bytes_to_write; |
| 654 | |
| 655 | bytes_written += bytes_to_write; |
| 656 | } |
| 657 | } |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 658 | mutex_unlock(&dev->mtx); |
| 659 | return bytes_written; |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 660 | |
| 661 | exit: |
Matthias Kaehlcke | 8293c56 | 2007-07-13 21:28:31 +0200 | [diff] [blame] | 662 | mutex_unlock(&dev->mtx); |
Oliver Neukum | ebc3ac1 | 2007-04-02 15:16:36 +0200 | [diff] [blame] | 663 | exit_nolock: |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 664 | dbg(2," %s : leave, return value %d", __func__, retval); |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 665 | return retval; |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 666 | |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 667 | exit_onqueue: |
| 668 | remove_wait_queue(&dev->write_wait, &waita); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 669 | return retval; |
| 670 | } |
| 671 | |
| 672 | /* file operations needed when we register this driver */ |
Arjan van de Ven | 00977a5 | 2007-02-12 00:55:34 -0800 | [diff] [blame] | 673 | static const struct file_operations adu_fops = { |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 674 | .owner = THIS_MODULE, |
| 675 | .read = adu_read, |
| 676 | .write = adu_write, |
| 677 | .open = adu_open, |
| 678 | .release = adu_release, |
| 679 | }; |
| 680 | |
| 681 | /* |
| 682 | * usb class driver info in order to get a minor number from the usb core, |
| 683 | * and to have the device registered with devfs and the driver core |
| 684 | */ |
| 685 | static struct usb_class_driver adu_class = { |
| 686 | .name = "usb/adutux%d", |
| 687 | .fops = &adu_fops, |
| 688 | .minor_base = ADU_MINOR_BASE, |
| 689 | }; |
| 690 | |
| 691 | /** |
| 692 | * adu_probe |
| 693 | * |
| 694 | * Called by the usb core when a new device is connected that it thinks |
| 695 | * this driver might be interested in. |
| 696 | */ |
| 697 | static int adu_probe(struct usb_interface *interface, |
| 698 | const struct usb_device_id *id) |
| 699 | { |
| 700 | struct usb_device *udev = interface_to_usbdev(interface); |
| 701 | struct adu_device *dev = NULL; |
| 702 | struct usb_host_interface *iface_desc; |
| 703 | struct usb_endpoint_descriptor *endpoint; |
| 704 | int retval = -ENODEV; |
| 705 | int in_end_size; |
| 706 | int out_end_size; |
| 707 | int i; |
| 708 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 709 | dbg(2," %s : enter", __func__); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 710 | |
| 711 | if (udev == NULL) { |
| 712 | dev_err(&interface->dev, "udev is NULL.\n"); |
| 713 | goto exit; |
| 714 | } |
| 715 | |
| 716 | /* allocate memory for our device state and intialize it */ |
| 717 | dev = kzalloc(sizeof(struct adu_device), GFP_KERNEL); |
| 718 | if (dev == NULL) { |
| 719 | dev_err(&interface->dev, "Out of memory\n"); |
| 720 | retval = -ENOMEM; |
| 721 | goto exit; |
| 722 | } |
| 723 | |
Matthias Kaehlcke | 8293c56 | 2007-07-13 21:28:31 +0200 | [diff] [blame] | 724 | mutex_init(&dev->mtx); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 725 | spin_lock_init(&dev->buflock); |
| 726 | dev->udev = udev; |
| 727 | init_waitqueue_head(&dev->read_wait); |
| 728 | init_waitqueue_head(&dev->write_wait); |
| 729 | |
| 730 | iface_desc = &interface->altsetting[0]; |
| 731 | |
| 732 | /* set up the endpoint information */ |
| 733 | for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) { |
| 734 | endpoint = &iface_desc->endpoint[i].desc; |
| 735 | |
| 736 | if (usb_endpoint_is_int_in(endpoint)) |
| 737 | dev->interrupt_in_endpoint = endpoint; |
| 738 | |
| 739 | if (usb_endpoint_is_int_out(endpoint)) |
| 740 | dev->interrupt_out_endpoint = endpoint; |
| 741 | } |
| 742 | if (dev->interrupt_in_endpoint == NULL) { |
| 743 | dev_err(&interface->dev, "interrupt in endpoint not found\n"); |
| 744 | goto error; |
| 745 | } |
| 746 | if (dev->interrupt_out_endpoint == NULL) { |
| 747 | dev_err(&interface->dev, "interrupt out endpoint not found\n"); |
| 748 | goto error; |
| 749 | } |
| 750 | |
| 751 | in_end_size = le16_to_cpu(dev->interrupt_in_endpoint->wMaxPacketSize); |
| 752 | out_end_size = le16_to_cpu(dev->interrupt_out_endpoint->wMaxPacketSize); |
| 753 | |
| 754 | dev->read_buffer_primary = kmalloc((4 * in_end_size), GFP_KERNEL); |
| 755 | if (!dev->read_buffer_primary) { |
| 756 | dev_err(&interface->dev, "Couldn't allocate read_buffer_primary\n"); |
| 757 | retval = -ENOMEM; |
| 758 | goto error; |
| 759 | } |
| 760 | |
| 761 | /* debug code prime the buffer */ |
| 762 | memset(dev->read_buffer_primary, 'a', in_end_size); |
| 763 | memset(dev->read_buffer_primary + in_end_size, 'b', in_end_size); |
| 764 | memset(dev->read_buffer_primary + (2 * in_end_size), 'c', in_end_size); |
| 765 | memset(dev->read_buffer_primary + (3 * in_end_size), 'd', in_end_size); |
| 766 | |
| 767 | dev->read_buffer_secondary = kmalloc((4 * in_end_size), GFP_KERNEL); |
| 768 | if (!dev->read_buffer_secondary) { |
| 769 | dev_err(&interface->dev, "Couldn't allocate read_buffer_secondary\n"); |
| 770 | retval = -ENOMEM; |
| 771 | goto error; |
| 772 | } |
| 773 | |
| 774 | /* debug code prime the buffer */ |
| 775 | memset(dev->read_buffer_secondary, 'e', in_end_size); |
| 776 | memset(dev->read_buffer_secondary + in_end_size, 'f', in_end_size); |
| 777 | memset(dev->read_buffer_secondary + (2 * in_end_size), 'g', in_end_size); |
| 778 | memset(dev->read_buffer_secondary + (3 * in_end_size), 'h', in_end_size); |
| 779 | |
| 780 | dev->interrupt_in_buffer = kmalloc(in_end_size, GFP_KERNEL); |
| 781 | if (!dev->interrupt_in_buffer) { |
| 782 | dev_err(&interface->dev, "Couldn't allocate interrupt_in_buffer\n"); |
| 783 | goto error; |
| 784 | } |
| 785 | |
| 786 | /* debug code prime the buffer */ |
| 787 | memset(dev->interrupt_in_buffer, 'i', in_end_size); |
| 788 | |
| 789 | dev->interrupt_in_urb = usb_alloc_urb(0, GFP_KERNEL); |
| 790 | if (!dev->interrupt_in_urb) { |
| 791 | dev_err(&interface->dev, "Couldn't allocate interrupt_in_urb\n"); |
| 792 | goto error; |
| 793 | } |
| 794 | dev->interrupt_out_buffer = kmalloc(out_end_size, GFP_KERNEL); |
| 795 | if (!dev->interrupt_out_buffer) { |
| 796 | dev_err(&interface->dev, "Couldn't allocate interrupt_out_buffer\n"); |
| 797 | goto error; |
| 798 | } |
| 799 | dev->interrupt_out_urb = usb_alloc_urb(0, GFP_KERNEL); |
| 800 | if (!dev->interrupt_out_urb) { |
| 801 | dev_err(&interface->dev, "Couldn't allocate interrupt_out_urb\n"); |
| 802 | goto error; |
| 803 | } |
| 804 | |
| 805 | if (!usb_string(udev, udev->descriptor.iSerialNumber, dev->serial_number, |
| 806 | sizeof(dev->serial_number))) { |
| 807 | dev_err(&interface->dev, "Could not retrieve serial number\n"); |
| 808 | goto error; |
| 809 | } |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 810 | dbg(2," %s : serial_number=%s", __func__, dev->serial_number); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 811 | |
| 812 | /* we can register the device now, as it is ready */ |
| 813 | usb_set_intfdata(interface, dev); |
| 814 | |
| 815 | retval = usb_register_dev(interface, &adu_class); |
| 816 | |
| 817 | if (retval) { |
| 818 | /* something prevented us from registering this driver */ |
| 819 | dev_err(&interface->dev, "Not able to get a minor for this device.\n"); |
| 820 | usb_set_intfdata(interface, NULL); |
| 821 | goto error; |
| 822 | } |
| 823 | |
| 824 | dev->minor = interface->minor; |
| 825 | |
| 826 | /* let the user know what node this device is now attached to */ |
Joe Perches | 898eb71 | 2007-10-18 03:06:30 -0700 | [diff] [blame] | 827 | dev_info(&interface->dev, "ADU%d %s now attached to /dev/usb/adutux%d\n", |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 828 | udev->descriptor.idProduct, dev->serial_number, |
| 829 | (dev->minor - ADU_MINOR_BASE)); |
| 830 | exit: |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 831 | dbg(2," %s : leave, return value %p (dev)", __func__, dev); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 832 | |
| 833 | return retval; |
| 834 | |
| 835 | error: |
| 836 | adu_delete(dev); |
| 837 | return retval; |
| 838 | } |
| 839 | |
| 840 | /** |
| 841 | * adu_disconnect |
| 842 | * |
| 843 | * Called by the usb core when the device is removed from the system. |
| 844 | */ |
| 845 | static void adu_disconnect(struct usb_interface *interface) |
| 846 | { |
| 847 | struct adu_device *dev; |
| 848 | int minor; |
| 849 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 850 | dbg(2," %s : enter", __func__); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 851 | |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 852 | dev = usb_get_intfdata(interface); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 853 | |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 854 | mutex_lock(&dev->mtx); /* not interruptible */ |
| 855 | dev->udev = NULL; /* poison */ |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 856 | minor = dev->minor; |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 857 | usb_deregister_dev(interface, &adu_class); |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 858 | mutex_unlock(&dev->mtx); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 859 | |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 860 | mutex_lock(&adutux_mutex); |
| 861 | usb_set_intfdata(interface, NULL); |
Alan Stern | d4ead16 | 2007-05-22 11:46:41 -0400 | [diff] [blame] | 862 | |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 863 | /* if the device is not opened, then we clean up right now */ |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 864 | dbg(2," %s : open count %d", __func__, dev->open_count); |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 865 | if (!dev->open_count) |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 866 | adu_delete(dev); |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 867 | |
| 868 | mutex_unlock(&adutux_mutex); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 869 | |
Joe Perches | 898eb71 | 2007-10-18 03:06:30 -0700 | [diff] [blame] | 870 | dev_info(&interface->dev, "ADU device adutux%d now disconnected\n", |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 871 | (minor - ADU_MINOR_BASE)); |
| 872 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 873 | dbg(2," %s : leave", __func__); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 874 | } |
| 875 | |
| 876 | /* usb specific object needed to register this driver with the usb subsystem */ |
| 877 | static struct usb_driver adu_driver = { |
| 878 | .name = "adutux", |
| 879 | .probe = adu_probe, |
| 880 | .disconnect = adu_disconnect, |
| 881 | .id_table = device_table, |
| 882 | }; |
| 883 | |
| 884 | static int __init adu_init(void) |
| 885 | { |
| 886 | int result; |
| 887 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 888 | dbg(2," %s : enter", __func__); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 889 | |
| 890 | /* register this driver with the USB subsystem */ |
| 891 | result = usb_register(&adu_driver); |
| 892 | if (result < 0) { |
| 893 | err("usb_register failed for the "__FILE__" driver. " |
| 894 | "Error number %d", result); |
| 895 | goto exit; |
| 896 | } |
| 897 | |
| 898 | info("adutux " DRIVER_DESC " " DRIVER_VERSION); |
| 899 | info("adutux is an experimental driver. Use at your own risk"); |
| 900 | |
| 901 | exit: |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 902 | dbg(2," %s : leave, return value %d", __func__, result); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 903 | |
| 904 | return result; |
| 905 | } |
| 906 | |
| 907 | static void __exit adu_exit(void) |
| 908 | { |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 909 | dbg(2," %s : enter", __func__); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 910 | /* deregister this driver with the USB subsystem */ |
| 911 | usb_deregister(&adu_driver); |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 912 | dbg(2," %s : leave", __func__); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 913 | } |
| 914 | |
| 915 | module_init(adu_init); |
| 916 | module_exit(adu_exit); |
| 917 | |
| 918 | MODULE_AUTHOR(DRIVER_AUTHOR); |
| 919 | MODULE_DESCRIPTION(DRIVER_DESC); |
| 920 | MODULE_LICENSE("GPL"); |