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