dummy_hcd.c 62.9 KB
Newer Older
L
Linus Torvalds 已提交
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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
/*
 * dummy_hcd.c -- Dummy/Loopback USB host and device emulator driver.
 *
 * Maintainer: Alan Stern <stern@rowland.harvard.edu>
 *
 * Copyright (C) 2003 David Brownell
 * Copyright (C) 2003-2005 Alan Stern
 *
 * 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.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */


/*
 * This exposes a device side "USB gadget" API, driven by requests to a
 * Linux-USB host controller driver.  USB traffic is simulated; there's
 * no need for USB hardware.  Use this with two other drivers:
 *
 *  - Gadget driver, responding to requests (slave);
 *  - Host-side device driver, as already familiar in Linux.
 *
 * Having this all in one kernel can help some stages of development,
 * bypassing some hardware (and driver) issues.  UML could help too.
 */

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/ioport.h>
#include <linux/slab.h>
#include <linux/errno.h>
#include <linux/init.h>
#include <linux/timer.h>
#include <linux/list.h>
#include <linux/interrupt.h>
47
#include <linux/platform_device.h>
L
Linus Torvalds 已提交
48
#include <linux/usb.h>
49
#include <linux/usb/gadget.h>
50
#include <linux/usb/hcd.h>
L
Linus Torvalds 已提交
51 52 53 54 55 56 57 58 59

#include <asm/byteorder.h>
#include <asm/io.h>
#include <asm/irq.h>
#include <asm/system.h>
#include <asm/unaligned.h>


#define DRIVER_DESC	"USB Host+Gadget Emulator"
60
#define DRIVER_VERSION	"02 May 2005"
L
Linus Torvalds 已提交
61

62 63
#define POWER_BUDGET	500	/* in mA; use 8 for low-power port testing */

L
Linus Torvalds 已提交
64 65 66 67 68 69 70 71 72
static const char	driver_name [] = "dummy_hcd";
static const char	driver_desc [] = "USB Host+Gadget Emulator";

static const char	gadget_name [] = "dummy_udc";

MODULE_DESCRIPTION (DRIVER_DESC);
MODULE_AUTHOR ("David Brownell");
MODULE_LICENSE ("GPL");

73 74
struct dummy_hcd_module_parameters {
	bool is_super_speed;
75
	bool is_high_speed;
76 77 78
};

static struct dummy_hcd_module_parameters mod_data = {
79 80
	.is_super_speed = false,
	.is_high_speed = true,
81 82 83
};
module_param_named(is_super_speed, mod_data.is_super_speed, bool, S_IRUGO);
MODULE_PARM_DESC(is_super_speed, "true to simulate SuperSpeed connection");
84 85
module_param_named(is_high_speed, mod_data.is_high_speed, bool, S_IRUGO);
MODULE_PARM_DESC(is_high_speed, "true to simulate HighSpeed connection");
L
Linus Torvalds 已提交
86 87 88 89 90 91 92 93 94 95
/*-------------------------------------------------------------------------*/

/* gadget side driver data structres */
struct dummy_ep {
	struct list_head		queue;
	unsigned long			last_io;	/* jiffies timestamp */
	struct usb_gadget		*gadget;
	const struct usb_endpoint_descriptor *desc;
	struct usb_ep			ep;
	unsigned			halted : 1;
96
	unsigned			wedged : 1;
L
Linus Torvalds 已提交
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 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
	unsigned			already_seen : 1;
	unsigned			setup_stage : 1;
};

struct dummy_request {
	struct list_head		queue;		/* ep's requests */
	struct usb_request		req;
};

static inline struct dummy_ep *usb_ep_to_dummy_ep (struct usb_ep *_ep)
{
	return container_of (_ep, struct dummy_ep, ep);
}

static inline struct dummy_request *usb_request_to_dummy_request
		(struct usb_request *_req)
{
	return container_of (_req, struct dummy_request, req);
}

/*-------------------------------------------------------------------------*/

/*
 * Every device has ep0 for control requests, plus up to 30 more endpoints,
 * in one of two types:
 *
 *   - Configurable:  direction (in/out), type (bulk, iso, etc), and endpoint
 *     number can be changed.  Names like "ep-a" are used for this type.
 *
 *   - Fixed Function:  in other cases.  some characteristics may be mutable;
 *     that'd be hardware-specific.  Names like "ep12out-bulk" are used.
 *
 * Gadget drivers are responsible for not setting up conflicting endpoint
 * configurations, illegal or unsupported packet lengths, and so on.
 */

static const char ep0name [] = "ep0";

static const char *const ep_name [] = {
	ep0name,				/* everyone has ep0 */

	/* act like a net2280: high speed, six configurable endpoints */
	"ep-a", "ep-b", "ep-c", "ep-d", "ep-e", "ep-f",

	/* or like pxa250: fifteen fixed function endpoints */
	"ep1in-bulk", "ep2out-bulk", "ep3in-iso", "ep4out-iso", "ep5in-int",
	"ep6in-bulk", "ep7out-bulk", "ep8in-iso", "ep9out-iso", "ep10in-int",
	"ep11in-bulk", "ep12out-bulk", "ep13in-iso", "ep14out-iso",
		"ep15in-int",

	/* or like sa1100: two fixed function endpoints */
	"ep1out-bulk", "ep2in-bulk",
};
150
#define DUMMY_ENDPOINTS	ARRAY_SIZE(ep_name)
L
Linus Torvalds 已提交
151

152 153
/*-------------------------------------------------------------------------*/

L
Linus Torvalds 已提交
154 155 156 157 158 159 160
#define FIFO_SIZE		64

struct urbp {
	struct urb		*urb;
	struct list_head	urbp_list;
};

161 162 163 164 165 166 167

enum dummy_rh_state {
	DUMMY_RH_RESET,
	DUMMY_RH_SUSPENDED,
	DUMMY_RH_RUNNING
};

168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
struct dummy_hcd {
	struct dummy			*dum;
	enum dummy_rh_state		rh_state;
	struct timer_list		timer;
	u32				port_status;
	u32				old_status;
	unsigned long			re_timeout;

	struct usb_device		*udev;
	struct list_head		urbp_list;

	unsigned			active:1;
	unsigned			old_active:1;
	unsigned			resuming:1;
};

L
Linus Torvalds 已提交
184 185 186 187 188 189 190 191 192 193 194 195 196
struct dummy {
	spinlock_t			lock;

	/*
	 * SLAVE/GADGET side support
	 */
	struct dummy_ep			ep [DUMMY_ENDPOINTS];
	int				address;
	struct usb_gadget		gadget;
	struct usb_gadget_driver	*driver;
	struct dummy_request		fifo_req;
	u8				fifo_buf [FIFO_SIZE];
	u16				devstatus;
197
	unsigned			udc_suspended:1;
198
	unsigned			pullup:1;
L
Linus Torvalds 已提交
199 200 201 202

	/*
	 * MASTER/HOST side support
	 */
203
	struct dummy_hcd		*hs_hcd;
204
	struct dummy_hcd		*ss_hcd;
L
Linus Torvalds 已提交
205 206
};

207
static inline struct dummy_hcd *hcd_to_dummy_hcd(struct usb_hcd *hcd)
L
Linus Torvalds 已提交
208
{
209
	return (struct dummy_hcd *) (hcd->hcd_priv);
L
Linus Torvalds 已提交
210 211
}

212
static inline struct usb_hcd *dummy_hcd_to_hcd(struct dummy_hcd *dum)
L
Linus Torvalds 已提交
213 214 215 216
{
	return container_of((void *) dum, struct usb_hcd, hcd_priv);
}

217
static inline struct device *dummy_dev(struct dummy_hcd *dum)
L
Linus Torvalds 已提交
218
{
219
	return dummy_hcd_to_hcd(dum)->self.controller;
L
Linus Torvalds 已提交
220 221
}

222 223 224 225 226
static inline struct device *udc_dev (struct dummy *dum)
{
	return dum->gadget.dev.parent;
}

L
Linus Torvalds 已提交
227 228 229 230 231
static inline struct dummy *ep_to_dummy (struct dummy_ep *ep)
{
	return container_of (ep->gadget, struct dummy, gadget);
}

232
static inline struct dummy_hcd *gadget_to_dummy_hcd(struct usb_gadget *gadget)
L
Linus Torvalds 已提交
233
{
234
	struct dummy *dum = container_of(gadget, struct dummy, gadget);
235 236 237 238
	if (dum->gadget.speed == USB_SPEED_SUPER)
		return dum->ss_hcd;
	else
		return dum->hs_hcd;
L
Linus Torvalds 已提交
239 240 241 242 243 244 245
}

static inline struct dummy *gadget_dev_to_dummy (struct device *dev)
{
	return container_of (dev, struct dummy, gadget.dev);
}

246
static struct dummy			the_controller;
L
Linus Torvalds 已提交
247 248 249

/*-------------------------------------------------------------------------*/

250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
/* SLAVE/GADGET SIDE UTILITY ROUTINES */

/* called with spinlock held */
static void nuke (struct dummy *dum, struct dummy_ep *ep)
{
	while (!list_empty (&ep->queue)) {
		struct dummy_request	*req;

		req = list_entry (ep->queue.next, struct dummy_request, queue);
		list_del_init (&req->queue);
		req->req.status = -ESHUTDOWN;

		spin_unlock (&dum->lock);
		req->req.complete (&ep->ep, &req->req);
		spin_lock (&dum->lock);
	}
}

/* caller must hold lock */
static void
stop_activity (struct dummy *dum)
{
	struct dummy_ep	*ep;

	/* prevent any more requests */
	dum->address = 0;

	/* The timer is left running so that outstanding URBs can fail */

	/* nuke any pending requests first, so driver i/o is quiesced */
	list_for_each_entry (ep, &dum->gadget.ep_list, ep.ep_list)
		nuke (dum, ep);

	/* driver now does any non-usb quiescing necessary */
}

286 287 288 289 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 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
/**
 * set_link_state_by_speed() - Sets the current state of the link according to
 *	the hcd speed
 * @dum_hcd: pointer to the dummy_hcd structure to update the link state for
 *
 * This function updates the port_status according to the link state and the
 * speed of the hcd.
 */
static void set_link_state_by_speed(struct dummy_hcd *dum_hcd)
{
	struct dummy *dum = dum_hcd->dum;

	if (dummy_hcd_to_hcd(dum_hcd)->speed == HCD_USB3) {
		if ((dum_hcd->port_status & USB_SS_PORT_STAT_POWER) == 0) {
			dum_hcd->port_status = 0;
		} else if (!dum->pullup || dum->udc_suspended) {
			/* UDC suspend must cause a disconnect */
			dum_hcd->port_status &= ~(USB_PORT_STAT_CONNECTION |
						USB_PORT_STAT_ENABLE);
			if ((dum_hcd->old_status &
			     USB_PORT_STAT_CONNECTION) != 0)
				dum_hcd->port_status |=
					(USB_PORT_STAT_C_CONNECTION << 16);
		} else {
			/* device is connected and not suspended */
			dum_hcd->port_status |= (USB_PORT_STAT_CONNECTION |
						 USB_PORT_STAT_SPEED_5GBPS) ;
			if ((dum_hcd->old_status &
			     USB_PORT_STAT_CONNECTION) == 0)
				dum_hcd->port_status |=
					(USB_PORT_STAT_C_CONNECTION << 16);
			if ((dum_hcd->port_status &
			     USB_PORT_STAT_ENABLE) == 1 &&
				(dum_hcd->port_status &
				 USB_SS_PORT_LS_U0) == 1 &&
				dum_hcd->rh_state != DUMMY_RH_SUSPENDED)
				dum_hcd->active = 1;
		}
	} else {
		if ((dum_hcd->port_status & USB_PORT_STAT_POWER) == 0) {
			dum_hcd->port_status = 0;
		} else if (!dum->pullup || dum->udc_suspended) {
			/* UDC suspend must cause a disconnect */
			dum_hcd->port_status &= ~(USB_PORT_STAT_CONNECTION |
						USB_PORT_STAT_ENABLE |
						USB_PORT_STAT_LOW_SPEED |
						USB_PORT_STAT_HIGH_SPEED |
						USB_PORT_STAT_SUSPEND);
			if ((dum_hcd->old_status &
			     USB_PORT_STAT_CONNECTION) != 0)
				dum_hcd->port_status |=
					(USB_PORT_STAT_C_CONNECTION << 16);
		} else {
			dum_hcd->port_status |= USB_PORT_STAT_CONNECTION;
			if ((dum_hcd->old_status &
			     USB_PORT_STAT_CONNECTION) == 0)
				dum_hcd->port_status |=
					(USB_PORT_STAT_C_CONNECTION << 16);
			if ((dum_hcd->port_status & USB_PORT_STAT_ENABLE) == 0)
				dum_hcd->port_status &= ~USB_PORT_STAT_SUSPEND;
			else if ((dum_hcd->port_status &
				  USB_PORT_STAT_SUSPEND) == 0 &&
					dum_hcd->rh_state != DUMMY_RH_SUSPENDED)
				dum_hcd->active = 1;
		}
	}
}

354
/* caller must hold lock */
355
static void set_link_state(struct dummy_hcd *dum_hcd)
356
{
357 358 359
	struct dummy *dum = dum_hcd->dum;

	dum_hcd->active = 0;
360 361 362 363 364 365 366 367
	if (dum->pullup)
		if ((dummy_hcd_to_hcd(dum_hcd)->speed == HCD_USB3 &&
		     dum->gadget.speed != USB_SPEED_SUPER) ||
		    (dummy_hcd_to_hcd(dum_hcd)->speed != HCD_USB3 &&
		     dum->gadget.speed == USB_SPEED_SUPER))
			return;

	set_link_state_by_speed(dum_hcd);
368

369 370 371
	if ((dum_hcd->port_status & USB_PORT_STAT_ENABLE) == 0 ||
	     dum_hcd->active)
		dum_hcd->resuming = 0;
372

373
	/* if !connected or reset */
374 375
	if ((dum_hcd->port_status & USB_PORT_STAT_CONNECTION) == 0 ||
			(dum_hcd->port_status & USB_PORT_STAT_RESET) != 0) {
376 377 378 379
		/*
		 * We're connected and not reset (reset occurred now),
		 * and driver attached - disconnect!
		 */
380
		if ((dum_hcd->old_status & USB_PORT_STAT_CONNECTION) != 0 &&
381 382 383 384 385 386
		    (dum_hcd->old_status & USB_PORT_STAT_RESET) == 0 &&
		    dum->driver) {
			stop_activity(dum);
			spin_unlock(&dum->lock);
			dum->driver->disconnect(&dum->gadget);
			spin_lock(&dum->lock);
387
		}
388 389
	} else if (dum_hcd->active != dum_hcd->old_active) {
		if (dum_hcd->old_active && dum->driver->suspend) {
390 391 392 393 394 395 396
			spin_unlock(&dum->lock);
			dum->driver->suspend(&dum->gadget);
			spin_lock(&dum->lock);
		} else if (!dum_hcd->old_active &&  dum->driver->resume) {
			spin_unlock(&dum->lock);
			dum->driver->resume(&dum->gadget);
			spin_lock(&dum->lock);
397 398 399
		}
	}

400 401
	dum_hcd->old_status = dum_hcd->port_status;
	dum_hcd->old_active = dum_hcd->active;
402 403 404 405
}

/*-------------------------------------------------------------------------*/

L
Linus Torvalds 已提交
406 407 408 409 410 411 412 413 414 415 416 417 418 419
/* SLAVE/GADGET SIDE DRIVER
 *
 * This only tracks gadget state.  All the work is done when the host
 * side tries some (emulated) i/o operation.  Real device controller
 * drivers would do real i/o using dma, fifos, irqs, timers, etc.
 */

#define is_enabled(dum) \
	(dum->port_status & USB_PORT_STAT_ENABLE)

static int
dummy_enable (struct usb_ep *_ep, const struct usb_endpoint_descriptor *desc)
{
	struct dummy		*dum;
420
	struct dummy_hcd	*dum_hcd;
L
Linus Torvalds 已提交
421 422 423 424 425 426 427 428 429
	struct dummy_ep		*ep;
	unsigned		max;
	int			retval;

	ep = usb_ep_to_dummy_ep (_ep);
	if (!_ep || !desc || ep->desc || _ep->name == ep0name
			|| desc->bDescriptorType != USB_DT_ENDPOINT)
		return -EINVAL;
	dum = ep_to_dummy (ep);
430 431
	if (!dum->driver)
		return -ESHUTDOWN;
432 433 434 435
	if (dum->gadget.speed == USB_SPEED_SUPER)
		dum_hcd = dum->ss_hcd;
	else
		dum_hcd = dum->hs_hcd;
436
	if (!is_enabled(dum_hcd))
L
Linus Torvalds 已提交
437
		return -ESHUTDOWN;
438 439 440 441

	/*
	 * For HS/FS devices only bits 0..10 of the wMaxPacketSize represent the
	 * maximum packet size.
442
	 * For SS devices the wMaxPacketSize is limited by 1024.
443 444
	 */
	max = le16_to_cpu(desc->wMaxPacketSize) & 0x7ff;
L
Linus Torvalds 已提交
445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461

	/* drivers must not request bad settings, since lower levels
	 * (hardware or its drivers) may not check.  some endpoints
	 * can't do iso, many have maxpacket limitations, etc.
	 *
	 * since this "hardware" driver is here to help debugging, we
	 * have some extra sanity checks.  (there could be more though,
	 * especially for "ep9out" style fixed function ones.)
	 */
	retval = -EINVAL;
	switch (desc->bmAttributes & 0x03) {
	case USB_ENDPOINT_XFER_BULK:
		if (strstr (ep->ep.name, "-iso")
				|| strstr (ep->ep.name, "-int")) {
			goto done;
		}
		switch (dum->gadget.speed) {
462 463 464 465
		case USB_SPEED_SUPER:
			if (max == 1024)
				break;
			goto done;
L
Linus Torvalds 已提交
466 467 468
		case USB_SPEED_HIGH:
			if (max == 512)
				break;
469 470 471
			goto done;
		case USB_SPEED_FULL:
			if (max == 8 || max == 16 || max == 32 || max == 64)
L
Linus Torvalds 已提交
472 473
				/* we'll fake any legal size */
				break;
474 475 476
			/* save a return statement */
		default:
			goto done;
L
Linus Torvalds 已提交
477 478 479 480 481 482 483
		}
		break;
	case USB_ENDPOINT_XFER_INT:
		if (strstr (ep->ep.name, "-iso")) /* bulk is ok */
			goto done;
		/* real hardware might not handle all packet sizes */
		switch (dum->gadget.speed) {
484
		case USB_SPEED_SUPER:
L
Linus Torvalds 已提交
485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504
		case USB_SPEED_HIGH:
			if (max <= 1024)
				break;
			/* save a return statement */
		case USB_SPEED_FULL:
			if (max <= 64)
				break;
			/* save a return statement */
		default:
			if (max <= 8)
				break;
			goto done;
		}
		break;
	case USB_ENDPOINT_XFER_ISOC:
		if (strstr (ep->ep.name, "-bulk")
				|| strstr (ep->ep.name, "-int"))
			goto done;
		/* real hardware might not handle all packet sizes */
		switch (dum->gadget.speed) {
505
		case USB_SPEED_SUPER:
L
Linus Torvalds 已提交
506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525
		case USB_SPEED_HIGH:
			if (max <= 1024)
				break;
			/* save a return statement */
		case USB_SPEED_FULL:
			if (max <= 1023)
				break;
			/* save a return statement */
		default:
			goto done;
		}
		break;
	default:
		/* few chips support control except on ep0 */
		goto done;
	}

	_ep->maxpacket = max;
	ep->desc = desc;

526
	dev_dbg (udc_dev(dum), "enabled %s (ep%d%s-%s) maxpacket %d\n",
L
Linus Torvalds 已提交
527 528 529 530 531
		_ep->name,
		desc->bEndpointAddress & 0x0f,
		(desc->bEndpointAddress & USB_DIR_IN) ? "in" : "out",
		({ char *val;
		 switch (desc->bmAttributes & 0x03) {
T
Tatyana Brokhman 已提交
532 533 534 535 536 537 538 539 540 541 542 543
		 case USB_ENDPOINT_XFER_BULK:
			 val = "bulk";
			 break;
		 case USB_ENDPOINT_XFER_ISOC:
			 val = "iso";
			 break;
		 case USB_ENDPOINT_XFER_INT:
			 val = "intr";
			 break;
		 default:
			 val = "ctrl";
			 break;
L
Linus Torvalds 已提交
544 545 546 547 548 549
		 }; val; }),
		max);

	/* at this point real hardware should be NAKing transfers
	 * to that endpoint, until a buffer is queued to it.
	 */
550
	ep->halted = ep->wedged = 0;
L
Linus Torvalds 已提交
551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573
	retval = 0;
done:
	return retval;
}

static int dummy_disable (struct usb_ep *_ep)
{
	struct dummy_ep		*ep;
	struct dummy		*dum;
	unsigned long		flags;
	int			retval;

	ep = usb_ep_to_dummy_ep (_ep);
	if (!_ep || !ep->desc || _ep->name == ep0name)
		return -EINVAL;
	dum = ep_to_dummy (ep);

	spin_lock_irqsave (&dum->lock, flags);
	ep->desc = NULL;
	retval = 0;
	nuke (dum, ep);
	spin_unlock_irqrestore (&dum->lock, flags);

574
	dev_dbg (udc_dev(dum), "disabled %s\n", _ep->name);
L
Linus Torvalds 已提交
575 576 577 578
	return retval;
}

static struct usb_request *
A
Al Viro 已提交
579
dummy_alloc_request (struct usb_ep *_ep, gfp_t mem_flags)
L
Linus Torvalds 已提交
580 581 582 583 584 585 586 587
{
	struct dummy_ep		*ep;
	struct dummy_request	*req;

	if (!_ep)
		return NULL;
	ep = usb_ep_to_dummy_ep (_ep);

588
	req = kzalloc(sizeof(*req), mem_flags);
L
Linus Torvalds 已提交
589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615
	if (!req)
		return NULL;
	INIT_LIST_HEAD (&req->queue);
	return &req->req;
}

static void
dummy_free_request (struct usb_ep *_ep, struct usb_request *_req)
{
	struct dummy_ep		*ep;
	struct dummy_request	*req;

	ep = usb_ep_to_dummy_ep (_ep);
	if (!ep || !_req || (!ep->desc && _ep->name != ep0name))
		return;

	req = usb_request_to_dummy_request (_req);
	WARN_ON (!list_empty (&req->queue));
	kfree (req);
}

static void
fifo_complete (struct usb_ep *ep, struct usb_request *req)
{
}

static int
616
dummy_queue (struct usb_ep *_ep, struct usb_request *_req,
A
Al Viro 已提交
617
		gfp_t mem_flags)
L
Linus Torvalds 已提交
618 619 620 621
{
	struct dummy_ep		*ep;
	struct dummy_request	*req;
	struct dummy		*dum;
622
	struct dummy_hcd	*dum_hcd;
L
Linus Torvalds 已提交
623 624 625 626 627 628 629 630 631 632 633
	unsigned long		flags;

	req = usb_request_to_dummy_request (_req);
	if (!_req || !list_empty (&req->queue) || !_req->complete)
		return -EINVAL;

	ep = usb_ep_to_dummy_ep (_ep);
	if (!_ep || (!ep->desc && _ep->name != ep0name))
		return -EINVAL;

	dum = ep_to_dummy (ep);
634 635 636 637
	if (dum->gadget.speed == USB_SPEED_SUPER)
		dum_hcd = dum->ss_hcd;
	else
		dum_hcd = dum->hs_hcd;
638
	if (!dum->driver || !is_enabled(dum_hcd))
L
Linus Torvalds 已提交
639 640 641
		return -ESHUTDOWN;

#if 0
642
	dev_dbg (udc_dev(dum), "ep %p queue req %p to %s, len %d buf %p\n",
L
Linus Torvalds 已提交
643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661
			ep, _req, _ep->name, _req->length, _req->buf);
#endif

	_req->status = -EINPROGRESS;
	_req->actual = 0;
	spin_lock_irqsave (&dum->lock, flags);

	/* implement an emulated single-request FIFO */
	if (ep->desc && (ep->desc->bEndpointAddress & USB_DIR_IN) &&
			list_empty (&dum->fifo_req.queue) &&
			list_empty (&ep->queue) &&
			_req->length <= FIFO_SIZE) {
		req = &dum->fifo_req;
		req->req = *_req;
		req->req.buf = dum->fifo_buf;
		memcpy (dum->fifo_buf, _req->buf, _req->length);
		req->req.context = dum;
		req->req.complete = fifo_complete;

662
		list_add_tail(&req->queue, &ep->queue);
L
Linus Torvalds 已提交
663 664 665 666 667
		spin_unlock (&dum->lock);
		_req->actual = _req->length;
		_req->status = 0;
		_req->complete (_ep, _req);
		spin_lock (&dum->lock);
668 669
	}  else
		list_add_tail(&req->queue, &ep->queue);
L
Linus Torvalds 已提交
670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693
	spin_unlock_irqrestore (&dum->lock, flags);

	/* real hardware would likely enable transfers here, in case
	 * it'd been left NAKing.
	 */
	return 0;
}

static int dummy_dequeue (struct usb_ep *_ep, struct usb_request *_req)
{
	struct dummy_ep		*ep;
	struct dummy		*dum;
	int			retval = -EINVAL;
	unsigned long		flags;
	struct dummy_request	*req = NULL;

	if (!_ep || !_req)
		return retval;
	ep = usb_ep_to_dummy_ep (_ep);
	dum = ep_to_dummy (ep);

	if (!dum->driver)
		return -ESHUTDOWN;

694 695
	local_irq_save (flags);
	spin_lock (&dum->lock);
L
Linus Torvalds 已提交
696 697 698 699 700 701 702 703
	list_for_each_entry (req, &ep->queue, queue) {
		if (&req->req == _req) {
			list_del_init (&req->queue);
			_req->status = -ECONNRESET;
			retval = 0;
			break;
		}
	}
704
	spin_unlock (&dum->lock);
L
Linus Torvalds 已提交
705 706

	if (retval == 0) {
707
		dev_dbg (udc_dev(dum),
L
Linus Torvalds 已提交
708 709 710 711
				"dequeued req %p from %s, len %d buf %p\n",
				req, _ep->name, _req->length, _req->buf);
		_req->complete (_ep, _req);
	}
712
	local_irq_restore (flags);
L
Linus Torvalds 已提交
713 714 715 716
	return retval;
}

static int
717
dummy_set_halt_and_wedge(struct usb_ep *_ep, int value, int wedged)
L
Linus Torvalds 已提交
718 719 720 721 722 723 724 725 726 727 728
{
	struct dummy_ep		*ep;
	struct dummy		*dum;

	if (!_ep)
		return -EINVAL;
	ep = usb_ep_to_dummy_ep (_ep);
	dum = ep_to_dummy (ep);
	if (!dum->driver)
		return -ESHUTDOWN;
	if (!value)
729
		ep->halted = ep->wedged = 0;
L
Linus Torvalds 已提交
730 731 732
	else if (ep->desc && (ep->desc->bEndpointAddress & USB_DIR_IN) &&
			!list_empty (&ep->queue))
		return -EAGAIN;
733
	else {
L
Linus Torvalds 已提交
734
		ep->halted = 1;
735 736 737
		if (wedged)
			ep->wedged = 1;
	}
L
Linus Torvalds 已提交
738 739 740 741
	/* FIXME clear emulated data toggle too */
	return 0;
}

742 743 744 745 746 747 748 749 750 751 752 753 754
static int
dummy_set_halt(struct usb_ep *_ep, int value)
{
	return dummy_set_halt_and_wedge(_ep, value, 0);
}

static int dummy_set_wedge(struct usb_ep *_ep)
{
	if (!_ep || _ep->name == ep0name)
		return -EINVAL;
	return dummy_set_halt_and_wedge(_ep, 1, 1);
}

L
Linus Torvalds 已提交
755 756 757 758 759 760 761 762 763 764 765
static const struct usb_ep_ops dummy_ep_ops = {
	.enable		= dummy_enable,
	.disable	= dummy_disable,

	.alloc_request	= dummy_alloc_request,
	.free_request	= dummy_free_request,

	.queue		= dummy_queue,
	.dequeue	= dummy_dequeue,

	.set_halt	= dummy_set_halt,
766
	.set_wedge	= dummy_set_wedge,
L
Linus Torvalds 已提交
767 768 769 770 771 772 773 774 775 776 777 778 779 780 781
};

/*-------------------------------------------------------------------------*/

/* there are both host and device side versions of this call ... */
static int dummy_g_get_frame (struct usb_gadget *_gadget)
{
	struct timeval	tv;

	do_gettimeofday (&tv);
	return tv.tv_usec / 1000;
}

static int dummy_wakeup (struct usb_gadget *_gadget)
{
782
	struct dummy_hcd *dum_hcd;
L
Linus Torvalds 已提交
783

784 785
	dum_hcd = gadget_to_dummy_hcd(_gadget);
	if (!(dum_hcd->dum->devstatus & ((1 << USB_DEVICE_B_HNP_ENABLE)
786
				| (1 << USB_DEVICE_REMOTE_WAKEUP))))
L
Linus Torvalds 已提交
787
		return -EINVAL;
788
	if ((dum_hcd->port_status & USB_PORT_STAT_CONNECTION) == 0)
789
		return -ENOLINK;
790 791
	if ((dum_hcd->port_status & USB_PORT_STAT_SUSPEND) == 0 &&
			 dum_hcd->rh_state != DUMMY_RH_SUSPENDED)
792 793 794
		return -EIO;

	/* FIXME: What if the root hub is suspended but the port isn't? */
L
Linus Torvalds 已提交
795 796

	/* hub notices our request, issues downstream resume, etc */
797 798 799
	dum_hcd->resuming = 1;
	dum_hcd->re_timeout = jiffies + msecs_to_jiffies(20);
	mod_timer(&dummy_hcd_to_hcd(dum_hcd)->rh_timer, dum_hcd->re_timeout);
L
Linus Torvalds 已提交
800 801 802 803 804 805 806
	return 0;
}

static int dummy_set_selfpowered (struct usb_gadget *_gadget, int value)
{
	struct dummy	*dum;

807
	dum = (gadget_to_dummy_hcd(_gadget))->dum;
L
Linus Torvalds 已提交
808 809 810 811 812 813 814
	if (value)
		dum->devstatus |= (1 << USB_DEVICE_SELF_POWERED);
	else
		dum->devstatus &= ~(1 << USB_DEVICE_SELF_POWERED);
	return 0;
}

815 816 817 818 819
static int dummy_pullup (struct usb_gadget *_gadget, int value)
{
	struct dummy	*dum;
	unsigned long	flags;

820
	dum = gadget_to_dummy_hcd(_gadget)->dum;
821 822
	spin_lock_irqsave (&dum->lock, flags);
	dum->pullup = (value != 0);
823 824
	set_link_state((dum->gadget.speed == USB_SPEED_SUPER ?
			dum->ss_hcd : dum->hs_hcd));
825
	spin_unlock_irqrestore (&dum->lock, flags);
826 827 828
	usb_hcd_poll_rh_status((dum->gadget.speed == USB_SPEED_SUPER ?
				dummy_hcd_to_hcd(dum->ss_hcd) :
				dummy_hcd_to_hcd(dum->hs_hcd)));
829 830 831
	return 0;
}

832 833 834 835
static int dummy_udc_start(struct usb_gadget_driver *driver,
		int (*bind)(struct usb_gadget *));
static int dummy_udc_stop(struct usb_gadget_driver *driver);

L
Linus Torvalds 已提交
836 837 838 839
static const struct usb_gadget_ops dummy_ops = {
	.get_frame	= dummy_g_get_frame,
	.wakeup		= dummy_wakeup,
	.set_selfpowered = dummy_set_selfpowered,
840
	.pullup		= dummy_pullup,
841 842
	.start		= dummy_udc_start,
	.stop		= dummy_udc_stop,
L
Linus Torvalds 已提交
843 844 845 846 847 848
};

/*-------------------------------------------------------------------------*/

/* "function" sysfs attribute */
static ssize_t
849
show_function (struct device *dev, struct device_attribute *attr, char *buf)
L
Linus Torvalds 已提交
850 851 852 853 854 855 856
{
	struct dummy	*dum = gadget_dev_to_dummy (dev);

	if (!dum->driver || !dum->driver->function)
		return 0;
	return scnprintf (buf, PAGE_SIZE, "%s\n", dum->driver->function);
}
857
static DEVICE_ATTR (function, S_IRUGO, show_function, NULL);
L
Linus Torvalds 已提交
858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874

/*-------------------------------------------------------------------------*/

/*
 * Driver registration/unregistration.
 *
 * This is basically hardware-specific; there's usually only one real USB
 * device (not host) controller since that's how USB devices are intended
 * to work.  So most implementations of these api calls will rely on the
 * fact that only one driver will ever bind to the hardware.  But curious
 * hardware can be built with discrete components, so the gadget API doesn't
 * require that assumption.
 *
 * For this emulator, it might be convenient to create a usb slave device
 * for each driver that registers:  just add to a big root hub.
 */

875
static int dummy_udc_start(struct usb_gadget_driver *driver,
876
		int (*bind)(struct usb_gadget *))
L
Linus Torvalds 已提交
877
{
878
	struct dummy	*dum = &the_controller;
L
Linus Torvalds 已提交
879 880 881 882 883 884
	int		retval, i;

	if (!dum)
		return -EINVAL;
	if (dum->driver)
		return -EBUSY;
885
	if (!bind || !driver->setup || driver->speed == USB_SPEED_UNKNOWN)
L
Linus Torvalds 已提交
886 887 888 889 890 891
		return -EINVAL;

	/*
	 * SLAVE side init ... the layer above hardware, which
	 * can't enumerate without help from the driver we're binding.
	 */
892

L
Linus Torvalds 已提交
893 894 895 896 897 898 899 900 901 902 903
	dum->devstatus = 0;

	INIT_LIST_HEAD (&dum->gadget.ep_list);
	for (i = 0; i < DUMMY_ENDPOINTS; i++) {
		struct dummy_ep	*ep = &dum->ep [i];

		if (!ep_name [i])
			break;
		ep->ep.name = ep_name [i];
		ep->ep.ops = &dummy_ep_ops;
		list_add_tail (&ep->ep.ep_list, &dum->gadget.ep_list);
904 905
		ep->halted = ep->wedged = ep->already_seen =
				ep->setup_stage = 0;
L
Linus Torvalds 已提交
906 907 908 909 910 911 912 913
		ep->ep.maxpacket = ~0;
		ep->last_io = jiffies;
		ep->gadget = &dum->gadget;
		ep->desc = NULL;
		INIT_LIST_HEAD (&ep->queue);
	}

	dum->gadget.ep0 = &dum->ep [0].ep;
914 915
	if (mod_data.is_super_speed)
		dum->gadget.speed = driver->speed;
916 917
	else if (mod_data.is_high_speed)
		dum->gadget.speed = min_t(u8, USB_SPEED_HIGH, driver->speed);
918
	else
919
		dum->gadget.speed = USB_SPEED_FULL;
920
	if (dum->gadget.speed < driver->speed)
921 922 923 924
		dev_dbg(udc_dev(dum), "This device can perform faster"
				" if you connect it to a %s port...\n",
			(driver->speed == USB_SPEED_SUPER ?
			 "SuperSpeed" : "HighSpeed"));
925 926 927 928 929 930 931

	if (dum->gadget.speed == USB_SPEED_SUPER) {
		for (i = 0; i < DUMMY_ENDPOINTS; i++)
			dum->ep[i].ep.max_streams = 0x10;
		dum->ep[0].ep.maxpacket = 9;
	} else
		dum->ep[0].ep.maxpacket = 64;
932 933 934 935 936 937 938 939

	if (dum->gadget.speed == USB_SPEED_SUPER)
		dum->gadget.is_otg =
			(dummy_hcd_to_hcd(dum->ss_hcd)->self.otg_port != 0);
	else
		dum->gadget.is_otg =
			(dummy_hcd_to_hcd(dum->hs_hcd)->self.otg_port != 0);

L
Linus Torvalds 已提交
940 941 942
	list_del_init (&dum->ep [0].ep.ep_list);
	INIT_LIST_HEAD(&dum->fifo_req.queue);

943
	driver->driver.bus = NULL;
L
Linus Torvalds 已提交
944 945
	dum->driver = driver;
	dum->gadget.dev.driver = &driver->driver;
946
	dev_dbg (udc_dev(dum), "binding gadget driver '%s'\n",
L
Linus Torvalds 已提交
947
			driver->driver.name);
948
	retval = bind(&dum->gadget);
949 950 951 952 953
	if (retval) {
		dum->driver = NULL;
		dum->gadget.dev.driver = NULL;
		return retval;
	}
L
Linus Torvalds 已提交
954

955 956
	/* khubd will enumerate this in a while */
	dummy_pullup(&dum->gadget, 1);
L
Linus Torvalds 已提交
957 958 959
	return 0;
}

960
static int dummy_udc_stop(struct usb_gadget_driver *driver)
L
Linus Torvalds 已提交
961
{
962
	struct dummy	*dum = &the_controller;
L
Linus Torvalds 已提交
963 964 965

	if (!dum)
		return -ENODEV;
966
	if (!driver || driver != dum->driver || !driver->unbind)
L
Linus Torvalds 已提交
967 968
		return -EINVAL;

969
	dev_dbg (udc_dev(dum), "unregister gadget driver '%s'\n",
L
Linus Torvalds 已提交
970 971
			driver->driver.name);

972
	dummy_pullup(&dum->gadget, 0);
L
Linus Torvalds 已提交
973 974

	driver->unbind (&dum->gadget);
975
	dum->gadget.dev.driver = NULL;
L
Linus Torvalds 已提交
976
	dum->driver = NULL;
977 978

	dummy_pullup(&dum->gadget, 0);
L
Linus Torvalds 已提交
979 980 981 982 983
	return 0;
}

#undef is_enabled

984 985 986
/* just declare this in any driver that really need it */
extern int net2280_set_fifo_mode (struct usb_gadget *gadget, int mode);

L
Linus Torvalds 已提交
987 988 989 990 991 992
int net2280_set_fifo_mode (struct usb_gadget *gadget, int mode)
{
	return -ENOSYS;
}
EXPORT_SYMBOL (net2280_set_fifo_mode);

993 994 995 996 997 998

/* The gadget structure is stored inside the hcd structure and will be
 * released along with it. */
static void
dummy_gadget_release (struct device *dev)
{
999
	return;
1000 1001
}

1002
static int dummy_udc_probe (struct platform_device *pdev)
1003
{
1004
	struct dummy	*dum = &the_controller;
1005 1006 1007 1008 1009 1010
	int		rc;

	dum->gadget.name = gadget_name;
	dum->gadget.ops = &dummy_ops;
	dum->gadget.is_dualspeed = 1;

1011
	dev_set_name(&dum->gadget.dev, "gadget");
1012
	dum->gadget.dev.parent = &pdev->dev;
1013 1014
	dum->gadget.dev.release = dummy_gadget_release;
	rc = device_register (&dum->gadget.dev);
1015 1016
	if (rc < 0) {
		put_device(&dum->gadget.dev);
1017
		return rc;
1018
	}
1019

1020 1021 1022 1023
	rc = usb_add_gadget_udc(&pdev->dev, &dum->gadget);
	if (rc < 0)
		goto err_udc;

1024 1025
	rc = device_create_file (&dum->gadget.dev, &dev_attr_function);
	if (rc < 0)
1026 1027 1028 1029 1030 1031 1032 1033
		goto err_dev;
	platform_set_drvdata(pdev, dum);
	return rc;

err_dev:
	usb_del_gadget_udc(&dum->gadget);
err_udc:
	device_unregister(&dum->gadget.dev);
1034 1035 1036
	return rc;
}

1037
static int dummy_udc_remove (struct platform_device *pdev)
1038
{
1039
	struct dummy	*dum = platform_get_drvdata (pdev);
1040

1041
	usb_del_gadget_udc(&dum->gadget);
1042
	platform_set_drvdata (pdev, NULL);
1043 1044 1045 1046 1047
	device_remove_file (&dum->gadget.dev, &dev_attr_function);
	device_unregister (&dum->gadget.dev);
	return 0;
}

1048
static int dummy_udc_suspend (struct platform_device *pdev, pm_message_t state)
1049
{
1050
	struct dummy	*dum = platform_get_drvdata(pdev);
1051

1052
	dev_dbg (&pdev->dev, "%s\n", __func__);
1053 1054
	spin_lock_irq (&dum->lock);
	dum->udc_suspended = 1;
1055 1056
	set_link_state((dum->gadget.speed == USB_SPEED_SUPER ?
			dum->ss_hcd : dum->hs_hcd));
1057 1058
	spin_unlock_irq (&dum->lock);

1059 1060 1061
	usb_hcd_poll_rh_status((dum->gadget.speed == USB_SPEED_SUPER ?
				dummy_hcd_to_hcd(dum->ss_hcd) :
				dummy_hcd_to_hcd(dum->hs_hcd)));
1062 1063 1064
	return 0;
}

1065
static int dummy_udc_resume (struct platform_device *pdev)
1066
{
1067
	struct dummy	*dum = platform_get_drvdata(pdev);
1068

1069
	dev_dbg (&pdev->dev, "%s\n", __func__);
1070 1071
	spin_lock_irq (&dum->lock);
	dum->udc_suspended = 0;
1072 1073
	set_link_state((dum->gadget.speed == USB_SPEED_SUPER ?
			dum->ss_hcd : dum->hs_hcd));
1074 1075
	spin_unlock_irq (&dum->lock);

1076 1077 1078
	usb_hcd_poll_rh_status((dum->gadget.speed == USB_SPEED_SUPER ?
				dummy_hcd_to_hcd(dum->ss_hcd) :
				dummy_hcd_to_hcd(dum->hs_hcd)));
1079 1080 1081
	return 0;
}

1082
static struct platform_driver dummy_udc_driver = {
1083 1084
	.probe		= dummy_udc_probe,
	.remove		= dummy_udc_remove,
1085 1086
	.suspend	= dummy_udc_suspend,
	.resume		= dummy_udc_resume,
1087 1088 1089 1090
	.driver		= {
		.name	= (char *) gadget_name,
		.owner	= THIS_MODULE,
	},
1091 1092
};

L
Linus Torvalds 已提交
1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109
/*-------------------------------------------------------------------------*/

/* MASTER/HOST SIDE DRIVER
 *
 * this uses the hcd framework to hook up to host side drivers.
 * its root hub will only have one device, otherwise it acts like
 * a normal host controller.
 *
 * when urbs are queued, they're just stuck on a list that we
 * scan in a timer callback.  that callback connects writes from
 * the host with reads from the device, and so on, based on the
 * usb 2.0 rules.
 */

static int dummy_urb_enqueue (
	struct usb_hcd			*hcd,
	struct urb			*urb,
A
Al Viro 已提交
1110
	gfp_t				mem_flags
L
Linus Torvalds 已提交
1111
) {
1112
	struct dummy_hcd *dum_hcd;
L
Linus Torvalds 已提交
1113 1114
	struct urbp	*urbp;
	unsigned long	flags;
1115
	int		rc;
L
Linus Torvalds 已提交
1116 1117 1118 1119 1120 1121 1122 1123 1124

	if (!urb->transfer_buffer && urb->transfer_buffer_length)
		return -EINVAL;

	urbp = kmalloc (sizeof *urbp, mem_flags);
	if (!urbp)
		return -ENOMEM;
	urbp->urb = urb;

1125 1126
	dum_hcd = hcd_to_dummy_hcd(hcd);
	spin_lock_irqsave(&dum_hcd->dum->lock, flags);
1127 1128 1129 1130 1131
	rc = usb_hcd_link_urb_to_ep(hcd, urb);
	if (rc) {
		kfree(urbp);
		goto done;
	}
L
Linus Torvalds 已提交
1132

1133 1134 1135 1136 1137
	if (!dum_hcd->udev) {
		dum_hcd->udev = urb->dev;
		usb_get_dev(dum_hcd->udev);
	} else if (unlikely(dum_hcd->udev != urb->dev))
		dev_err(dummy_dev(dum_hcd), "usb_device address has changed!\n");
L
Linus Torvalds 已提交
1138

1139
	list_add_tail(&urbp->urbp_list, &dum_hcd->urbp_list);
L
Linus Torvalds 已提交
1140 1141 1142 1143 1144
	urb->hcpriv = urbp;
	if (usb_pipetype (urb->pipe) == PIPE_CONTROL)
		urb->error_count = 1;		/* mark as a new urb */

	/* kick the scheduler, it'll do the rest */
1145 1146
	if (!timer_pending(&dum_hcd->timer))
		mod_timer(&dum_hcd->timer, jiffies + 1);
L
Linus Torvalds 已提交
1147

1148
 done:
1149
	spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
1150
	return rc;
L
Linus Torvalds 已提交
1151 1152
}

1153
static int dummy_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
L
Linus Torvalds 已提交
1154
{
1155
	struct dummy_hcd *dum_hcd;
1156
	unsigned long	flags;
1157
	int		rc;
1158 1159 1160

	/* giveback happens automatically in timer callback,
	 * so make sure the callback happens */
1161 1162
	dum_hcd = hcd_to_dummy_hcd(hcd);
	spin_lock_irqsave(&dum_hcd->dum->lock, flags);
1163 1164

	rc = usb_hcd_check_unlink_urb(hcd, urb, status);
1165 1166 1167
	if (!rc && dum_hcd->rh_state != DUMMY_RH_RUNNING &&
			!list_empty(&dum_hcd->urbp_list))
		mod_timer(&dum_hcd->timer, jiffies);
1168

1169
	spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
1170
	return rc;
L
Linus Torvalds 已提交
1171 1172 1173 1174
}

/* transfer up to a frame's worth; caller must own lock */
static int
1175 1176
transfer(struct dummy *dum, struct urb *urb, struct dummy_ep *ep, int limit,
		int *status)
L
Linus Torvalds 已提交
1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238
{
	struct dummy_request	*req;

top:
	/* if there's no request queued, the device is NAKing; return */
	list_for_each_entry (req, &ep->queue, queue) {
		unsigned	host_len, dev_len, len;
		int		is_short, to_host;
		int		rescan = 0;

		/* 1..N packets of ep->ep.maxpacket each ... the last one
		 * may be short (including zero length).
		 *
		 * writer can send a zlp explicitly (length 0) or implicitly
		 * (length mod maxpacket zero, and 'zero' flag); they always
		 * terminate reads.
		 */
		host_len = urb->transfer_buffer_length - urb->actual_length;
		dev_len = req->req.length - req->req.actual;
		len = min (host_len, dev_len);

		/* FIXME update emulated data toggle too */

		to_host = usb_pipein (urb->pipe);
		if (unlikely (len == 0))
			is_short = 1;
		else {
			char		*ubuf, *rbuf;

			/* not enough bandwidth left? */
			if (limit < ep->ep.maxpacket && limit < len)
				break;
			len = min (len, (unsigned) limit);
			if (len == 0)
				break;

			/* use an extra pass for the final short packet */
			if (len > ep->ep.maxpacket) {
				rescan = 1;
				len -= (len % ep->ep.maxpacket);
			}
			is_short = (len % ep->ep.maxpacket) != 0;

			/* else transfer packet(s) */
			ubuf = urb->transfer_buffer + urb->actual_length;
			rbuf = req->req.buf + req->req.actual;
			if (to_host)
				memcpy (ubuf, rbuf, len);
			else
				memcpy (rbuf, ubuf, len);
			ep->last_io = jiffies;

			limit -= len;
			urb->actual_length += len;
			req->req.actual += len;
		}

		/* short packets terminate, maybe with overflow/underflow.
		 * it's only really an error to write too much.
		 *
		 * partially filling a buffer optionally blocks queue advances
		 * (so completion handlers can clean up the queue) but we don't
A
Alan Stern 已提交
1239
		 * need to emulate such data-in-flight.
L
Linus Torvalds 已提交
1240 1241 1242 1243
		 */
		if (is_short) {
			if (host_len == dev_len) {
				req->req.status = 0;
1244
				*status = 0;
L
Linus Torvalds 已提交
1245 1246 1247
			} else if (to_host) {
				req->req.status = 0;
				if (dev_len > host_len)
1248
					*status = -EOVERFLOW;
L
Linus Torvalds 已提交
1249
				else
1250
					*status = 0;
L
Linus Torvalds 已提交
1251
			} else if (!to_host) {
1252
				*status = 0;
L
Linus Torvalds 已提交
1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265
				if (host_len > dev_len)
					req->req.status = -EOVERFLOW;
				else
					req->req.status = 0;
			}

		/* many requests terminate without a short packet */
		} else {
			if (req->req.length == req->req.actual
					&& !req->req.zero)
				req->req.status = 0;
			if (urb->transfer_buffer_length == urb->actual_length
					&& !(urb->transfer_flags
1266 1267
						& URB_ZERO_PACKET))
				*status = 0;
L
Linus Torvalds 已提交
1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282
		}

		/* device side completion --> continuable */
		if (req->req.status != -EINPROGRESS) {
			list_del_init (&req->queue);

			spin_unlock (&dum->lock);
			req->req.complete (&ep->ep, &req->req);
			spin_lock (&dum->lock);

			/* requests might have been unlinked... */
			rescan = 1;
		}

		/* host side completion --> terminate */
1283
		if (*status != -EINPROGRESS)
L
Linus Torvalds 已提交
1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305
			break;

		/* rescan to continue with any other queued i/o */
		if (rescan)
			goto top;
	}
	return limit;
}

static int periodic_bytes (struct dummy *dum, struct dummy_ep *ep)
{
	int	limit = ep->ep.maxpacket;

	if (dum->gadget.speed == USB_SPEED_HIGH) {
		int	tmp;

		/* high bandwidth mode */
		tmp = le16_to_cpu(ep->desc->wMaxPacketSize);
		tmp = (tmp >> 11) & 0x03;
		tmp *= 8 /* applies to entire frame */;
		limit += limit * tmp;
	}
1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320
	if (dum->gadget.speed == USB_SPEED_SUPER) {
		switch (ep->desc->bmAttributes & 0x03) {
		case USB_ENDPOINT_XFER_ISOC:
			/* Sec. 4.4.8.2 USB3.0 Spec */
			limit = 3 * 16 * 1024 * 8;
			break;
		case USB_ENDPOINT_XFER_INT:
			/* Sec. 4.4.7.2 USB3.0 Spec */
			limit = 3 * 1024 * 8;
			break;
		case USB_ENDPOINT_XFER_BULK:
		default:
			break;
		}
	}
L
Linus Torvalds 已提交
1321 1322 1323
	return limit;
}

1324
#define is_active(dum_hcd)	((dum_hcd->port_status & \
L
Linus Torvalds 已提交
1325 1326 1327 1328 1329 1330 1331 1332
		(USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE | \
			USB_PORT_STAT_SUSPEND)) \
		== (USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE))

static struct dummy_ep *find_endpoint (struct dummy *dum, u8 address)
{
	int		i;

1333 1334
	if (!is_active((dum->gadget.speed == USB_SPEED_SUPER ?
			dum->ss_hcd : dum->hs_hcd)))
L
Linus Torvalds 已提交
1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357
		return NULL;
	if ((address & ~USB_DIR_IN) == 0)
		return &dum->ep [0];
	for (i = 1; i < DUMMY_ENDPOINTS; i++) {
		struct dummy_ep	*ep = &dum->ep [i];

		if (!ep->desc)
			continue;
		if (ep->desc->bEndpointAddress == address)
			return ep;
	}
	return NULL;
}

#undef is_active

#define Dev_Request	(USB_TYPE_STANDARD | USB_RECIP_DEVICE)
#define Dev_InRequest	(Dev_Request | USB_DIR_IN)
#define Intf_Request	(USB_TYPE_STANDARD | USB_RECIP_INTERFACE)
#define Intf_InRequest	(Intf_Request | USB_DIR_IN)
#define Ep_Request	(USB_TYPE_STANDARD | USB_RECIP_ENDPOINT)
#define Ep_InRequest	(Ep_Request | USB_DIR_IN)

1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370

/**
 * handle_control_request() - handles all control transfers
 * @dum: pointer to dummy (the_controller)
 * @urb: the urb request to handle
 * @setup: pointer to the setup data for a USB device control
 *	 request
 * @status: pointer to request handling status
 *
 * Return 0 - if the request was handled
 *	  1 - if the request wasn't handles
 *	  error code on error
 */
1371
static int handle_control_request(struct dummy_hcd *dum_hcd, struct urb *urb,
1372 1373 1374 1375
				  struct usb_ctrlrequest *setup,
				  int *status)
{
	struct dummy_ep		*ep2;
1376
	struct dummy		*dum = dum_hcd->dum;
1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407
	int			ret_val = 1;
	unsigned	w_index;
	unsigned	w_value;

	w_index = le16_to_cpu(setup->wIndex);
	w_value = le16_to_cpu(setup->wValue);
	switch (setup->bRequest) {
	case USB_REQ_SET_ADDRESS:
		if (setup->bRequestType != Dev_Request)
			break;
		dum->address = w_value;
		*status = 0;
		dev_dbg(udc_dev(dum), "set_address = %d\n",
				w_value);
		ret_val = 0;
		break;
	case USB_REQ_SET_FEATURE:
		if (setup->bRequestType == Dev_Request) {
			ret_val = 0;
			switch (w_value) {
			case USB_DEVICE_REMOTE_WAKEUP:
				break;
			case USB_DEVICE_B_HNP_ENABLE:
				dum->gadget.b_hnp_enable = 1;
				break;
			case USB_DEVICE_A_HNP_SUPPORT:
				dum->gadget.a_hnp_support = 1;
				break;
			case USB_DEVICE_A_ALT_HNP_SUPPORT:
				dum->gadget.a_alt_hnp_support = 1;
				break;
1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428
			case USB_DEVICE_U1_ENABLE:
				if (dummy_hcd_to_hcd(dum_hcd)->speed ==
				    HCD_USB3)
					w_value = USB_DEV_STAT_U1_ENABLED;
				else
					ret_val = -EOPNOTSUPP;
				break;
			case USB_DEVICE_U2_ENABLE:
				if (dummy_hcd_to_hcd(dum_hcd)->speed ==
				    HCD_USB3)
					w_value = USB_DEV_STAT_U2_ENABLED;
				else
					ret_val = -EOPNOTSUPP;
				break;
			case USB_DEVICE_LTM_ENABLE:
				if (dummy_hcd_to_hcd(dum_hcd)->speed ==
				    HCD_USB3)
					w_value = USB_DEV_STAT_LTM_ENABLED;
				else
					ret_val = -EOPNOTSUPP;
				break;
1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454
			default:
				ret_val = -EOPNOTSUPP;
			}
			if (ret_val == 0) {
				dum->devstatus |= (1 << w_value);
				*status = 0;
			}
		} else if (setup->bRequestType == Ep_Request) {
			/* endpoint halt */
			ep2 = find_endpoint(dum, w_index);
			if (!ep2 || ep2->ep.name == ep0name) {
				ret_val = -EOPNOTSUPP;
				break;
			}
			ep2->halted = 1;
			ret_val = 0;
			*status = 0;
		}
		break;
	case USB_REQ_CLEAR_FEATURE:
		if (setup->bRequestType == Dev_Request) {
			ret_val = 0;
			switch (w_value) {
			case USB_DEVICE_REMOTE_WAKEUP:
				w_value = USB_DEVICE_REMOTE_WAKEUP;
				break;
1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475
			case USB_DEVICE_U1_ENABLE:
				if (dummy_hcd_to_hcd(dum_hcd)->speed ==
				    HCD_USB3)
					w_value = USB_DEV_STAT_U1_ENABLED;
				else
					ret_val = -EOPNOTSUPP;
				break;
			case USB_DEVICE_U2_ENABLE:
				if (dummy_hcd_to_hcd(dum_hcd)->speed ==
				    HCD_USB3)
					w_value = USB_DEV_STAT_U2_ENABLED;
				else
					ret_val = -EOPNOTSUPP;
				break;
			case USB_DEVICE_LTM_ENABLE:
				if (dummy_hcd_to_hcd(dum_hcd)->speed ==
				    HCD_USB3)
					w_value = USB_DEV_STAT_LTM_ENABLED;
				else
					ret_val = -EOPNOTSUPP;
				break;
1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533
			default:
				ret_val = -EOPNOTSUPP;
				break;
			}
			if (ret_val == 0) {
				dum->devstatus &= ~(1 << w_value);
				*status = 0;
			}
		} else if (setup->bRequestType == Ep_Request) {
			/* endpoint halt */
			ep2 = find_endpoint(dum, w_index);
			if (!ep2) {
				ret_val = -EOPNOTSUPP;
				break;
			}
			if (!ep2->wedged)
				ep2->halted = 0;
			ret_val = 0;
			*status = 0;
		}
		break;
	case USB_REQ_GET_STATUS:
		if (setup->bRequestType == Dev_InRequest
				|| setup->bRequestType == Intf_InRequest
				|| setup->bRequestType == Ep_InRequest) {
			char *buf;
			/*
			 * device: remote wakeup, selfpowered
			 * interface: nothing
			 * endpoint: halt
			 */
			buf = (char *)urb->transfer_buffer;
			if (urb->transfer_buffer_length > 0) {
				if (setup->bRequestType == Ep_InRequest) {
					ep2 = find_endpoint(dum, w_index);
					if (!ep2) {
						ret_val = -EOPNOTSUPP;
						break;
					}
					buf[0] = ep2->halted;
				} else if (setup->bRequestType ==
					   Dev_InRequest) {
					buf[0] = (u8)dum->devstatus;
				} else
					buf[0] = 0;
			}
			if (urb->transfer_buffer_length > 1)
				buf[1] = 0;
			urb->actual_length = min_t(u32, 2,
				urb->transfer_buffer_length);
			ret_val = 0;
			*status = 0;
		}
		break;
	}
	return ret_val;
}

L
Linus Torvalds 已提交
1534 1535 1536
/* drive both sides of the transfers; looks like irq handlers to
 * both drivers except the callbacks aren't in_irq().
 */
1537
static void dummy_timer(unsigned long _dum_hcd)
L
Linus Torvalds 已提交
1538
{
1539 1540
	struct dummy_hcd	*dum_hcd = (struct dummy_hcd *) _dum_hcd;
	struct dummy		*dum = dum_hcd->dum;
L
Linus Torvalds 已提交
1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556
	struct urbp		*urbp, *tmp;
	unsigned long		flags;
	int			limit, total;
	int			i;

	/* simplistic model for one frame's bandwidth */
	switch (dum->gadget.speed) {
	case USB_SPEED_LOW:
		total = 8/*bytes*/ * 12/*packets*/;
		break;
	case USB_SPEED_FULL:
		total = 64/*bytes*/ * 19/*packets*/;
		break;
	case USB_SPEED_HIGH:
		total = 512/*bytes*/ * 13/*packets*/ * 8/*uframes*/;
		break;
1557 1558 1559 1560
	case USB_SPEED_SUPER:
		/* Bus speed is 500000 bytes/ms, so use a little less */
		total = 490000;
		break;
L
Linus Torvalds 已提交
1561
	default:
1562
		dev_err(dummy_dev(dum_hcd), "bogus device speed\n");
L
Linus Torvalds 已提交
1563 1564 1565 1566 1567 1568 1569 1570
		return;
	}

	/* FIXME if HZ != 1000 this will probably misbehave ... */

	/* look at each urb queued by the host side driver */
	spin_lock_irqsave (&dum->lock, flags);

1571 1572
	if (!dum_hcd->udev) {
		dev_err(dummy_dev(dum_hcd),
L
Linus Torvalds 已提交
1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584
				"timer fired with no URBs pending?\n");
		spin_unlock_irqrestore (&dum->lock, flags);
		return;
	}

	for (i = 0; i < DUMMY_ENDPOINTS; i++) {
		if (!ep_name [i])
			break;
		dum->ep [i].already_seen = 0;
	}

restart:
1585
	list_for_each_entry_safe(urbp, tmp, &dum_hcd->urbp_list, urbp_list) {
L
Linus Torvalds 已提交
1586 1587 1588 1589 1590
		struct urb		*urb;
		struct dummy_request	*req;
		u8			address;
		struct dummy_ep		*ep = NULL;
		int			type;
1591
		int			status = -EINPROGRESS;
L
Linus Torvalds 已提交
1592 1593

		urb = urbp->urb;
A
Alan Stern 已提交
1594
		if (urb->unlinked)
L
Linus Torvalds 已提交
1595
			goto return_urb;
1596
		else if (dum_hcd->rh_state != DUMMY_RH_RUNNING)
1597
			continue;
L
Linus Torvalds 已提交
1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613
		type = usb_pipetype (urb->pipe);

		/* used up this frame's non-periodic bandwidth?
		 * FIXME there's infinite bandwidth for control and
		 * periodic transfers ... unrealistic.
		 */
		if (total <= 0 && type == PIPE_BULK)
			continue;

		/* find the gadget's ep for this request (if configured) */
		address = usb_pipeendpoint (urb->pipe);
		if (usb_pipein (urb->pipe))
			address |= USB_DIR_IN;
		ep = find_endpoint(dum, address);
		if (!ep) {
			/* set_configuration() disagreement */
1614
			dev_dbg(dummy_dev(dum_hcd),
L
Linus Torvalds 已提交
1615 1616
				"no ep configured for urb %p\n",
				urb);
1617
			status = -EPROTO;
L
Linus Torvalds 已提交
1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629
			goto return_urb;
		}

		if (ep->already_seen)
			continue;
		ep->already_seen = 1;
		if (ep == &dum->ep [0] && urb->error_count) {
			ep->setup_stage = 1;	/* a new urb */
			urb->error_count = 0;
		}
		if (ep->halted && !ep->setup_stage) {
			/* NOTE: must not be iso! */
1630
			dev_dbg(dummy_dev(dum_hcd), "ep %s halted, urb %p\n",
L
Linus Torvalds 已提交
1631
					ep->ep.name, urb);
1632
			status = -EPIPE;
L
Linus Torvalds 已提交
1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646
			goto return_urb;
		}
		/* FIXME make sure both ends agree on maxpacket */

		/* handle control requests */
		if (ep == &dum->ep [0] && ep->setup_stage) {
			struct usb_ctrlrequest		setup;
			int				value = 1;

			setup = *(struct usb_ctrlrequest*) urb->setup_packet;
			/* paranoia, in case of stale queued data */
			list_for_each_entry (req, &ep->queue, queue) {
				list_del_init (&req->queue);
				req->req.status = -EOVERFLOW;
1647
				dev_dbg (udc_dev(dum), "stale req = %p\n",
L
Linus Torvalds 已提交
1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664
						req);

				spin_unlock (&dum->lock);
				req->req.complete (&ep->ep, &req->req);
				spin_lock (&dum->lock);
				ep->already_seen = 0;
				goto restart;
			}

			/* gadget driver never sees set_address or operations
			 * on standard feature flags.  some hardware doesn't
			 * even expose them.
			 */
			ep->last_io = jiffies;
			ep->setup_stage = 0;
			ep->halted = 0;

1665
			value = handle_control_request(dum_hcd, urb, &setup,
1666
						       &status);
L
Linus Torvalds 已提交
1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686

			/* gadget driver handles all other requests.  block
			 * until setup() returns; no reentrancy issues etc.
			 */
			if (value > 0) {
				spin_unlock (&dum->lock);
				value = dum->driver->setup (&dum->gadget,
						&setup);
				spin_lock (&dum->lock);

				if (value >= 0) {
					/* no delays (max 64KB data stage) */
					limit = 64*1024;
					goto treat_control_like_bulk;
				}
				/* error, see below */
			}

			if (value < 0) {
				if (value != -EOPNOTSUPP)
1687
					dev_dbg (udc_dev(dum),
L
Linus Torvalds 已提交
1688 1689
						"setup --> %d\n",
						value);
1690
				status = -EPIPE;
L
Linus Torvalds 已提交
1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706
				urb->actual_length = 0;
			}

			goto return_urb;
		}

		/* non-control requests */
		limit = total;
		switch (usb_pipetype (urb->pipe)) {
		case PIPE_ISOCHRONOUS:
			/* FIXME is it urb->interval since the last xfer?
			 * use urb->iso_frame_desc[i].
			 * complete whether or not ep has requests queued.
			 * report random errors, to debug drivers.
			 */
			limit = max (limit, periodic_bytes (dum, ep));
1707
			status = -ENOSYS;
L
Linus Torvalds 已提交
1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720
			break;

		case PIPE_INTERRUPT:
			/* FIXME is it urb->interval since the last xfer?
			 * this almost certainly polls too fast.
			 */
			limit = max (limit, periodic_bytes (dum, ep));
			/* FALLTHROUGH */

		// case PIPE_BULK:  case PIPE_CONTROL:
		default:
		treat_control_like_bulk:
			ep->last_io = jiffies;
1721
			total = transfer(dum, urb, ep, limit, &status);
L
Linus Torvalds 已提交
1722 1723 1724 1725
			break;
		}

		/* incomplete transfer? */
1726
		if (status == -EINPROGRESS)
L
Linus Torvalds 已提交
1727 1728 1729 1730 1731 1732 1733 1734
			continue;

return_urb:
		list_del (&urbp->urbp_list);
		kfree (urbp);
		if (ep)
			ep->already_seen = ep->setup_stage = 0;

1735
		usb_hcd_unlink_urb_from_ep(dummy_hcd_to_hcd(dum_hcd), urb);
L
Linus Torvalds 已提交
1736
		spin_unlock (&dum->lock);
1737
		usb_hcd_giveback_urb(dummy_hcd_to_hcd(dum_hcd), urb, status);
L
Linus Torvalds 已提交
1738 1739 1740 1741 1742
		spin_lock (&dum->lock);

		goto restart;
	}

1743 1744 1745 1746
	if (list_empty(&dum_hcd->urbp_list)) {
		usb_put_dev(dum_hcd->udev);
		dum_hcd->udev = NULL;
	} else if (dum_hcd->rh_state == DUMMY_RH_RUNNING) {
1747
		/* want a 1 msec delay here */
1748
		mod_timer(&dum_hcd->timer, jiffies + msecs_to_jiffies(1));
L
Linus Torvalds 已提交
1749 1750 1751 1752 1753 1754 1755 1756
	}

	spin_unlock_irqrestore (&dum->lock, flags);
}

/*-------------------------------------------------------------------------*/

#define PORT_C_MASK \
1757 1758 1759 1760 1761
	((USB_PORT_STAT_C_CONNECTION \
	| USB_PORT_STAT_C_ENABLE \
	| USB_PORT_STAT_C_SUSPEND \
	| USB_PORT_STAT_C_OVERCURRENT \
	| USB_PORT_STAT_C_RESET) << 16)
L
Linus Torvalds 已提交
1762 1763 1764

static int dummy_hub_status (struct usb_hcd *hcd, char *buf)
{
1765
	struct dummy_hcd	*dum_hcd;
L
Linus Torvalds 已提交
1766
	unsigned long		flags;
1767
	int			retval = 0;
L
Linus Torvalds 已提交
1768

1769
	dum_hcd = hcd_to_dummy_hcd(hcd);
L
Linus Torvalds 已提交
1770

1771
	spin_lock_irqsave(&dum_hcd->dum->lock, flags);
1772
	if (!HCD_HW_ACCESSIBLE(hcd))
1773
		goto done;
1774

1775 1776 1777 1778
	if (dum_hcd->resuming && time_after_eq(jiffies, dum_hcd->re_timeout)) {
		dum_hcd->port_status |= (USB_PORT_STAT_C_SUSPEND << 16);
		dum_hcd->port_status &= ~USB_PORT_STAT_SUSPEND;
		set_link_state(dum_hcd);
1779 1780
	}

1781
	if ((dum_hcd->port_status & PORT_C_MASK) != 0) {
L
Linus Torvalds 已提交
1782
		*buf = (1 << 1);
1783 1784
		dev_dbg(dummy_dev(dum_hcd), "port status 0x%08x has changes\n",
				dum_hcd->port_status);
L
Linus Torvalds 已提交
1785
		retval = 1;
1786
		if (dum_hcd->rh_state == DUMMY_RH_SUSPENDED)
1787
			usb_hcd_resume_root_hub (hcd);
L
Linus Torvalds 已提交
1788
	}
1789
done:
1790
	spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
L
Linus Torvalds 已提交
1791 1792 1793
	return retval;
}

1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805
static inline void
ss_hub_descriptor(struct usb_hub_descriptor *desc)
{
	memset(desc, 0, sizeof *desc);
	desc->bDescriptorType = 0x2a;
	desc->bDescLength = 12;
	desc->wHubCharacteristics = cpu_to_le16(0x0001);
	desc->bNbrPorts = 1;
	desc->u.ss.bHubHdrDecLat = 0x04; /* Worst case: 0.4 micro sec*/
	desc->u.ss.DeviceRemovable = 0xffff;
}

L
Linus Torvalds 已提交
1806 1807 1808 1809 1810 1811
static inline void
hub_descriptor (struct usb_hub_descriptor *desc)
{
	memset (desc, 0, sizeof *desc);
	desc->bDescriptorType = 0x29;
	desc->bDescLength = 9;
A
Al Viro 已提交
1812
	desc->wHubCharacteristics = cpu_to_le16(0x0001);
L
Linus Torvalds 已提交
1813
	desc->bNbrPorts = 1;
J
John Youn 已提交
1814 1815
	desc->u.hs.DeviceRemovable[0] = 0xff;
	desc->u.hs.DeviceRemovable[1] = 0xff;
L
Linus Torvalds 已提交
1816 1817 1818 1819 1820 1821 1822 1823 1824 1825
}

static int dummy_hub_control (
	struct usb_hcd	*hcd,
	u16		typeReq,
	u16		wValue,
	u16		wIndex,
	char		*buf,
	u16		wLength
) {
1826
	struct dummy_hcd *dum_hcd;
L
Linus Torvalds 已提交
1827 1828 1829
	int		retval = 0;
	unsigned long	flags;

1830
	if (!HCD_HW_ACCESSIBLE(hcd))
1831 1832
		return -ETIMEDOUT;

1833 1834 1835
	dum_hcd = hcd_to_dummy_hcd(hcd);

	spin_lock_irqsave(&dum_hcd->dum->lock, flags);
L
Linus Torvalds 已提交
1836 1837 1838 1839 1840 1841
	switch (typeReq) {
	case ClearHubFeature:
		break;
	case ClearPortFeature:
		switch (wValue) {
		case USB_PORT_FEAT_SUSPEND:
1842 1843 1844 1845 1846 1847
			if (hcd->speed == HCD_USB3) {
				dev_dbg(dummy_dev(dum_hcd),
					 "USB_PORT_FEAT_SUSPEND req not "
					 "supported for USB 3.0 roothub\n");
				goto error;
			}
1848
			if (dum_hcd->port_status & USB_PORT_STAT_SUSPEND) {
L
Linus Torvalds 已提交
1849
				/* 20msec resume signaling */
1850 1851
				dum_hcd->resuming = 1;
				dum_hcd->re_timeout = jiffies +
1852
						msecs_to_jiffies(20);
L
Linus Torvalds 已提交
1853 1854 1855
			}
			break;
		case USB_PORT_FEAT_POWER:
1856 1857 1858 1859 1860 1861 1862 1863 1864
			if (hcd->speed == HCD_USB3) {
				if (dum_hcd->port_status & USB_PORT_STAT_POWER)
					dev_dbg(dummy_dev(dum_hcd),
						"power-off\n");
			} else
				if (dum_hcd->port_status &
							USB_SS_PORT_STAT_POWER)
					dev_dbg(dummy_dev(dum_hcd),
						"power-off\n");
1865
			/* FALLS THROUGH */
L
Linus Torvalds 已提交
1866
		default:
1867 1868
			dum_hcd->port_status &= ~(1 << wValue);
			set_link_state(dum_hcd);
L
Linus Torvalds 已提交
1869 1870 1871
		}
		break;
	case GetHubDescriptor:
1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883
		if (hcd->speed == HCD_USB3 &&
				(wLength < USB_DT_SS_HUB_SIZE ||
				 wValue != (USB_DT_SS_HUB << 8))) {
			dev_dbg(dummy_dev(dum_hcd),
				"Wrong hub descriptor type for "
				"USB 3.0 roothub.\n");
			goto error;
		}
		if (hcd->speed == HCD_USB3)
			ss_hub_descriptor((struct usb_hub_descriptor *) buf);
		else
			hub_descriptor((struct usb_hub_descriptor *) buf);
L
Linus Torvalds 已提交
1884 1885
		break;
	case GetHubStatus:
1886
		*(__le32 *) buf = cpu_to_le32 (0);
L
Linus Torvalds 已提交
1887 1888 1889 1890 1891 1892 1893 1894
		break;
	case GetPortStatus:
		if (wIndex != 1)
			retval = -EPIPE;

		/* whoever resets or resumes must GetPortStatus to
		 * complete it!!
		 */
1895 1896 1897 1898
		if (dum_hcd->resuming &&
				time_after_eq(jiffies, dum_hcd->re_timeout)) {
			dum_hcd->port_status |= (USB_PORT_STAT_C_SUSPEND << 16);
			dum_hcd->port_status &= ~USB_PORT_STAT_SUSPEND;
L
Linus Torvalds 已提交
1899
		}
1900 1901 1902 1903 1904 1905
		if ((dum_hcd->port_status & USB_PORT_STAT_RESET) != 0 &&
				time_after_eq(jiffies, dum_hcd->re_timeout)) {
			dum_hcd->port_status |= (USB_PORT_STAT_C_RESET << 16);
			dum_hcd->port_status &= ~USB_PORT_STAT_RESET;
			if (dum_hcd->dum->pullup) {
				dum_hcd->port_status |= USB_PORT_STAT_ENABLE;
1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923

				if (hcd->speed < HCD_USB3) {
					switch (dum_hcd->dum->gadget.speed) {
					case USB_SPEED_HIGH:
						dum_hcd->port_status |=
						      USB_PORT_STAT_HIGH_SPEED;
						break;
					case USB_SPEED_LOW:
						dum_hcd->dum->gadget.ep0->
							maxpacket = 8;
						dum_hcd->port_status |=
							USB_PORT_STAT_LOW_SPEED;
						break;
					default:
						dum_hcd->dum->gadget.speed =
							USB_SPEED_FULL;
						break;
					}
L
Linus Torvalds 已提交
1924 1925 1926
				}
			}
		}
1927 1928 1929
		set_link_state(dum_hcd);
		((__le16 *) buf)[0] = cpu_to_le16 (dum_hcd->port_status);
		((__le16 *) buf)[1] = cpu_to_le16 (dum_hcd->port_status >> 16);
L
Linus Torvalds 已提交
1930 1931 1932 1933 1934 1935
		break;
	case SetHubFeature:
		retval = -EPIPE;
		break;
	case SetPortFeature:
		switch (wValue) {
1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957
		case USB_PORT_FEAT_LINK_STATE:
			if (hcd->speed != HCD_USB3) {
				dev_dbg(dummy_dev(dum_hcd),
					 "USB_PORT_FEAT_LINK_STATE req not "
					 "supported for USB 2.0 roothub\n");
				goto error;
			}
			/*
			 * Since this is dummy we don't have an actual link so
			 * there is nothing to do for the SET_LINK_STATE cmd
			 */
			break;
		case USB_PORT_FEAT_U1_TIMEOUT:
		case USB_PORT_FEAT_U2_TIMEOUT:
			/* TODO: add suspend/resume support! */
			if (hcd->speed != HCD_USB3) {
				dev_dbg(dummy_dev(dum_hcd),
					 "USB_PORT_FEAT_U1/2_TIMEOUT req not "
					 "supported for USB 2.0 roothub\n");
				goto error;
			}
			break;
L
Linus Torvalds 已提交
1958
		case USB_PORT_FEAT_SUSPEND:
1959 1960 1961 1962 1963 1964 1965
			/* Applicable only for USB2.0 hub */
			if (hcd->speed == HCD_USB3) {
				dev_dbg(dummy_dev(dum_hcd),
					 "USB_PORT_FEAT_SUSPEND req not "
					 "supported for USB 3.0 roothub\n");
				goto error;
			}
1966 1967
			if (dum_hcd->active) {
				dum_hcd->port_status |= USB_PORT_STAT_SUSPEND;
1968 1969 1970 1971

				/* HNP would happen here; for now we
				 * assume b_bus_req is always true.
				 */
1972
				set_link_state(dum_hcd);
1973
				if (((1 << USB_DEVICE_B_HNP_ENABLE)
1974 1975
						& dum_hcd->dum->devstatus) != 0)
					dev_dbg(dummy_dev(dum_hcd),
1976
							"no HNP yet!\n");
L
Linus Torvalds 已提交
1977 1978
			}
			break;
1979
		case USB_PORT_FEAT_POWER:
1980 1981 1982 1983
			if (hcd->speed == HCD_USB3)
				dum_hcd->port_status |= USB_SS_PORT_STAT_POWER;
			else
				dum_hcd->port_status |= USB_PORT_STAT_POWER;
1984
			set_link_state(dum_hcd);
1985
			break;
1986 1987 1988 1989 1990 1991 1992 1993 1994
		case USB_PORT_FEAT_BH_PORT_RESET:
			/* Applicable only for USB3.0 hub */
			if (hcd->speed != HCD_USB3) {
				dev_dbg(dummy_dev(dum_hcd),
					 "USB_PORT_FEAT_BH_PORT_RESET req not "
					 "supported for USB 2.0 roothub\n");
				goto error;
			}
			/* FALLS THROUGH */
L
Linus Torvalds 已提交
1995
		case USB_PORT_FEAT_RESET:
1996
			/* if it's already enabled, disable */
1997 1998 1999 2000 2001 2002 2003 2004
			if (hcd->speed == HCD_USB3) {
				dum_hcd->port_status = 0;
				dum_hcd->port_status =
					(USB_SS_PORT_STAT_POWER |
					 USB_PORT_STAT_CONNECTION |
					 USB_PORT_STAT_RESET);
			} else
				dum_hcd->port_status &= ~(USB_PORT_STAT_ENABLE
2005 2006
					| USB_PORT_STAT_LOW_SPEED
					| USB_PORT_STAT_HIGH_SPEED);
2007 2008 2009 2010 2011 2012
			/*
			 * We want to reset device status. All but the
			 * Self powered feature
			 */
			dum_hcd->dum->devstatus &=
				(1 << USB_DEVICE_SELF_POWERED);
2013 2014 2015 2016
			/*
			 * FIXME USB3.0: what is the correct reset signaling
			 * interval? Is it still 50msec as for HS?
			 */
2017
			dum_hcd->re_timeout = jiffies + msecs_to_jiffies(50);
2018
			/* FALLS THROUGH */
L
Linus Torvalds 已提交
2019
		default:
2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049
			if (hcd->speed == HCD_USB3) {
				if ((dum_hcd->port_status &
				     USB_SS_PORT_STAT_POWER) != 0) {
					dum_hcd->port_status |= (1 << wValue);
					set_link_state(dum_hcd);
				}
			} else
				if ((dum_hcd->port_status &
				     USB_PORT_STAT_POWER) != 0) {
					dum_hcd->port_status |= (1 << wValue);
					set_link_state(dum_hcd);
				}
		}
		break;
	case GetPortErrorCount:
		if (hcd->speed != HCD_USB3) {
			dev_dbg(dummy_dev(dum_hcd),
				 "GetPortErrorCount req not "
				 "supported for USB 2.0 roothub\n");
			goto error;
		}
		/* We'll always return 0 since this is a dummy hub */
		*(__le32 *) buf = cpu_to_le32(0);
		break;
	case SetHubDepth:
		if (hcd->speed != HCD_USB3) {
			dev_dbg(dummy_dev(dum_hcd),
				 "SetHubDepth req not supported for "
				 "USB 2.0 roothub\n");
			goto error;
L
Linus Torvalds 已提交
2050 2051 2052
		}
		break;
	default:
2053
		dev_dbg(dummy_dev(dum_hcd),
L
Linus Torvalds 已提交
2054 2055
			"hub control req%04x v%04x i%04x l%d\n",
			typeReq, wValue, wIndex, wLength);
2056
error:
L
Linus Torvalds 已提交
2057 2058 2059
		/* "protocol stall" on error */
		retval = -EPIPE;
	}
2060
	spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
2061

2062
	if ((dum_hcd->port_status & PORT_C_MASK) != 0)
2063
		usb_hcd_poll_rh_status (hcd);
L
Linus Torvalds 已提交
2064 2065 2066
	return retval;
}

2067
static int dummy_bus_suspend (struct usb_hcd *hcd)
2068
{
2069
	struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
2070

2071
	dev_dbg (&hcd->self.root_hub->dev, "%s\n", __func__);
2072

2073 2074 2075
	spin_lock_irq(&dum_hcd->dum->lock);
	dum_hcd->rh_state = DUMMY_RH_SUSPENDED;
	set_link_state(dum_hcd);
2076
	hcd->state = HC_STATE_SUSPENDED;
2077
	spin_unlock_irq(&dum_hcd->dum->lock);
2078 2079 2080
	return 0;
}

2081
static int dummy_bus_resume (struct usb_hcd *hcd)
2082
{
2083
	struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
2084 2085
	int rc = 0;

2086
	dev_dbg (&hcd->self.root_hub->dev, "%s\n", __func__);
2087

2088
	spin_lock_irq(&dum_hcd->dum->lock);
2089
	if (!HCD_HW_ACCESSIBLE(hcd)) {
2090
		rc = -ESHUTDOWN;
2091
	} else {
2092 2093 2094 2095
		dum_hcd->rh_state = DUMMY_RH_RUNNING;
		set_link_state(dum_hcd);
		if (!list_empty(&dum_hcd->urbp_list))
			mod_timer(&dum_hcd->timer, jiffies);
2096 2097
		hcd->state = HC_STATE_RUNNING;
	}
2098
	spin_unlock_irq(&dum_hcd->dum->lock);
2099
	return rc;
2100
}
L
Linus Torvalds 已提交
2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113

/*-------------------------------------------------------------------------*/

static inline ssize_t
show_urb (char *buf, size_t size, struct urb *urb)
{
	int ep = usb_pipeendpoint (urb->pipe);

	return snprintf (buf, size,
		"urb/%p %s ep%d%s%s len %d/%d\n",
		urb,
		({ char *s;
		 switch (urb->dev->speed) {
T
Tatyana Brokhman 已提交
2114 2115 2116 2117 2118 2119 2120 2121 2122
		 case USB_SPEED_LOW:
			s = "ls";
			break;
		 case USB_SPEED_FULL:
			s = "fs";
			break;
		 case USB_SPEED_HIGH:
			s = "hs";
			break;
2123 2124 2125
		 case USB_SPEED_SUPER:
			s = "ss";
			break;
T
Tatyana Brokhman 已提交
2126 2127 2128
		 default:
			s = "?";
			break;
L
Linus Torvalds 已提交
2129 2130 2131 2132
		 }; s; }),
		ep, ep ? (usb_pipein (urb->pipe) ? "in" : "out") : "",
		({ char *s; \
		 switch (usb_pipetype (urb->pipe)) { \
T
Tatyana Brokhman 已提交
2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144
		 case PIPE_CONTROL: \
			s = ""; \
			break; \
		 case PIPE_BULK: \
			s = "-bulk"; \
			break; \
		 case PIPE_INTERRUPT: \
			s = "-int"; \
			break; \
		 default: \
			s = "-iso"; \
			break; \
L
Linus Torvalds 已提交
2145 2146 2147 2148 2149
		}; s;}),
		urb->actual_length, urb->transfer_buffer_length);
}

static ssize_t
2150
show_urbs (struct device *dev, struct device_attribute *attr, char *buf)
L
Linus Torvalds 已提交
2151 2152
{
	struct usb_hcd		*hcd = dev_get_drvdata (dev);
2153
	struct dummy_hcd	*dum_hcd = hcd_to_dummy_hcd(hcd);
L
Linus Torvalds 已提交
2154 2155 2156 2157
	struct urbp		*urbp;
	size_t			size = 0;
	unsigned long		flags;

2158 2159
	spin_lock_irqsave(&dum_hcd->dum->lock, flags);
	list_for_each_entry(urbp, &dum_hcd->urbp_list, urbp_list) {
L
Linus Torvalds 已提交
2160 2161 2162 2163 2164 2165
		size_t		temp;

		temp = show_urb (buf, PAGE_SIZE - size, urbp->urb);
		buf += temp;
		size += temp;
	}
2166
	spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
L
Linus Torvalds 已提交
2167 2168 2169 2170 2171

	return size;
}
static DEVICE_ATTR (urbs, S_IRUGO, show_urbs, NULL);

2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190
static int dummy_start_ss(struct dummy_hcd *dum_hcd)
{
	init_timer(&dum_hcd->timer);
	dum_hcd->timer.function = dummy_timer;
	dum_hcd->timer.data = (unsigned long)dum_hcd;
	dum_hcd->rh_state = DUMMY_RH_RUNNING;
	INIT_LIST_HEAD(&dum_hcd->urbp_list);
	dummy_hcd_to_hcd(dum_hcd)->power_budget = POWER_BUDGET;
	dummy_hcd_to_hcd(dum_hcd)->state = HC_STATE_RUNNING;
	dummy_hcd_to_hcd(dum_hcd)->uses_new_polling = 1;
#ifdef CONFIG_USB_OTG
	dummy_hcd_to_hcd(dum_hcd)->self.otg_port = 1;
#endif
	return 0;

	/* FIXME 'urbs' should be a per-device thing, maybe in usbcore */
	return device_create_file(dummy_dev(dum_hcd), &dev_attr_urbs);
}

2191
static int dummy_start(struct usb_hcd *hcd)
L
Linus Torvalds 已提交
2192
{
2193
	struct dummy_hcd	*dum_hcd = hcd_to_dummy_hcd(hcd);
L
Linus Torvalds 已提交
2194 2195 2196 2197 2198 2199

	/*
	 * MASTER side init ... we emulate a root hub that'll only ever
	 * talk to one device (the slave side).  Also appears in sysfs,
	 * just like more familiar pci-based HCDs.
	 */
2200 2201 2202
	if (!usb_hcd_is_primary_hcd(hcd))
		return dummy_start_ss(dum_hcd);

2203 2204 2205 2206 2207
	spin_lock_init(&dum_hcd->dum->lock);
	init_timer(&dum_hcd->timer);
	dum_hcd->timer.function = dummy_timer;
	dum_hcd->timer.data = (unsigned long)dum_hcd;
	dum_hcd->rh_state = DUMMY_RH_RUNNING;
L
Linus Torvalds 已提交
2208

2209
	INIT_LIST_HEAD(&dum_hcd->urbp_list);
L
Linus Torvalds 已提交
2210

2211
	hcd->power_budget = POWER_BUDGET;
L
Linus Torvalds 已提交
2212
	hcd->state = HC_STATE_RUNNING;
2213
	hcd->uses_new_polling = 1;
L
Linus Torvalds 已提交
2214

2215 2216 2217 2218
#ifdef CONFIG_USB_OTG
	hcd->self.otg_port = 1;
#endif

L
Linus Torvalds 已提交
2219
	/* FIXME 'urbs' should be a per-device thing, maybe in usbcore */
2220
	return device_create_file(dummy_dev(dum_hcd), &dev_attr_urbs);
L
Linus Torvalds 已提交
2221 2222 2223 2224 2225 2226
}

static void dummy_stop (struct usb_hcd *hcd)
{
	struct dummy		*dum;

2227 2228 2229 2230
	dum = (hcd_to_dummy_hcd(hcd))->dum;
	device_remove_file(dummy_dev(hcd_to_dummy_hcd(hcd)), &dev_attr_urbs);
	usb_gadget_unregister_driver(dum->driver);
	dev_info(dummy_dev(hcd_to_dummy_hcd(hcd)), "stopped\n");
L
Linus Torvalds 已提交
2231 2232 2233 2234 2235 2236 2237 2238 2239
}

/*-------------------------------------------------------------------------*/

static int dummy_h_get_frame (struct usb_hcd *hcd)
{
	return dummy_g_get_frame (NULL);
}

2240 2241 2242 2243 2244
static int dummy_setup(struct usb_hcd *hcd)
{
	if (usb_hcd_is_primary_hcd(hcd)) {
		the_controller.hs_hcd = hcd_to_dummy_hcd(hcd);
		the_controller.hs_hcd->dum = &the_controller;
2245 2246 2247 2248 2249
		/*
		 * Mark the first roothub as being USB 2.0.
		 * The USB 3.0 roothub will be registered later by
		 * dummy_hcd_probe()
		 */
2250 2251
		hcd->speed = HCD_USB2;
		hcd->self.root_hub->speed = USB_SPEED_HIGH;
2252 2253 2254 2255 2256
	} else {
		the_controller.ss_hcd = hcd_to_dummy_hcd(hcd);
		the_controller.ss_hcd->dum = &the_controller;
		hcd->speed = HCD_USB3;
		hcd->self.root_hub->speed = USB_SPEED_SUPER;
2257 2258 2259 2260
	}
	return 0;
}

2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285
/* Change a group of bulk endpoints to support multiple stream IDs */
int dummy_alloc_streams(struct usb_hcd *hcd, struct usb_device *udev,
	struct usb_host_endpoint **eps, unsigned int num_eps,
	unsigned int num_streams, gfp_t mem_flags)
{
	if (hcd->speed != HCD_USB3)
		dev_dbg(dummy_dev(hcd_to_dummy_hcd(hcd)),
			"%s() - ERROR! Not supported for USB2.0 roothub\n",
			__func__);
	return 0;
}

/* Reverts a group of bulk endpoints back to not using stream IDs. */
int dummy_free_streams(struct usb_hcd *hcd, struct usb_device *udev,
	struct usb_host_endpoint **eps, unsigned int num_eps,
	gfp_t mem_flags)
{
	if (hcd->speed != HCD_USB3)
		dev_dbg(dummy_dev(hcd_to_dummy_hcd(hcd)),
			"%s() - ERROR! Not supported for USB2.0 roothub\n",
			__func__);
	return 0;
}

static struct hc_driver dummy_hcd = {
L
Linus Torvalds 已提交
2286 2287
	.description =		(char *) driver_name,
	.product_desc =		"Dummy host controller",
2288
	.hcd_priv_size =	sizeof(struct dummy_hcd),
L
Linus Torvalds 已提交
2289

2290
	.flags =		HCD_USB3 | HCD_SHARED,
L
Linus Torvalds 已提交
2291

2292
	.reset =		dummy_setup,
L
Linus Torvalds 已提交
2293 2294 2295 2296 2297 2298 2299 2300 2301 2302
	.start =		dummy_start,
	.stop =			dummy_stop,

	.urb_enqueue = 		dummy_urb_enqueue,
	.urb_dequeue = 		dummy_urb_dequeue,

	.get_frame_number = 	dummy_h_get_frame,

	.hub_status_data = 	dummy_hub_status,
	.hub_control = 		dummy_hub_control,
2303 2304
	.bus_suspend =		dummy_bus_suspend,
	.bus_resume =		dummy_bus_resume,
2305 2306 2307

	.alloc_streams =	dummy_alloc_streams,
	.free_streams =		dummy_free_streams,
L
Linus Torvalds 已提交
2308 2309
};

2310
static int dummy_hcd_probe(struct platform_device *pdev)
L
Linus Torvalds 已提交
2311
{
2312
	struct usb_hcd		*hs_hcd;
2313
	struct usb_hcd		*ss_hcd;
L
Linus Torvalds 已提交
2314 2315
	int			retval;

2316
	dev_info(&pdev->dev, "%s, driver " DRIVER_VERSION "\n", driver_desc);
L
Linus Torvalds 已提交
2317

2318 2319
	if (!mod_data.is_super_speed)
		dummy_hcd.flags = HCD_USB2;
2320 2321
	hs_hcd = usb_create_hcd(&dummy_hcd, &pdev->dev, dev_name(&pdev->dev));
	if (!hs_hcd)
L
Linus Torvalds 已提交
2322
		return -ENOMEM;
2323
	hs_hcd->has_tt = 1;
L
Linus Torvalds 已提交
2324

2325
	retval = usb_add_hcd(hs_hcd, 0, 0);
L
Linus Torvalds 已提交
2326
	if (retval != 0) {
2327
		usb_put_hcd(hs_hcd);
2328
		return retval;
L
Linus Torvalds 已提交
2329
	}
2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349

	if (mod_data.is_super_speed) {
		ss_hcd = usb_create_shared_hcd(&dummy_hcd, &pdev->dev,
					dev_name(&pdev->dev), hs_hcd);
		if (!ss_hcd) {
			retval = -ENOMEM;
			goto dealloc_usb2_hcd;
		}

		retval = usb_add_hcd(ss_hcd, 0, 0);
		if (retval)
			goto put_usb3_hcd;
	}
	return 0;

put_usb3_hcd:
	usb_put_hcd(ss_hcd);
dealloc_usb2_hcd:
	usb_put_hcd(hs_hcd);
	the_controller.hs_hcd = the_controller.ss_hcd = NULL;
L
Linus Torvalds 已提交
2350 2351 2352
	return retval;
}

2353
static int dummy_hcd_remove(struct platform_device *pdev)
L
Linus Torvalds 已提交
2354
{
2355 2356 2357
	struct dummy		*dum;

	dum = (hcd_to_dummy_hcd(platform_get_drvdata(pdev)))->dum;
2358 2359 2360 2361 2362 2363

	if (dum->ss_hcd) {
		usb_remove_hcd(dummy_hcd_to_hcd(dum->ss_hcd));
		usb_put_hcd(dummy_hcd_to_hcd(dum->ss_hcd));
	}

2364 2365
	usb_remove_hcd(dummy_hcd_to_hcd(dum->hs_hcd));
	usb_put_hcd(dummy_hcd_to_hcd(dum->hs_hcd));
2366

2367
	the_controller.hs_hcd = NULL;
2368
	the_controller.ss_hcd = NULL;
L
Linus Torvalds 已提交
2369

2370
	return 0;
L
Linus Torvalds 已提交
2371 2372
}

2373
static int dummy_hcd_suspend (struct platform_device *pdev, pm_message_t state)
2374 2375
{
	struct usb_hcd		*hcd;
2376
	struct dummy_hcd	*dum_hcd;
2377
	int			rc = 0;
2378

2379
	dev_dbg (&pdev->dev, "%s\n", __func__);
2380

2381
	hcd = platform_get_drvdata (pdev);
2382 2383
	dum_hcd = hcd_to_dummy_hcd(hcd);
	if (dum_hcd->rh_state == DUMMY_RH_RUNNING) {
2384 2385 2386 2387 2388
		dev_warn(&pdev->dev, "Root hub isn't suspended!\n");
		rc = -EBUSY;
	} else
		clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
	return rc;
2389 2390
}

2391
static int dummy_hcd_resume (struct platform_device *pdev)
2392 2393 2394
{
	struct usb_hcd		*hcd;

2395
	dev_dbg (&pdev->dev, "%s\n", __func__);
2396

2397 2398
	hcd = platform_get_drvdata (pdev);
	set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
2399 2400 2401 2402
	usb_hcd_poll_rh_status (hcd);
	return 0;
}

2403
static struct platform_driver dummy_hcd_driver = {
2404 2405
	.probe		= dummy_hcd_probe,
	.remove		= dummy_hcd_remove,
2406 2407
	.suspend	= dummy_hcd_suspend,
	.resume		= dummy_hcd_resume,
2408 2409 2410 2411
	.driver		= {
		.name	= (char *) driver_name,
		.owner	= THIS_MODULE,
	},
2412
};
L
Linus Torvalds 已提交
2413

2414
/*-------------------------------------------------------------------------*/
L
Linus Torvalds 已提交
2415

2416 2417
static struct platform_device *the_udc_pdev;
static struct platform_device *the_hcd_pdev;
L
Linus Torvalds 已提交
2418 2419 2420

static int __init init (void)
{
2421
	int	retval = -ENOMEM;
L
Linus Torvalds 已提交
2422 2423 2424

	if (usb_disabled ())
		return -ENODEV;
2425

2426 2427 2428
	if (!mod_data.is_high_speed && mod_data.is_super_speed)
		return -EINVAL;

2429 2430
	the_hcd_pdev = platform_device_alloc(driver_name, -1);
	if (!the_hcd_pdev)
L
Linus Torvalds 已提交
2431
		return retval;
2432 2433 2434
	the_udc_pdev = platform_device_alloc(gadget_name, -1);
	if (!the_udc_pdev)
		goto err_alloc_udc;
2435

2436 2437 2438 2439
	retval = platform_driver_register(&dummy_hcd_driver);
	if (retval < 0)
		goto err_register_hcd_driver;
	retval = platform_driver_register(&dummy_udc_driver);
2440 2441 2442
	if (retval < 0)
		goto err_register_udc_driver;

2443
	retval = platform_device_add(the_hcd_pdev);
2444
	if (retval < 0)
2445
		goto err_add_hcd;
2446 2447
	if (!the_controller.hs_hcd ||
	    (!the_controller.ss_hcd && mod_data.is_super_speed)) {
2448 2449 2450 2451 2452 2453 2454
		/*
		 * The hcd was added successfully but its probe function failed
		 * for some reason.
		 */
		retval = -EINVAL;
		goto err_add_udc;
	}
2455
	retval = platform_device_add(the_udc_pdev);
2456
	if (retval < 0)
2457
		goto err_add_udc;
2458 2459 2460 2461 2462 2463 2464 2465
	if (!platform_get_drvdata(the_udc_pdev)) {
		/*
		 * The udc was added successfully but its probe function failed
		 * for some reason.
		 */
		retval = -EINVAL;
		goto err_probe_udc;
	}
2466 2467
	return retval;

2468 2469
err_probe_udc:
	platform_device_del(the_udc_pdev);
2470 2471 2472 2473
err_add_udc:
	platform_device_del(the_hcd_pdev);
err_add_hcd:
	platform_driver_unregister(&dummy_udc_driver);
2474
err_register_udc_driver:
2475 2476 2477 2478 2479
	platform_driver_unregister(&dummy_hcd_driver);
err_register_hcd_driver:
	platform_device_put(the_udc_pdev);
err_alloc_udc:
	platform_device_put(the_hcd_pdev);
L
Linus Torvalds 已提交
2480 2481 2482 2483 2484 2485
	return retval;
}
module_init (init);

static void __exit cleanup (void)
{
2486 2487 2488 2489
	platform_device_unregister(the_udc_pdev);
	platform_device_unregister(the_hcd_pdev);
	platform_driver_unregister(&dummy_udc_driver);
	platform_driver_unregister(&dummy_hcd_driver);
L
Linus Torvalds 已提交
2490 2491
}
module_exit (cleanup);