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>
41 #include <linux/version.h>
43 #include <asm/uaccess.h>
44 #include <linux/input.h>
45 #include <linux/usb.h>
46 #include <linux/poll.h>
48 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19)
49 #include frontier_compat.h
52 /* Define these values to match your devices */
53 #define VENDOR_ID 0x165b
54 #define PRODUCT_ID 0x8101
56 #ifdef CONFIG_USB_DYNAMIC_MINORS
57 #define USB_TRANZPORT_MINOR_BASE 0
59 // FIXME 176 - is the ldusb driver's minor - apply for a minor soon
60 #define USB_TRANZPORT_MINOR_BASE 177
63 /* table of devices that work with this driver */
64 static struct usb_device_id usb_tranzport_table [] = {
65 { USB_DEVICE(VENDOR_ID, PRODUCT_ID) },
66 { } /* Terminating entry */
69 MODULE_DEVICE_TABLE(usb, usb_tranzport_table);
70 MODULE_VERSION("0.33");
71 MODULE_AUTHOR("Mike Taht <m@taht.net>");
72 MODULE_DESCRIPTION("Tranzport USB Driver");
73 MODULE_LICENSE("GPL");
74 MODULE_SUPPORTED_DEVICE("Frontier Designs Tranzport Control Surface");
76 /* These two aren't done yet */
78 #define SUPPRESS_EXTRA_ONLINE_EVENTS 0
79 #define BUFFERED_WRITES 0
81 #define SUPPRESS_EXTRA_OFFLINE_EVENTS 1
82 #define COMPRESS_WHEEL_EVENTS 1
83 #define BUFFERED_READS 1
84 #define RING_BUFFER_SIZE 1000
85 #define WRITE_BUFFER_SIZE 34
86 #define TRANZPORT_USB_TIMEOUT 10
91 /* Use our own dbg macro */
92 #define dbg_info(dev, format, arg...) do { if (debug) dev_info(dev , format , ## arg); } while (0)
94 /* Module parameters */
96 module_param(debug, int, S_IRUGO | S_IWUSR);
97 MODULE_PARM_DESC(debug, "Debug enabled or not");
99 /* All interrupt in transfers are collected in a ring buffer to
100 * avoid racing conditions and get better performance of the driver.
103 static int ring_buffer_size = RING_BUFFER_SIZE;
105 module_param(ring_buffer_size, int, S_IRUGO);
106 MODULE_PARM_DESC(ring_buffer_size, "Read ring buffer size in reports");
108 /* The write_buffer can one day contain more than one interrupt out transfer.
110 static int write_buffer_size = WRITE_BUFFER_SIZE;
111 module_param(write_buffer_size, int, S_IRUGO);
112 MODULE_PARM_DESC(write_buffer_size, "Write buffer size");
115 * Increase the interval for debugging purposes.
116 * or set to 1 to use the standard interval from the endpoint descriptors.
119 static int min_interrupt_in_interval = TRANZPORT_USB_TIMEOUT;
120 module_param(min_interrupt_in_interval, int, 0);
121 MODULE_PARM_DESC(min_interrupt_in_interval, "Minimum interrupt in interval in ms");
123 static int min_interrupt_out_interval = TRANZPORT_USB_TIMEOUT;
124 module_param(min_interrupt_out_interval, int, 0);
125 MODULE_PARM_DESC(min_interrupt_out_interval, "Minimum interrupt out interval in ms");
127 struct tranzport_cmd {
128 unsigned char cmd[8];
141 static const char *Lightname[8] = { "LightRecord",
151 /* Structure to hold all of our device specific stuff */
153 struct usb_tranzport {
154 struct semaphore sem; /* locks this structure */
155 struct usb_interface* intf; /* save off the usb interface pointer */
157 int open_count; /* number of times this port has been opened */
159 struct tranzport_cmd (*ring_buffer)[RING_BUFFER_SIZE]; /* just make c happy */
160 unsigned int ring_head;
161 unsigned int ring_tail;
163 wait_queue_head_t read_wait;
164 wait_queue_head_t write_wait;
166 unsigned char* interrupt_in_buffer;
167 struct usb_endpoint_descriptor* interrupt_in_endpoint;
168 struct urb* interrupt_in_urb;
169 int interrupt_in_interval;
170 size_t interrupt_in_endpoint_size;
171 int interrupt_in_running;
172 int interrupt_in_done;
174 char* interrupt_out_buffer;
175 struct usb_endpoint_descriptor* interrupt_out_endpoint;
176 struct urb* interrupt_out_urb;
177 int interrupt_out_interval;
178 size_t interrupt_out_endpoint_size;
179 int interrupt_out_busy;
181 /* Sysfs and translation support */
183 int event; /* alternate interface to events */
184 int wheel; /* - for negative, 0 for none, + for positive */
185 unsigned char dump_state; /* 0 if disabled 1 if enabled */
186 unsigned char enable; /* 0 if disabled 1 if enabled */
187 unsigned char offline; /* if the device is out of range or asleep */
188 unsigned char compress_wheel; /* flag to compress wheel events */
189 unsigned char light; /* 7 bits used */
190 unsigned char last_cmd[8];
191 unsigned char last_input[8];
192 unsigned char screen[40]; // We'll also have cells
196 /* prevent races between open() and disconnect() */
197 static DEFINE_MUTEX(disconnect_mutex);
199 static struct usb_driver usb_tranzport_driver;
202 * usb_tranzport_abort_transfers
203 * aborts transfers and frees associated data structures
205 static void usb_tranzport_abort_transfers(struct usb_tranzport *dev)
207 /* shutdown transfer */
208 if (dev->interrupt_in_running) {
209 dev->interrupt_in_running = 0;
211 usb_kill_urb(dev->interrupt_in_urb);
213 if (dev->interrupt_out_busy)
215 usb_kill_urb(dev->interrupt_out_urb);
218 // FIXME ~light not good enough or correct - need atomic set_bit
220 #define show_set_light(value) \
221 static ssize_t show_##value(struct device *dev, struct device_attribute *attr, char *buf) \
223 struct usb_interface *intf = to_usb_interface(dev); \
224 struct usb_tranzport *t = usb_get_intfdata(intf); \
225 enum LightID light = value; \
226 int temp = (1 && (t->light & (1 << light))); \
227 return sprintf(buf, "%d\n", temp ); \
229 static ssize_t set_##value(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) \
231 struct usb_interface *intf = to_usb_interface(dev); \
232 struct usb_tranzport *t = usb_get_intfdata(intf); \
233 int temp = simple_strtoul(buf, NULL, 10); \
234 enum LightID light = (temp << value) & (t->light << value); \
235 t->light = (t->light & ~light) ; \
238 static DEVICE_ATTR(value, S_IWUGO | S_IRUGO, show_##value, set_##value);
240 show_set_light(LightRecord);
241 show_set_light(LightTrackrec);
242 show_set_light(LightTrackmute);
243 show_set_light(LightTracksolo);
244 show_set_light(LightAnysolo);
245 show_set_light(LightLoop);
246 show_set_light(LightPunch);
249 #define show_set_int(value) \
250 static ssize_t show_##value(struct device *dev, struct device_attribute *attr, char *buf) \
252 struct usb_interface *intf = to_usb_interface(dev); \
253 struct usb_tranzport *t = usb_get_intfdata(intf); \
255 return sprintf(buf, "%d\n", t->value); \
257 static ssize_t set_##value(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) \
259 struct usb_interface *intf = to_usb_interface(dev); \
260 struct usb_tranzport *t = usb_get_intfdata(intf); \
261 int temp = simple_strtoul(buf, NULL, 10); \
266 static DEVICE_ATTR(value, S_IWUGO | S_IRUGO, show_##value, set_##value);
268 show_set_int(enable);
269 show_set_int(offline);
270 show_set_int(compress_wheel);
271 show_set_int(dump_state);
275 #define show_set_cmd(value) \
276 static ssize_t show_##value(struct device *dev, struct device_attribute *attr, char *buf) \
278 struct usb_interface *intf = to_usb_interface(dev); \
279 struct usb_tranzport *t = usb_get_intfdata(intf); \
281 return sprintf(buf, "%d\n", t->value); \
283 static ssize_t set_##value(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) \
285 struct usb_interface *intf = to_usb_interface(dev); \
286 struct usb_tranzport *t = usb_get_intfdata(intf); \
287 int temp = simple_strtoul(buf, NULL, 10); \
292 static DEVICE_ATTR(value, S_IWUGO | S_IRUGO, show_##value, set_##value);
298 * usb_tranzport_delete
300 static void usb_tranzport_delete(struct usb_tranzport *dev)
302 usb_tranzport_abort_transfers(dev);
303 /* This is just too twisted to be correct */
304 if(dev->intf != NULL) {
305 device_remove_file(&dev->intf->dev, &dev_attr_LightRecord);
306 device_remove_file(&dev->intf->dev, &dev_attr_LightTrackrec);
307 device_remove_file(&dev->intf->dev, &dev_attr_LightTrackmute);
308 device_remove_file(&dev->intf->dev, &dev_attr_LightTracksolo);
309 device_remove_file(&dev->intf->dev, &dev_attr_LightTrackmute);
310 device_remove_file(&dev->intf->dev, &dev_attr_LightAnysolo);
311 device_remove_file(&dev->intf->dev, &dev_attr_LightLoop);
312 device_remove_file(&dev->intf->dev, &dev_attr_LightPunch);
313 device_remove_file(&dev->intf->dev, &dev_attr_wheel);
314 device_remove_file(&dev->intf->dev, &dev_attr_enable);
315 device_remove_file(&dev->intf->dev, &dev_attr_event);
316 device_remove_file(&dev->intf->dev, &dev_attr_offline);
317 device_remove_file(&dev->intf->dev, &dev_attr_compress_wheel);
319 device_remove_file(&dev->intf->dev, &dev_attr_dump_state);
322 /* free data structures */
323 usb_free_urb(dev->interrupt_in_urb);
324 usb_free_urb(dev->interrupt_out_urb);
325 kfree(dev->ring_buffer);
326 kfree(dev->interrupt_in_buffer);
327 kfree(dev->interrupt_out_buffer);
332 * usb_tranzport_interrupt_in_callback
335 static void usb_tranzport_interrupt_in_callback(struct urb *urb)
337 struct usb_tranzport *dev = urb->context;
338 unsigned int next_ring_head;
342 if (urb->status == -ENOENT ||
343 urb->status == -ECONNRESET ||
344 urb->status == -ESHUTDOWN) {
347 dbg_info(&dev->intf->dev, "%s: nonzero status received: %d\n",
348 __FUNCTION__, urb->status);
349 goto resubmit; /* maybe we can recover */
353 if (urb->actual_length != 8) {
354 dev_warn(&dev->intf->dev,
355 "Urb length was %d bytes!! Do something intelligent \n", urb->actual_length);
357 dbg_info(&dev->intf->dev, "%s: received: %02x%02x%02x%02x%02x%02x%02x%02x\n",
358 __FUNCTION__, dev->interrupt_in_buffer[0],dev->interrupt_in_buffer[1],dev->interrupt_in_buffer[2],dev->interrupt_in_buffer[3],dev->interrupt_in_buffer[4],dev->interrupt_in_buffer[5],dev->interrupt_in_buffer[6],dev->interrupt_in_buffer[7]);
359 #if SUPPRESS_EXTRA_OFFLINE_EVENTS
360 if(dev->offline == 2 && dev->interrupt_in_buffer[1] == 0xff) { goto resubmit; }
361 if(dev->offline == 1 && dev->interrupt_in_buffer[1] == 0xff) { dev->offline = 2; goto resubmit; }
363 /* Always pass one offline event up the stack */
364 if(dev->offline > 0 && dev->interrupt_in_buffer[1] != 0xff) { dev->offline = 0; }
365 if(dev->offline == 0 && dev->interrupt_in_buffer[1] == 0xff) { dev->offline = 1; }
368 dbg_info(&dev->intf->dev, "%s: head, tail are %x, %x\n", __FUNCTION__,dev->ring_head,dev->ring_tail);
370 next_ring_head = (dev->ring_head+1) % ring_buffer_size;
372 if (next_ring_head != dev->ring_tail) {
373 memcpy(&((*dev->ring_buffer)[dev->ring_head]), dev->interrupt_in_buffer, urb->actual_length);
374 dev->ring_head = next_ring_head;
376 memset(dev->interrupt_in_buffer, 0, urb->actual_length);
378 dev_warn(&dev->intf->dev,
379 "Ring buffer overflow, %d bytes dropped\n",
381 memset(dev->interrupt_in_buffer, 0, urb->actual_length);
386 /* resubmit if we're still running */
387 if (dev->interrupt_in_running && dev->intf) {
388 retval = usb_submit_urb(dev->interrupt_in_urb, GFP_ATOMIC);
390 dev_err(&dev->intf->dev,
391 "usb_submit_urb failed (%d)\n", retval);
395 dev->interrupt_in_done = 1;
396 wake_up_interruptible(&dev->read_wait);
400 * usb_tranzport_interrupt_out_callback
402 static void usb_tranzport_interrupt_out_callback(struct urb *urb)
404 struct usb_tranzport *dev = urb->context;
406 /* sync/async unlink faults aren't errors */
407 if (urb->status && !(urb->status == -ENOENT ||
408 urb->status == -ECONNRESET ||
409 urb->status == -ESHUTDOWN))
410 dbg_info(&dev->intf->dev,
411 "%s - nonzero write interrupt status received: %d\n",
412 __FUNCTION__, urb->status);
414 dev->interrupt_out_busy = 0;
415 wake_up_interruptible(&dev->write_wait);
421 static int usb_tranzport_open(struct inode *inode, struct file *file)
423 struct usb_tranzport *dev;
426 struct usb_interface *interface;
428 nonseekable_open(inode, file);
429 subminor = iminor(inode);
431 mutex_lock(&disconnect_mutex);
433 interface = usb_find_interface(&usb_tranzport_driver, subminor);
436 err("%s - error, can't find device for minor %d\n",
437 __FUNCTION__, subminor);
439 goto unlock_disconnect_exit;
442 dev = usb_get_intfdata(interface);
446 goto unlock_disconnect_exit;
449 /* lock this device */
450 if (down_interruptible(&dev->sem)) {
451 retval = -ERESTARTSYS;
452 goto unlock_disconnect_exit;
455 /* allow opening only once */
456 if (dev->open_count) {
462 /* initialize in direction */
465 usb_fill_int_urb(dev->interrupt_in_urb,
466 interface_to_usbdev(interface),
467 usb_rcvintpipe(interface_to_usbdev(interface),
468 dev->interrupt_in_endpoint->bEndpointAddress),
469 dev->interrupt_in_buffer,
470 dev->interrupt_in_endpoint_size,
471 usb_tranzport_interrupt_in_callback,
473 dev->interrupt_in_interval);
475 dev->interrupt_in_running = 1;
476 dev->interrupt_in_done = 0;
479 dev->compress_wheel = 1;
481 retval = usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL);
483 dev_err(&interface->dev, "Couldn't submit interrupt_in_urb %d\n", retval);
484 dev->interrupt_in_running = 0;
489 /* save device in the file's private structure */
490 file->private_data = dev;
496 unlock_disconnect_exit:
497 mutex_unlock(&disconnect_mutex);
503 * usb_tranzport_release
505 static int usb_tranzport_release(struct inode *inode, struct file *file)
507 struct usb_tranzport *dev;
510 dev = file->private_data;
517 if (down_interruptible(&dev->sem)) {
518 retval = -ERESTARTSYS;
522 if (dev->open_count != 1) {
527 if (dev->intf == NULL) {
528 /* the device was unplugged before the file was released */
530 /* unlock here as usb_tranzport_delete frees dev */
531 usb_tranzport_delete(dev);
536 /* wait until write transfer is finished */
537 if (dev->interrupt_out_busy)
538 wait_event_interruptible_timeout(dev->write_wait, !dev->interrupt_out_busy, 2 * HZ);
539 usb_tranzport_abort_transfers(dev);
552 static unsigned int usb_tranzport_poll(struct file *file, poll_table *wait)
554 struct usb_tranzport *dev;
555 unsigned int mask = 0;
557 dev = file->private_data;
559 poll_wait(file, &dev->read_wait, wait);
560 poll_wait(file, &dev->write_wait, wait);
562 if (dev->ring_head != dev->ring_tail)
563 mask |= POLLIN | POLLRDNORM;
564 if (!dev->interrupt_out_busy)
565 mask |= POLLOUT | POLLWRNORM;
573 static ssize_t usb_tranzport_read(struct file *file, char __user *buffer, size_t count,
576 struct usb_tranzport *dev;
577 size_t bytes_to_read;
584 #if COMPRESS_WHEEL_EVENTS
585 signed char oldwheel;
586 signed char newwheel;
591 /* do I have such a thing as a null event? */
593 dev = file->private_data;
595 /* verify that we actually have some data to read */
599 /* lock this object */
600 if (down_interruptible(&dev->sem)) {
601 retval = -ERESTARTSYS;
605 /* verify that the device wasn't unplugged */
606 if (dev->intf == NULL) {
608 err("No device or device unplugged %d\n", retval);
612 while (dev->ring_head == dev->ring_tail) {
614 if (file->f_flags & O_NONBLOCK) {
618 // atomic_cmp_exchange(&dev->interrupt_in_done,0,0);
619 dev->interrupt_in_done = 0 ; /* tiny race - FIXME: make atomic? */
620 retval = wait_event_interruptible(dev->read_wait, dev->interrupt_in_done);
626 dbg_info(&dev->intf->dev, "%s: copying to userspace: %02x%02x%02x%02x%02x%02x%02x%02x\n",
627 __FUNCTION__, (*dev->ring_buffer)[dev->ring_tail].cmd[0],(*dev->ring_buffer)[dev->ring_tail].cmd[1],(*dev->ring_buffer)[dev->ring_tail].cmd[2],(*dev->ring_buffer)[dev->ring_tail].cmd[3],(*dev->ring_buffer)[dev->ring_tail].cmd[4],(*dev->ring_buffer)[dev->ring_tail].cmd[5],(*dev->ring_buffer)[dev->ring_tail].cmd[6],(*dev->ring_buffer)[dev->ring_tail].cmd[7]);
631 while((c < count) && (dev->ring_tail != dev->ring_head)) {
633 /* This started off in the lower level service routine, and I moved it here. Then my brain died. Not done yet. */
634 #if COMPRESS_WHEEL_EVENTS
635 next_tail = (dev->ring_tail+1) % ring_buffer_size;
636 if(dev->compress_wheel) cancompress = 1;
637 while(dev->ring_head != next_tail && cancompress == 1 ) {
638 newwheel = (*dev->ring_buffer)[next_tail].cmd[6];
639 oldwheel = (*dev->ring_buffer)[dev->ring_tail].cmd[6];
640 // if both are wheel events, and no buttons have changes (FIXME, do I have to check?),
641 // and we are the same sign, we can compress +- 7F
642 // FIXME: saner check for overflow! - max of +- 7F
643 // FIXME the math is wrong for going in reverse, actually, as the midi spec doesn't allow signed chars
645 dbg_info(&dev->intf->dev, "%s: trying to compress: %02x%02x%02x%02x%02x %02x %02x %02x\n",
646 __FUNCTION__, (*dev->ring_buffer)[dev->ring_tail].cmd[0],(*dev->ring_buffer)[dev->ring_tail].cmd[1],(*dev->ring_buffer)[dev->ring_tail].cmd[2],(*dev->ring_buffer)[dev->ring_tail].cmd[3],(*dev->ring_buffer)[dev->ring_tail].cmd[4],(*dev->ring_buffer)[dev->ring_tail].cmd[5],(*dev->ring_buffer)[dev->ring_tail].cmd[6],(*dev->ring_buffer)[dev->ring_tail].cmd[7]);
649 if(((*dev->ring_buffer)[dev->ring_tail].cmd[6] != 0 &&
650 (*dev->ring_buffer)[next_tail].cmd[6] != 0 ) &&
651 ((newwheel > 0 && oldwheel > 0) ||
652 (newwheel < 0 && oldwheel < 0)) &&
653 ((*dev->ring_buffer)[dev->ring_tail].cmd[2] == (*dev->ring_buffer)[next_tail].cmd[2]) &&
654 ((*dev->ring_buffer)[dev->ring_tail].cmd[3] == (*dev->ring_buffer)[next_tail].cmd[3]) &&
655 ((*dev->ring_buffer)[dev->ring_tail].cmd[4] == (*dev->ring_buffer)[next_tail].cmd[4]) &&
656 ((*dev->ring_buffer)[dev->ring_tail].cmd[5] == (*dev->ring_buffer)[next_tail].cmd[5]))
658 dbg_info(&dev->intf->dev, "%s: should compress: %02x%02x%02x%02x%02x%02x%02x%02x\n",
659 __FUNCTION__, (*dev->ring_buffer)[dev->ring_tail].cmd[0],(*dev->ring_buffer)[dev->ring_tail].cmd[1],(*dev->ring_buffer)[dev->ring_tail].cmd[2],(*dev->ring_buffer)[dev->ring_tail].cmd[3],(*dev->ring_buffer)[dev->ring_tail].cmd[4],(*dev->ring_buffer)[dev->ring_tail].cmd[5],(*dev->ring_buffer)[dev->ring_tail].cmd[6],(*dev->ring_buffer)[dev->ring_tail].cmd[7]);
661 newwheel += oldwheel;
662 if(oldwheel > 0 && !(newwheel > 0)) {
666 if(oldwheel < 0 && !(newwheel < 0)) {
671 (*dev->ring_buffer)[next_tail].cmd[6] = newwheel;
672 dev->ring_tail = next_tail;
673 next_tail = (dev->ring_tail+1) % ring_buffer_size;
678 #endif /* COMPRESS_WHEEL_EVENTS */
680 if (copy_to_user(&buffer[c], &(*dev->ring_buffer)[dev->ring_tail], 8)) {
685 dev->ring_tail = (dev->ring_tail+1) % ring_buffer_size;
687 dbg_info(&dev->intf->dev, "%s: head, tail are %x, %x\n", __FUNCTION__,dev->ring_head,dev->ring_tail);
692 if (copy_to_user(buffer, &(*dev->ring_buffer)[dev->ring_tail], 8)) {
697 dev->ring_tail = (dev->ring_tail+1) % ring_buffer_size;
698 dbg_info(&dev->intf->dev, "%s: head, tail are %x, %x\n", __FUNCTION__,dev->ring_head,dev->ring_tail);
701 #endif /* BUFFERED_READS */
704 /* unlock the device */
712 * usb_tranzport_write
714 static ssize_t usb_tranzport_write(struct file *file, const char __user *buffer,
715 size_t count, loff_t *ppos)
717 struct usb_tranzport *dev;
718 size_t bytes_to_write;
721 dev = file->private_data;
723 /* verify that we actually have some data to write */
727 /* lock this object */
728 if (down_interruptible(&dev->sem)) {
729 retval = -ERESTARTSYS;
733 /* verify that the device wasn't unplugged */
734 if (dev->intf == NULL) {
736 err("No device or device unplugged %d\n", retval);
740 /* wait until previous transfer is finished */
741 if (dev->interrupt_out_busy) {
742 if (file->f_flags & O_NONBLOCK) {
746 retval = wait_event_interruptible(dev->write_wait, !dev->interrupt_out_busy);
752 /* write the data into interrupt_out_buffer from userspace */
753 bytes_to_write = min(count, write_buffer_size*dev->interrupt_out_endpoint_size);
754 if (bytes_to_write < count)
755 dev_warn(&dev->intf->dev, "Write buffer overflow, %zd bytes dropped\n",count-bytes_to_write);
757 dbg_info(&dev->intf->dev, "%s: count = %zd, bytes_to_write = %zd\n", __FUNCTION__, count, bytes_to_write);
759 if (copy_from_user(dev->interrupt_out_buffer, buffer, bytes_to_write)) {
764 if (dev->interrupt_out_endpoint == NULL) {
765 err("Endpoint should not be be null! \n");
769 /* send off the urb */
770 usb_fill_int_urb(dev->interrupt_out_urb,
771 interface_to_usbdev(dev->intf),
772 usb_sndintpipe(interface_to_usbdev(dev->intf),
773 dev->interrupt_out_endpoint->bEndpointAddress),
774 dev->interrupt_out_buffer,
776 usb_tranzport_interrupt_out_callback,
778 dev->interrupt_out_interval);
780 dev->interrupt_out_busy = 1;
783 retval = usb_submit_urb(dev->interrupt_out_urb, GFP_KERNEL);
785 dev->interrupt_out_busy = 0;
786 err("Couldn't submit interrupt_out_urb %d\n", retval);
789 retval = bytes_to_write;
792 /* unlock the device */
799 /* file operations needed when we register this driver */
800 static const struct file_operations usb_tranzport_fops = {
801 .owner = THIS_MODULE,
802 .read = usb_tranzport_read,
803 .write = usb_tranzport_write,
804 .open = usb_tranzport_open,
805 .release = usb_tranzport_release,
806 .poll = usb_tranzport_poll,
810 * usb class driver info in order to get a minor number from the usb core,
811 * and to have the device registered with the driver core
813 static struct usb_class_driver usb_tranzport_class = {
814 .name = "tranzport%d",
815 .fops = &usb_tranzport_fops,
816 .minor_base = USB_TRANZPORT_MINOR_BASE,
821 * usb_tranzport_probe
823 * Called by the usb core when a new device is connected that it thinks
824 * this driver might be interested in.
826 static int usb_tranzport_probe(struct usb_interface *intf, const struct usb_device_id *id)
828 struct usb_device *udev = interface_to_usbdev(intf);
829 struct usb_tranzport *dev = NULL;
830 struct usb_host_interface *iface_desc;
831 struct usb_endpoint_descriptor *endpoint;
834 int retval = -ENOMEM;
836 /* allocate memory for our device state and intialize it */
838 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
840 dev_err(&intf->dev, "Out of memory\n");
843 init_MUTEX(&dev->sem);
845 init_waitqueue_head(&dev->read_wait);
846 init_waitqueue_head(&dev->write_wait);
848 iface_desc = intf->cur_altsetting;
850 /* set up the endpoint information */
851 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
852 endpoint = &iface_desc->endpoint[i].desc;
854 if (usb_endpoint_is_int_in(endpoint))
855 dev->interrupt_in_endpoint = endpoint;
857 if (usb_endpoint_is_int_out(endpoint))
858 dev->interrupt_out_endpoint = endpoint;
860 if (dev->interrupt_in_endpoint == NULL) {
861 dev_err(&intf->dev, "Interrupt in endpoint not found\n");
864 if (dev->interrupt_out_endpoint == NULL)
865 dev_warn(&intf->dev, "Interrupt out endpoint not found (using control endpoint instead)\n");
868 dev->interrupt_in_endpoint_size = le16_to_cpu(dev->interrupt_in_endpoint->wMaxPacketSize);
870 if (dev->interrupt_in_endpoint_size != 8)
871 dev_warn(&intf->dev, "Interrupt in endpoint size is not 8!\n");
873 if(ring_buffer_size == 0) { ring_buffer_size = RING_BUFFER_SIZE; }
874 true_size = min(ring_buffer_size,RING_BUFFER_SIZE);
875 /* FIXME - there are more usb_alloc routines for dma correctness. Needed? */
877 dev->ring_buffer = kmalloc((true_size*sizeof(struct tranzport_cmd))+8, GFP_KERNEL);
879 if (!dev->ring_buffer) {
880 dev_err(&intf->dev, "Couldn't allocate ring_buffer of size %d\n",true_size);
883 dev->interrupt_in_buffer = kmalloc(dev->interrupt_in_endpoint_size, GFP_KERNEL);
884 if (!dev->interrupt_in_buffer) {
885 dev_err(&intf->dev, "Couldn't allocate interrupt_in_buffer\n");
888 dev->interrupt_in_urb = usb_alloc_urb(0, GFP_KERNEL);
889 if (!dev->interrupt_in_urb) {
890 dev_err(&intf->dev, "Couldn't allocate interrupt_in_urb\n");
893 dev->interrupt_out_endpoint_size = dev->interrupt_out_endpoint ? le16_to_cpu(dev->interrupt_out_endpoint->wMaxPacketSize) :
894 udev->descriptor.bMaxPacketSize0;
896 if (dev->interrupt_out_endpoint_size !=8)
897 dev_warn(&intf->dev, "Interrupt out endpoint size is not 8!)\n");
899 dev->interrupt_out_buffer = kmalloc(write_buffer_size*dev->interrupt_out_endpoint_size, GFP_KERNEL);
900 if (!dev->interrupt_out_buffer) {
901 dev_err(&intf->dev, "Couldn't allocate interrupt_out_buffer\n");
904 dev->interrupt_out_urb = usb_alloc_urb(0, GFP_KERNEL);
905 if (!dev->interrupt_out_urb) {
906 dev_err(&intf->dev, "Couldn't allocate interrupt_out_urb\n");
909 dev->interrupt_in_interval = min_interrupt_in_interval > dev->interrupt_in_endpoint->bInterval ? min_interrupt_in_interval : dev->interrupt_in_endpoint->bInterval;
910 if (dev->interrupt_out_endpoint)
911 dev->interrupt_out_interval = min_interrupt_out_interval > dev->interrupt_out_endpoint->bInterval ? min_interrupt_out_interval : dev->interrupt_out_endpoint->bInterval;
913 /* we can register the device now, as it is ready */
914 usb_set_intfdata(intf, dev);
916 retval = usb_register_dev(intf, &usb_tranzport_class);
918 /* something prevented us from registering this driver */
919 dev_err(&intf->dev, "Not able to get a minor for this device.\n");
920 usb_set_intfdata(intf, NULL);
924 if((retval = device_create_file(&intf->dev, &dev_attr_LightRecord))) goto error;
925 if((retval = device_create_file(&intf->dev, &dev_attr_LightTrackrec))) goto error;
926 if((retval = device_create_file(&intf->dev, &dev_attr_LightTrackmute))) goto error;
927 if((retval = device_create_file(&intf->dev, &dev_attr_LightTracksolo))) goto error;
928 if((retval = device_create_file(&intf->dev, &dev_attr_LightAnysolo))) goto error;
929 if((retval = device_create_file(&intf->dev, &dev_attr_LightLoop))) goto error;
930 if((retval = device_create_file(&intf->dev, &dev_attr_LightPunch))) goto error;
931 if((retval = device_create_file(&intf->dev, &dev_attr_wheel))) goto error;
932 if((retval = device_create_file(&intf->dev, &dev_attr_event))) goto error;
933 if((retval = device_create_file(&intf->dev, &dev_attr_dump_state))) goto error;
934 if((retval = device_create_file(&intf->dev, &dev_attr_compress_wheel))) goto error;
935 if((retval = device_create_file(&intf->dev, &dev_attr_enable))) goto error;
936 if((retval = device_create_file(&intf->dev, &dev_attr_offline))) goto error;
938 /* let the user know what node this device is now attached to */
939 dev_info(&intf->dev, "Tranzport Device #%d now attached to major %d minor %d\n",
940 (intf->minor - USB_TRANZPORT_MINOR_BASE), USB_MAJOR, intf->minor);
946 usb_tranzport_delete(dev);
952 * usb_tranzport_disconnect
954 * Called by the usb core when the device is removed from the system.
956 static void usb_tranzport_disconnect(struct usb_interface *intf)
958 struct usb_tranzport *dev;
960 mutex_lock(&disconnect_mutex);
961 dev = usb_get_intfdata(intf);
962 usb_set_intfdata(intf, NULL);
965 /* give back our minor */
966 usb_deregister_dev(intf, &usb_tranzport_class);
968 /* if the device is not opened, then we clean up right now */
969 if (!dev->open_count) {
971 usb_tranzport_delete(dev);
977 mutex_unlock(&disconnect_mutex);
979 dev_info(&intf->dev, "Tranzport Surface #%d now disconnected\n",
980 (minor - USB_TRANZPORT_MINOR_BASE));
983 /* usb specific object needed to register this driver with the usb subsystem */
984 static struct usb_driver usb_tranzport_driver = {
986 .probe = usb_tranzport_probe,
987 .disconnect = usb_tranzport_disconnect,
988 .id_table = usb_tranzport_table,
994 static int __init usb_tranzport_init(void)
998 /* register this driver with the USB subsystem */
999 retval = usb_register(&usb_tranzport_driver);
1001 err("usb_register failed for the "__FILE__" driver. Error number %d\n", retval);
1007 * usb_tranzport_exit
1009 static void __exit usb_tranzport_exit(void)
1011 /* deregister this driver with the USB subsystem */
1012 usb_deregister(&usb_tranzport_driver);
1015 module_init(usb_tranzport_init);
1016 module_exit(usb_tranzport_exit);