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