1/* 2 * Frontier Designs Alphatrack driver 3 * 4 * Copyright (C) 2007 Michael Taht (m@taht.net) 5 * 6 * Based on the usbled driver and ldusb drivers by 7 * 8 * Copyright (C) 2004 Greg Kroah-Hartman (greg@kroah.com) 9 * Copyright (C) 2005 Michael Hund <mhund@ld-didactic.de> 10 * 11 * The ldusb driver was, in turn, derived from Lego USB Tower driver 12 * Copyright (C) 2003 David Glance <advidgsf@sourceforge.net> 13 * 2001-2004 Juergen Stuber <starblue@users.sourceforge.net> 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, version 2. 18 * 19 */ 20 21/** 22 * This driver uses a ring buffer for time critical reading of 23 * interrupt in reports and provides read and write methods for 24 * raw interrupt reports. 25 */ 26 27/* Note: this currently uses a dumb ringbuffer for reads and writes. 28 * A more optimal driver would cache and kill off outstanding urbs that are 29 * now invalid, and ignore ones that already were in the queue but valid 30 * as we only have 30 commands for the alphatrack. In particular this is 31 * key for getting lights to flash in time as otherwise many commands 32 * can be buffered up before the light change makes it to the interface. 33*/ 34 35#include <linux/kernel.h> 36#include <linux/errno.h> 37#include <linux/init.h> 38#include <linux/slab.h> 39#include <linux/module.h> 40#include <linux/kobject.h> 41#include <linux/mutex.h> 42 43#include <linux/uaccess.h> 44#include <linux/input.h> 45#include <linux/usb.h> 46#include <linux/poll.h> 47 48#include "alphatrack.h" 49 50#define VENDOR_ID 0x165b 51#define PRODUCT_ID 0xfad1 52 53#ifdef CONFIG_USB_DYNAMIC_MINORS 54#define USB_ALPHATRACK_MINOR_BASE 0 55#else 56#define USB_ALPHATRACK_MINOR_BASE 176 57#endif 58 59/* table of devices that work with this driver */ 60static const struct usb_device_id usb_alphatrack_table[] = { 61 {USB_DEVICE(VENDOR_ID, PRODUCT_ID)}, 62 {} /* Terminating entry */ 63}; 64 65MODULE_DEVICE_TABLE(usb, usb_alphatrack_table); 66MODULE_VERSION("0.41"); 67MODULE_AUTHOR("Mike Taht <m@taht.net>"); 68MODULE_DESCRIPTION("Alphatrack USB Driver"); 69MODULE_LICENSE("GPL"); 70MODULE_SUPPORTED_DEVICE("Frontier Designs Alphatrack Control Surface"); 71 72/* These aren't done yet */ 73 74#define SUPPRESS_EXTRA_ONLINE_EVENTS 0 75#define BUFFERED_WRITES 0 76#define SUPPRESS_EXTRA_OFFLINE_EVENTS 0 77#define COMPRESS_FADER_EVENTS 0 78 79#define BUFFERED_READS 1 80#define RING_BUFFER_SIZE 512 81#define WRITE_BUFFER_SIZE 34 82#define ALPHATRACK_USB_TIMEOUT 10 83#define OUTPUT_CMD_SIZE 8 84#define INPUT_CMD_SIZE 12 85#define ALPHATRACK_DEBUG 0 86 87static int debug = ALPHATRACK_DEBUG; 88 89/* Use our own dbg macro */ 90#define dbg_info(dev, format, arg...) do \ 91 { if (debug) dev_info(dev , format , ## arg); } while (0) 92 93#define alphatrack_ocmd_info(dev, cmd, format, arg...) 94 95#define alphatrack_icmd_info(dev, cmd, format, arg...) 96 97/* Module parameters */ 98 99module_param(debug, int, S_IRUGO | S_IWUSR); 100MODULE_PARM_DESC(debug, "Debug enabled or not"); 101 102/* All interrupt in transfers are collected in a ring buffer to 103 * avoid racing conditions and get better performance of the driver. 104 */ 105 106static int ring_buffer_size = RING_BUFFER_SIZE; 107 108module_param(ring_buffer_size, int, S_IRUGO); 109MODULE_PARM_DESC(ring_buffer_size, "Read ring buffer size"); 110 111/* The write_buffer can one day contain more than one interrupt out transfer. 112 */ 113 114static int write_buffer_size = WRITE_BUFFER_SIZE; 115module_param(write_buffer_size, int, S_IRUGO); 116MODULE_PARM_DESC(write_buffer_size, "Write buffer size"); 117 118/* 119 * Increase the interval for debugging purposes. 120 * or set to 1 to use the standard interval from the endpoint descriptors. 121 */ 122 123static int min_interrupt_in_interval = ALPHATRACK_USB_TIMEOUT; 124module_param(min_interrupt_in_interval, int, 0); 125MODULE_PARM_DESC(min_interrupt_in_interval, 126 "Minimum interrupt in interval in ms"); 127 128static int min_interrupt_out_interval = ALPHATRACK_USB_TIMEOUT; 129module_param(min_interrupt_out_interval, int, 0); 130MODULE_PARM_DESC(min_interrupt_out_interval, 131 "Minimum interrupt out interval in ms"); 132 133/* Structure to hold all of our device specific stuff */ 134 135struct usb_alphatrack { 136 struct mutex mtx; /* locks this structure */ 137 struct usb_interface *intf; /* save off the usb interface pointer */ 138 int open_count; /* number of times this port has been opened */ 139 140 /* make gcc happy */ 141 struct alphatrack_icmd (*ring_buffer)[RING_BUFFER_SIZE]; 142 struct alphatrack_ocmd (*write_buffer)[WRITE_BUFFER_SIZE]; 143 unsigned int ring_head; 144 unsigned int ring_tail; 145 146 wait_queue_head_t read_wait; 147 wait_queue_head_t write_wait; 148 149 unsigned char *interrupt_in_buffer; 150 unsigned char *oldi_buffer; 151 struct usb_endpoint_descriptor *interrupt_in_endpoint; 152 struct urb *interrupt_in_urb; 153 int interrupt_in_interval; 154 size_t interrupt_in_endpoint_size; 155 int interrupt_in_running; 156 int interrupt_in_done; 157 158 char *interrupt_out_buffer; 159 struct usb_endpoint_descriptor *interrupt_out_endpoint; 160 struct urb *interrupt_out_urb; 161 int interrupt_out_interval; 162 size_t interrupt_out_endpoint_size; 163 int interrupt_out_busy; 164 165 atomic_t writes_pending; 166 int event; /* alternate interface to events */ 167 int fader; /* 10 bits */ 168 int lights; /* 23 bits */ 169 unsigned char dump_state; /* 0 if disabled 1 if enabled */ 170 unsigned char enable; /* 0 if disabled 1 if enabled */ 171 unsigned char offline; /* if the device is out of range or asleep */ 172 unsigned char verbose; /* be verbose in error reporting */ 173 unsigned char last_cmd[OUTPUT_CMD_SIZE]; 174 unsigned char screen[32]; 175}; 176 177/* prevent races between open() and disconnect() */ 178static DEFINE_MUTEX(disconnect_mutex); 179 180/* forward declaration */ 181 182static struct usb_driver usb_alphatrack_driver; 183 184/** 185 * usb_alphatrack_abort_transfers 186 * aborts transfers and frees associated data structures 187 */ 188static void usb_alphatrack_abort_transfers(struct usb_alphatrack *dev) 189{ 190 /* shutdown transfer */ 191 if (dev->interrupt_in_running) { 192 dev->interrupt_in_running = 0; 193 if (dev->intf) 194 usb_kill_urb(dev->interrupt_in_urb); 195 } 196 if (dev->interrupt_out_busy) 197 if (dev->intf) 198 usb_kill_urb(dev->interrupt_out_urb); 199} 200 201/** 202 * usb_alphatrack_delete 203 */ 204static void usb_alphatrack_delete(struct usb_alphatrack *dev) 205{ 206 usb_alphatrack_abort_transfers(dev); 207 usb_free_urb(dev->interrupt_in_urb); 208 usb_free_urb(dev->interrupt_out_urb); 209 kfree(dev->ring_buffer); 210 kfree(dev->interrupt_in_buffer); 211 kfree(dev->interrupt_out_buffer); 212 kfree(dev); 213} 214 215/** 216 * usb_alphatrack_interrupt_in_callback 217 */ 218 219static void usb_alphatrack_interrupt_in_callback(struct urb *urb) 220{ 221 struct usb_alphatrack *dev = urb->context; 222 unsigned int next_ring_head; 223 int retval = -1; 224 225 if (urb->status) { 226 if (urb->status == -ENOENT || 227 urb->status == -ECONNRESET || urb->status == -ESHUTDOWN) { 228 goto exit; 229 } else { 230 dbg_info(&dev->intf->dev, 231 "%s: nonzero status received: %d\n", __func__, 232 urb->status); 233 goto resubmit; /* maybe we can recover */ 234 } 235 } 236 237 if (urb->actual_length != INPUT_CMD_SIZE) { 238 dev_warn(&dev->intf->dev, 239 "Urb length was %d bytes!!" 240 "Do something intelligent\n", urb->actual_length); 241 } else { 242 alphatrack_ocmd_info(&dev->intf->dev, 243 &(*dev->ring_buffer)[dev->ring_tail].cmd, 244 "%s", "bla"); 245 if (memcmp 246 (dev->interrupt_in_buffer, dev->oldi_buffer, 247 INPUT_CMD_SIZE) == 0) { 248 goto resubmit; 249 } 250 memcpy(dev->oldi_buffer, dev->interrupt_in_buffer, 251 INPUT_CMD_SIZE); 252 253#if SUPPRESS_EXTRA_OFFLINE_EVENTS 254 if (dev->offline == 2 && dev->interrupt_in_buffer[1] == 0xff) 255 goto resubmit; 256 if (dev->offline == 1 && dev->interrupt_in_buffer[1] == 0xff) { 257 dev->offline = 2; 258 goto resubmit; 259 } 260/* Always pass one offline event up the stack */ 261 if (dev->offline > 0 && dev->interrupt_in_buffer[1] != 0xff) 262 dev->offline = 0; 263 if (dev->offline == 0 && dev->interrupt_in_buffer[1] == 0xff) 264 dev->offline = 1; 265#endif 266 dbg_info(&dev->intf->dev, "%s: head, tail are %x, %x\n", 267 __func__, dev->ring_head, dev->ring_tail); 268 next_ring_head = (dev->ring_head + 1) % ring_buffer_size; 269 270 if (next_ring_head != dev->ring_tail) { 271 memcpy(&((*dev->ring_buffer)[dev->ring_head]), 272 dev->interrupt_in_buffer, urb->actual_length); 273 dev->ring_head = next_ring_head; 274 retval = 0; 275 memset(dev->interrupt_in_buffer, 0, urb->actual_length); 276 } else { 277 dev_warn(&dev->intf->dev, 278 "Ring buffer overflow, %d bytes dropped\n", 279 urb->actual_length); 280 memset(dev->interrupt_in_buffer, 0, urb->actual_length); 281 } 282 } 283 284resubmit: 285 /* resubmit if we're still running */ 286 if (dev->interrupt_in_running && dev->intf) { 287 retval = usb_submit_urb(dev->interrupt_in_urb, GFP_ATOMIC); 288 if (retval) 289 dev_err(&dev->intf->dev, 290 "usb_submit_urb failed (%d)\n", retval); 291 } 292 293exit: 294 dev->interrupt_in_done = 1; 295 wake_up_interruptible(&dev->read_wait); 296} 297 298/** 299 * usb_alphatrack_interrupt_out_callback 300 */ 301static void usb_alphatrack_interrupt_out_callback(struct urb *urb) 302{ 303 struct usb_alphatrack *dev = urb->context; 304 305 /* sync/async unlink faults aren't errors */ 306 if (urb->status && !(urb->status == -ENOENT || 307 urb->status == -ECONNRESET || 308 urb->status == -ESHUTDOWN)) 309 dbg_info(&dev->intf->dev, 310 "%s - nonzero write interrupt status received: %d\n", 311 __func__, urb->status); 312 atomic_dec(&dev->writes_pending); 313 dev->interrupt_out_busy = 0; 314 wake_up_interruptible(&dev->write_wait); 315} 316 317/** 318 * usb_alphatrack_open 319 */ 320static int usb_alphatrack_open(struct inode *inode, struct file *file) 321{ 322 struct usb_alphatrack *dev; 323 int subminor; 324 int retval = 0; 325 struct usb_interface *interface; 326 327 nonseekable_open(inode, file); 328 subminor = iminor(inode); 329 330 mutex_lock(&disconnect_mutex); 331 332 interface = usb_find_interface(&usb_alphatrack_driver, subminor); 333 334 if (!interface) { 335 err("%s - error, can't find device for minor %d\n", 336 __func__, subminor); 337 retval = -ENODEV; 338 goto unlock_disconnect_exit; 339 } 340 341 dev = usb_get_intfdata(interface); 342 343 if (!dev) { 344 retval = -ENODEV; 345 goto unlock_disconnect_exit; 346 } 347 348 /* lock this device */ 349 if (mutex_lock_interruptible(&dev->mtx)) { 350 retval = -ERESTARTSYS; 351 goto unlock_disconnect_exit; 352 } 353 354 /* allow opening only once */ 355 if (dev->open_count) { 356 retval = -EBUSY; 357 goto unlock_exit; 358 } 359 dev->open_count = 1; 360 361 /* initialize in direction */ 362 dev->ring_head = 0; 363 dev->ring_tail = 0; 364 usb_fill_int_urb(dev->interrupt_in_urb, 365 interface_to_usbdev(interface), 366 usb_rcvintpipe(interface_to_usbdev(interface), 367 dev->interrupt_in_endpoint-> 368 bEndpointAddress), 369 dev->interrupt_in_buffer, 370 dev->interrupt_in_endpoint_size, 371 usb_alphatrack_interrupt_in_callback, dev, 372 dev->interrupt_in_interval); 373 374 dev->interrupt_in_running = 1; 375 dev->interrupt_in_done = 0; 376 dev->enable = 1; 377 dev->offline = 0; 378 379 retval = usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL); 380 if (retval) { 381 dev_err(&interface->dev, 382 "Couldn't submit interrupt_in_urb %d\n", retval); 383 dev->interrupt_in_running = 0; 384 dev->open_count = 0; 385 goto unlock_exit; 386 } 387 388 /* save device in the file's private structure */ 389 file->private_data = dev; 390 391unlock_exit: 392 mutex_unlock(&dev->mtx); 393 394unlock_disconnect_exit: 395 mutex_unlock(&disconnect_mutex); 396 397 return retval; 398} 399 400/** 401 * usb_alphatrack_release 402 */ 403static int usb_alphatrack_release(struct inode *inode, struct file *file) 404{ 405 struct usb_alphatrack *dev; 406 int retval = 0; 407 408 dev = file->private_data; 409 410 if (dev == NULL) { 411 retval = -ENODEV; 412 goto exit; 413 } 414 415 if (mutex_lock_interruptible(&dev->mtx)) { 416 retval = -ERESTARTSYS; 417 goto exit; 418 } 419 420 if (dev->open_count != 1) { 421 retval = -ENODEV; 422 goto unlock_exit; 423 } 424 425 if (dev->intf == NULL) { 426 /* the device was unplugged before the file was released */ 427 mutex_unlock(&dev->mtx); 428 /* unlock here as usb_alphatrack_delete frees dev */ 429 usb_alphatrack_delete(dev); 430 retval = -ENODEV; 431 goto exit; 432 } 433 434 /* wait until write transfer is finished */ 435 if (dev->interrupt_out_busy) 436 wait_event_interruptible_timeout(dev->write_wait, 437 !dev->interrupt_out_busy, 438 2 * HZ); 439 usb_alphatrack_abort_transfers(dev); 440 dev->open_count = 0; 441 442unlock_exit: 443 mutex_unlock(&dev->mtx); 444 445exit: 446 return retval; 447} 448 449/** 450 * usb_alphatrack_poll 451 */ 452static unsigned int usb_alphatrack_poll(struct file *file, poll_table * wait) 453{ 454 struct usb_alphatrack *dev; 455 unsigned int mask = 0; 456 457 dev = file->private_data; 458 459 poll_wait(file, &dev->read_wait, wait); 460 poll_wait(file, &dev->write_wait, wait); 461 462 if (dev->ring_head != dev->ring_tail) 463 mask |= POLLIN | POLLRDNORM; 464 if (!dev->interrupt_out_busy) 465 mask |= POLLOUT | POLLWRNORM; 466 467 return mask; 468} 469 470/** 471 * usb_alphatrack_read 472 */ 473static ssize_t usb_alphatrack_read(struct file *file, char __user *buffer, 474 size_t count, loff_t *ppos) 475{ 476 struct usb_alphatrack *dev; 477 int retval = 0; 478 479 int c = 0; 480 481 dev = file->private_data; 482 483 /* verify that we actually have some data to read */ 484 if (count == 0) 485 goto exit; 486 487 /* lock this object */ 488 if (mutex_lock_interruptible(&dev->mtx)) { 489 retval = -ERESTARTSYS; 490 goto exit; 491 } 492 493 /* verify that the device wasn't unplugged */ 494 if (dev->intf == NULL) { 495 retval = -ENODEV; 496 err("No device or device unplugged %d\n", retval); 497 goto unlock_exit; 498 } 499 500 while (dev->ring_head == dev->ring_tail) { 501 if (file->f_flags & O_NONBLOCK) { 502 retval = -EAGAIN; 503 goto unlock_exit; 504 } 505 dev->interrupt_in_done = 0; 506 retval = 507 wait_event_interruptible(dev->read_wait, 508 dev->interrupt_in_done); 509 if (retval < 0) 510 goto unlock_exit; 511 } 512 513 alphatrack_ocmd_info(&dev->intf->dev, 514 &(*dev->ring_buffer)[dev->ring_tail].cmd, "%s", 515 ": copying to userspace"); 516 517 c = 0; 518 while ((c < count) && (dev->ring_tail != dev->ring_head)) { 519 if (copy_to_user 520 (&buffer[c], &(*dev->ring_buffer)[dev->ring_tail], 521 INPUT_CMD_SIZE)) { 522 retval = -EFAULT; 523 goto unlock_exit; 524 } 525 dev->ring_tail = (dev->ring_tail + 1) % ring_buffer_size; 526 c += INPUT_CMD_SIZE; 527 dbg_info(&dev->intf->dev, "%s: head, tail are %x, %x\n", 528 __func__, dev->ring_head, dev->ring_tail); 529 } 530 retval = c; 531 532unlock_exit: 533 /* unlock the device */ 534 mutex_unlock(&dev->mtx); 535 536exit: 537 return retval; 538} 539 540/** 541 * usb_alphatrack_write 542 */ 543static ssize_t usb_alphatrack_write(struct file *file, 544 const char __user *buffer, size_t count, 545 loff_t *ppos) 546{ 547 struct usb_alphatrack *dev; 548 size_t bytes_to_write; 549 int retval = 0; 550 551 dev = file->private_data; 552 553 /* verify that we actually have some data to write */ 554 if (count == 0) 555 goto exit; 556 557 /* lock this object */ 558 if (mutex_lock_interruptible(&dev->mtx)) { 559 retval = -ERESTARTSYS; 560 goto exit; 561 } 562 563 /* verify that the device wasn't unplugged */ 564 if (dev->intf == NULL) { 565 retval = -ENODEV; 566 err("No device or device unplugged %d\n", retval); 567 goto unlock_exit; 568 } 569 570 /* wait until previous transfer is finished */ 571 if (dev->interrupt_out_busy) { 572 if (file->f_flags & O_NONBLOCK) { 573 retval = -EAGAIN; 574 goto unlock_exit; 575 } 576 retval = 577 wait_event_interruptible(dev->write_wait, 578 !dev->interrupt_out_busy); 579 if (retval < 0) 580 goto unlock_exit; 581 } 582 583 /* write the data into interrupt_out_buffer from userspace */ 584 bytes_to_write = 585 min(count, write_buffer_size * dev->interrupt_out_endpoint_size); 586 if (bytes_to_write < count) 587 dev_warn(&dev->intf->dev, 588 "Write buffer overflow, %zd bytes dropped\n", 589 count - bytes_to_write); 590 591 dbg_info(&dev->intf->dev, "%s: count = %zd, bytes_to_write = %zd\n", 592 __func__, count, bytes_to_write); 593 594 if (copy_from_user(dev->interrupt_out_buffer, buffer, bytes_to_write)) { 595 retval = -EFAULT; 596 goto unlock_exit; 597 } 598 599 if (dev->interrupt_out_endpoint == NULL) { 600 err("Endpoint should not be be null!\n"); 601 goto unlock_exit; 602 } 603 604 /* send off the urb */ 605 usb_fill_int_urb(dev->interrupt_out_urb, 606 interface_to_usbdev(dev->intf), 607 usb_sndintpipe(interface_to_usbdev(dev->intf), 608 dev->interrupt_out_endpoint-> 609 bEndpointAddress), 610 dev->interrupt_out_buffer, bytes_to_write, 611 usb_alphatrack_interrupt_out_callback, dev, 612 dev->interrupt_out_interval); 613 dev->interrupt_out_busy = 1; 614 atomic_inc(&dev->writes_pending); 615 wmb(); 616 617 retval = usb_submit_urb(dev->interrupt_out_urb, GFP_KERNEL); 618 if (retval) { 619 dev->interrupt_out_busy = 0; 620 err("Couldn't submit interrupt_out_urb %d\n", retval); 621 atomic_dec(&dev->writes_pending); 622 goto unlock_exit; 623 } 624 retval = bytes_to_write; 625 626unlock_exit: 627 /* unlock the device */ 628 mutex_unlock(&dev->mtx); 629 630exit: 631 return retval; 632} 633 634/* file operations needed when we register this driver */ 635static const struct file_operations usb_alphatrack_fops = { 636 .owner = THIS_MODULE, 637 .read = usb_alphatrack_read, 638 .write = usb_alphatrack_write, 639 .open = usb_alphatrack_open, 640 .release = usb_alphatrack_release, 641 .poll = usb_alphatrack_poll, 642}; 643 644/* 645 * usb class driver info in order to get a minor number from the usb core, 646 * and to have the device registered with the driver core 647 */ 648 649static struct usb_class_driver usb_alphatrack_class = { 650 .name = "alphatrack%d", 651 .fops = &usb_alphatrack_fops, 652 .minor_base = USB_ALPHATRACK_MINOR_BASE, 653}; 654 655/** 656 * usb_alphatrack_probe 657 * 658 * Called by the usb core when a new device is connected that it thinks 659 * this driver might be interested in. 660 */ 661static int usb_alphatrack_probe(struct usb_interface *intf, 662 const struct usb_device_id *id) 663{ 664 struct usb_device *udev = interface_to_usbdev(intf); 665 struct usb_alphatrack *dev = NULL; 666 struct usb_host_interface *iface_desc; 667 struct usb_endpoint_descriptor *endpoint; 668 int i; 669 int true_size; 670 int retval = -ENOMEM; 671 672 /* allocate memory for our device state and intialize it */ 673 674 dev = kzalloc(sizeof(*dev), GFP_KERNEL); 675 if (dev == NULL) { 676 dev_err(&intf->dev, "Out of memory\n"); 677 goto exit; 678 } 679 mutex_init(&dev->mtx); 680 dev->intf = intf; 681 init_waitqueue_head(&dev->read_wait); 682 init_waitqueue_head(&dev->write_wait); 683 684 iface_desc = intf->cur_altsetting; 685 686 /* set up the endpoint information */ 687 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) { 688 endpoint = &iface_desc->endpoint[i].desc; 689 690 if (usb_endpoint_is_int_in(endpoint)) 691 dev->interrupt_in_endpoint = endpoint; 692 693 if (usb_endpoint_is_int_out(endpoint)) 694 dev->interrupt_out_endpoint = endpoint; 695 } 696 if (dev->interrupt_in_endpoint == NULL) { 697 dev_err(&intf->dev, "Interrupt in endpoint not found\n"); 698 goto error; 699 } 700 if (dev->interrupt_out_endpoint == NULL) 701 dev_warn(&intf->dev, 702 "Interrupt out endpoint not found" 703 "(using control endpoint instead)\n"); 704 705 dev->interrupt_in_endpoint_size = 706 le16_to_cpu(dev->interrupt_in_endpoint->wMaxPacketSize); 707 708 if (dev->interrupt_in_endpoint_size != 64) 709 dev_warn(&intf->dev, "Interrupt in endpoint size is not 64!\n"); 710 711 if (ring_buffer_size == 0) 712 ring_buffer_size = RING_BUFFER_SIZE; 713 714 true_size = min(ring_buffer_size, RING_BUFFER_SIZE); 715 716 dev->ring_buffer = 717 kmalloc((true_size * sizeof(struct alphatrack_icmd)), GFP_KERNEL); 718 719 if (!dev->ring_buffer) { 720 dev_err(&intf->dev, 721 "Couldn't allocate input ring_buffer of size %d\n", 722 true_size); 723 goto error; 724 } 725 726 dev->interrupt_in_buffer = 727 kmalloc(dev->interrupt_in_endpoint_size, GFP_KERNEL); 728 729 if (!dev->interrupt_in_buffer) { 730 dev_err(&intf->dev, "Couldn't allocate interrupt_in_buffer\n"); 731 goto error; 732 } 733 dev->oldi_buffer = kmalloc(dev->interrupt_in_endpoint_size, GFP_KERNEL); 734 if (!dev->oldi_buffer) { 735 dev_err(&intf->dev, "Couldn't allocate old buffer\n"); 736 goto error; 737 } 738 dev->interrupt_in_urb = usb_alloc_urb(0, GFP_KERNEL); 739 if (!dev->interrupt_in_urb) { 740 dev_err(&intf->dev, "Couldn't allocate interrupt_in_urb\n"); 741 goto error; 742 } 743 744 dev->interrupt_out_endpoint_size = 745 dev->interrupt_out_endpoint ? le16_to_cpu(dev-> 746 interrupt_out_endpoint-> 747 wMaxPacketSize) : udev-> 748 descriptor.bMaxPacketSize0; 749 750 if (dev->interrupt_out_endpoint_size != 64) 751 dev_warn(&intf->dev, 752 "Interrupt out endpoint size is not 64!)\n"); 753 754 if (write_buffer_size == 0) 755 write_buffer_size = WRITE_BUFFER_SIZE; 756 true_size = min(write_buffer_size, WRITE_BUFFER_SIZE); 757 758 dev->interrupt_out_buffer = 759 kmalloc(true_size * dev->interrupt_out_endpoint_size, GFP_KERNEL); 760 761 if (!dev->interrupt_out_buffer) { 762 dev_err(&intf->dev, "Couldn't allocate interrupt_out_buffer\n"); 763 goto error; 764 } 765 766 dev->write_buffer = 767 kmalloc(sizeof(struct alphatrack_ocmd) * true_size, GFP_KERNEL); 768 769 if (!dev->write_buffer) { 770 dev_err(&intf->dev, "Couldn't allocate write_buffer\n"); 771 goto error; 772 } 773 774 dev->interrupt_out_urb = usb_alloc_urb(0, GFP_KERNEL); 775 if (!dev->interrupt_out_urb) { 776 dev_err(&intf->dev, "Couldn't allocate interrupt_out_urb\n"); 777 goto error; 778 } 779 dev->interrupt_in_interval = 780 min_interrupt_in_interval > 781 dev->interrupt_in_endpoint-> 782 bInterval ? min_interrupt_in_interval : dev->interrupt_in_endpoint-> 783 bInterval; 784 if (dev->interrupt_out_endpoint) 785 dev->interrupt_out_interval = 786 min_interrupt_out_interval > 787 dev->interrupt_out_endpoint-> 788 bInterval ? min_interrupt_out_interval : dev-> 789 interrupt_out_endpoint->bInterval; 790 791 /* we can register the device now, as it is ready */ 792 usb_set_intfdata(intf, dev); 793 794 atomic_set(&dev->writes_pending, 0); 795 retval = usb_register_dev(intf, &usb_alphatrack_class); 796 if (retval) { 797 /* something prevented us from registering this driver */ 798 dev_err(&intf->dev, 799 "Not able to get a minor for this device.\n"); 800 usb_set_intfdata(intf, NULL); 801 goto error; 802 } 803 804 /* let the user know what node this device is now attached to */ 805 dev_info(&intf->dev, 806 "Alphatrack Device #%d now attached to major %d minor %d\n", 807 (intf->minor - USB_ALPHATRACK_MINOR_BASE), USB_MAJOR, 808 intf->minor); 809 810exit: 811 return retval; 812 813error: 814 usb_alphatrack_delete(dev); 815 816 return retval; 817} 818 819/** 820 * usb_alphatrack_disconnect 821 * 822 * Called by the usb core when the device is removed from the system. 823 */ 824static void usb_alphatrack_disconnect(struct usb_interface *intf) 825{ 826 struct usb_alphatrack *dev; 827 int minor; 828 829 mutex_lock(&disconnect_mutex); 830 831 dev = usb_get_intfdata(intf); 832 usb_set_intfdata(intf, NULL); 833 834 mutex_lock(&dev->mtx); 835 836 minor = intf->minor; 837 838 /* give back our minor */ 839 usb_deregister_dev(intf, &usb_alphatrack_class); 840 841 /* if the device is not opened, then we clean up right now */ 842 if (!dev->open_count) { 843 mutex_unlock(&dev->mtx); 844 usb_alphatrack_delete(dev); 845 } else { 846 dev->intf = NULL; 847 mutex_unlock(&dev->mtx); 848 } 849 850 atomic_set(&dev->writes_pending, 0); 851 mutex_unlock(&disconnect_mutex); 852 853 dev_info(&intf->dev, "Alphatrack Surface #%d now disconnected\n", 854 (minor - USB_ALPHATRACK_MINOR_BASE)); 855} 856 857/* usb specific object needed to register this driver with the usb subsystem */ 858static struct usb_driver usb_alphatrack_driver = { 859 .name = "alphatrack", 860 .probe = usb_alphatrack_probe, 861 .disconnect = usb_alphatrack_disconnect, 862 .id_table = usb_alphatrack_table, 863}; 864 865/** 866 * usb_alphatrack_init 867 */ 868static int __init usb_alphatrack_init(void) 869{ 870 int retval; 871 872 /* register this driver with the USB subsystem */ 873 retval = usb_register(&usb_alphatrack_driver); 874 if (retval) 875 err("usb_register failed for the " __FILE__ 876 " driver. Error number %d\n", retval); 877 878 return retval; 879} 880 881/** 882 * usb_alphatrack_exit 883 */ 884static void __exit usb_alphatrack_exit(void) 885{ 886 /* deregister this driver with the USB subsystem */ 887 usb_deregister(&usb_alphatrack_driver); 888} 889 890module_init(usb_alphatrack_init); 891module_exit(usb_alphatrack_exit); 892