xref: /openbmc/linux/drivers/usb/misc/adutux.c (revision b8d312aa)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
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  *
9  * 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 
17 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18 
19 #include <linux/kernel.h>
20 #include <linux/sched/signal.h>
21 #include <linux/errno.h>
22 #include <linux/slab.h>
23 #include <linux/module.h>
24 #include <linux/usb.h>
25 #include <linux/mutex.h>
26 #include <linux/uaccess.h>
27 
28 #define DRIVER_AUTHOR "John Homppi"
29 #define DRIVER_DESC "adutux (see www.ontrak.net)"
30 
31 /* 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 */
36 static const struct usb_device_id device_table[] = {
37 	{ USB_DEVICE(ADU_VENDOR_ID, ADU_PRODUCT_ID) },		/* ADU100 */
38 	{ USB_DEVICE(ADU_VENDOR_ID, ADU_PRODUCT_ID+20) },	/* ADU120 */
39 	{ USB_DEVICE(ADU_VENDOR_ID, ADU_PRODUCT_ID+30) },	/* ADU130 */
40 	{ 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 */
43 	{ } /* Terminating entry */
44 };
45 
46 MODULE_DEVICE_TABLE(usb, device_table);
47 
48 #ifdef CONFIG_USB_DYNAMIC_MINORS
49 #define ADU_MINOR_BASE	0
50 #else
51 #define ADU_MINOR_BASE	67
52 #endif
53 
54 /* we can have up to this number of device plugged in at once */
55 #define MAX_DEVICES	16
56 
57 #define COMMAND_TIMEOUT	(2*HZ)
58 
59 /*
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 
69 /* Structure to hold all of our device specific stuff */
70 struct adu_device {
71 	struct mutex		mtx;
72 	struct usb_device *udev; /* save off the usb device pointer */
73 	struct usb_interface *interface;
74 	unsigned int		minor; /* the starting minor number for this device */
75 	char			serial_number[8];
76 
77 	int			open_count; /* number of times this port has been opened */
78 
79 	char		*read_buffer_primary;
80 	int			read_buffer_length;
81 	char		*read_buffer_secondary;
82 	int			secondary_head;
83 	int			secondary_tail;
84 	spinlock_t		buflock;
85 
86 	wait_queue_head_t	read_wait;
87 	wait_queue_head_t	write_wait;
88 
89 	char		*interrupt_in_buffer;
90 	struct usb_endpoint_descriptor *interrupt_in_endpoint;
91 	struct urb	*interrupt_in_urb;
92 	int			read_urb_finished;
93 
94 	char		*interrupt_out_buffer;
95 	struct usb_endpoint_descriptor *interrupt_out_endpoint;
96 	struct urb	*interrupt_out_urb;
97 	int			out_urb_finished;
98 };
99 
100 static DEFINE_MUTEX(adutux_mutex);
101 
102 static struct usb_driver adu_driver;
103 
104 static inline void adu_debug_data(struct device *dev, const char *function,
105 				  int size, const unsigned char *data)
106 {
107 	dev_dbg(dev, "%s - length = %d, data = %*ph\n",
108 		function, size, size, data);
109 }
110 
111 /**
112  * adu_abort_transfers
113  *      aborts transfers and frees associated data structures
114  */
115 static void adu_abort_transfers(struct adu_device *dev)
116 {
117 	unsigned long flags;
118 
119 	if (dev->udev == NULL)
120 		return;
121 
122 	/* shutdown transfer */
123 
124 	/* XXX Anchor these instead */
125 	spin_lock_irqsave(&dev->buflock, flags);
126 	if (!dev->read_urb_finished) {
127 		spin_unlock_irqrestore(&dev->buflock, flags);
128 		usb_kill_urb(dev->interrupt_in_urb);
129 	} else
130 		spin_unlock_irqrestore(&dev->buflock, flags);
131 
132 	spin_lock_irqsave(&dev->buflock, flags);
133 	if (!dev->out_urb_finished) {
134 		spin_unlock_irqrestore(&dev->buflock, flags);
135 		wait_event_timeout(dev->write_wait, dev->out_urb_finished,
136 			COMMAND_TIMEOUT);
137 		usb_kill_urb(dev->interrupt_out_urb);
138 	} else
139 		spin_unlock_irqrestore(&dev->buflock, flags);
140 }
141 
142 static void adu_delete(struct adu_device *dev)
143 {
144 	/* free data structures */
145 	usb_free_urb(dev->interrupt_in_urb);
146 	usb_free_urb(dev->interrupt_out_urb);
147 	kfree(dev->read_buffer_primary);
148 	kfree(dev->read_buffer_secondary);
149 	kfree(dev->interrupt_in_buffer);
150 	kfree(dev->interrupt_out_buffer);
151 	kfree(dev);
152 }
153 
154 static void adu_interrupt_in_callback(struct urb *urb)
155 {
156 	struct adu_device *dev = urb->context;
157 	int status = urb->status;
158 	unsigned long flags;
159 
160 	adu_debug_data(&dev->udev->dev, __func__,
161 		       urb->actual_length, urb->transfer_buffer);
162 
163 	spin_lock_irqsave(&dev->buflock, flags);
164 
165 	if (status != 0) {
166 		if ((status != -ENOENT) && (status != -ECONNRESET) &&
167 			(status != -ESHUTDOWN)) {
168 			dev_dbg(&dev->udev->dev,
169 				"%s : nonzero status received: %d\n",
170 				__func__, status);
171 		}
172 		goto exit;
173 	}
174 
175 	if (urb->actual_length > 0 && dev->interrupt_in_buffer[0] != 0x00) {
176 		if (dev->read_buffer_length <
177 		    (4 * usb_endpoint_maxp(dev->interrupt_in_endpoint)) -
178 		     (urb->actual_length)) {
179 			memcpy (dev->read_buffer_primary +
180 				dev->read_buffer_length,
181 				dev->interrupt_in_buffer, urb->actual_length);
182 
183 			dev->read_buffer_length += urb->actual_length;
184 			dev_dbg(&dev->udev->dev,"%s reading  %d\n", __func__,
185 				urb->actual_length);
186 		} else {
187 			dev_dbg(&dev->udev->dev,"%s : read_buffer overflow\n",
188 				__func__);
189 		}
190 	}
191 
192 exit:
193 	dev->read_urb_finished = 1;
194 	spin_unlock_irqrestore(&dev->buflock, flags);
195 	/* always wake up so we recover from errors */
196 	wake_up_interruptible(&dev->read_wait);
197 }
198 
199 static void adu_interrupt_out_callback(struct urb *urb)
200 {
201 	struct adu_device *dev = urb->context;
202 	int status = urb->status;
203 	unsigned long flags;
204 
205 	adu_debug_data(&dev->udev->dev, __func__,
206 		       urb->actual_length, urb->transfer_buffer);
207 
208 	if (status != 0) {
209 		if ((status != -ENOENT) &&
210 		    (status != -ECONNRESET)) {
211 			dev_dbg(&dev->udev->dev,
212 				"%s :nonzero status received: %d\n", __func__,
213 				status);
214 		}
215 		return;
216 	}
217 
218 	spin_lock_irqsave(&dev->buflock, flags);
219 	dev->out_urb_finished = 1;
220 	wake_up(&dev->write_wait);
221 	spin_unlock_irqrestore(&dev->buflock, flags);
222 }
223 
224 static int adu_open(struct inode *inode, struct file *file)
225 {
226 	struct adu_device *dev = NULL;
227 	struct usb_interface *interface;
228 	int subminor;
229 	int retval;
230 
231 	subminor = iminor(inode);
232 
233 	retval = mutex_lock_interruptible(&adutux_mutex);
234 	if (retval)
235 		goto exit_no_lock;
236 
237 	interface = usb_find_interface(&adu_driver, subminor);
238 	if (!interface) {
239 		pr_err("%s - error, can't find device for minor %d\n",
240 		       __func__, subminor);
241 		retval = -ENODEV;
242 		goto exit_no_device;
243 	}
244 
245 	dev = usb_get_intfdata(interface);
246 	if (!dev || !dev->udev) {
247 		retval = -ENODEV;
248 		goto exit_no_device;
249 	}
250 
251 	/* check that nobody else is using the device */
252 	if (dev->open_count) {
253 		retval = -EBUSY;
254 		goto exit_no_device;
255 	}
256 
257 	++dev->open_count;
258 	dev_dbg(&dev->udev->dev, "%s: open count %d\n", __func__,
259 		dev->open_count);
260 
261 	/* save device in the file's private structure */
262 	file->private_data = dev;
263 
264 	/* initialize in direction */
265 	dev->read_buffer_length = 0;
266 
267 	/* fixup first read by having urb waiting for it */
268 	usb_fill_int_urb(dev->interrupt_in_urb, dev->udev,
269 			 usb_rcvintpipe(dev->udev,
270 					dev->interrupt_in_endpoint->bEndpointAddress),
271 			 dev->interrupt_in_buffer,
272 			 usb_endpoint_maxp(dev->interrupt_in_endpoint),
273 			 adu_interrupt_in_callback, dev,
274 			 dev->interrupt_in_endpoint->bInterval);
275 	dev->read_urb_finished = 0;
276 	if (usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL))
277 		dev->read_urb_finished = 1;
278 	/* we ignore failure */
279 	/* end of fixup for first read */
280 
281 	/* initialize out direction */
282 	dev->out_urb_finished = 1;
283 
284 	retval = 0;
285 
286 exit_no_device:
287 	mutex_unlock(&adutux_mutex);
288 exit_no_lock:
289 	return retval;
290 }
291 
292 static void adu_release_internal(struct adu_device *dev)
293 {
294 	/* decrement our usage count for the device */
295 	--dev->open_count;
296 	dev_dbg(&dev->udev->dev, "%s : open count %d\n", __func__,
297 		dev->open_count);
298 	if (dev->open_count <= 0) {
299 		adu_abort_transfers(dev);
300 		dev->open_count = 0;
301 	}
302 }
303 
304 static int adu_release(struct inode *inode, struct file *file)
305 {
306 	struct adu_device *dev;
307 	int retval = 0;
308 
309 	if (file == NULL) {
310 		retval = -ENODEV;
311 		goto exit;
312 	}
313 
314 	dev = file->private_data;
315 	if (dev == NULL) {
316 		retval = -ENODEV;
317 		goto exit;
318 	}
319 
320 	mutex_lock(&adutux_mutex); /* not interruptible */
321 
322 	if (dev->open_count <= 0) {
323 		dev_dbg(&dev->udev->dev, "%s : device not opened\n", __func__);
324 		retval = -ENODEV;
325 		goto unlock;
326 	}
327 
328 	adu_release_internal(dev);
329 	if (dev->udev == NULL) {
330 		/* the device was unplugged before the file was released */
331 		if (!dev->open_count)	/* ... and we're the last user */
332 			adu_delete(dev);
333 	}
334 unlock:
335 	mutex_unlock(&adutux_mutex);
336 exit:
337 	return retval;
338 }
339 
340 static ssize_t adu_read(struct file *file, __user char *buffer, size_t count,
341 			loff_t *ppos)
342 {
343 	struct adu_device *dev;
344 	size_t bytes_read = 0;
345 	size_t bytes_to_read = count;
346 	int retval = 0;
347 	int timeout = 0;
348 	int should_submit = 0;
349 	unsigned long flags;
350 	DECLARE_WAITQUEUE(wait, current);
351 
352 	dev = file->private_data;
353 	if (mutex_lock_interruptible(&dev->mtx))
354 		return -ERESTARTSYS;
355 
356 	/* verify that the device wasn't unplugged */
357 	if (dev->udev == NULL) {
358 		retval = -ENODEV;
359 		pr_err("No device or device unplugged %d\n", retval);
360 		goto exit;
361 	}
362 
363 	/* verify that some data was requested */
364 	if (count == 0) {
365 		dev_dbg(&dev->udev->dev, "%s : read request of 0 bytes\n",
366 			__func__);
367 		goto exit;
368 	}
369 
370 	timeout = COMMAND_TIMEOUT;
371 	dev_dbg(&dev->udev->dev, "%s : about to start looping\n", __func__);
372 	while (bytes_to_read) {
373 		size_t data_in_secondary = dev->secondary_tail - dev->secondary_head;
374 		dev_dbg(&dev->udev->dev,
375 			"%s : while, data_in_secondary=%zu, status=%d\n",
376 			__func__, data_in_secondary,
377 			dev->interrupt_in_urb->status);
378 
379 		if (data_in_secondary) {
380 			/* drain secondary buffer */
381 			size_t amount = min(bytes_to_read, data_in_secondary);
382 			if (copy_to_user(buffer, dev->read_buffer_secondary+dev->secondary_head, amount)) {
383 				retval = -EFAULT;
384 				goto exit;
385 			}
386 			dev->secondary_head += amount;
387 			bytes_read += amount;
388 			bytes_to_read -= amount;
389 		} else {
390 			/* we check the primary buffer */
391 			spin_lock_irqsave (&dev->buflock, flags);
392 			if (dev->read_buffer_length) {
393 				/* we secure access to the primary */
394 				char *tmp;
395 				dev_dbg(&dev->udev->dev,
396 					"%s : swap, read_buffer_length = %d\n",
397 					__func__, dev->read_buffer_length);
398 				tmp = dev->read_buffer_secondary;
399 				dev->read_buffer_secondary = dev->read_buffer_primary;
400 				dev->read_buffer_primary = tmp;
401 				dev->secondary_head = 0;
402 				dev->secondary_tail = dev->read_buffer_length;
403 				dev->read_buffer_length = 0;
404 				spin_unlock_irqrestore(&dev->buflock, flags);
405 				/* we have a free buffer so use it */
406 				should_submit = 1;
407 			} else {
408 				/* even the primary was empty - we may need to do IO */
409 				if (!dev->read_urb_finished) {
410 					/* somebody is doing IO */
411 					spin_unlock_irqrestore(&dev->buflock, flags);
412 					dev_dbg(&dev->udev->dev,
413 						"%s : submitted already\n",
414 						__func__);
415 				} else {
416 					/* we must initiate input */
417 					dev_dbg(&dev->udev->dev,
418 						"%s : initiate input\n",
419 						__func__);
420 					dev->read_urb_finished = 0;
421 					spin_unlock_irqrestore(&dev->buflock, flags);
422 
423 					usb_fill_int_urb(dev->interrupt_in_urb, dev->udev,
424 							usb_rcvintpipe(dev->udev,
425 								dev->interrupt_in_endpoint->bEndpointAddress),
426 							 dev->interrupt_in_buffer,
427 							 usb_endpoint_maxp(dev->interrupt_in_endpoint),
428 							 adu_interrupt_in_callback,
429 							 dev,
430 							 dev->interrupt_in_endpoint->bInterval);
431 					retval = usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL);
432 					if (retval) {
433 						dev->read_urb_finished = 1;
434 						if (retval == -ENOMEM) {
435 							retval = bytes_read ? bytes_read : -ENOMEM;
436 						}
437 						dev_dbg(&dev->udev->dev,
438 							"%s : submit failed\n",
439 							__func__);
440 						goto exit;
441 					}
442 				}
443 
444 				/* we wait for I/O to complete */
445 				set_current_state(TASK_INTERRUPTIBLE);
446 				add_wait_queue(&dev->read_wait, &wait);
447 				spin_lock_irqsave(&dev->buflock, flags);
448 				if (!dev->read_urb_finished) {
449 					spin_unlock_irqrestore(&dev->buflock, flags);
450 					timeout = schedule_timeout(COMMAND_TIMEOUT);
451 				} else {
452 					spin_unlock_irqrestore(&dev->buflock, flags);
453 					set_current_state(TASK_RUNNING);
454 				}
455 				remove_wait_queue(&dev->read_wait, &wait);
456 
457 				if (timeout <= 0) {
458 					dev_dbg(&dev->udev->dev,
459 						"%s : timeout\n", __func__);
460 					retval = bytes_read ? bytes_read : -ETIMEDOUT;
461 					goto exit;
462 				}
463 
464 				if (signal_pending(current)) {
465 					dev_dbg(&dev->udev->dev,
466 						"%s : signal pending\n",
467 						__func__);
468 					retval = bytes_read ? bytes_read : -EINTR;
469 					goto exit;
470 				}
471 			}
472 		}
473 	}
474 
475 	retval = bytes_read;
476 	/* if the primary buffer is empty then use it */
477 	spin_lock_irqsave(&dev->buflock, flags);
478 	if (should_submit && dev->read_urb_finished) {
479 		dev->read_urb_finished = 0;
480 		spin_unlock_irqrestore(&dev->buflock, flags);
481 		usb_fill_int_urb(dev->interrupt_in_urb, dev->udev,
482 				 usb_rcvintpipe(dev->udev,
483 					dev->interrupt_in_endpoint->bEndpointAddress),
484 				dev->interrupt_in_buffer,
485 				usb_endpoint_maxp(dev->interrupt_in_endpoint),
486 				adu_interrupt_in_callback,
487 				dev,
488 				dev->interrupt_in_endpoint->bInterval);
489 		if (usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL) != 0)
490 			dev->read_urb_finished = 1;
491 		/* we ignore failure */
492 	} else {
493 		spin_unlock_irqrestore(&dev->buflock, flags);
494 	}
495 
496 exit:
497 	/* unlock the device */
498 	mutex_unlock(&dev->mtx);
499 
500 	return retval;
501 }
502 
503 static ssize_t adu_write(struct file *file, const __user char *buffer,
504 			 size_t count, loff_t *ppos)
505 {
506 	DECLARE_WAITQUEUE(waita, current);
507 	struct adu_device *dev;
508 	size_t bytes_written = 0;
509 	size_t bytes_to_write;
510 	size_t buffer_size;
511 	unsigned long flags;
512 	int retval;
513 
514 	dev = file->private_data;
515 
516 	retval = mutex_lock_interruptible(&dev->mtx);
517 	if (retval)
518 		goto exit_nolock;
519 
520 	/* verify that the device wasn't unplugged */
521 	if (dev->udev == NULL) {
522 		retval = -ENODEV;
523 		pr_err("No device or device unplugged %d\n", retval);
524 		goto exit;
525 	}
526 
527 	/* verify that we actually have some data to write */
528 	if (count == 0) {
529 		dev_dbg(&dev->udev->dev, "%s : write request of 0 bytes\n",
530 			__func__);
531 		goto exit;
532 	}
533 
534 	while (count > 0) {
535 		add_wait_queue(&dev->write_wait, &waita);
536 		set_current_state(TASK_INTERRUPTIBLE);
537 		spin_lock_irqsave(&dev->buflock, flags);
538 		if (!dev->out_urb_finished) {
539 			spin_unlock_irqrestore(&dev->buflock, flags);
540 
541 			mutex_unlock(&dev->mtx);
542 			if (signal_pending(current)) {
543 				dev_dbg(&dev->udev->dev, "%s : interrupted\n",
544 					__func__);
545 				set_current_state(TASK_RUNNING);
546 				retval = -EINTR;
547 				goto exit_onqueue;
548 			}
549 			if (schedule_timeout(COMMAND_TIMEOUT) == 0) {
550 				dev_dbg(&dev->udev->dev,
551 					"%s - command timed out.\n", __func__);
552 				retval = -ETIMEDOUT;
553 				goto exit_onqueue;
554 			}
555 			remove_wait_queue(&dev->write_wait, &waita);
556 			retval = mutex_lock_interruptible(&dev->mtx);
557 			if (retval) {
558 				retval = bytes_written ? bytes_written : retval;
559 				goto exit_nolock;
560 			}
561 
562 			dev_dbg(&dev->udev->dev,
563 				"%s : in progress, count = %zd\n",
564 				__func__, count);
565 		} else {
566 			spin_unlock_irqrestore(&dev->buflock, flags);
567 			set_current_state(TASK_RUNNING);
568 			remove_wait_queue(&dev->write_wait, &waita);
569 			dev_dbg(&dev->udev->dev, "%s : sending, count = %zd\n",
570 				__func__, count);
571 
572 			/* write the data into interrupt_out_buffer from userspace */
573 			buffer_size = usb_endpoint_maxp(dev->interrupt_out_endpoint);
574 			bytes_to_write = count > buffer_size ? buffer_size : count;
575 			dev_dbg(&dev->udev->dev,
576 				"%s : buffer_size = %zd, count = %zd, bytes_to_write = %zd\n",
577 				__func__, buffer_size, count, bytes_to_write);
578 
579 			if (copy_from_user(dev->interrupt_out_buffer, buffer, bytes_to_write) != 0) {
580 				retval = -EFAULT;
581 				goto exit;
582 			}
583 
584 			/* send off the urb */
585 			usb_fill_int_urb(
586 				dev->interrupt_out_urb,
587 				dev->udev,
588 				usb_sndintpipe(dev->udev, dev->interrupt_out_endpoint->bEndpointAddress),
589 				dev->interrupt_out_buffer,
590 				bytes_to_write,
591 				adu_interrupt_out_callback,
592 				dev,
593 				dev->interrupt_out_endpoint->bInterval);
594 			dev->interrupt_out_urb->actual_length = bytes_to_write;
595 			dev->out_urb_finished = 0;
596 			retval = usb_submit_urb(dev->interrupt_out_urb, GFP_KERNEL);
597 			if (retval < 0) {
598 				dev->out_urb_finished = 1;
599 				dev_err(&dev->udev->dev, "Couldn't submit "
600 					"interrupt_out_urb %d\n", retval);
601 				goto exit;
602 			}
603 
604 			buffer += bytes_to_write;
605 			count -= bytes_to_write;
606 
607 			bytes_written += bytes_to_write;
608 		}
609 	}
610 	mutex_unlock(&dev->mtx);
611 	return bytes_written;
612 
613 exit:
614 	mutex_unlock(&dev->mtx);
615 exit_nolock:
616 	return retval;
617 
618 exit_onqueue:
619 	remove_wait_queue(&dev->write_wait, &waita);
620 	return retval;
621 }
622 
623 /* file operations needed when we register this driver */
624 static const struct file_operations adu_fops = {
625 	.owner = THIS_MODULE,
626 	.read  = adu_read,
627 	.write = adu_write,
628 	.open = adu_open,
629 	.release = adu_release,
630 	.llseek = noop_llseek,
631 };
632 
633 /*
634  * usb class driver info in order to get a minor number from the usb core,
635  * and to have the device registered with devfs and the driver core
636  */
637 static struct usb_class_driver adu_class = {
638 	.name = "usb/adutux%d",
639 	.fops = &adu_fops,
640 	.minor_base = ADU_MINOR_BASE,
641 };
642 
643 /**
644  * adu_probe
645  *
646  * Called by the usb core when a new device is connected that it thinks
647  * this driver might be interested in.
648  */
649 static int adu_probe(struct usb_interface *interface,
650 		     const struct usb_device_id *id)
651 {
652 	struct usb_device *udev = interface_to_usbdev(interface);
653 	struct adu_device *dev = NULL;
654 	int retval = -ENOMEM;
655 	int in_end_size;
656 	int out_end_size;
657 	int res;
658 
659 	/* allocate memory for our device state and initialize it */
660 	dev = kzalloc(sizeof(struct adu_device), GFP_KERNEL);
661 	if (!dev)
662 		return -ENOMEM;
663 
664 	mutex_init(&dev->mtx);
665 	spin_lock_init(&dev->buflock);
666 	dev->udev = udev;
667 	init_waitqueue_head(&dev->read_wait);
668 	init_waitqueue_head(&dev->write_wait);
669 
670 	res = usb_find_common_endpoints_reverse(&interface->altsetting[0],
671 			NULL, NULL,
672 			&dev->interrupt_in_endpoint,
673 			&dev->interrupt_out_endpoint);
674 	if (res) {
675 		dev_err(&interface->dev, "interrupt endpoints not found\n");
676 		retval = res;
677 		goto error;
678 	}
679 
680 	in_end_size = usb_endpoint_maxp(dev->interrupt_in_endpoint);
681 	out_end_size = usb_endpoint_maxp(dev->interrupt_out_endpoint);
682 
683 	dev->read_buffer_primary = kmalloc((4 * in_end_size), GFP_KERNEL);
684 	if (!dev->read_buffer_primary)
685 		goto error;
686 
687 	/* debug code prime the buffer */
688 	memset(dev->read_buffer_primary, 'a', in_end_size);
689 	memset(dev->read_buffer_primary + in_end_size, 'b', in_end_size);
690 	memset(dev->read_buffer_primary + (2 * in_end_size), 'c', in_end_size);
691 	memset(dev->read_buffer_primary + (3 * in_end_size), 'd', in_end_size);
692 
693 	dev->read_buffer_secondary = kmalloc((4 * in_end_size), GFP_KERNEL);
694 	if (!dev->read_buffer_secondary)
695 		goto error;
696 
697 	/* debug code prime the buffer */
698 	memset(dev->read_buffer_secondary, 'e', in_end_size);
699 	memset(dev->read_buffer_secondary + in_end_size, 'f', in_end_size);
700 	memset(dev->read_buffer_secondary + (2 * in_end_size), 'g', in_end_size);
701 	memset(dev->read_buffer_secondary + (3 * in_end_size), 'h', in_end_size);
702 
703 	dev->interrupt_in_buffer = kmalloc(in_end_size, GFP_KERNEL);
704 	if (!dev->interrupt_in_buffer)
705 		goto error;
706 
707 	/* debug code prime the buffer */
708 	memset(dev->interrupt_in_buffer, 'i', in_end_size);
709 
710 	dev->interrupt_in_urb = usb_alloc_urb(0, GFP_KERNEL);
711 	if (!dev->interrupt_in_urb)
712 		goto error;
713 	dev->interrupt_out_buffer = kmalloc(out_end_size, GFP_KERNEL);
714 	if (!dev->interrupt_out_buffer)
715 		goto error;
716 	dev->interrupt_out_urb = usb_alloc_urb(0, GFP_KERNEL);
717 	if (!dev->interrupt_out_urb)
718 		goto error;
719 
720 	if (!usb_string(udev, udev->descriptor.iSerialNumber, dev->serial_number,
721 			sizeof(dev->serial_number))) {
722 		dev_err(&interface->dev, "Could not retrieve serial number\n");
723 		retval = -EIO;
724 		goto error;
725 	}
726 	dev_dbg(&interface->dev,"serial_number=%s", dev->serial_number);
727 
728 	/* we can register the device now, as it is ready */
729 	usb_set_intfdata(interface, dev);
730 
731 	retval = usb_register_dev(interface, &adu_class);
732 
733 	if (retval) {
734 		/* something prevented us from registering this driver */
735 		dev_err(&interface->dev, "Not able to get a minor for this device.\n");
736 		usb_set_intfdata(interface, NULL);
737 		goto error;
738 	}
739 
740 	dev->minor = interface->minor;
741 
742 	/* let the user know what node this device is now attached to */
743 	dev_info(&interface->dev, "ADU%d %s now attached to /dev/usb/adutux%d\n",
744 		 le16_to_cpu(udev->descriptor.idProduct), dev->serial_number,
745 		 (dev->minor - ADU_MINOR_BASE));
746 
747 	return 0;
748 
749 error:
750 	adu_delete(dev);
751 	return retval;
752 }
753 
754 /**
755  * adu_disconnect
756  *
757  * Called by the usb core when the device is removed from the system.
758  */
759 static void adu_disconnect(struct usb_interface *interface)
760 {
761 	struct adu_device *dev;
762 
763 	dev = usb_get_intfdata(interface);
764 
765 	mutex_lock(&dev->mtx);	/* not interruptible */
766 	dev->udev = NULL;	/* poison */
767 	usb_deregister_dev(interface, &adu_class);
768 	mutex_unlock(&dev->mtx);
769 
770 	mutex_lock(&adutux_mutex);
771 	usb_set_intfdata(interface, NULL);
772 
773 	/* if the device is not opened, then we clean up right now */
774 	if (!dev->open_count)
775 		adu_delete(dev);
776 
777 	mutex_unlock(&adutux_mutex);
778 }
779 
780 /* usb specific object needed to register this driver with the usb subsystem */
781 static struct usb_driver adu_driver = {
782 	.name = "adutux",
783 	.probe = adu_probe,
784 	.disconnect = adu_disconnect,
785 	.id_table = device_table,
786 };
787 
788 module_usb_driver(adu_driver);
789 
790 MODULE_AUTHOR(DRIVER_AUTHOR);
791 MODULE_DESCRIPTION(DRIVER_DESC);
792 MODULE_LICENSE("GPL");
793