rt2x00: Simplify rt2x00_check_rev
[linux/fpc-iii.git] / drivers / staging / frontier / alphatrack.c
blobbcba17eae9265a8a2bf5a1de74dd5a573f30de39
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.
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 30 commands for the alphatrack. 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/kobject.h>
41 #include <linux/mutex.h>
42 #include <linux/version.h>
44 #include <linux/uaccess.h>
45 #include <linux/input.h>
46 #include <linux/usb.h>
47 #include <linux/poll.h>
49 #include "alphatrack.h"
51 #define VENDOR_ID 0x165b
52 #define PRODUCT_ID 0xfad1
54 #ifdef CONFIG_USB_DYNAMIC_MINORS
55 #define USB_ALPHATRACK_MINOR_BASE 0
56 #else
57 /* FIXME 176 - is another driver's minor - apply for that */
58 #define USB_ALPHATRACK_MINOR_BASE 176
59 #endif
61 /* table of devices that work with this driver */
62 static struct usb_device_id usb_alphatrack_table[] = {
63 {USB_DEVICE(VENDOR_ID, PRODUCT_ID)},
64 {} /* Terminating entry */
67 MODULE_DEVICE_TABLE(usb, usb_alphatrack_table);
68 MODULE_VERSION("0.41");
69 MODULE_AUTHOR("Mike Taht <m@taht.net>");
70 MODULE_DESCRIPTION("Alphatrack USB Driver");
71 MODULE_LICENSE("GPL");
72 MODULE_SUPPORTED_DEVICE("Frontier Designs Alphatrack Control Surface");
74 /* These aren't done yet */
76 #define SUPPRESS_EXTRA_ONLINE_EVENTS 0
77 #define BUFFERED_WRITES 0
78 #define SUPPRESS_EXTRA_OFFLINE_EVENTS 0
79 #define COMPRESS_FADER_EVENTS 0
81 #define BUFFERED_READS 1
82 #define RING_BUFFER_SIZE 512
83 #define WRITE_BUFFER_SIZE 34
84 #define ALPHATRACK_USB_TIMEOUT 10
85 #define OUTPUT_CMD_SIZE 8
86 #define INPUT_CMD_SIZE 12
87 #define ALPHATRACK_DEBUG 0
89 static int debug = ALPHATRACK_DEBUG;
91 /* Use our own dbg macro */
92 #define dbg_info(dev, format, arg...) do \
93 { if (debug) dev_info(dev , format , ## arg); } while (0)
95 #define alphatrack_ocmd_info(dev, cmd, format, arg...)
97 #define alphatrack_icmd_info(dev, cmd, format, arg...)
99 /* Module parameters */
101 module_param(debug, int, S_IRUGO | S_IWUSR);
102 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.
116 static int write_buffer_size = WRITE_BUFFER_SIZE;
117 module_param(write_buffer_size, int, S_IRUGO);
118 MODULE_PARM_DESC(write_buffer_size, "Write buffer size");
121 * Increase the interval for debugging purposes.
122 * or set to 1 to use the standard interval from the endpoint descriptors.
125 static int min_interrupt_in_interval = ALPHATRACK_USB_TIMEOUT;
126 module_param(min_interrupt_in_interval, int, 0);
127 MODULE_PARM_DESC(min_interrupt_in_interval,
128 "Minimum interrupt in interval in ms");
130 static int min_interrupt_out_interval = ALPHATRACK_USB_TIMEOUT;
131 module_param(min_interrupt_out_interval, int, 0);
132 MODULE_PARM_DESC(min_interrupt_out_interval,
133 "Minimum interrupt out interval in ms");
135 /* Structure to hold all of our device specific stuff */
137 struct usb_alphatrack {
138 struct semaphore sem; /* locks this structure */
139 struct usb_interface *intf; /* save off the usb interface pointer */
140 int open_count; /* number of times this port has been opened */
142 /* make gcc happy */
143 struct alphatrack_icmd (*ring_buffer)[RING_BUFFER_SIZE];
144 struct alphatrack_ocmd (*write_buffer)[WRITE_BUFFER_SIZE];
145 unsigned int ring_head;
146 unsigned int ring_tail;
148 wait_queue_head_t read_wait;
149 wait_queue_head_t write_wait;
151 unsigned char *interrupt_in_buffer;
152 unsigned char *oldi_buffer;
153 struct usb_endpoint_descriptor *interrupt_in_endpoint;
154 struct urb *interrupt_in_urb;
155 int interrupt_in_interval;
156 size_t interrupt_in_endpoint_size;
157 int interrupt_in_running;
158 int interrupt_in_done;
160 char *interrupt_out_buffer;
161 struct usb_endpoint_descriptor *interrupt_out_endpoint;
162 struct urb *interrupt_out_urb;
163 int interrupt_out_interval;
164 size_t interrupt_out_endpoint_size;
165 int interrupt_out_busy;
167 atomic_t writes_pending;
168 int event; /* alternate interface to events */
169 int fader; /* 10 bits */
170 int lights; /* 23 bits */
171 unsigned char dump_state; /* 0 if disabled 1 if enabled */
172 unsigned char enable; /* 0 if disabled 1 if enabled */
173 unsigned char offline; /* if the device is out of range or asleep */
174 unsigned char verbose; /* be verbose in error reporting */
175 unsigned char last_cmd[OUTPUT_CMD_SIZE];
176 unsigned char screen[32];
179 /* prevent races between open() and disconnect() */
180 static DEFINE_MUTEX(disconnect_mutex);
182 /* forward declaration */
184 static struct usb_driver usb_alphatrack_driver;
187 * usb_alphatrack_abort_transfers
188 * aborts transfers and frees associated data structures
190 static void usb_alphatrack_abort_transfers(struct usb_alphatrack *dev)
192 /* shutdown transfer */
193 if (dev->interrupt_in_running) {
194 dev->interrupt_in_running = 0;
195 if (dev->intf)
196 usb_kill_urb(dev->interrupt_in_urb);
198 if (dev->interrupt_out_busy)
199 if (dev->intf)
200 usb_kill_urb(dev->interrupt_out_urb);
204 * usb_alphatrack_delete
206 static void usb_alphatrack_delete(struct usb_alphatrack *dev)
208 usb_alphatrack_abort_transfers(dev);
209 usb_free_urb(dev->interrupt_in_urb);
210 usb_free_urb(dev->interrupt_out_urb);
211 kfree(dev->ring_buffer);
212 kfree(dev->interrupt_in_buffer);
213 kfree(dev->interrupt_out_buffer);
214 kfree(dev); /* fixme oldi_buffer */
218 * usb_alphatrack_interrupt_in_callback
221 static void usb_alphatrack_interrupt_in_callback(struct urb *urb)
223 struct usb_alphatrack *dev = urb->context;
224 unsigned int next_ring_head;
225 int retval = -1;
227 if (urb->status) {
228 if (urb->status == -ENOENT ||
229 urb->status == -ECONNRESET || urb->status == -ESHUTDOWN) {
230 goto exit;
231 } else {
232 dbg_info(&dev->intf->dev,
233 "%s: nonzero status received: %d\n", __func__,
234 urb->status);
235 goto resubmit; /* maybe we can recover */
239 if (urb->actual_length != INPUT_CMD_SIZE) {
240 dev_warn(&dev->intf->dev,
241 "Urb length was %d bytes!!"
242 "Do something intelligent \n", urb->actual_length);
243 } else {
244 alphatrack_ocmd_info(&dev->intf->dev,
245 &(*dev->ring_buffer)[dev->ring_tail].cmd,
246 "%s", "bla");
247 if (memcmp
248 (dev->interrupt_in_buffer, dev->oldi_buffer,
249 INPUT_CMD_SIZE) == 0) {
250 goto resubmit;
252 memcpy(dev->oldi_buffer, dev->interrupt_in_buffer,
253 INPUT_CMD_SIZE);
255 #if SUPPRESS_EXTRA_OFFLINE_EVENTS
256 if (dev->offline == 2 && dev->interrupt_in_buffer[1] == 0xff)
257 goto resubmit;
258 if (dev->offline == 1 && dev->interrupt_in_buffer[1] == 0xff) {
259 dev->offline = 2;
260 goto resubmit;
262 /* Always pass one offline event up the stack */
263 if (dev->offline > 0 && dev->interrupt_in_buffer[1] != 0xff)
264 dev->offline = 0;
265 if (dev->offline == 0 && dev->interrupt_in_buffer[1] == 0xff)
266 dev->offline = 1;
267 #endif
268 dbg_info(&dev->intf->dev, "%s: head, tail are %x, %x\n",
269 __func__, dev->ring_head, dev->ring_tail);
270 next_ring_head = (dev->ring_head + 1) % ring_buffer_size;
272 if (next_ring_head != dev->ring_tail) {
273 memcpy(&((*dev->ring_buffer)[dev->ring_head]),
274 dev->interrupt_in_buffer, urb->actual_length);
275 dev->ring_head = next_ring_head;
276 retval = 0;
277 memset(dev->interrupt_in_buffer, 0, urb->actual_length);
278 } else {
279 dev_warn(&dev->intf->dev,
280 "Ring buffer overflow, %d bytes dropped\n",
281 urb->actual_length);
282 memset(dev->interrupt_in_buffer, 0, urb->actual_length);
286 resubmit:
287 /* resubmit if we're still running */
288 if (dev->interrupt_in_running && dev->intf) {
289 retval = usb_submit_urb(dev->interrupt_in_urb, GFP_ATOMIC);
290 if (retval)
291 dev_err(&dev->intf->dev,
292 "usb_submit_urb failed (%d)\n", retval);
295 exit:
296 dev->interrupt_in_done = 1;
297 wake_up_interruptible(&dev->read_wait);
301 * usb_alphatrack_interrupt_out_callback
303 static void usb_alphatrack_interrupt_out_callback(struct urb *urb)
305 struct usb_alphatrack *dev = urb->context;
307 /* sync/async unlink faults aren't errors */
308 if (urb->status && !(urb->status == -ENOENT ||
309 urb->status == -ECONNRESET ||
310 urb->status == -ESHUTDOWN))
311 dbg_info(&dev->intf->dev,
312 "%s - nonzero write interrupt status received: %d\n",
313 __func__, urb->status);
314 atomic_dec(&dev->writes_pending);
315 dev->interrupt_out_busy = 0;
316 wake_up_interruptible(&dev->write_wait);
320 * usb_alphatrack_open
322 static int usb_alphatrack_open(struct inode *inode, struct file *file)
324 struct usb_alphatrack *dev;
325 int subminor;
326 int retval = 0;
327 struct usb_interface *interface;
329 nonseekable_open(inode, file);
330 subminor = iminor(inode);
332 mutex_lock(&disconnect_mutex);
334 interface = usb_find_interface(&usb_alphatrack_driver, subminor);
336 if (!interface) {
337 err("%s - error, can't find device for minor %d\n",
338 __func__, subminor);
339 retval = -ENODEV;
340 goto unlock_disconnect_exit;
343 dev = usb_get_intfdata(interface);
345 if (!dev) {
346 retval = -ENODEV;
347 goto unlock_disconnect_exit;
350 /* lock this device */
351 if (down_interruptible(&dev->sem)) {
352 retval = -ERESTARTSYS;
353 goto unlock_disconnect_exit;
356 /* allow opening only once */
357 if (dev->open_count) {
358 retval = -EBUSY;
359 goto unlock_exit;
361 dev->open_count = 1;
363 /* initialize in direction */
364 dev->ring_head = 0;
365 dev->ring_tail = 0;
366 usb_fill_int_urb(dev->interrupt_in_urb,
367 interface_to_usbdev(interface),
368 usb_rcvintpipe(interface_to_usbdev(interface),
369 dev->interrupt_in_endpoint->
370 bEndpointAddress),
371 dev->interrupt_in_buffer,
372 dev->interrupt_in_endpoint_size,
373 usb_alphatrack_interrupt_in_callback, dev,
374 dev->interrupt_in_interval);
376 dev->interrupt_in_running = 1;
377 dev->interrupt_in_done = 0;
378 dev->enable = 1;
379 dev->offline = 0;
381 retval = usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL);
382 if (retval) {
383 dev_err(&interface->dev,
384 "Couldn't submit interrupt_in_urb %d\n", retval);
385 dev->interrupt_in_running = 0;
386 dev->open_count = 0;
387 goto unlock_exit;
390 /* save device in the file's private structure */
391 file->private_data = dev;
393 unlock_exit:
394 up(&dev->sem);
396 unlock_disconnect_exit:
397 mutex_unlock(&disconnect_mutex);
399 return retval;
403 * usb_alphatrack_release
405 static int usb_alphatrack_release(struct inode *inode, struct file *file)
407 struct usb_alphatrack *dev;
408 int retval = 0;
410 dev = file->private_data;
412 if (dev == NULL) {
413 retval = -ENODEV;
414 goto exit;
417 if (down_interruptible(&dev->sem)) {
418 retval = -ERESTARTSYS;
419 goto exit;
422 if (dev->open_count != 1) {
423 retval = -ENODEV;
424 goto unlock_exit;
427 if (dev->intf == NULL) {
428 /* the device was unplugged before the file was released */
429 up(&dev->sem);
430 /* unlock here as usb_alphatrack_delete frees dev */
431 usb_alphatrack_delete(dev);
432 retval = -ENODEV;
433 goto exit;
436 /* wait until write transfer is finished */
437 if (dev->interrupt_out_busy)
438 wait_event_interruptible_timeout(dev->write_wait,
439 !dev->interrupt_out_busy,
440 2 * HZ);
441 usb_alphatrack_abort_transfers(dev);
442 dev->open_count = 0;
444 unlock_exit:
445 up(&dev->sem);
447 exit:
448 return retval;
452 * usb_alphatrack_poll
454 static unsigned int usb_alphatrack_poll(struct file *file, poll_table * wait)
456 struct usb_alphatrack *dev;
457 unsigned int mask = 0;
459 dev = file->private_data;
461 poll_wait(file, &dev->read_wait, wait);
462 poll_wait(file, &dev->write_wait, wait);
464 if (dev->ring_head != dev->ring_tail)
465 mask |= POLLIN | POLLRDNORM;
466 if (!dev->interrupt_out_busy)
467 mask |= POLLOUT | POLLWRNORM;
469 return mask;
473 * usb_alphatrack_read
475 static ssize_t usb_alphatrack_read(struct file *file, char __user *buffer,
476 size_t count, loff_t *ppos)
478 struct usb_alphatrack *dev;
479 int retval = 0;
481 int c = 0;
483 dev = file->private_data;
485 /* verify that we actually have some data to read */
486 if (count == 0)
487 goto exit;
489 /* lock this object */
490 if (down_interruptible(&dev->sem)) {
491 retval = -ERESTARTSYS;
492 goto exit;
495 /* verify that the device wasn't unplugged */
496 if (dev->intf == NULL) {
497 retval = -ENODEV;
498 err("No device or device unplugged %d\n", retval);
499 goto unlock_exit;
502 while (dev->ring_head == dev->ring_tail) {
503 if (file->f_flags & O_NONBLOCK) {
504 retval = -EAGAIN;
505 goto unlock_exit;
507 dev->interrupt_in_done = 0;
508 retval =
509 wait_event_interruptible(dev->read_wait,
510 dev->interrupt_in_done);
511 if (retval < 0)
512 goto unlock_exit;
515 alphatrack_ocmd_info(&dev->intf->dev,
516 &(*dev->ring_buffer)[dev->ring_tail].cmd, "%s",
517 ": copying to userspace");
519 c = 0;
520 while ((c < count) && (dev->ring_tail != dev->ring_head)) {
521 if (copy_to_user
522 (&buffer[c], &(*dev->ring_buffer)[dev->ring_tail],
523 INPUT_CMD_SIZE)) {
524 retval = -EFAULT;
525 goto unlock_exit;
527 dev->ring_tail = (dev->ring_tail + 1) % ring_buffer_size;
528 c += INPUT_CMD_SIZE;
529 dbg_info(&dev->intf->dev, "%s: head, tail are %x, %x\n",
530 __func__, dev->ring_head, dev->ring_tail);
532 retval = c;
534 unlock_exit:
535 /* unlock the device */
536 up(&dev->sem);
538 exit:
539 return retval;
543 * usb_alphatrack_write
545 static ssize_t usb_alphatrack_write(struct file *file,
546 const char __user *buffer, size_t count,
547 loff_t *ppos)
549 struct usb_alphatrack *dev;
550 size_t bytes_to_write;
551 int retval = 0;
553 dev = file->private_data;
555 /* verify that we actually have some data to write */
556 if (count == 0)
557 goto exit;
559 /* lock this object */
560 if (down_interruptible(&dev->sem)) {
561 retval = -ERESTARTSYS;
562 goto exit;
565 /* verify that the device wasn't unplugged */
566 if (dev->intf == NULL) {
567 retval = -ENODEV;
568 err("No device or device unplugged %d\n", retval);
569 goto unlock_exit;
572 /* wait until previous transfer is finished */
573 if (dev->interrupt_out_busy) {
574 if (file->f_flags & O_NONBLOCK) {
575 retval = -EAGAIN;
576 goto unlock_exit;
578 retval =
579 wait_event_interruptible(dev->write_wait,
580 !dev->interrupt_out_busy);
581 if (retval < 0)
582 goto unlock_exit;
585 /* write the data into interrupt_out_buffer from userspace */
586 /* FIXME - if you write more than 12 bytes this breaks */
587 bytes_to_write =
588 min(count, write_buffer_size * dev->interrupt_out_endpoint_size);
589 if (bytes_to_write < count)
590 dev_warn(&dev->intf->dev,
591 "Write buffer overflow, %zd bytes dropped\n",
592 count - bytes_to_write);
594 dbg_info(&dev->intf->dev, "%s: count = %zd, bytes_to_write = %zd\n",
595 __func__, count, bytes_to_write);
597 if (copy_from_user(dev->interrupt_out_buffer, buffer, bytes_to_write)) {
598 retval = -EFAULT;
599 goto unlock_exit;
602 if (dev->interrupt_out_endpoint == NULL) {
603 err("Endpoint should not be be null! \n");
604 goto unlock_exit;
607 /* send off the urb */
608 usb_fill_int_urb(dev->interrupt_out_urb,
609 interface_to_usbdev(dev->intf),
610 usb_sndintpipe(interface_to_usbdev(dev->intf),
611 dev->interrupt_out_endpoint->
612 bEndpointAddress),
613 dev->interrupt_out_buffer, bytes_to_write,
614 usb_alphatrack_interrupt_out_callback, dev,
615 dev->interrupt_out_interval);
616 dev->interrupt_out_busy = 1;
617 atomic_inc(&dev->writes_pending);
618 wmb();
620 retval = usb_submit_urb(dev->interrupt_out_urb, GFP_KERNEL);
621 if (retval) {
622 dev->interrupt_out_busy = 0;
623 err("Couldn't submit interrupt_out_urb %d\n", retval);
624 atomic_dec(&dev->writes_pending);
625 goto unlock_exit;
627 retval = bytes_to_write;
629 unlock_exit:
630 /* unlock the device */
631 up(&dev->sem);
633 exit:
634 return retval;
637 /* file operations needed when we register this driver */
638 static const struct file_operations usb_alphatrack_fops = {
639 .owner = THIS_MODULE,
640 .read = usb_alphatrack_read,
641 .write = usb_alphatrack_write,
642 .open = usb_alphatrack_open,
643 .release = usb_alphatrack_release,
644 .poll = usb_alphatrack_poll,
648 * usb class driver info in order to get a minor number from the usb core,
649 * and to have the device registered with the driver core
652 static struct usb_class_driver usb_alphatrack_class = {
653 .name = "alphatrack%d",
654 .fops = &usb_alphatrack_fops,
655 .minor_base = USB_ALPHATRACK_MINOR_BASE,
659 * usb_alphatrack_probe
661 * Called by the usb core when a new device is connected that it thinks
662 * this driver might be interested in.
664 static int usb_alphatrack_probe(struct usb_interface *intf,
665 const struct usb_device_id *id)
667 struct usb_device *udev = interface_to_usbdev(intf);
668 struct usb_alphatrack *dev = NULL;
669 struct usb_host_interface *iface_desc;
670 struct usb_endpoint_descriptor *endpoint;
671 int i;
672 int true_size;
673 int retval = -ENOMEM;
675 /* allocate memory for our device state and intialize it */
677 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
678 if (dev == NULL) {
679 dev_err(&intf->dev, "Out of memory\n");
680 goto exit;
682 init_MUTEX(&dev->sem);
683 dev->intf = intf;
684 init_waitqueue_head(&dev->read_wait);
685 init_waitqueue_head(&dev->write_wait);
687 iface_desc = intf->cur_altsetting;
689 /* set up the endpoint information */
690 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
691 endpoint = &iface_desc->endpoint[i].desc;
693 if (usb_endpoint_is_int_in(endpoint))
694 dev->interrupt_in_endpoint = endpoint;
696 if (usb_endpoint_is_int_out(endpoint))
697 dev->interrupt_out_endpoint = endpoint;
699 if (dev->interrupt_in_endpoint == NULL) {
700 dev_err(&intf->dev, "Interrupt in endpoint not found\n");
701 goto error;
703 if (dev->interrupt_out_endpoint == NULL)
704 dev_warn(&intf->dev,
705 "Interrupt out endpoint not found"
706 "(using control endpoint instead)\n");
708 dev->interrupt_in_endpoint_size =
709 le16_to_cpu(dev->interrupt_in_endpoint->wMaxPacketSize);
711 if (dev->interrupt_in_endpoint_size != 64)
712 dev_warn(&intf->dev, "Interrupt in endpoint size is not 64!\n");
714 if (ring_buffer_size == 0)
715 ring_buffer_size = RING_BUFFER_SIZE;
717 true_size = min(ring_buffer_size, RING_BUFFER_SIZE);
719 /* FIXME - there are more usb_alloc routines for dma correctness.
720 Needed? */
721 dev->ring_buffer =
722 kmalloc((true_size * sizeof(struct alphatrack_icmd)), GFP_KERNEL);
724 if (!dev->ring_buffer) {
725 dev_err(&intf->dev,
726 "Couldn't allocate input ring_buffer of size %d\n",
727 true_size);
728 goto error;
731 dev->interrupt_in_buffer =
732 kmalloc(dev->interrupt_in_endpoint_size, GFP_KERNEL);
734 if (!dev->interrupt_in_buffer) {
735 dev_err(&intf->dev, "Couldn't allocate interrupt_in_buffer\n");
736 goto error;
738 dev->oldi_buffer = kmalloc(dev->interrupt_in_endpoint_size, GFP_KERNEL);
739 if (!dev->oldi_buffer) {
740 dev_err(&intf->dev, "Couldn't allocate old buffer\n");
741 goto error;
743 dev->interrupt_in_urb = usb_alloc_urb(0, GFP_KERNEL);
744 if (!dev->interrupt_in_urb) {
745 dev_err(&intf->dev, "Couldn't allocate interrupt_in_urb\n");
746 goto error;
749 dev->interrupt_out_endpoint_size =
750 dev->interrupt_out_endpoint ? le16_to_cpu(dev->
751 interrupt_out_endpoint->
752 wMaxPacketSize) : udev->
753 descriptor.bMaxPacketSize0;
755 if (dev->interrupt_out_endpoint_size != 64)
756 dev_warn(&intf->dev,
757 "Interrupt out endpoint size is not 64!)\n");
759 if (write_buffer_size == 0)
760 write_buffer_size = WRITE_BUFFER_SIZE;
761 true_size = min(write_buffer_size, WRITE_BUFFER_SIZE);
763 dev->interrupt_out_buffer =
764 kmalloc(true_size * dev->interrupt_out_endpoint_size, GFP_KERNEL);
766 if (!dev->interrupt_out_buffer) {
767 dev_err(&intf->dev, "Couldn't allocate interrupt_out_buffer\n");
768 goto error;
771 dev->write_buffer =
772 kmalloc(sizeof(struct alphatrack_ocmd) * true_size, GFP_KERNEL);
774 if (!dev->write_buffer) {
775 dev_err(&intf->dev, "Couldn't allocate write_buffer \n");
776 goto error;
779 dev->interrupt_out_urb = usb_alloc_urb(0, GFP_KERNEL);
780 if (!dev->interrupt_out_urb) {
781 dev_err(&intf->dev, "Couldn't allocate interrupt_out_urb\n");
782 goto error;
784 dev->interrupt_in_interval =
785 min_interrupt_in_interval >
786 dev->interrupt_in_endpoint->
787 bInterval ? min_interrupt_in_interval : dev->interrupt_in_endpoint->
788 bInterval;
789 if (dev->interrupt_out_endpoint)
790 dev->interrupt_out_interval =
791 min_interrupt_out_interval >
792 dev->interrupt_out_endpoint->
793 bInterval ? min_interrupt_out_interval : dev->
794 interrupt_out_endpoint->bInterval;
796 /* we can register the device now, as it is ready */
797 usb_set_intfdata(intf, dev);
799 atomic_set(&dev->writes_pending, 0);
800 retval = usb_register_dev(intf, &usb_alphatrack_class);
801 if (retval) {
802 /* something prevented us from registering this driver */
803 dev_err(&intf->dev,
804 "Not able to get a minor for this device.\n");
805 usb_set_intfdata(intf, NULL);
806 goto error;
809 /* let the user know what node this device is now attached to */
810 dev_info(&intf->dev,
811 "Alphatrack Device #%d now attached to major %d minor %d\n",
812 (intf->minor - USB_ALPHATRACK_MINOR_BASE), USB_MAJOR,
813 intf->minor);
815 exit:
816 return retval;
818 error:
819 usb_alphatrack_delete(dev);
821 return retval;
825 * usb_alphatrack_disconnect
827 * Called by the usb core when the device is removed from the system.
829 static void usb_alphatrack_disconnect(struct usb_interface *intf)
831 struct usb_alphatrack *dev;
832 int minor;
834 mutex_lock(&disconnect_mutex);
836 dev = usb_get_intfdata(intf);
837 usb_set_intfdata(intf, NULL);
839 down(&dev->sem);
841 minor = intf->minor;
843 /* give back our minor */
844 usb_deregister_dev(intf, &usb_alphatrack_class);
846 /* if the device is not opened, then we clean up right now */
847 if (!dev->open_count) {
848 up(&dev->sem);
849 usb_alphatrack_delete(dev);
850 } else {
851 dev->intf = NULL;
852 up(&dev->sem);
855 atomic_set(&dev->writes_pending, 0);
856 mutex_unlock(&disconnect_mutex);
858 dev_info(&intf->dev, "Alphatrack Surface #%d now disconnected\n",
859 (minor - USB_ALPHATRACK_MINOR_BASE));
862 /* usb specific object needed to register this driver with the usb subsystem */
863 static struct usb_driver usb_alphatrack_driver = {
864 .name = "alphatrack",
865 .probe = usb_alphatrack_probe,
866 .disconnect = usb_alphatrack_disconnect,
867 .id_table = usb_alphatrack_table,
871 * usb_alphatrack_init
873 static int __init usb_alphatrack_init(void)
875 int retval;
877 /* register this driver with the USB subsystem */
878 retval = usb_register(&usb_alphatrack_driver);
879 if (retval)
880 err("usb_register failed for the " __FILE__
881 " driver. Error number %d\n", retval);
883 return retval;
887 * usb_alphatrack_exit
889 static void __exit usb_alphatrack_exit(void)
891 /* deregister this driver with the USB subsystem */
892 usb_deregister(&usb_alphatrack_driver);
895 module_init(usb_alphatrack_init);
896 module_exit(usb_alphatrack_exit);