ehci-hub.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351
  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. static unsigned long ehci_get_resuming_ports(struct usb_hcd *hcd)
  437. {
  438. struct ehci_hcd *ehci = hcd_to_ehci(hcd);
  439. return ehci->resuming_ports;
  440. }
  441. #else
  442. #define ehci_bus_suspend NULL
  443. #define ehci_bus_resume NULL
  444. #define ehci_get_resuming_ports NULL
  445. #endif /* CONFIG_PM */
  446. /*-------------------------------------------------------------------------*/
  447. /*
  448. * Sets the owner of a port
  449. */
  450. static void set_owner(struct ehci_hcd *ehci, int portnum, int new_owner)
  451. {
  452. u32 __iomem *status_reg;
  453. u32 port_status;
  454. int try;
  455. status_reg = &ehci->regs->port_status[portnum];
  456. /*
  457. * The controller won't set the OWNER bit if the port is
  458. * enabled, so this loop will sometimes require at least two
  459. * iterations: one to disable the port and one to set OWNER.
  460. */
  461. for (try = 4; try > 0; --try) {
  462. spin_lock_irq(&ehci->lock);
  463. port_status = ehci_readl(ehci, status_reg);
  464. if ((port_status & PORT_OWNER) == new_owner
  465. || (port_status & (PORT_OWNER | PORT_CONNECT))
  466. == 0)
  467. try = 0;
  468. else {
  469. port_status ^= PORT_OWNER;
  470. port_status &= ~(PORT_PE | PORT_RWC_BITS);
  471. ehci_writel(ehci, port_status, status_reg);
  472. }
  473. spin_unlock_irq(&ehci->lock);
  474. if (try > 1)
  475. msleep(5);
  476. }
  477. }
  478. /*-------------------------------------------------------------------------*/
  479. static int check_reset_complete (
  480. struct ehci_hcd *ehci,
  481. int index,
  482. u32 __iomem *status_reg,
  483. int port_status
  484. ) {
  485. if (!(port_status & PORT_CONNECT))
  486. return port_status;
  487. /* if reset finished and it's still not enabled -- handoff */
  488. if (!(port_status & PORT_PE)) {
  489. /* with integrated TT, there's nobody to hand it to! */
  490. if (ehci_is_TDI(ehci)) {
  491. ehci_dbg (ehci,
  492. "Failed to enable port %d on root hub TT\n",
  493. index+1);
  494. return port_status;
  495. }
  496. ehci_dbg (ehci, "port %d full speed --> companion\n",
  497. index + 1);
  498. // what happens if HCS_N_CC(params) == 0 ?
  499. port_status |= PORT_OWNER;
  500. port_status &= ~PORT_RWC_BITS;
  501. ehci_writel(ehci, port_status, status_reg);
  502. /* ensure 440EPX ohci controller state is operational */
  503. if (ehci->has_amcc_usb23)
  504. set_ohci_hcfs(ehci, 1);
  505. } else {
  506. ehci_dbg(ehci, "port %d reset complete, port enabled\n",
  507. index + 1);
  508. /* ensure 440EPx ohci controller state is suspended */
  509. if (ehci->has_amcc_usb23)
  510. set_ohci_hcfs(ehci, 0);
  511. }
  512. return port_status;
  513. }
  514. /*-------------------------------------------------------------------------*/
  515. /* build "status change" packet (one or two bytes) from HC registers */
  516. static int
  517. ehci_hub_status_data (struct usb_hcd *hcd, char *buf)
  518. {
  519. struct ehci_hcd *ehci = hcd_to_ehci (hcd);
  520. u32 temp, status;
  521. u32 mask;
  522. int ports, i, retval = 1;
  523. unsigned long flags;
  524. u32 ppcd = ~0;
  525. /* init status to no-changes */
  526. buf [0] = 0;
  527. ports = HCS_N_PORTS (ehci->hcs_params);
  528. if (ports > 7) {
  529. buf [1] = 0;
  530. retval++;
  531. }
  532. /* Inform the core about resumes-in-progress by returning
  533. * a non-zero value even if there are no status changes.
  534. */
  535. status = ehci->resuming_ports;
  536. /* Some boards (mostly VIA?) report bogus overcurrent indications,
  537. * causing massive log spam unless we completely ignore them. It
  538. * may be relevant that VIA VT8235 controllers, where PORT_POWER is
  539. * always set, seem to clear PORT_OCC and PORT_CSC when writing to
  540. * PORT_POWER; that's surprising, but maybe within-spec.
  541. */
  542. if (!ignore_oc)
  543. mask = PORT_CSC | PORT_PEC | PORT_OCC;
  544. else
  545. mask = PORT_CSC | PORT_PEC;
  546. // PORT_RESUME from hardware ~= PORT_STAT_C_SUSPEND
  547. /* no hub change reports (bit 0) for now (power, ...) */
  548. /* port N changes (bit N)? */
  549. spin_lock_irqsave (&ehci->lock, flags);
  550. /* get per-port change detect bits */
  551. if (ehci->has_ppcd)
  552. ppcd = ehci_readl(ehci, &ehci->regs->status) >> 16;
  553. for (i = 0; i < ports; i++) {
  554. /* leverage per-port change bits feature */
  555. if (ppcd & (1 << i))
  556. temp = ehci_readl(ehci, &ehci->regs->port_status[i]);
  557. else
  558. temp = 0;
  559. /*
  560. * Return status information even for ports with OWNER set.
  561. * Otherwise hub_wq wouldn't see the disconnect event when a
  562. * high-speed device is switched over to the companion
  563. * controller by the user.
  564. */
  565. if ((temp & mask) != 0 || test_bit(i, &ehci->port_c_suspend)
  566. || (ehci->reset_done[i] && time_after_eq(
  567. jiffies, ehci->reset_done[i]))) {
  568. if (i < 7)
  569. buf [0] |= 1 << (i + 1);
  570. else
  571. buf [1] |= 1 << (i - 7);
  572. status = STS_PCD;
  573. }
  574. }
  575. /* If a resume is in progress, make sure it can finish */
  576. if (ehci->resuming_ports)
  577. mod_timer(&hcd->rh_timer, jiffies + msecs_to_jiffies(25));
  578. spin_unlock_irqrestore (&ehci->lock, flags);
  579. return status ? retval : 0;
  580. }
  581. /*-------------------------------------------------------------------------*/
  582. static void
  583. ehci_hub_descriptor (
  584. struct ehci_hcd *ehci,
  585. struct usb_hub_descriptor *desc
  586. ) {
  587. int ports = HCS_N_PORTS (ehci->hcs_params);
  588. u16 temp;
  589. desc->bDescriptorType = USB_DT_HUB;
  590. desc->bPwrOn2PwrGood = 10; /* ehci 1.0, 2.3.9 says 20ms max */
  591. desc->bHubContrCurrent = 0;
  592. desc->bNbrPorts = ports;
  593. temp = 1 + (ports / 8);
  594. desc->bDescLength = 7 + 2 * temp;
  595. /* two bitmaps: ports removable, and usb 1.0 legacy PortPwrCtrlMask */
  596. memset(&desc->u.hs.DeviceRemovable[0], 0, temp);
  597. memset(&desc->u.hs.DeviceRemovable[temp], 0xff, temp);
  598. temp = HUB_CHAR_INDV_PORT_OCPM; /* per-port overcurrent reporting */
  599. if (HCS_PPC (ehci->hcs_params))
  600. temp |= HUB_CHAR_INDV_PORT_LPSM; /* per-port power control */
  601. else
  602. temp |= HUB_CHAR_NO_LPSM; /* no power switching */
  603. #if 0
  604. // re-enable when we support USB_PORT_FEAT_INDICATOR below.
  605. if (HCS_INDICATOR (ehci->hcs_params))
  606. temp |= HUB_CHAR_PORTIND; /* per-port indicators (LEDs) */
  607. #endif
  608. desc->wHubCharacteristics = cpu_to_le16(temp);
  609. }
  610. /*-------------------------------------------------------------------------*/
  611. #ifdef CONFIG_USB_HCD_TEST_MODE
  612. #define EHSET_TEST_SINGLE_STEP_SET_FEATURE 0x06
  613. static void usb_ehset_completion(struct urb *urb)
  614. {
  615. struct completion *done = urb->context;
  616. complete(done);
  617. }
  618. static int submit_single_step_set_feature(
  619. struct usb_hcd *hcd,
  620. struct urb *urb,
  621. int is_setup
  622. );
  623. /*
  624. * Allocate and initialize a control URB. This request will be used by the
  625. * EHSET SINGLE_STEP_SET_FEATURE test in which the DATA and STATUS stages
  626. * of the GetDescriptor request are sent 15 seconds after the SETUP stage.
  627. * Return NULL if failed.
  628. */
  629. static struct urb *request_single_step_set_feature_urb(
  630. struct usb_device *udev,
  631. void *dr,
  632. void *buf,
  633. struct completion *done
  634. ) {
  635. struct urb *urb;
  636. struct usb_hcd *hcd = bus_to_hcd(udev->bus);
  637. struct usb_host_endpoint *ep;
  638. urb = usb_alloc_urb(0, GFP_KERNEL);
  639. if (!urb)
  640. return NULL;
  641. urb->pipe = usb_rcvctrlpipe(udev, 0);
  642. ep = (usb_pipein(urb->pipe) ? udev->ep_in : udev->ep_out)
  643. [usb_pipeendpoint(urb->pipe)];
  644. if (!ep) {
  645. usb_free_urb(urb);
  646. return NULL;
  647. }
  648. urb->ep = ep;
  649. urb->dev = udev;
  650. urb->setup_packet = (void *)dr;
  651. urb->transfer_buffer = buf;
  652. urb->transfer_buffer_length = USB_DT_DEVICE_SIZE;
  653. urb->complete = usb_ehset_completion;
  654. urb->status = -EINPROGRESS;
  655. urb->actual_length = 0;
  656. urb->transfer_flags = URB_DIR_IN;
  657. usb_get_urb(urb);
  658. atomic_inc(&urb->use_count);
  659. atomic_inc(&urb->dev->urbnum);
  660. urb->setup_dma = dma_map_single(
  661. hcd->self.sysdev,
  662. urb->setup_packet,
  663. sizeof(struct usb_ctrlrequest),
  664. DMA_TO_DEVICE);
  665. urb->transfer_dma = dma_map_single(
  666. hcd->self.sysdev,
  667. urb->transfer_buffer,
  668. urb->transfer_buffer_length,
  669. DMA_FROM_DEVICE);
  670. urb->context = done;
  671. return urb;
  672. }
  673. static int ehset_single_step_set_feature(struct usb_hcd *hcd, int port)
  674. {
  675. int retval = -ENOMEM;
  676. struct usb_ctrlrequest *dr;
  677. struct urb *urb;
  678. struct usb_device *udev;
  679. struct ehci_hcd *ehci = hcd_to_ehci(hcd);
  680. struct usb_device_descriptor *buf;
  681. DECLARE_COMPLETION_ONSTACK(done);
  682. /* Obtain udev of the rhub's child port */
  683. udev = usb_hub_find_child(hcd->self.root_hub, port);
  684. if (!udev) {
  685. ehci_err(ehci, "No device attached to the RootHub\n");
  686. return -ENODEV;
  687. }
  688. buf = kmalloc(USB_DT_DEVICE_SIZE, GFP_KERNEL);
  689. if (!buf)
  690. return -ENOMEM;
  691. dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
  692. if (!dr) {
  693. kfree(buf);
  694. return -ENOMEM;
  695. }
  696. /* Fill Setup packet for GetDescriptor */
  697. dr->bRequestType = USB_DIR_IN;
  698. dr->bRequest = USB_REQ_GET_DESCRIPTOR;
  699. dr->wValue = cpu_to_le16(USB_DT_DEVICE << 8);
  700. dr->wIndex = 0;
  701. dr->wLength = cpu_to_le16(USB_DT_DEVICE_SIZE);
  702. urb = request_single_step_set_feature_urb(udev, dr, buf, &done);
  703. if (!urb)
  704. goto cleanup;
  705. /* Submit just the SETUP stage */
  706. retval = submit_single_step_set_feature(hcd, urb, 1);
  707. if (retval)
  708. goto out1;
  709. if (!wait_for_completion_timeout(&done, msecs_to_jiffies(2000))) {
  710. usb_kill_urb(urb);
  711. retval = -ETIMEDOUT;
  712. ehci_err(ehci, "%s SETUP stage timed out on ep0\n", __func__);
  713. goto out1;
  714. }
  715. msleep(15 * 1000);
  716. /* Complete remaining DATA and STATUS stages using the same URB */
  717. urb->status = -EINPROGRESS;
  718. usb_get_urb(urb);
  719. atomic_inc(&urb->use_count);
  720. atomic_inc(&urb->dev->urbnum);
  721. retval = submit_single_step_set_feature(hcd, urb, 0);
  722. if (!retval && !wait_for_completion_timeout(&done,
  723. msecs_to_jiffies(2000))) {
  724. usb_kill_urb(urb);
  725. retval = -ETIMEDOUT;
  726. ehci_err(ehci, "%s IN stage timed out on ep0\n", __func__);
  727. }
  728. out1:
  729. usb_free_urb(urb);
  730. cleanup:
  731. kfree(dr);
  732. kfree(buf);
  733. return retval;
  734. }
  735. #endif /* CONFIG_USB_HCD_TEST_MODE */
  736. /*-------------------------------------------------------------------------*/
  737. int ehci_hub_control(
  738. struct usb_hcd *hcd,
  739. u16 typeReq,
  740. u16 wValue,
  741. u16 wIndex,
  742. char *buf,
  743. u16 wLength
  744. ) {
  745. struct ehci_hcd *ehci = hcd_to_ehci (hcd);
  746. int ports = HCS_N_PORTS (ehci->hcs_params);
  747. u32 __iomem *status_reg, *hostpc_reg;
  748. u32 temp, temp1, status;
  749. unsigned long flags;
  750. int retval = 0;
  751. unsigned selector;
  752. /*
  753. * Avoid underflow while calculating (wIndex & 0xff) - 1.
  754. * The compiler might deduce that wIndex can never be 0 and then
  755. * optimize away the tests for !wIndex below.
  756. */
  757. temp = wIndex & 0xff;
  758. temp -= (temp > 0);
  759. status_reg = &ehci->regs->port_status[temp];
  760. hostpc_reg = &ehci->regs->hostpc[temp];
  761. /*
  762. * FIXME: support SetPortFeatures USB_PORT_FEAT_INDICATOR.
  763. * HCS_INDICATOR may say we can change LEDs to off/amber/green.
  764. * (track current state ourselves) ... blink for diagnostics,
  765. * power, "this is the one", etc. EHCI spec supports this.
  766. */
  767. spin_lock_irqsave (&ehci->lock, flags);
  768. switch (typeReq) {
  769. case ClearHubFeature:
  770. switch (wValue) {
  771. case C_HUB_LOCAL_POWER:
  772. case C_HUB_OVER_CURRENT:
  773. /* no hub-wide feature/status flags */
  774. break;
  775. default:
  776. goto error;
  777. }
  778. break;
  779. case ClearPortFeature:
  780. if (!wIndex || wIndex > ports)
  781. goto error;
  782. wIndex--;
  783. temp = ehci_readl(ehci, status_reg);
  784. temp &= ~PORT_RWC_BITS;
  785. /*
  786. * Even if OWNER is set, so the port is owned by the
  787. * companion controller, hub_wq needs to be able to clear
  788. * the port-change status bits (especially
  789. * USB_PORT_STAT_C_CONNECTION).
  790. */
  791. switch (wValue) {
  792. case USB_PORT_FEAT_ENABLE:
  793. ehci_writel(ehci, temp & ~PORT_PE, status_reg);
  794. break;
  795. case USB_PORT_FEAT_C_ENABLE:
  796. ehci_writel(ehci, temp | PORT_PEC, status_reg);
  797. break;
  798. case USB_PORT_FEAT_SUSPEND:
  799. if (temp & PORT_RESET)
  800. goto error;
  801. if (ehci->no_selective_suspend)
  802. break;
  803. #ifdef CONFIG_USB_OTG
  804. if ((hcd->self.otg_port == (wIndex + 1))
  805. && hcd->self.b_hnp_enable) {
  806. otg_start_hnp(hcd->usb_phy->otg);
  807. break;
  808. }
  809. #endif
  810. if (!(temp & PORT_SUSPEND))
  811. break;
  812. if ((temp & PORT_PE) == 0)
  813. goto error;
  814. /* clear phy low-power mode before resume */
  815. if (ehci->has_tdi_phy_lpm) {
  816. temp1 = ehci_readl(ehci, hostpc_reg);
  817. ehci_writel(ehci, temp1 & ~HOSTPC_PHCD,
  818. hostpc_reg);
  819. spin_unlock_irqrestore(&ehci->lock, flags);
  820. msleep(5);/* wait to leave low-power mode */
  821. spin_lock_irqsave(&ehci->lock, flags);
  822. }
  823. /* resume signaling for 20 msec */
  824. temp &= ~PORT_WAKE_BITS;
  825. ehci_writel(ehci, temp | PORT_RESUME, status_reg);
  826. ehci->reset_done[wIndex] = jiffies
  827. + msecs_to_jiffies(USB_RESUME_TIMEOUT);
  828. set_bit(wIndex, &ehci->resuming_ports);
  829. usb_hcd_start_port_resume(&hcd->self, wIndex);
  830. break;
  831. case USB_PORT_FEAT_C_SUSPEND:
  832. clear_bit(wIndex, &ehci->port_c_suspend);
  833. break;
  834. case USB_PORT_FEAT_POWER:
  835. if (HCS_PPC(ehci->hcs_params)) {
  836. spin_unlock_irqrestore(&ehci->lock, flags);
  837. ehci_port_power(ehci, wIndex, false);
  838. spin_lock_irqsave(&ehci->lock, flags);
  839. }
  840. break;
  841. case USB_PORT_FEAT_C_CONNECTION:
  842. ehci_writel(ehci, temp | PORT_CSC, status_reg);
  843. break;
  844. case USB_PORT_FEAT_C_OVER_CURRENT:
  845. ehci_writel(ehci, temp | PORT_OCC, status_reg);
  846. break;
  847. case USB_PORT_FEAT_C_RESET:
  848. /* GetPortStatus clears reset */
  849. break;
  850. default:
  851. goto error;
  852. }
  853. ehci_readl(ehci, &ehci->regs->command); /* unblock posted write */
  854. break;
  855. case GetHubDescriptor:
  856. ehci_hub_descriptor (ehci, (struct usb_hub_descriptor *)
  857. buf);
  858. break;
  859. case GetHubStatus:
  860. /* no hub-wide feature/status flags */
  861. memset (buf, 0, 4);
  862. //cpu_to_le32s ((u32 *) buf);
  863. break;
  864. case GetPortStatus:
  865. if (!wIndex || wIndex > ports)
  866. goto error;
  867. wIndex--;
  868. status = 0;
  869. temp = ehci_readl(ehci, status_reg);
  870. // wPortChange bits
  871. if (temp & PORT_CSC)
  872. status |= USB_PORT_STAT_C_CONNECTION << 16;
  873. if (temp & PORT_PEC)
  874. status |= USB_PORT_STAT_C_ENABLE << 16;
  875. if ((temp & PORT_OCC) && !ignore_oc){
  876. status |= USB_PORT_STAT_C_OVERCURRENT << 16;
  877. /*
  878. * Hubs should disable port power on over-current.
  879. * However, not all EHCI implementations do this
  880. * automatically, even if they _do_ support per-port
  881. * power switching; they're allowed to just limit the
  882. * current. hub_wq will turn the power back on.
  883. */
  884. if (((temp & PORT_OC) || (ehci->need_oc_pp_cycle))
  885. && HCS_PPC(ehci->hcs_params)) {
  886. spin_unlock_irqrestore(&ehci->lock, flags);
  887. ehci_port_power(ehci, wIndex, false);
  888. spin_lock_irqsave(&ehci->lock, flags);
  889. temp = ehci_readl(ehci, status_reg);
  890. }
  891. }
  892. /* no reset or resume pending */
  893. if (!ehci->reset_done[wIndex]) {
  894. /* Remote Wakeup received? */
  895. if (temp & PORT_RESUME) {
  896. /* resume signaling for 20 msec */
  897. ehci->reset_done[wIndex] = jiffies
  898. + msecs_to_jiffies(20);
  899. usb_hcd_start_port_resume(&hcd->self, wIndex);
  900. set_bit(wIndex, &ehci->resuming_ports);
  901. /* check the port again */
  902. mod_timer(&ehci_to_hcd(ehci)->rh_timer,
  903. ehci->reset_done[wIndex]);
  904. }
  905. /* reset or resume not yet complete */
  906. } else if (!time_after_eq(jiffies, ehci->reset_done[wIndex])) {
  907. ; /* wait until it is complete */
  908. /* resume completed */
  909. } else if (test_bit(wIndex, &ehci->resuming_ports)) {
  910. clear_bit(wIndex, &ehci->suspended_ports);
  911. set_bit(wIndex, &ehci->port_c_suspend);
  912. ehci->reset_done[wIndex] = 0;
  913. usb_hcd_end_port_resume(&hcd->self, wIndex);
  914. /* stop resume signaling */
  915. temp &= ~(PORT_RWC_BITS | PORT_SUSPEND | PORT_RESUME);
  916. ehci_writel(ehci, temp, status_reg);
  917. clear_bit(wIndex, &ehci->resuming_ports);
  918. retval = ehci_handshake(ehci, status_reg,
  919. PORT_RESUME, 0, 2000 /* 2msec */);
  920. if (retval != 0) {
  921. ehci_err(ehci, "port %d resume error %d\n",
  922. wIndex + 1, retval);
  923. goto error;
  924. }
  925. temp = ehci_readl(ehci, status_reg);
  926. /* whoever resets must GetPortStatus to complete it!! */
  927. } else {
  928. status |= USB_PORT_STAT_C_RESET << 16;
  929. ehci->reset_done [wIndex] = 0;
  930. /* force reset to complete */
  931. ehci_writel(ehci, temp & ~(PORT_RWC_BITS | PORT_RESET),
  932. status_reg);
  933. /* REVISIT: some hardware needs 550+ usec to clear
  934. * this bit; seems too long to spin routinely...
  935. */
  936. retval = ehci_handshake(ehci, status_reg,
  937. PORT_RESET, 0, 1000);
  938. if (retval != 0) {
  939. ehci_err (ehci, "port %d reset error %d\n",
  940. wIndex + 1, retval);
  941. goto error;
  942. }
  943. /* see what we found out */
  944. temp = check_reset_complete (ehci, wIndex, status_reg,
  945. ehci_readl(ehci, status_reg));
  946. }
  947. /* transfer dedicated ports to the companion hc */
  948. if ((temp & PORT_CONNECT) &&
  949. test_bit(wIndex, &ehci->companion_ports)) {
  950. temp &= ~PORT_RWC_BITS;
  951. temp |= PORT_OWNER;
  952. ehci_writel(ehci, temp, status_reg);
  953. ehci_dbg(ehci, "port %d --> companion\n", wIndex + 1);
  954. temp = ehci_readl(ehci, status_reg);
  955. }
  956. /*
  957. * Even if OWNER is set, there's no harm letting hub_wq
  958. * see the wPortStatus values (they should all be 0 except
  959. * for PORT_POWER anyway).
  960. */
  961. if (temp & PORT_CONNECT) {
  962. status |= USB_PORT_STAT_CONNECTION;
  963. // status may be from integrated TT
  964. if (ehci->has_hostpc) {
  965. temp1 = ehci_readl(ehci, hostpc_reg);
  966. status |= ehci_port_speed(ehci, temp1);
  967. } else
  968. status |= ehci_port_speed(ehci, temp);
  969. }
  970. if (temp & PORT_PE)
  971. status |= USB_PORT_STAT_ENABLE;
  972. /* maybe the port was unsuspended without our knowledge */
  973. if (temp & (PORT_SUSPEND|PORT_RESUME)) {
  974. status |= USB_PORT_STAT_SUSPEND;
  975. } else if (test_bit(wIndex, &ehci->suspended_ports)) {
  976. clear_bit(wIndex, &ehci->suspended_ports);
  977. clear_bit(wIndex, &ehci->resuming_ports);
  978. ehci->reset_done[wIndex] = 0;
  979. if (temp & PORT_PE)
  980. set_bit(wIndex, &ehci->port_c_suspend);
  981. usb_hcd_end_port_resume(&hcd->self, wIndex);
  982. }
  983. if (temp & PORT_OC)
  984. status |= USB_PORT_STAT_OVERCURRENT;
  985. if (temp & PORT_RESET)
  986. status |= USB_PORT_STAT_RESET;
  987. if (temp & PORT_POWER)
  988. status |= USB_PORT_STAT_POWER;
  989. if (test_bit(wIndex, &ehci->port_c_suspend))
  990. status |= USB_PORT_STAT_C_SUSPEND << 16;
  991. if (status & ~0xffff) /* only if wPortChange is interesting */
  992. dbg_port(ehci, "GetStatus", wIndex + 1, temp);
  993. put_unaligned_le32(status, buf);
  994. break;
  995. case SetHubFeature:
  996. switch (wValue) {
  997. case C_HUB_LOCAL_POWER:
  998. case C_HUB_OVER_CURRENT:
  999. /* no hub-wide feature/status flags */
  1000. break;
  1001. default:
  1002. goto error;
  1003. }
  1004. break;
  1005. case SetPortFeature:
  1006. selector = wIndex >> 8;
  1007. wIndex &= 0xff;
  1008. if (unlikely(ehci->debug)) {
  1009. /* If the debug port is active any port
  1010. * feature requests should get denied */
  1011. if (wIndex == HCS_DEBUG_PORT(ehci->hcs_params) &&
  1012. (readl(&ehci->debug->control) & DBGP_ENABLED)) {
  1013. retval = -ENODEV;
  1014. goto error_exit;
  1015. }
  1016. }
  1017. if (!wIndex || wIndex > ports)
  1018. goto error;
  1019. wIndex--;
  1020. temp = ehci_readl(ehci, status_reg);
  1021. if (temp & PORT_OWNER)
  1022. break;
  1023. temp &= ~PORT_RWC_BITS;
  1024. switch (wValue) {
  1025. case USB_PORT_FEAT_SUSPEND:
  1026. if (ehci->no_selective_suspend)
  1027. break;
  1028. if ((temp & PORT_PE) == 0
  1029. || (temp & PORT_RESET) != 0)
  1030. goto error;
  1031. /* After above check the port must be connected.
  1032. * Set appropriate bit thus could put phy into low power
  1033. * mode if we have tdi_phy_lpm feature
  1034. */
  1035. temp &= ~PORT_WKCONN_E;
  1036. temp |= PORT_WKDISC_E | PORT_WKOC_E;
  1037. ehci_writel(ehci, temp | PORT_SUSPEND, status_reg);
  1038. if (ehci->has_tdi_phy_lpm) {
  1039. spin_unlock_irqrestore(&ehci->lock, flags);
  1040. msleep(5);/* 5ms for HCD enter low pwr mode */
  1041. spin_lock_irqsave(&ehci->lock, flags);
  1042. temp1 = ehci_readl(ehci, hostpc_reg);
  1043. ehci_writel(ehci, temp1 | HOSTPC_PHCD,
  1044. hostpc_reg);
  1045. temp1 = ehci_readl(ehci, hostpc_reg);
  1046. ehci_dbg(ehci, "Port%d phy low pwr mode %s\n",
  1047. wIndex, (temp1 & HOSTPC_PHCD) ?
  1048. "succeeded" : "failed");
  1049. }
  1050. if (ehci_has_fsl_susp_errata(ehci)) {
  1051. /* 10ms for HCD enter suspend */
  1052. spin_unlock_irqrestore(&ehci->lock, flags);
  1053. usleep_range(10000, 20000);
  1054. spin_lock_irqsave(&ehci->lock, flags);
  1055. }
  1056. set_bit(wIndex, &ehci->suspended_ports);
  1057. break;
  1058. case USB_PORT_FEAT_POWER:
  1059. if (HCS_PPC(ehci->hcs_params)) {
  1060. spin_unlock_irqrestore(&ehci->lock, flags);
  1061. ehci_port_power(ehci, wIndex, true);
  1062. spin_lock_irqsave(&ehci->lock, flags);
  1063. }
  1064. break;
  1065. case USB_PORT_FEAT_RESET:
  1066. if (temp & (PORT_SUSPEND|PORT_RESUME))
  1067. goto error;
  1068. /* line status bits may report this as low speed,
  1069. * which can be fine if this root hub has a
  1070. * transaction translator built in.
  1071. */
  1072. if ((temp & (PORT_PE|PORT_CONNECT)) == PORT_CONNECT
  1073. && !ehci_is_TDI(ehci)
  1074. && PORT_USB11 (temp)) {
  1075. ehci_dbg (ehci,
  1076. "port %d low speed --> companion\n",
  1077. wIndex + 1);
  1078. temp |= PORT_OWNER;
  1079. } else {
  1080. temp |= PORT_RESET;
  1081. temp &= ~PORT_PE;
  1082. /*
  1083. * caller must wait, then call GetPortStatus
  1084. * usb 2.0 spec says 50 ms resets on root
  1085. */
  1086. ehci->reset_done [wIndex] = jiffies
  1087. + msecs_to_jiffies (50);
  1088. /*
  1089. * Force full-speed connect for FSL high-speed
  1090. * erratum; disable HS Chirp by setting PFSC bit
  1091. */
  1092. if (ehci_has_fsl_hs_errata(ehci))
  1093. temp |= (1 << PORTSC_FSL_PFSC);
  1094. }
  1095. ehci_writel(ehci, temp, status_reg);
  1096. break;
  1097. /* For downstream facing ports (these): one hub port is put
  1098. * into test mode according to USB2 11.24.2.13, then the hub
  1099. * must be reset (which for root hub now means rmmod+modprobe,
  1100. * or else system reboot). See EHCI 2.3.9 and 4.14 for info
  1101. * about the EHCI-specific stuff.
  1102. */
  1103. case USB_PORT_FEAT_TEST:
  1104. #ifdef CONFIG_USB_HCD_TEST_MODE
  1105. if (selector == EHSET_TEST_SINGLE_STEP_SET_FEATURE) {
  1106. spin_unlock_irqrestore(&ehci->lock, flags);
  1107. retval = ehset_single_step_set_feature(hcd,
  1108. wIndex + 1);
  1109. spin_lock_irqsave(&ehci->lock, flags);
  1110. break;
  1111. }
  1112. #endif
  1113. if (!selector || selector > 5)
  1114. goto error;
  1115. spin_unlock_irqrestore(&ehci->lock, flags);
  1116. ehci_quiesce(ehci);
  1117. spin_lock_irqsave(&ehci->lock, flags);
  1118. /* Put all enabled ports into suspend */
  1119. while (ports--) {
  1120. u32 __iomem *sreg =
  1121. &ehci->regs->port_status[ports];
  1122. temp = ehci_readl(ehci, sreg) & ~PORT_RWC_BITS;
  1123. if (temp & PORT_PE)
  1124. ehci_writel(ehci, temp | PORT_SUSPEND,
  1125. sreg);
  1126. }
  1127. spin_unlock_irqrestore(&ehci->lock, flags);
  1128. ehci_halt(ehci);
  1129. spin_lock_irqsave(&ehci->lock, flags);
  1130. temp = ehci_readl(ehci, status_reg);
  1131. temp |= selector << 16;
  1132. ehci_writel(ehci, temp, status_reg);
  1133. break;
  1134. default:
  1135. goto error;
  1136. }
  1137. ehci_readl(ehci, &ehci->regs->command); /* unblock posted writes */
  1138. break;
  1139. default:
  1140. error:
  1141. /* "stall" on error */
  1142. retval = -EPIPE;
  1143. }
  1144. error_exit:
  1145. spin_unlock_irqrestore (&ehci->lock, flags);
  1146. return retval;
  1147. }
  1148. EXPORT_SYMBOL_GPL(ehci_hub_control);
  1149. static void ehci_relinquish_port(struct usb_hcd *hcd, int portnum)
  1150. {
  1151. struct ehci_hcd *ehci = hcd_to_ehci(hcd);
  1152. if (ehci_is_TDI(ehci))
  1153. return;
  1154. set_owner(ehci, --portnum, PORT_OWNER);
  1155. }
  1156. static int ehci_port_handed_over(struct usb_hcd *hcd, int portnum)
  1157. {
  1158. struct ehci_hcd *ehci = hcd_to_ehci(hcd);
  1159. u32 __iomem *reg;
  1160. if (ehci_is_TDI(ehci))
  1161. return 0;
  1162. reg = &ehci->regs->port_status[portnum - 1];
  1163. return ehci_readl(ehci, reg) & PORT_OWNER;
  1164. }
  1165. static int ehci_port_power(struct ehci_hcd *ehci, int portnum, bool enable)
  1166. {
  1167. struct usb_hcd *hcd = ehci_to_hcd(ehci);
  1168. u32 __iomem *status_reg = &ehci->regs->port_status[portnum];
  1169. u32 temp = ehci_readl(ehci, status_reg) & ~PORT_RWC_BITS;
  1170. if (enable)
  1171. ehci_writel(ehci, temp | PORT_POWER, status_reg);
  1172. else
  1173. ehci_writel(ehci, temp & ~PORT_POWER, status_reg);
  1174. if (hcd->driver->port_power)
  1175. hcd->driver->port_power(hcd, portnum, enable);
  1176. return 0;
  1177. }