ehci-hub.c 23 KB

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