blob: e8a9e16789f55c98cdf957da0a7b3e1b806a6f83 [file] [log] [blame]
Steven Haigh03270632006-08-09 07:42:06 +10001/*
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 Kaehlcke8293c562007-07-13 21:28:31 +020027#include <linux/mutex.h>
Lisa Nguyen40cf4832013-05-13 12:40:47 -070028#include <linux/uaccess.h>
Steven Haigh03270632006-08-09 07:42:06 +100029
30#ifdef CONFIG_USB_DEBUG
31static int debug = 5;
32#else
33static int debug = 1;
34#endif
35
36/* Use our own dbg macro */
37#undef dbg
Lisa Nguyeneb79c012013-05-13 12:41:10 -070038#define dbg(lvl, format, arg...) \
39do { \
Steven Haigh03270632006-08-09 07:42:06 +100040 if (debug >= lvl) \
Joe Perchesf45ba772010-02-05 17:51:13 -080041 printk(KERN_DEBUG "%s: " format "\n", __FILE__, ##arg); \
Steven Haigh03270632006-08-09 07:42:06 +100042} while (0)
43
44
45/* Version Information */
46#define DRIVER_VERSION "v0.0.13"
47#define DRIVER_AUTHOR "John Homppi"
48#define DRIVER_DESC "adutux (see www.ontrak.net)"
49
50/* Module parameters */
51module_param(debug, int, S_IRUGO | S_IWUSR);
52MODULE_PARM_DESC(debug, "Debug enabled or not");
53
54/* Define these values to match your device */
55#define ADU_VENDOR_ID 0x0a07
56#define ADU_PRODUCT_ID 0x0064
57
58/* table of devices that work with this driver */
Németh Márton33b9e162010-01-10 15:34:45 +010059static const struct usb_device_id device_table[] = {
Steven Haigh03270632006-08-09 07:42:06 +100060 { USB_DEVICE(ADU_VENDOR_ID, ADU_PRODUCT_ID) }, /* ADU100 */
Lisa Nguyeneb79c012013-05-13 12:41:10 -070061 { USB_DEVICE(ADU_VENDOR_ID, ADU_PRODUCT_ID+20) }, /* ADU120 */
62 { USB_DEVICE(ADU_VENDOR_ID, ADU_PRODUCT_ID+30) }, /* ADU130 */
Steven Haigh03270632006-08-09 07:42:06 +100063 { USB_DEVICE(ADU_VENDOR_ID, ADU_PRODUCT_ID+100) }, /* ADU200 */
64 { USB_DEVICE(ADU_VENDOR_ID, ADU_PRODUCT_ID+108) }, /* ADU208 */
65 { USB_DEVICE(ADU_VENDOR_ID, ADU_PRODUCT_ID+118) }, /* ADU218 */
Lisa Nguyen83fc1fc2013-05-13 12:42:21 -070066 { } /* Terminating entry */
Steven Haigh03270632006-08-09 07:42:06 +100067};
68
69MODULE_DEVICE_TABLE(usb, device_table);
70
71#ifdef CONFIG_USB_DYNAMIC_MINORS
72#define ADU_MINOR_BASE 0
73#else
74#define ADU_MINOR_BASE 67
75#endif
76
77/* we can have up to this number of device plugged in at once */
78#define MAX_DEVICES 16
79
80#define COMMAND_TIMEOUT (2*HZ) /* 60 second timeout for a command */
81
Pete Zaitcevf08812d2007-10-31 15:59:30 -070082/*
83 * The locking scheme is a vanilla 3-lock:
84 * adu_device.buflock: A spinlock, covers what IRQs touch.
85 * adutux_mutex: A Static lock to cover open_count. It would also cover
86 * any globals, but we don't have them in 2.6.
87 * adu_device.mtx: A mutex to hold across sleepers like copy_from_user.
88 * It covers all of adu_device, except the open_count
89 * and what .buflock covers.
90 */
91
Steven Haigh03270632006-08-09 07:42:06 +100092/* Structure to hold all of our device specific stuff */
93struct adu_device {
Pete Zaitcevf08812d2007-10-31 15:59:30 -070094 struct mutex mtx;
Lisa Nguyen30c5c642013-05-13 12:41:34 -070095 struct usb_device *udev; /* save off the usb device pointer */
96 struct usb_interface *interface;
Pete Zaitcevf08812d2007-10-31 15:59:30 -070097 unsigned int minor; /* the starting minor number for this device */
Steven Haigh03270632006-08-09 07:42:06 +100098 char serial_number[8];
99
100 int open_count; /* number of times this port has been opened */
101
Lisa Nguyen30c5c642013-05-13 12:41:34 -0700102 char *read_buffer_primary;
Steven Haigh03270632006-08-09 07:42:06 +1000103 int read_buffer_length;
Lisa Nguyen30c5c642013-05-13 12:41:34 -0700104 char *read_buffer_secondary;
Steven Haigh03270632006-08-09 07:42:06 +1000105 int secondary_head;
106 int secondary_tail;
107 spinlock_t buflock;
108
109 wait_queue_head_t read_wait;
110 wait_queue_head_t write_wait;
111
Lisa Nguyen30c5c642013-05-13 12:41:34 -0700112 char *interrupt_in_buffer;
113 struct usb_endpoint_descriptor *interrupt_in_endpoint;
114 struct urb *interrupt_in_urb;
Steven Haigh03270632006-08-09 07:42:06 +1000115 int read_urb_finished;
116
Lisa Nguyen30c5c642013-05-13 12:41:34 -0700117 char *interrupt_out_buffer;
118 struct usb_endpoint_descriptor *interrupt_out_endpoint;
119 struct urb *interrupt_out_urb;
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700120 int out_urb_finished;
Steven Haigh03270632006-08-09 07:42:06 +1000121};
122
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700123static DEFINE_MUTEX(adutux_mutex);
124
Steven Haigh03270632006-08-09 07:42:06 +1000125static struct usb_driver adu_driver;
126
127static void adu_debug_data(int level, const char *function, int size,
128 const unsigned char *data)
129{
130 int i;
131
132 if (debug < level)
133 return;
134
Joe Perchesf45ba772010-02-05 17:51:13 -0800135 printk(KERN_DEBUG "%s: %s - length = %d, data = ",
136 __FILE__, function, size);
Steven Haigh03270632006-08-09 07:42:06 +1000137 for (i = 0; i < size; ++i)
138 printk("%.2x ", data[i]);
139 printk("\n");
140}
141
142/**
143 * adu_abort_transfers
144 * aborts transfers and frees associated data structures
145 */
146static void adu_abort_transfers(struct adu_device *dev)
147{
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700148 unsigned long flags;
Steven Haigh03270632006-08-09 07:42:06 +1000149
Steven Haigh03270632006-08-09 07:42:06 +1000150 if (dev->udev == NULL) {
Lisa Nguyen05d76392013-05-13 12:41:54 -0700151 dbg(1, " %s : udev is null", __func__);
Greg Kroah-Hartman6e42a152013-06-26 16:30:43 -0700152 return;
Steven Haigh03270632006-08-09 07:42:06 +1000153 }
154
Steven Haigh03270632006-08-09 07:42:06 +1000155 /* shutdown transfer */
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700156
157 /* XXX Anchor these instead */
158 spin_lock_irqsave(&dev->buflock, flags);
159 if (!dev->read_urb_finished) {
160 spin_unlock_irqrestore(&dev->buflock, flags);
161 usb_kill_urb(dev->interrupt_in_urb);
162 } else
163 spin_unlock_irqrestore(&dev->buflock, flags);
164
165 spin_lock_irqsave(&dev->buflock, flags);
166 if (!dev->out_urb_finished) {
167 spin_unlock_irqrestore(&dev->buflock, flags);
168 usb_kill_urb(dev->interrupt_out_urb);
169 } else
170 spin_unlock_irqrestore(&dev->buflock, flags);
Steven Haigh03270632006-08-09 07:42:06 +1000171}
172
173static void adu_delete(struct adu_device *dev)
174{
Steven Haigh03270632006-08-09 07:42:06 +1000175 /* free data structures */
176 usb_free_urb(dev->interrupt_in_urb);
177 usb_free_urb(dev->interrupt_out_urb);
178 kfree(dev->read_buffer_primary);
179 kfree(dev->read_buffer_secondary);
180 kfree(dev->interrupt_in_buffer);
181 kfree(dev->interrupt_out_buffer);
182 kfree(dev);
Steven Haigh03270632006-08-09 07:42:06 +1000183}
184
David Howells7d12e782006-10-05 14:55:46 +0100185static void adu_interrupt_in_callback(struct urb *urb)
Steven Haigh03270632006-08-09 07:42:06 +1000186{
187 struct adu_device *dev = urb->context;
Greg Kroah-Hartman24497a02007-07-18 10:58:02 -0700188 int status = urb->status;
Steven Haigh03270632006-08-09 07:42:06 +1000189
Harvey Harrison441b62c2008-03-03 16:08:34 -0800190 adu_debug_data(5, __func__, urb->actual_length,
Steven Haigh03270632006-08-09 07:42:06 +1000191 urb->transfer_buffer);
192
193 spin_lock(&dev->buflock);
194
Greg Kroah-Hartman24497a02007-07-18 10:58:02 -0700195 if (status != 0) {
Oliver Neukumf6c1cea2007-08-16 16:02:08 +0200196 if ((status != -ENOENT) && (status != -ECONNRESET) &&
197 (status != -ESHUTDOWN)) {
Lisa Nguyen05d76392013-05-13 12:41:54 -0700198 dbg(1, " %s : nonzero status received: %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800199 __func__, status);
Steven Haigh03270632006-08-09 07:42:06 +1000200 }
201 goto exit;
202 }
203
204 if (urb->actual_length > 0 && dev->interrupt_in_buffer[0] != 0x00) {
205 if (dev->read_buffer_length <
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700206 (4 * usb_endpoint_maxp(dev->interrupt_in_endpoint)) -
Steven Haigh03270632006-08-09 07:42:06 +1000207 (urb->actual_length)) {
208 memcpy (dev->read_buffer_primary +
209 dev->read_buffer_length,
210 dev->interrupt_in_buffer, urb->actual_length);
211
212 dev->read_buffer_length += urb->actual_length;
Lisa Nguyen05d76392013-05-13 12:41:54 -0700213 dbg(2, " %s reading %d ", __func__,
Steven Haigh03270632006-08-09 07:42:06 +1000214 urb->actual_length);
215 } else {
Lisa Nguyen05d76392013-05-13 12:41:54 -0700216 dbg(1, " %s : read_buffer overflow", __func__);
Steven Haigh03270632006-08-09 07:42:06 +1000217 }
218 }
219
220exit:
221 dev->read_urb_finished = 1;
222 spin_unlock(&dev->buflock);
223 /* always wake up so we recover from errors */
224 wake_up_interruptible(&dev->read_wait);
Harvey Harrison441b62c2008-03-03 16:08:34 -0800225 adu_debug_data(5, __func__, urb->actual_length,
Steven Haigh03270632006-08-09 07:42:06 +1000226 urb->transfer_buffer);
Steven Haigh03270632006-08-09 07:42:06 +1000227}
228
David Howells7d12e782006-10-05 14:55:46 +0100229static void adu_interrupt_out_callback(struct urb *urb)
Steven Haigh03270632006-08-09 07:42:06 +1000230{
231 struct adu_device *dev = urb->context;
Greg Kroah-Hartman24497a02007-07-18 10:58:02 -0700232 int status = urb->status;
Steven Haigh03270632006-08-09 07:42:06 +1000233
Lisa Nguyen05d76392013-05-13 12:41:54 -0700234 adu_debug_data(5, __func__, urb->actual_length, urb->transfer_buffer);
Steven Haigh03270632006-08-09 07:42:06 +1000235
Greg Kroah-Hartman24497a02007-07-18 10:58:02 -0700236 if (status != 0) {
237 if ((status != -ENOENT) &&
238 (status != -ECONNRESET)) {
Steven Haigh03270632006-08-09 07:42:06 +1000239 dbg(1, " %s :nonzero status received: %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800240 __func__, status);
Steven Haigh03270632006-08-09 07:42:06 +1000241 }
242 goto exit;
243 }
244
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700245 spin_lock(&dev->buflock);
246 dev->out_urb_finished = 1;
247 wake_up(&dev->write_wait);
248 spin_unlock(&dev->buflock);
Steven Haigh03270632006-08-09 07:42:06 +1000249exit:
250
Harvey Harrison441b62c2008-03-03 16:08:34 -0800251 adu_debug_data(5, __func__, urb->actual_length,
Steven Haigh03270632006-08-09 07:42:06 +1000252 urb->transfer_buffer);
Steven Haigh03270632006-08-09 07:42:06 +1000253}
254
255static int adu_open(struct inode *inode, struct file *file)
256{
257 struct adu_device *dev = NULL;
258 struct usb_interface *interface;
259 int subminor;
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700260 int retval;
Steven Haigh03270632006-08-09 07:42:06 +1000261
Steven Haigh03270632006-08-09 07:42:06 +1000262 subminor = iminor(inode);
263
Lisa Nguyene77c4e62013-05-15 15:21:07 -0700264 retval = mutex_lock_interruptible(&adutux_mutex);
265 if (retval) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800266 dbg(2, "%s : mutex lock failed", __func__);
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700267 goto exit_no_lock;
268 }
269
Steven Haigh03270632006-08-09 07:42:06 +1000270 interface = usb_find_interface(&adu_driver, subminor);
271 if (!interface) {
Greg Kroah-Hartmanfd3f1912008-08-14 09:37:34 -0700272 printk(KERN_ERR "adutux: %s - error, can't find device for "
273 "minor %d\n", __func__, subminor);
Steven Haigh03270632006-08-09 07:42:06 +1000274 retval = -ENODEV;
275 goto exit_no_device;
276 }
277
278 dev = usb_get_intfdata(interface);
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700279 if (!dev || !dev->udev) {
Steven Haigh03270632006-08-09 07:42:06 +1000280 retval = -ENODEV;
281 goto exit_no_device;
282 }
283
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700284 /* check that nobody else is using the device */
285 if (dev->open_count) {
286 retval = -EBUSY;
Steven Haigh03270632006-08-09 07:42:06 +1000287 goto exit_no_device;
288 }
289
Steven Haigh03270632006-08-09 07:42:06 +1000290 ++dev->open_count;
Lisa Nguyen05d76392013-05-13 12:41:54 -0700291 dbg(2, "%s : open count %d", __func__, dev->open_count);
Steven Haigh03270632006-08-09 07:42:06 +1000292
293 /* save device in the file's private structure */
294 file->private_data = dev;
295
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700296 /* initialize in direction */
297 dev->read_buffer_length = 0;
Steven Haigh03270632006-08-09 07:42:06 +1000298
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700299 /* fixup first read by having urb waiting for it */
Lisa Nguyen05d76392013-05-13 12:41:54 -0700300 usb_fill_int_urb(dev->interrupt_in_urb, dev->udev,
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700301 usb_rcvintpipe(dev->udev,
302 dev->interrupt_in_endpoint->bEndpointAddress),
303 dev->interrupt_in_buffer,
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700304 usb_endpoint_maxp(dev->interrupt_in_endpoint),
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700305 adu_interrupt_in_callback, dev,
306 dev->interrupt_in_endpoint->bInterval);
307 dev->read_urb_finished = 0;
308 if (usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL))
309 dev->read_urb_finished = 1;
310 /* we ignore failure */
311 /* end of fixup for first read */
312
313 /* initialize out direction */
314 dev->out_urb_finished = 1;
315
316 retval = 0;
Steven Haigh03270632006-08-09 07:42:06 +1000317
318exit_no_device:
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700319 mutex_unlock(&adutux_mutex);
320exit_no_lock:
Steven Haigh03270632006-08-09 07:42:06 +1000321 return retval;
322}
323
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700324static void adu_release_internal(struct adu_device *dev)
Steven Haigh03270632006-08-09 07:42:06 +1000325{
Steven Haigh03270632006-08-09 07:42:06 +1000326 /* decrement our usage count for the device */
327 --dev->open_count;
Lisa Nguyen05d76392013-05-13 12:41:54 -0700328 dbg(2, " %s : open count %d", __func__, dev->open_count);
Steven Haigh03270632006-08-09 07:42:06 +1000329 if (dev->open_count <= 0) {
330 adu_abort_transfers(dev);
331 dev->open_count = 0;
332 }
Steven Haigh03270632006-08-09 07:42:06 +1000333}
334
335static int adu_release(struct inode *inode, struct file *file)
336{
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700337 struct adu_device *dev;
Steven Haigh03270632006-08-09 07:42:06 +1000338 int retval = 0;
339
Steven Haigh03270632006-08-09 07:42:06 +1000340 if (file == NULL) {
Lisa Nguyen05d76392013-05-13 12:41:54 -0700341 dbg(1, " %s : file is NULL", __func__);
Steven Haigh03270632006-08-09 07:42:06 +1000342 retval = -ENODEV;
343 goto exit;
344 }
345
346 dev = file->private_data;
Steven Haigh03270632006-08-09 07:42:06 +1000347 if (dev == NULL) {
Lisa Nguyen05d76392013-05-13 12:41:54 -0700348 dbg(1, " %s : object is NULL", __func__);
Steven Haigh03270632006-08-09 07:42:06 +1000349 retval = -ENODEV;
350 goto exit;
351 }
352
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700353 mutex_lock(&adutux_mutex); /* not interruptible */
Steven Haigh03270632006-08-09 07:42:06 +1000354
355 if (dev->open_count <= 0) {
Lisa Nguyen05d76392013-05-13 12:41:54 -0700356 dbg(1, " %s : device not opened", __func__);
Steven Haigh03270632006-08-09 07:42:06 +1000357 retval = -ENODEV;
Jiri Slaby46c98442009-03-11 21:47:38 +0100358 goto unlock;
Steven Haigh03270632006-08-09 07:42:06 +1000359 }
360
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700361 adu_release_internal(dev);
Alan Sternd4ead162007-05-22 11:46:41 -0400362 if (dev->udev == NULL) {
363 /* the device was unplugged before the file was released */
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700364 if (!dev->open_count) /* ... and we're the last user */
365 adu_delete(dev);
Alan Sternd4ead162007-05-22 11:46:41 -0400366 }
Jiri Slaby46c98442009-03-11 21:47:38 +0100367unlock:
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700368 mutex_unlock(&adutux_mutex);
Jiri Slaby46c98442009-03-11 21:47:38 +0100369exit:
Steven Haigh03270632006-08-09 07:42:06 +1000370 return retval;
371}
372
373static ssize_t adu_read(struct file *file, __user char *buffer, size_t count,
374 loff_t *ppos)
375{
376 struct adu_device *dev;
377 size_t bytes_read = 0;
378 size_t bytes_to_read = count;
379 int i;
380 int retval = 0;
381 int timeout = 0;
382 int should_submit = 0;
383 unsigned long flags;
384 DECLARE_WAITQUEUE(wait, current);
385
Steven Haigh03270632006-08-09 07:42:06 +1000386 dev = file->private_data;
Matthias Kaehlcke8293c562007-07-13 21:28:31 +0200387 if (mutex_lock_interruptible(&dev->mtx))
Steven Haigh03270632006-08-09 07:42:06 +1000388 return -ERESTARTSYS;
389
390 /* verify that the device wasn't unplugged */
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700391 if (dev->udev == NULL) {
Steven Haigh03270632006-08-09 07:42:06 +1000392 retval = -ENODEV;
Greg Kroah-Hartmanfd3f1912008-08-14 09:37:34 -0700393 printk(KERN_ERR "adutux: No device or device unplugged %d\n",
394 retval);
Steven Haigh03270632006-08-09 07:42:06 +1000395 goto exit;
396 }
397
398 /* verify that some data was requested */
399 if (count == 0) {
Lisa Nguyen05d76392013-05-13 12:41:54 -0700400 dbg(1, " %s : read request of 0 bytes", __func__);
Steven Haigh03270632006-08-09 07:42:06 +1000401 goto exit;
402 }
403
404 timeout = COMMAND_TIMEOUT;
Lisa Nguyen05d76392013-05-13 12:41:54 -0700405 dbg(2, " %s : about to start looping", __func__);
Steven Haigh03270632006-08-09 07:42:06 +1000406 while (bytes_to_read) {
407 int data_in_secondary = dev->secondary_tail - dev->secondary_head;
Lisa Nguyen05d76392013-05-13 12:41:54 -0700408 dbg(2, " %s : while, data_in_secondary=%d, status=%d",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800409 __func__, data_in_secondary,
Steven Haigh03270632006-08-09 07:42:06 +1000410 dev->interrupt_in_urb->status);
411
412 if (data_in_secondary) {
413 /* drain secondary buffer */
414 int amount = bytes_to_read < data_in_secondary ? bytes_to_read : data_in_secondary;
415 i = copy_to_user(buffer, dev->read_buffer_secondary+dev->secondary_head, amount);
Kulikov Vasiliy1865a9c2010-07-31 21:40:07 +0400416 if (i) {
Steven Haigh03270632006-08-09 07:42:06 +1000417 retval = -EFAULT;
418 goto exit;
419 }
420 dev->secondary_head += (amount - i);
421 bytes_read += (amount - i);
422 bytes_to_read -= (amount - i);
423 if (i) {
424 retval = bytes_read ? bytes_read : -EFAULT;
425 goto exit;
426 }
427 } else {
428 /* we check the primary buffer */
429 spin_lock_irqsave (&dev->buflock, flags);
430 if (dev->read_buffer_length) {
431 /* we secure access to the primary */
432 char *tmp;
Lisa Nguyen05d76392013-05-13 12:41:54 -0700433 dbg(2, " %s : swap, read_buffer_length = %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800434 __func__, dev->read_buffer_length);
Steven Haigh03270632006-08-09 07:42:06 +1000435 tmp = dev->read_buffer_secondary;
436 dev->read_buffer_secondary = dev->read_buffer_primary;
437 dev->read_buffer_primary = tmp;
438 dev->secondary_head = 0;
439 dev->secondary_tail = dev->read_buffer_length;
440 dev->read_buffer_length = 0;
441 spin_unlock_irqrestore(&dev->buflock, flags);
442 /* we have a free buffer so use it */
443 should_submit = 1;
444 } else {
445 /* even the primary was empty - we may need to do IO */
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700446 if (!dev->read_urb_finished) {
Steven Haigh03270632006-08-09 07:42:06 +1000447 /* somebody is doing IO */
448 spin_unlock_irqrestore(&dev->buflock, flags);
Lisa Nguyen05d76392013-05-13 12:41:54 -0700449 dbg(2, " %s : submitted already", __func__);
Steven Haigh03270632006-08-09 07:42:06 +1000450 } else {
451 /* we must initiate input */
Lisa Nguyen05d76392013-05-13 12:41:54 -0700452 dbg(2, " %s : initiate input", __func__);
Steven Haigh03270632006-08-09 07:42:06 +1000453 dev->read_urb_finished = 0;
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700454 spin_unlock_irqrestore(&dev->buflock, flags);
Steven Haigh03270632006-08-09 07:42:06 +1000455
Lisa Nguyen05d76392013-05-13 12:41:54 -0700456 usb_fill_int_urb(dev->interrupt_in_urb, dev->udev,
Lisa Nguyeneb79c012013-05-13 12:41:10 -0700457 usb_rcvintpipe(dev->udev,
458 dev->interrupt_in_endpoint->bEndpointAddress),
Steven Haigh03270632006-08-09 07:42:06 +1000459 dev->interrupt_in_buffer,
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700460 usb_endpoint_maxp(dev->interrupt_in_endpoint),
Steven Haigh03270632006-08-09 07:42:06 +1000461 adu_interrupt_in_callback,
462 dev,
463 dev->interrupt_in_endpoint->bInterval);
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700464 retval = usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL);
465 if (retval) {
466 dev->read_urb_finished = 1;
Steven Haigh03270632006-08-09 07:42:06 +1000467 if (retval == -ENOMEM) {
468 retval = bytes_read ? bytes_read : -ENOMEM;
469 }
Lisa Nguyen05d76392013-05-13 12:41:54 -0700470 dbg(2, " %s : submit failed", __func__);
Steven Haigh03270632006-08-09 07:42:06 +1000471 goto exit;
472 }
473 }
474
475 /* we wait for I/O to complete */
476 set_current_state(TASK_INTERRUPTIBLE);
477 add_wait_queue(&dev->read_wait, &wait);
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700478 spin_lock_irqsave(&dev->buflock, flags);
479 if (!dev->read_urb_finished) {
480 spin_unlock_irqrestore(&dev->buflock, flags);
Steven Haigh03270632006-08-09 07:42:06 +1000481 timeout = schedule_timeout(COMMAND_TIMEOUT);
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700482 } else {
483 spin_unlock_irqrestore(&dev->buflock, flags);
Steven Haigh03270632006-08-09 07:42:06 +1000484 set_current_state(TASK_RUNNING);
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700485 }
Steven Haigh03270632006-08-09 07:42:06 +1000486 remove_wait_queue(&dev->read_wait, &wait);
487
488 if (timeout <= 0) {
Lisa Nguyen05d76392013-05-13 12:41:54 -0700489 dbg(2, " %s : timeout", __func__);
Steven Haigh03270632006-08-09 07:42:06 +1000490 retval = bytes_read ? bytes_read : -ETIMEDOUT;
491 goto exit;
492 }
493
494 if (signal_pending(current)) {
Lisa Nguyen05d76392013-05-13 12:41:54 -0700495 dbg(2, " %s : signal pending", __func__);
Steven Haigh03270632006-08-09 07:42:06 +1000496 retval = bytes_read ? bytes_read : -EINTR;
497 goto exit;
498 }
499 }
500 }
501 }
502
503 retval = bytes_read;
504 /* if the primary buffer is empty then use it */
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700505 spin_lock_irqsave(&dev->buflock, flags);
506 if (should_submit && dev->read_urb_finished) {
507 dev->read_urb_finished = 0;
508 spin_unlock_irqrestore(&dev->buflock, flags);
Lisa Nguyen05d76392013-05-13 12:41:54 -0700509 usb_fill_int_urb(dev->interrupt_in_urb, dev->udev,
Steven Haigh03270632006-08-09 07:42:06 +1000510 usb_rcvintpipe(dev->udev,
Lisa Nguyeneb79c012013-05-13 12:41:10 -0700511 dev->interrupt_in_endpoint->bEndpointAddress),
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700512 dev->interrupt_in_buffer,
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700513 usb_endpoint_maxp(dev->interrupt_in_endpoint),
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700514 adu_interrupt_in_callback,
515 dev,
516 dev->interrupt_in_endpoint->bInterval);
517 if (usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL) != 0)
518 dev->read_urb_finished = 1;
Steven Haigh03270632006-08-09 07:42:06 +1000519 /* we ignore failure */
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700520 } else {
521 spin_unlock_irqrestore(&dev->buflock, flags);
Steven Haigh03270632006-08-09 07:42:06 +1000522 }
523
524exit:
525 /* unlock the device */
Matthias Kaehlcke8293c562007-07-13 21:28:31 +0200526 mutex_unlock(&dev->mtx);
Steven Haigh03270632006-08-09 07:42:06 +1000527
Steven Haigh03270632006-08-09 07:42:06 +1000528 return retval;
529}
530
531static ssize_t adu_write(struct file *file, const __user char *buffer,
532 size_t count, loff_t *ppos)
533{
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700534 DECLARE_WAITQUEUE(waita, current);
Steven Haigh03270632006-08-09 07:42:06 +1000535 struct adu_device *dev;
536 size_t bytes_written = 0;
537 size_t bytes_to_write;
538 size_t buffer_size;
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700539 unsigned long flags;
Oliver Neukumebc3ac12007-04-02 15:16:36 +0200540 int retval;
Steven Haigh03270632006-08-09 07:42:06 +1000541
Steven Haigh03270632006-08-09 07:42:06 +1000542 dev = file->private_data;
543
Matthias Kaehlcke8293c562007-07-13 21:28:31 +0200544 retval = mutex_lock_interruptible(&dev->mtx);
Oliver Neukumebc3ac12007-04-02 15:16:36 +0200545 if (retval)
546 goto exit_nolock;
Steven Haigh03270632006-08-09 07:42:06 +1000547
548 /* verify that the device wasn't unplugged */
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700549 if (dev->udev == NULL) {
Steven Haigh03270632006-08-09 07:42:06 +1000550 retval = -ENODEV;
Greg Kroah-Hartmanfd3f1912008-08-14 09:37:34 -0700551 printk(KERN_ERR "adutux: No device or device unplugged %d\n",
552 retval);
Steven Haigh03270632006-08-09 07:42:06 +1000553 goto exit;
554 }
555
556 /* verify that we actually have some data to write */
557 if (count == 0) {
Lisa Nguyen05d76392013-05-13 12:41:54 -0700558 dbg(1, " %s : write request of 0 bytes", __func__);
Steven Haigh03270632006-08-09 07:42:06 +1000559 goto exit;
560 }
561
Steven Haigh03270632006-08-09 07:42:06 +1000562 while (count > 0) {
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700563 add_wait_queue(&dev->write_wait, &waita);
564 set_current_state(TASK_INTERRUPTIBLE);
565 spin_lock_irqsave(&dev->buflock, flags);
566 if (!dev->out_urb_finished) {
567 spin_unlock_irqrestore(&dev->buflock, flags);
Steven Haigh03270632006-08-09 07:42:06 +1000568
Matthias Kaehlcke8293c562007-07-13 21:28:31 +0200569 mutex_unlock(&dev->mtx);
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700570 if (signal_pending(current)) {
Lisa Nguyen05d76392013-05-13 12:41:54 -0700571 dbg(1, " %s : interrupted", __func__);
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700572 set_current_state(TASK_RUNNING);
573 retval = -EINTR;
574 goto exit_onqueue;
575 }
576 if (schedule_timeout(COMMAND_TIMEOUT) == 0) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800577 dbg(1, "%s - command timed out.", __func__);
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700578 retval = -ETIMEDOUT;
579 goto exit_onqueue;
580 }
581 remove_wait_queue(&dev->write_wait, &waita);
Matthias Kaehlcke8293c562007-07-13 21:28:31 +0200582 retval = mutex_lock_interruptible(&dev->mtx);
Oliver Neukumebc3ac12007-04-02 15:16:36 +0200583 if (retval) {
584 retval = bytes_written ? bytes_written : retval;
585 goto exit_nolock;
586 }
Steven Haigh03270632006-08-09 07:42:06 +1000587
Lisa Nguyen05d76392013-05-13 12:41:54 -0700588 dbg(4, " %s : in progress, count = %Zd", __func__, count);
Steven Haigh03270632006-08-09 07:42:06 +1000589 } else {
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700590 spin_unlock_irqrestore(&dev->buflock, flags);
591 set_current_state(TASK_RUNNING);
592 remove_wait_queue(&dev->write_wait, &waita);
Lisa Nguyen05d76392013-05-13 12:41:54 -0700593 dbg(4, " %s : sending, count = %Zd", __func__, count);
Steven Haigh03270632006-08-09 07:42:06 +1000594
595 /* write the data into interrupt_out_buffer from userspace */
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700596 buffer_size = usb_endpoint_maxp(dev->interrupt_out_endpoint);
Steven Haigh03270632006-08-09 07:42:06 +1000597 bytes_to_write = count > buffer_size ? buffer_size : count;
Lisa Nguyen05d76392013-05-13 12:41:54 -0700598 dbg(4, " %s : buffer_size = %Zd, count = %Zd, bytes_to_write = %Zd",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800599 __func__, buffer_size, count, bytes_to_write);
Steven Haigh03270632006-08-09 07:42:06 +1000600
601 if (copy_from_user(dev->interrupt_out_buffer, buffer, bytes_to_write) != 0) {
602 retval = -EFAULT;
603 goto exit;
604 }
605
606 /* send off the urb */
607 usb_fill_int_urb(
608 dev->interrupt_out_urb,
609 dev->udev,
610 usb_sndintpipe(dev->udev, dev->interrupt_out_endpoint->bEndpointAddress),
611 dev->interrupt_out_buffer,
612 bytes_to_write,
613 adu_interrupt_out_callback,
614 dev,
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700615 dev->interrupt_out_endpoint->bInterval);
Steven Haigh03270632006-08-09 07:42:06 +1000616 dev->interrupt_out_urb->actual_length = bytes_to_write;
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700617 dev->out_urb_finished = 0;
Steven Haigh03270632006-08-09 07:42:06 +1000618 retval = usb_submit_urb(dev->interrupt_out_urb, GFP_KERNEL);
619 if (retval < 0) {
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700620 dev->out_urb_finished = 1;
Greg Kroah-Hartmanfd3f1912008-08-14 09:37:34 -0700621 dev_err(&dev->udev->dev, "Couldn't submit "
622 "interrupt_out_urb %d\n", retval);
Steven Haigh03270632006-08-09 07:42:06 +1000623 goto exit;
624 }
625
626 buffer += bytes_to_write;
627 count -= bytes_to_write;
628
629 bytes_written += bytes_to_write;
630 }
631 }
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700632 mutex_unlock(&dev->mtx);
633 return bytes_written;
Steven Haigh03270632006-08-09 07:42:06 +1000634
635exit:
Matthias Kaehlcke8293c562007-07-13 21:28:31 +0200636 mutex_unlock(&dev->mtx);
Oliver Neukumebc3ac12007-04-02 15:16:36 +0200637exit_nolock:
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700638 return retval;
Steven Haigh03270632006-08-09 07:42:06 +1000639
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700640exit_onqueue:
641 remove_wait_queue(&dev->write_wait, &waita);
Steven Haigh03270632006-08-09 07:42:06 +1000642 return retval;
643}
644
645/* file operations needed when we register this driver */
Arjan van de Ven00977a52007-02-12 00:55:34 -0800646static const struct file_operations adu_fops = {
Steven Haigh03270632006-08-09 07:42:06 +1000647 .owner = THIS_MODULE,
648 .read = adu_read,
649 .write = adu_write,
650 .open = adu_open,
651 .release = adu_release,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200652 .llseek = noop_llseek,
Steven Haigh03270632006-08-09 07:42:06 +1000653};
654
655/*
656 * usb class driver info in order to get a minor number from the usb core,
657 * and to have the device registered with devfs and the driver core
658 */
659static struct usb_class_driver adu_class = {
660 .name = "usb/adutux%d",
661 .fops = &adu_fops,
662 .minor_base = ADU_MINOR_BASE,
663};
664
665/**
666 * adu_probe
667 *
668 * Called by the usb core when a new device is connected that it thinks
669 * this driver might be interested in.
670 */
671static int adu_probe(struct usb_interface *interface,
672 const struct usb_device_id *id)
673{
674 struct usb_device *udev = interface_to_usbdev(interface);
675 struct adu_device *dev = NULL;
676 struct usb_host_interface *iface_desc;
677 struct usb_endpoint_descriptor *endpoint;
678 int retval = -ENODEV;
679 int in_end_size;
680 int out_end_size;
681 int i;
682
Steven Haigh03270632006-08-09 07:42:06 +1000683 if (udev == NULL) {
684 dev_err(&interface->dev, "udev is NULL.\n");
685 goto exit;
686 }
687
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400688 /* allocate memory for our device state and initialize it */
Steven Haigh03270632006-08-09 07:42:06 +1000689 dev = kzalloc(sizeof(struct adu_device), GFP_KERNEL);
690 if (dev == NULL) {
691 dev_err(&interface->dev, "Out of memory\n");
692 retval = -ENOMEM;
693 goto exit;
694 }
695
Matthias Kaehlcke8293c562007-07-13 21:28:31 +0200696 mutex_init(&dev->mtx);
Steven Haigh03270632006-08-09 07:42:06 +1000697 spin_lock_init(&dev->buflock);
698 dev->udev = udev;
699 init_waitqueue_head(&dev->read_wait);
700 init_waitqueue_head(&dev->write_wait);
701
702 iface_desc = &interface->altsetting[0];
703
704 /* set up the endpoint information */
705 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
706 endpoint = &iface_desc->endpoint[i].desc;
707
708 if (usb_endpoint_is_int_in(endpoint))
709 dev->interrupt_in_endpoint = endpoint;
710
711 if (usb_endpoint_is_int_out(endpoint))
712 dev->interrupt_out_endpoint = endpoint;
713 }
714 if (dev->interrupt_in_endpoint == NULL) {
715 dev_err(&interface->dev, "interrupt in endpoint not found\n");
716 goto error;
717 }
718 if (dev->interrupt_out_endpoint == NULL) {
719 dev_err(&interface->dev, "interrupt out endpoint not found\n");
720 goto error;
721 }
722
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700723 in_end_size = usb_endpoint_maxp(dev->interrupt_in_endpoint);
724 out_end_size = usb_endpoint_maxp(dev->interrupt_out_endpoint);
Steven Haigh03270632006-08-09 07:42:06 +1000725
726 dev->read_buffer_primary = kmalloc((4 * in_end_size), GFP_KERNEL);
727 if (!dev->read_buffer_primary) {
728 dev_err(&interface->dev, "Couldn't allocate read_buffer_primary\n");
729 retval = -ENOMEM;
730 goto error;
731 }
732
733 /* debug code prime the buffer */
734 memset(dev->read_buffer_primary, 'a', in_end_size);
735 memset(dev->read_buffer_primary + in_end_size, 'b', in_end_size);
736 memset(dev->read_buffer_primary + (2 * in_end_size), 'c', in_end_size);
737 memset(dev->read_buffer_primary + (3 * in_end_size), 'd', in_end_size);
738
739 dev->read_buffer_secondary = kmalloc((4 * in_end_size), GFP_KERNEL);
740 if (!dev->read_buffer_secondary) {
741 dev_err(&interface->dev, "Couldn't allocate read_buffer_secondary\n");
742 retval = -ENOMEM;
743 goto error;
744 }
745
746 /* debug code prime the buffer */
747 memset(dev->read_buffer_secondary, 'e', in_end_size);
748 memset(dev->read_buffer_secondary + in_end_size, 'f', in_end_size);
749 memset(dev->read_buffer_secondary + (2 * in_end_size), 'g', in_end_size);
750 memset(dev->read_buffer_secondary + (3 * in_end_size), 'h', in_end_size);
751
752 dev->interrupt_in_buffer = kmalloc(in_end_size, GFP_KERNEL);
753 if (!dev->interrupt_in_buffer) {
754 dev_err(&interface->dev, "Couldn't allocate interrupt_in_buffer\n");
755 goto error;
756 }
757
758 /* debug code prime the buffer */
759 memset(dev->interrupt_in_buffer, 'i', in_end_size);
760
761 dev->interrupt_in_urb = usb_alloc_urb(0, GFP_KERNEL);
762 if (!dev->interrupt_in_urb) {
763 dev_err(&interface->dev, "Couldn't allocate interrupt_in_urb\n");
764 goto error;
765 }
766 dev->interrupt_out_buffer = kmalloc(out_end_size, GFP_KERNEL);
767 if (!dev->interrupt_out_buffer) {
768 dev_err(&interface->dev, "Couldn't allocate interrupt_out_buffer\n");
769 goto error;
770 }
771 dev->interrupt_out_urb = usb_alloc_urb(0, GFP_KERNEL);
772 if (!dev->interrupt_out_urb) {
773 dev_err(&interface->dev, "Couldn't allocate interrupt_out_urb\n");
774 goto error;
775 }
776
777 if (!usb_string(udev, udev->descriptor.iSerialNumber, dev->serial_number,
778 sizeof(dev->serial_number))) {
779 dev_err(&interface->dev, "Could not retrieve serial number\n");
780 goto error;
781 }
Lisa Nguyen05d76392013-05-13 12:41:54 -0700782 dbg(2, " %s : serial_number=%s", __func__, dev->serial_number);
Steven Haigh03270632006-08-09 07:42:06 +1000783
784 /* we can register the device now, as it is ready */
785 usb_set_intfdata(interface, dev);
786
787 retval = usb_register_dev(interface, &adu_class);
788
789 if (retval) {
790 /* something prevented us from registering this driver */
791 dev_err(&interface->dev, "Not able to get a minor for this device.\n");
792 usb_set_intfdata(interface, NULL);
793 goto error;
794 }
795
796 dev->minor = interface->minor;
797
798 /* let the user know what node this device is now attached to */
Joe Perches898eb712007-10-18 03:06:30 -0700799 dev_info(&interface->dev, "ADU%d %s now attached to /dev/usb/adutux%d\n",
Steven Haigh03270632006-08-09 07:42:06 +1000800 udev->descriptor.idProduct, dev->serial_number,
801 (dev->minor - ADU_MINOR_BASE));
802exit:
Steven Haigh03270632006-08-09 07:42:06 +1000803 return retval;
804
805error:
806 adu_delete(dev);
807 return retval;
808}
809
810/**
811 * adu_disconnect
812 *
813 * Called by the usb core when the device is removed from the system.
814 */
815static void adu_disconnect(struct usb_interface *interface)
816{
817 struct adu_device *dev;
818 int minor;
819
Steven Haigh03270632006-08-09 07:42:06 +1000820 dev = usb_get_intfdata(interface);
Steven Haigh03270632006-08-09 07:42:06 +1000821
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700822 mutex_lock(&dev->mtx); /* not interruptible */
823 dev->udev = NULL; /* poison */
Steven Haigh03270632006-08-09 07:42:06 +1000824 minor = dev->minor;
Steven Haigh03270632006-08-09 07:42:06 +1000825 usb_deregister_dev(interface, &adu_class);
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700826 mutex_unlock(&dev->mtx);
Steven Haigh03270632006-08-09 07:42:06 +1000827
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700828 mutex_lock(&adutux_mutex);
829 usb_set_intfdata(interface, NULL);
Alan Sternd4ead162007-05-22 11:46:41 -0400830
Steven Haigh03270632006-08-09 07:42:06 +1000831 /* if the device is not opened, then we clean up right now */
Lisa Nguyen05d76392013-05-13 12:41:54 -0700832 dbg(2, " %s : open count %d", __func__, dev->open_count);
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700833 if (!dev->open_count)
Steven Haigh03270632006-08-09 07:42:06 +1000834 adu_delete(dev);
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700835
836 mutex_unlock(&adutux_mutex);
Steven Haigh03270632006-08-09 07:42:06 +1000837
Joe Perches898eb712007-10-18 03:06:30 -0700838 dev_info(&interface->dev, "ADU device adutux%d now disconnected\n",
Steven Haigh03270632006-08-09 07:42:06 +1000839 (minor - ADU_MINOR_BASE));
Steven Haigh03270632006-08-09 07:42:06 +1000840}
841
842/* usb specific object needed to register this driver with the usb subsystem */
843static struct usb_driver adu_driver = {
844 .name = "adutux",
845 .probe = adu_probe,
846 .disconnect = adu_disconnect,
847 .id_table = device_table,
848};
849
Greg Kroah-Hartman65db4302011-11-18 09:34:02 -0800850module_usb_driver(adu_driver);
Steven Haigh03270632006-08-09 07:42:06 +1000851
852MODULE_AUTHOR(DRIVER_AUTHOR);
853MODULE_DESCRIPTION(DRIVER_DESC);
854MODULE_LICENSE("GPL");