ehci-hub.c 23 KB

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