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