e2f82f0dbad87e8f12b6573e9b0dd3f4cb23670e
[pandora-kernel.git] / 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 /* 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.
33 */
34
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
43 #include <linux/uaccess.h>
44 #include <linux/smp_lock.h>
45 #include <linux/input.h>
46 #include <linux/usb.h>
47 #include <linux/poll.h>
48
49 #include "alphatrack.h"
50
51 #define VENDOR_ID       0x165b
52 #define PRODUCT_ID      0xfad1
53
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
60
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 */
65 };
66
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");
73
74 /* These aren't done yet */
75
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
80
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
88
89 static int debug = ALPHATRACK_DEBUG;
90
91 /* Use our own dbg macro */
92 #define dbg_info(dev, format, arg...) do \
93     { if (debug) dev_info(dev , format , ## arg); } while (0)
94
95 #define alphatrack_ocmd_info(dev, cmd, format, arg...)
96
97 #define alphatrack_icmd_info(dev, cmd, format, arg...)
98
99 /* Module parameters */
100
101 module_param(debug, int, S_IRUGO | S_IWUSR);
102 MODULE_PARM_DESC(debug, "Debug enabled or not");
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
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");
119
120 /*
121  * Increase the interval for debugging purposes.
122  * or set to 1 to use the standard interval from the endpoint descriptors.
123  */
124
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");
129
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");
134
135 /* Structure to hold all of our device specific stuff */
136
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 */
141
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;
147
148         wait_queue_head_t read_wait;
149         wait_queue_head_t write_wait;
150
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;
159
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;
166
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];
177 };
178
179 /* prevent races between open() and disconnect() */
180 static DEFINE_MUTEX(disconnect_mutex);
181
182 /* forward declaration */
183
184 static struct usb_driver usb_alphatrack_driver;
185
186 /**
187  *      usb_alphatrack_abort_transfers
188  *      aborts transfers and frees associated data structures
189  */
190 static void usb_alphatrack_abort_transfers(struct usb_alphatrack *dev)
191 {
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);
197         }
198         if (dev->interrupt_out_busy)
199                 if (dev->intf)
200                         usb_kill_urb(dev->interrupt_out_urb);
201 }
202
203 /**
204  *      usb_alphatrack_delete
205  */
206 static void usb_alphatrack_delete(struct usb_alphatrack *dev)
207 {
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 */
215 }
216
217 /**
218  *      usb_alphatrack_interrupt_in_callback
219  */
220
221 static void usb_alphatrack_interrupt_in_callback(struct urb *urb)
222 {
223         struct usb_alphatrack *dev = urb->context;
224         unsigned int next_ring_head;
225         int retval = -1;
226
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 */
236                 }
237         }
238
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;
251                 }
252                 memcpy(dev->oldi_buffer, dev->interrupt_in_buffer,
253                        INPUT_CMD_SIZE);
254
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;
261                 }
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;
271
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);
283                 }
284         }
285
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);
293         }
294
295 exit:
296         dev->interrupt_in_done = 1;
297         wake_up_interruptible(&dev->read_wait);
298 }
299
300 /**
301  *      usb_alphatrack_interrupt_out_callback
302  */
303 static void usb_alphatrack_interrupt_out_callback(struct urb *urb)
304 {
305         struct usb_alphatrack *dev = urb->context;
306
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);
317 }
318
319 /**
320  *      usb_alphatrack_open
321  */
322 static int usb_alphatrack_open(struct inode *inode, struct file *file)
323 {
324         struct usb_alphatrack *dev;
325         int subminor;
326         int retval = 0;
327         struct usb_interface *interface;
328
329         lock_kernel();
330         nonseekable_open(inode, file);
331         subminor = iminor(inode);
332
333         mutex_lock(&disconnect_mutex);
334
335         interface = usb_find_interface(&usb_alphatrack_driver, subminor);
336
337         if (!interface) {
338                 err("%s - error, can't find device for minor %d\n",
339                     __func__, subminor);
340                 retval = -ENODEV;
341                 goto unlock_disconnect_exit;
342         }
343
344         dev = usb_get_intfdata(interface);
345
346         if (!dev) {
347                 retval = -ENODEV;
348                 goto unlock_disconnect_exit;
349         }
350
351         /* lock this device */
352         if (down_interruptible(&dev->sem)) {
353                 retval = -ERESTARTSYS;
354                 goto unlock_disconnect_exit;
355         }
356
357         /* allow opening only once */
358         if (dev->open_count) {
359                 retval = -EBUSY;
360                 goto unlock_exit;
361         }
362         dev->open_count = 1;
363
364         /* initialize in direction */
365         dev->ring_head = 0;
366         dev->ring_tail = 0;
367         usb_fill_int_urb(dev->interrupt_in_urb,
368                          interface_to_usbdev(interface),
369                          usb_rcvintpipe(interface_to_usbdev(interface),
370                                         dev->interrupt_in_endpoint->
371                                         bEndpointAddress),
372                          dev->interrupt_in_buffer,
373                          dev->interrupt_in_endpoint_size,
374                          usb_alphatrack_interrupt_in_callback, dev,
375                          dev->interrupt_in_interval);
376
377         dev->interrupt_in_running = 1;
378         dev->interrupt_in_done = 0;
379         dev->enable = 1;
380         dev->offline = 0;
381
382         retval = usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL);
383         if (retval) {
384                 dev_err(&interface->dev,
385                         "Couldn't submit interrupt_in_urb %d\n", retval);
386                 dev->interrupt_in_running = 0;
387                 dev->open_count = 0;
388                 goto unlock_exit;
389         }
390
391         /* save device in the file's private structure */
392         file->private_data = dev;
393
394 unlock_exit:
395         up(&dev->sem);
396
397 unlock_disconnect_exit:
398         mutex_unlock(&disconnect_mutex);
399         unlock_kernel();
400
401         return retval;
402 }
403
404 /**
405  *      usb_alphatrack_release
406  */
407 static int usb_alphatrack_release(struct inode *inode, struct file *file)
408 {
409         struct usb_alphatrack *dev;
410         int retval = 0;
411
412         dev = file->private_data;
413
414         if (dev == NULL) {
415                 retval = -ENODEV;
416                 goto exit;
417         }
418
419         if (down_interruptible(&dev->sem)) {
420                 retval = -ERESTARTSYS;
421                 goto exit;
422         }
423
424         if (dev->open_count != 1) {
425                 retval = -ENODEV;
426                 goto unlock_exit;
427         }
428
429         if (dev->intf == NULL) {
430                 /* the device was unplugged before the file was released */
431                 up(&dev->sem);
432                 /* unlock here as usb_alphatrack_delete frees dev */
433                 usb_alphatrack_delete(dev);
434                 retval = -ENODEV;
435                 goto exit;
436         }
437
438         /* wait until write transfer is finished */
439         if (dev->interrupt_out_busy)
440                 wait_event_interruptible_timeout(dev->write_wait,
441                                                  !dev->interrupt_out_busy,
442                                                  2 * HZ);
443         usb_alphatrack_abort_transfers(dev);
444         dev->open_count = 0;
445
446 unlock_exit:
447         up(&dev->sem);
448
449 exit:
450         return retval;
451 }
452
453 /**
454  *      usb_alphatrack_poll
455  */
456 static unsigned int usb_alphatrack_poll(struct file *file, poll_table * wait)
457 {
458         struct usb_alphatrack *dev;
459         unsigned int mask = 0;
460
461         dev = file->private_data;
462
463         poll_wait(file, &dev->read_wait, wait);
464         poll_wait(file, &dev->write_wait, wait);
465
466         if (dev->ring_head != dev->ring_tail)
467                 mask |= POLLIN | POLLRDNORM;
468         if (!dev->interrupt_out_busy)
469                 mask |= POLLOUT | POLLWRNORM;
470
471         return mask;
472 }
473
474 /**
475  *      usb_alphatrack_read
476  */
477 static ssize_t usb_alphatrack_read(struct file *file, char __user *buffer,
478                                    size_t count, loff_t *ppos)
479 {
480         struct usb_alphatrack *dev;
481         int retval = 0;
482
483         int c = 0;
484
485         dev = file->private_data;
486
487         /* verify that we actually have some data to read */
488         if (count == 0)
489                 goto exit;
490
491         /* lock this object */
492         if (down_interruptible(&dev->sem)) {
493                 retval = -ERESTARTSYS;
494                 goto exit;
495         }
496
497         /* verify that the device wasn't unplugged */
498         if (dev->intf == NULL) {
499                 retval = -ENODEV;
500                 err("No device or device unplugged %d\n", retval);
501                 goto unlock_exit;
502         }
503
504         while (dev->ring_head == dev->ring_tail) {
505                 if (file->f_flags & O_NONBLOCK) {
506                         retval = -EAGAIN;
507                         goto unlock_exit;
508                 }
509                 dev->interrupt_in_done = 0;
510                 retval =
511                     wait_event_interruptible(dev->read_wait,
512                                              dev->interrupt_in_done);
513                 if (retval < 0)
514                         goto unlock_exit;
515         }
516
517         alphatrack_ocmd_info(&dev->intf->dev,
518                              &(*dev->ring_buffer)[dev->ring_tail].cmd, "%s",
519                              ": copying to userspace");
520
521         c = 0;
522         while ((c < count) && (dev->ring_tail != dev->ring_head)) {
523                 if (copy_to_user
524                     (&buffer[c], &(*dev->ring_buffer)[dev->ring_tail],
525                      INPUT_CMD_SIZE)) {
526                         retval = -EFAULT;
527                         goto unlock_exit;
528                 }
529                 dev->ring_tail = (dev->ring_tail + 1) % ring_buffer_size;
530                 c += INPUT_CMD_SIZE;
531                 dbg_info(&dev->intf->dev, "%s: head, tail are %x, %x\n",
532                          __func__, dev->ring_head, dev->ring_tail);
533         }
534         retval = c;
535
536 unlock_exit:
537         /* unlock the device */
538         up(&dev->sem);
539
540 exit:
541         return retval;
542 }
543
544 /**
545  *      usb_alphatrack_write
546  */
547 static ssize_t usb_alphatrack_write(struct file *file,
548                                     const char __user *buffer, size_t count,
549                                     loff_t *ppos)
550 {
551         struct usb_alphatrack *dev;
552         size_t bytes_to_write;
553         int retval = 0;
554
555         dev = file->private_data;
556
557         /* verify that we actually have some data to write */
558         if (count == 0)
559                 goto exit;
560
561         /* lock this object */
562         if (down_interruptible(&dev->sem)) {
563                 retval = -ERESTARTSYS;
564                 goto exit;
565         }
566
567         /* verify that the device wasn't unplugged */
568         if (dev->intf == NULL) {
569                 retval = -ENODEV;
570                 err("No device or device unplugged %d\n", retval);
571                 goto unlock_exit;
572         }
573
574         /* wait until previous transfer is finished */
575         if (dev->interrupt_out_busy) {
576                 if (file->f_flags & O_NONBLOCK) {
577                         retval = -EAGAIN;
578                         goto unlock_exit;
579                 }
580                 retval =
581                     wait_event_interruptible(dev->write_wait,
582                                              !dev->interrupt_out_busy);
583                 if (retval < 0)
584                         goto unlock_exit;
585         }
586
587         /* write the data into interrupt_out_buffer from userspace */
588         /* FIXME - if you write more than 12 bytes this breaks */
589         bytes_to_write =
590             min(count, write_buffer_size * dev->interrupt_out_endpoint_size);
591         if (bytes_to_write < count)
592                 dev_warn(&dev->intf->dev,
593                          "Write buffer overflow, %zd bytes dropped\n",
594                          count - bytes_to_write);
595
596         dbg_info(&dev->intf->dev, "%s: count = %zd, bytes_to_write = %zd\n",
597                  __func__, count, bytes_to_write);
598
599         if (copy_from_user(dev->interrupt_out_buffer, buffer, bytes_to_write)) {
600                 retval = -EFAULT;
601                 goto unlock_exit;
602         }
603
604         if (dev->interrupt_out_endpoint == NULL) {
605                 err("Endpoint should not be be null! \n");
606                 goto unlock_exit;
607         }
608
609         /* send off the urb */
610         usb_fill_int_urb(dev->interrupt_out_urb,
611                          interface_to_usbdev(dev->intf),
612                          usb_sndintpipe(interface_to_usbdev(dev->intf),
613                                         dev->interrupt_out_endpoint->
614                                         bEndpointAddress),
615                          dev->interrupt_out_buffer, bytes_to_write,
616                          usb_alphatrack_interrupt_out_callback, dev,
617                          dev->interrupt_out_interval);
618         dev->interrupt_out_busy = 1;
619         atomic_inc(&dev->writes_pending);
620         wmb();
621
622         retval = usb_submit_urb(dev->interrupt_out_urb, GFP_KERNEL);
623         if (retval) {
624                 dev->interrupt_out_busy = 0;
625                 err("Couldn't submit interrupt_out_urb %d\n", retval);
626                 atomic_dec(&dev->writes_pending);
627                 goto unlock_exit;
628         }
629         retval = bytes_to_write;
630
631 unlock_exit:
632         /* unlock the device */
633         up(&dev->sem);
634
635 exit:
636         return retval;
637 }
638
639 /* file operations needed when we register this driver */
640 static const struct file_operations usb_alphatrack_fops = {
641         .owner = THIS_MODULE,
642         .read = usb_alphatrack_read,
643         .write = usb_alphatrack_write,
644         .open = usb_alphatrack_open,
645         .release = usb_alphatrack_release,
646         .poll = usb_alphatrack_poll,
647 };
648
649 /*
650  * usb class driver info in order to get a minor number from the usb core,
651  * and to have the device registered with the driver core
652  */
653
654 static struct usb_class_driver usb_alphatrack_class = {
655         .name = "alphatrack%d",
656         .fops = &usb_alphatrack_fops,
657         .minor_base = USB_ALPHATRACK_MINOR_BASE,
658 };
659
660 /**
661  *      usb_alphatrack_probe
662  *
663  *      Called by the usb core when a new device is connected that it thinks
664  *      this driver might be interested in.
665  */
666 static int usb_alphatrack_probe(struct usb_interface *intf,
667                                 const struct usb_device_id *id)
668 {
669         struct usb_device *udev = interface_to_usbdev(intf);
670         struct usb_alphatrack *dev = NULL;
671         struct usb_host_interface *iface_desc;
672         struct usb_endpoint_descriptor *endpoint;
673         int i;
674         int true_size;
675         int retval = -ENOMEM;
676
677         /* allocate memory for our device state and intialize it */
678
679         dev = kzalloc(sizeof(*dev), GFP_KERNEL);
680         if (dev == NULL) {
681                 dev_err(&intf->dev, "Out of memory\n");
682                 goto exit;
683         }
684         init_MUTEX(&dev->sem);
685         dev->intf = intf;
686         init_waitqueue_head(&dev->read_wait);
687         init_waitqueue_head(&dev->write_wait);
688
689         iface_desc = intf->cur_altsetting;
690
691         /* set up the endpoint information */
692         for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
693                 endpoint = &iface_desc->endpoint[i].desc;
694
695                 if (usb_endpoint_is_int_in(endpoint))
696                         dev->interrupt_in_endpoint = endpoint;
697
698                 if (usb_endpoint_is_int_out(endpoint))
699                         dev->interrupt_out_endpoint = endpoint;
700         }
701         if (dev->interrupt_in_endpoint == NULL) {
702                 dev_err(&intf->dev, "Interrupt in endpoint not found\n");
703                 goto error;
704         }
705         if (dev->interrupt_out_endpoint == NULL)
706                 dev_warn(&intf->dev,
707                          "Interrupt out endpoint not found"
708                          "(using control endpoint instead)\n");
709
710         dev->interrupt_in_endpoint_size =
711             le16_to_cpu(dev->interrupt_in_endpoint->wMaxPacketSize);
712
713         if (dev->interrupt_in_endpoint_size != 64)
714                 dev_warn(&intf->dev, "Interrupt in endpoint size is not 64!\n");
715
716         if (ring_buffer_size == 0)
717                 ring_buffer_size = RING_BUFFER_SIZE;
718
719         true_size = min(ring_buffer_size, RING_BUFFER_SIZE);
720
721         /* FIXME - there are more usb_alloc routines for dma correctness.
722            Needed? */
723         dev->ring_buffer =
724             kmalloc((true_size * sizeof(struct alphatrack_icmd)), GFP_KERNEL);
725
726         if (!dev->ring_buffer) {
727                 dev_err(&intf->dev,
728                         "Couldn't allocate input ring_buffer of size %d\n",
729                         true_size);
730                 goto error;
731         }
732
733         dev->interrupt_in_buffer =
734             kmalloc(dev->interrupt_in_endpoint_size, GFP_KERNEL);
735
736         if (!dev->interrupt_in_buffer) {
737                 dev_err(&intf->dev, "Couldn't allocate interrupt_in_buffer\n");
738                 goto error;
739         }
740         dev->oldi_buffer = kmalloc(dev->interrupt_in_endpoint_size, GFP_KERNEL);
741         if (!dev->oldi_buffer) {
742                 dev_err(&intf->dev, "Couldn't allocate old buffer\n");
743                 goto error;
744         }
745         dev->interrupt_in_urb = usb_alloc_urb(0, GFP_KERNEL);
746         if (!dev->interrupt_in_urb) {
747                 dev_err(&intf->dev, "Couldn't allocate interrupt_in_urb\n");
748                 goto error;
749         }
750
751         dev->interrupt_out_endpoint_size =
752             dev->interrupt_out_endpoint ? le16_to_cpu(dev->
753                                                       interrupt_out_endpoint->
754                                                       wMaxPacketSize) : udev->
755             descriptor.bMaxPacketSize0;
756
757         if (dev->interrupt_out_endpoint_size != 64)
758                 dev_warn(&intf->dev,
759                          "Interrupt out endpoint size is not 64!)\n");
760
761         if (write_buffer_size == 0)
762                 write_buffer_size = WRITE_BUFFER_SIZE;
763         true_size = min(write_buffer_size, WRITE_BUFFER_SIZE);
764
765         dev->interrupt_out_buffer =
766             kmalloc(true_size * dev->interrupt_out_endpoint_size, GFP_KERNEL);
767
768         if (!dev->interrupt_out_buffer) {
769                 dev_err(&intf->dev, "Couldn't allocate interrupt_out_buffer\n");
770                 goto error;
771         }
772
773         dev->write_buffer =
774             kmalloc(sizeof(struct alphatrack_ocmd) * true_size, GFP_KERNEL);
775
776         if (!dev->write_buffer) {
777                 dev_err(&intf->dev, "Couldn't allocate write_buffer \n");
778                 goto error;
779         }
780
781         dev->interrupt_out_urb = usb_alloc_urb(0, GFP_KERNEL);
782         if (!dev->interrupt_out_urb) {
783                 dev_err(&intf->dev, "Couldn't allocate interrupt_out_urb\n");
784                 goto error;
785         }
786         dev->interrupt_in_interval =
787             min_interrupt_in_interval >
788             dev->interrupt_in_endpoint->
789             bInterval ? min_interrupt_in_interval : dev->interrupt_in_endpoint->
790             bInterval;
791         if (dev->interrupt_out_endpoint)
792                 dev->interrupt_out_interval =
793                     min_interrupt_out_interval >
794                     dev->interrupt_out_endpoint->
795                     bInterval ? min_interrupt_out_interval : dev->
796                     interrupt_out_endpoint->bInterval;
797
798         /* we can register the device now, as it is ready */
799         usb_set_intfdata(intf, dev);
800
801         atomic_set(&dev->writes_pending, 0);
802         retval = usb_register_dev(intf, &usb_alphatrack_class);
803         if (retval) {
804                 /* something prevented us from registering this driver */
805                 dev_err(&intf->dev,
806                         "Not able to get a minor for this device.\n");
807                 usb_set_intfdata(intf, NULL);
808                 goto error;
809         }
810
811         /* let the user know what node this device is now attached to */
812         dev_info(&intf->dev,
813                  "Alphatrack Device #%d now attached to major %d minor %d\n",
814                  (intf->minor - USB_ALPHATRACK_MINOR_BASE), USB_MAJOR,
815                  intf->minor);
816
817 exit:
818         return retval;
819
820 error:
821         usb_alphatrack_delete(dev);
822
823         return retval;
824 }
825
826 /**
827  *      usb_alphatrack_disconnect
828  *
829  *      Called by the usb core when the device is removed from the system.
830  */
831 static void usb_alphatrack_disconnect(struct usb_interface *intf)
832 {
833         struct usb_alphatrack *dev;
834         int minor;
835
836         mutex_lock(&disconnect_mutex);
837
838         dev = usb_get_intfdata(intf);
839         usb_set_intfdata(intf, NULL);
840
841         down(&dev->sem);
842
843         minor = intf->minor;
844
845         /* give back our minor */
846         usb_deregister_dev(intf, &usb_alphatrack_class);
847
848         /* if the device is not opened, then we clean up right now */
849         if (!dev->open_count) {
850                 up(&dev->sem);
851                 usb_alphatrack_delete(dev);
852         } else {
853                 dev->intf = NULL;
854                 up(&dev->sem);
855         }
856
857         atomic_set(&dev->writes_pending, 0);
858         mutex_unlock(&disconnect_mutex);
859
860         dev_info(&intf->dev, "Alphatrack Surface #%d now disconnected\n",
861                  (minor - USB_ALPHATRACK_MINOR_BASE));
862 }
863
864 /* usb specific object needed to register this driver with the usb subsystem */
865 static struct usb_driver usb_alphatrack_driver = {
866         .name = "alphatrack",
867         .probe = usb_alphatrack_probe,
868         .disconnect = usb_alphatrack_disconnect,
869         .id_table = usb_alphatrack_table,
870 };
871
872 /**
873  *      usb_alphatrack_init
874  */
875 static int __init usb_alphatrack_init(void)
876 {
877         int retval;
878
879         /* register this driver with the USB subsystem */
880         retval = usb_register(&usb_alphatrack_driver);
881         if (retval)
882                 err("usb_register failed for the " __FILE__
883                     " driver. Error number %d\n", retval);
884
885         return retval;
886 }
887
888 /**
889  *      usb_alphatrack_exit
890  */
891 static void __exit usb_alphatrack_exit(void)
892 {
893         /* deregister this driver with the USB subsystem */
894         usb_deregister(&usb_alphatrack_driver);
895 }
896
897 module_init(usb_alphatrack_init);
898 module_exit(usb_alphatrack_exit);