adutux.c 21.4 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0+
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * 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)
 *
 * 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)
 *
 */

17 18
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

19
#include <linux/kernel.h>
20
#include <linux/sched/signal.h>
21 22 23 24
#include <linux/errno.h>
#include <linux/slab.h>
#include <linux/module.h>
#include <linux/usb.h>
25
#include <linux/mutex.h>
26
#include <linux/uaccess.h>
27 28 29 30 31 32 33 34 35

#define DRIVER_AUTHOR "John Homppi"
#define DRIVER_DESC "adutux (see www.ontrak.net)"

/* 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 */
36
static const struct usb_device_id device_table[] = {
37
	{ USB_DEVICE(ADU_VENDOR_ID, ADU_PRODUCT_ID) },		/* ADU100 */
38 39
	{ USB_DEVICE(ADU_VENDOR_ID, ADU_PRODUCT_ID+20) },	/* ADU120 */
	{ USB_DEVICE(ADU_VENDOR_ID, ADU_PRODUCT_ID+30) },	/* ADU130 */
40 41 42
	{ 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 */
43
	{ } /* Terminating entry */
44 45 46 47 48 49 50 51 52 53 54 55 56
};

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

57
#define COMMAND_TIMEOUT	(2*HZ)
58

59 60 61 62 63 64 65 66 67 68
/*
 * 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.
 */

69 70
/* Structure to hold all of our device specific stuff */
struct adu_device {
71
	struct mutex		mtx;
72 73
	struct usb_device *udev; /* save off the usb device pointer */
	struct usb_interface *interface;
74
	unsigned int		minor; /* the starting minor number for this device */
75 76 77
	char			serial_number[8];

	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 84 85 86 87 88 89
	int			secondary_head;
	int			secondary_tail;
	spinlock_t		buflock;

	wait_queue_head_t	read_wait;
	wait_queue_head_t	write_wait;

90 91 92
	char		*interrupt_in_buffer;
	struct usb_endpoint_descriptor *interrupt_in_endpoint;
	struct urb	*interrupt_in_urb;
93 94
	int			read_urb_finished;

95 96 97
	char		*interrupt_out_buffer;
	struct usb_endpoint_descriptor *interrupt_out_endpoint;
	struct urb	*interrupt_out_urb;
98
	int			out_urb_finished;
99 100
};

101 102
static DEFINE_MUTEX(adutux_mutex);

103 104
static struct usb_driver adu_driver;

105 106
static inline void adu_debug_data(struct device *dev, const char *function,
				  int size, const unsigned char *data)
107
{
108 109
	dev_dbg(dev, "%s - length = %d, data = %*ph\n",
		function, size, size, data);
110 111 112 113 114 115 116 117
}

/**
 * adu_abort_transfers
 *      aborts transfers and frees associated data structures
 */
static void adu_abort_transfers(struct adu_device *dev)
{
118
	unsigned long flags;
119

120
	if (dev->disconnected)
121
		return;
122 123

	/* shutdown transfer */
124 125 126 127 128 129 130 131 132 133 134 135

	/* 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);
136 137
		wait_event_timeout(dev->write_wait, dev->out_urb_finished,
			COMMAND_TIMEOUT);
138 139 140
		usb_kill_urb(dev->interrupt_out_urb);
	} else
		spin_unlock_irqrestore(&dev->buflock, flags);
141 142 143 144 145 146 147 148 149 150 151
}

static void adu_delete(struct adu_device *dev)
{
	/* 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);
152
	usb_put_dev(dev->udev);
153 154 155
	kfree(dev);
}

156
static 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 163
	adu_debug_data(&dev->udev->dev, __func__,
		       urb->actual_length, urb->transfer_buffer);
164

165
	spin_lock_irqsave(&dev->buflock, flags);
166

167
	if (status != 0) {
168 169
		if ((status != -ENOENT) && (status != -ECONNRESET) &&
			(status != -ESHUTDOWN)) {
170 171 172
			dev_dbg(&dev->udev->dev,
				"%s : nonzero status received: %d\n",
				__func__, status);
173 174 175 176 177 178
		}
		goto exit;
	}

	if (urb->actual_length > 0 && dev->interrupt_in_buffer[0] != 0x00) {
		if (dev->read_buffer_length <
179
		    (4 * usb_endpoint_maxp(dev->interrupt_in_endpoint)) -
180 181 182 183 184 185
		     (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;
186 187
			dev_dbg(&dev->udev->dev,"%s reading  %d\n", __func__,
				urb->actual_length);
188
		} else {
189 190
			dev_dbg(&dev->udev->dev,"%s : read_buffer overflow\n",
				__func__);
191 192 193 194 195
		}
	}

exit:
	dev->read_urb_finished = 1;
196
	spin_unlock_irqrestore(&dev->buflock, flags);
197 198 199 200
	/* always wake up so we recover from errors */
	wake_up_interruptible(&dev->read_wait);
}

201
static 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 208
	adu_debug_data(&dev->udev->dev, __func__,
		       urb->actual_length, urb->transfer_buffer);
209

210 211 212
	if (status != 0) {
		if ((status != -ENOENT) &&
		    (status != -ECONNRESET)) {
213 214 215
			dev_dbg(&dev->udev->dev,
				"%s :nonzero status received: %d\n", __func__,
				status);
216
		}
217
		return;
218 219
	}

220
	spin_lock_irqsave(&dev->buflock, flags);
221 222
	dev->out_urb_finished = 1;
	wake_up(&dev->write_wait);
223
	spin_unlock_irqrestore(&dev->buflock, flags);
224 225 226 227 228 229 230
}

static int adu_open(struct inode *inode, struct file *file)
{
	struct adu_device *dev = NULL;
	struct usb_interface *interface;
	int subminor;
231
	int retval;
232 233 234

	subminor = iminor(inode);

235
	retval = mutex_lock_interruptible(&adutux_mutex);
236
	if (retval)
237 238
		goto exit_no_lock;

239 240
	interface = usb_find_interface(&adu_driver, subminor);
	if (!interface) {
241 242
		pr_err("%s - error, can't find device for minor %d\n",
		       __func__, subminor);
243 244 245 246 247
		retval = -ENODEV;
		goto exit_no_device;
	}

	dev = usb_get_intfdata(interface);
248
	if (!dev) {
249 250 251 252
		retval = -ENODEV;
		goto exit_no_device;
	}

253 254 255
	/* check that nobody else is using the device */
	if (dev->open_count) {
		retval = -EBUSY;
256 257 258 259
		goto exit_no_device;
	}

	++dev->open_count;
260 261
	dev_dbg(&dev->udev->dev, "%s: open count %d\n", __func__,
		dev->open_count);
262 263 264 265

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

266 267
	/* initialize in direction */
	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 272 273
			 usb_rcvintpipe(dev->udev,
					dev->interrupt_in_endpoint->bEndpointAddress),
			 dev->interrupt_in_buffer,
274
			 usb_endpoint_maxp(dev->interrupt_in_endpoint),
275 276 277 278 279 280 281 282 283 284 285 286
			 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;
287 288

exit_no_device:
289 290
	mutex_unlock(&adutux_mutex);
exit_no_lock:
291 292 293
	return retval;
}

294
static void adu_release_internal(struct adu_device *dev)
295 296 297
{
	/* decrement our usage count for the device */
	--dev->open_count;
298 299
	dev_dbg(&dev->udev->dev, "%s : open count %d\n", __func__,
		dev->open_count);
300 301 302 303 304 305 306 307
	if (dev->open_count <= 0) {
		adu_abort_transfers(dev);
		dev->open_count = 0;
	}
}

static int adu_release(struct inode *inode, struct file *file)
{
308
	struct adu_device *dev;
309 310 311 312 313 314 315 316 317 318 319 320 321
	int retval = 0;

	if (file == NULL) {
		retval = -ENODEV;
		goto exit;
	}

	dev = file->private_data;
	if (dev == NULL) {
		retval = -ENODEV;
		goto exit;
	}

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 334
		if (!dev->open_count)	/* ... and we're the last user */
			adu_delete(dev);
335
	}
336
unlock:
337
	mutex_unlock(&adutux_mutex);
338
exit:
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
	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);

	dev = file->private_data;
356
	if (mutex_lock_interruptible(&dev->mtx))
357 358 359
		return -ERESTARTSYS;

	/* verify that the device wasn't unplugged */
360
	if (dev->disconnected) {
361
		retval = -ENODEV;
362
		pr_err("No device or device unplugged %d\n", retval);
363 364 365 366 367
		goto exit;
	}

	/* verify that some data was requested */
	if (count == 0) {
368 369
		dev_dbg(&dev->udev->dev, "%s : read request of 0 bytes\n",
			__func__);
370 371 372 373
		goto exit;
	}

	timeout = COMMAND_TIMEOUT;
374
	dev_dbg(&dev->udev->dev, "%s : about to start looping\n", __func__);
375 376
	while (bytes_to_read) {
		int data_in_secondary = dev->secondary_tail - dev->secondary_head;
377 378 379 380
		dev_dbg(&dev->udev->dev,
			"%s : while, data_in_secondary=%d, status=%d\n",
			__func__, data_in_secondary,
			dev->interrupt_in_urb->status);
381 382 383 384 385

		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);
386
			if (i) {
387 388 389 390 391 392 393 394 395 396 397 398
				retval = -EFAULT;
				goto exit;
			}
			dev->secondary_head += (amount - i);
			bytes_read += (amount - i);
			bytes_to_read -= (amount - i);
		} 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;
399 400 401
				dev_dbg(&dev->udev->dev,
					"%s : swap, read_buffer_length = %d\n",
					__func__, dev->read_buffer_length);
402 403 404 405 406 407 408 409 410 411 412
				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 */
413
				if (!dev->read_urb_finished) {
414 415
					/* somebody is doing IO */
					spin_unlock_irqrestore(&dev->buflock, flags);
416 417 418
					dev_dbg(&dev->udev->dev,
						"%s : submitted already\n",
						__func__);
419 420
				} else {
					/* we must initiate input */
421 422 423
					dev_dbg(&dev->udev->dev,
						"%s : initiate input\n",
						__func__);
424
					dev->read_urb_finished = 0;
425
					spin_unlock_irqrestore(&dev->buflock, flags);
426

427
					usb_fill_int_urb(dev->interrupt_in_urb, dev->udev,
428 429
							usb_rcvintpipe(dev->udev,
								dev->interrupt_in_endpoint->bEndpointAddress),
430
							 dev->interrupt_in_buffer,
431
							 usb_endpoint_maxp(dev->interrupt_in_endpoint),
432 433 434
							 adu_interrupt_in_callback,
							 dev,
							 dev->interrupt_in_endpoint->bInterval);
435 436 437
					retval = usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL);
					if (retval) {
						dev->read_urb_finished = 1;
438 439 440
						if (retval == -ENOMEM) {
							retval = bytes_read ? bytes_read : -ENOMEM;
						}
441 442 443
						dev_dbg(&dev->udev->dev,
							"%s : submit failed\n",
							__func__);
444 445 446 447 448 449 450
						goto exit;
					}
				}

				/* we wait for I/O to complete */
				set_current_state(TASK_INTERRUPTIBLE);
				add_wait_queue(&dev->read_wait, &wait);
451 452 453
				spin_lock_irqsave(&dev->buflock, flags);
				if (!dev->read_urb_finished) {
					spin_unlock_irqrestore(&dev->buflock, flags);
454
					timeout = schedule_timeout(COMMAND_TIMEOUT);
455 456
				} else {
					spin_unlock_irqrestore(&dev->buflock, flags);
457
					set_current_state(TASK_RUNNING);
458
				}
459 460 461
				remove_wait_queue(&dev->read_wait, &wait);

				if (timeout <= 0) {
462 463
					dev_dbg(&dev->udev->dev,
						"%s : timeout\n", __func__);
464 465 466 467 468
					retval = bytes_read ? bytes_read : -ETIMEDOUT;
					goto exit;
				}

				if (signal_pending(current)) {
469 470 471
					dev_dbg(&dev->udev->dev,
						"%s : signal pending\n",
						__func__);
472 473 474 475 476 477 478 479 480
					retval = bytes_read ? bytes_read : -EINTR;
					goto exit;
				}
			}
		}
	}

	retval = bytes_read;
	/* if the primary buffer is empty then use it */
481 482 483 484
	spin_lock_irqsave(&dev->buflock, flags);
	if (should_submit && dev->read_urb_finished) {
		dev->read_urb_finished = 0;
		spin_unlock_irqrestore(&dev->buflock, flags);
485
		usb_fill_int_urb(dev->interrupt_in_urb, dev->udev,
486
				 usb_rcvintpipe(dev->udev,
487
					dev->interrupt_in_endpoint->bEndpointAddress),
488
				dev->interrupt_in_buffer,
489
				usb_endpoint_maxp(dev->interrupt_in_endpoint),
490 491 492 493 494
				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;
495
		/* we ignore failure */
496 497
	} else {
		spin_unlock_irqrestore(&dev->buflock, flags);
498 499 500 501
	}

exit:
	/* unlock the device */
502
	mutex_unlock(&dev->mtx);
503 504 505 506 507 508 509

	return retval;
}

static ssize_t adu_write(struct file *file, const __user char *buffer,
			 size_t count, loff_t *ppos)
{
510
	DECLARE_WAITQUEUE(waita, current);
511 512 513 514
	struct adu_device *dev;
	size_t bytes_written = 0;
	size_t bytes_to_write;
	size_t buffer_size;
515
	unsigned long flags;
O
Oliver Neukum 已提交
516
	int retval;
517 518 519

	dev = file->private_data;

520
	retval = mutex_lock_interruptible(&dev->mtx);
O
Oliver Neukum 已提交
521 522
	if (retval)
		goto exit_nolock;
523 524

	/* verify that the device wasn't unplugged */
525
	if (dev->disconnected) {
526
		retval = -ENODEV;
527
		pr_err("No device or device unplugged %d\n", retval);
528 529 530 531 532
		goto exit;
	}

	/* verify that we actually have some data to write */
	if (count == 0) {
533 534
		dev_dbg(&dev->udev->dev, "%s : write request of 0 bytes\n",
			__func__);
535 536 537 538
		goto exit;
	}

	while (count > 0) {
539 540 541 542 543
		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);
544

545 546
			mutex_unlock(&dev->mtx);
			if (signal_pending(current)) {
547 548
				dev_dbg(&dev->udev->dev, "%s : interrupted\n",
					__func__);
549
				set_current_state(TASK_RUNNING);
550
				retval = -EINTR;
551
				goto exit_onqueue;
552
			}
553
			if (schedule_timeout(COMMAND_TIMEOUT) == 0) {
554 555
				dev_dbg(&dev->udev->dev,
					"%s - command timed out.\n", __func__);
556 557 558 559
				retval = -ETIMEDOUT;
				goto exit_onqueue;
			}
			remove_wait_queue(&dev->write_wait, &waita);
560
			retval = mutex_lock_interruptible(&dev->mtx);
O
Oliver Neukum 已提交
561 562 563 564
			if (retval) {
				retval = bytes_written ? bytes_written : retval;
				goto exit_nolock;
			}
565

566
			dev_dbg(&dev->udev->dev,
567
				"%s : in progress, count = %zd\n",
568
				__func__, count);
569
		} else {
570 571 572
			spin_unlock_irqrestore(&dev->buflock, flags);
			set_current_state(TASK_RUNNING);
			remove_wait_queue(&dev->write_wait, &waita);
573
			dev_dbg(&dev->udev->dev, "%s : sending, count = %zd\n",
574
				__func__, count);
575 576

			/* write the data into interrupt_out_buffer from userspace */
577
			buffer_size = usb_endpoint_maxp(dev->interrupt_out_endpoint);
578
			bytes_to_write = count > buffer_size ? buffer_size : count;
579
			dev_dbg(&dev->udev->dev,
580
				"%s : buffer_size = %zd, count = %zd, bytes_to_write = %zd\n",
581
				__func__, buffer_size, count, bytes_to_write);
582 583 584 585 586 587 588 589 590 591 592 593 594 595 596

			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,
597
				dev->interrupt_out_endpoint->bInterval);
598
			dev->interrupt_out_urb->actual_length = bytes_to_write;
599
			dev->out_urb_finished = 0;
600 601
			retval = usb_submit_urb(dev->interrupt_out_urb, GFP_KERNEL);
			if (retval < 0) {
602
				dev->out_urb_finished = 1;
603 604
				dev_err(&dev->udev->dev, "Couldn't submit "
					"interrupt_out_urb %d\n", retval);
605 606 607 608 609 610 611 612 613
				goto exit;
			}

			buffer += bytes_to_write;
			count -= bytes_to_write;

			bytes_written += bytes_to_write;
		}
	}
614 615
	mutex_unlock(&dev->mtx);
	return bytes_written;
616 617

exit:
618
	mutex_unlock(&dev->mtx);
O
Oliver Neukum 已提交
619
exit_nolock:
620
	return retval;
621

622 623
exit_onqueue:
	remove_wait_queue(&dev->write_wait, &waita);
624 625 626 627
	return retval;
}

/* file operations needed when we register this driver */
628
static const struct file_operations adu_fops = {
629 630 631 632 633
	.owner = THIS_MODULE,
	.read  = adu_read,
	.write = adu_write,
	.open = adu_open,
	.release = adu_release,
634
	.llseek = noop_llseek,
635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657
};

/*
 * 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;
J
Johan Hovold 已提交
658
	int retval = -ENOMEM;
659 660
	int in_end_size;
	int out_end_size;
661
	int res;
662

663
	/* allocate memory for our device state and initialize it */
664
	dev = kzalloc(sizeof(struct adu_device), GFP_KERNEL);
J
Johan Hovold 已提交
665 666
	if (!dev)
		return -ENOMEM;
667

668
	mutex_init(&dev->mtx);
669
	spin_lock_init(&dev->buflock);
670
	dev->udev = usb_get_dev(udev);
671 672 673
	init_waitqueue_head(&dev->read_wait);
	init_waitqueue_head(&dev->write_wait);

674 675 676 677 678 679 680
	res = usb_find_common_endpoints_reverse(&interface->altsetting[0],
			NULL, NULL,
			&dev->interrupt_in_endpoint,
			&dev->interrupt_out_endpoint);
	if (res) {
		dev_err(&interface->dev, "interrupt endpoints not found\n");
		retval = res;
681 682 683
		goto error;
	}

684 685
	in_end_size = usb_endpoint_maxp(dev->interrupt_in_endpoint);
	out_end_size = usb_endpoint_maxp(dev->interrupt_out_endpoint);
686 687

	dev->read_buffer_primary = kmalloc((4 * in_end_size), GFP_KERNEL);
J
Johan Hovold 已提交
688
	if (!dev->read_buffer_primary)
689 690 691 692 693 694 695 696 697
		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);
J
Johan Hovold 已提交
698
	if (!dev->read_buffer_secondary)
699 700 701 702 703 704 705 706 707
		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);
708
	if (!dev->interrupt_in_buffer)
709 710 711 712 713 714
		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);
715
	if (!dev->interrupt_in_urb)
716 717
		goto error;
	dev->interrupt_out_buffer = kmalloc(out_end_size, GFP_KERNEL);
718
	if (!dev->interrupt_out_buffer)
719 720
		goto error;
	dev->interrupt_out_urb = usb_alloc_urb(0, GFP_KERNEL);
721
	if (!dev->interrupt_out_urb)
722 723 724 725 726
		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");
J
Johan Hovold 已提交
727
		retval = -EIO;
728 729
		goto error;
	}
730
	dev_dbg(&interface->dev,"serial_number=%s", dev->serial_number);
731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746

	/* 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 */
747
	dev_info(&interface->dev, "ADU%d %s now attached to /dev/usb/adutux%d\n",
748
		 le16_to_cpu(udev->descriptor.idProduct), dev->serial_number,
749
		 (dev->minor - ADU_MINOR_BASE));
J
Johan Hovold 已提交
750 751

	return 0;
752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770

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;

	dev = usb_get_intfdata(interface);

	usb_deregister_dev(interface, &adu_class);

771 772 773
	usb_poison_urb(dev->interrupt_in_urb);
	usb_poison_urb(dev->interrupt_out_urb);

774 775
	mutex_lock(&adutux_mutex);
	usb_set_intfdata(interface, NULL);
776

777
	mutex_lock(&dev->mtx);	/* not interruptible */
778
	dev->disconnected = 1;
779 780
	mutex_unlock(&dev->mtx);

781
	/* if the device is not opened, then we clean up right now */
782
	if (!dev->open_count)
783
		adu_delete(dev);
784 785

	mutex_unlock(&adutux_mutex);
786 787 788 789 790 791 792 793 794 795
}

/* 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,
};

796
module_usb_driver(adu_driver);
797 798 799 800

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