ehci-hub.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900
  1. /*
  2. * Copyright (C) 2001-2004 by David Brownell
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License as published by the
  6. * Free Software Foundation; either version 2 of the License, or (at your
  7. * option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  11. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. * for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software Foundation,
  16. * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. /* this file is part of ehci-hcd.c */
  19. /*-------------------------------------------------------------------------*/
  20. /*
  21. * EHCI Root Hub ... the nonsharable stuff
  22. *
  23. * Registers don't need cpu_to_le32, that happens transparently
  24. */
  25. /*-------------------------------------------------------------------------*/
  26. #define PORT_WAKE_BITS (PORT_WKOC_E|PORT_WKDISC_E|PORT_WKCONN_E)
  27. #ifdef CONFIG_PM
  28. static int ehci_hub_control(
  29. struct usb_hcd *hcd,
  30. u16 typeReq,
  31. u16 wValue,
  32. u16 wIndex,
  33. char *buf,
  34. u16 wLength
  35. );
  36. /* After a power loss, ports that were owned by the companion must be
  37. * reset so that the companion can still own them.
  38. */
  39. static void ehci_handover_companion_ports(struct ehci_hcd *ehci)
  40. {
  41. u32 __iomem *reg;
  42. u32 status;
  43. int port;
  44. __le32 buf;
  45. struct usb_hcd *hcd = ehci_to_hcd(ehci);
  46. if (!ehci->owned_ports)
  47. return;
  48. /* Give the connections some time to appear */
  49. msleep(20);
  50. port = HCS_N_PORTS(ehci->hcs_params);
  51. while (port--) {
  52. if (test_bit(port, &ehci->owned_ports)) {
  53. reg = &ehci->regs->port_status[port];
  54. status = ehci_readl(ehci, reg) & ~PORT_RWC_BITS;
  55. /* Port already owned by companion? */
  56. if (status & PORT_OWNER)
  57. clear_bit(port, &ehci->owned_ports);
  58. else if (test_bit(port, &ehci->companion_ports))
  59. ehci_writel(ehci, status & ~PORT_PE, reg);
  60. else
  61. ehci_hub_control(hcd, SetPortFeature,
  62. USB_PORT_FEAT_RESET, port + 1,
  63. NULL, 0);
  64. }
  65. }
  66. if (!ehci->owned_ports)
  67. return;
  68. msleep(90); /* Wait for resets to complete */
  69. port = HCS_N_PORTS(ehci->hcs_params);
  70. while (port--) {
  71. if (test_bit(port, &ehci->owned_ports)) {
  72. ehci_hub_control(hcd, GetPortStatus,
  73. 0, port + 1,
  74. (char *) &buf, sizeof(buf));
  75. /* The companion should now own the port,
  76. * but if something went wrong the port must not
  77. * remain enabled.
  78. */
  79. reg = &ehci->regs->port_status[port];
  80. status = ehci_readl(ehci, reg) & ~PORT_RWC_BITS;
  81. if (status & PORT_OWNER)
  82. ehci_writel(ehci, status | PORT_CSC, reg);
  83. else {
  84. ehci_dbg(ehci, "failed handover port %d: %x\n",
  85. port + 1, status);
  86. ehci_writel(ehci, status & ~PORT_PE, reg);
  87. }
  88. }
  89. }
  90. ehci->owned_ports = 0;
  91. }
  92. static int ehci_bus_suspend (struct usb_hcd *hcd)
  93. {
  94. struct ehci_hcd *ehci = hcd_to_ehci (hcd);
  95. int port;
  96. int mask;
  97. ehci_dbg(ehci, "suspend root hub\n");
  98. if (time_before (jiffies, ehci->next_statechange))
  99. msleep(5);
  100. del_timer_sync(&ehci->watchdog);
  101. del_timer_sync(&ehci->iaa_watchdog);
  102. port = HCS_N_PORTS (ehci->hcs_params);
  103. spin_lock_irq (&ehci->lock);
  104. /* stop schedules, clean any completed work */
  105. if (HC_IS_RUNNING(hcd->state)) {
  106. ehci_quiesce (ehci);
  107. hcd->state = HC_STATE_QUIESCING;
  108. }
  109. ehci->command = ehci_readl(ehci, &ehci->regs->command);
  110. ehci_work(ehci);
  111. /* Unlike other USB host controller types, EHCI doesn't have
  112. * any notion of "global" or bus-wide suspend. The driver has
  113. * to manually suspend all the active unsuspended ports, and
  114. * then manually resume them in the bus_resume() routine.
  115. */
  116. ehci->bus_suspended = 0;
  117. ehci->owned_ports = 0;
  118. while (port--) {
  119. u32 __iomem *reg = &ehci->regs->port_status [port];
  120. u32 t1 = ehci_readl(ehci, reg) & ~PORT_RWC_BITS;
  121. u32 t2 = t1;
  122. /* keep track of which ports we suspend */
  123. if (t1 & PORT_OWNER)
  124. set_bit(port, &ehci->owned_ports);
  125. else if ((t1 & PORT_PE) && !(t1 & PORT_SUSPEND)) {
  126. t2 |= PORT_SUSPEND;
  127. set_bit(port, &ehci->bus_suspended);
  128. }
  129. /* enable remote wakeup on all ports */
  130. if (hcd->self.root_hub->do_remote_wakeup)
  131. t2 |= PORT_WAKE_BITS;
  132. else
  133. t2 &= ~PORT_WAKE_BITS;
  134. if (t1 != t2) {
  135. ehci_vdbg (ehci, "port %d, %08x -> %08x\n",
  136. port + 1, t1, t2);
  137. ehci_writel(ehci, t2, reg);
  138. }
  139. }
  140. /* Apparently some devices need a >= 1-uframe delay here */
  141. if (ehci->bus_suspended)
  142. udelay(150);
  143. /* turn off now-idle HC */
  144. ehci_halt (ehci);
  145. hcd->state = HC_STATE_SUSPENDED;
  146. if (ehci->reclaim)
  147. end_unlink_async(ehci);
  148. /* allow remote wakeup */
  149. mask = INTR_MASK;
  150. if (!hcd->self.root_hub->do_remote_wakeup)
  151. mask &= ~STS_PCD;
  152. ehci_writel(ehci, mask, &ehci->regs->intr_enable);
  153. ehci_readl(ehci, &ehci->regs->intr_enable);
  154. ehci->next_statechange = jiffies + msecs_to_jiffies(10);
  155. spin_unlock_irq (&ehci->lock);
  156. return 0;
  157. }
  158. /* caller has locked the root hub, and should reset/reinit on error */
  159. static int ehci_bus_resume (struct usb_hcd *hcd)
  160. {
  161. struct ehci_hcd *ehci = hcd_to_ehci (hcd);
  162. u32 temp;
  163. u32 power_okay;
  164. int i;
  165. if (time_before (jiffies, ehci->next_statechange))
  166. msleep(5);
  167. spin_lock_irq (&ehci->lock);
  168. if (!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)) {
  169. spin_unlock_irq(&ehci->lock);
  170. return -ESHUTDOWN;
  171. }
  172. /* Ideally and we've got a real resume here, and no port's power
  173. * was lost. (For PCI, that means Vaux was maintained.) But we
  174. * could instead be restoring a swsusp snapshot -- so that BIOS was
  175. * the last user of the controller, not reset/pm hardware keeping
  176. * state we gave to it.
  177. */
  178. power_okay = ehci_readl(ehci, &ehci->regs->intr_enable);
  179. ehci_dbg(ehci, "resume root hub%s\n",
  180. power_okay ? "" : " after power loss");
  181. /* at least some APM implementations will try to deliver
  182. * IRQs right away, so delay them until we're ready.
  183. */
  184. ehci_writel(ehci, 0, &ehci->regs->intr_enable);
  185. /* re-init operational registers */
  186. ehci_writel(ehci, 0, &ehci->regs->segment);
  187. ehci_writel(ehci, ehci->periodic_dma, &ehci->regs->frame_list);
  188. ehci_writel(ehci, (u32) ehci->async->qh_dma, &ehci->regs->async_next);
  189. /* restore CMD_RUN, framelist size, and irq threshold */
  190. ehci_writel(ehci, ehci->command, &ehci->regs->command);
  191. /* Some controller/firmware combinations need a delay during which
  192. * they set up the port statuses. See Bugzilla #8190. */
  193. mdelay(8);
  194. /* manually resume the ports we suspended during bus_suspend() */
  195. i = HCS_N_PORTS (ehci->hcs_params);
  196. while (i--) {
  197. temp = ehci_readl(ehci, &ehci->regs->port_status [i]);
  198. temp &= ~(PORT_RWC_BITS | PORT_WAKE_BITS);
  199. if (test_bit(i, &ehci->bus_suspended) &&
  200. (temp & PORT_SUSPEND))
  201. temp |= PORT_RESUME;
  202. ehci_writel(ehci, temp, &ehci->regs->port_status [i]);
  203. }
  204. i = HCS_N_PORTS (ehci->hcs_params);
  205. mdelay (20);
  206. while (i--) {
  207. temp = ehci_readl(ehci, &ehci->regs->port_status [i]);
  208. if (test_bit(i, &ehci->bus_suspended) &&
  209. (temp & PORT_SUSPEND)) {
  210. temp &= ~(PORT_RWC_BITS | PORT_RESUME);
  211. ehci_writel(ehci, temp, &ehci->regs->port_status [i]);
  212. ehci_vdbg (ehci, "resumed port %d\n", i + 1);
  213. }
  214. }
  215. (void) ehci_readl(ehci, &ehci->regs->command);
  216. /* maybe re-activate the schedule(s) */
  217. temp = 0;
  218. if (ehci->async->qh_next.qh)
  219. temp |= CMD_ASE;
  220. if (ehci->periodic_sched)
  221. temp |= CMD_PSE;
  222. if (temp) {
  223. ehci->command |= temp;
  224. ehci_writel(ehci, ehci->command, &ehci->regs->command);
  225. }
  226. ehci->next_statechange = jiffies + msecs_to_jiffies(5);
  227. hcd->state = HC_STATE_RUNNING;
  228. /* Now we can safely re-enable irqs */
  229. ehci_writel(ehci, INTR_MASK, &ehci->regs->intr_enable);
  230. spin_unlock_irq (&ehci->lock);
  231. ehci_handover_companion_ports(ehci);
  232. return 0;
  233. }
  234. #else
  235. #define ehci_bus_suspend NULL
  236. #define ehci_bus_resume NULL
  237. #endif /* CONFIG_PM */
  238. /*-------------------------------------------------------------------------*/
  239. /* Display the ports dedicated to the companion controller */
  240. static ssize_t show_companion(struct device *dev,
  241. struct device_attribute *attr,
  242. char *buf)
  243. {
  244. struct ehci_hcd *ehci;
  245. int nports, index, n;
  246. int count = PAGE_SIZE;
  247. char *ptr = buf;
  248. ehci = hcd_to_ehci(bus_to_hcd(dev_get_drvdata(dev)));
  249. nports = HCS_N_PORTS(ehci->hcs_params);
  250. for (index = 0; index < nports; ++index) {
  251. if (test_bit(index, &ehci->companion_ports)) {
  252. n = scnprintf(ptr, count, "%d\n", index + 1);
  253. ptr += n;
  254. count -= n;
  255. }
  256. }
  257. return ptr - buf;
  258. }
  259. /*
  260. * Sets the owner of a port
  261. */
  262. static void set_owner(struct ehci_hcd *ehci, int portnum, int new_owner)
  263. {
  264. u32 __iomem *status_reg;
  265. u32 port_status;
  266. int try;
  267. status_reg = &ehci->regs->port_status[portnum];
  268. /*
  269. * The controller won't set the OWNER bit if the port is
  270. * enabled, so this loop will sometimes require at least two
  271. * iterations: one to disable the port and one to set OWNER.
  272. */
  273. for (try = 4; try > 0; --try) {
  274. spin_lock_irq(&ehci->lock);
  275. port_status = ehci_readl(ehci, status_reg);
  276. if ((port_status & PORT_OWNER) == new_owner
  277. || (port_status & (PORT_OWNER | PORT_CONNECT))
  278. == 0)
  279. try = 0;
  280. else {
  281. port_status ^= PORT_OWNER;
  282. port_status &= ~(PORT_PE | PORT_RWC_BITS);
  283. ehci_writel(ehci, port_status, status_reg);
  284. }
  285. spin_unlock_irq(&ehci->lock);
  286. if (try > 1)
  287. msleep(5);
  288. }
  289. }
  290. /*
  291. * Dedicate or undedicate a port to the companion controller.
  292. * Syntax is "[-]portnum", where a leading '-' sign means
  293. * return control of the port to the EHCI controller.
  294. */
  295. static ssize_t store_companion(struct device *dev,
  296. struct device_attribute *attr,
  297. const char *buf, size_t count)
  298. {
  299. struct ehci_hcd *ehci;
  300. int portnum, new_owner;
  301. ehci = hcd_to_ehci(bus_to_hcd(dev_get_drvdata(dev)));
  302. new_owner = PORT_OWNER; /* Owned by companion */
  303. if (sscanf(buf, "%d", &portnum) != 1)
  304. return -EINVAL;
  305. if (portnum < 0) {
  306. portnum = - portnum;
  307. new_owner = 0; /* Owned by EHCI */
  308. }
  309. if (portnum <= 0 || portnum > HCS_N_PORTS(ehci->hcs_params))
  310. return -ENOENT;
  311. portnum--;
  312. if (new_owner)
  313. set_bit(portnum, &ehci->companion_ports);
  314. else
  315. clear_bit(portnum, &ehci->companion_ports);
  316. set_owner(ehci, portnum, new_owner);
  317. return count;
  318. }
  319. static DEVICE_ATTR(companion, 0644, show_companion, store_companion);
  320. static inline void create_companion_file(struct ehci_hcd *ehci)
  321. {
  322. int i;
  323. /* with integrated TT there is no companion! */
  324. if (!ehci_is_TDI(ehci))
  325. i = device_create_file(ehci_to_hcd(ehci)->self.dev,
  326. &dev_attr_companion);
  327. }
  328. static inline void remove_companion_file(struct ehci_hcd *ehci)
  329. {
  330. /* with integrated TT there is no companion! */
  331. if (!ehci_is_TDI(ehci))
  332. device_remove_file(ehci_to_hcd(ehci)->self.dev,
  333. &dev_attr_companion);
  334. }
  335. /*-------------------------------------------------------------------------*/
  336. static int check_reset_complete (
  337. struct ehci_hcd *ehci,
  338. int index,
  339. u32 __iomem *status_reg,
  340. int port_status
  341. ) {
  342. if (!(port_status & PORT_CONNECT))
  343. return port_status;
  344. /* if reset finished and it's still not enabled -- handoff */
  345. if (!(port_status & PORT_PE)) {
  346. /* with integrated TT, there's nobody to hand it to! */
  347. if (ehci_is_TDI(ehci)) {
  348. ehci_dbg (ehci,
  349. "Failed to enable port %d on root hub TT\n",
  350. index+1);
  351. return port_status;
  352. }
  353. ehci_dbg (ehci, "port %d full speed --> companion\n",
  354. index + 1);
  355. // what happens if HCS_N_CC(params) == 0 ?
  356. port_status |= PORT_OWNER;
  357. port_status &= ~PORT_RWC_BITS;
  358. ehci_writel(ehci, port_status, status_reg);
  359. } else
  360. ehci_dbg (ehci, "port %d high speed\n", index + 1);
  361. return port_status;
  362. }
  363. /*-------------------------------------------------------------------------*/
  364. /* build "status change" packet (one or two bytes) from HC registers */
  365. static int
  366. ehci_hub_status_data (struct usb_hcd *hcd, char *buf)
  367. {
  368. struct ehci_hcd *ehci = hcd_to_ehci (hcd);
  369. u32 temp, status = 0;
  370. u32 mask;
  371. int ports, i, retval = 1;
  372. unsigned long flags;
  373. /* if !USB_SUSPEND, root hub timers won't get shut down ... */
  374. if (!HC_IS_RUNNING(hcd->state))
  375. return 0;
  376. /* init status to no-changes */
  377. buf [0] = 0;
  378. ports = HCS_N_PORTS (ehci->hcs_params);
  379. if (ports > 7) {
  380. buf [1] = 0;
  381. retval++;
  382. }
  383. /* Some boards (mostly VIA?) report bogus overcurrent indications,
  384. * causing massive log spam unless we completely ignore them. It
  385. * may be relevant that VIA VT8235 controllers, where PORT_POWER is
  386. * always set, seem to clear PORT_OCC and PORT_CSC when writing to
  387. * PORT_POWER; that's surprising, but maybe within-spec.
  388. */
  389. if (!ignore_oc)
  390. mask = PORT_CSC | PORT_PEC | PORT_OCC;
  391. else
  392. mask = PORT_CSC | PORT_PEC;
  393. // PORT_RESUME from hardware ~= PORT_STAT_C_SUSPEND
  394. /* no hub change reports (bit 0) for now (power, ...) */
  395. /* port N changes (bit N)? */
  396. spin_lock_irqsave (&ehci->lock, flags);
  397. for (i = 0; i < ports; i++) {
  398. temp = ehci_readl(ehci, &ehci->regs->port_status [i]);
  399. /*
  400. * Return status information even for ports with OWNER set.
  401. * Otherwise khubd wouldn't see the disconnect event when a
  402. * high-speed device is switched over to the companion
  403. * controller by the user.
  404. */
  405. if ((temp & mask) != 0 || test_bit(i, &ehci->port_c_suspend)
  406. || (ehci->reset_done[i] && time_after_eq(
  407. jiffies, ehci->reset_done[i]))) {
  408. if (i < 7)
  409. buf [0] |= 1 << (i + 1);
  410. else
  411. buf [1] |= 1 << (i - 7);
  412. status = STS_PCD;
  413. }
  414. }
  415. /* FIXME autosuspend idle root hubs */
  416. spin_unlock_irqrestore (&ehci->lock, flags);
  417. return status ? retval : 0;
  418. }
  419. /*-------------------------------------------------------------------------*/
  420. static void
  421. ehci_hub_descriptor (
  422. struct ehci_hcd *ehci,
  423. struct usb_hub_descriptor *desc
  424. ) {
  425. int ports = HCS_N_PORTS (ehci->hcs_params);
  426. u16 temp;
  427. desc->bDescriptorType = 0x29;
  428. desc->bPwrOn2PwrGood = 10; /* ehci 1.0, 2.3.9 says 20ms max */
  429. desc->bHubContrCurrent = 0;
  430. desc->bNbrPorts = ports;
  431. temp = 1 + (ports / 8);
  432. desc->bDescLength = 7 + 2 * temp;
  433. /* two bitmaps: ports removable, and usb 1.0 legacy PortPwrCtrlMask */
  434. memset (&desc->bitmap [0], 0, temp);
  435. memset (&desc->bitmap [temp], 0xff, temp);
  436. temp = 0x0008; /* per-port overcurrent reporting */
  437. if (HCS_PPC (ehci->hcs_params))
  438. temp |= 0x0001; /* per-port power control */
  439. else
  440. temp |= 0x0002; /* no power switching */
  441. #if 0
  442. // re-enable when we support USB_PORT_FEAT_INDICATOR below.
  443. if (HCS_INDICATOR (ehci->hcs_params))
  444. temp |= 0x0080; /* per-port indicators (LEDs) */
  445. #endif
  446. desc->wHubCharacteristics = cpu_to_le16(temp);
  447. }
  448. /*-------------------------------------------------------------------------*/
  449. static int ehci_hub_control (
  450. struct usb_hcd *hcd,
  451. u16 typeReq,
  452. u16 wValue,
  453. u16 wIndex,
  454. char *buf,
  455. u16 wLength
  456. ) {
  457. struct ehci_hcd *ehci = hcd_to_ehci (hcd);
  458. int ports = HCS_N_PORTS (ehci->hcs_params);
  459. u32 __iomem *status_reg = &ehci->regs->port_status[
  460. (wIndex & 0xff) - 1];
  461. u32 temp, status;
  462. unsigned long flags;
  463. int retval = 0;
  464. unsigned selector;
  465. /*
  466. * FIXME: support SetPortFeatures USB_PORT_FEAT_INDICATOR.
  467. * HCS_INDICATOR may say we can change LEDs to off/amber/green.
  468. * (track current state ourselves) ... blink for diagnostics,
  469. * power, "this is the one", etc. EHCI spec supports this.
  470. */
  471. spin_lock_irqsave (&ehci->lock, flags);
  472. switch (typeReq) {
  473. case ClearHubFeature:
  474. switch (wValue) {
  475. case C_HUB_LOCAL_POWER:
  476. case C_HUB_OVER_CURRENT:
  477. /* no hub-wide feature/status flags */
  478. break;
  479. default:
  480. goto error;
  481. }
  482. break;
  483. case ClearPortFeature:
  484. if (!wIndex || wIndex > ports)
  485. goto error;
  486. wIndex--;
  487. temp = ehci_readl(ehci, status_reg);
  488. /*
  489. * Even if OWNER is set, so the port is owned by the
  490. * companion controller, khubd needs to be able to clear
  491. * the port-change status bits (especially
  492. * USB_PORT_FEAT_C_CONNECTION).
  493. */
  494. switch (wValue) {
  495. case USB_PORT_FEAT_ENABLE:
  496. ehci_writel(ehci, temp & ~PORT_PE, status_reg);
  497. break;
  498. case USB_PORT_FEAT_C_ENABLE:
  499. ehci_writel(ehci, (temp & ~PORT_RWC_BITS) | PORT_PEC,
  500. status_reg);
  501. break;
  502. case USB_PORT_FEAT_SUSPEND:
  503. if (temp & PORT_RESET)
  504. goto error;
  505. if (ehci->no_selective_suspend)
  506. break;
  507. if (temp & PORT_SUSPEND) {
  508. if ((temp & PORT_PE) == 0)
  509. goto error;
  510. /* resume signaling for 20 msec */
  511. temp &= ~(PORT_RWC_BITS | PORT_WAKE_BITS);
  512. ehci_writel(ehci, temp | PORT_RESUME,
  513. status_reg);
  514. ehci->reset_done [wIndex] = jiffies
  515. + msecs_to_jiffies (20);
  516. }
  517. break;
  518. case USB_PORT_FEAT_C_SUSPEND:
  519. clear_bit(wIndex, &ehci->port_c_suspend);
  520. break;
  521. case USB_PORT_FEAT_POWER:
  522. if (HCS_PPC (ehci->hcs_params))
  523. ehci_writel(ehci,
  524. temp & ~(PORT_RWC_BITS | PORT_POWER),
  525. status_reg);
  526. break;
  527. case USB_PORT_FEAT_C_CONNECTION:
  528. ehci_writel(ehci, (temp & ~PORT_RWC_BITS) | PORT_CSC,
  529. status_reg);
  530. break;
  531. case USB_PORT_FEAT_C_OVER_CURRENT:
  532. ehci_writel(ehci, (temp & ~PORT_RWC_BITS) | PORT_OCC,
  533. status_reg);
  534. break;
  535. case USB_PORT_FEAT_C_RESET:
  536. /* GetPortStatus clears reset */
  537. break;
  538. default:
  539. goto error;
  540. }
  541. ehci_readl(ehci, &ehci->regs->command); /* unblock posted write */
  542. break;
  543. case GetHubDescriptor:
  544. ehci_hub_descriptor (ehci, (struct usb_hub_descriptor *)
  545. buf);
  546. break;
  547. case GetHubStatus:
  548. /* no hub-wide feature/status flags */
  549. memset (buf, 0, 4);
  550. //cpu_to_le32s ((u32 *) buf);
  551. break;
  552. case GetPortStatus:
  553. if (!wIndex || wIndex > ports)
  554. goto error;
  555. wIndex--;
  556. status = 0;
  557. temp = ehci_readl(ehci, status_reg);
  558. // wPortChange bits
  559. if (temp & PORT_CSC)
  560. status |= 1 << USB_PORT_FEAT_C_CONNECTION;
  561. if (temp & PORT_PEC)
  562. status |= 1 << USB_PORT_FEAT_C_ENABLE;
  563. if ((temp & PORT_OCC) && !ignore_oc){
  564. status |= 1 << USB_PORT_FEAT_C_OVER_CURRENT;
  565. /*
  566. * Hubs should disable port power on over-current.
  567. * However, not all EHCI implementations do this
  568. * automatically, even if they _do_ support per-port
  569. * power switching; they're allowed to just limit the
  570. * current. khubd will turn the power back on.
  571. */
  572. if (HCS_PPC (ehci->hcs_params)){
  573. ehci_writel(ehci,
  574. temp & ~(PORT_RWC_BITS | PORT_POWER),
  575. status_reg);
  576. }
  577. }
  578. /* whoever resumes must GetPortStatus to complete it!! */
  579. if (temp & PORT_RESUME) {
  580. /* Remote Wakeup received? */
  581. if (!ehci->reset_done[wIndex]) {
  582. /* resume signaling for 20 msec */
  583. ehci->reset_done[wIndex] = jiffies
  584. + msecs_to_jiffies(20);
  585. /* check the port again */
  586. mod_timer(&ehci_to_hcd(ehci)->rh_timer,
  587. ehci->reset_done[wIndex]);
  588. }
  589. /* resume completed? */
  590. else if (time_after_eq(jiffies,
  591. ehci->reset_done[wIndex])) {
  592. clear_bit(wIndex, &ehci->suspended_ports);
  593. set_bit(wIndex, &ehci->port_c_suspend);
  594. ehci->reset_done[wIndex] = 0;
  595. /* stop resume signaling */
  596. temp = ehci_readl(ehci, status_reg);
  597. ehci_writel(ehci,
  598. temp & ~(PORT_RWC_BITS | PORT_RESUME),
  599. status_reg);
  600. retval = handshake(ehci, status_reg,
  601. PORT_RESUME, 0, 2000 /* 2msec */);
  602. if (retval != 0) {
  603. ehci_err(ehci,
  604. "port %d resume error %d\n",
  605. wIndex + 1, retval);
  606. goto error;
  607. }
  608. temp &= ~(PORT_SUSPEND|PORT_RESUME|(3<<10));
  609. }
  610. }
  611. /* whoever resets must GetPortStatus to complete it!! */
  612. if ((temp & PORT_RESET)
  613. && time_after_eq(jiffies,
  614. ehci->reset_done[wIndex])) {
  615. status |= 1 << USB_PORT_FEAT_C_RESET;
  616. ehci->reset_done [wIndex] = 0;
  617. /* force reset to complete */
  618. ehci_writel(ehci, temp & ~(PORT_RWC_BITS | PORT_RESET),
  619. status_reg);
  620. /* REVISIT: some hardware needs 550+ usec to clear
  621. * this bit; seems too long to spin routinely...
  622. */
  623. retval = handshake(ehci, status_reg,
  624. PORT_RESET, 0, 750);
  625. if (retval != 0) {
  626. ehci_err (ehci, "port %d reset error %d\n",
  627. wIndex + 1, retval);
  628. goto error;
  629. }
  630. /* see what we found out */
  631. temp = check_reset_complete (ehci, wIndex, status_reg,
  632. ehci_readl(ehci, status_reg));
  633. }
  634. if (!(temp & (PORT_RESUME|PORT_RESET)))
  635. ehci->reset_done[wIndex] = 0;
  636. /* transfer dedicated ports to the companion hc */
  637. if ((temp & PORT_CONNECT) &&
  638. test_bit(wIndex, &ehci->companion_ports)) {
  639. temp &= ~PORT_RWC_BITS;
  640. temp |= PORT_OWNER;
  641. ehci_writel(ehci, temp, status_reg);
  642. ehci_dbg(ehci, "port %d --> companion\n", wIndex + 1);
  643. temp = ehci_readl(ehci, status_reg);
  644. }
  645. /*
  646. * Even if OWNER is set, there's no harm letting khubd
  647. * see the wPortStatus values (they should all be 0 except
  648. * for PORT_POWER anyway).
  649. */
  650. if (temp & PORT_CONNECT) {
  651. status |= 1 << USB_PORT_FEAT_CONNECTION;
  652. // status may be from integrated TT
  653. status |= ehci_port_speed(ehci, temp);
  654. }
  655. if (temp & PORT_PE)
  656. status |= 1 << USB_PORT_FEAT_ENABLE;
  657. /* maybe the port was unsuspended without our knowledge */
  658. if (temp & (PORT_SUSPEND|PORT_RESUME)) {
  659. status |= 1 << USB_PORT_FEAT_SUSPEND;
  660. } else if (test_bit(wIndex, &ehci->suspended_ports)) {
  661. clear_bit(wIndex, &ehci->suspended_ports);
  662. ehci->reset_done[wIndex] = 0;
  663. if (temp & PORT_PE)
  664. set_bit(wIndex, &ehci->port_c_suspend);
  665. }
  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. if (test_bit(wIndex, &ehci->port_c_suspend))
  673. status |= 1 << USB_PORT_FEAT_C_SUSPEND;
  674. #ifndef VERBOSE_DEBUG
  675. if (status & ~0xffff) /* only if wPortChange is interesting */
  676. #endif
  677. dbg_port (ehci, "GetStatus", wIndex + 1, temp);
  678. put_unaligned_le32(status, buf);
  679. break;
  680. case SetHubFeature:
  681. switch (wValue) {
  682. case C_HUB_LOCAL_POWER:
  683. case C_HUB_OVER_CURRENT:
  684. /* no hub-wide feature/status flags */
  685. break;
  686. default:
  687. goto error;
  688. }
  689. break;
  690. case SetPortFeature:
  691. selector = wIndex >> 8;
  692. wIndex &= 0xff;
  693. if (!wIndex || wIndex > ports)
  694. goto error;
  695. wIndex--;
  696. temp = ehci_readl(ehci, status_reg);
  697. if (temp & PORT_OWNER)
  698. break;
  699. temp &= ~PORT_RWC_BITS;
  700. switch (wValue) {
  701. case USB_PORT_FEAT_SUSPEND:
  702. if (ehci->no_selective_suspend)
  703. break;
  704. if ((temp & PORT_PE) == 0
  705. || (temp & PORT_RESET) != 0)
  706. goto error;
  707. ehci_writel(ehci, temp | PORT_SUSPEND, status_reg);
  708. set_bit(wIndex, &ehci->suspended_ports);
  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. }
  776. static int ehci_port_handed_over(struct usb_hcd *hcd, int portnum)
  777. {
  778. struct ehci_hcd *ehci = hcd_to_ehci(hcd);
  779. u32 __iomem *reg;
  780. if (ehci_is_TDI(ehci))
  781. return 0;
  782. reg = &ehci->regs->port_status[portnum - 1];
  783. return ehci_readl(ehci, reg) & PORT_OWNER;
  784. }