blob: 4b8712733fc7dee3908fe3c783a571453f1e0065 [file] [log] [blame]
Greg Kroah-Hartman5fd54ac2017-11-03 11:28:30 +01001// SPDX-License-Identifier: GPL-2.0+
Steven Haigh03270632006-08-09 07:42:06 +10002/*
3 * adutux - driver for ADU devices from Ontrak Control Systems
4 * This is an experimental driver. Use at your own risk.
5 * This driver is not supported by Ontrak Control Systems.
6 *
7 * Copyright (c) 2003 John Homppi (SCO, leave this notice here)
8 *
Steven Haigh03270632006-08-09 07:42:06 +10009 * derived from the Lego USB Tower driver 0.56:
10 * Copyright (c) 2003 David Glance <davidgsf@sourceforge.net>
11 * 2001 Juergen Stuber <stuber@loria.fr>
12 * that was derived from USB Skeleton driver - 0.5
13 * Copyright (c) 2001 Greg Kroah-Hartman (greg@kroah.com)
14 *
15 */
16
Greg Kroah-Hartman28f47c32013-06-26 16:30:46 -070017#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18
Steven Haigh03270632006-08-09 07:42:06 +100019#include <linux/kernel.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010020#include <linux/sched/signal.h>
Steven Haigh03270632006-08-09 07:42:06 +100021#include <linux/errno.h>
Steven Haigh03270632006-08-09 07:42:06 +100022#include <linux/slab.h>
23#include <linux/module.h>
24#include <linux/usb.h>
Matthias Kaehlcke8293c562007-07-13 21:28:31 +020025#include <linux/mutex.h>
Lisa Nguyen40cf4832013-05-13 12:40:47 -070026#include <linux/uaccess.h>
Steven Haigh03270632006-08-09 07:42:06 +100027
Steven Haigh03270632006-08-09 07:42:06 +100028#define DRIVER_AUTHOR "John Homppi"
29#define DRIVER_DESC "adutux (see www.ontrak.net)"
30
Steven Haigh03270632006-08-09 07:42:06 +100031/* Define these values to match your device */
32#define ADU_VENDOR_ID 0x0a07
33#define ADU_PRODUCT_ID 0x0064
34
35/* table of devices that work with this driver */
Németh Márton33b9e162010-01-10 15:34:45 +010036static const struct usb_device_id device_table[] = {
Steven Haigh03270632006-08-09 07:42:06 +100037 { USB_DEVICE(ADU_VENDOR_ID, ADU_PRODUCT_ID) }, /* ADU100 */
Lisa Nguyeneb79c012013-05-13 12:41:10 -070038 { USB_DEVICE(ADU_VENDOR_ID, ADU_PRODUCT_ID+20) }, /* ADU120 */
39 { USB_DEVICE(ADU_VENDOR_ID, ADU_PRODUCT_ID+30) }, /* ADU130 */
Steven Haigh03270632006-08-09 07:42:06 +100040 { USB_DEVICE(ADU_VENDOR_ID, ADU_PRODUCT_ID+100) }, /* ADU200 */
41 { USB_DEVICE(ADU_VENDOR_ID, ADU_PRODUCT_ID+108) }, /* ADU208 */
42 { USB_DEVICE(ADU_VENDOR_ID, ADU_PRODUCT_ID+118) }, /* ADU218 */
Lisa Nguyen83fc1fc2013-05-13 12:42:21 -070043 { } /* Terminating entry */
Steven Haigh03270632006-08-09 07:42:06 +100044};
45
46MODULE_DEVICE_TABLE(usb, device_table);
47
48#ifdef CONFIG_USB_DYNAMIC_MINORS
49#define ADU_MINOR_BASE 0
50#else
51#define ADU_MINOR_BASE 67
52#endif
53
54/* we can have up to this number of device plugged in at once */
55#define MAX_DEVICES 16
56
57#define COMMAND_TIMEOUT (2*HZ) /* 60 second timeout for a command */
58
Pete Zaitcevf08812d2007-10-31 15:59:30 -070059/*
60 * The locking scheme is a vanilla 3-lock:
61 * adu_device.buflock: A spinlock, covers what IRQs touch.
62 * adutux_mutex: A Static lock to cover open_count. It would also cover
63 * any globals, but we don't have them in 2.6.
64 * adu_device.mtx: A mutex to hold across sleepers like copy_from_user.
65 * It covers all of adu_device, except the open_count
66 * and what .buflock covers.
67 */
68
Steven Haigh03270632006-08-09 07:42:06 +100069/* Structure to hold all of our device specific stuff */
70struct adu_device {
Pete Zaitcevf08812d2007-10-31 15:59:30 -070071 struct mutex mtx;
Lisa Nguyen30c5c642013-05-13 12:41:34 -070072 struct usb_device *udev; /* save off the usb device pointer */
73 struct usb_interface *interface;
Pete Zaitcevf08812d2007-10-31 15:59:30 -070074 unsigned int minor; /* the starting minor number for this device */
Steven Haigh03270632006-08-09 07:42:06 +100075 char serial_number[8];
76
77 int open_count; /* number of times this port has been opened */
78
Lisa Nguyen30c5c642013-05-13 12:41:34 -070079 char *read_buffer_primary;
Steven Haigh03270632006-08-09 07:42:06 +100080 int read_buffer_length;
Lisa Nguyen30c5c642013-05-13 12:41:34 -070081 char *read_buffer_secondary;
Steven Haigh03270632006-08-09 07:42:06 +100082 int secondary_head;
83 int secondary_tail;
84 spinlock_t buflock;
85
86 wait_queue_head_t read_wait;
87 wait_queue_head_t write_wait;
88
Lisa Nguyen30c5c642013-05-13 12:41:34 -070089 char *interrupt_in_buffer;
90 struct usb_endpoint_descriptor *interrupt_in_endpoint;
91 struct urb *interrupt_in_urb;
Steven Haigh03270632006-08-09 07:42:06 +100092 int read_urb_finished;
93
Lisa Nguyen30c5c642013-05-13 12:41:34 -070094 char *interrupt_out_buffer;
95 struct usb_endpoint_descriptor *interrupt_out_endpoint;
96 struct urb *interrupt_out_urb;
Pete Zaitcevf08812d2007-10-31 15:59:30 -070097 int out_urb_finished;
Steven Haigh03270632006-08-09 07:42:06 +100098};
99
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700100static DEFINE_MUTEX(adutux_mutex);
101
Steven Haigh03270632006-08-09 07:42:06 +1000102static struct usb_driver adu_driver;
103
Greg Kroah-Hartman1ef37c62013-06-26 16:30:45 -0700104static inline void adu_debug_data(struct device *dev, const char *function,
105 int size, const unsigned char *data)
Steven Haigh03270632006-08-09 07:42:06 +1000106{
Greg Kroah-Hartman1ef37c62013-06-26 16:30:45 -0700107 dev_dbg(dev, "%s - length = %d, data = %*ph\n",
108 function, size, size, data);
Steven Haigh03270632006-08-09 07:42:06 +1000109}
110
111/**
112 * adu_abort_transfers
113 * aborts transfers and frees associated data structures
114 */
115static void adu_abort_transfers(struct adu_device *dev)
116{
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700117 unsigned long flags;
Steven Haigh03270632006-08-09 07:42:06 +1000118
Greg Kroah-Hartman66d4bc32013-06-26 16:30:44 -0700119 if (dev->udev == NULL)
Greg Kroah-Hartman6e42a152013-06-26 16:30:43 -0700120 return;
Steven Haigh03270632006-08-09 07:42:06 +1000121
Steven Haigh03270632006-08-09 07:42:06 +1000122 /* shutdown transfer */
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700123
124 /* XXX Anchor these instead */
125 spin_lock_irqsave(&dev->buflock, flags);
126 if (!dev->read_urb_finished) {
127 spin_unlock_irqrestore(&dev->buflock, flags);
128 usb_kill_urb(dev->interrupt_in_urb);
129 } else
130 spin_unlock_irqrestore(&dev->buflock, flags);
131
132 spin_lock_irqsave(&dev->buflock, flags);
133 if (!dev->out_urb_finished) {
134 spin_unlock_irqrestore(&dev->buflock, flags);
135 usb_kill_urb(dev->interrupt_out_urb);
136 } else
137 spin_unlock_irqrestore(&dev->buflock, flags);
Steven Haigh03270632006-08-09 07:42:06 +1000138}
139
140static void adu_delete(struct adu_device *dev)
141{
Steven Haigh03270632006-08-09 07:42:06 +1000142 /* free data structures */
143 usb_free_urb(dev->interrupt_in_urb);
144 usb_free_urb(dev->interrupt_out_urb);
145 kfree(dev->read_buffer_primary);
146 kfree(dev->read_buffer_secondary);
147 kfree(dev->interrupt_in_buffer);
148 kfree(dev->interrupt_out_buffer);
149 kfree(dev);
Steven Haigh03270632006-08-09 07:42:06 +1000150}
151
David Howells7d12e782006-10-05 14:55:46 +0100152static void adu_interrupt_in_callback(struct urb *urb)
Steven Haigh03270632006-08-09 07:42:06 +1000153{
154 struct adu_device *dev = urb->context;
Greg Kroah-Hartman24497a02007-07-18 10:58:02 -0700155 int status = urb->status;
Steven Haigh03270632006-08-09 07:42:06 +1000156
Greg Kroah-Hartman1ef37c62013-06-26 16:30:45 -0700157 adu_debug_data(&dev->udev->dev, __func__,
158 urb->actual_length, urb->transfer_buffer);
Steven Haigh03270632006-08-09 07:42:06 +1000159
160 spin_lock(&dev->buflock);
161
Greg Kroah-Hartman24497a02007-07-18 10:58:02 -0700162 if (status != 0) {
Oliver Neukumf6c1cea2007-08-16 16:02:08 +0200163 if ((status != -ENOENT) && (status != -ECONNRESET) &&
164 (status != -ESHUTDOWN)) {
Greg Kroah-Hartman66d4bc32013-06-26 16:30:44 -0700165 dev_dbg(&dev->udev->dev,
166 "%s : nonzero status received: %d\n",
167 __func__, status);
Steven Haigh03270632006-08-09 07:42:06 +1000168 }
169 goto exit;
170 }
171
172 if (urb->actual_length > 0 && dev->interrupt_in_buffer[0] != 0x00) {
173 if (dev->read_buffer_length <
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700174 (4 * usb_endpoint_maxp(dev->interrupt_in_endpoint)) -
Steven Haigh03270632006-08-09 07:42:06 +1000175 (urb->actual_length)) {
176 memcpy (dev->read_buffer_primary +
177 dev->read_buffer_length,
178 dev->interrupt_in_buffer, urb->actual_length);
179
180 dev->read_buffer_length += urb->actual_length;
Greg Kroah-Hartman66d4bc32013-06-26 16:30:44 -0700181 dev_dbg(&dev->udev->dev,"%s reading %d\n", __func__,
182 urb->actual_length);
Steven Haigh03270632006-08-09 07:42:06 +1000183 } else {
Greg Kroah-Hartman66d4bc32013-06-26 16:30:44 -0700184 dev_dbg(&dev->udev->dev,"%s : read_buffer overflow\n",
185 __func__);
Steven Haigh03270632006-08-09 07:42:06 +1000186 }
187 }
188
189exit:
190 dev->read_urb_finished = 1;
191 spin_unlock(&dev->buflock);
192 /* always wake up so we recover from errors */
193 wake_up_interruptible(&dev->read_wait);
Steven Haigh03270632006-08-09 07:42:06 +1000194}
195
David Howells7d12e782006-10-05 14:55:46 +0100196static void adu_interrupt_out_callback(struct urb *urb)
Steven Haigh03270632006-08-09 07:42:06 +1000197{
198 struct adu_device *dev = urb->context;
Greg Kroah-Hartman24497a02007-07-18 10:58:02 -0700199 int status = urb->status;
Steven Haigh03270632006-08-09 07:42:06 +1000200
Greg Kroah-Hartman1ef37c62013-06-26 16:30:45 -0700201 adu_debug_data(&dev->udev->dev, __func__,
202 urb->actual_length, urb->transfer_buffer);
Steven Haigh03270632006-08-09 07:42:06 +1000203
Greg Kroah-Hartman24497a02007-07-18 10:58:02 -0700204 if (status != 0) {
205 if ((status != -ENOENT) &&
206 (status != -ECONNRESET)) {
Greg Kroah-Hartman66d4bc32013-06-26 16:30:44 -0700207 dev_dbg(&dev->udev->dev,
208 "%s :nonzero status received: %d\n", __func__,
209 status);
Steven Haigh03270632006-08-09 07:42:06 +1000210 }
Greg Kroah-Hartman1ef37c62013-06-26 16:30:45 -0700211 return;
Steven Haigh03270632006-08-09 07:42:06 +1000212 }
213
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700214 spin_lock(&dev->buflock);
215 dev->out_urb_finished = 1;
216 wake_up(&dev->write_wait);
217 spin_unlock(&dev->buflock);
Steven Haigh03270632006-08-09 07:42:06 +1000218}
219
220static int adu_open(struct inode *inode, struct file *file)
221{
222 struct adu_device *dev = NULL;
223 struct usb_interface *interface;
224 int subminor;
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700225 int retval;
Steven Haigh03270632006-08-09 07:42:06 +1000226
Steven Haigh03270632006-08-09 07:42:06 +1000227 subminor = iminor(inode);
228
Lisa Nguyene77c4e62013-05-15 15:21:07 -0700229 retval = mutex_lock_interruptible(&adutux_mutex);
Greg Kroah-Hartman66d4bc32013-06-26 16:30:44 -0700230 if (retval)
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700231 goto exit_no_lock;
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700232
Steven Haigh03270632006-08-09 07:42:06 +1000233 interface = usb_find_interface(&adu_driver, subminor);
234 if (!interface) {
Greg Kroah-Hartman28f47c32013-06-26 16:30:46 -0700235 pr_err("%s - error, can't find device for minor %d\n",
236 __func__, subminor);
Steven Haigh03270632006-08-09 07:42:06 +1000237 retval = -ENODEV;
238 goto exit_no_device;
239 }
240
241 dev = usb_get_intfdata(interface);
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700242 if (!dev || !dev->udev) {
Steven Haigh03270632006-08-09 07:42:06 +1000243 retval = -ENODEV;
244 goto exit_no_device;
245 }
246
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700247 /* check that nobody else is using the device */
248 if (dev->open_count) {
249 retval = -EBUSY;
Steven Haigh03270632006-08-09 07:42:06 +1000250 goto exit_no_device;
251 }
252
Steven Haigh03270632006-08-09 07:42:06 +1000253 ++dev->open_count;
Greg Kroah-Hartman66d4bc32013-06-26 16:30:44 -0700254 dev_dbg(&dev->udev->dev, "%s: open count %d\n", __func__,
255 dev->open_count);
Steven Haigh03270632006-08-09 07:42:06 +1000256
257 /* save device in the file's private structure */
258 file->private_data = dev;
259
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700260 /* initialize in direction */
261 dev->read_buffer_length = 0;
Steven Haigh03270632006-08-09 07:42:06 +1000262
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700263 /* fixup first read by having urb waiting for it */
Lisa Nguyen05d76392013-05-13 12:41:54 -0700264 usb_fill_int_urb(dev->interrupt_in_urb, dev->udev,
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700265 usb_rcvintpipe(dev->udev,
266 dev->interrupt_in_endpoint->bEndpointAddress),
267 dev->interrupt_in_buffer,
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700268 usb_endpoint_maxp(dev->interrupt_in_endpoint),
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700269 adu_interrupt_in_callback, dev,
270 dev->interrupt_in_endpoint->bInterval);
271 dev->read_urb_finished = 0;
272 if (usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL))
273 dev->read_urb_finished = 1;
274 /* we ignore failure */
275 /* end of fixup for first read */
276
277 /* initialize out direction */
278 dev->out_urb_finished = 1;
279
280 retval = 0;
Steven Haigh03270632006-08-09 07:42:06 +1000281
282exit_no_device:
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700283 mutex_unlock(&adutux_mutex);
284exit_no_lock:
Steven Haigh03270632006-08-09 07:42:06 +1000285 return retval;
286}
287
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700288static void adu_release_internal(struct adu_device *dev)
Steven Haigh03270632006-08-09 07:42:06 +1000289{
Steven Haigh03270632006-08-09 07:42:06 +1000290 /* decrement our usage count for the device */
291 --dev->open_count;
Greg Kroah-Hartman66d4bc32013-06-26 16:30:44 -0700292 dev_dbg(&dev->udev->dev, "%s : open count %d\n", __func__,
293 dev->open_count);
Steven Haigh03270632006-08-09 07:42:06 +1000294 if (dev->open_count <= 0) {
295 adu_abort_transfers(dev);
296 dev->open_count = 0;
297 }
Steven Haigh03270632006-08-09 07:42:06 +1000298}
299
300static int adu_release(struct inode *inode, struct file *file)
301{
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700302 struct adu_device *dev;
Steven Haigh03270632006-08-09 07:42:06 +1000303 int retval = 0;
304
Steven Haigh03270632006-08-09 07:42:06 +1000305 if (file == NULL) {
Steven Haigh03270632006-08-09 07:42:06 +1000306 retval = -ENODEV;
307 goto exit;
308 }
309
310 dev = file->private_data;
Steven Haigh03270632006-08-09 07:42:06 +1000311 if (dev == NULL) {
Steven Haigh03270632006-08-09 07:42:06 +1000312 retval = -ENODEV;
313 goto exit;
314 }
315
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700316 mutex_lock(&adutux_mutex); /* not interruptible */
Steven Haigh03270632006-08-09 07:42:06 +1000317
318 if (dev->open_count <= 0) {
Greg Kroah-Hartman66d4bc32013-06-26 16:30:44 -0700319 dev_dbg(&dev->udev->dev, "%s : device not opened\n", __func__);
Steven Haigh03270632006-08-09 07:42:06 +1000320 retval = -ENODEV;
Jiri Slaby46c98442009-03-11 21:47:38 +0100321 goto unlock;
Steven Haigh03270632006-08-09 07:42:06 +1000322 }
323
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700324 adu_release_internal(dev);
Alan Sternd4ead162007-05-22 11:46:41 -0400325 if (dev->udev == NULL) {
326 /* the device was unplugged before the file was released */
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700327 if (!dev->open_count) /* ... and we're the last user */
328 adu_delete(dev);
Alan Sternd4ead162007-05-22 11:46:41 -0400329 }
Jiri Slaby46c98442009-03-11 21:47:38 +0100330unlock:
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700331 mutex_unlock(&adutux_mutex);
Jiri Slaby46c98442009-03-11 21:47:38 +0100332exit:
Steven Haigh03270632006-08-09 07:42:06 +1000333 return retval;
334}
335
336static ssize_t adu_read(struct file *file, __user char *buffer, size_t count,
337 loff_t *ppos)
338{
339 struct adu_device *dev;
340 size_t bytes_read = 0;
341 size_t bytes_to_read = count;
342 int i;
343 int retval = 0;
344 int timeout = 0;
345 int should_submit = 0;
346 unsigned long flags;
347 DECLARE_WAITQUEUE(wait, current);
348
Steven Haigh03270632006-08-09 07:42:06 +1000349 dev = file->private_data;
Matthias Kaehlcke8293c562007-07-13 21:28:31 +0200350 if (mutex_lock_interruptible(&dev->mtx))
Steven Haigh03270632006-08-09 07:42:06 +1000351 return -ERESTARTSYS;
352
353 /* verify that the device wasn't unplugged */
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700354 if (dev->udev == NULL) {
Steven Haigh03270632006-08-09 07:42:06 +1000355 retval = -ENODEV;
Greg Kroah-Hartman28f47c32013-06-26 16:30:46 -0700356 pr_err("No device or device unplugged %d\n", retval);
Steven Haigh03270632006-08-09 07:42:06 +1000357 goto exit;
358 }
359
360 /* verify that some data was requested */
361 if (count == 0) {
Greg Kroah-Hartman66d4bc32013-06-26 16:30:44 -0700362 dev_dbg(&dev->udev->dev, "%s : read request of 0 bytes\n",
363 __func__);
Steven Haigh03270632006-08-09 07:42:06 +1000364 goto exit;
365 }
366
367 timeout = COMMAND_TIMEOUT;
Greg Kroah-Hartman66d4bc32013-06-26 16:30:44 -0700368 dev_dbg(&dev->udev->dev, "%s : about to start looping\n", __func__);
Steven Haigh03270632006-08-09 07:42:06 +1000369 while (bytes_to_read) {
370 int data_in_secondary = dev->secondary_tail - dev->secondary_head;
Greg Kroah-Hartman66d4bc32013-06-26 16:30:44 -0700371 dev_dbg(&dev->udev->dev,
372 "%s : while, data_in_secondary=%d, status=%d\n",
373 __func__, data_in_secondary,
374 dev->interrupt_in_urb->status);
Steven Haigh03270632006-08-09 07:42:06 +1000375
376 if (data_in_secondary) {
377 /* drain secondary buffer */
378 int amount = bytes_to_read < data_in_secondary ? bytes_to_read : data_in_secondary;
379 i = copy_to_user(buffer, dev->read_buffer_secondary+dev->secondary_head, amount);
Kulikov Vasiliy1865a9c2010-07-31 21:40:07 +0400380 if (i) {
Steven Haigh03270632006-08-09 07:42:06 +1000381 retval = -EFAULT;
382 goto exit;
383 }
384 dev->secondary_head += (amount - i);
385 bytes_read += (amount - i);
386 bytes_to_read -= (amount - i);
Steven Haigh03270632006-08-09 07:42:06 +1000387 } else {
388 /* we check the primary buffer */
389 spin_lock_irqsave (&dev->buflock, flags);
390 if (dev->read_buffer_length) {
391 /* we secure access to the primary */
392 char *tmp;
Greg Kroah-Hartman66d4bc32013-06-26 16:30:44 -0700393 dev_dbg(&dev->udev->dev,
394 "%s : swap, read_buffer_length = %d\n",
395 __func__, dev->read_buffer_length);
Steven Haigh03270632006-08-09 07:42:06 +1000396 tmp = dev->read_buffer_secondary;
397 dev->read_buffer_secondary = dev->read_buffer_primary;
398 dev->read_buffer_primary = tmp;
399 dev->secondary_head = 0;
400 dev->secondary_tail = dev->read_buffer_length;
401 dev->read_buffer_length = 0;
402 spin_unlock_irqrestore(&dev->buflock, flags);
403 /* we have a free buffer so use it */
404 should_submit = 1;
405 } else {
406 /* even the primary was empty - we may need to do IO */
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700407 if (!dev->read_urb_finished) {
Steven Haigh03270632006-08-09 07:42:06 +1000408 /* somebody is doing IO */
409 spin_unlock_irqrestore(&dev->buflock, flags);
Greg Kroah-Hartman66d4bc32013-06-26 16:30:44 -0700410 dev_dbg(&dev->udev->dev,
411 "%s : submitted already\n",
412 __func__);
Steven Haigh03270632006-08-09 07:42:06 +1000413 } else {
414 /* we must initiate input */
Greg Kroah-Hartman66d4bc32013-06-26 16:30:44 -0700415 dev_dbg(&dev->udev->dev,
416 "%s : initiate input\n",
417 __func__);
Steven Haigh03270632006-08-09 07:42:06 +1000418 dev->read_urb_finished = 0;
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700419 spin_unlock_irqrestore(&dev->buflock, flags);
Steven Haigh03270632006-08-09 07:42:06 +1000420
Lisa Nguyen05d76392013-05-13 12:41:54 -0700421 usb_fill_int_urb(dev->interrupt_in_urb, dev->udev,
Lisa Nguyeneb79c012013-05-13 12:41:10 -0700422 usb_rcvintpipe(dev->udev,
423 dev->interrupt_in_endpoint->bEndpointAddress),
Steven Haigh03270632006-08-09 07:42:06 +1000424 dev->interrupt_in_buffer,
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700425 usb_endpoint_maxp(dev->interrupt_in_endpoint),
Steven Haigh03270632006-08-09 07:42:06 +1000426 adu_interrupt_in_callback,
427 dev,
428 dev->interrupt_in_endpoint->bInterval);
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700429 retval = usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL);
430 if (retval) {
431 dev->read_urb_finished = 1;
Steven Haigh03270632006-08-09 07:42:06 +1000432 if (retval == -ENOMEM) {
433 retval = bytes_read ? bytes_read : -ENOMEM;
434 }
Greg Kroah-Hartman66d4bc32013-06-26 16:30:44 -0700435 dev_dbg(&dev->udev->dev,
436 "%s : submit failed\n",
437 __func__);
Steven Haigh03270632006-08-09 07:42:06 +1000438 goto exit;
439 }
440 }
441
442 /* we wait for I/O to complete */
443 set_current_state(TASK_INTERRUPTIBLE);
444 add_wait_queue(&dev->read_wait, &wait);
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700445 spin_lock_irqsave(&dev->buflock, flags);
446 if (!dev->read_urb_finished) {
447 spin_unlock_irqrestore(&dev->buflock, flags);
Steven Haigh03270632006-08-09 07:42:06 +1000448 timeout = schedule_timeout(COMMAND_TIMEOUT);
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700449 } else {
450 spin_unlock_irqrestore(&dev->buflock, flags);
Steven Haigh03270632006-08-09 07:42:06 +1000451 set_current_state(TASK_RUNNING);
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700452 }
Steven Haigh03270632006-08-09 07:42:06 +1000453 remove_wait_queue(&dev->read_wait, &wait);
454
455 if (timeout <= 0) {
Greg Kroah-Hartman66d4bc32013-06-26 16:30:44 -0700456 dev_dbg(&dev->udev->dev,
457 "%s : timeout\n", __func__);
Steven Haigh03270632006-08-09 07:42:06 +1000458 retval = bytes_read ? bytes_read : -ETIMEDOUT;
459 goto exit;
460 }
461
462 if (signal_pending(current)) {
Greg Kroah-Hartman66d4bc32013-06-26 16:30:44 -0700463 dev_dbg(&dev->udev->dev,
464 "%s : signal pending\n",
465 __func__);
Steven Haigh03270632006-08-09 07:42:06 +1000466 retval = bytes_read ? bytes_read : -EINTR;
467 goto exit;
468 }
469 }
470 }
471 }
472
473 retval = bytes_read;
474 /* if the primary buffer is empty then use it */
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700475 spin_lock_irqsave(&dev->buflock, flags);
476 if (should_submit && dev->read_urb_finished) {
477 dev->read_urb_finished = 0;
478 spin_unlock_irqrestore(&dev->buflock, flags);
Lisa Nguyen05d76392013-05-13 12:41:54 -0700479 usb_fill_int_urb(dev->interrupt_in_urb, dev->udev,
Steven Haigh03270632006-08-09 07:42:06 +1000480 usb_rcvintpipe(dev->udev,
Lisa Nguyeneb79c012013-05-13 12:41:10 -0700481 dev->interrupt_in_endpoint->bEndpointAddress),
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700482 dev->interrupt_in_buffer,
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700483 usb_endpoint_maxp(dev->interrupt_in_endpoint),
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700484 adu_interrupt_in_callback,
485 dev,
486 dev->interrupt_in_endpoint->bInterval);
487 if (usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL) != 0)
488 dev->read_urb_finished = 1;
Steven Haigh03270632006-08-09 07:42:06 +1000489 /* we ignore failure */
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700490 } else {
491 spin_unlock_irqrestore(&dev->buflock, flags);
Steven Haigh03270632006-08-09 07:42:06 +1000492 }
493
494exit:
495 /* unlock the device */
Matthias Kaehlcke8293c562007-07-13 21:28:31 +0200496 mutex_unlock(&dev->mtx);
Steven Haigh03270632006-08-09 07:42:06 +1000497
Steven Haigh03270632006-08-09 07:42:06 +1000498 return retval;
499}
500
501static ssize_t adu_write(struct file *file, const __user char *buffer,
502 size_t count, loff_t *ppos)
503{
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700504 DECLARE_WAITQUEUE(waita, current);
Steven Haigh03270632006-08-09 07:42:06 +1000505 struct adu_device *dev;
506 size_t bytes_written = 0;
507 size_t bytes_to_write;
508 size_t buffer_size;
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700509 unsigned long flags;
Oliver Neukumebc3ac12007-04-02 15:16:36 +0200510 int retval;
Steven Haigh03270632006-08-09 07:42:06 +1000511
Steven Haigh03270632006-08-09 07:42:06 +1000512 dev = file->private_data;
513
Matthias Kaehlcke8293c562007-07-13 21:28:31 +0200514 retval = mutex_lock_interruptible(&dev->mtx);
Oliver Neukumebc3ac12007-04-02 15:16:36 +0200515 if (retval)
516 goto exit_nolock;
Steven Haigh03270632006-08-09 07:42:06 +1000517
518 /* verify that the device wasn't unplugged */
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700519 if (dev->udev == NULL) {
Steven Haigh03270632006-08-09 07:42:06 +1000520 retval = -ENODEV;
Greg Kroah-Hartman28f47c32013-06-26 16:30:46 -0700521 pr_err("No device or device unplugged %d\n", retval);
Steven Haigh03270632006-08-09 07:42:06 +1000522 goto exit;
523 }
524
525 /* verify that we actually have some data to write */
526 if (count == 0) {
Greg Kroah-Hartman66d4bc32013-06-26 16:30:44 -0700527 dev_dbg(&dev->udev->dev, "%s : write request of 0 bytes\n",
528 __func__);
Steven Haigh03270632006-08-09 07:42:06 +1000529 goto exit;
530 }
531
Steven Haigh03270632006-08-09 07:42:06 +1000532 while (count > 0) {
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700533 add_wait_queue(&dev->write_wait, &waita);
534 set_current_state(TASK_INTERRUPTIBLE);
535 spin_lock_irqsave(&dev->buflock, flags);
536 if (!dev->out_urb_finished) {
537 spin_unlock_irqrestore(&dev->buflock, flags);
Steven Haigh03270632006-08-09 07:42:06 +1000538
Matthias Kaehlcke8293c562007-07-13 21:28:31 +0200539 mutex_unlock(&dev->mtx);
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700540 if (signal_pending(current)) {
Greg Kroah-Hartman66d4bc32013-06-26 16:30:44 -0700541 dev_dbg(&dev->udev->dev, "%s : interrupted\n",
542 __func__);
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700543 set_current_state(TASK_RUNNING);
544 retval = -EINTR;
545 goto exit_onqueue;
546 }
547 if (schedule_timeout(COMMAND_TIMEOUT) == 0) {
Greg Kroah-Hartman66d4bc32013-06-26 16:30:44 -0700548 dev_dbg(&dev->udev->dev,
549 "%s - command timed out.\n", __func__);
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700550 retval = -ETIMEDOUT;
551 goto exit_onqueue;
552 }
553 remove_wait_queue(&dev->write_wait, &waita);
Matthias Kaehlcke8293c562007-07-13 21:28:31 +0200554 retval = mutex_lock_interruptible(&dev->mtx);
Oliver Neukumebc3ac12007-04-02 15:16:36 +0200555 if (retval) {
556 retval = bytes_written ? bytes_written : retval;
557 goto exit_nolock;
558 }
Steven Haigh03270632006-08-09 07:42:06 +1000559
Greg Kroah-Hartman66d4bc32013-06-26 16:30:44 -0700560 dev_dbg(&dev->udev->dev,
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -0800561 "%s : in progress, count = %zd\n",
Greg Kroah-Hartman66d4bc32013-06-26 16:30:44 -0700562 __func__, count);
Steven Haigh03270632006-08-09 07:42:06 +1000563 } else {
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700564 spin_unlock_irqrestore(&dev->buflock, flags);
565 set_current_state(TASK_RUNNING);
566 remove_wait_queue(&dev->write_wait, &waita);
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -0800567 dev_dbg(&dev->udev->dev, "%s : sending, count = %zd\n",
Greg Kroah-Hartman66d4bc32013-06-26 16:30:44 -0700568 __func__, count);
Steven Haigh03270632006-08-09 07:42:06 +1000569
570 /* write the data into interrupt_out_buffer from userspace */
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700571 buffer_size = usb_endpoint_maxp(dev->interrupt_out_endpoint);
Steven Haigh03270632006-08-09 07:42:06 +1000572 bytes_to_write = count > buffer_size ? buffer_size : count;
Greg Kroah-Hartman66d4bc32013-06-26 16:30:44 -0700573 dev_dbg(&dev->udev->dev,
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -0800574 "%s : buffer_size = %zd, count = %zd, bytes_to_write = %zd\n",
Greg Kroah-Hartman66d4bc32013-06-26 16:30:44 -0700575 __func__, buffer_size, count, bytes_to_write);
Steven Haigh03270632006-08-09 07:42:06 +1000576
577 if (copy_from_user(dev->interrupt_out_buffer, buffer, bytes_to_write) != 0) {
578 retval = -EFAULT;
579 goto exit;
580 }
581
582 /* send off the urb */
583 usb_fill_int_urb(
584 dev->interrupt_out_urb,
585 dev->udev,
586 usb_sndintpipe(dev->udev, dev->interrupt_out_endpoint->bEndpointAddress),
587 dev->interrupt_out_buffer,
588 bytes_to_write,
589 adu_interrupt_out_callback,
590 dev,
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700591 dev->interrupt_out_endpoint->bInterval);
Steven Haigh03270632006-08-09 07:42:06 +1000592 dev->interrupt_out_urb->actual_length = bytes_to_write;
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700593 dev->out_urb_finished = 0;
Steven Haigh03270632006-08-09 07:42:06 +1000594 retval = usb_submit_urb(dev->interrupt_out_urb, GFP_KERNEL);
595 if (retval < 0) {
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700596 dev->out_urb_finished = 1;
Greg Kroah-Hartmanfd3f1912008-08-14 09:37:34 -0700597 dev_err(&dev->udev->dev, "Couldn't submit "
598 "interrupt_out_urb %d\n", retval);
Steven Haigh03270632006-08-09 07:42:06 +1000599 goto exit;
600 }
601
602 buffer += bytes_to_write;
603 count -= bytes_to_write;
604
605 bytes_written += bytes_to_write;
606 }
607 }
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700608 mutex_unlock(&dev->mtx);
609 return bytes_written;
Steven Haigh03270632006-08-09 07:42:06 +1000610
611exit:
Matthias Kaehlcke8293c562007-07-13 21:28:31 +0200612 mutex_unlock(&dev->mtx);
Oliver Neukumebc3ac12007-04-02 15:16:36 +0200613exit_nolock:
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700614 return retval;
Steven Haigh03270632006-08-09 07:42:06 +1000615
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700616exit_onqueue:
617 remove_wait_queue(&dev->write_wait, &waita);
Steven Haigh03270632006-08-09 07:42:06 +1000618 return retval;
619}
620
621/* file operations needed when we register this driver */
Arjan van de Ven00977a52007-02-12 00:55:34 -0800622static const struct file_operations adu_fops = {
Steven Haigh03270632006-08-09 07:42:06 +1000623 .owner = THIS_MODULE,
624 .read = adu_read,
625 .write = adu_write,
626 .open = adu_open,
627 .release = adu_release,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200628 .llseek = noop_llseek,
Steven Haigh03270632006-08-09 07:42:06 +1000629};
630
631/*
632 * usb class driver info in order to get a minor number from the usb core,
633 * and to have the device registered with devfs and the driver core
634 */
635static struct usb_class_driver adu_class = {
636 .name = "usb/adutux%d",
637 .fops = &adu_fops,
638 .minor_base = ADU_MINOR_BASE,
639};
640
641/**
642 * adu_probe
643 *
644 * Called by the usb core when a new device is connected that it thinks
645 * this driver might be interested in.
646 */
647static int adu_probe(struct usb_interface *interface,
648 const struct usb_device_id *id)
649{
650 struct usb_device *udev = interface_to_usbdev(interface);
651 struct adu_device *dev = NULL;
Johan Hovolde0e90522017-03-17 11:35:33 +0100652 int retval = -ENOMEM;
Steven Haigh03270632006-08-09 07:42:06 +1000653 int in_end_size;
654 int out_end_size;
Johan Hovolde53e0342017-03-17 11:35:34 +0100655 int res;
Steven Haigh03270632006-08-09 07:42:06 +1000656
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400657 /* allocate memory for our device state and initialize it */
Steven Haigh03270632006-08-09 07:42:06 +1000658 dev = kzalloc(sizeof(struct adu_device), GFP_KERNEL);
Johan Hovolde0e90522017-03-17 11:35:33 +0100659 if (!dev)
660 return -ENOMEM;
Steven Haigh03270632006-08-09 07:42:06 +1000661
Matthias Kaehlcke8293c562007-07-13 21:28:31 +0200662 mutex_init(&dev->mtx);
Steven Haigh03270632006-08-09 07:42:06 +1000663 spin_lock_init(&dev->buflock);
664 dev->udev = udev;
665 init_waitqueue_head(&dev->read_wait);
666 init_waitqueue_head(&dev->write_wait);
667
Johan Hovolde53e0342017-03-17 11:35:34 +0100668 res = usb_find_common_endpoints_reverse(&interface->altsetting[0],
669 NULL, NULL,
670 &dev->interrupt_in_endpoint,
671 &dev->interrupt_out_endpoint);
672 if (res) {
673 dev_err(&interface->dev, "interrupt endpoints not found\n");
674 retval = res;
Steven Haigh03270632006-08-09 07:42:06 +1000675 goto error;
676 }
677
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700678 in_end_size = usb_endpoint_maxp(dev->interrupt_in_endpoint);
679 out_end_size = usb_endpoint_maxp(dev->interrupt_out_endpoint);
Steven Haigh03270632006-08-09 07:42:06 +1000680
681 dev->read_buffer_primary = kmalloc((4 * in_end_size), GFP_KERNEL);
Johan Hovolde0e90522017-03-17 11:35:33 +0100682 if (!dev->read_buffer_primary)
Steven Haigh03270632006-08-09 07:42:06 +1000683 goto error;
Steven Haigh03270632006-08-09 07:42:06 +1000684
685 /* debug code prime the buffer */
686 memset(dev->read_buffer_primary, 'a', in_end_size);
687 memset(dev->read_buffer_primary + in_end_size, 'b', in_end_size);
688 memset(dev->read_buffer_primary + (2 * in_end_size), 'c', in_end_size);
689 memset(dev->read_buffer_primary + (3 * in_end_size), 'd', in_end_size);
690
691 dev->read_buffer_secondary = kmalloc((4 * in_end_size), GFP_KERNEL);
Johan Hovolde0e90522017-03-17 11:35:33 +0100692 if (!dev->read_buffer_secondary)
Steven Haigh03270632006-08-09 07:42:06 +1000693 goto error;
Steven Haigh03270632006-08-09 07:42:06 +1000694
695 /* debug code prime the buffer */
696 memset(dev->read_buffer_secondary, 'e', in_end_size);
697 memset(dev->read_buffer_secondary + in_end_size, 'f', in_end_size);
698 memset(dev->read_buffer_secondary + (2 * in_end_size), 'g', in_end_size);
699 memset(dev->read_buffer_secondary + (3 * in_end_size), 'h', in_end_size);
700
701 dev->interrupt_in_buffer = kmalloc(in_end_size, GFP_KERNEL);
Wolfram Sanga02b55c2016-08-25 19:39:11 +0200702 if (!dev->interrupt_in_buffer)
Steven Haigh03270632006-08-09 07:42:06 +1000703 goto error;
Steven Haigh03270632006-08-09 07:42:06 +1000704
705 /* debug code prime the buffer */
706 memset(dev->interrupt_in_buffer, 'i', in_end_size);
707
708 dev->interrupt_in_urb = usb_alloc_urb(0, GFP_KERNEL);
Wolfram Sang71574a52016-08-11 23:14:36 +0200709 if (!dev->interrupt_in_urb)
Steven Haigh03270632006-08-09 07:42:06 +1000710 goto error;
Steven Haigh03270632006-08-09 07:42:06 +1000711 dev->interrupt_out_buffer = kmalloc(out_end_size, GFP_KERNEL);
Wolfram Sanga02b55c2016-08-25 19:39:11 +0200712 if (!dev->interrupt_out_buffer)
Steven Haigh03270632006-08-09 07:42:06 +1000713 goto error;
Steven Haigh03270632006-08-09 07:42:06 +1000714 dev->interrupt_out_urb = usb_alloc_urb(0, GFP_KERNEL);
Wolfram Sang71574a52016-08-11 23:14:36 +0200715 if (!dev->interrupt_out_urb)
Steven Haigh03270632006-08-09 07:42:06 +1000716 goto error;
Steven Haigh03270632006-08-09 07:42:06 +1000717
718 if (!usb_string(udev, udev->descriptor.iSerialNumber, dev->serial_number,
719 sizeof(dev->serial_number))) {
720 dev_err(&interface->dev, "Could not retrieve serial number\n");
Johan Hovolde0e90522017-03-17 11:35:33 +0100721 retval = -EIO;
Steven Haigh03270632006-08-09 07:42:06 +1000722 goto error;
723 }
Greg Kroah-Hartman66d4bc32013-06-26 16:30:44 -0700724 dev_dbg(&interface->dev,"serial_number=%s", dev->serial_number);
Steven Haigh03270632006-08-09 07:42:06 +1000725
726 /* we can register the device now, as it is ready */
727 usb_set_intfdata(interface, dev);
728
729 retval = usb_register_dev(interface, &adu_class);
730
731 if (retval) {
732 /* something prevented us from registering this driver */
733 dev_err(&interface->dev, "Not able to get a minor for this device.\n");
734 usb_set_intfdata(interface, NULL);
735 goto error;
736 }
737
738 dev->minor = interface->minor;
739
740 /* let the user know what node this device is now attached to */
Joe Perches898eb712007-10-18 03:06:30 -0700741 dev_info(&interface->dev, "ADU%d %s now attached to /dev/usb/adutux%d\n",
Johan Hovoldd482b9d2013-08-11 16:49:22 +0200742 le16_to_cpu(udev->descriptor.idProduct), dev->serial_number,
Steven Haigh03270632006-08-09 07:42:06 +1000743 (dev->minor - ADU_MINOR_BASE));
Johan Hovolde0e90522017-03-17 11:35:33 +0100744
745 return 0;
Steven Haigh03270632006-08-09 07:42:06 +1000746
747error:
748 adu_delete(dev);
749 return retval;
750}
751
752/**
753 * adu_disconnect
754 *
755 * Called by the usb core when the device is removed from the system.
756 */
757static void adu_disconnect(struct usb_interface *interface)
758{
759 struct adu_device *dev;
Steven Haigh03270632006-08-09 07:42:06 +1000760
Steven Haigh03270632006-08-09 07:42:06 +1000761 dev = usb_get_intfdata(interface);
Steven Haigh03270632006-08-09 07:42:06 +1000762
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700763 mutex_lock(&dev->mtx); /* not interruptible */
764 dev->udev = NULL; /* poison */
Steven Haigh03270632006-08-09 07:42:06 +1000765 usb_deregister_dev(interface, &adu_class);
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700766 mutex_unlock(&dev->mtx);
Steven Haigh03270632006-08-09 07:42:06 +1000767
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700768 mutex_lock(&adutux_mutex);
769 usb_set_intfdata(interface, NULL);
Alan Sternd4ead162007-05-22 11:46:41 -0400770
Steven Haigh03270632006-08-09 07:42:06 +1000771 /* if the device is not opened, then we clean up right now */
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700772 if (!dev->open_count)
Steven Haigh03270632006-08-09 07:42:06 +1000773 adu_delete(dev);
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700774
775 mutex_unlock(&adutux_mutex);
Steven Haigh03270632006-08-09 07:42:06 +1000776}
777
778/* usb specific object needed to register this driver with the usb subsystem */
779static struct usb_driver adu_driver = {
780 .name = "adutux",
781 .probe = adu_probe,
782 .disconnect = adu_disconnect,
783 .id_table = device_table,
784};
785
Greg Kroah-Hartman65db4302011-11-18 09:34:02 -0800786module_usb_driver(adu_driver);
Steven Haigh03270632006-08-09 07:42:06 +1000787
788MODULE_AUTHOR(DRIVER_AUTHOR);
789MODULE_DESCRIPTION(DRIVER_DESC);
790MODULE_LICENSE("GPL");