ehci-hub.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  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_hub_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 (hcd->remote_wakeup)
  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_hub_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. /* re-init operational registers in case we lost power */
  80. if (readl (&ehci->regs->intr_enable) == 0) {
  81. /* at least some APM implementations will try to deliver
  82. * IRQs right away, so delay them until we're ready.
  83. */
  84. intr_enable = 1;
  85. writel (0, &ehci->regs->segment);
  86. writel (ehci->periodic_dma, &ehci->regs->frame_list);
  87. writel ((u32)ehci->async->qh_dma, &ehci->regs->async_next);
  88. } else
  89. intr_enable = 0;
  90. ehci_dbg(ehci, "resume root hub%s\n",
  91. intr_enable ? " after power loss" : "");
  92. /* restore CMD_RUN, framelist size, and irq threshold */
  93. writel (ehci->command, &ehci->regs->command);
  94. /* take ports out of suspend */
  95. i = HCS_N_PORTS (ehci->hcs_params);
  96. while (i--) {
  97. temp = readl (&ehci->regs->port_status [i]);
  98. temp &= ~(PORT_RWC_BITS
  99. | PORT_WKOC_E | PORT_WKDISC_E | PORT_WKCONN_E);
  100. if (temp & PORT_SUSPEND) {
  101. ehci->reset_done [i] = jiffies + msecs_to_jiffies (20);
  102. temp |= PORT_RESUME;
  103. }
  104. writel (temp, &ehci->regs->port_status [i]);
  105. }
  106. i = HCS_N_PORTS (ehci->hcs_params);
  107. mdelay (20);
  108. while (i--) {
  109. temp = readl (&ehci->regs->port_status [i]);
  110. if ((temp & PORT_SUSPEND) == 0)
  111. continue;
  112. temp &= ~(PORT_RWC_BITS | PORT_RESUME);
  113. writel (temp, &ehci->regs->port_status [i]);
  114. ehci_vdbg (ehci, "resumed port %d\n", i + 1);
  115. }
  116. (void) readl (&ehci->regs->command);
  117. /* maybe re-activate the schedule(s) */
  118. temp = 0;
  119. if (ehci->async->qh_next.qh)
  120. temp |= CMD_ASE;
  121. if (ehci->periodic_sched)
  122. temp |= CMD_PSE;
  123. if (temp) {
  124. ehci->command |= temp;
  125. writel (ehci->command, &ehci->regs->command);
  126. }
  127. ehci->next_statechange = jiffies + msecs_to_jiffies(5);
  128. hcd->state = HC_STATE_RUNNING;
  129. /* Now we can safely re-enable irqs */
  130. if (intr_enable)
  131. writel (INTR_MASK, &ehci->regs->intr_enable);
  132. spin_unlock_irq (&ehci->lock);
  133. return 0;
  134. }
  135. #else
  136. #define ehci_hub_suspend NULL
  137. #define ehci_hub_resume NULL
  138. #endif /* CONFIG_PM */
  139. /*-------------------------------------------------------------------------*/
  140. static int check_reset_complete (
  141. struct ehci_hcd *ehci,
  142. int index,
  143. int port_status
  144. ) {
  145. if (!(port_status & PORT_CONNECT)) {
  146. ehci->reset_done [index] = 0;
  147. return port_status;
  148. }
  149. /* if reset finished and it's still not enabled -- handoff */
  150. if (!(port_status & PORT_PE)) {
  151. /* with integrated TT, there's nobody to hand it to! */
  152. if (ehci_is_TDI(ehci)) {
  153. ehci_dbg (ehci,
  154. "Failed to enable port %d on root hub TT\n",
  155. index+1);
  156. return port_status;
  157. }
  158. ehci_dbg (ehci, "port %d full speed --> companion\n",
  159. index + 1);
  160. // what happens if HCS_N_CC(params) == 0 ?
  161. port_status |= PORT_OWNER;
  162. port_status &= ~PORT_RWC_BITS;
  163. writel (port_status, &ehci->regs->port_status [index]);
  164. } else
  165. ehci_dbg (ehci, "port %d high speed\n", index + 1);
  166. return port_status;
  167. }
  168. /*-------------------------------------------------------------------------*/
  169. /* build "status change" packet (one or two bytes) from HC registers */
  170. static int
  171. ehci_hub_status_data (struct usb_hcd *hcd, char *buf)
  172. {
  173. struct ehci_hcd *ehci = hcd_to_ehci (hcd);
  174. u32 temp, status = 0;
  175. int ports, i, retval = 1;
  176. unsigned long flags;
  177. /* if !USB_SUSPEND, root hub timers won't get shut down ... */
  178. if (!HC_IS_RUNNING(hcd->state))
  179. return 0;
  180. /* init status to no-changes */
  181. buf [0] = 0;
  182. ports = HCS_N_PORTS (ehci->hcs_params);
  183. if (ports > 7) {
  184. buf [1] = 0;
  185. retval++;
  186. }
  187. /* no hub change reports (bit 0) for now (power, ...) */
  188. /* port N changes (bit N)? */
  189. spin_lock_irqsave (&ehci->lock, flags);
  190. for (i = 0; i < ports; i++) {
  191. temp = readl (&ehci->regs->port_status [i]);
  192. if (temp & PORT_OWNER) {
  193. /* don't report this in GetPortStatus */
  194. if (temp & PORT_CSC) {
  195. temp &= ~PORT_RWC_BITS;
  196. temp |= PORT_CSC;
  197. writel (temp, &ehci->regs->port_status [i]);
  198. }
  199. continue;
  200. }
  201. if (!(temp & PORT_CONNECT))
  202. ehci->reset_done [i] = 0;
  203. if ((temp & (PORT_CSC | PORT_PEC | PORT_OCC)) != 0
  204. // PORT_STAT_C_SUSPEND?
  205. || ((temp & PORT_RESUME) != 0
  206. && time_after (jiffies,
  207. ehci->reset_done [i]))) {
  208. if (i < 7)
  209. buf [0] |= 1 << (i + 1);
  210. else
  211. buf [1] |= 1 << (i - 7);
  212. status = STS_PCD;
  213. }
  214. }
  215. /* FIXME autosuspend idle root hubs */
  216. spin_unlock_irqrestore (&ehci->lock, flags);
  217. return status ? retval : 0;
  218. }
  219. /*-------------------------------------------------------------------------*/
  220. static void
  221. ehci_hub_descriptor (
  222. struct ehci_hcd *ehci,
  223. struct usb_hub_descriptor *desc
  224. ) {
  225. int ports = HCS_N_PORTS (ehci->hcs_params);
  226. u16 temp;
  227. desc->bDescriptorType = 0x29;
  228. desc->bPwrOn2PwrGood = 10; /* ehci 1.0, 2.3.9 says 20ms max */
  229. desc->bHubContrCurrent = 0;
  230. desc->bNbrPorts = ports;
  231. temp = 1 + (ports / 8);
  232. desc->bDescLength = 7 + 2 * temp;
  233. /* two bitmaps: ports removable, and usb 1.0 legacy PortPwrCtrlMask */
  234. memset (&desc->bitmap [0], 0, temp);
  235. memset (&desc->bitmap [temp], 0xff, temp);
  236. temp = 0x0008; /* per-port overcurrent reporting */
  237. if (HCS_PPC (ehci->hcs_params))
  238. temp |= 0x0001; /* per-port power control */
  239. else
  240. temp |= 0x0002; /* no power switching */
  241. #if 0
  242. // re-enable when we support USB_PORT_FEAT_INDICATOR below.
  243. if (HCS_INDICATOR (ehci->hcs_params))
  244. temp |= 0x0080; /* per-port indicators (LEDs) */
  245. #endif
  246. desc->wHubCharacteristics = (__force __u16)cpu_to_le16 (temp);
  247. }
  248. /*-------------------------------------------------------------------------*/
  249. #define PORT_WAKE_BITS (PORT_WKOC_E|PORT_WKDISC_E|PORT_WKCONN_E)
  250. static int ehci_hub_control (
  251. struct usb_hcd *hcd,
  252. u16 typeReq,
  253. u16 wValue,
  254. u16 wIndex,
  255. char *buf,
  256. u16 wLength
  257. ) {
  258. struct ehci_hcd *ehci = hcd_to_ehci (hcd);
  259. int ports = HCS_N_PORTS (ehci->hcs_params);
  260. u32 temp, status;
  261. unsigned long flags;
  262. int retval = 0;
  263. /*
  264. * FIXME: support SetPortFeatures USB_PORT_FEAT_INDICATOR.
  265. * HCS_INDICATOR may say we can change LEDs to off/amber/green.
  266. * (track current state ourselves) ... blink for diagnostics,
  267. * power, "this is the one", etc. EHCI spec supports this.
  268. */
  269. spin_lock_irqsave (&ehci->lock, flags);
  270. switch (typeReq) {
  271. case ClearHubFeature:
  272. switch (wValue) {
  273. case C_HUB_LOCAL_POWER:
  274. case C_HUB_OVER_CURRENT:
  275. /* no hub-wide feature/status flags */
  276. break;
  277. default:
  278. goto error;
  279. }
  280. break;
  281. case ClearPortFeature:
  282. if (!wIndex || wIndex > ports)
  283. goto error;
  284. wIndex--;
  285. temp = readl (&ehci->regs->port_status [wIndex]);
  286. if (temp & PORT_OWNER)
  287. break;
  288. switch (wValue) {
  289. case USB_PORT_FEAT_ENABLE:
  290. writel (temp & ~PORT_PE,
  291. &ehci->regs->port_status [wIndex]);
  292. break;
  293. case USB_PORT_FEAT_C_ENABLE:
  294. writel((temp & ~PORT_RWC_BITS) | PORT_PEC,
  295. &ehci->regs->port_status [wIndex]);
  296. break;
  297. case USB_PORT_FEAT_SUSPEND:
  298. if (temp & PORT_RESET)
  299. goto error;
  300. if (temp & PORT_SUSPEND) {
  301. if ((temp & PORT_PE) == 0)
  302. goto error;
  303. /* resume signaling for 20 msec */
  304. temp &= ~(PORT_RWC_BITS | PORT_WAKE_BITS);
  305. writel (temp | PORT_RESUME,
  306. &ehci->regs->port_status [wIndex]);
  307. ehci->reset_done [wIndex] = jiffies
  308. + msecs_to_jiffies (20);
  309. }
  310. break;
  311. case USB_PORT_FEAT_C_SUSPEND:
  312. /* we auto-clear this feature */
  313. break;
  314. case USB_PORT_FEAT_POWER:
  315. if (HCS_PPC (ehci->hcs_params))
  316. writel (temp & ~(PORT_RWC_BITS | PORT_POWER),
  317. &ehci->regs->port_status [wIndex]);
  318. break;
  319. case USB_PORT_FEAT_C_CONNECTION:
  320. writel((temp & ~PORT_RWC_BITS) | PORT_CSC,
  321. &ehci->regs->port_status [wIndex]);
  322. break;
  323. case USB_PORT_FEAT_C_OVER_CURRENT:
  324. writel((temp & ~PORT_RWC_BITS) | PORT_OCC,
  325. &ehci->regs->port_status [wIndex]);
  326. break;
  327. case USB_PORT_FEAT_C_RESET:
  328. /* GetPortStatus clears reset */
  329. break;
  330. default:
  331. goto error;
  332. }
  333. readl (&ehci->regs->command); /* unblock posted write */
  334. break;
  335. case GetHubDescriptor:
  336. ehci_hub_descriptor (ehci, (struct usb_hub_descriptor *)
  337. buf);
  338. break;
  339. case GetHubStatus:
  340. /* no hub-wide feature/status flags */
  341. memset (buf, 0, 4);
  342. //cpu_to_le32s ((u32 *) buf);
  343. break;
  344. case GetPortStatus:
  345. if (!wIndex || wIndex > ports)
  346. goto error;
  347. wIndex--;
  348. status = 0;
  349. temp = readl (&ehci->regs->port_status [wIndex]);
  350. // wPortChange bits
  351. if (temp & PORT_CSC)
  352. status |= 1 << USB_PORT_FEAT_C_CONNECTION;
  353. if (temp & PORT_PEC)
  354. status |= 1 << USB_PORT_FEAT_C_ENABLE;
  355. if (temp & PORT_OCC)
  356. status |= 1 << USB_PORT_FEAT_C_OVER_CURRENT;
  357. /* whoever resumes must GetPortStatus to complete it!! */
  358. if ((temp & PORT_RESUME)
  359. && time_after (jiffies,
  360. ehci->reset_done [wIndex])) {
  361. status |= 1 << USB_PORT_FEAT_C_SUSPEND;
  362. ehci->reset_done [wIndex] = 0;
  363. /* stop resume signaling */
  364. temp = readl (&ehci->regs->port_status [wIndex]);
  365. writel (temp & ~(PORT_RWC_BITS | PORT_RESUME),
  366. &ehci->regs->port_status [wIndex]);
  367. retval = handshake (
  368. &ehci->regs->port_status [wIndex],
  369. PORT_RESUME, 0, 2000 /* 2msec */);
  370. if (retval != 0) {
  371. ehci_err (ehci, "port %d resume error %d\n",
  372. wIndex + 1, retval);
  373. goto error;
  374. }
  375. temp &= ~(PORT_SUSPEND|PORT_RESUME|(3<<10));
  376. }
  377. /* whoever resets must GetPortStatus to complete it!! */
  378. if ((temp & PORT_RESET)
  379. && time_after (jiffies,
  380. ehci->reset_done [wIndex])) {
  381. status |= 1 << USB_PORT_FEAT_C_RESET;
  382. ehci->reset_done [wIndex] = 0;
  383. /* force reset to complete */
  384. writel (temp & ~(PORT_RWC_BITS | PORT_RESET),
  385. &ehci->regs->port_status [wIndex]);
  386. /* REVISIT: some hardware needs 550+ usec to clear
  387. * this bit; seems too long to spin routinely...
  388. */
  389. retval = handshake (
  390. &ehci->regs->port_status [wIndex],
  391. PORT_RESET, 0, 750);
  392. if (retval != 0) {
  393. ehci_err (ehci, "port %d reset error %d\n",
  394. wIndex + 1, retval);
  395. goto error;
  396. }
  397. /* see what we found out */
  398. temp = check_reset_complete (ehci, wIndex,
  399. readl (&ehci->regs->port_status [wIndex]));
  400. }
  401. // don't show wPortStatus if it's owned by a companion hc
  402. if (!(temp & PORT_OWNER)) {
  403. if (temp & PORT_CONNECT) {
  404. status |= 1 << USB_PORT_FEAT_CONNECTION;
  405. // status may be from integrated TT
  406. status |= ehci_port_speed(ehci, temp);
  407. }
  408. if (temp & PORT_PE)
  409. status |= 1 << USB_PORT_FEAT_ENABLE;
  410. if (temp & (PORT_SUSPEND|PORT_RESUME))
  411. status |= 1 << USB_PORT_FEAT_SUSPEND;
  412. if (temp & PORT_OC)
  413. status |= 1 << USB_PORT_FEAT_OVER_CURRENT;
  414. if (temp & PORT_RESET)
  415. status |= 1 << USB_PORT_FEAT_RESET;
  416. if (temp & PORT_POWER)
  417. status |= 1 << USB_PORT_FEAT_POWER;
  418. }
  419. #ifndef EHCI_VERBOSE_DEBUG
  420. if (status & ~0xffff) /* only if wPortChange is interesting */
  421. #endif
  422. dbg_port (ehci, "GetStatus", wIndex + 1, temp);
  423. // we "know" this alignment is good, caller used kmalloc()...
  424. *((__le32 *) buf) = cpu_to_le32 (status);
  425. break;
  426. case SetHubFeature:
  427. switch (wValue) {
  428. case C_HUB_LOCAL_POWER:
  429. case C_HUB_OVER_CURRENT:
  430. /* no hub-wide feature/status flags */
  431. break;
  432. default:
  433. goto error;
  434. }
  435. break;
  436. case SetPortFeature:
  437. if (!wIndex || wIndex > ports)
  438. goto error;
  439. wIndex--;
  440. temp = readl (&ehci->regs->port_status [wIndex]);
  441. if (temp & PORT_OWNER)
  442. break;
  443. temp &= ~PORT_RWC_BITS;
  444. switch (wValue) {
  445. case USB_PORT_FEAT_SUSPEND:
  446. if ((temp & PORT_PE) == 0
  447. || (temp & PORT_RESET) != 0)
  448. goto error;
  449. if (hcd->remote_wakeup)
  450. temp |= PORT_WAKE_BITS;
  451. writel (temp | PORT_SUSPEND,
  452. &ehci->regs->port_status [wIndex]);
  453. break;
  454. case USB_PORT_FEAT_POWER:
  455. if (HCS_PPC (ehci->hcs_params))
  456. writel (temp | PORT_POWER,
  457. &ehci->regs->port_status [wIndex]);
  458. break;
  459. case USB_PORT_FEAT_RESET:
  460. if (temp & PORT_RESUME)
  461. goto error;
  462. /* line status bits may report this as low speed,
  463. * which can be fine if this root hub has a
  464. * transaction translator built in.
  465. */
  466. if ((temp & (PORT_PE|PORT_CONNECT)) == PORT_CONNECT
  467. && !ehci_is_TDI(ehci)
  468. && PORT_USB11 (temp)) {
  469. ehci_dbg (ehci,
  470. "port %d low speed --> companion\n",
  471. wIndex + 1);
  472. temp |= PORT_OWNER;
  473. } else {
  474. ehci_vdbg (ehci, "port %d reset\n", wIndex + 1);
  475. temp |= PORT_RESET;
  476. temp &= ~PORT_PE;
  477. /*
  478. * caller must wait, then call GetPortStatus
  479. * usb 2.0 spec says 50 ms resets on root
  480. */
  481. ehci->reset_done [wIndex] = jiffies
  482. + msecs_to_jiffies (50);
  483. }
  484. writel (temp, &ehci->regs->port_status [wIndex]);
  485. break;
  486. default:
  487. goto error;
  488. }
  489. readl (&ehci->regs->command); /* unblock posted writes */
  490. break;
  491. default:
  492. error:
  493. /* "stall" on error */
  494. retval = -EPIPE;
  495. }
  496. spin_unlock_irqrestore (&ehci->lock, flags);
  497. return retval;
  498. }