xref: /openbmc/linux/drivers/usb/misc/adutux.c (revision 1c2dd16a)
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/sched/signal.h>
25 #include <linux/errno.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 		} else {
394 			/* we check the primary buffer */
395 			spin_lock_irqsave (&dev->buflock, flags);
396 			if (dev->read_buffer_length) {
397 				/* we secure access to the primary */
398 				char *tmp;
399 				dev_dbg(&dev->udev->dev,
400 					"%s : swap, read_buffer_length = %d\n",
401 					__func__, dev->read_buffer_length);
402 				tmp = dev->read_buffer_secondary;
403 				dev->read_buffer_secondary = dev->read_buffer_primary;
404 				dev->read_buffer_primary = tmp;
405 				dev->secondary_head = 0;
406 				dev->secondary_tail = dev->read_buffer_length;
407 				dev->read_buffer_length = 0;
408 				spin_unlock_irqrestore(&dev->buflock, flags);
409 				/* we have a free buffer so use it */
410 				should_submit = 1;
411 			} else {
412 				/* even the primary was empty - we may need to do IO */
413 				if (!dev->read_urb_finished) {
414 					/* somebody is doing IO */
415 					spin_unlock_irqrestore(&dev->buflock, flags);
416 					dev_dbg(&dev->udev->dev,
417 						"%s : submitted already\n",
418 						__func__);
419 				} else {
420 					/* we must initiate input */
421 					dev_dbg(&dev->udev->dev,
422 						"%s : initiate input\n",
423 						__func__);
424 					dev->read_urb_finished = 0;
425 					spin_unlock_irqrestore(&dev->buflock, flags);
426 
427 					usb_fill_int_urb(dev->interrupt_in_urb, dev->udev,
428 							usb_rcvintpipe(dev->udev,
429 								dev->interrupt_in_endpoint->bEndpointAddress),
430 							 dev->interrupt_in_buffer,
431 							 usb_endpoint_maxp(dev->interrupt_in_endpoint),
432 							 adu_interrupt_in_callback,
433 							 dev,
434 							 dev->interrupt_in_endpoint->bInterval);
435 					retval = usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL);
436 					if (retval) {
437 						dev->read_urb_finished = 1;
438 						if (retval == -ENOMEM) {
439 							retval = bytes_read ? bytes_read : -ENOMEM;
440 						}
441 						dev_dbg(&dev->udev->dev,
442 							"%s : submit failed\n",
443 							__func__);
444 						goto exit;
445 					}
446 				}
447 
448 				/* we wait for I/O to complete */
449 				set_current_state(TASK_INTERRUPTIBLE);
450 				add_wait_queue(&dev->read_wait, &wait);
451 				spin_lock_irqsave(&dev->buflock, flags);
452 				if (!dev->read_urb_finished) {
453 					spin_unlock_irqrestore(&dev->buflock, flags);
454 					timeout = schedule_timeout(COMMAND_TIMEOUT);
455 				} else {
456 					spin_unlock_irqrestore(&dev->buflock, flags);
457 					set_current_state(TASK_RUNNING);
458 				}
459 				remove_wait_queue(&dev->read_wait, &wait);
460 
461 				if (timeout <= 0) {
462 					dev_dbg(&dev->udev->dev,
463 						"%s : timeout\n", __func__);
464 					retval = bytes_read ? bytes_read : -ETIMEDOUT;
465 					goto exit;
466 				}
467 
468 				if (signal_pending(current)) {
469 					dev_dbg(&dev->udev->dev,
470 						"%s : signal pending\n",
471 						__func__);
472 					retval = bytes_read ? bytes_read : -EINTR;
473 					goto exit;
474 				}
475 			}
476 		}
477 	}
478 
479 	retval = bytes_read;
480 	/* if the primary buffer is empty then use it */
481 	spin_lock_irqsave(&dev->buflock, flags);
482 	if (should_submit && dev->read_urb_finished) {
483 		dev->read_urb_finished = 0;
484 		spin_unlock_irqrestore(&dev->buflock, flags);
485 		usb_fill_int_urb(dev->interrupt_in_urb, dev->udev,
486 				 usb_rcvintpipe(dev->udev,
487 					dev->interrupt_in_endpoint->bEndpointAddress),
488 				dev->interrupt_in_buffer,
489 				usb_endpoint_maxp(dev->interrupt_in_endpoint),
490 				adu_interrupt_in_callback,
491 				dev,
492 				dev->interrupt_in_endpoint->bInterval);
493 		if (usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL) != 0)
494 			dev->read_urb_finished = 1;
495 		/* we ignore failure */
496 	} else {
497 		spin_unlock_irqrestore(&dev->buflock, flags);
498 	}
499 
500 exit:
501 	/* unlock the device */
502 	mutex_unlock(&dev->mtx);
503 
504 	return retval;
505 }
506 
507 static ssize_t adu_write(struct file *file, const __user char *buffer,
508 			 size_t count, loff_t *ppos)
509 {
510 	DECLARE_WAITQUEUE(waita, current);
511 	struct adu_device *dev;
512 	size_t bytes_written = 0;
513 	size_t bytes_to_write;
514 	size_t buffer_size;
515 	unsigned long flags;
516 	int retval;
517 
518 	dev = file->private_data;
519 
520 	retval = mutex_lock_interruptible(&dev->mtx);
521 	if (retval)
522 		goto exit_nolock;
523 
524 	/* verify that the device wasn't unplugged */
525 	if (dev->udev == NULL) {
526 		retval = -ENODEV;
527 		pr_err("No device or device unplugged %d\n", retval);
528 		goto exit;
529 	}
530 
531 	/* verify that we actually have some data to write */
532 	if (count == 0) {
533 		dev_dbg(&dev->udev->dev, "%s : write request of 0 bytes\n",
534 			__func__);
535 		goto exit;
536 	}
537 
538 	while (count > 0) {
539 		add_wait_queue(&dev->write_wait, &waita);
540 		set_current_state(TASK_INTERRUPTIBLE);
541 		spin_lock_irqsave(&dev->buflock, flags);
542 		if (!dev->out_urb_finished) {
543 			spin_unlock_irqrestore(&dev->buflock, flags);
544 
545 			mutex_unlock(&dev->mtx);
546 			if (signal_pending(current)) {
547 				dev_dbg(&dev->udev->dev, "%s : interrupted\n",
548 					__func__);
549 				set_current_state(TASK_RUNNING);
550 				retval = -EINTR;
551 				goto exit_onqueue;
552 			}
553 			if (schedule_timeout(COMMAND_TIMEOUT) == 0) {
554 				dev_dbg(&dev->udev->dev,
555 					"%s - command timed out.\n", __func__);
556 				retval = -ETIMEDOUT;
557 				goto exit_onqueue;
558 			}
559 			remove_wait_queue(&dev->write_wait, &waita);
560 			retval = mutex_lock_interruptible(&dev->mtx);
561 			if (retval) {
562 				retval = bytes_written ? bytes_written : retval;
563 				goto exit_nolock;
564 			}
565 
566 			dev_dbg(&dev->udev->dev,
567 				"%s : in progress, count = %zd\n",
568 				__func__, count);
569 		} else {
570 			spin_unlock_irqrestore(&dev->buflock, flags);
571 			set_current_state(TASK_RUNNING);
572 			remove_wait_queue(&dev->write_wait, &waita);
573 			dev_dbg(&dev->udev->dev, "%s : sending, count = %zd\n",
574 				__func__, count);
575 
576 			/* write the data into interrupt_out_buffer from userspace */
577 			buffer_size = usb_endpoint_maxp(dev->interrupt_out_endpoint);
578 			bytes_to_write = count > buffer_size ? buffer_size : count;
579 			dev_dbg(&dev->udev->dev,
580 				"%s : buffer_size = %zd, count = %zd, bytes_to_write = %zd\n",
581 				__func__, buffer_size, count, bytes_to_write);
582 
583 			if (copy_from_user(dev->interrupt_out_buffer, buffer, bytes_to_write) != 0) {
584 				retval = -EFAULT;
585 				goto exit;
586 			}
587 
588 			/* send off the urb */
589 			usb_fill_int_urb(
590 				dev->interrupt_out_urb,
591 				dev->udev,
592 				usb_sndintpipe(dev->udev, dev->interrupt_out_endpoint->bEndpointAddress),
593 				dev->interrupt_out_buffer,
594 				bytes_to_write,
595 				adu_interrupt_out_callback,
596 				dev,
597 				dev->interrupt_out_endpoint->bInterval);
598 			dev->interrupt_out_urb->actual_length = bytes_to_write;
599 			dev->out_urb_finished = 0;
600 			retval = usb_submit_urb(dev->interrupt_out_urb, GFP_KERNEL);
601 			if (retval < 0) {
602 				dev->out_urb_finished = 1;
603 				dev_err(&dev->udev->dev, "Couldn't submit "
604 					"interrupt_out_urb %d\n", retval);
605 				goto exit;
606 			}
607 
608 			buffer += bytes_to_write;
609 			count -= bytes_to_write;
610 
611 			bytes_written += bytes_to_write;
612 		}
613 	}
614 	mutex_unlock(&dev->mtx);
615 	return bytes_written;
616 
617 exit:
618 	mutex_unlock(&dev->mtx);
619 exit_nolock:
620 	return retval;
621 
622 exit_onqueue:
623 	remove_wait_queue(&dev->write_wait, &waita);
624 	return retval;
625 }
626 
627 /* file operations needed when we register this driver */
628 static const struct file_operations adu_fops = {
629 	.owner = THIS_MODULE,
630 	.read  = adu_read,
631 	.write = adu_write,
632 	.open = adu_open,
633 	.release = adu_release,
634 	.llseek = noop_llseek,
635 };
636 
637 /*
638  * usb class driver info in order to get a minor number from the usb core,
639  * and to have the device registered with devfs and the driver core
640  */
641 static struct usb_class_driver adu_class = {
642 	.name = "usb/adutux%d",
643 	.fops = &adu_fops,
644 	.minor_base = ADU_MINOR_BASE,
645 };
646 
647 /**
648  * adu_probe
649  *
650  * Called by the usb core when a new device is connected that it thinks
651  * this driver might be interested in.
652  */
653 static int adu_probe(struct usb_interface *interface,
654 		     const struct usb_device_id *id)
655 {
656 	struct usb_device *udev = interface_to_usbdev(interface);
657 	struct adu_device *dev = NULL;
658 	struct usb_host_interface *iface_desc;
659 	struct usb_endpoint_descriptor *endpoint;
660 	int retval = -ENODEV;
661 	int in_end_size;
662 	int out_end_size;
663 	int i;
664 
665 	if (udev == NULL) {
666 		dev_err(&interface->dev, "udev is NULL.\n");
667 		goto exit;
668 	}
669 
670 	/* allocate memory for our device state and initialize it */
671 	dev = kzalloc(sizeof(struct adu_device), GFP_KERNEL);
672 	if (!dev) {
673 		retval = -ENOMEM;
674 		goto exit;
675 	}
676 
677 	mutex_init(&dev->mtx);
678 	spin_lock_init(&dev->buflock);
679 	dev->udev = udev;
680 	init_waitqueue_head(&dev->read_wait);
681 	init_waitqueue_head(&dev->write_wait);
682 
683 	iface_desc = &interface->altsetting[0];
684 
685 	/* set up the endpoint information */
686 	for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
687 		endpoint = &iface_desc->endpoint[i].desc;
688 
689 		if (usb_endpoint_is_int_in(endpoint))
690 			dev->interrupt_in_endpoint = endpoint;
691 
692 		if (usb_endpoint_is_int_out(endpoint))
693 			dev->interrupt_out_endpoint = endpoint;
694 	}
695 	if (dev->interrupt_in_endpoint == NULL) {
696 		dev_err(&interface->dev, "interrupt in endpoint not found\n");
697 		goto error;
698 	}
699 	if (dev->interrupt_out_endpoint == NULL) {
700 		dev_err(&interface->dev, "interrupt out endpoint not found\n");
701 		goto error;
702 	}
703 
704 	in_end_size = usb_endpoint_maxp(dev->interrupt_in_endpoint);
705 	out_end_size = usb_endpoint_maxp(dev->interrupt_out_endpoint);
706 
707 	dev->read_buffer_primary = kmalloc((4 * in_end_size), GFP_KERNEL);
708 	if (!dev->read_buffer_primary) {
709 		retval = -ENOMEM;
710 		goto error;
711 	}
712 
713 	/* debug code prime the buffer */
714 	memset(dev->read_buffer_primary, 'a', in_end_size);
715 	memset(dev->read_buffer_primary + in_end_size, 'b', in_end_size);
716 	memset(dev->read_buffer_primary + (2 * in_end_size), 'c', in_end_size);
717 	memset(dev->read_buffer_primary + (3 * in_end_size), 'd', in_end_size);
718 
719 	dev->read_buffer_secondary = kmalloc((4 * in_end_size), GFP_KERNEL);
720 	if (!dev->read_buffer_secondary) {
721 		retval = -ENOMEM;
722 		goto error;
723 	}
724 
725 	/* debug code prime the buffer */
726 	memset(dev->read_buffer_secondary, 'e', in_end_size);
727 	memset(dev->read_buffer_secondary + in_end_size, 'f', in_end_size);
728 	memset(dev->read_buffer_secondary + (2 * in_end_size), 'g', in_end_size);
729 	memset(dev->read_buffer_secondary + (3 * in_end_size), 'h', in_end_size);
730 
731 	dev->interrupt_in_buffer = kmalloc(in_end_size, GFP_KERNEL);
732 	if (!dev->interrupt_in_buffer)
733 		goto error;
734 
735 	/* debug code prime the buffer */
736 	memset(dev->interrupt_in_buffer, 'i', in_end_size);
737 
738 	dev->interrupt_in_urb = usb_alloc_urb(0, GFP_KERNEL);
739 	if (!dev->interrupt_in_urb)
740 		goto error;
741 	dev->interrupt_out_buffer = kmalloc(out_end_size, GFP_KERNEL);
742 	if (!dev->interrupt_out_buffer)
743 		goto error;
744 	dev->interrupt_out_urb = usb_alloc_urb(0, GFP_KERNEL);
745 	if (!dev->interrupt_out_urb)
746 		goto error;
747 
748 	if (!usb_string(udev, udev->descriptor.iSerialNumber, dev->serial_number,
749 			sizeof(dev->serial_number))) {
750 		dev_err(&interface->dev, "Could not retrieve serial number\n");
751 		goto error;
752 	}
753 	dev_dbg(&interface->dev,"serial_number=%s", dev->serial_number);
754 
755 	/* we can register the device now, as it is ready */
756 	usb_set_intfdata(interface, dev);
757 
758 	retval = usb_register_dev(interface, &adu_class);
759 
760 	if (retval) {
761 		/* something prevented us from registering this driver */
762 		dev_err(&interface->dev, "Not able to get a minor for this device.\n");
763 		usb_set_intfdata(interface, NULL);
764 		goto error;
765 	}
766 
767 	dev->minor = interface->minor;
768 
769 	/* let the user know what node this device is now attached to */
770 	dev_info(&interface->dev, "ADU%d %s now attached to /dev/usb/adutux%d\n",
771 		 le16_to_cpu(udev->descriptor.idProduct), dev->serial_number,
772 		 (dev->minor - ADU_MINOR_BASE));
773 exit:
774 	return retval;
775 
776 error:
777 	adu_delete(dev);
778 	return retval;
779 }
780 
781 /**
782  * adu_disconnect
783  *
784  * Called by the usb core when the device is removed from the system.
785  */
786 static void adu_disconnect(struct usb_interface *interface)
787 {
788 	struct adu_device *dev;
789 	int minor;
790 
791 	dev = usb_get_intfdata(interface);
792 
793 	mutex_lock(&dev->mtx);	/* not interruptible */
794 	dev->udev = NULL;	/* poison */
795 	minor = dev->minor;
796 	usb_deregister_dev(interface, &adu_class);
797 	mutex_unlock(&dev->mtx);
798 
799 	mutex_lock(&adutux_mutex);
800 	usb_set_intfdata(interface, NULL);
801 
802 	/* if the device is not opened, then we clean up right now */
803 	if (!dev->open_count)
804 		adu_delete(dev);
805 
806 	mutex_unlock(&adutux_mutex);
807 }
808 
809 /* usb specific object needed to register this driver with the usb subsystem */
810 static struct usb_driver adu_driver = {
811 	.name = "adutux",
812 	.probe = adu_probe,
813 	.disconnect = adu_disconnect,
814 	.id_table = device_table,
815 };
816 
817 module_usb_driver(adu_driver);
818 
819 MODULE_AUTHOR(DRIVER_AUTHOR);
820 MODULE_DESCRIPTION(DRIVER_DESC);
821 MODULE_LICENSE("GPL");
822