]> Pileus Git - ~andy/linux/blob - drivers/staging/frontier/alphatrack.c
Linux 3.14
[~andy/linux] / drivers / staging / frontier / alphatrack.c
1 /*
2  * Frontier Designs Alphatrack driver
3  *
4  * Copyright (C) 2007 Michael Taht (m@taht.net)
5  *
6  * Based on the usbled driver and ldusb drivers by
7  *
8  * Copyright (C) 2004 Greg Kroah-Hartman (greg@kroah.com)
9  * Copyright (C) 2005 Michael Hund <mhund@ld-didactic.de>
10  *
11  * The ldusb driver was, in turn, derived from Lego USB Tower driver
12  * Copyright (C) 2003 David Glance <advidgsf@sourceforge.net>
13  *               2001-2004 Juergen Stuber <starblue@users.sourceforge.net>
14  *
15  *      This program is free software; you can redistribute it and/or
16  *      modify it under the terms of the GNU General Public License as
17  *      published by the Free Software Foundation, version 2.
18  *
19  */
20
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.
25  */
26
27 /*
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.
34  */
35
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>
42
43 #include <linux/uaccess.h>
44 #include <linux/input.h>
45 #include <linux/usb.h>
46 #include <linux/poll.h>
47
48 #include "alphatrack.h"
49
50 #define VENDOR_ID       0x165b
51 #define PRODUCT_ID      0xfad1
52
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
59
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 */
64 };
65
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");
72
73 /* These aren't done yet */
74
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
79
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
87
88 static int debug = ALPHATRACK_DEBUG;
89
90 /* Use our own dbg macro */
91 #define dbg_info(dev, format, arg...) do \
92         { if (debug) dev_info(dev , format , ## arg); } while (0)
93
94 #define alphatrack_ocmd_info(dev, cmd, format, arg...)
95
96 #define alphatrack_icmd_info(dev, cmd, format, arg...)
97
98 /* Module parameters */
99
100 module_param(debug, int, S_IRUGO | S_IWUSR);
101 MODULE_PARM_DESC(debug, "Debug enabled or not");
102
103 /*
104  * All interrupt in transfers are collected in a ring buffer to
105  * avoid racing conditions and get better performance of the driver.
106  */
107
108 static int ring_buffer_size = RING_BUFFER_SIZE;
109
110 module_param(ring_buffer_size, int, S_IRUGO);
111 MODULE_PARM_DESC(ring_buffer_size, "Read ring buffer size");
112
113 /* The write_buffer can one day contain more than one interrupt out transfer.*/
114
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");
118
119 /*
120  * Increase the interval for debugging purposes.
121  * or set to 1 to use the standard interval from the endpoint descriptors.
122  */
123
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");
128
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");
133
134 /* Structure to hold all of our device specific stuff */
135
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 */
140
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;
146
147         wait_queue_head_t read_wait;
148         wait_queue_head_t write_wait;
149
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;
158
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;
165
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];
176 };
177
178 /* prevent races between open() and disconnect() */
179 static DEFINE_MUTEX(disconnect_mutex);
180
181 /* forward declaration */
182
183 static struct usb_driver usb_alphatrack_driver;
184
185 /**
186  *      usb_alphatrack_abort_transfers
187  *      aborts transfers and frees associated data structures
188  */
189 static void usb_alphatrack_abort_transfers(struct usb_alphatrack *dev)
190 {
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);
196         }
197         if (dev->interrupt_out_busy)
198                 if (dev->intf)
199                         usb_kill_urb(dev->interrupt_out_urb);
200 }
201
202 /** usb_alphatrack_delete */
203 static void usb_alphatrack_delete(struct usb_alphatrack *dev)
204 {
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 */
212 }
213
214 /** usb_alphatrack_interrupt_in_callback */
215
216 static void usb_alphatrack_interrupt_in_callback(struct urb *urb)
217 {
218         struct usb_alphatrack *dev = urb->context;
219         unsigned int next_ring_head;
220         int retval = -1;
221
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 */
231                 }
232         }
233
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;
246                 }
247                 memcpy(dev->oldi_buffer, dev->interrupt_in_buffer,
248                        INPUT_CMD_SIZE);
249
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;
256                 }
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;
266
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);
278                 }
279         }
280
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);
288         }
289
290 exit:
291         dev->interrupt_in_done = 1;
292         wake_up_interruptible(&dev->read_wait);
293 }
294
295 /** usb_alphatrack_interrupt_out_callback */
296 static void usb_alphatrack_interrupt_out_callback(struct urb *urb)
297 {
298         struct usb_alphatrack *dev = urb->context;
299
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);
310 }
311
312 /** usb_alphatrack_open */
313 static int usb_alphatrack_open(struct inode *inode, struct file *file)
314 {
315         struct usb_alphatrack *dev;
316         int subminor;
317         int retval = 0;
318         struct usb_interface *interface;
319
320         nonseekable_open(inode, file);
321         subminor = iminor(inode);
322
323         mutex_lock(&disconnect_mutex);
324
325         interface = usb_find_interface(&usb_alphatrack_driver, subminor);
326
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;
332         }
333
334         dev = usb_get_intfdata(interface);
335
336         if (!dev) {
337                 retval = -ENODEV;
338                 goto unlock_disconnect_exit;
339         }
340
341         /* lock this device */
342         if (mutex_lock_interruptible(&dev->mtx)) {
343                 retval = -ERESTARTSYS;
344                 goto unlock_disconnect_exit;
345         }
346
347         /* allow opening only once */
348         if (dev->open_count) {
349                 retval = -EBUSY;
350                 goto unlock_exit;
351         }
352         dev->open_count = 1;
353
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);
366
367         dev->interrupt_in_running = 1;
368         dev->interrupt_in_done = 0;
369         dev->enable = 1;
370         dev->offline = 0;
371
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;
379         }
380
381         /* save device in the file's private structure */
382         file->private_data = dev;
383
384 unlock_exit:
385         mutex_unlock(&dev->mtx);
386
387 unlock_disconnect_exit:
388         mutex_unlock(&disconnect_mutex);
389
390         return retval;
391 }
392
393 /** usb_alphatrack_release */
394 static int usb_alphatrack_release(struct inode *inode, struct file *file)
395 {
396         struct usb_alphatrack *dev;
397         int retval = 0;
398
399         dev = file->private_data;
400
401         if (dev == NULL) {
402                 retval = -ENODEV;
403                 goto exit;
404         }
405
406         if (mutex_lock_interruptible(&dev->mtx)) {
407                 retval = -ERESTARTSYS;
408                 goto exit;
409         }
410
411         if (dev->open_count != 1) {
412                 retval = -ENODEV;
413                 goto unlock_exit;
414         }
415
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;
423         }
424
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;
432
433 unlock_exit:
434         mutex_unlock(&dev->mtx);
435
436 exit:
437         return retval;
438 }
439
440 /** usb_alphatrack_poll */
441 static unsigned int usb_alphatrack_poll(struct file *file, poll_table *wait)
442 {
443         struct usb_alphatrack *dev;
444         unsigned int mask = 0;
445
446         dev = file->private_data;
447
448         poll_wait(file, &dev->read_wait, wait);
449         poll_wait(file, &dev->write_wait, wait);
450
451         if (dev->ring_head != dev->ring_tail)
452                 mask |= POLLIN | POLLRDNORM;
453         if (!dev->interrupt_out_busy)
454                 mask |= POLLOUT | POLLWRNORM;
455
456         return mask;
457 }
458
459 /** usb_alphatrack_read */
460 static ssize_t usb_alphatrack_read(struct file *file, char __user *buffer,
461                                    size_t count, loff_t *ppos)
462 {
463         struct usb_alphatrack *dev;
464         int retval = 0;
465
466         int c = 0;
467
468         dev = file->private_data;
469
470         /* verify that we actually have some data to read */
471         if (count == 0)
472                 goto exit;
473
474         /* lock this object */
475         if (mutex_lock_interruptible(&dev->mtx)) {
476                 retval = -ERESTARTSYS;
477                 goto exit;
478         }
479
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;
486         }
487
488         while (dev->ring_head == dev->ring_tail) {
489                 if (file->f_flags & O_NONBLOCK) {
490                         retval = -EAGAIN;
491                         goto unlock_exit;
492                 }
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;
499         }
500
501         alphatrack_ocmd_info(&dev->intf->dev,
502                              &(*dev->ring_buffer)[dev->ring_tail].cmd, "%s",
503                              ": copying to userspace");
504
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;
512                 }
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);
517         }
518         retval = c;
519
520 unlock_exit:
521         /* unlock the device */
522         mutex_unlock(&dev->mtx);
523
524 exit:
525         return retval;
526 }
527
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)
532 {
533         struct usb_alphatrack *dev;
534         size_t bytes_to_write;
535         int retval = 0;
536
537         dev = file->private_data;
538
539         /* verify that we actually have some data to write */
540         if (count == 0)
541                 goto exit;
542
543         /* lock this object */
544         if (mutex_lock_interruptible(&dev->mtx)) {
545                 retval = -ERESTARTSYS;
546                 goto exit;
547         }
548
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;
555         }
556
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;
562                 }
563                 retval =
564                     wait_event_interruptible(dev->write_wait,
565                                              !dev->interrupt_out_busy);
566                 if (retval < 0)
567                         goto unlock_exit;
568         }
569
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);
578
579         dbg_info(&dev->intf->dev, "%s: count = %zd, bytes_to_write = %zd\n",
580                  __func__, count, bytes_to_write);
581
582         if (copy_from_user(dev->interrupt_out_buffer, buffer, bytes_to_write)) {
583                 retval = -EFAULT;
584                 goto unlock_exit;
585         }
586
587         if (dev->interrupt_out_endpoint == NULL) {
588                 dev_err(&dev->intf->dev, "Endpoint should not be null!\n");
589                 goto unlock_exit;
590         }
591
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();
604
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;
612         }
613         retval = bytes_to_write;
614
615 unlock_exit:
616         /* unlock the device */
617         mutex_unlock(&dev->mtx);
618
619 exit:
620         return retval;
621 }
622
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,
632 };
633
634 /*
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
637  */
638
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,
643 };
644
645 /**
646  *      usb_alphatrack_probe
647  *
648  *      Called by the usb core when a new device is connected that it thinks
649  *      this driver might be interested in.
650  */
651 static int usb_alphatrack_probe(struct usb_interface *intf,
652                                 const struct usb_device_id *id)
653 {
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;
661
662         /* allocate memory for our device state and initialize it */
663
664         dev = kzalloc(sizeof(*dev), GFP_KERNEL);
665         if (dev == NULL)
666                 goto exit;
667
668         mutex_init(&dev->mtx);
669         dev->intf = intf;
670         init_waitqueue_head(&dev->read_wait);
671         init_waitqueue_head(&dev->write_wait);
672
673         iface_desc = intf->cur_altsetting;
674
675         /* set up the endpoint information */
676         for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
677                 endpoint = &iface_desc->endpoint[i].desc;
678
679                 if (usb_endpoint_is_int_in(endpoint))
680                         dev->interrupt_in_endpoint = endpoint;
681
682                 if (usb_endpoint_is_int_out(endpoint))
683                         dev->interrupt_out_endpoint = endpoint;
684         }
685         if (dev->interrupt_in_endpoint == NULL) {
686                 dev_err(&intf->dev, "Interrupt in endpoint not found\n");
687                 goto error;
688         }
689         if (dev->interrupt_out_endpoint == NULL)
690                 dev_warn(&intf->dev,
691                          "Interrupt out endpoint not found"
692                          "(using control endpoint instead)\n");
693
694         dev->interrupt_in_endpoint_size =
695             le16_to_cpu(dev->interrupt_in_endpoint->wMaxPacketSize);
696
697         if (dev->interrupt_in_endpoint_size != 64)
698                 dev_warn(&intf->dev, "Interrupt in endpoint size is not 64!\n");
699
700         if (ring_buffer_size == 0)
701                 ring_buffer_size = RING_BUFFER_SIZE;
702
703         true_size = min(ring_buffer_size, RING_BUFFER_SIZE);
704
705         /*
706          * FIXME - there are more usb_alloc routines for dma correctness.
707          * Needed?
708          */
709         dev->ring_buffer = kmalloc_array(true_size,
710                                          sizeof(struct alphatrack_icmd),
711                                          GFP_KERNEL);
712         if (!dev->ring_buffer)
713                 goto error;
714
715         dev->interrupt_in_buffer = kmalloc(dev->interrupt_in_endpoint_size,
716                                            GFP_KERNEL);
717         if (!dev->interrupt_in_buffer)
718                 goto error;
719
720         dev->oldi_buffer = kmalloc(dev->interrupt_in_endpoint_size, GFP_KERNEL);
721         if (!dev->oldi_buffer)
722                 goto error;
723
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;
728         }
729
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;
735
736         if (dev->interrupt_out_endpoint_size != 64)
737                 dev_warn(&intf->dev,
738                          "Interrupt out endpoint size is not 64!)\n");
739
740         if (write_buffer_size == 0)
741                 write_buffer_size = WRITE_BUFFER_SIZE;
742         true_size = min(write_buffer_size, WRITE_BUFFER_SIZE);
743
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;
750
751         dev->write_buffer = kmalloc_array(true_size,
752                                           sizeof(struct alphatrack_ocmd),
753                                           GFP_KERNEL);
754         if (!dev->write_buffer)
755                 goto error;
756
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;
761         }
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;
773
774         /* we can register the device now, as it is ready */
775         usb_set_intfdata(intf, dev);
776
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;
785         }
786
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);
792
793 exit:
794         return retval;
795
796 error:
797         usb_alphatrack_delete(dev);
798
799         return retval;
800 }
801
802 /**
803  *      usb_alphatrack_disconnect
804  *
805  *      Called by the usb core when the device is removed from the system.
806  */
807 static void usb_alphatrack_disconnect(struct usb_interface *intf)
808 {
809         struct usb_alphatrack *dev;
810         int minor;
811
812         mutex_lock(&disconnect_mutex);
813
814         dev = usb_get_intfdata(intf);
815         usb_set_intfdata(intf, NULL);
816
817         mutex_lock(&dev->mtx);
818
819         minor = intf->minor;
820
821         /* give back our minor */
822         usb_deregister_dev(intf, &usb_alphatrack_class);
823
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);
832         }
833
834         mutex_unlock(&disconnect_mutex);
835
836         dev_info(&intf->dev, "Alphatrack Surface #%d now disconnected\n",
837                  (minor - USB_ALPHATRACK_MINOR_BASE));
838 }
839
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,
846 };
847
848 module_usb_driver(usb_alphatrack_driver);