Staging: add frontier tranzport and alphatrack drivers
[pandora-kernel.git] / drivers / staging / frontier / tranzport.c
1 /*
2  * Frontier Designs Tranzport 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 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 #include <linux/version.h>
42
43 #include <asm/uaccess.h>
44 #include <linux/input.h>
45 #include <linux/usb.h>
46 #include <linux/poll.h>
47
48 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19)
49 #include frontier_compat.h
50 #endif
51
52 /* Define these values to match your devices */
53 #define VENDOR_ID   0x165b
54 #define PRODUCT_ID      0x8101
55
56 #ifdef CONFIG_USB_DYNAMIC_MINORS
57 #define USB_TRANZPORT_MINOR_BASE        0
58 #else
59 // FIXME 176 - is the ldusb driver's minor - apply for a minor soon
60 #define USB_TRANZPORT_MINOR_BASE        177
61 #endif
62
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 */
67 };
68
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");
75
76 /* These two aren't done yet */
77
78 #define SUPPRESS_EXTRA_ONLINE_EVENTS 0
79 #define BUFFERED_WRITES 0
80
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
87
88
89 static int debug = 0;
90
91 /* Use our own dbg macro */
92 #define dbg_info(dev, format, arg...) do { if (debug) dev_info(dev , format , ## arg); } while (0)
93
94 /* Module parameters */
95
96 module_param(debug, int, S_IRUGO | S_IWUSR);
97 MODULE_PARM_DESC(debug, "Debug enabled or not");
98
99 /* All interrupt in transfers are collected in a ring buffer to
100  * avoid racing conditions and get better performance of the driver.
101  */
102
103 static int ring_buffer_size = RING_BUFFER_SIZE;
104
105 module_param(ring_buffer_size, int,  S_IRUGO);
106 MODULE_PARM_DESC(ring_buffer_size, "Read ring buffer size in reports");
107
108 /* The write_buffer can one day contain more than one interrupt out transfer.
109  */
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");
113
114 /*
115  * Increase the interval for debugging purposes.
116  * or set to 1 to use the standard interval from the endpoint descriptors.
117  */
118
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");
122
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");
126
127 struct tranzport_cmd {
128     unsigned char cmd[8];
129 };
130
131 enum LightID {
132         LightRecord = 0,
133         LightTrackrec,
134         LightTrackmute,
135         LightTracksolo,
136         LightAnysolo,
137         LightLoop,
138         LightPunch
139         };
140
141 static const char *Lightname[8] = { "LightRecord",
142                                                                                                                                                 "LightTracrec",
143                                                                                                                                                 "LightTrackmute",
144                                                                                                                                                 "LightTrackSolo",
145                                                                                                                                                 "LightAnySolo",
146                                                                                                                                                 "LightLoop",
147                                                                                                                                                 "LightPunch",
148                                                                                                                                                 NULL };
149
150
151 /* Structure to hold all of our device specific stuff */
152
153 struct usb_tranzport {
154         struct semaphore        sem;            /* locks this structure */
155         struct usb_interface*   intf;           /* save off the usb interface pointer */
156
157         int                     open_count;     /* number of times this port has been opened */
158
159         struct tranzport_cmd    (*ring_buffer)[RING_BUFFER_SIZE]; /* just make c happy */
160         unsigned int            ring_head;
161         unsigned int            ring_tail;
162
163         wait_queue_head_t       read_wait;
164         wait_queue_head_t       write_wait;
165
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;
173
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;
180
181         /* Sysfs and translation support */
182
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
193
194 };
195
196 /* prevent races between open() and disconnect() */
197 static DEFINE_MUTEX(disconnect_mutex);
198
199 static struct usb_driver usb_tranzport_driver;
200
201 /**
202  *      usb_tranzport_abort_transfers
203  *      aborts transfers and frees associated data structures
204  */
205 static void usb_tranzport_abort_transfers(struct usb_tranzport *dev)
206 {
207         /* shutdown transfer */
208         if (dev->interrupt_in_running) {
209                 dev->interrupt_in_running = 0;
210                 if (dev->intf)
211                         usb_kill_urb(dev->interrupt_in_urb);
212         }
213         if (dev->interrupt_out_busy)
214                 if (dev->intf)
215                         usb_kill_urb(dev->interrupt_out_urb);
216 }
217
218 // FIXME ~light not good enough or correct - need atomic set_bit
219
220 #define show_set_light(value)   \
221 static ssize_t show_##value(struct device *dev, struct device_attribute *attr, char *buf)               \
222 {                                                                       \
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 );                             \
228 }                                                                       \
229 static ssize_t set_##value(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)    \
230 {                                                                       \
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) ;                                \
236         return count;                                                   \
237 }                                                                       \
238 static DEVICE_ATTR(value, S_IWUGO | S_IRUGO, show_##value, set_##value);
239
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);
247
248
249 #define show_set_int(value)     \
250 static ssize_t show_##value(struct device *dev, struct device_attribute *attr, char *buf)               \
251 {                                                                       \
252         struct usb_interface *intf = to_usb_interface(dev);             \
253         struct usb_tranzport *t = usb_get_intfdata(intf);                       \
254                                                                         \
255         return sprintf(buf, "%d\n", t->value);                  \
256 }                                                                       \
257 static ssize_t set_##value(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)    \
258 {                                                                       \
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);                       \
262                                                                         \
263         t->value = temp;                                                \
264         return count;                                                   \
265 }                                                                       \
266 static DEVICE_ATTR(value, S_IWUGO | S_IRUGO, show_##value, set_##value);
267
268 show_set_int(enable);
269 show_set_int(offline);
270 show_set_int(compress_wheel);
271 show_set_int(dump_state);
272 show_set_int(wheel);
273 show_set_int(event);
274
275 #define show_set_cmd(value)     \
276 static ssize_t show_##value(struct device *dev, struct device_attribute *attr, char *buf)               \
277 {                                                                       \
278         struct usb_interface *intf = to_usb_interface(dev);             \
279         struct usb_tranzport *t = usb_get_intfdata(intf);                       \
280                                                                         \
281         return sprintf(buf, "%d\n", t->value);                  \
282 }                                                                       \
283 static ssize_t set_##value(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)    \
284 {                                                                       \
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);                       \
288                                                                         \
289         t->value = temp;                                                \
290         return count;                                                   \
291 }                                                                       \
292 static DEVICE_ATTR(value, S_IWUGO | S_IRUGO, show_##value, set_##value);
293
294
295
296
297 /**
298  *      usb_tranzport_delete
299  */
300 static void usb_tranzport_delete(struct usb_tranzport *dev)
301 {
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);
318
319         device_remove_file(&dev->intf->dev, &dev_attr_dump_state);
320         }
321
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);
328         kfree(dev);
329 }
330
331 /**
332  *      usb_tranzport_interrupt_in_callback
333  */
334
335 static void usb_tranzport_interrupt_in_callback(struct urb *urb)
336 {
337         struct usb_tranzport *dev = urb->context;
338         unsigned int next_ring_head;
339         int retval = -1;
340
341         if (urb->status) {
342                 if (urb->status == -ENOENT ||
343                     urb->status == -ECONNRESET ||
344                     urb->status == -ESHUTDOWN) {
345                         goto exit;
346                 } else {
347                         dbg_info(&dev->intf->dev, "%s: nonzero status received: %d\n",
348                                  __FUNCTION__, urb->status);
349                         goto resubmit; /* maybe we can recover */
350                 }
351         }
352
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);
356         } else {
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; }
362
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; }
366
367 #endif
368                 dbg_info(&dev->intf->dev, "%s: head, tail are %x, %x\n", __FUNCTION__,dev->ring_head,dev->ring_tail);
369
370                 next_ring_head = (dev->ring_head+1) % ring_buffer_size;
371
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;
375                         retval = 0;
376                         memset(dev->interrupt_in_buffer, 0, urb->actual_length);
377                 } else {
378                         dev_warn(&dev->intf->dev,
379                                  "Ring buffer overflow, %d bytes dropped\n",
380                                  urb->actual_length);
381                         memset(dev->interrupt_in_buffer, 0, urb->actual_length);
382                 }
383         }
384
385 resubmit:
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);
389                 if (retval)
390                         dev_err(&dev->intf->dev,
391                                 "usb_submit_urb failed (%d)\n", retval);
392         }
393
394 exit:
395         dev->interrupt_in_done = 1;
396         wake_up_interruptible(&dev->read_wait);
397 }
398
399 /**
400  *      usb_tranzport_interrupt_out_callback
401  */
402 static void usb_tranzport_interrupt_out_callback(struct urb *urb)
403 {
404         struct usb_tranzport *dev = urb->context;
405
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);
413
414         dev->interrupt_out_busy = 0;
415         wake_up_interruptible(&dev->write_wait);
416 }
417
418 /**
419  *      usb_tranzport_open
420  */
421 static int usb_tranzport_open(struct inode *inode, struct file *file)
422 {
423         struct usb_tranzport *dev;
424         int subminor;
425         int retval = 0;
426         struct usb_interface *interface;
427
428         nonseekable_open(inode, file);
429         subminor = iminor(inode);
430
431         mutex_lock(&disconnect_mutex);
432
433         interface = usb_find_interface(&usb_tranzport_driver, subminor);
434
435         if (!interface) {
436                 err("%s - error, can't find device for minor %d\n",
437                      __FUNCTION__, subminor);
438                 retval = -ENODEV;
439                 goto unlock_disconnect_exit;
440         }
441
442         dev = usb_get_intfdata(interface);
443
444         if (!dev) {
445                 retval = -ENODEV;
446                 goto unlock_disconnect_exit;
447         }
448
449         /* lock this device */
450         if (down_interruptible(&dev->sem)) {
451                 retval = -ERESTARTSYS;
452                 goto unlock_disconnect_exit;
453         }
454
455         /* allow opening only once */
456         if (dev->open_count) {
457                 retval = -EBUSY;
458                 goto unlock_exit;
459         }
460         dev->open_count = 1;
461
462         /* initialize in direction */
463         dev->ring_head = 0;
464         dev->ring_tail = 0;
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,
472                          dev,
473                          dev->interrupt_in_interval);
474
475         dev->interrupt_in_running = 1;
476         dev->interrupt_in_done = 0;
477         dev->enable = 1;
478         dev->offline = 0;
479         dev->compress_wheel = 1;
480
481         retval = usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL);
482         if (retval) {
483                 dev_err(&interface->dev, "Couldn't submit interrupt_in_urb %d\n", retval);
484                 dev->interrupt_in_running = 0;
485                 dev->open_count = 0;
486                 goto unlock_exit;
487         }
488
489         /* save device in the file's private structure */
490         file->private_data = dev;
491
492
493 unlock_exit:
494         up(&dev->sem);
495
496 unlock_disconnect_exit:
497         mutex_unlock(&disconnect_mutex);
498
499         return retval;
500 }
501
502 /**
503  *      usb_tranzport_release
504  */
505 static int usb_tranzport_release(struct inode *inode, struct file *file)
506 {
507         struct usb_tranzport *dev;
508         int retval = 0;
509
510         dev = file->private_data;
511
512         if (dev == NULL) {
513                 retval = -ENODEV;
514                 goto exit;
515         }
516
517         if (down_interruptible(&dev->sem)) {
518                 retval = -ERESTARTSYS;
519                 goto exit;
520         }
521
522         if (dev->open_count != 1) {
523                 retval = -ENODEV;
524                 goto unlock_exit;
525         }
526
527         if (dev->intf == NULL) {
528                 /* the device was unplugged before the file was released */
529                 up(&dev->sem);
530                 /* unlock here as usb_tranzport_delete frees dev */
531                 usb_tranzport_delete(dev);
532                 retval = -ENODEV;
533                 goto exit;
534         }
535
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);
540         dev->open_count = 0;
541
542 unlock_exit:
543         up(&dev->sem);
544
545 exit:
546         return retval;
547 }
548
549 /**
550  *      usb_tranzport_poll
551  */
552 static unsigned int usb_tranzport_poll(struct file *file, poll_table *wait)
553 {
554         struct usb_tranzport *dev;
555         unsigned int mask = 0;
556
557         dev = file->private_data;
558
559         poll_wait(file, &dev->read_wait, wait);
560         poll_wait(file, &dev->write_wait, wait);
561
562         if (dev->ring_head != dev->ring_tail)
563                 mask |= POLLIN | POLLRDNORM;
564         if (!dev->interrupt_out_busy)
565                 mask |= POLLOUT | POLLWRNORM;
566
567         return mask;
568 }
569
570 /**
571  *      usb_tranzport_read
572  */
573 static ssize_t usb_tranzport_read(struct file *file, char __user *buffer, size_t count,
574                            loff_t *ppos)
575 {
576         struct usb_tranzport *dev;
577         size_t bytes_to_read;
578         int retval = 0;
579
580 #if BUFFERED_READS
581         int c = 0;
582 #endif
583
584 #if COMPRESS_WHEEL_EVENTS
585         signed char oldwheel;
586         signed char newwheel;
587         int cancompress = 1;
588         int next_tail;
589 #endif
590
591 /* do I have such a thing as a null event? */
592
593         dev = file->private_data;
594
595         /* verify that we actually have some data to read */
596         if (count == 0)
597                 goto exit;
598
599         /* lock this object */
600         if (down_interruptible(&dev->sem)) {
601                 retval = -ERESTARTSYS;
602                 goto exit;
603         }
604
605         /* verify that the device wasn't unplugged */
606         if (dev->intf == NULL) {
607                 retval = -ENODEV;
608                 err("No device or device unplugged %d\n", retval);
609                 goto unlock_exit;
610         }
611
612         while (dev->ring_head == dev->ring_tail) {
613
614                 if (file->f_flags & O_NONBLOCK) {
615                         retval = -EAGAIN;
616                         goto unlock_exit;
617                 }
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);
621                 if (retval < 0) {
622                         goto unlock_exit;
623                 }
624         }
625
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]);
628
629 #if BUFFERED_READS
630            c = 0;
631            while((c < count) && (dev->ring_tail != dev->ring_head)) {
632
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
644
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]);
647
648
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]))
657  {
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]);
660
661                                 newwheel += oldwheel;
662                                 if(oldwheel > 0 && !(newwheel > 0)) {
663                                         newwheel = 0x7f;
664                                         cancompress = 0;
665                                 }
666                                 if(oldwheel < 0 && !(newwheel < 0)) {
667                                         newwheel = 0x80;
668                                         cancompress = 0;
669                                 }
670
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;
674                         } else {
675                                 cancompress = 0;
676                         }
677                 }
678 #endif /* COMPRESS_WHEEL_EVENTS */
679
680                 if (copy_to_user(&buffer[c], &(*dev->ring_buffer)[dev->ring_tail], 8)) {
681                         retval = -EFAULT;
682                         goto unlock_exit;
683                 }
684
685                 dev->ring_tail = (dev->ring_tail+1) % ring_buffer_size;
686                 c+=8;
687                 dbg_info(&dev->intf->dev, "%s: head, tail are %x, %x\n", __FUNCTION__,dev->ring_head,dev->ring_tail);
688            }
689            retval = c;
690
691 #else
692         if (copy_to_user(buffer, &(*dev->ring_buffer)[dev->ring_tail], 8)) {
693                 retval = -EFAULT;
694                 goto unlock_exit;
695         }
696
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);
699
700         retval = 8;
701 #endif /* BUFFERED_READS */
702
703 unlock_exit:
704         /* unlock the device */
705         up(&dev->sem);
706
707 exit:
708         return retval;
709 }
710
711 /**
712  *      usb_tranzport_write
713  */
714 static ssize_t usb_tranzport_write(struct file *file, const char __user *buffer,
715                             size_t count, loff_t *ppos)
716 {
717         struct usb_tranzport *dev;
718         size_t bytes_to_write;
719         int retval = 0;
720
721         dev = file->private_data;
722
723         /* verify that we actually have some data to write */
724         if (count == 0)
725                 goto exit;
726
727         /* lock this object */
728         if (down_interruptible(&dev->sem)) {
729                 retval = -ERESTARTSYS;
730                 goto exit;
731         }
732
733         /* verify that the device wasn't unplugged */
734         if (dev->intf == NULL) {
735                 retval = -ENODEV;
736                 err("No device or device unplugged %d\n", retval);
737                 goto unlock_exit;
738         }
739
740         /* wait until previous transfer is finished */
741         if (dev->interrupt_out_busy) {
742                 if (file->f_flags & O_NONBLOCK) {
743                         retval = -EAGAIN;
744                         goto unlock_exit;
745                 }
746                 retval = wait_event_interruptible(dev->write_wait, !dev->interrupt_out_busy);
747                 if (retval < 0) {
748                         goto unlock_exit;
749                 }
750         }
751
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);
756
757         dbg_info(&dev->intf->dev, "%s: count = %zd, bytes_to_write = %zd\n", __FUNCTION__, count, bytes_to_write);
758
759         if (copy_from_user(dev->interrupt_out_buffer, buffer, bytes_to_write)) {
760                 retval = -EFAULT;
761                 goto unlock_exit;
762         }
763
764         if (dev->interrupt_out_endpoint == NULL) {
765                 err("Endpoint should not be be null! \n");
766                 goto unlock_exit;
767         }
768
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,
775                          bytes_to_write,
776                          usb_tranzport_interrupt_out_callback,
777                          dev,
778                          dev->interrupt_out_interval);
779
780         dev->interrupt_out_busy = 1;
781         wmb();
782
783         retval = usb_submit_urb(dev->interrupt_out_urb, GFP_KERNEL);
784         if (retval) {
785                 dev->interrupt_out_busy = 0;
786                 err("Couldn't submit interrupt_out_urb %d\n", retval);
787                 goto unlock_exit;
788         }
789         retval = bytes_to_write;
790
791 unlock_exit:
792         /* unlock the device */
793         up(&dev->sem);
794
795 exit:
796         return retval;
797 }
798
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,
807 };
808
809 /*
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
812  */
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,
817 };
818
819
820 /**
821  *      usb_tranzport_probe
822  *
823  *      Called by the usb core when a new device is connected that it thinks
824  *      this driver might be interested in.
825  */
826 static int usb_tranzport_probe(struct usb_interface *intf, const struct usb_device_id *id)
827 {
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;
832         int i;
833         int true_size;
834         int retval = -ENOMEM;
835
836         /* allocate memory for our device state and intialize it */
837
838         dev = kzalloc(sizeof(*dev), GFP_KERNEL);
839         if (dev == NULL) {
840                 dev_err(&intf->dev, "Out of memory\n");
841                 goto exit;
842         }
843         init_MUTEX(&dev->sem);
844         dev->intf = intf;
845         init_waitqueue_head(&dev->read_wait);
846         init_waitqueue_head(&dev->write_wait);
847
848         iface_desc = intf->cur_altsetting;
849
850         /* set up the endpoint information */
851         for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
852                 endpoint = &iface_desc->endpoint[i].desc;
853
854                 if (usb_endpoint_is_int_in(endpoint))
855                         dev->interrupt_in_endpoint = endpoint;
856
857                 if (usb_endpoint_is_int_out(endpoint))
858                         dev->interrupt_out_endpoint = endpoint;
859         }
860         if (dev->interrupt_in_endpoint == NULL) {
861                 dev_err(&intf->dev, "Interrupt in endpoint not found\n");
862                 goto error;
863         }
864         if (dev->interrupt_out_endpoint == NULL)
865                 dev_warn(&intf->dev, "Interrupt out endpoint not found (using control endpoint instead)\n");
866
867
868         dev->interrupt_in_endpoint_size = le16_to_cpu(dev->interrupt_in_endpoint->wMaxPacketSize);
869
870         if (dev->interrupt_in_endpoint_size != 8)
871             dev_warn(&intf->dev, "Interrupt in endpoint size is not 8!\n");
872
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? */
876
877         dev->ring_buffer = kmalloc((true_size*sizeof(struct tranzport_cmd))+8, GFP_KERNEL);
878
879         if (!dev->ring_buffer) {
880                 dev_err(&intf->dev, "Couldn't allocate ring_buffer of size %d\n",true_size);
881                 goto error;
882         }
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");
886                 goto error;
887         }
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");
891                 goto error;
892         }
893         dev->interrupt_out_endpoint_size = dev->interrupt_out_endpoint ? le16_to_cpu(dev->interrupt_out_endpoint->wMaxPacketSize) :
894                                                                          udev->descriptor.bMaxPacketSize0;
895
896         if (dev->interrupt_out_endpoint_size !=8)
897                 dev_warn(&intf->dev, "Interrupt out endpoint size is not 8!)\n");
898
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");
902                 goto error;
903         }
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");
907                 goto error;
908         }
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;
912
913         /* we can register the device now, as it is ready */
914         usb_set_intfdata(intf, dev);
915
916         retval = usb_register_dev(intf, &usb_tranzport_class);
917         if (retval) {
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);
921                 goto error;
922         }
923
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;
937
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);
941
942 exit:
943         return retval;
944
945 error:
946         usb_tranzport_delete(dev);
947
948         return retval;
949 }
950
951 /**
952  *      usb_tranzport_disconnect
953  *
954  *      Called by the usb core when the device is removed from the system.
955  */
956 static void usb_tranzport_disconnect(struct usb_interface *intf)
957 {
958         struct usb_tranzport *dev;
959         int minor;
960         mutex_lock(&disconnect_mutex);
961         dev = usb_get_intfdata(intf);
962         usb_set_intfdata(intf, NULL);
963         down(&dev->sem);
964         minor = intf->minor;
965         /* give back our minor */
966         usb_deregister_dev(intf, &usb_tranzport_class);
967
968         /* if the device is not opened, then we clean up right now */
969         if (!dev->open_count) {
970                 up(&dev->sem);
971                 usb_tranzport_delete(dev);
972         } else {
973                 dev->intf = NULL;
974                 up(&dev->sem);
975         }
976
977         mutex_unlock(&disconnect_mutex);
978
979         dev_info(&intf->dev, "Tranzport Surface #%d now disconnected\n",
980                  (minor - USB_TRANZPORT_MINOR_BASE));
981 }
982
983 /* usb specific object needed to register this driver with the usb subsystem */
984 static struct usb_driver usb_tranzport_driver = {
985         .name =         "tranzport",
986         .probe =        usb_tranzport_probe,
987         .disconnect =   usb_tranzport_disconnect,
988         .id_table =     usb_tranzport_table,
989 };
990
991 /**
992  *      usb_tranzport_init
993  */
994 static int __init usb_tranzport_init(void)
995 {
996         int retval;
997
998         /* register this driver with the USB subsystem */
999         retval = usb_register(&usb_tranzport_driver);
1000         if (retval)
1001                 err("usb_register failed for the "__FILE__" driver. Error number %d\n", retval);
1002
1003         return retval;
1004 }
1005
1006 /**
1007  *      usb_tranzport_exit
1008  */
1009 static void __exit usb_tranzport_exit(void)
1010 {
1011         /* deregister this driver with the USB subsystem */
1012         usb_deregister(&usb_tranzport_driver);
1013 }
1014
1015 module_init(usb_tranzport_init);
1016 module_exit(usb_tranzport_exit);
1017