fix a kmap leak in virtio_console
[linux/fpc-iii.git] / drivers / staging / frontier / alphatrack.c
blobedd5cef300d034a5709542058ca9c4e1159125e3
1 /*
2 * Frontier Designs Alphatrack 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.
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.
28 * Note: this currently uses a dumb ringbuffer for reads and writes.
29 * A more optimal driver would cache and kill off outstanding urbs that are
30 * now invalid, and ignore ones that already were in the queue but valid
31 * as we only have 30 commands for the alphatrack. In particular this is
32 * key for getting lights to flash in time as otherwise many commands
33 * can be buffered up before the light change makes it to the interface.
36 #include <linux/kernel.h>
37 #include <linux/errno.h>
38 #include <linux/slab.h>
39 #include <linux/module.h>
40 #include <linux/kobject.h>
41 #include <linux/mutex.h>
43 #include <linux/uaccess.h>
44 #include <linux/input.h>
45 #include <linux/usb.h>
46 #include <linux/poll.h>
48 #include "alphatrack.h"
50 #define VENDOR_ID 0x165b
51 #define PRODUCT_ID 0xfad1
53 #ifdef CONFIG_USB_DYNAMIC_MINORS
54 #define USB_ALPHATRACK_MINOR_BASE 0
55 #else
56 /* FIXME 176 - is another driver's minor - apply for that */
57 #define USB_ALPHATRACK_MINOR_BASE 176
58 #endif
60 /* table of devices that work with this driver */
61 static const struct usb_device_id usb_alphatrack_table[] = {
62 {USB_DEVICE(VENDOR_ID, PRODUCT_ID)},
63 {} /* Terminating entry */
66 MODULE_DEVICE_TABLE(usb, usb_alphatrack_table);
67 MODULE_VERSION("0.41");
68 MODULE_AUTHOR("Mike Taht <m@taht.net>");
69 MODULE_DESCRIPTION("Alphatrack USB Driver");
70 MODULE_LICENSE("GPL");
71 MODULE_SUPPORTED_DEVICE("Frontier Designs Alphatrack Control Surface");
73 /* These aren't done yet */
75 #define SUPPRESS_EXTRA_ONLINE_EVENTS 0
76 #define BUFFERED_WRITES 0
77 #define SUPPRESS_EXTRA_OFFLINE_EVENTS 0
78 #define COMPRESS_FADER_EVENTS 0
80 #define BUFFERED_READS 1
81 #define RING_BUFFER_SIZE 512
82 #define WRITE_BUFFER_SIZE 34
83 #define ALPHATRACK_USB_TIMEOUT 10
84 #define OUTPUT_CMD_SIZE 8
85 #define INPUT_CMD_SIZE 12
86 #define ALPHATRACK_DEBUG 0
88 static int debug = ALPHATRACK_DEBUG;
90 /* Use our own dbg macro */
91 #define dbg_info(dev, format, arg...) do \
92 { if (debug) dev_info(dev , format , ## arg); } while (0)
94 #define alphatrack_ocmd_info(dev, cmd, format, arg...)
96 #define alphatrack_icmd_info(dev, cmd, format, arg...)
98 /* Module parameters */
100 module_param(debug, int, S_IRUGO | S_IWUSR);
101 MODULE_PARM_DESC(debug, "Debug enabled or not");
104 * All interrupt in transfers are collected in a ring buffer to
105 * avoid racing conditions and get better performance of the driver.
108 static int ring_buffer_size = RING_BUFFER_SIZE;
110 module_param(ring_buffer_size, int, S_IRUGO);
111 MODULE_PARM_DESC(ring_buffer_size, "Read ring buffer size");
113 /* The write_buffer can one day contain more than one interrupt out transfer.*/
115 static int write_buffer_size = WRITE_BUFFER_SIZE;
116 module_param(write_buffer_size, int, S_IRUGO);
117 MODULE_PARM_DESC(write_buffer_size, "Write buffer size");
120 * Increase the interval for debugging purposes.
121 * or set to 1 to use the standard interval from the endpoint descriptors.
124 static int min_interrupt_in_interval = ALPHATRACK_USB_TIMEOUT;
125 module_param(min_interrupt_in_interval, int, 0);
126 MODULE_PARM_DESC(min_interrupt_in_interval,
127 "Minimum interrupt in interval in ms");
129 static int min_interrupt_out_interval = ALPHATRACK_USB_TIMEOUT;
130 module_param(min_interrupt_out_interval, int, 0);
131 MODULE_PARM_DESC(min_interrupt_out_interval,
132 "Minimum interrupt out interval in ms");
134 /* Structure to hold all of our device specific stuff */
136 struct usb_alphatrack {
137 struct mutex mtx; /* locks this structure */
138 struct usb_interface *intf; /* save off the usb interface pointer */
139 int open_count; /* number of times this port has been opened */
141 /* make gcc happy */
142 struct alphatrack_icmd (*ring_buffer)[RING_BUFFER_SIZE];
143 struct alphatrack_ocmd (*write_buffer)[WRITE_BUFFER_SIZE];
144 unsigned int ring_head;
145 unsigned int ring_tail;
147 wait_queue_head_t read_wait;
148 wait_queue_head_t write_wait;
150 unsigned char *interrupt_in_buffer;
151 unsigned char *oldi_buffer;
152 struct usb_endpoint_descriptor *interrupt_in_endpoint;
153 struct urb *interrupt_in_urb;
154 int interrupt_in_interval;
155 size_t interrupt_in_endpoint_size;
156 int interrupt_in_running;
157 int interrupt_in_done;
159 char *interrupt_out_buffer;
160 struct usb_endpoint_descriptor *interrupt_out_endpoint;
161 struct urb *interrupt_out_urb;
162 int interrupt_out_interval;
163 size_t interrupt_out_endpoint_size;
164 int interrupt_out_busy;
166 atomic_t writes_pending;
167 int event; /* alternate interface to events */
168 int fader; /* 10 bits */
169 int lights; /* 23 bits */
170 unsigned char dump_state; /* 0 if disabled 1 if enabled */
171 unsigned char enable; /* 0 if disabled 1 if enabled */
172 unsigned char offline; /* if the device is out of range or asleep */
173 unsigned char verbose; /* be verbose in error reporting */
174 unsigned char last_cmd[OUTPUT_CMD_SIZE];
175 unsigned char screen[32];
178 /* prevent races between open() and disconnect() */
179 static DEFINE_MUTEX(disconnect_mutex);
181 /* forward declaration */
183 static struct usb_driver usb_alphatrack_driver;
186 * usb_alphatrack_abort_transfers
187 * aborts transfers and frees associated data structures
189 static void usb_alphatrack_abort_transfers(struct usb_alphatrack *dev)
191 /* shutdown transfer */
192 if (dev->interrupt_in_running) {
193 dev->interrupt_in_running = 0;
194 if (dev->intf)
195 usb_kill_urb(dev->interrupt_in_urb);
197 if (dev->interrupt_out_busy)
198 if (dev->intf)
199 usb_kill_urb(dev->interrupt_out_urb);
202 /** usb_alphatrack_delete */
203 static void usb_alphatrack_delete(struct usb_alphatrack *dev)
205 usb_alphatrack_abort_transfers(dev);
206 usb_free_urb(dev->interrupt_in_urb);
207 usb_free_urb(dev->interrupt_out_urb);
208 kfree(dev->ring_buffer);
209 kfree(dev->interrupt_in_buffer);
210 kfree(dev->interrupt_out_buffer);
211 kfree(dev); /* fixme oldi_buffer */
214 /** usb_alphatrack_interrupt_in_callback */
216 static void usb_alphatrack_interrupt_in_callback(struct urb *urb)
218 struct usb_alphatrack *dev = urb->context;
219 unsigned int next_ring_head;
220 int retval = -1;
222 if (urb->status) {
223 if (urb->status == -ENOENT ||
224 urb->status == -ECONNRESET || urb->status == -ESHUTDOWN) {
225 goto exit;
226 } else {
227 dbg_info(&dev->intf->dev,
228 "%s: nonzero status received: %d\n", __func__,
229 urb->status);
230 goto resubmit; /* maybe we can recover */
234 if (urb->actual_length != INPUT_CMD_SIZE) {
235 dev_warn(&dev->intf->dev,
236 "Urb length was %d bytes!!"
237 "Do something intelligent\n", urb->actual_length);
238 } else {
239 alphatrack_ocmd_info(&dev->intf->dev,
240 &(*dev->ring_buffer)[dev->ring_tail].cmd,
241 "%s", "bla");
242 if (memcmp
243 (dev->interrupt_in_buffer, dev->oldi_buffer,
244 INPUT_CMD_SIZE) == 0) {
245 goto resubmit;
247 memcpy(dev->oldi_buffer, dev->interrupt_in_buffer,
248 INPUT_CMD_SIZE);
250 #if SUPPRESS_EXTRA_OFFLINE_EVENTS
251 if (dev->offline == 2 && dev->interrupt_in_buffer[1] == 0xff)
252 goto resubmit;
253 if (dev->offline == 1 && dev->interrupt_in_buffer[1] == 0xff) {
254 dev->offline = 2;
255 goto resubmit;
257 /* Always pass one offline event up the stack */
258 if (dev->offline > 0 && dev->interrupt_in_buffer[1] != 0xff)
259 dev->offline = 0;
260 if (dev->offline == 0 && dev->interrupt_in_buffer[1] == 0xff)
261 dev->offline = 1;
262 #endif
263 dbg_info(&dev->intf->dev, "%s: head, tail are %x, %x\n",
264 __func__, dev->ring_head, dev->ring_tail);
265 next_ring_head = (dev->ring_head + 1) % ring_buffer_size;
267 if (next_ring_head != dev->ring_tail) {
268 memcpy(&((*dev->ring_buffer)[dev->ring_head]),
269 dev->interrupt_in_buffer, urb->actual_length);
270 dev->ring_head = next_ring_head;
271 retval = 0;
272 memset(dev->interrupt_in_buffer, 0, urb->actual_length);
273 } else {
274 dev_warn(&dev->intf->dev,
275 "Ring buffer overflow, %d bytes dropped\n",
276 urb->actual_length);
277 memset(dev->interrupt_in_buffer, 0, urb->actual_length);
281 resubmit:
282 /* resubmit if we're still running */
283 if (dev->interrupt_in_running && dev->intf) {
284 retval = usb_submit_urb(dev->interrupt_in_urb, GFP_ATOMIC);
285 if (retval)
286 dev_err(&dev->intf->dev,
287 "usb_submit_urb failed (%d)\n", retval);
290 exit:
291 dev->interrupt_in_done = 1;
292 wake_up_interruptible(&dev->read_wait);
295 /** usb_alphatrack_interrupt_out_callback */
296 static void usb_alphatrack_interrupt_out_callback(struct urb *urb)
298 struct usb_alphatrack *dev = urb->context;
300 /* sync/async unlink faults aren't errors */
301 if (urb->status && !(urb->status == -ENOENT ||
302 urb->status == -ECONNRESET ||
303 urb->status == -ESHUTDOWN))
304 dbg_info(&dev->intf->dev,
305 "%s - nonzero write interrupt status received: %d\n",
306 __func__, urb->status);
307 atomic_dec(&dev->writes_pending);
308 dev->interrupt_out_busy = 0;
309 wake_up_interruptible(&dev->write_wait);
312 /** usb_alphatrack_open */
313 static int usb_alphatrack_open(struct inode *inode, struct file *file)
315 struct usb_alphatrack *dev;
316 int subminor;
317 int retval = 0;
318 struct usb_interface *interface;
320 nonseekable_open(inode, file);
321 subminor = iminor(inode);
323 mutex_lock(&disconnect_mutex);
325 interface = usb_find_interface(&usb_alphatrack_driver, subminor);
327 if (!interface) {
328 pr_err("%s - error, can't find device for minor %d\n",
329 __func__, subminor);
330 retval = -ENODEV;
331 goto unlock_disconnect_exit;
334 dev = usb_get_intfdata(interface);
336 if (!dev) {
337 retval = -ENODEV;
338 goto unlock_disconnect_exit;
341 /* lock this device */
342 if (mutex_lock_interruptible(&dev->mtx)) {
343 retval = -ERESTARTSYS;
344 goto unlock_disconnect_exit;
347 /* allow opening only once */
348 if (dev->open_count) {
349 retval = -EBUSY;
350 goto unlock_exit;
352 dev->open_count = 1;
354 /* initialize in direction */
355 dev->ring_head = 0;
356 dev->ring_tail = 0;
357 usb_fill_int_urb(dev->interrupt_in_urb,
358 interface_to_usbdev(interface),
359 usb_rcvintpipe(interface_to_usbdev(interface),
360 dev->interrupt_in_endpoint->
361 bEndpointAddress),
362 dev->interrupt_in_buffer,
363 dev->interrupt_in_endpoint_size,
364 usb_alphatrack_interrupt_in_callback, dev,
365 dev->interrupt_in_interval);
367 dev->interrupt_in_running = 1;
368 dev->interrupt_in_done = 0;
369 dev->enable = 1;
370 dev->offline = 0;
372 retval = usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL);
373 if (retval) {
374 dev_err(&interface->dev,
375 "Couldn't submit interrupt_in_urb %d\n", retval);
376 dev->interrupt_in_running = 0;
377 dev->open_count = 0;
378 goto unlock_exit;
381 /* save device in the file's private structure */
382 file->private_data = dev;
384 unlock_exit:
385 mutex_unlock(&dev->mtx);
387 unlock_disconnect_exit:
388 mutex_unlock(&disconnect_mutex);
390 return retval;
393 /** usb_alphatrack_release */
394 static int usb_alphatrack_release(struct inode *inode, struct file *file)
396 struct usb_alphatrack *dev;
397 int retval = 0;
399 dev = file->private_data;
401 if (dev == NULL) {
402 retval = -ENODEV;
403 goto exit;
406 if (mutex_lock_interruptible(&dev->mtx)) {
407 retval = -ERESTARTSYS;
408 goto exit;
411 if (dev->open_count != 1) {
412 retval = -ENODEV;
413 goto unlock_exit;
416 if (dev->intf == NULL) {
417 /* the device was unplugged before the file was released */
418 mutex_unlock(&dev->mtx);
419 /* unlock here as usb_alphatrack_delete frees dev */
420 usb_alphatrack_delete(dev);
421 retval = -ENODEV;
422 goto exit;
425 /* wait until write transfer is finished */
426 if (dev->interrupt_out_busy)
427 wait_event_interruptible_timeout(dev->write_wait,
428 !dev->interrupt_out_busy,
429 2 * HZ);
430 usb_alphatrack_abort_transfers(dev);
431 dev->open_count = 0;
433 unlock_exit:
434 mutex_unlock(&dev->mtx);
436 exit:
437 return retval;
440 /** usb_alphatrack_poll */
441 static unsigned int usb_alphatrack_poll(struct file *file, poll_table *wait)
443 struct usb_alphatrack *dev;
444 unsigned int mask = 0;
446 dev = file->private_data;
448 poll_wait(file, &dev->read_wait, wait);
449 poll_wait(file, &dev->write_wait, wait);
451 if (dev->ring_head != dev->ring_tail)
452 mask |= POLLIN | POLLRDNORM;
453 if (!dev->interrupt_out_busy)
454 mask |= POLLOUT | POLLWRNORM;
456 return mask;
459 /** usb_alphatrack_read */
460 static ssize_t usb_alphatrack_read(struct file *file, char __user *buffer,
461 size_t count, loff_t *ppos)
463 struct usb_alphatrack *dev;
464 int retval = 0;
466 int c = 0;
468 dev = file->private_data;
470 /* verify that we actually have some data to read */
471 if (count == 0)
472 goto exit;
474 /* lock this object */
475 if (mutex_lock_interruptible(&dev->mtx)) {
476 retval = -ERESTARTSYS;
477 goto exit;
480 /* verify that the device wasn't unplugged */
481 if (dev->intf == NULL) {
482 retval = -ENODEV;
483 pr_err("%s: No device or device unplugged %d\n",
484 __func__, retval);
485 goto unlock_exit;
488 while (dev->ring_head == dev->ring_tail) {
489 if (file->f_flags & O_NONBLOCK) {
490 retval = -EAGAIN;
491 goto unlock_exit;
493 dev->interrupt_in_done = 0;
494 retval =
495 wait_event_interruptible(dev->read_wait,
496 dev->interrupt_in_done);
497 if (retval < 0)
498 goto unlock_exit;
501 alphatrack_ocmd_info(&dev->intf->dev,
502 &(*dev->ring_buffer)[dev->ring_tail].cmd, "%s",
503 ": copying to userspace");
505 c = 0;
506 while ((c < count) && (dev->ring_tail != dev->ring_head)) {
507 if (copy_to_user
508 (&buffer[c], &(*dev->ring_buffer)[dev->ring_tail],
509 INPUT_CMD_SIZE)) {
510 retval = -EFAULT;
511 goto unlock_exit;
513 dev->ring_tail = (dev->ring_tail + 1) % ring_buffer_size;
514 c += INPUT_CMD_SIZE;
515 dbg_info(&dev->intf->dev, "%s: head, tail are %x, %x\n",
516 __func__, dev->ring_head, dev->ring_tail);
518 retval = c;
520 unlock_exit:
521 /* unlock the device */
522 mutex_unlock(&dev->mtx);
524 exit:
525 return retval;
528 /** usb_alphatrack_write */
529 static ssize_t usb_alphatrack_write(struct file *file,
530 const char __user *buffer, size_t count,
531 loff_t *ppos)
533 struct usb_alphatrack *dev;
534 size_t bytes_to_write;
535 int retval = 0;
537 dev = file->private_data;
539 /* verify that we actually have some data to write */
540 if (count == 0)
541 goto exit;
543 /* lock this object */
544 if (mutex_lock_interruptible(&dev->mtx)) {
545 retval = -ERESTARTSYS;
546 goto exit;
549 /* verify that the device wasn't unplugged */
550 if (dev->intf == NULL) {
551 retval = -ENODEV;
552 pr_err("%s: No device or device unplugged %d\n",
553 __func__, retval);
554 goto unlock_exit;
557 /* wait until previous transfer is finished */
558 if (dev->interrupt_out_busy) {
559 if (file->f_flags & O_NONBLOCK) {
560 retval = -EAGAIN;
561 goto unlock_exit;
563 retval =
564 wait_event_interruptible(dev->write_wait,
565 !dev->interrupt_out_busy);
566 if (retval < 0)
567 goto unlock_exit;
570 /* write the data into interrupt_out_buffer from userspace */
571 /* FIXME - if you write more than 12 bytes this breaks */
572 bytes_to_write =
573 min(count, write_buffer_size * dev->interrupt_out_endpoint_size);
574 if (bytes_to_write < count)
575 dev_warn(&dev->intf->dev,
576 "Write buffer overflow, %zd bytes dropped\n",
577 count - bytes_to_write);
579 dbg_info(&dev->intf->dev, "%s: count = %zd, bytes_to_write = %zd\n",
580 __func__, count, bytes_to_write);
582 if (copy_from_user(dev->interrupt_out_buffer, buffer, bytes_to_write)) {
583 retval = -EFAULT;
584 goto unlock_exit;
587 if (dev->interrupt_out_endpoint == NULL) {
588 dev_err(&dev->intf->dev, "Endpoint should not be null!\n");
589 goto unlock_exit;
592 /* send off the urb */
593 usb_fill_int_urb(dev->interrupt_out_urb,
594 interface_to_usbdev(dev->intf),
595 usb_sndintpipe(interface_to_usbdev(dev->intf),
596 dev->interrupt_out_endpoint->
597 bEndpointAddress),
598 dev->interrupt_out_buffer, bytes_to_write,
599 usb_alphatrack_interrupt_out_callback, dev,
600 dev->interrupt_out_interval);
601 dev->interrupt_out_busy = 1;
602 atomic_inc(&dev->writes_pending);
603 wmb();
605 retval = usb_submit_urb(dev->interrupt_out_urb, GFP_KERNEL);
606 if (retval) {
607 dev->interrupt_out_busy = 0;
608 dev_err(&dev->intf->dev,
609 "Couldn't submit interrupt_out_urb %d\n", retval);
610 atomic_dec(&dev->writes_pending);
611 goto unlock_exit;
613 retval = bytes_to_write;
615 unlock_exit:
616 /* unlock the device */
617 mutex_unlock(&dev->mtx);
619 exit:
620 return retval;
623 /* file operations needed when we register this driver */
624 static const struct file_operations usb_alphatrack_fops = {
625 .owner = THIS_MODULE,
626 .read = usb_alphatrack_read,
627 .write = usb_alphatrack_write,
628 .open = usb_alphatrack_open,
629 .release = usb_alphatrack_release,
630 .poll = usb_alphatrack_poll,
631 .llseek = no_llseek,
635 * usb class driver info in order to get a minor number from the usb core,
636 * and to have the device registered with the driver core
639 static struct usb_class_driver usb_alphatrack_class = {
640 .name = "alphatrack%d",
641 .fops = &usb_alphatrack_fops,
642 .minor_base = USB_ALPHATRACK_MINOR_BASE,
646 * usb_alphatrack_probe
648 * Called by the usb core when a new device is connected that it thinks
649 * this driver might be interested in.
651 static int usb_alphatrack_probe(struct usb_interface *intf,
652 const struct usb_device_id *id)
654 struct usb_device *udev = interface_to_usbdev(intf);
655 struct usb_alphatrack *dev = NULL;
656 struct usb_host_interface *iface_desc;
657 struct usb_endpoint_descriptor *endpoint;
658 int i;
659 int true_size;
660 int retval = -ENOMEM;
662 /* allocate memory for our device state and initialize it */
664 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
665 if (dev == NULL)
666 goto exit;
668 mutex_init(&dev->mtx);
669 dev->intf = intf;
670 init_waitqueue_head(&dev->read_wait);
671 init_waitqueue_head(&dev->write_wait);
673 iface_desc = intf->cur_altsetting;
675 /* set up the endpoint information */
676 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
677 endpoint = &iface_desc->endpoint[i].desc;
679 if (usb_endpoint_is_int_in(endpoint))
680 dev->interrupt_in_endpoint = endpoint;
682 if (usb_endpoint_is_int_out(endpoint))
683 dev->interrupt_out_endpoint = endpoint;
685 if (dev->interrupt_in_endpoint == NULL) {
686 dev_err(&intf->dev, "Interrupt in endpoint not found\n");
687 goto error;
689 if (dev->interrupt_out_endpoint == NULL)
690 dev_warn(&intf->dev,
691 "Interrupt out endpoint not found"
692 "(using control endpoint instead)\n");
694 dev->interrupt_in_endpoint_size =
695 le16_to_cpu(dev->interrupt_in_endpoint->wMaxPacketSize);
697 if (dev->interrupt_in_endpoint_size != 64)
698 dev_warn(&intf->dev, "Interrupt in endpoint size is not 64!\n");
700 if (ring_buffer_size == 0)
701 ring_buffer_size = RING_BUFFER_SIZE;
703 true_size = min(ring_buffer_size, RING_BUFFER_SIZE);
706 * FIXME - there are more usb_alloc routines for dma correctness.
707 * Needed?
709 dev->ring_buffer = kmalloc_array(true_size,
710 sizeof(struct alphatrack_icmd),
711 GFP_KERNEL);
712 if (!dev->ring_buffer)
713 goto error;
715 dev->interrupt_in_buffer = kmalloc(dev->interrupt_in_endpoint_size,
716 GFP_KERNEL);
717 if (!dev->interrupt_in_buffer)
718 goto error;
720 dev->oldi_buffer = kmalloc(dev->interrupt_in_endpoint_size, GFP_KERNEL);
721 if (!dev->oldi_buffer)
722 goto error;
724 dev->interrupt_in_urb = usb_alloc_urb(0, GFP_KERNEL);
725 if (!dev->interrupt_in_urb) {
726 dev_err(&intf->dev, "Couldn't allocate interrupt_in_urb\n");
727 goto error;
730 dev->interrupt_out_endpoint_size =
731 dev->interrupt_out_endpoint ? le16_to_cpu(dev->
732 interrupt_out_endpoint->
733 wMaxPacketSize) : udev->
734 descriptor.bMaxPacketSize0;
736 if (dev->interrupt_out_endpoint_size != 64)
737 dev_warn(&intf->dev,
738 "Interrupt out endpoint size is not 64!)\n");
740 if (write_buffer_size == 0)
741 write_buffer_size = WRITE_BUFFER_SIZE;
742 true_size = min(write_buffer_size, WRITE_BUFFER_SIZE);
744 dev->interrupt_out_buffer =
745 kmalloc_array(true_size,
746 dev->interrupt_out_endpoint_size,
747 GFP_KERNEL);
748 if (!dev->interrupt_out_buffer)
749 goto error;
751 dev->write_buffer = kmalloc_array(true_size,
752 sizeof(struct alphatrack_ocmd),
753 GFP_KERNEL);
754 if (!dev->write_buffer)
755 goto error;
757 dev->interrupt_out_urb = usb_alloc_urb(0, GFP_KERNEL);
758 if (!dev->interrupt_out_urb) {
759 dev_err(&intf->dev, "Couldn't allocate interrupt_out_urb\n");
760 goto error;
762 dev->interrupt_in_interval =
763 min_interrupt_in_interval >
764 dev->interrupt_in_endpoint->
765 bInterval ? min_interrupt_in_interval : dev->interrupt_in_endpoint->
766 bInterval;
767 if (dev->interrupt_out_endpoint)
768 dev->interrupt_out_interval =
769 min_interrupt_out_interval >
770 dev->interrupt_out_endpoint->
771 bInterval ? min_interrupt_out_interval : dev->
772 interrupt_out_endpoint->bInterval;
774 /* we can register the device now, as it is ready */
775 usb_set_intfdata(intf, dev);
777 atomic_set(&dev->writes_pending, 0);
778 retval = usb_register_dev(intf, &usb_alphatrack_class);
779 if (retval) {
780 /* something prevented us from registering this driver */
781 dev_err(&intf->dev,
782 "Not able to get a minor for this device.\n");
783 usb_set_intfdata(intf, NULL);
784 goto error;
787 /* let the user know what node this device is now attached to */
788 dev_info(&intf->dev,
789 "Alphatrack Device #%d now attached to major %d minor %d\n",
790 (intf->minor - USB_ALPHATRACK_MINOR_BASE), USB_MAJOR,
791 intf->minor);
793 exit:
794 return retval;
796 error:
797 usb_alphatrack_delete(dev);
799 return retval;
803 * usb_alphatrack_disconnect
805 * Called by the usb core when the device is removed from the system.
807 static void usb_alphatrack_disconnect(struct usb_interface *intf)
809 struct usb_alphatrack *dev;
810 int minor;
812 mutex_lock(&disconnect_mutex);
814 dev = usb_get_intfdata(intf);
815 usb_set_intfdata(intf, NULL);
817 mutex_lock(&dev->mtx);
819 minor = intf->minor;
821 /* give back our minor */
822 usb_deregister_dev(intf, &usb_alphatrack_class);
824 /* if the device is not opened, then we clean up right now */
825 if (!dev->open_count) {
826 mutex_unlock(&dev->mtx);
827 usb_alphatrack_delete(dev);
828 } else {
829 atomic_set(&dev->writes_pending, 0);
830 dev->intf = NULL;
831 mutex_unlock(&dev->mtx);
834 mutex_unlock(&disconnect_mutex);
836 dev_info(&intf->dev, "Alphatrack Surface #%d now disconnected\n",
837 (minor - USB_ALPHATRACK_MINOR_BASE));
840 /* usb specific object needed to register this driver with the usb subsystem */
841 static struct usb_driver usb_alphatrack_driver = {
842 .name = "alphatrack",
843 .probe = usb_alphatrack_probe,
844 .disconnect = usb_alphatrack_disconnect,
845 .id_table = usb_alphatrack_table,
848 module_usb_driver(usb_alphatrack_driver);