1 // SPDX-License-Identifier: GPL-2.0+
3 * adutux - driver for ADU devices from Ontrak Control Systems
4 * This is an experimental driver. Use at your own risk.
5 * This driver is not supported by Ontrak Control Systems.
7 * Copyright (c) 2003 John Homppi (SCO, leave this notice here)
9 * derived from the Lego USB Tower driver 0.56:
12 * that was derived from USB Skeleton driver - 0.5
17 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19 #include <linux/kernel.h>
20 #include <linux/sched/signal.h>
21 #include <linux/errno.h>
22 #include <linux/slab.h>
23 #include <linux/module.h>
24 #include <linux/usb.h>
25 #include <linux/mutex.h>
26 #include <linux/uaccess.h>
28 #define DRIVER_AUTHOR "John Homppi"
29 #define DRIVER_DESC "adutux (see www.ontrak.net)"
31 /* Define these values to match your device */
32 #define ADU_VENDOR_ID 0x0a07
33 #define ADU_PRODUCT_ID 0x0064
35 /* table of devices that work with this driver */
36 static const struct usb_device_id device_table[] = {
37 { USB_DEVICE(ADU_VENDOR_ID, ADU_PRODUCT_ID) }, /* ADU100 */
38 { USB_DEVICE(ADU_VENDOR_ID, ADU_PRODUCT_ID+20) }, /* ADU120 */
39 { USB_DEVICE(ADU_VENDOR_ID, ADU_PRODUCT_ID+30) }, /* ADU130 */
40 { USB_DEVICE(ADU_VENDOR_ID, ADU_PRODUCT_ID+100) }, /* ADU200 */
41 { USB_DEVICE(ADU_VENDOR_ID, ADU_PRODUCT_ID+108) }, /* ADU208 */
42 { USB_DEVICE(ADU_VENDOR_ID, ADU_PRODUCT_ID+118) }, /* ADU218 */
43 { } /* Terminating entry */
46 MODULE_DEVICE_TABLE(usb, device_table);
48 #ifdef CONFIG_USB_DYNAMIC_MINORS
49 #define ADU_MINOR_BASE 0
51 #define ADU_MINOR_BASE 67
54 /* we can have up to this number of device plugged in at once */
55 #define MAX_DEVICES 16
57 #define COMMAND_TIMEOUT (2*HZ)
60 * The locking scheme is a vanilla 3-lock:
61 * adu_device.buflock: A spinlock, covers what IRQs touch.
62 * adutux_mutex: A Static lock to cover open_count. It would also cover
63 * any globals, but we don't have them in 2.6.
64 * adu_device.mtx: A mutex to hold across sleepers like copy_from_user.
65 * It covers all of adu_device, except the open_count
66 * and what .buflock covers.
69 /* Structure to hold all of our device specific stuff */
72 struct usb_device *udev; /* save off the usb device pointer */
73 struct usb_interface *interface;
74 unsigned int minor; /* the starting minor number for this device */
75 char serial_number[8];
77 int open_count; /* number of times this port has been opened */
78 unsigned long disconnected:1;
80 char *read_buffer_primary;
81 int read_buffer_length;
82 char *read_buffer_secondary;
87 wait_queue_head_t read_wait;
88 wait_queue_head_t write_wait;
90 char *interrupt_in_buffer;
91 struct usb_endpoint_descriptor *interrupt_in_endpoint;
92 struct urb *interrupt_in_urb;
93 int read_urb_finished;
95 char *interrupt_out_buffer;
96 struct usb_endpoint_descriptor *interrupt_out_endpoint;
97 struct urb *interrupt_out_urb;
101 static DEFINE_MUTEX(adutux_mutex);
103 static struct usb_driver adu_driver;
105 static inline void adu_debug_data(struct device *dev, const char *function,
106 int size, const unsigned char *data)
108 dev_dbg(dev, "%s - length = %d, data = %*ph\n",
109 function, size, size, data);
113 * adu_abort_transfers
114 * aborts transfers and frees associated data structures
116 static void adu_abort_transfers(struct adu_device *dev)
120 if (dev->disconnected)
123 /* shutdown transfer */
125 /* XXX Anchor these instead */
126 spin_lock_irqsave(&dev->buflock, flags);
127 if (!dev->read_urb_finished) {
128 spin_unlock_irqrestore(&dev->buflock, flags);
129 usb_kill_urb(dev->interrupt_in_urb);
131 spin_unlock_irqrestore(&dev->buflock, flags);
133 spin_lock_irqsave(&dev->buflock, flags);
134 if (!dev->out_urb_finished) {
135 spin_unlock_irqrestore(&dev->buflock, flags);
136 wait_event_timeout(dev->write_wait, dev->out_urb_finished,
138 usb_kill_urb(dev->interrupt_out_urb);
140 spin_unlock_irqrestore(&dev->buflock, flags);
143 static void adu_delete(struct adu_device *dev)
145 /* free data structures */
146 usb_free_urb(dev->interrupt_in_urb);
147 usb_free_urb(dev->interrupt_out_urb);
148 kfree(dev->read_buffer_primary);
149 kfree(dev->read_buffer_secondary);
150 kfree(dev->interrupt_in_buffer);
151 kfree(dev->interrupt_out_buffer);
152 usb_put_dev(dev->udev);
156 static void adu_interrupt_in_callback(struct urb *urb)
158 struct adu_device *dev = urb->context;
159 int status = urb->status;
162 adu_debug_data(&dev->udev->dev, __func__,
163 urb->actual_length, urb->transfer_buffer);
165 spin_lock_irqsave(&dev->buflock, flags);
168 if ((status != -ENOENT) && (status != -ECONNRESET) &&
169 (status != -ESHUTDOWN)) {
170 dev_dbg(&dev->udev->dev,
171 "%s : nonzero status received: %d\n",
177 if (urb->actual_length > 0 && dev->interrupt_in_buffer[0] != 0x00) {
178 if (dev->read_buffer_length <
179 (4 * usb_endpoint_maxp(dev->interrupt_in_endpoint)) -
180 (urb->actual_length)) {
181 memcpy (dev->read_buffer_primary +
182 dev->read_buffer_length,
183 dev->interrupt_in_buffer, urb->actual_length);
185 dev->read_buffer_length += urb->actual_length;
186 dev_dbg(&dev->udev->dev,"%s reading %d\n", __func__,
189 dev_dbg(&dev->udev->dev,"%s : read_buffer overflow\n",
195 dev->read_urb_finished = 1;
196 spin_unlock_irqrestore(&dev->buflock, flags);
197 /* always wake up so we recover from errors */
198 wake_up_interruptible(&dev->read_wait);
201 static void adu_interrupt_out_callback(struct urb *urb)
203 struct adu_device *dev = urb->context;
204 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 != -ECONNRESET)) {
213 dev_dbg(&dev->udev->dev,
214 "%s :nonzero status received: %d\n", __func__,
220 spin_lock_irqsave(&dev->buflock, flags);
221 dev->out_urb_finished = 1;
222 wake_up(&dev->write_wait);
223 spin_unlock_irqrestore(&dev->buflock, flags);
226 static int adu_open(struct inode *inode, struct file *file)
228 struct adu_device *dev = NULL;
229 struct usb_interface *interface;
233 subminor = iminor(inode);
235 retval = mutex_lock_interruptible(&adutux_mutex);
239 interface = usb_find_interface(&adu_driver, subminor);
241 pr_err("%s - error, can't find device for minor %d\n",
247 dev = usb_get_intfdata(interface);
253 /* check that nobody else is using the device */
254 if (dev->open_count) {
260 dev_dbg(&dev->udev->dev, "%s: open count %d\n", __func__,
263 /* save device in the file's private structure */
264 file->private_data = dev;
266 /* initialize in direction */
267 dev->read_buffer_length = 0;
269 /* fixup first read by having urb waiting for it */
270 usb_fill_int_urb(dev->interrupt_in_urb, dev->udev,
271 usb_rcvintpipe(dev->udev,
272 dev->interrupt_in_endpoint->bEndpointAddress),
273 dev->interrupt_in_buffer,
274 usb_endpoint_maxp(dev->interrupt_in_endpoint),
275 adu_interrupt_in_callback, dev,
276 dev->interrupt_in_endpoint->bInterval);
277 dev->read_urb_finished = 0;
278 if (usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL))
279 dev->read_urb_finished = 1;
280 /* we ignore failure */
281 /* end of fixup for first read */
283 /* initialize out direction */
284 dev->out_urb_finished = 1;
289 mutex_unlock(&adutux_mutex);
294 static void adu_release_internal(struct adu_device *dev)
296 /* decrement our usage count for the device */
298 dev_dbg(&dev->udev->dev, "%s : open count %d\n", __func__,
300 if (dev->open_count <= 0) {
301 adu_abort_transfers(dev);
306 static int adu_release(struct inode *inode, struct file *file)
308 struct adu_device *dev;
316 dev = file->private_data;
322 mutex_lock(&adutux_mutex); /* not interruptible */
324 if (dev->open_count <= 0) {
325 dev_dbg(&dev->udev->dev, "%s : device not opened\n", __func__);
330 adu_release_internal(dev);
331 if (dev->disconnected) {
332 /* the device was unplugged before the file was released */
333 if (!dev->open_count) /* ... and we're the last user */
337 mutex_unlock(&adutux_mutex);
342 static ssize_t adu_read(struct file *file, __user char *buffer, size_t count,
345 struct adu_device *dev;
346 size_t bytes_read = 0;
347 size_t bytes_to_read = count;
350 int should_submit = 0;
352 DECLARE_WAITQUEUE(wait, current);
354 dev = file->private_data;
355 if (mutex_lock_interruptible(&dev->mtx))
358 /* verify that the device wasn't unplugged */
359 if (dev->disconnected) {
361 pr_err("No device or device unplugged %d\n", retval);
365 /* verify that some data was requested */
367 dev_dbg(&dev->udev->dev, "%s : read request of 0 bytes\n",
372 timeout = COMMAND_TIMEOUT;
373 dev_dbg(&dev->udev->dev, "%s : about to start looping\n", __func__);
374 while (bytes_to_read) {
375 size_t data_in_secondary = dev->secondary_tail - dev->secondary_head;
376 dev_dbg(&dev->udev->dev,
377 "%s : while, data_in_secondary=%zu, status=%d\n",
378 __func__, data_in_secondary,
379 dev->interrupt_in_urb->status);
381 if (data_in_secondary) {
382 /* drain secondary buffer */
383 size_t amount = min(bytes_to_read, data_in_secondary);
384 if (copy_to_user(buffer, dev->read_buffer_secondary+dev->secondary_head, amount)) {
388 dev->secondary_head += amount;
389 bytes_read += amount;
390 bytes_to_read -= amount;
392 /* we check the primary buffer */
393 spin_lock_irqsave (&dev->buflock, flags);
394 if (dev->read_buffer_length) {
395 /* we secure access to the primary */
397 dev_dbg(&dev->udev->dev,
398 "%s : swap, read_buffer_length = %d\n",
399 __func__, dev->read_buffer_length);
400 tmp = dev->read_buffer_secondary;
401 dev->read_buffer_secondary = dev->read_buffer_primary;
402 dev->read_buffer_primary = tmp;
403 dev->secondary_head = 0;
404 dev->secondary_tail = dev->read_buffer_length;
405 dev->read_buffer_length = 0;
406 spin_unlock_irqrestore(&dev->buflock, flags);
407 /* we have a free buffer so use it */
410 /* even the primary was empty - we may need to do IO */
411 if (!dev->read_urb_finished) {
412 /* somebody is doing IO */
413 spin_unlock_irqrestore(&dev->buflock, flags);
414 dev_dbg(&dev->udev->dev,
415 "%s : submitted already\n",
418 /* we must initiate input */
419 dev_dbg(&dev->udev->dev,
420 "%s : initiate input\n",
422 dev->read_urb_finished = 0;
423 spin_unlock_irqrestore(&dev->buflock, flags);
425 usb_fill_int_urb(dev->interrupt_in_urb, dev->udev,
426 usb_rcvintpipe(dev->udev,
427 dev->interrupt_in_endpoint->bEndpointAddress),
428 dev->interrupt_in_buffer,
429 usb_endpoint_maxp(dev->interrupt_in_endpoint),
430 adu_interrupt_in_callback,
432 dev->interrupt_in_endpoint->bInterval);
433 retval = usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL);
435 dev->read_urb_finished = 1;
436 if (retval == -ENOMEM) {
437 retval = bytes_read ? bytes_read : -ENOMEM;
439 dev_dbg(&dev->udev->dev,
440 "%s : submit failed\n",
446 /* we wait for I/O to complete */
447 set_current_state(TASK_INTERRUPTIBLE);
448 add_wait_queue(&dev->read_wait, &wait);
449 spin_lock_irqsave(&dev->buflock, flags);
450 if (!dev->read_urb_finished) {
451 spin_unlock_irqrestore(&dev->buflock, flags);
452 timeout = schedule_timeout(COMMAND_TIMEOUT);
454 spin_unlock_irqrestore(&dev->buflock, flags);
455 set_current_state(TASK_RUNNING);
457 remove_wait_queue(&dev->read_wait, &wait);
460 dev_dbg(&dev->udev->dev,
461 "%s : timeout\n", __func__);
462 retval = bytes_read ? bytes_read : -ETIMEDOUT;
466 if (signal_pending(current)) {
467 dev_dbg(&dev->udev->dev,
468 "%s : signal pending\n",
470 retval = bytes_read ? bytes_read : -EINTR;
478 /* if the primary buffer is empty then use it */
479 spin_lock_irqsave(&dev->buflock, flags);
480 if (should_submit && dev->read_urb_finished) {
481 dev->read_urb_finished = 0;
482 spin_unlock_irqrestore(&dev->buflock, flags);
483 usb_fill_int_urb(dev->interrupt_in_urb, dev->udev,
484 usb_rcvintpipe(dev->udev,
485 dev->interrupt_in_endpoint->bEndpointAddress),
486 dev->interrupt_in_buffer,
487 usb_endpoint_maxp(dev->interrupt_in_endpoint),
488 adu_interrupt_in_callback,
490 dev->interrupt_in_endpoint->bInterval);
491 if (usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL) != 0)
492 dev->read_urb_finished = 1;
493 /* we ignore failure */
495 spin_unlock_irqrestore(&dev->buflock, flags);
499 /* unlock the device */
500 mutex_unlock(&dev->mtx);
505 static ssize_t adu_write(struct file *file, const __user char *buffer,
506 size_t count, loff_t *ppos)
508 DECLARE_WAITQUEUE(waita, current);
509 struct adu_device *dev;
510 size_t bytes_written = 0;
511 size_t bytes_to_write;
516 dev = file->private_data;
518 retval = mutex_lock_interruptible(&dev->mtx);
522 /* verify that the device wasn't unplugged */
523 if (dev->disconnected) {
525 pr_err("No device or device unplugged %d\n", retval);
529 /* verify that we actually have some data to write */
531 dev_dbg(&dev->udev->dev, "%s : write request of 0 bytes\n",
537 add_wait_queue(&dev->write_wait, &waita);
538 set_current_state(TASK_INTERRUPTIBLE);
539 spin_lock_irqsave(&dev->buflock, flags);
540 if (!dev->out_urb_finished) {
541 spin_unlock_irqrestore(&dev->buflock, flags);
543 mutex_unlock(&dev->mtx);
544 if (signal_pending(current)) {
545 dev_dbg(&dev->udev->dev, "%s : interrupted\n",
547 set_current_state(TASK_RUNNING);
551 if (schedule_timeout(COMMAND_TIMEOUT) == 0) {
552 dev_dbg(&dev->udev->dev,
553 "%s - command timed out.\n", __func__);
557 remove_wait_queue(&dev->write_wait, &waita);
558 retval = mutex_lock_interruptible(&dev->mtx);
560 retval = bytes_written ? bytes_written : retval;
564 dev_dbg(&dev->udev->dev,
565 "%s : in progress, count = %zd\n",
568 spin_unlock_irqrestore(&dev->buflock, flags);
569 set_current_state(TASK_RUNNING);
570 remove_wait_queue(&dev->write_wait, &waita);
571 dev_dbg(&dev->udev->dev, "%s : sending, count = %zd\n",
574 /* write the data into interrupt_out_buffer from userspace */
575 buffer_size = usb_endpoint_maxp(dev->interrupt_out_endpoint);
576 bytes_to_write = count > buffer_size ? buffer_size : count;
577 dev_dbg(&dev->udev->dev,
578 "%s : buffer_size = %zd, count = %zd, bytes_to_write = %zd\n",
579 __func__, buffer_size, count, bytes_to_write);
581 if (copy_from_user(dev->interrupt_out_buffer, buffer, bytes_to_write) != 0) {
586 /* send off the urb */
588 dev->interrupt_out_urb,
590 usb_sndintpipe(dev->udev, dev->interrupt_out_endpoint->bEndpointAddress),
591 dev->interrupt_out_buffer,
593 adu_interrupt_out_callback,
595 dev->interrupt_out_endpoint->bInterval);
596 dev->interrupt_out_urb->actual_length = bytes_to_write;
597 dev->out_urb_finished = 0;
598 retval = usb_submit_urb(dev->interrupt_out_urb, GFP_KERNEL);
600 dev->out_urb_finished = 1;
601 dev_err(&dev->udev->dev, "Couldn't submit "
602 "interrupt_out_urb %d\n", retval);
606 buffer += bytes_to_write;
607 count -= bytes_to_write;
609 bytes_written += bytes_to_write;
612 mutex_unlock(&dev->mtx);
613 return bytes_written;
616 mutex_unlock(&dev->mtx);
621 remove_wait_queue(&dev->write_wait, &waita);
625 /* file operations needed when we register this driver */
626 static const struct file_operations adu_fops = {
627 .owner = THIS_MODULE,
631 .release = adu_release,
632 .llseek = noop_llseek,
636 * usb class driver info in order to get a minor number from the usb core,
637 * and to have the device registered with devfs and the driver core
639 static struct usb_class_driver adu_class = {
640 .name = "usb/adutux%d",
642 .minor_base = ADU_MINOR_BASE,
648 * Called by the usb core when a new device is connected that it thinks
649 * this driver might be interested in.
651 static int adu_probe(struct usb_interface *interface,
652 const struct usb_device_id *id)
654 struct usb_device *udev = interface_to_usbdev(interface);
655 struct adu_device *dev = NULL;
656 int retval = -ENOMEM;
661 /* allocate memory for our device state and initialize it */
662 dev = kzalloc(sizeof(struct adu_device), GFP_KERNEL);
666 mutex_init(&dev->mtx);
667 spin_lock_init(&dev->buflock);
668 dev->udev = usb_get_dev(udev);
669 init_waitqueue_head(&dev->read_wait);
670 init_waitqueue_head(&dev->write_wait);
672 res = usb_find_common_endpoints_reverse(interface->cur_altsetting,
674 &dev->interrupt_in_endpoint,
675 &dev->interrupt_out_endpoint);
677 dev_err(&interface->dev, "interrupt endpoints not found\n");
682 in_end_size = usb_endpoint_maxp(dev->interrupt_in_endpoint);
683 out_end_size = usb_endpoint_maxp(dev->interrupt_out_endpoint);
685 dev->read_buffer_primary = kmalloc((4 * in_end_size), GFP_KERNEL);
686 if (!dev->read_buffer_primary)
689 /* debug code prime the buffer */
690 memset(dev->read_buffer_primary, 'a', in_end_size);
691 memset(dev->read_buffer_primary + in_end_size, 'b', in_end_size);
692 memset(dev->read_buffer_primary + (2 * in_end_size), 'c', in_end_size);
693 memset(dev->read_buffer_primary + (3 * in_end_size), 'd', in_end_size);
695 dev->read_buffer_secondary = kmalloc((4 * in_end_size), GFP_KERNEL);
696 if (!dev->read_buffer_secondary)
699 /* debug code prime the buffer */
700 memset(dev->read_buffer_secondary, 'e', in_end_size);
701 memset(dev->read_buffer_secondary + in_end_size, 'f', in_end_size);
702 memset(dev->read_buffer_secondary + (2 * in_end_size), 'g', in_end_size);
703 memset(dev->read_buffer_secondary + (3 * in_end_size), 'h', in_end_size);
705 dev->interrupt_in_buffer = kmalloc(in_end_size, GFP_KERNEL);
706 if (!dev->interrupt_in_buffer)
709 /* debug code prime the buffer */
710 memset(dev->interrupt_in_buffer, 'i', in_end_size);
712 dev->interrupt_in_urb = usb_alloc_urb(0, GFP_KERNEL);
713 if (!dev->interrupt_in_urb)
715 dev->interrupt_out_buffer = kmalloc(out_end_size, GFP_KERNEL);
716 if (!dev->interrupt_out_buffer)
718 dev->interrupt_out_urb = usb_alloc_urb(0, GFP_KERNEL);
719 if (!dev->interrupt_out_urb)
722 if (!usb_string(udev, udev->descriptor.iSerialNumber, dev->serial_number,
723 sizeof(dev->serial_number))) {
724 dev_err(&interface->dev, "Could not retrieve serial number\n");
728 dev_dbg(&interface->dev,"serial_number=%s", dev->serial_number);
730 /* we can register the device now, as it is ready */
731 usb_set_intfdata(interface, dev);
733 retval = usb_register_dev(interface, &adu_class);
736 /* something prevented us from registering this driver */
737 dev_err(&interface->dev, "Not able to get a minor for this device.\n");
738 usb_set_intfdata(interface, NULL);
742 dev->minor = interface->minor;
744 /* let the user know what node this device is now attached to */
745 dev_info(&interface->dev, "ADU%d %s now attached to /dev/usb/adutux%d\n",
746 le16_to_cpu(udev->descriptor.idProduct), dev->serial_number,
747 (dev->minor - ADU_MINOR_BASE));
759 * Called by the usb core when the device is removed from the system.
761 static void adu_disconnect(struct usb_interface *interface)
763 struct adu_device *dev;
765 dev = usb_get_intfdata(interface);
767 usb_deregister_dev(interface, &adu_class);
769 usb_poison_urb(dev->interrupt_in_urb);
770 usb_poison_urb(dev->interrupt_out_urb);
772 mutex_lock(&adutux_mutex);
773 usb_set_intfdata(interface, NULL);
775 mutex_lock(&dev->mtx); /* not interruptible */
776 dev->disconnected = 1;
777 mutex_unlock(&dev->mtx);
779 /* if the device is not opened, then we clean up right now */
780 if (!dev->open_count)
783 mutex_unlock(&adutux_mutex);
786 /* usb specific object needed to register this driver with the usb subsystem */
787 static struct usb_driver adu_driver = {
790 .disconnect = adu_disconnect,
791 .id_table = device_table,
794 module_usb_driver(adu_driver);
796 MODULE_AUTHOR(DRIVER_AUTHOR);
797 MODULE_DESCRIPTION(DRIVER_DESC);
798 MODULE_LICENSE("GPL");