1 /** 2 * Generic USB driver for report based interrupt in/out devices 3 * like LD Didactic's USB devices. LD Didactic's USB devices are 4 * HID devices which do not use HID report definitons (they use 5 * raw interrupt in and our reports only for communication). 6 * 7 * This driver uses a ring buffer for time critical reading of 8 * interrupt in reports and provides read and write methods for 9 * raw interrupt reports (similar to the Windows HID driver). 10 * Devices based on the book USB COMPLETE by Jan Axelson may need 11 * such a compatibility to the Windows HID driver. 12 * 13 * Copyright (C) 2005 Michael Hund <mhund@ld-didactic.de> 14 * 15 * This program is free software; you can redistribute it and/or 16 * modify it under the terms of the GNU General Public License as 17 * published by the Free Software Foundation; either version 2 of 18 * the License, or (at your option) any later version. 19 * 20 * Derived from Lego USB Tower driver 21 * Copyright (C) 2003 David Glance <advidgsf@sourceforge.net> 22 * 2001-2004 Juergen Stuber <starblue@users.sourceforge.net> 23 */ 24 25 #include <linux/kernel.h> 26 #include <linux/errno.h> 27 #include <linux/init.h> 28 #include <linux/slab.h> 29 #include <linux/module.h> 30 #include <linux/mutex.h> 31 32 #include <asm/uaccess.h> 33 #include <linux/input.h> 34 #include <linux/usb.h> 35 #include <linux/poll.h> 36 37 /* Define these values to match your devices */ 38 #define USB_VENDOR_ID_LD 0x0f11 /* USB Vendor ID of LD Didactic GmbH */ 39 #define USB_DEVICE_ID_LD_CASSY 0x1000 /* USB Product ID of CASSY-S modules with 8 bytes endpoint size */ 40 #define USB_DEVICE_ID_LD_CASSY2 0x1001 /* USB Product ID of CASSY-S modules with 64 bytes endpoint size */ 41 #define USB_DEVICE_ID_LD_POCKETCASSY 0x1010 /* USB Product ID of Pocket-CASSY */ 42 #define USB_DEVICE_ID_LD_POCKETCASSY2 0x1011 /* USB Product ID of Pocket-CASSY 2 (reserved) */ 43 #define USB_DEVICE_ID_LD_MOBILECASSY 0x1020 /* USB Product ID of Mobile-CASSY */ 44 #define USB_DEVICE_ID_LD_MOBILECASSY2 0x1021 /* USB Product ID of Mobile-CASSY 2 (reserved) */ 45 #define USB_DEVICE_ID_LD_MICROCASSYVOLTAGE 0x1031 /* USB Product ID of Micro-CASSY Voltage */ 46 #define USB_DEVICE_ID_LD_MICROCASSYCURRENT 0x1032 /* USB Product ID of Micro-CASSY Current */ 47 #define USB_DEVICE_ID_LD_MICROCASSYTIME 0x1033 /* USB Product ID of Micro-CASSY Time (reserved) */ 48 #define USB_DEVICE_ID_LD_MICROCASSYTEMPERATURE 0x1035 /* USB Product ID of Micro-CASSY Temperature */ 49 #define USB_DEVICE_ID_LD_MICROCASSYPH 0x1038 /* USB Product ID of Micro-CASSY pH */ 50 #define USB_DEVICE_ID_LD_JWM 0x1080 /* USB Product ID of Joule and Wattmeter */ 51 #define USB_DEVICE_ID_LD_DMMP 0x1081 /* USB Product ID of Digital Multimeter P (reserved) */ 52 #define USB_DEVICE_ID_LD_UMIP 0x1090 /* USB Product ID of UMI P */ 53 #define USB_DEVICE_ID_LD_UMIC 0x10A0 /* USB Product ID of UMI C */ 54 #define USB_DEVICE_ID_LD_UMIB 0x10B0 /* USB Product ID of UMI B */ 55 #define USB_DEVICE_ID_LD_XRAY 0x1100 /* USB Product ID of X-Ray Apparatus 55481 */ 56 #define USB_DEVICE_ID_LD_XRAY2 0x1101 /* USB Product ID of X-Ray Apparatus 554800 */ 57 #define USB_DEVICE_ID_LD_XRAYCT 0x1110 /* USB Product ID of X-Ray Apparatus CT 554821*/ 58 #define USB_DEVICE_ID_LD_VIDEOCOM 0x1200 /* USB Product ID of VideoCom */ 59 #define USB_DEVICE_ID_LD_MOTOR 0x1210 /* USB Product ID of Motor (reserved) */ 60 #define USB_DEVICE_ID_LD_COM3LAB 0x2000 /* USB Product ID of COM3LAB */ 61 #define USB_DEVICE_ID_LD_TELEPORT 0x2010 /* USB Product ID of Terminal Adapter */ 62 #define USB_DEVICE_ID_LD_NETWORKANALYSER 0x2020 /* USB Product ID of Network Analyser */ 63 #define USB_DEVICE_ID_LD_POWERCONTROL 0x2030 /* USB Product ID of Converter Control Unit */ 64 #define USB_DEVICE_ID_LD_MACHINETEST 0x2040 /* USB Product ID of Machine Test System */ 65 #define USB_DEVICE_ID_LD_MOSTANALYSER 0x2050 /* USB Product ID of MOST Protocol Analyser */ 66 #define USB_DEVICE_ID_LD_MOSTANALYSER2 0x2051 /* USB Product ID of MOST Protocol Analyser 2 */ 67 #define USB_DEVICE_ID_LD_ABSESP 0x2060 /* USB Product ID of ABS ESP */ 68 #define USB_DEVICE_ID_LD_AUTODATABUS 0x2070 /* USB Product ID of Automotive Data Buses */ 69 #define USB_DEVICE_ID_LD_MCT 0x2080 /* USB Product ID of Microcontroller technique */ 70 #define USB_DEVICE_ID_LD_HYBRID 0x2090 /* USB Product ID of Automotive Hybrid */ 71 #define USB_DEVICE_ID_LD_HEATCONTROL 0x20A0 /* USB Product ID of Heat control */ 72 73 #define USB_VENDOR_ID_VERNIER 0x08f7 74 #define USB_DEVICE_ID_VERNIER_GOTEMP 0x0002 75 #define USB_DEVICE_ID_VERNIER_SKIP 0x0003 76 #define USB_DEVICE_ID_VERNIER_CYCLOPS 0x0004 77 #define USB_DEVICE_ID_VERNIER_LCSPEC 0x0006 78 79 #ifdef CONFIG_USB_DYNAMIC_MINORS 80 #define USB_LD_MINOR_BASE 0 81 #else 82 #define USB_LD_MINOR_BASE 176 83 #endif 84 85 /* table of devices that work with this driver */ 86 static const struct usb_device_id ld_usb_table[] = { 87 { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_CASSY) }, 88 { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_CASSY2) }, 89 { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_POCKETCASSY) }, 90 { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_POCKETCASSY2) }, 91 { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MOBILECASSY) }, 92 { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MOBILECASSY2) }, 93 { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MICROCASSYVOLTAGE) }, 94 { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MICROCASSYCURRENT) }, 95 { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MICROCASSYTIME) }, 96 { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MICROCASSYTEMPERATURE) }, 97 { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MICROCASSYPH) }, 98 { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_JWM) }, 99 { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_DMMP) }, 100 { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_UMIP) }, 101 { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_UMIC) }, 102 { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_UMIB) }, 103 { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_XRAY) }, 104 { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_XRAY2) }, 105 { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_VIDEOCOM) }, 106 { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MOTOR) }, 107 { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_COM3LAB) }, 108 { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_TELEPORT) }, 109 { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_NETWORKANALYSER) }, 110 { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_POWERCONTROL) }, 111 { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MACHINETEST) }, 112 { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MOSTANALYSER) }, 113 { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MOSTANALYSER2) }, 114 { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_ABSESP) }, 115 { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_AUTODATABUS) }, 116 { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MCT) }, 117 { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_HYBRID) }, 118 { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_HEATCONTROL) }, 119 { USB_DEVICE(USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_GOTEMP) }, 120 { USB_DEVICE(USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_SKIP) }, 121 { USB_DEVICE(USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_CYCLOPS) }, 122 { USB_DEVICE(USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_LCSPEC) }, 123 { } /* Terminating entry */ 124 }; 125 MODULE_DEVICE_TABLE(usb, ld_usb_table); 126 MODULE_VERSION("V0.14"); 127 MODULE_AUTHOR("Michael Hund <mhund@ld-didactic.de>"); 128 MODULE_DESCRIPTION("LD USB Driver"); 129 MODULE_LICENSE("GPL"); 130 MODULE_SUPPORTED_DEVICE("LD USB Devices"); 131 132 /* All interrupt in transfers are collected in a ring buffer to 133 * avoid racing conditions and get better performance of the driver. 134 */ 135 static int ring_buffer_size = 128; 136 module_param(ring_buffer_size, int, 0); 137 MODULE_PARM_DESC(ring_buffer_size, "Read ring buffer size in reports"); 138 139 /* The write_buffer can contain more than one interrupt out transfer. 140 */ 141 static int write_buffer_size = 10; 142 module_param(write_buffer_size, int, 0); 143 MODULE_PARM_DESC(write_buffer_size, "Write buffer size in reports"); 144 145 /* As of kernel version 2.6.4 ehci-hcd uses an 146 * "only one interrupt transfer per frame" shortcut 147 * to simplify the scheduling of periodic transfers. 148 * This conflicts with our standard 1ms intervals for in and out URBs. 149 * We use default intervals of 2ms for in and 2ms for out transfers, 150 * which should be fast enough. 151 * Increase the interval to allow more devices that do interrupt transfers, 152 * or set to 1 to use the standard interval from the endpoint descriptors. 153 */ 154 static int min_interrupt_in_interval = 2; 155 module_param(min_interrupt_in_interval, int, 0); 156 MODULE_PARM_DESC(min_interrupt_in_interval, "Minimum interrupt in interval in ms"); 157 158 static int min_interrupt_out_interval = 2; 159 module_param(min_interrupt_out_interval, int, 0); 160 MODULE_PARM_DESC(min_interrupt_out_interval, "Minimum interrupt out interval in ms"); 161 162 /* Structure to hold all of our device specific stuff */ 163 struct ld_usb { 164 struct mutex mutex; /* locks this structure */ 165 struct usb_interface* intf; /* save off the usb interface pointer */ 166 167 int open_count; /* number of times this port has been opened */ 168 169 char* ring_buffer; 170 unsigned int ring_head; 171 unsigned int ring_tail; 172 173 wait_queue_head_t read_wait; 174 wait_queue_head_t write_wait; 175 176 char* interrupt_in_buffer; 177 struct usb_endpoint_descriptor* interrupt_in_endpoint; 178 struct urb* interrupt_in_urb; 179 int interrupt_in_interval; 180 size_t interrupt_in_endpoint_size; 181 int interrupt_in_running; 182 int interrupt_in_done; 183 int buffer_overflow; 184 spinlock_t rbsl; 185 186 char* interrupt_out_buffer; 187 struct usb_endpoint_descriptor* interrupt_out_endpoint; 188 struct urb* interrupt_out_urb; 189 int interrupt_out_interval; 190 size_t interrupt_out_endpoint_size; 191 int interrupt_out_busy; 192 }; 193 194 static struct usb_driver ld_usb_driver; 195 196 /** 197 * ld_usb_abort_transfers 198 * aborts transfers and frees associated data structures 199 */ 200 static void ld_usb_abort_transfers(struct ld_usb *dev) 201 { 202 /* shutdown transfer */ 203 if (dev->interrupt_in_running) { 204 dev->interrupt_in_running = 0; 205 if (dev->intf) 206 usb_kill_urb(dev->interrupt_in_urb); 207 } 208 if (dev->interrupt_out_busy) 209 if (dev->intf) 210 usb_kill_urb(dev->interrupt_out_urb); 211 } 212 213 /** 214 * ld_usb_delete 215 */ 216 static void ld_usb_delete(struct ld_usb *dev) 217 { 218 ld_usb_abort_transfers(dev); 219 220 /* free data structures */ 221 usb_free_urb(dev->interrupt_in_urb); 222 usb_free_urb(dev->interrupt_out_urb); 223 kfree(dev->ring_buffer); 224 kfree(dev->interrupt_in_buffer); 225 kfree(dev->interrupt_out_buffer); 226 kfree(dev); 227 } 228 229 /** 230 * ld_usb_interrupt_in_callback 231 */ 232 static void ld_usb_interrupt_in_callback(struct urb *urb) 233 { 234 struct ld_usb *dev = urb->context; 235 size_t *actual_buffer; 236 unsigned int next_ring_head; 237 int status = urb->status; 238 int retval; 239 240 if (status) { 241 if (status == -ENOENT || 242 status == -ECONNRESET || 243 status == -ESHUTDOWN) { 244 goto exit; 245 } else { 246 dev_dbg(&dev->intf->dev, 247 "%s: nonzero status received: %d\n", __func__, 248 status); 249 spin_lock(&dev->rbsl); 250 goto resubmit; /* maybe we can recover */ 251 } 252 } 253 254 spin_lock(&dev->rbsl); 255 if (urb->actual_length > 0) { 256 next_ring_head = (dev->ring_head+1) % ring_buffer_size; 257 if (next_ring_head != dev->ring_tail) { 258 actual_buffer = (size_t*)(dev->ring_buffer + dev->ring_head*(sizeof(size_t)+dev->interrupt_in_endpoint_size)); 259 /* actual_buffer gets urb->actual_length + interrupt_in_buffer */ 260 *actual_buffer = urb->actual_length; 261 memcpy(actual_buffer+1, dev->interrupt_in_buffer, urb->actual_length); 262 dev->ring_head = next_ring_head; 263 dev_dbg(&dev->intf->dev, "%s: received %d bytes\n", 264 __func__, urb->actual_length); 265 } else { 266 dev_warn(&dev->intf->dev, 267 "Ring buffer overflow, %d bytes dropped\n", 268 urb->actual_length); 269 dev->buffer_overflow = 1; 270 } 271 } 272 273 resubmit: 274 /* resubmit if we're still running */ 275 if (dev->interrupt_in_running && !dev->buffer_overflow && dev->intf) { 276 retval = usb_submit_urb(dev->interrupt_in_urb, GFP_ATOMIC); 277 if (retval) { 278 dev_err(&dev->intf->dev, 279 "usb_submit_urb failed (%d)\n", retval); 280 dev->buffer_overflow = 1; 281 } 282 } 283 spin_unlock(&dev->rbsl); 284 exit: 285 dev->interrupt_in_done = 1; 286 wake_up_interruptible(&dev->read_wait); 287 } 288 289 /** 290 * ld_usb_interrupt_out_callback 291 */ 292 static void ld_usb_interrupt_out_callback(struct urb *urb) 293 { 294 struct ld_usb *dev = urb->context; 295 int status = urb->status; 296 297 /* sync/async unlink faults aren't errors */ 298 if (status && !(status == -ENOENT || 299 status == -ECONNRESET || 300 status == -ESHUTDOWN)) 301 dev_dbg(&dev->intf->dev, 302 "%s - nonzero write interrupt status received: %d\n", 303 __func__, status); 304 305 dev->interrupt_out_busy = 0; 306 wake_up_interruptible(&dev->write_wait); 307 } 308 309 /** 310 * ld_usb_open 311 */ 312 static int ld_usb_open(struct inode *inode, struct file *file) 313 { 314 struct ld_usb *dev; 315 int subminor; 316 int retval; 317 struct usb_interface *interface; 318 319 nonseekable_open(inode, file); 320 subminor = iminor(inode); 321 322 interface = usb_find_interface(&ld_usb_driver, subminor); 323 324 if (!interface) { 325 printk(KERN_ERR "%s - error, can't find device for minor %d\n", 326 __func__, subminor); 327 return -ENODEV; 328 } 329 330 dev = usb_get_intfdata(interface); 331 332 if (!dev) 333 return -ENODEV; 334 335 /* lock this device */ 336 if (mutex_lock_interruptible(&dev->mutex)) 337 return -ERESTARTSYS; 338 339 /* allow opening only once */ 340 if (dev->open_count) { 341 retval = -EBUSY; 342 goto unlock_exit; 343 } 344 dev->open_count = 1; 345 346 /* initialize in direction */ 347 dev->ring_head = 0; 348 dev->ring_tail = 0; 349 dev->buffer_overflow = 0; 350 usb_fill_int_urb(dev->interrupt_in_urb, 351 interface_to_usbdev(interface), 352 usb_rcvintpipe(interface_to_usbdev(interface), 353 dev->interrupt_in_endpoint->bEndpointAddress), 354 dev->interrupt_in_buffer, 355 dev->interrupt_in_endpoint_size, 356 ld_usb_interrupt_in_callback, 357 dev, 358 dev->interrupt_in_interval); 359 360 dev->interrupt_in_running = 1; 361 dev->interrupt_in_done = 0; 362 363 retval = usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL); 364 if (retval) { 365 dev_err(&interface->dev, "Couldn't submit interrupt_in_urb %d\n", retval); 366 dev->interrupt_in_running = 0; 367 dev->open_count = 0; 368 goto unlock_exit; 369 } 370 371 /* save device in the file's private structure */ 372 file->private_data = dev; 373 374 unlock_exit: 375 mutex_unlock(&dev->mutex); 376 377 return retval; 378 } 379 380 /** 381 * ld_usb_release 382 */ 383 static int ld_usb_release(struct inode *inode, struct file *file) 384 { 385 struct ld_usb *dev; 386 int retval = 0; 387 388 dev = file->private_data; 389 390 if (dev == NULL) { 391 retval = -ENODEV; 392 goto exit; 393 } 394 395 if (mutex_lock_interruptible(&dev->mutex)) { 396 retval = -ERESTARTSYS; 397 goto exit; 398 } 399 400 if (dev->open_count != 1) { 401 retval = -ENODEV; 402 goto unlock_exit; 403 } 404 if (dev->intf == NULL) { 405 /* the device was unplugged before the file was released */ 406 mutex_unlock(&dev->mutex); 407 /* unlock here as ld_usb_delete frees dev */ 408 ld_usb_delete(dev); 409 goto exit; 410 } 411 412 /* wait until write transfer is finished */ 413 if (dev->interrupt_out_busy) 414 wait_event_interruptible_timeout(dev->write_wait, !dev->interrupt_out_busy, 2 * HZ); 415 ld_usb_abort_transfers(dev); 416 dev->open_count = 0; 417 418 unlock_exit: 419 mutex_unlock(&dev->mutex); 420 421 exit: 422 return retval; 423 } 424 425 /** 426 * ld_usb_poll 427 */ 428 static unsigned int ld_usb_poll(struct file *file, poll_table *wait) 429 { 430 struct ld_usb *dev; 431 unsigned int mask = 0; 432 433 dev = file->private_data; 434 435 if (!dev->intf) 436 return POLLERR | POLLHUP; 437 438 poll_wait(file, &dev->read_wait, wait); 439 poll_wait(file, &dev->write_wait, wait); 440 441 if (dev->ring_head != dev->ring_tail) 442 mask |= POLLIN | POLLRDNORM; 443 if (!dev->interrupt_out_busy) 444 mask |= POLLOUT | POLLWRNORM; 445 446 return mask; 447 } 448 449 /** 450 * ld_usb_read 451 */ 452 static ssize_t ld_usb_read(struct file *file, char __user *buffer, size_t count, 453 loff_t *ppos) 454 { 455 struct ld_usb *dev; 456 size_t *actual_buffer; 457 size_t bytes_to_read; 458 int retval = 0; 459 int rv; 460 461 dev = file->private_data; 462 463 /* verify that we actually have some data to read */ 464 if (count == 0) 465 goto exit; 466 467 /* lock this object */ 468 if (mutex_lock_interruptible(&dev->mutex)) { 469 retval = -ERESTARTSYS; 470 goto exit; 471 } 472 473 /* verify that the device wasn't unplugged */ 474 if (dev->intf == NULL) { 475 retval = -ENODEV; 476 printk(KERN_ERR "ldusb: No device or device unplugged %d\n", retval); 477 goto unlock_exit; 478 } 479 480 /* wait for data */ 481 spin_lock_irq(&dev->rbsl); 482 if (dev->ring_head == dev->ring_tail) { 483 dev->interrupt_in_done = 0; 484 spin_unlock_irq(&dev->rbsl); 485 if (file->f_flags & O_NONBLOCK) { 486 retval = -EAGAIN; 487 goto unlock_exit; 488 } 489 retval = wait_event_interruptible(dev->read_wait, dev->interrupt_in_done); 490 if (retval < 0) 491 goto unlock_exit; 492 } else { 493 spin_unlock_irq(&dev->rbsl); 494 } 495 496 /* actual_buffer contains actual_length + interrupt_in_buffer */ 497 actual_buffer = (size_t*)(dev->ring_buffer + dev->ring_tail*(sizeof(size_t)+dev->interrupt_in_endpoint_size)); 498 bytes_to_read = min(count, *actual_buffer); 499 if (bytes_to_read < *actual_buffer) 500 dev_warn(&dev->intf->dev, "Read buffer overflow, %zd bytes dropped\n", 501 *actual_buffer-bytes_to_read); 502 503 /* copy one interrupt_in_buffer from ring_buffer into userspace */ 504 if (copy_to_user(buffer, actual_buffer+1, bytes_to_read)) { 505 retval = -EFAULT; 506 goto unlock_exit; 507 } 508 dev->ring_tail = (dev->ring_tail+1) % ring_buffer_size; 509 510 retval = bytes_to_read; 511 512 spin_lock_irq(&dev->rbsl); 513 if (dev->buffer_overflow) { 514 dev->buffer_overflow = 0; 515 spin_unlock_irq(&dev->rbsl); 516 rv = usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL); 517 if (rv < 0) 518 dev->buffer_overflow = 1; 519 } else { 520 spin_unlock_irq(&dev->rbsl); 521 } 522 523 unlock_exit: 524 /* unlock the device */ 525 mutex_unlock(&dev->mutex); 526 527 exit: 528 return retval; 529 } 530 531 /** 532 * ld_usb_write 533 */ 534 static ssize_t ld_usb_write(struct file *file, const char __user *buffer, 535 size_t count, loff_t *ppos) 536 { 537 struct ld_usb *dev; 538 size_t bytes_to_write; 539 int retval = 0; 540 541 dev = file->private_data; 542 543 /* verify that we actually have some data to write */ 544 if (count == 0) 545 goto exit; 546 547 /* lock this object */ 548 if (mutex_lock_interruptible(&dev->mutex)) { 549 retval = -ERESTARTSYS; 550 goto exit; 551 } 552 553 /* verify that the device wasn't unplugged */ 554 if (dev->intf == NULL) { 555 retval = -ENODEV; 556 printk(KERN_ERR "ldusb: No device or device unplugged %d\n", retval); 557 goto unlock_exit; 558 } 559 560 /* wait until previous transfer is finished */ 561 if (dev->interrupt_out_busy) { 562 if (file->f_flags & O_NONBLOCK) { 563 retval = -EAGAIN; 564 goto unlock_exit; 565 } 566 retval = wait_event_interruptible(dev->write_wait, !dev->interrupt_out_busy); 567 if (retval < 0) { 568 goto unlock_exit; 569 } 570 } 571 572 /* write the data into interrupt_out_buffer from userspace */ 573 bytes_to_write = min(count, write_buffer_size*dev->interrupt_out_endpoint_size); 574 if (bytes_to_write < count) 575 dev_warn(&dev->intf->dev, "Write buffer overflow, %zd bytes dropped\n",count-bytes_to_write); 576 dev_dbg(&dev->intf->dev, "%s: count = %zd, bytes_to_write = %zd\n", 577 __func__, count, bytes_to_write); 578 579 if (copy_from_user(dev->interrupt_out_buffer, buffer, bytes_to_write)) { 580 retval = -EFAULT; 581 goto unlock_exit; 582 } 583 584 if (dev->interrupt_out_endpoint == NULL) { 585 /* try HID_REQ_SET_REPORT=9 on control_endpoint instead of interrupt_out_endpoint */ 586 retval = usb_control_msg(interface_to_usbdev(dev->intf), 587 usb_sndctrlpipe(interface_to_usbdev(dev->intf), 0), 588 9, 589 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT, 590 1 << 8, 0, 591 dev->interrupt_out_buffer, 592 bytes_to_write, 593 USB_CTRL_SET_TIMEOUT * HZ); 594 if (retval < 0) 595 dev_err(&dev->intf->dev, 596 "Couldn't submit HID_REQ_SET_REPORT %d\n", 597 retval); 598 goto unlock_exit; 599 } 600 601 /* send off the urb */ 602 usb_fill_int_urb(dev->interrupt_out_urb, 603 interface_to_usbdev(dev->intf), 604 usb_sndintpipe(interface_to_usbdev(dev->intf), 605 dev->interrupt_out_endpoint->bEndpointAddress), 606 dev->interrupt_out_buffer, 607 bytes_to_write, 608 ld_usb_interrupt_out_callback, 609 dev, 610 dev->interrupt_out_interval); 611 612 dev->interrupt_out_busy = 1; 613 wmb(); 614 615 retval = usb_submit_urb(dev->interrupt_out_urb, GFP_KERNEL); 616 if (retval) { 617 dev->interrupt_out_busy = 0; 618 dev_err(&dev->intf->dev, 619 "Couldn't submit interrupt_out_urb %d\n", retval); 620 goto unlock_exit; 621 } 622 retval = bytes_to_write; 623 624 unlock_exit: 625 /* unlock the device */ 626 mutex_unlock(&dev->mutex); 627 628 exit: 629 return retval; 630 } 631 632 /* file operations needed when we register this driver */ 633 static const struct file_operations ld_usb_fops = { 634 .owner = THIS_MODULE, 635 .read = ld_usb_read, 636 .write = ld_usb_write, 637 .open = ld_usb_open, 638 .release = ld_usb_release, 639 .poll = ld_usb_poll, 640 .llseek = no_llseek, 641 }; 642 643 /* 644 * usb class driver info in order to get a minor number from the usb core, 645 * and to have the device registered with the driver core 646 */ 647 static struct usb_class_driver ld_usb_class = { 648 .name = "ldusb%d", 649 .fops = &ld_usb_fops, 650 .minor_base = USB_LD_MINOR_BASE, 651 }; 652 653 /** 654 * ld_usb_probe 655 * 656 * Called by the usb core when a new device is connected that it thinks 657 * this driver might be interested in. 658 */ 659 static int ld_usb_probe(struct usb_interface *intf, const struct usb_device_id *id) 660 { 661 struct usb_device *udev = interface_to_usbdev(intf); 662 struct ld_usb *dev = NULL; 663 struct usb_host_interface *iface_desc; 664 struct usb_endpoint_descriptor *endpoint; 665 char *buffer; 666 int i; 667 int retval = -ENOMEM; 668 669 /* allocate memory for our device state and initialize it */ 670 671 dev = kzalloc(sizeof(*dev), GFP_KERNEL); 672 if (dev == NULL) { 673 dev_err(&intf->dev, "Out of memory\n"); 674 goto exit; 675 } 676 mutex_init(&dev->mutex); 677 spin_lock_init(&dev->rbsl); 678 dev->intf = intf; 679 init_waitqueue_head(&dev->read_wait); 680 init_waitqueue_head(&dev->write_wait); 681 682 /* workaround for early firmware versions on fast computers */ 683 if ((le16_to_cpu(udev->descriptor.idVendor) == USB_VENDOR_ID_LD) && 684 ((le16_to_cpu(udev->descriptor.idProduct) == USB_DEVICE_ID_LD_CASSY) || 685 (le16_to_cpu(udev->descriptor.idProduct) == USB_DEVICE_ID_LD_COM3LAB)) && 686 (le16_to_cpu(udev->descriptor.bcdDevice) <= 0x103)) { 687 buffer = kmalloc(256, GFP_KERNEL); 688 if (buffer == NULL) { 689 dev_err(&intf->dev, "Couldn't allocate string buffer\n"); 690 goto error; 691 } 692 /* usb_string makes SETUP+STALL to leave always ControlReadLoop */ 693 usb_string(udev, 255, buffer, 256); 694 kfree(buffer); 695 } 696 697 iface_desc = intf->cur_altsetting; 698 699 /* set up the endpoint information */ 700 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) { 701 endpoint = &iface_desc->endpoint[i].desc; 702 703 if (usb_endpoint_is_int_in(endpoint)) 704 dev->interrupt_in_endpoint = endpoint; 705 706 if (usb_endpoint_is_int_out(endpoint)) 707 dev->interrupt_out_endpoint = endpoint; 708 } 709 if (dev->interrupt_in_endpoint == NULL) { 710 dev_err(&intf->dev, "Interrupt in endpoint not found\n"); 711 goto error; 712 } 713 if (dev->interrupt_out_endpoint == NULL) 714 dev_warn(&intf->dev, "Interrupt out endpoint not found (using control endpoint instead)\n"); 715 716 dev->interrupt_in_endpoint_size = usb_endpoint_maxp(dev->interrupt_in_endpoint); 717 dev->ring_buffer = kmalloc(ring_buffer_size*(sizeof(size_t)+dev->interrupt_in_endpoint_size), GFP_KERNEL); 718 if (!dev->ring_buffer) { 719 dev_err(&intf->dev, "Couldn't allocate ring_buffer\n"); 720 goto error; 721 } 722 dev->interrupt_in_buffer = kmalloc(dev->interrupt_in_endpoint_size, GFP_KERNEL); 723 if (!dev->interrupt_in_buffer) { 724 dev_err(&intf->dev, "Couldn't allocate interrupt_in_buffer\n"); 725 goto error; 726 } 727 dev->interrupt_in_urb = usb_alloc_urb(0, GFP_KERNEL); 728 if (!dev->interrupt_in_urb) { 729 dev_err(&intf->dev, "Couldn't allocate interrupt_in_urb\n"); 730 goto error; 731 } 732 dev->interrupt_out_endpoint_size = dev->interrupt_out_endpoint ? usb_endpoint_maxp(dev->interrupt_out_endpoint) : 733 udev->descriptor.bMaxPacketSize0; 734 dev->interrupt_out_buffer = kmalloc(write_buffer_size*dev->interrupt_out_endpoint_size, GFP_KERNEL); 735 if (!dev->interrupt_out_buffer) { 736 dev_err(&intf->dev, "Couldn't allocate interrupt_out_buffer\n"); 737 goto error; 738 } 739 dev->interrupt_out_urb = usb_alloc_urb(0, GFP_KERNEL); 740 if (!dev->interrupt_out_urb) { 741 dev_err(&intf->dev, "Couldn't allocate interrupt_out_urb\n"); 742 goto error; 743 } 744 dev->interrupt_in_interval = min_interrupt_in_interval > dev->interrupt_in_endpoint->bInterval ? min_interrupt_in_interval : dev->interrupt_in_endpoint->bInterval; 745 if (dev->interrupt_out_endpoint) 746 dev->interrupt_out_interval = min_interrupt_out_interval > dev->interrupt_out_endpoint->bInterval ? min_interrupt_out_interval : dev->interrupt_out_endpoint->bInterval; 747 748 /* we can register the device now, as it is ready */ 749 usb_set_intfdata(intf, dev); 750 751 retval = usb_register_dev(intf, &ld_usb_class); 752 if (retval) { 753 /* something prevented us from registering this driver */ 754 dev_err(&intf->dev, "Not able to get a minor for this device.\n"); 755 usb_set_intfdata(intf, NULL); 756 goto error; 757 } 758 759 /* let the user know what node this device is now attached to */ 760 dev_info(&intf->dev, "LD USB Device #%d now attached to major %d minor %d\n", 761 (intf->minor - USB_LD_MINOR_BASE), USB_MAJOR, intf->minor); 762 763 exit: 764 return retval; 765 766 error: 767 ld_usb_delete(dev); 768 769 return retval; 770 } 771 772 /** 773 * ld_usb_disconnect 774 * 775 * Called by the usb core when the device is removed from the system. 776 */ 777 static void ld_usb_disconnect(struct usb_interface *intf) 778 { 779 struct ld_usb *dev; 780 int minor; 781 782 dev = usb_get_intfdata(intf); 783 usb_set_intfdata(intf, NULL); 784 785 minor = intf->minor; 786 787 /* give back our minor */ 788 usb_deregister_dev(intf, &ld_usb_class); 789 790 mutex_lock(&dev->mutex); 791 792 /* if the device is not opened, then we clean up right now */ 793 if (!dev->open_count) { 794 mutex_unlock(&dev->mutex); 795 ld_usb_delete(dev); 796 } else { 797 dev->intf = NULL; 798 /* wake up pollers */ 799 wake_up_interruptible_all(&dev->read_wait); 800 wake_up_interruptible_all(&dev->write_wait); 801 mutex_unlock(&dev->mutex); 802 } 803 804 dev_info(&intf->dev, "LD USB Device #%d now disconnected\n", 805 (minor - USB_LD_MINOR_BASE)); 806 } 807 808 /* usb specific object needed to register this driver with the usb subsystem */ 809 static struct usb_driver ld_usb_driver = { 810 .name = "ldusb", 811 .probe = ld_usb_probe, 812 .disconnect = ld_usb_disconnect, 813 .id_table = ld_usb_table, 814 }; 815 816 module_usb_driver(ld_usb_driver); 817 818