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