2 * adutux - driver for ADU devices from Ontrak Control Systems
3 * This is an experimental driver. Use at your own risk.
4 * This driver is not supported by Ontrak Control Systems.
6 * Copyright (c) 2003 John Homppi (SCO, leave this notice here)
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
13 * derived from the Lego USB Tower driver 0.56:
14 * Copyright (c) 2003 David Glance <davidgsf@sourceforge.net>
15 * 2001 Juergen Stuber <stuber@loria.fr>
16 * that was derived from USB Skeleton driver - 0.5
17 * Copyright (c) 2001 Greg Kroah-Hartman (greg@kroah.com)
21 #include <linux/kernel.h>
22 #include <linux/errno.h>
23 #include <linux/init.h>
24 #include <linux/slab.h>
25 #include <linux/module.h>
26 #include <linux/usb.h>
27 #include <linux/mutex.h>
28 #include <asm/uaccess.h>
30 #ifdef CONFIG_USB_DEBUG
36 /* Use our own dbg macro */
38 #define dbg(lvl, format, arg...) \
41 printk(KERN_DEBUG __FILE__ " : " format " \n", ## arg); \
45 /* Version Information */
46 #define DRIVER_VERSION "v0.0.13"
47 #define DRIVER_AUTHOR "John Homppi"
48 #define DRIVER_DESC "adutux (see www.ontrak.net)"
50 /* Module parameters */
51 module_param(debug
, int, S_IRUGO
| S_IWUSR
);
52 MODULE_PARM_DESC(debug
, "Debug enabled or not");
54 /* Define these values to match your device */
55 #define ADU_VENDOR_ID 0x0a07
56 #define ADU_PRODUCT_ID 0x0064
58 /* table of devices that work with this driver */
59 static struct usb_device_id device_table
[] = {
60 { USB_DEVICE(ADU_VENDOR_ID
, ADU_PRODUCT_ID
) }, /* ADU100 */
61 { USB_DEVICE(ADU_VENDOR_ID
, ADU_PRODUCT_ID
+20) }, /* ADU120 */
62 { USB_DEVICE(ADU_VENDOR_ID
, ADU_PRODUCT_ID
+30) }, /* ADU130 */
63 { USB_DEVICE(ADU_VENDOR_ID
, ADU_PRODUCT_ID
+100) }, /* ADU200 */
64 { USB_DEVICE(ADU_VENDOR_ID
, ADU_PRODUCT_ID
+108) }, /* ADU208 */
65 { USB_DEVICE(ADU_VENDOR_ID
, ADU_PRODUCT_ID
+118) }, /* ADU218 */
66 { }/* Terminating entry */
69 MODULE_DEVICE_TABLE(usb
, device_table
);
71 #ifdef CONFIG_USB_DYNAMIC_MINORS
72 #define ADU_MINOR_BASE 0
74 #define ADU_MINOR_BASE 67
77 /* we can have up to this number of device plugged in at once */
78 #define MAX_DEVICES 16
80 #define COMMAND_TIMEOUT (2*HZ) /* 60 second timeout for a command */
82 /* Structure to hold all of our device specific stuff */
84 struct mutex mtx
; /* locks this structure */
85 struct usb_device
* udev
; /* save off the usb device pointer */
86 struct usb_interface
* interface
;
87 unsigned char minor
; /* the starting minor number for this device */
88 char serial_number
[8];
90 int open_count
; /* number of times this port has been opened */
92 char* read_buffer_primary
;
93 int read_buffer_length
;
94 char* read_buffer_secondary
;
99 wait_queue_head_t read_wait
;
100 wait_queue_head_t write_wait
;
102 char* interrupt_in_buffer
;
103 struct usb_endpoint_descriptor
* interrupt_in_endpoint
;
104 struct urb
* interrupt_in_urb
;
105 int read_urb_finished
;
107 char* interrupt_out_buffer
;
108 struct usb_endpoint_descriptor
* interrupt_out_endpoint
;
109 struct urb
* interrupt_out_urb
;
112 static struct usb_driver adu_driver
;
114 static void adu_debug_data(int level
, const char *function
, int size
,
115 const unsigned char *data
)
122 printk(KERN_DEBUG __FILE__
": %s - length = %d, data = ",
124 for (i
= 0; i
< size
; ++i
)
125 printk("%.2x ", data
[i
]);
130 * adu_abort_transfers
131 * aborts transfers and frees associated data structures
133 static void adu_abort_transfers(struct adu_device
*dev
)
135 dbg(2," %s : enter", __FUNCTION__
);
138 dbg(1," %s : dev is null", __FUNCTION__
);
142 if (dev
->udev
== NULL
) {
143 dbg(1," %s : udev is null", __FUNCTION__
);
147 dbg(2," %s : udev state %d", __FUNCTION__
, dev
->udev
->state
);
148 if (dev
->udev
->state
== USB_STATE_NOTATTACHED
) {
149 dbg(1," %s : udev is not attached", __FUNCTION__
);
153 /* shutdown transfer */
154 usb_unlink_urb(dev
->interrupt_in_urb
);
155 usb_unlink_urb(dev
->interrupt_out_urb
);
158 dbg(2," %s : leave", __FUNCTION__
);
161 static void adu_delete(struct adu_device
*dev
)
163 dbg(2, "%s enter", __FUNCTION__
);
165 adu_abort_transfers(dev
);
167 /* free data structures */
168 usb_free_urb(dev
->interrupt_in_urb
);
169 usb_free_urb(dev
->interrupt_out_urb
);
170 kfree(dev
->read_buffer_primary
);
171 kfree(dev
->read_buffer_secondary
);
172 kfree(dev
->interrupt_in_buffer
);
173 kfree(dev
->interrupt_out_buffer
);
176 dbg(2, "%s : leave", __FUNCTION__
);
179 static void adu_interrupt_in_callback(struct urb
*urb
)
181 struct adu_device
*dev
= urb
->context
;
182 int status
= urb
->status
;
184 dbg(4," %s : enter, status %d", __FUNCTION__
, status
);
185 adu_debug_data(5, __FUNCTION__
, urb
->actual_length
,
186 urb
->transfer_buffer
);
188 spin_lock(&dev
->buflock
);
191 if ((status
!= -ENOENT
) && (status
!= -ECONNRESET
) &&
192 (status
!= -ESHUTDOWN
)) {
193 dbg(1," %s : nonzero status received: %d",
194 __FUNCTION__
, status
);
199 if (urb
->actual_length
> 0 && dev
->interrupt_in_buffer
[0] != 0x00) {
200 if (dev
->read_buffer_length
<
201 (4 * le16_to_cpu(dev
->interrupt_in_endpoint
->wMaxPacketSize
)) -
202 (urb
->actual_length
)) {
203 memcpy (dev
->read_buffer_primary
+
204 dev
->read_buffer_length
,
205 dev
->interrupt_in_buffer
, urb
->actual_length
);
207 dev
->read_buffer_length
+= urb
->actual_length
;
208 dbg(2," %s reading %d ", __FUNCTION__
,
211 dbg(1," %s : read_buffer overflow", __FUNCTION__
);
216 dev
->read_urb_finished
= 1;
217 spin_unlock(&dev
->buflock
);
218 /* always wake up so we recover from errors */
219 wake_up_interruptible(&dev
->read_wait
);
220 adu_debug_data(5, __FUNCTION__
, urb
->actual_length
,
221 urb
->transfer_buffer
);
222 dbg(4," %s : leave, status %d", __FUNCTION__
, status
);
225 static void adu_interrupt_out_callback(struct urb
*urb
)
227 struct adu_device
*dev
= urb
->context
;
228 int status
= urb
->status
;
230 dbg(4," %s : enter, status %d", __FUNCTION__
, status
);
231 adu_debug_data(5,__FUNCTION__
, urb
->actual_length
, urb
->transfer_buffer
);
234 if ((status
!= -ENOENT
) &&
235 (status
!= -ECONNRESET
)) {
236 dbg(1, " %s :nonzero status received: %d",
237 __FUNCTION__
, status
);
242 wake_up_interruptible(&dev
->write_wait
);
245 adu_debug_data(5, __FUNCTION__
, urb
->actual_length
,
246 urb
->transfer_buffer
);
247 dbg(4," %s : leave, status %d", __FUNCTION__
, status
);
250 static int adu_open(struct inode
*inode
, struct file
*file
)
252 struct adu_device
*dev
= NULL
;
253 struct usb_interface
*interface
;
257 dbg(2,"%s : enter", __FUNCTION__
);
259 subminor
= iminor(inode
);
261 interface
= usb_find_interface(&adu_driver
, subminor
);
263 err("%s - error, can't find device for minor %d",
264 __FUNCTION__
, subminor
);
269 dev
= usb_get_intfdata(interface
);
275 /* lock this device */
276 if ((retval
= mutex_lock_interruptible(&dev
->mtx
))) {
277 dbg(2, "%s : mutex lock failed", __FUNCTION__
);
281 /* increment our usage count for the device */
283 dbg(2,"%s : open count %d", __FUNCTION__
, dev
->open_count
);
285 /* save device in the file's private structure */
286 file
->private_data
= dev
;
288 if (dev
->open_count
== 1) {
289 /* initialize in direction */
290 dev
->read_buffer_length
= 0;
292 /* fixup first read by having urb waiting for it */
293 usb_fill_int_urb(dev
->interrupt_in_urb
,dev
->udev
,
294 usb_rcvintpipe(dev
->udev
,
295 dev
->interrupt_in_endpoint
->bEndpointAddress
),
296 dev
->interrupt_in_buffer
,
297 le16_to_cpu(dev
->interrupt_in_endpoint
->wMaxPacketSize
),
298 adu_interrupt_in_callback
, dev
,
299 dev
->interrupt_in_endpoint
->bInterval
);
300 /* dev->interrupt_in_urb->transfer_flags |= URB_ASYNC_UNLINK; */
301 dev
->read_urb_finished
= 0;
302 retval
= usb_submit_urb(dev
->interrupt_in_urb
, GFP_KERNEL
);
306 mutex_unlock(&dev
->mtx
);
309 dbg(2,"%s : leave, return value %d ", __FUNCTION__
, retval
);
314 static int adu_release_internal(struct adu_device
*dev
)
318 dbg(2," %s : enter", __FUNCTION__
);
320 /* decrement our usage count for the device */
322 dbg(2," %s : open count %d", __FUNCTION__
, dev
->open_count
);
323 if (dev
->open_count
<= 0) {
324 adu_abort_transfers(dev
);
328 dbg(2," %s : leave", __FUNCTION__
);
332 static int adu_release(struct inode
*inode
, struct file
*file
)
334 struct adu_device
*dev
= NULL
;
337 dbg(2," %s : enter", __FUNCTION__
);
340 dbg(1," %s : file is NULL", __FUNCTION__
);
345 dev
= file
->private_data
;
348 dbg(1," %s : object is NULL", __FUNCTION__
);
353 /* lock our device */
354 mutex_lock(&dev
->mtx
); /* not interruptible */
356 if (dev
->open_count
<= 0) {
357 dbg(1," %s : device not opened", __FUNCTION__
);
362 if (dev
->udev
== NULL
) {
363 /* the device was unplugged before the file was released */
364 mutex_unlock(&dev
->mtx
);
369 retval
= adu_release_internal(dev
);
374 mutex_unlock(&dev
->mtx
);
375 dbg(2," %s : leave, return value %d", __FUNCTION__
, retval
);
379 static ssize_t
adu_read(struct file
*file
, __user
char *buffer
, size_t count
,
382 struct adu_device
*dev
;
383 size_t bytes_read
= 0;
384 size_t bytes_to_read
= count
;
388 int should_submit
= 0;
390 DECLARE_WAITQUEUE(wait
, current
);
392 dbg(2," %s : enter, count = %Zd, file=%p", __FUNCTION__
, count
, file
);
394 dev
= file
->private_data
;
395 dbg(2," %s : dev=%p", __FUNCTION__
, dev
);
396 /* lock this object */
397 if (mutex_lock_interruptible(&dev
->mtx
))
400 /* verify that the device wasn't unplugged */
401 if (dev
->udev
== NULL
|| dev
->minor
== 0) {
403 err("No device or device unplugged %d", retval
);
407 /* verify that some data was requested */
409 dbg(1," %s : read request of 0 bytes", __FUNCTION__
);
413 timeout
= COMMAND_TIMEOUT
;
414 dbg(2," %s : about to start looping", __FUNCTION__
);
415 while (bytes_to_read
) {
416 int data_in_secondary
= dev
->secondary_tail
- dev
->secondary_head
;
417 dbg(2," %s : while, data_in_secondary=%d, status=%d",
418 __FUNCTION__
, data_in_secondary
,
419 dev
->interrupt_in_urb
->status
);
421 if (data_in_secondary
) {
422 /* drain secondary buffer */
423 int amount
= bytes_to_read
< data_in_secondary
? bytes_to_read
: data_in_secondary
;
424 i
= copy_to_user(buffer
, dev
->read_buffer_secondary
+dev
->secondary_head
, amount
);
429 dev
->secondary_head
+= (amount
- i
);
430 bytes_read
+= (amount
- i
);
431 bytes_to_read
-= (amount
- i
);
433 retval
= bytes_read
? bytes_read
: -EFAULT
;
437 /* we check the primary buffer */
438 spin_lock_irqsave (&dev
->buflock
, flags
);
439 if (dev
->read_buffer_length
) {
440 /* we secure access to the primary */
442 dbg(2," %s : swap, read_buffer_length = %d",
443 __FUNCTION__
, dev
->read_buffer_length
);
444 tmp
= dev
->read_buffer_secondary
;
445 dev
->read_buffer_secondary
= dev
->read_buffer_primary
;
446 dev
->read_buffer_primary
= tmp
;
447 dev
->secondary_head
= 0;
448 dev
->secondary_tail
= dev
->read_buffer_length
;
449 dev
->read_buffer_length
= 0;
450 spin_unlock_irqrestore(&dev
->buflock
, flags
);
451 /* we have a free buffer so use it */
454 /* even the primary was empty - we may need to do IO */
455 if (dev
->interrupt_in_urb
->status
== -EINPROGRESS
) {
456 /* somebody is doing IO */
457 spin_unlock_irqrestore(&dev
->buflock
, flags
);
458 dbg(2," %s : submitted already", __FUNCTION__
);
460 /* we must initiate input */
461 dbg(2," %s : initiate input", __FUNCTION__
);
462 dev
->read_urb_finished
= 0;
464 usb_fill_int_urb(dev
->interrupt_in_urb
,dev
->udev
,
465 usb_rcvintpipe(dev
->udev
,
466 dev
->interrupt_in_endpoint
->bEndpointAddress
),
467 dev
->interrupt_in_buffer
,
468 le16_to_cpu(dev
->interrupt_in_endpoint
->wMaxPacketSize
),
469 adu_interrupt_in_callback
,
471 dev
->interrupt_in_endpoint
->bInterval
);
472 retval
= usb_submit_urb(dev
->interrupt_in_urb
, GFP_ATOMIC
);
474 spin_unlock_irqrestore(&dev
->buflock
, flags
);
475 dbg(2," %s : submitted OK", __FUNCTION__
);
477 if (retval
== -ENOMEM
) {
478 retval
= bytes_read
? bytes_read
: -ENOMEM
;
480 spin_unlock_irqrestore(&dev
->buflock
, flags
);
481 dbg(2," %s : submit failed", __FUNCTION__
);
486 /* we wait for I/O to complete */
487 set_current_state(TASK_INTERRUPTIBLE
);
488 add_wait_queue(&dev
->read_wait
, &wait
);
489 if (!dev
->read_urb_finished
)
490 timeout
= schedule_timeout(COMMAND_TIMEOUT
);
492 set_current_state(TASK_RUNNING
);
493 remove_wait_queue(&dev
->read_wait
, &wait
);
496 dbg(2," %s : timeout", __FUNCTION__
);
497 retval
= bytes_read
? bytes_read
: -ETIMEDOUT
;
501 if (signal_pending(current
)) {
502 dbg(2," %s : signal pending", __FUNCTION__
);
503 retval
= bytes_read
? bytes_read
: -EINTR
;
511 /* if the primary buffer is empty then use it */
512 if (should_submit
&& !dev
->interrupt_in_urb
->status
==-EINPROGRESS
) {
513 usb_fill_int_urb(dev
->interrupt_in_urb
,dev
->udev
,
514 usb_rcvintpipe(dev
->udev
,
515 dev
->interrupt_in_endpoint
->bEndpointAddress
),
516 dev
->interrupt_in_buffer
,
517 le16_to_cpu(dev
->interrupt_in_endpoint
->wMaxPacketSize
),
518 adu_interrupt_in_callback
,
520 dev
->interrupt_in_endpoint
->bInterval
);
521 /* dev->interrupt_in_urb->transfer_flags |= URB_ASYNC_UNLINK; */
522 dev
->read_urb_finished
= 0;
523 usb_submit_urb(dev
->interrupt_in_urb
, GFP_KERNEL
);
524 /* we ignore failure */
528 /* unlock the device */
529 mutex_unlock(&dev
->mtx
);
531 dbg(2," %s : leave, return value %d", __FUNCTION__
, retval
);
535 static ssize_t
adu_write(struct file
*file
, const __user
char *buffer
,
536 size_t count
, loff_t
*ppos
)
538 struct adu_device
*dev
;
539 size_t bytes_written
= 0;
540 size_t bytes_to_write
;
545 dbg(2," %s : enter, count = %Zd", __FUNCTION__
, count
);
547 dev
= file
->private_data
;
549 /* lock this object */
550 retval
= mutex_lock_interruptible(&dev
->mtx
);
554 /* verify that the device wasn't unplugged */
555 if (dev
->udev
== NULL
|| dev
->minor
== 0) {
557 err("No device or device unplugged %d", retval
);
561 /* verify that we actually have some data to write */
563 dbg(1," %s : write request of 0 bytes", __FUNCTION__
);
569 if (dev
->interrupt_out_urb
->status
== -EINPROGRESS
) {
570 timeout
= COMMAND_TIMEOUT
;
572 while (timeout
> 0) {
573 if (signal_pending(current
)) {
574 dbg(1," %s : interrupted", __FUNCTION__
);
578 mutex_unlock(&dev
->mtx
);
579 timeout
= interruptible_sleep_on_timeout(&dev
->write_wait
, timeout
);
580 retval
= mutex_lock_interruptible(&dev
->mtx
);
582 retval
= bytes_written
? bytes_written
: retval
;
588 dbg(1," %s : interrupted timeout: %d", __FUNCTION__
, timeout
);
592 dbg(1," %s : final timeout: %d", __FUNCTION__
, timeout
);
595 dbg(1, "%s - command timed out.", __FUNCTION__
);
600 dbg(4," %s : in progress, count = %Zd", __FUNCTION__
, count
);
603 dbg(4," %s : sending, count = %Zd", __FUNCTION__
, count
);
605 /* write the data into interrupt_out_buffer from userspace */
606 buffer_size
= le16_to_cpu(dev
->interrupt_out_endpoint
->wMaxPacketSize
);
607 bytes_to_write
= count
> buffer_size
? buffer_size
: count
;
608 dbg(4," %s : buffer_size = %Zd, count = %Zd, bytes_to_write = %Zd",
609 __FUNCTION__
, buffer_size
, count
, bytes_to_write
);
611 if (copy_from_user(dev
->interrupt_out_buffer
, buffer
, bytes_to_write
) != 0) {
616 /* send off the urb */
618 dev
->interrupt_out_urb
,
620 usb_sndintpipe(dev
->udev
, dev
->interrupt_out_endpoint
->bEndpointAddress
),
621 dev
->interrupt_out_buffer
,
623 adu_interrupt_out_callback
,
625 dev
->interrupt_in_endpoint
->bInterval
);
626 /* dev->interrupt_in_urb->transfer_flags |= URB_ASYNC_UNLINK; */
627 dev
->interrupt_out_urb
->actual_length
= bytes_to_write
;
628 retval
= usb_submit_urb(dev
->interrupt_out_urb
, GFP_KERNEL
);
630 err("Couldn't submit interrupt_out_urb %d", retval
);
634 buffer
+= bytes_to_write
;
635 count
-= bytes_to_write
;
637 bytes_written
+= bytes_to_write
;
641 retval
= bytes_written
;
644 /* unlock the device */
645 mutex_unlock(&dev
->mtx
);
648 dbg(2," %s : leave, return value %d", __FUNCTION__
, retval
);
653 /* file operations needed when we register this driver */
654 static const struct file_operations adu_fops
= {
655 .owner
= THIS_MODULE
,
659 .release
= adu_release
,
663 * usb class driver info in order to get a minor number from the usb core,
664 * and to have the device registered with devfs and the driver core
666 static struct usb_class_driver adu_class
= {
667 .name
= "usb/adutux%d",
669 .minor_base
= ADU_MINOR_BASE
,
675 * Called by the usb core when a new device is connected that it thinks
676 * this driver might be interested in.
678 static int adu_probe(struct usb_interface
*interface
,
679 const struct usb_device_id
*id
)
681 struct usb_device
*udev
= interface_to_usbdev(interface
);
682 struct adu_device
*dev
= NULL
;
683 struct usb_host_interface
*iface_desc
;
684 struct usb_endpoint_descriptor
*endpoint
;
685 int retval
= -ENODEV
;
690 dbg(2," %s : enter", __FUNCTION__
);
693 dev_err(&interface
->dev
, "udev is NULL.\n");
697 /* allocate memory for our device state and intialize it */
698 dev
= kzalloc(sizeof(struct adu_device
), GFP_KERNEL
);
700 dev_err(&interface
->dev
, "Out of memory\n");
705 mutex_init(&dev
->mtx
);
706 spin_lock_init(&dev
->buflock
);
708 init_waitqueue_head(&dev
->read_wait
);
709 init_waitqueue_head(&dev
->write_wait
);
711 iface_desc
= &interface
->altsetting
[0];
713 /* set up the endpoint information */
714 for (i
= 0; i
< iface_desc
->desc
.bNumEndpoints
; ++i
) {
715 endpoint
= &iface_desc
->endpoint
[i
].desc
;
717 if (usb_endpoint_is_int_in(endpoint
))
718 dev
->interrupt_in_endpoint
= endpoint
;
720 if (usb_endpoint_is_int_out(endpoint
))
721 dev
->interrupt_out_endpoint
= endpoint
;
723 if (dev
->interrupt_in_endpoint
== NULL
) {
724 dev_err(&interface
->dev
, "interrupt in endpoint not found\n");
727 if (dev
->interrupt_out_endpoint
== NULL
) {
728 dev_err(&interface
->dev
, "interrupt out endpoint not found\n");
732 in_end_size
= le16_to_cpu(dev
->interrupt_in_endpoint
->wMaxPacketSize
);
733 out_end_size
= le16_to_cpu(dev
->interrupt_out_endpoint
->wMaxPacketSize
);
735 dev
->read_buffer_primary
= kmalloc((4 * in_end_size
), GFP_KERNEL
);
736 if (!dev
->read_buffer_primary
) {
737 dev_err(&interface
->dev
, "Couldn't allocate read_buffer_primary\n");
742 /* debug code prime the buffer */
743 memset(dev
->read_buffer_primary
, 'a', in_end_size
);
744 memset(dev
->read_buffer_primary
+ in_end_size
, 'b', in_end_size
);
745 memset(dev
->read_buffer_primary
+ (2 * in_end_size
), 'c', in_end_size
);
746 memset(dev
->read_buffer_primary
+ (3 * in_end_size
), 'd', in_end_size
);
748 dev
->read_buffer_secondary
= kmalloc((4 * in_end_size
), GFP_KERNEL
);
749 if (!dev
->read_buffer_secondary
) {
750 dev_err(&interface
->dev
, "Couldn't allocate read_buffer_secondary\n");
755 /* debug code prime the buffer */
756 memset(dev
->read_buffer_secondary
, 'e', in_end_size
);
757 memset(dev
->read_buffer_secondary
+ in_end_size
, 'f', in_end_size
);
758 memset(dev
->read_buffer_secondary
+ (2 * in_end_size
), 'g', in_end_size
);
759 memset(dev
->read_buffer_secondary
+ (3 * in_end_size
), 'h', in_end_size
);
761 dev
->interrupt_in_buffer
= kmalloc(in_end_size
, GFP_KERNEL
);
762 if (!dev
->interrupt_in_buffer
) {
763 dev_err(&interface
->dev
, "Couldn't allocate interrupt_in_buffer\n");
767 /* debug code prime the buffer */
768 memset(dev
->interrupt_in_buffer
, 'i', in_end_size
);
770 dev
->interrupt_in_urb
= usb_alloc_urb(0, GFP_KERNEL
);
771 if (!dev
->interrupt_in_urb
) {
772 dev_err(&interface
->dev
, "Couldn't allocate interrupt_in_urb\n");
775 dev
->interrupt_out_buffer
= kmalloc(out_end_size
, GFP_KERNEL
);
776 if (!dev
->interrupt_out_buffer
) {
777 dev_err(&interface
->dev
, "Couldn't allocate interrupt_out_buffer\n");
780 dev
->interrupt_out_urb
= usb_alloc_urb(0, GFP_KERNEL
);
781 if (!dev
->interrupt_out_urb
) {
782 dev_err(&interface
->dev
, "Couldn't allocate interrupt_out_urb\n");
786 if (!usb_string(udev
, udev
->descriptor
.iSerialNumber
, dev
->serial_number
,
787 sizeof(dev
->serial_number
))) {
788 dev_err(&interface
->dev
, "Could not retrieve serial number\n");
791 dbg(2," %s : serial_number=%s", __FUNCTION__
, dev
->serial_number
);
793 /* we can register the device now, as it is ready */
794 usb_set_intfdata(interface
, dev
);
796 retval
= usb_register_dev(interface
, &adu_class
);
799 /* something prevented us from registering this driver */
800 dev_err(&interface
->dev
, "Not able to get a minor for this device.\n");
801 usb_set_intfdata(interface
, NULL
);
805 dev
->minor
= interface
->minor
;
807 /* let the user know what node this device is now attached to */
808 dev_info(&interface
->dev
, "ADU%d %s now attached to /dev/usb/adutux%d\n",
809 udev
->descriptor
.idProduct
, dev
->serial_number
,
810 (dev
->minor
- ADU_MINOR_BASE
));
812 dbg(2," %s : leave, return value %p (dev)", __FUNCTION__
, dev
);
824 * Called by the usb core when the device is removed from the system.
826 static void adu_disconnect(struct usb_interface
*interface
)
828 struct adu_device
*dev
;
831 dbg(2," %s : enter", __FUNCTION__
);
833 dev
= usb_get_intfdata(interface
);
834 usb_set_intfdata(interface
, NULL
);
838 /* give back our minor */
839 usb_deregister_dev(interface
, &adu_class
);
842 mutex_lock(&dev
->mtx
); /* not interruptible */
844 /* if the device is not opened, then we clean up right now */
845 dbg(2," %s : open count %d", __FUNCTION__
, dev
->open_count
);
846 if (!dev
->open_count
) {
847 mutex_unlock(&dev
->mtx
);
851 mutex_unlock(&dev
->mtx
);
854 dev_info(&interface
->dev
, "ADU device adutux%d now disconnected\n",
855 (minor
- ADU_MINOR_BASE
));
857 dbg(2," %s : leave", __FUNCTION__
);
860 /* usb specific object needed to register this driver with the usb subsystem */
861 static struct usb_driver adu_driver
= {
864 .disconnect
= adu_disconnect
,
865 .id_table
= device_table
,
868 static int __init
adu_init(void)
872 dbg(2," %s : enter", __FUNCTION__
);
874 /* register this driver with the USB subsystem */
875 result
= usb_register(&adu_driver
);
877 err("usb_register failed for the "__FILE__
" driver. "
878 "Error number %d", result
);
882 info("adutux " DRIVER_DESC
" " DRIVER_VERSION
);
883 info("adutux is an experimental driver. Use at your own risk");
886 dbg(2," %s : leave, return value %d", __FUNCTION__
, result
);
891 static void __exit
adu_exit(void)
893 dbg(2," %s : enter", __FUNCTION__
);
894 /* deregister this driver with the USB subsystem */
895 usb_deregister(&adu_driver
);
896 dbg(2," %s : leave", __FUNCTION__
);
899 module_init(adu_init
);
900 module_exit(adu_exit
);
902 MODULE_AUTHOR(DRIVER_AUTHOR
);
903 MODULE_DESCRIPTION(DRIVER_DESC
);
904 MODULE_LICENSE("GPL");