blob: eb3c8c142fa988de1aaaee07d036e93554bb48a3 [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
Lisa Nguyen05d76392013-05-13 12:41:54 -0700150 dbg(2, " %s : enter", __func__);
Steven Haigh03270632006-08-09 07:42:06 +1000151
152 if (dev->udev == NULL) {
Lisa Nguyen05d76392013-05-13 12:41:54 -0700153 dbg(1, " %s : udev is null", __func__);
Steven Haigh03270632006-08-09 07:42:06 +1000154 goto exit;
155 }
156
Steven Haigh03270632006-08-09 07:42:06 +1000157 /* shutdown transfer */
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700158
159 /* XXX Anchor these instead */
160 spin_lock_irqsave(&dev->buflock, flags);
161 if (!dev->read_urb_finished) {
162 spin_unlock_irqrestore(&dev->buflock, flags);
163 usb_kill_urb(dev->interrupt_in_urb);
164 } else
165 spin_unlock_irqrestore(&dev->buflock, flags);
166
167 spin_lock_irqsave(&dev->buflock, flags);
168 if (!dev->out_urb_finished) {
169 spin_unlock_irqrestore(&dev->buflock, flags);
170 usb_kill_urb(dev->interrupt_out_urb);
171 } else
172 spin_unlock_irqrestore(&dev->buflock, flags);
Steven Haigh03270632006-08-09 07:42:06 +1000173
174exit:
Lisa Nguyen05d76392013-05-13 12:41:54 -0700175 dbg(2, " %s : leave", __func__);
Steven Haigh03270632006-08-09 07:42:06 +1000176}
177
178static void adu_delete(struct adu_device *dev)
179{
Harvey Harrison441b62c2008-03-03 16:08:34 -0800180 dbg(2, "%s enter", __func__);
Steven Haigh03270632006-08-09 07:42:06 +1000181
Steven Haigh03270632006-08-09 07:42:06 +1000182 /* free data structures */
183 usb_free_urb(dev->interrupt_in_urb);
184 usb_free_urb(dev->interrupt_out_urb);
185 kfree(dev->read_buffer_primary);
186 kfree(dev->read_buffer_secondary);
187 kfree(dev->interrupt_in_buffer);
188 kfree(dev->interrupt_out_buffer);
189 kfree(dev);
190
Harvey Harrison441b62c2008-03-03 16:08:34 -0800191 dbg(2, "%s : leave", __func__);
Steven Haigh03270632006-08-09 07:42:06 +1000192}
193
David Howells7d12e782006-10-05 14:55:46 +0100194static void adu_interrupt_in_callback(struct urb *urb)
Steven Haigh03270632006-08-09 07:42:06 +1000195{
196 struct adu_device *dev = urb->context;
Greg Kroah-Hartman24497a02007-07-18 10:58:02 -0700197 int status = urb->status;
Steven Haigh03270632006-08-09 07:42:06 +1000198
Lisa Nguyen05d76392013-05-13 12:41:54 -0700199 dbg(4, " %s : enter, status %d", __func__, status);
Harvey Harrison441b62c2008-03-03 16:08:34 -0800200 adu_debug_data(5, __func__, urb->actual_length,
Steven Haigh03270632006-08-09 07:42:06 +1000201 urb->transfer_buffer);
202
203 spin_lock(&dev->buflock);
204
Greg Kroah-Hartman24497a02007-07-18 10:58:02 -0700205 if (status != 0) {
Oliver Neukumf6c1cea2007-08-16 16:02:08 +0200206 if ((status != -ENOENT) && (status != -ECONNRESET) &&
207 (status != -ESHUTDOWN)) {
Lisa Nguyen05d76392013-05-13 12:41:54 -0700208 dbg(1, " %s : nonzero status received: %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800209 __func__, status);
Steven Haigh03270632006-08-09 07:42:06 +1000210 }
211 goto exit;
212 }
213
214 if (urb->actual_length > 0 && dev->interrupt_in_buffer[0] != 0x00) {
215 if (dev->read_buffer_length <
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700216 (4 * usb_endpoint_maxp(dev->interrupt_in_endpoint)) -
Steven Haigh03270632006-08-09 07:42:06 +1000217 (urb->actual_length)) {
218 memcpy (dev->read_buffer_primary +
219 dev->read_buffer_length,
220 dev->interrupt_in_buffer, urb->actual_length);
221
222 dev->read_buffer_length += urb->actual_length;
Lisa Nguyen05d76392013-05-13 12:41:54 -0700223 dbg(2, " %s reading %d ", __func__,
Steven Haigh03270632006-08-09 07:42:06 +1000224 urb->actual_length);
225 } else {
Lisa Nguyen05d76392013-05-13 12:41:54 -0700226 dbg(1, " %s : read_buffer overflow", __func__);
Steven Haigh03270632006-08-09 07:42:06 +1000227 }
228 }
229
230exit:
231 dev->read_urb_finished = 1;
232 spin_unlock(&dev->buflock);
233 /* always wake up so we recover from errors */
234 wake_up_interruptible(&dev->read_wait);
Harvey Harrison441b62c2008-03-03 16:08:34 -0800235 adu_debug_data(5, __func__, urb->actual_length,
Steven Haigh03270632006-08-09 07:42:06 +1000236 urb->transfer_buffer);
Lisa Nguyen05d76392013-05-13 12:41:54 -0700237 dbg(4, " %s : leave, status %d", __func__, status);
Steven Haigh03270632006-08-09 07:42:06 +1000238}
239
David Howells7d12e782006-10-05 14:55:46 +0100240static void adu_interrupt_out_callback(struct urb *urb)
Steven Haigh03270632006-08-09 07:42:06 +1000241{
242 struct adu_device *dev = urb->context;
Greg Kroah-Hartman24497a02007-07-18 10:58:02 -0700243 int status = urb->status;
Steven Haigh03270632006-08-09 07:42:06 +1000244
Lisa Nguyen05d76392013-05-13 12:41:54 -0700245 dbg(4, " %s : enter, status %d", __func__, status);
246 adu_debug_data(5, __func__, urb->actual_length, urb->transfer_buffer);
Steven Haigh03270632006-08-09 07:42:06 +1000247
Greg Kroah-Hartman24497a02007-07-18 10:58:02 -0700248 if (status != 0) {
249 if ((status != -ENOENT) &&
250 (status != -ECONNRESET)) {
Steven Haigh03270632006-08-09 07:42:06 +1000251 dbg(1, " %s :nonzero status received: %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800252 __func__, status);
Steven Haigh03270632006-08-09 07:42:06 +1000253 }
254 goto exit;
255 }
256
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700257 spin_lock(&dev->buflock);
258 dev->out_urb_finished = 1;
259 wake_up(&dev->write_wait);
260 spin_unlock(&dev->buflock);
Steven Haigh03270632006-08-09 07:42:06 +1000261exit:
262
Harvey Harrison441b62c2008-03-03 16:08:34 -0800263 adu_debug_data(5, __func__, urb->actual_length,
Steven Haigh03270632006-08-09 07:42:06 +1000264 urb->transfer_buffer);
Lisa Nguyen05d76392013-05-13 12:41:54 -0700265 dbg(4, " %s : leave, status %d", __func__, status);
Steven Haigh03270632006-08-09 07:42:06 +1000266}
267
268static int adu_open(struct inode *inode, struct file *file)
269{
270 struct adu_device *dev = NULL;
271 struct usb_interface *interface;
272 int subminor;
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700273 int retval;
Steven Haigh03270632006-08-09 07:42:06 +1000274
Lisa Nguyen05d76392013-05-13 12:41:54 -0700275 dbg(2, "%s : enter", __func__);
Steven Haigh03270632006-08-09 07:42:06 +1000276
277 subminor = iminor(inode);
278
Lisa Nguyene77c4e62013-05-15 15:21:07 -0700279 retval = mutex_lock_interruptible(&adutux_mutex);
280 if (retval) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800281 dbg(2, "%s : mutex lock failed", __func__);
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700282 goto exit_no_lock;
283 }
284
Steven Haigh03270632006-08-09 07:42:06 +1000285 interface = usb_find_interface(&adu_driver, subminor);
286 if (!interface) {
Greg Kroah-Hartmanfd3f1912008-08-14 09:37:34 -0700287 printk(KERN_ERR "adutux: %s - error, can't find device for "
288 "minor %d\n", __func__, subminor);
Steven Haigh03270632006-08-09 07:42:06 +1000289 retval = -ENODEV;
290 goto exit_no_device;
291 }
292
293 dev = usb_get_intfdata(interface);
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700294 if (!dev || !dev->udev) {
Steven Haigh03270632006-08-09 07:42:06 +1000295 retval = -ENODEV;
296 goto exit_no_device;
297 }
298
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700299 /* check that nobody else is using the device */
300 if (dev->open_count) {
301 retval = -EBUSY;
Steven Haigh03270632006-08-09 07:42:06 +1000302 goto exit_no_device;
303 }
304
Steven Haigh03270632006-08-09 07:42:06 +1000305 ++dev->open_count;
Lisa Nguyen05d76392013-05-13 12:41:54 -0700306 dbg(2, "%s : open count %d", __func__, dev->open_count);
Steven Haigh03270632006-08-09 07:42:06 +1000307
308 /* save device in the file's private structure */
309 file->private_data = dev;
310
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700311 /* initialize in direction */
312 dev->read_buffer_length = 0;
Steven Haigh03270632006-08-09 07:42:06 +1000313
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700314 /* fixup first read by having urb waiting for it */
Lisa Nguyen05d76392013-05-13 12:41:54 -0700315 usb_fill_int_urb(dev->interrupt_in_urb, dev->udev,
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700316 usb_rcvintpipe(dev->udev,
317 dev->interrupt_in_endpoint->bEndpointAddress),
318 dev->interrupt_in_buffer,
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700319 usb_endpoint_maxp(dev->interrupt_in_endpoint),
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700320 adu_interrupt_in_callback, dev,
321 dev->interrupt_in_endpoint->bInterval);
322 dev->read_urb_finished = 0;
323 if (usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL))
324 dev->read_urb_finished = 1;
325 /* we ignore failure */
326 /* end of fixup for first read */
327
328 /* initialize out direction */
329 dev->out_urb_finished = 1;
330
331 retval = 0;
Steven Haigh03270632006-08-09 07:42:06 +1000332
333exit_no_device:
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700334 mutex_unlock(&adutux_mutex);
335exit_no_lock:
Lisa Nguyen05d76392013-05-13 12:41:54 -0700336 dbg(2, "%s : leave, return value %d ", __func__, retval);
Steven Haigh03270632006-08-09 07:42:06 +1000337 return retval;
338}
339
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700340static void adu_release_internal(struct adu_device *dev)
Steven Haigh03270632006-08-09 07:42:06 +1000341{
Lisa Nguyen05d76392013-05-13 12:41:54 -0700342 dbg(2, " %s : enter", __func__);
Steven Haigh03270632006-08-09 07:42:06 +1000343
Steven Haigh03270632006-08-09 07:42:06 +1000344 /* decrement our usage count for the device */
345 --dev->open_count;
Lisa Nguyen05d76392013-05-13 12:41:54 -0700346 dbg(2, " %s : open count %d", __func__, dev->open_count);
Steven Haigh03270632006-08-09 07:42:06 +1000347 if (dev->open_count <= 0) {
348 adu_abort_transfers(dev);
349 dev->open_count = 0;
350 }
351
Lisa Nguyen05d76392013-05-13 12:41:54 -0700352 dbg(2, " %s : leave", __func__);
Steven Haigh03270632006-08-09 07:42:06 +1000353}
354
355static int adu_release(struct inode *inode, struct file *file)
356{
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700357 struct adu_device *dev;
Steven Haigh03270632006-08-09 07:42:06 +1000358 int retval = 0;
359
Lisa Nguyen05d76392013-05-13 12:41:54 -0700360 dbg(2, " %s : enter", __func__);
Steven Haigh03270632006-08-09 07:42:06 +1000361
362 if (file == NULL) {
Lisa Nguyen05d76392013-05-13 12:41:54 -0700363 dbg(1, " %s : file is NULL", __func__);
Steven Haigh03270632006-08-09 07:42:06 +1000364 retval = -ENODEV;
365 goto exit;
366 }
367
368 dev = file->private_data;
Steven Haigh03270632006-08-09 07:42:06 +1000369 if (dev == NULL) {
Lisa Nguyen05d76392013-05-13 12:41:54 -0700370 dbg(1, " %s : object is NULL", __func__);
Steven Haigh03270632006-08-09 07:42:06 +1000371 retval = -ENODEV;
372 goto exit;
373 }
374
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700375 mutex_lock(&adutux_mutex); /* not interruptible */
Steven Haigh03270632006-08-09 07:42:06 +1000376
377 if (dev->open_count <= 0) {
Lisa Nguyen05d76392013-05-13 12:41:54 -0700378 dbg(1, " %s : device not opened", __func__);
Steven Haigh03270632006-08-09 07:42:06 +1000379 retval = -ENODEV;
Jiri Slaby46c98442009-03-11 21:47:38 +0100380 goto unlock;
Steven Haigh03270632006-08-09 07:42:06 +1000381 }
382
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700383 adu_release_internal(dev);
Alan Sternd4ead162007-05-22 11:46:41 -0400384 if (dev->udev == NULL) {
385 /* the device was unplugged before the file was released */
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700386 if (!dev->open_count) /* ... and we're the last user */
387 adu_delete(dev);
Alan Sternd4ead162007-05-22 11:46:41 -0400388 }
Jiri Slaby46c98442009-03-11 21:47:38 +0100389unlock:
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700390 mutex_unlock(&adutux_mutex);
Jiri Slaby46c98442009-03-11 21:47:38 +0100391exit:
Lisa Nguyen05d76392013-05-13 12:41:54 -0700392 dbg(2, " %s : leave, return value %d", __func__, retval);
Steven Haigh03270632006-08-09 07:42:06 +1000393 return retval;
394}
395
396static ssize_t adu_read(struct file *file, __user char *buffer, size_t count,
397 loff_t *ppos)
398{
399 struct adu_device *dev;
400 size_t bytes_read = 0;
401 size_t bytes_to_read = count;
402 int i;
403 int retval = 0;
404 int timeout = 0;
405 int should_submit = 0;
406 unsigned long flags;
407 DECLARE_WAITQUEUE(wait, current);
408
Lisa Nguyen05d76392013-05-13 12:41:54 -0700409 dbg(2, " %s : enter, count = %Zd, file=%p", __func__, count, file);
Steven Haigh03270632006-08-09 07:42:06 +1000410
411 dev = file->private_data;
Lisa Nguyen05d76392013-05-13 12:41:54 -0700412 dbg(2, " %s : dev=%p", __func__, dev);
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700413
Matthias Kaehlcke8293c562007-07-13 21:28:31 +0200414 if (mutex_lock_interruptible(&dev->mtx))
Steven Haigh03270632006-08-09 07:42:06 +1000415 return -ERESTARTSYS;
416
417 /* verify that the device wasn't unplugged */
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700418 if (dev->udev == NULL) {
Steven Haigh03270632006-08-09 07:42:06 +1000419 retval = -ENODEV;
Greg Kroah-Hartmanfd3f1912008-08-14 09:37:34 -0700420 printk(KERN_ERR "adutux: No device or device unplugged %d\n",
421 retval);
Steven Haigh03270632006-08-09 07:42:06 +1000422 goto exit;
423 }
424
425 /* verify that some data was requested */
426 if (count == 0) {
Lisa Nguyen05d76392013-05-13 12:41:54 -0700427 dbg(1, " %s : read request of 0 bytes", __func__);
Steven Haigh03270632006-08-09 07:42:06 +1000428 goto exit;
429 }
430
431 timeout = COMMAND_TIMEOUT;
Lisa Nguyen05d76392013-05-13 12:41:54 -0700432 dbg(2, " %s : about to start looping", __func__);
Steven Haigh03270632006-08-09 07:42:06 +1000433 while (bytes_to_read) {
434 int data_in_secondary = dev->secondary_tail - dev->secondary_head;
Lisa Nguyen05d76392013-05-13 12:41:54 -0700435 dbg(2, " %s : while, data_in_secondary=%d, status=%d",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800436 __func__, data_in_secondary,
Steven Haigh03270632006-08-09 07:42:06 +1000437 dev->interrupt_in_urb->status);
438
439 if (data_in_secondary) {
440 /* drain secondary buffer */
441 int amount = bytes_to_read < data_in_secondary ? bytes_to_read : data_in_secondary;
442 i = copy_to_user(buffer, dev->read_buffer_secondary+dev->secondary_head, amount);
Kulikov Vasiliy1865a9c2010-07-31 21:40:07 +0400443 if (i) {
Steven Haigh03270632006-08-09 07:42:06 +1000444 retval = -EFAULT;
445 goto exit;
446 }
447 dev->secondary_head += (amount - i);
448 bytes_read += (amount - i);
449 bytes_to_read -= (amount - i);
450 if (i) {
451 retval = bytes_read ? bytes_read : -EFAULT;
452 goto exit;
453 }
454 } else {
455 /* we check the primary buffer */
456 spin_lock_irqsave (&dev->buflock, flags);
457 if (dev->read_buffer_length) {
458 /* we secure access to the primary */
459 char *tmp;
Lisa Nguyen05d76392013-05-13 12:41:54 -0700460 dbg(2, " %s : swap, read_buffer_length = %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800461 __func__, dev->read_buffer_length);
Steven Haigh03270632006-08-09 07:42:06 +1000462 tmp = dev->read_buffer_secondary;
463 dev->read_buffer_secondary = dev->read_buffer_primary;
464 dev->read_buffer_primary = tmp;
465 dev->secondary_head = 0;
466 dev->secondary_tail = dev->read_buffer_length;
467 dev->read_buffer_length = 0;
468 spin_unlock_irqrestore(&dev->buflock, flags);
469 /* we have a free buffer so use it */
470 should_submit = 1;
471 } else {
472 /* even the primary was empty - we may need to do IO */
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700473 if (!dev->read_urb_finished) {
Steven Haigh03270632006-08-09 07:42:06 +1000474 /* somebody is doing IO */
475 spin_unlock_irqrestore(&dev->buflock, flags);
Lisa Nguyen05d76392013-05-13 12:41:54 -0700476 dbg(2, " %s : submitted already", __func__);
Steven Haigh03270632006-08-09 07:42:06 +1000477 } else {
478 /* we must initiate input */
Lisa Nguyen05d76392013-05-13 12:41:54 -0700479 dbg(2, " %s : initiate input", __func__);
Steven Haigh03270632006-08-09 07:42:06 +1000480 dev->read_urb_finished = 0;
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700481 spin_unlock_irqrestore(&dev->buflock, flags);
Steven Haigh03270632006-08-09 07:42:06 +1000482
Lisa Nguyen05d76392013-05-13 12:41:54 -0700483 usb_fill_int_urb(dev->interrupt_in_urb, dev->udev,
Lisa Nguyeneb79c012013-05-13 12:41:10 -0700484 usb_rcvintpipe(dev->udev,
485 dev->interrupt_in_endpoint->bEndpointAddress),
Steven Haigh03270632006-08-09 07:42:06 +1000486 dev->interrupt_in_buffer,
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700487 usb_endpoint_maxp(dev->interrupt_in_endpoint),
Steven Haigh03270632006-08-09 07:42:06 +1000488 adu_interrupt_in_callback,
489 dev,
490 dev->interrupt_in_endpoint->bInterval);
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700491 retval = usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL);
492 if (retval) {
493 dev->read_urb_finished = 1;
Steven Haigh03270632006-08-09 07:42:06 +1000494 if (retval == -ENOMEM) {
495 retval = bytes_read ? bytes_read : -ENOMEM;
496 }
Lisa Nguyen05d76392013-05-13 12:41:54 -0700497 dbg(2, " %s : submit failed", __func__);
Steven Haigh03270632006-08-09 07:42:06 +1000498 goto exit;
499 }
500 }
501
502 /* we wait for I/O to complete */
503 set_current_state(TASK_INTERRUPTIBLE);
504 add_wait_queue(&dev->read_wait, &wait);
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700505 spin_lock_irqsave(&dev->buflock, flags);
506 if (!dev->read_urb_finished) {
507 spin_unlock_irqrestore(&dev->buflock, flags);
Steven Haigh03270632006-08-09 07:42:06 +1000508 timeout = schedule_timeout(COMMAND_TIMEOUT);
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700509 } else {
510 spin_unlock_irqrestore(&dev->buflock, flags);
Steven Haigh03270632006-08-09 07:42:06 +1000511 set_current_state(TASK_RUNNING);
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700512 }
Steven Haigh03270632006-08-09 07:42:06 +1000513 remove_wait_queue(&dev->read_wait, &wait);
514
515 if (timeout <= 0) {
Lisa Nguyen05d76392013-05-13 12:41:54 -0700516 dbg(2, " %s : timeout", __func__);
Steven Haigh03270632006-08-09 07:42:06 +1000517 retval = bytes_read ? bytes_read : -ETIMEDOUT;
518 goto exit;
519 }
520
521 if (signal_pending(current)) {
Lisa Nguyen05d76392013-05-13 12:41:54 -0700522 dbg(2, " %s : signal pending", __func__);
Steven Haigh03270632006-08-09 07:42:06 +1000523 retval = bytes_read ? bytes_read : -EINTR;
524 goto exit;
525 }
526 }
527 }
528 }
529
530 retval = bytes_read;
531 /* if the primary buffer is empty then use it */
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700532 spin_lock_irqsave(&dev->buflock, flags);
533 if (should_submit && dev->read_urb_finished) {
534 dev->read_urb_finished = 0;
535 spin_unlock_irqrestore(&dev->buflock, flags);
Lisa Nguyen05d76392013-05-13 12:41:54 -0700536 usb_fill_int_urb(dev->interrupt_in_urb, dev->udev,
Steven Haigh03270632006-08-09 07:42:06 +1000537 usb_rcvintpipe(dev->udev,
Lisa Nguyeneb79c012013-05-13 12:41:10 -0700538 dev->interrupt_in_endpoint->bEndpointAddress),
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700539 dev->interrupt_in_buffer,
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700540 usb_endpoint_maxp(dev->interrupt_in_endpoint),
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700541 adu_interrupt_in_callback,
542 dev,
543 dev->interrupt_in_endpoint->bInterval);
544 if (usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL) != 0)
545 dev->read_urb_finished = 1;
Steven Haigh03270632006-08-09 07:42:06 +1000546 /* we ignore failure */
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700547 } else {
548 spin_unlock_irqrestore(&dev->buflock, flags);
Steven Haigh03270632006-08-09 07:42:06 +1000549 }
550
551exit:
552 /* unlock the device */
Matthias Kaehlcke8293c562007-07-13 21:28:31 +0200553 mutex_unlock(&dev->mtx);
Steven Haigh03270632006-08-09 07:42:06 +1000554
Lisa Nguyen05d76392013-05-13 12:41:54 -0700555 dbg(2, " %s : leave, return value %d", __func__, retval);
Steven Haigh03270632006-08-09 07:42:06 +1000556 return retval;
557}
558
559static ssize_t adu_write(struct file *file, const __user char *buffer,
560 size_t count, loff_t *ppos)
561{
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700562 DECLARE_WAITQUEUE(waita, current);
Steven Haigh03270632006-08-09 07:42:06 +1000563 struct adu_device *dev;
564 size_t bytes_written = 0;
565 size_t bytes_to_write;
566 size_t buffer_size;
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700567 unsigned long flags;
Oliver Neukumebc3ac12007-04-02 15:16:36 +0200568 int retval;
Steven Haigh03270632006-08-09 07:42:06 +1000569
Lisa Nguyen05d76392013-05-13 12:41:54 -0700570 dbg(2, " %s : enter, count = %Zd", __func__, count);
Steven Haigh03270632006-08-09 07:42:06 +1000571
572 dev = file->private_data;
573
Matthias Kaehlcke8293c562007-07-13 21:28:31 +0200574 retval = mutex_lock_interruptible(&dev->mtx);
Oliver Neukumebc3ac12007-04-02 15:16:36 +0200575 if (retval)
576 goto exit_nolock;
Steven Haigh03270632006-08-09 07:42:06 +1000577
578 /* verify that the device wasn't unplugged */
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700579 if (dev->udev == NULL) {
Steven Haigh03270632006-08-09 07:42:06 +1000580 retval = -ENODEV;
Greg Kroah-Hartmanfd3f1912008-08-14 09:37:34 -0700581 printk(KERN_ERR "adutux: No device or device unplugged %d\n",
582 retval);
Steven Haigh03270632006-08-09 07:42:06 +1000583 goto exit;
584 }
585
586 /* verify that we actually have some data to write */
587 if (count == 0) {
Lisa Nguyen05d76392013-05-13 12:41:54 -0700588 dbg(1, " %s : write request of 0 bytes", __func__);
Steven Haigh03270632006-08-09 07:42:06 +1000589 goto exit;
590 }
591
Steven Haigh03270632006-08-09 07:42:06 +1000592 while (count > 0) {
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700593 add_wait_queue(&dev->write_wait, &waita);
594 set_current_state(TASK_INTERRUPTIBLE);
595 spin_lock_irqsave(&dev->buflock, flags);
596 if (!dev->out_urb_finished) {
597 spin_unlock_irqrestore(&dev->buflock, flags);
Steven Haigh03270632006-08-09 07:42:06 +1000598
Matthias Kaehlcke8293c562007-07-13 21:28:31 +0200599 mutex_unlock(&dev->mtx);
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700600 if (signal_pending(current)) {
Lisa Nguyen05d76392013-05-13 12:41:54 -0700601 dbg(1, " %s : interrupted", __func__);
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700602 set_current_state(TASK_RUNNING);
603 retval = -EINTR;
604 goto exit_onqueue;
605 }
606 if (schedule_timeout(COMMAND_TIMEOUT) == 0) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800607 dbg(1, "%s - command timed out.", __func__);
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700608 retval = -ETIMEDOUT;
609 goto exit_onqueue;
610 }
611 remove_wait_queue(&dev->write_wait, &waita);
Matthias Kaehlcke8293c562007-07-13 21:28:31 +0200612 retval = mutex_lock_interruptible(&dev->mtx);
Oliver Neukumebc3ac12007-04-02 15:16:36 +0200613 if (retval) {
614 retval = bytes_written ? bytes_written : retval;
615 goto exit_nolock;
616 }
Steven Haigh03270632006-08-09 07:42:06 +1000617
Lisa Nguyen05d76392013-05-13 12:41:54 -0700618 dbg(4, " %s : in progress, count = %Zd", __func__, count);
Steven Haigh03270632006-08-09 07:42:06 +1000619 } else {
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700620 spin_unlock_irqrestore(&dev->buflock, flags);
621 set_current_state(TASK_RUNNING);
622 remove_wait_queue(&dev->write_wait, &waita);
Lisa Nguyen05d76392013-05-13 12:41:54 -0700623 dbg(4, " %s : sending, count = %Zd", __func__, count);
Steven Haigh03270632006-08-09 07:42:06 +1000624
625 /* write the data into interrupt_out_buffer from userspace */
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700626 buffer_size = usb_endpoint_maxp(dev->interrupt_out_endpoint);
Steven Haigh03270632006-08-09 07:42:06 +1000627 bytes_to_write = count > buffer_size ? buffer_size : count;
Lisa Nguyen05d76392013-05-13 12:41:54 -0700628 dbg(4, " %s : buffer_size = %Zd, count = %Zd, bytes_to_write = %Zd",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800629 __func__, buffer_size, count, bytes_to_write);
Steven Haigh03270632006-08-09 07:42:06 +1000630
631 if (copy_from_user(dev->interrupt_out_buffer, buffer, bytes_to_write) != 0) {
632 retval = -EFAULT;
633 goto exit;
634 }
635
636 /* send off the urb */
637 usb_fill_int_urb(
638 dev->interrupt_out_urb,
639 dev->udev,
640 usb_sndintpipe(dev->udev, dev->interrupt_out_endpoint->bEndpointAddress),
641 dev->interrupt_out_buffer,
642 bytes_to_write,
643 adu_interrupt_out_callback,
644 dev,
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700645 dev->interrupt_out_endpoint->bInterval);
Steven Haigh03270632006-08-09 07:42:06 +1000646 dev->interrupt_out_urb->actual_length = bytes_to_write;
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700647 dev->out_urb_finished = 0;
Steven Haigh03270632006-08-09 07:42:06 +1000648 retval = usb_submit_urb(dev->interrupt_out_urb, GFP_KERNEL);
649 if (retval < 0) {
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700650 dev->out_urb_finished = 1;
Greg Kroah-Hartmanfd3f1912008-08-14 09:37:34 -0700651 dev_err(&dev->udev->dev, "Couldn't submit "
652 "interrupt_out_urb %d\n", retval);
Steven Haigh03270632006-08-09 07:42:06 +1000653 goto exit;
654 }
655
656 buffer += bytes_to_write;
657 count -= bytes_to_write;
658
659 bytes_written += bytes_to_write;
660 }
661 }
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700662 mutex_unlock(&dev->mtx);
663 return bytes_written;
Steven Haigh03270632006-08-09 07:42:06 +1000664
665exit:
Matthias Kaehlcke8293c562007-07-13 21:28:31 +0200666 mutex_unlock(&dev->mtx);
Oliver Neukumebc3ac12007-04-02 15:16:36 +0200667exit_nolock:
Lisa Nguyen05d76392013-05-13 12:41:54 -0700668 dbg(2, " %s : leave, return value %d", __func__, retval);
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700669 return retval;
Steven Haigh03270632006-08-09 07:42:06 +1000670
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700671exit_onqueue:
672 remove_wait_queue(&dev->write_wait, &waita);
Steven Haigh03270632006-08-09 07:42:06 +1000673 return retval;
674}
675
676/* file operations needed when we register this driver */
Arjan van de Ven00977a52007-02-12 00:55:34 -0800677static const struct file_operations adu_fops = {
Steven Haigh03270632006-08-09 07:42:06 +1000678 .owner = THIS_MODULE,
679 .read = adu_read,
680 .write = adu_write,
681 .open = adu_open,
682 .release = adu_release,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200683 .llseek = noop_llseek,
Steven Haigh03270632006-08-09 07:42:06 +1000684};
685
686/*
687 * usb class driver info in order to get a minor number from the usb core,
688 * and to have the device registered with devfs and the driver core
689 */
690static struct usb_class_driver adu_class = {
691 .name = "usb/adutux%d",
692 .fops = &adu_fops,
693 .minor_base = ADU_MINOR_BASE,
694};
695
696/**
697 * adu_probe
698 *
699 * Called by the usb core when a new device is connected that it thinks
700 * this driver might be interested in.
701 */
702static int adu_probe(struct usb_interface *interface,
703 const struct usb_device_id *id)
704{
705 struct usb_device *udev = interface_to_usbdev(interface);
706 struct adu_device *dev = NULL;
707 struct usb_host_interface *iface_desc;
708 struct usb_endpoint_descriptor *endpoint;
709 int retval = -ENODEV;
710 int in_end_size;
711 int out_end_size;
712 int i;
713
Lisa Nguyen05d76392013-05-13 12:41:54 -0700714 dbg(2, " %s : enter", __func__);
Steven Haigh03270632006-08-09 07:42:06 +1000715
716 if (udev == NULL) {
717 dev_err(&interface->dev, "udev is NULL.\n");
718 goto exit;
719 }
720
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400721 /* allocate memory for our device state and initialize it */
Steven Haigh03270632006-08-09 07:42:06 +1000722 dev = kzalloc(sizeof(struct adu_device), GFP_KERNEL);
723 if (dev == NULL) {
724 dev_err(&interface->dev, "Out of memory\n");
725 retval = -ENOMEM;
726 goto exit;
727 }
728
Matthias Kaehlcke8293c562007-07-13 21:28:31 +0200729 mutex_init(&dev->mtx);
Steven Haigh03270632006-08-09 07:42:06 +1000730 spin_lock_init(&dev->buflock);
731 dev->udev = udev;
732 init_waitqueue_head(&dev->read_wait);
733 init_waitqueue_head(&dev->write_wait);
734
735 iface_desc = &interface->altsetting[0];
736
737 /* set up the endpoint information */
738 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
739 endpoint = &iface_desc->endpoint[i].desc;
740
741 if (usb_endpoint_is_int_in(endpoint))
742 dev->interrupt_in_endpoint = endpoint;
743
744 if (usb_endpoint_is_int_out(endpoint))
745 dev->interrupt_out_endpoint = endpoint;
746 }
747 if (dev->interrupt_in_endpoint == NULL) {
748 dev_err(&interface->dev, "interrupt in endpoint not found\n");
749 goto error;
750 }
751 if (dev->interrupt_out_endpoint == NULL) {
752 dev_err(&interface->dev, "interrupt out endpoint not found\n");
753 goto error;
754 }
755
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700756 in_end_size = usb_endpoint_maxp(dev->interrupt_in_endpoint);
757 out_end_size = usb_endpoint_maxp(dev->interrupt_out_endpoint);
Steven Haigh03270632006-08-09 07:42:06 +1000758
759 dev->read_buffer_primary = kmalloc((4 * in_end_size), GFP_KERNEL);
760 if (!dev->read_buffer_primary) {
761 dev_err(&interface->dev, "Couldn't allocate read_buffer_primary\n");
762 retval = -ENOMEM;
763 goto error;
764 }
765
766 /* debug code prime the buffer */
767 memset(dev->read_buffer_primary, 'a', in_end_size);
768 memset(dev->read_buffer_primary + in_end_size, 'b', in_end_size);
769 memset(dev->read_buffer_primary + (2 * in_end_size), 'c', in_end_size);
770 memset(dev->read_buffer_primary + (3 * in_end_size), 'd', in_end_size);
771
772 dev->read_buffer_secondary = kmalloc((4 * in_end_size), GFP_KERNEL);
773 if (!dev->read_buffer_secondary) {
774 dev_err(&interface->dev, "Couldn't allocate read_buffer_secondary\n");
775 retval = -ENOMEM;
776 goto error;
777 }
778
779 /* debug code prime the buffer */
780 memset(dev->read_buffer_secondary, 'e', in_end_size);
781 memset(dev->read_buffer_secondary + in_end_size, 'f', in_end_size);
782 memset(dev->read_buffer_secondary + (2 * in_end_size), 'g', in_end_size);
783 memset(dev->read_buffer_secondary + (3 * in_end_size), 'h', in_end_size);
784
785 dev->interrupt_in_buffer = kmalloc(in_end_size, GFP_KERNEL);
786 if (!dev->interrupt_in_buffer) {
787 dev_err(&interface->dev, "Couldn't allocate interrupt_in_buffer\n");
788 goto error;
789 }
790
791 /* debug code prime the buffer */
792 memset(dev->interrupt_in_buffer, 'i', in_end_size);
793
794 dev->interrupt_in_urb = usb_alloc_urb(0, GFP_KERNEL);
795 if (!dev->interrupt_in_urb) {
796 dev_err(&interface->dev, "Couldn't allocate interrupt_in_urb\n");
797 goto error;
798 }
799 dev->interrupt_out_buffer = kmalloc(out_end_size, GFP_KERNEL);
800 if (!dev->interrupt_out_buffer) {
801 dev_err(&interface->dev, "Couldn't allocate interrupt_out_buffer\n");
802 goto error;
803 }
804 dev->interrupt_out_urb = usb_alloc_urb(0, GFP_KERNEL);
805 if (!dev->interrupt_out_urb) {
806 dev_err(&interface->dev, "Couldn't allocate interrupt_out_urb\n");
807 goto error;
808 }
809
810 if (!usb_string(udev, udev->descriptor.iSerialNumber, dev->serial_number,
811 sizeof(dev->serial_number))) {
812 dev_err(&interface->dev, "Could not retrieve serial number\n");
813 goto error;
814 }
Lisa Nguyen05d76392013-05-13 12:41:54 -0700815 dbg(2, " %s : serial_number=%s", __func__, dev->serial_number);
Steven Haigh03270632006-08-09 07:42:06 +1000816
817 /* we can register the device now, as it is ready */
818 usb_set_intfdata(interface, dev);
819
820 retval = usb_register_dev(interface, &adu_class);
821
822 if (retval) {
823 /* something prevented us from registering this driver */
824 dev_err(&interface->dev, "Not able to get a minor for this device.\n");
825 usb_set_intfdata(interface, NULL);
826 goto error;
827 }
828
829 dev->minor = interface->minor;
830
831 /* let the user know what node this device is now attached to */
Joe Perches898eb712007-10-18 03:06:30 -0700832 dev_info(&interface->dev, "ADU%d %s now attached to /dev/usb/adutux%d\n",
Steven Haigh03270632006-08-09 07:42:06 +1000833 udev->descriptor.idProduct, dev->serial_number,
834 (dev->minor - ADU_MINOR_BASE));
835exit:
Lisa Nguyen05d76392013-05-13 12:41:54 -0700836 dbg(2, " %s : leave, return value %p (dev)", __func__, dev);
Steven Haigh03270632006-08-09 07:42:06 +1000837
838 return retval;
839
840error:
841 adu_delete(dev);
842 return retval;
843}
844
845/**
846 * adu_disconnect
847 *
848 * Called by the usb core when the device is removed from the system.
849 */
850static void adu_disconnect(struct usb_interface *interface)
851{
852 struct adu_device *dev;
853 int minor;
854
Lisa Nguyen05d76392013-05-13 12:41:54 -0700855 dbg(2, " %s : enter", __func__);
Steven Haigh03270632006-08-09 07:42:06 +1000856
Steven Haigh03270632006-08-09 07:42:06 +1000857 dev = usb_get_intfdata(interface);
Steven Haigh03270632006-08-09 07:42:06 +1000858
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700859 mutex_lock(&dev->mtx); /* not interruptible */
860 dev->udev = NULL; /* poison */
Steven Haigh03270632006-08-09 07:42:06 +1000861 minor = dev->minor;
Steven Haigh03270632006-08-09 07:42:06 +1000862 usb_deregister_dev(interface, &adu_class);
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700863 mutex_unlock(&dev->mtx);
Steven Haigh03270632006-08-09 07:42:06 +1000864
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700865 mutex_lock(&adutux_mutex);
866 usb_set_intfdata(interface, NULL);
Alan Sternd4ead162007-05-22 11:46:41 -0400867
Steven Haigh03270632006-08-09 07:42:06 +1000868 /* if the device is not opened, then we clean up right now */
Lisa Nguyen05d76392013-05-13 12:41:54 -0700869 dbg(2, " %s : open count %d", __func__, dev->open_count);
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700870 if (!dev->open_count)
Steven Haigh03270632006-08-09 07:42:06 +1000871 adu_delete(dev);
Pete Zaitcevf08812d2007-10-31 15:59:30 -0700872
873 mutex_unlock(&adutux_mutex);
Steven Haigh03270632006-08-09 07:42:06 +1000874
Joe Perches898eb712007-10-18 03:06:30 -0700875 dev_info(&interface->dev, "ADU device adutux%d now disconnected\n",
Steven Haigh03270632006-08-09 07:42:06 +1000876 (minor - ADU_MINOR_BASE));
877
Lisa Nguyen05d76392013-05-13 12:41:54 -0700878 dbg(2, " %s : leave", __func__);
Steven Haigh03270632006-08-09 07:42:06 +1000879}
880
881/* usb specific object needed to register this driver with the usb subsystem */
882static struct usb_driver adu_driver = {
883 .name = "adutux",
884 .probe = adu_probe,
885 .disconnect = adu_disconnect,
886 .id_table = device_table,
887};
888
Greg Kroah-Hartman65db4302011-11-18 09:34:02 -0800889module_usb_driver(adu_driver);
Steven Haigh03270632006-08-09 07:42:06 +1000890
891MODULE_AUTHOR(DRIVER_AUTHOR);
892MODULE_DESCRIPTION(DRIVER_DESC);
893MODULE_LICENSE("GPL");