blob: 885f99322a84f075fe76755c50172c7fdb06c53c [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
Greg Kroah-Hartman66d4bc32013-06-26 16:30:44 -0700150 if (dev->udev == NULL)
Greg Kroah-Hartman6e42a152013-06-26 16:30:43 -0700151 return;
Steven Haigh03270632006-08-09 07:42:06 +1000152
Steven Haigh03270632006-08-09 07:42:06 +1000153 /* shutdown transfer */
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700154
155 /* XXX Anchor these instead */
156 spin_lock_irqsave(&dev->buflock, flags);
157 if (!dev->read_urb_finished) {
158 spin_unlock_irqrestore(&dev->buflock, flags);
159 usb_kill_urb(dev->interrupt_in_urb);
160 } else
161 spin_unlock_irqrestore(&dev->buflock, flags);
162
163 spin_lock_irqsave(&dev->buflock, flags);
164 if (!dev->out_urb_finished) {
165 spin_unlock_irqrestore(&dev->buflock, flags);
166 usb_kill_urb(dev->interrupt_out_urb);
167 } else
168 spin_unlock_irqrestore(&dev->buflock, flags);
Steven Haigh03270632006-08-09 07:42:06 +1000169}
170
171static void adu_delete(struct adu_device *dev)
172{
Steven Haigh03270632006-08-09 07:42:06 +1000173 /* free data structures */
174 usb_free_urb(dev->interrupt_in_urb);
175 usb_free_urb(dev->interrupt_out_urb);
176 kfree(dev->read_buffer_primary);
177 kfree(dev->read_buffer_secondary);
178 kfree(dev->interrupt_in_buffer);
179 kfree(dev->interrupt_out_buffer);
180 kfree(dev);
Steven Haigh03270632006-08-09 07:42:06 +1000181}
182
David Howells7d12e782006-10-05 14:55:46 +0100183static void adu_interrupt_in_callback(struct urb *urb)
Steven Haigh03270632006-08-09 07:42:06 +1000184{
185 struct adu_device *dev = urb->context;
Greg Kroah-Hartman24497a02007-07-18 10:58:02 -0700186 int status = urb->status;
Steven Haigh03270632006-08-09 07:42:06 +1000187
Harvey Harrison441b62c2008-03-03 16:08:34 -0800188 adu_debug_data(5, __func__, urb->actual_length,
Steven Haigh03270632006-08-09 07:42:06 +1000189 urb->transfer_buffer);
190
191 spin_lock(&dev->buflock);
192
Greg Kroah-Hartman24497a02007-07-18 10:58:02 -0700193 if (status != 0) {
Oliver Neukumf6c1cea2007-08-16 16:02:08 +0200194 if ((status != -ENOENT) && (status != -ECONNRESET) &&
195 (status != -ESHUTDOWN)) {
Greg Kroah-Hartman66d4bc32013-06-26 16:30:44 -0700196 dev_dbg(&dev->udev->dev,
197 "%s : nonzero status received: %d\n",
198 __func__, status);
Steven Haigh03270632006-08-09 07:42:06 +1000199 }
200 goto exit;
201 }
202
203 if (urb->actual_length > 0 && dev->interrupt_in_buffer[0] != 0x00) {
204 if (dev->read_buffer_length <
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700205 (4 * usb_endpoint_maxp(dev->interrupt_in_endpoint)) -
Steven Haigh03270632006-08-09 07:42:06 +1000206 (urb->actual_length)) {
207 memcpy (dev->read_buffer_primary +
208 dev->read_buffer_length,
209 dev->interrupt_in_buffer, urb->actual_length);
210
211 dev->read_buffer_length += urb->actual_length;
Greg Kroah-Hartman66d4bc32013-06-26 16:30:44 -0700212 dev_dbg(&dev->udev->dev,"%s reading %d\n", __func__,
213 urb->actual_length);
Steven Haigh03270632006-08-09 07:42:06 +1000214 } else {
Greg Kroah-Hartman66d4bc32013-06-26 16:30:44 -0700215 dev_dbg(&dev->udev->dev,"%s : read_buffer overflow\n",
216 __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)) {
Greg Kroah-Hartman66d4bc32013-06-26 16:30:44 -0700239 dev_dbg(&dev->udev->dev,
240 "%s :nonzero status received: %d\n", __func__,
241 status);
Steven Haigh03270632006-08-09 07:42:06 +1000242 }
243 goto exit;
244 }
245
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700246 spin_lock(&dev->buflock);
247 dev->out_urb_finished = 1;
248 wake_up(&dev->write_wait);
249 spin_unlock(&dev->buflock);
Steven Haigh03270632006-08-09 07:42:06 +1000250exit:
251
Harvey Harrison441b62c2008-03-03 16:08:34 -0800252 adu_debug_data(5, __func__, urb->actual_length,
Steven Haigh03270632006-08-09 07:42:06 +1000253 urb->transfer_buffer);
Steven Haigh03270632006-08-09 07:42:06 +1000254}
255
256static int adu_open(struct inode *inode, struct file *file)
257{
258 struct adu_device *dev = NULL;
259 struct usb_interface *interface;
260 int subminor;
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700261 int retval;
Steven Haigh03270632006-08-09 07:42:06 +1000262
Steven Haigh03270632006-08-09 07:42:06 +1000263 subminor = iminor(inode);
264
Lisa Nguyene77c4e62013-05-15 15:21:07 -0700265 retval = mutex_lock_interruptible(&adutux_mutex);
Greg Kroah-Hartman66d4bc32013-06-26 16:30:44 -0700266 if (retval)
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700267 goto exit_no_lock;
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700268
Steven Haigh03270632006-08-09 07:42:06 +1000269 interface = usb_find_interface(&adu_driver, subminor);
270 if (!interface) {
Greg Kroah-Hartmanfd3f1912008-08-14 09:37:34 -0700271 printk(KERN_ERR "adutux: %s - error, can't find device for "
272 "minor %d\n", __func__, subminor);
Steven Haigh03270632006-08-09 07:42:06 +1000273 retval = -ENODEV;
274 goto exit_no_device;
275 }
276
277 dev = usb_get_intfdata(interface);
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700278 if (!dev || !dev->udev) {
Steven Haigh03270632006-08-09 07:42:06 +1000279 retval = -ENODEV;
280 goto exit_no_device;
281 }
282
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700283 /* check that nobody else is using the device */
284 if (dev->open_count) {
285 retval = -EBUSY;
Steven Haigh03270632006-08-09 07:42:06 +1000286 goto exit_no_device;
287 }
288
Steven Haigh03270632006-08-09 07:42:06 +1000289 ++dev->open_count;
Greg Kroah-Hartman66d4bc32013-06-26 16:30:44 -0700290 dev_dbg(&dev->udev->dev, "%s: open count %d\n", __func__,
291 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;
Greg Kroah-Hartman66d4bc32013-06-26 16:30:44 -0700328 dev_dbg(&dev->udev->dev, "%s : open count %d\n", __func__,
329 dev->open_count);
Steven Haigh03270632006-08-09 07:42:06 +1000330 if (dev->open_count <= 0) {
331 adu_abort_transfers(dev);
332 dev->open_count = 0;
333 }
Steven Haigh03270632006-08-09 07:42:06 +1000334}
335
336static int adu_release(struct inode *inode, struct file *file)
337{
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700338 struct adu_device *dev;
Steven Haigh03270632006-08-09 07:42:06 +1000339 int retval = 0;
340
Steven Haigh03270632006-08-09 07:42:06 +1000341 if (file == NULL) {
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) {
Steven Haigh03270632006-08-09 07:42:06 +1000348 retval = -ENODEV;
349 goto exit;
350 }
351
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700352 mutex_lock(&adutux_mutex); /* not interruptible */
Steven Haigh03270632006-08-09 07:42:06 +1000353
354 if (dev->open_count <= 0) {
Greg Kroah-Hartman66d4bc32013-06-26 16:30:44 -0700355 dev_dbg(&dev->udev->dev, "%s : device not opened\n", __func__);
Steven Haigh03270632006-08-09 07:42:06 +1000356 retval = -ENODEV;
Jiri Slaby46c98442009-03-11 21:47:38 +0100357 goto unlock;
Steven Haigh03270632006-08-09 07:42:06 +1000358 }
359
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700360 adu_release_internal(dev);
Alan Sternd4ead162007-05-22 11:46:41 -0400361 if (dev->udev == NULL) {
362 /* the device was unplugged before the file was released */
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700363 if (!dev->open_count) /* ... and we're the last user */
364 adu_delete(dev);
Alan Sternd4ead162007-05-22 11:46:41 -0400365 }
Jiri Slaby46c98442009-03-11 21:47:38 +0100366unlock:
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700367 mutex_unlock(&adutux_mutex);
Jiri Slaby46c98442009-03-11 21:47:38 +0100368exit:
Steven Haigh03270632006-08-09 07:42:06 +1000369 return retval;
370}
371
372static ssize_t adu_read(struct file *file, __user char *buffer, size_t count,
373 loff_t *ppos)
374{
375 struct adu_device *dev;
376 size_t bytes_read = 0;
377 size_t bytes_to_read = count;
378 int i;
379 int retval = 0;
380 int timeout = 0;
381 int should_submit = 0;
382 unsigned long flags;
383 DECLARE_WAITQUEUE(wait, current);
384
Steven Haigh03270632006-08-09 07:42:06 +1000385 dev = file->private_data;
Matthias Kaehlcke8293c562007-07-13 21:28:31 +0200386 if (mutex_lock_interruptible(&dev->mtx))
Steven Haigh03270632006-08-09 07:42:06 +1000387 return -ERESTARTSYS;
388
389 /* verify that the device wasn't unplugged */
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700390 if (dev->udev == NULL) {
Steven Haigh03270632006-08-09 07:42:06 +1000391 retval = -ENODEV;
Greg Kroah-Hartmanfd3f1912008-08-14 09:37:34 -0700392 printk(KERN_ERR "adutux: No device or device unplugged %d\n",
393 retval);
Steven Haigh03270632006-08-09 07:42:06 +1000394 goto exit;
395 }
396
397 /* verify that some data was requested */
398 if (count == 0) {
Greg Kroah-Hartman66d4bc32013-06-26 16:30:44 -0700399 dev_dbg(&dev->udev->dev, "%s : read request of 0 bytes\n",
400 __func__);
Steven Haigh03270632006-08-09 07:42:06 +1000401 goto exit;
402 }
403
404 timeout = COMMAND_TIMEOUT;
Greg Kroah-Hartman66d4bc32013-06-26 16:30:44 -0700405 dev_dbg(&dev->udev->dev, "%s : about to start looping\n", __func__);
Steven Haigh03270632006-08-09 07:42:06 +1000406 while (bytes_to_read) {
407 int data_in_secondary = dev->secondary_tail - dev->secondary_head;
Greg Kroah-Hartman66d4bc32013-06-26 16:30:44 -0700408 dev_dbg(&dev->udev->dev,
409 "%s : while, data_in_secondary=%d, status=%d\n",
410 __func__, data_in_secondary,
411 dev->interrupt_in_urb->status);
Steven Haigh03270632006-08-09 07:42:06 +1000412
413 if (data_in_secondary) {
414 /* drain secondary buffer */
415 int amount = bytes_to_read < data_in_secondary ? bytes_to_read : data_in_secondary;
416 i = copy_to_user(buffer, dev->read_buffer_secondary+dev->secondary_head, amount);
Kulikov Vasiliy1865a9c2010-07-31 21:40:07 +0400417 if (i) {
Steven Haigh03270632006-08-09 07:42:06 +1000418 retval = -EFAULT;
419 goto exit;
420 }
421 dev->secondary_head += (amount - i);
422 bytes_read += (amount - i);
423 bytes_to_read -= (amount - i);
424 if (i) {
425 retval = bytes_read ? bytes_read : -EFAULT;
426 goto exit;
427 }
428 } else {
429 /* we check the primary buffer */
430 spin_lock_irqsave (&dev->buflock, flags);
431 if (dev->read_buffer_length) {
432 /* we secure access to the primary */
433 char *tmp;
Greg Kroah-Hartman66d4bc32013-06-26 16:30:44 -0700434 dev_dbg(&dev->udev->dev,
435 "%s : swap, read_buffer_length = %d\n",
436 __func__, dev->read_buffer_length);
Steven Haigh03270632006-08-09 07:42:06 +1000437 tmp = dev->read_buffer_secondary;
438 dev->read_buffer_secondary = dev->read_buffer_primary;
439 dev->read_buffer_primary = tmp;
440 dev->secondary_head = 0;
441 dev->secondary_tail = dev->read_buffer_length;
442 dev->read_buffer_length = 0;
443 spin_unlock_irqrestore(&dev->buflock, flags);
444 /* we have a free buffer so use it */
445 should_submit = 1;
446 } else {
447 /* even the primary was empty - we may need to do IO */
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700448 if (!dev->read_urb_finished) {
Steven Haigh03270632006-08-09 07:42:06 +1000449 /* somebody is doing IO */
450 spin_unlock_irqrestore(&dev->buflock, flags);
Greg Kroah-Hartman66d4bc32013-06-26 16:30:44 -0700451 dev_dbg(&dev->udev->dev,
452 "%s : submitted already\n",
453 __func__);
Steven Haigh03270632006-08-09 07:42:06 +1000454 } else {
455 /* we must initiate input */
Greg Kroah-Hartman66d4bc32013-06-26 16:30:44 -0700456 dev_dbg(&dev->udev->dev,
457 "%s : initiate input\n",
458 __func__);
Steven Haigh03270632006-08-09 07:42:06 +1000459 dev->read_urb_finished = 0;
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700460 spin_unlock_irqrestore(&dev->buflock, flags);
Steven Haigh03270632006-08-09 07:42:06 +1000461
Lisa Nguyen05d76392013-05-13 12:41:54 -0700462 usb_fill_int_urb(dev->interrupt_in_urb, dev->udev,
Lisa Nguyeneb79c012013-05-13 12:41:10 -0700463 usb_rcvintpipe(dev->udev,
464 dev->interrupt_in_endpoint->bEndpointAddress),
Steven Haigh03270632006-08-09 07:42:06 +1000465 dev->interrupt_in_buffer,
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700466 usb_endpoint_maxp(dev->interrupt_in_endpoint),
Steven Haigh03270632006-08-09 07:42:06 +1000467 adu_interrupt_in_callback,
468 dev,
469 dev->interrupt_in_endpoint->bInterval);
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700470 retval = usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL);
471 if (retval) {
472 dev->read_urb_finished = 1;
Steven Haigh03270632006-08-09 07:42:06 +1000473 if (retval == -ENOMEM) {
474 retval = bytes_read ? bytes_read : -ENOMEM;
475 }
Greg Kroah-Hartman66d4bc32013-06-26 16:30:44 -0700476 dev_dbg(&dev->udev->dev,
477 "%s : submit failed\n",
478 __func__);
Steven Haigh03270632006-08-09 07:42:06 +1000479 goto exit;
480 }
481 }
482
483 /* we wait for I/O to complete */
484 set_current_state(TASK_INTERRUPTIBLE);
485 add_wait_queue(&dev->read_wait, &wait);
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700486 spin_lock_irqsave(&dev->buflock, flags);
487 if (!dev->read_urb_finished) {
488 spin_unlock_irqrestore(&dev->buflock, flags);
Steven Haigh03270632006-08-09 07:42:06 +1000489 timeout = schedule_timeout(COMMAND_TIMEOUT);
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700490 } else {
491 spin_unlock_irqrestore(&dev->buflock, flags);
Steven Haigh03270632006-08-09 07:42:06 +1000492 set_current_state(TASK_RUNNING);
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700493 }
Steven Haigh03270632006-08-09 07:42:06 +1000494 remove_wait_queue(&dev->read_wait, &wait);
495
496 if (timeout <= 0) {
Greg Kroah-Hartman66d4bc32013-06-26 16:30:44 -0700497 dev_dbg(&dev->udev->dev,
498 "%s : timeout\n", __func__);
Steven Haigh03270632006-08-09 07:42:06 +1000499 retval = bytes_read ? bytes_read : -ETIMEDOUT;
500 goto exit;
501 }
502
503 if (signal_pending(current)) {
Greg Kroah-Hartman66d4bc32013-06-26 16:30:44 -0700504 dev_dbg(&dev->udev->dev,
505 "%s : signal pending\n",
506 __func__);
Steven Haigh03270632006-08-09 07:42:06 +1000507 retval = bytes_read ? bytes_read : -EINTR;
508 goto exit;
509 }
510 }
511 }
512 }
513
514 retval = bytes_read;
515 /* if the primary buffer is empty then use it */
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700516 spin_lock_irqsave(&dev->buflock, flags);
517 if (should_submit && dev->read_urb_finished) {
518 dev->read_urb_finished = 0;
519 spin_unlock_irqrestore(&dev->buflock, flags);
Lisa Nguyen05d76392013-05-13 12:41:54 -0700520 usb_fill_int_urb(dev->interrupt_in_urb, dev->udev,
Steven Haigh03270632006-08-09 07:42:06 +1000521 usb_rcvintpipe(dev->udev,
Lisa Nguyeneb79c012013-05-13 12:41:10 -0700522 dev->interrupt_in_endpoint->bEndpointAddress),
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700523 dev->interrupt_in_buffer,
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700524 usb_endpoint_maxp(dev->interrupt_in_endpoint),
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700525 adu_interrupt_in_callback,
526 dev,
527 dev->interrupt_in_endpoint->bInterval);
528 if (usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL) != 0)
529 dev->read_urb_finished = 1;
Steven Haigh03270632006-08-09 07:42:06 +1000530 /* we ignore failure */
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700531 } else {
532 spin_unlock_irqrestore(&dev->buflock, flags);
Steven Haigh03270632006-08-09 07:42:06 +1000533 }
534
535exit:
536 /* unlock the device */
Matthias Kaehlcke8293c562007-07-13 21:28:31 +0200537 mutex_unlock(&dev->mtx);
Steven Haigh03270632006-08-09 07:42:06 +1000538
Steven Haigh03270632006-08-09 07:42:06 +1000539 return retval;
540}
541
542static ssize_t adu_write(struct file *file, const __user char *buffer,
543 size_t count, loff_t *ppos)
544{
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700545 DECLARE_WAITQUEUE(waita, current);
Steven Haigh03270632006-08-09 07:42:06 +1000546 struct adu_device *dev;
547 size_t bytes_written = 0;
548 size_t bytes_to_write;
549 size_t buffer_size;
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700550 unsigned long flags;
Oliver Neukumebc3ac12007-04-02 15:16:36 +0200551 int retval;
Steven Haigh03270632006-08-09 07:42:06 +1000552
Steven Haigh03270632006-08-09 07:42:06 +1000553 dev = file->private_data;
554
Matthias Kaehlcke8293c562007-07-13 21:28:31 +0200555 retval = mutex_lock_interruptible(&dev->mtx);
Oliver Neukumebc3ac12007-04-02 15:16:36 +0200556 if (retval)
557 goto exit_nolock;
Steven Haigh03270632006-08-09 07:42:06 +1000558
559 /* verify that the device wasn't unplugged */
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700560 if (dev->udev == NULL) {
Steven Haigh03270632006-08-09 07:42:06 +1000561 retval = -ENODEV;
Greg Kroah-Hartmanfd3f1912008-08-14 09:37:34 -0700562 printk(KERN_ERR "adutux: No device or device unplugged %d\n",
563 retval);
Steven Haigh03270632006-08-09 07:42:06 +1000564 goto exit;
565 }
566
567 /* verify that we actually have some data to write */
568 if (count == 0) {
Greg Kroah-Hartman66d4bc32013-06-26 16:30:44 -0700569 dev_dbg(&dev->udev->dev, "%s : write request of 0 bytes\n",
570 __func__);
Steven Haigh03270632006-08-09 07:42:06 +1000571 goto exit;
572 }
573
Steven Haigh03270632006-08-09 07:42:06 +1000574 while (count > 0) {
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700575 add_wait_queue(&dev->write_wait, &waita);
576 set_current_state(TASK_INTERRUPTIBLE);
577 spin_lock_irqsave(&dev->buflock, flags);
578 if (!dev->out_urb_finished) {
579 spin_unlock_irqrestore(&dev->buflock, flags);
Steven Haigh03270632006-08-09 07:42:06 +1000580
Matthias Kaehlcke8293c562007-07-13 21:28:31 +0200581 mutex_unlock(&dev->mtx);
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700582 if (signal_pending(current)) {
Greg Kroah-Hartman66d4bc32013-06-26 16:30:44 -0700583 dev_dbg(&dev->udev->dev, "%s : interrupted\n",
584 __func__);
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700585 set_current_state(TASK_RUNNING);
586 retval = -EINTR;
587 goto exit_onqueue;
588 }
589 if (schedule_timeout(COMMAND_TIMEOUT) == 0) {
Greg Kroah-Hartman66d4bc32013-06-26 16:30:44 -0700590 dev_dbg(&dev->udev->dev,
591 "%s - command timed out.\n", __func__);
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700592 retval = -ETIMEDOUT;
593 goto exit_onqueue;
594 }
595 remove_wait_queue(&dev->write_wait, &waita);
Matthias Kaehlcke8293c562007-07-13 21:28:31 +0200596 retval = mutex_lock_interruptible(&dev->mtx);
Oliver Neukumebc3ac12007-04-02 15:16:36 +0200597 if (retval) {
598 retval = bytes_written ? bytes_written : retval;
599 goto exit_nolock;
600 }
Steven Haigh03270632006-08-09 07:42:06 +1000601
Greg Kroah-Hartman66d4bc32013-06-26 16:30:44 -0700602 dev_dbg(&dev->udev->dev,
603 "%s : in progress, count = %Zd\n",
604 __func__, count);
Steven Haigh03270632006-08-09 07:42:06 +1000605 } else {
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700606 spin_unlock_irqrestore(&dev->buflock, flags);
607 set_current_state(TASK_RUNNING);
608 remove_wait_queue(&dev->write_wait, &waita);
Greg Kroah-Hartman66d4bc32013-06-26 16:30:44 -0700609 dev_dbg(&dev->udev->dev, "%s : sending, count = %Zd\n",
610 __func__, count);
Steven Haigh03270632006-08-09 07:42:06 +1000611
612 /* write the data into interrupt_out_buffer from userspace */
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700613 buffer_size = usb_endpoint_maxp(dev->interrupt_out_endpoint);
Steven Haigh03270632006-08-09 07:42:06 +1000614 bytes_to_write = count > buffer_size ? buffer_size : count;
Greg Kroah-Hartman66d4bc32013-06-26 16:30:44 -0700615 dev_dbg(&dev->udev->dev,
616 "%s : buffer_size = %Zd, count = %Zd, bytes_to_write = %Zd\n",
617 __func__, buffer_size, count, bytes_to_write);
Steven Haigh03270632006-08-09 07:42:06 +1000618
619 if (copy_from_user(dev->interrupt_out_buffer, buffer, bytes_to_write) != 0) {
620 retval = -EFAULT;
621 goto exit;
622 }
623
624 /* send off the urb */
625 usb_fill_int_urb(
626 dev->interrupt_out_urb,
627 dev->udev,
628 usb_sndintpipe(dev->udev, dev->interrupt_out_endpoint->bEndpointAddress),
629 dev->interrupt_out_buffer,
630 bytes_to_write,
631 adu_interrupt_out_callback,
632 dev,
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700633 dev->interrupt_out_endpoint->bInterval);
Steven Haigh03270632006-08-09 07:42:06 +1000634 dev->interrupt_out_urb->actual_length = bytes_to_write;
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700635 dev->out_urb_finished = 0;
Steven Haigh03270632006-08-09 07:42:06 +1000636 retval = usb_submit_urb(dev->interrupt_out_urb, GFP_KERNEL);
637 if (retval < 0) {
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700638 dev->out_urb_finished = 1;
Greg Kroah-Hartmanfd3f1912008-08-14 09:37:34 -0700639 dev_err(&dev->udev->dev, "Couldn't submit "
640 "interrupt_out_urb %d\n", retval);
Steven Haigh03270632006-08-09 07:42:06 +1000641 goto exit;
642 }
643
644 buffer += bytes_to_write;
645 count -= bytes_to_write;
646
647 bytes_written += bytes_to_write;
648 }
649 }
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700650 mutex_unlock(&dev->mtx);
651 return bytes_written;
Steven Haigh03270632006-08-09 07:42:06 +1000652
653exit:
Matthias Kaehlcke8293c562007-07-13 21:28:31 +0200654 mutex_unlock(&dev->mtx);
Oliver Neukumebc3ac12007-04-02 15:16:36 +0200655exit_nolock:
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700656 return retval;
Steven Haigh03270632006-08-09 07:42:06 +1000657
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700658exit_onqueue:
659 remove_wait_queue(&dev->write_wait, &waita);
Steven Haigh03270632006-08-09 07:42:06 +1000660 return retval;
661}
662
663/* file operations needed when we register this driver */
Arjan van de Ven00977a52007-02-12 00:55:34 -0800664static const struct file_operations adu_fops = {
Steven Haigh03270632006-08-09 07:42:06 +1000665 .owner = THIS_MODULE,
666 .read = adu_read,
667 .write = adu_write,
668 .open = adu_open,
669 .release = adu_release,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200670 .llseek = noop_llseek,
Steven Haigh03270632006-08-09 07:42:06 +1000671};
672
673/*
674 * usb class driver info in order to get a minor number from the usb core,
675 * and to have the device registered with devfs and the driver core
676 */
677static struct usb_class_driver adu_class = {
678 .name = "usb/adutux%d",
679 .fops = &adu_fops,
680 .minor_base = ADU_MINOR_BASE,
681};
682
683/**
684 * adu_probe
685 *
686 * Called by the usb core when a new device is connected that it thinks
687 * this driver might be interested in.
688 */
689static int adu_probe(struct usb_interface *interface,
690 const struct usb_device_id *id)
691{
692 struct usb_device *udev = interface_to_usbdev(interface);
693 struct adu_device *dev = NULL;
694 struct usb_host_interface *iface_desc;
695 struct usb_endpoint_descriptor *endpoint;
696 int retval = -ENODEV;
697 int in_end_size;
698 int out_end_size;
699 int i;
700
Steven Haigh03270632006-08-09 07:42:06 +1000701 if (udev == NULL) {
702 dev_err(&interface->dev, "udev is NULL.\n");
703 goto exit;
704 }
705
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400706 /* allocate memory for our device state and initialize it */
Steven Haigh03270632006-08-09 07:42:06 +1000707 dev = kzalloc(sizeof(struct adu_device), GFP_KERNEL);
708 if (dev == NULL) {
709 dev_err(&interface->dev, "Out of memory\n");
710 retval = -ENOMEM;
711 goto exit;
712 }
713
Matthias Kaehlcke8293c562007-07-13 21:28:31 +0200714 mutex_init(&dev->mtx);
Steven Haigh03270632006-08-09 07:42:06 +1000715 spin_lock_init(&dev->buflock);
716 dev->udev = udev;
717 init_waitqueue_head(&dev->read_wait);
718 init_waitqueue_head(&dev->write_wait);
719
720 iface_desc = &interface->altsetting[0];
721
722 /* set up the endpoint information */
723 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
724 endpoint = &iface_desc->endpoint[i].desc;
725
726 if (usb_endpoint_is_int_in(endpoint))
727 dev->interrupt_in_endpoint = endpoint;
728
729 if (usb_endpoint_is_int_out(endpoint))
730 dev->interrupt_out_endpoint = endpoint;
731 }
732 if (dev->interrupt_in_endpoint == NULL) {
733 dev_err(&interface->dev, "interrupt in endpoint not found\n");
734 goto error;
735 }
736 if (dev->interrupt_out_endpoint == NULL) {
737 dev_err(&interface->dev, "interrupt out endpoint not found\n");
738 goto error;
739 }
740
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700741 in_end_size = usb_endpoint_maxp(dev->interrupt_in_endpoint);
742 out_end_size = usb_endpoint_maxp(dev->interrupt_out_endpoint);
Steven Haigh03270632006-08-09 07:42:06 +1000743
744 dev->read_buffer_primary = kmalloc((4 * in_end_size), GFP_KERNEL);
745 if (!dev->read_buffer_primary) {
746 dev_err(&interface->dev, "Couldn't allocate read_buffer_primary\n");
747 retval = -ENOMEM;
748 goto error;
749 }
750
751 /* debug code prime the buffer */
752 memset(dev->read_buffer_primary, 'a', in_end_size);
753 memset(dev->read_buffer_primary + in_end_size, 'b', in_end_size);
754 memset(dev->read_buffer_primary + (2 * in_end_size), 'c', in_end_size);
755 memset(dev->read_buffer_primary + (3 * in_end_size), 'd', in_end_size);
756
757 dev->read_buffer_secondary = kmalloc((4 * in_end_size), GFP_KERNEL);
758 if (!dev->read_buffer_secondary) {
759 dev_err(&interface->dev, "Couldn't allocate read_buffer_secondary\n");
760 retval = -ENOMEM;
761 goto error;
762 }
763
764 /* debug code prime the buffer */
765 memset(dev->read_buffer_secondary, 'e', in_end_size);
766 memset(dev->read_buffer_secondary + in_end_size, 'f', in_end_size);
767 memset(dev->read_buffer_secondary + (2 * in_end_size), 'g', in_end_size);
768 memset(dev->read_buffer_secondary + (3 * in_end_size), 'h', in_end_size);
769
770 dev->interrupt_in_buffer = kmalloc(in_end_size, GFP_KERNEL);
771 if (!dev->interrupt_in_buffer) {
772 dev_err(&interface->dev, "Couldn't allocate interrupt_in_buffer\n");
773 goto error;
774 }
775
776 /* debug code prime the buffer */
777 memset(dev->interrupt_in_buffer, 'i', in_end_size);
778
779 dev->interrupt_in_urb = usb_alloc_urb(0, GFP_KERNEL);
780 if (!dev->interrupt_in_urb) {
781 dev_err(&interface->dev, "Couldn't allocate interrupt_in_urb\n");
782 goto error;
783 }
784 dev->interrupt_out_buffer = kmalloc(out_end_size, GFP_KERNEL);
785 if (!dev->interrupt_out_buffer) {
786 dev_err(&interface->dev, "Couldn't allocate interrupt_out_buffer\n");
787 goto error;
788 }
789 dev->interrupt_out_urb = usb_alloc_urb(0, GFP_KERNEL);
790 if (!dev->interrupt_out_urb) {
791 dev_err(&interface->dev, "Couldn't allocate interrupt_out_urb\n");
792 goto error;
793 }
794
795 if (!usb_string(udev, udev->descriptor.iSerialNumber, dev->serial_number,
796 sizeof(dev->serial_number))) {
797 dev_err(&interface->dev, "Could not retrieve serial number\n");
798 goto error;
799 }
Greg Kroah-Hartman66d4bc32013-06-26 16:30:44 -0700800 dev_dbg(&interface->dev,"serial_number=%s", dev->serial_number);
Steven Haigh03270632006-08-09 07:42:06 +1000801
802 /* we can register the device now, as it is ready */
803 usb_set_intfdata(interface, dev);
804
805 retval = usb_register_dev(interface, &adu_class);
806
807 if (retval) {
808 /* something prevented us from registering this driver */
809 dev_err(&interface->dev, "Not able to get a minor for this device.\n");
810 usb_set_intfdata(interface, NULL);
811 goto error;
812 }
813
814 dev->minor = interface->minor;
815
816 /* let the user know what node this device is now attached to */
Joe Perches898eb712007-10-18 03:06:30 -0700817 dev_info(&interface->dev, "ADU%d %s now attached to /dev/usb/adutux%d\n",
Steven Haigh03270632006-08-09 07:42:06 +1000818 udev->descriptor.idProduct, dev->serial_number,
819 (dev->minor - ADU_MINOR_BASE));
820exit:
Steven Haigh03270632006-08-09 07:42:06 +1000821 return retval;
822
823error:
824 adu_delete(dev);
825 return retval;
826}
827
828/**
829 * adu_disconnect
830 *
831 * Called by the usb core when the device is removed from the system.
832 */
833static void adu_disconnect(struct usb_interface *interface)
834{
835 struct adu_device *dev;
836 int minor;
837
Steven Haigh03270632006-08-09 07:42:06 +1000838 dev = usb_get_intfdata(interface);
Steven Haigh03270632006-08-09 07:42:06 +1000839
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700840 mutex_lock(&dev->mtx); /* not interruptible */
841 dev->udev = NULL; /* poison */
Steven Haigh03270632006-08-09 07:42:06 +1000842 minor = dev->minor;
Steven Haigh03270632006-08-09 07:42:06 +1000843 usb_deregister_dev(interface, &adu_class);
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700844 mutex_unlock(&dev->mtx);
Steven Haigh03270632006-08-09 07:42:06 +1000845
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700846 mutex_lock(&adutux_mutex);
847 usb_set_intfdata(interface, NULL);
Alan Sternd4ead162007-05-22 11:46:41 -0400848
Steven Haigh03270632006-08-09 07:42:06 +1000849 /* if the device is not opened, then we clean up right now */
Greg Kroah-Hartman66d4bc32013-06-26 16:30:44 -0700850 dev_dbg(&dev->udev->dev, "%s : open count %d\n",
851 __func__, dev->open_count);
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700852 if (!dev->open_count)
Steven Haigh03270632006-08-09 07:42:06 +1000853 adu_delete(dev);
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700854
855 mutex_unlock(&adutux_mutex);
Steven Haigh03270632006-08-09 07:42:06 +1000856
Joe Perches898eb712007-10-18 03:06:30 -0700857 dev_info(&interface->dev, "ADU device adutux%d now disconnected\n",
Steven Haigh03270632006-08-09 07:42:06 +1000858 (minor - ADU_MINOR_BASE));
Steven Haigh03270632006-08-09 07:42:06 +1000859}
860
861/* usb specific object needed to register this driver with the usb subsystem */
862static struct usb_driver adu_driver = {
863 .name = "adutux",
864 .probe = adu_probe,
865 .disconnect = adu_disconnect,
866 .id_table = device_table,
867};
868
Greg Kroah-Hartman65db4302011-11-18 09:34:02 -0800869module_usb_driver(adu_driver);
Steven Haigh03270632006-08-09 07:42:06 +1000870
871MODULE_AUTHOR(DRIVER_AUTHOR);
872MODULE_DESCRIPTION(DRIVER_DESC);
873MODULE_LICENSE("GPL");