Staging: frontier: remove unused alphatrack_sysfs.c file
[linux-2.6/mini2440.git] / drivers / staging / frontier / alphatrack.c
blob028873e70fe9bc47b2e4281ab296f9e225661747
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 #if 0
104 #define alphatrack_ocmd_info(dev, cmd, format, arg...) do { if (debug) ocmd_info(dev , cmd , format, ## arg); } while (0)
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...)
110 #define alphatrack_icmd_info(dev, cmd, format, arg...)
112 #endif
114 /* Module parameters */
116 module_param(debug, int, S_IRUGO | S_IWUSR);
117 MODULE_PARM_DESC(debug, "Debug enabled or not");
119 /* All interrupt in transfers are collected in a ring buffer to
120 * avoid racing conditions and get better performance of the driver.
123 static int ring_buffer_size = RING_BUFFER_SIZE;
125 module_param(ring_buffer_size, int, S_IRUGO);
126 MODULE_PARM_DESC(ring_buffer_size, "Read ring buffer size");
128 /* The write_buffer can one day contain more than one interrupt out transfer.
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");
136 * Increase the interval for debugging purposes.
137 * or set to 1 to use the standard interval from the endpoint descriptors.
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");
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");
150 /* Structure to hold all of our device specific stuff */
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 */
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;
162 wait_queue_head_t read_wait;
163 wait_queue_head_t write_wait;
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;
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;
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];
193 /* prevent races between open() and disconnect() */
194 static DEFINE_MUTEX(disconnect_mutex);
196 /* forward declaration */
198 static struct usb_driver usb_alphatrack_driver;
200 static void icmd_info(struct usb_alphatrack *dev, char *cmd, char *str, char *a) {
202 if (dev->verbose) {
203 } else {
208 static void ocmd_info(struct usb_alphatrack *dev, char *cmd, char *str, char* a) {
210 if (dev->verbose) {
211 } else {
218 * usb_alphatrack_abort_transfers
219 * aborts transfers and frees associated data structures
221 static void usb_alphatrack_abort_transfers(struct usb_alphatrack *dev)
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);
229 if (dev->interrupt_out_busy)
230 if (dev->intf)
231 usb_kill_urb(dev->interrupt_out_urb);
235 * usb_alphatrack_delete
237 static void usb_alphatrack_delete(struct usb_alphatrack *dev)
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
249 * usb_alphatrack_interrupt_in_callback
252 static void usb_alphatrack_interrupt_in_callback(struct urb *urb)
254 struct usb_alphatrack *dev = urb->context;
255 unsigned int next_ring_head;
256 int retval = -1;
257 int *iptr;
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 */
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;
279 memcpy(dev->oldi_buffer,dev->interrupt_in_buffer,INPUT_CMD_SIZE);
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;
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);
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);
314 exit:
315 dev->interrupt_in_done = 1;
316 wake_up_interruptible(&dev->read_wait);
320 * usb_alphatrack_interrupt_out_callback
322 static void usb_alphatrack_interrupt_out_callback(struct urb *urb)
324 struct usb_alphatrack *dev = urb->context;
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);
339 * usb_alphatrack_open
341 static int usb_alphatrack_open(struct inode *inode, struct file *file)
343 struct usb_alphatrack *dev;
344 int subminor;
345 int retval = 0;
346 struct usb_interface *interface;
348 nonseekable_open(inode, file);
349 subminor = iminor(inode);
351 mutex_lock(&disconnect_mutex);
353 interface = usb_find_interface(&usb_alphatrack_driver, subminor);
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;
362 dev = usb_get_intfdata(interface);
364 if (!dev) {
365 retval = -ENODEV;
366 goto unlock_disconnect_exit;
369 /* lock this device */
370 if (down_interruptible(&dev->sem)) {
371 retval = -ERESTARTSYS;
372 goto unlock_disconnect_exit;
375 /* allow opening only once */
376 if (dev->open_count) {
377 retval = -EBUSY;
378 goto unlock_exit;
380 dev->open_count = 1;
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);
395 dev->interrupt_in_running = 1;
396 dev->interrupt_in_done = 0;
397 dev->enable = 1;
398 dev->offline = 0;
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;
408 /* save device in the file's private structure */
409 file->private_data = dev;
412 unlock_exit:
413 up(&dev->sem);
415 unlock_disconnect_exit:
416 mutex_unlock(&disconnect_mutex);
418 return retval;
422 * usb_alphatrack_release
424 static int usb_alphatrack_release(struct inode *inode, struct file *file)
426 struct usb_alphatrack *dev;
427 int retval = 0;
429 dev = file->private_data;
431 if (dev == NULL) {
432 retval = -ENODEV;
433 goto exit;
436 if (down_interruptible(&dev->sem)) {
437 retval = -ERESTARTSYS;
438 goto exit;
441 if (dev->open_count != 1) {
442 retval = -ENODEV;
443 goto unlock_exit;
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;
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;
461 unlock_exit:
462 up(&dev->sem);
464 exit:
465 return retval;
469 * usb_alphatrack_poll
471 static unsigned int usb_alphatrack_poll(struct file *file, poll_table *wait)
473 struct usb_alphatrack *dev;
474 unsigned int mask = 0;
476 dev = file->private_data;
478 poll_wait(file, &dev->read_wait, wait);
479 poll_wait(file, &dev->write_wait, wait);
481 if (dev->ring_head != dev->ring_tail)
482 mask |= POLLIN | POLLRDNORM;
483 if (!dev->interrupt_out_busy)
484 mask |= POLLOUT | POLLWRNORM;
486 return mask;
490 * usb_alphatrack_read
492 static ssize_t usb_alphatrack_read(struct file *file, char __user *buffer, size_t count,
493 loff_t *ppos)
495 struct usb_alphatrack *dev;
496 int retval = 0;
498 int c = 0;
500 dev = file->private_data;
502 /* verify that we actually have some data to read */
503 if (count == 0)
504 goto exit;
506 /* lock this object */
507 if (down_interruptible(&dev->sem)) {
508 retval = -ERESTARTSYS;
509 goto exit;
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;
519 while (dev->ring_head == dev->ring_tail) {
520 if (file->f_flags & O_NONBLOCK) {
521 retval = -EAGAIN;
522 goto unlock_exit;
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;
531 alphatrack_ocmd_info(&dev->intf->dev, &(*dev->ring_buffer)[dev->ring_tail].cmd, "%s", ": copying to userspace");
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;
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);
543 retval = c;
545 unlock_exit:
546 /* unlock the device */
547 up(&dev->sem);
549 exit:
550 return retval;
554 * usb_alphatrack_write
556 static ssize_t usb_alphatrack_write(struct file *file, const char __user *buffer,
557 size_t count, loff_t *ppos)
559 struct usb_alphatrack *dev;
560 size_t bytes_to_write;
561 int retval = 0;
563 dev = file->private_data;
565 /* verify that we actually have some data to write */
566 if (count == 0)
567 goto exit;
569 /* lock this object */
570 if (down_interruptible(&dev->sem)) {
571 retval = -ERESTARTSYS;
572 goto exit;
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;
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;
588 retval = wait_event_interruptible(dev->write_wait, !dev->interrupt_out_busy);
589 if (retval < 0) {
590 goto unlock_exit;
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);
600 dbg_info(&dev->intf->dev, "%s: count = %zd, bytes_to_write = %zd\n", __FUNCTION__, count, bytes_to_write);
602 if (copy_from_user(dev->interrupt_out_buffer, buffer, bytes_to_write)) {
603 retval = -EFAULT;
604 goto unlock_exit;
607 if (dev->interrupt_out_endpoint == NULL) {
608 err("Endpoint should not be be null! \n");
609 goto unlock_exit;
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();
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;
633 retval = bytes_to_write;
635 unlock_exit:
636 /* unlock the device */
637 up(&dev->sem);
639 exit:
640 return retval;
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,
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
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,
666 * usb_alphatrack_probe
668 * Called by the usb core when a new device is connected that it thinks
669 * this driver might be interested in.
671 static int usb_alphatrack_probe(struct usb_interface *intf, const struct usb_device_id *id)
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;
681 /* allocate memory for our device state and intialize it */
683 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
684 if (dev == NULL) {
685 dev_err(&intf->dev, "Out of memory\n");
686 goto exit;
688 init_MUTEX(&dev->sem);
689 dev->intf = intf;
690 init_waitqueue_head(&dev->read_wait);
691 init_waitqueue_head(&dev->write_wait);
693 iface_desc = intf->cur_altsetting;
695 /* set up the endpoint information */
696 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
697 endpoint = &iface_desc->endpoint[i].desc;
699 if (usb_endpoint_is_int_in(endpoint))
700 dev->interrupt_in_endpoint = endpoint;
702 if (usb_endpoint_is_int_out(endpoint))
703 dev->interrupt_out_endpoint = endpoint;
705 if (dev->interrupt_in_endpoint == NULL) {
706 dev_err(&intf->dev, "Interrupt in endpoint not found\n");
707 goto error;
709 if (dev->interrupt_out_endpoint == NULL)
710 dev_warn(&intf->dev, "Interrupt out endpoint not found (using control endpoint instead)\n");
712 dev->interrupt_in_endpoint_size = le16_to_cpu(dev->interrupt_in_endpoint->wMaxPacketSize);
714 if (dev->interrupt_in_endpoint_size != 64)
715 dev_warn(&intf->dev, "Interrupt in endpoint size is not 64!\n");
717 if(ring_buffer_size == 0) { ring_buffer_size = RING_BUFFER_SIZE; }
719 true_size = min(ring_buffer_size,RING_BUFFER_SIZE);
721 /* FIXME - there are more usb_alloc routines for dma correctness. Needed? */
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);
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;
731 dev->interrupt_in_buffer = kmalloc(dev->interrupt_in_endpoint_size, GFP_KERNEL);
733 if (!dev->interrupt_in_buffer) {
734 dev_err(&intf->dev, "Couldn't allocate interrupt_in_buffer\n");
735 goto error;
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;
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;
748 dev->interrupt_out_endpoint_size = dev->interrupt_out_endpoint ? le16_to_cpu(dev->interrupt_out_endpoint->wMaxPacketSize) :
749 udev->descriptor.bMaxPacketSize0;
751 if (dev->interrupt_out_endpoint_size !=64)
752 dev_warn(&intf->dev, "Interrupt out endpoint size is not 64!)\n");
754 if(write_buffer_size == 0) { write_buffer_size = WRITE_BUFFER_SIZE; }
755 true_size = min(write_buffer_size,WRITE_BUFFER_SIZE);
757 dev->interrupt_out_buffer = kmalloc(true_size*dev->interrupt_out_endpoint_size, GFP_KERNEL);
759 if (!dev->interrupt_out_buffer) {
760 dev_err(&intf->dev, "Couldn't allocate interrupt_out_buffer\n");
761 goto error;
764 dev->write_buffer = kmalloc(sizeof(struct alphatrack_ocmd)*true_size, GFP_KERNEL);
766 if (!dev->write_buffer) {
767 dev_err(&intf->dev, "Couldn't allocate write_buffer \n");
768 goto error;
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;
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;
780 /* we can register the device now, as it is ready */
781 usb_set_intfdata(intf, dev);
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;
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);
796 exit:
797 return retval;
799 error:
800 usb_alphatrack_delete(dev);
802 return retval;
806 * usb_alphatrack_disconnect
808 * Called by the usb core when the device is removed from the system.
810 static void usb_alphatrack_disconnect(struct usb_interface *intf)
812 struct usb_alphatrack *dev;
813 int minor;
815 mutex_lock(&disconnect_mutex);
817 dev = usb_get_intfdata(intf);
818 usb_set_intfdata(intf, NULL);
820 down(&dev->sem);
822 minor = intf->minor;
824 /* give back our minor */
825 usb_deregister_dev(intf, &usb_alphatrack_class);
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);
836 atomic_set(&dev->writes_pending,0);
837 mutex_unlock(&disconnect_mutex);
839 dev_info(&intf->dev, "Alphatrack Surface #%d now disconnected\n",
840 (minor - USB_ALPHATRACK_MINOR_BASE));
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,
852 * usb_alphatrack_init
854 static int __init usb_alphatrack_init(void)
856 int retval;
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);
863 return retval;
867 * usb_alphatrack_exit
869 static void __exit usb_alphatrack_exit(void)
871 /* deregister this driver with the USB subsystem */
872 usb_deregister(&usb_alphatrack_driver);
875 module_init(usb_alphatrack_init);
876 module_exit(usb_alphatrack_exit);