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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
23 #include <linux/kernel.h>
24 #include <linux/sched/signal.h>
25 #include <linux/errno.h>
26 #include <linux/slab.h>
27 #include <linux/module.h>
28 #include <linux/usb.h>
29 #include <linux/mutex.h>
30 #include <linux/uaccess.h>
32 #define DRIVER_AUTHOR "John Homppi"
33 #define DRIVER_DESC "adutux (see www.ontrak.net)"
35 /* Define these values to match your device */
36 #define ADU_VENDOR_ID 0x0a07
37 #define ADU_PRODUCT_ID 0x0064
39 /* table of devices that work with this driver */
40 static const struct usb_device_id device_table[] = {
41 { USB_DEVICE(ADU_VENDOR_ID, ADU_PRODUCT_ID) }, /* ADU100 */
42 { USB_DEVICE(ADU_VENDOR_ID, ADU_PRODUCT_ID+20) }, /* ADU120 */
43 { USB_DEVICE(ADU_VENDOR_ID, ADU_PRODUCT_ID+30) }, /* ADU130 */
44 { USB_DEVICE(ADU_VENDOR_ID, ADU_PRODUCT_ID+100) }, /* ADU200 */
45 { USB_DEVICE(ADU_VENDOR_ID, ADU_PRODUCT_ID+108) }, /* ADU208 */
46 { USB_DEVICE(ADU_VENDOR_ID, ADU_PRODUCT_ID+118) }, /* ADU218 */
47 { } /* Terminating entry */
50 MODULE_DEVICE_TABLE(usb, device_table);
52 #ifdef CONFIG_USB_DYNAMIC_MINORS
53 #define ADU_MINOR_BASE 0
55 #define ADU_MINOR_BASE 67
58 /* we can have up to this number of device plugged in at once */
59 #define MAX_DEVICES 16
61 #define COMMAND_TIMEOUT (2*HZ) /* 60 second timeout for a command */
64 * The locking scheme is a vanilla 3-lock:
65 * adu_device.buflock: A spinlock, covers what IRQs touch.
66 * adutux_mutex: A Static lock to cover open_count. It would also cover
67 * any globals, but we don't have them in 2.6.
68 * adu_device.mtx: A mutex to hold across sleepers like copy_from_user.
69 * It covers all of adu_device, except the open_count
70 * and what .buflock covers.
73 /* Structure to hold all of our device specific stuff */
76 struct usb_device *udev; /* save off the usb device pointer */
77 struct usb_interface *interface;
78 unsigned int minor; /* the starting minor number for this device */
79 char serial_number[8];
81 int open_count; /* number of times this port has been opened */
82 unsigned long disconnected:1;
84 char *read_buffer_primary;
85 int read_buffer_length;
86 char *read_buffer_secondary;
91 wait_queue_head_t read_wait;
92 wait_queue_head_t write_wait;
94 char *interrupt_in_buffer;
95 struct usb_endpoint_descriptor *interrupt_in_endpoint;
96 struct urb *interrupt_in_urb;
97 int read_urb_finished;
99 char *interrupt_out_buffer;
100 struct usb_endpoint_descriptor *interrupt_out_endpoint;
101 struct urb *interrupt_out_urb;
102 int out_urb_finished;
105 static DEFINE_MUTEX(adutux_mutex);
107 static struct usb_driver adu_driver;
109 static inline void adu_debug_data(struct device *dev, const char *function,
110 int size, const unsigned char *data)
112 dev_dbg(dev, "%s - length = %d, data = %*ph\n",
113 function, size, size, data);
117 * adu_abort_transfers
118 * aborts transfers and frees associated data structures
120 static void adu_abort_transfers(struct adu_device *dev)
124 if (dev->disconnected)
127 /* shutdown transfer */
129 /* XXX Anchor these instead */
130 spin_lock_irqsave(&dev->buflock, flags);
131 if (!dev->read_urb_finished) {
132 spin_unlock_irqrestore(&dev->buflock, flags);
133 usb_kill_urb(dev->interrupt_in_urb);
135 spin_unlock_irqrestore(&dev->buflock, flags);
137 spin_lock_irqsave(&dev->buflock, flags);
138 if (!dev->out_urb_finished) {
139 spin_unlock_irqrestore(&dev->buflock, flags);
140 usb_kill_urb(dev->interrupt_out_urb);
142 spin_unlock_irqrestore(&dev->buflock, flags);
145 static void adu_delete(struct adu_device *dev)
147 /* free data structures */
148 usb_free_urb(dev->interrupt_in_urb);
149 usb_free_urb(dev->interrupt_out_urb);
150 kfree(dev->read_buffer_primary);
151 kfree(dev->read_buffer_secondary);
152 kfree(dev->interrupt_in_buffer);
153 kfree(dev->interrupt_out_buffer);
154 usb_put_dev(dev->udev);
158 static void adu_interrupt_in_callback(struct urb *urb)
160 struct adu_device *dev = urb->context;
161 int status = urb->status;
163 adu_debug_data(&dev->udev->dev, __func__,
164 urb->actual_length, urb->transfer_buffer);
166 spin_lock(&dev->buflock);
169 if ((status != -ENOENT) && (status != -ECONNRESET) &&
170 (status != -ESHUTDOWN)) {
171 dev_dbg(&dev->udev->dev,
172 "%s : nonzero status received: %d\n",
178 if (urb->actual_length > 0 && dev->interrupt_in_buffer[0] != 0x00) {
179 if (dev->read_buffer_length <
180 (4 * usb_endpoint_maxp(dev->interrupt_in_endpoint)) -
181 (urb->actual_length)) {
182 memcpy (dev->read_buffer_primary +
183 dev->read_buffer_length,
184 dev->interrupt_in_buffer, urb->actual_length);
186 dev->read_buffer_length += urb->actual_length;
187 dev_dbg(&dev->udev->dev,"%s reading %d\n", __func__,
190 dev_dbg(&dev->udev->dev,"%s : read_buffer overflow\n",
196 dev->read_urb_finished = 1;
197 spin_unlock(&dev->buflock);
198 /* always wake up so we recover from errors */
199 wake_up_interruptible(&dev->read_wait);
202 static void adu_interrupt_out_callback(struct urb *urb)
204 struct adu_device *dev = urb->context;
205 int status = urb->status;
207 adu_debug_data(&dev->udev->dev, __func__,
208 urb->actual_length, urb->transfer_buffer);
211 if ((status != -ENOENT) &&
212 (status != -ESHUTDOWN) &&
213 (status != -ECONNRESET)) {
214 dev_dbg(&dev->udev->dev,
215 "%s :nonzero status received: %d\n", __func__,
221 spin_lock(&dev->buflock);
222 dev->out_urb_finished = 1;
223 wake_up(&dev->write_wait);
224 spin_unlock(&dev->buflock);
227 static int adu_open(struct inode *inode, struct file *file)
229 struct adu_device *dev = NULL;
230 struct usb_interface *interface;
234 subminor = iminor(inode);
236 retval = mutex_lock_interruptible(&adutux_mutex);
240 interface = usb_find_interface(&adu_driver, subminor);
242 pr_err("%s - error, can't find device for minor %d\n",
248 dev = usb_get_intfdata(interface);
254 /* check that nobody else is using the device */
255 if (dev->open_count) {
261 dev_dbg(&dev->udev->dev, "%s: open count %d\n", __func__,
264 /* save device in the file's private structure */
265 file->private_data = dev;
267 /* initialize in direction */
268 dev->read_buffer_length = 0;
270 /* fixup first read by having urb waiting for it */
271 usb_fill_int_urb(dev->interrupt_in_urb, dev->udev,
272 usb_rcvintpipe(dev->udev,
273 dev->interrupt_in_endpoint->bEndpointAddress),
274 dev->interrupt_in_buffer,
275 usb_endpoint_maxp(dev->interrupt_in_endpoint),
276 adu_interrupt_in_callback, dev,
277 dev->interrupt_in_endpoint->bInterval);
278 dev->read_urb_finished = 0;
279 if (usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL))
280 dev->read_urb_finished = 1;
281 /* we ignore failure */
282 /* end of fixup for first read */
284 /* initialize out direction */
285 dev->out_urb_finished = 1;
290 mutex_unlock(&adutux_mutex);
295 static void adu_release_internal(struct adu_device *dev)
297 /* decrement our usage count for the device */
299 dev_dbg(&dev->udev->dev, "%s : open count %d\n", __func__,
301 if (dev->open_count <= 0) {
302 adu_abort_transfers(dev);
307 static int adu_release(struct inode *inode, struct file *file)
309 struct adu_device *dev;
317 dev = file->private_data;
323 mutex_lock(&adutux_mutex); /* not interruptible */
325 if (dev->open_count <= 0) {
326 dev_dbg(&dev->udev->dev, "%s : device not opened\n", __func__);
331 adu_release_internal(dev);
332 if (dev->disconnected) {
333 /* the device was unplugged before the file was released */
334 if (!dev->open_count) /* ... and we're the last user */
338 mutex_unlock(&adutux_mutex);
343 static ssize_t adu_read(struct file *file, __user char *buffer, size_t count,
346 struct adu_device *dev;
347 size_t bytes_read = 0;
348 size_t bytes_to_read = count;
352 int should_submit = 0;
354 DECLARE_WAITQUEUE(wait, current);
356 dev = file->private_data;
357 if (mutex_lock_interruptible(&dev->mtx))
360 /* verify that the device wasn't unplugged */
361 if (dev->disconnected) {
363 pr_err("No device or device unplugged %d\n", retval);
367 /* verify that some data was requested */
369 dev_dbg(&dev->udev->dev, "%s : read request of 0 bytes\n",
374 timeout = COMMAND_TIMEOUT;
375 dev_dbg(&dev->udev->dev, "%s : about to start looping\n", __func__);
376 while (bytes_to_read) {
377 int data_in_secondary = dev->secondary_tail - dev->secondary_head;
378 dev_dbg(&dev->udev->dev,
379 "%s : while, data_in_secondary=%d, status=%d\n",
380 __func__, data_in_secondary,
381 dev->interrupt_in_urb->status);
383 if (data_in_secondary) {
384 /* drain secondary buffer */
385 int amount = bytes_to_read < data_in_secondary ? bytes_to_read : data_in_secondary;
386 i = copy_to_user(buffer, dev->read_buffer_secondary+dev->secondary_head, amount);
391 dev->secondary_head += (amount - i);
392 bytes_read += (amount - i);
393 bytes_to_read -= (amount - i);
395 /* we check the primary buffer */
396 spin_lock_irqsave (&dev->buflock, flags);
397 if (dev->read_buffer_length) {
398 /* we secure access to the primary */
400 dev_dbg(&dev->udev->dev,
401 "%s : swap, read_buffer_length = %d\n",
402 __func__, dev->read_buffer_length);
403 tmp = dev->read_buffer_secondary;
404 dev->read_buffer_secondary = dev->read_buffer_primary;
405 dev->read_buffer_primary = tmp;
406 dev->secondary_head = 0;
407 dev->secondary_tail = dev->read_buffer_length;
408 dev->read_buffer_length = 0;
409 spin_unlock_irqrestore(&dev->buflock, flags);
410 /* we have a free buffer so use it */
413 /* even the primary was empty - we may need to do IO */
414 if (!dev->read_urb_finished) {
415 /* somebody is doing IO */
416 spin_unlock_irqrestore(&dev->buflock, flags);
417 dev_dbg(&dev->udev->dev,
418 "%s : submitted already\n",
421 /* we must initiate input */
422 dev_dbg(&dev->udev->dev,
423 "%s : initiate input\n",
425 dev->read_urb_finished = 0;
426 spin_unlock_irqrestore(&dev->buflock, flags);
428 usb_fill_int_urb(dev->interrupt_in_urb, dev->udev,
429 usb_rcvintpipe(dev->udev,
430 dev->interrupt_in_endpoint->bEndpointAddress),
431 dev->interrupt_in_buffer,
432 usb_endpoint_maxp(dev->interrupt_in_endpoint),
433 adu_interrupt_in_callback,
435 dev->interrupt_in_endpoint->bInterval);
436 retval = usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL);
438 dev->read_urb_finished = 1;
439 if (retval == -ENOMEM) {
440 retval = bytes_read ? bytes_read : -ENOMEM;
442 dev_dbg(&dev->udev->dev,
443 "%s : submit failed\n",
449 /* we wait for I/O to complete */
450 set_current_state(TASK_INTERRUPTIBLE);
451 add_wait_queue(&dev->read_wait, &wait);
452 spin_lock_irqsave(&dev->buflock, flags);
453 if (!dev->read_urb_finished) {
454 spin_unlock_irqrestore(&dev->buflock, flags);
455 timeout = schedule_timeout(COMMAND_TIMEOUT);
457 spin_unlock_irqrestore(&dev->buflock, flags);
458 set_current_state(TASK_RUNNING);
460 remove_wait_queue(&dev->read_wait, &wait);
463 dev_dbg(&dev->udev->dev,
464 "%s : timeout\n", __func__);
465 retval = bytes_read ? bytes_read : -ETIMEDOUT;
469 if (signal_pending(current)) {
470 dev_dbg(&dev->udev->dev,
471 "%s : signal pending\n",
473 retval = bytes_read ? bytes_read : -EINTR;
481 /* if the primary buffer is empty then use it */
482 spin_lock_irqsave(&dev->buflock, flags);
483 if (should_submit && dev->read_urb_finished) {
484 dev->read_urb_finished = 0;
485 spin_unlock_irqrestore(&dev->buflock, flags);
486 usb_fill_int_urb(dev->interrupt_in_urb, dev->udev,
487 usb_rcvintpipe(dev->udev,
488 dev->interrupt_in_endpoint->bEndpointAddress),
489 dev->interrupt_in_buffer,
490 usb_endpoint_maxp(dev->interrupt_in_endpoint),
491 adu_interrupt_in_callback,
493 dev->interrupt_in_endpoint->bInterval);
494 if (usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL) != 0)
495 dev->read_urb_finished = 1;
496 /* we ignore failure */
498 spin_unlock_irqrestore(&dev->buflock, flags);
502 /* unlock the device */
503 mutex_unlock(&dev->mtx);
508 static ssize_t adu_write(struct file *file, const __user char *buffer,
509 size_t count, loff_t *ppos)
511 DECLARE_WAITQUEUE(waita, current);
512 struct adu_device *dev;
513 size_t bytes_written = 0;
514 size_t bytes_to_write;
519 dev = file->private_data;
521 retval = mutex_lock_interruptible(&dev->mtx);
525 /* verify that the device wasn't unplugged */
526 if (dev->disconnected) {
528 pr_err("No device or device unplugged %d\n", retval);
532 /* verify that we actually have some data to write */
534 dev_dbg(&dev->udev->dev, "%s : write request of 0 bytes\n",
540 add_wait_queue(&dev->write_wait, &waita);
541 set_current_state(TASK_INTERRUPTIBLE);
542 spin_lock_irqsave(&dev->buflock, flags);
543 if (!dev->out_urb_finished) {
544 spin_unlock_irqrestore(&dev->buflock, flags);
546 mutex_unlock(&dev->mtx);
547 if (signal_pending(current)) {
548 dev_dbg(&dev->udev->dev, "%s : interrupted\n",
550 set_current_state(TASK_RUNNING);
554 if (schedule_timeout(COMMAND_TIMEOUT) == 0) {
555 dev_dbg(&dev->udev->dev,
556 "%s - command timed out.\n", __func__);
560 remove_wait_queue(&dev->write_wait, &waita);
561 retval = mutex_lock_interruptible(&dev->mtx);
563 retval = bytes_written ? bytes_written : retval;
567 dev_dbg(&dev->udev->dev,
568 "%s : in progress, count = %zd\n",
571 spin_unlock_irqrestore(&dev->buflock, flags);
572 set_current_state(TASK_RUNNING);
573 remove_wait_queue(&dev->write_wait, &waita);
574 dev_dbg(&dev->udev->dev, "%s : sending, count = %zd\n",
577 /* write the data into interrupt_out_buffer from userspace */
578 buffer_size = usb_endpoint_maxp(dev->interrupt_out_endpoint);
579 bytes_to_write = count > buffer_size ? buffer_size : count;
580 dev_dbg(&dev->udev->dev,
581 "%s : buffer_size = %zd, count = %zd, bytes_to_write = %zd\n",
582 __func__, buffer_size, count, bytes_to_write);
584 if (copy_from_user(dev->interrupt_out_buffer, buffer, bytes_to_write) != 0) {
589 /* send off the urb */
591 dev->interrupt_out_urb,
593 usb_sndintpipe(dev->udev, dev->interrupt_out_endpoint->bEndpointAddress),
594 dev->interrupt_out_buffer,
596 adu_interrupt_out_callback,
598 dev->interrupt_out_endpoint->bInterval);
599 dev->interrupt_out_urb->actual_length = bytes_to_write;
600 dev->out_urb_finished = 0;
601 retval = usb_submit_urb(dev->interrupt_out_urb, GFP_KERNEL);
603 dev->out_urb_finished = 1;
604 dev_err(&dev->udev->dev, "Couldn't submit "
605 "interrupt_out_urb %d\n", retval);
609 buffer += bytes_to_write;
610 count -= bytes_to_write;
612 bytes_written += bytes_to_write;
615 mutex_unlock(&dev->mtx);
616 return bytes_written;
619 mutex_unlock(&dev->mtx);
624 remove_wait_queue(&dev->write_wait, &waita);
628 /* file operations needed when we register this driver */
629 static const struct file_operations adu_fops = {
630 .owner = THIS_MODULE,
634 .release = adu_release,
635 .llseek = noop_llseek,
639 * usb class driver info in order to get a minor number from the usb core,
640 * and to have the device registered with devfs and the driver core
642 static struct usb_class_driver adu_class = {
643 .name = "usb/adutux%d",
645 .minor_base = ADU_MINOR_BASE,
651 * Called by the usb core when a new device is connected that it thinks
652 * this driver might be interested in.
654 static int adu_probe(struct usb_interface *interface,
655 const struct usb_device_id *id)
657 struct usb_device *udev = interface_to_usbdev(interface);
658 struct adu_device *dev = NULL;
659 int retval = -ENOMEM;
664 /* allocate memory for our device state and initialize it */
665 dev = kzalloc(sizeof(struct adu_device), GFP_KERNEL);
669 mutex_init(&dev->mtx);
670 spin_lock_init(&dev->buflock);
671 dev->udev = usb_get_dev(udev);
672 init_waitqueue_head(&dev->read_wait);
673 init_waitqueue_head(&dev->write_wait);
675 res = usb_find_common_endpoints_reverse(interface->cur_altsetting,
677 &dev->interrupt_in_endpoint,
678 &dev->interrupt_out_endpoint);
680 dev_err(&interface->dev, "interrupt endpoints not found\n");
685 in_end_size = usb_endpoint_maxp(dev->interrupt_in_endpoint);
686 out_end_size = usb_endpoint_maxp(dev->interrupt_out_endpoint);
688 dev->read_buffer_primary = kmalloc((4 * in_end_size), GFP_KERNEL);
689 if (!dev->read_buffer_primary)
692 /* debug code prime the buffer */
693 memset(dev->read_buffer_primary, 'a', in_end_size);
694 memset(dev->read_buffer_primary + in_end_size, 'b', in_end_size);
695 memset(dev->read_buffer_primary + (2 * in_end_size), 'c', in_end_size);
696 memset(dev->read_buffer_primary + (3 * in_end_size), 'd', in_end_size);
698 dev->read_buffer_secondary = kmalloc((4 * in_end_size), GFP_KERNEL);
699 if (!dev->read_buffer_secondary)
702 /* debug code prime the buffer */
703 memset(dev->read_buffer_secondary, 'e', in_end_size);
704 memset(dev->read_buffer_secondary + in_end_size, 'f', in_end_size);
705 memset(dev->read_buffer_secondary + (2 * in_end_size), 'g', in_end_size);
706 memset(dev->read_buffer_secondary + (3 * in_end_size), 'h', in_end_size);
708 dev->interrupt_in_buffer = kmalloc(in_end_size, GFP_KERNEL);
709 if (!dev->interrupt_in_buffer)
712 /* debug code prime the buffer */
713 memset(dev->interrupt_in_buffer, 'i', in_end_size);
715 dev->interrupt_in_urb = usb_alloc_urb(0, GFP_KERNEL);
716 if (!dev->interrupt_in_urb)
718 dev->interrupt_out_buffer = kmalloc(out_end_size, GFP_KERNEL);
719 if (!dev->interrupt_out_buffer)
721 dev->interrupt_out_urb = usb_alloc_urb(0, GFP_KERNEL);
722 if (!dev->interrupt_out_urb)
725 if (!usb_string(udev, udev->descriptor.iSerialNumber, dev->serial_number,
726 sizeof(dev->serial_number))) {
727 dev_err(&interface->dev, "Could not retrieve serial number\n");
731 dev_dbg(&interface->dev,"serial_number=%s", dev->serial_number);
733 /* we can register the device now, as it is ready */
734 usb_set_intfdata(interface, dev);
736 retval = usb_register_dev(interface, &adu_class);
739 /* something prevented us from registering this driver */
740 dev_err(&interface->dev, "Not able to get a minor for this device.\n");
741 usb_set_intfdata(interface, NULL);
745 dev->minor = interface->minor;
747 /* let the user know what node this device is now attached to */
748 dev_info(&interface->dev, "ADU%d %s now attached to /dev/usb/adutux%d\n",
749 le16_to_cpu(udev->descriptor.idProduct), dev->serial_number,
750 (dev->minor - ADU_MINOR_BASE));
762 * Called by the usb core when the device is removed from the system.
764 static void adu_disconnect(struct usb_interface *interface)
766 struct adu_device *dev;
768 dev = usb_get_intfdata(interface);
770 usb_deregister_dev(interface, &adu_class);
772 usb_poison_urb(dev->interrupt_in_urb);
773 usb_poison_urb(dev->interrupt_out_urb);
775 mutex_lock(&adutux_mutex);
776 usb_set_intfdata(interface, NULL);
778 mutex_lock(&dev->mtx); /* not interruptible */
779 dev->disconnected = 1;
780 mutex_unlock(&dev->mtx);
782 /* if the device is not opened, then we clean up right now */
783 if (!dev->open_count)
786 mutex_unlock(&adutux_mutex);
789 /* usb specific object needed to register this driver with the usb subsystem */
790 static struct usb_driver adu_driver = {
793 .disconnect = adu_disconnect,
794 .id_table = device_table,
797 module_usb_driver(adu_driver);
799 MODULE_AUTHOR(DRIVER_AUTHOR);
800 MODULE_DESCRIPTION(DRIVER_DESC);
801 MODULE_LICENSE("GPL");