ehci-hub.c 36 KB

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