xhci-hub.c 21 KB

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