blob: a7eefe11f31aab79cce4e4ece8a02feaac969a25 [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
Kirill Kapranovcc9debf2018-02-17 23:02:10 +020057#define COMMAND_TIMEOUT (2*HZ)
Steven Haigh03270632006-08-09 07:42:06 +100058
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 */
Johan Hovoldb2fa7ba2019-09-25 11:29:13 +020078 unsigned long disconnected:1;
Steven Haigh03270632006-08-09 07:42:06 +100079
Lisa Nguyen30c5c642013-05-13 12:41:34 -070080 char *read_buffer_primary;
Steven Haigh03270632006-08-09 07:42:06 +100081 int read_buffer_length;
Lisa Nguyen30c5c642013-05-13 12:41:34 -070082 char *read_buffer_secondary;
Steven Haigh03270632006-08-09 07:42:06 +100083 int secondary_head;
84 int secondary_tail;
85 spinlock_t buflock;
86
87 wait_queue_head_t read_wait;
88 wait_queue_head_t write_wait;
89
Lisa Nguyen30c5c642013-05-13 12:41:34 -070090 char *interrupt_in_buffer;
91 struct usb_endpoint_descriptor *interrupt_in_endpoint;
92 struct urb *interrupt_in_urb;
Steven Haigh03270632006-08-09 07:42:06 +100093 int read_urb_finished;
94
Lisa Nguyen30c5c642013-05-13 12:41:34 -070095 char *interrupt_out_buffer;
96 struct usb_endpoint_descriptor *interrupt_out_endpoint;
97 struct urb *interrupt_out_urb;
Pete Zaitcevf08812d2007-10-31 15:59:30 -070098 int out_urb_finished;
Steven Haigh03270632006-08-09 07:42:06 +100099};
100
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700101static DEFINE_MUTEX(adutux_mutex);
102
Steven Haigh03270632006-08-09 07:42:06 +1000103static struct usb_driver adu_driver;
104
Greg Kroah-Hartman1ef37c62013-06-26 16:30:45 -0700105static inline void adu_debug_data(struct device *dev, const char *function,
106 int size, const unsigned char *data)
Steven Haigh03270632006-08-09 07:42:06 +1000107{
Greg Kroah-Hartman1ef37c62013-06-26 16:30:45 -0700108 dev_dbg(dev, "%s - length = %d, data = %*ph\n",
109 function, size, size, data);
Steven Haigh03270632006-08-09 07:42:06 +1000110}
111
Lee Jones874ae832020-07-03 18:41:24 +0100112/*
Steven Haigh03270632006-08-09 07:42:06 +1000113 * adu_abort_transfers
114 * aborts transfers and frees associated data structures
115 */
116static void adu_abort_transfers(struct adu_device *dev)
117{
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700118 unsigned long flags;
Steven Haigh03270632006-08-09 07:42:06 +1000119
Johan Hovoldb2fa7ba2019-09-25 11:29:13 +0200120 if (dev->disconnected)
Greg Kroah-Hartman6e42a152013-06-26 16:30:43 -0700121 return;
Steven Haigh03270632006-08-09 07:42:06 +1000122
Steven Haigh03270632006-08-09 07:42:06 +1000123 /* shutdown transfer */
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700124
125 /* XXX Anchor these instead */
126 spin_lock_irqsave(&dev->buflock, flags);
127 if (!dev->read_urb_finished) {
128 spin_unlock_irqrestore(&dev->buflock, flags);
129 usb_kill_urb(dev->interrupt_in_urb);
130 } else
131 spin_unlock_irqrestore(&dev->buflock, flags);
132
133 spin_lock_irqsave(&dev->buflock, flags);
134 if (!dev->out_urb_finished) {
135 spin_unlock_irqrestore(&dev->buflock, flags);
Kirill Kapranov687ca632018-02-17 23:01:40 +0200136 wait_event_timeout(dev->write_wait, dev->out_urb_finished,
137 COMMAND_TIMEOUT);
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700138 usb_kill_urb(dev->interrupt_out_urb);
139 } else
140 spin_unlock_irqrestore(&dev->buflock, flags);
Steven Haigh03270632006-08-09 07:42:06 +1000141}
142
143static void adu_delete(struct adu_device *dev)
144{
Steven Haigh03270632006-08-09 07:42:06 +1000145 /* free data structures */
146 usb_free_urb(dev->interrupt_in_urb);
147 usb_free_urb(dev->interrupt_out_urb);
148 kfree(dev->read_buffer_primary);
149 kfree(dev->read_buffer_secondary);
150 kfree(dev->interrupt_in_buffer);
151 kfree(dev->interrupt_out_buffer);
Johan Hovold123a0f12019-10-09 17:38:44 +0200152 usb_put_dev(dev->udev);
Steven Haigh03270632006-08-09 07:42:06 +1000153 kfree(dev);
Steven Haigh03270632006-08-09 07:42:06 +1000154}
155
David Howells7d12e782006-10-05 14:55:46 +0100156static void adu_interrupt_in_callback(struct urb *urb)
Steven Haigh03270632006-08-09 07:42:06 +1000157{
158 struct adu_device *dev = urb->context;
Greg Kroah-Hartman24497a02007-07-18 10:58:02 -0700159 int status = urb->status;
Sebastian Andrzej Siewior957ada72018-06-25 00:08:42 +0200160 unsigned long flags;
Steven Haigh03270632006-08-09 07:42:06 +1000161
Greg Kroah-Hartman1ef37c62013-06-26 16:30:45 -0700162 adu_debug_data(&dev->udev->dev, __func__,
163 urb->actual_length, urb->transfer_buffer);
Steven Haigh03270632006-08-09 07:42:06 +1000164
Sebastian Andrzej Siewior957ada72018-06-25 00:08:42 +0200165 spin_lock_irqsave(&dev->buflock, flags);
Steven Haigh03270632006-08-09 07:42:06 +1000166
Greg Kroah-Hartman24497a02007-07-18 10:58:02 -0700167 if (status != 0) {
Oliver Neukumf6c1cea2007-08-16 16:02:08 +0200168 if ((status != -ENOENT) && (status != -ECONNRESET) &&
169 (status != -ESHUTDOWN)) {
Greg Kroah-Hartman66d4bc32013-06-26 16:30:44 -0700170 dev_dbg(&dev->udev->dev,
171 "%s : nonzero status received: %d\n",
172 __func__, status);
Steven Haigh03270632006-08-09 07:42:06 +1000173 }
174 goto exit;
175 }
176
177 if (urb->actual_length > 0 && dev->interrupt_in_buffer[0] != 0x00) {
178 if (dev->read_buffer_length <
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700179 (4 * usb_endpoint_maxp(dev->interrupt_in_endpoint)) -
Steven Haigh03270632006-08-09 07:42:06 +1000180 (urb->actual_length)) {
181 memcpy (dev->read_buffer_primary +
182 dev->read_buffer_length,
183 dev->interrupt_in_buffer, urb->actual_length);
184
185 dev->read_buffer_length += urb->actual_length;
Greg Kroah-Hartman66d4bc32013-06-26 16:30:44 -0700186 dev_dbg(&dev->udev->dev,"%s reading %d\n", __func__,
187 urb->actual_length);
Steven Haigh03270632006-08-09 07:42:06 +1000188 } else {
Greg Kroah-Hartman66d4bc32013-06-26 16:30:44 -0700189 dev_dbg(&dev->udev->dev,"%s : read_buffer overflow\n",
190 __func__);
Steven Haigh03270632006-08-09 07:42:06 +1000191 }
192 }
193
194exit:
195 dev->read_urb_finished = 1;
Sebastian Andrzej Siewior957ada72018-06-25 00:08:42 +0200196 spin_unlock_irqrestore(&dev->buflock, flags);
Steven Haigh03270632006-08-09 07:42:06 +1000197 /* always wake up so we recover from errors */
198 wake_up_interruptible(&dev->read_wait);
Steven Haigh03270632006-08-09 07:42:06 +1000199}
200
David Howells7d12e782006-10-05 14:55:46 +0100201static void adu_interrupt_out_callback(struct urb *urb)
Steven Haigh03270632006-08-09 07:42:06 +1000202{
203 struct adu_device *dev = urb->context;
Greg Kroah-Hartman24497a02007-07-18 10:58:02 -0700204 int status = urb->status;
Sebastian Andrzej Siewior957ada72018-06-25 00:08:42 +0200205 unsigned long flags;
Steven Haigh03270632006-08-09 07:42:06 +1000206
Greg Kroah-Hartman1ef37c62013-06-26 16:30:45 -0700207 adu_debug_data(&dev->udev->dev, __func__,
208 urb->actual_length, urb->transfer_buffer);
Steven Haigh03270632006-08-09 07:42:06 +1000209
Greg Kroah-Hartman24497a02007-07-18 10:58:02 -0700210 if (status != 0) {
211 if ((status != -ENOENT) &&
212 (status != -ECONNRESET)) {
Greg Kroah-Hartman66d4bc32013-06-26 16:30:44 -0700213 dev_dbg(&dev->udev->dev,
214 "%s :nonzero status received: %d\n", __func__,
215 status);
Steven Haigh03270632006-08-09 07:42:06 +1000216 }
Greg Kroah-Hartman1ef37c62013-06-26 16:30:45 -0700217 return;
Steven Haigh03270632006-08-09 07:42:06 +1000218 }
219
Sebastian Andrzej Siewior957ada72018-06-25 00:08:42 +0200220 spin_lock_irqsave(&dev->buflock, flags);
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700221 dev->out_urb_finished = 1;
222 wake_up(&dev->write_wait);
Sebastian Andrzej Siewior957ada72018-06-25 00:08:42 +0200223 spin_unlock_irqrestore(&dev->buflock, flags);
Steven Haigh03270632006-08-09 07:42:06 +1000224}
225
226static int adu_open(struct inode *inode, struct file *file)
227{
228 struct adu_device *dev = NULL;
229 struct usb_interface *interface;
230 int subminor;
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700231 int retval;
Steven Haigh03270632006-08-09 07:42:06 +1000232
Steven Haigh03270632006-08-09 07:42:06 +1000233 subminor = iminor(inode);
234
Lisa Nguyene77c4e62013-05-15 15:21:07 -0700235 retval = mutex_lock_interruptible(&adutux_mutex);
Greg Kroah-Hartman66d4bc32013-06-26 16:30:44 -0700236 if (retval)
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700237 goto exit_no_lock;
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700238
Steven Haigh03270632006-08-09 07:42:06 +1000239 interface = usb_find_interface(&adu_driver, subminor);
240 if (!interface) {
Greg Kroah-Hartman28f47c32013-06-26 16:30:46 -0700241 pr_err("%s - error, can't find device for minor %d\n",
242 __func__, subminor);
Steven Haigh03270632006-08-09 07:42:06 +1000243 retval = -ENODEV;
244 goto exit_no_device;
245 }
246
247 dev = usb_get_intfdata(interface);
Johan Hovoldb2fa7ba2019-09-25 11:29:13 +0200248 if (!dev) {
Steven Haigh03270632006-08-09 07:42:06 +1000249 retval = -ENODEV;
250 goto exit_no_device;
251 }
252
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700253 /* check that nobody else is using the device */
254 if (dev->open_count) {
255 retval = -EBUSY;
Steven Haigh03270632006-08-09 07:42:06 +1000256 goto exit_no_device;
257 }
258
Steven Haigh03270632006-08-09 07:42:06 +1000259 ++dev->open_count;
Greg Kroah-Hartman66d4bc32013-06-26 16:30:44 -0700260 dev_dbg(&dev->udev->dev, "%s: open count %d\n", __func__,
261 dev->open_count);
Steven Haigh03270632006-08-09 07:42:06 +1000262
263 /* save device in the file's private structure */
264 file->private_data = dev;
265
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700266 /* initialize in direction */
267 dev->read_buffer_length = 0;
Steven Haigh03270632006-08-09 07:42:06 +1000268
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700269 /* fixup first read by having urb waiting for it */
Lisa Nguyen05d76392013-05-13 12:41:54 -0700270 usb_fill_int_urb(dev->interrupt_in_urb, dev->udev,
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700271 usb_rcvintpipe(dev->udev,
272 dev->interrupt_in_endpoint->bEndpointAddress),
273 dev->interrupt_in_buffer,
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700274 usb_endpoint_maxp(dev->interrupt_in_endpoint),
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700275 adu_interrupt_in_callback, dev,
276 dev->interrupt_in_endpoint->bInterval);
277 dev->read_urb_finished = 0;
278 if (usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL))
279 dev->read_urb_finished = 1;
280 /* we ignore failure */
281 /* end of fixup for first read */
282
283 /* initialize out direction */
284 dev->out_urb_finished = 1;
285
286 retval = 0;
Steven Haigh03270632006-08-09 07:42:06 +1000287
288exit_no_device:
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700289 mutex_unlock(&adutux_mutex);
290exit_no_lock:
Steven Haigh03270632006-08-09 07:42:06 +1000291 return retval;
292}
293
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700294static void adu_release_internal(struct adu_device *dev)
Steven Haigh03270632006-08-09 07:42:06 +1000295{
Steven Haigh03270632006-08-09 07:42:06 +1000296 /* decrement our usage count for the device */
297 --dev->open_count;
Greg Kroah-Hartman66d4bc32013-06-26 16:30:44 -0700298 dev_dbg(&dev->udev->dev, "%s : open count %d\n", __func__,
299 dev->open_count);
Steven Haigh03270632006-08-09 07:42:06 +1000300 if (dev->open_count <= 0) {
301 adu_abort_transfers(dev);
302 dev->open_count = 0;
303 }
Steven Haigh03270632006-08-09 07:42:06 +1000304}
305
306static int adu_release(struct inode *inode, struct file *file)
307{
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700308 struct adu_device *dev;
Steven Haigh03270632006-08-09 07:42:06 +1000309 int retval = 0;
310
Steven Haigh03270632006-08-09 07:42:06 +1000311 if (file == NULL) {
Steven Haigh03270632006-08-09 07:42:06 +1000312 retval = -ENODEV;
313 goto exit;
314 }
315
316 dev = file->private_data;
Steven Haigh03270632006-08-09 07:42:06 +1000317 if (dev == NULL) {
Steven Haigh03270632006-08-09 07:42:06 +1000318 retval = -ENODEV;
319 goto exit;
320 }
321
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700322 mutex_lock(&adutux_mutex); /* not interruptible */
Steven Haigh03270632006-08-09 07:42:06 +1000323
324 if (dev->open_count <= 0) {
Greg Kroah-Hartman66d4bc32013-06-26 16:30:44 -0700325 dev_dbg(&dev->udev->dev, "%s : device not opened\n", __func__);
Steven Haigh03270632006-08-09 07:42:06 +1000326 retval = -ENODEV;
Jiri Slaby46c98442009-03-11 21:47:38 +0100327 goto unlock;
Steven Haigh03270632006-08-09 07:42:06 +1000328 }
329
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700330 adu_release_internal(dev);
Johan Hovoldb2fa7ba2019-09-25 11:29:13 +0200331 if (dev->disconnected) {
Alan Sternd4ead162007-05-22 11:46:41 -0400332 /* the device was unplugged before the file was released */
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700333 if (!dev->open_count) /* ... and we're the last user */
334 adu_delete(dev);
Alan Sternd4ead162007-05-22 11:46:41 -0400335 }
Jiri Slaby46c98442009-03-11 21:47:38 +0100336unlock:
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700337 mutex_unlock(&adutux_mutex);
Jiri Slaby46c98442009-03-11 21:47:38 +0100338exit:
Steven Haigh03270632006-08-09 07:42:06 +1000339 return retval;
340}
341
342static ssize_t adu_read(struct file *file, __user char *buffer, size_t count,
343 loff_t *ppos)
344{
345 struct adu_device *dev;
346 size_t bytes_read = 0;
347 size_t bytes_to_read = count;
Steven Haigh03270632006-08-09 07:42:06 +1000348 int retval = 0;
349 int timeout = 0;
350 int should_submit = 0;
351 unsigned long flags;
352 DECLARE_WAITQUEUE(wait, current);
353
Steven Haigh03270632006-08-09 07:42:06 +1000354 dev = file->private_data;
Matthias Kaehlcke8293c562007-07-13 21:28:31 +0200355 if (mutex_lock_interruptible(&dev->mtx))
Steven Haigh03270632006-08-09 07:42:06 +1000356 return -ERESTARTSYS;
357
358 /* verify that the device wasn't unplugged */
Johan Hovoldb2fa7ba2019-09-25 11:29:13 +0200359 if (dev->disconnected) {
Steven Haigh03270632006-08-09 07:42:06 +1000360 retval = -ENODEV;
Greg Kroah-Hartman28f47c32013-06-26 16:30:46 -0700361 pr_err("No device or device unplugged %d\n", retval);
Steven Haigh03270632006-08-09 07:42:06 +1000362 goto exit;
363 }
364
365 /* verify that some data was requested */
366 if (count == 0) {
Greg Kroah-Hartman66d4bc32013-06-26 16:30:44 -0700367 dev_dbg(&dev->udev->dev, "%s : read request of 0 bytes\n",
368 __func__);
Steven Haigh03270632006-08-09 07:42:06 +1000369 goto exit;
370 }
371
372 timeout = COMMAND_TIMEOUT;
Greg Kroah-Hartman66d4bc32013-06-26 16:30:44 -0700373 dev_dbg(&dev->udev->dev, "%s : about to start looping\n", __func__);
Steven Haigh03270632006-08-09 07:42:06 +1000374 while (bytes_to_read) {
Daniel M German4850f26a2019-06-21 08:22:40 -0700375 size_t data_in_secondary = dev->secondary_tail - dev->secondary_head;
Greg Kroah-Hartman66d4bc32013-06-26 16:30:44 -0700376 dev_dbg(&dev->udev->dev,
Daniel M German4850f26a2019-06-21 08:22:40 -0700377 "%s : while, data_in_secondary=%zu, status=%d\n",
Greg Kroah-Hartman66d4bc32013-06-26 16:30:44 -0700378 __func__, data_in_secondary,
379 dev->interrupt_in_urb->status);
Steven Haigh03270632006-08-09 07:42:06 +1000380
381 if (data_in_secondary) {
382 /* drain secondary buffer */
Daniel M German4850f26a2019-06-21 08:22:40 -0700383 size_t amount = min(bytes_to_read, data_in_secondary);
384 if (copy_to_user(buffer, dev->read_buffer_secondary+dev->secondary_head, amount)) {
Steven Haigh03270632006-08-09 07:42:06 +1000385 retval = -EFAULT;
386 goto exit;
387 }
Daniel M German4850f26a2019-06-21 08:22:40 -0700388 dev->secondary_head += amount;
389 bytes_read += amount;
390 bytes_to_read -= amount;
Steven Haigh03270632006-08-09 07:42:06 +1000391 } else {
392 /* we check the primary buffer */
393 spin_lock_irqsave (&dev->buflock, flags);
394 if (dev->read_buffer_length) {
395 /* we secure access to the primary */
396 char *tmp;
Greg Kroah-Hartman66d4bc32013-06-26 16:30:44 -0700397 dev_dbg(&dev->udev->dev,
398 "%s : swap, read_buffer_length = %d\n",
399 __func__, dev->read_buffer_length);
Steven Haigh03270632006-08-09 07:42:06 +1000400 tmp = dev->read_buffer_secondary;
401 dev->read_buffer_secondary = dev->read_buffer_primary;
402 dev->read_buffer_primary = tmp;
403 dev->secondary_head = 0;
404 dev->secondary_tail = dev->read_buffer_length;
405 dev->read_buffer_length = 0;
406 spin_unlock_irqrestore(&dev->buflock, flags);
407 /* we have a free buffer so use it */
408 should_submit = 1;
409 } else {
410 /* even the primary was empty - we may need to do IO */
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700411 if (!dev->read_urb_finished) {
Steven Haigh03270632006-08-09 07:42:06 +1000412 /* somebody is doing IO */
413 spin_unlock_irqrestore(&dev->buflock, flags);
Greg Kroah-Hartman66d4bc32013-06-26 16:30:44 -0700414 dev_dbg(&dev->udev->dev,
415 "%s : submitted already\n",
416 __func__);
Steven Haigh03270632006-08-09 07:42:06 +1000417 } else {
418 /* we must initiate input */
Greg Kroah-Hartman66d4bc32013-06-26 16:30:44 -0700419 dev_dbg(&dev->udev->dev,
420 "%s : initiate input\n",
421 __func__);
Steven Haigh03270632006-08-09 07:42:06 +1000422 dev->read_urb_finished = 0;
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700423 spin_unlock_irqrestore(&dev->buflock, flags);
Steven Haigh03270632006-08-09 07:42:06 +1000424
Lisa Nguyen05d76392013-05-13 12:41:54 -0700425 usb_fill_int_urb(dev->interrupt_in_urb, dev->udev,
Lisa Nguyeneb79c012013-05-13 12:41:10 -0700426 usb_rcvintpipe(dev->udev,
427 dev->interrupt_in_endpoint->bEndpointAddress),
Steven Haigh03270632006-08-09 07:42:06 +1000428 dev->interrupt_in_buffer,
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700429 usb_endpoint_maxp(dev->interrupt_in_endpoint),
Steven Haigh03270632006-08-09 07:42:06 +1000430 adu_interrupt_in_callback,
431 dev,
432 dev->interrupt_in_endpoint->bInterval);
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700433 retval = usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL);
434 if (retval) {
435 dev->read_urb_finished = 1;
Steven Haigh03270632006-08-09 07:42:06 +1000436 if (retval == -ENOMEM) {
437 retval = bytes_read ? bytes_read : -ENOMEM;
438 }
Greg Kroah-Hartman66d4bc32013-06-26 16:30:44 -0700439 dev_dbg(&dev->udev->dev,
440 "%s : submit failed\n",
441 __func__);
Steven Haigh03270632006-08-09 07:42:06 +1000442 goto exit;
443 }
444 }
445
446 /* we wait for I/O to complete */
447 set_current_state(TASK_INTERRUPTIBLE);
448 add_wait_queue(&dev->read_wait, &wait);
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700449 spin_lock_irqsave(&dev->buflock, flags);
450 if (!dev->read_urb_finished) {
451 spin_unlock_irqrestore(&dev->buflock, flags);
Steven Haigh03270632006-08-09 07:42:06 +1000452 timeout = schedule_timeout(COMMAND_TIMEOUT);
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700453 } else {
454 spin_unlock_irqrestore(&dev->buflock, flags);
Steven Haigh03270632006-08-09 07:42:06 +1000455 set_current_state(TASK_RUNNING);
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700456 }
Steven Haigh03270632006-08-09 07:42:06 +1000457 remove_wait_queue(&dev->read_wait, &wait);
458
459 if (timeout <= 0) {
Greg Kroah-Hartman66d4bc32013-06-26 16:30:44 -0700460 dev_dbg(&dev->udev->dev,
461 "%s : timeout\n", __func__);
Steven Haigh03270632006-08-09 07:42:06 +1000462 retval = bytes_read ? bytes_read : -ETIMEDOUT;
463 goto exit;
464 }
465
466 if (signal_pending(current)) {
Greg Kroah-Hartman66d4bc32013-06-26 16:30:44 -0700467 dev_dbg(&dev->udev->dev,
468 "%s : signal pending\n",
469 __func__);
Steven Haigh03270632006-08-09 07:42:06 +1000470 retval = bytes_read ? bytes_read : -EINTR;
471 goto exit;
472 }
473 }
474 }
475 }
476
477 retval = bytes_read;
478 /* if the primary buffer is empty then use it */
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700479 spin_lock_irqsave(&dev->buflock, flags);
480 if (should_submit && dev->read_urb_finished) {
481 dev->read_urb_finished = 0;
482 spin_unlock_irqrestore(&dev->buflock, flags);
Lisa Nguyen05d76392013-05-13 12:41:54 -0700483 usb_fill_int_urb(dev->interrupt_in_urb, dev->udev,
Steven Haigh03270632006-08-09 07:42:06 +1000484 usb_rcvintpipe(dev->udev,
Lisa Nguyeneb79c012013-05-13 12:41:10 -0700485 dev->interrupt_in_endpoint->bEndpointAddress),
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700486 dev->interrupt_in_buffer,
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700487 usb_endpoint_maxp(dev->interrupt_in_endpoint),
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700488 adu_interrupt_in_callback,
489 dev,
490 dev->interrupt_in_endpoint->bInterval);
491 if (usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL) != 0)
492 dev->read_urb_finished = 1;
Steven Haigh03270632006-08-09 07:42:06 +1000493 /* we ignore failure */
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700494 } else {
495 spin_unlock_irqrestore(&dev->buflock, flags);
Steven Haigh03270632006-08-09 07:42:06 +1000496 }
497
498exit:
499 /* unlock the device */
Matthias Kaehlcke8293c562007-07-13 21:28:31 +0200500 mutex_unlock(&dev->mtx);
Steven Haigh03270632006-08-09 07:42:06 +1000501
Steven Haigh03270632006-08-09 07:42:06 +1000502 return retval;
503}
504
505static ssize_t adu_write(struct file *file, const __user char *buffer,
506 size_t count, loff_t *ppos)
507{
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700508 DECLARE_WAITQUEUE(waita, current);
Steven Haigh03270632006-08-09 07:42:06 +1000509 struct adu_device *dev;
510 size_t bytes_written = 0;
511 size_t bytes_to_write;
512 size_t buffer_size;
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700513 unsigned long flags;
Oliver Neukumebc3ac12007-04-02 15:16:36 +0200514 int retval;
Steven Haigh03270632006-08-09 07:42:06 +1000515
Steven Haigh03270632006-08-09 07:42:06 +1000516 dev = file->private_data;
517
Matthias Kaehlcke8293c562007-07-13 21:28:31 +0200518 retval = mutex_lock_interruptible(&dev->mtx);
Oliver Neukumebc3ac12007-04-02 15:16:36 +0200519 if (retval)
520 goto exit_nolock;
Steven Haigh03270632006-08-09 07:42:06 +1000521
522 /* verify that the device wasn't unplugged */
Johan Hovoldb2fa7ba2019-09-25 11:29:13 +0200523 if (dev->disconnected) {
Steven Haigh03270632006-08-09 07:42:06 +1000524 retval = -ENODEV;
Greg Kroah-Hartman28f47c32013-06-26 16:30:46 -0700525 pr_err("No device or device unplugged %d\n", retval);
Steven Haigh03270632006-08-09 07:42:06 +1000526 goto exit;
527 }
528
529 /* verify that we actually have some data to write */
530 if (count == 0) {
Greg Kroah-Hartman66d4bc32013-06-26 16:30:44 -0700531 dev_dbg(&dev->udev->dev, "%s : write request of 0 bytes\n",
532 __func__);
Steven Haigh03270632006-08-09 07:42:06 +1000533 goto exit;
534 }
535
Steven Haigh03270632006-08-09 07:42:06 +1000536 while (count > 0) {
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700537 add_wait_queue(&dev->write_wait, &waita);
538 set_current_state(TASK_INTERRUPTIBLE);
539 spin_lock_irqsave(&dev->buflock, flags);
540 if (!dev->out_urb_finished) {
541 spin_unlock_irqrestore(&dev->buflock, flags);
Steven Haigh03270632006-08-09 07:42:06 +1000542
Matthias Kaehlcke8293c562007-07-13 21:28:31 +0200543 mutex_unlock(&dev->mtx);
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700544 if (signal_pending(current)) {
Greg Kroah-Hartman66d4bc32013-06-26 16:30:44 -0700545 dev_dbg(&dev->udev->dev, "%s : interrupted\n",
546 __func__);
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700547 set_current_state(TASK_RUNNING);
548 retval = -EINTR;
549 goto exit_onqueue;
550 }
551 if (schedule_timeout(COMMAND_TIMEOUT) == 0) {
Greg Kroah-Hartman66d4bc32013-06-26 16:30:44 -0700552 dev_dbg(&dev->udev->dev,
553 "%s - command timed out.\n", __func__);
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700554 retval = -ETIMEDOUT;
555 goto exit_onqueue;
556 }
557 remove_wait_queue(&dev->write_wait, &waita);
Matthias Kaehlcke8293c562007-07-13 21:28:31 +0200558 retval = mutex_lock_interruptible(&dev->mtx);
Oliver Neukumebc3ac12007-04-02 15:16:36 +0200559 if (retval) {
560 retval = bytes_written ? bytes_written : retval;
561 goto exit_nolock;
562 }
Steven Haigh03270632006-08-09 07:42:06 +1000563
Greg Kroah-Hartman66d4bc32013-06-26 16:30:44 -0700564 dev_dbg(&dev->udev->dev,
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -0800565 "%s : in progress, count = %zd\n",
Greg Kroah-Hartman66d4bc32013-06-26 16:30:44 -0700566 __func__, count);
Steven Haigh03270632006-08-09 07:42:06 +1000567 } else {
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700568 spin_unlock_irqrestore(&dev->buflock, flags);
569 set_current_state(TASK_RUNNING);
570 remove_wait_queue(&dev->write_wait, &waita);
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -0800571 dev_dbg(&dev->udev->dev, "%s : sending, count = %zd\n",
Greg Kroah-Hartman66d4bc32013-06-26 16:30:44 -0700572 __func__, count);
Steven Haigh03270632006-08-09 07:42:06 +1000573
574 /* write the data into interrupt_out_buffer from userspace */
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700575 buffer_size = usb_endpoint_maxp(dev->interrupt_out_endpoint);
Steven Haigh03270632006-08-09 07:42:06 +1000576 bytes_to_write = count > buffer_size ? buffer_size : count;
Greg Kroah-Hartman66d4bc32013-06-26 16:30:44 -0700577 dev_dbg(&dev->udev->dev,
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -0800578 "%s : buffer_size = %zd, count = %zd, bytes_to_write = %zd\n",
Greg Kroah-Hartman66d4bc32013-06-26 16:30:44 -0700579 __func__, buffer_size, count, bytes_to_write);
Steven Haigh03270632006-08-09 07:42:06 +1000580
581 if (copy_from_user(dev->interrupt_out_buffer, buffer, bytes_to_write) != 0) {
582 retval = -EFAULT;
583 goto exit;
584 }
585
586 /* send off the urb */
587 usb_fill_int_urb(
588 dev->interrupt_out_urb,
589 dev->udev,
590 usb_sndintpipe(dev->udev, dev->interrupt_out_endpoint->bEndpointAddress),
591 dev->interrupt_out_buffer,
592 bytes_to_write,
593 adu_interrupt_out_callback,
594 dev,
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700595 dev->interrupt_out_endpoint->bInterval);
Steven Haigh03270632006-08-09 07:42:06 +1000596 dev->interrupt_out_urb->actual_length = bytes_to_write;
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700597 dev->out_urb_finished = 0;
Steven Haigh03270632006-08-09 07:42:06 +1000598 retval = usb_submit_urb(dev->interrupt_out_urb, GFP_KERNEL);
599 if (retval < 0) {
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700600 dev->out_urb_finished = 1;
Greg Kroah-Hartmanfd3f1912008-08-14 09:37:34 -0700601 dev_err(&dev->udev->dev, "Couldn't submit "
602 "interrupt_out_urb %d\n", retval);
Steven Haigh03270632006-08-09 07:42:06 +1000603 goto exit;
604 }
605
606 buffer += bytes_to_write;
607 count -= bytes_to_write;
608
609 bytes_written += bytes_to_write;
610 }
611 }
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700612 mutex_unlock(&dev->mtx);
613 return bytes_written;
Steven Haigh03270632006-08-09 07:42:06 +1000614
615exit:
Matthias Kaehlcke8293c562007-07-13 21:28:31 +0200616 mutex_unlock(&dev->mtx);
Oliver Neukumebc3ac12007-04-02 15:16:36 +0200617exit_nolock:
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700618 return retval;
Steven Haigh03270632006-08-09 07:42:06 +1000619
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700620exit_onqueue:
621 remove_wait_queue(&dev->write_wait, &waita);
Steven Haigh03270632006-08-09 07:42:06 +1000622 return retval;
623}
624
625/* file operations needed when we register this driver */
Arjan van de Ven00977a52007-02-12 00:55:34 -0800626static const struct file_operations adu_fops = {
Steven Haigh03270632006-08-09 07:42:06 +1000627 .owner = THIS_MODULE,
628 .read = adu_read,
629 .write = adu_write,
630 .open = adu_open,
631 .release = adu_release,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200632 .llseek = noop_llseek,
Steven Haigh03270632006-08-09 07:42:06 +1000633};
634
635/*
636 * usb class driver info in order to get a minor number from the usb core,
637 * and to have the device registered with devfs and the driver core
638 */
639static struct usb_class_driver adu_class = {
640 .name = "usb/adutux%d",
641 .fops = &adu_fops,
642 .minor_base = ADU_MINOR_BASE,
643};
644
Lee Jones874ae832020-07-03 18:41:24 +0100645/*
Steven Haigh03270632006-08-09 07:42:06 +1000646 * adu_probe
647 *
648 * Called by the usb core when a new device is connected that it thinks
649 * this driver might be interested in.
650 */
651static int adu_probe(struct usb_interface *interface,
652 const struct usb_device_id *id)
653{
654 struct usb_device *udev = interface_to_usbdev(interface);
655 struct adu_device *dev = NULL;
Johan Hovolde0e90522017-03-17 11:35:33 +0100656 int retval = -ENOMEM;
Steven Haigh03270632006-08-09 07:42:06 +1000657 int in_end_size;
658 int out_end_size;
Johan Hovolde53e0342017-03-17 11:35:34 +0100659 int res;
Steven Haigh03270632006-08-09 07:42:06 +1000660
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400661 /* allocate memory for our device state and initialize it */
Steven Haigh03270632006-08-09 07:42:06 +1000662 dev = kzalloc(sizeof(struct adu_device), GFP_KERNEL);
Johan Hovolde0e90522017-03-17 11:35:33 +0100663 if (!dev)
664 return -ENOMEM;
Steven Haigh03270632006-08-09 07:42:06 +1000665
Matthias Kaehlcke8293c562007-07-13 21:28:31 +0200666 mutex_init(&dev->mtx);
Steven Haigh03270632006-08-09 07:42:06 +1000667 spin_lock_init(&dev->buflock);
Johan Hovold123a0f12019-10-09 17:38:44 +0200668 dev->udev = usb_get_dev(udev);
Steven Haigh03270632006-08-09 07:42:06 +1000669 init_waitqueue_head(&dev->read_wait);
670 init_waitqueue_head(&dev->write_wait);
671
Johan Hovold3c11c4b2019-12-10 12:25:59 +0100672 res = usb_find_common_endpoints_reverse(interface->cur_altsetting,
Johan Hovolde53e0342017-03-17 11:35:34 +0100673 NULL, NULL,
674 &dev->interrupt_in_endpoint,
675 &dev->interrupt_out_endpoint);
676 if (res) {
677 dev_err(&interface->dev, "interrupt endpoints not found\n");
678 retval = res;
Steven Haigh03270632006-08-09 07:42:06 +1000679 goto error;
680 }
681
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700682 in_end_size = usb_endpoint_maxp(dev->interrupt_in_endpoint);
683 out_end_size = usb_endpoint_maxp(dev->interrupt_out_endpoint);
Steven Haigh03270632006-08-09 07:42:06 +1000684
685 dev->read_buffer_primary = kmalloc((4 * in_end_size), GFP_KERNEL);
Johan Hovolde0e90522017-03-17 11:35:33 +0100686 if (!dev->read_buffer_primary)
Steven Haigh03270632006-08-09 07:42:06 +1000687 goto error;
Steven Haigh03270632006-08-09 07:42:06 +1000688
689 /* debug code prime the buffer */
690 memset(dev->read_buffer_primary, 'a', in_end_size);
691 memset(dev->read_buffer_primary + in_end_size, 'b', in_end_size);
692 memset(dev->read_buffer_primary + (2 * in_end_size), 'c', in_end_size);
693 memset(dev->read_buffer_primary + (3 * in_end_size), 'd', in_end_size);
694
695 dev->read_buffer_secondary = kmalloc((4 * in_end_size), GFP_KERNEL);
Johan Hovolde0e90522017-03-17 11:35:33 +0100696 if (!dev->read_buffer_secondary)
Steven Haigh03270632006-08-09 07:42:06 +1000697 goto error;
Steven Haigh03270632006-08-09 07:42:06 +1000698
699 /* debug code prime the buffer */
700 memset(dev->read_buffer_secondary, 'e', in_end_size);
701 memset(dev->read_buffer_secondary + in_end_size, 'f', in_end_size);
702 memset(dev->read_buffer_secondary + (2 * in_end_size), 'g', in_end_size);
703 memset(dev->read_buffer_secondary + (3 * in_end_size), 'h', in_end_size);
704
705 dev->interrupt_in_buffer = kmalloc(in_end_size, GFP_KERNEL);
Wolfram Sanga02b55c2016-08-25 19:39:11 +0200706 if (!dev->interrupt_in_buffer)
Steven Haigh03270632006-08-09 07:42:06 +1000707 goto error;
Steven Haigh03270632006-08-09 07:42:06 +1000708
709 /* debug code prime the buffer */
710 memset(dev->interrupt_in_buffer, 'i', in_end_size);
711
712 dev->interrupt_in_urb = usb_alloc_urb(0, GFP_KERNEL);
Wolfram Sang71574a52016-08-11 23:14:36 +0200713 if (!dev->interrupt_in_urb)
Steven Haigh03270632006-08-09 07:42:06 +1000714 goto error;
Steven Haigh03270632006-08-09 07:42:06 +1000715 dev->interrupt_out_buffer = kmalloc(out_end_size, GFP_KERNEL);
Wolfram Sanga02b55c2016-08-25 19:39:11 +0200716 if (!dev->interrupt_out_buffer)
Steven Haigh03270632006-08-09 07:42:06 +1000717 goto error;
Steven Haigh03270632006-08-09 07:42:06 +1000718 dev->interrupt_out_urb = usb_alloc_urb(0, GFP_KERNEL);
Wolfram Sang71574a52016-08-11 23:14:36 +0200719 if (!dev->interrupt_out_urb)
Steven Haigh03270632006-08-09 07:42:06 +1000720 goto error;
Steven Haigh03270632006-08-09 07:42:06 +1000721
722 if (!usb_string(udev, udev->descriptor.iSerialNumber, dev->serial_number,
723 sizeof(dev->serial_number))) {
724 dev_err(&interface->dev, "Could not retrieve serial number\n");
Johan Hovolde0e90522017-03-17 11:35:33 +0100725 retval = -EIO;
Steven Haigh03270632006-08-09 07:42:06 +1000726 goto error;
727 }
Greg Kroah-Hartman66d4bc32013-06-26 16:30:44 -0700728 dev_dbg(&interface->dev,"serial_number=%s", dev->serial_number);
Steven Haigh03270632006-08-09 07:42:06 +1000729
730 /* we can register the device now, as it is ready */
731 usb_set_intfdata(interface, dev);
732
733 retval = usb_register_dev(interface, &adu_class);
734
735 if (retval) {
736 /* something prevented us from registering this driver */
737 dev_err(&interface->dev, "Not able to get a minor for this device.\n");
738 usb_set_intfdata(interface, NULL);
739 goto error;
740 }
741
742 dev->minor = interface->minor;
743
744 /* let the user know what node this device is now attached to */
Joe Perches898eb712007-10-18 03:06:30 -0700745 dev_info(&interface->dev, "ADU%d %s now attached to /dev/usb/adutux%d\n",
Johan Hovoldd482b9d2013-08-11 16:49:22 +0200746 le16_to_cpu(udev->descriptor.idProduct), dev->serial_number,
Steven Haigh03270632006-08-09 07:42:06 +1000747 (dev->minor - ADU_MINOR_BASE));
Johan Hovolde0e90522017-03-17 11:35:33 +0100748
749 return 0;
Steven Haigh03270632006-08-09 07:42:06 +1000750
751error:
752 adu_delete(dev);
753 return retval;
754}
755
Lee Jones874ae832020-07-03 18:41:24 +0100756/*
Steven Haigh03270632006-08-09 07:42:06 +1000757 * adu_disconnect
758 *
759 * Called by the usb core when the device is removed from the system.
760 */
761static void adu_disconnect(struct usb_interface *interface)
762{
763 struct adu_device *dev;
Steven Haigh03270632006-08-09 07:42:06 +1000764
Steven Haigh03270632006-08-09 07:42:06 +1000765 dev = usb_get_intfdata(interface);
Steven Haigh03270632006-08-09 07:42:06 +1000766
Steven Haigh03270632006-08-09 07:42:06 +1000767 usb_deregister_dev(interface, &adu_class);
Steven Haigh03270632006-08-09 07:42:06 +1000768
Johan Hovoldb2fa7ba2019-09-25 11:29:13 +0200769 usb_poison_urb(dev->interrupt_in_urb);
770 usb_poison_urb(dev->interrupt_out_urb);
771
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700772 mutex_lock(&adutux_mutex);
773 usb_set_intfdata(interface, NULL);
Alan Sternd4ead162007-05-22 11:46:41 -0400774
Johan Hovold44efc262019-09-25 11:29:12 +0200775 mutex_lock(&dev->mtx); /* not interruptible */
Johan Hovoldb2fa7ba2019-09-25 11:29:13 +0200776 dev->disconnected = 1;
Johan Hovold44efc262019-09-25 11:29:12 +0200777 mutex_unlock(&dev->mtx);
778
Steven Haigh03270632006-08-09 07:42:06 +1000779 /* if the device is not opened, then we clean up right now */
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700780 if (!dev->open_count)
Steven Haigh03270632006-08-09 07:42:06 +1000781 adu_delete(dev);
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700782
783 mutex_unlock(&adutux_mutex);
Steven Haigh03270632006-08-09 07:42:06 +1000784}
785
786/* usb specific object needed to register this driver with the usb subsystem */
787static struct usb_driver adu_driver = {
788 .name = "adutux",
789 .probe = adu_probe,
790 .disconnect = adu_disconnect,
791 .id_table = device_table,
792};
793
Greg Kroah-Hartman65db4302011-11-18 09:34:02 -0800794module_usb_driver(adu_driver);
Steven Haigh03270632006-08-09 07:42:06 +1000795
796MODULE_AUTHOR(DRIVER_AUTHOR);
797MODULE_DESCRIPTION(DRIVER_DESC);
798MODULE_LICENSE("GPL");