ehci-hub.c 36 KB

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