ehci-hub.c 36 KB

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