ehci-hub.c 36 KB

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