]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/staging/frontier/alphatrack.c
Staging: frontier: remove unused alphatrack_sysfs.c file
[linux-2.6-omap-h63xx.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 #include <linux/version.h>
43
44 #include <asm/uaccess.h>
45 #include <linux/input.h>
46 #include <linux/usb.h>
47 #include <linux/poll.h>
48
49 #include "surface_sysfs.h"
50
51 /* make this work on older kernel versions */
52
53 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19)
54 #include "frontier_compat.h"
55 #endif /* older kernel versions */
56
57 #include "alphatrack.h"
58
59 #define VENDOR_ID       0x165b
60 #define PRODUCT_ID      0xfad1
61
62 #ifdef CONFIG_USB_DYNAMIC_MINORS
63 #define USB_ALPHATRACK_MINOR_BASE       0
64 #else
65 // FIXME 176 - is another driver's minor - apply for that
66 // #define USB_ALPHATRACK_MINOR_BASE    177
67 #define USB_ALPHATRACK_MINOR_BASE       176
68 #endif
69
70 /* table of devices that work with this driver */
71 static struct usb_device_id usb_alphatrack_table [] = {
72         { USB_DEVICE(VENDOR_ID, PRODUCT_ID) },
73         { }                                     /* Terminating entry */
74 };
75
76 MODULE_DEVICE_TABLE(usb, usb_alphatrack_table);
77 MODULE_VERSION("0.40");
78 MODULE_AUTHOR("Mike Taht <m@taht.net>");
79 MODULE_DESCRIPTION("Alphatrack USB Driver");
80 MODULE_LICENSE("GPL");
81 MODULE_SUPPORTED_DEVICE("Frontier Designs Alphatrack Control Surface");
82
83 /* These aren't done yet */
84
85 #define SUPPRESS_EXTRA_ONLINE_EVENTS 0
86 #define BUFFERED_WRITES 0
87 #define SUPPRESS_EXTRA_OFFLINE_EVENTS 0
88 #define COMPRESS_FADER_EVENTS 0
89
90 #define BUFFERED_READS 1
91 #define RING_BUFFER_SIZE 512
92 #define WRITE_BUFFER_SIZE 34
93 #define ALPHATRACK_USB_TIMEOUT 10
94 #define OUTPUT_CMD_SIZE 8
95 #define INPUT_CMD_SIZE 12
96
97
98 static int debug = 0;
99
100 /* Use our own dbg macro */
101 #define dbg_info(dev, format, arg...) do { if (debug) dev_info(dev , format , ## arg); } while (0)
102
103 #if 0
104 #define alphatrack_ocmd_info(dev, cmd, format, arg...) do { if (debug) ocmd_info(dev , cmd , format, ## arg); } while (0)
105
106 #define alphatrack_icmd_info(dev, cmd, format, arg...) do { if (debug) icmd_info(dev , cmd, format, ## arg); } while (0)
107 #else
108 #define alphatrack_ocmd_info(dev, cmd, format, arg...)
109
110 #define alphatrack_icmd_info(dev, cmd, format, arg...)
111
112 #endif
113
114 /* Module parameters */
115
116 module_param(debug, int, S_IRUGO | S_IWUSR);
117 MODULE_PARM_DESC(debug, "Debug enabled or not");
118
119 /* All interrupt in transfers are collected in a ring buffer to
120  * avoid racing conditions and get better performance of the driver.
121  */
122
123 static int ring_buffer_size = RING_BUFFER_SIZE;
124
125 module_param(ring_buffer_size, int,  S_IRUGO);
126 MODULE_PARM_DESC(ring_buffer_size, "Read ring buffer size");
127
128 /* The write_buffer can one day contain more than one interrupt out transfer.
129  */
130
131 static int write_buffer_size = WRITE_BUFFER_SIZE;
132 module_param(write_buffer_size, int,  S_IRUGO);
133 MODULE_PARM_DESC(write_buffer_size, "Write buffer size");
134
135 /*
136  * Increase the interval for debugging purposes.
137  * or set to 1 to use the standard interval from the endpoint descriptors.
138  */
139
140 static int min_interrupt_in_interval = ALPHATRACK_USB_TIMEOUT;
141 module_param(min_interrupt_in_interval, int, 0);
142 MODULE_PARM_DESC(min_interrupt_in_interval, "Minimum interrupt in interval in ms");
143
144 static int min_interrupt_out_interval = ALPHATRACK_USB_TIMEOUT;
145 module_param(min_interrupt_out_interval, int, 0);
146 MODULE_PARM_DESC(min_interrupt_out_interval, "Minimum interrupt out interval in ms");
147
148
149
150 /* Structure to hold all of our device specific stuff */
151
152 struct usb_alphatrack {
153         struct semaphore        sem;            /* locks this structure */
154         struct usb_interface*   intf;           /* save off the usb interface pointer */
155         int                     open_count;     /* number of times this port has been opened */
156
157         struct alphatrack_icmd  (*ring_buffer)[RING_BUFFER_SIZE]; /* just make c happy */
158         struct alphatrack_ocmd  (*write_buffer)[WRITE_BUFFER_SIZE]; /* just make c happy */
159         unsigned int            ring_head;
160         unsigned int            ring_tail;
161
162         wait_queue_head_t       read_wait;
163         wait_queue_head_t       write_wait;
164
165         unsigned char*          interrupt_in_buffer;
166         unsigned char*          oldi_buffer;
167         struct usb_endpoint_descriptor* interrupt_in_endpoint;
168         struct urb*             interrupt_in_urb;
169         int                     interrupt_in_interval;
170         size_t                  interrupt_in_endpoint_size;
171         int                     interrupt_in_running;
172         int                     interrupt_in_done;
173
174         char*                   interrupt_out_buffer;
175         struct usb_endpoint_descriptor* interrupt_out_endpoint;
176         struct urb*             interrupt_out_urb;
177         int                     interrupt_out_interval;
178         size_t                  interrupt_out_endpoint_size;
179         int                     interrupt_out_busy;
180
181         atomic_t writes_pending;
182         int event; /* alternate interface to events */
183         int fader; /* 10 bits */
184         int lights; /* 23 bits */
185         unsigned char dump_state; /* 0 if disabled 1 if enabled */
186         unsigned char enable; /* 0 if disabled 1 if enabled */
187         unsigned char offline; /* if the device is out of range or asleep */
188         unsigned char verbose; /* be verbose in error reporting */
189         unsigned char  last_cmd[OUTPUT_CMD_SIZE];
190         unsigned char  screen[32];
191 };
192
193 /* prevent races between open() and disconnect() */
194 static DEFINE_MUTEX(disconnect_mutex);
195
196 /* forward declaration */
197
198 static struct usb_driver usb_alphatrack_driver;
199
200 static void icmd_info(struct usb_alphatrack *dev, char *cmd, char *str, char *a) {
201 /*
202 if (dev->verbose) {
203 } else {
204 }
205 */
206 }
207
208 static void ocmd_info(struct usb_alphatrack *dev, char *cmd, char *str, char* a) {
209 /*
210 if (dev->verbose) {
211 } else {
212 }
213 */
214 }
215
216
217 /**
218  *      usb_alphatrack_abort_transfers
219  *      aborts transfers and frees associated data structures
220  */
221 static void usb_alphatrack_abort_transfers(struct usb_alphatrack *dev)
222 {
223         /* shutdown transfer */
224         if (dev->interrupt_in_running) {
225                 dev->interrupt_in_running = 0;
226                 if (dev->intf)
227                         usb_kill_urb(dev->interrupt_in_urb);
228         }
229         if (dev->interrupt_out_busy)
230                 if (dev->intf)
231                         usb_kill_urb(dev->interrupt_out_urb);
232 }
233
234 /**
235  *      usb_alphatrack_delete
236  */
237 static void usb_alphatrack_delete(struct usb_alphatrack *dev)
238 {
239         usb_alphatrack_abort_transfers(dev);
240         usb_free_urb(dev->interrupt_in_urb);
241         usb_free_urb(dev->interrupt_out_urb);
242         kfree(dev->ring_buffer);
243         kfree(dev->interrupt_in_buffer);
244         kfree(dev->interrupt_out_buffer);
245         kfree(dev); // fixme oldi_buffer
246 }
247
248 /**
249  *      usb_alphatrack_interrupt_in_callback
250  */
251
252 static void usb_alphatrack_interrupt_in_callback(struct urb *urb)
253 {
254         struct usb_alphatrack *dev = urb->context;
255         unsigned int next_ring_head;
256         int retval = -1;
257         int *iptr;
258
259         if (urb->status) {
260                 if (urb->status == -ENOENT ||
261                     urb->status == -ECONNRESET ||
262                     urb->status == -ESHUTDOWN) {
263                         goto exit;
264                 } else {
265                         dbg_info(&dev->intf->dev, "%s: nonzero status received: %d\n",
266                                  __FUNCTION__, urb->status);
267                         goto resubmit; /* maybe we can recover */
268                 }
269         }
270
271         if (urb->actual_length != INPUT_CMD_SIZE) {
272                 dev_warn(&dev->intf->dev,
273                          "Urb length was %d bytes!! Do something intelligent \n", urb->actual_length);
274         } else {
275                  alphatrack_ocmd_info(&dev->intf->dev,&(*dev->ring_buffer)[dev->ring_tail].cmd,"%s", "bla");
276                  if(memcmp(dev->interrupt_in_buffer,dev->oldi_buffer,INPUT_CMD_SIZE)==0) {
277                                                 goto resubmit;
278                 }
279                 memcpy(dev->oldi_buffer,dev->interrupt_in_buffer,INPUT_CMD_SIZE);
280
281 #if SUPPRESS_EXTRA_OFFLINE_EVENTS
282         if(dev->offline == 2 && dev->interrupt_in_buffer[1] == 0xff) { goto resubmit; }
283                 if(dev->offline == 1 && dev->interrupt_in_buffer[1] == 0xff) { dev->offline = 2; goto resubmit; }
284 /* Always pass one offline event up the stack */
285                 if(dev->offline > 0 && dev->interrupt_in_buffer[1] != 0xff) { dev->offline = 0; }
286                 if(dev->offline == 0 && dev->interrupt_in_buffer[1] == 0xff) { dev->offline = 1; }
287 #endif
288                 dbg_info(&dev->intf->dev, "%s: head, tail are %x, %x\n", __FUNCTION__,dev->ring_head,dev->ring_tail);
289                 next_ring_head = (dev->ring_head+1) % ring_buffer_size;
290
291                 if (next_ring_head != dev->ring_tail) {
292                         memcpy(&((*dev->ring_buffer)[dev->ring_head]),
293                                                  dev->interrupt_in_buffer, urb->actual_length);
294                         dev->ring_head = next_ring_head;
295                         retval = 0;
296                         memset(dev->interrupt_in_buffer, 0, urb->actual_length);
297                 } else {
298                         dev_warn(&dev->intf->dev,
299                                  "Ring buffer overflow, %d bytes dropped\n",
300                                  urb->actual_length);
301                         memset(dev->interrupt_in_buffer, 0, urb->actual_length);
302                 }
303         }
304
305 resubmit:
306         /* resubmit if we're still running */
307         if (dev->interrupt_in_running && dev->intf) {
308                 retval = usb_submit_urb(dev->interrupt_in_urb, GFP_ATOMIC);
309                 if (retval)
310                         dev_err(&dev->intf->dev,
311                                 "usb_submit_urb failed (%d)\n", retval);
312         }
313
314 exit:
315         dev->interrupt_in_done = 1;
316         wake_up_interruptible(&dev->read_wait);
317 }
318
319 /**
320  *      usb_alphatrack_interrupt_out_callback
321  */
322 static void usb_alphatrack_interrupt_out_callback(struct urb *urb)
323 {
324         struct usb_alphatrack *dev = urb->context;
325
326         /* sync/async unlink faults aren't errors */
327         if (urb->status && !(urb->status == -ENOENT ||
328                              urb->status == -ECONNRESET ||
329                              urb->status == -ESHUTDOWN))
330                 dbg_info(&dev->intf->dev,
331                          "%s - nonzero write interrupt status received: %d\n",
332                          __FUNCTION__, urb->status);
333         atomic_dec(&dev->writes_pending);
334         dev->interrupt_out_busy = 0;
335         wake_up_interruptible(&dev->write_wait);
336 }
337
338 /**
339  *      usb_alphatrack_open
340  */
341 static int usb_alphatrack_open(struct inode *inode, struct file *file)
342 {
343         struct usb_alphatrack *dev;
344         int subminor;
345         int retval = 0;
346         struct usb_interface *interface;
347
348         nonseekable_open(inode, file);
349         subminor = iminor(inode);
350
351         mutex_lock(&disconnect_mutex);
352
353         interface = usb_find_interface(&usb_alphatrack_driver, subminor);
354
355         if (!interface) {
356                 err("%s - error, can't find device for minor %d\n",
357                      __FUNCTION__, subminor);
358                 retval = -ENODEV;
359                 goto unlock_disconnect_exit;
360         }
361
362         dev = usb_get_intfdata(interface);
363
364         if (!dev) {
365                 retval = -ENODEV;
366                 goto unlock_disconnect_exit;
367         }
368
369         /* lock this device */
370         if (down_interruptible(&dev->sem)) {
371                 retval = -ERESTARTSYS;
372                 goto unlock_disconnect_exit;
373         }
374
375         /* allow opening only once */
376         if (dev->open_count) {
377                 retval = -EBUSY;
378                 goto unlock_exit;
379         }
380         dev->open_count = 1;
381
382         /* initialize in direction */
383         dev->ring_head = 0;
384         dev->ring_tail = 0;
385         usb_fill_int_urb(dev->interrupt_in_urb,
386                          interface_to_usbdev(interface),
387                          usb_rcvintpipe(interface_to_usbdev(interface),
388                                         dev->interrupt_in_endpoint->bEndpointAddress),
389                          dev->interrupt_in_buffer,
390                          dev->interrupt_in_endpoint_size,
391                          usb_alphatrack_interrupt_in_callback,
392                          dev,
393                          dev->interrupt_in_interval);
394
395         dev->interrupt_in_running = 1;
396         dev->interrupt_in_done = 0;
397         dev->enable = 1;
398         dev->offline = 0;
399
400         retval = usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL);
401         if (retval) {
402                 dev_err(&interface->dev, "Couldn't submit interrupt_in_urb %d\n", retval);
403                 dev->interrupt_in_running = 0;
404                 dev->open_count = 0;
405                 goto unlock_exit;
406         }
407
408         /* save device in the file's private structure */
409         file->private_data = dev;
410
411
412 unlock_exit:
413         up(&dev->sem);
414
415 unlock_disconnect_exit:
416         mutex_unlock(&disconnect_mutex);
417
418         return retval;
419 }
420
421 /**
422  *      usb_alphatrack_release
423  */
424 static int usb_alphatrack_release(struct inode *inode, struct file *file)
425 {
426         struct usb_alphatrack *dev;
427         int retval = 0;
428
429         dev = file->private_data;
430
431         if (dev == NULL) {
432                 retval = -ENODEV;
433                 goto exit;
434         }
435
436         if (down_interruptible(&dev->sem)) {
437                 retval = -ERESTARTSYS;
438                 goto exit;
439         }
440
441         if (dev->open_count != 1) {
442                 retval = -ENODEV;
443                 goto unlock_exit;
444         }
445
446         if (dev->intf == NULL) {
447                 /* the device was unplugged before the file was released */
448                 up(&dev->sem);
449                 /* unlock here as usb_alphatrack_delete frees dev */
450                 usb_alphatrack_delete(dev);
451                 retval = -ENODEV;
452                 goto exit;
453         }
454
455         /* wait until write transfer is finished */
456         if (dev->interrupt_out_busy)
457                 wait_event_interruptible_timeout(dev->write_wait, !dev->interrupt_out_busy, 2 * HZ);
458         usb_alphatrack_abort_transfers(dev);
459         dev->open_count = 0;
460
461 unlock_exit:
462         up(&dev->sem);
463
464 exit:
465         return retval;
466 }
467
468 /**
469  *      usb_alphatrack_poll
470  */
471 static unsigned int usb_alphatrack_poll(struct file *file, poll_table *wait)
472 {
473         struct usb_alphatrack *dev;
474         unsigned int mask = 0;
475
476         dev = file->private_data;
477
478         poll_wait(file, &dev->read_wait, wait);
479         poll_wait(file, &dev->write_wait, wait);
480
481         if (dev->ring_head != dev->ring_tail)
482                 mask |= POLLIN | POLLRDNORM;
483         if (!dev->interrupt_out_busy)
484                 mask |= POLLOUT | POLLWRNORM;
485
486         return mask;
487 }
488
489 /**
490  *      usb_alphatrack_read
491  */
492 static ssize_t usb_alphatrack_read(struct file *file, char __user *buffer, size_t count,
493                            loff_t *ppos)
494 {
495         struct usb_alphatrack *dev;
496         int retval = 0;
497
498         int c = 0;
499
500         dev = file->private_data;
501
502         /* verify that we actually have some data to read */
503         if (count == 0)
504                 goto exit;
505
506         /* lock this object */
507         if (down_interruptible(&dev->sem)) {
508                 retval = -ERESTARTSYS;
509                 goto exit;
510         }
511
512         /* verify that the device wasn't unplugged */
513         if (dev->intf == NULL) {
514                 retval = -ENODEV;
515                 err("No device or device unplugged %d\n", retval);
516                 goto unlock_exit;
517         }
518
519         while (dev->ring_head == dev->ring_tail) {
520                                         if (file->f_flags & O_NONBLOCK) {
521                                                                         retval = -EAGAIN;
522                                                                         goto unlock_exit;
523                                         }
524                                         dev->interrupt_in_done = 0 ;
525                                         retval = wait_event_interruptible(dev->read_wait, dev->interrupt_in_done);
526                                         if (retval < 0) {
527                                                                         goto unlock_exit;
528                                         }
529         }
530
531         alphatrack_ocmd_info(&dev->intf->dev, &(*dev->ring_buffer)[dev->ring_tail].cmd, "%s", ": copying to userspace");
532
533            c = 0;
534            while((c < count) && (dev->ring_tail != dev->ring_head)) {
535                                                  if (copy_to_user(&buffer[c], &(*dev->ring_buffer)[dev->ring_tail], INPUT_CMD_SIZE)) {
536                                                                                  retval = -EFAULT;
537                                                                                  goto unlock_exit;
538                                                  }
539                                                  dev->ring_tail = (dev->ring_tail+1) % ring_buffer_size;
540                                                  c+=INPUT_CMD_SIZE;
541                                                  dbg_info(&dev->intf->dev, "%s: head, tail are %x, %x\n", __FUNCTION__,dev->ring_head,dev->ring_tail);
542            }
543            retval = c;
544
545 unlock_exit:
546         /* unlock the device */
547         up(&dev->sem);
548
549 exit:
550         return retval;
551 }
552
553 /**
554  *      usb_alphatrack_write
555  */
556 static ssize_t usb_alphatrack_write(struct file *file, const char __user *buffer,
557                             size_t count, loff_t *ppos)
558 {
559         struct usb_alphatrack *dev;
560         size_t bytes_to_write;
561         int retval = 0;
562
563         dev = file->private_data;
564
565         /* verify that we actually have some data to write */
566         if (count == 0)
567                 goto exit;
568
569         /* lock this object */
570         if (down_interruptible(&dev->sem)) {
571                 retval = -ERESTARTSYS;
572                 goto exit;
573         }
574
575         /* verify that the device wasn't unplugged */
576         if (dev->intf == NULL) {
577                 retval = -ENODEV;
578                 err("No device or device unplugged %d\n", retval);
579                 goto unlock_exit;
580         }
581
582         /* wait until previous transfer is finished */
583         if (dev->interrupt_out_busy) {
584                 if (file->f_flags & O_NONBLOCK) {
585                         retval = -EAGAIN;
586                         goto unlock_exit;
587                 }
588                 retval = wait_event_interruptible(dev->write_wait, !dev->interrupt_out_busy);
589                 if (retval < 0) {
590                         goto unlock_exit;
591                 }
592         }
593
594         /* write the data into interrupt_out_buffer from userspace */
595   /* FIXME - if you write more than 12 bytes this breaks */
596         bytes_to_write = min(count, write_buffer_size*dev->interrupt_out_endpoint_size);
597         if (bytes_to_write < count)
598                 dev_warn(&dev->intf->dev, "Write buffer overflow, %zd bytes dropped\n",count-bytes_to_write);
599
600         dbg_info(&dev->intf->dev, "%s: count = %zd, bytes_to_write = %zd\n", __FUNCTION__, count, bytes_to_write);
601
602         if (copy_from_user(dev->interrupt_out_buffer, buffer, bytes_to_write)) {
603                 retval = -EFAULT;
604                 goto unlock_exit;
605         }
606
607         if (dev->interrupt_out_endpoint == NULL) {
608                 err("Endpoint should not be be null! \n");
609                 goto unlock_exit;
610         }
611
612         /* send off the urb */
613         usb_fill_int_urb(dev->interrupt_out_urb,
614                          interface_to_usbdev(dev->intf),
615                          usb_sndintpipe(interface_to_usbdev(dev->intf),
616                                         dev->interrupt_out_endpoint->bEndpointAddress),
617                          dev->interrupt_out_buffer,
618                          bytes_to_write,
619                          usb_alphatrack_interrupt_out_callback,
620                          dev,
621                          dev->interrupt_out_interval);
622         dev->interrupt_out_busy = 1;
623         atomic_inc(&dev->writes_pending);
624         wmb();
625
626         retval = usb_submit_urb(dev->interrupt_out_urb, GFP_KERNEL);
627         if (retval) {
628                 dev->interrupt_out_busy = 0;
629                 err("Couldn't submit interrupt_out_urb %d\n", retval);
630                 atomic_dec(&dev->writes_pending);
631                 goto unlock_exit;
632         }
633         retval = bytes_to_write;
634
635 unlock_exit:
636         /* unlock the device */
637         up(&dev->sem);
638
639 exit:
640         return retval;
641 }
642
643 /* file operations needed when we register this driver */
644 static const struct file_operations usb_alphatrack_fops = {
645         .owner =        THIS_MODULE,
646         .read  =        usb_alphatrack_read,
647         .write =        usb_alphatrack_write,
648         .open =         usb_alphatrack_open,
649         .release =      usb_alphatrack_release,
650         .poll =         usb_alphatrack_poll,
651 };
652
653 /*
654  * usb class driver info in order to get a minor number from the usb core,
655  * and to have the device registered with the driver core
656  */
657
658 static struct usb_class_driver usb_alphatrack_class = {
659         .name =         "alphatrack%d",
660         .fops =         &usb_alphatrack_fops,
661         .minor_base =   USB_ALPHATRACK_MINOR_BASE,
662 };
663
664
665 /**
666  *      usb_alphatrack_probe
667  *
668  *      Called by the usb core when a new device is connected that it thinks
669  *      this driver might be interested in.
670  */
671 static int usb_alphatrack_probe(struct usb_interface *intf, const struct usb_device_id *id)
672 {
673         struct usb_device *udev = interface_to_usbdev(intf);
674         struct usb_alphatrack *dev = NULL;
675         struct usb_host_interface *iface_desc;
676         struct usb_endpoint_descriptor *endpoint;
677         int i;
678         int true_size;
679         int retval = -ENOMEM;
680
681         /* allocate memory for our device state and intialize it */
682
683         dev = kzalloc(sizeof(*dev), GFP_KERNEL);
684         if (dev == NULL) {
685                 dev_err(&intf->dev, "Out of memory\n");
686                 goto exit;
687         }
688         init_MUTEX(&dev->sem);
689         dev->intf = intf;
690         init_waitqueue_head(&dev->read_wait);
691         init_waitqueue_head(&dev->write_wait);
692
693         iface_desc = intf->cur_altsetting;
694
695         /* set up the endpoint information */
696         for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
697                 endpoint = &iface_desc->endpoint[i].desc;
698
699                 if (usb_endpoint_is_int_in(endpoint))
700                         dev->interrupt_in_endpoint = endpoint;
701
702                 if (usb_endpoint_is_int_out(endpoint))
703                         dev->interrupt_out_endpoint = endpoint;
704         }
705         if (dev->interrupt_in_endpoint == NULL) {
706                 dev_err(&intf->dev, "Interrupt in endpoint not found\n");
707                 goto error;
708         }
709         if (dev->interrupt_out_endpoint == NULL)
710                 dev_warn(&intf->dev, "Interrupt out endpoint not found (using control endpoint instead)\n");
711
712         dev->interrupt_in_endpoint_size = le16_to_cpu(dev->interrupt_in_endpoint->wMaxPacketSize);
713
714         if (dev->interrupt_in_endpoint_size != 64)
715             dev_warn(&intf->dev, "Interrupt in endpoint size is not 64!\n");
716
717         if(ring_buffer_size == 0) { 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. Needed? */
722
723 //      dev->ring_buffer = kmalloc((true_size*sizeof(struct alphatrack_icmd))+12, GFP_KERNEL);
724         dev->ring_buffer = kmalloc((true_size*sizeof(struct alphatrack_icmd)), GFP_KERNEL);
725
726         if (!dev->ring_buffer) {
727                 dev_err(&intf->dev, "Couldn't allocate input ring_buffer of size %d\n",true_size);
728                 goto error;
729         }
730
731         dev->interrupt_in_buffer = kmalloc(dev->interrupt_in_endpoint_size, GFP_KERNEL);
732
733         if (!dev->interrupt_in_buffer) {
734                 dev_err(&intf->dev, "Couldn't allocate interrupt_in_buffer\n");
735                 goto error;
736         }
737         dev->oldi_buffer = kmalloc(dev->interrupt_in_endpoint_size, GFP_KERNEL);
738         if (!dev->oldi_buffer) {
739                 dev_err(&intf->dev, "Couldn't allocate old buffer\n");
740                 goto error;
741         }
742         dev->interrupt_in_urb = usb_alloc_urb(0, GFP_KERNEL);
743         if (!dev->interrupt_in_urb) {
744                 dev_err(&intf->dev, "Couldn't allocate interrupt_in_urb\n");
745                 goto error;
746         }
747
748         dev->interrupt_out_endpoint_size = dev->interrupt_out_endpoint ? le16_to_cpu(dev->interrupt_out_endpoint->wMaxPacketSize) :
749                                                                          udev->descriptor.bMaxPacketSize0;
750
751         if (dev->interrupt_out_endpoint_size !=64)
752                 dev_warn(&intf->dev, "Interrupt out endpoint size is not 64!)\n");
753
754         if(write_buffer_size == 0) { write_buffer_size = WRITE_BUFFER_SIZE; }
755         true_size = min(write_buffer_size,WRITE_BUFFER_SIZE);
756
757         dev->interrupt_out_buffer = kmalloc(true_size*dev->interrupt_out_endpoint_size, GFP_KERNEL);
758
759         if (!dev->interrupt_out_buffer) {
760                 dev_err(&intf->dev, "Couldn't allocate interrupt_out_buffer\n");
761                 goto error;
762         }
763
764         dev->write_buffer = kmalloc(sizeof(struct alphatrack_ocmd)*true_size, GFP_KERNEL);
765
766         if (!dev->write_buffer) {
767                 dev_err(&intf->dev, "Couldn't allocate write_buffer \n");
768                 goto error;
769         }
770
771         dev->interrupt_out_urb = usb_alloc_urb(0, GFP_KERNEL);
772         if (!dev->interrupt_out_urb) {
773                 dev_err(&intf->dev, "Couldn't allocate interrupt_out_urb\n");
774                 goto error;
775         }
776         dev->interrupt_in_interval = min_interrupt_in_interval > dev->interrupt_in_endpoint->bInterval ? min_interrupt_in_interval : dev->interrupt_in_endpoint->bInterval;
777         if (dev->interrupt_out_endpoint)
778                 dev->interrupt_out_interval = min_interrupt_out_interval > dev->interrupt_out_endpoint->bInterval ? min_interrupt_out_interval : dev->interrupt_out_endpoint->bInterval;
779
780         /* we can register the device now, as it is ready */
781         usb_set_intfdata(intf, dev);
782
783         atomic_set(&dev->writes_pending,0);
784         retval = usb_register_dev(intf, &usb_alphatrack_class);
785         if (retval) {
786                 /* something prevented us from registering this driver */
787                 dev_err(&intf->dev, "Not able to get a minor for this device.\n");
788                 usb_set_intfdata(intf, NULL);
789                 goto error;
790         }
791
792         /* let the user know what node this device is now attached to */
793         dev_info(&intf->dev, "Alphatrack Device #%d now attached to major %d minor %d\n",
794                 (intf->minor - USB_ALPHATRACK_MINOR_BASE), USB_MAJOR, intf->minor);
795
796 exit:
797         return retval;
798
799 error:
800         usb_alphatrack_delete(dev);
801
802         return retval;
803 }
804
805 /**
806  *      usb_alphatrack_disconnect
807  *
808  *      Called by the usb core when the device is removed from the system.
809  */
810 static void usb_alphatrack_disconnect(struct usb_interface *intf)
811 {
812         struct usb_alphatrack *dev;
813         int minor;
814
815         mutex_lock(&disconnect_mutex);
816
817         dev = usb_get_intfdata(intf);
818         usb_set_intfdata(intf, NULL);
819
820         down(&dev->sem);
821
822         minor = intf->minor;
823
824         /* give back our minor */
825         usb_deregister_dev(intf, &usb_alphatrack_class);
826
827         /* if the device is not opened, then we clean up right now */
828         if (!dev->open_count) {
829                 up(&dev->sem);
830                 usb_alphatrack_delete(dev);
831         } else {
832                 dev->intf = NULL;
833                 up(&dev->sem);
834         }
835
836         atomic_set(&dev->writes_pending,0);
837         mutex_unlock(&disconnect_mutex);
838
839         dev_info(&intf->dev, "Alphatrack Surface #%d now disconnected\n",
840                  (minor - USB_ALPHATRACK_MINOR_BASE));
841 }
842
843 /* usb specific object needed to register this driver with the usb subsystem */
844 static struct usb_driver usb_alphatrack_driver = {
845         .name =         "alphatrack",
846         .probe =        usb_alphatrack_probe,
847         .disconnect =   usb_alphatrack_disconnect,
848         .id_table =     usb_alphatrack_table,
849 };
850
851 /**
852  *      usb_alphatrack_init
853  */
854 static int __init usb_alphatrack_init(void)
855 {
856         int retval;
857
858         /* register this driver with the USB subsystem */
859         retval = usb_register(&usb_alphatrack_driver);
860         if (retval)
861                 err("usb_register failed for the "__FILE__" driver. Error number %d\n", retval);
862
863         return retval;
864 }
865
866 /**
867  *      usb_alphatrack_exit
868  */
869 static void __exit usb_alphatrack_exit(void)
870 {
871         /* deregister this driver with the USB subsystem */
872         usb_deregister(&usb_alphatrack_driver);
873 }
874
875 module_init(usb_alphatrack_init);
876 module_exit(usb_alphatrack_exit);
877