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