1 /* 2 * LEGO USB Tower driver 3 * 4 * Copyright (C) 2003 David Glance <davidgsf@sourceforge.net> 5 * 2001-2004 Juergen Stuber <starblue@users.sourceforge.net> 6 * 7 * This program is free software; you can redistribute it and/or 8 * modify it under the terms of the GNU General Public License as 9 * published by the Free Software Foundation; either version 2 of 10 * the License, or (at your option) any later version. 11 * 12 * derived from USB Skeleton driver - 0.5 13 * Copyright (C) 2001 Greg Kroah-Hartman (greg@kroah.com) 14 * 15 * History: 16 * 17 * 2001-10-13 - 0.1 js 18 * - first version 19 * 2001-11-03 - 0.2 js 20 * - simplified buffering, one-shot URBs for writing 21 * 2001-11-10 - 0.3 js 22 * - removed IOCTL (setting power/mode is more complicated, postponed) 23 * 2001-11-28 - 0.4 js 24 * - added vendor commands for mode of operation and power level in open 25 * 2001-12-04 - 0.5 js 26 * - set IR mode by default (by oversight 0.4 set VLL mode) 27 * 2002-01-11 - 0.5? pcchan 28 * - make read buffer reusable and work around bytes_to_write issue between 29 * uhci and legusbtower 30 * 2002-09-23 - 0.52 david (david@csse.uwa.edu.au) 31 * - imported into lejos project 32 * - changed wake_up to wake_up_interruptible 33 * - changed to use lego0 rather than tower0 34 * - changed dbg() to use __func__ rather than deprecated __func__ 35 * 2003-01-12 - 0.53 david (david@csse.uwa.edu.au) 36 * - changed read and write to write everything or 37 * timeout (from a patch by Chris Riesen and Brett Thaeler driver) 38 * - added ioctl functionality to set timeouts 39 * 2003-07-18 - 0.54 davidgsf (david@csse.uwa.edu.au) 40 * - initial import into LegoUSB project 41 * - merge of existing LegoUSB.c driver 42 * 2003-07-18 - 0.56 davidgsf (david@csse.uwa.edu.au) 43 * - port to 2.6 style driver 44 * 2004-02-29 - 0.6 Juergen Stuber <starblue@users.sourceforge.net> 45 * - fix locking 46 * - unlink read URBs which are no longer needed 47 * - allow increased buffer size, eliminates need for timeout on write 48 * - have read URB running continuously 49 * - added poll 50 * - forbid seeking 51 * - added nonblocking I/O 52 * - changed back __func__ to __func__ 53 * - read and log tower firmware version 54 * - reset tower on probe, avoids failure of first write 55 * 2004-03-09 - 0.7 Juergen Stuber <starblue@users.sourceforge.net> 56 * - timeout read now only after inactivity, shorten default accordingly 57 * 2004-03-11 - 0.8 Juergen Stuber <starblue@users.sourceforge.net> 58 * - log major, minor instead of possibly confusing device filename 59 * - whitespace cleanup 60 * 2004-03-12 - 0.9 Juergen Stuber <starblue@users.sourceforge.net> 61 * - normalize whitespace in debug messages 62 * - take care about endianness in control message responses 63 * 2004-03-13 - 0.91 Juergen Stuber <starblue@users.sourceforge.net> 64 * - make default intervals longer to accommodate current EHCI driver 65 * 2004-03-19 - 0.92 Juergen Stuber <starblue@users.sourceforge.net> 66 * - replaced atomic_t by memory barriers 67 * 2004-04-21 - 0.93 Juergen Stuber <starblue@users.sourceforge.net> 68 * - wait for completion of write urb in release (needed for remotecontrol) 69 * - corrected poll for write direction (missing negation) 70 * 2004-04-22 - 0.94 Juergen Stuber <starblue@users.sourceforge.net> 71 * - make device locking interruptible 72 * 2004-04-30 - 0.95 Juergen Stuber <starblue@users.sourceforge.net> 73 * - check for valid udev on resubmitting and unlinking urbs 74 * 2004-08-03 - 0.96 Juergen Stuber <starblue@users.sourceforge.net> 75 * - move reset into open to clean out spurious data 76 */ 77 78 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 79 80 #include <linux/kernel.h> 81 #include <linux/errno.h> 82 #include <linux/init.h> 83 #include <linux/slab.h> 84 #include <linux/module.h> 85 #include <linux/completion.h> 86 #include <linux/mutex.h> 87 #include <asm/uaccess.h> 88 #include <linux/usb.h> 89 #include <linux/poll.h> 90 91 92 /* Version Information */ 93 #define DRIVER_VERSION "v0.96" 94 #define DRIVER_AUTHOR "Juergen Stuber <starblue@sourceforge.net>" 95 #define DRIVER_DESC "LEGO USB Tower Driver" 96 97 98 /* The defaults are chosen to work with the latest versions of leJOS and NQC. 99 */ 100 101 /* Some legacy software likes to receive packets in one piece. 102 * In this case read_buffer_size should exceed the maximal packet length 103 * (417 for datalog uploads), and packet_timeout should be set. 104 */ 105 static int read_buffer_size = 480; 106 module_param(read_buffer_size, int, 0); 107 MODULE_PARM_DESC(read_buffer_size, "Read buffer size"); 108 109 /* Some legacy software likes to send packets in one piece. 110 * In this case write_buffer_size should exceed the maximal packet length 111 * (417 for firmware and program downloads). 112 * A problem with long writes is that the following read may time out 113 * if the software is not prepared to wait long enough. 114 */ 115 static int write_buffer_size = 480; 116 module_param(write_buffer_size, int, 0); 117 MODULE_PARM_DESC(write_buffer_size, "Write buffer size"); 118 119 /* Some legacy software expects reads to contain whole LASM packets. 120 * To achieve this, characters which arrive before a packet timeout 121 * occurs will be returned in a single read operation. 122 * A problem with long reads is that the software may time out 123 * if it is not prepared to wait long enough. 124 * The packet timeout should be greater than the time between the 125 * reception of subsequent characters, which should arrive about 126 * every 5ms for the standard 2400 baud. 127 * Set it to 0 to disable. 128 */ 129 static int packet_timeout = 50; 130 module_param(packet_timeout, int, 0); 131 MODULE_PARM_DESC(packet_timeout, "Packet timeout in ms"); 132 133 /* Some legacy software expects blocking reads to time out. 134 * Timeout occurs after the specified time of read and write inactivity. 135 * Set it to 0 to disable. 136 */ 137 static int read_timeout = 200; 138 module_param(read_timeout, int, 0); 139 MODULE_PARM_DESC(read_timeout, "Read timeout in ms"); 140 141 /* As of kernel version 2.6.4 ehci-hcd uses an 142 * "only one interrupt transfer per frame" shortcut 143 * to simplify the scheduling of periodic transfers. 144 * This conflicts with our standard 1ms intervals for in and out URBs. 145 * We use default intervals of 2ms for in and 8ms for out transfers, 146 * which is fast enough for 2400 baud and allows a small additional load. 147 * Increase the interval to allow more devices that do interrupt transfers, 148 * or set to 0 to use the standard interval from the endpoint descriptors. 149 */ 150 static int interrupt_in_interval = 2; 151 module_param(interrupt_in_interval, int, 0); 152 MODULE_PARM_DESC(interrupt_in_interval, "Interrupt in interval in ms"); 153 154 static int interrupt_out_interval = 8; 155 module_param(interrupt_out_interval, int, 0); 156 MODULE_PARM_DESC(interrupt_out_interval, "Interrupt out interval in ms"); 157 158 /* Define these values to match your device */ 159 #define LEGO_USB_TOWER_VENDOR_ID 0x0694 160 #define LEGO_USB_TOWER_PRODUCT_ID 0x0001 161 162 /* Vendor requests */ 163 #define LEGO_USB_TOWER_REQUEST_RESET 0x04 164 #define LEGO_USB_TOWER_REQUEST_GET_VERSION 0xFD 165 166 struct tower_reset_reply { 167 __le16 size; /* little-endian */ 168 __u8 err_code; 169 __u8 spare; 170 } __attribute__ ((packed)); 171 172 struct tower_get_version_reply { 173 __le16 size; /* little-endian */ 174 __u8 err_code; 175 __u8 spare; 176 __u8 major; 177 __u8 minor; 178 __le16 build_no; /* little-endian */ 179 } __attribute__ ((packed)); 180 181 182 /* table of devices that work with this driver */ 183 static const struct usb_device_id tower_table[] = { 184 { USB_DEVICE(LEGO_USB_TOWER_VENDOR_ID, LEGO_USB_TOWER_PRODUCT_ID) }, 185 { } /* Terminating entry */ 186 }; 187 188 MODULE_DEVICE_TABLE (usb, tower_table); 189 static DEFINE_MUTEX(open_disc_mutex); 190 191 #define LEGO_USB_TOWER_MINOR_BASE 160 192 193 194 /* Structure to hold all of our device specific stuff */ 195 struct lego_usb_tower { 196 struct mutex lock; /* locks this structure */ 197 struct usb_device* udev; /* save off the usb device pointer */ 198 unsigned char minor; /* the starting minor number for this device */ 199 200 int open_count; /* number of times this port has been opened */ 201 202 char* read_buffer; 203 size_t read_buffer_length; /* this much came in */ 204 size_t read_packet_length; /* this much will be returned on read */ 205 spinlock_t read_buffer_lock; 206 int packet_timeout_jiffies; 207 unsigned long read_last_arrival; 208 209 wait_queue_head_t read_wait; 210 wait_queue_head_t write_wait; 211 212 char* interrupt_in_buffer; 213 struct usb_endpoint_descriptor* interrupt_in_endpoint; 214 struct urb* interrupt_in_urb; 215 int interrupt_in_interval; 216 int interrupt_in_running; 217 int interrupt_in_done; 218 219 char* interrupt_out_buffer; 220 struct usb_endpoint_descriptor* interrupt_out_endpoint; 221 struct urb* interrupt_out_urb; 222 int interrupt_out_interval; 223 int interrupt_out_busy; 224 225 }; 226 227 228 /* local function prototypes */ 229 static ssize_t tower_read (struct file *file, char __user *buffer, size_t count, loff_t *ppos); 230 static ssize_t tower_write (struct file *file, const char __user *buffer, size_t count, loff_t *ppos); 231 static inline void tower_delete (struct lego_usb_tower *dev); 232 static int tower_open (struct inode *inode, struct file *file); 233 static int tower_release (struct inode *inode, struct file *file); 234 static unsigned int tower_poll (struct file *file, poll_table *wait); 235 static loff_t tower_llseek (struct file *file, loff_t off, int whence); 236 237 static void tower_abort_transfers (struct lego_usb_tower *dev); 238 static void tower_check_for_read_packet (struct lego_usb_tower *dev); 239 static void tower_interrupt_in_callback (struct urb *urb); 240 static void tower_interrupt_out_callback (struct urb *urb); 241 242 static int tower_probe (struct usb_interface *interface, const struct usb_device_id *id); 243 static void tower_disconnect (struct usb_interface *interface); 244 245 246 /* file operations needed when we register this driver */ 247 static const struct file_operations tower_fops = { 248 .owner = THIS_MODULE, 249 .read = tower_read, 250 .write = tower_write, 251 .open = tower_open, 252 .release = tower_release, 253 .poll = tower_poll, 254 .llseek = tower_llseek, 255 }; 256 257 static char *legousbtower_devnode(struct device *dev, umode_t *mode) 258 { 259 return kasprintf(GFP_KERNEL, "usb/%s", dev_name(dev)); 260 } 261 262 /* 263 * usb class driver info in order to get a minor number from the usb core, 264 * and to have the device registered with the driver core 265 */ 266 static struct usb_class_driver tower_class = { 267 .name = "legousbtower%d", 268 .devnode = legousbtower_devnode, 269 .fops = &tower_fops, 270 .minor_base = LEGO_USB_TOWER_MINOR_BASE, 271 }; 272 273 274 /* usb specific object needed to register this driver with the usb subsystem */ 275 static struct usb_driver tower_driver = { 276 .name = "legousbtower", 277 .probe = tower_probe, 278 .disconnect = tower_disconnect, 279 .id_table = tower_table, 280 }; 281 282 283 /** 284 * lego_usb_tower_debug_data 285 */ 286 static inline void lego_usb_tower_debug_data(struct device *dev, 287 const char *function, int size, 288 const unsigned char *data) 289 { 290 dev_dbg(dev, "%s - length = %d, data = %*ph\n", 291 function, size, size, data); 292 } 293 294 295 /** 296 * tower_delete 297 */ 298 static inline void tower_delete (struct lego_usb_tower *dev) 299 { 300 tower_abort_transfers (dev); 301 302 /* free data structures */ 303 usb_free_urb(dev->interrupt_in_urb); 304 usb_free_urb(dev->interrupt_out_urb); 305 kfree (dev->read_buffer); 306 kfree (dev->interrupt_in_buffer); 307 kfree (dev->interrupt_out_buffer); 308 kfree (dev); 309 } 310 311 312 /** 313 * tower_open 314 */ 315 static int tower_open (struct inode *inode, struct file *file) 316 { 317 struct lego_usb_tower *dev = NULL; 318 int subminor; 319 int retval = 0; 320 struct usb_interface *interface; 321 struct tower_reset_reply reset_reply; 322 int result; 323 324 nonseekable_open(inode, file); 325 subminor = iminor(inode); 326 327 interface = usb_find_interface (&tower_driver, subminor); 328 329 if (!interface) { 330 pr_err("error, can't find device for minor %d\n", subminor); 331 retval = -ENODEV; 332 goto exit; 333 } 334 335 mutex_lock(&open_disc_mutex); 336 dev = usb_get_intfdata(interface); 337 338 if (!dev) { 339 mutex_unlock(&open_disc_mutex); 340 retval = -ENODEV; 341 goto exit; 342 } 343 344 /* lock this device */ 345 if (mutex_lock_interruptible(&dev->lock)) { 346 mutex_unlock(&open_disc_mutex); 347 retval = -ERESTARTSYS; 348 goto exit; 349 } 350 351 352 /* allow opening only once */ 353 if (dev->open_count) { 354 mutex_unlock(&open_disc_mutex); 355 retval = -EBUSY; 356 goto unlock_exit; 357 } 358 dev->open_count = 1; 359 mutex_unlock(&open_disc_mutex); 360 361 /* reset the tower */ 362 result = usb_control_msg (dev->udev, 363 usb_rcvctrlpipe(dev->udev, 0), 364 LEGO_USB_TOWER_REQUEST_RESET, 365 USB_TYPE_VENDOR | USB_DIR_IN | USB_RECIP_DEVICE, 366 0, 367 0, 368 &reset_reply, 369 sizeof(reset_reply), 370 1000); 371 if (result < 0) { 372 dev_err(&dev->udev->dev, 373 "LEGO USB Tower reset control request failed\n"); 374 retval = result; 375 goto unlock_exit; 376 } 377 378 /* initialize in direction */ 379 dev->read_buffer_length = 0; 380 dev->read_packet_length = 0; 381 usb_fill_int_urb (dev->interrupt_in_urb, 382 dev->udev, 383 usb_rcvintpipe(dev->udev, dev->interrupt_in_endpoint->bEndpointAddress), 384 dev->interrupt_in_buffer, 385 usb_endpoint_maxp(dev->interrupt_in_endpoint), 386 tower_interrupt_in_callback, 387 dev, 388 dev->interrupt_in_interval); 389 390 dev->interrupt_in_running = 1; 391 dev->interrupt_in_done = 0; 392 mb(); 393 394 retval = usb_submit_urb (dev->interrupt_in_urb, GFP_KERNEL); 395 if (retval) { 396 dev_err(&dev->udev->dev, 397 "Couldn't submit interrupt_in_urb %d\n", retval); 398 dev->interrupt_in_running = 0; 399 dev->open_count = 0; 400 goto unlock_exit; 401 } 402 403 /* save device in the file's private structure */ 404 file->private_data = dev; 405 406 unlock_exit: 407 mutex_unlock(&dev->lock); 408 409 exit: 410 return retval; 411 } 412 413 /** 414 * tower_release 415 */ 416 static int tower_release (struct inode *inode, struct file *file) 417 { 418 struct lego_usb_tower *dev; 419 int retval = 0; 420 421 dev = file->private_data; 422 423 if (dev == NULL) { 424 retval = -ENODEV; 425 goto exit_nolock; 426 } 427 428 mutex_lock(&open_disc_mutex); 429 if (mutex_lock_interruptible(&dev->lock)) { 430 retval = -ERESTARTSYS; 431 goto exit; 432 } 433 434 if (dev->open_count != 1) { 435 dev_dbg(&dev->udev->dev, "%s: device not opened exactly once\n", 436 __func__); 437 retval = -ENODEV; 438 goto unlock_exit; 439 } 440 if (dev->udev == NULL) { 441 /* the device was unplugged before the file was released */ 442 443 /* unlock here as tower_delete frees dev */ 444 mutex_unlock(&dev->lock); 445 tower_delete (dev); 446 goto exit; 447 } 448 449 /* wait until write transfer is finished */ 450 if (dev->interrupt_out_busy) { 451 wait_event_interruptible_timeout (dev->write_wait, !dev->interrupt_out_busy, 2 * HZ); 452 } 453 tower_abort_transfers (dev); 454 dev->open_count = 0; 455 456 unlock_exit: 457 mutex_unlock(&dev->lock); 458 459 exit: 460 mutex_unlock(&open_disc_mutex); 461 exit_nolock: 462 return retval; 463 } 464 465 466 /** 467 * tower_abort_transfers 468 * aborts transfers and frees associated data structures 469 */ 470 static void tower_abort_transfers (struct lego_usb_tower *dev) 471 { 472 if (dev == NULL) 473 return; 474 475 /* shutdown transfer */ 476 if (dev->interrupt_in_running) { 477 dev->interrupt_in_running = 0; 478 mb(); 479 if (dev->udev) 480 usb_kill_urb (dev->interrupt_in_urb); 481 } 482 if (dev->interrupt_out_busy && dev->udev) 483 usb_kill_urb(dev->interrupt_out_urb); 484 } 485 486 487 /** 488 * tower_check_for_read_packet 489 * 490 * To get correct semantics for signals and non-blocking I/O 491 * with packetizing we pretend not to see any data in the read buffer 492 * until it has been there unchanged for at least 493 * dev->packet_timeout_jiffies, or until the buffer is full. 494 */ 495 static void tower_check_for_read_packet (struct lego_usb_tower *dev) 496 { 497 spin_lock_irq (&dev->read_buffer_lock); 498 if (!packet_timeout 499 || time_after(jiffies, dev->read_last_arrival + dev->packet_timeout_jiffies) 500 || dev->read_buffer_length == read_buffer_size) { 501 dev->read_packet_length = dev->read_buffer_length; 502 } 503 dev->interrupt_in_done = 0; 504 spin_unlock_irq (&dev->read_buffer_lock); 505 } 506 507 508 /** 509 * tower_poll 510 */ 511 static unsigned int tower_poll (struct file *file, poll_table *wait) 512 { 513 struct lego_usb_tower *dev; 514 unsigned int mask = 0; 515 516 dev = file->private_data; 517 518 if (!dev->udev) 519 return POLLERR | POLLHUP; 520 521 poll_wait(file, &dev->read_wait, wait); 522 poll_wait(file, &dev->write_wait, wait); 523 524 tower_check_for_read_packet(dev); 525 if (dev->read_packet_length > 0) { 526 mask |= POLLIN | POLLRDNORM; 527 } 528 if (!dev->interrupt_out_busy) { 529 mask |= POLLOUT | POLLWRNORM; 530 } 531 532 return mask; 533 } 534 535 536 /** 537 * tower_llseek 538 */ 539 static loff_t tower_llseek (struct file *file, loff_t off, int whence) 540 { 541 return -ESPIPE; /* unseekable */ 542 } 543 544 545 /** 546 * tower_read 547 */ 548 static ssize_t tower_read (struct file *file, char __user *buffer, size_t count, loff_t *ppos) 549 { 550 struct lego_usb_tower *dev; 551 size_t bytes_to_read; 552 int i; 553 int retval = 0; 554 unsigned long timeout = 0; 555 556 dev = file->private_data; 557 558 /* lock this object */ 559 if (mutex_lock_interruptible(&dev->lock)) { 560 retval = -ERESTARTSYS; 561 goto exit; 562 } 563 564 /* verify that the device wasn't unplugged */ 565 if (dev->udev == NULL) { 566 retval = -ENODEV; 567 pr_err("No device or device unplugged %d\n", retval); 568 goto unlock_exit; 569 } 570 571 /* verify that we actually have some data to read */ 572 if (count == 0) { 573 dev_dbg(&dev->udev->dev, "read request of 0 bytes\n"); 574 goto unlock_exit; 575 } 576 577 if (read_timeout) { 578 timeout = jiffies + read_timeout * HZ / 1000; 579 } 580 581 /* wait for data */ 582 tower_check_for_read_packet (dev); 583 while (dev->read_packet_length == 0) { 584 if (file->f_flags & O_NONBLOCK) { 585 retval = -EAGAIN; 586 goto unlock_exit; 587 } 588 retval = wait_event_interruptible_timeout(dev->read_wait, dev->interrupt_in_done, dev->packet_timeout_jiffies); 589 if (retval < 0) { 590 goto unlock_exit; 591 } 592 593 /* reset read timeout during read or write activity */ 594 if (read_timeout 595 && (dev->read_buffer_length || dev->interrupt_out_busy)) { 596 timeout = jiffies + read_timeout * HZ / 1000; 597 } 598 /* check for read timeout */ 599 if (read_timeout && time_after (jiffies, timeout)) { 600 retval = -ETIMEDOUT; 601 goto unlock_exit; 602 } 603 tower_check_for_read_packet (dev); 604 } 605 606 /* copy the data from read_buffer into userspace */ 607 bytes_to_read = min(count, dev->read_packet_length); 608 609 if (copy_to_user (buffer, dev->read_buffer, bytes_to_read)) { 610 retval = -EFAULT; 611 goto unlock_exit; 612 } 613 614 spin_lock_irq (&dev->read_buffer_lock); 615 dev->read_buffer_length -= bytes_to_read; 616 dev->read_packet_length -= bytes_to_read; 617 for (i=0; i<dev->read_buffer_length; i++) { 618 dev->read_buffer[i] = dev->read_buffer[i+bytes_to_read]; 619 } 620 spin_unlock_irq (&dev->read_buffer_lock); 621 622 retval = bytes_to_read; 623 624 unlock_exit: 625 /* unlock the device */ 626 mutex_unlock(&dev->lock); 627 628 exit: 629 return retval; 630 } 631 632 633 /** 634 * tower_write 635 */ 636 static ssize_t tower_write (struct file *file, const char __user *buffer, size_t count, loff_t *ppos) 637 { 638 struct lego_usb_tower *dev; 639 size_t bytes_to_write; 640 int retval = 0; 641 642 dev = file->private_data; 643 644 /* lock this object */ 645 if (mutex_lock_interruptible(&dev->lock)) { 646 retval = -ERESTARTSYS; 647 goto exit; 648 } 649 650 /* verify that the device wasn't unplugged */ 651 if (dev->udev == NULL) { 652 retval = -ENODEV; 653 pr_err("No device or device unplugged %d\n", retval); 654 goto unlock_exit; 655 } 656 657 /* verify that we actually have some data to write */ 658 if (count == 0) { 659 dev_dbg(&dev->udev->dev, "write request of 0 bytes\n"); 660 goto unlock_exit; 661 } 662 663 /* wait until previous transfer is finished */ 664 while (dev->interrupt_out_busy) { 665 if (file->f_flags & O_NONBLOCK) { 666 retval = -EAGAIN; 667 goto unlock_exit; 668 } 669 retval = wait_event_interruptible (dev->write_wait, !dev->interrupt_out_busy); 670 if (retval) { 671 goto unlock_exit; 672 } 673 } 674 675 /* write the data into interrupt_out_buffer from userspace */ 676 bytes_to_write = min_t(int, count, write_buffer_size); 677 dev_dbg(&dev->udev->dev, "%s: count = %Zd, bytes_to_write = %Zd\n", 678 __func__, count, bytes_to_write); 679 680 if (copy_from_user (dev->interrupt_out_buffer, buffer, bytes_to_write)) { 681 retval = -EFAULT; 682 goto unlock_exit; 683 } 684 685 /* send off the urb */ 686 usb_fill_int_urb(dev->interrupt_out_urb, 687 dev->udev, 688 usb_sndintpipe(dev->udev, dev->interrupt_out_endpoint->bEndpointAddress), 689 dev->interrupt_out_buffer, 690 bytes_to_write, 691 tower_interrupt_out_callback, 692 dev, 693 dev->interrupt_out_interval); 694 695 dev->interrupt_out_busy = 1; 696 wmb(); 697 698 retval = usb_submit_urb (dev->interrupt_out_urb, GFP_KERNEL); 699 if (retval) { 700 dev->interrupt_out_busy = 0; 701 dev_err(&dev->udev->dev, 702 "Couldn't submit interrupt_out_urb %d\n", retval); 703 goto unlock_exit; 704 } 705 retval = bytes_to_write; 706 707 unlock_exit: 708 /* unlock the device */ 709 mutex_unlock(&dev->lock); 710 711 exit: 712 return retval; 713 } 714 715 716 /** 717 * tower_interrupt_in_callback 718 */ 719 static void tower_interrupt_in_callback (struct urb *urb) 720 { 721 struct lego_usb_tower *dev = urb->context; 722 int status = urb->status; 723 int retval; 724 725 lego_usb_tower_debug_data(&dev->udev->dev, __func__, 726 urb->actual_length, urb->transfer_buffer); 727 728 if (status) { 729 if (status == -ENOENT || 730 status == -ECONNRESET || 731 status == -ESHUTDOWN) { 732 goto exit; 733 } else { 734 dev_dbg(&dev->udev->dev, 735 "%s: nonzero status received: %d\n", __func__, 736 status); 737 goto resubmit; /* maybe we can recover */ 738 } 739 } 740 741 if (urb->actual_length > 0) { 742 spin_lock (&dev->read_buffer_lock); 743 if (dev->read_buffer_length + urb->actual_length < read_buffer_size) { 744 memcpy (dev->read_buffer + dev->read_buffer_length, 745 dev->interrupt_in_buffer, 746 urb->actual_length); 747 dev->read_buffer_length += urb->actual_length; 748 dev->read_last_arrival = jiffies; 749 dev_dbg(&dev->udev->dev, "%s: received %d bytes\n", 750 __func__, urb->actual_length); 751 } else { 752 pr_warn("read_buffer overflow, %d bytes dropped\n", 753 urb->actual_length); 754 } 755 spin_unlock (&dev->read_buffer_lock); 756 } 757 758 resubmit: 759 /* resubmit if we're still running */ 760 if (dev->interrupt_in_running && dev->udev) { 761 retval = usb_submit_urb (dev->interrupt_in_urb, GFP_ATOMIC); 762 if (retval) 763 dev_err(&dev->udev->dev, 764 "%s: usb_submit_urb failed (%d)\n", 765 __func__, retval); 766 } 767 768 exit: 769 dev->interrupt_in_done = 1; 770 wake_up_interruptible (&dev->read_wait); 771 } 772 773 774 /** 775 * tower_interrupt_out_callback 776 */ 777 static void tower_interrupt_out_callback (struct urb *urb) 778 { 779 struct lego_usb_tower *dev = urb->context; 780 int status = urb->status; 781 782 lego_usb_tower_debug_data(&dev->udev->dev, __func__, 783 urb->actual_length, urb->transfer_buffer); 784 785 /* sync/async unlink faults aren't errors */ 786 if (status && !(status == -ENOENT || 787 status == -ECONNRESET || 788 status == -ESHUTDOWN)) { 789 dev_dbg(&dev->udev->dev, 790 "%s: nonzero write bulk status received: %d\n", __func__, 791 status); 792 } 793 794 dev->interrupt_out_busy = 0; 795 wake_up_interruptible(&dev->write_wait); 796 } 797 798 799 /** 800 * tower_probe 801 * 802 * Called by the usb core when a new device is connected that it thinks 803 * this driver might be interested in. 804 */ 805 static int tower_probe (struct usb_interface *interface, const struct usb_device_id *id) 806 { 807 struct device *idev = &interface->dev; 808 struct usb_device *udev = interface_to_usbdev(interface); 809 struct lego_usb_tower *dev = NULL; 810 struct usb_host_interface *iface_desc; 811 struct usb_endpoint_descriptor* endpoint; 812 struct tower_get_version_reply get_version_reply; 813 int i; 814 int retval = -ENOMEM; 815 int result; 816 817 /* allocate memory for our device state and initialize it */ 818 819 dev = kmalloc (sizeof(struct lego_usb_tower), GFP_KERNEL); 820 821 if (dev == NULL) { 822 dev_err(idev, "Out of memory\n"); 823 goto exit; 824 } 825 826 mutex_init(&dev->lock); 827 828 dev->udev = udev; 829 dev->open_count = 0; 830 831 dev->read_buffer = NULL; 832 dev->read_buffer_length = 0; 833 dev->read_packet_length = 0; 834 spin_lock_init (&dev->read_buffer_lock); 835 dev->packet_timeout_jiffies = packet_timeout * HZ / 1000; 836 dev->read_last_arrival = jiffies; 837 838 init_waitqueue_head (&dev->read_wait); 839 init_waitqueue_head (&dev->write_wait); 840 841 dev->interrupt_in_buffer = NULL; 842 dev->interrupt_in_endpoint = NULL; 843 dev->interrupt_in_urb = NULL; 844 dev->interrupt_in_running = 0; 845 dev->interrupt_in_done = 0; 846 847 dev->interrupt_out_buffer = NULL; 848 dev->interrupt_out_endpoint = NULL; 849 dev->interrupt_out_urb = NULL; 850 dev->interrupt_out_busy = 0; 851 852 iface_desc = interface->cur_altsetting; 853 854 /* set up the endpoint information */ 855 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) { 856 endpoint = &iface_desc->endpoint[i].desc; 857 858 if (usb_endpoint_xfer_int(endpoint)) { 859 if (usb_endpoint_dir_in(endpoint)) 860 dev->interrupt_in_endpoint = endpoint; 861 else 862 dev->interrupt_out_endpoint = endpoint; 863 } 864 } 865 if(dev->interrupt_in_endpoint == NULL) { 866 dev_err(idev, "interrupt in endpoint not found\n"); 867 goto error; 868 } 869 if (dev->interrupt_out_endpoint == NULL) { 870 dev_err(idev, "interrupt out endpoint not found\n"); 871 goto error; 872 } 873 874 dev->read_buffer = kmalloc (read_buffer_size, GFP_KERNEL); 875 if (!dev->read_buffer) { 876 dev_err(idev, "Couldn't allocate read_buffer\n"); 877 goto error; 878 } 879 dev->interrupt_in_buffer = kmalloc (usb_endpoint_maxp(dev->interrupt_in_endpoint), GFP_KERNEL); 880 if (!dev->interrupt_in_buffer) { 881 dev_err(idev, "Couldn't allocate interrupt_in_buffer\n"); 882 goto error; 883 } 884 dev->interrupt_in_urb = usb_alloc_urb(0, GFP_KERNEL); 885 if (!dev->interrupt_in_urb) { 886 dev_err(idev, "Couldn't allocate interrupt_in_urb\n"); 887 goto error; 888 } 889 dev->interrupt_out_buffer = kmalloc (write_buffer_size, GFP_KERNEL); 890 if (!dev->interrupt_out_buffer) { 891 dev_err(idev, "Couldn't allocate interrupt_out_buffer\n"); 892 goto error; 893 } 894 dev->interrupt_out_urb = usb_alloc_urb(0, GFP_KERNEL); 895 if (!dev->interrupt_out_urb) { 896 dev_err(idev, "Couldn't allocate interrupt_out_urb\n"); 897 goto error; 898 } 899 dev->interrupt_in_interval = interrupt_in_interval ? interrupt_in_interval : dev->interrupt_in_endpoint->bInterval; 900 dev->interrupt_out_interval = interrupt_out_interval ? interrupt_out_interval : dev->interrupt_out_endpoint->bInterval; 901 902 /* we can register the device now, as it is ready */ 903 usb_set_intfdata (interface, dev); 904 905 retval = usb_register_dev (interface, &tower_class); 906 907 if (retval) { 908 /* something prevented us from registering this driver */ 909 dev_err(idev, "Not able to get a minor for this device.\n"); 910 usb_set_intfdata (interface, NULL); 911 goto error; 912 } 913 dev->minor = interface->minor; 914 915 /* let the user know what node this device is now attached to */ 916 dev_info(&interface->dev, "LEGO USB Tower #%d now attached to major " 917 "%d minor %d\n", (dev->minor - LEGO_USB_TOWER_MINOR_BASE), 918 USB_MAJOR, dev->minor); 919 920 /* get the firmware version and log it */ 921 result = usb_control_msg (udev, 922 usb_rcvctrlpipe(udev, 0), 923 LEGO_USB_TOWER_REQUEST_GET_VERSION, 924 USB_TYPE_VENDOR | USB_DIR_IN | USB_RECIP_DEVICE, 925 0, 926 0, 927 &get_version_reply, 928 sizeof(get_version_reply), 929 1000); 930 if (result < 0) { 931 dev_err(idev, "LEGO USB Tower get version control request failed\n"); 932 retval = result; 933 goto error; 934 } 935 dev_info(&interface->dev, "LEGO USB Tower firmware version is %d.%d " 936 "build %d\n", get_version_reply.major, 937 get_version_reply.minor, 938 le16_to_cpu(get_version_reply.build_no)); 939 940 941 exit: 942 return retval; 943 944 error: 945 tower_delete(dev); 946 return retval; 947 } 948 949 950 /** 951 * tower_disconnect 952 * 953 * Called by the usb core when the device is removed from the system. 954 */ 955 static void tower_disconnect (struct usb_interface *interface) 956 { 957 struct lego_usb_tower *dev; 958 int minor; 959 960 dev = usb_get_intfdata (interface); 961 mutex_lock(&open_disc_mutex); 962 usb_set_intfdata (interface, NULL); 963 964 minor = dev->minor; 965 966 /* give back our minor */ 967 usb_deregister_dev (interface, &tower_class); 968 969 mutex_lock(&dev->lock); 970 mutex_unlock(&open_disc_mutex); 971 972 /* if the device is not opened, then we clean up right now */ 973 if (!dev->open_count) { 974 mutex_unlock(&dev->lock); 975 tower_delete (dev); 976 } else { 977 dev->udev = NULL; 978 /* wake up pollers */ 979 wake_up_interruptible_all(&dev->read_wait); 980 wake_up_interruptible_all(&dev->write_wait); 981 mutex_unlock(&dev->lock); 982 } 983 984 dev_info(&interface->dev, "LEGO USB Tower #%d now disconnected\n", 985 (minor - LEGO_USB_TOWER_MINOR_BASE)); 986 } 987 988 module_usb_driver(tower_driver); 989 990 MODULE_AUTHOR(DRIVER_AUTHOR); 991 MODULE_DESCRIPTION(DRIVER_DESC); 992 #ifdef MODULE_LICENSE 993 MODULE_LICENSE("GPL"); 994 #endif 995