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