Linux Audio

Check our new training course

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