Greg Kroah-Hartman | 5fd54ac | 2017-11-03 11:28:30 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 2 | /* |
| 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. |
| 6 | * |
| 7 | * Copyright (c) 2003 John Homppi (SCO, leave this notice here) |
| 8 | * |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 9 | * derived from the Lego USB Tower driver 0.56: |
| 10 | * Copyright (c) 2003 David Glance <davidgsf@sourceforge.net> |
| 11 | * 2001 Juergen Stuber <stuber@loria.fr> |
| 12 | * that was derived from USB Skeleton driver - 0.5 |
| 13 | * Copyright (c) 2001 Greg Kroah-Hartman (greg@kroah.com) |
| 14 | * |
| 15 | */ |
| 16 | |
Greg Kroah-Hartman | 28f47c3 | 2013-06-26 16:30:46 -0700 | [diff] [blame] | 17 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 18 | |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 19 | #include <linux/kernel.h> |
Ingo Molnar | 174cd4b | 2017-02-02 19:15:33 +0100 | [diff] [blame] | 20 | #include <linux/sched/signal.h> |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 21 | #include <linux/errno.h> |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 22 | #include <linux/slab.h> |
| 23 | #include <linux/module.h> |
| 24 | #include <linux/usb.h> |
Matthias Kaehlcke | 8293c56 | 2007-07-13 21:28:31 +0200 | [diff] [blame] | 25 | #include <linux/mutex.h> |
Lisa Nguyen | 40cf483 | 2013-05-13 12:40:47 -0700 | [diff] [blame] | 26 | #include <linux/uaccess.h> |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 27 | |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 28 | #define DRIVER_AUTHOR "John Homppi" |
| 29 | #define DRIVER_DESC "adutux (see www.ontrak.net)" |
| 30 | |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 31 | /* Define these values to match your device */ |
| 32 | #define ADU_VENDOR_ID 0x0a07 |
| 33 | #define ADU_PRODUCT_ID 0x0064 |
| 34 | |
| 35 | /* table of devices that work with this driver */ |
Németh Márton | 33b9e16 | 2010-01-10 15:34:45 +0100 | [diff] [blame] | 36 | static const struct usb_device_id device_table[] = { |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 37 | { USB_DEVICE(ADU_VENDOR_ID, ADU_PRODUCT_ID) }, /* ADU100 */ |
Lisa Nguyen | eb79c01 | 2013-05-13 12:41:10 -0700 | [diff] [blame] | 38 | { USB_DEVICE(ADU_VENDOR_ID, ADU_PRODUCT_ID+20) }, /* ADU120 */ |
| 39 | { USB_DEVICE(ADU_VENDOR_ID, ADU_PRODUCT_ID+30) }, /* ADU130 */ |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 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 */ |
Lisa Nguyen | 83fc1fc | 2013-05-13 12:42:21 -0700 | [diff] [blame] | 43 | { } /* Terminating entry */ |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 44 | }; |
| 45 | |
| 46 | MODULE_DEVICE_TABLE(usb, device_table); |
| 47 | |
| 48 | #ifdef CONFIG_USB_DYNAMIC_MINORS |
| 49 | #define ADU_MINOR_BASE 0 |
| 50 | #else |
| 51 | #define ADU_MINOR_BASE 67 |
| 52 | #endif |
| 53 | |
| 54 | /* we can have up to this number of device plugged in at once */ |
| 55 | #define MAX_DEVICES 16 |
| 56 | |
Kirill Kapranov | cc9debf | 2018-02-17 23:02:10 +0200 | [diff] [blame] | 57 | #define COMMAND_TIMEOUT (2*HZ) |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 58 | |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 59 | /* |
| 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. |
| 67 | */ |
| 68 | |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 69 | /* Structure to hold all of our device specific stuff */ |
| 70 | struct adu_device { |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 71 | struct mutex mtx; |
Lisa Nguyen | 30c5c64 | 2013-05-13 12:41:34 -0700 | [diff] [blame] | 72 | struct usb_device *udev; /* save off the usb device pointer */ |
| 73 | struct usb_interface *interface; |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 74 | unsigned int minor; /* the starting minor number for this device */ |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 75 | char serial_number[8]; |
| 76 | |
| 77 | int open_count; /* number of times this port has been opened */ |
Johan Hovold | b2fa7ba | 2019-09-25 11:29:13 +0200 | [diff] [blame] | 78 | unsigned long disconnected:1; |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 79 | |
Lisa Nguyen | 30c5c64 | 2013-05-13 12:41:34 -0700 | [diff] [blame] | 80 | char *read_buffer_primary; |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 81 | int read_buffer_length; |
Lisa Nguyen | 30c5c64 | 2013-05-13 12:41:34 -0700 | [diff] [blame] | 82 | char *read_buffer_secondary; |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 83 | int secondary_head; |
| 84 | int secondary_tail; |
| 85 | spinlock_t buflock; |
| 86 | |
| 87 | wait_queue_head_t read_wait; |
| 88 | wait_queue_head_t write_wait; |
| 89 | |
Lisa Nguyen | 30c5c64 | 2013-05-13 12:41:34 -0700 | [diff] [blame] | 90 | char *interrupt_in_buffer; |
| 91 | struct usb_endpoint_descriptor *interrupt_in_endpoint; |
| 92 | struct urb *interrupt_in_urb; |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 93 | int read_urb_finished; |
| 94 | |
Lisa Nguyen | 30c5c64 | 2013-05-13 12:41:34 -0700 | [diff] [blame] | 95 | char *interrupt_out_buffer; |
| 96 | struct usb_endpoint_descriptor *interrupt_out_endpoint; |
| 97 | struct urb *interrupt_out_urb; |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 98 | int out_urb_finished; |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 99 | }; |
| 100 | |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 101 | static DEFINE_MUTEX(adutux_mutex); |
| 102 | |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 103 | static struct usb_driver adu_driver; |
| 104 | |
Greg Kroah-Hartman | 1ef37c6 | 2013-06-26 16:30:45 -0700 | [diff] [blame] | 105 | static inline void adu_debug_data(struct device *dev, const char *function, |
| 106 | int size, const unsigned char *data) |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 107 | { |
Greg Kroah-Hartman | 1ef37c6 | 2013-06-26 16:30:45 -0700 | [diff] [blame] | 108 | dev_dbg(dev, "%s - length = %d, data = %*ph\n", |
| 109 | function, size, size, data); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 110 | } |
| 111 | |
Lee Jones | 874ae83 | 2020-07-03 18:41:24 +0100 | [diff] [blame] | 112 | /* |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 113 | * adu_abort_transfers |
| 114 | * aborts transfers and frees associated data structures |
| 115 | */ |
| 116 | static void adu_abort_transfers(struct adu_device *dev) |
| 117 | { |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 118 | unsigned long flags; |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 119 | |
Johan Hovold | b2fa7ba | 2019-09-25 11:29:13 +0200 | [diff] [blame] | 120 | if (dev->disconnected) |
Greg Kroah-Hartman | 6e42a15 | 2013-06-26 16:30:43 -0700 | [diff] [blame] | 121 | return; |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 122 | |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 123 | /* shutdown transfer */ |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 124 | |
| 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); |
| 130 | } else |
| 131 | spin_unlock_irqrestore(&dev->buflock, flags); |
| 132 | |
| 133 | spin_lock_irqsave(&dev->buflock, flags); |
| 134 | if (!dev->out_urb_finished) { |
| 135 | spin_unlock_irqrestore(&dev->buflock, flags); |
Kirill Kapranov | 687ca63 | 2018-02-17 23:01:40 +0200 | [diff] [blame] | 136 | wait_event_timeout(dev->write_wait, dev->out_urb_finished, |
| 137 | COMMAND_TIMEOUT); |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 138 | usb_kill_urb(dev->interrupt_out_urb); |
| 139 | } else |
| 140 | spin_unlock_irqrestore(&dev->buflock, flags); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | static void adu_delete(struct adu_device *dev) |
| 144 | { |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 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); |
Johan Hovold | 123a0f1 | 2019-10-09 17:38:44 +0200 | [diff] [blame] | 152 | usb_put_dev(dev->udev); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 153 | kfree(dev); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 154 | } |
| 155 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 156 | static void adu_interrupt_in_callback(struct urb *urb) |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 157 | { |
| 158 | struct adu_device *dev = urb->context; |
Greg Kroah-Hartman | 24497a0 | 2007-07-18 10:58:02 -0700 | [diff] [blame] | 159 | int status = urb->status; |
Sebastian Andrzej Siewior | 957ada7 | 2018-06-25 00:08:42 +0200 | [diff] [blame] | 160 | unsigned long flags; |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 161 | |
Greg Kroah-Hartman | 1ef37c6 | 2013-06-26 16:30:45 -0700 | [diff] [blame] | 162 | adu_debug_data(&dev->udev->dev, __func__, |
| 163 | urb->actual_length, urb->transfer_buffer); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 164 | |
Sebastian Andrzej Siewior | 957ada7 | 2018-06-25 00:08:42 +0200 | [diff] [blame] | 165 | spin_lock_irqsave(&dev->buflock, flags); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 166 | |
Greg Kroah-Hartman | 24497a0 | 2007-07-18 10:58:02 -0700 | [diff] [blame] | 167 | if (status != 0) { |
Oliver Neukum | f6c1cea | 2007-08-16 16:02:08 +0200 | [diff] [blame] | 168 | if ((status != -ENOENT) && (status != -ECONNRESET) && |
| 169 | (status != -ESHUTDOWN)) { |
Greg Kroah-Hartman | 66d4bc3 | 2013-06-26 16:30:44 -0700 | [diff] [blame] | 170 | dev_dbg(&dev->udev->dev, |
| 171 | "%s : nonzero status received: %d\n", |
| 172 | __func__, status); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 173 | } |
| 174 | goto exit; |
| 175 | } |
| 176 | |
| 177 | if (urb->actual_length > 0 && dev->interrupt_in_buffer[0] != 0x00) { |
| 178 | if (dev->read_buffer_length < |
Kuninori Morimoto | 29cc889 | 2011-08-23 03:12:03 -0700 | [diff] [blame] | 179 | (4 * usb_endpoint_maxp(dev->interrupt_in_endpoint)) - |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 180 | (urb->actual_length)) { |
| 181 | memcpy (dev->read_buffer_primary + |
| 182 | dev->read_buffer_length, |
| 183 | dev->interrupt_in_buffer, urb->actual_length); |
| 184 | |
| 185 | dev->read_buffer_length += urb->actual_length; |
Greg Kroah-Hartman | 66d4bc3 | 2013-06-26 16:30:44 -0700 | [diff] [blame] | 186 | dev_dbg(&dev->udev->dev,"%s reading %d\n", __func__, |
| 187 | urb->actual_length); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 188 | } else { |
Greg Kroah-Hartman | 66d4bc3 | 2013-06-26 16:30:44 -0700 | [diff] [blame] | 189 | dev_dbg(&dev->udev->dev,"%s : read_buffer overflow\n", |
| 190 | __func__); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 191 | } |
| 192 | } |
| 193 | |
| 194 | exit: |
| 195 | dev->read_urb_finished = 1; |
Sebastian Andrzej Siewior | 957ada7 | 2018-06-25 00:08:42 +0200 | [diff] [blame] | 196 | spin_unlock_irqrestore(&dev->buflock, flags); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 197 | /* always wake up so we recover from errors */ |
| 198 | wake_up_interruptible(&dev->read_wait); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 199 | } |
| 200 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 201 | static void adu_interrupt_out_callback(struct urb *urb) |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 202 | { |
| 203 | struct adu_device *dev = urb->context; |
Greg Kroah-Hartman | 24497a0 | 2007-07-18 10:58:02 -0700 | [diff] [blame] | 204 | int status = urb->status; |
Sebastian Andrzej Siewior | 957ada7 | 2018-06-25 00:08:42 +0200 | [diff] [blame] | 205 | unsigned long flags; |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 206 | |
Greg Kroah-Hartman | 1ef37c6 | 2013-06-26 16:30:45 -0700 | [diff] [blame] | 207 | adu_debug_data(&dev->udev->dev, __func__, |
| 208 | urb->actual_length, urb->transfer_buffer); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 209 | |
Greg Kroah-Hartman | 24497a0 | 2007-07-18 10:58:02 -0700 | [diff] [blame] | 210 | if (status != 0) { |
| 211 | if ((status != -ENOENT) && |
Oliver Neukum | c56150c | 2020-09-17 13:26:00 +0200 | [diff] [blame] | 212 | (status != -ESHUTDOWN) && |
Greg Kroah-Hartman | 24497a0 | 2007-07-18 10:58:02 -0700 | [diff] [blame] | 213 | (status != -ECONNRESET)) { |
Greg Kroah-Hartman | 66d4bc3 | 2013-06-26 16:30:44 -0700 | [diff] [blame] | 214 | dev_dbg(&dev->udev->dev, |
| 215 | "%s :nonzero status received: %d\n", __func__, |
| 216 | status); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 217 | } |
Greg Kroah-Hartman | 1ef37c6 | 2013-06-26 16:30:45 -0700 | [diff] [blame] | 218 | return; |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 219 | } |
| 220 | |
Sebastian Andrzej Siewior | 957ada7 | 2018-06-25 00:08:42 +0200 | [diff] [blame] | 221 | spin_lock_irqsave(&dev->buflock, flags); |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 222 | dev->out_urb_finished = 1; |
| 223 | wake_up(&dev->write_wait); |
Sebastian Andrzej Siewior | 957ada7 | 2018-06-25 00:08:42 +0200 | [diff] [blame] | 224 | spin_unlock_irqrestore(&dev->buflock, flags); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | static int adu_open(struct inode *inode, struct file *file) |
| 228 | { |
| 229 | struct adu_device *dev = NULL; |
| 230 | struct usb_interface *interface; |
| 231 | int subminor; |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 232 | int retval; |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 233 | |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 234 | subminor = iminor(inode); |
| 235 | |
Lisa Nguyen | e77c4e6 | 2013-05-15 15:21:07 -0700 | [diff] [blame] | 236 | retval = mutex_lock_interruptible(&adutux_mutex); |
Greg Kroah-Hartman | 66d4bc3 | 2013-06-26 16:30:44 -0700 | [diff] [blame] | 237 | if (retval) |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 238 | goto exit_no_lock; |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 239 | |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 240 | interface = usb_find_interface(&adu_driver, subminor); |
| 241 | if (!interface) { |
Greg Kroah-Hartman | 28f47c3 | 2013-06-26 16:30:46 -0700 | [diff] [blame] | 242 | pr_err("%s - error, can't find device for minor %d\n", |
| 243 | __func__, subminor); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 244 | retval = -ENODEV; |
| 245 | goto exit_no_device; |
| 246 | } |
| 247 | |
| 248 | dev = usb_get_intfdata(interface); |
Johan Hovold | b2fa7ba | 2019-09-25 11:29:13 +0200 | [diff] [blame] | 249 | if (!dev) { |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 250 | retval = -ENODEV; |
| 251 | goto exit_no_device; |
| 252 | } |
| 253 | |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 254 | /* check that nobody else is using the device */ |
| 255 | if (dev->open_count) { |
| 256 | retval = -EBUSY; |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 257 | goto exit_no_device; |
| 258 | } |
| 259 | |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 260 | ++dev->open_count; |
Greg Kroah-Hartman | 66d4bc3 | 2013-06-26 16:30:44 -0700 | [diff] [blame] | 261 | dev_dbg(&dev->udev->dev, "%s: open count %d\n", __func__, |
| 262 | dev->open_count); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 263 | |
| 264 | /* save device in the file's private structure */ |
| 265 | file->private_data = dev; |
| 266 | |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 267 | /* initialize in direction */ |
| 268 | dev->read_buffer_length = 0; |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 269 | |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 270 | /* fixup first read by having urb waiting for it */ |
Lisa Nguyen | 05d7639 | 2013-05-13 12:41:54 -0700 | [diff] [blame] | 271 | usb_fill_int_urb(dev->interrupt_in_urb, dev->udev, |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 272 | usb_rcvintpipe(dev->udev, |
| 273 | dev->interrupt_in_endpoint->bEndpointAddress), |
| 274 | dev->interrupt_in_buffer, |
Kuninori Morimoto | 29cc889 | 2011-08-23 03:12:03 -0700 | [diff] [blame] | 275 | usb_endpoint_maxp(dev->interrupt_in_endpoint), |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 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 */ |
| 283 | |
| 284 | /* initialize out direction */ |
| 285 | dev->out_urb_finished = 1; |
| 286 | |
| 287 | retval = 0; |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 288 | |
| 289 | exit_no_device: |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 290 | mutex_unlock(&adutux_mutex); |
| 291 | exit_no_lock: |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 292 | return retval; |
| 293 | } |
| 294 | |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 295 | static void adu_release_internal(struct adu_device *dev) |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 296 | { |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 297 | /* decrement our usage count for the device */ |
| 298 | --dev->open_count; |
Greg Kroah-Hartman | 66d4bc3 | 2013-06-26 16:30:44 -0700 | [diff] [blame] | 299 | dev_dbg(&dev->udev->dev, "%s : open count %d\n", __func__, |
| 300 | dev->open_count); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 301 | if (dev->open_count <= 0) { |
| 302 | adu_abort_transfers(dev); |
| 303 | dev->open_count = 0; |
| 304 | } |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | static int adu_release(struct inode *inode, struct file *file) |
| 308 | { |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 309 | struct adu_device *dev; |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 310 | int retval = 0; |
| 311 | |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 312 | if (file == NULL) { |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 313 | retval = -ENODEV; |
| 314 | goto exit; |
| 315 | } |
| 316 | |
| 317 | dev = file->private_data; |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 318 | if (dev == NULL) { |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 319 | retval = -ENODEV; |
| 320 | goto exit; |
| 321 | } |
| 322 | |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 323 | mutex_lock(&adutux_mutex); /* not interruptible */ |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 324 | |
| 325 | if (dev->open_count <= 0) { |
Greg Kroah-Hartman | 66d4bc3 | 2013-06-26 16:30:44 -0700 | [diff] [blame] | 326 | dev_dbg(&dev->udev->dev, "%s : device not opened\n", __func__); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 327 | retval = -ENODEV; |
Jiri Slaby | 46c9844 | 2009-03-11 21:47:38 +0100 | [diff] [blame] | 328 | goto unlock; |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 329 | } |
| 330 | |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 331 | adu_release_internal(dev); |
Johan Hovold | b2fa7ba | 2019-09-25 11:29:13 +0200 | [diff] [blame] | 332 | if (dev->disconnected) { |
Alan Stern | d4ead16 | 2007-05-22 11:46:41 -0400 | [diff] [blame] | 333 | /* the device was unplugged before the file was released */ |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 334 | if (!dev->open_count) /* ... and we're the last user */ |
| 335 | adu_delete(dev); |
Alan Stern | d4ead16 | 2007-05-22 11:46:41 -0400 | [diff] [blame] | 336 | } |
Jiri Slaby | 46c9844 | 2009-03-11 21:47:38 +0100 | [diff] [blame] | 337 | unlock: |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 338 | mutex_unlock(&adutux_mutex); |
Jiri Slaby | 46c9844 | 2009-03-11 21:47:38 +0100 | [diff] [blame] | 339 | exit: |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 340 | return retval; |
| 341 | } |
| 342 | |
| 343 | static ssize_t adu_read(struct file *file, __user char *buffer, size_t count, |
| 344 | loff_t *ppos) |
| 345 | { |
| 346 | struct adu_device *dev; |
| 347 | size_t bytes_read = 0; |
| 348 | size_t bytes_to_read = count; |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 349 | int retval = 0; |
| 350 | int timeout = 0; |
| 351 | int should_submit = 0; |
| 352 | unsigned long flags; |
| 353 | DECLARE_WAITQUEUE(wait, current); |
| 354 | |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 355 | dev = file->private_data; |
Matthias Kaehlcke | 8293c56 | 2007-07-13 21:28:31 +0200 | [diff] [blame] | 356 | if (mutex_lock_interruptible(&dev->mtx)) |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 357 | return -ERESTARTSYS; |
| 358 | |
| 359 | /* verify that the device wasn't unplugged */ |
Johan Hovold | b2fa7ba | 2019-09-25 11:29:13 +0200 | [diff] [blame] | 360 | if (dev->disconnected) { |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 361 | retval = -ENODEV; |
Greg Kroah-Hartman | 28f47c3 | 2013-06-26 16:30:46 -0700 | [diff] [blame] | 362 | pr_err("No device or device unplugged %d\n", retval); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 363 | goto exit; |
| 364 | } |
| 365 | |
| 366 | /* verify that some data was requested */ |
| 367 | if (count == 0) { |
Greg Kroah-Hartman | 66d4bc3 | 2013-06-26 16:30:44 -0700 | [diff] [blame] | 368 | dev_dbg(&dev->udev->dev, "%s : read request of 0 bytes\n", |
| 369 | __func__); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 370 | goto exit; |
| 371 | } |
| 372 | |
| 373 | timeout = COMMAND_TIMEOUT; |
Greg Kroah-Hartman | 66d4bc3 | 2013-06-26 16:30:44 -0700 | [diff] [blame] | 374 | dev_dbg(&dev->udev->dev, "%s : about to start looping\n", __func__); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 375 | while (bytes_to_read) { |
Daniel M German | 4850f26a | 2019-06-21 08:22:40 -0700 | [diff] [blame] | 376 | size_t data_in_secondary = dev->secondary_tail - dev->secondary_head; |
Greg Kroah-Hartman | 66d4bc3 | 2013-06-26 16:30:44 -0700 | [diff] [blame] | 377 | dev_dbg(&dev->udev->dev, |
Daniel M German | 4850f26a | 2019-06-21 08:22:40 -0700 | [diff] [blame] | 378 | "%s : while, data_in_secondary=%zu, status=%d\n", |
Greg Kroah-Hartman | 66d4bc3 | 2013-06-26 16:30:44 -0700 | [diff] [blame] | 379 | __func__, data_in_secondary, |
| 380 | dev->interrupt_in_urb->status); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 381 | |
| 382 | if (data_in_secondary) { |
| 383 | /* drain secondary buffer */ |
Daniel M German | 4850f26a | 2019-06-21 08:22:40 -0700 | [diff] [blame] | 384 | size_t amount = min(bytes_to_read, data_in_secondary); |
| 385 | if (copy_to_user(buffer, dev->read_buffer_secondary+dev->secondary_head, amount)) { |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 386 | retval = -EFAULT; |
| 387 | goto exit; |
| 388 | } |
Daniel M German | 4850f26a | 2019-06-21 08:22:40 -0700 | [diff] [blame] | 389 | dev->secondary_head += amount; |
| 390 | bytes_read += amount; |
| 391 | bytes_to_read -= amount; |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 392 | } else { |
| 393 | /* we check the primary buffer */ |
| 394 | spin_lock_irqsave (&dev->buflock, flags); |
| 395 | if (dev->read_buffer_length) { |
| 396 | /* we secure access to the primary */ |
| 397 | char *tmp; |
Greg Kroah-Hartman | 66d4bc3 | 2013-06-26 16:30:44 -0700 | [diff] [blame] | 398 | dev_dbg(&dev->udev->dev, |
| 399 | "%s : swap, read_buffer_length = %d\n", |
| 400 | __func__, dev->read_buffer_length); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 401 | tmp = dev->read_buffer_secondary; |
| 402 | dev->read_buffer_secondary = dev->read_buffer_primary; |
| 403 | dev->read_buffer_primary = tmp; |
| 404 | dev->secondary_head = 0; |
| 405 | dev->secondary_tail = dev->read_buffer_length; |
| 406 | dev->read_buffer_length = 0; |
| 407 | spin_unlock_irqrestore(&dev->buflock, flags); |
| 408 | /* we have a free buffer so use it */ |
| 409 | should_submit = 1; |
| 410 | } else { |
| 411 | /* even the primary was empty - we may need to do IO */ |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 412 | if (!dev->read_urb_finished) { |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 413 | /* somebody is doing IO */ |
| 414 | spin_unlock_irqrestore(&dev->buflock, flags); |
Greg Kroah-Hartman | 66d4bc3 | 2013-06-26 16:30:44 -0700 | [diff] [blame] | 415 | dev_dbg(&dev->udev->dev, |
| 416 | "%s : submitted already\n", |
| 417 | __func__); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 418 | } else { |
| 419 | /* we must initiate input */ |
Greg Kroah-Hartman | 66d4bc3 | 2013-06-26 16:30:44 -0700 | [diff] [blame] | 420 | dev_dbg(&dev->udev->dev, |
| 421 | "%s : initiate input\n", |
| 422 | __func__); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 423 | dev->read_urb_finished = 0; |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 424 | spin_unlock_irqrestore(&dev->buflock, flags); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 425 | |
Lisa Nguyen | 05d7639 | 2013-05-13 12:41:54 -0700 | [diff] [blame] | 426 | usb_fill_int_urb(dev->interrupt_in_urb, dev->udev, |
Lisa Nguyen | eb79c01 | 2013-05-13 12:41:10 -0700 | [diff] [blame] | 427 | usb_rcvintpipe(dev->udev, |
| 428 | dev->interrupt_in_endpoint->bEndpointAddress), |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 429 | dev->interrupt_in_buffer, |
Kuninori Morimoto | 29cc889 | 2011-08-23 03:12:03 -0700 | [diff] [blame] | 430 | usb_endpoint_maxp(dev->interrupt_in_endpoint), |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 431 | adu_interrupt_in_callback, |
| 432 | dev, |
| 433 | dev->interrupt_in_endpoint->bInterval); |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 434 | retval = usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL); |
| 435 | if (retval) { |
| 436 | dev->read_urb_finished = 1; |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 437 | if (retval == -ENOMEM) { |
| 438 | retval = bytes_read ? bytes_read : -ENOMEM; |
| 439 | } |
Greg Kroah-Hartman | 66d4bc3 | 2013-06-26 16:30:44 -0700 | [diff] [blame] | 440 | dev_dbg(&dev->udev->dev, |
| 441 | "%s : submit failed\n", |
| 442 | __func__); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 443 | goto exit; |
| 444 | } |
| 445 | } |
| 446 | |
| 447 | /* we wait for I/O to complete */ |
| 448 | set_current_state(TASK_INTERRUPTIBLE); |
| 449 | add_wait_queue(&dev->read_wait, &wait); |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 450 | spin_lock_irqsave(&dev->buflock, flags); |
| 451 | if (!dev->read_urb_finished) { |
| 452 | spin_unlock_irqrestore(&dev->buflock, flags); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 453 | timeout = schedule_timeout(COMMAND_TIMEOUT); |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 454 | } else { |
| 455 | spin_unlock_irqrestore(&dev->buflock, flags); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 456 | set_current_state(TASK_RUNNING); |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 457 | } |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 458 | remove_wait_queue(&dev->read_wait, &wait); |
| 459 | |
| 460 | if (timeout <= 0) { |
Greg Kroah-Hartman | 66d4bc3 | 2013-06-26 16:30:44 -0700 | [diff] [blame] | 461 | dev_dbg(&dev->udev->dev, |
| 462 | "%s : timeout\n", __func__); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 463 | retval = bytes_read ? bytes_read : -ETIMEDOUT; |
| 464 | goto exit; |
| 465 | } |
| 466 | |
| 467 | if (signal_pending(current)) { |
Greg Kroah-Hartman | 66d4bc3 | 2013-06-26 16:30:44 -0700 | [diff] [blame] | 468 | dev_dbg(&dev->udev->dev, |
| 469 | "%s : signal pending\n", |
| 470 | __func__); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 471 | retval = bytes_read ? bytes_read : -EINTR; |
| 472 | goto exit; |
| 473 | } |
| 474 | } |
| 475 | } |
| 476 | } |
| 477 | |
| 478 | retval = bytes_read; |
| 479 | /* if the primary buffer is empty then use it */ |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 480 | spin_lock_irqsave(&dev->buflock, flags); |
| 481 | if (should_submit && dev->read_urb_finished) { |
| 482 | dev->read_urb_finished = 0; |
| 483 | spin_unlock_irqrestore(&dev->buflock, flags); |
Lisa Nguyen | 05d7639 | 2013-05-13 12:41:54 -0700 | [diff] [blame] | 484 | usb_fill_int_urb(dev->interrupt_in_urb, dev->udev, |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 485 | usb_rcvintpipe(dev->udev, |
Lisa Nguyen | eb79c01 | 2013-05-13 12:41:10 -0700 | [diff] [blame] | 486 | dev->interrupt_in_endpoint->bEndpointAddress), |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 487 | dev->interrupt_in_buffer, |
Kuninori Morimoto | 29cc889 | 2011-08-23 03:12:03 -0700 | [diff] [blame] | 488 | usb_endpoint_maxp(dev->interrupt_in_endpoint), |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 489 | adu_interrupt_in_callback, |
| 490 | dev, |
| 491 | dev->interrupt_in_endpoint->bInterval); |
| 492 | if (usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL) != 0) |
| 493 | dev->read_urb_finished = 1; |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 494 | /* we ignore failure */ |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 495 | } else { |
| 496 | spin_unlock_irqrestore(&dev->buflock, flags); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 497 | } |
| 498 | |
| 499 | exit: |
| 500 | /* unlock the device */ |
Matthias Kaehlcke | 8293c56 | 2007-07-13 21:28:31 +0200 | [diff] [blame] | 501 | mutex_unlock(&dev->mtx); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 502 | |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 503 | return retval; |
| 504 | } |
| 505 | |
| 506 | static ssize_t adu_write(struct file *file, const __user char *buffer, |
| 507 | size_t count, loff_t *ppos) |
| 508 | { |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 509 | DECLARE_WAITQUEUE(waita, current); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 510 | struct adu_device *dev; |
| 511 | size_t bytes_written = 0; |
| 512 | size_t bytes_to_write; |
| 513 | size_t buffer_size; |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 514 | unsigned long flags; |
Oliver Neukum | ebc3ac1 | 2007-04-02 15:16:36 +0200 | [diff] [blame] | 515 | int retval; |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 516 | |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 517 | dev = file->private_data; |
| 518 | |
Matthias Kaehlcke | 8293c56 | 2007-07-13 21:28:31 +0200 | [diff] [blame] | 519 | retval = mutex_lock_interruptible(&dev->mtx); |
Oliver Neukum | ebc3ac1 | 2007-04-02 15:16:36 +0200 | [diff] [blame] | 520 | if (retval) |
| 521 | goto exit_nolock; |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 522 | |
| 523 | /* verify that the device wasn't unplugged */ |
Johan Hovold | b2fa7ba | 2019-09-25 11:29:13 +0200 | [diff] [blame] | 524 | if (dev->disconnected) { |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 525 | retval = -ENODEV; |
Greg Kroah-Hartman | 28f47c3 | 2013-06-26 16:30:46 -0700 | [diff] [blame] | 526 | pr_err("No device or device unplugged %d\n", retval); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 527 | goto exit; |
| 528 | } |
| 529 | |
| 530 | /* verify that we actually have some data to write */ |
| 531 | if (count == 0) { |
Greg Kroah-Hartman | 66d4bc3 | 2013-06-26 16:30:44 -0700 | [diff] [blame] | 532 | dev_dbg(&dev->udev->dev, "%s : write request of 0 bytes\n", |
| 533 | __func__); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 534 | goto exit; |
| 535 | } |
| 536 | |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 537 | while (count > 0) { |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 538 | add_wait_queue(&dev->write_wait, &waita); |
| 539 | set_current_state(TASK_INTERRUPTIBLE); |
| 540 | spin_lock_irqsave(&dev->buflock, flags); |
| 541 | if (!dev->out_urb_finished) { |
| 542 | spin_unlock_irqrestore(&dev->buflock, flags); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 543 | |
Matthias Kaehlcke | 8293c56 | 2007-07-13 21:28:31 +0200 | [diff] [blame] | 544 | mutex_unlock(&dev->mtx); |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 545 | if (signal_pending(current)) { |
Greg Kroah-Hartman | 66d4bc3 | 2013-06-26 16:30:44 -0700 | [diff] [blame] | 546 | dev_dbg(&dev->udev->dev, "%s : interrupted\n", |
| 547 | __func__); |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 548 | set_current_state(TASK_RUNNING); |
| 549 | retval = -EINTR; |
| 550 | goto exit_onqueue; |
| 551 | } |
| 552 | if (schedule_timeout(COMMAND_TIMEOUT) == 0) { |
Greg Kroah-Hartman | 66d4bc3 | 2013-06-26 16:30:44 -0700 | [diff] [blame] | 553 | dev_dbg(&dev->udev->dev, |
| 554 | "%s - command timed out.\n", __func__); |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 555 | retval = -ETIMEDOUT; |
| 556 | goto exit_onqueue; |
| 557 | } |
| 558 | remove_wait_queue(&dev->write_wait, &waita); |
Matthias Kaehlcke | 8293c56 | 2007-07-13 21:28:31 +0200 | [diff] [blame] | 559 | retval = mutex_lock_interruptible(&dev->mtx); |
Oliver Neukum | ebc3ac1 | 2007-04-02 15:16:36 +0200 | [diff] [blame] | 560 | if (retval) { |
| 561 | retval = bytes_written ? bytes_written : retval; |
| 562 | goto exit_nolock; |
| 563 | } |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 564 | |
Greg Kroah-Hartman | 66d4bc3 | 2013-06-26 16:30:44 -0700 | [diff] [blame] | 565 | dev_dbg(&dev->udev->dev, |
Alexey Dobriyan | 5b5e092 | 2017-02-27 14:30:02 -0800 | [diff] [blame] | 566 | "%s : in progress, count = %zd\n", |
Greg Kroah-Hartman | 66d4bc3 | 2013-06-26 16:30:44 -0700 | [diff] [blame] | 567 | __func__, count); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 568 | } else { |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 569 | spin_unlock_irqrestore(&dev->buflock, flags); |
| 570 | set_current_state(TASK_RUNNING); |
| 571 | remove_wait_queue(&dev->write_wait, &waita); |
Alexey Dobriyan | 5b5e092 | 2017-02-27 14:30:02 -0800 | [diff] [blame] | 572 | dev_dbg(&dev->udev->dev, "%s : sending, count = %zd\n", |
Greg Kroah-Hartman | 66d4bc3 | 2013-06-26 16:30:44 -0700 | [diff] [blame] | 573 | __func__, count); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 574 | |
| 575 | /* write the data into interrupt_out_buffer from userspace */ |
Kuninori Morimoto | 29cc889 | 2011-08-23 03:12:03 -0700 | [diff] [blame] | 576 | buffer_size = usb_endpoint_maxp(dev->interrupt_out_endpoint); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 577 | bytes_to_write = count > buffer_size ? buffer_size : count; |
Greg Kroah-Hartman | 66d4bc3 | 2013-06-26 16:30:44 -0700 | [diff] [blame] | 578 | dev_dbg(&dev->udev->dev, |
Alexey Dobriyan | 5b5e092 | 2017-02-27 14:30:02 -0800 | [diff] [blame] | 579 | "%s : buffer_size = %zd, count = %zd, bytes_to_write = %zd\n", |
Greg Kroah-Hartman | 66d4bc3 | 2013-06-26 16:30:44 -0700 | [diff] [blame] | 580 | __func__, buffer_size, count, bytes_to_write); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 581 | |
| 582 | if (copy_from_user(dev->interrupt_out_buffer, buffer, bytes_to_write) != 0) { |
| 583 | retval = -EFAULT; |
| 584 | goto exit; |
| 585 | } |
| 586 | |
| 587 | /* send off the urb */ |
| 588 | usb_fill_int_urb( |
| 589 | dev->interrupt_out_urb, |
| 590 | dev->udev, |
| 591 | usb_sndintpipe(dev->udev, dev->interrupt_out_endpoint->bEndpointAddress), |
| 592 | dev->interrupt_out_buffer, |
| 593 | bytes_to_write, |
| 594 | adu_interrupt_out_callback, |
| 595 | dev, |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 596 | dev->interrupt_out_endpoint->bInterval); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 597 | dev->interrupt_out_urb->actual_length = bytes_to_write; |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 598 | dev->out_urb_finished = 0; |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 599 | retval = usb_submit_urb(dev->interrupt_out_urb, GFP_KERNEL); |
| 600 | if (retval < 0) { |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 601 | dev->out_urb_finished = 1; |
Greg Kroah-Hartman | fd3f191 | 2008-08-14 09:37:34 -0700 | [diff] [blame] | 602 | dev_err(&dev->udev->dev, "Couldn't submit " |
| 603 | "interrupt_out_urb %d\n", retval); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 604 | goto exit; |
| 605 | } |
| 606 | |
| 607 | buffer += bytes_to_write; |
| 608 | count -= bytes_to_write; |
| 609 | |
| 610 | bytes_written += bytes_to_write; |
| 611 | } |
| 612 | } |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 613 | mutex_unlock(&dev->mtx); |
| 614 | return bytes_written; |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 615 | |
| 616 | exit: |
Matthias Kaehlcke | 8293c56 | 2007-07-13 21:28:31 +0200 | [diff] [blame] | 617 | mutex_unlock(&dev->mtx); |
Oliver Neukum | ebc3ac1 | 2007-04-02 15:16:36 +0200 | [diff] [blame] | 618 | exit_nolock: |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 619 | return retval; |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 620 | |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 621 | exit_onqueue: |
| 622 | remove_wait_queue(&dev->write_wait, &waita); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 623 | return retval; |
| 624 | } |
| 625 | |
| 626 | /* file operations needed when we register this driver */ |
Arjan van de Ven | 00977a5 | 2007-02-12 00:55:34 -0800 | [diff] [blame] | 627 | static const struct file_operations adu_fops = { |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 628 | .owner = THIS_MODULE, |
| 629 | .read = adu_read, |
| 630 | .write = adu_write, |
| 631 | .open = adu_open, |
| 632 | .release = adu_release, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 633 | .llseek = noop_llseek, |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 634 | }; |
| 635 | |
| 636 | /* |
| 637 | * usb class driver info in order to get a minor number from the usb core, |
| 638 | * and to have the device registered with devfs and the driver core |
| 639 | */ |
| 640 | static struct usb_class_driver adu_class = { |
| 641 | .name = "usb/adutux%d", |
| 642 | .fops = &adu_fops, |
| 643 | .minor_base = ADU_MINOR_BASE, |
| 644 | }; |
| 645 | |
Lee Jones | 874ae83 | 2020-07-03 18:41:24 +0100 | [diff] [blame] | 646 | /* |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 647 | * adu_probe |
| 648 | * |
| 649 | * Called by the usb core when a new device is connected that it thinks |
| 650 | * this driver might be interested in. |
| 651 | */ |
| 652 | static int adu_probe(struct usb_interface *interface, |
| 653 | const struct usb_device_id *id) |
| 654 | { |
| 655 | struct usb_device *udev = interface_to_usbdev(interface); |
| 656 | struct adu_device *dev = NULL; |
Johan Hovold | e0e9052 | 2017-03-17 11:35:33 +0100 | [diff] [blame] | 657 | int retval = -ENOMEM; |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 658 | int in_end_size; |
| 659 | int out_end_size; |
Johan Hovold | e53e034 | 2017-03-17 11:35:34 +0100 | [diff] [blame] | 660 | int res; |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 661 | |
Uwe Kleine-König | b595076 | 2010-11-01 15:38:34 -0400 | [diff] [blame] | 662 | /* allocate memory for our device state and initialize it */ |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 663 | dev = kzalloc(sizeof(struct adu_device), GFP_KERNEL); |
Johan Hovold | e0e9052 | 2017-03-17 11:35:33 +0100 | [diff] [blame] | 664 | if (!dev) |
| 665 | return -ENOMEM; |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 666 | |
Matthias Kaehlcke | 8293c56 | 2007-07-13 21:28:31 +0200 | [diff] [blame] | 667 | mutex_init(&dev->mtx); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 668 | spin_lock_init(&dev->buflock); |
Johan Hovold | 123a0f1 | 2019-10-09 17:38:44 +0200 | [diff] [blame] | 669 | dev->udev = usb_get_dev(udev); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 670 | init_waitqueue_head(&dev->read_wait); |
| 671 | init_waitqueue_head(&dev->write_wait); |
| 672 | |
Johan Hovold | 3c11c4b | 2019-12-10 12:25:59 +0100 | [diff] [blame] | 673 | res = usb_find_common_endpoints_reverse(interface->cur_altsetting, |
Johan Hovold | e53e034 | 2017-03-17 11:35:34 +0100 | [diff] [blame] | 674 | NULL, NULL, |
| 675 | &dev->interrupt_in_endpoint, |
| 676 | &dev->interrupt_out_endpoint); |
| 677 | if (res) { |
| 678 | dev_err(&interface->dev, "interrupt endpoints not found\n"); |
| 679 | retval = res; |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 680 | goto error; |
| 681 | } |
| 682 | |
Kuninori Morimoto | 29cc889 | 2011-08-23 03:12:03 -0700 | [diff] [blame] | 683 | in_end_size = usb_endpoint_maxp(dev->interrupt_in_endpoint); |
| 684 | out_end_size = usb_endpoint_maxp(dev->interrupt_out_endpoint); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 685 | |
| 686 | dev->read_buffer_primary = kmalloc((4 * in_end_size), GFP_KERNEL); |
Johan Hovold | e0e9052 | 2017-03-17 11:35:33 +0100 | [diff] [blame] | 687 | if (!dev->read_buffer_primary) |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 688 | goto error; |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 689 | |
| 690 | /* debug code prime the buffer */ |
| 691 | memset(dev->read_buffer_primary, 'a', in_end_size); |
| 692 | memset(dev->read_buffer_primary + in_end_size, 'b', in_end_size); |
| 693 | memset(dev->read_buffer_primary + (2 * in_end_size), 'c', in_end_size); |
| 694 | memset(dev->read_buffer_primary + (3 * in_end_size), 'd', in_end_size); |
| 695 | |
| 696 | dev->read_buffer_secondary = kmalloc((4 * in_end_size), GFP_KERNEL); |
Johan Hovold | e0e9052 | 2017-03-17 11:35:33 +0100 | [diff] [blame] | 697 | if (!dev->read_buffer_secondary) |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 698 | goto error; |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 699 | |
| 700 | /* debug code prime the buffer */ |
| 701 | memset(dev->read_buffer_secondary, 'e', in_end_size); |
| 702 | memset(dev->read_buffer_secondary + in_end_size, 'f', in_end_size); |
| 703 | memset(dev->read_buffer_secondary + (2 * in_end_size), 'g', in_end_size); |
| 704 | memset(dev->read_buffer_secondary + (3 * in_end_size), 'h', in_end_size); |
| 705 | |
| 706 | dev->interrupt_in_buffer = kmalloc(in_end_size, GFP_KERNEL); |
Wolfram Sang | a02b55c | 2016-08-25 19:39:11 +0200 | [diff] [blame] | 707 | if (!dev->interrupt_in_buffer) |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 708 | goto error; |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 709 | |
| 710 | /* debug code prime the buffer */ |
| 711 | memset(dev->interrupt_in_buffer, 'i', in_end_size); |
| 712 | |
| 713 | dev->interrupt_in_urb = usb_alloc_urb(0, GFP_KERNEL); |
Wolfram Sang | 71574a5 | 2016-08-11 23:14:36 +0200 | [diff] [blame] | 714 | if (!dev->interrupt_in_urb) |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 715 | goto error; |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 716 | dev->interrupt_out_buffer = kmalloc(out_end_size, GFP_KERNEL); |
Wolfram Sang | a02b55c | 2016-08-25 19:39:11 +0200 | [diff] [blame] | 717 | if (!dev->interrupt_out_buffer) |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 718 | goto error; |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 719 | dev->interrupt_out_urb = usb_alloc_urb(0, GFP_KERNEL); |
Wolfram Sang | 71574a5 | 2016-08-11 23:14:36 +0200 | [diff] [blame] | 720 | if (!dev->interrupt_out_urb) |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 721 | goto error; |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 722 | |
| 723 | if (!usb_string(udev, udev->descriptor.iSerialNumber, dev->serial_number, |
| 724 | sizeof(dev->serial_number))) { |
| 725 | dev_err(&interface->dev, "Could not retrieve serial number\n"); |
Johan Hovold | e0e9052 | 2017-03-17 11:35:33 +0100 | [diff] [blame] | 726 | retval = -EIO; |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 727 | goto error; |
| 728 | } |
Greg Kroah-Hartman | 66d4bc3 | 2013-06-26 16:30:44 -0700 | [diff] [blame] | 729 | dev_dbg(&interface->dev,"serial_number=%s", dev->serial_number); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 730 | |
| 731 | /* we can register the device now, as it is ready */ |
| 732 | usb_set_intfdata(interface, dev); |
| 733 | |
| 734 | retval = usb_register_dev(interface, &adu_class); |
| 735 | |
| 736 | if (retval) { |
| 737 | /* something prevented us from registering this driver */ |
| 738 | dev_err(&interface->dev, "Not able to get a minor for this device.\n"); |
| 739 | usb_set_intfdata(interface, NULL); |
| 740 | goto error; |
| 741 | } |
| 742 | |
| 743 | dev->minor = interface->minor; |
| 744 | |
| 745 | /* let the user know what node this device is now attached to */ |
Joe Perches | 898eb71 | 2007-10-18 03:06:30 -0700 | [diff] [blame] | 746 | dev_info(&interface->dev, "ADU%d %s now attached to /dev/usb/adutux%d\n", |
Johan Hovold | d482b9d | 2013-08-11 16:49:22 +0200 | [diff] [blame] | 747 | le16_to_cpu(udev->descriptor.idProduct), dev->serial_number, |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 748 | (dev->minor - ADU_MINOR_BASE)); |
Johan Hovold | e0e9052 | 2017-03-17 11:35:33 +0100 | [diff] [blame] | 749 | |
| 750 | return 0; |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 751 | |
| 752 | error: |
| 753 | adu_delete(dev); |
| 754 | return retval; |
| 755 | } |
| 756 | |
Lee Jones | 874ae83 | 2020-07-03 18:41:24 +0100 | [diff] [blame] | 757 | /* |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 758 | * adu_disconnect |
| 759 | * |
| 760 | * Called by the usb core when the device is removed from the system. |
| 761 | */ |
| 762 | static void adu_disconnect(struct usb_interface *interface) |
| 763 | { |
| 764 | struct adu_device *dev; |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 765 | |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 766 | dev = usb_get_intfdata(interface); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 767 | |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 768 | usb_deregister_dev(interface, &adu_class); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 769 | |
Johan Hovold | b2fa7ba | 2019-09-25 11:29:13 +0200 | [diff] [blame] | 770 | usb_poison_urb(dev->interrupt_in_urb); |
| 771 | usb_poison_urb(dev->interrupt_out_urb); |
| 772 | |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 773 | mutex_lock(&adutux_mutex); |
| 774 | usb_set_intfdata(interface, NULL); |
Alan Stern | d4ead16 | 2007-05-22 11:46:41 -0400 | [diff] [blame] | 775 | |
Johan Hovold | 44efc26 | 2019-09-25 11:29:12 +0200 | [diff] [blame] | 776 | mutex_lock(&dev->mtx); /* not interruptible */ |
Johan Hovold | b2fa7ba | 2019-09-25 11:29:13 +0200 | [diff] [blame] | 777 | dev->disconnected = 1; |
Johan Hovold | 44efc26 | 2019-09-25 11:29:12 +0200 | [diff] [blame] | 778 | mutex_unlock(&dev->mtx); |
| 779 | |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 780 | /* if the device is not opened, then we clean up right now */ |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 781 | if (!dev->open_count) |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 782 | adu_delete(dev); |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 783 | |
| 784 | mutex_unlock(&adutux_mutex); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 785 | } |
| 786 | |
| 787 | /* usb specific object needed to register this driver with the usb subsystem */ |
| 788 | static struct usb_driver adu_driver = { |
| 789 | .name = "adutux", |
| 790 | .probe = adu_probe, |
| 791 | .disconnect = adu_disconnect, |
| 792 | .id_table = device_table, |
| 793 | }; |
| 794 | |
Greg Kroah-Hartman | 65db430 | 2011-11-18 09:34:02 -0800 | [diff] [blame] | 795 | module_usb_driver(adu_driver); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 796 | |
| 797 | MODULE_AUTHOR(DRIVER_AUTHOR); |
| 798 | MODULE_DESCRIPTION(DRIVER_DESC); |
| 799 | MODULE_LICENSE("GPL"); |