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 | |
| 21 | #include <linux/kernel.h> |
| 22 | #include <linux/errno.h> |
| 23 | #include <linux/init.h> |
| 24 | #include <linux/slab.h> |
| 25 | #include <linux/module.h> |
| 26 | #include <linux/usb.h> |
Matthias Kaehlcke | 8293c56 | 2007-07-13 21:28:31 +0200 | [diff] [blame] | 27 | #include <linux/mutex.h> |
Lisa Nguyen | 40cf483 | 2013-05-13 12:40:47 -0700 | [diff] [blame] | 28 | #include <linux/uaccess.h> |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 29 | |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 30 | /* Version Information */ |
| 31 | #define DRIVER_VERSION "v0.0.13" |
| 32 | #define DRIVER_AUTHOR "John Homppi" |
| 33 | #define DRIVER_DESC "adutux (see www.ontrak.net)" |
| 34 | |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 35 | /* Define these values to match your device */ |
| 36 | #define ADU_VENDOR_ID 0x0a07 |
| 37 | #define ADU_PRODUCT_ID 0x0064 |
| 38 | |
| 39 | /* table of devices that work with this driver */ |
Németh Márton | 33b9e16 | 2010-01-10 15:34:45 +0100 | [diff] [blame] | 40 | static const struct usb_device_id device_table[] = { |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 41 | { USB_DEVICE(ADU_VENDOR_ID, ADU_PRODUCT_ID) }, /* ADU100 */ |
Lisa Nguyen | eb79c01 | 2013-05-13 12:41:10 -0700 | [diff] [blame] | 42 | { USB_DEVICE(ADU_VENDOR_ID, ADU_PRODUCT_ID+20) }, /* ADU120 */ |
| 43 | { USB_DEVICE(ADU_VENDOR_ID, ADU_PRODUCT_ID+30) }, /* ADU130 */ |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 44 | { USB_DEVICE(ADU_VENDOR_ID, ADU_PRODUCT_ID+100) }, /* ADU200 */ |
| 45 | { USB_DEVICE(ADU_VENDOR_ID, ADU_PRODUCT_ID+108) }, /* ADU208 */ |
| 46 | { USB_DEVICE(ADU_VENDOR_ID, ADU_PRODUCT_ID+118) }, /* ADU218 */ |
Lisa Nguyen | 83fc1fc | 2013-05-13 12:42:21 -0700 | [diff] [blame] | 47 | { } /* Terminating entry */ |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 48 | }; |
| 49 | |
| 50 | MODULE_DEVICE_TABLE(usb, device_table); |
| 51 | |
| 52 | #ifdef CONFIG_USB_DYNAMIC_MINORS |
| 53 | #define ADU_MINOR_BASE 0 |
| 54 | #else |
| 55 | #define ADU_MINOR_BASE 67 |
| 56 | #endif |
| 57 | |
| 58 | /* we can have up to this number of device plugged in at once */ |
| 59 | #define MAX_DEVICES 16 |
| 60 | |
| 61 | #define COMMAND_TIMEOUT (2*HZ) /* 60 second timeout for a command */ |
| 62 | |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 63 | /* |
| 64 | * The locking scheme is a vanilla 3-lock: |
| 65 | * adu_device.buflock: A spinlock, covers what IRQs touch. |
| 66 | * adutux_mutex: A Static lock to cover open_count. It would also cover |
| 67 | * any globals, but we don't have them in 2.6. |
| 68 | * adu_device.mtx: A mutex to hold across sleepers like copy_from_user. |
| 69 | * It covers all of adu_device, except the open_count |
| 70 | * and what .buflock covers. |
| 71 | */ |
| 72 | |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 73 | /* Structure to hold all of our device specific stuff */ |
| 74 | struct adu_device { |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 75 | struct mutex mtx; |
Lisa Nguyen | 30c5c64 | 2013-05-13 12:41:34 -0700 | [diff] [blame] | 76 | struct usb_device *udev; /* save off the usb device pointer */ |
| 77 | struct usb_interface *interface; |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 78 | unsigned int minor; /* the starting minor number for this device */ |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 79 | char serial_number[8]; |
| 80 | |
| 81 | int open_count; /* number of times this port has been opened */ |
| 82 | |
Lisa Nguyen | 30c5c64 | 2013-05-13 12:41:34 -0700 | [diff] [blame] | 83 | char *read_buffer_primary; |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 84 | int read_buffer_length; |
Lisa Nguyen | 30c5c64 | 2013-05-13 12:41:34 -0700 | [diff] [blame] | 85 | char *read_buffer_secondary; |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 86 | int secondary_head; |
| 87 | int secondary_tail; |
| 88 | spinlock_t buflock; |
| 89 | |
| 90 | wait_queue_head_t read_wait; |
| 91 | wait_queue_head_t write_wait; |
| 92 | |
Lisa Nguyen | 30c5c64 | 2013-05-13 12:41:34 -0700 | [diff] [blame] | 93 | char *interrupt_in_buffer; |
| 94 | struct usb_endpoint_descriptor *interrupt_in_endpoint; |
| 95 | struct urb *interrupt_in_urb; |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 96 | int read_urb_finished; |
| 97 | |
Lisa Nguyen | 30c5c64 | 2013-05-13 12:41:34 -0700 | [diff] [blame] | 98 | char *interrupt_out_buffer; |
| 99 | struct usb_endpoint_descriptor *interrupt_out_endpoint; |
| 100 | struct urb *interrupt_out_urb; |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 101 | int out_urb_finished; |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 102 | }; |
| 103 | |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 104 | static DEFINE_MUTEX(adutux_mutex); |
| 105 | |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 106 | static struct usb_driver adu_driver; |
| 107 | |
Greg Kroah-Hartman | 1ef37c6 | 2013-06-26 16:30:45 -0700 | [diff] [blame^] | 108 | static inline void adu_debug_data(struct device *dev, const char *function, |
| 109 | int size, const unsigned char *data) |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 110 | { |
Greg Kroah-Hartman | 1ef37c6 | 2013-06-26 16:30:45 -0700 | [diff] [blame^] | 111 | dev_dbg(dev, "%s - length = %d, data = %*ph\n", |
| 112 | function, size, size, data); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | /** |
| 116 | * adu_abort_transfers |
| 117 | * aborts transfers and frees associated data structures |
| 118 | */ |
| 119 | static void adu_abort_transfers(struct adu_device *dev) |
| 120 | { |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 121 | unsigned long flags; |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 122 | |
Greg Kroah-Hartman | 66d4bc3 | 2013-06-26 16:30:44 -0700 | [diff] [blame] | 123 | if (dev->udev == NULL) |
Greg Kroah-Hartman | 6e42a15 | 2013-06-26 16:30:43 -0700 | [diff] [blame] | 124 | return; |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 125 | |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 126 | /* shutdown transfer */ |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 127 | |
| 128 | /* XXX Anchor these instead */ |
| 129 | spin_lock_irqsave(&dev->buflock, flags); |
| 130 | if (!dev->read_urb_finished) { |
| 131 | spin_unlock_irqrestore(&dev->buflock, flags); |
| 132 | usb_kill_urb(dev->interrupt_in_urb); |
| 133 | } else |
| 134 | spin_unlock_irqrestore(&dev->buflock, flags); |
| 135 | |
| 136 | spin_lock_irqsave(&dev->buflock, flags); |
| 137 | if (!dev->out_urb_finished) { |
| 138 | spin_unlock_irqrestore(&dev->buflock, flags); |
| 139 | usb_kill_urb(dev->interrupt_out_urb); |
| 140 | } else |
| 141 | spin_unlock_irqrestore(&dev->buflock, flags); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | static void adu_delete(struct adu_device *dev) |
| 145 | { |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 146 | /* free data structures */ |
| 147 | usb_free_urb(dev->interrupt_in_urb); |
| 148 | usb_free_urb(dev->interrupt_out_urb); |
| 149 | kfree(dev->read_buffer_primary); |
| 150 | kfree(dev->read_buffer_secondary); |
| 151 | kfree(dev->interrupt_in_buffer); |
| 152 | kfree(dev->interrupt_out_buffer); |
| 153 | kfree(dev); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 154 | } |
| 155 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 156 | static void adu_interrupt_in_callback(struct urb *urb) |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 157 | { |
| 158 | struct adu_device *dev = urb->context; |
Greg Kroah-Hartman | 24497a0 | 2007-07-18 10:58:02 -0700 | [diff] [blame] | 159 | int status = urb->status; |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 160 | |
Greg Kroah-Hartman | 1ef37c6 | 2013-06-26 16:30:45 -0700 | [diff] [blame^] | 161 | adu_debug_data(&dev->udev->dev, __func__, |
| 162 | urb->actual_length, urb->transfer_buffer); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 163 | |
| 164 | spin_lock(&dev->buflock); |
| 165 | |
Greg Kroah-Hartman | 24497a0 | 2007-07-18 10:58:02 -0700 | [diff] [blame] | 166 | if (status != 0) { |
Oliver Neukum | f6c1cea | 2007-08-16 16:02:08 +0200 | [diff] [blame] | 167 | if ((status != -ENOENT) && (status != -ECONNRESET) && |
| 168 | (status != -ESHUTDOWN)) { |
Greg Kroah-Hartman | 66d4bc3 | 2013-06-26 16:30:44 -0700 | [diff] [blame] | 169 | dev_dbg(&dev->udev->dev, |
| 170 | "%s : nonzero status received: %d\n", |
| 171 | __func__, status); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 172 | } |
| 173 | goto exit; |
| 174 | } |
| 175 | |
| 176 | if (urb->actual_length > 0 && dev->interrupt_in_buffer[0] != 0x00) { |
| 177 | if (dev->read_buffer_length < |
Kuninori Morimoto | 29cc889 | 2011-08-23 03:12:03 -0700 | [diff] [blame] | 178 | (4 * usb_endpoint_maxp(dev->interrupt_in_endpoint)) - |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 179 | (urb->actual_length)) { |
| 180 | memcpy (dev->read_buffer_primary + |
| 181 | dev->read_buffer_length, |
| 182 | dev->interrupt_in_buffer, urb->actual_length); |
| 183 | |
| 184 | dev->read_buffer_length += urb->actual_length; |
Greg Kroah-Hartman | 66d4bc3 | 2013-06-26 16:30:44 -0700 | [diff] [blame] | 185 | dev_dbg(&dev->udev->dev,"%s reading %d\n", __func__, |
| 186 | urb->actual_length); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 187 | } else { |
Greg Kroah-Hartman | 66d4bc3 | 2013-06-26 16:30:44 -0700 | [diff] [blame] | 188 | dev_dbg(&dev->udev->dev,"%s : read_buffer overflow\n", |
| 189 | __func__); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 190 | } |
| 191 | } |
| 192 | |
| 193 | exit: |
| 194 | dev->read_urb_finished = 1; |
| 195 | spin_unlock(&dev->buflock); |
| 196 | /* always wake up so we recover from errors */ |
| 197 | wake_up_interruptible(&dev->read_wait); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 198 | } |
| 199 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 200 | static void adu_interrupt_out_callback(struct urb *urb) |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 201 | { |
| 202 | struct adu_device *dev = urb->context; |
Greg Kroah-Hartman | 24497a0 | 2007-07-18 10:58:02 -0700 | [diff] [blame] | 203 | int status = urb->status; |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 204 | |
Greg Kroah-Hartman | 1ef37c6 | 2013-06-26 16:30:45 -0700 | [diff] [blame^] | 205 | adu_debug_data(&dev->udev->dev, __func__, |
| 206 | urb->actual_length, urb->transfer_buffer); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 207 | |
Greg Kroah-Hartman | 24497a0 | 2007-07-18 10:58:02 -0700 | [diff] [blame] | 208 | if (status != 0) { |
| 209 | if ((status != -ENOENT) && |
| 210 | (status != -ECONNRESET)) { |
Greg Kroah-Hartman | 66d4bc3 | 2013-06-26 16:30:44 -0700 | [diff] [blame] | 211 | dev_dbg(&dev->udev->dev, |
| 212 | "%s :nonzero status received: %d\n", __func__, |
| 213 | status); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 214 | } |
Greg Kroah-Hartman | 1ef37c6 | 2013-06-26 16:30:45 -0700 | [diff] [blame^] | 215 | return; |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 216 | } |
| 217 | |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 218 | spin_lock(&dev->buflock); |
| 219 | dev->out_urb_finished = 1; |
| 220 | wake_up(&dev->write_wait); |
| 221 | spin_unlock(&dev->buflock); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | static int adu_open(struct inode *inode, struct file *file) |
| 225 | { |
| 226 | struct adu_device *dev = NULL; |
| 227 | struct usb_interface *interface; |
| 228 | int subminor; |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 229 | int retval; |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 230 | |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 231 | subminor = iminor(inode); |
| 232 | |
Lisa Nguyen | e77c4e6 | 2013-05-15 15:21:07 -0700 | [diff] [blame] | 233 | retval = mutex_lock_interruptible(&adutux_mutex); |
Greg Kroah-Hartman | 66d4bc3 | 2013-06-26 16:30:44 -0700 | [diff] [blame] | 234 | if (retval) |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 235 | goto exit_no_lock; |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 236 | |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 237 | interface = usb_find_interface(&adu_driver, subminor); |
| 238 | if (!interface) { |
Greg Kroah-Hartman | fd3f191 | 2008-08-14 09:37:34 -0700 | [diff] [blame] | 239 | printk(KERN_ERR "adutux: %s - error, can't find device for " |
| 240 | "minor %d\n", __func__, subminor); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 241 | retval = -ENODEV; |
| 242 | goto exit_no_device; |
| 243 | } |
| 244 | |
| 245 | dev = usb_get_intfdata(interface); |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 246 | if (!dev || !dev->udev) { |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 247 | retval = -ENODEV; |
| 248 | goto exit_no_device; |
| 249 | } |
| 250 | |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 251 | /* check that nobody else is using the device */ |
| 252 | if (dev->open_count) { |
| 253 | retval = -EBUSY; |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 254 | goto exit_no_device; |
| 255 | } |
| 256 | |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 257 | ++dev->open_count; |
Greg Kroah-Hartman | 66d4bc3 | 2013-06-26 16:30:44 -0700 | [diff] [blame] | 258 | dev_dbg(&dev->udev->dev, "%s: open count %d\n", __func__, |
| 259 | dev->open_count); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 260 | |
| 261 | /* save device in the file's private structure */ |
| 262 | file->private_data = dev; |
| 263 | |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 264 | /* initialize in direction */ |
| 265 | dev->read_buffer_length = 0; |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 266 | |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 267 | /* fixup first read by having urb waiting for it */ |
Lisa Nguyen | 05d7639 | 2013-05-13 12:41:54 -0700 | [diff] [blame] | 268 | usb_fill_int_urb(dev->interrupt_in_urb, dev->udev, |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 269 | usb_rcvintpipe(dev->udev, |
| 270 | dev->interrupt_in_endpoint->bEndpointAddress), |
| 271 | dev->interrupt_in_buffer, |
Kuninori Morimoto | 29cc889 | 2011-08-23 03:12:03 -0700 | [diff] [blame] | 272 | usb_endpoint_maxp(dev->interrupt_in_endpoint), |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 273 | adu_interrupt_in_callback, dev, |
| 274 | dev->interrupt_in_endpoint->bInterval); |
| 275 | dev->read_urb_finished = 0; |
| 276 | if (usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL)) |
| 277 | dev->read_urb_finished = 1; |
| 278 | /* we ignore failure */ |
| 279 | /* end of fixup for first read */ |
| 280 | |
| 281 | /* initialize out direction */ |
| 282 | dev->out_urb_finished = 1; |
| 283 | |
| 284 | retval = 0; |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 285 | |
| 286 | exit_no_device: |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 287 | mutex_unlock(&adutux_mutex); |
| 288 | exit_no_lock: |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 289 | return retval; |
| 290 | } |
| 291 | |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 292 | static void adu_release_internal(struct adu_device *dev) |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 293 | { |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 294 | /* decrement our usage count for the device */ |
| 295 | --dev->open_count; |
Greg Kroah-Hartman | 66d4bc3 | 2013-06-26 16:30:44 -0700 | [diff] [blame] | 296 | dev_dbg(&dev->udev->dev, "%s : open count %d\n", __func__, |
| 297 | dev->open_count); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 298 | if (dev->open_count <= 0) { |
| 299 | adu_abort_transfers(dev); |
| 300 | dev->open_count = 0; |
| 301 | } |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | static int adu_release(struct inode *inode, struct file *file) |
| 305 | { |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 306 | struct adu_device *dev; |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 307 | int retval = 0; |
| 308 | |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 309 | if (file == NULL) { |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 310 | retval = -ENODEV; |
| 311 | goto exit; |
| 312 | } |
| 313 | |
| 314 | dev = file->private_data; |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 315 | if (dev == NULL) { |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 316 | retval = -ENODEV; |
| 317 | goto exit; |
| 318 | } |
| 319 | |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 320 | mutex_lock(&adutux_mutex); /* not interruptible */ |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 321 | |
| 322 | if (dev->open_count <= 0) { |
Greg Kroah-Hartman | 66d4bc3 | 2013-06-26 16:30:44 -0700 | [diff] [blame] | 323 | dev_dbg(&dev->udev->dev, "%s : device not opened\n", __func__); |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 324 | retval = -ENODEV; |
Jiri Slaby | 46c9844 | 2009-03-11 21:47:38 +0100 | [diff] [blame] | 325 | goto unlock; |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 326 | } |
| 327 | |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 328 | adu_release_internal(dev); |
Alan Stern | d4ead16 | 2007-05-22 11:46:41 -0400 | [diff] [blame] | 329 | if (dev->udev == NULL) { |
| 330 | /* the device was unplugged before the file was released */ |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 331 | if (!dev->open_count) /* ... and we're the last user */ |
| 332 | adu_delete(dev); |
Alan Stern | d4ead16 | 2007-05-22 11:46:41 -0400 | [diff] [blame] | 333 | } |
Jiri Slaby | 46c9844 | 2009-03-11 21:47:38 +0100 | [diff] [blame] | 334 | unlock: |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 335 | mutex_unlock(&adutux_mutex); |
Jiri Slaby | 46c9844 | 2009-03-11 21:47:38 +0100 | [diff] [blame] | 336 | exit: |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 337 | return retval; |
| 338 | } |
| 339 | |
| 340 | static ssize_t adu_read(struct file *file, __user char *buffer, size_t count, |
| 341 | loff_t *ppos) |
| 342 | { |
| 343 | struct adu_device *dev; |
| 344 | size_t bytes_read = 0; |
| 345 | size_t bytes_to_read = count; |
| 346 | int i; |
| 347 | int retval = 0; |
| 348 | int timeout = 0; |
| 349 | int should_submit = 0; |
| 350 | unsigned long flags; |
| 351 | DECLARE_WAITQUEUE(wait, current); |
| 352 | |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 353 | dev = file->private_data; |
Matthias Kaehlcke | 8293c56 | 2007-07-13 21:28:31 +0200 | [diff] [blame] | 354 | if (mutex_lock_interruptible(&dev->mtx)) |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 355 | return -ERESTARTSYS; |
| 356 | |
| 357 | /* verify that the device wasn't unplugged */ |
Pete Zaitcev | f08812d | 2007-10-31 15:59:30 -0700 | [diff] [blame] | 358 | if (dev->udev == NULL) { |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 359 | retval = -ENODEV; |
Greg Kroah-Hartman | fd3f191 | 2008-08-14 09:37:34 -0700 | [diff] [blame] | 360 | printk(KERN_ERR "adutux: No device or device unplugged %d\n", |
| 361 | 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 | fd3f191 | 2008-08-14 09:37:34 -0700 | [diff] [blame] | 530 | printk(KERN_ERR "adutux: No device or device unplugged %d\n", |
| 531 | 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", |
Steven Haigh | 0327063 | 2006-08-09 07:42:06 +1000 | [diff] [blame] | 786 | udev->descriptor.idProduct, dev->serial_number, |
| 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"); |