ehci-hub.c 36 KB

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