adutux.c 24.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
/*
 * adutux - driver for ADU devices from Ontrak Control Systems
 * This is an experimental driver. Use at your own risk.
 * This driver is not supported by Ontrak Control Systems.
 *
 * Copyright (c) 2003 John Homppi (SCO, leave this notice here)
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * derived from the Lego USB Tower driver 0.56:
 * Copyright (c) 2003 David Glance <davidgsf@sourceforge.net>
 *               2001 Juergen Stuber <stuber@loria.fr>
 * that was derived from USB Skeleton driver - 0.5
 * Copyright (c) 2001 Greg Kroah-Hartman (greg@kroah.com)
 *
 */

#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/module.h>
#include <linux/usb.h>
27
#include <linux/mutex.h>
28 29 30 31 32 33 34 35 36 37 38 39 40
#include <asm/uaccess.h>

#ifdef CONFIG_USB_DEBUG
static int debug = 5;
#else
static int debug = 1;
#endif

/* Use our own dbg macro */
#undef dbg
#define dbg(lvl, format, arg...) 					\
do { 									\
	if (debug >= lvl)						\
41
		printk(KERN_DEBUG "%s: " format "\n", __FILE__, ##arg);	\
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
} while (0)


/* Version Information */
#define DRIVER_VERSION "v0.0.13"
#define DRIVER_AUTHOR "John Homppi"
#define DRIVER_DESC "adutux (see www.ontrak.net)"

/* Module parameters */
module_param(debug, int, S_IRUGO | S_IWUSR);
MODULE_PARM_DESC(debug, "Debug enabled or not");

/* Define these values to match your device */
#define ADU_VENDOR_ID 0x0a07
#define ADU_PRODUCT_ID 0x0064

/* table of devices that work with this driver */
59
static const struct usb_device_id device_table[] = {
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
	{ USB_DEVICE(ADU_VENDOR_ID, ADU_PRODUCT_ID) },		/* ADU100 */
	{ USB_DEVICE(ADU_VENDOR_ID, ADU_PRODUCT_ID+20) }, 	/* ADU120 */
	{ USB_DEVICE(ADU_VENDOR_ID, ADU_PRODUCT_ID+30) }, 	/* ADU130 */
	{ USB_DEVICE(ADU_VENDOR_ID, ADU_PRODUCT_ID+100) },	/* ADU200 */
	{ USB_DEVICE(ADU_VENDOR_ID, ADU_PRODUCT_ID+108) },	/* ADU208 */
	{ USB_DEVICE(ADU_VENDOR_ID, ADU_PRODUCT_ID+118) },	/* ADU218 */
	{ }/* Terminating entry */
};

MODULE_DEVICE_TABLE(usb, device_table);

#ifdef CONFIG_USB_DYNAMIC_MINORS
#define ADU_MINOR_BASE	0
#else
#define ADU_MINOR_BASE	67
#endif

/* we can have up to this number of device plugged in at once */
#define MAX_DEVICES	16

#define COMMAND_TIMEOUT	(2*HZ)	/* 60 second timeout for a command */

82 83 84 85 86 87 88 89 90 91
/*
 * The locking scheme is a vanilla 3-lock:
 *   adu_device.buflock: A spinlock, covers what IRQs touch.
 *   adutux_mutex:       A Static lock to cover open_count. It would also cover
 *                       any globals, but we don't have them in 2.6.
 *   adu_device.mtx:     A mutex to hold across sleepers like copy_from_user.
 *                       It covers all of adu_device, except the open_count
 *                       and what .buflock covers.
 */

92 93
/* Structure to hold all of our device specific stuff */
struct adu_device {
94
	struct mutex		mtx;
95 96
	struct usb_device*	udev; /* save off the usb device pointer */
	struct usb_interface*	interface;
97
	unsigned int		minor; /* the starting minor number for this device */
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
	char			serial_number[8];

	int			open_count; /* number of times this port has been opened */

	char*			read_buffer_primary;
	int			read_buffer_length;
	char*			read_buffer_secondary;
	int			secondary_head;
	int			secondary_tail;
	spinlock_t		buflock;

	wait_queue_head_t	read_wait;
	wait_queue_head_t	write_wait;

	char*			interrupt_in_buffer;
	struct usb_endpoint_descriptor* interrupt_in_endpoint;
	struct urb*		interrupt_in_urb;
	int			read_urb_finished;

	char*			interrupt_out_buffer;
	struct usb_endpoint_descriptor* interrupt_out_endpoint;
	struct urb*		interrupt_out_urb;
120
	int			out_urb_finished;
121 122
};

123 124
static DEFINE_MUTEX(adutux_mutex);

125 126 127 128 129 130 131 132 133 134
static struct usb_driver adu_driver;

static void adu_debug_data(int level, const char *function, int size,
			   const unsigned char *data)
{
	int i;

	if (debug < level)
		return;

135 136
	printk(KERN_DEBUG "%s: %s - length = %d, data = ",
	       __FILE__, function, size);
137 138 139 140 141 142 143 144 145 146 147
	for (i = 0; i < size; ++i)
		printk("%.2x ", data[i]);
	printk("\n");
}

/**
 * adu_abort_transfers
 *      aborts transfers and frees associated data structures
 */
static void adu_abort_transfers(struct adu_device *dev)
{
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 155 156 157
		goto exit;
	}

	/* shutdown transfer */
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172

	/* XXX Anchor these instead */
	spin_lock_irqsave(&dev->buflock, flags);
	if (!dev->read_urb_finished) {
		spin_unlock_irqrestore(&dev->buflock, flags);
		usb_kill_urb(dev->interrupt_in_urb);
	} else
		spin_unlock_irqrestore(&dev->buflock, flags);

	spin_lock_irqsave(&dev->buflock, flags);
	if (!dev->out_urb_finished) {
		spin_unlock_irqrestore(&dev->buflock, flags);
		usb_kill_urb(dev->interrupt_out_urb);
	} else
		spin_unlock_irqrestore(&dev->buflock, flags);
173 174

exit:
175
	dbg(2," %s : leave", __func__);
176 177 178 179
}

static void adu_delete(struct adu_device *dev)
{
180
	dbg(2, "%s enter", __func__);
181 182 183 184 185 186 187 188 189 190

	/* free data structures */
	usb_free_urb(dev->interrupt_in_urb);
	usb_free_urb(dev->interrupt_out_urb);
	kfree(dev->read_buffer_primary);
	kfree(dev->read_buffer_secondary);
	kfree(dev->interrupt_in_buffer);
	kfree(dev->interrupt_out_buffer);
	kfree(dev);

191
	dbg(2, "%s : leave", __func__);
192 193
}

194
static void adu_interrupt_in_callback(struct urb *urb)
195 196
{
	struct adu_device *dev = urb->context;
197
	int status = urb->status;
198

199 200
	dbg(4," %s : enter, status %d", __func__, status);
	adu_debug_data(5, __func__, urb->actual_length,
201 202 203 204
		       urb->transfer_buffer);

	spin_lock(&dev->buflock);

205
	if (status != 0) {
206 207
		if ((status != -ENOENT) && (status != -ECONNRESET) &&
			(status != -ESHUTDOWN)) {
208
			dbg(1," %s : nonzero status received: %d",
209
			    __func__, status);
210 211 212 213 214 215 216 217 218 219 220 221 222
		}
		goto exit;
	}

	if (urb->actual_length > 0 && dev->interrupt_in_buffer[0] != 0x00) {
		if (dev->read_buffer_length <
		    (4 * le16_to_cpu(dev->interrupt_in_endpoint->wMaxPacketSize)) -
		     (urb->actual_length)) {
			memcpy (dev->read_buffer_primary +
				dev->read_buffer_length,
				dev->interrupt_in_buffer, urb->actual_length);

			dev->read_buffer_length += urb->actual_length;
223
			dbg(2," %s reading  %d ", __func__,
224 225
			    urb->actual_length);
		} else {
226
			dbg(1," %s : read_buffer overflow", __func__);
227 228 229 230 231 232 233 234
		}
	}

exit:
	dev->read_urb_finished = 1;
	spin_unlock(&dev->buflock);
	/* always wake up so we recover from errors */
	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
}

240
static void adu_interrupt_out_callback(struct urb *urb)
241 242
{
	struct adu_device *dev = urb->context;
243
	int status = urb->status;
244

245 246
	dbg(4," %s : enter, status %d", __func__, status);
	adu_debug_data(5,__func__, urb->actual_length, urb->transfer_buffer);
247

248 249 250
	if (status != 0) {
		if ((status != -ENOENT) &&
		    (status != -ECONNRESET)) {
251
			dbg(1, " %s :nonzero status received: %d",
252
			    __func__, status);
253 254 255 256
		}
		goto exit;
	}

257 258 259 260
	spin_lock(&dev->buflock);
	dev->out_urb_finished = 1;
	wake_up(&dev->write_wait);
	spin_unlock(&dev->buflock);
261 262
exit:

263
	adu_debug_data(5, __func__, urb->actual_length,
264
		       urb->transfer_buffer);
265
	dbg(4," %s : leave, status %d", __func__, status);
266 267 268 269 270 271 272
}

static int adu_open(struct inode *inode, struct file *file)
{
	struct adu_device *dev = NULL;
	struct usb_interface *interface;
	int subminor;
273
	int retval;
274

275
	dbg(2,"%s : enter", __func__);
276 277 278

	subminor = iminor(inode);

279
	if ((retval = mutex_lock_interruptible(&adutux_mutex))) {
280
		dbg(2, "%s : mutex lock failed", __func__);
281 282 283
		goto exit_no_lock;
	}

284 285
	interface = usb_find_interface(&adu_driver, subminor);
	if (!interface) {
286 287
		printk(KERN_ERR "adutux: %s - error, can't find device for "
		       "minor %d\n", __func__, subminor);
288 289 290 291 292
		retval = -ENODEV;
		goto exit_no_device;
	}

	dev = usb_get_intfdata(interface);
293
	if (!dev || !dev->udev) {
294 295 296 297
		retval = -ENODEV;
		goto exit_no_device;
	}

298 299 300
	/* check that nobody else is using the device */
	if (dev->open_count) {
		retval = -EBUSY;
301 302 303 304
		goto exit_no_device;
	}

	++dev->open_count;
305
	dbg(2,"%s : open count %d", __func__, dev->open_count);
306 307 308 309

	/* save device in the file's private structure */
	file->private_data = dev;

310 311
	/* initialize in direction */
	dev->read_buffer_length = 0;
312

313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
	/* fixup first read by having urb waiting for it */
	usb_fill_int_urb(dev->interrupt_in_urb,dev->udev,
			 usb_rcvintpipe(dev->udev,
					dev->interrupt_in_endpoint->bEndpointAddress),
			 dev->interrupt_in_buffer,
			 le16_to_cpu(dev->interrupt_in_endpoint->wMaxPacketSize),
			 adu_interrupt_in_callback, dev,
			 dev->interrupt_in_endpoint->bInterval);
	dev->read_urb_finished = 0;
	if (usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL))
		dev->read_urb_finished = 1;
	/* we ignore failure */
	/* end of fixup for first read */

	/* initialize out direction */
	dev->out_urb_finished = 1;

	retval = 0;
331 332

exit_no_device:
333 334
	mutex_unlock(&adutux_mutex);
exit_no_lock:
335
	dbg(2,"%s : leave, return value %d ", __func__, retval);
336 337 338
	return retval;
}

339
static void adu_release_internal(struct adu_device *dev)
340
{
341
	dbg(2," %s : enter", __func__);
342 343 344

	/* decrement our usage count for the device */
	--dev->open_count;
345
	dbg(2," %s : open count %d", __func__, dev->open_count);
346 347 348 349 350
	if (dev->open_count <= 0) {
		adu_abort_transfers(dev);
		dev->open_count = 0;
	}

351
	dbg(2," %s : leave", __func__);
352 353 354 355
}

static int adu_release(struct inode *inode, struct file *file)
{
356
	struct adu_device *dev;
357 358
	int retval = 0;

359
	dbg(2," %s : enter", __func__);
360 361

	if (file == NULL) {
362
 		dbg(1," %s : file is NULL", __func__);
363 364 365 366 367 368
		retval = -ENODEV;
		goto exit;
	}

	dev = file->private_data;
	if (dev == NULL) {
369
 		dbg(1," %s : object is NULL", __func__);
370 371 372 373
		retval = -ENODEV;
		goto exit;
	}

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 384
	if (dev->udev == NULL) {
		/* the device was unplugged before the file was released */
385 386
		if (!dev->open_count)	/* ... and we're the last user */
			adu_delete(dev);
387
	}
388
unlock:
389
	mutex_unlock(&adutux_mutex);
390
exit:
391
	dbg(2," %s : leave, return value %d", __func__, retval);
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
	return retval;
}

static ssize_t adu_read(struct file *file, __user char *buffer, size_t count,
			loff_t *ppos)
{
	struct adu_device *dev;
	size_t bytes_read = 0;
	size_t bytes_to_read = count;
	int i;
	int retval = 0;
	int timeout = 0;
	int should_submit = 0;
	unsigned long flags;
	DECLARE_WAITQUEUE(wait, current);

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 415 416
		return -ERESTARTSYS;

	/* verify that the device wasn't unplugged */
417
	if (dev->udev == NULL) {
418
		retval = -ENODEV;
419 420
		printk(KERN_ERR "adutux: No device or device unplugged %d\n",
		       retval);
421 422 423 424 425
		goto exit;
	}

	/* verify that some data was requested */
	if (count == 0) {
426
		dbg(1," %s : read request of 0 bytes", __func__);
427 428 429 430
		goto exit;
	}

	timeout = COMMAND_TIMEOUT;
431
	dbg(2," %s : about to start looping", __func__);
432 433 434
	while (bytes_to_read) {
		int data_in_secondary = dev->secondary_tail - dev->secondary_head;
		dbg(2," %s : while, data_in_secondary=%d, status=%d",
435
		    __func__, data_in_secondary,
436 437 438 439 440 441
		    dev->interrupt_in_urb->status);

		if (data_in_secondary) {
			/* drain secondary buffer */
			int amount = bytes_to_read < data_in_secondary ? bytes_to_read : data_in_secondary;
			i = copy_to_user(buffer, dev->read_buffer_secondary+dev->secondary_head, amount);
442
			if (i) {
443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
				retval = -EFAULT;
				goto exit;
			}
			dev->secondary_head += (amount - i);
			bytes_read += (amount - i);
			bytes_to_read -= (amount - i);
			if (i) {
				retval = bytes_read ? bytes_read : -EFAULT;
				goto exit;
			}
		} else {
			/* we check the primary buffer */
			spin_lock_irqsave (&dev->buflock, flags);
			if (dev->read_buffer_length) {
				/* we secure access to the primary */
				char *tmp;
				dbg(2," %s : swap, read_buffer_length = %d",
460
				    __func__, dev->read_buffer_length);
461 462 463 464 465 466 467 468 469 470 471
				tmp = dev->read_buffer_secondary;
				dev->read_buffer_secondary = dev->read_buffer_primary;
				dev->read_buffer_primary = tmp;
				dev->secondary_head = 0;
				dev->secondary_tail = dev->read_buffer_length;
				dev->read_buffer_length = 0;
				spin_unlock_irqrestore(&dev->buflock, flags);
				/* we have a free buffer so use it */
				should_submit = 1;
			} else {
				/* even the primary was empty - we may need to do IO */
472
				if (!dev->read_urb_finished) {
473 474
					/* somebody is doing IO */
					spin_unlock_irqrestore(&dev->buflock, flags);
475
					dbg(2," %s : submitted already", __func__);
476 477
				} else {
					/* 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 483 484 485 486 487 488 489

					usb_fill_int_urb(dev->interrupt_in_urb,dev->udev,
							 usb_rcvintpipe(dev->udev,
							 		dev->interrupt_in_endpoint->bEndpointAddress),
							 dev->interrupt_in_buffer,
							 le16_to_cpu(dev->interrupt_in_endpoint->wMaxPacketSize),
							 adu_interrupt_in_callback,
							 dev,
							 dev->interrupt_in_endpoint->bInterval);
490 491 492
					retval = usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL);
					if (retval) {
						dev->read_urb_finished = 1;
493 494 495
						if (retval == -ENOMEM) {
							retval = bytes_read ? bytes_read : -ENOMEM;
						}
496
						dbg(2," %s : submit failed", __func__);
497 498 499 500 501 502 503
						goto exit;
					}
				}

				/* we wait for I/O to complete */
				set_current_state(TASK_INTERRUPTIBLE);
				add_wait_queue(&dev->read_wait, &wait);
504 505 506
				spin_lock_irqsave(&dev->buflock, flags);
				if (!dev->read_urb_finished) {
					spin_unlock_irqrestore(&dev->buflock, flags);
507
					timeout = schedule_timeout(COMMAND_TIMEOUT);
508 509
				} else {
					spin_unlock_irqrestore(&dev->buflock, flags);
510
					set_current_state(TASK_RUNNING);
511
				}
512 513 514
				remove_wait_queue(&dev->read_wait, &wait);

				if (timeout <= 0) {
515
					dbg(2," %s : timeout", __func__);
516 517 518 519 520
					retval = bytes_read ? bytes_read : -ETIMEDOUT;
					goto exit;
				}

				if (signal_pending(current)) {
521
					dbg(2," %s : signal pending", __func__);
522 523 524 525 526 527 528 529 530
					retval = bytes_read ? bytes_read : -EINTR;
					goto exit;
				}
			}
		}
	}

	retval = bytes_read;
	/* if the primary buffer is empty then use it */
531 532 533 534
	spin_lock_irqsave(&dev->buflock, flags);
	if (should_submit && dev->read_urb_finished) {
		dev->read_urb_finished = 0;
		spin_unlock_irqrestore(&dev->buflock, flags);
535 536 537
		usb_fill_int_urb(dev->interrupt_in_urb,dev->udev,
				 usb_rcvintpipe(dev->udev,
				 		dev->interrupt_in_endpoint->bEndpointAddress),
538 539 540 541 542 543 544
				dev->interrupt_in_buffer,
				le16_to_cpu(dev->interrupt_in_endpoint->wMaxPacketSize),
				adu_interrupt_in_callback,
				dev,
				dev->interrupt_in_endpoint->bInterval);
		if (usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL) != 0)
			dev->read_urb_finished = 1;
545
		/* we ignore failure */
546 547
	} else {
		spin_unlock_irqrestore(&dev->buflock, flags);
548 549 550 551
	}

exit:
	/* unlock the device */
552
	mutex_unlock(&dev->mtx);
553

554
	dbg(2," %s : leave, return value %d", __func__, retval);
555 556 557 558 559 560
	return retval;
}

static ssize_t adu_write(struct file *file, const __user char *buffer,
			 size_t count, loff_t *ppos)
{
561
	DECLARE_WAITQUEUE(waita, current);
562 563 564 565
	struct adu_device *dev;
	size_t bytes_written = 0;
	size_t bytes_to_write;
	size_t buffer_size;
566
	unsigned long flags;
O
Oliver Neukum 已提交
567
	int retval;
568

569
	dbg(2," %s : enter, count = %Zd", __func__, count);
570 571 572

	dev = file->private_data;

573
	retval = mutex_lock_interruptible(&dev->mtx);
O
Oliver Neukum 已提交
574 575
	if (retval)
		goto exit_nolock;
576 577

	/* verify that the device wasn't unplugged */
578
	if (dev->udev == NULL) {
579
		retval = -ENODEV;
580 581
		printk(KERN_ERR "adutux: No device or device unplugged %d\n",
		       retval);
582 583 584 585 586
		goto exit;
	}

	/* verify that we actually have some data to write */
	if (count == 0) {
587
		dbg(1," %s : write request of 0 bytes", __func__);
588 589 590 591
		goto exit;
	}

	while (count > 0) {
592 593 594 595 596
		add_wait_queue(&dev->write_wait, &waita);
		set_current_state(TASK_INTERRUPTIBLE);
		spin_lock_irqsave(&dev->buflock, flags);
		if (!dev->out_urb_finished) {
			spin_unlock_irqrestore(&dev->buflock, flags);
597

598 599
			mutex_unlock(&dev->mtx);
			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 608 609 610
				retval = -ETIMEDOUT;
				goto exit_onqueue;
			}
			remove_wait_queue(&dev->write_wait, &waita);
611
			retval = mutex_lock_interruptible(&dev->mtx);
O
Oliver Neukum 已提交
612 613 614 615
			if (retval) {
				retval = bytes_written ? bytes_written : retval;
				goto exit_nolock;
			}
616

617
			dbg(4," %s : in progress, count = %Zd", __func__, count);
618
		} else {
619 620 621
			spin_unlock_irqrestore(&dev->buflock, flags);
			set_current_state(TASK_RUNNING);
			remove_wait_queue(&dev->write_wait, &waita);
622
			dbg(4," %s : sending, count = %Zd", __func__, count);
623 624 625 626 627

			/* write the data into interrupt_out_buffer from userspace */
			buffer_size = le16_to_cpu(dev->interrupt_out_endpoint->wMaxPacketSize);
			bytes_to_write = count > buffer_size ? buffer_size : count;
			dbg(4," %s : buffer_size = %Zd, count = %Zd, bytes_to_write = %Zd",
628
			    __func__, buffer_size, count, bytes_to_write);
629 630 631 632 633 634 635 636 637 638 639 640 641 642 643

			if (copy_from_user(dev->interrupt_out_buffer, buffer, bytes_to_write) != 0) {
				retval = -EFAULT;
				goto exit;
			}

			/* send off the urb */
			usb_fill_int_urb(
				dev->interrupt_out_urb,
				dev->udev,
				usb_sndintpipe(dev->udev, dev->interrupt_out_endpoint->bEndpointAddress),
				dev->interrupt_out_buffer,
				bytes_to_write,
				adu_interrupt_out_callback,
				dev,
644
				dev->interrupt_out_endpoint->bInterval);
645
			dev->interrupt_out_urb->actual_length = bytes_to_write;
646
			dev->out_urb_finished = 0;
647 648
			retval = usb_submit_urb(dev->interrupt_out_urb, GFP_KERNEL);
			if (retval < 0) {
649
				dev->out_urb_finished = 1;
650 651
				dev_err(&dev->udev->dev, "Couldn't submit "
					"interrupt_out_urb %d\n", retval);
652 653 654 655 656 657 658 659 660
				goto exit;
			}

			buffer += bytes_to_write;
			count -= bytes_to_write;

			bytes_written += bytes_to_write;
		}
	}
661 662
	mutex_unlock(&dev->mtx);
	return bytes_written;
663 664

exit:
665
	mutex_unlock(&dev->mtx);
O
Oliver Neukum 已提交
666
exit_nolock:
667
	dbg(2," %s : leave, return value %d", __func__, retval);
668
	return retval;
669

670 671
exit_onqueue:
	remove_wait_queue(&dev->write_wait, &waita);
672 673 674 675
	return retval;
}

/* file operations needed when we register this driver */
676
static const struct file_operations adu_fops = {
677 678 679 680 681
	.owner = THIS_MODULE,
	.read  = adu_read,
	.write = adu_write,
	.open = adu_open,
	.release = adu_release,
682
	.llseek = noop_llseek,
683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712
};

/*
 * usb class driver info in order to get a minor number from the usb core,
 * and to have the device registered with devfs and the driver core
 */
static struct usb_class_driver adu_class = {
	.name = "usb/adutux%d",
	.fops = &adu_fops,
	.minor_base = ADU_MINOR_BASE,
};

/**
 * adu_probe
 *
 * Called by the usb core when a new device is connected that it thinks
 * this driver might be interested in.
 */
static int adu_probe(struct usb_interface *interface,
		     const struct usb_device_id *id)
{
	struct usb_device *udev = interface_to_usbdev(interface);
	struct adu_device *dev = NULL;
	struct usb_host_interface *iface_desc;
	struct usb_endpoint_descriptor *endpoint;
	int retval = -ENODEV;
	int in_end_size;
	int out_end_size;
	int i;

713
	dbg(2," %s : enter", __func__);
714 715 716 717 718 719 720 721 722 723 724 725 726 727

	if (udev == NULL) {
		dev_err(&interface->dev, "udev is NULL.\n");
		goto exit;
	}

	/* allocate memory for our device state and intialize it */
	dev = kzalloc(sizeof(struct adu_device), GFP_KERNEL);
	if (dev == NULL) {
		dev_err(&interface->dev, "Out of memory\n");
		retval = -ENOMEM;
		goto exit;
	}

728
	mutex_init(&dev->mtx);
729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813
	spin_lock_init(&dev->buflock);
	dev->udev = udev;
	init_waitqueue_head(&dev->read_wait);
	init_waitqueue_head(&dev->write_wait);

	iface_desc = &interface->altsetting[0];

	/* set up the endpoint information */
	for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
		endpoint = &iface_desc->endpoint[i].desc;

		if (usb_endpoint_is_int_in(endpoint))
			dev->interrupt_in_endpoint = endpoint;

		if (usb_endpoint_is_int_out(endpoint))
			dev->interrupt_out_endpoint = endpoint;
	}
	if (dev->interrupt_in_endpoint == NULL) {
		dev_err(&interface->dev, "interrupt in endpoint not found\n");
		goto error;
	}
	if (dev->interrupt_out_endpoint == NULL) {
		dev_err(&interface->dev, "interrupt out endpoint not found\n");
		goto error;
	}

	in_end_size = le16_to_cpu(dev->interrupt_in_endpoint->wMaxPacketSize);
	out_end_size = le16_to_cpu(dev->interrupt_out_endpoint->wMaxPacketSize);

	dev->read_buffer_primary = kmalloc((4 * in_end_size), GFP_KERNEL);
	if (!dev->read_buffer_primary) {
		dev_err(&interface->dev, "Couldn't allocate read_buffer_primary\n");
		retval = -ENOMEM;
		goto error;
	}

	/* debug code prime the buffer */
	memset(dev->read_buffer_primary, 'a', in_end_size);
	memset(dev->read_buffer_primary + in_end_size, 'b', in_end_size);
	memset(dev->read_buffer_primary + (2 * in_end_size), 'c', in_end_size);
	memset(dev->read_buffer_primary + (3 * in_end_size), 'd', in_end_size);

	dev->read_buffer_secondary = kmalloc((4 * in_end_size), GFP_KERNEL);
	if (!dev->read_buffer_secondary) {
		dev_err(&interface->dev, "Couldn't allocate read_buffer_secondary\n");
		retval = -ENOMEM;
		goto error;
	}

	/* debug code prime the buffer */
	memset(dev->read_buffer_secondary, 'e', in_end_size);
	memset(dev->read_buffer_secondary + in_end_size, 'f', in_end_size);
	memset(dev->read_buffer_secondary + (2 * in_end_size), 'g', in_end_size);
	memset(dev->read_buffer_secondary + (3 * in_end_size), 'h', in_end_size);

	dev->interrupt_in_buffer = kmalloc(in_end_size, GFP_KERNEL);
	if (!dev->interrupt_in_buffer) {
		dev_err(&interface->dev, "Couldn't allocate interrupt_in_buffer\n");
		goto error;
	}

	/* debug code prime the buffer */
	memset(dev->interrupt_in_buffer, 'i', in_end_size);

	dev->interrupt_in_urb = usb_alloc_urb(0, GFP_KERNEL);
	if (!dev->interrupt_in_urb) {
		dev_err(&interface->dev, "Couldn't allocate interrupt_in_urb\n");
		goto error;
	}
	dev->interrupt_out_buffer = kmalloc(out_end_size, GFP_KERNEL);
	if (!dev->interrupt_out_buffer) {
		dev_err(&interface->dev, "Couldn't allocate interrupt_out_buffer\n");
		goto error;
	}
	dev->interrupt_out_urb = usb_alloc_urb(0, GFP_KERNEL);
	if (!dev->interrupt_out_urb) {
		dev_err(&interface->dev, "Couldn't allocate interrupt_out_urb\n");
		goto error;
	}

	if (!usb_string(udev, udev->descriptor.iSerialNumber, dev->serial_number,
			sizeof(dev->serial_number))) {
		dev_err(&interface->dev, "Could not retrieve serial number\n");
		goto error;
	}
814
	dbg(2," %s : serial_number=%s", __func__, dev->serial_number);
815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830

	/* we can register the device now, as it is ready */
	usb_set_intfdata(interface, dev);

	retval = usb_register_dev(interface, &adu_class);

	if (retval) {
		/* something prevented us from registering this driver */
		dev_err(&interface->dev, "Not able to get a minor for this device.\n");
		usb_set_intfdata(interface, NULL);
		goto error;
	}

	dev->minor = interface->minor;

	/* 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 833 834
		 udev->descriptor.idProduct, dev->serial_number,
		 (dev->minor - ADU_MINOR_BASE));
exit:
835
	dbg(2," %s : leave, return value %p (dev)", __func__, dev);
836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853

	return retval;

error:
	adu_delete(dev);
	return retval;
}

/**
 * adu_disconnect
 *
 * Called by the usb core when the device is removed from the system.
 */
static void adu_disconnect(struct usb_interface *interface)
{
	struct adu_device *dev;
	int minor;

854
	dbg(2," %s : enter", __func__);
855 856 857

	dev = usb_get_intfdata(interface);

858 859
	mutex_lock(&dev->mtx);	/* not interruptible */
	dev->udev = NULL;	/* poison */
860 861
	minor = dev->minor;
	usb_deregister_dev(interface, &adu_class);
862
	mutex_unlock(&dev->mtx);
863

864 865
	mutex_lock(&adutux_mutex);
	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 876
		 (minor - ADU_MINOR_BASE));

877
	dbg(2," %s : leave", __func__);
878 879 880 881 882 883 884 885 886 887 888 889 890 891
}

/* usb specific object needed to register this driver with the usb subsystem */
static struct usb_driver adu_driver = {
	.name = "adutux",
	.probe = adu_probe,
	.disconnect = adu_disconnect,
	.id_table = device_table,
};

static int __init adu_init(void)
{
	int result;

892
	dbg(2," %s : enter", __func__);
893 894 895 896

	/* register this driver with the USB subsystem */
	result = usb_register(&adu_driver);
	if (result < 0) {
897 898
		printk(KERN_ERR "usb_register failed for the "__FILE__
		       " driver. Error number %d\n", result);
899 900 901
		goto exit;
	}

902 903 904
	printk(KERN_INFO "adutux " DRIVER_DESC " " DRIVER_VERSION "\n");
	printk(KERN_INFO "adutux is an experimental driver. "
	       "Use at your own risk\n");
905 906

exit:
907
	dbg(2," %s : leave, return value %d", __func__, result);
908 909 910 911 912 913

	return result;
}

static void __exit adu_exit(void)
{
914
	dbg(2," %s : enter", __func__);
915 916
	/* deregister this driver with the USB subsystem */
	usb_deregister(&adu_driver);
917
	dbg(2," %s : leave", __func__);
918 919 920 921 922 923 924 925
}

module_init(adu_init);
module_exit(adu_exit);

MODULE_AUTHOR(DRIVER_AUTHOR);
MODULE_DESCRIPTION(DRIVER_DESC);
MODULE_LICENSE("GPL");