ehci-hub.c 27 KB

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