cdc-wdm.c 20.7 KB
Newer Older
O
Oliver Neukum 已提交
1 2 3 4 5
/*
 * cdc-wdm.c
 *
 * This driver supports USB CDC WCM Device Management.
 *
6
 * Copyright (c) 2007-2009 Oliver Neukum
O
Oliver Neukum 已提交
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
 *
 * Some code taken from cdc-acm.c
 *
 * Released under the GPLv2.
 *
 * Many thanks to Carl Nordbeck
 */
#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/slab.h>
#include <linux/module.h>
#include <linux/mutex.h>
#include <linux/uaccess.h>
#include <linux/bitops.h>
#include <linux/poll.h>
#include <linux/usb.h>
#include <linux/usb/cdc.h>
#include <asm/byteorder.h>
#include <asm/unaligned.h>

/*
 * Version Information
 */
O
Oliver Neukum 已提交
30
#define DRIVER_VERSION "v0.03"
O
Oliver Neukum 已提交
31
#define DRIVER_AUTHOR "Oliver Neukum"
O
Oliver Neukum 已提交
32
#define DRIVER_DESC "USB Abstract Control Model driver for USB WCM Device Management"
O
Oliver Neukum 已提交
33

34
static const struct usb_device_id wdm_ids[] = {
O
Oliver Neukum 已提交
35 36 37 38 39 40 41 42 43
	{
		.match_flags = USB_DEVICE_ID_MATCH_INT_CLASS |
				 USB_DEVICE_ID_MATCH_INT_SUBCLASS,
		.bInterfaceClass = USB_CLASS_COMM,
		.bInterfaceSubClass = USB_CDC_SUBCLASS_DMM
	},
	{ }
};

44 45
MODULE_DEVICE_TABLE (usb, wdm_ids);

O
Oliver Neukum 已提交
46 47 48 49 50 51 52 53 54
#define WDM_MINOR_BASE	176


#define WDM_IN_USE		1
#define WDM_DISCONNECTING	2
#define WDM_RESULT		3
#define WDM_READ		4
#define WDM_INT_STALL		5
#define WDM_POLL_RUNNING	6
55
#define WDM_RESPONDING		7
56
#define WDM_SUSPENDING		8
O
Oliver Neukum 已提交
57 58 59

#define WDM_MAX			16

60 61
/* CDC-WMC r1.1 requires wMaxCommand to be "at least 256 decimal (0x100)" */
#define WDM_DEFAULT_BUFSIZE	256
O
Oliver Neukum 已提交
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92

static DEFINE_MUTEX(wdm_mutex);

/* --- method tables --- */

struct wdm_device {
	u8			*inbuf; /* buffer for response */
	u8			*outbuf; /* buffer for command */
	u8			*sbuf; /* buffer for status */
	u8			*ubuf; /* buffer for copy to user space */

	struct urb		*command;
	struct urb		*response;
	struct urb		*validity;
	struct usb_interface	*intf;
	struct usb_ctrlrequest	*orq;
	struct usb_ctrlrequest	*irq;
	spinlock_t		iuspin;

	unsigned long		flags;
	u16			bufsize;
	u16			wMaxCommand;
	u16			wMaxPacketSize;
	u16			bMaxPacketSize0;
	__le16			inum;
	int			reslength;
	int			length;
	int			read;
	int			count;
	dma_addr_t		shandle;
	dma_addr_t		ihandle;
93 94
	struct mutex		wlock;
	struct mutex		rlock;
O
Oliver Neukum 已提交
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
	wait_queue_head_t	wait;
	struct work_struct	rxwork;
	int			werr;
	int			rerr;
};

static struct usb_driver wdm_driver;

/* --- callbacks --- */
static void wdm_out_callback(struct urb *urb)
{
	struct wdm_device *desc;
	desc = urb->context;
	spin_lock(&desc->iuspin);
	desc->werr = urb->status;
	spin_unlock(&desc->iuspin);
	clear_bit(WDM_IN_USE, &desc->flags);
	kfree(desc->outbuf);
	wake_up(&desc->wait);
}

static void wdm_in_callback(struct urb *urb)
{
	struct wdm_device *desc = urb->context;
	int status = urb->status;

	spin_lock(&desc->iuspin);
122
	clear_bit(WDM_RESPONDING, &desc->flags);
O
Oliver Neukum 已提交
123 124 125 126 127 128

	if (status) {
		switch (status) {
		case -ENOENT:
			dev_dbg(&desc->intf->dev,
				"nonzero urb status received: -ENOENT");
129
			goto skip_error;
O
Oliver Neukum 已提交
130 131 132
		case -ECONNRESET:
			dev_dbg(&desc->intf->dev,
				"nonzero urb status received: -ECONNRESET");
133
			goto skip_error;
O
Oliver Neukum 已提交
134 135 136
		case -ESHUTDOWN:
			dev_dbg(&desc->intf->dev,
				"nonzero urb status received: -ESHUTDOWN");
137
			goto skip_error;
O
Oliver Neukum 已提交
138
		case -EPIPE:
139 140
			dev_err(&desc->intf->dev,
				"nonzero urb status received: -EPIPE\n");
O
Oliver Neukum 已提交
141 142
			break;
		default:
143 144
			dev_err(&desc->intf->dev,
				"Unexpected error %d\n", status);
O
Oliver Neukum 已提交
145 146 147 148 149 150 151 152
			break;
		}
	}

	desc->rerr = status;
	desc->reslength = urb->actual_length;
	memmove(desc->ubuf + desc->length, desc->inbuf, desc->reslength);
	desc->length += desc->reslength;
153
skip_error:
O
Oliver Neukum 已提交
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
	wake_up(&desc->wait);

	set_bit(WDM_READ, &desc->flags);
	spin_unlock(&desc->iuspin);
}

static void wdm_int_callback(struct urb *urb)
{
	int rv = 0;
	int status = urb->status;
	struct wdm_device *desc;
	struct usb_ctrlrequest *req;
	struct usb_cdc_notification *dr;

	desc = urb->context;
	req = desc->irq;
	dr = (struct usb_cdc_notification *)desc->sbuf;

	if (status) {
		switch (status) {
		case -ESHUTDOWN:
		case -ENOENT:
		case -ECONNRESET:
			return; /* unplug */
		case -EPIPE:
			set_bit(WDM_INT_STALL, &desc->flags);
180
			dev_err(&desc->intf->dev, "Stall on int endpoint\n");
O
Oliver Neukum 已提交
181 182
			goto sw; /* halt is cleared in work */
		default:
183 184
			dev_err(&desc->intf->dev,
				"nonzero urb status received: %d\n", status);
O
Oliver Neukum 已提交
185 186 187 188 189
			break;
		}
	}

	if (urb->actual_length < sizeof(struct usb_cdc_notification)) {
190 191
		dev_err(&desc->intf->dev, "wdm_int_callback - %d bytes\n",
			urb->actual_length);
O
Oliver Neukum 已提交
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
		goto exit;
	}

	switch (dr->bNotificationType) {
	case USB_CDC_NOTIFY_RESPONSE_AVAILABLE:
		dev_dbg(&desc->intf->dev,
			"NOTIFY_RESPONSE_AVAILABLE received: index %d len %d",
			dr->wIndex, dr->wLength);
		break;

	case USB_CDC_NOTIFY_NETWORK_CONNECTION:

		dev_dbg(&desc->intf->dev,
			"NOTIFY_NETWORK_CONNECTION %s network",
			dr->wValue ? "connected to" : "disconnected from");
		goto exit;
	default:
		clear_bit(WDM_POLL_RUNNING, &desc->flags);
210 211
		dev_err(&desc->intf->dev,
			"unknown notification %d received: index %d len %d\n",
O
Oliver Neukum 已提交
212 213 214 215 216 217 218 219
			dr->bNotificationType, dr->wIndex, dr->wLength);
		goto exit;
	}

	req->bRequestType = (USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE);
	req->bRequest = USB_CDC_GET_ENCAPSULATED_RESPONSE;
	req->wValue = 0;
	req->wIndex = desc->inum;
O
Oliver Neukum 已提交
220
	req->wLength = cpu_to_le16(desc->wMaxCommand);
O
Oliver Neukum 已提交
221 222 223 224 225 226 227 228

	usb_fill_control_urb(
		desc->response,
		interface_to_usbdev(desc->intf),
		/* using common endpoint 0 */
		usb_rcvctrlpipe(interface_to_usbdev(desc->intf), 0),
		(unsigned char *)req,
		desc->inbuf,
O
Oliver Neukum 已提交
229
		desc->wMaxCommand,
O
Oliver Neukum 已提交
230 231 232 233 234 235
		wdm_in_callback,
		desc
	);
	desc->response->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
	spin_lock(&desc->iuspin);
	clear_bit(WDM_READ, &desc->flags);
236
	set_bit(WDM_RESPONDING, &desc->flags);
237 238
	if (!test_bit(WDM_DISCONNECTING, &desc->flags)
		&& !test_bit(WDM_SUSPENDING, &desc->flags)) {
O
Oliver Neukum 已提交
239 240 241 242 243 244
		rv = usb_submit_urb(desc->response, GFP_ATOMIC);
		dev_dbg(&desc->intf->dev, "%s: usb_submit_urb %d",
			__func__, rv);
	}
	spin_unlock(&desc->iuspin);
	if (rv < 0) {
245
		clear_bit(WDM_RESPONDING, &desc->flags);
O
Oliver Neukum 已提交
246 247 248 249 250 251
		if (rv == -EPERM)
			return;
		if (rv == -ENOMEM) {
sw:
			rv = schedule_work(&desc->rxwork);
			if (rv)
252 253
				dev_err(&desc->intf->dev,
					"Cannot schedule work\n");
O
Oliver Neukum 已提交
254 255 256 257 258
		}
	}
exit:
	rv = usb_submit_urb(urb, GFP_ATOMIC);
	if (rv)
259 260 261
		dev_err(&desc->intf->dev,
			"%s - usb_submit_urb failed with result %d\n",
			__func__, rv);
O
Oliver Neukum 已提交
262 263 264 265 266

}

static void kill_urbs(struct wdm_device *desc)
{
O
Oliver Neukum 已提交
267
	/* the order here is essential */
O
Oliver Neukum 已提交
268 269 270 271 272 273 274 275 276 277 278 279 280 281
	usb_kill_urb(desc->command);
	usb_kill_urb(desc->validity);
	usb_kill_urb(desc->response);
}

static void free_urbs(struct wdm_device *desc)
{
	usb_free_urb(desc->validity);
	usb_free_urb(desc->response);
	usb_free_urb(desc->command);
}

static void cleanup(struct wdm_device *desc)
{
282 283 284 285 286
	usb_free_coherent(interface_to_usbdev(desc->intf),
			  desc->wMaxPacketSize,
			  desc->sbuf,
			  desc->validity->transfer_dma);
	usb_free_coherent(interface_to_usbdev(desc->intf),
287
			  desc->bMaxPacketSize0,
288 289
			  desc->inbuf,
			  desc->response->transfer_dma);
O
Oliver Neukum 已提交
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
	kfree(desc->orq);
	kfree(desc->irq);
	kfree(desc->ubuf);
	free_urbs(desc);
	kfree(desc);
}

static ssize_t wdm_write
(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
{
	u8 *buf;
	int rv = -EMSGSIZE, r, we;
	struct wdm_device *desc = file->private_data;
	struct usb_ctrlrequest *req;

	if (count > desc->wMaxCommand)
		count = desc->wMaxCommand;

	spin_lock_irq(&desc->iuspin);
	we = desc->werr;
	desc->werr = 0;
	spin_unlock_irq(&desc->iuspin);
	if (we < 0)
		return -EIO;

315 316 317 318 319 320 321 322 323 324 325 326 327 328
	desc->outbuf = buf = kmalloc(count, GFP_KERNEL);
	if (!buf) {
		rv = -ENOMEM;
		goto outnl;
	}

	r = copy_from_user(buf, buffer, count);
	if (r > 0) {
		kfree(buf);
		rv = -EFAULT;
		goto outnl;
	}

	/* concurrent writes and disconnect */
329
	r = mutex_lock_interruptible(&desc->wlock);
O
Oliver Neukum 已提交
330
	rv = -ERESTARTSYS;
331 332
	if (r) {
		kfree(buf);
O
Oliver Neukum 已提交
333
		goto outnl;
334 335 336 337 338 339 340
	}

	if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
		kfree(buf);
		rv = -ENODEV;
		goto outnp;
	}
O
Oliver Neukum 已提交
341

O
Oliver Neukum 已提交
342
	r = usb_autopm_get_interface(desc->intf);
343 344
	if (r < 0) {
		kfree(buf);
O
Oliver Neukum 已提交
345
		goto outnp;
346
	}
347

348
	if (!(file->f_flags & O_NONBLOCK))
349 350 351 352 353
		r = wait_event_interruptible(desc->wait, !test_bit(WDM_IN_USE,
								&desc->flags));
	else
		if (test_bit(WDM_IN_USE, &desc->flags))
			r = -EAGAIN;
354
	if (r < 0) {
O
Oliver Neukum 已提交
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
		kfree(buf);
		goto out;
	}

	req = desc->orq;
	usb_fill_control_urb(
		desc->command,
		interface_to_usbdev(desc->intf),
		/* using common endpoint 0 */
		usb_sndctrlpipe(interface_to_usbdev(desc->intf), 0),
		(unsigned char *)req,
		buf,
		count,
		wdm_out_callback,
		desc
	);

	req->bRequestType = (USB_DIR_OUT | USB_TYPE_CLASS |
			     USB_RECIP_INTERFACE);
	req->bRequest = USB_CDC_SEND_ENCAPSULATED_COMMAND;
	req->wValue = 0;
	req->wIndex = desc->inum;
	req->wLength = cpu_to_le16(count);
	set_bit(WDM_IN_USE, &desc->flags);

	rv = usb_submit_urb(desc->command, GFP_KERNEL);
	if (rv < 0) {
		kfree(buf);
		clear_bit(WDM_IN_USE, &desc->flags);
384
		dev_err(&desc->intf->dev, "Tx URB error: %d\n", rv);
O
Oliver Neukum 已提交
385 386 387 388 389
	} else {
		dev_dbg(&desc->intf->dev, "Tx URB has been submitted index=%d",
			req->wIndex);
	}
out:
O
Oliver Neukum 已提交
390 391
	usb_autopm_put_interface(desc->intf);
outnp:
392
	mutex_unlock(&desc->wlock);
O
Oliver Neukum 已提交
393 394 395 396 397 398 399
outnl:
	return rv < 0 ? rv : count;
}

static ssize_t wdm_read
(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
{
400
	int rv, cntr = 0;
O
Oliver Neukum 已提交
401 402 403 404
	int i = 0;
	struct wdm_device *desc = file->private_data;


405
	rv = mutex_lock_interruptible(&desc->rlock); /*concurrent reads */
O
Oliver Neukum 已提交
406 407 408 409 410 411
	if (rv < 0)
		return -ERESTARTSYS;

	if (desc->length == 0) {
		desc->read = 0;
retry:
412 413 414 415
		if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
			rv = -ENODEV;
			goto err;
		}
O
Oliver Neukum 已提交
416
		i++;
417 418 419 420 421 422 423 424 425 426
		if (file->f_flags & O_NONBLOCK) {
			if (!test_bit(WDM_READ, &desc->flags)) {
				rv = cntr ? cntr : -EAGAIN;
				goto err;
			}
			rv = 0;
		} else {
			rv = wait_event_interruptible(desc->wait,
				test_bit(WDM_READ, &desc->flags));
		}
O
Oliver Neukum 已提交
427

428
		/* may have happened while we slept */
O
Oliver Neukum 已提交
429 430 431 432 433
		if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
			rv = -ENODEV;
			goto err;
		}
		usb_mark_last_busy(interface_to_usbdev(desc->intf));
O
Oliver Neukum 已提交
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472
		if (rv < 0) {
			rv = -ERESTARTSYS;
			goto err;
		}

		spin_lock_irq(&desc->iuspin);

		if (desc->rerr) { /* read completed, error happened */
			desc->rerr = 0;
			spin_unlock_irq(&desc->iuspin);
			rv = -EIO;
			goto err;
		}
		/*
		 * recheck whether we've lost the race
		 * against the completion handler
		 */
		if (!test_bit(WDM_READ, &desc->flags)) { /* lost race */
			spin_unlock_irq(&desc->iuspin);
			goto retry;
		}
		if (!desc->reslength) { /* zero length read */
			spin_unlock_irq(&desc->iuspin);
			goto retry;
		}
		clear_bit(WDM_READ, &desc->flags);
		spin_unlock_irq(&desc->iuspin);
	}

	cntr = count > desc->length ? desc->length : count;
	rv = copy_to_user(buffer, desc->ubuf, cntr);
	if (rv > 0) {
		rv = -EFAULT;
		goto err;
	}

	for (i = 0; i < desc->length - cntr; i++)
		desc->ubuf[i] = desc->ubuf[i + cntr];

473
	spin_lock_irq(&desc->iuspin);
O
Oliver Neukum 已提交
474
	desc->length -= cntr;
475
	spin_unlock_irq(&desc->iuspin);
O
Oliver Neukum 已提交
476 477 478
	/* in case we had outstanding data */
	if (!desc->length)
		clear_bit(WDM_READ, &desc->flags);
O
Oliver Neukum 已提交
479 480 481
	rv = cntr;

err:
482
	mutex_unlock(&desc->rlock);
O
Oliver Neukum 已提交
483 484 485 486 487 488 489 490 491
	return rv;
}

static int wdm_flush(struct file *file, fl_owner_t id)
{
	struct wdm_device *desc = file->private_data;

	wait_event(desc->wait, !test_bit(WDM_IN_USE, &desc->flags));
	if (desc->werr < 0)
492 493
		dev_err(&desc->intf->dev, "Error in flush path: %d\n",
			desc->werr);
O
Oliver Neukum 已提交
494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540

	return desc->werr;
}

static unsigned int wdm_poll(struct file *file, struct poll_table_struct *wait)
{
	struct wdm_device *desc = file->private_data;
	unsigned long flags;
	unsigned int mask = 0;

	spin_lock_irqsave(&desc->iuspin, flags);
	if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
		mask = POLLERR;
		spin_unlock_irqrestore(&desc->iuspin, flags);
		goto desc_out;
	}
	if (test_bit(WDM_READ, &desc->flags))
		mask = POLLIN | POLLRDNORM;
	if (desc->rerr || desc->werr)
		mask |= POLLERR;
	if (!test_bit(WDM_IN_USE, &desc->flags))
		mask |= POLLOUT | POLLWRNORM;
	spin_unlock_irqrestore(&desc->iuspin, flags);

	poll_wait(file, &desc->wait, wait);

desc_out:
	return mask;
}

static int wdm_open(struct inode *inode, struct file *file)
{
	int minor = iminor(inode);
	int rv = -ENODEV;
	struct usb_interface *intf;
	struct wdm_device *desc;

	mutex_lock(&wdm_mutex);
	intf = usb_find_interface(&wdm_driver, minor);
	if (!intf)
		goto out;

	desc = usb_get_intfdata(intf);
	if (test_bit(WDM_DISCONNECTING, &desc->flags))
		goto out;
	file->private_data = desc;

O
Oliver Neukum 已提交
541
	rv = usb_autopm_get_interface(desc->intf);
O
Oliver Neukum 已提交
542
	if (rv < 0) {
543
		dev_err(&desc->intf->dev, "Error autopm - %d\n", rv);
O
Oliver Neukum 已提交
544 545
		goto out;
	}
O
Oliver Neukum 已提交
546
	intf->needs_remote_wakeup = 1;
O
Oliver Neukum 已提交
547

548 549
	/* using write lock to protect desc->count */
	mutex_lock(&desc->wlock);
O
Oliver Neukum 已提交
550
	if (!desc->count++) {
551 552
		desc->werr = 0;
		desc->rerr = 0;
O
Oliver Neukum 已提交
553 554 555
		rv = usb_submit_urb(desc->validity, GFP_KERNEL);
		if (rv < 0) {
			desc->count--;
556 557
			dev_err(&desc->intf->dev,
				"Error submitting int urb - %d\n", rv);
O
Oliver Neukum 已提交
558 559 560 561
		}
	} else {
		rv = 0;
	}
562
	mutex_unlock(&desc->wlock);
O
Oliver Neukum 已提交
563
	usb_autopm_put_interface(desc->intf);
O
Oliver Neukum 已提交
564 565 566 567 568 569 570 571 572 573
out:
	mutex_unlock(&wdm_mutex);
	return rv;
}

static int wdm_release(struct inode *inode, struct file *file)
{
	struct wdm_device *desc = file->private_data;

	mutex_lock(&wdm_mutex);
574 575 576

	/* using write lock to protect desc->count */
	mutex_lock(&desc->wlock);
O
Oliver Neukum 已提交
577
	desc->count--;
578
	mutex_unlock(&desc->wlock);
O
Oliver Neukum 已提交
579

O
Oliver Neukum 已提交
580 581 582
	if (!desc->count) {
		dev_dbg(&desc->intf->dev, "wdm_release: cleanup");
		kill_urbs(desc);
O
Oliver Neukum 已提交
583 584
		if (!test_bit(WDM_DISCONNECTING, &desc->flags))
			desc->intf->needs_remote_wakeup = 0;
O
Oliver Neukum 已提交
585 586 587 588 589 590 591 592 593 594 595 596
	}
	mutex_unlock(&wdm_mutex);
	return 0;
}

static const struct file_operations wdm_fops = {
	.owner =	THIS_MODULE,
	.read =		wdm_read,
	.write =	wdm_write,
	.open =		wdm_open,
	.flush =	wdm_flush,
	.release =	wdm_release,
597 598
	.poll =		wdm_poll,
	.llseek =	noop_llseek,
O
Oliver Neukum 已提交
599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640
};

static struct usb_class_driver wdm_class = {
	.name =		"cdc-wdm%d",
	.fops =		&wdm_fops,
	.minor_base =	WDM_MINOR_BASE,
};

/* --- error handling --- */
static void wdm_rxwork(struct work_struct *work)
{
	struct wdm_device *desc = container_of(work, struct wdm_device, rxwork);
	unsigned long flags;
	int rv;

	spin_lock_irqsave(&desc->iuspin, flags);
	if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
		spin_unlock_irqrestore(&desc->iuspin, flags);
	} else {
		spin_unlock_irqrestore(&desc->iuspin, flags);
		rv = usb_submit_urb(desc->response, GFP_KERNEL);
		if (rv < 0 && rv != -EPERM) {
			spin_lock_irqsave(&desc->iuspin, flags);
			if (!test_bit(WDM_DISCONNECTING, &desc->flags))
				schedule_work(&desc->rxwork);
			spin_unlock_irqrestore(&desc->iuspin, flags);
		}
	}
}

/* --- hotplug --- */

static int wdm_probe(struct usb_interface *intf, const struct usb_device_id *id)
{
	int rv = -EINVAL;
	struct usb_device *udev = interface_to_usbdev(intf);
	struct wdm_device *desc;
	struct usb_host_interface *iface;
	struct usb_endpoint_descriptor *ep;
	struct usb_cdc_dmm_desc *dmhd;
	u8 *buffer = intf->altsetting->extra;
	int buflen = intf->altsetting->extralen;
641
	u16 maxcom = WDM_DEFAULT_BUFSIZE;
O
Oliver Neukum 已提交
642 643 644 645

	if (!buffer)
		goto out;

646
	while (buflen > 2) {
O
Oliver Neukum 已提交
647
		if (buffer [1] != USB_DT_CS_INTERFACE) {
648
			dev_err(&intf->dev, "skipping garbage\n");
O
Oliver Neukum 已提交
649 650 651 652 653 654 655 656 657 658 659 660 661
			goto next_desc;
		}

		switch (buffer [2]) {
		case USB_CDC_HEADER_TYPE:
			break;
		case USB_CDC_DMM_TYPE:
			dmhd = (struct usb_cdc_dmm_desc *)buffer;
			maxcom = le16_to_cpu(dmhd->wMaxCommand);
			dev_dbg(&intf->dev,
				"Finding maximum buffer length: %d", maxcom);
			break;
		default:
662 663
			dev_err(&intf->dev,
				"Ignoring extra header, type %d, length %d\n",
O
Oliver Neukum 已提交
664 665 666 667 668 669 670 671 672 673 674 675
				buffer[2], buffer[0]);
			break;
		}
next_desc:
		buflen -= buffer[0];
		buffer += buffer[0];
	}

	rv = -ENOMEM;
	desc = kzalloc(sizeof(struct wdm_device), GFP_KERNEL);
	if (!desc)
		goto out;
676 677
	mutex_init(&desc->rlock);
	mutex_init(&desc->wlock);
O
Oliver Neukum 已提交
678 679 680
	spin_lock_init(&desc->iuspin);
	init_waitqueue_head(&desc->wait);
	desc->wMaxCommand = maxcom;
681
	/* this will be expanded and needed in hardware endianness */
O
Oliver Neukum 已提交
682 683 684 685
	desc->inum = cpu_to_le16((u16)intf->cur_altsetting->desc.bInterfaceNumber);
	desc->intf = intf;
	INIT_WORK(&desc->rxwork, wdm_rxwork);

686 687 688 689
	rv = -EINVAL;
	iface = intf->cur_altsetting;
	if (iface->desc.bNumEndpoints != 1)
		goto err;
O
Oliver Neukum 已提交
690
	ep = &iface->endpoint[0].desc;
691
	if (!ep || !usb_endpoint_is_int_in(ep))
O
Oliver Neukum 已提交
692 693
		goto err;

694
	desc->wMaxPacketSize = usb_endpoint_maxp(ep);
A
Al Viro 已提交
695
	desc->bMaxPacketSize0 = udev->descriptor.bMaxPacketSize0;
O
Oliver Neukum 已提交
696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719

	desc->orq = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
	if (!desc->orq)
		goto err;
	desc->irq = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
	if (!desc->irq)
		goto err;

	desc->validity = usb_alloc_urb(0, GFP_KERNEL);
	if (!desc->validity)
		goto err;

	desc->response = usb_alloc_urb(0, GFP_KERNEL);
	if (!desc->response)
		goto err;

	desc->command = usb_alloc_urb(0, GFP_KERNEL);
	if (!desc->command)
		goto err;

	desc->ubuf = kmalloc(desc->wMaxCommand, GFP_KERNEL);
	if (!desc->ubuf)
		goto err;

720
	desc->sbuf = usb_alloc_coherent(interface_to_usbdev(intf),
O
Oliver Neukum 已提交
721 722 723 724 725 726
					desc->wMaxPacketSize,
					GFP_KERNEL,
					&desc->validity->transfer_dma);
	if (!desc->sbuf)
		goto err;

727
	desc->inbuf = usb_alloc_coherent(interface_to_usbdev(intf),
728
					 desc->wMaxCommand,
729 730
					 GFP_KERNEL,
					 &desc->response->transfer_dma);
O
Oliver Neukum 已提交
731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748
	if (!desc->inbuf)
		goto err2;

	usb_fill_int_urb(
		desc->validity,
		interface_to_usbdev(intf),
		usb_rcvintpipe(interface_to_usbdev(intf), ep->bEndpointAddress),
		desc->sbuf,
		desc->wMaxPacketSize,
		wdm_int_callback,
		desc,
		ep->bInterval
	);
	desc->validity->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;

	usb_set_intfdata(intf, desc);
	rv = usb_register_dev(intf, &wdm_class);
	if (rv < 0)
749 750 751 752
		goto err3;
	else
		dev_info(&intf->dev, "cdc-wdm%d: USB WDM device\n",
			intf->minor - WDM_MINOR_BASE);
O
Oliver Neukum 已提交
753 754
out:
	return rv;
755 756
err3:
	usb_set_intfdata(intf, NULL);
757 758
	usb_free_coherent(interface_to_usbdev(desc->intf),
			  desc->bMaxPacketSize0,
759 760
			desc->inbuf,
			desc->response->transfer_dma);
O
Oliver Neukum 已提交
761
err2:
762 763 764 765
	usb_free_coherent(interface_to_usbdev(desc->intf),
			  desc->wMaxPacketSize,
			  desc->sbuf,
			  desc->validity->transfer_dma);
O
Oliver Neukum 已提交
766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787
err:
	free_urbs(desc);
	kfree(desc->ubuf);
	kfree(desc->orq);
	kfree(desc->irq);
	kfree(desc);
	return rv;
}

static void wdm_disconnect(struct usb_interface *intf)
{
	struct wdm_device *desc;
	unsigned long flags;

	usb_deregister_dev(intf, &wdm_class);
	mutex_lock(&wdm_mutex);
	desc = usb_get_intfdata(intf);

	/* the spinlock makes sure no new urbs are generated in the callbacks */
	spin_lock_irqsave(&desc->iuspin, flags);
	set_bit(WDM_DISCONNECTING, &desc->flags);
	set_bit(WDM_READ, &desc->flags);
O
Oliver Neukum 已提交
788
	/* to terminate pending flushes */
O
Oliver Neukum 已提交
789 790
	clear_bit(WDM_IN_USE, &desc->flags);
	spin_unlock_irqrestore(&desc->iuspin, flags);
791
	wake_up_all(&desc->wait);
792 793
	mutex_lock(&desc->rlock);
	mutex_lock(&desc->wlock);
O
Oliver Neukum 已提交
794
	kill_urbs(desc);
795
	cancel_work_sync(&desc->rxwork);
796 797
	mutex_unlock(&desc->wlock);
	mutex_unlock(&desc->rlock);
O
Oliver Neukum 已提交
798 799 800 801 802
	if (!desc->count)
		cleanup(desc);
	mutex_unlock(&wdm_mutex);
}

803
#ifdef CONFIG_PM
O
Oliver Neukum 已提交
804 805 806 807 808 809 810
static int wdm_suspend(struct usb_interface *intf, pm_message_t message)
{
	struct wdm_device *desc = usb_get_intfdata(intf);
	int rv = 0;

	dev_dbg(&desc->intf->dev, "wdm%d_suspend\n", intf->minor);

811
	/* if this is an autosuspend the caller does the locking */
812 813 814 815
	if (!PMSG_IS_AUTO(message)) {
		mutex_lock(&desc->rlock);
		mutex_lock(&desc->wlock);
	}
816
	spin_lock_irq(&desc->iuspin);
817

818
	if (PMSG_IS_AUTO(message) &&
819 820
			(test_bit(WDM_IN_USE, &desc->flags)
			|| test_bit(WDM_RESPONDING, &desc->flags))) {
821
		spin_unlock_irq(&desc->iuspin);
O
Oliver Neukum 已提交
822 823
		rv = -EBUSY;
	} else {
824

825
		set_bit(WDM_SUSPENDING, &desc->flags);
826
		spin_unlock_irq(&desc->iuspin);
827
		/* callback submits work - order is essential */
O
Oliver Neukum 已提交
828
		kill_urbs(desc);
829
		cancel_work_sync(&desc->rxwork);
O
Oliver Neukum 已提交
830
	}
831 832 833 834
	if (!PMSG_IS_AUTO(message)) {
		mutex_unlock(&desc->wlock);
		mutex_unlock(&desc->rlock);
	}
O
Oliver Neukum 已提交
835 836 837

	return rv;
}
838
#endif
O
Oliver Neukum 已提交
839 840 841 842 843 844 845 846

static int recover_from_urb_loss(struct wdm_device *desc)
{
	int rv = 0;

	if (desc->count) {
		rv = usb_submit_urb(desc->validity, GFP_NOIO);
		if (rv < 0)
847 848
			dev_err(&desc->intf->dev,
				"Error resume submitting int urb - %d\n", rv);
O
Oliver Neukum 已提交
849 850 851
	}
	return rv;
}
852 853

#ifdef CONFIG_PM
O
Oliver Neukum 已提交
854 855 856 857 858 859
static int wdm_resume(struct usb_interface *intf)
{
	struct wdm_device *desc = usb_get_intfdata(intf);
	int rv;

	dev_dbg(&desc->intf->dev, "wdm%d_resume\n", intf->minor);
860

861
	clear_bit(WDM_SUSPENDING, &desc->flags);
862
	rv = recover_from_urb_loss(desc);
863

O
Oliver Neukum 已提交
864 865
	return rv;
}
866
#endif
O
Oliver Neukum 已提交
867 868 869 870 871

static int wdm_pre_reset(struct usb_interface *intf)
{
	struct wdm_device *desc = usb_get_intfdata(intf);

872 873
	mutex_lock(&desc->rlock);
	mutex_lock(&desc->wlock);
874 875 876 877 878 879 880 881 882 883 884 885
	kill_urbs(desc);

	/*
	 * we notify everybody using poll of
	 * an exceptional situation
	 * must be done before recovery lest a spontaneous
	 * message from the device is lost
	 */
	spin_lock_irq(&desc->iuspin);
	desc->rerr = -EINTR;
	spin_unlock_irq(&desc->iuspin);
	wake_up_all(&desc->wait);
O
Oliver Neukum 已提交
886 887 888 889 890 891 892 893 894
	return 0;
}

static int wdm_post_reset(struct usb_interface *intf)
{
	struct wdm_device *desc = usb_get_intfdata(intf);
	int rv;

	rv = recover_from_urb_loss(desc);
895 896
	mutex_unlock(&desc->wlock);
	mutex_unlock(&desc->rlock);
O
Oliver Neukum 已提交
897 898 899
	return 0;
}

O
Oliver Neukum 已提交
900 901 902 903
static struct usb_driver wdm_driver = {
	.name =		"cdc_wdm",
	.probe =	wdm_probe,
	.disconnect =	wdm_disconnect,
904
#ifdef CONFIG_PM
O
Oliver Neukum 已提交
905 906 907
	.suspend =	wdm_suspend,
	.resume =	wdm_resume,
	.reset_resume =	wdm_resume,
908
#endif
O
Oliver Neukum 已提交
909 910
	.pre_reset =	wdm_pre_reset,
	.post_reset =	wdm_post_reset,
O
Oliver Neukum 已提交
911
	.id_table =	wdm_ids,
O
Oliver Neukum 已提交
912
	.supports_autosuspend = 1,
O
Oliver Neukum 已提交
913 914
};

915
module_usb_driver(wdm_driver);
O
Oliver Neukum 已提交
916 917

MODULE_AUTHOR(DRIVER_AUTHOR);
O
Oliver Neukum 已提交
918
MODULE_DESCRIPTION(DRIVER_DESC);
O
Oliver Neukum 已提交
919
MODULE_LICENSE("GPL");