ehci-hub.c 23 KB

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