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