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