RT-AC56 3.0.0.4.374.37 core
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / drivers / staging / frontier / tranzport.c
blob2e76d17a4518089a4ffa3ac51d8430758a03917d
1 /*
2 * Frontier Designs Tranzport driver
4 * Copyright (C) 2007 Michael Taht (m@taht.net)
6 * Based on the usbled driver and ldusb drivers by
8 * Copyright (C) 2004 Greg Kroah-Hartman (greg@kroah.com)
9 * Copyright (C) 2005 Michael Hund <mhund@ld-didactic.de>
11 * The ldusb driver was, in turn, derived from Lego USB Tower driver
12 * Copyright (C) 2003 David Glance <advidgsf@sourceforge.net>
13 * 2001-2004 Juergen Stuber <starblue@users.sourceforge.net>
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License as
17 * published by the Free Software Foundation, version 2.
22 * This driver uses a ring buffer for time critical reading of
23 * interrupt in reports and provides read and write methods for
24 * raw interrupt reports.
27 /* Note: this currently uses a dumb ringbuffer for reads and writes.
28 * A more optimal driver would cache and kill off outstanding urbs that are
29 * now invalid, and ignore ones that already were in the queue but valid
30 * as we only have 17 commands for the tranzport. In particular this is
31 * key for getting lights to flash in time as otherwise many commands
32 * can be buffered up before the light change makes it to the interface.
35 #include <linux/kernel.h>
36 #include <linux/errno.h>
37 #include <linux/init.h>
38 #include <linux/slab.h>
39 #include <linux/module.h>
40 #include <linux/mutex.h>
42 #include <linux/uaccess.h>
43 #include <linux/input.h>
44 #include <linux/usb.h>
45 #include <linux/poll.h>
47 /* Define these values to match your devices */
48 #define VENDOR_ID 0x165b
49 #define PRODUCT_ID 0x8101
51 #ifdef CONFIG_USB_DYNAMIC_MINORS
52 #define USB_TRANZPORT_MINOR_BASE 0
53 #else
54 #define USB_TRANZPORT_MINOR_BASE 177
55 #endif
57 /* table of devices that work with this driver */
58 static const struct usb_device_id usb_tranzport_table[] = {
59 {USB_DEVICE(VENDOR_ID, PRODUCT_ID)},
60 {} /* Terminating entry */
63 MODULE_DEVICE_TABLE(usb, usb_tranzport_table);
64 MODULE_VERSION("0.35");
65 MODULE_AUTHOR("Mike Taht <m@taht.net>");
66 MODULE_DESCRIPTION("Tranzport USB Driver");
67 MODULE_LICENSE("GPL");
68 MODULE_SUPPORTED_DEVICE("Frontier Designs Tranzport Control Surface");
70 #define SUPPRESS_EXTRA_OFFLINE_EVENTS 1
71 #define COMPRESS_WHEEL_EVENTS 1
72 #define BUFFERED_READS 1
73 #define RING_BUFFER_SIZE 1000
74 #define WRITE_BUFFER_SIZE 34
75 #define TRANZPORT_USB_TIMEOUT 10
76 #define TRANZPORT_DEBUG 0
78 static int debug = TRANZPORT_DEBUG;
80 /* Use our own dbg macro */
81 #define dbg_info(dev, format, arg...) do \
82 { if (debug) dev_info(dev , format , ## arg); } while (0)
84 /* Module parameters */
86 module_param(debug, int, S_IRUGO | S_IWUSR);
87 MODULE_PARM_DESC(debug, "Debug enabled or not");
89 /* All interrupt in transfers are collected in a ring buffer to
90 * avoid racing conditions and get better performance of the driver.
93 static int ring_buffer_size = RING_BUFFER_SIZE;
95 module_param(ring_buffer_size, int, S_IRUGO);
96 MODULE_PARM_DESC(ring_buffer_size, "Read ring buffer size in reports");
98 /* The write_buffer can one day contain more than one interrupt out transfer.
100 static int write_buffer_size = WRITE_BUFFER_SIZE;
101 module_param(write_buffer_size, int, S_IRUGO);
102 MODULE_PARM_DESC(write_buffer_size, "Write buffer size");
105 * Increase the interval for debugging purposes.
106 * or set to 1 to use the standard interval from the endpoint descriptors.
109 static int min_interrupt_in_interval = TRANZPORT_USB_TIMEOUT;
110 module_param(min_interrupt_in_interval, int, 0);
111 MODULE_PARM_DESC(min_interrupt_in_interval,
112 "Minimum interrupt in interval in ms");
114 static int min_interrupt_out_interval = TRANZPORT_USB_TIMEOUT;
115 module_param(min_interrupt_out_interval, int, 0);
116 MODULE_PARM_DESC(min_interrupt_out_interval,
117 "Minimum interrupt out interval in ms");
119 struct tranzport_cmd {
120 unsigned char cmd[8];
123 /* Structure to hold all of our device specific stuff */
125 struct usb_tranzport {
126 struct mutex mtx; /* locks this structure */
127 struct usb_interface *intf; /* save off the usb interface pointer */
128 int open_count; /* number of times this port opened */
129 struct tranzport_cmd (*ring_buffer)[RING_BUFFER_SIZE];
130 unsigned int ring_head;
131 unsigned int ring_tail;
132 wait_queue_head_t read_wait;
133 wait_queue_head_t write_wait;
134 unsigned char *interrupt_in_buffer;
135 struct usb_endpoint_descriptor *interrupt_in_endpoint;
136 struct urb *interrupt_in_urb;
137 int interrupt_in_interval;
138 size_t interrupt_in_endpoint_size;
139 int interrupt_in_running;
140 int interrupt_in_done;
141 char *interrupt_out_buffer;
142 struct usb_endpoint_descriptor *interrupt_out_endpoint;
143 struct urb *interrupt_out_urb;
144 int interrupt_out_interval;
145 size_t interrupt_out_endpoint_size;
146 int interrupt_out_busy;
148 /* Sysfs support */
150 unsigned char enable; /* 0 if disabled 1 if enabled */
151 unsigned char offline; /* if the device is out of range or asleep */
152 unsigned char compress_wheel; /* flag to compress wheel events */
155 /* prevent races between open() and disconnect() */
156 static DEFINE_MUTEX(disconnect_mutex);
158 static struct usb_driver usb_tranzport_driver;
161 * usb_tranzport_abort_transfers
162 * aborts transfers and frees associated data structures
164 static void usb_tranzport_abort_transfers(struct usb_tranzport *dev)
166 /* shutdown transfer */
167 if (dev->interrupt_in_running) {
168 dev->interrupt_in_running = 0;
169 if (dev->intf)
170 usb_kill_urb(dev->interrupt_in_urb);
172 if (dev->interrupt_out_busy)
173 if (dev->intf)
174 usb_kill_urb(dev->interrupt_out_urb);
177 #define show_int(value) \
178 static ssize_t show_##value(struct device *dev, \
179 struct device_attribute *attr, char *buf) \
181 struct usb_interface *intf = to_usb_interface(dev); \
182 struct usb_tranzport *t = usb_get_intfdata(intf); \
183 return sprintf(buf, "%d\n", t->value); \
185 static DEVICE_ATTR(value, S_IRUGO, show_##value, NULL);
187 #define show_set_int(value) \
188 static ssize_t show_##value(struct device *dev, \
189 struct device_attribute *attr, char *buf) \
191 struct usb_interface *intf = to_usb_interface(dev); \
192 struct usb_tranzport *t = usb_get_intfdata(intf); \
193 return sprintf(buf, "%d\n", t->value); \
195 static ssize_t set_##value(struct device *dev, \
196 struct device_attribute *attr, \
197 const char *buf, size_t count) \
199 struct usb_interface *intf = to_usb_interface(dev); \
200 struct usb_tranzport *t = usb_get_intfdata(intf); \
201 unsigned long temp; \
202 if (strict_strtoul(buf, 10, &temp)) \
203 return -EINVAL; \
204 t->value = temp; \
205 return count; \
207 static DEVICE_ATTR(value, S_IWUSR | S_IRUGO, show_##value, set_##value);
209 show_int(enable);
210 show_int(offline);
211 show_set_int(compress_wheel);
214 * usb_tranzport_delete
216 static void usb_tranzport_delete(struct usb_tranzport *dev)
218 usb_tranzport_abort_transfers(dev);
219 if (dev->intf != NULL) {
220 device_remove_file(&dev->intf->dev, &dev_attr_enable);
221 device_remove_file(&dev->intf->dev, &dev_attr_offline);
222 device_remove_file(&dev->intf->dev, &dev_attr_compress_wheel);
225 /* free data structures */
226 usb_free_urb(dev->interrupt_in_urb);
227 usb_free_urb(dev->interrupt_out_urb);
228 kfree(dev->ring_buffer);
229 kfree(dev->interrupt_in_buffer);
230 kfree(dev->interrupt_out_buffer);
231 kfree(dev);
235 * usb_tranzport_interrupt_in_callback
238 static void usb_tranzport_interrupt_in_callback(struct urb *urb)
240 struct usb_tranzport *dev = urb->context;
241 unsigned int next_ring_head;
242 int retval = -1;
244 if (urb->status) {
245 if (urb->status == -ENOENT ||
246 urb->status == -ECONNRESET ||
247 urb->status == -ESHUTDOWN) {
248 goto exit;
249 } else {
250 dbg_info(&dev->intf->dev,
251 "%s: nonzero status received: %d\n",
252 __func__, urb->status);
253 goto resubmit; /* maybe we can recover */
257 if (urb->actual_length != 8) {
258 dev_warn(&dev->intf->dev,
259 "Urb length was %d bytes!!"
260 "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;
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;
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);
291 next_ring_head = (dev->ring_head + 1) % ring_buffer_size;
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);
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);
316 exit:
317 dev->interrupt_in_done = 1;
318 wake_up_interruptible(&dev->read_wait);
322 * usb_tranzport_interrupt_out_callback
324 static void usb_tranzport_interrupt_out_callback(struct urb *urb)
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);
335 dev->interrupt_out_busy = 0;
336 wake_up_interruptible(&dev->write_wait);
339 * usb_tranzport_open
341 static int usb_tranzport_open(struct inode *inode, struct file *file)
343 struct usb_tranzport *dev;
344 int subminor;
345 int retval = 0;
346 struct usb_interface *interface;
348 nonseekable_open(inode, file);
349 subminor = iminor(inode);
351 mutex_lock(&disconnect_mutex);
353 interface = usb_find_interface(&usb_tranzport_driver, subminor);
355 if (!interface) {
356 err("%s - error, can't find device for minor %d\n",
357 __func__, subminor);
358 retval = -ENODEV;
359 goto unlock_disconnect_exit;
362 dev = usb_get_intfdata(interface);
364 if (!dev) {
365 retval = -ENODEV;
366 goto unlock_disconnect_exit;
369 /* lock this device */
370 if (mutex_lock_interruptible(&dev->mtx)) {
371 retval = -ERESTARTSYS;
372 goto unlock_disconnect_exit;
375 /* allow opening only once */
376 if (dev->open_count) {
377 retval = -EBUSY;
378 goto unlock_exit;
380 dev->open_count = 1;
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);
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;
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;
410 /* save device in the file's private structure */
411 file->private_data = dev;
413 unlock_exit:
414 mutex_unlock(&dev->mtx);
416 unlock_disconnect_exit:
417 mutex_unlock(&disconnect_mutex);
419 return retval;
423 * usb_tranzport_release
425 static int usb_tranzport_release(struct inode *inode, struct file *file)
427 struct usb_tranzport *dev;
428 int retval = 0;
430 dev = file->private_data;
432 if (dev == NULL) {
433 retval = -ENODEV;
434 goto exit;
437 if (mutex_lock_interruptible(&dev->mtx)) {
438 retval = -ERESTARTSYS;
439 goto exit;
442 if (dev->open_count != 1) {
443 retval = -ENODEV;
444 goto unlock_exit;
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;
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;
464 unlock_exit:
465 mutex_unlock(&dev->mtx);
467 exit:
468 return retval;
472 * usb_tranzport_poll
474 static unsigned int usb_tranzport_poll(struct file *file, poll_table * wait)
476 struct usb_tranzport *dev;
477 unsigned int mask = 0;
478 dev = file->private_data;
479 poll_wait(file, &dev->read_wait, wait);
480 poll_wait(file, &dev->write_wait, wait);
481 if (dev->ring_head != dev->ring_tail)
482 mask |= POLLIN | POLLRDNORM;
483 if (!dev->interrupt_out_busy)
484 mask |= POLLOUT | POLLWRNORM;
485 return mask;
488 * usb_tranzport_read
491 static ssize_t usb_tranzport_read(struct file *file, char __user *buffer,
492 size_t count, loff_t *ppos)
494 struct usb_tranzport *dev;
495 int retval = 0;
496 #if BUFFERED_READS
497 int c = 0;
498 #endif
499 #if COMPRESS_WHEEL_EVENTS
500 signed char oldwheel;
501 signed char newwheel;
502 int cancompress = 1;
503 int next_tail;
504 #endif
506 /* do I have such a thing as a null event? */
508 dev = file->private_data;
510 /* verify that we actually have some data to read */
511 if (count == 0)
512 goto exit;
514 /* lock this object */
515 if (mutex_lock_interruptible(&dev->mtx)) {
516 retval = -ERESTARTSYS;
517 goto exit;
520 /* verify that the device wasn't unplugged */ if (dev->intf == NULL) {
521 retval = -ENODEV;
522 err("No device or device unplugged %d\n", retval);
523 goto unlock_exit;
526 while (dev->ring_head == dev->ring_tail) {
528 if (file->f_flags & O_NONBLOCK) {
529 retval = -EAGAIN;
530 goto unlock_exit;
532 /* atomic_cmp_exchange(&dev->interrupt_in_done,0,0); */
533 dev->interrupt_in_done = 0;
534 retval = wait_event_interruptible(dev->read_wait,
535 dev->interrupt_in_done);
536 if (retval < 0)
537 goto unlock_exit;
540 dbg_info(&dev->intf->dev,
541 "%s: copying to userspace: "
542 "%02x%02x%02x%02x%02x%02x%02x%02x\n",
543 __func__,
544 (*dev->ring_buffer)[dev->ring_tail].cmd[0],
545 (*dev->ring_buffer)[dev->ring_tail].cmd[1],
546 (*dev->ring_buffer)[dev->ring_tail].cmd[2],
547 (*dev->ring_buffer)[dev->ring_tail].cmd[3],
548 (*dev->ring_buffer)[dev->ring_tail].cmd[4],
549 (*dev->ring_buffer)[dev->ring_tail].cmd[5],
550 (*dev->ring_buffer)[dev->ring_tail].cmd[6],
551 (*dev->ring_buffer)[dev->ring_tail].cmd[7]);
553 #if BUFFERED_READS
554 c = 0;
555 while ((c < count) && (dev->ring_tail != dev->ring_head)) {
557 #if COMPRESS_WHEEL_EVENTS
558 next_tail = (dev->ring_tail+1) % ring_buffer_size;
559 if (dev->compress_wheel)
560 cancompress = 1;
561 while (dev->ring_head != next_tail && cancompress == 1) {
562 newwheel = (*dev->ring_buffer)[next_tail].cmd[6];
563 oldwheel = (*dev->ring_buffer)[dev->ring_tail].cmd[6];
564 dbg_info(&dev->intf->dev,
565 "%s: trying to compress: "
566 "%02x%02x%02x%02x%02x%02x%02x%02x\n",
567 __func__,
568 (*dev->ring_buffer)[dev->ring_tail].cmd[0],
569 (*dev->ring_buffer)[dev->ring_tail].cmd[1],
570 (*dev->ring_buffer)[dev->ring_tail].cmd[2],
571 (*dev->ring_buffer)[dev->ring_tail].cmd[3],
572 (*dev->ring_buffer)[dev->ring_tail].cmd[4],
573 (*dev->ring_buffer)[dev->ring_tail].cmd[5],
574 (*dev->ring_buffer)[dev->ring_tail].cmd[6],
575 (*dev->ring_buffer)[dev->ring_tail].cmd[7]);
577 if (((*dev->ring_buffer)[dev->ring_tail].cmd[6] != 0 &&
578 (*dev->ring_buffer)[next_tail].cmd[6] != 0) &&
579 ((newwheel > 0 && oldwheel > 0) ||
580 (newwheel < 0 && oldwheel < 0)) &&
581 ((*dev->ring_buffer)[dev->ring_tail].cmd[2] ==
582 (*dev->ring_buffer)[next_tail].cmd[2]) &&
583 ((*dev->ring_buffer)[dev->ring_tail].cmd[3] ==
584 (*dev->ring_buffer)[next_tail].cmd[3]) &&
585 ((*dev->ring_buffer)[dev->ring_tail].cmd[4] ==
586 (*dev->ring_buffer)[next_tail].cmd[4]) &&
587 ((*dev->ring_buffer)[dev->ring_tail].cmd[5] ==
588 (*dev->ring_buffer)[next_tail].cmd[5])) {
589 dbg_info(&dev->intf->dev,
590 "%s: should compress: "
591 "%02x%02x%02x%02x%02x%02x%02x%02x\n",
592 __func__,
593 (*dev->ring_buffer)[dev->ring_tail].
594 cmd[0],
595 (*dev->ring_buffer)[dev->ring_tail].
596 cmd[1],
597 (*dev->ring_buffer)[dev->ring_tail].
598 cmd[2],
599 (*dev->ring_buffer)[dev->ring_tail].
600 cmd[3],
601 (*dev->ring_buffer)[dev->ring_tail].
602 cmd[4],
603 (*dev->ring_buffer)[dev->ring_tail].
604 cmd[5],
605 (*dev->ring_buffer)[dev->ring_tail].
606 cmd[6],
607 (*dev->ring_buffer)[dev->ring_tail].
608 cmd[7]);
609 newwheel += oldwheel;
610 if (oldwheel > 0 && !(newwheel > 0)) {
611 newwheel = 0x7f;
612 cancompress = 0;
614 if (oldwheel < 0 && !(newwheel < 0)) {
615 newwheel = 0x80;
616 cancompress = 0;
619 (*dev->ring_buffer)[next_tail].cmd[6] =
620 newwheel;
621 dev->ring_tail = next_tail;
622 next_tail =
623 (dev->ring_tail + 1) % ring_buffer_size;
624 } else {
625 cancompress = 0;
628 #endif /* COMPRESS_WHEEL_EVENTS */
629 if (copy_to_user(
630 &buffer[c],
631 &(*dev->ring_buffer)[dev->ring_tail], 8)) {
632 retval = -EFAULT;
633 goto unlock_exit;
635 dev->ring_tail = (dev->ring_tail + 1) % ring_buffer_size;
636 c += 8;
637 dbg_info(&dev->intf->dev,
638 "%s: head, tail are %x, %x\n",
639 __func__, dev->ring_head, dev->ring_tail);
641 retval = c;
643 #else
644 /* if (copy_to_user(buffer, &(*dev->ring_buffer)[dev->ring_tail], 8)) { */
645 retval = -EFAULT;
646 goto unlock_exit;
649 dev->ring_tail = (dev->ring_tail + 1) % ring_buffer_size;
650 dbg_info(&dev->intf->dev, "%s: head, tail are %x, %x\n",
651 __func__, dev->ring_head, dev->ring_tail);
653 retval = 8;
654 #endif /* BUFFERED_READS */
656 unlock_exit:
657 /* unlock the device */
658 mutex_unlock(&dev->mtx);
660 exit:
661 return retval;
665 * usb_tranzport_write
667 static ssize_t usb_tranzport_write(struct file *file,
668 const char __user *buffer, size_t count,
669 loff_t *ppos)
671 struct usb_tranzport *dev;
672 size_t bytes_to_write;
673 int retval = 0;
675 dev = file->private_data;
677 /* verify that we actually have some data to write */
678 if (count == 0)
679 goto exit;
681 /* lock this object */
682 if (mutex_lock_interruptible(&dev->mtx)) {
683 retval = -ERESTARTSYS;
684 goto exit;
686 /* verify that the device wasn't unplugged */
687 if (dev->intf == NULL) {
688 retval = -ENODEV;
689 err("No device or device unplugged %d\n", retval);
690 goto unlock_exit;
693 /* wait until previous transfer is finished */
694 if (dev->interrupt_out_busy) {
695 if (file->f_flags & O_NONBLOCK) {
696 retval = -EAGAIN;
697 goto unlock_exit;
699 retval = wait_event_interruptible(dev->write_wait,
700 !dev->interrupt_out_busy);
701 if (retval < 0)
702 goto unlock_exit;
705 /* write the data into interrupt_out_buffer from userspace */
706 bytes_to_write = min(count,
707 write_buffer_size *
708 dev->interrupt_out_endpoint_size);
709 if (bytes_to_write < count)
710 dev_warn(&dev->intf->dev,
711 "Write buffer overflow, %zd bytes dropped\n",
712 count - bytes_to_write);
714 dbg_info(&dev->intf->dev,
715 "%s: count = %zd, bytes_to_write = %zd\n", __func__,
716 count, bytes_to_write);
718 if (copy_from_user(dev->interrupt_out_buffer, buffer, bytes_to_write)) {
719 retval = -EFAULT;
720 goto unlock_exit;
723 if (dev->interrupt_out_endpoint == NULL) {
724 err("Endpoint should not be be null!\n");
725 goto unlock_exit;
728 /* send off the urb */
729 usb_fill_int_urb(dev->interrupt_out_urb,
730 interface_to_usbdev(dev->intf),
731 usb_sndintpipe(interface_to_usbdev(dev->intf),
732 dev->interrupt_out_endpoint->
733 bEndpointAddress),
734 dev->interrupt_out_buffer, bytes_to_write,
735 usb_tranzport_interrupt_out_callback, dev,
736 dev->interrupt_out_interval);
738 dev->interrupt_out_busy = 1;
739 wmb();
741 retval = usb_submit_urb(dev->interrupt_out_urb, GFP_KERNEL);
742 if (retval) {
743 dev->interrupt_out_busy = 0;
744 err("Couldn't submit interrupt_out_urb %d\n", retval);
745 goto unlock_exit;
747 retval = bytes_to_write;
749 unlock_exit:
750 /* unlock the device */
751 mutex_unlock(&dev->mtx);
753 exit:
754 return retval;
757 /* file operations needed when we register this driver */
758 static const struct file_operations usb_tranzport_fops = {
759 .owner = THIS_MODULE,
760 .read = usb_tranzport_read,
761 .write = usb_tranzport_write,
762 .open = usb_tranzport_open,
763 .release = usb_tranzport_release,
764 .poll = usb_tranzport_poll,
768 * usb class driver info in order to get a minor number from the usb core,
769 * and to have the device registered with the driver core
771 static struct usb_class_driver usb_tranzport_class = {
772 .name = "tranzport%d",
773 .fops = &usb_tranzport_fops,
774 .minor_base = USB_TRANZPORT_MINOR_BASE,
778 * usb_tranzport_probe
780 * Called by the usb core when a new device is connected that it thinks
781 * this driver might be interested in.
783 static int usb_tranzport_probe(struct usb_interface *intf,
784 const struct usb_device_id *id) {
785 struct usb_device *udev = interface_to_usbdev(intf);
786 struct usb_tranzport *dev = NULL;
787 struct usb_host_interface *iface_desc;
788 struct usb_endpoint_descriptor *endpoint;
789 int i;
790 int true_size;
791 int retval = -ENOMEM;
793 /* allocate memory for our device state and intialize it */
795 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
796 if (dev == NULL) {
797 dev_err(&intf->dev, "Out of memory\n");
798 goto exit;
800 mutex_init(&dev->mtx);
801 dev->intf = intf;
802 init_waitqueue_head(&dev->read_wait);
803 init_waitqueue_head(&dev->write_wait);
805 iface_desc = intf->cur_altsetting;
807 /* set up the endpoint information */
808 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
809 endpoint = &iface_desc->endpoint[i].desc;
811 if (usb_endpoint_is_int_in(endpoint))
812 dev->interrupt_in_endpoint = endpoint;
814 if (usb_endpoint_is_int_out(endpoint))
815 dev->interrupt_out_endpoint = endpoint;
817 if (dev->interrupt_in_endpoint == NULL) {
818 dev_err(&intf->dev, "Interrupt in endpoint not found\n");
819 goto error;
821 if (dev->interrupt_out_endpoint == NULL)
822 dev_warn(&intf->dev,
823 "Interrupt out endpoint not found"
824 "(using control endpoint instead)\n");
826 dev->interrupt_in_endpoint_size =
827 le16_to_cpu(dev->interrupt_in_endpoint->wMaxPacketSize);
829 if (dev->interrupt_in_endpoint_size != 8)
830 dev_warn(&intf->dev, "Interrupt in endpoint size is not 8!\n");
832 if (ring_buffer_size == 0)
833 ring_buffer_size = RING_BUFFER_SIZE;
834 true_size = min(ring_buffer_size, RING_BUFFER_SIZE);
837 dev->ring_buffer =
838 kmalloc((true_size * sizeof(struct tranzport_cmd)) + 8, GFP_KERNEL);
840 if (!dev->ring_buffer) {
841 dev_err(&intf->dev,
842 "Couldn't allocate ring_buffer size %d\n", true_size);
843 goto error;
845 dev->interrupt_in_buffer =
846 kmalloc(dev->interrupt_in_endpoint_size, GFP_KERNEL);
847 if (!dev->interrupt_in_buffer) {
848 dev_err(&intf->dev, "Couldn't allocate interrupt_in_buffer\n");
849 goto error;
851 dev->interrupt_in_urb = usb_alloc_urb(0, GFP_KERNEL);
852 if (!dev->interrupt_in_urb) {
853 dev_err(&intf->dev, "Couldn't allocate interrupt_in_urb\n");
854 goto error;
856 dev->interrupt_out_endpoint_size =
857 dev->interrupt_out_endpoint ?
858 le16_to_cpu(dev->interrupt_out_endpoint->wMaxPacketSize) :
859 udev->descriptor.bMaxPacketSize0;
861 if (dev->interrupt_out_endpoint_size != 8)
862 dev_warn(&intf->dev,
863 "Interrupt out endpoint size is not 8!)\n");
865 dev->interrupt_out_buffer =
866 kmalloc(write_buffer_size * dev->interrupt_out_endpoint_size,
867 GFP_KERNEL);
868 if (!dev->interrupt_out_buffer) {
869 dev_err(&intf->dev, "Couldn't allocate interrupt_out_buffer\n");
870 goto error;
872 dev->interrupt_out_urb = usb_alloc_urb(0, GFP_KERNEL);
873 if (!dev->interrupt_out_urb) {
874 dev_err(&intf->dev, "Couldn't allocate interrupt_out_urb\n");
875 goto error;
877 dev->interrupt_in_interval =
878 min_interrupt_in_interval >
879 dev->interrupt_in_endpoint->bInterval ? min_interrupt_in_interval
880 : dev->interrupt_in_endpoint->bInterval;
882 if (dev->interrupt_out_endpoint) {
883 dev->interrupt_out_interval =
884 min_interrupt_out_interval >
885 dev->interrupt_out_endpoint->bInterval ?
886 min_interrupt_out_interval :
887 dev->interrupt_out_endpoint->bInterval;
890 /* we can register the device now, as it is ready */
891 usb_set_intfdata(intf, dev);
893 retval = usb_register_dev(intf, &usb_tranzport_class);
894 if (retval) {
895 /* something prevented us from registering this driver */
896 dev_err(&intf->dev,
897 "Not able to get a minor for this device.\n");
898 usb_set_intfdata(intf, NULL);
899 goto error;
902 retval = device_create_file(&intf->dev, &dev_attr_compress_wheel);
903 if (retval)
904 goto error;
905 retval = device_create_file(&intf->dev, &dev_attr_enable);
906 if (retval)
907 goto error;
908 retval = device_create_file(&intf->dev, &dev_attr_offline);
909 if (retval)
910 goto error;
912 /* let the user know what node this device is now attached to */
913 dev_info(&intf->dev,
914 "Tranzport Device #%d now attached to major %d minor %d\n",
915 (intf->minor - USB_TRANZPORT_MINOR_BASE), USB_MAJOR,
916 intf->minor);
918 exit:
919 return retval;
921 error:
922 usb_tranzport_delete(dev);
923 return retval;
927 * usb_tranzport_disconnect
929 * Called by the usb core when the device is removed from the system.
931 static void usb_tranzport_disconnect(struct usb_interface *intf)
933 struct usb_tranzport *dev;
934 int minor;
935 mutex_lock(&disconnect_mutex);
936 dev = usb_get_intfdata(intf);
937 usb_set_intfdata(intf, NULL);
938 mutex_lock(&dev->mtx);
939 minor = intf->minor;
940 /* give back our minor */
941 usb_deregister_dev(intf, &usb_tranzport_class);
943 /* if the device is not opened, then we clean up right now */
944 if (!dev->open_count) {
945 mutex_unlock(&dev->mtx);
946 usb_tranzport_delete(dev);
947 } else {
948 dev->intf = NULL;
949 mutex_unlock(&dev->mtx);
952 mutex_unlock(&disconnect_mutex);
954 dev_info(&intf->dev, "Tranzport Surface #%d now disconnected\n",
955 (minor - USB_TRANZPORT_MINOR_BASE));
958 /* usb specific object needed to register this driver with the usb subsystem */
959 static struct usb_driver usb_tranzport_driver = {
960 .name = "tranzport",
961 .probe = usb_tranzport_probe,
962 .disconnect = usb_tranzport_disconnect,
963 .id_table = usb_tranzport_table,
967 * usb_tranzport_init
969 static int __init usb_tranzport_init(void)
971 int retval;
973 /* register this driver with the USB subsystem */
974 retval = usb_register(&usb_tranzport_driver);
975 if (retval)
976 err("usb_register failed for the " __FILE__
977 " driver. Error number %d\n", retval);
978 return retval;
981 * usb_tranzport_exit
984 static void __exit usb_tranzport_exit(void)
986 /* deregister this driver with the USB subsystem */
987 usb_deregister(&usb_tranzport_driver);
990 module_init(usb_tranzport_init);
991 module_exit(usb_tranzport_exit);