ehci-hub.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756
  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. #ifdef CONFIG_PM
  27. static int ehci_bus_suspend (struct usb_hcd *hcd)
  28. {
  29. struct ehci_hcd *ehci = hcd_to_ehci (hcd);
  30. int port;
  31. int mask;
  32. ehci_dbg(ehci, "suspend root hub\n");
  33. if (time_before (jiffies, ehci->next_statechange))
  34. msleep(5);
  35. port = HCS_N_PORTS (ehci->hcs_params);
  36. spin_lock_irq (&ehci->lock);
  37. /* stop schedules, clean any completed work */
  38. if (HC_IS_RUNNING(hcd->state)) {
  39. ehci_quiesce (ehci);
  40. hcd->state = HC_STATE_QUIESCING;
  41. }
  42. ehci->command = ehci_readl(ehci, &ehci->regs->command);
  43. if (ehci->reclaim)
  44. ehci->reclaim_ready = 1;
  45. ehci_work(ehci);
  46. /* Unlike other USB host controller types, EHCI doesn't have
  47. * any notion of "global" or bus-wide suspend. The driver has
  48. * to manually suspend all the active unsuspended ports, and
  49. * then manually resume them in the bus_resume() routine.
  50. */
  51. ehci->bus_suspended = 0;
  52. while (port--) {
  53. u32 __iomem *reg = &ehci->regs->port_status [port];
  54. u32 t1 = ehci_readl(ehci, reg) & ~PORT_RWC_BITS;
  55. u32 t2 = t1;
  56. /* keep track of which ports we suspend */
  57. if ((t1 & PORT_PE) && !(t1 & PORT_OWNER) &&
  58. !(t1 & PORT_SUSPEND)) {
  59. t2 |= PORT_SUSPEND;
  60. set_bit(port, &ehci->bus_suspended);
  61. }
  62. /* enable remote wakeup on all ports */
  63. if (device_may_wakeup(&hcd->self.root_hub->dev))
  64. t2 |= PORT_WKOC_E|PORT_WKDISC_E|PORT_WKCONN_E;
  65. else
  66. t2 &= ~(PORT_WKOC_E|PORT_WKDISC_E|PORT_WKCONN_E);
  67. if (t1 != t2) {
  68. ehci_vdbg (ehci, "port %d, %08x -> %08x\n",
  69. port + 1, t1, t2);
  70. ehci_writel(ehci, t2, reg);
  71. }
  72. }
  73. /* turn off now-idle HC */
  74. del_timer_sync (&ehci->watchdog);
  75. ehci_halt (ehci);
  76. hcd->state = HC_STATE_SUSPENDED;
  77. /* allow remote wakeup */
  78. mask = INTR_MASK;
  79. if (!device_may_wakeup(&hcd->self.root_hub->dev))
  80. mask &= ~STS_PCD;
  81. ehci_writel(ehci, mask, &ehci->regs->intr_enable);
  82. ehci_readl(ehci, &ehci->regs->intr_enable);
  83. ehci->next_statechange = jiffies + msecs_to_jiffies(10);
  84. spin_unlock_irq (&ehci->lock);
  85. return 0;
  86. }
  87. /* caller has locked the root hub, and should reset/reinit on error */
  88. static int ehci_bus_resume (struct usb_hcd *hcd)
  89. {
  90. struct ehci_hcd *ehci = hcd_to_ehci (hcd);
  91. u32 temp;
  92. int i;
  93. if (time_before (jiffies, ehci->next_statechange))
  94. msleep(5);
  95. spin_lock_irq (&ehci->lock);
  96. /* Ideally and we've got a real resume here, and no port's power
  97. * was lost. (For PCI, that means Vaux was maintained.) But we
  98. * could instead be restoring a swsusp snapshot -- so that BIOS was
  99. * the last user of the controller, not reset/pm hardware keeping
  100. * state we gave to it.
  101. */
  102. temp = ehci_readl(ehci, &ehci->regs->intr_enable);
  103. ehci_dbg(ehci, "resume root hub%s\n", temp ? "" : " after power loss");
  104. /* at least some APM implementations will try to deliver
  105. * IRQs right away, so delay them until we're ready.
  106. */
  107. ehci_writel(ehci, 0, &ehci->regs->intr_enable);
  108. /* re-init operational registers */
  109. ehci_writel(ehci, 0, &ehci->regs->segment);
  110. ehci_writel(ehci, ehci->periodic_dma, &ehci->regs->frame_list);
  111. ehci_writel(ehci, (u32) ehci->async->qh_dma, &ehci->regs->async_next);
  112. /* restore CMD_RUN, framelist size, and irq threshold */
  113. ehci_writel(ehci, ehci->command, &ehci->regs->command);
  114. /* Some controller/firmware combinations need a delay during which
  115. * they set up the port statuses. See Bugzilla #8190. */
  116. mdelay(8);
  117. /* manually resume the ports we suspended during bus_suspend() */
  118. i = HCS_N_PORTS (ehci->hcs_params);
  119. while (i--) {
  120. temp = ehci_readl(ehci, &ehci->regs->port_status [i]);
  121. temp &= ~(PORT_RWC_BITS
  122. | PORT_WKOC_E | PORT_WKDISC_E | PORT_WKCONN_E);
  123. if (test_bit(i, &ehci->bus_suspended) &&
  124. (temp & PORT_SUSPEND)) {
  125. ehci->reset_done [i] = jiffies + msecs_to_jiffies (20);
  126. temp |= PORT_RESUME;
  127. }
  128. ehci_writel(ehci, temp, &ehci->regs->port_status [i]);
  129. }
  130. i = HCS_N_PORTS (ehci->hcs_params);
  131. mdelay (20);
  132. while (i--) {
  133. temp = ehci_readl(ehci, &ehci->regs->port_status [i]);
  134. if (test_bit(i, &ehci->bus_suspended) &&
  135. (temp & PORT_SUSPEND)) {
  136. temp &= ~(PORT_RWC_BITS | PORT_RESUME);
  137. ehci_writel(ehci, temp, &ehci->regs->port_status [i]);
  138. ehci_vdbg (ehci, "resumed port %d\n", i + 1);
  139. }
  140. }
  141. (void) ehci_readl(ehci, &ehci->regs->command);
  142. /* maybe re-activate the schedule(s) */
  143. temp = 0;
  144. if (ehci->async->qh_next.qh)
  145. temp |= CMD_ASE;
  146. if (ehci->periodic_sched)
  147. temp |= CMD_PSE;
  148. if (temp) {
  149. ehci->command |= temp;
  150. ehci_writel(ehci, ehci->command, &ehci->regs->command);
  151. }
  152. ehci->next_statechange = jiffies + msecs_to_jiffies(5);
  153. hcd->state = HC_STATE_RUNNING;
  154. /* Now we can safely re-enable irqs */
  155. ehci_writel(ehci, INTR_MASK, &ehci->regs->intr_enable);
  156. spin_unlock_irq (&ehci->lock);
  157. return 0;
  158. }
  159. #else
  160. #define ehci_bus_suspend NULL
  161. #define ehci_bus_resume NULL
  162. #endif /* CONFIG_PM */
  163. /*-------------------------------------------------------------------------*/
  164. /* Display the ports dedicated to the companion controller */
  165. static ssize_t show_companion(struct class_device *class_dev, char *buf)
  166. {
  167. struct ehci_hcd *ehci;
  168. int nports, index, n;
  169. int count = PAGE_SIZE;
  170. char *ptr = buf;
  171. ehci = hcd_to_ehci(bus_to_hcd(class_get_devdata(class_dev)));
  172. nports = HCS_N_PORTS(ehci->hcs_params);
  173. for (index = 0; index < nports; ++index) {
  174. if (test_bit(index, &ehci->companion_ports)) {
  175. n = scnprintf(ptr, count, "%d\n", index + 1);
  176. ptr += n;
  177. count -= n;
  178. }
  179. }
  180. return ptr - buf;
  181. }
  182. /*
  183. * Dedicate or undedicate a port to the companion controller.
  184. * Syntax is "[-]portnum", where a leading '-' sign means
  185. * return control of the port to the EHCI controller.
  186. */
  187. static ssize_t store_companion(struct class_device *class_dev,
  188. const char *buf, size_t count)
  189. {
  190. struct ehci_hcd *ehci;
  191. int portnum, new_owner, try;
  192. u32 __iomem *status_reg;
  193. u32 port_status;
  194. ehci = hcd_to_ehci(bus_to_hcd(class_get_devdata(class_dev)));
  195. new_owner = PORT_OWNER; /* Owned by companion */
  196. if (sscanf(buf, "%d", &portnum) != 1)
  197. return -EINVAL;
  198. if (portnum < 0) {
  199. portnum = - portnum;
  200. new_owner = 0; /* Owned by EHCI */
  201. }
  202. if (portnum <= 0 || portnum > HCS_N_PORTS(ehci->hcs_params))
  203. return -ENOENT;
  204. status_reg = &ehci->regs->port_status[--portnum];
  205. if (new_owner)
  206. set_bit(portnum, &ehci->companion_ports);
  207. else
  208. clear_bit(portnum, &ehci->companion_ports);
  209. /*
  210. * The controller won't set the OWNER bit if the port is
  211. * enabled, so this loop will sometimes require at least two
  212. * iterations: one to disable the port and one to set OWNER.
  213. */
  214. for (try = 4; try > 0; --try) {
  215. spin_lock_irq(&ehci->lock);
  216. port_status = ehci_readl(ehci, status_reg);
  217. if ((port_status & PORT_OWNER) == new_owner
  218. || (port_status & (PORT_OWNER | PORT_CONNECT))
  219. == 0)
  220. try = 0;
  221. else {
  222. port_status ^= PORT_OWNER;
  223. port_status &= ~(PORT_PE | PORT_RWC_BITS);
  224. ehci_writel(ehci, port_status, status_reg);
  225. }
  226. spin_unlock_irq(&ehci->lock);
  227. if (try > 1)
  228. msleep(5);
  229. }
  230. return count;
  231. }
  232. static CLASS_DEVICE_ATTR(companion, 0644, show_companion, store_companion);
  233. static inline void create_companion_file(struct ehci_hcd *ehci)
  234. {
  235. int i;
  236. /* with integrated TT there is no companion! */
  237. if (!ehci_is_TDI(ehci))
  238. i = class_device_create_file(ehci_to_hcd(ehci)->self.class_dev,
  239. &class_device_attr_companion);
  240. }
  241. static inline void remove_companion_file(struct ehci_hcd *ehci)
  242. {
  243. /* with integrated TT there is no companion! */
  244. if (!ehci_is_TDI(ehci))
  245. class_device_remove_file(ehci_to_hcd(ehci)->self.class_dev,
  246. &class_device_attr_companion);
  247. }
  248. /*-------------------------------------------------------------------------*/
  249. static int check_reset_complete (
  250. struct ehci_hcd *ehci,
  251. int index,
  252. u32 __iomem *status_reg,
  253. int port_status
  254. ) {
  255. if (!(port_status & PORT_CONNECT)) {
  256. ehci->reset_done [index] = 0;
  257. return port_status;
  258. }
  259. /* if reset finished and it's still not enabled -- handoff */
  260. if (!(port_status & PORT_PE)) {
  261. /* with integrated TT, there's nobody to hand it to! */
  262. if (ehci_is_TDI(ehci)) {
  263. ehci_dbg (ehci,
  264. "Failed to enable port %d on root hub TT\n",
  265. index+1);
  266. return port_status;
  267. }
  268. ehci_dbg (ehci, "port %d full speed --> companion\n",
  269. index + 1);
  270. // what happens if HCS_N_CC(params) == 0 ?
  271. port_status |= PORT_OWNER;
  272. port_status &= ~PORT_RWC_BITS;
  273. ehci_writel(ehci, port_status, status_reg);
  274. } else
  275. ehci_dbg (ehci, "port %d high speed\n", index + 1);
  276. return port_status;
  277. }
  278. /*-------------------------------------------------------------------------*/
  279. /* build "status change" packet (one or two bytes) from HC registers */
  280. static int
  281. ehci_hub_status_data (struct usb_hcd *hcd, char *buf)
  282. {
  283. struct ehci_hcd *ehci = hcd_to_ehci (hcd);
  284. u32 temp, status = 0;
  285. u32 mask;
  286. int ports, i, retval = 1;
  287. unsigned long flags;
  288. /* if !USB_SUSPEND, root hub timers won't get shut down ... */
  289. if (!HC_IS_RUNNING(hcd->state))
  290. return 0;
  291. /* init status to no-changes */
  292. buf [0] = 0;
  293. ports = HCS_N_PORTS (ehci->hcs_params);
  294. if (ports > 7) {
  295. buf [1] = 0;
  296. retval++;
  297. }
  298. /* Some boards (mostly VIA?) report bogus overcurrent indications,
  299. * causing massive log spam unless we completely ignore them. It
  300. * may be relevant that VIA VT8235 controlers, where PORT_POWER is
  301. * always set, seem to clear PORT_OCC and PORT_CSC when writing to
  302. * PORT_POWER; that's surprising, but maybe within-spec.
  303. */
  304. if (!ignore_oc)
  305. mask = PORT_CSC | PORT_PEC | PORT_OCC;
  306. else
  307. mask = PORT_CSC | PORT_PEC;
  308. // PORT_RESUME from hardware ~= PORT_STAT_C_SUSPEND
  309. /* no hub change reports (bit 0) for now (power, ...) */
  310. /* port N changes (bit N)? */
  311. spin_lock_irqsave (&ehci->lock, flags);
  312. for (i = 0; i < ports; i++) {
  313. temp = ehci_readl(ehci, &ehci->regs->port_status [i]);
  314. /*
  315. * Return status information even for ports with OWNER set.
  316. * Otherwise khubd wouldn't see the disconnect event when a
  317. * high-speed device is switched over to the companion
  318. * controller by the user.
  319. */
  320. if (!(temp & PORT_CONNECT))
  321. ehci->reset_done [i] = 0;
  322. if ((temp & mask) != 0
  323. || ((temp & PORT_RESUME) != 0
  324. && time_after_eq(jiffies,
  325. ehci->reset_done[i]))) {
  326. if (i < 7)
  327. buf [0] |= 1 << (i + 1);
  328. else
  329. buf [1] |= 1 << (i - 7);
  330. status = STS_PCD;
  331. }
  332. }
  333. /* FIXME autosuspend idle root hubs */
  334. spin_unlock_irqrestore (&ehci->lock, flags);
  335. return status ? retval : 0;
  336. }
  337. /*-------------------------------------------------------------------------*/
  338. static void
  339. ehci_hub_descriptor (
  340. struct ehci_hcd *ehci,
  341. struct usb_hub_descriptor *desc
  342. ) {
  343. int ports = HCS_N_PORTS (ehci->hcs_params);
  344. u16 temp;
  345. desc->bDescriptorType = 0x29;
  346. desc->bPwrOn2PwrGood = 10; /* ehci 1.0, 2.3.9 says 20ms max */
  347. desc->bHubContrCurrent = 0;
  348. desc->bNbrPorts = ports;
  349. temp = 1 + (ports / 8);
  350. desc->bDescLength = 7 + 2 * temp;
  351. /* two bitmaps: ports removable, and usb 1.0 legacy PortPwrCtrlMask */
  352. memset (&desc->bitmap [0], 0, temp);
  353. memset (&desc->bitmap [temp], 0xff, temp);
  354. temp = 0x0008; /* per-port overcurrent reporting */
  355. if (HCS_PPC (ehci->hcs_params))
  356. temp |= 0x0001; /* per-port power control */
  357. else
  358. temp |= 0x0002; /* no power switching */
  359. #if 0
  360. // re-enable when we support USB_PORT_FEAT_INDICATOR below.
  361. if (HCS_INDICATOR (ehci->hcs_params))
  362. temp |= 0x0080; /* per-port indicators (LEDs) */
  363. #endif
  364. desc->wHubCharacteristics = (__force __u16)cpu_to_le16 (temp);
  365. }
  366. /*-------------------------------------------------------------------------*/
  367. #define PORT_WAKE_BITS (PORT_WKOC_E|PORT_WKDISC_E|PORT_WKCONN_E)
  368. static int ehci_hub_control (
  369. struct usb_hcd *hcd,
  370. u16 typeReq,
  371. u16 wValue,
  372. u16 wIndex,
  373. char *buf,
  374. u16 wLength
  375. ) {
  376. struct ehci_hcd *ehci = hcd_to_ehci (hcd);
  377. int ports = HCS_N_PORTS (ehci->hcs_params);
  378. u32 __iomem *status_reg = &ehci->regs->port_status[wIndex - 1];
  379. u32 temp, status;
  380. unsigned long flags;
  381. int retval = 0;
  382. unsigned selector;
  383. /*
  384. * FIXME: support SetPortFeatures USB_PORT_FEAT_INDICATOR.
  385. * HCS_INDICATOR may say we can change LEDs to off/amber/green.
  386. * (track current state ourselves) ... blink for diagnostics,
  387. * power, "this is the one", etc. EHCI spec supports this.
  388. */
  389. spin_lock_irqsave (&ehci->lock, flags);
  390. switch (typeReq) {
  391. case ClearHubFeature:
  392. switch (wValue) {
  393. case C_HUB_LOCAL_POWER:
  394. case C_HUB_OVER_CURRENT:
  395. /* no hub-wide feature/status flags */
  396. break;
  397. default:
  398. goto error;
  399. }
  400. break;
  401. case ClearPortFeature:
  402. if (!wIndex || wIndex > ports)
  403. goto error;
  404. wIndex--;
  405. temp = ehci_readl(ehci, status_reg);
  406. /*
  407. * Even if OWNER is set, so the port is owned by the
  408. * companion controller, khubd needs to be able to clear
  409. * the port-change status bits (especially
  410. * USB_PORT_FEAT_C_CONNECTION).
  411. */
  412. switch (wValue) {
  413. case USB_PORT_FEAT_ENABLE:
  414. ehci_writel(ehci, temp & ~PORT_PE, status_reg);
  415. break;
  416. case USB_PORT_FEAT_C_ENABLE:
  417. ehci_writel(ehci, (temp & ~PORT_RWC_BITS) | PORT_PEC,
  418. status_reg);
  419. break;
  420. case USB_PORT_FEAT_SUSPEND:
  421. if (temp & PORT_RESET)
  422. goto error;
  423. if (ehci->no_selective_suspend)
  424. break;
  425. if (temp & PORT_SUSPEND) {
  426. if ((temp & PORT_PE) == 0)
  427. goto error;
  428. /* resume signaling for 20 msec */
  429. temp &= ~(PORT_RWC_BITS | PORT_WAKE_BITS);
  430. ehci_writel(ehci, temp | PORT_RESUME,
  431. status_reg);
  432. ehci->reset_done [wIndex] = jiffies
  433. + msecs_to_jiffies (20);
  434. }
  435. break;
  436. case USB_PORT_FEAT_C_SUSPEND:
  437. /* we auto-clear this feature */
  438. break;
  439. case USB_PORT_FEAT_POWER:
  440. if (HCS_PPC (ehci->hcs_params))
  441. ehci_writel(ehci,
  442. temp & ~(PORT_RWC_BITS | PORT_POWER),
  443. status_reg);
  444. break;
  445. case USB_PORT_FEAT_C_CONNECTION:
  446. ehci_writel(ehci, (temp & ~PORT_RWC_BITS) | PORT_CSC,
  447. status_reg);
  448. break;
  449. case USB_PORT_FEAT_C_OVER_CURRENT:
  450. ehci_writel(ehci, (temp & ~PORT_RWC_BITS) | PORT_OCC,
  451. status_reg);
  452. break;
  453. case USB_PORT_FEAT_C_RESET:
  454. /* GetPortStatus clears reset */
  455. break;
  456. default:
  457. goto error;
  458. }
  459. ehci_readl(ehci, &ehci->regs->command); /* unblock posted write */
  460. break;
  461. case GetHubDescriptor:
  462. ehci_hub_descriptor (ehci, (struct usb_hub_descriptor *)
  463. buf);
  464. break;
  465. case GetHubStatus:
  466. /* no hub-wide feature/status flags */
  467. memset (buf, 0, 4);
  468. //cpu_to_le32s ((u32 *) buf);
  469. break;
  470. case GetPortStatus:
  471. if (!wIndex || wIndex > ports)
  472. goto error;
  473. wIndex--;
  474. status = 0;
  475. temp = ehci_readl(ehci, status_reg);
  476. // wPortChange bits
  477. if (temp & PORT_CSC)
  478. status |= 1 << USB_PORT_FEAT_C_CONNECTION;
  479. if (temp & PORT_PEC)
  480. status |= 1 << USB_PORT_FEAT_C_ENABLE;
  481. if ((temp & PORT_OCC) && !ignore_oc)
  482. status |= 1 << USB_PORT_FEAT_C_OVER_CURRENT;
  483. /* whoever resumes must GetPortStatus to complete it!! */
  484. if (temp & PORT_RESUME) {
  485. /* Remote Wakeup received? */
  486. if (!ehci->reset_done[wIndex]) {
  487. /* resume signaling for 20 msec */
  488. ehci->reset_done[wIndex] = jiffies
  489. + msecs_to_jiffies(20);
  490. /* check the port again */
  491. mod_timer(&ehci_to_hcd(ehci)->rh_timer,
  492. ehci->reset_done[wIndex]);
  493. }
  494. /* resume completed? */
  495. else if (time_after_eq(jiffies,
  496. ehci->reset_done[wIndex])) {
  497. status |= 1 << USB_PORT_FEAT_C_SUSPEND;
  498. ehci->reset_done[wIndex] = 0;
  499. /* stop resume signaling */
  500. temp = ehci_readl(ehci, status_reg);
  501. ehci_writel(ehci,
  502. temp & ~(PORT_RWC_BITS | PORT_RESUME),
  503. status_reg);
  504. retval = handshake(ehci, status_reg,
  505. PORT_RESUME, 0, 2000 /* 2msec */);
  506. if (retval != 0) {
  507. ehci_err(ehci,
  508. "port %d resume error %d\n",
  509. wIndex + 1, retval);
  510. goto error;
  511. }
  512. temp &= ~(PORT_SUSPEND|PORT_RESUME|(3<<10));
  513. }
  514. }
  515. /* whoever resets must GetPortStatus to complete it!! */
  516. if ((temp & PORT_RESET)
  517. && time_after_eq(jiffies,
  518. ehci->reset_done[wIndex])) {
  519. status |= 1 << USB_PORT_FEAT_C_RESET;
  520. ehci->reset_done [wIndex] = 0;
  521. /* force reset to complete */
  522. ehci_writel(ehci, temp & ~(PORT_RWC_BITS | PORT_RESET),
  523. status_reg);
  524. /* REVISIT: some hardware needs 550+ usec to clear
  525. * this bit; seems too long to spin routinely...
  526. */
  527. retval = handshake(ehci, status_reg,
  528. PORT_RESET, 0, 750);
  529. if (retval != 0) {
  530. ehci_err (ehci, "port %d reset error %d\n",
  531. wIndex + 1, retval);
  532. goto error;
  533. }
  534. /* see what we found out */
  535. temp = check_reset_complete (ehci, wIndex, status_reg,
  536. ehci_readl(ehci, status_reg));
  537. }
  538. /* transfer dedicated ports to the companion hc */
  539. if ((temp & PORT_CONNECT) &&
  540. test_bit(wIndex, &ehci->companion_ports)) {
  541. temp &= ~PORT_RWC_BITS;
  542. temp |= PORT_OWNER;
  543. ehci_writel(ehci, temp, status_reg);
  544. ehci_dbg(ehci, "port %d --> companion\n", wIndex + 1);
  545. temp = ehci_readl(ehci, status_reg);
  546. }
  547. /*
  548. * Even if OWNER is set, there's no harm letting khubd
  549. * see the wPortStatus values (they should all be 0 except
  550. * for PORT_POWER anyway).
  551. */
  552. if (temp & PORT_CONNECT) {
  553. status |= 1 << USB_PORT_FEAT_CONNECTION;
  554. // status may be from integrated TT
  555. status |= ehci_port_speed(ehci, temp);
  556. }
  557. if (temp & PORT_PE)
  558. status |= 1 << USB_PORT_FEAT_ENABLE;
  559. if (temp & (PORT_SUSPEND|PORT_RESUME))
  560. status |= 1 << USB_PORT_FEAT_SUSPEND;
  561. if (temp & PORT_OC)
  562. status |= 1 << USB_PORT_FEAT_OVER_CURRENT;
  563. if (temp & PORT_RESET)
  564. status |= 1 << USB_PORT_FEAT_RESET;
  565. if (temp & PORT_POWER)
  566. status |= 1 << USB_PORT_FEAT_POWER;
  567. #ifndef EHCI_VERBOSE_DEBUG
  568. if (status & ~0xffff) /* only if wPortChange is interesting */
  569. #endif
  570. dbg_port (ehci, "GetStatus", wIndex + 1, temp);
  571. put_unaligned(cpu_to_le32 (status), (__le32 *) buf);
  572. break;
  573. case SetHubFeature:
  574. switch (wValue) {
  575. case C_HUB_LOCAL_POWER:
  576. case C_HUB_OVER_CURRENT:
  577. /* no hub-wide feature/status flags */
  578. break;
  579. default:
  580. goto error;
  581. }
  582. break;
  583. case SetPortFeature:
  584. selector = wIndex >> 8;
  585. wIndex &= 0xff;
  586. if (!wIndex || wIndex > ports)
  587. goto error;
  588. wIndex--;
  589. temp = ehci_readl(ehci, status_reg);
  590. if (temp & PORT_OWNER)
  591. break;
  592. temp &= ~PORT_RWC_BITS;
  593. switch (wValue) {
  594. case USB_PORT_FEAT_SUSPEND:
  595. if (ehci->no_selective_suspend)
  596. break;
  597. if ((temp & PORT_PE) == 0
  598. || (temp & PORT_RESET) != 0)
  599. goto error;
  600. if (device_may_wakeup(&hcd->self.root_hub->dev))
  601. temp |= PORT_WAKE_BITS;
  602. ehci_writel(ehci, temp | PORT_SUSPEND, status_reg);
  603. break;
  604. case USB_PORT_FEAT_POWER:
  605. if (HCS_PPC (ehci->hcs_params))
  606. ehci_writel(ehci, temp | PORT_POWER,
  607. status_reg);
  608. break;
  609. case USB_PORT_FEAT_RESET:
  610. if (temp & PORT_RESUME)
  611. goto error;
  612. /* line status bits may report this as low speed,
  613. * which can be fine if this root hub has a
  614. * transaction translator built in.
  615. */
  616. if ((temp & (PORT_PE|PORT_CONNECT)) == PORT_CONNECT
  617. && !ehci_is_TDI(ehci)
  618. && PORT_USB11 (temp)) {
  619. ehci_dbg (ehci,
  620. "port %d low speed --> companion\n",
  621. wIndex + 1);
  622. temp |= PORT_OWNER;
  623. } else {
  624. ehci_vdbg (ehci, "port %d reset\n", wIndex + 1);
  625. temp |= PORT_RESET;
  626. temp &= ~PORT_PE;
  627. /*
  628. * caller must wait, then call GetPortStatus
  629. * usb 2.0 spec says 50 ms resets on root
  630. */
  631. ehci->reset_done [wIndex] = jiffies
  632. + msecs_to_jiffies (50);
  633. }
  634. ehci_writel(ehci, temp, status_reg);
  635. break;
  636. /* For downstream facing ports (these): one hub port is put
  637. * into test mode according to USB2 11.24.2.13, then the hub
  638. * must be reset (which for root hub now means rmmod+modprobe,
  639. * or else system reboot). See EHCI 2.3.9 and 4.14 for info
  640. * about the EHCI-specific stuff.
  641. */
  642. case USB_PORT_FEAT_TEST:
  643. if (!selector || selector > 5)
  644. goto error;
  645. ehci_quiesce(ehci);
  646. ehci_halt(ehci);
  647. temp |= selector << 16;
  648. ehci_writel(ehci, temp, status_reg);
  649. break;
  650. default:
  651. goto error;
  652. }
  653. ehci_readl(ehci, &ehci->regs->command); /* unblock posted writes */
  654. break;
  655. default:
  656. error:
  657. /* "stall" on error */
  658. retval = -EPIPE;
  659. }
  660. spin_unlock_irqrestore (&ehci->lock, flags);
  661. return retval;
  662. }