ehci-hub.c 36 KB

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