]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/staging/frontier/tranzport.c
staging: __FUNCTION__ is gcc-specific, use __func__
[net-next-2.6.git] / drivers / staging / frontier / tranzport.c
CommitLineData
8da3dc28
DT
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 */
64static struct usb_device_id usb_tranzport_table [] = {
65 { USB_DEVICE(VENDOR_ID, PRODUCT_ID) },
66 { } /* Terminating entry */
67};
68
69MODULE_DEVICE_TABLE(usb, usb_tranzport_table);
70MODULE_VERSION("0.33");
71MODULE_AUTHOR("Mike Taht <m@taht.net>");
72MODULE_DESCRIPTION("Tranzport USB Driver");
73MODULE_LICENSE("GPL");
74MODULE_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
89static 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
96module_param(debug, int, S_IRUGO | S_IWUSR);
97MODULE_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
103static int ring_buffer_size = RING_BUFFER_SIZE;
104
105module_param(ring_buffer_size, int, S_IRUGO);
106MODULE_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 */
110static int write_buffer_size = WRITE_BUFFER_SIZE;
111module_param(write_buffer_size, int, S_IRUGO);
112MODULE_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
119static int min_interrupt_in_interval = TRANZPORT_USB_TIMEOUT;
120module_param(min_interrupt_in_interval, int, 0);
121MODULE_PARM_DESC(min_interrupt_in_interval, "Minimum interrupt in interval in ms");
122
123static int min_interrupt_out_interval = TRANZPORT_USB_TIMEOUT;
124module_param(min_interrupt_out_interval, int, 0);
125MODULE_PARM_DESC(min_interrupt_out_interval, "Minimum interrupt out interval in ms");
126
127struct tranzport_cmd {
128 unsigned char cmd[8];
129};
130
131enum LightID {
132 LightRecord = 0,
133 LightTrackrec,
134 LightTrackmute,
135 LightTracksolo,
136 LightAnysolo,
137 LightLoop,
138 LightPunch
139 };
140
8da3dc28
DT
141/* Structure to hold all of our device specific stuff */
142
143struct usb_tranzport {
144 struct semaphore sem; /* locks this structure */
145 struct usb_interface* intf; /* save off the usb interface pointer */
146
147 int open_count; /* number of times this port has been opened */
148
149 struct tranzport_cmd (*ring_buffer)[RING_BUFFER_SIZE]; /* just make c happy */
150 unsigned int ring_head;
151 unsigned int ring_tail;
152
153 wait_queue_head_t read_wait;
154 wait_queue_head_t write_wait;
155
156 unsigned char* interrupt_in_buffer;
157 struct usb_endpoint_descriptor* interrupt_in_endpoint;
158 struct urb* interrupt_in_urb;
159 int interrupt_in_interval;
160 size_t interrupt_in_endpoint_size;
161 int interrupt_in_running;
162 int interrupt_in_done;
163
164 char* interrupt_out_buffer;
165 struct usb_endpoint_descriptor* interrupt_out_endpoint;
166 struct urb* interrupt_out_urb;
167 int interrupt_out_interval;
168 size_t interrupt_out_endpoint_size;
169 int interrupt_out_busy;
170
171 /* Sysfs and translation support */
172
173 int event; /* alternate interface to events */
174 int wheel; /* - for negative, 0 for none, + for positive */
175 unsigned char dump_state; /* 0 if disabled 1 if enabled */
176 unsigned char enable; /* 0 if disabled 1 if enabled */
177 unsigned char offline; /* if the device is out of range or asleep */
178 unsigned char compress_wheel; /* flag to compress wheel events */
179 unsigned char light; /* 7 bits used */
180 unsigned char last_cmd[8];
181 unsigned char last_input[8];
182 unsigned char screen[40]; // We'll also have cells
183
184};
185
186/* prevent races between open() and disconnect() */
187static DEFINE_MUTEX(disconnect_mutex);
188
189static struct usb_driver usb_tranzport_driver;
190
191/**
192 * usb_tranzport_abort_transfers
193 * aborts transfers and frees associated data structures
194 */
195static void usb_tranzport_abort_transfers(struct usb_tranzport *dev)
196{
197 /* shutdown transfer */
198 if (dev->interrupt_in_running) {
199 dev->interrupt_in_running = 0;
200 if (dev->intf)
201 usb_kill_urb(dev->interrupt_in_urb);
202 }
203 if (dev->interrupt_out_busy)
204 if (dev->intf)
205 usb_kill_urb(dev->interrupt_out_urb);
206}
207
208// FIXME ~light not good enough or correct - need atomic set_bit
209
210#define show_set_light(value) \
211static ssize_t show_##value(struct device *dev, struct device_attribute *attr, char *buf) \
212{ \
213 struct usb_interface *intf = to_usb_interface(dev); \
214 struct usb_tranzport *t = usb_get_intfdata(intf); \
215 enum LightID light = value; \
216 int temp = (1 && (t->light & (1 << light))); \
217 return sprintf(buf, "%d\n", temp ); \
218} \
219static ssize_t set_##value(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) \
220{ \
221 struct usb_interface *intf = to_usb_interface(dev); \
222 struct usb_tranzport *t = usb_get_intfdata(intf); \
223 int temp = simple_strtoul(buf, NULL, 10); \
224 enum LightID light = (temp << value) & (t->light << value); \
225 t->light = (t->light & ~light) ; \
226 return count; \
227} \
228static DEVICE_ATTR(value, S_IWUGO | S_IRUGO, show_##value, set_##value);
229
230show_set_light(LightRecord);
231show_set_light(LightTrackrec);
232show_set_light(LightTrackmute);
233show_set_light(LightTracksolo);
234show_set_light(LightAnysolo);
235show_set_light(LightLoop);
236show_set_light(LightPunch);
237
238
239#define show_set_int(value) \
240static ssize_t show_##value(struct device *dev, struct device_attribute *attr, char *buf) \
241{ \
242 struct usb_interface *intf = to_usb_interface(dev); \
243 struct usb_tranzport *t = usb_get_intfdata(intf); \
244 \
245 return sprintf(buf, "%d\n", t->value); \
246} \
247static ssize_t set_##value(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) \
248{ \
249 struct usb_interface *intf = to_usb_interface(dev); \
250 struct usb_tranzport *t = usb_get_intfdata(intf); \
251 int temp = simple_strtoul(buf, NULL, 10); \
252 \
253 t->value = temp; \
254 return count; \
255} \
256static DEVICE_ATTR(value, S_IWUGO | S_IRUGO, show_##value, set_##value);
257
258show_set_int(enable);
259show_set_int(offline);
260show_set_int(compress_wheel);
261show_set_int(dump_state);
262show_set_int(wheel);
263show_set_int(event);
264
265#define show_set_cmd(value) \
266static ssize_t show_##value(struct device *dev, struct device_attribute *attr, char *buf) \
267{ \
268 struct usb_interface *intf = to_usb_interface(dev); \
269 struct usb_tranzport *t = usb_get_intfdata(intf); \
270 \
271 return sprintf(buf, "%d\n", t->value); \
272} \
273static ssize_t set_##value(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) \
274{ \
275 struct usb_interface *intf = to_usb_interface(dev); \
276 struct usb_tranzport *t = usb_get_intfdata(intf); \
277 int temp = simple_strtoul(buf, NULL, 10); \
278 \
279 t->value = temp; \
280 return count; \
281} \
282static DEVICE_ATTR(value, S_IWUGO | S_IRUGO, show_##value, set_##value);
283
284
285
286
287/**
288 * usb_tranzport_delete
289 */
290static void usb_tranzport_delete(struct usb_tranzport *dev)
291{
292 usb_tranzport_abort_transfers(dev);
293 /* This is just too twisted to be correct */
294 if(dev->intf != NULL) {
295 device_remove_file(&dev->intf->dev, &dev_attr_LightRecord);
296 device_remove_file(&dev->intf->dev, &dev_attr_LightTrackrec);
297 device_remove_file(&dev->intf->dev, &dev_attr_LightTrackmute);
298 device_remove_file(&dev->intf->dev, &dev_attr_LightTracksolo);
299 device_remove_file(&dev->intf->dev, &dev_attr_LightTrackmute);
300 device_remove_file(&dev->intf->dev, &dev_attr_LightAnysolo);
301 device_remove_file(&dev->intf->dev, &dev_attr_LightLoop);
302 device_remove_file(&dev->intf->dev, &dev_attr_LightPunch);
303 device_remove_file(&dev->intf->dev, &dev_attr_wheel);
304 device_remove_file(&dev->intf->dev, &dev_attr_enable);
305 device_remove_file(&dev->intf->dev, &dev_attr_event);
306 device_remove_file(&dev->intf->dev, &dev_attr_offline);
307 device_remove_file(&dev->intf->dev, &dev_attr_compress_wheel);
308
309 device_remove_file(&dev->intf->dev, &dev_attr_dump_state);
310 }
311
312 /* free data structures */
313 usb_free_urb(dev->interrupt_in_urb);
314 usb_free_urb(dev->interrupt_out_urb);
315 kfree(dev->ring_buffer);
316 kfree(dev->interrupt_in_buffer);
317 kfree(dev->interrupt_out_buffer);
318 kfree(dev);
319}
320
321/**
322 * usb_tranzport_interrupt_in_callback
323 */
324
325static void usb_tranzport_interrupt_in_callback(struct urb *urb)
326{
327 struct usb_tranzport *dev = urb->context;
328 unsigned int next_ring_head;
329 int retval = -1;
330
331 if (urb->status) {
332 if (urb->status == -ENOENT ||
333 urb->status == -ECONNRESET ||
334 urb->status == -ESHUTDOWN) {
335 goto exit;
336 } else {
337 dbg_info(&dev->intf->dev, "%s: nonzero status received: %d\n",
d599edca 338 __func__, urb->status);
8da3dc28
DT
339 goto resubmit; /* maybe we can recover */
340 }
341 }
342
343 if (urb->actual_length != 8) {
344 dev_warn(&dev->intf->dev,
345 "Urb length was %d bytes!! Do something intelligent \n", urb->actual_length);
346 } else {
347 dbg_info(&dev->intf->dev, "%s: received: %02x%02x%02x%02x%02x%02x%02x%02x\n",
d599edca 348 __func__, 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]);
8da3dc28
DT
349#if SUPPRESS_EXTRA_OFFLINE_EVENTS
350 if(dev->offline == 2 && dev->interrupt_in_buffer[1] == 0xff) { goto resubmit; }
351 if(dev->offline == 1 && dev->interrupt_in_buffer[1] == 0xff) { dev->offline = 2; goto resubmit; }
352
353/* Always pass one offline event up the stack */
354 if(dev->offline > 0 && dev->interrupt_in_buffer[1] != 0xff) { dev->offline = 0; }
355 if(dev->offline == 0 && dev->interrupt_in_buffer[1] == 0xff) { dev->offline = 1; }
356
357#endif
d599edca 358 dbg_info(&dev->intf->dev, "%s: head, tail are %x, %x\n", __func__,dev->ring_head,dev->ring_tail);
8da3dc28
DT
359
360 next_ring_head = (dev->ring_head+1) % ring_buffer_size;
361
362 if (next_ring_head != dev->ring_tail) {
363 memcpy(&((*dev->ring_buffer)[dev->ring_head]), dev->interrupt_in_buffer, urb->actual_length);
364 dev->ring_head = next_ring_head;
365 retval = 0;
366 memset(dev->interrupt_in_buffer, 0, urb->actual_length);
367 } else {
368 dev_warn(&dev->intf->dev,
369 "Ring buffer overflow, %d bytes dropped\n",
370 urb->actual_length);
371 memset(dev->interrupt_in_buffer, 0, urb->actual_length);
372 }
373 }
374
375resubmit:
376 /* resubmit if we're still running */
377 if (dev->interrupt_in_running && dev->intf) {
378 retval = usb_submit_urb(dev->interrupt_in_urb, GFP_ATOMIC);
379 if (retval)
380 dev_err(&dev->intf->dev,
381 "usb_submit_urb failed (%d)\n", retval);
382 }
383
384exit:
385 dev->interrupt_in_done = 1;
386 wake_up_interruptible(&dev->read_wait);
387}
388
389/**
390 * usb_tranzport_interrupt_out_callback
391 */
392static void usb_tranzport_interrupt_out_callback(struct urb *urb)
393{
394 struct usb_tranzport *dev = urb->context;
395
396 /* sync/async unlink faults aren't errors */
397 if (urb->status && !(urb->status == -ENOENT ||
398 urb->status == -ECONNRESET ||
399 urb->status == -ESHUTDOWN))
400 dbg_info(&dev->intf->dev,
401 "%s - nonzero write interrupt status received: %d\n",
d599edca 402 __func__, urb->status);
8da3dc28
DT
403
404 dev->interrupt_out_busy = 0;
405 wake_up_interruptible(&dev->write_wait);
406}
407
408/**
409 * usb_tranzport_open
410 */
411static int usb_tranzport_open(struct inode *inode, struct file *file)
412{
413 struct usb_tranzport *dev;
414 int subminor;
415 int retval = 0;
416 struct usb_interface *interface;
417
418 nonseekable_open(inode, file);
419 subminor = iminor(inode);
420
421 mutex_lock(&disconnect_mutex);
422
423 interface = usb_find_interface(&usb_tranzport_driver, subminor);
424
425 if (!interface) {
426 err("%s - error, can't find device for minor %d\n",
d599edca 427 __func__, subminor);
8da3dc28
DT
428 retval = -ENODEV;
429 goto unlock_disconnect_exit;
430 }
431
432 dev = usb_get_intfdata(interface);
433
434 if (!dev) {
435 retval = -ENODEV;
436 goto unlock_disconnect_exit;
437 }
438
439 /* lock this device */
440 if (down_interruptible(&dev->sem)) {
441 retval = -ERESTARTSYS;
442 goto unlock_disconnect_exit;
443 }
444
445 /* allow opening only once */
446 if (dev->open_count) {
447 retval = -EBUSY;
448 goto unlock_exit;
449 }
450 dev->open_count = 1;
451
452 /* initialize in direction */
453 dev->ring_head = 0;
454 dev->ring_tail = 0;
455 usb_fill_int_urb(dev->interrupt_in_urb,
456 interface_to_usbdev(interface),
457 usb_rcvintpipe(interface_to_usbdev(interface),
458 dev->interrupt_in_endpoint->bEndpointAddress),
459 dev->interrupt_in_buffer,
460 dev->interrupt_in_endpoint_size,
461 usb_tranzport_interrupt_in_callback,
462 dev,
463 dev->interrupt_in_interval);
464
465 dev->interrupt_in_running = 1;
466 dev->interrupt_in_done = 0;
467 dev->enable = 1;
468 dev->offline = 0;
469 dev->compress_wheel = 1;
470
471 retval = usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL);
472 if (retval) {
473 dev_err(&interface->dev, "Couldn't submit interrupt_in_urb %d\n", retval);
474 dev->interrupt_in_running = 0;
475 dev->open_count = 0;
476 goto unlock_exit;
477 }
478
479 /* save device in the file's private structure */
480 file->private_data = dev;
481
482
483unlock_exit:
484 up(&dev->sem);
485
486unlock_disconnect_exit:
487 mutex_unlock(&disconnect_mutex);
488
489 return retval;
490}
491
492/**
493 * usb_tranzport_release
494 */
495static int usb_tranzport_release(struct inode *inode, struct file *file)
496{
497 struct usb_tranzport *dev;
498 int retval = 0;
499
500 dev = file->private_data;
501
502 if (dev == NULL) {
503 retval = -ENODEV;
504 goto exit;
505 }
506
507 if (down_interruptible(&dev->sem)) {
508 retval = -ERESTARTSYS;
509 goto exit;
510 }
511
512 if (dev->open_count != 1) {
513 retval = -ENODEV;
514 goto unlock_exit;
515 }
516
517 if (dev->intf == NULL) {
518 /* the device was unplugged before the file was released */
519 up(&dev->sem);
520 /* unlock here as usb_tranzport_delete frees dev */
521 usb_tranzport_delete(dev);
522 retval = -ENODEV;
523 goto exit;
524 }
525
526 /* wait until write transfer is finished */
527 if (dev->interrupt_out_busy)
528 wait_event_interruptible_timeout(dev->write_wait, !dev->interrupt_out_busy, 2 * HZ);
529 usb_tranzport_abort_transfers(dev);
530 dev->open_count = 0;
531
532unlock_exit:
533 up(&dev->sem);
534
535exit:
536 return retval;
537}
538
539/**
540 * usb_tranzport_poll
541 */
542static unsigned int usb_tranzport_poll(struct file *file, poll_table *wait)
543{
544 struct usb_tranzport *dev;
545 unsigned int mask = 0;
546
547 dev = file->private_data;
548
549 poll_wait(file, &dev->read_wait, wait);
550 poll_wait(file, &dev->write_wait, wait);
551
552 if (dev->ring_head != dev->ring_tail)
553 mask |= POLLIN | POLLRDNORM;
554 if (!dev->interrupt_out_busy)
555 mask |= POLLOUT | POLLWRNORM;
556
557 return mask;
558}
559
560/**
561 * usb_tranzport_read
562 */
563static ssize_t usb_tranzport_read(struct file *file, char __user *buffer, size_t count,
564 loff_t *ppos)
565{
566 struct usb_tranzport *dev;
8da3dc28
DT
567 int retval = 0;
568
569#if BUFFERED_READS
570 int c = 0;
571#endif
572
573#if COMPRESS_WHEEL_EVENTS
574 signed char oldwheel;
575 signed char newwheel;
576 int cancompress = 1;
577 int next_tail;
578#endif
579
580/* do I have such a thing as a null event? */
581
582 dev = file->private_data;
583
584 /* verify that we actually have some data to read */
585 if (count == 0)
586 goto exit;
587
588 /* lock this object */
589 if (down_interruptible(&dev->sem)) {
590 retval = -ERESTARTSYS;
591 goto exit;
592 }
593
594 /* verify that the device wasn't unplugged */
595 if (dev->intf == NULL) {
596 retval = -ENODEV;
597 err("No device or device unplugged %d\n", retval);
598 goto unlock_exit;
599 }
600
601 while (dev->ring_head == dev->ring_tail) {
602
603 if (file->f_flags & O_NONBLOCK) {
604 retval = -EAGAIN;
605 goto unlock_exit;
606 }
607 // atomic_cmp_exchange(&dev->interrupt_in_done,0,0);
608 dev->interrupt_in_done = 0 ; /* tiny race - FIXME: make atomic? */
609 retval = wait_event_interruptible(dev->read_wait, dev->interrupt_in_done);
610 if (retval < 0) {
611 goto unlock_exit;
612 }
613 }
614
615 dbg_info(&dev->intf->dev, "%s: copying to userspace: %02x%02x%02x%02x%02x%02x%02x%02x\n",
d599edca 616 __func__, (*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]);
8da3dc28
DT
617
618#if BUFFERED_READS
619 c = 0;
620 while((c < count) && (dev->ring_tail != dev->ring_head)) {
621
622/* This started off in the lower level service routine, and I moved it here. Then my brain died. Not done yet. */
623#if COMPRESS_WHEEL_EVENTS
624 next_tail = (dev->ring_tail+1) % ring_buffer_size;
625 if(dev->compress_wheel) cancompress = 1;
626 while(dev->ring_head != next_tail && cancompress == 1 ) {
627 newwheel = (*dev->ring_buffer)[next_tail].cmd[6];
628 oldwheel = (*dev->ring_buffer)[dev->ring_tail].cmd[6];
629 // if both are wheel events, and no buttons have changes (FIXME, do I have to check?),
630 // and we are the same sign, we can compress +- 7F
631 // FIXME: saner check for overflow! - max of +- 7F
632 // FIXME the math is wrong for going in reverse, actually, as the midi spec doesn't allow signed chars
633
634 dbg_info(&dev->intf->dev, "%s: trying to compress: %02x%02x%02x%02x%02x %02x %02x %02x\n",
d599edca 635 __func__, (*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]);
8da3dc28
DT
636
637
638 if(((*dev->ring_buffer)[dev->ring_tail].cmd[6] != 0 &&
639 (*dev->ring_buffer)[next_tail].cmd[6] != 0 ) &&
640 ((newwheel > 0 && oldwheel > 0) ||
641 (newwheel < 0 && oldwheel < 0)) &&
642 ((*dev->ring_buffer)[dev->ring_tail].cmd[2] == (*dev->ring_buffer)[next_tail].cmd[2]) &&
643 ((*dev->ring_buffer)[dev->ring_tail].cmd[3] == (*dev->ring_buffer)[next_tail].cmd[3]) &&
644 ((*dev->ring_buffer)[dev->ring_tail].cmd[4] == (*dev->ring_buffer)[next_tail].cmd[4]) &&
645 ((*dev->ring_buffer)[dev->ring_tail].cmd[5] == (*dev->ring_buffer)[next_tail].cmd[5]))
646 {
647 dbg_info(&dev->intf->dev, "%s: should compress: %02x%02x%02x%02x%02x%02x%02x%02x\n",
d599edca 648 __func__, (*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]);
8da3dc28
DT
649
650 newwheel += oldwheel;
651 if(oldwheel > 0 && !(newwheel > 0)) {
652 newwheel = 0x7f;
653 cancompress = 0;
654 }
655 if(oldwheel < 0 && !(newwheel < 0)) {
656 newwheel = 0x80;
657 cancompress = 0;
658 }
659
660 (*dev->ring_buffer)[next_tail].cmd[6] = newwheel;
661 dev->ring_tail = next_tail;
662 next_tail = (dev->ring_tail+1) % ring_buffer_size;
663 } else {
664 cancompress = 0;
665 }
666 }
667#endif /* COMPRESS_WHEEL_EVENTS */
668
669 if (copy_to_user(&buffer[c], &(*dev->ring_buffer)[dev->ring_tail], 8)) {
670 retval = -EFAULT;
671 goto unlock_exit;
672 }
673
674 dev->ring_tail = (dev->ring_tail+1) % ring_buffer_size;
675 c+=8;
d599edca 676 dbg_info(&dev->intf->dev, "%s: head, tail are %x, %x\n", __func__,dev->ring_head,dev->ring_tail);
8da3dc28
DT
677 }
678 retval = c;
679
680#else
681 if (copy_to_user(buffer, &(*dev->ring_buffer)[dev->ring_tail], 8)) {
682 retval = -EFAULT;
683 goto unlock_exit;
684 }
685
686 dev->ring_tail = (dev->ring_tail+1) % ring_buffer_size;
d599edca 687 dbg_info(&dev->intf->dev, "%s: head, tail are %x, %x\n", __func__,dev->ring_head,dev->ring_tail);
8da3dc28
DT
688
689 retval = 8;
690#endif /* BUFFERED_READS */
691
692unlock_exit:
693 /* unlock the device */
694 up(&dev->sem);
695
696exit:
697 return retval;
698}
699
700/**
701 * usb_tranzport_write
702 */
703static ssize_t usb_tranzport_write(struct file *file, const char __user *buffer,
704 size_t count, loff_t *ppos)
705{
706 struct usb_tranzport *dev;
707 size_t bytes_to_write;
708 int retval = 0;
709
710 dev = file->private_data;
711
712 /* verify that we actually have some data to write */
713 if (count == 0)
714 goto exit;
715
716 /* lock this object */
717 if (down_interruptible(&dev->sem)) {
718 retval = -ERESTARTSYS;
719 goto exit;
720 }
721
722 /* verify that the device wasn't unplugged */
723 if (dev->intf == NULL) {
724 retval = -ENODEV;
725 err("No device or device unplugged %d\n", retval);
726 goto unlock_exit;
727 }
728
729 /* wait until previous transfer is finished */
730 if (dev->interrupt_out_busy) {
731 if (file->f_flags & O_NONBLOCK) {
732 retval = -EAGAIN;
733 goto unlock_exit;
734 }
735 retval = wait_event_interruptible(dev->write_wait, !dev->interrupt_out_busy);
736 if (retval < 0) {
737 goto unlock_exit;
738 }
739 }
740
741 /* write the data into interrupt_out_buffer from userspace */
742 bytes_to_write = min(count, write_buffer_size*dev->interrupt_out_endpoint_size);
743 if (bytes_to_write < count)
744 dev_warn(&dev->intf->dev, "Write buffer overflow, %zd bytes dropped\n",count-bytes_to_write);
745
d599edca 746 dbg_info(&dev->intf->dev, "%s: count = %zd, bytes_to_write = %zd\n", __func__, count, bytes_to_write);
8da3dc28
DT
747
748 if (copy_from_user(dev->interrupt_out_buffer, buffer, bytes_to_write)) {
749 retval = -EFAULT;
750 goto unlock_exit;
751 }
752
753 if (dev->interrupt_out_endpoint == NULL) {
754 err("Endpoint should not be be null! \n");
755 goto unlock_exit;
756 }
757
758 /* send off the urb */
759 usb_fill_int_urb(dev->interrupt_out_urb,
760 interface_to_usbdev(dev->intf),
761 usb_sndintpipe(interface_to_usbdev(dev->intf),
762 dev->interrupt_out_endpoint->bEndpointAddress),
763 dev->interrupt_out_buffer,
764 bytes_to_write,
765 usb_tranzport_interrupt_out_callback,
766 dev,
767 dev->interrupt_out_interval);
768
769 dev->interrupt_out_busy = 1;
770 wmb();
771
772 retval = usb_submit_urb(dev->interrupt_out_urb, GFP_KERNEL);
773 if (retval) {
774 dev->interrupt_out_busy = 0;
775 err("Couldn't submit interrupt_out_urb %d\n", retval);
776 goto unlock_exit;
777 }
778 retval = bytes_to_write;
779
780unlock_exit:
781 /* unlock the device */
782 up(&dev->sem);
783
784exit:
785 return retval;
786}
787
788/* file operations needed when we register this driver */
789static const struct file_operations usb_tranzport_fops = {
790 .owner = THIS_MODULE,
791 .read = usb_tranzport_read,
792 .write = usb_tranzport_write,
793 .open = usb_tranzport_open,
794 .release = usb_tranzport_release,
795 .poll = usb_tranzport_poll,
796};
797
798/*
799 * usb class driver info in order to get a minor number from the usb core,
800 * and to have the device registered with the driver core
801 */
802static struct usb_class_driver usb_tranzport_class = {
803 .name = "tranzport%d",
804 .fops = &usb_tranzport_fops,
805 .minor_base = USB_TRANZPORT_MINOR_BASE,
806};
807
808
809/**
810 * usb_tranzport_probe
811 *
812 * Called by the usb core when a new device is connected that it thinks
813 * this driver might be interested in.
814 */
815static int usb_tranzport_probe(struct usb_interface *intf, const struct usb_device_id *id)
816{
817 struct usb_device *udev = interface_to_usbdev(intf);
818 struct usb_tranzport *dev = NULL;
819 struct usb_host_interface *iface_desc;
820 struct usb_endpoint_descriptor *endpoint;
821 int i;
822 int true_size;
823 int retval = -ENOMEM;
824
825 /* allocate memory for our device state and intialize it */
826
827 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
828 if (dev == NULL) {
829 dev_err(&intf->dev, "Out of memory\n");
830 goto exit;
831 }
832 init_MUTEX(&dev->sem);
833 dev->intf = intf;
834 init_waitqueue_head(&dev->read_wait);
835 init_waitqueue_head(&dev->write_wait);
836
837 iface_desc = intf->cur_altsetting;
838
839 /* set up the endpoint information */
840 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
841 endpoint = &iface_desc->endpoint[i].desc;
842
843 if (usb_endpoint_is_int_in(endpoint))
844 dev->interrupt_in_endpoint = endpoint;
845
846 if (usb_endpoint_is_int_out(endpoint))
847 dev->interrupt_out_endpoint = endpoint;
848 }
849 if (dev->interrupt_in_endpoint == NULL) {
850 dev_err(&intf->dev, "Interrupt in endpoint not found\n");
851 goto error;
852 }
853 if (dev->interrupt_out_endpoint == NULL)
854 dev_warn(&intf->dev, "Interrupt out endpoint not found (using control endpoint instead)\n");
855
856
857 dev->interrupt_in_endpoint_size = le16_to_cpu(dev->interrupt_in_endpoint->wMaxPacketSize);
858
859 if (dev->interrupt_in_endpoint_size != 8)
860 dev_warn(&intf->dev, "Interrupt in endpoint size is not 8!\n");
861
862 if(ring_buffer_size == 0) { ring_buffer_size = RING_BUFFER_SIZE; }
863 true_size = min(ring_buffer_size,RING_BUFFER_SIZE);
864 /* FIXME - there are more usb_alloc routines for dma correctness. Needed? */
865
866 dev->ring_buffer = kmalloc((true_size*sizeof(struct tranzport_cmd))+8, GFP_KERNEL);
867
868 if (!dev->ring_buffer) {
869 dev_err(&intf->dev, "Couldn't allocate ring_buffer of size %d\n",true_size);
870 goto error;
871 }
872 dev->interrupt_in_buffer = kmalloc(dev->interrupt_in_endpoint_size, GFP_KERNEL);
873 if (!dev->interrupt_in_buffer) {
874 dev_err(&intf->dev, "Couldn't allocate interrupt_in_buffer\n");
875 goto error;
876 }
877 dev->interrupt_in_urb = usb_alloc_urb(0, GFP_KERNEL);
878 if (!dev->interrupt_in_urb) {
879 dev_err(&intf->dev, "Couldn't allocate interrupt_in_urb\n");
880 goto error;
881 }
882 dev->interrupt_out_endpoint_size = dev->interrupt_out_endpoint ? le16_to_cpu(dev->interrupt_out_endpoint->wMaxPacketSize) :
883 udev->descriptor.bMaxPacketSize0;
884
885 if (dev->interrupt_out_endpoint_size !=8)
886 dev_warn(&intf->dev, "Interrupt out endpoint size is not 8!)\n");
887
888 dev->interrupt_out_buffer = kmalloc(write_buffer_size*dev->interrupt_out_endpoint_size, GFP_KERNEL);
889 if (!dev->interrupt_out_buffer) {
890 dev_err(&intf->dev, "Couldn't allocate interrupt_out_buffer\n");
891 goto error;
892 }
893 dev->interrupt_out_urb = usb_alloc_urb(0, GFP_KERNEL);
894 if (!dev->interrupt_out_urb) {
895 dev_err(&intf->dev, "Couldn't allocate interrupt_out_urb\n");
896 goto error;
897 }
898 dev->interrupt_in_interval = min_interrupt_in_interval > dev->interrupt_in_endpoint->bInterval ? min_interrupt_in_interval : dev->interrupt_in_endpoint->bInterval;
899 if (dev->interrupt_out_endpoint)
900 dev->interrupt_out_interval = min_interrupt_out_interval > dev->interrupt_out_endpoint->bInterval ? min_interrupt_out_interval : dev->interrupt_out_endpoint->bInterval;
901
902 /* we can register the device now, as it is ready */
903 usb_set_intfdata(intf, dev);
904
905 retval = usb_register_dev(intf, &usb_tranzport_class);
906 if (retval) {
907 /* something prevented us from registering this driver */
908 dev_err(&intf->dev, "Not able to get a minor for this device.\n");
909 usb_set_intfdata(intf, NULL);
910 goto error;
911 }
912
913 if((retval = device_create_file(&intf->dev, &dev_attr_LightRecord))) goto error;
914 if((retval = device_create_file(&intf->dev, &dev_attr_LightTrackrec))) goto error;
915 if((retval = device_create_file(&intf->dev, &dev_attr_LightTrackmute))) goto error;
916 if((retval = device_create_file(&intf->dev, &dev_attr_LightTracksolo))) goto error;
917 if((retval = device_create_file(&intf->dev, &dev_attr_LightAnysolo))) goto error;
918 if((retval = device_create_file(&intf->dev, &dev_attr_LightLoop))) goto error;
919 if((retval = device_create_file(&intf->dev, &dev_attr_LightPunch))) goto error;
920 if((retval = device_create_file(&intf->dev, &dev_attr_wheel))) goto error;
921 if((retval = device_create_file(&intf->dev, &dev_attr_event))) goto error;
922 if((retval = device_create_file(&intf->dev, &dev_attr_dump_state))) goto error;
923 if((retval = device_create_file(&intf->dev, &dev_attr_compress_wheel))) goto error;
924 if((retval = device_create_file(&intf->dev, &dev_attr_enable))) goto error;
925 if((retval = device_create_file(&intf->dev, &dev_attr_offline))) goto error;
926
927 /* let the user know what node this device is now attached to */
928 dev_info(&intf->dev, "Tranzport Device #%d now attached to major %d minor %d\n",
929 (intf->minor - USB_TRANZPORT_MINOR_BASE), USB_MAJOR, intf->minor);
930
931exit:
932 return retval;
933
934error:
935 usb_tranzport_delete(dev);
936
937 return retval;
938}
939
940/**
941 * usb_tranzport_disconnect
942 *
943 * Called by the usb core when the device is removed from the system.
944 */
945static void usb_tranzport_disconnect(struct usb_interface *intf)
946{
947 struct usb_tranzport *dev;
948 int minor;
949 mutex_lock(&disconnect_mutex);
950 dev = usb_get_intfdata(intf);
951 usb_set_intfdata(intf, NULL);
952 down(&dev->sem);
953 minor = intf->minor;
954 /* give back our minor */
955 usb_deregister_dev(intf, &usb_tranzport_class);
956
957 /* if the device is not opened, then we clean up right now */
958 if (!dev->open_count) {
959 up(&dev->sem);
960 usb_tranzport_delete(dev);
961 } else {
962 dev->intf = NULL;
963 up(&dev->sem);
964 }
965
966 mutex_unlock(&disconnect_mutex);
967
968 dev_info(&intf->dev, "Tranzport Surface #%d now disconnected\n",
969 (minor - USB_TRANZPORT_MINOR_BASE));
970}
971
972/* usb specific object needed to register this driver with the usb subsystem */
973static struct usb_driver usb_tranzport_driver = {
974 .name = "tranzport",
975 .probe = usb_tranzport_probe,
976 .disconnect = usb_tranzport_disconnect,
977 .id_table = usb_tranzport_table,
978};
979
980/**
981 * usb_tranzport_init
982 */
983static int __init usb_tranzport_init(void)
984{
985 int retval;
986
987 /* register this driver with the USB subsystem */
988 retval = usb_register(&usb_tranzport_driver);
989 if (retval)
990 err("usb_register failed for the "__FILE__" driver. Error number %d\n", retval);
991
992 return retval;
993}
994
995/**
996 * usb_tranzport_exit
997 */
998static void __exit usb_tranzport_exit(void)
999{
1000 /* deregister this driver with the USB subsystem */
1001 usb_deregister(&usb_tranzport_driver);
1002}
1003
1004module_init(usb_tranzport_init);
1005module_exit(usb_tranzport_exit);
1006