ACPI: thinkpad-acpi: update documents for the new location
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / staging / frontier / alphatrack.c
blob6136e3f8762d4f60c1548de243ad7c627f6c1008
1 /*
2 * Frontier Designs Alphatrack driver
4 * Copyright (C) 2007 Michael Taht (m@taht.net)
6 * Based on the usbled driver and ldusb drivers by
8 * Copyright (C) 2004 Greg Kroah-Hartman (greg@kroah.com)
9 * Copyright (C) 2005 Michael Hund <mhund@ld-didactic.de>
11 * The ldusb driver was, in turn, derived from Lego USB Tower driver
12 * Copyright (C) 2003 David Glance <advidgsf@sourceforge.net>
13 * 2001-2004 Juergen Stuber <starblue@users.sourceforge.net>
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License as
17 * published by the Free Software Foundation, version 2.
21 /**
22 * This driver uses a ring buffer for time critical reading of
23 * interrupt in reports and provides read and write methods for
24 * raw interrupt reports.
27 /* Note: this currently uses a dumb ringbuffer for reads and writes.
28 * A more optimal driver would cache and kill off outstanding urbs that are
29 * now invalid, and ignore ones that already were in the queue but valid
30 * as we only have 30 commands for the alphatrack. In particular this is
31 * key for getting lights to flash in time as otherwise many commands
32 * can be buffered up before the light change makes it to the interface.
35 #include <linux/kernel.h>
36 #include <linux/errno.h>
37 #include <linux/init.h>
38 #include <linux/slab.h>
39 #include <linux/module.h>
40 #include <linux/kobject.h>
41 #include <linux/mutex.h>
42 #include <linux/version.h>
44 #include <asm/uaccess.h>
45 #include <linux/input.h>
46 #include <linux/usb.h>
47 #include <linux/poll.h>
49 #include "surface_sysfs.h"
51 /* make this work on older kernel versions */
53 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19)
54 #include "frontier_compat.h"
55 #endif /* older kernel versions */
57 #include "alphatrack.h"
59 #define VENDOR_ID 0x165b
60 #define PRODUCT_ID 0xfad1
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
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 */
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");
83 /* These aren't done yet */
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
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
98 static int debug = 0;
100 /* Use our own dbg macro */
101 #define dbg_info(dev, format, arg...) do { if (debug) dev_info(dev , format , ## arg); } while (0)
103 #define alphatrack_ocmd_info(dev, cmd, format, arg...)
105 #define alphatrack_icmd_info(dev, cmd, format, arg...)
108 /* Module parameters */
110 module_param(debug, int, S_IRUGO | S_IWUSR);
111 MODULE_PARM_DESC(debug, "Debug enabled or not");
113 /* All interrupt in transfers are collected in a ring buffer to
114 * avoid racing conditions and get better performance of the driver.
117 static int ring_buffer_size = RING_BUFFER_SIZE;
119 module_param(ring_buffer_size, int, S_IRUGO);
120 MODULE_PARM_DESC(ring_buffer_size, "Read ring buffer size");
122 /* The write_buffer can one day contain more than one interrupt out transfer.
125 static int write_buffer_size = WRITE_BUFFER_SIZE;
126 module_param(write_buffer_size, int, S_IRUGO);
127 MODULE_PARM_DESC(write_buffer_size, "Write buffer size");
130 * Increase the interval for debugging purposes.
131 * or set to 1 to use the standard interval from the endpoint descriptors.
134 static int min_interrupt_in_interval = ALPHATRACK_USB_TIMEOUT;
135 module_param(min_interrupt_in_interval, int, 0);
136 MODULE_PARM_DESC(min_interrupt_in_interval, "Minimum interrupt in interval in ms");
138 static int min_interrupt_out_interval = ALPHATRACK_USB_TIMEOUT;
139 module_param(min_interrupt_out_interval, int, 0);
140 MODULE_PARM_DESC(min_interrupt_out_interval, "Minimum interrupt out interval in ms");
144 /* Structure to hold all of our device specific stuff */
146 struct usb_alphatrack {
147 struct semaphore sem; /* locks this structure */
148 struct usb_interface* intf; /* save off the usb interface pointer */
149 int open_count; /* number of times this port has been opened */
151 struct alphatrack_icmd (*ring_buffer)[RING_BUFFER_SIZE]; /* just make c happy */
152 struct alphatrack_ocmd (*write_buffer)[WRITE_BUFFER_SIZE]; /* just make c happy */
153 unsigned int ring_head;
154 unsigned int ring_tail;
156 wait_queue_head_t read_wait;
157 wait_queue_head_t write_wait;
159 unsigned char* interrupt_in_buffer;
160 unsigned char* oldi_buffer;
161 struct usb_endpoint_descriptor* interrupt_in_endpoint;
162 struct urb* interrupt_in_urb;
163 int interrupt_in_interval;
164 size_t interrupt_in_endpoint_size;
165 int interrupt_in_running;
166 int interrupt_in_done;
168 char* interrupt_out_buffer;
169 struct usb_endpoint_descriptor* interrupt_out_endpoint;
170 struct urb* interrupt_out_urb;
171 int interrupt_out_interval;
172 size_t interrupt_out_endpoint_size;
173 int interrupt_out_busy;
175 atomic_t writes_pending;
176 int event; /* alternate interface to events */
177 int fader; /* 10 bits */
178 int lights; /* 23 bits */
179 unsigned char dump_state; /* 0 if disabled 1 if enabled */
180 unsigned char enable; /* 0 if disabled 1 if enabled */
181 unsigned char offline; /* if the device is out of range or asleep */
182 unsigned char verbose; /* be verbose in error reporting */
183 unsigned char last_cmd[OUTPUT_CMD_SIZE];
184 unsigned char screen[32];
187 /* prevent races between open() and disconnect() */
188 static DEFINE_MUTEX(disconnect_mutex);
190 /* forward declaration */
192 static struct usb_driver usb_alphatrack_driver;
195 * usb_alphatrack_abort_transfers
196 * aborts transfers and frees associated data structures
198 static void usb_alphatrack_abort_transfers(struct usb_alphatrack *dev)
200 /* shutdown transfer */
201 if (dev->interrupt_in_running) {
202 dev->interrupt_in_running = 0;
203 if (dev->intf)
204 usb_kill_urb(dev->interrupt_in_urb);
206 if (dev->interrupt_out_busy)
207 if (dev->intf)
208 usb_kill_urb(dev->interrupt_out_urb);
212 * usb_alphatrack_delete
214 static void usb_alphatrack_delete(struct usb_alphatrack *dev)
216 usb_alphatrack_abort_transfers(dev);
217 usb_free_urb(dev->interrupt_in_urb);
218 usb_free_urb(dev->interrupt_out_urb);
219 kfree(dev->ring_buffer);
220 kfree(dev->interrupt_in_buffer);
221 kfree(dev->interrupt_out_buffer);
222 kfree(dev); // fixme oldi_buffer
226 * usb_alphatrack_interrupt_in_callback
229 static void usb_alphatrack_interrupt_in_callback(struct urb *urb)
231 struct usb_alphatrack *dev = urb->context;
232 unsigned int next_ring_head;
233 int retval = -1;
235 if (urb->status) {
236 if (urb->status == -ENOENT ||
237 urb->status == -ECONNRESET ||
238 urb->status == -ESHUTDOWN) {
239 goto exit;
240 } else {
241 dbg_info(&dev->intf->dev, "%s: nonzero status received: %d\n",
242 __func__, urb->status);
243 goto resubmit; /* maybe we can recover */
247 if (urb->actual_length != INPUT_CMD_SIZE) {
248 dev_warn(&dev->intf->dev,
249 "Urb length was %d bytes!! Do something intelligent \n", urb->actual_length);
250 } else {
251 alphatrack_ocmd_info(&dev->intf->dev,&(*dev->ring_buffer)[dev->ring_tail].cmd,"%s", "bla");
252 if(memcmp(dev->interrupt_in_buffer,dev->oldi_buffer,INPUT_CMD_SIZE)==0) {
253 goto resubmit;
255 memcpy(dev->oldi_buffer,dev->interrupt_in_buffer,INPUT_CMD_SIZE);
257 #if SUPPRESS_EXTRA_OFFLINE_EVENTS
258 if(dev->offline == 2 && dev->interrupt_in_buffer[1] == 0xff) { goto resubmit; }
259 if(dev->offline == 1 && dev->interrupt_in_buffer[1] == 0xff) { dev->offline = 2; goto resubmit; }
260 /* Always pass one offline event up the stack */
261 if(dev->offline > 0 && dev->interrupt_in_buffer[1] != 0xff) { dev->offline = 0; }
262 if(dev->offline == 0 && dev->interrupt_in_buffer[1] == 0xff) { dev->offline = 1; }
263 #endif
264 dbg_info(&dev->intf->dev, "%s: head, tail are %x, %x\n", __func__,dev->ring_head,dev->ring_tail);
265 next_ring_head = (dev->ring_head+1) % ring_buffer_size;
267 if (next_ring_head != dev->ring_tail) {
268 memcpy(&((*dev->ring_buffer)[dev->ring_head]),
269 dev->interrupt_in_buffer, urb->actual_length);
270 dev->ring_head = next_ring_head;
271 retval = 0;
272 memset(dev->interrupt_in_buffer, 0, urb->actual_length);
273 } else {
274 dev_warn(&dev->intf->dev,
275 "Ring buffer overflow, %d bytes dropped\n",
276 urb->actual_length);
277 memset(dev->interrupt_in_buffer, 0, urb->actual_length);
281 resubmit:
282 /* resubmit if we're still running */
283 if (dev->interrupt_in_running && dev->intf) {
284 retval = usb_submit_urb(dev->interrupt_in_urb, GFP_ATOMIC);
285 if (retval)
286 dev_err(&dev->intf->dev,
287 "usb_submit_urb failed (%d)\n", retval);
290 exit:
291 dev->interrupt_in_done = 1;
292 wake_up_interruptible(&dev->read_wait);
296 * usb_alphatrack_interrupt_out_callback
298 static void usb_alphatrack_interrupt_out_callback(struct urb *urb)
300 struct usb_alphatrack *dev = urb->context;
302 /* sync/async unlink faults aren't errors */
303 if (urb->status && !(urb->status == -ENOENT ||
304 urb->status == -ECONNRESET ||
305 urb->status == -ESHUTDOWN))
306 dbg_info(&dev->intf->dev,
307 "%s - nonzero write interrupt status received: %d\n",
308 __func__, urb->status);
309 atomic_dec(&dev->writes_pending);
310 dev->interrupt_out_busy = 0;
311 wake_up_interruptible(&dev->write_wait);
315 * usb_alphatrack_open
317 static int usb_alphatrack_open(struct inode *inode, struct file *file)
319 struct usb_alphatrack *dev;
320 int subminor;
321 int retval = 0;
322 struct usb_interface *interface;
324 nonseekable_open(inode, file);
325 subminor = iminor(inode);
327 mutex_lock(&disconnect_mutex);
329 interface = usb_find_interface(&usb_alphatrack_driver, subminor);
331 if (!interface) {
332 err("%s - error, can't find device for minor %d\n",
333 __func__, subminor);
334 retval = -ENODEV;
335 goto unlock_disconnect_exit;
338 dev = usb_get_intfdata(interface);
340 if (!dev) {
341 retval = -ENODEV;
342 goto unlock_disconnect_exit;
345 /* lock this device */
346 if (down_interruptible(&dev->sem)) {
347 retval = -ERESTARTSYS;
348 goto unlock_disconnect_exit;
351 /* allow opening only once */
352 if (dev->open_count) {
353 retval = -EBUSY;
354 goto unlock_exit;
356 dev->open_count = 1;
358 /* initialize in direction */
359 dev->ring_head = 0;
360 dev->ring_tail = 0;
361 usb_fill_int_urb(dev->interrupt_in_urb,
362 interface_to_usbdev(interface),
363 usb_rcvintpipe(interface_to_usbdev(interface),
364 dev->interrupt_in_endpoint->bEndpointAddress),
365 dev->interrupt_in_buffer,
366 dev->interrupt_in_endpoint_size,
367 usb_alphatrack_interrupt_in_callback,
368 dev,
369 dev->interrupt_in_interval);
371 dev->interrupt_in_running = 1;
372 dev->interrupt_in_done = 0;
373 dev->enable = 1;
374 dev->offline = 0;
376 retval = usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL);
377 if (retval) {
378 dev_err(&interface->dev, "Couldn't submit interrupt_in_urb %d\n", retval);
379 dev->interrupt_in_running = 0;
380 dev->open_count = 0;
381 goto unlock_exit;
384 /* save device in the file's private structure */
385 file->private_data = dev;
388 unlock_exit:
389 up(&dev->sem);
391 unlock_disconnect_exit:
392 mutex_unlock(&disconnect_mutex);
394 return retval;
398 * usb_alphatrack_release
400 static int usb_alphatrack_release(struct inode *inode, struct file *file)
402 struct usb_alphatrack *dev;
403 int retval = 0;
405 dev = file->private_data;
407 if (dev == NULL) {
408 retval = -ENODEV;
409 goto exit;
412 if (down_interruptible(&dev->sem)) {
413 retval = -ERESTARTSYS;
414 goto exit;
417 if (dev->open_count != 1) {
418 retval = -ENODEV;
419 goto unlock_exit;
422 if (dev->intf == NULL) {
423 /* the device was unplugged before the file was released */
424 up(&dev->sem);
425 /* unlock here as usb_alphatrack_delete frees dev */
426 usb_alphatrack_delete(dev);
427 retval = -ENODEV;
428 goto exit;
431 /* wait until write transfer is finished */
432 if (dev->interrupt_out_busy)
433 wait_event_interruptible_timeout(dev->write_wait, !dev->interrupt_out_busy, 2 * HZ);
434 usb_alphatrack_abort_transfers(dev);
435 dev->open_count = 0;
437 unlock_exit:
438 up(&dev->sem);
440 exit:
441 return retval;
445 * usb_alphatrack_poll
447 static unsigned int usb_alphatrack_poll(struct file *file, poll_table *wait)
449 struct usb_alphatrack *dev;
450 unsigned int mask = 0;
452 dev = file->private_data;
454 poll_wait(file, &dev->read_wait, wait);
455 poll_wait(file, &dev->write_wait, wait);
457 if (dev->ring_head != dev->ring_tail)
458 mask |= POLLIN | POLLRDNORM;
459 if (!dev->interrupt_out_busy)
460 mask |= POLLOUT | POLLWRNORM;
462 return mask;
466 * usb_alphatrack_read
468 static ssize_t usb_alphatrack_read(struct file *file, char __user *buffer, size_t count,
469 loff_t *ppos)
471 struct usb_alphatrack *dev;
472 int retval = 0;
474 int c = 0;
476 dev = file->private_data;
478 /* verify that we actually have some data to read */
479 if (count == 0)
480 goto exit;
482 /* lock this object */
483 if (down_interruptible(&dev->sem)) {
484 retval = -ERESTARTSYS;
485 goto exit;
488 /* verify that the device wasn't unplugged */
489 if (dev->intf == NULL) {
490 retval = -ENODEV;
491 err("No device or device unplugged %d\n", retval);
492 goto unlock_exit;
495 while (dev->ring_head == dev->ring_tail) {
496 if (file->f_flags & O_NONBLOCK) {
497 retval = -EAGAIN;
498 goto unlock_exit;
500 dev->interrupt_in_done = 0 ;
501 retval = wait_event_interruptible(dev->read_wait, dev->interrupt_in_done);
502 if (retval < 0) {
503 goto unlock_exit;
507 alphatrack_ocmd_info(&dev->intf->dev, &(*dev->ring_buffer)[dev->ring_tail].cmd, "%s", ": copying to userspace");
509 c = 0;
510 while((c < count) && (dev->ring_tail != dev->ring_head)) {
511 if (copy_to_user(&buffer[c], &(*dev->ring_buffer)[dev->ring_tail], INPUT_CMD_SIZE)) {
512 retval = -EFAULT;
513 goto unlock_exit;
515 dev->ring_tail = (dev->ring_tail+1) % ring_buffer_size;
516 c+=INPUT_CMD_SIZE;
517 dbg_info(&dev->intf->dev, "%s: head, tail are %x, %x\n", __func__,dev->ring_head,dev->ring_tail);
519 retval = c;
521 unlock_exit:
522 /* unlock the device */
523 up(&dev->sem);
525 exit:
526 return retval;
530 * usb_alphatrack_write
532 static ssize_t usb_alphatrack_write(struct file *file, const char __user *buffer,
533 size_t count, loff_t *ppos)
535 struct usb_alphatrack *dev;
536 size_t bytes_to_write;
537 int retval = 0;
539 dev = file->private_data;
541 /* verify that we actually have some data to write */
542 if (count == 0)
543 goto exit;
545 /* lock this object */
546 if (down_interruptible(&dev->sem)) {
547 retval = -ERESTARTSYS;
548 goto exit;
551 /* verify that the device wasn't unplugged */
552 if (dev->intf == NULL) {
553 retval = -ENODEV;
554 err("No device or device unplugged %d\n", retval);
555 goto unlock_exit;
558 /* wait until previous transfer is finished */
559 if (dev->interrupt_out_busy) {
560 if (file->f_flags & O_NONBLOCK) {
561 retval = -EAGAIN;
562 goto unlock_exit;
564 retval = wait_event_interruptible(dev->write_wait, !dev->interrupt_out_busy);
565 if (retval < 0) {
566 goto unlock_exit;
570 /* write the data into interrupt_out_buffer from userspace */
571 /* FIXME - if you write more than 12 bytes this breaks */
572 bytes_to_write = min(count, write_buffer_size*dev->interrupt_out_endpoint_size);
573 if (bytes_to_write < count)
574 dev_warn(&dev->intf->dev, "Write buffer overflow, %zd bytes dropped\n",count-bytes_to_write);
576 dbg_info(&dev->intf->dev, "%s: count = %zd, bytes_to_write = %zd\n", __func__, count, bytes_to_write);
578 if (copy_from_user(dev->interrupt_out_buffer, buffer, bytes_to_write)) {
579 retval = -EFAULT;
580 goto unlock_exit;
583 if (dev->interrupt_out_endpoint == NULL) {
584 err("Endpoint should not be be null! \n");
585 goto unlock_exit;
588 /* send off the urb */
589 usb_fill_int_urb(dev->interrupt_out_urb,
590 interface_to_usbdev(dev->intf),
591 usb_sndintpipe(interface_to_usbdev(dev->intf),
592 dev->interrupt_out_endpoint->bEndpointAddress),
593 dev->interrupt_out_buffer,
594 bytes_to_write,
595 usb_alphatrack_interrupt_out_callback,
596 dev,
597 dev->interrupt_out_interval);
598 dev->interrupt_out_busy = 1;
599 atomic_inc(&dev->writes_pending);
600 wmb();
602 retval = usb_submit_urb(dev->interrupt_out_urb, GFP_KERNEL);
603 if (retval) {
604 dev->interrupt_out_busy = 0;
605 err("Couldn't submit interrupt_out_urb %d\n", retval);
606 atomic_dec(&dev->writes_pending);
607 goto unlock_exit;
609 retval = bytes_to_write;
611 unlock_exit:
612 /* unlock the device */
613 up(&dev->sem);
615 exit:
616 return retval;
619 /* file operations needed when we register this driver */
620 static const struct file_operations usb_alphatrack_fops = {
621 .owner = THIS_MODULE,
622 .read = usb_alphatrack_read,
623 .write = usb_alphatrack_write,
624 .open = usb_alphatrack_open,
625 .release = usb_alphatrack_release,
626 .poll = usb_alphatrack_poll,
630 * usb class driver info in order to get a minor number from the usb core,
631 * and to have the device registered with the driver core
634 static struct usb_class_driver usb_alphatrack_class = {
635 .name = "alphatrack%d",
636 .fops = &usb_alphatrack_fops,
637 .minor_base = USB_ALPHATRACK_MINOR_BASE,
642 * usb_alphatrack_probe
644 * Called by the usb core when a new device is connected that it thinks
645 * this driver might be interested in.
647 static int usb_alphatrack_probe(struct usb_interface *intf, const struct usb_device_id *id)
649 struct usb_device *udev = interface_to_usbdev(intf);
650 struct usb_alphatrack *dev = NULL;
651 struct usb_host_interface *iface_desc;
652 struct usb_endpoint_descriptor *endpoint;
653 int i;
654 int true_size;
655 int retval = -ENOMEM;
657 /* allocate memory for our device state and intialize it */
659 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
660 if (dev == NULL) {
661 dev_err(&intf->dev, "Out of memory\n");
662 goto exit;
664 init_MUTEX(&dev->sem);
665 dev->intf = intf;
666 init_waitqueue_head(&dev->read_wait);
667 init_waitqueue_head(&dev->write_wait);
669 iface_desc = intf->cur_altsetting;
671 /* set up the endpoint information */
672 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
673 endpoint = &iface_desc->endpoint[i].desc;
675 if (usb_endpoint_is_int_in(endpoint))
676 dev->interrupt_in_endpoint = endpoint;
678 if (usb_endpoint_is_int_out(endpoint))
679 dev->interrupt_out_endpoint = endpoint;
681 if (dev->interrupt_in_endpoint == NULL) {
682 dev_err(&intf->dev, "Interrupt in endpoint not found\n");
683 goto error;
685 if (dev->interrupt_out_endpoint == NULL)
686 dev_warn(&intf->dev, "Interrupt out endpoint not found (using control endpoint instead)\n");
688 dev->interrupt_in_endpoint_size = le16_to_cpu(dev->interrupt_in_endpoint->wMaxPacketSize);
690 if (dev->interrupt_in_endpoint_size != 64)
691 dev_warn(&intf->dev, "Interrupt in endpoint size is not 64!\n");
693 if(ring_buffer_size == 0) { ring_buffer_size = RING_BUFFER_SIZE; }
695 true_size = min(ring_buffer_size,RING_BUFFER_SIZE);
697 /* FIXME - there are more usb_alloc routines for dma correctness. Needed? */
699 // dev->ring_buffer = kmalloc((true_size*sizeof(struct alphatrack_icmd))+12, GFP_KERNEL);
700 dev->ring_buffer = kmalloc((true_size*sizeof(struct alphatrack_icmd)), GFP_KERNEL);
702 if (!dev->ring_buffer) {
703 dev_err(&intf->dev, "Couldn't allocate input ring_buffer of size %d\n",true_size);
704 goto error;
707 dev->interrupt_in_buffer = kmalloc(dev->interrupt_in_endpoint_size, GFP_KERNEL);
709 if (!dev->interrupt_in_buffer) {
710 dev_err(&intf->dev, "Couldn't allocate interrupt_in_buffer\n");
711 goto error;
713 dev->oldi_buffer = kmalloc(dev->interrupt_in_endpoint_size, GFP_KERNEL);
714 if (!dev->oldi_buffer) {
715 dev_err(&intf->dev, "Couldn't allocate old buffer\n");
716 goto error;
718 dev->interrupt_in_urb = usb_alloc_urb(0, GFP_KERNEL);
719 if (!dev->interrupt_in_urb) {
720 dev_err(&intf->dev, "Couldn't allocate interrupt_in_urb\n");
721 goto error;
724 dev->interrupt_out_endpoint_size = dev->interrupt_out_endpoint ? le16_to_cpu(dev->interrupt_out_endpoint->wMaxPacketSize) :
725 udev->descriptor.bMaxPacketSize0;
727 if (dev->interrupt_out_endpoint_size !=64)
728 dev_warn(&intf->dev, "Interrupt out endpoint size is not 64!)\n");
730 if(write_buffer_size == 0) { write_buffer_size = WRITE_BUFFER_SIZE; }
731 true_size = min(write_buffer_size,WRITE_BUFFER_SIZE);
733 dev->interrupt_out_buffer = kmalloc(true_size*dev->interrupt_out_endpoint_size, GFP_KERNEL);
735 if (!dev->interrupt_out_buffer) {
736 dev_err(&intf->dev, "Couldn't allocate interrupt_out_buffer\n");
737 goto error;
740 dev->write_buffer = kmalloc(sizeof(struct alphatrack_ocmd)*true_size, GFP_KERNEL);
742 if (!dev->write_buffer) {
743 dev_err(&intf->dev, "Couldn't allocate write_buffer \n");
744 goto error;
747 dev->interrupt_out_urb = usb_alloc_urb(0, GFP_KERNEL);
748 if (!dev->interrupt_out_urb) {
749 dev_err(&intf->dev, "Couldn't allocate interrupt_out_urb\n");
750 goto error;
752 dev->interrupt_in_interval = min_interrupt_in_interval > dev->interrupt_in_endpoint->bInterval ? min_interrupt_in_interval : dev->interrupt_in_endpoint->bInterval;
753 if (dev->interrupt_out_endpoint)
754 dev->interrupt_out_interval = min_interrupt_out_interval > dev->interrupt_out_endpoint->bInterval ? min_interrupt_out_interval : dev->interrupt_out_endpoint->bInterval;
756 /* we can register the device now, as it is ready */
757 usb_set_intfdata(intf, dev);
759 atomic_set(&dev->writes_pending,0);
760 retval = usb_register_dev(intf, &usb_alphatrack_class);
761 if (retval) {
762 /* something prevented us from registering this driver */
763 dev_err(&intf->dev, "Not able to get a minor for this device.\n");
764 usb_set_intfdata(intf, NULL);
765 goto error;
768 /* let the user know what node this device is now attached to */
769 dev_info(&intf->dev, "Alphatrack Device #%d now attached to major %d minor %d\n",
770 (intf->minor - USB_ALPHATRACK_MINOR_BASE), USB_MAJOR, intf->minor);
772 exit:
773 return retval;
775 error:
776 usb_alphatrack_delete(dev);
778 return retval;
782 * usb_alphatrack_disconnect
784 * Called by the usb core when the device is removed from the system.
786 static void usb_alphatrack_disconnect(struct usb_interface *intf)
788 struct usb_alphatrack *dev;
789 int minor;
791 mutex_lock(&disconnect_mutex);
793 dev = usb_get_intfdata(intf);
794 usb_set_intfdata(intf, NULL);
796 down(&dev->sem);
798 minor = intf->minor;
800 /* give back our minor */
801 usb_deregister_dev(intf, &usb_alphatrack_class);
803 /* if the device is not opened, then we clean up right now */
804 if (!dev->open_count) {
805 up(&dev->sem);
806 usb_alphatrack_delete(dev);
807 } else {
808 dev->intf = NULL;
809 up(&dev->sem);
812 atomic_set(&dev->writes_pending,0);
813 mutex_unlock(&disconnect_mutex);
815 dev_info(&intf->dev, "Alphatrack Surface #%d now disconnected\n",
816 (minor - USB_ALPHATRACK_MINOR_BASE));
819 /* usb specific object needed to register this driver with the usb subsystem */
820 static struct usb_driver usb_alphatrack_driver = {
821 .name = "alphatrack",
822 .probe = usb_alphatrack_probe,
823 .disconnect = usb_alphatrack_disconnect,
824 .id_table = usb_alphatrack_table,
828 * usb_alphatrack_init
830 static int __init usb_alphatrack_init(void)
832 int retval;
834 /* register this driver with the USB subsystem */
835 retval = usb_register(&usb_alphatrack_driver);
836 if (retval)
837 err("usb_register failed for the "__FILE__" driver. Error number %d\n", retval);
839 return retval;
843 * usb_alphatrack_exit
845 static void __exit usb_alphatrack_exit(void)
847 /* deregister this driver with the USB subsystem */
848 usb_deregister(&usb_alphatrack_driver);
851 module_init(usb_alphatrack_init);
852 module_exit(usb_alphatrack_exit);