xhci-hub.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743
  1. /*
  2. * xHCI host controller driver
  3. *
  4. * Copyright (C) 2008 Intel Corp.
  5. *
  6. * Author: Sarah Sharp
  7. * Some code borrowed from the Linux EHCI driver.
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  15. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  16. * for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software Foundation,
  20. * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. */
  22. #include <asm/unaligned.h>
  23. #include "xhci.h"
  24. #define PORT_WAKE_BITS (PORT_WKOC_E | PORT_WKDISC_E | PORT_WKCONN_E)
  25. #define PORT_RWC_BITS (PORT_CSC | PORT_PEC | PORT_WRC | PORT_OCC | \
  26. PORT_RC | PORT_PLC | PORT_PE)
  27. static void xhci_hub_descriptor(struct xhci_hcd *xhci,
  28. struct usb_hub_descriptor *desc)
  29. {
  30. int ports;
  31. u16 temp;
  32. ports = HCS_MAX_PORTS(xhci->hcs_params1);
  33. /* USB 3.0 hubs have a different descriptor, but we fake this for now */
  34. desc->bDescriptorType = 0x29;
  35. desc->bPwrOn2PwrGood = 10; /* xhci section 5.4.9 says 20ms max */
  36. desc->bHubContrCurrent = 0;
  37. desc->bNbrPorts = ports;
  38. temp = 1 + (ports / 8);
  39. desc->bDescLength = 7 + 2 * temp;
  40. memset(&desc->DeviceRemovable[0], 0, temp);
  41. memset(&desc->DeviceRemovable[temp], 0xff, temp);
  42. /* Ugh, these should be #defines, FIXME */
  43. /* Using table 11-13 in USB 2.0 spec. */
  44. temp = 0;
  45. /* Bits 1:0 - support port power switching, or power always on */
  46. if (HCC_PPC(xhci->hcc_params))
  47. temp |= 0x0001;
  48. else
  49. temp |= 0x0002;
  50. /* Bit 2 - root hubs are not part of a compound device */
  51. /* Bits 4:3 - individual port over current protection */
  52. temp |= 0x0008;
  53. /* Bits 6:5 - no TTs in root ports */
  54. /* Bit 7 - no port indicators */
  55. desc->wHubCharacteristics = (__force __u16) cpu_to_le16(temp);
  56. }
  57. static unsigned int xhci_port_speed(unsigned int port_status)
  58. {
  59. if (DEV_LOWSPEED(port_status))
  60. return USB_PORT_STAT_LOW_SPEED;
  61. if (DEV_HIGHSPEED(port_status))
  62. return USB_PORT_STAT_HIGH_SPEED;
  63. if (DEV_SUPERSPEED(port_status))
  64. return USB_PORT_STAT_SUPER_SPEED;
  65. /*
  66. * FIXME: Yes, we should check for full speed, but the core uses that as
  67. * a default in portspeed() in usb/core/hub.c (which is the only place
  68. * USB_PORT_STAT_*_SPEED is used).
  69. */
  70. return 0;
  71. }
  72. /*
  73. * These bits are Read Only (RO) and should be saved and written to the
  74. * registers: 0, 3, 10:13, 30
  75. * connect status, over-current status, port speed, and device removable.
  76. * connect status and port speed are also sticky - meaning they're in
  77. * the AUX well and they aren't changed by a hot, warm, or cold reset.
  78. */
  79. #define XHCI_PORT_RO ((1<<0) | (1<<3) | (0xf<<10) | (1<<30))
  80. /*
  81. * These bits are RW; writing a 0 clears the bit, writing a 1 sets the bit:
  82. * bits 5:8, 9, 14:15, 25:27
  83. * link state, port power, port indicator state, "wake on" enable state
  84. */
  85. #define XHCI_PORT_RWS ((0xf<<5) | (1<<9) | (0x3<<14) | (0x7<<25))
  86. /*
  87. * These bits are RW; writing a 1 sets the bit, writing a 0 has no effect:
  88. * bit 4 (port reset)
  89. */
  90. #define XHCI_PORT_RW1S ((1<<4))
  91. /*
  92. * These bits are RW; writing a 1 clears the bit, writing a 0 has no effect:
  93. * bits 1, 17, 18, 19, 20, 21, 22, 23
  94. * port enable/disable, and
  95. * change bits: connect, PED, warm port reset changed (reserved zero for USB 2.0 ports),
  96. * over-current, reset, link state, and L1 change
  97. */
  98. #define XHCI_PORT_RW1CS ((1<<1) | (0x7f<<17))
  99. /*
  100. * Bit 16 is RW, and writing a '1' to it causes the link state control to be
  101. * latched in
  102. */
  103. #define XHCI_PORT_RW ((1<<16))
  104. /*
  105. * These bits are Reserved Zero (RsvdZ) and zero should be written to them:
  106. * bits 2, 24, 28:31
  107. */
  108. #define XHCI_PORT_RZ ((1<<2) | (1<<24) | (0xf<<28))
  109. /*
  110. * Given a port state, this function returns a value that would result in the
  111. * port being in the same state, if the value was written to the port status
  112. * control register.
  113. * Save Read Only (RO) bits and save read/write bits where
  114. * writing a 0 clears the bit and writing a 1 sets the bit (RWS).
  115. * For all other types (RW1S, RW1CS, RW, and RZ), writing a '0' has no effect.
  116. */
  117. u32 xhci_port_state_to_neutral(u32 state)
  118. {
  119. /* Save read-only status and port state */
  120. return (state & XHCI_PORT_RO) | (state & XHCI_PORT_RWS);
  121. }
  122. /*
  123. * find slot id based on port number.
  124. */
  125. int xhci_find_slot_id_by_port(struct xhci_hcd *xhci, u16 port)
  126. {
  127. int slot_id;
  128. int i;
  129. slot_id = 0;
  130. for (i = 0; i < MAX_HC_SLOTS; i++) {
  131. if (!xhci->devs[i])
  132. continue;
  133. if (xhci->devs[i]->port == port) {
  134. slot_id = i;
  135. break;
  136. }
  137. }
  138. return slot_id;
  139. }
  140. /*
  141. * Stop device
  142. * It issues stop endpoint command for EP 0 to 30. And wait the last command
  143. * to complete.
  144. * suspend will set to 1, if suspend bit need to set in command.
  145. */
  146. static int xhci_stop_device(struct xhci_hcd *xhci, int slot_id, int suspend)
  147. {
  148. struct xhci_virt_device *virt_dev;
  149. struct xhci_command *cmd;
  150. unsigned long flags;
  151. int timeleft;
  152. int ret;
  153. int i;
  154. ret = 0;
  155. virt_dev = xhci->devs[slot_id];
  156. cmd = xhci_alloc_command(xhci, false, true, GFP_NOIO);
  157. if (!cmd) {
  158. xhci_dbg(xhci, "Couldn't allocate command structure.\n");
  159. return -ENOMEM;
  160. }
  161. spin_lock_irqsave(&xhci->lock, flags);
  162. for (i = LAST_EP_INDEX; i > 0; i--) {
  163. if (virt_dev->eps[i].ring && virt_dev->eps[i].ring->dequeue)
  164. xhci_queue_stop_endpoint(xhci, slot_id, i, suspend);
  165. }
  166. cmd->command_trb = xhci->cmd_ring->enqueue;
  167. list_add_tail(&cmd->cmd_list, &virt_dev->cmd_list);
  168. xhci_queue_stop_endpoint(xhci, slot_id, 0, suspend);
  169. xhci_ring_cmd_db(xhci);
  170. spin_unlock_irqrestore(&xhci->lock, flags);
  171. /* Wait for last stop endpoint command to finish */
  172. timeleft = wait_for_completion_interruptible_timeout(
  173. cmd->completion,
  174. USB_CTRL_SET_TIMEOUT);
  175. if (timeleft <= 0) {
  176. xhci_warn(xhci, "%s while waiting for stop endpoint command\n",
  177. timeleft == 0 ? "Timeout" : "Signal");
  178. spin_lock_irqsave(&xhci->lock, flags);
  179. /* The timeout might have raced with the event ring handler, so
  180. * only delete from the list if the item isn't poisoned.
  181. */
  182. if (cmd->cmd_list.next != LIST_POISON1)
  183. list_del(&cmd->cmd_list);
  184. spin_unlock_irqrestore(&xhci->lock, flags);
  185. ret = -ETIME;
  186. goto command_cleanup;
  187. }
  188. command_cleanup:
  189. xhci_free_command(xhci, cmd);
  190. return ret;
  191. }
  192. /*
  193. * Ring device, it rings the all doorbells unconditionally.
  194. */
  195. void xhci_ring_device(struct xhci_hcd *xhci, int slot_id)
  196. {
  197. int i;
  198. for (i = 0; i < LAST_EP_INDEX + 1; i++)
  199. if (xhci->devs[slot_id]->eps[i].ring &&
  200. xhci->devs[slot_id]->eps[i].ring->dequeue)
  201. xhci_ring_ep_doorbell(xhci, slot_id, i, 0);
  202. return;
  203. }
  204. static void xhci_disable_port(struct xhci_hcd *xhci, u16 wIndex,
  205. u32 __iomem *addr, u32 port_status)
  206. {
  207. /* Don't allow the USB core to disable SuperSpeed ports. */
  208. if (xhci->port_array[wIndex] == 0x03) {
  209. xhci_dbg(xhci, "Ignoring request to disable "
  210. "SuperSpeed port.\n");
  211. return;
  212. }
  213. /* Write 1 to disable the port */
  214. xhci_writel(xhci, port_status | PORT_PE, addr);
  215. port_status = xhci_readl(xhci, addr);
  216. xhci_dbg(xhci, "disable port, actual port %d status = 0x%x\n",
  217. wIndex, port_status);
  218. }
  219. static void xhci_clear_port_change_bit(struct xhci_hcd *xhci, u16 wValue,
  220. u16 wIndex, u32 __iomem *addr, u32 port_status)
  221. {
  222. char *port_change_bit;
  223. u32 status;
  224. switch (wValue) {
  225. case USB_PORT_FEAT_C_RESET:
  226. status = PORT_RC;
  227. port_change_bit = "reset";
  228. break;
  229. case USB_PORT_FEAT_C_CONNECTION:
  230. status = PORT_CSC;
  231. port_change_bit = "connect";
  232. break;
  233. case USB_PORT_FEAT_C_OVER_CURRENT:
  234. status = PORT_OCC;
  235. port_change_bit = "over-current";
  236. break;
  237. case USB_PORT_FEAT_C_ENABLE:
  238. status = PORT_PEC;
  239. port_change_bit = "enable/disable";
  240. break;
  241. case USB_PORT_FEAT_C_SUSPEND:
  242. status = PORT_PLC;
  243. port_change_bit = "suspend/resume";
  244. break;
  245. default:
  246. /* Should never happen */
  247. return;
  248. }
  249. /* Change bits are all write 1 to clear */
  250. xhci_writel(xhci, port_status | status, addr);
  251. port_status = xhci_readl(xhci, addr);
  252. xhci_dbg(xhci, "clear port %s change, actual port %d status = 0x%x\n",
  253. port_change_bit, wIndex, port_status);
  254. }
  255. int xhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
  256. u16 wIndex, char *buf, u16 wLength)
  257. {
  258. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  259. int ports;
  260. unsigned long flags;
  261. u32 temp, temp1, status;
  262. int retval = 0;
  263. u32 __iomem *addr;
  264. int slot_id;
  265. ports = HCS_MAX_PORTS(xhci->hcs_params1);
  266. spin_lock_irqsave(&xhci->lock, flags);
  267. switch (typeReq) {
  268. case GetHubStatus:
  269. /* No power source, over-current reported per port */
  270. memset(buf, 0, 4);
  271. break;
  272. case GetHubDescriptor:
  273. xhci_hub_descriptor(xhci, (struct usb_hub_descriptor *) buf);
  274. break;
  275. case GetPortStatus:
  276. if (!wIndex || wIndex > ports)
  277. goto error;
  278. wIndex--;
  279. status = 0;
  280. addr = &xhci->op_regs->port_status_base + NUM_PORT_REGS*(wIndex & 0xff);
  281. temp = xhci_readl(xhci, addr);
  282. xhci_dbg(xhci, "get port status, actual port %d status = 0x%x\n", wIndex, temp);
  283. /* wPortChange bits */
  284. if (temp & PORT_CSC)
  285. status |= USB_PORT_STAT_C_CONNECTION << 16;
  286. if (temp & PORT_PEC)
  287. status |= USB_PORT_STAT_C_ENABLE << 16;
  288. if ((temp & PORT_OCC))
  289. status |= USB_PORT_STAT_C_OVERCURRENT << 16;
  290. /*
  291. * FIXME ignoring reset and USB 2.1/3.0 specific
  292. * changes
  293. */
  294. if ((temp & PORT_PLS_MASK) == XDEV_U3
  295. && (temp & PORT_POWER))
  296. status |= 1 << USB_PORT_FEAT_SUSPEND;
  297. if ((temp & PORT_PLS_MASK) == XDEV_RESUME) {
  298. if ((temp & PORT_RESET) || !(temp & PORT_PE))
  299. goto error;
  300. if (!DEV_SUPERSPEED(temp) && time_after_eq(jiffies,
  301. xhci->resume_done[wIndex])) {
  302. xhci_dbg(xhci, "Resume USB2 port %d\n",
  303. wIndex + 1);
  304. xhci->resume_done[wIndex] = 0;
  305. temp1 = xhci_port_state_to_neutral(temp);
  306. temp1 &= ~PORT_PLS_MASK;
  307. temp1 |= PORT_LINK_STROBE | XDEV_U0;
  308. xhci_writel(xhci, temp1, addr);
  309. xhci_dbg(xhci, "set port %d resume\n",
  310. wIndex + 1);
  311. slot_id = xhci_find_slot_id_by_port(xhci,
  312. wIndex + 1);
  313. if (!slot_id) {
  314. xhci_dbg(xhci, "slot_id is zero\n");
  315. goto error;
  316. }
  317. xhci_ring_device(xhci, slot_id);
  318. xhci->port_c_suspend |= 1 << wIndex;
  319. xhci->suspended_ports &= ~(1 << wIndex);
  320. }
  321. }
  322. if ((temp & PORT_PLS_MASK) == XDEV_U0
  323. && (temp & PORT_POWER)
  324. && (xhci->suspended_ports & (1 << wIndex))) {
  325. xhci->suspended_ports &= ~(1 << wIndex);
  326. xhci->port_c_suspend |= 1 << wIndex;
  327. }
  328. if (temp & PORT_CONNECT) {
  329. status |= USB_PORT_STAT_CONNECTION;
  330. status |= xhci_port_speed(temp);
  331. }
  332. if (temp & PORT_PE)
  333. status |= USB_PORT_STAT_ENABLE;
  334. if (temp & PORT_OC)
  335. status |= USB_PORT_STAT_OVERCURRENT;
  336. if (temp & PORT_RESET)
  337. status |= USB_PORT_STAT_RESET;
  338. if (temp & PORT_POWER)
  339. status |= USB_PORT_STAT_POWER;
  340. if (xhci->port_c_suspend & (1 << wIndex))
  341. status |= 1 << USB_PORT_FEAT_C_SUSPEND;
  342. xhci_dbg(xhci, "Get port status returned 0x%x\n", status);
  343. put_unaligned(cpu_to_le32(status), (__le32 *) buf);
  344. break;
  345. case SetPortFeature:
  346. wIndex &= 0xff;
  347. if (!wIndex || wIndex > ports)
  348. goto error;
  349. wIndex--;
  350. addr = &xhci->op_regs->port_status_base + NUM_PORT_REGS*(wIndex & 0xff);
  351. temp = xhci_readl(xhci, addr);
  352. temp = xhci_port_state_to_neutral(temp);
  353. switch (wValue) {
  354. case USB_PORT_FEAT_SUSPEND:
  355. temp = xhci_readl(xhci, addr);
  356. /* In spec software should not attempt to suspend
  357. * a port unless the port reports that it is in the
  358. * enabled (PED = ‘1’,PLS < ‘3’) state.
  359. */
  360. if ((temp & PORT_PE) == 0 || (temp & PORT_RESET)
  361. || (temp & PORT_PLS_MASK) >= XDEV_U3) {
  362. xhci_warn(xhci, "USB core suspending device "
  363. "not in U0/U1/U2.\n");
  364. goto error;
  365. }
  366. slot_id = xhci_find_slot_id_by_port(xhci, wIndex + 1);
  367. if (!slot_id) {
  368. xhci_warn(xhci, "slot_id is zero\n");
  369. goto error;
  370. }
  371. /* unlock to execute stop endpoint commands */
  372. spin_unlock_irqrestore(&xhci->lock, flags);
  373. xhci_stop_device(xhci, slot_id, 1);
  374. spin_lock_irqsave(&xhci->lock, flags);
  375. temp = xhci_port_state_to_neutral(temp);
  376. temp &= ~PORT_PLS_MASK;
  377. temp |= PORT_LINK_STROBE | XDEV_U3;
  378. xhci_writel(xhci, temp, addr);
  379. spin_unlock_irqrestore(&xhci->lock, flags);
  380. msleep(10); /* wait device to enter */
  381. spin_lock_irqsave(&xhci->lock, flags);
  382. temp = xhci_readl(xhci, addr);
  383. xhci->suspended_ports |= 1 << wIndex;
  384. break;
  385. case USB_PORT_FEAT_POWER:
  386. /*
  387. * Turn on ports, even if there isn't per-port switching.
  388. * HC will report connect events even before this is set.
  389. * However, khubd will ignore the roothub events until
  390. * the roothub is registered.
  391. */
  392. xhci_writel(xhci, temp | PORT_POWER, addr);
  393. temp = xhci_readl(xhci, addr);
  394. xhci_dbg(xhci, "set port power, actual port %d status = 0x%x\n", wIndex, temp);
  395. break;
  396. case USB_PORT_FEAT_RESET:
  397. temp = (temp | PORT_RESET);
  398. xhci_writel(xhci, temp, addr);
  399. temp = xhci_readl(xhci, addr);
  400. xhci_dbg(xhci, "set port reset, actual port %d status = 0x%x\n", wIndex, temp);
  401. break;
  402. default:
  403. goto error;
  404. }
  405. temp = xhci_readl(xhci, addr); /* unblock any posted writes */
  406. break;
  407. case ClearPortFeature:
  408. if (!wIndex || wIndex > ports)
  409. goto error;
  410. wIndex--;
  411. addr = &xhci->op_regs->port_status_base +
  412. NUM_PORT_REGS*(wIndex & 0xff);
  413. temp = xhci_readl(xhci, addr);
  414. temp = xhci_port_state_to_neutral(temp);
  415. switch (wValue) {
  416. case USB_PORT_FEAT_SUSPEND:
  417. temp = xhci_readl(xhci, addr);
  418. xhci_dbg(xhci, "clear USB_PORT_FEAT_SUSPEND\n");
  419. xhci_dbg(xhci, "PORTSC %04x\n", temp);
  420. if (temp & PORT_RESET)
  421. goto error;
  422. if (temp & XDEV_U3) {
  423. if ((temp & PORT_PE) == 0)
  424. goto error;
  425. if (DEV_SUPERSPEED(temp)) {
  426. temp = xhci_port_state_to_neutral(temp);
  427. temp &= ~PORT_PLS_MASK;
  428. temp |= PORT_LINK_STROBE | XDEV_U0;
  429. xhci_writel(xhci, temp, addr);
  430. xhci_readl(xhci, addr);
  431. } else {
  432. temp = xhci_port_state_to_neutral(temp);
  433. temp &= ~PORT_PLS_MASK;
  434. temp |= PORT_LINK_STROBE | XDEV_RESUME;
  435. xhci_writel(xhci, temp, addr);
  436. spin_unlock_irqrestore(&xhci->lock,
  437. flags);
  438. msleep(20);
  439. spin_lock_irqsave(&xhci->lock, flags);
  440. temp = xhci_readl(xhci, addr);
  441. temp = xhci_port_state_to_neutral(temp);
  442. temp &= ~PORT_PLS_MASK;
  443. temp |= PORT_LINK_STROBE | XDEV_U0;
  444. xhci_writel(xhci, temp, addr);
  445. }
  446. xhci->port_c_suspend |= 1 << wIndex;
  447. }
  448. slot_id = xhci_find_slot_id_by_port(xhci, wIndex + 1);
  449. if (!slot_id) {
  450. xhci_dbg(xhci, "slot_id is zero\n");
  451. goto error;
  452. }
  453. xhci_ring_device(xhci, slot_id);
  454. break;
  455. case USB_PORT_FEAT_C_SUSPEND:
  456. xhci->port_c_suspend &= ~(1 << wIndex);
  457. case USB_PORT_FEAT_C_RESET:
  458. case USB_PORT_FEAT_C_CONNECTION:
  459. case USB_PORT_FEAT_C_OVER_CURRENT:
  460. case USB_PORT_FEAT_C_ENABLE:
  461. xhci_clear_port_change_bit(xhci, wValue, wIndex,
  462. addr, temp);
  463. break;
  464. case USB_PORT_FEAT_ENABLE:
  465. xhci_disable_port(xhci, wIndex, addr, temp);
  466. break;
  467. default:
  468. goto error;
  469. }
  470. break;
  471. default:
  472. error:
  473. /* "stall" on error */
  474. retval = -EPIPE;
  475. }
  476. spin_unlock_irqrestore(&xhci->lock, flags);
  477. return retval;
  478. }
  479. /*
  480. * Returns 0 if the status hasn't changed, or the number of bytes in buf.
  481. * Ports are 0-indexed from the HCD point of view,
  482. * and 1-indexed from the USB core pointer of view.
  483. *
  484. * Note that the status change bits will be cleared as soon as a port status
  485. * change event is generated, so we use the saved status from that event.
  486. */
  487. int xhci_hub_status_data(struct usb_hcd *hcd, char *buf)
  488. {
  489. unsigned long flags;
  490. u32 temp, status;
  491. u32 mask;
  492. int i, retval;
  493. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  494. int ports;
  495. u32 __iomem *addr;
  496. ports = HCS_MAX_PORTS(xhci->hcs_params1);
  497. /* Initial status is no changes */
  498. retval = (ports + 8) / 8;
  499. memset(buf, 0, retval);
  500. status = 0;
  501. mask = PORT_CSC | PORT_PEC | PORT_OCC;
  502. spin_lock_irqsave(&xhci->lock, flags);
  503. /* For each port, did anything change? If so, set that bit in buf. */
  504. for (i = 0; i < ports; i++) {
  505. addr = &xhci->op_regs->port_status_base +
  506. NUM_PORT_REGS*i;
  507. temp = xhci_readl(xhci, addr);
  508. if ((temp & mask) != 0 ||
  509. (xhci->port_c_suspend & 1 << i) ||
  510. (xhci->resume_done[i] && time_after_eq(
  511. jiffies, xhci->resume_done[i]))) {
  512. buf[(i + 1) / 8] |= 1 << (i + 1) % 8;
  513. status = 1;
  514. }
  515. }
  516. spin_unlock_irqrestore(&xhci->lock, flags);
  517. return status ? retval : 0;
  518. }
  519. #ifdef CONFIG_PM
  520. int xhci_bus_suspend(struct usb_hcd *hcd)
  521. {
  522. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  523. int port;
  524. unsigned long flags;
  525. xhci_dbg(xhci, "suspend root hub\n");
  526. spin_lock_irqsave(&xhci->lock, flags);
  527. if (hcd->self.root_hub->do_remote_wakeup) {
  528. port = HCS_MAX_PORTS(xhci->hcs_params1);
  529. while (port--) {
  530. if (xhci->resume_done[port] != 0) {
  531. spin_unlock_irqrestore(&xhci->lock, flags);
  532. xhci_dbg(xhci, "suspend failed because "
  533. "port %d is resuming\n",
  534. port + 1);
  535. return -EBUSY;
  536. }
  537. }
  538. }
  539. port = HCS_MAX_PORTS(xhci->hcs_params1);
  540. xhci->bus_suspended = 0;
  541. while (port--) {
  542. /* suspend the port if the port is not suspended */
  543. u32 __iomem *addr;
  544. u32 t1, t2;
  545. int slot_id;
  546. addr = &xhci->op_regs->port_status_base +
  547. NUM_PORT_REGS * (port & 0xff);
  548. t1 = xhci_readl(xhci, addr);
  549. t2 = xhci_port_state_to_neutral(t1);
  550. if ((t1 & PORT_PE) && !(t1 & PORT_PLS_MASK)) {
  551. xhci_dbg(xhci, "port %d not suspended\n", port);
  552. slot_id = xhci_find_slot_id_by_port(xhci, port + 1);
  553. if (slot_id) {
  554. spin_unlock_irqrestore(&xhci->lock, flags);
  555. xhci_stop_device(xhci, slot_id, 1);
  556. spin_lock_irqsave(&xhci->lock, flags);
  557. }
  558. t2 &= ~PORT_PLS_MASK;
  559. t2 |= PORT_LINK_STROBE | XDEV_U3;
  560. set_bit(port, &xhci->bus_suspended);
  561. }
  562. if (hcd->self.root_hub->do_remote_wakeup) {
  563. if (t1 & PORT_CONNECT) {
  564. t2 |= PORT_WKOC_E | PORT_WKDISC_E;
  565. t2 &= ~PORT_WKCONN_E;
  566. } else {
  567. t2 |= PORT_WKOC_E | PORT_WKCONN_E;
  568. t2 &= ~PORT_WKDISC_E;
  569. }
  570. } else
  571. t2 &= ~PORT_WAKE_BITS;
  572. t1 = xhci_port_state_to_neutral(t1);
  573. if (t1 != t2)
  574. xhci_writel(xhci, t2, addr);
  575. if (DEV_HIGHSPEED(t1)) {
  576. /* enable remote wake up for USB 2.0 */
  577. u32 __iomem *addr;
  578. u32 tmp;
  579. addr = &xhci->op_regs->port_power_base +
  580. NUM_PORT_REGS * (port & 0xff);
  581. tmp = xhci_readl(xhci, addr);
  582. tmp |= PORT_RWE;
  583. xhci_writel(xhci, tmp, addr);
  584. }
  585. }
  586. hcd->state = HC_STATE_SUSPENDED;
  587. xhci->next_statechange = jiffies + msecs_to_jiffies(10);
  588. spin_unlock_irqrestore(&xhci->lock, flags);
  589. return 0;
  590. }
  591. int xhci_bus_resume(struct usb_hcd *hcd)
  592. {
  593. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  594. int port;
  595. u32 temp;
  596. unsigned long flags;
  597. xhci_dbg(xhci, "resume root hub\n");
  598. if (time_before(jiffies, xhci->next_statechange))
  599. msleep(5);
  600. spin_lock_irqsave(&xhci->lock, flags);
  601. if (!HCD_HW_ACCESSIBLE(hcd)) {
  602. spin_unlock_irqrestore(&xhci->lock, flags);
  603. return -ESHUTDOWN;
  604. }
  605. /* delay the irqs */
  606. temp = xhci_readl(xhci, &xhci->op_regs->command);
  607. temp &= ~CMD_EIE;
  608. xhci_writel(xhci, temp, &xhci->op_regs->command);
  609. port = HCS_MAX_PORTS(xhci->hcs_params1);
  610. while (port--) {
  611. /* Check whether need resume ports. If needed
  612. resume port and disable remote wakeup */
  613. u32 __iomem *addr;
  614. u32 temp;
  615. int slot_id;
  616. addr = &xhci->op_regs->port_status_base +
  617. NUM_PORT_REGS * (port & 0xff);
  618. temp = xhci_readl(xhci, addr);
  619. if (DEV_SUPERSPEED(temp))
  620. temp &= ~(PORT_RWC_BITS | PORT_CEC | PORT_WAKE_BITS);
  621. else
  622. temp &= ~(PORT_RWC_BITS | PORT_WAKE_BITS);
  623. if (test_bit(port, &xhci->bus_suspended) &&
  624. (temp & PORT_PLS_MASK)) {
  625. if (DEV_SUPERSPEED(temp)) {
  626. temp = xhci_port_state_to_neutral(temp);
  627. temp &= ~PORT_PLS_MASK;
  628. temp |= PORT_LINK_STROBE | XDEV_U0;
  629. xhci_writel(xhci, temp, addr);
  630. } else {
  631. temp = xhci_port_state_to_neutral(temp);
  632. temp &= ~PORT_PLS_MASK;
  633. temp |= PORT_LINK_STROBE | XDEV_RESUME;
  634. xhci_writel(xhci, temp, addr);
  635. spin_unlock_irqrestore(&xhci->lock, flags);
  636. msleep(20);
  637. spin_lock_irqsave(&xhci->lock, flags);
  638. temp = xhci_readl(xhci, addr);
  639. temp = xhci_port_state_to_neutral(temp);
  640. temp &= ~PORT_PLS_MASK;
  641. temp |= PORT_LINK_STROBE | XDEV_U0;
  642. xhci_writel(xhci, temp, addr);
  643. }
  644. slot_id = xhci_find_slot_id_by_port(xhci, port + 1);
  645. if (slot_id)
  646. xhci_ring_device(xhci, slot_id);
  647. } else
  648. xhci_writel(xhci, temp, addr);
  649. if (DEV_HIGHSPEED(temp)) {
  650. /* disable remote wake up for USB 2.0 */
  651. u32 __iomem *addr;
  652. u32 tmp;
  653. addr = &xhci->op_regs->port_power_base +
  654. NUM_PORT_REGS * (port & 0xff);
  655. tmp = xhci_readl(xhci, addr);
  656. tmp &= ~PORT_RWE;
  657. xhci_writel(xhci, tmp, addr);
  658. }
  659. }
  660. (void) xhci_readl(xhci, &xhci->op_regs->command);
  661. xhci->next_statechange = jiffies + msecs_to_jiffies(5);
  662. hcd->state = HC_STATE_RUNNING;
  663. /* re-enable irqs */
  664. temp = xhci_readl(xhci, &xhci->op_regs->command);
  665. temp |= CMD_EIE;
  666. xhci_writel(xhci, temp, &xhci->op_regs->command);
  667. temp = xhci_readl(xhci, &xhci->op_regs->command);
  668. spin_unlock_irqrestore(&xhci->lock, flags);
  669. return 0;
  670. }
  671. #endif /* CONFIG_PM */