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.
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.
28 * Note: this currently uses a dumb ringbuffer for reads and writes.
29 * A more optimal driver would cache and kill off outstanding urbs that are
30 * now invalid, and ignore ones that already were in the queue but valid
31 * as we only have 30 commands for the alphatrack. In particular this is
32 * key for getting lights to flash in time as otherwise many commands
33 * can be buffered up before the light change makes it to the interface.
36 #include <linux/kernel.h>
37 #include <linux/errno.h>
38 #include <linux/slab.h>
39 #include <linux/module.h>
40 #include <linux/kobject.h>
41 #include <linux/mutex.h>
43 #include <linux/uaccess.h>
44 #include <linux/input.h>
45 #include <linux/usb.h>
46 #include <linux/poll.h>
48 #include "alphatrack.h"
50 #define VENDOR_ID 0x165b
51 #define PRODUCT_ID 0xfad1
53 #ifdef CONFIG_USB_DYNAMIC_MINORS
54 #define USB_ALPHATRACK_MINOR_BASE 0
56 /* FIXME 176 - is another driver's minor - apply for that */
57 #define USB_ALPHATRACK_MINOR_BASE 176
60 /* table of devices that work with this driver */
61 static const struct usb_device_id usb_alphatrack_table
[] = {
62 {USB_DEVICE(VENDOR_ID
, PRODUCT_ID
)},
63 {} /* Terminating entry */
66 MODULE_DEVICE_TABLE(usb
, usb_alphatrack_table
);
67 MODULE_VERSION("0.41");
68 MODULE_AUTHOR("Mike Taht <m@taht.net>");
69 MODULE_DESCRIPTION("Alphatrack USB Driver");
70 MODULE_LICENSE("GPL");
71 MODULE_SUPPORTED_DEVICE("Frontier Designs Alphatrack Control Surface");
73 /* These aren't done yet */
75 #define SUPPRESS_EXTRA_ONLINE_EVENTS 0
76 #define BUFFERED_WRITES 0
77 #define SUPPRESS_EXTRA_OFFLINE_EVENTS 0
78 #define COMPRESS_FADER_EVENTS 0
80 #define BUFFERED_READS 1
81 #define RING_BUFFER_SIZE 512
82 #define WRITE_BUFFER_SIZE 34
83 #define ALPHATRACK_USB_TIMEOUT 10
84 #define OUTPUT_CMD_SIZE 8
85 #define INPUT_CMD_SIZE 12
86 #define ALPHATRACK_DEBUG 0
88 static int debug
= ALPHATRACK_DEBUG
;
90 /* Use our own dbg macro */
91 #define dbg_info(dev, format, arg...) do \
92 { if (debug) dev_info(dev , format , ## arg); } while (0)
94 #define alphatrack_ocmd_info(dev, cmd, format, arg...)
96 #define alphatrack_icmd_info(dev, cmd, format, arg...)
98 /* Module parameters */
100 module_param(debug
, int, S_IRUGO
| S_IWUSR
);
101 MODULE_PARM_DESC(debug
, "Debug enabled or not");
104 * All interrupt in transfers are collected in a ring buffer to
105 * avoid racing conditions and get better performance of the driver.
108 static int ring_buffer_size
= RING_BUFFER_SIZE
;
110 module_param(ring_buffer_size
, int, S_IRUGO
);
111 MODULE_PARM_DESC(ring_buffer_size
, "Read ring buffer size");
113 /* The write_buffer can one day contain more than one interrupt out transfer.*/
115 static int write_buffer_size
= WRITE_BUFFER_SIZE
;
116 module_param(write_buffer_size
, int, S_IRUGO
);
117 MODULE_PARM_DESC(write_buffer_size
, "Write buffer size");
120 * Increase the interval for debugging purposes.
121 * or set to 1 to use the standard interval from the endpoint descriptors.
124 static int min_interrupt_in_interval
= ALPHATRACK_USB_TIMEOUT
;
125 module_param(min_interrupt_in_interval
, int, 0);
126 MODULE_PARM_DESC(min_interrupt_in_interval
,
127 "Minimum interrupt in interval in ms");
129 static int min_interrupt_out_interval
= ALPHATRACK_USB_TIMEOUT
;
130 module_param(min_interrupt_out_interval
, int, 0);
131 MODULE_PARM_DESC(min_interrupt_out_interval
,
132 "Minimum interrupt out interval in ms");
134 /* Structure to hold all of our device specific stuff */
136 struct usb_alphatrack
{
137 struct mutex mtx
; /* locks this structure */
138 struct usb_interface
*intf
; /* save off the usb interface pointer */
139 int open_count
; /* number of times this port has been opened */
142 struct alphatrack_icmd (*ring_buffer
)[RING_BUFFER_SIZE
];
143 struct alphatrack_ocmd (*write_buffer
)[WRITE_BUFFER_SIZE
];
144 unsigned int ring_head
;
145 unsigned int ring_tail
;
147 wait_queue_head_t read_wait
;
148 wait_queue_head_t write_wait
;
150 unsigned char *interrupt_in_buffer
;
151 unsigned char *oldi_buffer
;
152 struct usb_endpoint_descriptor
*interrupt_in_endpoint
;
153 struct urb
*interrupt_in_urb
;
154 int interrupt_in_interval
;
155 size_t interrupt_in_endpoint_size
;
156 int interrupt_in_running
;
157 int interrupt_in_done
;
159 char *interrupt_out_buffer
;
160 struct usb_endpoint_descriptor
*interrupt_out_endpoint
;
161 struct urb
*interrupt_out_urb
;
162 int interrupt_out_interval
;
163 size_t interrupt_out_endpoint_size
;
164 int interrupt_out_busy
;
166 atomic_t writes_pending
;
167 int event
; /* alternate interface to events */
168 int fader
; /* 10 bits */
169 int lights
; /* 23 bits */
170 unsigned char dump_state
; /* 0 if disabled 1 if enabled */
171 unsigned char enable
; /* 0 if disabled 1 if enabled */
172 unsigned char offline
; /* if the device is out of range or asleep */
173 unsigned char verbose
; /* be verbose in error reporting */
174 unsigned char last_cmd
[OUTPUT_CMD_SIZE
];
175 unsigned char screen
[32];
178 /* prevent races between open() and disconnect() */
179 static DEFINE_MUTEX(disconnect_mutex
);
181 /* forward declaration */
183 static struct usb_driver usb_alphatrack_driver
;
186 * usb_alphatrack_abort_transfers
187 * aborts transfers and frees associated data structures
189 static void usb_alphatrack_abort_transfers(struct usb_alphatrack
*dev
)
191 /* shutdown transfer */
192 if (dev
->interrupt_in_running
) {
193 dev
->interrupt_in_running
= 0;
195 usb_kill_urb(dev
->interrupt_in_urb
);
197 if (dev
->interrupt_out_busy
)
199 usb_kill_urb(dev
->interrupt_out_urb
);
202 /** usb_alphatrack_delete */
203 static void usb_alphatrack_delete(struct usb_alphatrack
*dev
)
205 usb_alphatrack_abort_transfers(dev
);
206 usb_free_urb(dev
->interrupt_in_urb
);
207 usb_free_urb(dev
->interrupt_out_urb
);
208 kfree(dev
->ring_buffer
);
209 kfree(dev
->interrupt_in_buffer
);
210 kfree(dev
->interrupt_out_buffer
);
211 kfree(dev
->oldi_buffer
);
212 kfree(dev
->write_buffer
);
216 /** usb_alphatrack_interrupt_in_callback */
218 static void usb_alphatrack_interrupt_in_callback(struct urb
*urb
)
220 struct usb_alphatrack
*dev
= urb
->context
;
221 unsigned int next_ring_head
;
225 if (urb
->status
== -ENOENT
||
226 urb
->status
== -ECONNRESET
|| urb
->status
== -ESHUTDOWN
) {
229 dbg_info(&dev
->intf
->dev
,
230 "%s: nonzero status received: %d\n", __func__
,
232 goto resubmit
; /* maybe we can recover */
236 if (urb
->actual_length
!= INPUT_CMD_SIZE
) {
237 dev_warn(&dev
->intf
->dev
,
238 "Urb length was %d bytes!! Do something intelligent\n",
241 alphatrack_ocmd_info(&dev
->intf
->dev
,
242 &(*dev
->ring_buffer
)[dev
->ring_tail
].cmd
,
245 (dev
->interrupt_in_buffer
, dev
->oldi_buffer
,
246 INPUT_CMD_SIZE
) == 0) {
249 memcpy(dev
->oldi_buffer
, dev
->interrupt_in_buffer
,
252 #if SUPPRESS_EXTRA_OFFLINE_EVENTS
253 if (dev
->offline
== 2 && dev
->interrupt_in_buffer
[1] == 0xff)
255 if (dev
->offline
== 1 && dev
->interrupt_in_buffer
[1] == 0xff) {
259 /* Always pass one offline event up the stack */
260 if (dev
->offline
> 0 && dev
->interrupt_in_buffer
[1] != 0xff)
262 if (dev
->offline
== 0 && dev
->interrupt_in_buffer
[1] == 0xff)
265 dbg_info(&dev
->intf
->dev
, "%s: head, tail are %x, %x\n",
266 __func__
, dev
->ring_head
, dev
->ring_tail
);
267 next_ring_head
= (dev
->ring_head
+ 1) % ring_buffer_size
;
269 if (next_ring_head
!= dev
->ring_tail
) {
270 memcpy(&((*dev
->ring_buffer
)[dev
->ring_head
]),
271 dev
->interrupt_in_buffer
, urb
->actual_length
);
272 dev
->ring_head
= next_ring_head
;
274 memset(dev
->interrupt_in_buffer
, 0, urb
->actual_length
);
276 dev_warn(&dev
->intf
->dev
,
277 "Ring buffer overflow, %d bytes dropped\n",
279 memset(dev
->interrupt_in_buffer
, 0, urb
->actual_length
);
284 /* resubmit if we're still running */
285 if (dev
->interrupt_in_running
&& dev
->intf
) {
286 retval
= usb_submit_urb(dev
->interrupt_in_urb
, GFP_ATOMIC
);
288 dev_err(&dev
->intf
->dev
,
289 "usb_submit_urb failed (%d)\n", retval
);
293 dev
->interrupt_in_done
= 1;
294 wake_up_interruptible(&dev
->read_wait
);
297 /** 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
);
314 /** usb_alphatrack_open */
315 static int usb_alphatrack_open(struct inode
*inode
, struct file
*file
)
317 struct usb_alphatrack
*dev
;
320 struct usb_interface
*interface
;
322 nonseekable_open(inode
, file
);
323 subminor
= iminor(inode
);
325 mutex_lock(&disconnect_mutex
);
327 interface
= usb_find_interface(&usb_alphatrack_driver
, subminor
);
330 pr_err("%s - error, can't find device for minor %d\n",
333 goto unlock_disconnect_exit
;
336 dev
= usb_get_intfdata(interface
);
340 goto unlock_disconnect_exit
;
343 /* lock this device */
344 if (mutex_lock_interruptible(&dev
->mtx
)) {
345 retval
= -ERESTARTSYS
;
346 goto unlock_disconnect_exit
;
349 /* allow opening only once */
350 if (dev
->open_count
) {
356 /* initialize in direction */
359 usb_fill_int_urb(dev
->interrupt_in_urb
,
360 interface_to_usbdev(interface
),
361 usb_rcvintpipe(interface_to_usbdev(interface
),
362 dev
->interrupt_in_endpoint
->
364 dev
->interrupt_in_buffer
,
365 dev
->interrupt_in_endpoint_size
,
366 usb_alphatrack_interrupt_in_callback
, dev
,
367 dev
->interrupt_in_interval
);
369 dev
->interrupt_in_running
= 1;
370 dev
->interrupt_in_done
= 0;
374 retval
= usb_submit_urb(dev
->interrupt_in_urb
, GFP_KERNEL
);
376 dev_err(&interface
->dev
,
377 "Couldn't submit interrupt_in_urb %d\n", retval
);
378 dev
->interrupt_in_running
= 0;
383 /* save device in the file's private structure */
384 file
->private_data
= dev
;
387 mutex_unlock(&dev
->mtx
);
389 unlock_disconnect_exit
:
390 mutex_unlock(&disconnect_mutex
);
395 /** usb_alphatrack_release */
396 static int usb_alphatrack_release(struct inode
*inode
, struct file
*file
)
398 struct usb_alphatrack
*dev
;
401 dev
= file
->private_data
;
408 if (mutex_lock_interruptible(&dev
->mtx
)) {
409 retval
= -ERESTARTSYS
;
413 if (dev
->open_count
!= 1) {
418 if (dev
->intf
== NULL
) {
419 /* the device was unplugged before the file was released */
420 mutex_unlock(&dev
->mtx
);
421 /* unlock here as usb_alphatrack_delete frees dev */
422 usb_alphatrack_delete(dev
);
427 /* wait until write transfer is finished */
428 if (dev
->interrupt_out_busy
)
429 wait_event_interruptible_timeout(dev
->write_wait
,
430 !dev
->interrupt_out_busy
,
432 usb_alphatrack_abort_transfers(dev
);
436 mutex_unlock(&dev
->mtx
);
442 /** usb_alphatrack_poll */
443 static unsigned int usb_alphatrack_poll(struct file
*file
, poll_table
*wait
)
445 struct usb_alphatrack
*dev
;
446 unsigned int mask
= 0;
448 dev
= file
->private_data
;
450 poll_wait(file
, &dev
->read_wait
, wait
);
451 poll_wait(file
, &dev
->write_wait
, wait
);
453 if (dev
->ring_head
!= dev
->ring_tail
)
454 mask
|= POLLIN
| POLLRDNORM
;
455 if (!dev
->interrupt_out_busy
)
456 mask
|= POLLOUT
| POLLWRNORM
;
461 /** usb_alphatrack_read */
462 static ssize_t
usb_alphatrack_read(struct file
*file
, char __user
*buffer
,
463 size_t count
, loff_t
*ppos
)
465 struct usb_alphatrack
*dev
;
470 dev
= file
->private_data
;
472 /* verify that we actually have some data to read */
476 /* lock this object */
477 if (mutex_lock_interruptible(&dev
->mtx
)) {
478 retval
= -ERESTARTSYS
;
482 /* verify that the device wasn't unplugged */
483 if (dev
->intf
== NULL
) {
485 pr_err("%s: No device or device unplugged %d\n",
490 while (dev
->ring_head
== dev
->ring_tail
) {
491 if (file
->f_flags
& O_NONBLOCK
) {
495 dev
->interrupt_in_done
= 0;
497 wait_event_interruptible(dev
->read_wait
,
498 dev
->interrupt_in_done
);
503 alphatrack_ocmd_info(&dev
->intf
->dev
,
504 &(*dev
->ring_buffer
)[dev
->ring_tail
].cmd
, "%s",
505 ": copying to userspace");
508 while ((c
< count
) && (dev
->ring_tail
!= dev
->ring_head
)) {
510 (&buffer
[c
], &(*dev
->ring_buffer
)[dev
->ring_tail
],
515 dev
->ring_tail
= (dev
->ring_tail
+ 1) % ring_buffer_size
;
517 dbg_info(&dev
->intf
->dev
, "%s: head, tail are %x, %x\n",
518 __func__
, dev
->ring_head
, dev
->ring_tail
);
523 /* unlock the device */
524 mutex_unlock(&dev
->mtx
);
530 /** usb_alphatrack_write */
531 static ssize_t
usb_alphatrack_write(struct file
*file
,
532 const char __user
*buffer
, size_t count
,
535 struct usb_alphatrack
*dev
;
536 size_t bytes_to_write
;
539 dev
= file
->private_data
;
541 /* verify that we actually have some data to write */
545 /* lock this object */
546 if (mutex_lock_interruptible(&dev
->mtx
)) {
547 retval
= -ERESTARTSYS
;
551 /* verify that the device wasn't unplugged */
552 if (dev
->intf
== NULL
) {
554 pr_err("%s: No device or device unplugged %d\n",
559 /* wait until previous transfer is finished */
560 if (dev
->interrupt_out_busy
) {
561 if (file
->f_flags
& O_NONBLOCK
) {
566 wait_event_interruptible(dev
->write_wait
,
567 !dev
->interrupt_out_busy
);
572 /* write the data into interrupt_out_buffer from userspace */
573 /* FIXME - if you write more than 12 bytes this breaks */
575 min(count
, write_buffer_size
* dev
->interrupt_out_endpoint_size
);
576 if (bytes_to_write
< count
)
577 dev_warn(&dev
->intf
->dev
,
578 "Write buffer overflow, %zd bytes dropped\n",
579 count
- bytes_to_write
);
581 dbg_info(&dev
->intf
->dev
, "%s: count = %zd, bytes_to_write = %zd\n",
582 __func__
, count
, bytes_to_write
);
584 if (copy_from_user(dev
->interrupt_out_buffer
, buffer
, bytes_to_write
)) {
589 if (dev
->interrupt_out_endpoint
== NULL
) {
590 dev_err(&dev
->intf
->dev
, "Endpoint should not be null!\n");
594 /* send off the urb */
595 usb_fill_int_urb(dev
->interrupt_out_urb
,
596 interface_to_usbdev(dev
->intf
),
597 usb_sndintpipe(interface_to_usbdev(dev
->intf
),
598 dev
->interrupt_out_endpoint
->
600 dev
->interrupt_out_buffer
, bytes_to_write
,
601 usb_alphatrack_interrupt_out_callback
, dev
,
602 dev
->interrupt_out_interval
);
603 dev
->interrupt_out_busy
= 1;
604 atomic_inc(&dev
->writes_pending
);
607 retval
= usb_submit_urb(dev
->interrupt_out_urb
, GFP_KERNEL
);
609 dev
->interrupt_out_busy
= 0;
610 dev_err(&dev
->intf
->dev
,
611 "Couldn't submit interrupt_out_urb %d\n", retval
);
612 atomic_dec(&dev
->writes_pending
);
615 retval
= bytes_to_write
;
618 /* unlock the device */
619 mutex_unlock(&dev
->mtx
);
625 /* file operations needed when we register this driver */
626 static const struct file_operations usb_alphatrack_fops
= {
627 .owner
= THIS_MODULE
,
628 .read
= usb_alphatrack_read
,
629 .write
= usb_alphatrack_write
,
630 .open
= usb_alphatrack_open
,
631 .release
= usb_alphatrack_release
,
632 .poll
= usb_alphatrack_poll
,
637 * usb class driver info in order to get a minor number from the usb core,
638 * and to have the device registered with the driver core
641 static struct usb_class_driver usb_alphatrack_class
= {
642 .name
= "alphatrack%d",
643 .fops
= &usb_alphatrack_fops
,
644 .minor_base
= USB_ALPHATRACK_MINOR_BASE
,
648 * usb_alphatrack_probe
650 * Called by the usb core when a new device is connected that it thinks
651 * this driver might be interested in.
653 static int usb_alphatrack_probe(struct usb_interface
*intf
,
654 const struct usb_device_id
*id
)
656 struct usb_device
*udev
= interface_to_usbdev(intf
);
657 struct usb_alphatrack
*dev
= NULL
;
658 struct usb_host_interface
*iface_desc
;
659 struct usb_endpoint_descriptor
*endpoint
;
662 int retval
= -ENOMEM
;
664 /* allocate memory for our device state and initialize it */
666 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
670 mutex_init(&dev
->mtx
);
672 init_waitqueue_head(&dev
->read_wait
);
673 init_waitqueue_head(&dev
->write_wait
);
675 iface_desc
= intf
->cur_altsetting
;
677 /* set up the endpoint information */
678 for (i
= 0; i
< iface_desc
->desc
.bNumEndpoints
; ++i
) {
679 endpoint
= &iface_desc
->endpoint
[i
].desc
;
681 if (usb_endpoint_is_int_in(endpoint
))
682 dev
->interrupt_in_endpoint
= endpoint
;
684 if (usb_endpoint_is_int_out(endpoint
))
685 dev
->interrupt_out_endpoint
= endpoint
;
687 if (dev
->interrupt_in_endpoint
== NULL
) {
688 dev_err(&intf
->dev
, "Interrupt in endpoint not found\n");
691 if (dev
->interrupt_out_endpoint
== NULL
)
693 "Interrupt out endpoint not found (using control endpoint instead)\n");
695 dev
->interrupt_in_endpoint_size
=
696 le16_to_cpu(dev
->interrupt_in_endpoint
->wMaxPacketSize
);
698 if (dev
->interrupt_in_endpoint_size
!= 64)
699 dev_warn(&intf
->dev
, "Interrupt in endpoint size is not 64!\n");
701 if (ring_buffer_size
== 0)
702 ring_buffer_size
= RING_BUFFER_SIZE
;
704 true_size
= min(ring_buffer_size
, RING_BUFFER_SIZE
);
707 * FIXME - there are more usb_alloc routines for dma correctness.
710 dev
->ring_buffer
= kmalloc_array(true_size
,
711 sizeof(struct alphatrack_icmd
),
713 if (!dev
->ring_buffer
)
716 dev
->interrupt_in_buffer
= kmalloc(dev
->interrupt_in_endpoint_size
,
718 if (!dev
->interrupt_in_buffer
)
721 dev
->oldi_buffer
= kmalloc(dev
->interrupt_in_endpoint_size
, GFP_KERNEL
);
722 if (!dev
->oldi_buffer
)
725 dev
->interrupt_in_urb
= usb_alloc_urb(0, GFP_KERNEL
);
726 if (!dev
->interrupt_in_urb
) {
727 dev_err(&intf
->dev
, "Couldn't allocate interrupt_in_urb\n");
731 dev
->interrupt_out_endpoint_size
=
732 dev
->interrupt_out_endpoint
? le16_to_cpu(dev
->
733 interrupt_out_endpoint
->
734 wMaxPacketSize
) : udev
->
735 descriptor
.bMaxPacketSize0
;
737 if (dev
->interrupt_out_endpoint_size
!= 64)
739 "Interrupt out endpoint size is not 64!)\n");
741 if (write_buffer_size
== 0)
742 write_buffer_size
= WRITE_BUFFER_SIZE
;
743 true_size
= min(write_buffer_size
, WRITE_BUFFER_SIZE
);
745 dev
->interrupt_out_buffer
=
746 kmalloc_array(true_size
,
747 dev
->interrupt_out_endpoint_size
,
749 if (!dev
->interrupt_out_buffer
)
752 dev
->write_buffer
= kmalloc_array(true_size
,
753 sizeof(struct alphatrack_ocmd
),
755 if (!dev
->write_buffer
)
758 dev
->interrupt_out_urb
= usb_alloc_urb(0, GFP_KERNEL
);
759 if (!dev
->interrupt_out_urb
) {
760 dev_err(&intf
->dev
, "Couldn't allocate interrupt_out_urb\n");
763 dev
->interrupt_in_interval
=
764 min_interrupt_in_interval
>
765 dev
->interrupt_in_endpoint
->
766 bInterval
? min_interrupt_in_interval
: dev
->interrupt_in_endpoint
->
768 if (dev
->interrupt_out_endpoint
)
769 dev
->interrupt_out_interval
=
770 min_interrupt_out_interval
>
771 dev
->interrupt_out_endpoint
->
772 bInterval
? min_interrupt_out_interval
: dev
->
773 interrupt_out_endpoint
->bInterval
;
775 /* we can register the device now, as it is ready */
776 usb_set_intfdata(intf
, dev
);
778 atomic_set(&dev
->writes_pending
, 0);
779 retval
= usb_register_dev(intf
, &usb_alphatrack_class
);
781 /* something prevented us from registering this driver */
783 "Not able to get a minor for this device.\n");
784 usb_set_intfdata(intf
, NULL
);
788 /* let the user know what node this device is now attached to */
790 "Alphatrack Device #%d now attached to major %d minor %d\n",
791 (intf
->minor
- USB_ALPHATRACK_MINOR_BASE
), USB_MAJOR
,
798 usb_alphatrack_delete(dev
);
804 * usb_alphatrack_disconnect
806 * Called by the usb core when the device is removed from the system.
808 static void usb_alphatrack_disconnect(struct usb_interface
*intf
)
810 struct usb_alphatrack
*dev
;
813 mutex_lock(&disconnect_mutex
);
815 dev
= usb_get_intfdata(intf
);
816 usb_set_intfdata(intf
, NULL
);
818 mutex_lock(&dev
->mtx
);
822 /* give back our minor */
823 usb_deregister_dev(intf
, &usb_alphatrack_class
);
825 /* if the device is not opened, then we clean up right now */
826 if (!dev
->open_count
) {
827 mutex_unlock(&dev
->mtx
);
828 usb_alphatrack_delete(dev
);
830 atomic_set(&dev
->writes_pending
, 0);
832 mutex_unlock(&dev
->mtx
);
835 mutex_unlock(&disconnect_mutex
);
837 dev_info(&intf
->dev
, "Alphatrack Surface #%d now disconnected\n",
838 (minor
- USB_ALPHATRACK_MINOR_BASE
));
841 /* usb specific object needed to register this driver with the usb subsystem */
842 static struct usb_driver usb_alphatrack_driver
= {
843 .name
= "alphatrack",
844 .probe
= usb_alphatrack_probe
,
845 .disconnect
= usb_alphatrack_disconnect
,
846 .id_table
= usb_alphatrack_table
,
849 module_usb_driver(usb_alphatrack_driver
);