PageRenderTime 270ms CodeModel.GetById 89ms app.highlight 152ms RepoModel.GetById 1ms app.codeStats 1ms

/drivers/usb/host/ehci-hub.c

https://bitbucket.org/cyanogenmod/android_kernel_asus_tf300t
C | 1091 lines | 749 code | 138 blank | 204 comment | 169 complexity | 9eb1158a1535498b825a368d2985f4c4 MD5 | raw file
Possible License(s): LGPL-2.0, AGPL-1.0, GPL-2.0
   1/*
   2 * Copyright (C) 2001-2004 by David Brownell
   3 *
   4 * This program is free software; you can redistribute it and/or modify it
   5 * under the terms of the GNU General Public License as published by the
   6 * Free Software Foundation; either version 2 of the License, or (at your
   7 * option) any later version.
   8 *
   9 * This program is distributed in the hope that it will be useful, but
  10 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  11 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12 * for more details.
  13 *
  14 * You should have received a copy of the GNU General Public License
  15 * along with this program; if not, write to the Free Software Foundation,
  16 * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17 */
  18
  19/* this file is part of ehci-hcd.c */
  20
  21/*-------------------------------------------------------------------------*/
  22
  23/*
  24 * EHCI Root Hub ... the nonsharable stuff
  25 *
  26 * Registers don't need cpu_to_le32, that happens transparently
  27 */
  28
  29/*-------------------------------------------------------------------------*/
  30
  31#define	PORT_WAKE_BITS	(PORT_WKOC_E|PORT_WKDISC_E|PORT_WKCONN_E)
  32
  33#ifdef	CONFIG_PM
  34
  35static int ehci_hub_control(
  36	struct usb_hcd	*hcd,
  37	u16		typeReq,
  38	u16		wValue,
  39	u16		wIndex,
  40	char		*buf,
  41	u16		wLength
  42);
  43
  44/* After a power loss, ports that were owned by the companion must be
  45 * reset so that the companion can still own them.
  46 */
  47static void ehci_handover_companion_ports(struct ehci_hcd *ehci)
  48{
  49	u32 __iomem	*reg;
  50	u32		status;
  51	int		port;
  52	__le32		buf;
  53	struct usb_hcd	*hcd = ehci_to_hcd(ehci);
  54
  55	if (!ehci->owned_ports)
  56		return;
  57
  58	/* Give the connections some time to appear */
  59	msleep(20);
  60
  61	port = HCS_N_PORTS(ehci->hcs_params);
  62	while (port--) {
  63		if (test_bit(port, &ehci->owned_ports)) {
  64			reg = &ehci->regs->port_status[port];
  65			status = ehci_readl(ehci, reg) & ~PORT_RWC_BITS;
  66
  67			/* Port already owned by companion? */
  68			if (status & PORT_OWNER)
  69				clear_bit(port, &ehci->owned_ports);
  70			else if (test_bit(port, &ehci->companion_ports))
  71				ehci_writel(ehci, status & ~PORT_PE, reg);
  72			else
  73				ehci_hub_control(hcd, SetPortFeature,
  74						USB_PORT_FEAT_RESET, port + 1,
  75						NULL, 0);
  76		}
  77	}
  78
  79	if (!ehci->owned_ports)
  80		return;
  81	msleep(90);		/* Wait for resets to complete */
  82
  83	port = HCS_N_PORTS(ehci->hcs_params);
  84	while (port--) {
  85		if (test_bit(port, &ehci->owned_ports)) {
  86			ehci_hub_control(hcd, GetPortStatus,
  87					0, port + 1,
  88					(char *) &buf, sizeof(buf));
  89
  90			/* The companion should now own the port,
  91			 * but if something went wrong the port must not
  92			 * remain enabled.
  93			 */
  94			reg = &ehci->regs->port_status[port];
  95			status = ehci_readl(ehci, reg) & ~PORT_RWC_BITS;
  96			if (status & PORT_OWNER)
  97				ehci_writel(ehci, status | PORT_CSC, reg);
  98			else {
  99				ehci_dbg(ehci, "failed handover port %d: %x\n",
 100						port + 1, status);
 101				ehci_writel(ehci, status & ~PORT_PE, reg);
 102			}
 103		}
 104	}
 105
 106	ehci->owned_ports = 0;
 107}
 108
 109static int ehci_port_change(struct ehci_hcd *ehci)
 110{
 111	int i = HCS_N_PORTS(ehci->hcs_params);
 112
 113	/* First check if the controller indicates a change event */
 114
 115	if (ehci_readl(ehci, &ehci->regs->status) & STS_PCD)
 116		return 1;
 117
 118	/*
 119	 * Not all controllers appear to update this while going from D3 to D0,
 120	 * so check the individual port status registers as well
 121	 */
 122
 123	while (i--)
 124		if (ehci_readl(ehci, &ehci->regs->port_status[i]) & PORT_CSC)
 125			return 1;
 126
 127	return 0;
 128}
 129
 130static __maybe_unused void ehci_adjust_port_wakeup_flags(struct ehci_hcd *ehci,
 131		bool suspending, bool do_wakeup)
 132{
 133	int		port;
 134	u32		temp;
 135	unsigned long	flags;
 136
 137	/* If remote wakeup is enabled for the root hub but disabled
 138	 * for the controller, we must adjust all the port wakeup flags
 139	 * when the controller is suspended or resumed.  In all other
 140	 * cases they don't need to be changed.
 141	 */
 142	if (!ehci_to_hcd(ehci)->self.root_hub->do_remote_wakeup || do_wakeup)
 143		return;
 144
 145	spin_lock_irqsave(&ehci->lock, flags);
 146
 147	/* clear phy low-power mode before changing wakeup flags */
 148	if (ehci->has_hostpc && !ehci->broken_hostpc_phcd) {
 149		port = HCS_N_PORTS(ehci->hcs_params);
 150		while (port--) {
 151			u32 __iomem	*hostpc_reg;
 152
 153			hostpc_reg = (u32 __iomem *)((u8 *) ehci->regs
 154					+ HOSTPC0 + 4 * port);
 155			temp = ehci_readl(ehci, hostpc_reg);
 156			ehci_writel(ehci, temp & ~HOSTPC_PHCD, hostpc_reg);
 157		}
 158		spin_unlock_irqrestore(&ehci->lock, flags);
 159		msleep(5);
 160		spin_lock_irqsave(&ehci->lock, flags);
 161	}
 162
 163	port = HCS_N_PORTS(ehci->hcs_params);
 164	while (port--) {
 165		u32 __iomem	*reg = &ehci->regs->port_status[port];
 166		u32		t1 = ehci_readl(ehci, reg) & ~PORT_RWC_BITS;
 167		u32		t2 = t1 & ~PORT_WAKE_BITS;
 168
 169		/* If we are suspending the controller, clear the flags.
 170		 * If we are resuming the controller, set the wakeup flags.
 171		 */
 172		if (!suspending) {
 173			if (t1 & PORT_CONNECT)
 174				t2 |= PORT_WKOC_E | PORT_WKDISC_E;
 175			else
 176				t2 |= PORT_WKOC_E | PORT_WKCONN_E;
 177		}
 178		ehci_vdbg(ehci, "port %d, %08x -> %08x\n",
 179				port + 1, t1, t2);
 180		ehci_writel(ehci, t2, reg);
 181	}
 182
 183	/* enter phy low-power mode again */
 184	if (ehci->has_hostpc && !ehci->broken_hostpc_phcd) {
 185		port = HCS_N_PORTS(ehci->hcs_params);
 186		while (port--) {
 187			u32 __iomem	*hostpc_reg;
 188
 189			hostpc_reg = (u32 __iomem *)((u8 *) ehci->regs
 190					+ HOSTPC0 + 4 * port);
 191			temp = ehci_readl(ehci, hostpc_reg);
 192			ehci_writel(ehci, temp | HOSTPC_PHCD, hostpc_reg);
 193		}
 194	}
 195
 196	/* Does the root hub have a port wakeup pending? */
 197	if (!suspending && ehci_port_change(ehci))
 198		usb_hcd_resume_root_hub(ehci_to_hcd(ehci));
 199
 200	spin_unlock_irqrestore(&ehci->lock, flags);
 201}
 202
 203static int ehci_bus_suspend (struct usb_hcd *hcd)
 204{
 205	struct ehci_hcd		*ehci = hcd_to_ehci (hcd);
 206	int			port;
 207	int			mask;
 208	int			changed;
 209
 210	ehci_dbg(ehci, "suspend root hub\n");
 211
 212	if (time_before (jiffies, ehci->next_statechange))
 213		msleep(5);
 214	del_timer_sync(&ehci->watchdog);
 215	del_timer_sync(&ehci->iaa_watchdog);
 216
 217	spin_lock_irq (&ehci->lock);
 218
 219	/* Once the controller is stopped, port resumes that are already
 220	 * in progress won't complete.  Hence if remote wakeup is enabled
 221	 * for the root hub and any ports are in the middle of a resume or
 222	 * remote wakeup, we must fail the suspend.
 223	 */
 224	if (hcd->self.root_hub->do_remote_wakeup) {
 225		port = HCS_N_PORTS(ehci->hcs_params);
 226		while (port--) {
 227			if (ehci->reset_done[port] != 0) {
 228				spin_unlock_irq(&ehci->lock);
 229				ehci_dbg(ehci, "suspend failed because "
 230						"port %d is resuming\n",
 231						port + 1);
 232				return -EBUSY;
 233			}
 234		}
 235	}
 236
 237	/* stop schedules, clean any completed work */
 238	if (HC_IS_RUNNING(hcd->state)) {
 239		ehci_quiesce (ehci);
 240		hcd->state = HC_STATE_QUIESCING;
 241	}
 242	ehci->command = ehci_readl(ehci, &ehci->regs->command);
 243	ehci_work(ehci);
 244
 245	/* Unlike other USB host controller types, EHCI doesn't have
 246	 * any notion of "global" or bus-wide suspend.  The driver has
 247	 * to manually suspend all the active unsuspended ports, and
 248	 * then manually resume them in the bus_resume() routine.
 249	 */
 250	ehci->bus_suspended = 0;
 251	ehci->owned_ports = 0;
 252	changed = 0;
 253	port = HCS_N_PORTS(ehci->hcs_params);
 254	while (port--) {
 255		u32 __iomem	*reg = &ehci->regs->port_status [port];
 256		u32		t1 = ehci_readl(ehci, reg) & ~PORT_RWC_BITS;
 257		u32		t2 = t1 & ~PORT_WAKE_BITS;
 258
 259		/* keep track of which ports we suspend */
 260		if (t1 & PORT_OWNER)
 261			set_bit(port, &ehci->owned_ports);
 262		else if ((t1 & PORT_PE) && !(t1 & PORT_SUSPEND)) {
 263			t2 |= PORT_SUSPEND;
 264			set_bit(port, &ehci->bus_suspended);
 265		}
 266
 267		/* enable remote wakeup on all ports, if told to do so */
 268		if (hcd->self.root_hub->do_remote_wakeup) {
 269			/* only enable appropriate wake bits, otherwise the
 270			 * hardware can not go phy low power mode. If a race
 271			 * condition happens here(connection change during bits
 272			 * set), the port change detection will finally fix it.
 273			 */
 274			if (t1 & PORT_CONNECT)
 275				t2 |= PORT_WKOC_E | PORT_WKDISC_E;
 276			else
 277				t2 |= PORT_WKOC_E | PORT_WKCONN_E;
 278		}
 279
 280		if (t1 != t2) {
 281			ehci_vdbg (ehci, "port %d, %08x -> %08x\n",
 282				port + 1, t1, t2);
 283			ehci_writel(ehci, t2, reg);
 284			changed = 1;
 285		}
 286	}
 287#ifdef CONFIG_ARCH_TEGRA_2x_SOC
 288	if (changed && ehci->has_hostpc && !ehci->broken_hostpc_phcd) {
 289		spin_unlock_irq(&ehci->lock);
 290		msleep(5);	/* 5 ms for HCD to enter low-power mode */
 291		spin_lock_irq(&ehci->lock);
 292
 293		port = HCS_N_PORTS(ehci->hcs_params);
 294		while (port--) {
 295			u32 __iomem	*hostpc_reg;
 296			u32		t3;
 297
 298			hostpc_reg = (u32 __iomem *)((u8 *) ehci->regs
 299					+ HOSTPC0 + 4 * port);
 300			t3 = ehci_readl(ehci, hostpc_reg);
 301			ehci_writel(ehci, t3 | HOSTPC_PHCD, hostpc_reg);
 302			t3 = ehci_readl(ehci, hostpc_reg);
 303			ehci_dbg(ehci, "Port %d phy low-power mode %s\n",
 304					port, (t3 & HOSTPC_PHCD) ?
 305					"succeeded" : "failed");
 306		}
 307	}
 308#endif
 309	/* Apparently some devices need a >= 1-uframe delay here */
 310	if (ehci->bus_suspended)
 311		udelay(150);
 312
 313	/* turn off now-idle HC */
 314	ehci_halt (ehci);
 315	hcd->state = HC_STATE_SUSPENDED;
 316
 317	if (ehci->reclaim)
 318		end_unlink_async(ehci);
 319
 320	/* allow remote wakeup */
 321	mask = INTR_MASK;
 322	if (!hcd->self.root_hub->do_remote_wakeup)
 323		mask &= ~STS_PCD;
 324	ehci_writel(ehci, mask, &ehci->regs->intr_enable);
 325	ehci_readl(ehci, &ehci->regs->intr_enable);
 326
 327	ehci->next_statechange = jiffies + msecs_to_jiffies(10);
 328	spin_unlock_irq (&ehci->lock);
 329
 330	/* ehci_work() may have re-enabled the watchdog timer, which we do not
 331	 * want, and so we must delete any pending watchdog timer events.
 332	 */
 333	del_timer_sync(&ehci->watchdog);
 334	return 0;
 335}
 336
 337
 338/* caller has locked the root hub, and should reset/reinit on error */
 339static int ehci_bus_resume (struct usb_hcd *hcd)
 340{
 341	struct ehci_hcd		*ehci = hcd_to_ehci (hcd);
 342	u32			temp;
 343	u32			power_okay;
 344	int			i;
 345	unsigned long		resume_needed = 0;
 346
 347	if (time_before (jiffies, ehci->next_statechange))
 348		msleep(5);
 349	spin_lock_irq (&ehci->lock);
 350	if (!HCD_HW_ACCESSIBLE(hcd)) {
 351		spin_unlock_irq(&ehci->lock);
 352		return -ESHUTDOWN;
 353	}
 354
 355	if (unlikely(ehci->debug)) {
 356		if (!dbgp_reset_prep())
 357			ehci->debug = NULL;
 358		else
 359			dbgp_external_startup();
 360	}
 361
 362	/* Ideally and we've got a real resume here, and no port's power
 363	 * was lost.  (For PCI, that means Vaux was maintained.)  But we
 364	 * could instead be restoring a swsusp snapshot -- so that BIOS was
 365	 * the last user of the controller, not reset/pm hardware keeping
 366	 * state we gave to it.
 367	 */
 368	power_okay = ehci_readl(ehci, &ehci->regs->intr_enable);
 369	ehci_dbg(ehci, "resume root hub%s\n",
 370			power_okay ? "" : " after power loss");
 371
 372	/* at least some APM implementations will try to deliver
 373	 * IRQs right away, so delay them until we're ready.
 374	 */
 375	ehci_writel(ehci, 0, &ehci->regs->intr_enable);
 376
 377	/* re-init operational registers */
 378	ehci_writel(ehci, 0, &ehci->regs->segment);
 379	ehci_writel(ehci, ehci->periodic_dma, &ehci->regs->frame_list);
 380	ehci_writel(ehci, (u32) ehci->async->qh_dma, &ehci->regs->async_next);
 381
 382	/* restore CMD_RUN, framelist size, and irq threshold */
 383	ehci_writel(ehci, ehci->command, &ehci->regs->command);
 384
 385	/* Some controller/firmware combinations need a delay during which
 386	 * they set up the port statuses.  See Bugzilla #8190. */
 387	spin_unlock_irq(&ehci->lock);
 388	msleep(8);
 389	spin_lock_irq(&ehci->lock);
 390
 391	/* clear phy low-power mode before resume */
 392	if (ehci->bus_suspended && ehci->has_hostpc && !ehci->broken_hostpc_phcd) {
 393		i = HCS_N_PORTS(ehci->hcs_params);
 394		while (i--) {
 395			if (test_bit(i, &ehci->bus_suspended)) {
 396				u32 __iomem	*hostpc_reg;
 397
 398				hostpc_reg = (u32 __iomem *)((u8 *) ehci->regs
 399						+ HOSTPC0 + 4 * i);
 400				temp = ehci_readl(ehci, hostpc_reg);
 401				ehci_writel(ehci, temp & ~HOSTPC_PHCD,
 402						hostpc_reg);
 403			}
 404		}
 405		spin_unlock_irq(&ehci->lock);
 406		msleep(5);
 407		spin_lock_irq(&ehci->lock);
 408	}
 409
 410	/* manually resume the ports we suspended during bus_suspend() */
 411	i = HCS_N_PORTS (ehci->hcs_params);
 412	while (i--) {
 413		temp = ehci_readl(ehci, &ehci->regs->port_status [i]);
 414		temp &= ~(PORT_RWC_BITS | PORT_WAKE_BITS);
 415		if (test_bit(i, &ehci->bus_suspended) &&
 416				(temp & PORT_SUSPEND)) {
 417			temp |= PORT_RESUME;
 418			set_bit(i, &resume_needed);
 419		}
 420		ehci_writel(ehci, temp, &ehci->regs->port_status [i]);
 421	}
 422
 423	/* msleep for 20ms only if code is trying to resume port */
 424	if (resume_needed) {
 425		spin_unlock_irq(&ehci->lock);
 426		msleep(20);
 427		spin_lock_irq(&ehci->lock);
 428	}
 429
 430	i = HCS_N_PORTS (ehci->hcs_params);
 431	while (i--) {
 432		temp = ehci_readl(ehci, &ehci->regs->port_status [i]);
 433		if (test_bit(i, &resume_needed)) {
 434			temp &= ~(PORT_RWC_BITS | PORT_RESUME);
 435			ehci_writel(ehci, temp, &ehci->regs->port_status [i]);
 436			ehci_vdbg (ehci, "resumed port %d\n", i + 1);
 437		}
 438	}
 439	(void) ehci_readl(ehci, &ehci->regs->command);
 440
 441	/* maybe re-activate the schedule(s) */
 442	temp = 0;
 443	if (ehci->async->qh_next.qh)
 444		temp |= CMD_ASE;
 445	if (ehci->periodic_sched)
 446		temp |= CMD_PSE;
 447	if (temp) {
 448		ehci->command |= temp;
 449		ehci_writel(ehci, ehci->command, &ehci->regs->command);
 450	}
 451
 452	ehci->next_statechange = jiffies + msecs_to_jiffies(5);
 453	hcd->state = HC_STATE_RUNNING;
 454
 455	/* Now we can safely re-enable irqs */
 456	ehci_writel(ehci, INTR_MASK, &ehci->regs->intr_enable);
 457
 458	spin_unlock_irq (&ehci->lock);
 459	ehci_handover_companion_ports(ehci);
 460	return 0;
 461}
 462
 463#else
 464
 465#define ehci_bus_suspend	NULL
 466#define ehci_bus_resume		NULL
 467
 468#endif	/* CONFIG_PM */
 469
 470/*-------------------------------------------------------------------------*/
 471
 472/*
 473 * Sets the owner of a port
 474 */
 475static void set_owner(struct ehci_hcd *ehci, int portnum, int new_owner)
 476{
 477	u32 __iomem		*status_reg;
 478	u32			port_status;
 479	int 			try;
 480
 481	status_reg = &ehci->regs->port_status[portnum];
 482
 483	/*
 484	 * The controller won't set the OWNER bit if the port is
 485	 * enabled, so this loop will sometimes require at least two
 486	 * iterations: one to disable the port and one to set OWNER.
 487	 */
 488	for (try = 4; try > 0; --try) {
 489		spin_lock_irq(&ehci->lock);
 490		port_status = ehci_readl(ehci, status_reg);
 491		if ((port_status & PORT_OWNER) == new_owner
 492				|| (port_status & (PORT_OWNER | PORT_CONNECT))
 493					== 0)
 494			try = 0;
 495		else {
 496			port_status ^= PORT_OWNER;
 497			port_status &= ~(PORT_PE | PORT_RWC_BITS);
 498			ehci_writel(ehci, port_status, status_reg);
 499		}
 500		spin_unlock_irq(&ehci->lock);
 501		if (try > 1)
 502			msleep(5);
 503	}
 504}
 505
 506/*-------------------------------------------------------------------------*/
 507
 508static int check_reset_complete (
 509	struct ehci_hcd	*ehci,
 510	int		index,
 511	u32 __iomem	*status_reg,
 512	int		port_status
 513) {
 514	if (!(port_status & PORT_CONNECT))
 515		return port_status;
 516
 517	/* if reset finished and it's still not enabled -- handoff */
 518	if (!(port_status & PORT_PE)) {
 519
 520		/* with integrated TT, there's nobody to hand it to! */
 521		if (ehci_is_TDI(ehci)) {
 522			ehci_dbg (ehci,
 523				"Failed to enable port %d on root hub TT\n",
 524				index+1);
 525			return port_status;
 526		}
 527
 528		ehci_dbg (ehci, "port %d full speed --> companion\n",
 529			index + 1);
 530
 531		// what happens if HCS_N_CC(params) == 0 ?
 532		port_status |= PORT_OWNER;
 533		port_status &= ~PORT_RWC_BITS;
 534		ehci_writel(ehci, port_status, status_reg);
 535
 536		/* ensure 440EPX ohci controller state is operational */
 537		if (ehci->has_amcc_usb23)
 538			set_ohci_hcfs(ehci, 1);
 539	} else {
 540		ehci_dbg (ehci, "port %d high speed\n", index + 1);
 541		/* ensure 440EPx ohci controller state is suspended */
 542		if (ehci->has_amcc_usb23)
 543			set_ohci_hcfs(ehci, 0);
 544	}
 545
 546	return port_status;
 547}
 548
 549/*-------------------------------------------------------------------------*/
 550
 551
 552/* build "status change" packet (one or two bytes) from HC registers */
 553
 554static int
 555ehci_hub_status_data (struct usb_hcd *hcd, char *buf)
 556{
 557	struct ehci_hcd	*ehci = hcd_to_ehci (hcd);
 558	u32		temp, status = 0;
 559	u32		mask;
 560	int		ports, i, retval = 1;
 561	unsigned long	flags;
 562	u32		ppcd = 0;
 563
 564	/* if !USB_SUSPEND, root hub timers won't get shut down ... */
 565	if (!HC_IS_RUNNING(hcd->state))
 566		return 0;
 567
 568	/* init status to no-changes */
 569	buf [0] = 0;
 570	ports = HCS_N_PORTS (ehci->hcs_params);
 571	if (ports > 7) {
 572		buf [1] = 0;
 573		retval++;
 574	}
 575
 576	/* Some boards (mostly VIA?) report bogus overcurrent indications,
 577	 * causing massive log spam unless we completely ignore them.  It
 578	 * may be relevant that VIA VT8235 controllers, where PORT_POWER is
 579	 * always set, seem to clear PORT_OCC and PORT_CSC when writing to
 580	 * PORT_POWER; that's surprising, but maybe within-spec.
 581	 */
 582	if (!ignore_oc)
 583		mask = PORT_CSC | PORT_PEC | PORT_OCC;
 584	else
 585		mask = PORT_CSC | PORT_PEC;
 586	// PORT_RESUME from hardware ~= PORT_STAT_C_SUSPEND
 587
 588	/* no hub change reports (bit 0) for now (power, ...) */
 589
 590	/* port N changes (bit N)? */
 591	spin_lock_irqsave (&ehci->lock, flags);
 592
 593	/* get per-port change detect bits */
 594	if (ehci->has_ppcd)
 595		ppcd = ehci_readl(ehci, &ehci->regs->status) >> 16;
 596
 597	for (i = 0; i < ports; i++) {
 598		/* leverage per-port change bits feature */
 599		if (ehci->has_ppcd && !(ppcd & (1 << i)))
 600			continue;
 601		temp = ehci_readl(ehci, &ehci->regs->port_status [i]);
 602
 603		/*
 604		 * Return status information even for ports with OWNER set.
 605		 * Otherwise khubd wouldn't see the disconnect event when a
 606		 * high-speed device is switched over to the companion
 607		 * controller by the user.
 608		 */
 609
 610		if ((temp & mask) != 0 || test_bit(i, &ehci->port_c_suspend)
 611				|| (ehci->reset_done[i] && time_after_eq(
 612					jiffies, ehci->reset_done[i]))) {
 613			if (i < 7)
 614			    buf [0] |= 1 << (i + 1);
 615			else
 616			    buf [1] |= 1 << (i - 7);
 617			status = STS_PCD;
 618		}
 619	}
 620	/* FIXME autosuspend idle root hubs */
 621	spin_unlock_irqrestore (&ehci->lock, flags);
 622	return status ? retval : 0;
 623}
 624
 625/*-------------------------------------------------------------------------*/
 626
 627static void
 628ehci_hub_descriptor (
 629	struct ehci_hcd			*ehci,
 630	struct usb_hub_descriptor	*desc
 631) {
 632	int		ports = HCS_N_PORTS (ehci->hcs_params);
 633	u16		temp;
 634
 635	desc->bDescriptorType = 0x29;
 636	desc->bPwrOn2PwrGood = 10;	/* ehci 1.0, 2.3.9 says 20ms max */
 637	desc->bHubContrCurrent = 0;
 638
 639	desc->bNbrPorts = ports;
 640	temp = 1 + (ports / 8);
 641	desc->bDescLength = 7 + 2 * temp;
 642
 643	/* two bitmaps:  ports removable, and usb 1.0 legacy PortPwrCtrlMask */
 644	memset(&desc->u.hs.DeviceRemovable[0], 0, temp);
 645	memset(&desc->u.hs.DeviceRemovable[temp], 0xff, temp);
 646
 647	temp = 0x0008;			/* per-port overcurrent reporting */
 648	if (HCS_PPC (ehci->hcs_params))
 649		temp |= 0x0001;		/* per-port power control */
 650	else
 651		temp |= 0x0002;		/* no power switching */
 652#if 0
 653// re-enable when we support USB_PORT_FEAT_INDICATOR below.
 654	if (HCS_INDICATOR (ehci->hcs_params))
 655		temp |= 0x0080;		/* per-port indicators (LEDs) */
 656#endif
 657	desc->wHubCharacteristics = cpu_to_le16(temp);
 658}
 659
 660/*-------------------------------------------------------------------------*/
 661
 662static int ehci_hub_control (
 663	struct usb_hcd	*hcd,
 664	u16		typeReq,
 665	u16		wValue,
 666	u16		wIndex,
 667	char		*buf,
 668	u16		wLength
 669) {
 670	struct ehci_hcd	*ehci = hcd_to_ehci (hcd);
 671	int		ports = HCS_N_PORTS (ehci->hcs_params);
 672	u32 __iomem	*status_reg = &ehci->regs->port_status[
 673				(wIndex & 0xff) - 1];
 674	u32 __iomem	*hostpc_reg = NULL;
 675	u32		temp, temp1, status;
 676	unsigned long	flags;
 677	int		retval = 0;
 678	unsigned	selector;
 679
 680	/*
 681	 * FIXME:  support SetPortFeatures USB_PORT_FEAT_INDICATOR.
 682	 * HCS_INDICATOR may say we can change LEDs to off/amber/green.
 683	 * (track current state ourselves) ... blink for diagnostics,
 684	 * power, "this is the one", etc.  EHCI spec supports this.
 685	 */
 686
 687	if (ehci->has_hostpc)
 688		hostpc_reg = (u32 __iomem *)((u8 *)ehci->regs
 689				+ HOSTPC0 + 4 * ((wIndex & 0xff) - 1));
 690	spin_lock_irqsave (&ehci->lock, flags);
 691	switch (typeReq) {
 692	case ClearHubFeature:
 693		switch (wValue) {
 694		case C_HUB_LOCAL_POWER:
 695		case C_HUB_OVER_CURRENT:
 696			/* no hub-wide feature/status flags */
 697			break;
 698		default:
 699			goto error;
 700		}
 701		break;
 702	case ClearPortFeature:
 703		if (!wIndex || wIndex > ports)
 704			goto error;
 705		wIndex--;
 706		temp = ehci_readl(ehci, status_reg);
 707
 708		/*
 709		 * Even if OWNER is set, so the port is owned by the
 710		 * companion controller, khubd needs to be able to clear
 711		 * the port-change status bits (especially
 712		 * USB_PORT_STAT_C_CONNECTION).
 713		 */
 714
 715		switch (wValue) {
 716		case USB_PORT_FEAT_ENABLE:
 717			ehci_writel(ehci, temp & ~PORT_PE, status_reg);
 718			break;
 719		case USB_PORT_FEAT_C_ENABLE:
 720			ehci_writel(ehci, (temp & ~PORT_RWC_BITS) | PORT_PEC,
 721					status_reg);
 722			break;
 723		case USB_PORT_FEAT_SUSPEND:
 724			if (temp & PORT_RESET)
 725				goto error;
 726			if (ehci->no_selective_suspend)
 727				break;
 728			if (!(temp & PORT_SUSPEND))
 729				break;
 730			if ((temp & PORT_PE) == 0)
 731				goto error;
 732
 733			/* clear phy low-power mode before resume */
 734			if (hostpc_reg && !ehci->broken_hostpc_phcd) {
 735				temp1 = ehci_readl(ehci, hostpc_reg);
 736				ehci_writel(ehci, temp1 & ~HOSTPC_PHCD,
 737						hostpc_reg);
 738				spin_unlock_irqrestore(&ehci->lock, flags);
 739				msleep(5);/* wait to leave low-power mode */
 740				spin_lock_irqsave(&ehci->lock, flags);
 741			}
 742			/* resume signaling for 20 msec */
 743			temp &= ~(PORT_RWC_BITS | PORT_WAKE_BITS);
 744			ehci_writel(ehci, temp | PORT_RESUME, status_reg);
 745			ehci->reset_done[wIndex] = jiffies
 746					+ msecs_to_jiffies(20);
 747			break;
 748		case USB_PORT_FEAT_C_SUSPEND:
 749			clear_bit(wIndex, &ehci->port_c_suspend);
 750			break;
 751		case USB_PORT_FEAT_POWER:
 752			if (HCS_PPC (ehci->hcs_params))
 753				ehci_writel(ehci,
 754					  temp & ~(PORT_RWC_BITS | PORT_POWER),
 755					  status_reg);
 756			break;
 757		case USB_PORT_FEAT_C_CONNECTION:
 758			if (ehci->has_lpm) {
 759				/* clear PORTSC bits on disconnect */
 760				temp &= ~PORT_LPM;
 761				temp &= ~PORT_DEV_ADDR;
 762			}
 763			ehci_writel(ehci, (temp & ~PORT_RWC_BITS) | PORT_CSC,
 764					status_reg);
 765			break;
 766		case USB_PORT_FEAT_C_OVER_CURRENT:
 767			ehci_writel(ehci, (temp & ~PORT_RWC_BITS) | PORT_OCC,
 768					status_reg);
 769			break;
 770		case USB_PORT_FEAT_C_RESET:
 771			/* GetPortStatus clears reset */
 772			break;
 773		default:
 774			goto error;
 775		}
 776		ehci_readl(ehci, &ehci->regs->command);	/* unblock posted write */
 777		break;
 778	case GetHubDescriptor:
 779		ehci_hub_descriptor (ehci, (struct usb_hub_descriptor *)
 780			buf);
 781		break;
 782	case GetHubStatus:
 783		/* no hub-wide feature/status flags */
 784		memset (buf, 0, 4);
 785		//cpu_to_le32s ((u32 *) buf);
 786		break;
 787	case GetPortStatus:
 788		if (!wIndex || wIndex > ports)
 789			goto error;
 790		wIndex--;
 791		status = 0;
 792		temp = ehci_readl(ehci, status_reg);
 793
 794		// wPortChange bits
 795		if (temp & PORT_CSC)
 796			status |= USB_PORT_STAT_C_CONNECTION << 16;
 797		if (temp & PORT_PEC)
 798			status |= USB_PORT_STAT_C_ENABLE << 16;
 799
 800		if ((temp & PORT_OCC) && !ignore_oc){
 801			status |= USB_PORT_STAT_C_OVERCURRENT << 16;
 802
 803			/*
 804			 * Hubs should disable port power on over-current.
 805			 * However, not all EHCI implementations do this
 806			 * automatically, even if they _do_ support per-port
 807			 * power switching; they're allowed to just limit the
 808			 * current.  khubd will turn the power back on.
 809			 */
 810			if ((temp & PORT_OC) && HCS_PPC(ehci->hcs_params)) {
 811				ehci_writel(ehci,
 812					temp & ~(PORT_RWC_BITS | PORT_POWER),
 813					status_reg);
 814				temp = ehci_readl(ehci, status_reg);
 815			}
 816		}
 817
 818		/* whoever resumes must GetPortStatus to complete it!! */
 819		if (temp & PORT_RESUME) {
 820
 821			/* Remote Wakeup received? */
 822			if (!ehci->reset_done[wIndex]) {
 823				/* resume signaling for 20 msec */
 824				ehci->reset_done[wIndex] = jiffies
 825						+ msecs_to_jiffies(20);
 826				/* check the port again */
 827				mod_timer(&ehci_to_hcd(ehci)->rh_timer,
 828						ehci->reset_done[wIndex]);
 829			}
 830
 831			/* resume completed? */
 832			else if (time_after_eq(jiffies,
 833					ehci->reset_done[wIndex])) {
 834				clear_bit(wIndex, &ehci->suspended_ports);
 835				set_bit(wIndex, &ehci->port_c_suspend);
 836				ehci->reset_done[wIndex] = 0;
 837
 838				/* stop resume signaling */
 839				temp = ehci_readl(ehci, status_reg);
 840				ehci_writel(ehci,
 841					temp & ~(PORT_RWC_BITS | PORT_RESUME),
 842					status_reg);
 843				retval = handshake(ehci, status_reg,
 844					   PORT_RESUME, 0, 2000 /* 2msec */);
 845				if (retval != 0) {
 846					ehci_err(ehci,
 847						"port %d resume error %d\n",
 848						wIndex + 1, retval);
 849					goto error;
 850				}
 851				temp &= ~(PORT_SUSPEND|PORT_RESUME|(3<<10));
 852			}
 853		}
 854
 855		/* whoever resets must GetPortStatus to complete it!! */
 856		if ((temp & PORT_RESET)
 857				&& time_after_eq(jiffies,
 858					ehci->reset_done[wIndex])) {
 859			status |= USB_PORT_STAT_C_RESET << 16;
 860			ehci->reset_done [wIndex] = 0;
 861
 862			/* force reset to complete */
 863			ehci_writel(ehci, temp & ~(PORT_RWC_BITS | PORT_RESET),
 864					status_reg);
 865			/* REVISIT:  some hardware needs 550+ usec to clear
 866			 * this bit; seems too long to spin routinely...
 867			 */
 868			retval = handshake(ehci, status_reg,
 869					PORT_RESET, 0, 1000);
 870			if (retval != 0) {
 871				ehci_err (ehci, "port %d reset error %d\n",
 872					wIndex + 1, retval);
 873				goto error;
 874			}
 875
 876			/* see what we found out */
 877			temp = check_reset_complete (ehci, wIndex, status_reg,
 878					ehci_readl(ehci, status_reg));
 879		}
 880
 881		if (!(temp & (PORT_RESUME|PORT_RESET)))
 882			ehci->reset_done[wIndex] = 0;
 883
 884		/* transfer dedicated ports to the companion hc */
 885		if ((temp & PORT_CONNECT) &&
 886				test_bit(wIndex, &ehci->companion_ports)) {
 887			temp &= ~PORT_RWC_BITS;
 888			temp |= PORT_OWNER;
 889			ehci_writel(ehci, temp, status_reg);
 890			ehci_dbg(ehci, "port %d --> companion\n", wIndex + 1);
 891			temp = ehci_readl(ehci, status_reg);
 892		}
 893
 894		/*
 895		 * Even if OWNER is set, there's no harm letting khubd
 896		 * see the wPortStatus values (they should all be 0 except
 897		 * for PORT_POWER anyway).
 898		 */
 899
 900		if (temp & PORT_CONNECT) {
 901			status |= USB_PORT_STAT_CONNECTION;
 902			// status may be from integrated TT
 903			if (ehci->has_hostpc) {
 904				temp1 = ehci_readl(ehci, hostpc_reg);
 905				status |= ehci_port_speed(ehci, temp1);
 906			} else
 907				status |= ehci_port_speed(ehci, temp);
 908		}
 909		if (temp & PORT_PE)
 910			status |= USB_PORT_STAT_ENABLE;
 911
 912		/* maybe the port was unsuspended without our knowledge */
 913		if (temp & (PORT_SUSPEND|PORT_RESUME)) {
 914			status |= USB_PORT_STAT_SUSPEND;
 915		} else if (test_bit(wIndex, &ehci->suspended_ports)) {
 916			clear_bit(wIndex, &ehci->suspended_ports);
 917			ehci->reset_done[wIndex] = 0;
 918			if (temp & PORT_PE)
 919				set_bit(wIndex, &ehci->port_c_suspend);
 920		}
 921
 922		if (temp & PORT_OC)
 923			status |= USB_PORT_STAT_OVERCURRENT;
 924		if (temp & PORT_RESET)
 925			status |= USB_PORT_STAT_RESET;
 926		if (temp & PORT_POWER)
 927			status |= USB_PORT_STAT_POWER;
 928		if (test_bit(wIndex, &ehci->port_c_suspend))
 929			status |= USB_PORT_STAT_C_SUSPEND << 16;
 930
 931#ifndef	VERBOSE_DEBUG
 932	if (status & ~0xffff)	/* only if wPortChange is interesting */
 933#endif
 934		dbg_port (ehci, "GetStatus", wIndex + 1, temp);
 935		put_unaligned_le32(status, buf);
 936		break;
 937	case SetHubFeature:
 938		switch (wValue) {
 939		case C_HUB_LOCAL_POWER:
 940		case C_HUB_OVER_CURRENT:
 941			/* no hub-wide feature/status flags */
 942			break;
 943		default:
 944			goto error;
 945		}
 946		break;
 947	case SetPortFeature:
 948		selector = wIndex >> 8;
 949		wIndex &= 0xff;
 950		if (unlikely(ehci->debug)) {
 951			/* If the debug port is active any port
 952			 * feature requests should get denied */
 953			if (wIndex == HCS_DEBUG_PORT(ehci->hcs_params) &&
 954			    (readl(&ehci->debug->control) & DBGP_ENABLED)) {
 955				retval = -ENODEV;
 956				goto error_exit;
 957			}
 958		}
 959		if (!wIndex || wIndex > ports)
 960			goto error;
 961		wIndex--;
 962		temp = ehci_readl(ehci, status_reg);
 963		if (temp & PORT_OWNER)
 964			break;
 965
 966		temp &= ~PORT_RWC_BITS;
 967		switch (wValue) {
 968		case USB_PORT_FEAT_SUSPEND:
 969			if (ehci->no_selective_suspend)
 970				break;
 971			if ((temp & PORT_PE) == 0
 972					|| (temp & PORT_RESET) != 0)
 973				goto error;
 974
 975			/* After above check the port must be connected.
 976			 * Set appropriate bit thus could put phy into low power
 977			 * mode if we have hostpc feature
 978			 */
 979			temp &= ~PORT_WKCONN_E;
 980			temp |= PORT_WKDISC_E | PORT_WKOC_E;
 981			ehci_writel(ehci, temp | PORT_SUSPEND, status_reg);
 982			if (hostpc_reg && !ehci->broken_hostpc_phcd) {
 983				spin_unlock_irqrestore(&ehci->lock, flags);
 984				msleep(5);/* 5ms for HCD enter low pwr mode */
 985				spin_lock_irqsave(&ehci->lock, flags);
 986				temp1 = ehci_readl(ehci, hostpc_reg);
 987				ehci_writel(ehci, temp1 | HOSTPC_PHCD,
 988					hostpc_reg);
 989				temp1 = ehci_readl(ehci, hostpc_reg);
 990				ehci_dbg(ehci, "Port%d phy low pwr mode %s\n",
 991					wIndex, (temp1 & HOSTPC_PHCD) ?
 992					"succeeded" : "failed");
 993			}
 994			set_bit(wIndex, &ehci->suspended_ports);
 995			break;
 996		case USB_PORT_FEAT_POWER:
 997			if (HCS_PPC (ehci->hcs_params))
 998				ehci_writel(ehci, temp | PORT_POWER,
 999						status_reg);
1000			break;
1001		case USB_PORT_FEAT_RESET:
1002			if (temp & PORT_RESUME)
1003				goto error;
1004			/* line status bits may report this as low speed,
1005			 * which can be fine if this root hub has a
1006			 * transaction translator built in.
1007			 */
1008			if ((temp & (PORT_PE|PORT_CONNECT)) == PORT_CONNECT
1009					&& !ehci_is_TDI(ehci)
1010					&& PORT_USB11 (temp)) {
1011				ehci_dbg (ehci,
1012					"port %d low speed --> companion\n",
1013					wIndex + 1);
1014				temp |= PORT_OWNER;
1015			} else {
1016				ehci_vdbg (ehci, "port %d reset\n", wIndex + 1);
1017				temp |= PORT_RESET;
1018				temp &= ~PORT_PE;
1019
1020				/*
1021				 * caller must wait, then call GetPortStatus
1022				 * usb 2.0 spec says 50 ms resets on root
1023				 */
1024				ehci->reset_done [wIndex] = jiffies
1025						+ msecs_to_jiffies (50);
1026			}
1027			ehci_writel(ehci, temp, status_reg);
1028			break;
1029
1030		/* For downstream facing ports (these):  one hub port is put
1031		 * into test mode according to USB2 11.24.2.13, then the hub
1032		 * must be reset (which for root hub now means rmmod+modprobe,
1033		 * or else system reboot).  See EHCI 2.3.9 and 4.14 for info
1034		 * about the EHCI-specific stuff.
1035		 */
1036		case USB_PORT_FEAT_TEST:
1037			if (!selector || selector > 5)
1038				goto error;
1039			ehci_quiesce(ehci);
1040
1041			/* Put all enabled ports into suspend */
1042			while (ports--) {
1043				u32 __iomem *sreg =
1044						&ehci->regs->port_status[ports];
1045
1046				temp = ehci_readl(ehci, sreg) & ~PORT_RWC_BITS;
1047				if (temp & PORT_PE)
1048					ehci_writel(ehci, temp | PORT_SUSPEND,
1049							sreg);
1050			}
1051			ehci_halt(ehci);
1052			temp = ehci_readl(ehci, status_reg);
1053			temp |= selector << 16;
1054			ehci_writel(ehci, temp, status_reg);
1055			break;
1056
1057		default:
1058			goto error;
1059		}
1060		ehci_readl(ehci, &ehci->regs->command);	/* unblock posted writes */
1061		break;
1062
1063	default:
1064error:
1065		/* "stall" on error */
1066		retval = -EPIPE;
1067	}
1068error_exit:
1069	spin_unlock_irqrestore (&ehci->lock, flags);
1070	return retval;
1071}
1072
1073static void ehci_relinquish_port(struct usb_hcd *hcd, int portnum)
1074{
1075	struct ehci_hcd		*ehci = hcd_to_ehci(hcd);
1076
1077	if (ehci_is_TDI(ehci))
1078		return;
1079	set_owner(ehci, --portnum, PORT_OWNER);
1080}
1081
1082static int ehci_port_handed_over(struct usb_hcd *hcd, int portnum)
1083{
1084	struct ehci_hcd		*ehci = hcd_to_ehci(hcd);
1085	u32 __iomem		*reg;
1086
1087	if (ehci_is_TDI(ehci))
1088		return 0;
1089	reg = &ehci->regs->port_status[portnum - 1];
1090	return ehci_readl(ehci, reg) & PORT_OWNER;
1091}