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 "%s: " format "\n", __FILE__, ##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 const 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 */
83 * The locking scheme is a vanilla 3-lock:
84 * adu_device.buflock: A spinlock, covers what IRQs touch.
85 * adutux_mutex: A Static lock to cover open_count. It would also cover
86 * any globals, but we don't have them in 2.6.
87 * adu_device.mtx: A mutex to hold across sleepers like copy_from_user.
88 * It covers all of adu_device, except the open_count
89 * and what .buflock covers.
92 /* Structure to hold all of our device specific stuff */
95 struct usb_device
* udev
; /* save off the usb device pointer */
96 struct usb_interface
* interface
;
97 unsigned int minor
; /* the starting minor number for this device */
98 char serial_number
[8];
100 int open_count
; /* number of times this port has been opened */
102 char* read_buffer_primary
;
103 int read_buffer_length
;
104 char* read_buffer_secondary
;
109 wait_queue_head_t read_wait
;
110 wait_queue_head_t write_wait
;
112 char* interrupt_in_buffer
;
113 struct usb_endpoint_descriptor
* interrupt_in_endpoint
;
114 struct urb
* interrupt_in_urb
;
115 int read_urb_finished
;
117 char* interrupt_out_buffer
;
118 struct usb_endpoint_descriptor
* interrupt_out_endpoint
;
119 struct urb
* interrupt_out_urb
;
120 int out_urb_finished
;
123 static DEFINE_MUTEX(adutux_mutex
);
125 static struct usb_driver adu_driver
;
127 static void adu_debug_data(int level
, const char *function
, int size
,
128 const unsigned char *data
)
135 printk(KERN_DEBUG
"%s: %s - length = %d, data = ",
136 __FILE__
, function
, size
);
137 for (i
= 0; i
< size
; ++i
)
138 printk("%.2x ", data
[i
]);
143 * adu_abort_transfers
144 * aborts transfers and frees associated data structures
146 static void adu_abort_transfers(struct adu_device
*dev
)
150 dbg(2," %s : enter", __func__
);
152 if (dev
->udev
== NULL
) {
153 dbg(1," %s : udev is null", __func__
);
157 /* shutdown transfer */
159 /* XXX Anchor these instead */
160 spin_lock_irqsave(&dev
->buflock
, flags
);
161 if (!dev
->read_urb_finished
) {
162 spin_unlock_irqrestore(&dev
->buflock
, flags
);
163 usb_kill_urb(dev
->interrupt_in_urb
);
165 spin_unlock_irqrestore(&dev
->buflock
, flags
);
167 spin_lock_irqsave(&dev
->buflock
, flags
);
168 if (!dev
->out_urb_finished
) {
169 spin_unlock_irqrestore(&dev
->buflock
, flags
);
170 usb_kill_urb(dev
->interrupt_out_urb
);
172 spin_unlock_irqrestore(&dev
->buflock
, flags
);
175 dbg(2," %s : leave", __func__
);
178 static void adu_delete(struct adu_device
*dev
)
180 dbg(2, "%s enter", __func__
);
182 /* free data structures */
183 usb_free_urb(dev
->interrupt_in_urb
);
184 usb_free_urb(dev
->interrupt_out_urb
);
185 kfree(dev
->read_buffer_primary
);
186 kfree(dev
->read_buffer_secondary
);
187 kfree(dev
->interrupt_in_buffer
);
188 kfree(dev
->interrupt_out_buffer
);
191 dbg(2, "%s : leave", __func__
);
194 static void adu_interrupt_in_callback(struct urb
*urb
)
196 struct adu_device
*dev
= urb
->context
;
197 int status
= urb
->status
;
199 dbg(4," %s : enter, status %d", __func__
, status
);
200 adu_debug_data(5, __func__
, urb
->actual_length
,
201 urb
->transfer_buffer
);
203 spin_lock(&dev
->buflock
);
206 if ((status
!= -ENOENT
) && (status
!= -ECONNRESET
) &&
207 (status
!= -ESHUTDOWN
)) {
208 dbg(1," %s : nonzero status received: %d",
214 if (urb
->actual_length
> 0 && dev
->interrupt_in_buffer
[0] != 0x00) {
215 if (dev
->read_buffer_length
<
216 (4 * le16_to_cpu(dev
->interrupt_in_endpoint
->wMaxPacketSize
)) -
217 (urb
->actual_length
)) {
218 memcpy (dev
->read_buffer_primary
+
219 dev
->read_buffer_length
,
220 dev
->interrupt_in_buffer
, urb
->actual_length
);
222 dev
->read_buffer_length
+= urb
->actual_length
;
223 dbg(2," %s reading %d ", __func__
,
226 dbg(1," %s : read_buffer overflow", __func__
);
231 dev
->read_urb_finished
= 1;
232 spin_unlock(&dev
->buflock
);
233 /* always wake up so we recover from errors */
234 wake_up_interruptible(&dev
->read_wait
);
235 adu_debug_data(5, __func__
, urb
->actual_length
,
236 urb
->transfer_buffer
);
237 dbg(4," %s : leave, status %d", __func__
, status
);
240 static void adu_interrupt_out_callback(struct urb
*urb
)
242 struct adu_device
*dev
= urb
->context
;
243 int status
= urb
->status
;
245 dbg(4," %s : enter, status %d", __func__
, status
);
246 adu_debug_data(5,__func__
, urb
->actual_length
, urb
->transfer_buffer
);
249 if ((status
!= -ENOENT
) &&
250 (status
!= -ECONNRESET
)) {
251 dbg(1, " %s :nonzero status received: %d",
257 spin_lock(&dev
->buflock
);
258 dev
->out_urb_finished
= 1;
259 wake_up(&dev
->write_wait
);
260 spin_unlock(&dev
->buflock
);
263 adu_debug_data(5, __func__
, urb
->actual_length
,
264 urb
->transfer_buffer
);
265 dbg(4," %s : leave, status %d", __func__
, status
);
268 static int adu_open(struct inode
*inode
, struct file
*file
)
270 struct adu_device
*dev
= NULL
;
271 struct usb_interface
*interface
;
275 dbg(2,"%s : enter", __func__
);
277 subminor
= iminor(inode
);
279 if ((retval
= mutex_lock_interruptible(&adutux_mutex
))) {
280 dbg(2, "%s : mutex lock failed", __func__
);
284 interface
= usb_find_interface(&adu_driver
, subminor
);
286 printk(KERN_ERR
"adutux: %s - error, can't find device for "
287 "minor %d\n", __func__
, subminor
);
292 dev
= usb_get_intfdata(interface
);
293 if (!dev
|| !dev
->udev
) {
298 /* check that nobody else is using the device */
299 if (dev
->open_count
) {
305 dbg(2,"%s : open count %d", __func__
, dev
->open_count
);
307 /* save device in the file's private structure */
308 file
->private_data
= dev
;
310 /* initialize in direction */
311 dev
->read_buffer_length
= 0;
313 /* fixup first read by having urb waiting for it */
314 usb_fill_int_urb(dev
->interrupt_in_urb
,dev
->udev
,
315 usb_rcvintpipe(dev
->udev
,
316 dev
->interrupt_in_endpoint
->bEndpointAddress
),
317 dev
->interrupt_in_buffer
,
318 le16_to_cpu(dev
->interrupt_in_endpoint
->wMaxPacketSize
),
319 adu_interrupt_in_callback
, dev
,
320 dev
->interrupt_in_endpoint
->bInterval
);
321 dev
->read_urb_finished
= 0;
322 if (usb_submit_urb(dev
->interrupt_in_urb
, GFP_KERNEL
))
323 dev
->read_urb_finished
= 1;
324 /* we ignore failure */
325 /* end of fixup for first read */
327 /* initialize out direction */
328 dev
->out_urb_finished
= 1;
333 mutex_unlock(&adutux_mutex
);
335 dbg(2,"%s : leave, return value %d ", __func__
, retval
);
339 static void adu_release_internal(struct adu_device
*dev
)
341 dbg(2," %s : enter", __func__
);
343 /* decrement our usage count for the device */
345 dbg(2," %s : open count %d", __func__
, dev
->open_count
);
346 if (dev
->open_count
<= 0) {
347 adu_abort_transfers(dev
);
351 dbg(2," %s : leave", __func__
);
354 static int adu_release(struct inode
*inode
, struct file
*file
)
356 struct adu_device
*dev
;
359 dbg(2," %s : enter", __func__
);
362 dbg(1," %s : file is NULL", __func__
);
367 dev
= file
->private_data
;
369 dbg(1," %s : object is NULL", __func__
);
374 mutex_lock(&adutux_mutex
); /* not interruptible */
376 if (dev
->open_count
<= 0) {
377 dbg(1," %s : device not opened", __func__
);
382 adu_release_internal(dev
);
383 if (dev
->udev
== NULL
) {
384 /* the device was unplugged before the file was released */
385 if (!dev
->open_count
) /* ... and we're the last user */
389 mutex_unlock(&adutux_mutex
);
391 dbg(2," %s : leave, return value %d", __func__
, retval
);
395 static ssize_t
adu_read(struct file
*file
, __user
char *buffer
, size_t count
,
398 struct adu_device
*dev
;
399 size_t bytes_read
= 0;
400 size_t bytes_to_read
= count
;
404 int should_submit
= 0;
406 DECLARE_WAITQUEUE(wait
, current
);
408 dbg(2," %s : enter, count = %Zd, file=%p", __func__
, count
, file
);
410 dev
= file
->private_data
;
411 dbg(2," %s : dev=%p", __func__
, dev
);
413 if (mutex_lock_interruptible(&dev
->mtx
))
416 /* verify that the device wasn't unplugged */
417 if (dev
->udev
== NULL
) {
419 printk(KERN_ERR
"adutux: No device or device unplugged %d\n",
424 /* verify that some data was requested */
426 dbg(1," %s : read request of 0 bytes", __func__
);
430 timeout
= COMMAND_TIMEOUT
;
431 dbg(2," %s : about to start looping", __func__
);
432 while (bytes_to_read
) {
433 int data_in_secondary
= dev
->secondary_tail
- dev
->secondary_head
;
434 dbg(2," %s : while, data_in_secondary=%d, status=%d",
435 __func__
, data_in_secondary
,
436 dev
->interrupt_in_urb
->status
);
438 if (data_in_secondary
) {
439 /* drain secondary buffer */
440 int amount
= bytes_to_read
< data_in_secondary
? bytes_to_read
: data_in_secondary
;
441 i
= copy_to_user(buffer
, dev
->read_buffer_secondary
+dev
->secondary_head
, amount
);
446 dev
->secondary_head
+= (amount
- i
);
447 bytes_read
+= (amount
- i
);
448 bytes_to_read
-= (amount
- i
);
450 retval
= bytes_read
? bytes_read
: -EFAULT
;
454 /* we check the primary buffer */
455 spin_lock_irqsave (&dev
->buflock
, flags
);
456 if (dev
->read_buffer_length
) {
457 /* we secure access to the primary */
459 dbg(2," %s : swap, read_buffer_length = %d",
460 __func__
, dev
->read_buffer_length
);
461 tmp
= dev
->read_buffer_secondary
;
462 dev
->read_buffer_secondary
= dev
->read_buffer_primary
;
463 dev
->read_buffer_primary
= tmp
;
464 dev
->secondary_head
= 0;
465 dev
->secondary_tail
= dev
->read_buffer_length
;
466 dev
->read_buffer_length
= 0;
467 spin_unlock_irqrestore(&dev
->buflock
, flags
);
468 /* we have a free buffer so use it */
471 /* even the primary was empty - we may need to do IO */
472 if (!dev
->read_urb_finished
) {
473 /* somebody is doing IO */
474 spin_unlock_irqrestore(&dev
->buflock
, flags
);
475 dbg(2," %s : submitted already", __func__
);
477 /* we must initiate input */
478 dbg(2," %s : initiate input", __func__
);
479 dev
->read_urb_finished
= 0;
480 spin_unlock_irqrestore(&dev
->buflock
, flags
);
482 usb_fill_int_urb(dev
->interrupt_in_urb
,dev
->udev
,
483 usb_rcvintpipe(dev
->udev
,
484 dev
->interrupt_in_endpoint
->bEndpointAddress
),
485 dev
->interrupt_in_buffer
,
486 le16_to_cpu(dev
->interrupt_in_endpoint
->wMaxPacketSize
),
487 adu_interrupt_in_callback
,
489 dev
->interrupt_in_endpoint
->bInterval
);
490 retval
= usb_submit_urb(dev
->interrupt_in_urb
, GFP_KERNEL
);
492 dev
->read_urb_finished
= 1;
493 if (retval
== -ENOMEM
) {
494 retval
= bytes_read
? bytes_read
: -ENOMEM
;
496 dbg(2," %s : submit failed", __func__
);
501 /* we wait for I/O to complete */
502 set_current_state(TASK_INTERRUPTIBLE
);
503 add_wait_queue(&dev
->read_wait
, &wait
);
504 spin_lock_irqsave(&dev
->buflock
, flags
);
505 if (!dev
->read_urb_finished
) {
506 spin_unlock_irqrestore(&dev
->buflock
, flags
);
507 timeout
= schedule_timeout(COMMAND_TIMEOUT
);
509 spin_unlock_irqrestore(&dev
->buflock
, flags
);
510 set_current_state(TASK_RUNNING
);
512 remove_wait_queue(&dev
->read_wait
, &wait
);
515 dbg(2," %s : timeout", __func__
);
516 retval
= bytes_read
? bytes_read
: -ETIMEDOUT
;
520 if (signal_pending(current
)) {
521 dbg(2," %s : signal pending", __func__
);
522 retval
= bytes_read
? bytes_read
: -EINTR
;
530 /* if the primary buffer is empty then use it */
531 spin_lock_irqsave(&dev
->buflock
, flags
);
532 if (should_submit
&& dev
->read_urb_finished
) {
533 dev
->read_urb_finished
= 0;
534 spin_unlock_irqrestore(&dev
->buflock
, flags
);
535 usb_fill_int_urb(dev
->interrupt_in_urb
,dev
->udev
,
536 usb_rcvintpipe(dev
->udev
,
537 dev
->interrupt_in_endpoint
->bEndpointAddress
),
538 dev
->interrupt_in_buffer
,
539 le16_to_cpu(dev
->interrupt_in_endpoint
->wMaxPacketSize
),
540 adu_interrupt_in_callback
,
542 dev
->interrupt_in_endpoint
->bInterval
);
543 if (usb_submit_urb(dev
->interrupt_in_urb
, GFP_KERNEL
) != 0)
544 dev
->read_urb_finished
= 1;
545 /* we ignore failure */
547 spin_unlock_irqrestore(&dev
->buflock
, flags
);
551 /* unlock the device */
552 mutex_unlock(&dev
->mtx
);
554 dbg(2," %s : leave, return value %d", __func__
, retval
);
558 static ssize_t
adu_write(struct file
*file
, const __user
char *buffer
,
559 size_t count
, loff_t
*ppos
)
561 DECLARE_WAITQUEUE(waita
, current
);
562 struct adu_device
*dev
;
563 size_t bytes_written
= 0;
564 size_t bytes_to_write
;
569 dbg(2," %s : enter, count = %Zd", __func__
, count
);
571 dev
= file
->private_data
;
573 retval
= mutex_lock_interruptible(&dev
->mtx
);
577 /* verify that the device wasn't unplugged */
578 if (dev
->udev
== NULL
) {
580 printk(KERN_ERR
"adutux: No device or device unplugged %d\n",
585 /* verify that we actually have some data to write */
587 dbg(1," %s : write request of 0 bytes", __func__
);
592 add_wait_queue(&dev
->write_wait
, &waita
);
593 set_current_state(TASK_INTERRUPTIBLE
);
594 spin_lock_irqsave(&dev
->buflock
, flags
);
595 if (!dev
->out_urb_finished
) {
596 spin_unlock_irqrestore(&dev
->buflock
, flags
);
598 mutex_unlock(&dev
->mtx
);
599 if (signal_pending(current
)) {
600 dbg(1," %s : interrupted", __func__
);
601 set_current_state(TASK_RUNNING
);
605 if (schedule_timeout(COMMAND_TIMEOUT
) == 0) {
606 dbg(1, "%s - command timed out.", __func__
);
610 remove_wait_queue(&dev
->write_wait
, &waita
);
611 retval
= mutex_lock_interruptible(&dev
->mtx
);
613 retval
= bytes_written
? bytes_written
: retval
;
617 dbg(4," %s : in progress, count = %Zd", __func__
, count
);
619 spin_unlock_irqrestore(&dev
->buflock
, flags
);
620 set_current_state(TASK_RUNNING
);
621 remove_wait_queue(&dev
->write_wait
, &waita
);
622 dbg(4," %s : sending, count = %Zd", __func__
, count
);
624 /* write the data into interrupt_out_buffer from userspace */
625 buffer_size
= le16_to_cpu(dev
->interrupt_out_endpoint
->wMaxPacketSize
);
626 bytes_to_write
= count
> buffer_size
? buffer_size
: count
;
627 dbg(4," %s : buffer_size = %Zd, count = %Zd, bytes_to_write = %Zd",
628 __func__
, buffer_size
, count
, bytes_to_write
);
630 if (copy_from_user(dev
->interrupt_out_buffer
, buffer
, bytes_to_write
) != 0) {
635 /* send off the urb */
637 dev
->interrupt_out_urb
,
639 usb_sndintpipe(dev
->udev
, dev
->interrupt_out_endpoint
->bEndpointAddress
),
640 dev
->interrupt_out_buffer
,
642 adu_interrupt_out_callback
,
644 dev
->interrupt_out_endpoint
->bInterval
);
645 dev
->interrupt_out_urb
->actual_length
= bytes_to_write
;
646 dev
->out_urb_finished
= 0;
647 retval
= usb_submit_urb(dev
->interrupt_out_urb
, GFP_KERNEL
);
649 dev
->out_urb_finished
= 1;
650 dev_err(&dev
->udev
->dev
, "Couldn't submit "
651 "interrupt_out_urb %d\n", retval
);
655 buffer
+= bytes_to_write
;
656 count
-= bytes_to_write
;
658 bytes_written
+= bytes_to_write
;
661 mutex_unlock(&dev
->mtx
);
662 return bytes_written
;
665 mutex_unlock(&dev
->mtx
);
667 dbg(2," %s : leave, return value %d", __func__
, retval
);
671 remove_wait_queue(&dev
->write_wait
, &waita
);
675 /* file operations needed when we register this driver */
676 static const struct file_operations adu_fops
= {
677 .owner
= THIS_MODULE
,
681 .release
= adu_release
,
682 .llseek
= noop_llseek
,
686 * usb class driver info in order to get a minor number from the usb core,
687 * and to have the device registered with devfs and the driver core
689 static struct usb_class_driver adu_class
= {
690 .name
= "usb/adutux%d",
692 .minor_base
= ADU_MINOR_BASE
,
698 * Called by the usb core when a new device is connected that it thinks
699 * this driver might be interested in.
701 static int adu_probe(struct usb_interface
*interface
,
702 const struct usb_device_id
*id
)
704 struct usb_device
*udev
= interface_to_usbdev(interface
);
705 struct adu_device
*dev
= NULL
;
706 struct usb_host_interface
*iface_desc
;
707 struct usb_endpoint_descriptor
*endpoint
;
708 int retval
= -ENODEV
;
713 dbg(2," %s : enter", __func__
);
716 dev_err(&interface
->dev
, "udev is NULL.\n");
720 /* allocate memory for our device state and initialize it */
721 dev
= kzalloc(sizeof(struct adu_device
), GFP_KERNEL
);
723 dev_err(&interface
->dev
, "Out of memory\n");
728 mutex_init(&dev
->mtx
);
729 spin_lock_init(&dev
->buflock
);
731 init_waitqueue_head(&dev
->read_wait
);
732 init_waitqueue_head(&dev
->write_wait
);
734 iface_desc
= &interface
->altsetting
[0];
736 /* set up the endpoint information */
737 for (i
= 0; i
< iface_desc
->desc
.bNumEndpoints
; ++i
) {
738 endpoint
= &iface_desc
->endpoint
[i
].desc
;
740 if (usb_endpoint_is_int_in(endpoint
))
741 dev
->interrupt_in_endpoint
= endpoint
;
743 if (usb_endpoint_is_int_out(endpoint
))
744 dev
->interrupt_out_endpoint
= endpoint
;
746 if (dev
->interrupt_in_endpoint
== NULL
) {
747 dev_err(&interface
->dev
, "interrupt in endpoint not found\n");
750 if (dev
->interrupt_out_endpoint
== NULL
) {
751 dev_err(&interface
->dev
, "interrupt out endpoint not found\n");
755 in_end_size
= le16_to_cpu(dev
->interrupt_in_endpoint
->wMaxPacketSize
);
756 out_end_size
= le16_to_cpu(dev
->interrupt_out_endpoint
->wMaxPacketSize
);
758 dev
->read_buffer_primary
= kmalloc((4 * in_end_size
), GFP_KERNEL
);
759 if (!dev
->read_buffer_primary
) {
760 dev_err(&interface
->dev
, "Couldn't allocate read_buffer_primary\n");
765 /* debug code prime the buffer */
766 memset(dev
->read_buffer_primary
, 'a', in_end_size
);
767 memset(dev
->read_buffer_primary
+ in_end_size
, 'b', in_end_size
);
768 memset(dev
->read_buffer_primary
+ (2 * in_end_size
), 'c', in_end_size
);
769 memset(dev
->read_buffer_primary
+ (3 * in_end_size
), 'd', in_end_size
);
771 dev
->read_buffer_secondary
= kmalloc((4 * in_end_size
), GFP_KERNEL
);
772 if (!dev
->read_buffer_secondary
) {
773 dev_err(&interface
->dev
, "Couldn't allocate read_buffer_secondary\n");
778 /* debug code prime the buffer */
779 memset(dev
->read_buffer_secondary
, 'e', in_end_size
);
780 memset(dev
->read_buffer_secondary
+ in_end_size
, 'f', in_end_size
);
781 memset(dev
->read_buffer_secondary
+ (2 * in_end_size
), 'g', in_end_size
);
782 memset(dev
->read_buffer_secondary
+ (3 * in_end_size
), 'h', in_end_size
);
784 dev
->interrupt_in_buffer
= kmalloc(in_end_size
, GFP_KERNEL
);
785 if (!dev
->interrupt_in_buffer
) {
786 dev_err(&interface
->dev
, "Couldn't allocate interrupt_in_buffer\n");
790 /* debug code prime the buffer */
791 memset(dev
->interrupt_in_buffer
, 'i', in_end_size
);
793 dev
->interrupt_in_urb
= usb_alloc_urb(0, GFP_KERNEL
);
794 if (!dev
->interrupt_in_urb
) {
795 dev_err(&interface
->dev
, "Couldn't allocate interrupt_in_urb\n");
798 dev
->interrupt_out_buffer
= kmalloc(out_end_size
, GFP_KERNEL
);
799 if (!dev
->interrupt_out_buffer
) {
800 dev_err(&interface
->dev
, "Couldn't allocate interrupt_out_buffer\n");
803 dev
->interrupt_out_urb
= usb_alloc_urb(0, GFP_KERNEL
);
804 if (!dev
->interrupt_out_urb
) {
805 dev_err(&interface
->dev
, "Couldn't allocate interrupt_out_urb\n");
809 if (!usb_string(udev
, udev
->descriptor
.iSerialNumber
, dev
->serial_number
,
810 sizeof(dev
->serial_number
))) {
811 dev_err(&interface
->dev
, "Could not retrieve serial number\n");
814 dbg(2," %s : serial_number=%s", __func__
, dev
->serial_number
);
816 /* we can register the device now, as it is ready */
817 usb_set_intfdata(interface
, dev
);
819 retval
= usb_register_dev(interface
, &adu_class
);
822 /* something prevented us from registering this driver */
823 dev_err(&interface
->dev
, "Not able to get a minor for this device.\n");
824 usb_set_intfdata(interface
, NULL
);
828 dev
->minor
= interface
->minor
;
830 /* let the user know what node this device is now attached to */
831 dev_info(&interface
->dev
, "ADU%d %s now attached to /dev/usb/adutux%d\n",
832 udev
->descriptor
.idProduct
, dev
->serial_number
,
833 (dev
->minor
- ADU_MINOR_BASE
));
835 dbg(2," %s : leave, return value %p (dev)", __func__
, dev
);
847 * Called by the usb core when the device is removed from the system.
849 static void adu_disconnect(struct usb_interface
*interface
)
851 struct adu_device
*dev
;
854 dbg(2," %s : enter", __func__
);
856 dev
= usb_get_intfdata(interface
);
858 mutex_lock(&dev
->mtx
); /* not interruptible */
859 dev
->udev
= NULL
; /* poison */
861 usb_deregister_dev(interface
, &adu_class
);
862 mutex_unlock(&dev
->mtx
);
864 mutex_lock(&adutux_mutex
);
865 usb_set_intfdata(interface
, NULL
);
867 /* if the device is not opened, then we clean up right now */
868 dbg(2," %s : open count %d", __func__
, dev
->open_count
);
869 if (!dev
->open_count
)
872 mutex_unlock(&adutux_mutex
);
874 dev_info(&interface
->dev
, "ADU device adutux%d now disconnected\n",
875 (minor
- ADU_MINOR_BASE
));
877 dbg(2," %s : leave", __func__
);
880 /* usb specific object needed to register this driver with the usb subsystem */
881 static struct usb_driver adu_driver
= {
884 .disconnect
= adu_disconnect
,
885 .id_table
= device_table
,
888 static int __init
adu_init(void)
892 dbg(2," %s : enter", __func__
);
894 /* register this driver with the USB subsystem */
895 result
= usb_register(&adu_driver
);
897 printk(KERN_ERR
"usb_register failed for the "__FILE__
898 " driver. Error number %d\n", result
);
902 printk(KERN_INFO
"adutux " DRIVER_DESC
" " DRIVER_VERSION
"\n");
903 printk(KERN_INFO
"adutux is an experimental driver. "
904 "Use at your own risk\n");
907 dbg(2," %s : leave, return value %d", __func__
, result
);
912 static void __exit
adu_exit(void)
914 dbg(2," %s : enter", __func__
);
915 /* deregister this driver with the USB subsystem */
916 usb_deregister(&adu_driver
);
917 dbg(2," %s : leave", __func__
);
920 module_init(adu_init
);
921 module_exit(adu_exit
);
923 MODULE_AUTHOR(DRIVER_AUTHOR
);
924 MODULE_DESCRIPTION(DRIVER_DESC
);
925 MODULE_LICENSE("GPL");