ehci-hub.c 23 KB

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