OMAP2/3: Add omap_type() for determining GP/EMU/HS
[linux-ginger.git] / drivers / staging / frontier / tranzport.c
blob274b82bd7863e301d93bf02a755b5d3aea02ed67
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>
41 #include <linux/version.h>
43 #include <linux/uaccess.h>
44 #include <linux/input.h>
45 #include <linux/usb.h>
46 #include <linux/poll.h>
48 /* Define these values to match your devices */
49 #define VENDOR_ID 0x165b
50 #define PRODUCT_ID 0x8101
52 #ifdef CONFIG_USB_DYNAMIC_MINORS
53 #define USB_TRANZPORT_MINOR_BASE 0
54 #else /* FIXME 177- is the another driver's minor - apply for a minor soon */
55 #define USB_TRANZPORT_MINOR_BASE 177
56 #endif
58 /* table of devices that work with this driver */
59 static struct usb_device_id usb_tranzport_table[] = {
60 {USB_DEVICE(VENDOR_ID, PRODUCT_ID)},
61 {} /* Terminating entry */
64 MODULE_DEVICE_TABLE(usb, usb_tranzport_table);
65 MODULE_VERSION("0.35");
66 MODULE_AUTHOR("Mike Taht <m@taht.net>");
67 MODULE_DESCRIPTION("Tranzport USB Driver");
68 MODULE_LICENSE("GPL");
69 MODULE_SUPPORTED_DEVICE("Frontier Designs Tranzport Control Surface");
71 #define SUPPRESS_EXTRA_OFFLINE_EVENTS 1
72 #define COMPRESS_WHEEL_EVENTS 1
73 #define BUFFERED_READS 1
74 #define RING_BUFFER_SIZE 1000
75 #define WRITE_BUFFER_SIZE 34
76 #define TRANZPORT_USB_TIMEOUT 10
77 #define TRANZPORT_DEBUG 0
79 static int debug = TRANZPORT_DEBUG;
81 /* Use our own dbg macro */
82 #define dbg_info(dev, format, arg...) do \
83 { if (debug) dev_info(dev , format , ## arg); } while (0)
85 /* Module parameters */
87 module_param(debug, int, S_IRUGO | S_IWUSR);
88 MODULE_PARM_DESC(debug, "Debug enabled or not");
90 /* All interrupt in transfers are collected in a ring buffer to
91 * avoid racing conditions and get better performance of the driver.
94 static int ring_buffer_size = RING_BUFFER_SIZE;
96 module_param(ring_buffer_size, int, S_IRUGO);
97 MODULE_PARM_DESC(ring_buffer_size, "Read ring buffer size in reports");
99 /* The write_buffer can one day contain more than one interrupt out transfer.
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");
106 * Increase the interval for debugging purposes.
107 * or set to 1 to use the standard interval from the endpoint descriptors.
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");
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");
120 struct tranzport_cmd {
121 unsigned char cmd[8];
124 /* Structure to hold all of our device specific stuff */
126 struct usb_tranzport {
127 struct semaphore sem; /* 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;
149 /* Sysfs support */
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 */
156 /* prevent races between open() and disconnect() */
157 static DEFINE_MUTEX(disconnect_mutex);
159 static struct usb_driver usb_tranzport_driver;
162 * usb_tranzport_abort_transfers
163 * aborts transfers and frees associated data structures
165 static void usb_tranzport_abort_transfers(struct usb_tranzport *dev)
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);
173 if (dev->interrupt_out_busy)
174 if (dev->intf)
175 usb_kill_urb(dev->interrupt_out_urb);
178 #define show_int(value) \
179 static ssize_t show_##value(struct device *dev, \
180 struct device_attribute *attr, char *buf) \
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); \
186 static DEVICE_ATTR(value, S_IRUGO, show_##value, NULL);
188 #define show_set_int(value) \
189 static ssize_t show_##value(struct device *dev, \
190 struct device_attribute *attr, char *buf) \
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); \
196 static ssize_t set_##value(struct device *dev, \
197 struct device_attribute *attr, \
198 const char *buf, size_t count) \
200 struct usb_interface *intf = to_usb_interface(dev); \
201 struct usb_tranzport *t = usb_get_intfdata(intf); \
202 int temp = simple_strtoul(buf, NULL, 10); \
203 t->value = temp; \
204 return count; \
206 static DEVICE_ATTR(value, S_IWUGO | S_IRUGO, show_##value, set_##value);
208 show_int(enable);
209 show_int(offline);
210 show_set_int(compress_wheel);
213 * usb_tranzport_delete
215 static void usb_tranzport_delete(struct usb_tranzport *dev)
217 usb_tranzport_abort_transfers(dev);
218 if (dev->intf != NULL) {
219 device_remove_file(&dev->intf->dev, &dev_attr_enable);
220 device_remove_file(&dev->intf->dev, &dev_attr_offline);
221 device_remove_file(&dev->intf->dev, &dev_attr_compress_wheel);
224 /* free data structures */
225 usb_free_urb(dev->interrupt_in_urb);
226 usb_free_urb(dev->interrupt_out_urb);
227 kfree(dev->ring_buffer);
228 kfree(dev->interrupt_in_buffer);
229 kfree(dev->interrupt_out_buffer);
230 kfree(dev);
234 * usb_tranzport_interrupt_in_callback
237 static void usb_tranzport_interrupt_in_callback(struct urb *urb)
239 struct usb_tranzport *dev = urb->context;
240 unsigned int next_ring_head;
241 int retval = -1;
243 if (urb->status) {
244 if (urb->status == -ENOENT ||
245 urb->status == -ECONNRESET ||
246 urb->status == -ESHUTDOWN) {
247 goto exit;
248 } else {
249 dbg_info(&dev->intf->dev,
250 "%s: nonzero status received: %d\n",
251 __func__, urb->status);
252 goto resubmit; /* maybe we can recover */
256 if (urb->actual_length != 8) {
257 dev_warn(&dev->intf->dev,
258 "Urb length was %d bytes!!"
259 "Do something intelligent \n",
260 urb->actual_length);
261 } else {
262 dbg_info(&dev->intf->dev,
263 "%s: received: %02x%02x%02x%02x%02x%02x%02x%02x\n",
264 __func__, dev->interrupt_in_buffer[0],
265 dev->interrupt_in_buffer[1],
266 dev->interrupt_in_buffer[2],
267 dev->interrupt_in_buffer[3],
268 dev->interrupt_in_buffer[4],
269 dev->interrupt_in_buffer[5],
270 dev->interrupt_in_buffer[6],
271 dev->interrupt_in_buffer[7]);
272 #if SUPPRESS_EXTRA_OFFLINE_EVENTS
273 if (dev->offline == 2 && dev->interrupt_in_buffer[1] == 0xff)
274 goto resubmit;
275 if (dev->offline == 1 && dev->interrupt_in_buffer[1] == 0xff) {
276 dev->offline = 2;
277 goto resubmit;
280 /* Always pass one offline event up the stack */
281 if (dev->offline > 0 && dev->interrupt_in_buffer[1] != 0xff)
282 dev->offline = 0;
283 if (dev->offline == 0 && dev->interrupt_in_buffer[1] == 0xff)
284 dev->offline = 1;
286 #endif /* SUPPRESS_EXTRA_OFFLINE_EVENTS */
287 dbg_info(&dev->intf->dev, "%s: head, tail are %x, %x\n",
288 __func__, dev->ring_head, dev->ring_tail);
290 next_ring_head = (dev->ring_head + 1) % ring_buffer_size;
292 if (next_ring_head != dev->ring_tail) {
293 memcpy(&((*dev->ring_buffer)[dev->ring_head]),
294 dev->interrupt_in_buffer, urb->actual_length);
295 dev->ring_head = next_ring_head;
296 retval = 0;
297 memset(dev->interrupt_in_buffer, 0, urb->actual_length);
298 } else {
299 dev_warn(&dev->intf->dev,
300 "Ring buffer overflow, %d bytes dropped\n",
301 urb->actual_length);
302 memset(dev->interrupt_in_buffer, 0, urb->actual_length);
306 resubmit:
307 /* resubmit if we're still running */
308 if (dev->interrupt_in_running && dev->intf) {
309 retval = usb_submit_urb(dev->interrupt_in_urb, GFP_ATOMIC);
310 if (retval)
311 dev_err(&dev->intf->dev,
312 "usb_submit_urb failed (%d)\n", retval);
315 exit:
316 dev->interrupt_in_done = 1;
317 wake_up_interruptible(&dev->read_wait);
321 * usb_tranzport_interrupt_out_callback
323 static void usb_tranzport_interrupt_out_callback(struct urb *urb)
325 struct usb_tranzport *dev = urb->context;
326 /* sync/async unlink faults aren't errors */
327 if (urb->status && !(urb->status == -ENOENT ||
328 urb->status == -ECONNRESET ||
329 urb->status == -ESHUTDOWN))
330 dbg_info(&dev->intf->dev,
331 "%s - nonzero write interrupt status received: %d\n",
332 __func__, urb->status);
334 dev->interrupt_out_busy = 0;
335 wake_up_interruptible(&dev->write_wait);
338 * usb_tranzport_open
340 static int usb_tranzport_open(struct inode *inode, struct file *file)
342 struct usb_tranzport *dev;
343 int subminor;
344 int retval = 0;
345 struct usb_interface *interface;
347 nonseekable_open(inode, file);
348 subminor = iminor(inode);
350 mutex_lock(&disconnect_mutex);
352 interface = usb_find_interface(&usb_tranzport_driver, subminor);
354 if (!interface) {
355 err("%s - error, can't find device for minor %d\n",
356 __func__, subminor);
357 retval = -ENODEV;
358 goto unlock_disconnect_exit;
361 dev = usb_get_intfdata(interface);
363 if (!dev) {
364 retval = -ENODEV;
365 goto unlock_disconnect_exit;
368 /* lock this device */
369 if (down_interruptible(&dev->sem)) {
370 retval = -ERESTARTSYS;
371 goto unlock_disconnect_exit;
374 /* allow opening only once */
375 if (dev->open_count) {
376 retval = -EBUSY;
377 goto unlock_exit;
379 dev->open_count = 1;
381 /* initialize in direction */
382 dev->ring_head = 0;
383 dev->ring_tail = 0;
384 usb_fill_int_urb(dev->interrupt_in_urb,
385 interface_to_usbdev(interface),
386 usb_rcvintpipe(interface_to_usbdev(interface),
387 dev->interrupt_in_endpoint->
388 bEndpointAddress),
389 dev->interrupt_in_buffer,
390 dev->interrupt_in_endpoint_size,
391 usb_tranzport_interrupt_in_callback, dev,
392 dev->interrupt_in_interval);
394 dev->interrupt_in_running = 1;
395 dev->interrupt_in_done = 0;
396 dev->enable = 1;
397 dev->offline = 0;
398 dev->compress_wheel = 1;
400 retval = usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL);
401 if (retval) {
402 dev_err(&interface->dev,
403 "Couldn't submit interrupt_in_urb %d\n", retval);
404 dev->interrupt_in_running = 0;
405 dev->open_count = 0;
406 goto unlock_exit;
409 /* save device in the file's private structure */
410 file->private_data = dev;
412 unlock_exit:
413 up(&dev->sem);
415 unlock_disconnect_exit:
416 mutex_unlock(&disconnect_mutex);
418 return retval;
422 * usb_tranzport_release
424 static int usb_tranzport_release(struct inode *inode, struct file *file)
426 struct usb_tranzport *dev;
427 int retval = 0;
429 dev = file->private_data;
431 if (dev == NULL) {
432 retval = -ENODEV;
433 goto exit;
436 if (down_interruptible(&dev->sem)) {
437 retval = -ERESTARTSYS;
438 goto exit;
441 if (dev->open_count != 1) {
442 retval = -ENODEV;
443 goto unlock_exit;
446 if (dev->intf == NULL) {
447 /* the device was unplugged before the file was released */
448 up(&dev->sem);
449 /* unlock here as usb_tranzport_delete frees dev */
450 usb_tranzport_delete(dev);
451 retval = -ENODEV;
452 goto exit;
455 /* wait until write transfer is finished */
456 if (dev->interrupt_out_busy)
457 wait_event_interruptible_timeout(dev->write_wait,
458 !dev->interrupt_out_busy,
459 2 * HZ);
460 usb_tranzport_abort_transfers(dev);
461 dev->open_count = 0;
463 unlock_exit:
464 up(&dev->sem);
466 exit:
467 return retval;
471 * usb_tranzport_poll
473 static unsigned int usb_tranzport_poll(struct file *file, poll_table * wait)
475 struct usb_tranzport *dev;
476 unsigned int mask = 0;
477 dev = file->private_data;
478 poll_wait(file, &dev->read_wait, wait);
479 poll_wait(file, &dev->write_wait, wait);
480 if (dev->ring_head != dev->ring_tail)
481 mask |= POLLIN | POLLRDNORM;
482 if (!dev->interrupt_out_busy)
483 mask |= POLLOUT | POLLWRNORM;
484 return mask;
487 * usb_tranzport_read
490 static ssize_t usb_tranzport_read(struct file *file, char __user *buffer,
491 size_t count, loff_t *ppos)
493 struct usb_tranzport *dev;
494 int retval = 0;
495 #if BUFFERED_READS
496 int c = 0;
497 #endif
498 #if COMPRESS_WHEEL_EVENTS
499 signed char oldwheel;
500 signed char newwheel;
501 int cancompress = 1;
502 int next_tail;
503 #endif
505 /* do I have such a thing as a null event? */
507 dev = file->private_data;
509 /* verify that we actually have some data to read */
510 if (count == 0)
511 goto exit;
513 /* lock this object */
514 if (down_interruptible(&dev->sem)) {
515 retval = -ERESTARTSYS;
516 goto exit;
519 /* verify that the device wasn't unplugged */ if (dev->intf == NULL) {
520 retval = -ENODEV;
521 err("No device or device unplugged %d\n", retval);
522 goto unlock_exit;
525 while (dev->ring_head == dev->ring_tail) {
527 if (file->f_flags & O_NONBLOCK) {
528 retval = -EAGAIN;
529 goto unlock_exit;
531 /* tiny race - FIXME: make atomic? */
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 /* if both are wheel events, and
565 no buttons have changes (FIXME, do I have to check?),
566 and we are the same sign, we can compress +- 7F
568 dbg_info(&dev->intf->dev,
569 "%s: trying to compress: "
570 "%02x%02x%02x%02x%02x%02x%02x%02x\n",
571 __func__,
572 (*dev->ring_buffer)[dev->ring_tail].cmd[0],
573 (*dev->ring_buffer)[dev->ring_tail].cmd[1],
574 (*dev->ring_buffer)[dev->ring_tail].cmd[2],
575 (*dev->ring_buffer)[dev->ring_tail].cmd[3],
576 (*dev->ring_buffer)[dev->ring_tail].cmd[4],
577 (*dev->ring_buffer)[dev->ring_tail].cmd[5],
578 (*dev->ring_buffer)[dev->ring_tail].cmd[6],
579 (*dev->ring_buffer)[dev->ring_tail].cmd[7]);
581 if (((*dev->ring_buffer)[dev->ring_tail].cmd[6] != 0 &&
582 (*dev->ring_buffer)[next_tail].cmd[6] != 0) &&
583 ((newwheel > 0 && oldwheel > 0) ||
584 (newwheel < 0 && oldwheel < 0)) &&
585 ((*dev->ring_buffer)[dev->ring_tail].cmd[2] ==
586 (*dev->ring_buffer)[next_tail].cmd[2]) &&
587 ((*dev->ring_buffer)[dev->ring_tail].cmd[3] ==
588 (*dev->ring_buffer)[next_tail].cmd[3]) &&
589 ((*dev->ring_buffer)[dev->ring_tail].cmd[4] ==
590 (*dev->ring_buffer)[next_tail].cmd[4]) &&
591 ((*dev->ring_buffer)[dev->ring_tail].cmd[5] ==
592 (*dev->ring_buffer)[next_tail].cmd[5])) {
593 dbg_info(&dev->intf->dev,
594 "%s: should compress: "
595 "%02x%02x%02x%02x%02x%02x%02x%02x\n",
596 __func__,
597 (*dev->ring_buffer)[dev->ring_tail].
598 cmd[0],
599 (*dev->ring_buffer)[dev->ring_tail].
600 cmd[1],
601 (*dev->ring_buffer)[dev->ring_tail].
602 cmd[2],
603 (*dev->ring_buffer)[dev->ring_tail].
604 cmd[3],
605 (*dev->ring_buffer)[dev->ring_tail].
606 cmd[4],
607 (*dev->ring_buffer)[dev->ring_tail].
608 cmd[5],
609 (*dev->ring_buffer)[dev->ring_tail].
610 cmd[6],
611 (*dev->ring_buffer)[dev->ring_tail].
612 cmd[7]);
613 newwheel += oldwheel;
614 if (oldwheel > 0 && !(newwheel > 0)) {
615 newwheel = 0x7f;
616 cancompress = 0;
618 if (oldwheel < 0 && !(newwheel < 0)) {
619 newwheel = 0x80;
620 cancompress = 0;
623 (*dev->ring_buffer)[next_tail].cmd[6] =
624 newwheel;
625 dev->ring_tail = next_tail;
626 next_tail =
627 (dev->ring_tail + 1) % ring_buffer_size;
628 } else {
629 cancompress = 0;
632 #endif /* COMPRESS_WHEEL_EVENTS */
633 if (copy_to_user(
634 &buffer[c],
635 &(*dev->ring_buffer)[dev->ring_tail], 8)) {
636 retval = -EFAULT;
637 goto unlock_exit;
639 dev->ring_tail = (dev->ring_tail + 1) % ring_buffer_size;
640 c += 8;
641 dbg_info(&dev->intf->dev,
642 "%s: head, tail are %x, %x\n",
643 __func__, dev->ring_head, dev->ring_tail);
645 retval = c;
647 #else
648 /* if (copy_to_user(buffer, &(*dev->ring_buffer)[dev->ring_tail], 8)) { */
649 retval = -EFAULT;
650 goto unlock_exit;
653 dev->ring_tail = (dev->ring_tail + 1) % ring_buffer_size;
654 dbg_info(&dev->intf->dev, "%s: head, tail are %x, %x\n",
655 __func__, dev->ring_head, dev->ring_tail);
657 retval = 8;
658 #endif /* BUFFERED_READS */
660 unlock_exit:
661 /* unlock the device */
662 up(&dev->sem);
664 exit:
665 return retval;
669 * usb_tranzport_write
671 static ssize_t usb_tranzport_write(struct file *file,
672 const char __user *buffer, size_t count,
673 loff_t *ppos)
675 struct usb_tranzport *dev;
676 size_t bytes_to_write;
677 int retval = 0;
679 dev = file->private_data;
681 /* verify that we actually have some data to write */
682 if (count == 0)
683 goto exit;
685 /* lock this object */
686 if (down_interruptible(&dev->sem)) {
687 retval = -ERESTARTSYS;
688 goto exit;
690 /* verify that the device wasn't unplugged */
691 if (dev->intf == NULL) {
692 retval = -ENODEV;
693 err("No device or device unplugged %d\n", retval);
694 goto unlock_exit;
697 /* wait until previous transfer is finished */
698 if (dev->interrupt_out_busy) {
699 if (file->f_flags & O_NONBLOCK) {
700 retval = -EAGAIN;
701 goto unlock_exit;
703 retval = wait_event_interruptible(dev->write_wait,
704 !dev->interrupt_out_busy);
705 if (retval < 0)
706 goto unlock_exit;
709 /* write the data into interrupt_out_buffer from userspace */
710 bytes_to_write = min(count,
711 write_buffer_size *
712 dev->interrupt_out_endpoint_size);
713 if (bytes_to_write < count)
714 dev_warn(&dev->intf->dev,
715 "Write buffer overflow, %zd bytes dropped\n",
716 count - bytes_to_write);
718 dbg_info(&dev->intf->dev,
719 "%s: count = %zd, bytes_to_write = %zd\n", __func__,
720 count, bytes_to_write);
722 if (copy_from_user(dev->interrupt_out_buffer, buffer, bytes_to_write)) {
723 retval = -EFAULT;
724 goto unlock_exit;
727 if (dev->interrupt_out_endpoint == NULL) {
728 err("Endpoint should not be be null! \n");
729 goto unlock_exit;
732 /* send off the urb */
733 usb_fill_int_urb(dev->interrupt_out_urb,
734 interface_to_usbdev(dev->intf),
735 usb_sndintpipe(interface_to_usbdev(dev->intf),
736 dev->interrupt_out_endpoint->
737 bEndpointAddress),
738 dev->interrupt_out_buffer, bytes_to_write,
739 usb_tranzport_interrupt_out_callback, dev,
740 dev->interrupt_out_interval);
742 dev->interrupt_out_busy = 1;
743 wmb();
745 retval = usb_submit_urb(dev->interrupt_out_urb, GFP_KERNEL);
746 if (retval) {
747 dev->interrupt_out_busy = 0;
748 err("Couldn't submit interrupt_out_urb %d\n", retval);
749 goto unlock_exit;
751 retval = bytes_to_write;
753 unlock_exit:
754 /* unlock the device */
755 up(&dev->sem);
757 exit:
758 return retval;
761 /* file operations needed when we register this driver */
762 static const struct file_operations usb_tranzport_fops = {
763 .owner = THIS_MODULE,
764 .read = usb_tranzport_read,
765 .write = usb_tranzport_write,
766 .open = usb_tranzport_open,
767 .release = usb_tranzport_release,
768 .poll = usb_tranzport_poll,
772 * usb class driver info in order to get a minor number from the usb core,
773 * and to have the device registered with the driver core
775 static struct usb_class_driver usb_tranzport_class = {
776 .name = "tranzport%d",
777 .fops = &usb_tranzport_fops,
778 .minor_base = USB_TRANZPORT_MINOR_BASE,
782 * usb_tranzport_probe
784 * Called by the usb core when a new device is connected that it thinks
785 * this driver might be interested in.
787 static int usb_tranzport_probe(struct usb_interface *intf,
788 const struct usb_device_id *id) {
789 struct usb_device *udev = interface_to_usbdev(intf);
790 struct usb_tranzport *dev = NULL;
791 struct usb_host_interface *iface_desc;
792 struct usb_endpoint_descriptor *endpoint;
793 int i;
794 int true_size;
795 int retval = -ENOMEM;
797 /* allocate memory for our device state and intialize it */
799 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
800 if (dev == NULL) {
801 dev_err(&intf->dev, "Out of memory\n");
802 goto exit;
804 init_MUTEX(&dev->sem);
805 dev->intf = intf;
806 init_waitqueue_head(&dev->read_wait);
807 init_waitqueue_head(&dev->write_wait);
809 iface_desc = intf->cur_altsetting;
811 /* set up the endpoint information */
812 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
813 endpoint = &iface_desc->endpoint[i].desc;
815 if (usb_endpoint_is_int_in(endpoint))
816 dev->interrupt_in_endpoint = endpoint;
818 if (usb_endpoint_is_int_out(endpoint))
819 dev->interrupt_out_endpoint = endpoint;
821 if (dev->interrupt_in_endpoint == NULL) {
822 dev_err(&intf->dev, "Interrupt in endpoint not found\n");
823 goto error;
825 if (dev->interrupt_out_endpoint == NULL)
826 dev_warn(&intf->dev,
827 "Interrupt out endpoint not found"
828 "(using control endpoint instead)\n");
830 dev->interrupt_in_endpoint_size =
831 le16_to_cpu(dev->interrupt_in_endpoint->wMaxPacketSize);
833 if (dev->interrupt_in_endpoint_size != 8)
834 dev_warn(&intf->dev, "Interrupt in endpoint size is not 8!\n");
836 if (ring_buffer_size == 0)
837 ring_buffer_size = RING_BUFFER_SIZE;
838 true_size = min(ring_buffer_size, RING_BUFFER_SIZE);
840 /* FIXME - there are more usb_alloc routines for dma correctness.
841 Needed? */
843 dev->ring_buffer =
844 kmalloc((true_size * sizeof(struct tranzport_cmd)) + 8, GFP_KERNEL);
846 if (!dev->ring_buffer) {
847 dev_err(&intf->dev,
848 "Couldn't allocate ring_buffer size %d\n", true_size);
849 goto error;
851 dev->interrupt_in_buffer =
852 kmalloc(dev->interrupt_in_endpoint_size, GFP_KERNEL);
853 if (!dev->interrupt_in_buffer) {
854 dev_err(&intf->dev, "Couldn't allocate interrupt_in_buffer\n");
855 goto error;
857 dev->interrupt_in_urb = usb_alloc_urb(0, GFP_KERNEL);
858 if (!dev->interrupt_in_urb) {
859 dev_err(&intf->dev, "Couldn't allocate interrupt_in_urb\n");
860 goto error;
862 dev->interrupt_out_endpoint_size =
863 dev->interrupt_out_endpoint ?
864 le16_to_cpu(dev->interrupt_out_endpoint->wMaxPacketSize) :
865 udev->descriptor.bMaxPacketSize0;
867 if (dev->interrupt_out_endpoint_size != 8)
868 dev_warn(&intf->dev,
869 "Interrupt out endpoint size is not 8!)\n");
871 dev->interrupt_out_buffer =
872 kmalloc(write_buffer_size * dev->interrupt_out_endpoint_size,
873 GFP_KERNEL);
874 if (!dev->interrupt_out_buffer) {
875 dev_err(&intf->dev, "Couldn't allocate interrupt_out_buffer\n");
876 goto error;
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;
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;
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;
896 /* we can register the device now, as it is ready */
897 usb_set_intfdata(intf, dev);
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;
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;
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);
924 exit:
925 return retval;
927 error:
928 usb_tranzport_delete(dev);
929 return retval;
933 * usb_tranzport_disconnect
935 * Called by the usb core when the device is removed from the system.
937 static void usb_tranzport_disconnect(struct usb_interface *intf)
939 struct usb_tranzport *dev;
940 int minor;
941 mutex_lock(&disconnect_mutex);
942 dev = usb_get_intfdata(intf);
943 usb_set_intfdata(intf, NULL);
944 down(&dev->sem);
945 minor = intf->minor;
946 /* give back our minor */
947 usb_deregister_dev(intf, &usb_tranzport_class);
949 /* if the device is not opened, then we clean up right now */
950 if (!dev->open_count) {
951 up(&dev->sem);
952 usb_tranzport_delete(dev);
953 } else {
954 dev->intf = NULL;
955 up(&dev->sem);
958 mutex_unlock(&disconnect_mutex);
960 dev_info(&intf->dev, "Tranzport Surface #%d now disconnected\n",
961 (minor - USB_TRANZPORT_MINOR_BASE));
964 /* usb specific object needed to register this driver with the usb subsystem */
965 static struct usb_driver usb_tranzport_driver = {
966 .name = "tranzport",
967 .probe = usb_tranzport_probe,
968 .disconnect = usb_tranzport_disconnect,
969 .id_table = usb_tranzport_table,
973 * usb_tranzport_init
975 static int __init usb_tranzport_init(void)
977 int retval;
979 /* register this driver with the USB subsystem */
980 retval = usb_register(&usb_tranzport_driver);
981 if (retval)
982 err("usb_register failed for the " __FILE__
983 " driver. Error number %d\n", retval);
984 return retval;
987 * usb_tranzport_exit
990 static void __exit usb_tranzport_exit(void)
992 /* deregister this driver with the USB subsystem */
993 usb_deregister(&usb_tranzport_driver);
996 module_init(usb_tranzport_init);
997 module_exit(usb_tranzport_exit);