ehci-hub.c 27 KB

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