2 * Frontier Designs Tranzport driver
4 * Copyright (C) 2007 Michael Taht (m@taht.net)
6 * Based on the usbled driver and ldusb drivers by
8 * Copyright (C) 2004 Greg Kroah-Hartman (greg@kroah.com)
9 * Copyright (C) 2005 Michael Hund <mhund@ld-didactic.de>
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>
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.
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.
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.
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>
42 #include <linux/uaccess.h>
43 #include <linux/input.h>
44 #include <linux/usb.h>
45 #include <linux/poll.h>
47 /* Define these values to match your devices */
48 #define VENDOR_ID 0x165b
49 #define PRODUCT_ID 0x8101
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
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 */
63 MODULE_DEVICE_TABLE(usb, usb_tranzport_table);
64 MODULE_VERSION("0.35");
65 MODULE_AUTHOR("Mike Taht <m@taht.net>");
66 MODULE_DESCRIPTION("Tranzport USB Driver");
67 MODULE_LICENSE("GPL");
68 MODULE_SUPPORTED_DEVICE("Frontier Designs Tranzport Control Surface");
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
78 static int debug = TRANZPORT_DEBUG;
80 /* Use our own dbg macro */
81 #define dbg_info(dev, format, arg...) do \
82 { if (debug) dev_info(dev , format , ## arg); } while (0)
84 /* Module parameters */
86 module_param(debug, int, S_IRUGO | S_IWUSR);
87 MODULE_PARM_DESC(debug, "Debug enabled or not");
90 * All interrupt in transfers are collected in a ring buffer to
91 * avoid racing conditions and get better performance of the driver.
94 static int ring_buffer_size = RING_BUFFER_SIZE;
96 module_param(ring_buffer_size, int, S_IRUGO);
97 MODULE_PARM_DESC(ring_buffer_size, "Read ring buffer size in reports");
100 * The write_buffer can one day contain more than one interrupt out transfer.
102 static int write_buffer_size = WRITE_BUFFER_SIZE;
103 module_param(write_buffer_size, int, S_IRUGO);
104 MODULE_PARM_DESC(write_buffer_size, "Write buffer size");
107 * Increase the interval for debugging purposes.
108 * or set to 1 to use the standard interval from the endpoint descriptors.
111 static int min_interrupt_in_interval = TRANZPORT_USB_TIMEOUT;
112 module_param(min_interrupt_in_interval, int, 0);
113 MODULE_PARM_DESC(min_interrupt_in_interval,
114 "Minimum interrupt in interval in ms");
116 static int min_interrupt_out_interval = TRANZPORT_USB_TIMEOUT;
117 module_param(min_interrupt_out_interval, int, 0);
118 MODULE_PARM_DESC(min_interrupt_out_interval,
119 "Minimum interrupt out interval in ms");
121 struct tranzport_cmd {
122 unsigned char cmd[8];
125 /* Structure to hold all of our device specific stuff */
127 struct usb_tranzport {
128 struct mutex mtx; /* locks this structure */
129 struct usb_interface *intf; /* save off the usb interface pointer */
130 int open_count; /* number of times this port opened */
131 struct tranzport_cmd (*ring_buffer)[RING_BUFFER_SIZE];
132 unsigned int ring_head;
133 unsigned int ring_tail;
134 wait_queue_head_t read_wait;
135 wait_queue_head_t write_wait;
136 unsigned char *interrupt_in_buffer;
137 struct usb_endpoint_descriptor *interrupt_in_endpoint;
138 struct urb *interrupt_in_urb;
139 int interrupt_in_interval;
140 size_t interrupt_in_endpoint_size;
141 int interrupt_in_running;
142 int interrupt_in_done;
143 char *interrupt_out_buffer;
144 struct usb_endpoint_descriptor *interrupt_out_endpoint;
145 struct urb *interrupt_out_urb;
146 int interrupt_out_interval;
147 size_t interrupt_out_endpoint_size;
148 int interrupt_out_busy;
152 unsigned char enable; /* 0 if disabled 1 if enabled */
153 unsigned char offline; /* if the device is out of range or asleep */
154 unsigned char compress_wheel; /* flag to compress wheel events */
157 /* prevent races between open() and disconnect() */
158 static DEFINE_MUTEX(disconnect_mutex);
160 static struct usb_driver usb_tranzport_driver;
163 * usb_tranzport_abort_transfers
164 * aborts transfers and frees associated data structures
166 static void usb_tranzport_abort_transfers(struct usb_tranzport *dev)
168 /* shutdown transfer */
169 if (dev->interrupt_in_running) {
170 dev->interrupt_in_running = 0;
172 usb_kill_urb(dev->interrupt_in_urb);
174 if (dev->interrupt_out_busy)
176 usb_kill_urb(dev->interrupt_out_urb);
179 #define show_int(value) \
180 static ssize_t value##_show(struct device *dev, \
181 struct device_attribute *attr, char *buf) \
183 struct usb_interface *intf = to_usb_interface(dev); \
184 struct usb_tranzport *t = usb_get_intfdata(intf); \
185 return sprintf(buf, "%d\n", t->value); \
187 static DEVICE_ATTR_RO(value)
189 #define show_set_int(value) \
190 static ssize_t value##_show(struct device *dev, \
191 struct device_attribute *attr, char *buf) \
193 struct usb_interface *intf = to_usb_interface(dev); \
194 struct usb_tranzport *t = usb_get_intfdata(intf); \
195 return sprintf(buf, "%d\n", t->value); \
197 static ssize_t value##_store(struct device *dev, \
198 struct device_attribute *attr, \
199 const char *buf, size_t count) \
201 struct usb_interface *intf = to_usb_interface(dev); \
202 struct usb_tranzport *t = usb_get_intfdata(intf); \
203 unsigned long temp; \
204 if (kstrtoul(buf, 10, &temp)) \
209 static DEVICE_ATTR_RW(value)
213 show_set_int(compress_wheel);
216 * usb_tranzport_delete
218 static void usb_tranzport_delete(struct usb_tranzport *dev)
220 usb_tranzport_abort_transfers(dev);
221 if (dev->intf != NULL) {
222 device_remove_file(&dev->intf->dev, &dev_attr_enable);
223 device_remove_file(&dev->intf->dev, &dev_attr_offline);
224 device_remove_file(&dev->intf->dev, &dev_attr_compress_wheel);
227 /* free data structures */
228 usb_free_urb(dev->interrupt_in_urb);
229 usb_free_urb(dev->interrupt_out_urb);
230 kfree(dev->ring_buffer);
231 kfree(dev->interrupt_in_buffer);
232 kfree(dev->interrupt_out_buffer);
237 * usb_tranzport_interrupt_in_callback
240 static void usb_tranzport_interrupt_in_callback(struct urb *urb)
242 struct usb_tranzport *dev = urb->context;
243 unsigned int next_ring_head;
247 if (urb->status == -ENOENT ||
248 urb->status == -ECONNRESET ||
249 urb->status == -ESHUTDOWN) {
252 dbg_info(&dev->intf->dev,
253 "%s: nonzero status received: %d\n",
254 __func__, urb->status);
255 goto resubmit; /* maybe we can recover */
259 if (urb->actual_length != 8) {
260 dev_warn(&dev->intf->dev,
261 "Urb length was %d bytes!!"
262 "Do something intelligent\n",
265 dbg_info(&dev->intf->dev,
266 "%s: received: %02x%02x%02x%02x%02x%02x%02x%02x\n",
267 __func__, dev->interrupt_in_buffer[0],
268 dev->interrupt_in_buffer[1],
269 dev->interrupt_in_buffer[2],
270 dev->interrupt_in_buffer[3],
271 dev->interrupt_in_buffer[4],
272 dev->interrupt_in_buffer[5],
273 dev->interrupt_in_buffer[6],
274 dev->interrupt_in_buffer[7]);
275 #if SUPPRESS_EXTRA_OFFLINE_EVENTS
276 if (dev->offline == 2 && dev->interrupt_in_buffer[1] == 0xff)
278 if (dev->offline == 1 && dev->interrupt_in_buffer[1] == 0xff) {
283 /* Always pass one offline event up the stack */
284 if (dev->offline > 0 && dev->interrupt_in_buffer[1] != 0xff)
286 if (dev->offline == 0 && dev->interrupt_in_buffer[1] == 0xff)
289 #endif /* SUPPRESS_EXTRA_OFFLINE_EVENTS */
290 dbg_info(&dev->intf->dev, "%s: head, tail are %x, %x\n",
291 __func__, dev->ring_head, dev->ring_tail);
293 next_ring_head = (dev->ring_head + 1) % ring_buffer_size;
295 if (next_ring_head != dev->ring_tail) {
296 memcpy(&((*dev->ring_buffer)[dev->ring_head]),
297 dev->interrupt_in_buffer, urb->actual_length);
298 dev->ring_head = next_ring_head;
300 memset(dev->interrupt_in_buffer, 0, urb->actual_length);
302 dev_warn(&dev->intf->dev,
303 "Ring buffer overflow, %d bytes dropped\n",
305 memset(dev->interrupt_in_buffer, 0, urb->actual_length);
310 /* resubmit if we're still running */
311 if (dev->interrupt_in_running && dev->intf) {
312 retval = usb_submit_urb(dev->interrupt_in_urb, GFP_ATOMIC);
314 dev_err(&dev->intf->dev,
315 "usb_submit_urb failed (%d)\n", retval);
319 dev->interrupt_in_done = 1;
320 wake_up_interruptible(&dev->read_wait);
324 * usb_tranzport_interrupt_out_callback
326 static void usb_tranzport_interrupt_out_callback(struct urb *urb)
328 struct usb_tranzport *dev = urb->context;
329 /* sync/async unlink faults aren't errors */
330 if (urb->status && !(urb->status == -ENOENT ||
331 urb->status == -ECONNRESET ||
332 urb->status == -ESHUTDOWN))
333 dbg_info(&dev->intf->dev,
334 "%s - nonzero write interrupt status received: %d\n",
335 __func__, urb->status);
337 dev->interrupt_out_busy = 0;
338 wake_up_interruptible(&dev->write_wait);
343 static int usb_tranzport_open(struct inode *inode, struct file *file)
345 struct usb_tranzport *dev;
348 struct usb_interface *interface;
350 nonseekable_open(inode, file);
351 subminor = iminor(inode);
353 mutex_lock(&disconnect_mutex);
355 interface = usb_find_interface(&usb_tranzport_driver, subminor);
358 pr_err("%s - error, can't find device for minor %d\n",
361 goto unlock_disconnect_exit;
364 dev = usb_get_intfdata(interface);
368 goto unlock_disconnect_exit;
371 /* lock this device */
372 if (mutex_lock_interruptible(&dev->mtx)) {
373 retval = -ERESTARTSYS;
374 goto unlock_disconnect_exit;
377 /* allow opening only once */
378 if (dev->open_count) {
384 /* initialize in direction */
387 usb_fill_int_urb(dev->interrupt_in_urb,
388 interface_to_usbdev(interface),
389 usb_rcvintpipe(interface_to_usbdev(interface),
390 dev->interrupt_in_endpoint->
392 dev->interrupt_in_buffer,
393 dev->interrupt_in_endpoint_size,
394 usb_tranzport_interrupt_in_callback, dev,
395 dev->interrupt_in_interval);
397 dev->interrupt_in_running = 1;
398 dev->interrupt_in_done = 0;
401 dev->compress_wheel = 1;
403 retval = usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL);
405 dev_err(&interface->dev,
406 "Couldn't submit interrupt_in_urb %d\n", retval);
407 dev->interrupt_in_running = 0;
412 /* save device in the file's private structure */
413 file->private_data = dev;
416 mutex_unlock(&dev->mtx);
418 unlock_disconnect_exit:
419 mutex_unlock(&disconnect_mutex);
425 * usb_tranzport_release
427 static int usb_tranzport_release(struct inode *inode, struct file *file)
429 struct usb_tranzport *dev;
432 dev = file->private_data;
439 if (mutex_lock_interruptible(&dev->mtx)) {
440 retval = -ERESTARTSYS;
444 if (dev->open_count != 1) {
449 if (dev->intf == NULL) {
450 /* the device was unplugged before the file was released */
451 mutex_unlock(&dev->mtx);
452 /* unlock here as usb_tranzport_delete frees dev */
453 usb_tranzport_delete(dev);
458 /* wait until write transfer is finished */
459 if (dev->interrupt_out_busy)
460 wait_event_interruptible_timeout(dev->write_wait,
461 !dev->interrupt_out_busy,
463 usb_tranzport_abort_transfers(dev);
467 mutex_unlock(&dev->mtx);
476 static unsigned int usb_tranzport_poll(struct file *file, poll_table *wait)
478 struct usb_tranzport *dev;
479 unsigned int mask = 0;
480 dev = file->private_data;
481 poll_wait(file, &dev->read_wait, wait);
482 poll_wait(file, &dev->write_wait, wait);
483 if (dev->ring_head != dev->ring_tail)
484 mask |= POLLIN | POLLRDNORM;
485 if (!dev->interrupt_out_busy)
486 mask |= POLLOUT | POLLWRNORM;
493 static ssize_t usb_tranzport_read(struct file *file, char __user *buffer,
494 size_t count, loff_t *ppos)
496 struct usb_tranzport *dev;
501 #if COMPRESS_WHEEL_EVENTS
502 signed char oldwheel;
503 signed char newwheel;
508 /* do I have such a thing as a null event? */
510 dev = file->private_data;
512 /* verify that we actually have some data to read */
516 /* lock this object */
517 if (mutex_lock_interruptible(&dev->mtx)) {
518 retval = -ERESTARTSYS;
522 /* verify that the device wasn't unplugged */
523 if (dev->intf == NULL) {
525 pr_err("%s: No device or device unplugged %d\n",
530 while (dev->ring_head == dev->ring_tail) {
532 if (file->f_flags & O_NONBLOCK) {
536 /* tiny race - FIXME: make atomic? */
537 /* atomic_cmp_exchange(&dev->interrupt_in_done,0,0); */
538 dev->interrupt_in_done = 0;
539 retval = wait_event_interruptible(dev->read_wait,
540 dev->interrupt_in_done);
545 dbg_info(&dev->intf->dev,
546 "%s: copying to userspace: "
547 "%02x%02x%02x%02x%02x%02x%02x%02x\n",
549 (*dev->ring_buffer)[dev->ring_tail].cmd[0],
550 (*dev->ring_buffer)[dev->ring_tail].cmd[1],
551 (*dev->ring_buffer)[dev->ring_tail].cmd[2],
552 (*dev->ring_buffer)[dev->ring_tail].cmd[3],
553 (*dev->ring_buffer)[dev->ring_tail].cmd[4],
554 (*dev->ring_buffer)[dev->ring_tail].cmd[5],
555 (*dev->ring_buffer)[dev->ring_tail].cmd[6],
556 (*dev->ring_buffer)[dev->ring_tail].cmd[7]);
560 while ((c < count) && (dev->ring_tail != dev->ring_head)) {
562 #if COMPRESS_WHEEL_EVENTS
563 next_tail = (dev->ring_tail+1) % ring_buffer_size;
564 if (dev->compress_wheel)
566 while (dev->ring_head != next_tail && cancompress == 1) {
567 newwheel = (*dev->ring_buffer)[next_tail].cmd[6];
568 oldwheel = (*dev->ring_buffer)[dev->ring_tail].cmd[6];
569 /* if both are wheel events, and
570 * no buttons have changes (FIXME, do I have to check?),
571 * and we are the same sign, we can compress +- 7F
573 dbg_info(&dev->intf->dev,
574 "%s: trying to compress: "
575 "%02x%02x%02x%02x%02x%02x%02x%02x\n",
577 (*dev->ring_buffer)[dev->ring_tail].cmd[0],
578 (*dev->ring_buffer)[dev->ring_tail].cmd[1],
579 (*dev->ring_buffer)[dev->ring_tail].cmd[2],
580 (*dev->ring_buffer)[dev->ring_tail].cmd[3],
581 (*dev->ring_buffer)[dev->ring_tail].cmd[4],
582 (*dev->ring_buffer)[dev->ring_tail].cmd[5],
583 (*dev->ring_buffer)[dev->ring_tail].cmd[6],
584 (*dev->ring_buffer)[dev->ring_tail].cmd[7]);
586 if (((*dev->ring_buffer)[dev->ring_tail].cmd[6] != 0 &&
587 (*dev->ring_buffer)[next_tail].cmd[6] != 0) &&
588 ((newwheel > 0 && oldwheel > 0) ||
589 (newwheel < 0 && oldwheel < 0)) &&
590 ((*dev->ring_buffer)[dev->ring_tail].cmd[2] ==
591 (*dev->ring_buffer)[next_tail].cmd[2]) &&
592 ((*dev->ring_buffer)[dev->ring_tail].cmd[3] ==
593 (*dev->ring_buffer)[next_tail].cmd[3]) &&
594 ((*dev->ring_buffer)[dev->ring_tail].cmd[4] ==
595 (*dev->ring_buffer)[next_tail].cmd[4]) &&
596 ((*dev->ring_buffer)[dev->ring_tail].cmd[5] ==
597 (*dev->ring_buffer)[next_tail].cmd[5])) {
598 dbg_info(&dev->intf->dev,
599 "%s: should compress: "
600 "%02x%02x%02x%02x%02x%02x%02x%02x\n",
602 (*dev->ring_buffer)[dev->ring_tail].
604 (*dev->ring_buffer)[dev->ring_tail].
606 (*dev->ring_buffer)[dev->ring_tail].
608 (*dev->ring_buffer)[dev->ring_tail].
610 (*dev->ring_buffer)[dev->ring_tail].
612 (*dev->ring_buffer)[dev->ring_tail].
614 (*dev->ring_buffer)[dev->ring_tail].
616 (*dev->ring_buffer)[dev->ring_tail].
618 newwheel += oldwheel;
619 if (oldwheel > 0 && !(newwheel > 0)) {
623 if (oldwheel < 0 && !(newwheel < 0)) {
628 (*dev->ring_buffer)[next_tail].cmd[6] =
630 dev->ring_tail = next_tail;
632 (dev->ring_tail + 1) % ring_buffer_size;
637 #endif /* COMPRESS_WHEEL_EVENTS */
640 &(*dev->ring_buffer)[dev->ring_tail], 8)) {
644 dev->ring_tail = (dev->ring_tail + 1) % ring_buffer_size;
646 dbg_info(&dev->intf->dev,
647 "%s: head, tail are %x, %x\n",
648 __func__, dev->ring_head, dev->ring_tail);
653 /* if (copy_to_user(buffer, &(*dev->ring_buffer)[dev->ring_tail], 8)) { */
658 dev->ring_tail = (dev->ring_tail + 1) % ring_buffer_size;
659 dbg_info(&dev->intf->dev, "%s: head, tail are %x, %x\n",
660 __func__, dev->ring_head, dev->ring_tail);
663 #endif /* BUFFERED_READS */
666 /* unlock the device */
667 mutex_unlock(&dev->mtx);
674 * usb_tranzport_write
676 static ssize_t usb_tranzport_write(struct file *file,
677 const char __user *buffer, size_t count,
680 struct usb_tranzport *dev;
681 size_t bytes_to_write;
684 dev = file->private_data;
686 /* verify that we actually have some data to write */
690 /* lock this object */
691 if (mutex_lock_interruptible(&dev->mtx)) {
692 retval = -ERESTARTSYS;
695 /* verify that the device wasn't unplugged */
696 if (dev->intf == NULL) {
698 pr_err("%s: No device or device unplugged %d\n",
703 /* wait until previous transfer is finished */
704 if (dev->interrupt_out_busy) {
705 if (file->f_flags & O_NONBLOCK) {
709 retval = wait_event_interruptible(dev->write_wait,
710 !dev->interrupt_out_busy);
715 /* write the data into interrupt_out_buffer from userspace */
716 bytes_to_write = min(count,
718 dev->interrupt_out_endpoint_size);
719 if (bytes_to_write < count)
720 dev_warn(&dev->intf->dev,
721 "Write buffer overflow, %zd bytes dropped\n",
722 count - bytes_to_write);
724 dbg_info(&dev->intf->dev,
725 "%s: count = %zd, bytes_to_write = %zd\n", __func__,
726 count, bytes_to_write);
728 if (copy_from_user(dev->interrupt_out_buffer, buffer, bytes_to_write)) {
733 if (dev->interrupt_out_endpoint == NULL) {
734 dev_err(&dev->intf->dev, "Endpoint should not be null!\n");
738 /* send off the urb */
739 usb_fill_int_urb(dev->interrupt_out_urb,
740 interface_to_usbdev(dev->intf),
741 usb_sndintpipe(interface_to_usbdev(dev->intf),
742 dev->interrupt_out_endpoint->
744 dev->interrupt_out_buffer, bytes_to_write,
745 usb_tranzport_interrupt_out_callback, dev,
746 dev->interrupt_out_interval);
748 dev->interrupt_out_busy = 1;
751 retval = usb_submit_urb(dev->interrupt_out_urb, GFP_KERNEL);
753 dev->interrupt_out_busy = 0;
754 dev_err(&dev->intf->dev,
755 "Couldn't submit interrupt_out_urb %d\n", retval);
758 retval = bytes_to_write;
761 /* unlock the device */
762 mutex_unlock(&dev->mtx);
768 /* file operations needed when we register this driver */
769 static const struct file_operations usb_tranzport_fops = {
770 .owner = THIS_MODULE,
771 .read = usb_tranzport_read,
772 .write = usb_tranzport_write,
773 .open = usb_tranzport_open,
774 .release = usb_tranzport_release,
775 .poll = usb_tranzport_poll,
780 * usb class driver info in order to get a minor number from the usb core,
781 * and to have the device registered with the driver core
783 static struct usb_class_driver usb_tranzport_class = {
784 .name = "tranzport%d",
785 .fops = &usb_tranzport_fops,
786 .minor_base = USB_TRANZPORT_MINOR_BASE,
790 * usb_tranzport_probe
792 * Called by the usb core when a new device is connected that it thinks
793 * this driver might be interested in.
795 static int usb_tranzport_probe(struct usb_interface *intf,
796 const struct usb_device_id *id) {
797 struct usb_device *udev = interface_to_usbdev(intf);
798 struct usb_tranzport *dev = NULL;
799 struct usb_host_interface *iface_desc;
800 struct usb_endpoint_descriptor *endpoint;
803 int retval = -ENOMEM;
805 /* allocate memory for our device state and initialize it */
807 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
811 mutex_init(&dev->mtx);
813 init_waitqueue_head(&dev->read_wait);
814 init_waitqueue_head(&dev->write_wait);
816 iface_desc = intf->cur_altsetting;
818 /* set up the endpoint information */
819 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
820 endpoint = &iface_desc->endpoint[i].desc;
822 if (usb_endpoint_is_int_in(endpoint))
823 dev->interrupt_in_endpoint = endpoint;
825 if (usb_endpoint_is_int_out(endpoint))
826 dev->interrupt_out_endpoint = endpoint;
828 if (dev->interrupt_in_endpoint == NULL) {
829 dev_err(&intf->dev, "Interrupt in endpoint not found\n");
832 if (dev->interrupt_out_endpoint == NULL)
834 "Interrupt out endpoint not found"
835 "(using control endpoint instead)\n");
837 dev->interrupt_in_endpoint_size =
838 le16_to_cpu(dev->interrupt_in_endpoint->wMaxPacketSize);
840 if (dev->interrupt_in_endpoint_size != 8)
841 dev_warn(&intf->dev, "Interrupt in endpoint size is not 8!\n");
843 if (ring_buffer_size == 0)
844 ring_buffer_size = RING_BUFFER_SIZE;
845 true_size = min(ring_buffer_size, RING_BUFFER_SIZE);
848 * FIXME - there are more usb_alloc routines for dma correctness.
853 kmalloc((true_size * sizeof(struct tranzport_cmd)) + 8, GFP_KERNEL);
854 if (!dev->ring_buffer)
857 dev->interrupt_in_buffer =
858 kmalloc(dev->interrupt_in_endpoint_size, GFP_KERNEL);
859 if (!dev->interrupt_in_buffer)
862 dev->interrupt_in_urb = usb_alloc_urb(0, GFP_KERNEL);
863 if (!dev->interrupt_in_urb) {
864 dev_err(&intf->dev, "Couldn't allocate interrupt_in_urb\n");
867 dev->interrupt_out_endpoint_size =
868 dev->interrupt_out_endpoint ?
869 le16_to_cpu(dev->interrupt_out_endpoint->wMaxPacketSize) :
870 udev->descriptor.bMaxPacketSize0;
872 if (dev->interrupt_out_endpoint_size != 8)
874 "Interrupt out endpoint size is not 8!)\n");
876 dev->interrupt_out_buffer =
877 kmalloc_array(write_buffer_size,
878 dev->interrupt_out_endpoint_size, GFP_KERNEL);
879 if (!dev->interrupt_out_buffer)
882 dev->interrupt_out_urb = usb_alloc_urb(0, GFP_KERNEL);
883 if (!dev->interrupt_out_urb) {
884 dev_err(&intf->dev, "Couldn't allocate interrupt_out_urb\n");
887 dev->interrupt_in_interval =
888 min_interrupt_in_interval >
889 dev->interrupt_in_endpoint->bInterval ? min_interrupt_in_interval
890 : dev->interrupt_in_endpoint->bInterval;
892 if (dev->interrupt_out_endpoint) {
893 dev->interrupt_out_interval =
894 min_interrupt_out_interval >
895 dev->interrupt_out_endpoint->bInterval ?
896 min_interrupt_out_interval :
897 dev->interrupt_out_endpoint->bInterval;
900 /* we can register the device now, as it is ready */
901 usb_set_intfdata(intf, dev);
903 retval = usb_register_dev(intf, &usb_tranzport_class);
905 /* something prevented us from registering this driver */
907 "Not able to get a minor for this device.\n");
908 usb_set_intfdata(intf, NULL);
912 retval = device_create_file(&intf->dev, &dev_attr_compress_wheel);
915 retval = device_create_file(&intf->dev, &dev_attr_enable);
918 retval = device_create_file(&intf->dev, &dev_attr_offline);
922 /* let the user know what node this device is now attached to */
924 "Tranzport Device #%d now attached to major %d minor %d\n",
925 (intf->minor - USB_TRANZPORT_MINOR_BASE), USB_MAJOR,
932 usb_tranzport_delete(dev);
937 * usb_tranzport_disconnect
939 * Called by the usb core when the device is removed from the system.
941 static void usb_tranzport_disconnect(struct usb_interface *intf)
943 struct usb_tranzport *dev;
945 mutex_lock(&disconnect_mutex);
946 dev = usb_get_intfdata(intf);
947 usb_set_intfdata(intf, NULL);
948 mutex_lock(&dev->mtx);
950 /* give back our minor */
951 usb_deregister_dev(intf, &usb_tranzport_class);
953 /* if the device is not opened, then we clean up right now */
954 if (!dev->open_count) {
955 mutex_unlock(&dev->mtx);
956 usb_tranzport_delete(dev);
959 mutex_unlock(&dev->mtx);
962 mutex_unlock(&disconnect_mutex);
964 dev_info(&intf->dev, "Tranzport Surface #%d now disconnected\n",
965 (minor - USB_TRANZPORT_MINOR_BASE));
968 /* usb specific object needed to register this driver with the usb subsystem */
969 static struct usb_driver usb_tranzport_driver = {
971 .probe = usb_tranzport_probe,
972 .disconnect = usb_tranzport_disconnect,
973 .id_table = usb_tranzport_table,
976 module_usb_driver(usb_tranzport_driver);