ehci-hub.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  1. /*
  2. * Copyright (C) 2001-2004 by David Brownell
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License as published by the
  6. * Free Software Foundation; either version 2 of the License, or (at your
  7. * option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  11. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. * for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software Foundation,
  16. * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. /* this file is part of ehci-hcd.c */
  19. /*-------------------------------------------------------------------------*/
  20. /*
  21. * EHCI Root Hub ... the nonsharable stuff
  22. *
  23. * Registers don't need cpu_to_le32, that happens transparently
  24. */
  25. /*-------------------------------------------------------------------------*/
  26. #ifdef CONFIG_PM
  27. static int ehci_bus_suspend (struct usb_hcd *hcd)
  28. {
  29. struct ehci_hcd *ehci = hcd_to_ehci (hcd);
  30. int port;
  31. if (time_before (jiffies, ehci->next_statechange))
  32. msleep(5);
  33. port = HCS_N_PORTS (ehci->hcs_params);
  34. spin_lock_irq (&ehci->lock);
  35. /* stop schedules, clean any completed work */
  36. if (HC_IS_RUNNING(hcd->state)) {
  37. ehci_quiesce (ehci);
  38. hcd->state = HC_STATE_QUIESCING;
  39. }
  40. ehci->command = readl (&ehci->regs->command);
  41. if (ehci->reclaim)
  42. ehci->reclaim_ready = 1;
  43. ehci_work(ehci, NULL);
  44. /* suspend any active/unsuspended ports, maybe allow wakeup */
  45. while (port--) {
  46. u32 __iomem *reg = &ehci->regs->port_status [port];
  47. u32 t1 = readl (reg) & ~PORT_RWC_BITS;
  48. u32 t2 = t1;
  49. if ((t1 & PORT_PE) && !(t1 & PORT_OWNER))
  50. t2 |= PORT_SUSPEND;
  51. if (device_may_wakeup(&hcd->self.root_hub->dev))
  52. t2 |= PORT_WKOC_E|PORT_WKDISC_E|PORT_WKCONN_E;
  53. else
  54. t2 &= ~(PORT_WKOC_E|PORT_WKDISC_E|PORT_WKCONN_E);
  55. if (t1 != t2) {
  56. ehci_vdbg (ehci, "port %d, %08x -> %08x\n",
  57. port + 1, t1, t2);
  58. writel (t2, reg);
  59. }
  60. }
  61. /* turn off now-idle HC */
  62. del_timer_sync (&ehci->watchdog);
  63. ehci_halt (ehci);
  64. hcd->state = HC_STATE_SUSPENDED;
  65. ehci->next_statechange = jiffies + msecs_to_jiffies(10);
  66. spin_unlock_irq (&ehci->lock);
  67. return 0;
  68. }
  69. /* caller has locked the root hub, and should reset/reinit on error */
  70. static int ehci_bus_resume (struct usb_hcd *hcd)
  71. {
  72. struct ehci_hcd *ehci = hcd_to_ehci (hcd);
  73. u32 temp;
  74. int i;
  75. int intr_enable;
  76. if (time_before (jiffies, ehci->next_statechange))
  77. msleep(5);
  78. spin_lock_irq (&ehci->lock);
  79. /* Ideally and we've got a real resume here, and no port's power
  80. * was lost. (For PCI, that means Vaux was maintained.) But we
  81. * could instead be restoring a swsusp snapshot -- so that BIOS was
  82. * the last user of the controller, not reset/pm hardware keeping
  83. * state we gave to it.
  84. */
  85. /* re-init operational registers in case we lost power */
  86. if (readl (&ehci->regs->intr_enable) == 0) {
  87. /* at least some APM implementations will try to deliver
  88. * IRQs right away, so delay them until we're ready.
  89. */
  90. intr_enable = 1;
  91. writel (0, &ehci->regs->segment);
  92. writel (ehci->periodic_dma, &ehci->regs->frame_list);
  93. writel ((u32)ehci->async->qh_dma, &ehci->regs->async_next);
  94. } else
  95. intr_enable = 0;
  96. ehci_dbg(ehci, "resume root hub%s\n",
  97. intr_enable ? " after power loss" : "");
  98. /* restore CMD_RUN, framelist size, and irq threshold */
  99. writel (ehci->command, &ehci->regs->command);
  100. /* take ports out of suspend */
  101. i = HCS_N_PORTS (ehci->hcs_params);
  102. while (i--) {
  103. temp = readl (&ehci->regs->port_status [i]);
  104. temp &= ~(PORT_RWC_BITS
  105. | PORT_WKOC_E | PORT_WKDISC_E | PORT_WKCONN_E);
  106. if (temp & PORT_SUSPEND) {
  107. ehci->reset_done [i] = jiffies + msecs_to_jiffies (20);
  108. temp |= PORT_RESUME;
  109. }
  110. writel (temp, &ehci->regs->port_status [i]);
  111. }
  112. i = HCS_N_PORTS (ehci->hcs_params);
  113. mdelay (20);
  114. while (i--) {
  115. temp = readl (&ehci->regs->port_status [i]);
  116. if ((temp & PORT_SUSPEND) == 0)
  117. continue;
  118. temp &= ~(PORT_RWC_BITS | PORT_RESUME);
  119. writel (temp, &ehci->regs->port_status [i]);
  120. ehci_vdbg (ehci, "resumed port %d\n", i + 1);
  121. }
  122. (void) readl (&ehci->regs->command);
  123. /* maybe re-activate the schedule(s) */
  124. temp = 0;
  125. if (ehci->async->qh_next.qh)
  126. temp |= CMD_ASE;
  127. if (ehci->periodic_sched)
  128. temp |= CMD_PSE;
  129. if (temp) {
  130. ehci->command |= temp;
  131. writel (ehci->command, &ehci->regs->command);
  132. }
  133. ehci->next_statechange = jiffies + msecs_to_jiffies(5);
  134. hcd->state = HC_STATE_RUNNING;
  135. /* Now we can safely re-enable irqs */
  136. if (intr_enable)
  137. writel (INTR_MASK, &ehci->regs->intr_enable);
  138. spin_unlock_irq (&ehci->lock);
  139. return 0;
  140. }
  141. #else
  142. #define ehci_bus_suspend NULL
  143. #define ehci_bus_resume NULL
  144. #endif /* CONFIG_PM */
  145. /*-------------------------------------------------------------------------*/
  146. static int check_reset_complete (
  147. struct ehci_hcd *ehci,
  148. int index,
  149. int port_status
  150. ) {
  151. if (!(port_status & PORT_CONNECT)) {
  152. ehci->reset_done [index] = 0;
  153. return port_status;
  154. }
  155. /* if reset finished and it's still not enabled -- handoff */
  156. if (!(port_status & PORT_PE)) {
  157. /* with integrated TT, there's nobody to hand it to! */
  158. if (ehci_is_TDI(ehci)) {
  159. ehci_dbg (ehci,
  160. "Failed to enable port %d on root hub TT\n",
  161. index+1);
  162. return port_status;
  163. }
  164. ehci_dbg (ehci, "port %d full speed --> companion\n",
  165. index + 1);
  166. // what happens if HCS_N_CC(params) == 0 ?
  167. port_status |= PORT_OWNER;
  168. port_status &= ~PORT_RWC_BITS;
  169. writel (port_status, &ehci->regs->port_status [index]);
  170. } else
  171. ehci_dbg (ehci, "port %d high speed\n", index + 1);
  172. return port_status;
  173. }
  174. /*-------------------------------------------------------------------------*/
  175. /* build "status change" packet (one or two bytes) from HC registers */
  176. static int
  177. ehci_hub_status_data (struct usb_hcd *hcd, char *buf)
  178. {
  179. struct ehci_hcd *ehci = hcd_to_ehci (hcd);
  180. u32 temp, status = 0;
  181. int ports, i, retval = 1;
  182. unsigned long flags;
  183. /* if !USB_SUSPEND, root hub timers won't get shut down ... */
  184. if (!HC_IS_RUNNING(hcd->state))
  185. return 0;
  186. /* init status to no-changes */
  187. buf [0] = 0;
  188. ports = HCS_N_PORTS (ehci->hcs_params);
  189. if (ports > 7) {
  190. buf [1] = 0;
  191. retval++;
  192. }
  193. /* no hub change reports (bit 0) for now (power, ...) */
  194. /* port N changes (bit N)? */
  195. spin_lock_irqsave (&ehci->lock, flags);
  196. for (i = 0; i < ports; i++) {
  197. temp = readl (&ehci->regs->port_status [i]);
  198. if (temp & PORT_OWNER) {
  199. /* don't report this in GetPortStatus */
  200. if (temp & PORT_CSC) {
  201. temp &= ~PORT_RWC_BITS;
  202. temp |= PORT_CSC;
  203. writel (temp, &ehci->regs->port_status [i]);
  204. }
  205. continue;
  206. }
  207. if (!(temp & PORT_CONNECT))
  208. ehci->reset_done [i] = 0;
  209. if ((temp & (PORT_CSC | PORT_PEC | PORT_OCC)) != 0
  210. // PORT_STAT_C_SUSPEND?
  211. || ((temp & PORT_RESUME) != 0
  212. && time_after (jiffies,
  213. ehci->reset_done [i]))) {
  214. if (i < 7)
  215. buf [0] |= 1 << (i + 1);
  216. else
  217. buf [1] |= 1 << (i - 7);
  218. status = STS_PCD;
  219. }
  220. }
  221. /* FIXME autosuspend idle root hubs */
  222. spin_unlock_irqrestore (&ehci->lock, flags);
  223. return status ? retval : 0;
  224. }
  225. /*-------------------------------------------------------------------------*/
  226. static void
  227. ehci_hub_descriptor (
  228. struct ehci_hcd *ehci,
  229. struct usb_hub_descriptor *desc
  230. ) {
  231. int ports = HCS_N_PORTS (ehci->hcs_params);
  232. u16 temp;
  233. desc->bDescriptorType = 0x29;
  234. desc->bPwrOn2PwrGood = 10; /* ehci 1.0, 2.3.9 says 20ms max */
  235. desc->bHubContrCurrent = 0;
  236. desc->bNbrPorts = ports;
  237. temp = 1 + (ports / 8);
  238. desc->bDescLength = 7 + 2 * temp;
  239. /* two bitmaps: ports removable, and usb 1.0 legacy PortPwrCtrlMask */
  240. memset (&desc->bitmap [0], 0, temp);
  241. memset (&desc->bitmap [temp], 0xff, temp);
  242. temp = 0x0008; /* per-port overcurrent reporting */
  243. if (HCS_PPC (ehci->hcs_params))
  244. temp |= 0x0001; /* per-port power control */
  245. else
  246. temp |= 0x0002; /* no power switching */
  247. #if 0
  248. // re-enable when we support USB_PORT_FEAT_INDICATOR below.
  249. if (HCS_INDICATOR (ehci->hcs_params))
  250. temp |= 0x0080; /* per-port indicators (LEDs) */
  251. #endif
  252. desc->wHubCharacteristics = (__force __u16)cpu_to_le16 (temp);
  253. }
  254. /*-------------------------------------------------------------------------*/
  255. #define PORT_WAKE_BITS (PORT_WKOC_E|PORT_WKDISC_E|PORT_WKCONN_E)
  256. static int ehci_hub_control (
  257. struct usb_hcd *hcd,
  258. u16 typeReq,
  259. u16 wValue,
  260. u16 wIndex,
  261. char *buf,
  262. u16 wLength
  263. ) {
  264. struct ehci_hcd *ehci = hcd_to_ehci (hcd);
  265. int ports = HCS_N_PORTS (ehci->hcs_params);
  266. u32 temp, status;
  267. unsigned long flags;
  268. int retval = 0;
  269. /*
  270. * FIXME: support SetPortFeatures USB_PORT_FEAT_INDICATOR.
  271. * HCS_INDICATOR may say we can change LEDs to off/amber/green.
  272. * (track current state ourselves) ... blink for diagnostics,
  273. * power, "this is the one", etc. EHCI spec supports this.
  274. */
  275. spin_lock_irqsave (&ehci->lock, flags);
  276. switch (typeReq) {
  277. case ClearHubFeature:
  278. switch (wValue) {
  279. case C_HUB_LOCAL_POWER:
  280. case C_HUB_OVER_CURRENT:
  281. /* no hub-wide feature/status flags */
  282. break;
  283. default:
  284. goto error;
  285. }
  286. break;
  287. case ClearPortFeature:
  288. if (!wIndex || wIndex > ports)
  289. goto error;
  290. wIndex--;
  291. temp = readl (&ehci->regs->port_status [wIndex]);
  292. if (temp & PORT_OWNER)
  293. break;
  294. switch (wValue) {
  295. case USB_PORT_FEAT_ENABLE:
  296. writel (temp & ~PORT_PE,
  297. &ehci->regs->port_status [wIndex]);
  298. break;
  299. case USB_PORT_FEAT_C_ENABLE:
  300. writel((temp & ~PORT_RWC_BITS) | PORT_PEC,
  301. &ehci->regs->port_status [wIndex]);
  302. break;
  303. case USB_PORT_FEAT_SUSPEND:
  304. if (temp & PORT_RESET)
  305. goto error;
  306. if (ehci->no_selective_suspend)
  307. break;
  308. if (temp & PORT_SUSPEND) {
  309. if ((temp & PORT_PE) == 0)
  310. goto error;
  311. /* resume signaling for 20 msec */
  312. temp &= ~(PORT_RWC_BITS | PORT_WAKE_BITS);
  313. writel (temp | PORT_RESUME,
  314. &ehci->regs->port_status [wIndex]);
  315. ehci->reset_done [wIndex] = jiffies
  316. + msecs_to_jiffies (20);
  317. }
  318. break;
  319. case USB_PORT_FEAT_C_SUSPEND:
  320. /* we auto-clear this feature */
  321. break;
  322. case USB_PORT_FEAT_POWER:
  323. if (HCS_PPC (ehci->hcs_params))
  324. writel (temp & ~(PORT_RWC_BITS | PORT_POWER),
  325. &ehci->regs->port_status [wIndex]);
  326. break;
  327. case USB_PORT_FEAT_C_CONNECTION:
  328. writel((temp & ~PORT_RWC_BITS) | PORT_CSC,
  329. &ehci->regs->port_status [wIndex]);
  330. break;
  331. case USB_PORT_FEAT_C_OVER_CURRENT:
  332. writel((temp & ~PORT_RWC_BITS) | PORT_OCC,
  333. &ehci->regs->port_status [wIndex]);
  334. break;
  335. case USB_PORT_FEAT_C_RESET:
  336. /* GetPortStatus clears reset */
  337. break;
  338. default:
  339. goto error;
  340. }
  341. readl (&ehci->regs->command); /* unblock posted write */
  342. break;
  343. case GetHubDescriptor:
  344. ehci_hub_descriptor (ehci, (struct usb_hub_descriptor *)
  345. buf);
  346. break;
  347. case GetHubStatus:
  348. /* no hub-wide feature/status flags */
  349. memset (buf, 0, 4);
  350. //cpu_to_le32s ((u32 *) buf);
  351. break;
  352. case GetPortStatus:
  353. if (!wIndex || wIndex > ports)
  354. goto error;
  355. wIndex--;
  356. status = 0;
  357. temp = readl (&ehci->regs->port_status [wIndex]);
  358. // wPortChange bits
  359. if (temp & PORT_CSC)
  360. status |= 1 << USB_PORT_FEAT_C_CONNECTION;
  361. if (temp & PORT_PEC)
  362. status |= 1 << USB_PORT_FEAT_C_ENABLE;
  363. if (temp & PORT_OCC)
  364. status |= 1 << USB_PORT_FEAT_C_OVER_CURRENT;
  365. /* whoever resumes must GetPortStatus to complete it!! */
  366. if ((temp & PORT_RESUME)
  367. && time_after (jiffies,
  368. ehci->reset_done [wIndex])) {
  369. status |= 1 << USB_PORT_FEAT_C_SUSPEND;
  370. ehci->reset_done [wIndex] = 0;
  371. /* stop resume signaling */
  372. temp = readl (&ehci->regs->port_status [wIndex]);
  373. writel (temp & ~(PORT_RWC_BITS | PORT_RESUME),
  374. &ehci->regs->port_status [wIndex]);
  375. retval = handshake (
  376. &ehci->regs->port_status [wIndex],
  377. PORT_RESUME, 0, 2000 /* 2msec */);
  378. if (retval != 0) {
  379. ehci_err (ehci, "port %d resume error %d\n",
  380. wIndex + 1, retval);
  381. goto error;
  382. }
  383. temp &= ~(PORT_SUSPEND|PORT_RESUME|(3<<10));
  384. }
  385. /* whoever resets must GetPortStatus to complete it!! */
  386. if ((temp & PORT_RESET)
  387. && time_after (jiffies,
  388. ehci->reset_done [wIndex])) {
  389. status |= 1 << USB_PORT_FEAT_C_RESET;
  390. ehci->reset_done [wIndex] = 0;
  391. /* force reset to complete */
  392. writel (temp & ~(PORT_RWC_BITS | PORT_RESET),
  393. &ehci->regs->port_status [wIndex]);
  394. /* REVISIT: some hardware needs 550+ usec to clear
  395. * this bit; seems too long to spin routinely...
  396. */
  397. retval = handshake (
  398. &ehci->regs->port_status [wIndex],
  399. PORT_RESET, 0, 750);
  400. if (retval != 0) {
  401. ehci_err (ehci, "port %d reset error %d\n",
  402. wIndex + 1, retval);
  403. goto error;
  404. }
  405. /* see what we found out */
  406. temp = check_reset_complete (ehci, wIndex,
  407. readl (&ehci->regs->port_status [wIndex]));
  408. }
  409. // don't show wPortStatus if it's owned by a companion hc
  410. if (!(temp & PORT_OWNER)) {
  411. if (temp & PORT_CONNECT) {
  412. status |= 1 << USB_PORT_FEAT_CONNECTION;
  413. // status may be from integrated TT
  414. status |= ehci_port_speed(ehci, temp);
  415. }
  416. if (temp & PORT_PE)
  417. status |= 1 << USB_PORT_FEAT_ENABLE;
  418. if (temp & (PORT_SUSPEND|PORT_RESUME))
  419. status |= 1 << USB_PORT_FEAT_SUSPEND;
  420. if (temp & PORT_OC)
  421. status |= 1 << USB_PORT_FEAT_OVER_CURRENT;
  422. if (temp & PORT_RESET)
  423. status |= 1 << USB_PORT_FEAT_RESET;
  424. if (temp & PORT_POWER)
  425. status |= 1 << USB_PORT_FEAT_POWER;
  426. }
  427. #ifndef EHCI_VERBOSE_DEBUG
  428. if (status & ~0xffff) /* only if wPortChange is interesting */
  429. #endif
  430. dbg_port (ehci, "GetStatus", wIndex + 1, temp);
  431. // we "know" this alignment is good, caller used kmalloc()...
  432. *((__le32 *) buf) = cpu_to_le32 (status);
  433. break;
  434. case SetHubFeature:
  435. switch (wValue) {
  436. case C_HUB_LOCAL_POWER:
  437. case C_HUB_OVER_CURRENT:
  438. /* no hub-wide feature/status flags */
  439. break;
  440. default:
  441. goto error;
  442. }
  443. break;
  444. case SetPortFeature:
  445. if (!wIndex || wIndex > ports)
  446. goto error;
  447. wIndex--;
  448. temp = readl (&ehci->regs->port_status [wIndex]);
  449. if (temp & PORT_OWNER)
  450. break;
  451. temp &= ~PORT_RWC_BITS;
  452. switch (wValue) {
  453. case USB_PORT_FEAT_SUSPEND:
  454. if (ehci->no_selective_suspend)
  455. break;
  456. if ((temp & PORT_PE) == 0
  457. || (temp & PORT_RESET) != 0)
  458. goto error;
  459. if (device_may_wakeup(&hcd->self.root_hub->dev))
  460. temp |= PORT_WAKE_BITS;
  461. writel (temp | PORT_SUSPEND,
  462. &ehci->regs->port_status [wIndex]);
  463. break;
  464. case USB_PORT_FEAT_POWER:
  465. if (HCS_PPC (ehci->hcs_params))
  466. writel (temp | PORT_POWER,
  467. &ehci->regs->port_status [wIndex]);
  468. break;
  469. case USB_PORT_FEAT_RESET:
  470. if (temp & PORT_RESUME)
  471. goto error;
  472. /* line status bits may report this as low speed,
  473. * which can be fine if this root hub has a
  474. * transaction translator built in.
  475. */
  476. if ((temp & (PORT_PE|PORT_CONNECT)) == PORT_CONNECT
  477. && !ehci_is_TDI(ehci)
  478. && PORT_USB11 (temp)) {
  479. ehci_dbg (ehci,
  480. "port %d low speed --> companion\n",
  481. wIndex + 1);
  482. temp |= PORT_OWNER;
  483. } else {
  484. ehci_vdbg (ehci, "port %d reset\n", wIndex + 1);
  485. temp |= PORT_RESET;
  486. temp &= ~PORT_PE;
  487. /*
  488. * caller must wait, then call GetPortStatus
  489. * usb 2.0 spec says 50 ms resets on root
  490. */
  491. ehci->reset_done [wIndex] = jiffies
  492. + msecs_to_jiffies (50);
  493. }
  494. writel (temp, &ehci->regs->port_status [wIndex]);
  495. break;
  496. default:
  497. goto error;
  498. }
  499. readl (&ehci->regs->command); /* unblock posted writes */
  500. break;
  501. default:
  502. error:
  503. /* "stall" on error */
  504. retval = -EPIPE;
  505. }
  506. spin_unlock_irqrestore (&ehci->lock, flags);
  507. return retval;
  508. }