ehci-hub.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877
  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. #define PORT_WAKE_BITS (PORT_WKOC_E|PORT_WKDISC_E|PORT_WKCONN_E)
  27. #ifdef CONFIG_PM
  28. static int ehci_hub_control(
  29. struct usb_hcd *hcd,
  30. u16 typeReq,
  31. u16 wValue,
  32. u16 wIndex,
  33. char *buf,
  34. u16 wLength
  35. );
  36. /* After a power loss, ports that were owned by the companion must be
  37. * reset so that the companion can still own them.
  38. */
  39. static void ehci_handover_companion_ports(struct ehci_hcd *ehci)
  40. {
  41. u32 __iomem *reg;
  42. u32 status;
  43. int port;
  44. __le32 buf;
  45. struct usb_hcd *hcd = ehci_to_hcd(ehci);
  46. if (!ehci->owned_ports)
  47. return;
  48. /* Give the connections some time to appear */
  49. msleep(20);
  50. port = HCS_N_PORTS(ehci->hcs_params);
  51. while (port--) {
  52. if (test_bit(port, &ehci->owned_ports)) {
  53. reg = &ehci->regs->port_status[port];
  54. status = ehci_readl(ehci, reg) & ~PORT_RWC_BITS;
  55. /* Port already owned by companion? */
  56. if (status & PORT_OWNER)
  57. clear_bit(port, &ehci->owned_ports);
  58. else if (test_bit(port, &ehci->companion_ports))
  59. ehci_writel(ehci, status & ~PORT_PE, reg);
  60. else
  61. ehci_hub_control(hcd, SetPortFeature,
  62. USB_PORT_FEAT_RESET, port + 1,
  63. NULL, 0);
  64. }
  65. }
  66. if (!ehci->owned_ports)
  67. return;
  68. msleep(90); /* Wait for resets to complete */
  69. port = HCS_N_PORTS(ehci->hcs_params);
  70. while (port--) {
  71. if (test_bit(port, &ehci->owned_ports)) {
  72. ehci_hub_control(hcd, GetPortStatus,
  73. 0, port + 1,
  74. (char *) &buf, sizeof(buf));
  75. /* The companion should now own the port,
  76. * but if something went wrong the port must not
  77. * remain enabled.
  78. */
  79. reg = &ehci->regs->port_status[port];
  80. status = ehci_readl(ehci, reg) & ~PORT_RWC_BITS;
  81. if (status & PORT_OWNER)
  82. ehci_writel(ehci, status | PORT_CSC, reg);
  83. else {
  84. ehci_dbg(ehci, "failed handover port %d: %x\n",
  85. port + 1, status);
  86. ehci_writel(ehci, status & ~PORT_PE, reg);
  87. }
  88. }
  89. }
  90. ehci->owned_ports = 0;
  91. }
  92. static int ehci_bus_suspend (struct usb_hcd *hcd)
  93. {
  94. struct ehci_hcd *ehci = hcd_to_ehci (hcd);
  95. int port;
  96. int mask;
  97. ehci_dbg(ehci, "suspend root hub\n");
  98. if (time_before (jiffies, ehci->next_statechange))
  99. msleep(5);
  100. del_timer_sync(&ehci->watchdog);
  101. del_timer_sync(&ehci->iaa_watchdog);
  102. port = HCS_N_PORTS (ehci->hcs_params);
  103. spin_lock_irq (&ehci->lock);
  104. /* stop schedules, clean any completed work */
  105. if (HC_IS_RUNNING(hcd->state)) {
  106. ehci_quiesce (ehci);
  107. hcd->state = HC_STATE_QUIESCING;
  108. }
  109. ehci->command = ehci_readl(ehci, &ehci->regs->command);
  110. ehci_work(ehci);
  111. /* Unlike other USB host controller types, EHCI doesn't have
  112. * any notion of "global" or bus-wide suspend. The driver has
  113. * to manually suspend all the active unsuspended ports, and
  114. * then manually resume them in the bus_resume() routine.
  115. */
  116. ehci->bus_suspended = 0;
  117. ehci->owned_ports = 0;
  118. while (port--) {
  119. u32 __iomem *reg = &ehci->regs->port_status [port];
  120. u32 t1 = ehci_readl(ehci, reg) & ~PORT_RWC_BITS;
  121. u32 t2 = t1;
  122. /* keep track of which ports we suspend */
  123. if (t1 & PORT_OWNER)
  124. set_bit(port, &ehci->owned_ports);
  125. else if ((t1 & PORT_PE) && !(t1 & PORT_SUSPEND)) {
  126. t2 |= PORT_SUSPEND;
  127. set_bit(port, &ehci->bus_suspended);
  128. }
  129. /* enable remote wakeup on all ports */
  130. if (hcd->self.root_hub->do_remote_wakeup)
  131. t2 |= PORT_WAKE_BITS;
  132. else
  133. t2 &= ~PORT_WAKE_BITS;
  134. if (t1 != t2) {
  135. ehci_vdbg (ehci, "port %d, %08x -> %08x\n",
  136. port + 1, t1, t2);
  137. ehci_writel(ehci, t2, reg);
  138. }
  139. }
  140. /* Apparently some devices need a >= 1-uframe delay here */
  141. if (ehci->bus_suspended)
  142. udelay(150);
  143. /* turn off now-idle HC */
  144. ehci_halt (ehci);
  145. hcd->state = HC_STATE_SUSPENDED;
  146. if (ehci->reclaim)
  147. end_unlink_async(ehci);
  148. /* allow remote wakeup */
  149. mask = INTR_MASK;
  150. if (!hcd->self.root_hub->do_remote_wakeup)
  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 | PORT_WAKE_BITS);
  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 = cpu_to_le16(temp);
  450. }
  451. /*-------------------------------------------------------------------------*/
  452. static int ehci_hub_control (
  453. struct usb_hcd *hcd,
  454. u16 typeReq,
  455. u16 wValue,
  456. u16 wIndex,
  457. char *buf,
  458. u16 wLength
  459. ) {
  460. struct ehci_hcd *ehci = hcd_to_ehci (hcd);
  461. int ports = HCS_N_PORTS (ehci->hcs_params);
  462. u32 __iomem *status_reg = &ehci->regs->port_status[
  463. (wIndex & 0xff) - 1];
  464. u32 temp, status;
  465. unsigned long flags;
  466. int retval = 0;
  467. unsigned selector;
  468. /*
  469. * FIXME: support SetPortFeatures USB_PORT_FEAT_INDICATOR.
  470. * HCS_INDICATOR may say we can change LEDs to off/amber/green.
  471. * (track current state ourselves) ... blink for diagnostics,
  472. * power, "this is the one", etc. EHCI spec supports this.
  473. */
  474. spin_lock_irqsave (&ehci->lock, flags);
  475. switch (typeReq) {
  476. case ClearHubFeature:
  477. switch (wValue) {
  478. case C_HUB_LOCAL_POWER:
  479. case C_HUB_OVER_CURRENT:
  480. /* no hub-wide feature/status flags */
  481. break;
  482. default:
  483. goto error;
  484. }
  485. break;
  486. case ClearPortFeature:
  487. if (!wIndex || wIndex > ports)
  488. goto error;
  489. wIndex--;
  490. temp = ehci_readl(ehci, status_reg);
  491. /*
  492. * Even if OWNER is set, so the port is owned by the
  493. * companion controller, khubd needs to be able to clear
  494. * the port-change status bits (especially
  495. * USB_PORT_FEAT_C_CONNECTION).
  496. */
  497. switch (wValue) {
  498. case USB_PORT_FEAT_ENABLE:
  499. ehci_writel(ehci, temp & ~PORT_PE, status_reg);
  500. break;
  501. case USB_PORT_FEAT_C_ENABLE:
  502. ehci_writel(ehci, (temp & ~PORT_RWC_BITS) | PORT_PEC,
  503. status_reg);
  504. break;
  505. case USB_PORT_FEAT_SUSPEND:
  506. if (temp & PORT_RESET)
  507. goto error;
  508. if (ehci->no_selective_suspend)
  509. break;
  510. if (temp & PORT_SUSPEND) {
  511. if ((temp & PORT_PE) == 0)
  512. goto error;
  513. /* resume signaling for 20 msec */
  514. temp &= ~(PORT_RWC_BITS | PORT_WAKE_BITS);
  515. ehci_writel(ehci, temp | PORT_RESUME,
  516. status_reg);
  517. ehci->reset_done [wIndex] = jiffies
  518. + msecs_to_jiffies (20);
  519. }
  520. break;
  521. case USB_PORT_FEAT_C_SUSPEND:
  522. /* we auto-clear this feature */
  523. break;
  524. case USB_PORT_FEAT_POWER:
  525. if (HCS_PPC (ehci->hcs_params))
  526. ehci_writel(ehci,
  527. temp & ~(PORT_RWC_BITS | PORT_POWER),
  528. status_reg);
  529. break;
  530. case USB_PORT_FEAT_C_CONNECTION:
  531. ehci_writel(ehci, (temp & ~PORT_RWC_BITS) | PORT_CSC,
  532. status_reg);
  533. break;
  534. case USB_PORT_FEAT_C_OVER_CURRENT:
  535. ehci_writel(ehci, (temp & ~PORT_RWC_BITS) | PORT_OCC,
  536. status_reg);
  537. break;
  538. case USB_PORT_FEAT_C_RESET:
  539. /* GetPortStatus clears reset */
  540. break;
  541. default:
  542. goto error;
  543. }
  544. ehci_readl(ehci, &ehci->regs->command); /* unblock posted write */
  545. break;
  546. case GetHubDescriptor:
  547. ehci_hub_descriptor (ehci, (struct usb_hub_descriptor *)
  548. buf);
  549. break;
  550. case GetHubStatus:
  551. /* no hub-wide feature/status flags */
  552. memset (buf, 0, 4);
  553. //cpu_to_le32s ((u32 *) buf);
  554. break;
  555. case GetPortStatus:
  556. if (!wIndex || wIndex > ports)
  557. goto error;
  558. wIndex--;
  559. status = 0;
  560. temp = ehci_readl(ehci, status_reg);
  561. // wPortChange bits
  562. if (temp & PORT_CSC)
  563. status |= 1 << USB_PORT_FEAT_C_CONNECTION;
  564. if (temp & PORT_PEC)
  565. status |= 1 << USB_PORT_FEAT_C_ENABLE;
  566. if ((temp & PORT_OCC) && !ignore_oc){
  567. status |= 1 << USB_PORT_FEAT_C_OVER_CURRENT;
  568. /*
  569. * Hubs should disable port power on over-current.
  570. * However, not all EHCI implementations do this
  571. * automatically, even if they _do_ support per-port
  572. * power switching; they're allowed to just limit the
  573. * current. khubd will turn the power back on.
  574. */
  575. if (HCS_PPC (ehci->hcs_params)){
  576. ehci_writel(ehci,
  577. temp & ~(PORT_RWC_BITS | PORT_POWER),
  578. status_reg);
  579. }
  580. }
  581. /* whoever resumes must GetPortStatus to complete it!! */
  582. if (temp & PORT_RESUME) {
  583. /* Remote Wakeup received? */
  584. if (!ehci->reset_done[wIndex]) {
  585. /* resume signaling for 20 msec */
  586. ehci->reset_done[wIndex] = jiffies
  587. + msecs_to_jiffies(20);
  588. /* check the port again */
  589. mod_timer(&ehci_to_hcd(ehci)->rh_timer,
  590. ehci->reset_done[wIndex]);
  591. }
  592. /* resume completed? */
  593. else if (time_after_eq(jiffies,
  594. ehci->reset_done[wIndex])) {
  595. status |= 1 << USB_PORT_FEAT_C_SUSPEND;
  596. ehci->reset_done[wIndex] = 0;
  597. /* stop resume signaling */
  598. temp = ehci_readl(ehci, status_reg);
  599. ehci_writel(ehci,
  600. temp & ~(PORT_RWC_BITS | PORT_RESUME),
  601. status_reg);
  602. retval = handshake(ehci, status_reg,
  603. PORT_RESUME, 0, 2000 /* 2msec */);
  604. if (retval != 0) {
  605. ehci_err(ehci,
  606. "port %d resume error %d\n",
  607. wIndex + 1, retval);
  608. goto error;
  609. }
  610. temp &= ~(PORT_SUSPEND|PORT_RESUME|(3<<10));
  611. }
  612. }
  613. /* whoever resets must GetPortStatus to complete it!! */
  614. if ((temp & PORT_RESET)
  615. && time_after_eq(jiffies,
  616. ehci->reset_done[wIndex])) {
  617. status |= 1 << USB_PORT_FEAT_C_RESET;
  618. ehci->reset_done [wIndex] = 0;
  619. /* force reset to complete */
  620. ehci_writel(ehci, temp & ~(PORT_RWC_BITS | PORT_RESET),
  621. status_reg);
  622. /* REVISIT: some hardware needs 550+ usec to clear
  623. * this bit; seems too long to spin routinely...
  624. */
  625. retval = handshake(ehci, status_reg,
  626. PORT_RESET, 0, 750);
  627. if (retval != 0) {
  628. ehci_err (ehci, "port %d reset error %d\n",
  629. wIndex + 1, retval);
  630. goto error;
  631. }
  632. /* see what we found out */
  633. temp = check_reset_complete (ehci, wIndex, status_reg,
  634. ehci_readl(ehci, status_reg));
  635. }
  636. /* transfer dedicated ports to the companion hc */
  637. if ((temp & PORT_CONNECT) &&
  638. test_bit(wIndex, &ehci->companion_ports)) {
  639. temp &= ~PORT_RWC_BITS;
  640. temp |= PORT_OWNER;
  641. ehci_writel(ehci, temp, status_reg);
  642. ehci_dbg(ehci, "port %d --> companion\n", wIndex + 1);
  643. temp = ehci_readl(ehci, status_reg);
  644. }
  645. /*
  646. * Even if OWNER is set, there's no harm letting khubd
  647. * see the wPortStatus values (they should all be 0 except
  648. * for PORT_POWER anyway).
  649. */
  650. if (temp & PORT_CONNECT) {
  651. status |= 1 << USB_PORT_FEAT_CONNECTION;
  652. // status may be from integrated TT
  653. status |= ehci_port_speed(ehci, temp);
  654. }
  655. if (temp & PORT_PE)
  656. status |= 1 << USB_PORT_FEAT_ENABLE;
  657. if (temp & (PORT_SUSPEND|PORT_RESUME))
  658. status |= 1 << USB_PORT_FEAT_SUSPEND;
  659. if (temp & PORT_OC)
  660. status |= 1 << USB_PORT_FEAT_OVER_CURRENT;
  661. if (temp & PORT_RESET)
  662. status |= 1 << USB_PORT_FEAT_RESET;
  663. if (temp & PORT_POWER)
  664. status |= 1 << USB_PORT_FEAT_POWER;
  665. #ifndef VERBOSE_DEBUG
  666. if (status & ~0xffff) /* only if wPortChange is interesting */
  667. #endif
  668. dbg_port (ehci, "GetStatus", wIndex + 1, temp);
  669. put_unaligned_le32(status, buf);
  670. break;
  671. case SetHubFeature:
  672. switch (wValue) {
  673. case C_HUB_LOCAL_POWER:
  674. case C_HUB_OVER_CURRENT:
  675. /* no hub-wide feature/status flags */
  676. break;
  677. default:
  678. goto error;
  679. }
  680. break;
  681. case SetPortFeature:
  682. selector = wIndex >> 8;
  683. wIndex &= 0xff;
  684. if (!wIndex || wIndex > ports)
  685. goto error;
  686. wIndex--;
  687. temp = ehci_readl(ehci, status_reg);
  688. if (temp & PORT_OWNER)
  689. break;
  690. temp &= ~PORT_RWC_BITS;
  691. switch (wValue) {
  692. case USB_PORT_FEAT_SUSPEND:
  693. if (ehci->no_selective_suspend)
  694. break;
  695. if ((temp & PORT_PE) == 0
  696. || (temp & PORT_RESET) != 0)
  697. goto error;
  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. }
  759. static void ehci_relinquish_port(struct usb_hcd *hcd, int portnum)
  760. {
  761. struct ehci_hcd *ehci = hcd_to_ehci(hcd);
  762. if (ehci_is_TDI(ehci))
  763. return;
  764. set_owner(ehci, --portnum, PORT_OWNER);
  765. }