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