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