Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
tranzport.c
Go to the documentation of this file.
1 /*
2  * Frontier Designs Tranzport driver
3  *
4  * Copyright (C) 2007 Michael Taht ([email protected])
5  *
6  * Based on the usbled driver and ldusb drivers by
7  *
8  * Copyright (C) 2004 Greg Kroah-Hartman ([email protected])
9  * Copyright (C) 2005 Michael Hund <[email protected]>
10  *
11  * The ldusb driver was, in turn, derived from Lego USB Tower driver
12  * Copyright (C) 2003 David Glance <[email protected]>
13  * 2001-2004 Juergen Stuber <[email protected]>
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 17 commands for the tranzport. 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/mutex.h>
41 
42 #include <linux/uaccess.h>
43 #include <linux/input.h>
44 #include <linux/usb.h>
45 #include <linux/poll.h>
46 
47 /* Define these values to match your devices */
48 #define VENDOR_ID 0x165b
49 #define PRODUCT_ID 0x8101
50 
51 #ifdef CONFIG_USB_DYNAMIC_MINORS
52 #define USB_TRANZPORT_MINOR_BASE 0
53 #else /* FIXME 177- is the another driver's minor - apply for a minor soon */
54 #define USB_TRANZPORT_MINOR_BASE 177
55 #endif
56 
57 /* table of devices that work with this driver */
58 static const struct usb_device_id usb_tranzport_table[] = {
59  {USB_DEVICE(VENDOR_ID, PRODUCT_ID)},
60  {} /* Terminating entry */
61 };
62 
63 MODULE_DEVICE_TABLE(usb, usb_tranzport_table);
64 MODULE_VERSION("0.35");
65 MODULE_AUTHOR("Mike Taht <[email protected]>");
66 MODULE_DESCRIPTION("Tranzport USB Driver");
67 MODULE_LICENSE("GPL");
68 MODULE_SUPPORTED_DEVICE("Frontier Designs Tranzport Control Surface");
69 
70 #define SUPPRESS_EXTRA_OFFLINE_EVENTS 1
71 #define COMPRESS_WHEEL_EVENTS 1
72 #define BUFFERED_READS 1
73 #define RING_BUFFER_SIZE 1000
74 #define WRITE_BUFFER_SIZE 34
75 #define TRANZPORT_USB_TIMEOUT 10
76 #define TRANZPORT_DEBUG 0
77 
78 static int debug = TRANZPORT_DEBUG;
79 
80 /* Use our own dbg macro */
81 #define dbg_info(dev, format, arg...) do \
82  { if (debug) dev_info(dev , format , ## arg); } while (0)
83 
84 /* Module parameters */
85 
87 MODULE_PARM_DESC(debug, "Debug enabled or not");
88 
89 /* All interrupt in transfers are collected in a ring buffer to
90  * avoid racing conditions and get better performance of the driver.
91  */
92 
93 static int ring_buffer_size = RING_BUFFER_SIZE;
94 
95 module_param(ring_buffer_size, int, S_IRUGO);
96 MODULE_PARM_DESC(ring_buffer_size, "Read ring buffer size in reports");
97 
98 /* The write_buffer can one day contain more than one interrupt out transfer.
99  */
100 static int write_buffer_size = WRITE_BUFFER_SIZE;
101 module_param(write_buffer_size, int, S_IRUGO);
102 MODULE_PARM_DESC(write_buffer_size, "Write buffer size");
103 
104 /*
105  * Increase the interval for debugging purposes.
106  * or set to 1 to use the standard interval from the endpoint descriptors.
107  */
108 
109 static int min_interrupt_in_interval = TRANZPORT_USB_TIMEOUT;
110 module_param(min_interrupt_in_interval, int, 0);
111 MODULE_PARM_DESC(min_interrupt_in_interval,
112  "Minimum interrupt in interval in ms");
113 
114 static int min_interrupt_out_interval = TRANZPORT_USB_TIMEOUT;
115 module_param(min_interrupt_out_interval, int, 0);
116 MODULE_PARM_DESC(min_interrupt_out_interval,
117  "Minimum interrupt out interval in ms");
118 
120  unsigned char cmd[8];
121 };
122 
123 /* Structure to hold all of our device specific stuff */
124 
126  struct mutex mtx; /* locks this structure */
127  struct usb_interface *intf; /* save off the usb interface pointer */
128  int open_count; /* number of times this port opened */
130  unsigned int ring_head;
131  unsigned int ring_tail;
134  unsigned char *interrupt_in_buffer;
147 
148  /* Sysfs support */
149 
150  unsigned char enable; /* 0 if disabled 1 if enabled */
151  unsigned char offline; /* if the device is out of range or asleep */
152  unsigned char compress_wheel; /* flag to compress wheel events */
153 };
154 
155 /* prevent races between open() and disconnect() */
156 static DEFINE_MUTEX(disconnect_mutex);
157 
158 static struct usb_driver usb_tranzport_driver;
159 
164 static void usb_tranzport_abort_transfers(struct usb_tranzport *dev)
165 {
166  /* shutdown transfer */
167  if (dev->interrupt_in_running) {
168  dev->interrupt_in_running = 0;
169  if (dev->intf)
171  }
172  if (dev->interrupt_out_busy)
173  if (dev->intf)
175 }
176 
177 #define show_int(value) \
178  static ssize_t show_##value(struct device *dev, \
179  struct device_attribute *attr, char *buf) \
180  { \
181  struct usb_interface *intf = to_usb_interface(dev); \
182  struct usb_tranzport *t = usb_get_intfdata(intf); \
183  return sprintf(buf, "%d\n", t->value); \
184  } \
185  static DEVICE_ATTR(value, S_IRUGO, show_##value, NULL);
186 
187 #define show_set_int(value) \
188  static ssize_t show_##value(struct device *dev, \
189  struct device_attribute *attr, char *buf) \
190  { \
191  struct usb_interface *intf = to_usb_interface(dev); \
192  struct usb_tranzport *t = usb_get_intfdata(intf); \
193  return sprintf(buf, "%d\n", t->value); \
194  } \
195  static ssize_t set_##value(struct device *dev, \
196  struct device_attribute *attr, \
197  const char *buf, size_t count) \
198  { \
199  struct usb_interface *intf = to_usb_interface(dev); \
200  struct usb_tranzport *t = usb_get_intfdata(intf); \
201  unsigned long temp; \
202  if (kstrtoul(buf, 10, &temp)) \
203  return -EINVAL; \
204  t->value = temp; \
205  return count; \
206  } \
207  static DEVICE_ATTR(value, S_IWUSR | S_IRUGO, show_##value, set_##value);
208 
211 show_set_int(compress_wheel);
212 
216 static void usb_tranzport_delete(struct usb_tranzport *dev)
217 {
218  usb_tranzport_abort_transfers(dev);
219  if (dev->intf != NULL) {
220  device_remove_file(&dev->intf->dev, &dev_attr_enable);
221  device_remove_file(&dev->intf->dev, &dev_attr_offline);
222  device_remove_file(&dev->intf->dev, &dev_attr_compress_wheel);
223  }
224 
225  /* free data structures */
228  kfree(dev->ring_buffer);
231  kfree(dev);
232 }
233 
238 static void usb_tranzport_interrupt_in_callback(struct urb *urb)
239 {
240  struct usb_tranzport *dev = urb->context;
241  unsigned int next_ring_head;
242  int retval = -1;
243 
244  if (urb->status) {
245  if (urb->status == -ENOENT ||
246  urb->status == -ECONNRESET ||
247  urb->status == -ESHUTDOWN) {
248  goto exit;
249  } else {
250  dbg_info(&dev->intf->dev,
251  "%s: nonzero status received: %d\n",
252  __func__, urb->status);
253  goto resubmit; /* maybe we can recover */
254  }
255  }
256 
257  if (urb->actual_length != 8) {
258  dev_warn(&dev->intf->dev,
259  "Urb length was %d bytes!!"
260  "Do something intelligent\n",
261  urb->actual_length);
262  } else {
263  dbg_info(&dev->intf->dev,
264  "%s: received: %02x%02x%02x%02x%02x%02x%02x%02x\n",
265  __func__, dev->interrupt_in_buffer[0],
266  dev->interrupt_in_buffer[1],
267  dev->interrupt_in_buffer[2],
268  dev->interrupt_in_buffer[3],
269  dev->interrupt_in_buffer[4],
270  dev->interrupt_in_buffer[5],
271  dev->interrupt_in_buffer[6],
272  dev->interrupt_in_buffer[7]);
273 #if SUPPRESS_EXTRA_OFFLINE_EVENTS
274  if (dev->offline == 2 && dev->interrupt_in_buffer[1] == 0xff)
275  goto resubmit;
276  if (dev->offline == 1 && dev->interrupt_in_buffer[1] == 0xff) {
277  dev->offline = 2;
278  goto resubmit;
279  }
280 
281  /* Always pass one offline event up the stack */
282  if (dev->offline > 0 && dev->interrupt_in_buffer[1] != 0xff)
283  dev->offline = 0;
284  if (dev->offline == 0 && dev->interrupt_in_buffer[1] == 0xff)
285  dev->offline = 1;
286 
287 #endif /* SUPPRESS_EXTRA_OFFLINE_EVENTS */
288  dbg_info(&dev->intf->dev, "%s: head, tail are %x, %x\n",
289  __func__, dev->ring_head, dev->ring_tail);
290 
291  next_ring_head = (dev->ring_head + 1) % ring_buffer_size;
292 
293  if (next_ring_head != dev->ring_tail) {
294  memcpy(&((*dev->ring_buffer)[dev->ring_head]),
295  dev->interrupt_in_buffer, urb->actual_length);
296  dev->ring_head = next_ring_head;
297  retval = 0;
298  memset(dev->interrupt_in_buffer, 0, urb->actual_length);
299  } else {
300  dev_warn(&dev->intf->dev,
301  "Ring buffer overflow, %d bytes dropped\n",
302  urb->actual_length);
303  memset(dev->interrupt_in_buffer, 0, urb->actual_length);
304  }
305  }
306 
307 resubmit:
308 /* resubmit if we're still running */
309  if (dev->interrupt_in_running && dev->intf) {
311  if (retval)
312  dev_err(&dev->intf->dev,
313  "usb_submit_urb failed (%d)\n", retval);
314  }
315 
316 exit:
317  dev->interrupt_in_done = 1;
319 }
320 
324 static void usb_tranzport_interrupt_out_callback(struct urb *urb)
325 {
326  struct usb_tranzport *dev = urb->context;
327  /* sync/async unlink faults aren't errors */
328  if (urb->status && !(urb->status == -ENOENT ||
329  urb->status == -ECONNRESET ||
330  urb->status == -ESHUTDOWN))
331  dbg_info(&dev->intf->dev,
332  "%s - nonzero write interrupt status received: %d\n",
333  __func__, urb->status);
334 
335  dev->interrupt_out_busy = 0;
337 }
341 static int usb_tranzport_open(struct inode *inode, struct file *file)
342 {
343  struct usb_tranzport *dev;
344  int subminor;
345  int retval = 0;
346  struct usb_interface *interface;
347 
348  nonseekable_open(inode, file);
349  subminor = iminor(inode);
350 
351  mutex_lock(&disconnect_mutex);
352 
353  interface = usb_find_interface(&usb_tranzport_driver, subminor);
354 
355  if (!interface) {
356  pr_err("%s - error, can't find device for minor %d\n",
357  __func__, subminor);
358  retval = -ENODEV;
359  goto unlock_disconnect_exit;
360  }
361 
362  dev = usb_get_intfdata(interface);
363 
364  if (!dev) {
365  retval = -ENODEV;
366  goto unlock_disconnect_exit;
367  }
368 
369  /* lock this device */
370  if (mutex_lock_interruptible(&dev->mtx)) {
371  retval = -ERESTARTSYS;
372  goto unlock_disconnect_exit;
373  }
374 
375  /* allow opening only once */
376  if (dev->open_count) {
377  retval = -EBUSY;
378  goto unlock_exit;
379  }
380  dev->open_count = 1;
381 
382  /* initialize in direction */
383  dev->ring_head = 0;
384  dev->ring_tail = 0;
385  usb_fill_int_urb(dev->interrupt_in_urb,
386  interface_to_usbdev(interface),
387  usb_rcvintpipe(interface_to_usbdev(interface),
388  dev->interrupt_in_endpoint->
390  dev->interrupt_in_buffer,
392  usb_tranzport_interrupt_in_callback, dev,
393  dev->interrupt_in_interval);
394 
395  dev->interrupt_in_running = 1;
396  dev->interrupt_in_done = 0;
397  dev->enable = 1;
398  dev->offline = 0;
399  dev->compress_wheel = 1;
400 
402  if (retval) {
403  dev_err(&interface->dev,
404  "Couldn't submit interrupt_in_urb %d\n", retval);
405  dev->interrupt_in_running = 0;
406  dev->open_count = 0;
407  goto unlock_exit;
408  }
409 
410  /* save device in the file's private structure */
411  file->private_data = dev;
412 
413 unlock_exit:
414  mutex_unlock(&dev->mtx);
415 
416 unlock_disconnect_exit:
417  mutex_unlock(&disconnect_mutex);
418 
419  return retval;
420 }
421 
425 static int usb_tranzport_release(struct inode *inode, struct file *file)
426 {
427  struct usb_tranzport *dev;
428  int retval = 0;
429 
430  dev = file->private_data;
431 
432  if (dev == NULL) {
433  retval = -ENODEV;
434  goto exit;
435  }
436 
437  if (mutex_lock_interruptible(&dev->mtx)) {
438  retval = -ERESTARTSYS;
439  goto exit;
440  }
441 
442  if (dev->open_count != 1) {
443  retval = -ENODEV;
444  goto unlock_exit;
445  }
446 
447  if (dev->intf == NULL) {
448  /* the device was unplugged before the file was released */
449  mutex_unlock(&dev->mtx);
450  /* unlock here as usb_tranzport_delete frees dev */
451  usb_tranzport_delete(dev);
452  retval = -ENODEV;
453  goto exit;
454  }
455 
456  /* wait until write transfer is finished */
457  if (dev->interrupt_out_busy)
459  !dev->interrupt_out_busy,
460  2 * HZ);
461  usb_tranzport_abort_transfers(dev);
462  dev->open_count = 0;
463 
464 unlock_exit:
465  mutex_unlock(&dev->mtx);
466 
467 exit:
468  return retval;
469 }
470 
474 static unsigned int usb_tranzport_poll(struct file *file, poll_table *wait)
475 {
476  struct usb_tranzport *dev;
477  unsigned int mask = 0;
478  dev = file->private_data;
479  poll_wait(file, &dev->read_wait, wait);
480  poll_wait(file, &dev->write_wait, wait);
481  if (dev->ring_head != dev->ring_tail)
482  mask |= POLLIN | POLLRDNORM;
483  if (!dev->interrupt_out_busy)
484  mask |= POLLOUT | POLLWRNORM;
485  return mask;
486 }
491 static ssize_t usb_tranzport_read(struct file *file, char __user *buffer,
492  size_t count, loff_t *ppos)
493 {
494  struct usb_tranzport *dev;
495  int retval = 0;
496 #if BUFFERED_READS
497  int c = 0;
498 #endif
499 #if COMPRESS_WHEEL_EVENTS
500  signed char oldwheel;
501  signed char newwheel;
502  int cancompress = 1;
503  int next_tail;
504 #endif
505 
506  /* do I have such a thing as a null event? */
507 
508  dev = file->private_data;
509 
510  /* verify that we actually have some data to read */
511  if (count == 0)
512  goto exit;
513 
514  /* lock this object */
515  if (mutex_lock_interruptible(&dev->mtx)) {
516  retval = -ERESTARTSYS;
517  goto exit;
518  }
519 
520  /* verify that the device wasn't unplugged */
521  if (dev->intf == NULL) {
522  retval = -ENODEV;
523  pr_err("%s: No device or device unplugged %d\n",
524  __func__, retval);
525  goto unlock_exit;
526  }
527 
528  while (dev->ring_head == dev->ring_tail) {
529 
530  if (file->f_flags & O_NONBLOCK) {
531  retval = -EAGAIN;
532  goto unlock_exit;
533  }
534  /* tiny race - FIXME: make atomic? */
535  /* atomic_cmp_exchange(&dev->interrupt_in_done,0,0); */
536  dev->interrupt_in_done = 0;
537  retval = wait_event_interruptible(dev->read_wait,
538  dev->interrupt_in_done);
539  if (retval < 0)
540  goto unlock_exit;
541  }
542 
543  dbg_info(&dev->intf->dev,
544  "%s: copying to userspace: "
545  "%02x%02x%02x%02x%02x%02x%02x%02x\n",
546  __func__,
547  (*dev->ring_buffer)[dev->ring_tail].cmd[0],
548  (*dev->ring_buffer)[dev->ring_tail].cmd[1],
549  (*dev->ring_buffer)[dev->ring_tail].cmd[2],
550  (*dev->ring_buffer)[dev->ring_tail].cmd[3],
551  (*dev->ring_buffer)[dev->ring_tail].cmd[4],
552  (*dev->ring_buffer)[dev->ring_tail].cmd[5],
553  (*dev->ring_buffer)[dev->ring_tail].cmd[6],
554  (*dev->ring_buffer)[dev->ring_tail].cmd[7]);
555 
556 #if BUFFERED_READS
557  c = 0;
558  while ((c < count) && (dev->ring_tail != dev->ring_head)) {
559 
560 #if COMPRESS_WHEEL_EVENTS
561  next_tail = (dev->ring_tail+1) % ring_buffer_size;
562  if (dev->compress_wheel)
563  cancompress = 1;
564  while (dev->ring_head != next_tail && cancompress == 1) {
565  newwheel = (*dev->ring_buffer)[next_tail].cmd[6];
566  oldwheel = (*dev->ring_buffer)[dev->ring_tail].cmd[6];
567  /* if both are wheel events, and
568  no buttons have changes (FIXME, do I have to check?),
569  and we are the same sign, we can compress +- 7F
570  */
571  dbg_info(&dev->intf->dev,
572  "%s: trying to compress: "
573  "%02x%02x%02x%02x%02x%02x%02x%02x\n",
574  __func__,
575  (*dev->ring_buffer)[dev->ring_tail].cmd[0],
576  (*dev->ring_buffer)[dev->ring_tail].cmd[1],
577  (*dev->ring_buffer)[dev->ring_tail].cmd[2],
578  (*dev->ring_buffer)[dev->ring_tail].cmd[3],
579  (*dev->ring_buffer)[dev->ring_tail].cmd[4],
580  (*dev->ring_buffer)[dev->ring_tail].cmd[5],
581  (*dev->ring_buffer)[dev->ring_tail].cmd[6],
582  (*dev->ring_buffer)[dev->ring_tail].cmd[7]);
583 
584  if (((*dev->ring_buffer)[dev->ring_tail].cmd[6] != 0 &&
585  (*dev->ring_buffer)[next_tail].cmd[6] != 0) &&
586  ((newwheel > 0 && oldwheel > 0) ||
587  (newwheel < 0 && oldwheel < 0)) &&
588  ((*dev->ring_buffer)[dev->ring_tail].cmd[2] ==
589  (*dev->ring_buffer)[next_tail].cmd[2]) &&
590  ((*dev->ring_buffer)[dev->ring_tail].cmd[3] ==
591  (*dev->ring_buffer)[next_tail].cmd[3]) &&
592  ((*dev->ring_buffer)[dev->ring_tail].cmd[4] ==
593  (*dev->ring_buffer)[next_tail].cmd[4]) &&
594  ((*dev->ring_buffer)[dev->ring_tail].cmd[5] ==
595  (*dev->ring_buffer)[next_tail].cmd[5])) {
596  dbg_info(&dev->intf->dev,
597  "%s: should compress: "
598  "%02x%02x%02x%02x%02x%02x%02x%02x\n",
599  __func__,
600  (*dev->ring_buffer)[dev->ring_tail].
601  cmd[0],
602  (*dev->ring_buffer)[dev->ring_tail].
603  cmd[1],
604  (*dev->ring_buffer)[dev->ring_tail].
605  cmd[2],
606  (*dev->ring_buffer)[dev->ring_tail].
607  cmd[3],
608  (*dev->ring_buffer)[dev->ring_tail].
609  cmd[4],
610  (*dev->ring_buffer)[dev->ring_tail].
611  cmd[5],
612  (*dev->ring_buffer)[dev->ring_tail].
613  cmd[6],
614  (*dev->ring_buffer)[dev->ring_tail].
615  cmd[7]);
616  newwheel += oldwheel;
617  if (oldwheel > 0 && !(newwheel > 0)) {
618  newwheel = 0x7f;
619  cancompress = 0;
620  }
621  if (oldwheel < 0 && !(newwheel < 0)) {
622  newwheel = 0x80;
623  cancompress = 0;
624  }
625 
626  (*dev->ring_buffer)[next_tail].cmd[6] =
627  newwheel;
628  dev->ring_tail = next_tail;
629  next_tail =
630  (dev->ring_tail + 1) % ring_buffer_size;
631  } else {
632  cancompress = 0;
633  }
634  }
635 #endif /* COMPRESS_WHEEL_EVENTS */
636  if (copy_to_user(
637  &buffer[c],
638  &(*dev->ring_buffer)[dev->ring_tail], 8)) {
639  retval = -EFAULT;
640  goto unlock_exit;
641  }
642  dev->ring_tail = (dev->ring_tail + 1) % ring_buffer_size;
643  c += 8;
644  dbg_info(&dev->intf->dev,
645  "%s: head, tail are %x, %x\n",
646  __func__, dev->ring_head, dev->ring_tail);
647  }
648  retval = c;
649 
650 #else
651 /* if (copy_to_user(buffer, &(*dev->ring_buffer)[dev->ring_tail], 8)) { */
652  retval = -EFAULT;
653  goto unlock_exit;
654 }
655 
656 dev->ring_tail = (dev->ring_tail + 1) % ring_buffer_size;
657 dbg_info(&dev->intf->dev, "%s: head, tail are %x, %x\n",
658  __func__, dev->ring_head, dev->ring_tail);
659 
660 retval = 8;
661 #endif /* BUFFERED_READS */
662 
663 unlock_exit:
664 /* unlock the device */
665 mutex_unlock(&dev->mtx);
666 
667 exit:
668 return retval;
669 }
670 
674 static ssize_t usb_tranzport_write(struct file *file,
675  const char __user *buffer, size_t count,
676  loff_t *ppos)
677 {
678  struct usb_tranzport *dev;
679  size_t bytes_to_write;
680  int retval = 0;
681 
682  dev = file->private_data;
683 
684  /* verify that we actually have some data to write */
685  if (count == 0)
686  goto exit;
687 
688  /* lock this object */
689  if (mutex_lock_interruptible(&dev->mtx)) {
690  retval = -ERESTARTSYS;
691  goto exit;
692  }
693  /* verify that the device wasn't unplugged */
694  if (dev->intf == NULL) {
695  retval = -ENODEV;
696  pr_err("%s: No device or device unplugged %d\n",
697  __func__, retval);
698  goto unlock_exit;
699  }
700 
701  /* wait until previous transfer is finished */
702  if (dev->interrupt_out_busy) {
703  if (file->f_flags & O_NONBLOCK) {
704  retval = -EAGAIN;
705  goto unlock_exit;
706  }
707  retval = wait_event_interruptible(dev->write_wait,
708  !dev->interrupt_out_busy);
709  if (retval < 0)
710  goto unlock_exit;
711  }
712 
713  /* write the data into interrupt_out_buffer from userspace */
714  bytes_to_write = min(count,
715  write_buffer_size *
717  if (bytes_to_write < count)
718  dev_warn(&dev->intf->dev,
719  "Write buffer overflow, %zd bytes dropped\n",
720  count - bytes_to_write);
721 
722  dbg_info(&dev->intf->dev,
723  "%s: count = %zd, bytes_to_write = %zd\n", __func__,
724  count, bytes_to_write);
725 
726  if (copy_from_user(dev->interrupt_out_buffer, buffer, bytes_to_write)) {
727  retval = -EFAULT;
728  goto unlock_exit;
729  }
730 
731  if (dev->interrupt_out_endpoint == NULL) {
732  dev_err(&dev->intf->dev, "Endpoint should not be be null!\n");
733  goto unlock_exit;
734  }
735 
736  /* send off the urb */
737  usb_fill_int_urb(dev->interrupt_out_urb,
738  interface_to_usbdev(dev->intf),
739  usb_sndintpipe(interface_to_usbdev(dev->intf),
742  dev->interrupt_out_buffer, bytes_to_write,
743  usb_tranzport_interrupt_out_callback, dev,
745 
746  dev->interrupt_out_busy = 1;
747  wmb();
748 
750  if (retval) {
751  dev->interrupt_out_busy = 0;
752  dev_err(&dev->intf->dev,
753  "Couldn't submit interrupt_out_urb %d\n", retval);
754  goto unlock_exit;
755  }
756  retval = bytes_to_write;
757 
758 unlock_exit:
759  /* unlock the device */
760  mutex_unlock(&dev->mtx);
761 
762 exit:
763  return retval;
764 }
765 
766 /* file operations needed when we register this driver */
767 static const struct file_operations usb_tranzport_fops = {
768  .owner = THIS_MODULE,
769  .read = usb_tranzport_read,
770  .write = usb_tranzport_write,
771  .open = usb_tranzport_open,
772  .release = usb_tranzport_release,
773  .poll = usb_tranzport_poll,
774  .llseek = no_llseek,
775 };
776 
777 /*
778  * usb class driver info in order to get a minor number from the usb core,
779  * and to have the device registered with the driver core
780  */
781 static struct usb_class_driver usb_tranzport_class = {
782  .name = "tranzport%d",
783  .fops = &usb_tranzport_fops,
784  .minor_base = USB_TRANZPORT_MINOR_BASE,
785 };
786 
793 static int usb_tranzport_probe(struct usb_interface *intf,
794  const struct usb_device_id *id) {
795  struct usb_device *udev = interface_to_usbdev(intf);
796  struct usb_tranzport *dev = NULL;
797  struct usb_host_interface *iface_desc;
799  int i;
800  int true_size;
801  int retval = -ENOMEM;
802 
803  /* allocate memory for our device state and initialize it */
804 
805  dev = kzalloc(sizeof(*dev), GFP_KERNEL);
806  if (dev == NULL) {
807  dev_err(&intf->dev, "Out of memory\n");
808  goto exit;
809  }
810  mutex_init(&dev->mtx);
811  dev->intf = intf;
814 
815  iface_desc = intf->cur_altsetting;
816 
817  /* set up the endpoint information */
818  for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
819  endpoint = &iface_desc->endpoint[i].desc;
820 
821  if (usb_endpoint_is_int_in(endpoint))
822  dev->interrupt_in_endpoint = endpoint;
823 
824  if (usb_endpoint_is_int_out(endpoint))
825  dev->interrupt_out_endpoint = endpoint;
826  }
827  if (dev->interrupt_in_endpoint == NULL) {
828  dev_err(&intf->dev, "Interrupt in endpoint not found\n");
829  goto error;
830  }
831  if (dev->interrupt_out_endpoint == NULL)
832  dev_warn(&intf->dev,
833  "Interrupt out endpoint not found"
834  "(using control endpoint instead)\n");
835 
837  le16_to_cpu(dev->interrupt_in_endpoint->wMaxPacketSize);
838 
839  if (dev->interrupt_in_endpoint_size != 8)
840  dev_warn(&intf->dev, "Interrupt in endpoint size is not 8!\n");
841 
842  if (ring_buffer_size == 0)
843  ring_buffer_size = RING_BUFFER_SIZE;
844  true_size = min(ring_buffer_size, RING_BUFFER_SIZE);
845 
846  /* FIXME - there are more usb_alloc routines for dma correctness.
847  Needed? */
848 
849  dev->ring_buffer =
850  kmalloc((true_size * sizeof(struct tranzport_cmd)) + 8, GFP_KERNEL);
851 
852  if (!dev->ring_buffer) {
853  dev_err(&intf->dev,
854  "Couldn't allocate ring_buffer size %d\n", true_size);
855  goto error;
856  }
857  dev->interrupt_in_buffer =
859  if (!dev->interrupt_in_buffer) {
860  dev_err(&intf->dev, "Couldn't allocate interrupt_in_buffer\n");
861  goto error;
862  }
864  if (!dev->interrupt_in_urb) {
865  dev_err(&intf->dev, "Couldn't allocate interrupt_in_urb\n");
866  goto error;
867  }
870  le16_to_cpu(dev->interrupt_out_endpoint->wMaxPacketSize) :
872 
873  if (dev->interrupt_out_endpoint_size != 8)
874  dev_warn(&intf->dev,
875  "Interrupt out endpoint size is not 8!)\n");
876 
877  dev->interrupt_out_buffer =
878  kmalloc(write_buffer_size * dev->interrupt_out_endpoint_size,
879  GFP_KERNEL);
880  if (!dev->interrupt_out_buffer) {
881  dev_err(&intf->dev, "Couldn't allocate interrupt_out_buffer\n");
882  goto error;
883  }
885  if (!dev->interrupt_out_urb) {
886  dev_err(&intf->dev, "Couldn't allocate interrupt_out_urb\n");
887  goto error;
888  }
889  dev->interrupt_in_interval =
890  min_interrupt_in_interval >
891  dev->interrupt_in_endpoint->bInterval ? min_interrupt_in_interval
892  : dev->interrupt_in_endpoint->bInterval;
893 
894  if (dev->interrupt_out_endpoint) {
896  min_interrupt_out_interval >
897  dev->interrupt_out_endpoint->bInterval ?
898  min_interrupt_out_interval :
899  dev->interrupt_out_endpoint->bInterval;
900  }
901 
902  /* we can register the device now, as it is ready */
903  usb_set_intfdata(intf, dev);
904 
905  retval = usb_register_dev(intf, &usb_tranzport_class);
906  if (retval) {
907  /* something prevented us from registering this driver */
908  dev_err(&intf->dev,
909  "Not able to get a minor for this device.\n");
910  usb_set_intfdata(intf, NULL);
911  goto error;
912  }
913 
914  retval = device_create_file(&intf->dev, &dev_attr_compress_wheel);
915  if (retval)
916  goto error;
917  retval = device_create_file(&intf->dev, &dev_attr_enable);
918  if (retval)
919  goto error;
920  retval = device_create_file(&intf->dev, &dev_attr_offline);
921  if (retval)
922  goto error;
923 
924  /* let the user know what node this device is now attached to */
925  dev_info(&intf->dev,
926  "Tranzport Device #%d now attached to major %d minor %d\n",
927  (intf->minor - USB_TRANZPORT_MINOR_BASE), USB_MAJOR,
928  intf->minor);
929 
930 exit:
931  return retval;
932 
933 error:
934  usb_tranzport_delete(dev);
935  return retval;
936 }
937 
943 static void usb_tranzport_disconnect(struct usb_interface *intf)
944 {
945  struct usb_tranzport *dev;
946  int minor;
947  mutex_lock(&disconnect_mutex);
948  dev = usb_get_intfdata(intf);
949  usb_set_intfdata(intf, NULL);
950  mutex_lock(&dev->mtx);
951  minor = intf->minor;
952  /* give back our minor */
953  usb_deregister_dev(intf, &usb_tranzport_class);
954 
955  /* if the device is not opened, then we clean up right now */
956  if (!dev->open_count) {
957  mutex_unlock(&dev->mtx);
958  usb_tranzport_delete(dev);
959  } else {
960  dev->intf = NULL;
961  mutex_unlock(&dev->mtx);
962  }
963 
964  mutex_unlock(&disconnect_mutex);
965 
966  dev_info(&intf->dev, "Tranzport Surface #%d now disconnected\n",
967  (minor - USB_TRANZPORT_MINOR_BASE));
968 }
969 
970 /* usb specific object needed to register this driver with the usb subsystem */
971 static struct usb_driver usb_tranzport_driver = {
972  .name = "tranzport",
973  .probe = usb_tranzport_probe,
974  .disconnect = usb_tranzport_disconnect,
975  .id_table = usb_tranzport_table,
976 };
977 
978 module_usb_driver(usb_tranzport_driver);