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