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