ehci-hub.c 27 KB

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