ehci-hub.c 23 KB

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