ohci-hub.c 20.6 KB
Newer Older
L
Linus Torvalds 已提交
1 2
/*
 * OHCI HCD (Host Controller Driver) for USB.
3
 *
L
Linus Torvalds 已提交
4 5
 * (C) Copyright 1999 Roman Weissgaerber <weissg@vienna.at>
 * (C) Copyright 2000-2004 David Brownell <dbrownell@users.sourceforge.net>
6
 *
L
Linus Torvalds 已提交
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 * This file is licenced under GPL
 */

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

/*
 * OHCI Root Hub ... the nonsharable stuff
 */

#define dbg_port(hc,label,num,value) \
	ohci_dbg (hc, \
		"%s roothub.portstatus [%d] " \
		"= 0x%08x%s%s%s%s%s%s%s%s%s%s%s%s\n", \
		label, num, temp, \
		(temp & RH_PS_PRSC) ? " PRSC" : "", \
		(temp & RH_PS_OCIC) ? " OCIC" : "", \
		(temp & RH_PS_PSSC) ? " PSSC" : "", \
		(temp & RH_PS_PESC) ? " PESC" : "", \
		(temp & RH_PS_CSC) ? " CSC" : "", \
26
		\
L
Linus Torvalds 已提交
27 28 29 30 31
		(temp & RH_PS_LSDA) ? " LSDA" : "", \
		(temp & RH_PS_PPS) ? " PPS" : "", \
		(temp & RH_PS_PRS) ? " PRS" : "", \
		(temp & RH_PS_POCI) ? " POCI" : "", \
		(temp & RH_PS_PSS) ? " PSS" : "", \
32
		\
L
Linus Torvalds 已提交
33 34 35 36 37 38 39 40 41
		(temp & RH_PS_PES) ? " PES" : "", \
		(temp & RH_PS_CCS) ? " CCS" : "" \
		);

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

#define OHCI_SCHED_ENABLES \
	(OHCI_CTRL_CLE|OHCI_CTRL_BLE|OHCI_CTRL_PLE|OHCI_CTRL_IE)

42 43
static void dl_done_list (struct ohci_hcd *);
static void finish_unlinks (struct ohci_hcd *, u16);
L
Linus Torvalds 已提交
44

A
Alan Stern 已提交
45
#ifdef	CONFIG_PM
A
Alan Stern 已提交
46 47 48
static int ohci_rh_suspend (struct ohci_hcd *ohci, int autostop)
__releases(ohci->lock)
__acquires(ohci->lock)
L
Linus Torvalds 已提交
49 50
{
	int			status = 0;
51

L
Linus Torvalds 已提交
52 53 54 55 56 57 58 59 60 61 62 63 64 65
	ohci->hc_control = ohci_readl (ohci, &ohci->regs->control);
	switch (ohci->hc_control & OHCI_CTRL_HCFS) {
	case OHCI_USB_RESUME:
		ohci_dbg (ohci, "resume/suspend?\n");
		ohci->hc_control &= ~OHCI_CTRL_HCFS;
		ohci->hc_control |= OHCI_USB_RESET;
		ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
		(void) ohci_readl (ohci, &ohci->regs->control);
		/* FALL THROUGH */
	case OHCI_USB_RESET:
		status = -EBUSY;
		ohci_dbg (ohci, "needs reinit!\n");
		goto done;
	case OHCI_USB_SUSPEND:
A
Alan Stern 已提交
66 67 68 69
		if (!ohci->autostop) {
			ohci_dbg (ohci, "already suspended\n");
			goto done;
		}
L
Linus Torvalds 已提交
70
	}
A
Alan Stern 已提交
71 72
	ohci_dbg (ohci, "%s root hub\n",
			autostop ? "auto-stop" : "suspend");
L
Linus Torvalds 已提交
73 74

	/* First stop any processing */
A
Alan Stern 已提交
75
	if (!autostop && (ohci->hc_control & OHCI_SCHED_ENABLES)) {
L
Linus Torvalds 已提交
76 77 78 79 80 81 82 83 84
		ohci->hc_control &= ~OHCI_SCHED_ENABLES;
		ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
		ohci->hc_control = ohci_readl (ohci, &ohci->regs->control);
		ohci_writel (ohci, OHCI_INTR_SF, &ohci->regs->intrstatus);

		/* sched disables take effect on the next frame,
		 * then the last WDH could take 6+ msec
		 */
		ohci_dbg (ohci, "stopping schedules ...\n");
A
Alan Stern 已提交
85 86 87 88
		ohci->autostop = 0;
		spin_unlock_irq (&ohci->lock);
		msleep (8);
		spin_lock_irq (&ohci->lock);
L
Linus Torvalds 已提交
89
	}
90 91
	dl_done_list (ohci);
	finish_unlinks (ohci, ohci_frame_no(ohci));
L
Linus Torvalds 已提交
92 93

	/* maybe resume can wake root hub */
94
	if (ohci_to_hcd(ohci)->self.root_hub->do_remote_wakeup || autostop) {
L
Linus Torvalds 已提交
95
		ohci->hc_control |= OHCI_CTRL_RWE;
96
	} else {
97 98
		ohci_writel(ohci, OHCI_INTR_RHSC | OHCI_INTR_RD,
				&ohci->regs->intrdisable);
L
Linus Torvalds 已提交
99
		ohci->hc_control &= ~OHCI_CTRL_RWE;
100
	}
L
Linus Torvalds 已提交
101

D
David Brownell 已提交
102 103 104
	/* Suspend hub ... this is the "global (to this bus) suspend" mode,
	 * which doesn't imply ports will first be individually suspended.
	 */
L
Linus Torvalds 已提交
105 106 107 108 109 110
	ohci->hc_control &= ~OHCI_CTRL_HCFS;
	ohci->hc_control |= OHCI_USB_SUSPEND;
	ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
	(void) ohci_readl (ohci, &ohci->regs->control);

	/* no resumes until devices finish suspending */
A
Alan Stern 已提交
111 112 113 114
	if (!autostop) {
		ohci->next_statechange = jiffies + msecs_to_jiffies (5);
		ohci->autostop = 0;
	}
115

L
Linus Torvalds 已提交
116 117 118 119 120 121 122 123 124 125 126 127 128
done:
	return status;
}

static inline struct ed *find_head (struct ed *ed)
{
	/* for bulk and control lists */
	while (ed->ed_prev)
		ed = ed->ed_prev;
	return ed;
}

/* caller has locked the root hub */
A
Alan Stern 已提交
129 130 131
static int ohci_rh_resume (struct ohci_hcd *ohci)
__releases(ohci->lock)
__acquires(ohci->lock)
L
Linus Torvalds 已提交
132
{
A
Alan Stern 已提交
133
	struct usb_hcd		*hcd = ohci_to_hcd (ohci);
L
Linus Torvalds 已提交
134 135
	u32			temp, enables;
	int			status = -EINPROGRESS;
A
Alan Stern 已提交
136
	int			autostopped = ohci->autostop;
137

A
Alan Stern 已提交
138
	ohci->autostop = 0;
L
Linus Torvalds 已提交
139 140 141
	ohci->hc_control = ohci_readl (ohci, &ohci->regs->control);

	if (ohci->hc_control & (OHCI_CTRL_IR | OHCI_SCHED_ENABLES)) {
D
David Brownell 已提交
142
		/* this can happen after resuming a swsusp snapshot */
L
Linus Torvalds 已提交
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
		if (hcd->state == HC_STATE_RESUMING) {
			ohci_dbg (ohci, "BIOS/SMM active, control %03x\n",
					ohci->hc_control);
			status = -EBUSY;
		/* this happens when pmcore resumes HC then root */
		} else {
			ohci_dbg (ohci, "duplicate resume\n");
			status = 0;
		}
	} else switch (ohci->hc_control & OHCI_CTRL_HCFS) {
	case OHCI_USB_SUSPEND:
		ohci->hc_control &= ~(OHCI_CTRL_HCFS|OHCI_SCHED_ENABLES);
		ohci->hc_control |= OHCI_USB_RESUME;
		ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
		(void) ohci_readl (ohci, &ohci->regs->control);
A
Alan Stern 已提交
158 159
		ohci_dbg (ohci, "%s root hub\n",
				autostopped ? "auto-start" : "resume");
L
Linus Torvalds 已提交
160 161 162
		break;
	case OHCI_USB_RESUME:
		/* HCFS changes sometime after INTR_RD */
163
		ohci_dbg(ohci, "%swakeup root hub\n",
A
Alan Stern 已提交
164
				autostopped ? "auto-" : "");
L
Linus Torvalds 已提交
165 166
		break;
	case OHCI_USB_OPER:
D
David Brownell 已提交
167 168 169
		/* this can happen after resuming a swsusp snapshot */
		ohci_dbg (ohci, "snapshot resume? reinit\n");
		status = -EBUSY;
L
Linus Torvalds 已提交
170 171
		break;
	default:		/* RESET, we lost power */
D
David Brownell 已提交
172
		ohci_dbg (ohci, "lost power\n");
L
Linus Torvalds 已提交
173 174 175
		status = -EBUSY;
	}
	if (status == -EBUSY) {
A
Alan Stern 已提交
176 177 178 179
		if (!autostopped) {
			spin_unlock_irq (&ohci->lock);
			(void) ohci_init (ohci);
			status = ohci_restart (ohci);
180 181 182

			usb_root_hub_lost_power(hcd->self.root_hub);

A
Alan Stern 已提交
183 184 185
			spin_lock_irq (&ohci->lock);
		}
		return status;
L
Linus Torvalds 已提交
186 187 188
	}
	if (status != -EINPROGRESS)
		return status;
A
Alan Stern 已提交
189 190 191
	if (autostopped)
		goto skip_resume;
	spin_unlock_irq (&ohci->lock);
L
Linus Torvalds 已提交
192 193

	/* Some controllers (lucent erratum) need extra-long delays */
D
David Brownell 已提交
194
	msleep (20 /* usb 11.5.1.10 */ + 12 /* 32 msec counter */ + 1);
L
Linus Torvalds 已提交
195 196 197 198 199

	temp = ohci_readl (ohci, &ohci->regs->control);
	temp &= OHCI_CTRL_HCFS;
	if (temp != OHCI_USB_RESUME) {
		ohci_err (ohci, "controller won't resume\n");
200
		spin_lock_irq(&ohci->lock);
L
Linus Torvalds 已提交
201 202 203 204 205 206 207 208 209 210 211 212 213 214
		return -EBUSY;
	}

	/* disable old schedule state, reinit from scratch */
	ohci_writel (ohci, 0, &ohci->regs->ed_controlhead);
	ohci_writel (ohci, 0, &ohci->regs->ed_controlcurrent);
	ohci_writel (ohci, 0, &ohci->regs->ed_bulkhead);
	ohci_writel (ohci, 0, &ohci->regs->ed_bulkcurrent);
	ohci_writel (ohci, 0, &ohci->regs->ed_periodcurrent);
	ohci_writel (ohci, (u32) ohci->hcca_dma, &ohci->regs->hcca);

	/* Sometimes PCI D3 suspend trashes frame timings ... */
	periodic_reinit (ohci);

A
Alan Stern 已提交
215 216 217 218 219
	/* the following code is executed with ohci->lock held and
	 * irqs disabled if and only if autostopped is true
	 */

skip_resume:
L
Linus Torvalds 已提交
220 221 222 223 224 225 226 227
	/* interrupts might have been disabled */
	ohci_writel (ohci, OHCI_INTR_INIT, &ohci->regs->intrenable);
	if (ohci->ed_rm_list)
		ohci_writel (ohci, OHCI_INTR_SF, &ohci->regs->intrenable);

	/* Then re-enable operations */
	ohci_writel (ohci, OHCI_USB_OPER, &ohci->regs->control);
	(void) ohci_readl (ohci, &ohci->regs->control);
A
Alan Stern 已提交
228 229
	if (!autostopped)
		msleep (3);
L
Linus Torvalds 已提交
230

231 232 233
	temp = ohci->hc_control;
	temp &= OHCI_CTRL_RWC;
	temp |= OHCI_CONTROL_INIT | OHCI_USB_OPER;
L
Linus Torvalds 已提交
234 235 236 237 238
	ohci->hc_control = temp;
	ohci_writel (ohci, temp, &ohci->regs->control);
	(void) ohci_readl (ohci, &ohci->regs->control);

	/* TRSMRCY */
A
Alan Stern 已提交
239 240 241 242 243
	if (!autostopped) {
		msleep (10);
		spin_lock_irq (&ohci->lock);
	}
	/* now ohci->lock is always held and irqs are always disabled */
L
Linus Torvalds 已提交
244

245 246
	/* keep it alive for more than ~5x suspend + resume costs */
	ohci->next_statechange = jiffies + STATECHANGE_DELAY;
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

	/* maybe turn schedules back on */
	enables = 0;
	temp = 0;
	if (!ohci->ed_rm_list) {
		if (ohci->ed_controltail) {
			ohci_writel (ohci,
					find_head (ohci->ed_controltail)->dma,
					&ohci->regs->ed_controlhead);
			enables |= OHCI_CTRL_CLE;
			temp |= OHCI_CLF;
		}
		if (ohci->ed_bulktail) {
			ohci_writel (ohci, find_head (ohci->ed_bulktail)->dma,
				&ohci->regs->ed_bulkhead);
			enables |= OHCI_CTRL_BLE;
			temp |= OHCI_BLF;
		}
	}
	if (hcd->self.bandwidth_isoc_reqs || hcd->self.bandwidth_int_reqs)
		enables |= OHCI_CTRL_PLE|OHCI_CTRL_IE;
	if (enables) {
		ohci_dbg (ohci, "restarting schedules ... %08x\n", enables);
		ohci->hc_control |= enables;
		ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
		if (temp)
			ohci_writel (ohci, temp, &ohci->regs->cmdstatus);
		(void) ohci_readl (ohci, &ohci->regs->control);
	}

	return 0;
}

A
Alan Stern 已提交
280 281 282 283 284 285 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
static int ohci_bus_suspend (struct usb_hcd *hcd)
{
	struct ohci_hcd		*ohci = hcd_to_ohci (hcd);
	int			rc;

	spin_lock_irq (&ohci->lock);

	if (unlikely(!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)))
		rc = -ESHUTDOWN;
	else
		rc = ohci_rh_suspend (ohci, 0);
	spin_unlock_irq (&ohci->lock);
	return rc;
}

static int ohci_bus_resume (struct usb_hcd *hcd)
{
	struct ohci_hcd		*ohci = hcd_to_ohci (hcd);
	int			rc;

	if (time_before (jiffies, ohci->next_statechange))
		msleep(5);

	spin_lock_irq (&ohci->lock);

	if (unlikely(!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)))
		rc = -ESHUTDOWN;
	else
		rc = ohci_rh_resume (ohci);
	spin_unlock_irq (&ohci->lock);

	/* poll until we know a device is connected or we autostop */
	if (rc == 0)
		usb_hcd_poll_rh_status(hcd);
	return rc;
}

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 354 355 356 357 358 359
/* Carry out the final steps of resuming the controller device */
static void ohci_finish_controller_resume(struct usb_hcd *hcd)
{
	struct ohci_hcd		*ohci = hcd_to_ohci(hcd);
	int			port;
	bool			need_reinit = false;

	/* See if the controller is already running or has been reset */
	ohci->hc_control = ohci_readl(ohci, &ohci->regs->control);
	if (ohci->hc_control & (OHCI_CTRL_IR | OHCI_SCHED_ENABLES)) {
		need_reinit = true;
	} else {
		switch (ohci->hc_control & OHCI_CTRL_HCFS) {
		case OHCI_USB_OPER:
		case OHCI_USB_RESET:
			need_reinit = true;
		}
	}

	/* If needed, reinitialize and suspend the root hub */
	if (need_reinit) {
		spin_lock_irq(&ohci->lock);
		hcd->state = HC_STATE_RESUMING;
		ohci_rh_resume(ohci);
		hcd->state = HC_STATE_QUIESCING;
		ohci_rh_suspend(ohci, 0);
		hcd->state = HC_STATE_SUSPENDED;
		spin_unlock_irq(&ohci->lock);
	}

	/* Normally just turn on port power and enable interrupts */
	else {
		ohci_dbg(ohci, "powerup ports\n");
		for (port = 0; port < ohci->num_ports; port++)
			ohci_writel(ohci, RH_PS_PPS,
					&ohci->regs->roothub.portstatus[port]);

		ohci_writel(ohci, OHCI_INTR_MIE, &ohci->regs->intrenable);
		ohci_readl(ohci, &ohci->regs->intrenable);
		msleep(20);
	}
}

360 361 362 363 364
/* Carry out polling-, autostop-, and autoresume-related state changes */
static int ohci_root_hub_state_changes(struct ohci_hcd *ohci, int changed,
		int any_connected)
{
	int	poll_rh = 1;
365
	int	rhsc;
366

367
	rhsc = ohci_readl(ohci, &ohci->regs->intrenable) & OHCI_INTR_RHSC;
368 369 370
	switch (ohci->hc_control & OHCI_CTRL_HCFS) {

	case OHCI_USB_OPER:
371 372 373 374 375 376 377 378 379 380 381
		/* If no status changes are pending, enable status-change
		 * interrupts.
		 */
		if (!rhsc && !changed) {
			rhsc = OHCI_INTR_RHSC;
			ohci_writel(ohci, rhsc, &ohci->regs->intrenable);
		}

		/* Keep on polling until we know a device is connected
		 * and RHSC is enabled, or until we autostop.
		 */
382 383 384 385
		if (!ohci->autostop) {
			if (any_connected ||
					!device_may_wakeup(&ohci_to_hcd(ohci)
						->self.root_hub->dev)) {
386
				if (rhsc)
387 388 389 390 391 392 393 394 395 396 397 398
					poll_rh = 0;
			} else {
				ohci->autostop = 1;
				ohci->next_statechange = jiffies + HZ;
			}

		/* if no devices have been attached for one second, autostop */
		} else {
			if (changed || any_connected) {
				ohci->autostop = 0;
				ohci->next_statechange = jiffies +
						STATECHANGE_DELAY;
399
			} else if (rhsc && time_after_eq(jiffies,
400 401 402 403 404
						ohci->next_statechange)
					&& !ohci->ed_rm_list
					&& !(ohci->hc_control &
						OHCI_SCHED_ENABLES)) {
				ohci_rh_suspend(ohci, 1);
405
				poll_rh = 0;
406 407 408 409 410 411 412 413 414 415 416 417 418
			}
		}
		break;

	/* if there is a port change, autostart or ask to be resumed */
	case OHCI_USB_SUSPEND:
	case OHCI_USB_RESUME:
		if (changed) {
			if (ohci->autostop)
				ohci_rh_resume(ohci);
			else
				usb_hcd_resume_root_hub(ohci_to_hcd(ohci));
		} else {
419 420 421 422 423 424
			if (!rhsc && (ohci->autostop ||
					ohci_to_hcd(ohci)->self.root_hub->
						do_remote_wakeup))
				ohci_writel(ohci, OHCI_INTR_RHSC,
						&ohci->regs->intrenable);

425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
			/* everything is idle, no need for polling */
			poll_rh = 0;
		}
		break;
	}
	return poll_rh;
}

#else	/* CONFIG_PM */

static inline int ohci_rh_resume(struct ohci_hcd *ohci)
{
	return 0;
}

/* Carry out polling-related state changes.
 * autostop isn't used when CONFIG_PM is turned off.
 */
static int ohci_root_hub_state_changes(struct ohci_hcd *ohci, int changed,
		int any_connected)
{
446
	/* If RHSC is enabled, don't poll */
447
	if (ohci_readl(ohci, &ohci->regs->intrenable) & OHCI_INTR_RHSC)
448 449 450 451 452 453 454 455
		return 0;

	/* If no status changes are pending, enable status-change interrupts */
	if (!changed) {
		ohci_writel(ohci, OHCI_INTR_RHSC, &ohci->regs->intrenable);
		return 0;
	}
	return 1;
456 457
}

458
#endif	/* CONFIG_PM */
L
Linus Torvalds 已提交
459 460 461 462 463 464 465 466 467

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

/* build "status change" packet (one or two bytes) from HC registers */

static int
ohci_hub_status_data (struct usb_hcd *hcd, char *buf)
{
	struct ohci_hcd	*ohci = hcd_to_ohci (hcd);
468
	int		i, changed = 0, length = 1;
469
	int		any_connected = 0;
L
Linus Torvalds 已提交
470 471 472
	unsigned long	flags;

	spin_lock_irqsave (&ohci->lock, flags);
473 474
	if (!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags))
		goto done;
L
Linus Torvalds 已提交
475

476 477 478 479
	/* undocumented erratum seen on at least rev D */
	if ((ohci->flags & OHCI_QUIRK_AMD756)
			&& (roothub_a (ohci) & RH_A_NDP) > MAX_ROOT_PORTS) {
		ohci_warn (ohci, "bogus NDP, rereads as NDP=%d\n",
L
Linus Torvalds 已提交
480 481 482 483 484 485 486 487 488 489
			  ohci_readl (ohci, &ohci->regs->roothub.a) & RH_A_NDP);
		/* retry later; "should not happen" */
		goto done;
	}

	/* init status */
	if (roothub_status (ohci) & (RH_HS_LPSC | RH_HS_OCIC))
		buf [0] = changed = 1;
	else
		buf [0] = 0;
490
	if (ohci->num_ports > 7) {
L
Linus Torvalds 已提交
491 492 493 494
		buf [1] = 0;
		length++;
	}

495 496 497 498 499 500 501
	/* Some broken controllers never turn off RHCS in the interrupt
	 * status register.  For their sake we won't re-enable RHSC
	 * interrupts if the flag is already set.
	 */
	if (ohci_readl(ohci, &ohci->regs->intrstatus) & OHCI_INTR_RHSC)
		changed = 1;

L
Linus Torvalds 已提交
502
	/* look at each port */
503
	for (i = 0; i < ohci->num_ports; i++) {
L
Linus Torvalds 已提交
504 505
		u32	status = roothub_portstatus (ohci, i);

A
Alan Stern 已提交
506 507 508
		/* can't autostop if ports are connected */
		any_connected |= (status & RH_PS_CCS);

L
Linus Torvalds 已提交
509 510 511 512 513 514 515 516
		if (status & (RH_PS_CSC | RH_PS_PESC | RH_PS_PSSC
				| RH_PS_OCIC | RH_PS_PRSC)) {
			changed = 1;
			if (i < 7)
			    buf [0] |= 1 << (i + 1);
			else
			    buf [1] |= 1 << (i - 7);
		}
517
	}
L
Linus Torvalds 已提交
518

519 520
	hcd->poll_rh = ohci_root_hub_state_changes(ohci, changed,
			any_connected);
521

L
Linus Torvalds 已提交
522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541
done:
	spin_unlock_irqrestore (&ohci->lock, flags);

	return changed ? length : 0;
}

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

static void
ohci_hub_descriptor (
	struct ohci_hcd			*ohci,
	struct usb_hub_descriptor	*desc
) {
	u32		rh = roothub_a (ohci);
	u16		temp;

	desc->bDescriptorType = 0x29;
	desc->bPwrOn2PwrGood = (rh & RH_A_POTPGT) >> 24;
	desc->bHubContrCurrent = 0;

542 543
	desc->bNbrPorts = ohci->num_ports;
	temp = 1 + (ohci->num_ports / 8);
L
Linus Torvalds 已提交
544 545 546 547 548
	desc->bDescLength = 7 + 2 * temp;

	temp = 0;
	if (rh & RH_A_NPS)		/* no power switching? */
	    temp |= 0x0002;
549
	if (rh & RH_A_PSM)		/* per-port power switching? */
L
Linus Torvalds 已提交
550 551 552 553 554 555 556 557 558
	    temp |= 0x0001;
	if (rh & RH_A_NOCP)		/* no overcurrent reporting? */
	    temp |= 0x0010;
	else if (rh & RH_A_OCPM)	/* per-port overcurrent reporting? */
	    temp |= 0x0008;
	desc->wHubCharacteristics = (__force __u16)cpu_to_hc16(ohci, temp);

	/* two bitmaps:  ports removable, and usb 1.0 legacy PortPwrCtrlMask */
	rh = roothub_b (ohci);
559
	memset(desc->bitmap, 0xff, sizeof(desc->bitmap));
L
Linus Torvalds 已提交
560
	desc->bitmap [0] = rh & RH_B_DR;
561
	if (ohci->num_ports > 7) {
L
Linus Torvalds 已提交
562
		desc->bitmap [1] = (rh & RH_B_DR) >> 8;
563
		desc->bitmap [2] = 0xff;
L
Linus Torvalds 已提交
564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 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 616 617
	} else
		desc->bitmap [1] = 0xff;
}

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

#ifdef	CONFIG_USB_OTG

static int ohci_start_port_reset (struct usb_hcd *hcd, unsigned port)
{
	struct ohci_hcd	*ohci = hcd_to_ohci (hcd);
	u32			status;

	if (!port)
		return -EINVAL;
	port--;

	/* start port reset before HNP protocol times out */
	status = ohci_readl(ohci, &ohci->regs->roothub.portstatus [port]);
	if (!(status & RH_PS_CCS))
		return -ENODEV;

	/* khubd will finish the reset later */
	ohci_writel(ohci, RH_PS_PRS, &ohci->regs->roothub.portstatus [port]);
	return 0;
}

#else

#define	ohci_start_port_reset		NULL

#endif

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


/* See usb 7.1.7.5:  root hubs must issue at least 50 msec reset signaling,
 * not necessarily continuous ... to guard against resume signaling.
 * The short timeout is safe for non-root hubs, and is backward-compatible
 * with earlier Linux hosts.
 */
#ifdef	CONFIG_USB_SUSPEND
#define	PORT_RESET_MSEC		50
#else
#define	PORT_RESET_MSEC		10
#endif

/* this timer value might be vendor-specific ... */
#define	PORT_RESET_HW_MSEC	10

/* wrap-aware logic morphed from <linux/jiffies.h> */
#define tick_before(t1,t2) ((s16)(((s16)(t1))-((s16)(t2))) < 0)

/* called from some task, normally khubd */
618
static inline int root_port_reset (struct ohci_hcd *ohci, unsigned port)
L
Linus Torvalds 已提交
619 620
{
	__hc32 __iomem *portstat = &ohci->regs->roothub.portstatus [port];
621
	u32	temp = 0;
L
Linus Torvalds 已提交
622 623
	u16	now = ohci_readl(ohci, &ohci->regs->fmnumber);
	u16	reset_done = now + PORT_RESET_MSEC;
624
	int	limit_1 = DIV_ROUND_UP(PORT_RESET_MSEC, PORT_RESET_HW_MSEC);
L
Linus Torvalds 已提交
625 626 627 628 629 630

	/* build a "continuous enough" reset signal, with up to
	 * 3msec gap between pulses.  scheduler HZ==100 must work;
	 * this might need to be deadline-scheduled.
	 */
	do {
631 632
		int limit_2;

L
Linus Torvalds 已提交
633
		/* spin until any current reset finishes */
634 635
		limit_2 = PORT_RESET_HW_MSEC * 2;
		while (--limit_2 >= 0) {
L
Linus Torvalds 已提交
636
			temp = ohci_readl (ohci, portstat);
637 638 639
			/* handle e.g. CardBus eject */
			if (temp == ~(u32)0)
				return -ESHUTDOWN;
L
Linus Torvalds 已提交
640 641 642
			if (!(temp & RH_PS_PRS))
				break;
			udelay (500);
643
		}
L
Linus Torvalds 已提交
644

645 646 647 648 649 650 651 652 653 654 655
		/* timeout (a hardware error) has been observed when
		 * EHCI sets CF while this driver is resetting a port;
		 * presumably other disconnect paths might do it too.
		 */
		if (limit_2 < 0) {
			ohci_dbg(ohci,
				"port[%d] reset timeout, stat %08x\n",
				port, temp);
			break;
		}

L
Linus Torvalds 已提交
656 657 658 659 660 661 662 663 664
		if (!(temp & RH_PS_CCS))
			break;
		if (temp & RH_PS_PRSC)
			ohci_writel (ohci, RH_PS_PRSC, portstat);

		/* start the next reset, sleep till it's probably done */
		ohci_writel (ohci, RH_PS_PRS, portstat);
		msleep(PORT_RESET_HW_MSEC);
		now = ohci_readl(ohci, &ohci->regs->fmnumber);
665 666 667 668 669
	} while (tick_before(now, reset_done) && --limit_1 >= 0);

	/* caller synchronizes using PRSC ... and handles PRS
	 * still being set when this returns.
	 */
670 671

	return 0;
L
Linus Torvalds 已提交
672 673 674 675 676 677 678 679 680 681 682 683 684 685 686
}

static int ohci_hub_control (
	struct usb_hcd	*hcd,
	u16		typeReq,
	u16		wValue,
	u16		wIndex,
	char		*buf,
	u16		wLength
) {
	struct ohci_hcd	*ohci = hcd_to_ohci (hcd);
	int		ports = hcd_to_bus (hcd)->root_hub->maxchild;
	u32		temp;
	int		retval = 0;

687 688 689
	if (unlikely(!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)))
		return -ESHUTDOWN;

L
Linus Torvalds 已提交
690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743
	switch (typeReq) {
	case ClearHubFeature:
		switch (wValue) {
		case C_HUB_OVER_CURRENT:
			ohci_writel (ohci, RH_HS_OCIC,
					&ohci->regs->roothub.status);
		case C_HUB_LOCAL_POWER:
			break;
		default:
			goto error;
		}
		break;
	case ClearPortFeature:
		if (!wIndex || wIndex > ports)
			goto error;
		wIndex--;

		switch (wValue) {
		case USB_PORT_FEAT_ENABLE:
			temp = RH_PS_CCS;
			break;
		case USB_PORT_FEAT_C_ENABLE:
			temp = RH_PS_PESC;
			break;
		case USB_PORT_FEAT_SUSPEND:
			temp = RH_PS_POCI;
			break;
		case USB_PORT_FEAT_C_SUSPEND:
			temp = RH_PS_PSSC;
			break;
		case USB_PORT_FEAT_POWER:
			temp = RH_PS_LSDA;
			break;
		case USB_PORT_FEAT_C_CONNECTION:
			temp = RH_PS_CSC;
			break;
		case USB_PORT_FEAT_C_OVER_CURRENT:
			temp = RH_PS_OCIC;
			break;
		case USB_PORT_FEAT_C_RESET:
			temp = RH_PS_PRSC;
			break;
		default:
			goto error;
		}
		ohci_writel (ohci, temp,
				&ohci->regs->roothub.portstatus [wIndex]);
		// ohci_readl (ohci, &ohci->regs->roothub.portstatus [wIndex]);
		break;
	case GetHubDescriptor:
		ohci_hub_descriptor (ohci, (struct usb_hub_descriptor *) buf);
		break;
	case GetHubStatus:
		temp = roothub_status (ohci) & ~(RH_HS_CRWE | RH_HS_DRWE);
744
		put_unaligned_le32(temp, buf);
L
Linus Torvalds 已提交
745 746 747 748 749 750
		break;
	case GetPortStatus:
		if (!wIndex || wIndex > ports)
			goto error;
		wIndex--;
		temp = roothub_portstatus (ohci, wIndex);
751
		put_unaligned_le32(temp, buf);
L
Linus Torvalds 已提交
752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776

#ifndef	OHCI_VERBOSE_DEBUG
	if (*(u16*)(buf+2))	/* only if wPortChange is interesting */
#endif
		dbg_port (ohci, "GetStatus", wIndex, temp);
		break;
	case SetHubFeature:
		switch (wValue) {
		case C_HUB_OVER_CURRENT:
			// FIXME:  this can be cleared, yes?
		case C_HUB_LOCAL_POWER:
			break;
		default:
			goto error;
		}
		break;
	case SetPortFeature:
		if (!wIndex || wIndex > ports)
			goto error;
		wIndex--;
		switch (wValue) {
		case USB_PORT_FEAT_SUSPEND:
#ifdef	CONFIG_USB_OTG
			if (hcd->self.otg_port == (wIndex + 1)
					&& hcd->self.b_hnp_enable)
777
				ohci->start_hnp(ohci);
L
Linus Torvalds 已提交
778 779 780 781 782 783 784 785 786 787
			else
#endif
			ohci_writel (ohci, RH_PS_PSS,
				&ohci->regs->roothub.portstatus [wIndex]);
			break;
		case USB_PORT_FEAT_POWER:
			ohci_writel (ohci, RH_PS_PPS,
				&ohci->regs->roothub.portstatus [wIndex]);
			break;
		case USB_PORT_FEAT_RESET:
788
			retval = root_port_reset (ohci, wIndex);
L
Linus Torvalds 已提交
789 790 791 792 793 794 795 796 797 798 799 800 801 802
			break;
		default:
			goto error;
		}
		break;

	default:
error:
		/* "protocol stall" on error */
		retval = -EPIPE;
	}
	return retval;
}