xref: /openbmc/linux/drivers/usb/misc/adutux.c (revision 687ca639)
15fd54aceSGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0+
203270634SSteven Haigh /*
303270634SSteven Haigh  * adutux - driver for ADU devices from Ontrak Control Systems
403270634SSteven Haigh  * This is an experimental driver. Use at your own risk.
503270634SSteven Haigh  * This driver is not supported by Ontrak Control Systems.
603270634SSteven Haigh  *
703270634SSteven Haigh  * Copyright (c) 2003 John Homppi (SCO, leave this notice here)
803270634SSteven Haigh  *
903270634SSteven Haigh  * derived from the Lego USB Tower driver 0.56:
1003270634SSteven Haigh  * Copyright (c) 2003 David Glance <davidgsf@sourceforge.net>
1103270634SSteven Haigh  *               2001 Juergen Stuber <stuber@loria.fr>
1203270634SSteven Haigh  * that was derived from USB Skeleton driver - 0.5
1303270634SSteven Haigh  * Copyright (c) 2001 Greg Kroah-Hartman (greg@kroah.com)
1403270634SSteven Haigh  *
1503270634SSteven Haigh  */
1603270634SSteven Haigh 
1728f47c34SGreg Kroah-Hartman #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
1828f47c34SGreg Kroah-Hartman 
1903270634SSteven Haigh #include <linux/kernel.h>
20174cd4b1SIngo Molnar #include <linux/sched/signal.h>
2103270634SSteven Haigh #include <linux/errno.h>
2203270634SSteven Haigh #include <linux/slab.h>
2303270634SSteven Haigh #include <linux/module.h>
2403270634SSteven Haigh #include <linux/usb.h>
258293c568SMatthias Kaehlcke #include <linux/mutex.h>
2640cf4833SLisa Nguyen #include <linux/uaccess.h>
2703270634SSteven Haigh 
2803270634SSteven Haigh #define DRIVER_AUTHOR "John Homppi"
2903270634SSteven Haigh #define DRIVER_DESC "adutux (see www.ontrak.net)"
3003270634SSteven Haigh 
3103270634SSteven Haigh /* Define these values to match your device */
3203270634SSteven Haigh #define ADU_VENDOR_ID 0x0a07
3303270634SSteven Haigh #define ADU_PRODUCT_ID 0x0064
3403270634SSteven Haigh 
3503270634SSteven Haigh /* table of devices that work with this driver */
3633b9e162SNémeth Márton static const struct usb_device_id device_table[] = {
3703270634SSteven Haigh 	{ USB_DEVICE(ADU_VENDOR_ID, ADU_PRODUCT_ID) },		/* ADU100 */
3803270634SSteven Haigh 	{ USB_DEVICE(ADU_VENDOR_ID, ADU_PRODUCT_ID+20) },	/* ADU120 */
3903270634SSteven Haigh 	{ USB_DEVICE(ADU_VENDOR_ID, ADU_PRODUCT_ID+30) },	/* ADU130 */
4003270634SSteven Haigh 	{ USB_DEVICE(ADU_VENDOR_ID, ADU_PRODUCT_ID+100) },	/* ADU200 */
4103270634SSteven Haigh 	{ USB_DEVICE(ADU_VENDOR_ID, ADU_PRODUCT_ID+108) },	/* ADU208 */
4203270634SSteven Haigh 	{ USB_DEVICE(ADU_VENDOR_ID, ADU_PRODUCT_ID+118) },	/* ADU218 */
4303270634SSteven Haigh 	{ } /* Terminating entry */
4403270634SSteven Haigh };
4503270634SSteven Haigh 
4603270634SSteven Haigh MODULE_DEVICE_TABLE(usb, device_table);
4703270634SSteven Haigh 
4803270634SSteven Haigh #ifdef CONFIG_USB_DYNAMIC_MINORS
4903270634SSteven Haigh #define ADU_MINOR_BASE	0
5003270634SSteven Haigh #else
5103270634SSteven Haigh #define ADU_MINOR_BASE	67
5203270634SSteven Haigh #endif
5303270634SSteven Haigh 
5403270634SSteven Haigh /* we can have up to this number of device plugged in at once */
5503270634SSteven Haigh #define MAX_DEVICES	16
5603270634SSteven Haigh 
57cc9debf8SKirill Kapranov #define COMMAND_TIMEOUT	(2*HZ)
5803270634SSteven Haigh 
59f08812d5SPete Zaitcev /*
60f08812d5SPete Zaitcev  * The locking scheme is a vanilla 3-lock:
61f08812d5SPete Zaitcev  *   adu_device.buflock: A spinlock, covers what IRQs touch.
62f08812d5SPete Zaitcev  *   adutux_mutex:       A Static lock to cover open_count. It would also cover
63f08812d5SPete Zaitcev  *                       any globals, but we don't have them in 2.6.
64f08812d5SPete Zaitcev  *   adu_device.mtx:     A mutex to hold across sleepers like copy_from_user.
65f08812d5SPete Zaitcev  *                       It covers all of adu_device, except the open_count
66f08812d5SPete Zaitcev  *                       and what .buflock covers.
67f08812d5SPete Zaitcev  */
68f08812d5SPete Zaitcev 
6903270634SSteven Haigh /* Structure to hold all of our device specific stuff */
7003270634SSteven Haigh struct adu_device {
71f08812d5SPete Zaitcev 	struct mutex		mtx;
7203270634SSteven Haigh 	struct usb_device *udev; /* save off the usb device pointer */
7303270634SSteven Haigh 	struct usb_interface *interface;
74f08812d5SPete Zaitcev 	unsigned int		minor; /* the starting minor number for this device */
7503270634SSteven Haigh 	char			serial_number[8];
7603270634SSteven Haigh 
7703270634SSteven Haigh 	int			open_count; /* number of times this port has been opened */
7803270634SSteven Haigh 
7903270634SSteven Haigh 	char		*read_buffer_primary;
8003270634SSteven Haigh 	int			read_buffer_length;
8103270634SSteven Haigh 	char		*read_buffer_secondary;
8203270634SSteven Haigh 	int			secondary_head;
8303270634SSteven Haigh 	int			secondary_tail;
8403270634SSteven Haigh 	spinlock_t		buflock;
8503270634SSteven Haigh 
8603270634SSteven Haigh 	wait_queue_head_t	read_wait;
8703270634SSteven Haigh 	wait_queue_head_t	write_wait;
8803270634SSteven Haigh 
8903270634SSteven Haigh 	char		*interrupt_in_buffer;
9003270634SSteven Haigh 	struct usb_endpoint_descriptor *interrupt_in_endpoint;
9103270634SSteven Haigh 	struct urb	*interrupt_in_urb;
9203270634SSteven Haigh 	int			read_urb_finished;
9303270634SSteven Haigh 
9403270634SSteven Haigh 	char		*interrupt_out_buffer;
9503270634SSteven Haigh 	struct usb_endpoint_descriptor *interrupt_out_endpoint;
9603270634SSteven Haigh 	struct urb	*interrupt_out_urb;
97f08812d5SPete Zaitcev 	int			out_urb_finished;
9803270634SSteven Haigh };
9903270634SSteven Haigh 
100f08812d5SPete Zaitcev static DEFINE_MUTEX(adutux_mutex);
101f08812d5SPete Zaitcev 
10203270634SSteven Haigh static struct usb_driver adu_driver;
10303270634SSteven Haigh 
1041ef37c60SGreg Kroah-Hartman static inline void adu_debug_data(struct device *dev, const char *function,
1051ef37c60SGreg Kroah-Hartman 				  int size, const unsigned char *data)
10603270634SSteven Haigh {
1071ef37c60SGreg Kroah-Hartman 	dev_dbg(dev, "%s - length = %d, data = %*ph\n",
1081ef37c60SGreg Kroah-Hartman 		function, size, size, data);
10903270634SSteven Haigh }
11003270634SSteven Haigh 
11103270634SSteven Haigh /**
11203270634SSteven Haigh  * adu_abort_transfers
11303270634SSteven Haigh  *      aborts transfers and frees associated data structures
11403270634SSteven Haigh  */
11503270634SSteven Haigh static void adu_abort_transfers(struct adu_device *dev)
11603270634SSteven Haigh {
117f08812d5SPete Zaitcev 	unsigned long flags;
11803270634SSteven Haigh 
11966d4bc30SGreg Kroah-Hartman 	if (dev->udev == NULL)
1206e42a158SGreg Kroah-Hartman 		return;
12103270634SSteven Haigh 
12203270634SSteven Haigh 	/* shutdown transfer */
123f08812d5SPete Zaitcev 
124f08812d5SPete Zaitcev 	/* XXX Anchor these instead */
125f08812d5SPete Zaitcev 	spin_lock_irqsave(&dev->buflock, flags);
126f08812d5SPete Zaitcev 	if (!dev->read_urb_finished) {
127f08812d5SPete Zaitcev 		spin_unlock_irqrestore(&dev->buflock, flags);
128f08812d5SPete Zaitcev 		usb_kill_urb(dev->interrupt_in_urb);
129f08812d5SPete Zaitcev 	} else
130f08812d5SPete Zaitcev 		spin_unlock_irqrestore(&dev->buflock, flags);
131f08812d5SPete Zaitcev 
132f08812d5SPete Zaitcev 	spin_lock_irqsave(&dev->buflock, flags);
133f08812d5SPete Zaitcev 	if (!dev->out_urb_finished) {
134f08812d5SPete Zaitcev 		spin_unlock_irqrestore(&dev->buflock, flags);
135687ca639SKirill Kapranov 		wait_event_timeout(dev->write_wait, dev->out_urb_finished,
136687ca639SKirill Kapranov 			COMMAND_TIMEOUT);
137f08812d5SPete Zaitcev 		usb_kill_urb(dev->interrupt_out_urb);
138f08812d5SPete Zaitcev 	} else
139f08812d5SPete Zaitcev 		spin_unlock_irqrestore(&dev->buflock, flags);
14003270634SSteven Haigh }
14103270634SSteven Haigh 
14203270634SSteven Haigh static void adu_delete(struct adu_device *dev)
14303270634SSteven Haigh {
14403270634SSteven Haigh 	/* free data structures */
14503270634SSteven Haigh 	usb_free_urb(dev->interrupt_in_urb);
14603270634SSteven Haigh 	usb_free_urb(dev->interrupt_out_urb);
14703270634SSteven Haigh 	kfree(dev->read_buffer_primary);
14803270634SSteven Haigh 	kfree(dev->read_buffer_secondary);
14903270634SSteven Haigh 	kfree(dev->interrupt_in_buffer);
15003270634SSteven Haigh 	kfree(dev->interrupt_out_buffer);
15103270634SSteven Haigh 	kfree(dev);
15203270634SSteven Haigh }
15303270634SSteven Haigh 
1547d12e780SDavid Howells static void adu_interrupt_in_callback(struct urb *urb)
15503270634SSteven Haigh {
15603270634SSteven Haigh 	struct adu_device *dev = urb->context;
15724497a00SGreg Kroah-Hartman 	int status = urb->status;
15803270634SSteven Haigh 
1591ef37c60SGreg Kroah-Hartman 	adu_debug_data(&dev->udev->dev, __func__,
1601ef37c60SGreg Kroah-Hartman 		       urb->actual_length, urb->transfer_buffer);
16103270634SSteven Haigh 
16203270634SSteven Haigh 	spin_lock(&dev->buflock);
16303270634SSteven Haigh 
16424497a00SGreg Kroah-Hartman 	if (status != 0) {
165f6c1ceaaSOliver Neukum 		if ((status != -ENOENT) && (status != -ECONNRESET) &&
166f6c1ceaaSOliver Neukum 			(status != -ESHUTDOWN)) {
16766d4bc30SGreg Kroah-Hartman 			dev_dbg(&dev->udev->dev,
16866d4bc30SGreg Kroah-Hartman 				"%s : nonzero status received: %d\n",
169441b62c1SHarvey Harrison 				__func__, status);
17003270634SSteven Haigh 		}
17103270634SSteven Haigh 		goto exit;
17203270634SSteven Haigh 	}
17303270634SSteven Haigh 
17403270634SSteven Haigh 	if (urb->actual_length > 0 && dev->interrupt_in_buffer[0] != 0x00) {
17503270634SSteven Haigh 		if (dev->read_buffer_length <
17629cc8897SKuninori Morimoto 		    (4 * usb_endpoint_maxp(dev->interrupt_in_endpoint)) -
17703270634SSteven Haigh 		     (urb->actual_length)) {
17803270634SSteven Haigh 			memcpy (dev->read_buffer_primary +
17903270634SSteven Haigh 				dev->read_buffer_length,
18003270634SSteven Haigh 				dev->interrupt_in_buffer, urb->actual_length);
18103270634SSteven Haigh 
18203270634SSteven Haigh 			dev->read_buffer_length += urb->actual_length;
18366d4bc30SGreg Kroah-Hartman 			dev_dbg(&dev->udev->dev,"%s reading  %d\n", __func__,
18403270634SSteven Haigh 				urb->actual_length);
18503270634SSteven Haigh 		} else {
18666d4bc30SGreg Kroah-Hartman 			dev_dbg(&dev->udev->dev,"%s : read_buffer overflow\n",
18766d4bc30SGreg Kroah-Hartman 				__func__);
18803270634SSteven Haigh 		}
18903270634SSteven Haigh 	}
19003270634SSteven Haigh 
19103270634SSteven Haigh exit:
19203270634SSteven Haigh 	dev->read_urb_finished = 1;
19303270634SSteven Haigh 	spin_unlock(&dev->buflock);
19403270634SSteven Haigh 	/* always wake up so we recover from errors */
19503270634SSteven Haigh 	wake_up_interruptible(&dev->read_wait);
19603270634SSteven Haigh }
19703270634SSteven Haigh 
1987d12e780SDavid Howells static void adu_interrupt_out_callback(struct urb *urb)
19903270634SSteven Haigh {
20003270634SSteven Haigh 	struct adu_device *dev = urb->context;
20124497a00SGreg Kroah-Hartman 	int status = urb->status;
20203270634SSteven Haigh 
2031ef37c60SGreg Kroah-Hartman 	adu_debug_data(&dev->udev->dev, __func__,
2041ef37c60SGreg Kroah-Hartman 		       urb->actual_length, urb->transfer_buffer);
20503270634SSteven Haigh 
20624497a00SGreg Kroah-Hartman 	if (status != 0) {
20724497a00SGreg Kroah-Hartman 		if ((status != -ENOENT) &&
20824497a00SGreg Kroah-Hartman 		    (status != -ECONNRESET)) {
20966d4bc30SGreg Kroah-Hartman 			dev_dbg(&dev->udev->dev,
21066d4bc30SGreg Kroah-Hartman 				"%s :nonzero status received: %d\n", __func__,
21166d4bc30SGreg Kroah-Hartman 				status);
21203270634SSteven Haigh 		}
2131ef37c60SGreg Kroah-Hartman 		return;
21403270634SSteven Haigh 	}
21503270634SSteven Haigh 
216f08812d5SPete Zaitcev 	spin_lock(&dev->buflock);
217f08812d5SPete Zaitcev 	dev->out_urb_finished = 1;
218f08812d5SPete Zaitcev 	wake_up(&dev->write_wait);
219f08812d5SPete Zaitcev 	spin_unlock(&dev->buflock);
22003270634SSteven Haigh }
22103270634SSteven Haigh 
22203270634SSteven Haigh static int adu_open(struct inode *inode, struct file *file)
22303270634SSteven Haigh {
22403270634SSteven Haigh 	struct adu_device *dev = NULL;
22503270634SSteven Haigh 	struct usb_interface *interface;
22603270634SSteven Haigh 	int subminor;
227f08812d5SPete Zaitcev 	int retval;
22803270634SSteven Haigh 
22903270634SSteven Haigh 	subminor = iminor(inode);
23003270634SSteven Haigh 
231e77c4e6aSLisa Nguyen 	retval = mutex_lock_interruptible(&adutux_mutex);
23266d4bc30SGreg Kroah-Hartman 	if (retval)
233f08812d5SPete Zaitcev 		goto exit_no_lock;
234f08812d5SPete Zaitcev 
23503270634SSteven Haigh 	interface = usb_find_interface(&adu_driver, subminor);
23603270634SSteven Haigh 	if (!interface) {
23728f47c34SGreg Kroah-Hartman 		pr_err("%s - error, can't find device for minor %d\n",
23828f47c34SGreg Kroah-Hartman 		       __func__, subminor);
23903270634SSteven Haigh 		retval = -ENODEV;
24003270634SSteven Haigh 		goto exit_no_device;
24103270634SSteven Haigh 	}
24203270634SSteven Haigh 
24303270634SSteven Haigh 	dev = usb_get_intfdata(interface);
244f08812d5SPete Zaitcev 	if (!dev || !dev->udev) {
24503270634SSteven Haigh 		retval = -ENODEV;
24603270634SSteven Haigh 		goto exit_no_device;
24703270634SSteven Haigh 	}
24803270634SSteven Haigh 
249f08812d5SPete Zaitcev 	/* check that nobody else is using the device */
250f08812d5SPete Zaitcev 	if (dev->open_count) {
251f08812d5SPete Zaitcev 		retval = -EBUSY;
25203270634SSteven Haigh 		goto exit_no_device;
25303270634SSteven Haigh 	}
25403270634SSteven Haigh 
25503270634SSteven Haigh 	++dev->open_count;
25666d4bc30SGreg Kroah-Hartman 	dev_dbg(&dev->udev->dev, "%s: open count %d\n", __func__,
25766d4bc30SGreg Kroah-Hartman 		dev->open_count);
25803270634SSteven Haigh 
25903270634SSteven Haigh 	/* save device in the file's private structure */
26003270634SSteven Haigh 	file->private_data = dev;
26103270634SSteven Haigh 
26203270634SSteven Haigh 	/* initialize in direction */
26303270634SSteven Haigh 	dev->read_buffer_length = 0;
26403270634SSteven Haigh 
26503270634SSteven Haigh 	/* fixup first read by having urb waiting for it */
26603270634SSteven Haigh 	usb_fill_int_urb(dev->interrupt_in_urb, dev->udev,
26703270634SSteven Haigh 			 usb_rcvintpipe(dev->udev,
26803270634SSteven Haigh 					dev->interrupt_in_endpoint->bEndpointAddress),
26903270634SSteven Haigh 			 dev->interrupt_in_buffer,
27029cc8897SKuninori Morimoto 			 usb_endpoint_maxp(dev->interrupt_in_endpoint),
27103270634SSteven Haigh 			 adu_interrupt_in_callback, dev,
27203270634SSteven Haigh 			 dev->interrupt_in_endpoint->bInterval);
27303270634SSteven Haigh 	dev->read_urb_finished = 0;
274f08812d5SPete Zaitcev 	if (usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL))
275f08812d5SPete Zaitcev 		dev->read_urb_finished = 1;
276f08812d5SPete Zaitcev 	/* we ignore failure */
277f08812d5SPete Zaitcev 	/* end of fixup for first read */
278f08812d5SPete Zaitcev 
279f08812d5SPete Zaitcev 	/* initialize out direction */
280f08812d5SPete Zaitcev 	dev->out_urb_finished = 1;
281f08812d5SPete Zaitcev 
282f08812d5SPete Zaitcev 	retval = 0;
28303270634SSteven Haigh 
28403270634SSteven Haigh exit_no_device:
285f08812d5SPete Zaitcev 	mutex_unlock(&adutux_mutex);
286f08812d5SPete Zaitcev exit_no_lock:
28703270634SSteven Haigh 	return retval;
28803270634SSteven Haigh }
28903270634SSteven Haigh 
290f08812d5SPete Zaitcev static void adu_release_internal(struct adu_device *dev)
29103270634SSteven Haigh {
29203270634SSteven Haigh 	/* decrement our usage count for the device */
29303270634SSteven Haigh 	--dev->open_count;
29466d4bc30SGreg Kroah-Hartman 	dev_dbg(&dev->udev->dev, "%s : open count %d\n", __func__,
29566d4bc30SGreg Kroah-Hartman 		dev->open_count);
29603270634SSteven Haigh 	if (dev->open_count <= 0) {
29703270634SSteven Haigh 		adu_abort_transfers(dev);
29803270634SSteven Haigh 		dev->open_count = 0;
29903270634SSteven Haigh 	}
30003270634SSteven Haigh }
30103270634SSteven Haigh 
30203270634SSteven Haigh static int adu_release(struct inode *inode, struct file *file)
30303270634SSteven Haigh {
304f08812d5SPete Zaitcev 	struct adu_device *dev;
30503270634SSteven Haigh 	int retval = 0;
30603270634SSteven Haigh 
30703270634SSteven Haigh 	if (file == NULL) {
30803270634SSteven Haigh 		retval = -ENODEV;
30903270634SSteven Haigh 		goto exit;
31003270634SSteven Haigh 	}
31103270634SSteven Haigh 
31203270634SSteven Haigh 	dev = file->private_data;
31303270634SSteven Haigh 	if (dev == NULL) {
31403270634SSteven Haigh 		retval = -ENODEV;
31503270634SSteven Haigh 		goto exit;
31603270634SSteven Haigh 	}
31703270634SSteven Haigh 
318f08812d5SPete Zaitcev 	mutex_lock(&adutux_mutex); /* not interruptible */
31903270634SSteven Haigh 
32003270634SSteven Haigh 	if (dev->open_count <= 0) {
32166d4bc30SGreg Kroah-Hartman 		dev_dbg(&dev->udev->dev, "%s : device not opened\n", __func__);
32203270634SSteven Haigh 		retval = -ENODEV;
32346c9844cSJiri Slaby 		goto unlock;
32403270634SSteven Haigh 	}
32503270634SSteven Haigh 
326f08812d5SPete Zaitcev 	adu_release_internal(dev);
327d4ead16fSAlan Stern 	if (dev->udev == NULL) {
328d4ead16fSAlan Stern 		/* the device was unplugged before the file was released */
329f08812d5SPete Zaitcev 		if (!dev->open_count)	/* ... and we're the last user */
330d4ead16fSAlan Stern 			adu_delete(dev);
331d4ead16fSAlan Stern 	}
33246c9844cSJiri Slaby unlock:
333f08812d5SPete Zaitcev 	mutex_unlock(&adutux_mutex);
33446c9844cSJiri Slaby exit:
33503270634SSteven Haigh 	return retval;
33603270634SSteven Haigh }
33703270634SSteven Haigh 
33803270634SSteven Haigh static ssize_t adu_read(struct file *file, __user char *buffer, size_t count,
33903270634SSteven Haigh 			loff_t *ppos)
34003270634SSteven Haigh {
34103270634SSteven Haigh 	struct adu_device *dev;
34203270634SSteven Haigh 	size_t bytes_read = 0;
34303270634SSteven Haigh 	size_t bytes_to_read = count;
34403270634SSteven Haigh 	int i;
34503270634SSteven Haigh 	int retval = 0;
34603270634SSteven Haigh 	int timeout = 0;
34703270634SSteven Haigh 	int should_submit = 0;
34803270634SSteven Haigh 	unsigned long flags;
34903270634SSteven Haigh 	DECLARE_WAITQUEUE(wait, current);
35003270634SSteven Haigh 
35103270634SSteven Haigh 	dev = file->private_data;
3528293c568SMatthias Kaehlcke 	if (mutex_lock_interruptible(&dev->mtx))
35303270634SSteven Haigh 		return -ERESTARTSYS;
35403270634SSteven Haigh 
35503270634SSteven Haigh 	/* verify that the device wasn't unplugged */
356f08812d5SPete Zaitcev 	if (dev->udev == NULL) {
35703270634SSteven Haigh 		retval = -ENODEV;
35828f47c34SGreg Kroah-Hartman 		pr_err("No device or device unplugged %d\n", retval);
35903270634SSteven Haigh 		goto exit;
36003270634SSteven Haigh 	}
36103270634SSteven Haigh 
36203270634SSteven Haigh 	/* verify that some data was requested */
36303270634SSteven Haigh 	if (count == 0) {
36466d4bc30SGreg Kroah-Hartman 		dev_dbg(&dev->udev->dev, "%s : read request of 0 bytes\n",
36566d4bc30SGreg Kroah-Hartman 			__func__);
36603270634SSteven Haigh 		goto exit;
36703270634SSteven Haigh 	}
36803270634SSteven Haigh 
36903270634SSteven Haigh 	timeout = COMMAND_TIMEOUT;
37066d4bc30SGreg Kroah-Hartman 	dev_dbg(&dev->udev->dev, "%s : about to start looping\n", __func__);
37103270634SSteven Haigh 	while (bytes_to_read) {
37203270634SSteven Haigh 		int data_in_secondary = dev->secondary_tail - dev->secondary_head;
37366d4bc30SGreg Kroah-Hartman 		dev_dbg(&dev->udev->dev,
37466d4bc30SGreg Kroah-Hartman 			"%s : while, data_in_secondary=%d, status=%d\n",
375441b62c1SHarvey Harrison 			__func__, data_in_secondary,
37603270634SSteven Haigh 			dev->interrupt_in_urb->status);
37703270634SSteven Haigh 
37803270634SSteven Haigh 		if (data_in_secondary) {
37903270634SSteven Haigh 			/* drain secondary buffer */
38003270634SSteven Haigh 			int amount = bytes_to_read < data_in_secondary ? bytes_to_read : data_in_secondary;
38103270634SSteven Haigh 			i = copy_to_user(buffer, dev->read_buffer_secondary+dev->secondary_head, amount);
3821865a9c3SKulikov Vasiliy 			if (i) {
38303270634SSteven Haigh 				retval = -EFAULT;
38403270634SSteven Haigh 				goto exit;
38503270634SSteven Haigh 			}
38603270634SSteven Haigh 			dev->secondary_head += (amount - i);
38703270634SSteven Haigh 			bytes_read += (amount - i);
38803270634SSteven Haigh 			bytes_to_read -= (amount - i);
38903270634SSteven Haigh 		} else {
39003270634SSteven Haigh 			/* we check the primary buffer */
39103270634SSteven Haigh 			spin_lock_irqsave (&dev->buflock, flags);
39203270634SSteven Haigh 			if (dev->read_buffer_length) {
39303270634SSteven Haigh 				/* we secure access to the primary */
39403270634SSteven Haigh 				char *tmp;
39566d4bc30SGreg Kroah-Hartman 				dev_dbg(&dev->udev->dev,
39666d4bc30SGreg Kroah-Hartman 					"%s : swap, read_buffer_length = %d\n",
397441b62c1SHarvey Harrison 					__func__, dev->read_buffer_length);
39803270634SSteven Haigh 				tmp = dev->read_buffer_secondary;
39903270634SSteven Haigh 				dev->read_buffer_secondary = dev->read_buffer_primary;
40003270634SSteven Haigh 				dev->read_buffer_primary = tmp;
40103270634SSteven Haigh 				dev->secondary_head = 0;
40203270634SSteven Haigh 				dev->secondary_tail = dev->read_buffer_length;
40303270634SSteven Haigh 				dev->read_buffer_length = 0;
40403270634SSteven Haigh 				spin_unlock_irqrestore(&dev->buflock, flags);
40503270634SSteven Haigh 				/* we have a free buffer so use it */
40603270634SSteven Haigh 				should_submit = 1;
40703270634SSteven Haigh 			} else {
40803270634SSteven Haigh 				/* even the primary was empty - we may need to do IO */
409f08812d5SPete Zaitcev 				if (!dev->read_urb_finished) {
41003270634SSteven Haigh 					/* somebody is doing IO */
41103270634SSteven Haigh 					spin_unlock_irqrestore(&dev->buflock, flags);
41266d4bc30SGreg Kroah-Hartman 					dev_dbg(&dev->udev->dev,
41366d4bc30SGreg Kroah-Hartman 						"%s : submitted already\n",
41466d4bc30SGreg Kroah-Hartman 						__func__);
41503270634SSteven Haigh 				} else {
41603270634SSteven Haigh 					/* we must initiate input */
41766d4bc30SGreg Kroah-Hartman 					dev_dbg(&dev->udev->dev,
41866d4bc30SGreg Kroah-Hartman 						"%s : initiate input\n",
41966d4bc30SGreg Kroah-Hartman 						__func__);
42003270634SSteven Haigh 					dev->read_urb_finished = 0;
421f08812d5SPete Zaitcev 					spin_unlock_irqrestore(&dev->buflock, flags);
42203270634SSteven Haigh 
42303270634SSteven Haigh 					usb_fill_int_urb(dev->interrupt_in_urb, dev->udev,
42403270634SSteven Haigh 							usb_rcvintpipe(dev->udev,
42503270634SSteven Haigh 								dev->interrupt_in_endpoint->bEndpointAddress),
42603270634SSteven Haigh 							 dev->interrupt_in_buffer,
42729cc8897SKuninori Morimoto 							 usb_endpoint_maxp(dev->interrupt_in_endpoint),
42803270634SSteven Haigh 							 adu_interrupt_in_callback,
42903270634SSteven Haigh 							 dev,
43003270634SSteven Haigh 							 dev->interrupt_in_endpoint->bInterval);
431f08812d5SPete Zaitcev 					retval = usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL);
432f08812d5SPete Zaitcev 					if (retval) {
433f08812d5SPete Zaitcev 						dev->read_urb_finished = 1;
43403270634SSteven Haigh 						if (retval == -ENOMEM) {
43503270634SSteven Haigh 							retval = bytes_read ? bytes_read : -ENOMEM;
43603270634SSteven Haigh 						}
43766d4bc30SGreg Kroah-Hartman 						dev_dbg(&dev->udev->dev,
43866d4bc30SGreg Kroah-Hartman 							"%s : submit failed\n",
43966d4bc30SGreg Kroah-Hartman 							__func__);
44003270634SSteven Haigh 						goto exit;
44103270634SSteven Haigh 					}
44203270634SSteven Haigh 				}
44303270634SSteven Haigh 
44403270634SSteven Haigh 				/* we wait for I/O to complete */
44503270634SSteven Haigh 				set_current_state(TASK_INTERRUPTIBLE);
44603270634SSteven Haigh 				add_wait_queue(&dev->read_wait, &wait);
447f08812d5SPete Zaitcev 				spin_lock_irqsave(&dev->buflock, flags);
448f08812d5SPete Zaitcev 				if (!dev->read_urb_finished) {
449f08812d5SPete Zaitcev 					spin_unlock_irqrestore(&dev->buflock, flags);
45003270634SSteven Haigh 					timeout = schedule_timeout(COMMAND_TIMEOUT);
451f08812d5SPete Zaitcev 				} else {
452f08812d5SPete Zaitcev 					spin_unlock_irqrestore(&dev->buflock, flags);
45303270634SSteven Haigh 					set_current_state(TASK_RUNNING);
454f08812d5SPete Zaitcev 				}
45503270634SSteven Haigh 				remove_wait_queue(&dev->read_wait, &wait);
45603270634SSteven Haigh 
45703270634SSteven Haigh 				if (timeout <= 0) {
45866d4bc30SGreg Kroah-Hartman 					dev_dbg(&dev->udev->dev,
45966d4bc30SGreg Kroah-Hartman 						"%s : timeout\n", __func__);
46003270634SSteven Haigh 					retval = bytes_read ? bytes_read : -ETIMEDOUT;
46103270634SSteven Haigh 					goto exit;
46203270634SSteven Haigh 				}
46303270634SSteven Haigh 
46403270634SSteven Haigh 				if (signal_pending(current)) {
46566d4bc30SGreg Kroah-Hartman 					dev_dbg(&dev->udev->dev,
46666d4bc30SGreg Kroah-Hartman 						"%s : signal pending\n",
46766d4bc30SGreg Kroah-Hartman 						__func__);
46803270634SSteven Haigh 					retval = bytes_read ? bytes_read : -EINTR;
46903270634SSteven Haigh 					goto exit;
47003270634SSteven Haigh 				}
47103270634SSteven Haigh 			}
47203270634SSteven Haigh 		}
47303270634SSteven Haigh 	}
47403270634SSteven Haigh 
47503270634SSteven Haigh 	retval = bytes_read;
47603270634SSteven Haigh 	/* if the primary buffer is empty then use it */
477f08812d5SPete Zaitcev 	spin_lock_irqsave(&dev->buflock, flags);
478f08812d5SPete Zaitcev 	if (should_submit && dev->read_urb_finished) {
479f08812d5SPete Zaitcev 		dev->read_urb_finished = 0;
480f08812d5SPete Zaitcev 		spin_unlock_irqrestore(&dev->buflock, flags);
48103270634SSteven Haigh 		usb_fill_int_urb(dev->interrupt_in_urb, dev->udev,
48203270634SSteven Haigh 				 usb_rcvintpipe(dev->udev,
48303270634SSteven Haigh 					dev->interrupt_in_endpoint->bEndpointAddress),
48403270634SSteven Haigh 				dev->interrupt_in_buffer,
48529cc8897SKuninori Morimoto 				usb_endpoint_maxp(dev->interrupt_in_endpoint),
48603270634SSteven Haigh 				adu_interrupt_in_callback,
48703270634SSteven Haigh 				dev,
48803270634SSteven Haigh 				dev->interrupt_in_endpoint->bInterval);
489f08812d5SPete Zaitcev 		if (usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL) != 0)
490f08812d5SPete Zaitcev 			dev->read_urb_finished = 1;
49103270634SSteven Haigh 		/* we ignore failure */
492f08812d5SPete Zaitcev 	} else {
493f08812d5SPete Zaitcev 		spin_unlock_irqrestore(&dev->buflock, flags);
49403270634SSteven Haigh 	}
49503270634SSteven Haigh 
49603270634SSteven Haigh exit:
49703270634SSteven Haigh 	/* unlock the device */
4988293c568SMatthias Kaehlcke 	mutex_unlock(&dev->mtx);
49903270634SSteven Haigh 
50003270634SSteven Haigh 	return retval;
50103270634SSteven Haigh }
50203270634SSteven Haigh 
50303270634SSteven Haigh static ssize_t adu_write(struct file *file, const __user char *buffer,
50403270634SSteven Haigh 			 size_t count, loff_t *ppos)
50503270634SSteven Haigh {
506f08812d5SPete Zaitcev 	DECLARE_WAITQUEUE(waita, current);
50703270634SSteven Haigh 	struct adu_device *dev;
50803270634SSteven Haigh 	size_t bytes_written = 0;
50903270634SSteven Haigh 	size_t bytes_to_write;
51003270634SSteven Haigh 	size_t buffer_size;
511f08812d5SPete Zaitcev 	unsigned long flags;
512ebc3ac14SOliver Neukum 	int retval;
51303270634SSteven Haigh 
51403270634SSteven Haigh 	dev = file->private_data;
51503270634SSteven Haigh 
5168293c568SMatthias Kaehlcke 	retval = mutex_lock_interruptible(&dev->mtx);
517ebc3ac14SOliver Neukum 	if (retval)
518ebc3ac14SOliver Neukum 		goto exit_nolock;
51903270634SSteven Haigh 
52003270634SSteven Haigh 	/* verify that the device wasn't unplugged */
521f08812d5SPete Zaitcev 	if (dev->udev == NULL) {
52203270634SSteven Haigh 		retval = -ENODEV;
52328f47c34SGreg Kroah-Hartman 		pr_err("No device or device unplugged %d\n", retval);
52403270634SSteven Haigh 		goto exit;
52503270634SSteven Haigh 	}
52603270634SSteven Haigh 
52703270634SSteven Haigh 	/* verify that we actually have some data to write */
52803270634SSteven Haigh 	if (count == 0) {
52966d4bc30SGreg Kroah-Hartman 		dev_dbg(&dev->udev->dev, "%s : write request of 0 bytes\n",
53066d4bc30SGreg Kroah-Hartman 			__func__);
53103270634SSteven Haigh 		goto exit;
53203270634SSteven Haigh 	}
53303270634SSteven Haigh 
53403270634SSteven Haigh 	while (count > 0) {
535f08812d5SPete Zaitcev 		add_wait_queue(&dev->write_wait, &waita);
536f08812d5SPete Zaitcev 		set_current_state(TASK_INTERRUPTIBLE);
537f08812d5SPete Zaitcev 		spin_lock_irqsave(&dev->buflock, flags);
538f08812d5SPete Zaitcev 		if (!dev->out_urb_finished) {
539f08812d5SPete Zaitcev 			spin_unlock_irqrestore(&dev->buflock, flags);
54003270634SSteven Haigh 
541f08812d5SPete Zaitcev 			mutex_unlock(&dev->mtx);
54203270634SSteven Haigh 			if (signal_pending(current)) {
54366d4bc30SGreg Kroah-Hartman 				dev_dbg(&dev->udev->dev, "%s : interrupted\n",
54466d4bc30SGreg Kroah-Hartman 					__func__);
545f08812d5SPete Zaitcev 				set_current_state(TASK_RUNNING);
54603270634SSteven Haigh 				retval = -EINTR;
547f08812d5SPete Zaitcev 				goto exit_onqueue;
54803270634SSteven Haigh 			}
549f08812d5SPete Zaitcev 			if (schedule_timeout(COMMAND_TIMEOUT) == 0) {
55066d4bc30SGreg Kroah-Hartman 				dev_dbg(&dev->udev->dev,
55166d4bc30SGreg Kroah-Hartman 					"%s - command timed out.\n", __func__);
552f08812d5SPete Zaitcev 				retval = -ETIMEDOUT;
553f08812d5SPete Zaitcev 				goto exit_onqueue;
554f08812d5SPete Zaitcev 			}
555f08812d5SPete Zaitcev 			remove_wait_queue(&dev->write_wait, &waita);
5568293c568SMatthias Kaehlcke 			retval = mutex_lock_interruptible(&dev->mtx);
557ebc3ac14SOliver Neukum 			if (retval) {
558ebc3ac14SOliver Neukum 				retval = bytes_written ? bytes_written : retval;
559ebc3ac14SOliver Neukum 				goto exit_nolock;
560ebc3ac14SOliver Neukum 			}
56103270634SSteven Haigh 
56266d4bc30SGreg Kroah-Hartman 			dev_dbg(&dev->udev->dev,
5635b5e0928SAlexey Dobriyan 				"%s : in progress, count = %zd\n",
56466d4bc30SGreg Kroah-Hartman 				__func__, count);
56503270634SSteven Haigh 		} else {
566f08812d5SPete Zaitcev 			spin_unlock_irqrestore(&dev->buflock, flags);
567f08812d5SPete Zaitcev 			set_current_state(TASK_RUNNING);
568f08812d5SPete Zaitcev 			remove_wait_queue(&dev->write_wait, &waita);
5695b5e0928SAlexey Dobriyan 			dev_dbg(&dev->udev->dev, "%s : sending, count = %zd\n",
57066d4bc30SGreg Kroah-Hartman 				__func__, count);
57103270634SSteven Haigh 
57203270634SSteven Haigh 			/* write the data into interrupt_out_buffer from userspace */
57329cc8897SKuninori Morimoto 			buffer_size = usb_endpoint_maxp(dev->interrupt_out_endpoint);
57403270634SSteven Haigh 			bytes_to_write = count > buffer_size ? buffer_size : count;
57566d4bc30SGreg Kroah-Hartman 			dev_dbg(&dev->udev->dev,
5765b5e0928SAlexey Dobriyan 				"%s : buffer_size = %zd, count = %zd, bytes_to_write = %zd\n",
577441b62c1SHarvey Harrison 				__func__, buffer_size, count, bytes_to_write);
57803270634SSteven Haigh 
57903270634SSteven Haigh 			if (copy_from_user(dev->interrupt_out_buffer, buffer, bytes_to_write) != 0) {
58003270634SSteven Haigh 				retval = -EFAULT;
58103270634SSteven Haigh 				goto exit;
58203270634SSteven Haigh 			}
58303270634SSteven Haigh 
58403270634SSteven Haigh 			/* send off the urb */
58503270634SSteven Haigh 			usb_fill_int_urb(
58603270634SSteven Haigh 				dev->interrupt_out_urb,
58703270634SSteven Haigh 				dev->udev,
58803270634SSteven Haigh 				usb_sndintpipe(dev->udev, dev->interrupt_out_endpoint->bEndpointAddress),
58903270634SSteven Haigh 				dev->interrupt_out_buffer,
59003270634SSteven Haigh 				bytes_to_write,
59103270634SSteven Haigh 				adu_interrupt_out_callback,
59203270634SSteven Haigh 				dev,
593f08812d5SPete Zaitcev 				dev->interrupt_out_endpoint->bInterval);
59403270634SSteven Haigh 			dev->interrupt_out_urb->actual_length = bytes_to_write;
595f08812d5SPete Zaitcev 			dev->out_urb_finished = 0;
59603270634SSteven Haigh 			retval = usb_submit_urb(dev->interrupt_out_urb, GFP_KERNEL);
59703270634SSteven Haigh 			if (retval < 0) {
598f08812d5SPete Zaitcev 				dev->out_urb_finished = 1;
599fd3f1917SGreg Kroah-Hartman 				dev_err(&dev->udev->dev, "Couldn't submit "
600fd3f1917SGreg Kroah-Hartman 					"interrupt_out_urb %d\n", retval);
60103270634SSteven Haigh 				goto exit;
60203270634SSteven Haigh 			}
60303270634SSteven Haigh 
60403270634SSteven Haigh 			buffer += bytes_to_write;
60503270634SSteven Haigh 			count -= bytes_to_write;
60603270634SSteven Haigh 
60703270634SSteven Haigh 			bytes_written += bytes_to_write;
60803270634SSteven Haigh 		}
60903270634SSteven Haigh 	}
610f08812d5SPete Zaitcev 	mutex_unlock(&dev->mtx);
611f08812d5SPete Zaitcev 	return bytes_written;
61203270634SSteven Haigh 
61303270634SSteven Haigh exit:
6148293c568SMatthias Kaehlcke 	mutex_unlock(&dev->mtx);
615ebc3ac14SOliver Neukum exit_nolock:
616f08812d5SPete Zaitcev 	return retval;
61703270634SSteven Haigh 
618f08812d5SPete Zaitcev exit_onqueue:
619f08812d5SPete Zaitcev 	remove_wait_queue(&dev->write_wait, &waita);
62003270634SSteven Haigh 	return retval;
62103270634SSteven Haigh }
62203270634SSteven Haigh 
62303270634SSteven Haigh /* file operations needed when we register this driver */
62400977a59SArjan van de Ven static const struct file_operations adu_fops = {
62503270634SSteven Haigh 	.owner = THIS_MODULE,
62603270634SSteven Haigh 	.read  = adu_read,
62703270634SSteven Haigh 	.write = adu_write,
62803270634SSteven Haigh 	.open = adu_open,
62903270634SSteven Haigh 	.release = adu_release,
6306038f373SArnd Bergmann 	.llseek = noop_llseek,
63103270634SSteven Haigh };
63203270634SSteven Haigh 
63303270634SSteven Haigh /*
63403270634SSteven Haigh  * usb class driver info in order to get a minor number from the usb core,
63503270634SSteven Haigh  * and to have the device registered with devfs and the driver core
63603270634SSteven Haigh  */
63703270634SSteven Haigh static struct usb_class_driver adu_class = {
63803270634SSteven Haigh 	.name = "usb/adutux%d",
63903270634SSteven Haigh 	.fops = &adu_fops,
64003270634SSteven Haigh 	.minor_base = ADU_MINOR_BASE,
64103270634SSteven Haigh };
64203270634SSteven Haigh 
64303270634SSteven Haigh /**
64403270634SSteven Haigh  * adu_probe
64503270634SSteven Haigh  *
64603270634SSteven Haigh  * Called by the usb core when a new device is connected that it thinks
64703270634SSteven Haigh  * this driver might be interested in.
64803270634SSteven Haigh  */
64903270634SSteven Haigh static int adu_probe(struct usb_interface *interface,
65003270634SSteven Haigh 		     const struct usb_device_id *id)
65103270634SSteven Haigh {
65203270634SSteven Haigh 	struct usb_device *udev = interface_to_usbdev(interface);
65303270634SSteven Haigh 	struct adu_device *dev = NULL;
654e0e90520SJohan Hovold 	int retval = -ENOMEM;
65503270634SSteven Haigh 	int in_end_size;
65603270634SSteven Haigh 	int out_end_size;
657e53e0342SJohan Hovold 	int res;
65803270634SSteven Haigh 
659b595076aSUwe Kleine-König 	/* allocate memory for our device state and initialize it */
66003270634SSteven Haigh 	dev = kzalloc(sizeof(struct adu_device), GFP_KERNEL);
661e0e90520SJohan Hovold 	if (!dev)
662e0e90520SJohan Hovold 		return -ENOMEM;
66303270634SSteven Haigh 
6648293c568SMatthias Kaehlcke 	mutex_init(&dev->mtx);
66503270634SSteven Haigh 	spin_lock_init(&dev->buflock);
66603270634SSteven Haigh 	dev->udev = udev;
66703270634SSteven Haigh 	init_waitqueue_head(&dev->read_wait);
66803270634SSteven Haigh 	init_waitqueue_head(&dev->write_wait);
66903270634SSteven Haigh 
670e53e0342SJohan Hovold 	res = usb_find_common_endpoints_reverse(&interface->altsetting[0],
671e53e0342SJohan Hovold 			NULL, NULL,
672e53e0342SJohan Hovold 			&dev->interrupt_in_endpoint,
673e53e0342SJohan Hovold 			&dev->interrupt_out_endpoint);
674e53e0342SJohan Hovold 	if (res) {
675e53e0342SJohan Hovold 		dev_err(&interface->dev, "interrupt endpoints not found\n");
676e53e0342SJohan Hovold 		retval = res;
67703270634SSteven Haigh 		goto error;
67803270634SSteven Haigh 	}
67903270634SSteven Haigh 
68029cc8897SKuninori Morimoto 	in_end_size = usb_endpoint_maxp(dev->interrupt_in_endpoint);
68129cc8897SKuninori Morimoto 	out_end_size = usb_endpoint_maxp(dev->interrupt_out_endpoint);
68203270634SSteven Haigh 
68303270634SSteven Haigh 	dev->read_buffer_primary = kmalloc((4 * in_end_size), GFP_KERNEL);
684e0e90520SJohan Hovold 	if (!dev->read_buffer_primary)
68503270634SSteven Haigh 		goto error;
68603270634SSteven Haigh 
68703270634SSteven Haigh 	/* debug code prime the buffer */
68803270634SSteven Haigh 	memset(dev->read_buffer_primary, 'a', in_end_size);
68903270634SSteven Haigh 	memset(dev->read_buffer_primary + in_end_size, 'b', in_end_size);
69003270634SSteven Haigh 	memset(dev->read_buffer_primary + (2 * in_end_size), 'c', in_end_size);
69103270634SSteven Haigh 	memset(dev->read_buffer_primary + (3 * in_end_size), 'd', in_end_size);
69203270634SSteven Haigh 
69303270634SSteven Haigh 	dev->read_buffer_secondary = kmalloc((4 * in_end_size), GFP_KERNEL);
694e0e90520SJohan Hovold 	if (!dev->read_buffer_secondary)
69503270634SSteven Haigh 		goto error;
69603270634SSteven Haigh 
69703270634SSteven Haigh 	/* debug code prime the buffer */
69803270634SSteven Haigh 	memset(dev->read_buffer_secondary, 'e', in_end_size);
69903270634SSteven Haigh 	memset(dev->read_buffer_secondary + in_end_size, 'f', in_end_size);
70003270634SSteven Haigh 	memset(dev->read_buffer_secondary + (2 * in_end_size), 'g', in_end_size);
70103270634SSteven Haigh 	memset(dev->read_buffer_secondary + (3 * in_end_size), 'h', in_end_size);
70203270634SSteven Haigh 
70303270634SSteven Haigh 	dev->interrupt_in_buffer = kmalloc(in_end_size, GFP_KERNEL);
704a02b55c8SWolfram Sang 	if (!dev->interrupt_in_buffer)
70503270634SSteven Haigh 		goto error;
70603270634SSteven Haigh 
70703270634SSteven Haigh 	/* debug code prime the buffer */
70803270634SSteven Haigh 	memset(dev->interrupt_in_buffer, 'i', in_end_size);
70903270634SSteven Haigh 
71003270634SSteven Haigh 	dev->interrupt_in_urb = usb_alloc_urb(0, GFP_KERNEL);
71171574a55SWolfram Sang 	if (!dev->interrupt_in_urb)
71203270634SSteven Haigh 		goto error;
71303270634SSteven Haigh 	dev->interrupt_out_buffer = kmalloc(out_end_size, GFP_KERNEL);
714a02b55c8SWolfram Sang 	if (!dev->interrupt_out_buffer)
71503270634SSteven Haigh 		goto error;
71603270634SSteven Haigh 	dev->interrupt_out_urb = usb_alloc_urb(0, GFP_KERNEL);
71771574a55SWolfram Sang 	if (!dev->interrupt_out_urb)
71803270634SSteven Haigh 		goto error;
71903270634SSteven Haigh 
72003270634SSteven Haigh 	if (!usb_string(udev, udev->descriptor.iSerialNumber, dev->serial_number,
72103270634SSteven Haigh 			sizeof(dev->serial_number))) {
72203270634SSteven Haigh 		dev_err(&interface->dev, "Could not retrieve serial number\n");
723e0e90520SJohan Hovold 		retval = -EIO;
72403270634SSteven Haigh 		goto error;
72503270634SSteven Haigh 	}
72666d4bc30SGreg Kroah-Hartman 	dev_dbg(&interface->dev,"serial_number=%s", dev->serial_number);
72703270634SSteven Haigh 
72803270634SSteven Haigh 	/* we can register the device now, as it is ready */
72903270634SSteven Haigh 	usb_set_intfdata(interface, dev);
73003270634SSteven Haigh 
73103270634SSteven Haigh 	retval = usb_register_dev(interface, &adu_class);
73203270634SSteven Haigh 
73303270634SSteven Haigh 	if (retval) {
73403270634SSteven Haigh 		/* something prevented us from registering this driver */
73503270634SSteven Haigh 		dev_err(&interface->dev, "Not able to get a minor for this device.\n");
73603270634SSteven Haigh 		usb_set_intfdata(interface, NULL);
73703270634SSteven Haigh 		goto error;
73803270634SSteven Haigh 	}
73903270634SSteven Haigh 
74003270634SSteven Haigh 	dev->minor = interface->minor;
74103270634SSteven Haigh 
74203270634SSteven Haigh 	/* let the user know what node this device is now attached to */
743898eb71cSJoe Perches 	dev_info(&interface->dev, "ADU%d %s now attached to /dev/usb/adutux%d\n",
744d482b9d5SJohan Hovold 		 le16_to_cpu(udev->descriptor.idProduct), dev->serial_number,
74503270634SSteven Haigh 		 (dev->minor - ADU_MINOR_BASE));
746e0e90520SJohan Hovold 
747e0e90520SJohan Hovold 	return 0;
74803270634SSteven Haigh 
74903270634SSteven Haigh error:
75003270634SSteven Haigh 	adu_delete(dev);
75103270634SSteven Haigh 	return retval;
75203270634SSteven Haigh }
75303270634SSteven Haigh 
75403270634SSteven Haigh /**
75503270634SSteven Haigh  * adu_disconnect
75603270634SSteven Haigh  *
75703270634SSteven Haigh  * Called by the usb core when the device is removed from the system.
75803270634SSteven Haigh  */
75903270634SSteven Haigh static void adu_disconnect(struct usb_interface *interface)
76003270634SSteven Haigh {
76103270634SSteven Haigh 	struct adu_device *dev;
76203270634SSteven Haigh 
76303270634SSteven Haigh 	dev = usb_get_intfdata(interface);
76403270634SSteven Haigh 
7658293c568SMatthias Kaehlcke 	mutex_lock(&dev->mtx);	/* not interruptible */
766f08812d5SPete Zaitcev 	dev->udev = NULL;	/* poison */
767f08812d5SPete Zaitcev 	usb_deregister_dev(interface, &adu_class);
768f08812d5SPete Zaitcev 	mutex_unlock(&dev->mtx);
769f08812d5SPete Zaitcev 
770f08812d5SPete Zaitcev 	mutex_lock(&adutux_mutex);
771f08812d5SPete Zaitcev 	usb_set_intfdata(interface, NULL);
772d4ead16fSAlan Stern 
77303270634SSteven Haigh 	/* if the device is not opened, then we clean up right now */
774f08812d5SPete Zaitcev 	if (!dev->open_count)
77503270634SSteven Haigh 		adu_delete(dev);
776f08812d5SPete Zaitcev 
777f08812d5SPete Zaitcev 	mutex_unlock(&adutux_mutex);
77803270634SSteven Haigh }
77903270634SSteven Haigh 
78003270634SSteven Haigh /* usb specific object needed to register this driver with the usb subsystem */
78103270634SSteven Haigh static struct usb_driver adu_driver = {
78203270634SSteven Haigh 	.name = "adutux",
78303270634SSteven Haigh 	.probe = adu_probe,
78403270634SSteven Haigh 	.disconnect = adu_disconnect,
78503270634SSteven Haigh 	.id_table = device_table,
78603270634SSteven Haigh };
78703270634SSteven Haigh 
78865db4305SGreg Kroah-Hartman module_usb_driver(adu_driver);
78903270634SSteven Haigh 
79003270634SSteven Haigh MODULE_AUTHOR(DRIVER_AUTHOR);
79103270634SSteven Haigh MODULE_DESCRIPTION(DRIVER_DESC);
79203270634SSteven Haigh MODULE_LICENSE("GPL");
793