ehci-hub.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318
  1. /*
  2. * Copyright (C) 2001-2004 by David Brownell
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License as published by the
  6. * Free Software Foundation; either version 2 of the License, or (at your
  7. * option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  11. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. * for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software Foundation,
  16. * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. /* this file is part of ehci-hcd.c */
  19. /*-------------------------------------------------------------------------*/
  20. /*
  21. * EHCI Root Hub ... the nonsharable stuff
  22. *
  23. * Registers don't need cpu_to_le32, that happens transparently
  24. */
  25. /*-------------------------------------------------------------------------*/
  26. #include <linux/usb/otg.h>
  27. #define PORT_WAKE_BITS (PORT_WKOC_E|PORT_WKDISC_E|PORT_WKCONN_E)
  28. #ifdef CONFIG_PM
  29. static int persist_enabled_on_companion(struct usb_device *udev, void *unused)
  30. {
  31. return !udev->maxchild && udev->persist_enabled &&
  32. udev->bus->root_hub->speed < USB_SPEED_HIGH;
  33. }
  34. /* After a power loss, ports that were owned by the companion must be
  35. * reset so that the companion can still own them.
  36. */
  37. static void ehci_handover_companion_ports(struct ehci_hcd *ehci)
  38. {
  39. u32 __iomem *reg;
  40. u32 status;
  41. int port;
  42. __le32 buf;
  43. struct usb_hcd *hcd = ehci_to_hcd(ehci);
  44. if (!ehci->owned_ports)
  45. return;
  46. /*
  47. * USB 1.1 devices are mostly HIDs, which don't need to persist across
  48. * suspends. If we ensure that none of our companion's devices have
  49. * persist_enabled (by looking through all USB 1.1 buses in the system),
  50. * we can skip this and avoid slowing resume down. Devices without
  51. * persist will just get reenumerated shortly after resume anyway.
  52. */
  53. if (!usb_for_each_dev(NULL, persist_enabled_on_companion))
  54. return;
  55. /* Make sure the ports are powered */
  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. if (!(status & PORT_POWER))
  62. ehci_port_power(ehci, port, true);
  63. }
  64. }
  65. /* Give the connections some time to appear */
  66. msleep(20);
  67. spin_lock_irq(&ehci->lock);
  68. port = HCS_N_PORTS(ehci->hcs_params);
  69. while (port--) {
  70. if (test_bit(port, &ehci->owned_ports)) {
  71. reg = &ehci->regs->port_status[port];
  72. status = ehci_readl(ehci, reg) & ~PORT_RWC_BITS;
  73. /* Port already owned by companion? */
  74. if (status & PORT_OWNER)
  75. clear_bit(port, &ehci->owned_ports);
  76. else if (test_bit(port, &ehci->companion_ports))
  77. ehci_writel(ehci, status & ~PORT_PE, reg);
  78. else {
  79. spin_unlock_irq(&ehci->lock);
  80. ehci_hub_control(hcd, SetPortFeature,
  81. USB_PORT_FEAT_RESET, port + 1,
  82. NULL, 0);
  83. spin_lock_irq(&ehci->lock);
  84. }
  85. }
  86. }
  87. spin_unlock_irq(&ehci->lock);
  88. if (!ehci->owned_ports)
  89. return;
  90. msleep(90); /* Wait for resets to complete */
  91. spin_lock_irq(&ehci->lock);
  92. port = HCS_N_PORTS(ehci->hcs_params);
  93. while (port--) {
  94. if (test_bit(port, &ehci->owned_ports)) {
  95. spin_unlock_irq(&ehci->lock);
  96. ehci_hub_control(hcd, GetPortStatus,
  97. 0, port + 1,
  98. (char *) &buf, sizeof(buf));
  99. spin_lock_irq(&ehci->lock);
  100. /* The companion should now own the port,
  101. * but if something went wrong the port must not
  102. * remain enabled.
  103. */
  104. reg = &ehci->regs->port_status[port];
  105. status = ehci_readl(ehci, reg) & ~PORT_RWC_BITS;
  106. if (status & PORT_OWNER)
  107. ehci_writel(ehci, status | PORT_CSC, reg);
  108. else {
  109. ehci_dbg(ehci, "failed handover port %d: %x\n",
  110. port + 1, status);
  111. ehci_writel(ehci, status & ~PORT_PE, reg);
  112. }
  113. }
  114. }
  115. ehci->owned_ports = 0;
  116. spin_unlock_irq(&ehci->lock);
  117. }
  118. static int ehci_port_change(struct ehci_hcd *ehci)
  119. {
  120. int i = HCS_N_PORTS(ehci->hcs_params);
  121. /* First check if the controller indicates a change event */
  122. if (ehci_readl(ehci, &ehci->regs->status) & STS_PCD)
  123. return 1;
  124. /*
  125. * Not all controllers appear to update this while going from D3 to D0,
  126. * so check the individual port status registers as well
  127. */
  128. while (i--)
  129. if (ehci_readl(ehci, &ehci->regs->port_status[i]) & PORT_CSC)
  130. return 1;
  131. return 0;
  132. }
  133. static void ehci_adjust_port_wakeup_flags(struct ehci_hcd *ehci,
  134. bool suspending, bool do_wakeup)
  135. {
  136. int port;
  137. u32 temp;
  138. /* If remote wakeup is enabled for the root hub but disabled
  139. * for the controller, we must adjust all the port wakeup flags
  140. * when the controller is suspended or resumed. In all other
  141. * cases they don't need to be changed.
  142. */
  143. if (!ehci_to_hcd(ehci)->self.root_hub->do_remote_wakeup || do_wakeup)
  144. return;
  145. spin_lock_irq(&ehci->lock);
  146. /* clear phy low-power mode before changing wakeup flags */
  147. if (ehci->has_tdi_phy_lpm) {
  148. port = HCS_N_PORTS(ehci->hcs_params);
  149. while (port--) {
  150. u32 __iomem *hostpc_reg = &ehci->regs->hostpc[port];
  151. temp = ehci_readl(ehci, hostpc_reg);
  152. ehci_writel(ehci, temp & ~HOSTPC_PHCD, hostpc_reg);
  153. }
  154. spin_unlock_irq(&ehci->lock);
  155. msleep(5);
  156. spin_lock_irq(&ehci->lock);
  157. }
  158. port = HCS_N_PORTS(ehci->hcs_params);
  159. while (port--) {
  160. u32 __iomem *reg = &ehci->regs->port_status[port];
  161. u32 t1 = ehci_readl(ehci, reg) & ~PORT_RWC_BITS;
  162. u32 t2 = t1 & ~PORT_WAKE_BITS;
  163. /* If we are suspending the controller, clear the flags.
  164. * If we are resuming the controller, set the wakeup flags.
  165. */
  166. if (!suspending) {
  167. if (t1 & PORT_CONNECT)
  168. t2 |= PORT_WKOC_E | PORT_WKDISC_E;
  169. else
  170. t2 |= PORT_WKOC_E | PORT_WKCONN_E;
  171. }
  172. ehci_writel(ehci, t2, reg);
  173. }
  174. /* enter phy low-power mode again */
  175. if (ehci->has_tdi_phy_lpm) {
  176. port = HCS_N_PORTS(ehci->hcs_params);
  177. while (port--) {
  178. u32 __iomem *hostpc_reg = &ehci->regs->hostpc[port];
  179. temp = ehci_readl(ehci, hostpc_reg);
  180. ehci_writel(ehci, temp | HOSTPC_PHCD, hostpc_reg);
  181. }
  182. }
  183. /* Does the root hub have a port wakeup pending? */
  184. if (!suspending && ehci_port_change(ehci))
  185. usb_hcd_resume_root_hub(ehci_to_hcd(ehci));
  186. spin_unlock_irq(&ehci->lock);
  187. }
  188. static int ehci_bus_suspend (struct usb_hcd *hcd)
  189. {
  190. struct ehci_hcd *ehci = hcd_to_ehci (hcd);
  191. int port;
  192. int mask;
  193. int changed;
  194. bool fs_idle_delay;
  195. ehci_dbg(ehci, "suspend root hub\n");
  196. if (time_before (jiffies, ehci->next_statechange))
  197. msleep(5);
  198. /* stop the schedules */
  199. ehci_quiesce(ehci);
  200. spin_lock_irq (&ehci->lock);
  201. if (ehci->rh_state < EHCI_RH_RUNNING)
  202. goto done;
  203. /* Once the controller is stopped, port resumes that are already
  204. * in progress won't complete. Hence if remote wakeup is enabled
  205. * for the root hub and any ports are in the middle of a resume or
  206. * remote wakeup, we must fail the suspend.
  207. */
  208. if (hcd->self.root_hub->do_remote_wakeup) {
  209. if (ehci->resuming_ports) {
  210. spin_unlock_irq(&ehci->lock);
  211. ehci_dbg(ehci, "suspend failed because a port is resuming\n");
  212. return -EBUSY;
  213. }
  214. }
  215. /* Unlike other USB host controller types, EHCI doesn't have
  216. * any notion of "global" or bus-wide suspend. The driver has
  217. * to manually suspend all the active unsuspended ports, and
  218. * then manually resume them in the bus_resume() routine.
  219. */
  220. ehci->bus_suspended = 0;
  221. ehci->owned_ports = 0;
  222. changed = 0;
  223. fs_idle_delay = false;
  224. port = HCS_N_PORTS(ehci->hcs_params);
  225. while (port--) {
  226. u32 __iomem *reg = &ehci->regs->port_status [port];
  227. u32 t1 = ehci_readl(ehci, reg) & ~PORT_RWC_BITS;
  228. u32 t2 = t1 & ~PORT_WAKE_BITS;
  229. /* keep track of which ports we suspend */
  230. if (t1 & PORT_OWNER)
  231. set_bit(port, &ehci->owned_ports);
  232. else if ((t1 & PORT_PE) && !(t1 & PORT_SUSPEND)) {
  233. t2 |= PORT_SUSPEND;
  234. set_bit(port, &ehci->bus_suspended);
  235. }
  236. /* enable remote wakeup on all ports, if told to do so */
  237. if (hcd->self.root_hub->do_remote_wakeup) {
  238. /* only enable appropriate wake bits, otherwise the
  239. * hardware can not go phy low power mode. If a race
  240. * condition happens here(connection change during bits
  241. * set), the port change detection will finally fix it.
  242. */
  243. if (t1 & PORT_CONNECT)
  244. t2 |= PORT_WKOC_E | PORT_WKDISC_E;
  245. else
  246. t2 |= PORT_WKOC_E | PORT_WKCONN_E;
  247. }
  248. if (t1 != t2) {
  249. /*
  250. * On some controllers, Wake-On-Disconnect will
  251. * generate false wakeup signals until the bus
  252. * switches over to full-speed idle. For their
  253. * sake, add a delay if we need one.
  254. */
  255. if ((t2 & PORT_WKDISC_E) &&
  256. ehci_port_speed(ehci, t2) ==
  257. USB_PORT_STAT_HIGH_SPEED)
  258. fs_idle_delay = true;
  259. ehci_writel(ehci, t2, reg);
  260. changed = 1;
  261. }
  262. }
  263. spin_unlock_irq(&ehci->lock);
  264. if ((changed && ehci->has_tdi_phy_lpm) || fs_idle_delay) {
  265. /*
  266. * Wait for HCD to enter low-power mode or for the bus
  267. * to switch to full-speed idle.
  268. */
  269. usleep_range(5000, 5500);
  270. }
  271. if (changed && ehci->has_tdi_phy_lpm) {
  272. spin_lock_irq(&ehci->lock);
  273. port = HCS_N_PORTS(ehci->hcs_params);
  274. while (port--) {
  275. u32 __iomem *hostpc_reg = &ehci->regs->hostpc[port];
  276. u32 t3;
  277. t3 = ehci_readl(ehci, hostpc_reg);
  278. ehci_writel(ehci, t3 | HOSTPC_PHCD, hostpc_reg);
  279. t3 = ehci_readl(ehci, hostpc_reg);
  280. ehci_dbg(ehci, "Port %d phy low-power mode %s\n",
  281. port, (t3 & HOSTPC_PHCD) ?
  282. "succeeded" : "failed");
  283. }
  284. spin_unlock_irq(&ehci->lock);
  285. }
  286. /* Apparently some devices need a >= 1-uframe delay here */
  287. if (ehci->bus_suspended)
  288. udelay(150);
  289. /* turn off now-idle HC */
  290. ehci_halt (ehci);
  291. spin_lock_irq(&ehci->lock);
  292. if (ehci->enabled_hrtimer_events & BIT(EHCI_HRTIMER_POLL_DEAD))
  293. ehci_handle_controller_death(ehci);
  294. if (ehci->rh_state != EHCI_RH_RUNNING)
  295. goto done;
  296. ehci->rh_state = EHCI_RH_SUSPENDED;
  297. end_unlink_async(ehci);
  298. unlink_empty_async_suspended(ehci);
  299. ehci_handle_start_intr_unlinks(ehci);
  300. ehci_handle_intr_unlinks(ehci);
  301. end_free_itds(ehci);
  302. /* allow remote wakeup */
  303. mask = INTR_MASK;
  304. if (!hcd->self.root_hub->do_remote_wakeup)
  305. mask &= ~STS_PCD;
  306. ehci_writel(ehci, mask, &ehci->regs->intr_enable);
  307. ehci_readl(ehci, &ehci->regs->intr_enable);
  308. done:
  309. ehci->next_statechange = jiffies + msecs_to_jiffies(10);
  310. ehci->enabled_hrtimer_events = 0;
  311. ehci->next_hrtimer_event = EHCI_HRTIMER_NO_EVENT;
  312. spin_unlock_irq (&ehci->lock);
  313. hrtimer_cancel(&ehci->hrtimer);
  314. return 0;
  315. }
  316. /* caller has locked the root hub, and should reset/reinit on error */
  317. static int ehci_bus_resume (struct usb_hcd *hcd)
  318. {
  319. struct ehci_hcd *ehci = hcd_to_ehci (hcd);
  320. u32 temp;
  321. u32 power_okay;
  322. int i;
  323. unsigned long resume_needed = 0;
  324. if (time_before (jiffies, ehci->next_statechange))
  325. msleep(5);
  326. spin_lock_irq (&ehci->lock);
  327. if (!HCD_HW_ACCESSIBLE(hcd) || ehci->shutdown)
  328. goto shutdown;
  329. if (unlikely(ehci->debug)) {
  330. if (!dbgp_reset_prep(hcd))
  331. ehci->debug = NULL;
  332. else
  333. dbgp_external_startup(hcd);
  334. }
  335. /* Ideally and we've got a real resume here, and no port's power
  336. * was lost. (For PCI, that means Vaux was maintained.) But we
  337. * could instead be restoring a swsusp snapshot -- so that BIOS was
  338. * the last user of the controller, not reset/pm hardware keeping
  339. * state we gave to it.
  340. */
  341. power_okay = ehci_readl(ehci, &ehci->regs->intr_enable);
  342. ehci_dbg(ehci, "resume root hub%s\n",
  343. power_okay ? "" : " after power loss");
  344. /* at least some APM implementations will try to deliver
  345. * IRQs right away, so delay them until we're ready.
  346. */
  347. ehci_writel(ehci, 0, &ehci->regs->intr_enable);
  348. /* re-init operational registers */
  349. ehci_writel(ehci, 0, &ehci->regs->segment);
  350. ehci_writel(ehci, ehci->periodic_dma, &ehci->regs->frame_list);
  351. ehci_writel(ehci, (u32) ehci->async->qh_dma, &ehci->regs->async_next);
  352. /* restore CMD_RUN, framelist size, and irq threshold */
  353. ehci->command |= CMD_RUN;
  354. ehci_writel(ehci, ehci->command, &ehci->regs->command);
  355. ehci->rh_state = EHCI_RH_RUNNING;
  356. /*
  357. * According to Bugzilla #8190, the port status for some controllers
  358. * will be wrong without a delay. At their wrong status, the port
  359. * is enabled, but not suspended neither resumed.
  360. */
  361. i = HCS_N_PORTS(ehci->hcs_params);
  362. while (i--) {
  363. temp = ehci_readl(ehci, &ehci->regs->port_status[i]);
  364. if ((temp & PORT_PE) &&
  365. !(temp & (PORT_SUSPEND | PORT_RESUME))) {
  366. ehci_dbg(ehci, "Port status(0x%x) is wrong\n", temp);
  367. spin_unlock_irq(&ehci->lock);
  368. msleep(8);
  369. spin_lock_irq(&ehci->lock);
  370. break;
  371. }
  372. }
  373. if (ehci->shutdown)
  374. goto shutdown;
  375. /* clear phy low-power mode before resume */
  376. if (ehci->bus_suspended && ehci->has_tdi_phy_lpm) {
  377. i = HCS_N_PORTS(ehci->hcs_params);
  378. while (i--) {
  379. if (test_bit(i, &ehci->bus_suspended)) {
  380. u32 __iomem *hostpc_reg =
  381. &ehci->regs->hostpc[i];
  382. temp = ehci_readl(ehci, hostpc_reg);
  383. ehci_writel(ehci, temp & ~HOSTPC_PHCD,
  384. hostpc_reg);
  385. }
  386. }
  387. spin_unlock_irq(&ehci->lock);
  388. msleep(5);
  389. spin_lock_irq(&ehci->lock);
  390. if (ehci->shutdown)
  391. goto shutdown;
  392. }
  393. /* manually resume the ports we suspended during bus_suspend() */
  394. i = HCS_N_PORTS (ehci->hcs_params);
  395. while (i--) {
  396. temp = ehci_readl(ehci, &ehci->regs->port_status [i]);
  397. temp &= ~(PORT_RWC_BITS | PORT_WAKE_BITS);
  398. if (test_bit(i, &ehci->bus_suspended) &&
  399. (temp & PORT_SUSPEND)) {
  400. temp |= PORT_RESUME;
  401. set_bit(i, &resume_needed);
  402. }
  403. ehci_writel(ehci, temp, &ehci->regs->port_status [i]);
  404. }
  405. /* msleep for 20ms only if code is trying to resume port */
  406. if (resume_needed) {
  407. spin_unlock_irq(&ehci->lock);
  408. msleep(20);
  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 = 0x29;
  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 = 0x0008; /* per-port overcurrent reporting */
  593. if (HCS_PPC (ehci->hcs_params))
  594. temp |= 0x0001; /* per-port power control */
  595. else
  596. temp |= 0x0002; /* 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 |= 0x0080; /* 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 = &ehci->regs->port_status[
  742. (wIndex & 0xff) - 1];
  743. u32 __iomem *hostpc_reg = &ehci->regs->hostpc[(wIndex & 0xff) - 1];
  744. u32 temp, temp1, status;
  745. unsigned long flags;
  746. int retval = 0;
  747. unsigned selector;
  748. /*
  749. * FIXME: support SetPortFeatures USB_PORT_FEAT_INDICATOR.
  750. * HCS_INDICATOR may say we can change LEDs to off/amber/green.
  751. * (track current state ourselves) ... blink for diagnostics,
  752. * power, "this is the one", etc. EHCI spec supports this.
  753. */
  754. spin_lock_irqsave (&ehci->lock, flags);
  755. switch (typeReq) {
  756. case ClearHubFeature:
  757. switch (wValue) {
  758. case C_HUB_LOCAL_POWER:
  759. case C_HUB_OVER_CURRENT:
  760. /* no hub-wide feature/status flags */
  761. break;
  762. default:
  763. goto error;
  764. }
  765. break;
  766. case ClearPortFeature:
  767. if (!wIndex || wIndex > ports)
  768. goto error;
  769. wIndex--;
  770. temp = ehci_readl(ehci, status_reg);
  771. temp &= ~PORT_RWC_BITS;
  772. /*
  773. * Even if OWNER is set, so the port is owned by the
  774. * companion controller, hub_wq needs to be able to clear
  775. * the port-change status bits (especially
  776. * USB_PORT_STAT_C_CONNECTION).
  777. */
  778. switch (wValue) {
  779. case USB_PORT_FEAT_ENABLE:
  780. ehci_writel(ehci, temp & ~PORT_PE, status_reg);
  781. break;
  782. case USB_PORT_FEAT_C_ENABLE:
  783. ehci_writel(ehci, temp | PORT_PEC, status_reg);
  784. break;
  785. case USB_PORT_FEAT_SUSPEND:
  786. if (temp & PORT_RESET)
  787. goto error;
  788. if (ehci->no_selective_suspend)
  789. break;
  790. #ifdef CONFIG_USB_OTG
  791. if ((hcd->self.otg_port == (wIndex + 1))
  792. && hcd->self.b_hnp_enable) {
  793. otg_start_hnp(hcd->usb_phy->otg);
  794. break;
  795. }
  796. #endif
  797. if (!(temp & PORT_SUSPEND))
  798. break;
  799. if ((temp & PORT_PE) == 0)
  800. goto error;
  801. /* clear phy low-power mode before resume */
  802. if (ehci->has_tdi_phy_lpm) {
  803. temp1 = ehci_readl(ehci, hostpc_reg);
  804. ehci_writel(ehci, temp1 & ~HOSTPC_PHCD,
  805. hostpc_reg);
  806. spin_unlock_irqrestore(&ehci->lock, flags);
  807. msleep(5);/* wait to leave low-power mode */
  808. spin_lock_irqsave(&ehci->lock, flags);
  809. }
  810. /* resume signaling for 20 msec */
  811. temp &= ~PORT_WAKE_BITS;
  812. ehci_writel(ehci, temp | PORT_RESUME, status_reg);
  813. ehci->reset_done[wIndex] = jiffies
  814. + msecs_to_jiffies(20);
  815. set_bit(wIndex, &ehci->resuming_ports);
  816. usb_hcd_start_port_resume(&hcd->self, wIndex);
  817. break;
  818. case USB_PORT_FEAT_C_SUSPEND:
  819. clear_bit(wIndex, &ehci->port_c_suspend);
  820. break;
  821. case USB_PORT_FEAT_POWER:
  822. if (HCS_PPC(ehci->hcs_params)) {
  823. spin_unlock_irqrestore(&ehci->lock, flags);
  824. ehci_port_power(ehci, wIndex, false);
  825. spin_lock_irqsave(&ehci->lock, flags);
  826. }
  827. break;
  828. case USB_PORT_FEAT_C_CONNECTION:
  829. ehci_writel(ehci, temp | PORT_CSC, status_reg);
  830. break;
  831. case USB_PORT_FEAT_C_OVER_CURRENT:
  832. ehci_writel(ehci, temp | PORT_OCC, status_reg);
  833. break;
  834. case USB_PORT_FEAT_C_RESET:
  835. /* GetPortStatus clears reset */
  836. break;
  837. default:
  838. goto error;
  839. }
  840. ehci_readl(ehci, &ehci->regs->command); /* unblock posted write */
  841. break;
  842. case GetHubDescriptor:
  843. ehci_hub_descriptor (ehci, (struct usb_hub_descriptor *)
  844. buf);
  845. break;
  846. case GetHubStatus:
  847. /* no hub-wide feature/status flags */
  848. memset (buf, 0, 4);
  849. //cpu_to_le32s ((u32 *) buf);
  850. break;
  851. case GetPortStatus:
  852. if (!wIndex || wIndex > ports)
  853. goto error;
  854. wIndex--;
  855. status = 0;
  856. temp = ehci_readl(ehci, status_reg);
  857. // wPortChange bits
  858. if (temp & PORT_CSC)
  859. status |= USB_PORT_STAT_C_CONNECTION << 16;
  860. if (temp & PORT_PEC)
  861. status |= USB_PORT_STAT_C_ENABLE << 16;
  862. if ((temp & PORT_OCC) && !ignore_oc){
  863. status |= USB_PORT_STAT_C_OVERCURRENT << 16;
  864. /*
  865. * Hubs should disable port power on over-current.
  866. * However, not all EHCI implementations do this
  867. * automatically, even if they _do_ support per-port
  868. * power switching; they're allowed to just limit the
  869. * current. hub_wq will turn the power back on.
  870. */
  871. if (((temp & PORT_OC) || (ehci->need_oc_pp_cycle))
  872. && HCS_PPC(ehci->hcs_params)) {
  873. spin_unlock_irqrestore(&ehci->lock, flags);
  874. ehci_port_power(ehci, wIndex, false);
  875. spin_lock_irqsave(&ehci->lock, flags);
  876. temp = ehci_readl(ehci, status_reg);
  877. }
  878. }
  879. /* no reset or resume pending */
  880. if (!ehci->reset_done[wIndex]) {
  881. /* Remote Wakeup received? */
  882. if (temp & PORT_RESUME) {
  883. /* resume signaling for 20 msec */
  884. ehci->reset_done[wIndex] = jiffies
  885. + msecs_to_jiffies(20);
  886. usb_hcd_start_port_resume(&hcd->self, wIndex);
  887. set_bit(wIndex, &ehci->resuming_ports);
  888. /* check the port again */
  889. mod_timer(&ehci_to_hcd(ehci)->rh_timer,
  890. ehci->reset_done[wIndex]);
  891. }
  892. /* reset or resume not yet complete */
  893. } else if (!time_after_eq(jiffies, ehci->reset_done[wIndex])) {
  894. ; /* wait until it is complete */
  895. /* resume completed */
  896. } else if (test_bit(wIndex, &ehci->resuming_ports)) {
  897. clear_bit(wIndex, &ehci->suspended_ports);
  898. set_bit(wIndex, &ehci->port_c_suspend);
  899. ehci->reset_done[wIndex] = 0;
  900. usb_hcd_end_port_resume(&hcd->self, wIndex);
  901. /* stop resume signaling */
  902. temp &= ~(PORT_RWC_BITS | PORT_SUSPEND | PORT_RESUME);
  903. ehci_writel(ehci, temp, status_reg);
  904. clear_bit(wIndex, &ehci->resuming_ports);
  905. retval = ehci_handshake(ehci, status_reg,
  906. PORT_RESUME, 0, 2000 /* 2msec */);
  907. if (retval != 0) {
  908. ehci_err(ehci, "port %d resume error %d\n",
  909. wIndex + 1, retval);
  910. goto error;
  911. }
  912. temp = ehci_readl(ehci, status_reg);
  913. /* whoever resets must GetPortStatus to complete it!! */
  914. } else {
  915. status |= USB_PORT_STAT_C_RESET << 16;
  916. ehci->reset_done [wIndex] = 0;
  917. /* force reset to complete */
  918. ehci_writel(ehci, temp & ~(PORT_RWC_BITS | PORT_RESET),
  919. status_reg);
  920. /* REVISIT: some hardware needs 550+ usec to clear
  921. * this bit; seems too long to spin routinely...
  922. */
  923. retval = ehci_handshake(ehci, status_reg,
  924. PORT_RESET, 0, 1000);
  925. if (retval != 0) {
  926. ehci_err (ehci, "port %d reset error %d\n",
  927. wIndex + 1, retval);
  928. goto error;
  929. }
  930. /* see what we found out */
  931. temp = check_reset_complete (ehci, wIndex, status_reg,
  932. ehci_readl(ehci, status_reg));
  933. }
  934. /* transfer dedicated ports to the companion hc */
  935. if ((temp & PORT_CONNECT) &&
  936. test_bit(wIndex, &ehci->companion_ports)) {
  937. temp &= ~PORT_RWC_BITS;
  938. temp |= PORT_OWNER;
  939. ehci_writel(ehci, temp, status_reg);
  940. ehci_dbg(ehci, "port %d --> companion\n", wIndex + 1);
  941. temp = ehci_readl(ehci, status_reg);
  942. }
  943. /*
  944. * Even if OWNER is set, there's no harm letting hub_wq
  945. * see the wPortStatus values (they should all be 0 except
  946. * for PORT_POWER anyway).
  947. */
  948. if (temp & PORT_CONNECT) {
  949. status |= USB_PORT_STAT_CONNECTION;
  950. // status may be from integrated TT
  951. if (ehci->has_hostpc) {
  952. temp1 = ehci_readl(ehci, hostpc_reg);
  953. status |= ehci_port_speed(ehci, temp1);
  954. } else
  955. status |= ehci_port_speed(ehci, temp);
  956. }
  957. if (temp & PORT_PE)
  958. status |= USB_PORT_STAT_ENABLE;
  959. /* maybe the port was unsuspended without our knowledge */
  960. if (temp & (PORT_SUSPEND|PORT_RESUME)) {
  961. status |= USB_PORT_STAT_SUSPEND;
  962. } else if (test_bit(wIndex, &ehci->suspended_ports)) {
  963. clear_bit(wIndex, &ehci->suspended_ports);
  964. clear_bit(wIndex, &ehci->resuming_ports);
  965. ehci->reset_done[wIndex] = 0;
  966. if (temp & PORT_PE)
  967. set_bit(wIndex, &ehci->port_c_suspend);
  968. usb_hcd_end_port_resume(&hcd->self, wIndex);
  969. }
  970. if (temp & PORT_OC)
  971. status |= USB_PORT_STAT_OVERCURRENT;
  972. if (temp & PORT_RESET)
  973. status |= USB_PORT_STAT_RESET;
  974. if (temp & PORT_POWER)
  975. status |= USB_PORT_STAT_POWER;
  976. if (test_bit(wIndex, &ehci->port_c_suspend))
  977. status |= USB_PORT_STAT_C_SUSPEND << 16;
  978. if (status & ~0xffff) /* only if wPortChange is interesting */
  979. dbg_port(ehci, "GetStatus", wIndex + 1, temp);
  980. put_unaligned_le32(status, buf);
  981. break;
  982. case SetHubFeature:
  983. switch (wValue) {
  984. case C_HUB_LOCAL_POWER:
  985. case C_HUB_OVER_CURRENT:
  986. /* no hub-wide feature/status flags */
  987. break;
  988. default:
  989. goto error;
  990. }
  991. break;
  992. case SetPortFeature:
  993. selector = wIndex >> 8;
  994. wIndex &= 0xff;
  995. if (unlikely(ehci->debug)) {
  996. /* If the debug port is active any port
  997. * feature requests should get denied */
  998. if (wIndex == HCS_DEBUG_PORT(ehci->hcs_params) &&
  999. (readl(&ehci->debug->control) & DBGP_ENABLED)) {
  1000. retval = -ENODEV;
  1001. goto error_exit;
  1002. }
  1003. }
  1004. if (!wIndex || wIndex > ports)
  1005. goto error;
  1006. wIndex--;
  1007. temp = ehci_readl(ehci, status_reg);
  1008. if (temp & PORT_OWNER)
  1009. break;
  1010. temp &= ~PORT_RWC_BITS;
  1011. switch (wValue) {
  1012. case USB_PORT_FEAT_SUSPEND:
  1013. if (ehci->no_selective_suspend)
  1014. break;
  1015. if ((temp & PORT_PE) == 0
  1016. || (temp & PORT_RESET) != 0)
  1017. goto error;
  1018. /* After above check the port must be connected.
  1019. * Set appropriate bit thus could put phy into low power
  1020. * mode if we have tdi_phy_lpm feature
  1021. */
  1022. temp &= ~PORT_WKCONN_E;
  1023. temp |= PORT_WKDISC_E | PORT_WKOC_E;
  1024. ehci_writel(ehci, temp | PORT_SUSPEND, status_reg);
  1025. if (ehci->has_tdi_phy_lpm) {
  1026. spin_unlock_irqrestore(&ehci->lock, flags);
  1027. msleep(5);/* 5ms for HCD enter low pwr mode */
  1028. spin_lock_irqsave(&ehci->lock, flags);
  1029. temp1 = ehci_readl(ehci, hostpc_reg);
  1030. ehci_writel(ehci, temp1 | HOSTPC_PHCD,
  1031. hostpc_reg);
  1032. temp1 = ehci_readl(ehci, hostpc_reg);
  1033. ehci_dbg(ehci, "Port%d phy low pwr mode %s\n",
  1034. wIndex, (temp1 & HOSTPC_PHCD) ?
  1035. "succeeded" : "failed");
  1036. }
  1037. set_bit(wIndex, &ehci->suspended_ports);
  1038. break;
  1039. case USB_PORT_FEAT_POWER:
  1040. if (HCS_PPC(ehci->hcs_params)) {
  1041. spin_unlock_irqrestore(&ehci->lock, flags);
  1042. ehci_port_power(ehci, wIndex, true);
  1043. spin_lock_irqsave(&ehci->lock, flags);
  1044. }
  1045. break;
  1046. case USB_PORT_FEAT_RESET:
  1047. if (temp & (PORT_SUSPEND|PORT_RESUME))
  1048. goto error;
  1049. /* line status bits may report this as low speed,
  1050. * which can be fine if this root hub has a
  1051. * transaction translator built in.
  1052. */
  1053. if ((temp & (PORT_PE|PORT_CONNECT)) == PORT_CONNECT
  1054. && !ehci_is_TDI(ehci)
  1055. && PORT_USB11 (temp)) {
  1056. ehci_dbg (ehci,
  1057. "port %d low speed --> companion\n",
  1058. wIndex + 1);
  1059. temp |= PORT_OWNER;
  1060. } else {
  1061. temp |= PORT_RESET;
  1062. temp &= ~PORT_PE;
  1063. /*
  1064. * caller must wait, then call GetPortStatus
  1065. * usb 2.0 spec says 50 ms resets on root
  1066. */
  1067. ehci->reset_done [wIndex] = jiffies
  1068. + msecs_to_jiffies (50);
  1069. }
  1070. ehci_writel(ehci, temp, status_reg);
  1071. break;
  1072. /* For downstream facing ports (these): one hub port is put
  1073. * into test mode according to USB2 11.24.2.13, then the hub
  1074. * must be reset (which for root hub now means rmmod+modprobe,
  1075. * or else system reboot). See EHCI 2.3.9 and 4.14 for info
  1076. * about the EHCI-specific stuff.
  1077. */
  1078. case USB_PORT_FEAT_TEST:
  1079. #ifdef CONFIG_USB_HCD_TEST_MODE
  1080. if (selector == EHSET_TEST_SINGLE_STEP_SET_FEATURE) {
  1081. spin_unlock_irqrestore(&ehci->lock, flags);
  1082. retval = ehset_single_step_set_feature(hcd,
  1083. wIndex + 1);
  1084. spin_lock_irqsave(&ehci->lock, flags);
  1085. break;
  1086. }
  1087. #endif
  1088. if (!selector || selector > 5)
  1089. goto error;
  1090. spin_unlock_irqrestore(&ehci->lock, flags);
  1091. ehci_quiesce(ehci);
  1092. spin_lock_irqsave(&ehci->lock, flags);
  1093. /* Put all enabled ports into suspend */
  1094. while (ports--) {
  1095. u32 __iomem *sreg =
  1096. &ehci->regs->port_status[ports];
  1097. temp = ehci_readl(ehci, sreg) & ~PORT_RWC_BITS;
  1098. if (temp & PORT_PE)
  1099. ehci_writel(ehci, temp | PORT_SUSPEND,
  1100. sreg);
  1101. }
  1102. spin_unlock_irqrestore(&ehci->lock, flags);
  1103. ehci_halt(ehci);
  1104. spin_lock_irqsave(&ehci->lock, flags);
  1105. temp = ehci_readl(ehci, status_reg);
  1106. temp |= selector << 16;
  1107. ehci_writel(ehci, temp, status_reg);
  1108. break;
  1109. default:
  1110. goto error;
  1111. }
  1112. ehci_readl(ehci, &ehci->regs->command); /* unblock posted writes */
  1113. break;
  1114. default:
  1115. error:
  1116. /* "stall" on error */
  1117. retval = -EPIPE;
  1118. }
  1119. error_exit:
  1120. spin_unlock_irqrestore (&ehci->lock, flags);
  1121. return retval;
  1122. }
  1123. EXPORT_SYMBOL_GPL(ehci_hub_control);
  1124. static void ehci_relinquish_port(struct usb_hcd *hcd, int portnum)
  1125. {
  1126. struct ehci_hcd *ehci = hcd_to_ehci(hcd);
  1127. if (ehci_is_TDI(ehci))
  1128. return;
  1129. set_owner(ehci, --portnum, PORT_OWNER);
  1130. }
  1131. static int ehci_port_handed_over(struct usb_hcd *hcd, int portnum)
  1132. {
  1133. struct ehci_hcd *ehci = hcd_to_ehci(hcd);
  1134. u32 __iomem *reg;
  1135. if (ehci_is_TDI(ehci))
  1136. return 0;
  1137. reg = &ehci->regs->port_status[portnum - 1];
  1138. return ehci_readl(ehci, reg) & PORT_OWNER;
  1139. }
  1140. static int ehci_port_power(struct ehci_hcd *ehci, int portnum, bool enable)
  1141. {
  1142. struct usb_hcd *hcd = ehci_to_hcd(ehci);
  1143. u32 __iomem *status_reg = &ehci->regs->port_status[portnum];
  1144. u32 temp = ehci_readl(ehci, status_reg) & ~PORT_RWC_BITS;
  1145. if (enable)
  1146. ehci_writel(ehci, temp | PORT_POWER, status_reg);
  1147. else
  1148. ehci_writel(ehci, temp & ~PORT_POWER, status_reg);
  1149. if (hcd->driver->port_power)
  1150. hcd->driver->port_power(hcd, portnum, enable);
  1151. return 0;
  1152. }