xhci-hub.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252
  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 <linux/slab.h>
  23. #include <linux/device.h>
  24. #include <asm/unaligned.h>
  25. #include "xhci.h"
  26. #include "xhci-trace.h"
  27. #define PORT_WAKE_BITS (PORT_WKOC_E | PORT_WKDISC_E | PORT_WKCONN_E)
  28. #define PORT_RWC_BITS (PORT_CSC | PORT_PEC | PORT_WRC | PORT_OCC | \
  29. PORT_RC | PORT_PLC | PORT_PE)
  30. /* USB 3.0 BOS descriptor and a capability descriptor, combined */
  31. static u8 usb_bos_descriptor [] = {
  32. USB_DT_BOS_SIZE, /* __u8 bLength, 5 bytes */
  33. USB_DT_BOS, /* __u8 bDescriptorType */
  34. 0x0F, 0x00, /* __le16 wTotalLength, 15 bytes */
  35. 0x1, /* __u8 bNumDeviceCaps */
  36. /* First device capability */
  37. USB_DT_USB_SS_CAP_SIZE, /* __u8 bLength, 10 bytes */
  38. USB_DT_DEVICE_CAPABILITY, /* Device Capability */
  39. USB_SS_CAP_TYPE, /* bDevCapabilityType, SUPERSPEED_USB */
  40. 0x00, /* bmAttributes, LTM off by default */
  41. USB_5GBPS_OPERATION, 0x00, /* wSpeedsSupported, 5Gbps only */
  42. 0x03, /* bFunctionalitySupport,
  43. USB 3.0 speed only */
  44. 0x00, /* bU1DevExitLat, set later. */
  45. 0x00, 0x00 /* __le16 bU2DevExitLat, set later. */
  46. };
  47. static void xhci_common_hub_descriptor(struct xhci_hcd *xhci,
  48. struct usb_hub_descriptor *desc, int ports)
  49. {
  50. u16 temp;
  51. desc->bPwrOn2PwrGood = 10; /* xhci section 5.4.9 says 20ms max */
  52. desc->bHubContrCurrent = 0;
  53. desc->bNbrPorts = ports;
  54. temp = 0;
  55. /* Bits 1:0 - support per-port power switching, or power always on */
  56. if (HCC_PPC(xhci->hcc_params))
  57. temp |= HUB_CHAR_INDV_PORT_LPSM;
  58. else
  59. temp |= HUB_CHAR_NO_LPSM;
  60. /* Bit 2 - root hubs are not part of a compound device */
  61. /* Bits 4:3 - individual port over current protection */
  62. temp |= HUB_CHAR_INDV_PORT_OCPM;
  63. /* Bits 6:5 - no TTs in root ports */
  64. /* Bit 7 - no port indicators */
  65. desc->wHubCharacteristics = cpu_to_le16(temp);
  66. }
  67. /* Fill in the USB 2.0 roothub descriptor */
  68. static void xhci_usb2_hub_descriptor(struct usb_hcd *hcd, struct xhci_hcd *xhci,
  69. struct usb_hub_descriptor *desc)
  70. {
  71. int ports;
  72. u16 temp;
  73. __u8 port_removable[(USB_MAXCHILDREN + 1 + 7) / 8];
  74. u32 portsc;
  75. unsigned int i;
  76. ports = xhci->num_usb2_ports;
  77. xhci_common_hub_descriptor(xhci, desc, ports);
  78. desc->bDescriptorType = USB_DT_HUB;
  79. temp = 1 + (ports / 8);
  80. desc->bDescLength = USB_DT_HUB_NONVAR_SIZE + 2 * temp;
  81. /* The Device Removable bits are reported on a byte granularity.
  82. * If the port doesn't exist within that byte, the bit is set to 0.
  83. */
  84. memset(port_removable, 0, sizeof(port_removable));
  85. for (i = 0; i < ports; i++) {
  86. portsc = readl(xhci->usb2_ports[i]);
  87. /* If a device is removable, PORTSC reports a 0, same as in the
  88. * hub descriptor DeviceRemovable bits.
  89. */
  90. if (portsc & PORT_DEV_REMOVE)
  91. /* This math is hairy because bit 0 of DeviceRemovable
  92. * is reserved, and bit 1 is for port 1, etc.
  93. */
  94. port_removable[(i + 1) / 8] |= 1 << ((i + 1) % 8);
  95. }
  96. /* ch11.h defines a hub descriptor that has room for USB_MAXCHILDREN
  97. * ports on it. The USB 2.0 specification says that there are two
  98. * variable length fields at the end of the hub descriptor:
  99. * DeviceRemovable and PortPwrCtrlMask. But since we can have less than
  100. * USB_MAXCHILDREN ports, we may need to use the DeviceRemovable array
  101. * to set PortPwrCtrlMask bits. PortPwrCtrlMask must always be set to
  102. * 0xFF, so we initialize the both arrays (DeviceRemovable and
  103. * PortPwrCtrlMask) to 0xFF. Then we set the DeviceRemovable for each
  104. * set of ports that actually exist.
  105. */
  106. memset(desc->u.hs.DeviceRemovable, 0xff,
  107. sizeof(desc->u.hs.DeviceRemovable));
  108. memset(desc->u.hs.PortPwrCtrlMask, 0xff,
  109. sizeof(desc->u.hs.PortPwrCtrlMask));
  110. for (i = 0; i < (ports + 1 + 7) / 8; i++)
  111. memset(&desc->u.hs.DeviceRemovable[i], port_removable[i],
  112. sizeof(__u8));
  113. }
  114. /* Fill in the USB 3.0 roothub descriptor */
  115. static void xhci_usb3_hub_descriptor(struct usb_hcd *hcd, struct xhci_hcd *xhci,
  116. struct usb_hub_descriptor *desc)
  117. {
  118. int ports;
  119. u16 port_removable;
  120. u32 portsc;
  121. unsigned int i;
  122. ports = xhci->num_usb3_ports;
  123. xhci_common_hub_descriptor(xhci, desc, ports);
  124. desc->bDescriptorType = USB_DT_SS_HUB;
  125. desc->bDescLength = USB_DT_SS_HUB_SIZE;
  126. /* header decode latency should be zero for roothubs,
  127. * see section 4.23.5.2.
  128. */
  129. desc->u.ss.bHubHdrDecLat = 0;
  130. desc->u.ss.wHubDelay = 0;
  131. port_removable = 0;
  132. /* bit 0 is reserved, bit 1 is for port 1, etc. */
  133. for (i = 0; i < ports; i++) {
  134. portsc = readl(xhci->usb3_ports[i]);
  135. if (portsc & PORT_DEV_REMOVE)
  136. port_removable |= 1 << (i + 1);
  137. }
  138. desc->u.ss.DeviceRemovable = cpu_to_le16(port_removable);
  139. }
  140. static void xhci_hub_descriptor(struct usb_hcd *hcd, struct xhci_hcd *xhci,
  141. struct usb_hub_descriptor *desc)
  142. {
  143. if (hcd->speed == HCD_USB3)
  144. xhci_usb3_hub_descriptor(hcd, xhci, desc);
  145. else
  146. xhci_usb2_hub_descriptor(hcd, xhci, desc);
  147. }
  148. static unsigned int xhci_port_speed(unsigned int port_status)
  149. {
  150. if (DEV_LOWSPEED(port_status))
  151. return USB_PORT_STAT_LOW_SPEED;
  152. if (DEV_HIGHSPEED(port_status))
  153. return USB_PORT_STAT_HIGH_SPEED;
  154. /*
  155. * FIXME: Yes, we should check for full speed, but the core uses that as
  156. * a default in portspeed() in usb/core/hub.c (which is the only place
  157. * USB_PORT_STAT_*_SPEED is used).
  158. */
  159. return 0;
  160. }
  161. /*
  162. * These bits are Read Only (RO) and should be saved and written to the
  163. * registers: 0, 3, 10:13, 30
  164. * connect status, over-current status, port speed, and device removable.
  165. * connect status and port speed are also sticky - meaning they're in
  166. * the AUX well and they aren't changed by a hot, warm, or cold reset.
  167. */
  168. #define XHCI_PORT_RO ((1<<0) | (1<<3) | (0xf<<10) | (1<<30))
  169. /*
  170. * These bits are RW; writing a 0 clears the bit, writing a 1 sets the bit:
  171. * bits 5:8, 9, 14:15, 25:27
  172. * link state, port power, port indicator state, "wake on" enable state
  173. */
  174. #define XHCI_PORT_RWS ((0xf<<5) | (1<<9) | (0x3<<14) | (0x7<<25))
  175. /*
  176. * These bits are RW; writing a 1 sets the bit, writing a 0 has no effect:
  177. * bit 4 (port reset)
  178. */
  179. #define XHCI_PORT_RW1S ((1<<4))
  180. /*
  181. * These bits are RW; writing a 1 clears the bit, writing a 0 has no effect:
  182. * bits 1, 17, 18, 19, 20, 21, 22, 23
  183. * port enable/disable, and
  184. * change bits: connect, PED, warm port reset changed (reserved zero for USB 2.0 ports),
  185. * over-current, reset, link state, and L1 change
  186. */
  187. #define XHCI_PORT_RW1CS ((1<<1) | (0x7f<<17))
  188. /*
  189. * Bit 16 is RW, and writing a '1' to it causes the link state control to be
  190. * latched in
  191. */
  192. #define XHCI_PORT_RW ((1<<16))
  193. /*
  194. * These bits are Reserved Zero (RsvdZ) and zero should be written to them:
  195. * bits 2, 24, 28:31
  196. */
  197. #define XHCI_PORT_RZ ((1<<2) | (1<<24) | (0xf<<28))
  198. /*
  199. * Given a port state, this function returns a value that would result in the
  200. * port being in the same state, if the value was written to the port status
  201. * control register.
  202. * Save Read Only (RO) bits and save read/write bits where
  203. * writing a 0 clears the bit and writing a 1 sets the bit (RWS).
  204. * For all other types (RW1S, RW1CS, RW, and RZ), writing a '0' has no effect.
  205. */
  206. u32 xhci_port_state_to_neutral(u32 state)
  207. {
  208. /* Save read-only status and port state */
  209. return (state & XHCI_PORT_RO) | (state & XHCI_PORT_RWS);
  210. }
  211. /*
  212. * find slot id based on port number.
  213. * @port: The one-based port number from one of the two split roothubs.
  214. */
  215. int xhci_find_slot_id_by_port(struct usb_hcd *hcd, struct xhci_hcd *xhci,
  216. u16 port)
  217. {
  218. int slot_id;
  219. int i;
  220. enum usb_device_speed speed;
  221. slot_id = 0;
  222. for (i = 0; i < MAX_HC_SLOTS; i++) {
  223. if (!xhci->devs[i])
  224. continue;
  225. speed = xhci->devs[i]->udev->speed;
  226. if (((speed == USB_SPEED_SUPER) == (hcd->speed == HCD_USB3))
  227. && xhci->devs[i]->fake_port == port) {
  228. slot_id = i;
  229. break;
  230. }
  231. }
  232. return slot_id;
  233. }
  234. /*
  235. * Stop device
  236. * It issues stop endpoint command for EP 0 to 30. And wait the last command
  237. * to complete.
  238. * suspend will set to 1, if suspend bit need to set in command.
  239. */
  240. static int xhci_stop_device(struct xhci_hcd *xhci, int slot_id, int suspend)
  241. {
  242. struct xhci_virt_device *virt_dev;
  243. struct xhci_command *cmd;
  244. unsigned long flags;
  245. int ret;
  246. int i;
  247. ret = 0;
  248. virt_dev = xhci->devs[slot_id];
  249. cmd = xhci_alloc_command(xhci, false, true, GFP_NOIO);
  250. if (!cmd) {
  251. xhci_dbg(xhci, "Couldn't allocate command structure.\n");
  252. return -ENOMEM;
  253. }
  254. spin_lock_irqsave(&xhci->lock, flags);
  255. for (i = LAST_EP_INDEX; i > 0; i--) {
  256. if (virt_dev->eps[i].ring && virt_dev->eps[i].ring->dequeue) {
  257. struct xhci_command *command;
  258. command = xhci_alloc_command(xhci, false, false,
  259. GFP_NOWAIT);
  260. if (!command) {
  261. spin_unlock_irqrestore(&xhci->lock, flags);
  262. xhci_free_command(xhci, cmd);
  263. return -ENOMEM;
  264. }
  265. xhci_queue_stop_endpoint(xhci, command, slot_id, i,
  266. suspend);
  267. }
  268. }
  269. xhci_queue_stop_endpoint(xhci, cmd, slot_id, 0, suspend);
  270. xhci_ring_cmd_db(xhci);
  271. spin_unlock_irqrestore(&xhci->lock, flags);
  272. /* Wait for last stop endpoint command to finish */
  273. wait_for_completion(cmd->completion);
  274. if (cmd->status == COMP_CMD_ABORT || cmd->status == COMP_CMD_STOP) {
  275. xhci_warn(xhci, "Timeout while waiting for stop endpoint command\n");
  276. ret = -ETIME;
  277. }
  278. xhci_free_command(xhci, cmd);
  279. return ret;
  280. }
  281. /*
  282. * Ring device, it rings the all doorbells unconditionally.
  283. */
  284. void xhci_ring_device(struct xhci_hcd *xhci, int slot_id)
  285. {
  286. int i;
  287. for (i = 0; i < LAST_EP_INDEX + 1; i++)
  288. if (xhci->devs[slot_id]->eps[i].ring &&
  289. xhci->devs[slot_id]->eps[i].ring->dequeue)
  290. xhci_ring_ep_doorbell(xhci, slot_id, i, 0);
  291. return;
  292. }
  293. static void xhci_disable_port(struct usb_hcd *hcd, struct xhci_hcd *xhci,
  294. u16 wIndex, __le32 __iomem *addr, u32 port_status)
  295. {
  296. /* Don't allow the USB core to disable SuperSpeed ports. */
  297. if (hcd->speed == HCD_USB3) {
  298. xhci_dbg(xhci, "Ignoring request to disable "
  299. "SuperSpeed port.\n");
  300. return;
  301. }
  302. /* Write 1 to disable the port */
  303. writel(port_status | PORT_PE, addr);
  304. port_status = readl(addr);
  305. xhci_dbg(xhci, "disable port, actual port %d status = 0x%x\n",
  306. wIndex, port_status);
  307. }
  308. static void xhci_clear_port_change_bit(struct xhci_hcd *xhci, u16 wValue,
  309. u16 wIndex, __le32 __iomem *addr, u32 port_status)
  310. {
  311. char *port_change_bit;
  312. u32 status;
  313. switch (wValue) {
  314. case USB_PORT_FEAT_C_RESET:
  315. status = PORT_RC;
  316. port_change_bit = "reset";
  317. break;
  318. case USB_PORT_FEAT_C_BH_PORT_RESET:
  319. status = PORT_WRC;
  320. port_change_bit = "warm(BH) reset";
  321. break;
  322. case USB_PORT_FEAT_C_CONNECTION:
  323. status = PORT_CSC;
  324. port_change_bit = "connect";
  325. break;
  326. case USB_PORT_FEAT_C_OVER_CURRENT:
  327. status = PORT_OCC;
  328. port_change_bit = "over-current";
  329. break;
  330. case USB_PORT_FEAT_C_ENABLE:
  331. status = PORT_PEC;
  332. port_change_bit = "enable/disable";
  333. break;
  334. case USB_PORT_FEAT_C_SUSPEND:
  335. status = PORT_PLC;
  336. port_change_bit = "suspend/resume";
  337. break;
  338. case USB_PORT_FEAT_C_PORT_LINK_STATE:
  339. status = PORT_PLC;
  340. port_change_bit = "link state";
  341. break;
  342. default:
  343. /* Should never happen */
  344. return;
  345. }
  346. /* Change bits are all write 1 to clear */
  347. writel(port_status | status, addr);
  348. port_status = readl(addr);
  349. xhci_dbg(xhci, "clear port %s change, actual port %d status = 0x%x\n",
  350. port_change_bit, wIndex, port_status);
  351. }
  352. static int xhci_get_ports(struct usb_hcd *hcd, __le32 __iomem ***port_array)
  353. {
  354. int max_ports;
  355. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  356. if (hcd->speed == HCD_USB3) {
  357. max_ports = xhci->num_usb3_ports;
  358. *port_array = xhci->usb3_ports;
  359. } else {
  360. max_ports = xhci->num_usb2_ports;
  361. *port_array = xhci->usb2_ports;
  362. }
  363. return max_ports;
  364. }
  365. void xhci_set_link_state(struct xhci_hcd *xhci, __le32 __iomem **port_array,
  366. int port_id, u32 link_state)
  367. {
  368. u32 temp;
  369. temp = readl(port_array[port_id]);
  370. temp = xhci_port_state_to_neutral(temp);
  371. temp &= ~PORT_PLS_MASK;
  372. temp |= PORT_LINK_STROBE | link_state;
  373. writel(temp, port_array[port_id]);
  374. }
  375. static void xhci_set_remote_wake_mask(struct xhci_hcd *xhci,
  376. __le32 __iomem **port_array, int port_id, u16 wake_mask)
  377. {
  378. u32 temp;
  379. temp = readl(port_array[port_id]);
  380. temp = xhci_port_state_to_neutral(temp);
  381. if (wake_mask & USB_PORT_FEAT_REMOTE_WAKE_CONNECT)
  382. temp |= PORT_WKCONN_E;
  383. else
  384. temp &= ~PORT_WKCONN_E;
  385. if (wake_mask & USB_PORT_FEAT_REMOTE_WAKE_DISCONNECT)
  386. temp |= PORT_WKDISC_E;
  387. else
  388. temp &= ~PORT_WKDISC_E;
  389. if (wake_mask & USB_PORT_FEAT_REMOTE_WAKE_OVER_CURRENT)
  390. temp |= PORT_WKOC_E;
  391. else
  392. temp &= ~PORT_WKOC_E;
  393. writel(temp, port_array[port_id]);
  394. }
  395. /* Test and clear port RWC bit */
  396. void xhci_test_and_clear_bit(struct xhci_hcd *xhci, __le32 __iomem **port_array,
  397. int port_id, u32 port_bit)
  398. {
  399. u32 temp;
  400. temp = readl(port_array[port_id]);
  401. if (temp & port_bit) {
  402. temp = xhci_port_state_to_neutral(temp);
  403. temp |= port_bit;
  404. writel(temp, port_array[port_id]);
  405. }
  406. }
  407. /* Updates Link Status for USB 2.1 port */
  408. static void xhci_hub_report_usb2_link_state(u32 *status, u32 status_reg)
  409. {
  410. if ((status_reg & PORT_PLS_MASK) == XDEV_U2)
  411. *status |= USB_PORT_STAT_L1;
  412. }
  413. /* Updates Link Status for super Speed port */
  414. static void xhci_hub_report_usb3_link_state(u32 *status, u32 status_reg)
  415. {
  416. u32 pls = status_reg & PORT_PLS_MASK;
  417. /* resume state is a xHCI internal state.
  418. * Do not report it to usb core.
  419. */
  420. if (pls == XDEV_RESUME)
  421. return;
  422. /* When the CAS bit is set then warm reset
  423. * should be performed on port
  424. */
  425. if (status_reg & PORT_CAS) {
  426. /* The CAS bit can be set while the port is
  427. * in any link state.
  428. * Only roothubs have CAS bit, so we
  429. * pretend to be in compliance mode
  430. * unless we're already in compliance
  431. * or the inactive state.
  432. */
  433. if (pls != USB_SS_PORT_LS_COMP_MOD &&
  434. pls != USB_SS_PORT_LS_SS_INACTIVE) {
  435. pls = USB_SS_PORT_LS_COMP_MOD;
  436. }
  437. /* Return also connection bit -
  438. * hub state machine resets port
  439. * when this bit is set.
  440. */
  441. pls |= USB_PORT_STAT_CONNECTION;
  442. } else {
  443. /*
  444. * If CAS bit isn't set but the Port is already at
  445. * Compliance Mode, fake a connection so the USB core
  446. * notices the Compliance state and resets the port.
  447. * This resolves an issue generated by the SN65LVPE502CP
  448. * in which sometimes the port enters compliance mode
  449. * caused by a delay on the host-device negotiation.
  450. */
  451. if (pls == USB_SS_PORT_LS_COMP_MOD)
  452. pls |= USB_PORT_STAT_CONNECTION;
  453. }
  454. /* update status field */
  455. *status |= pls;
  456. }
  457. /*
  458. * Function for Compliance Mode Quirk.
  459. *
  460. * This Function verifies if all xhc USB3 ports have entered U0, if so,
  461. * the compliance mode timer is deleted. A port won't enter
  462. * compliance mode if it has previously entered U0.
  463. */
  464. static void xhci_del_comp_mod_timer(struct xhci_hcd *xhci, u32 status,
  465. u16 wIndex)
  466. {
  467. u32 all_ports_seen_u0 = ((1 << xhci->num_usb3_ports)-1);
  468. bool port_in_u0 = ((status & PORT_PLS_MASK) == XDEV_U0);
  469. if (!(xhci->quirks & XHCI_COMP_MODE_QUIRK))
  470. return;
  471. if ((xhci->port_status_u0 != all_ports_seen_u0) && port_in_u0) {
  472. xhci->port_status_u0 |= 1 << wIndex;
  473. if (xhci->port_status_u0 == all_ports_seen_u0) {
  474. del_timer_sync(&xhci->comp_mode_recovery_timer);
  475. xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
  476. "All USB3 ports have entered U0 already!");
  477. xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
  478. "Compliance Mode Recovery Timer Deleted.");
  479. }
  480. }
  481. }
  482. /*
  483. * Converts a raw xHCI port status into the format that external USB 2.0 or USB
  484. * 3.0 hubs use.
  485. *
  486. * Possible side effects:
  487. * - Mark a port as being done with device resume,
  488. * and ring the endpoint doorbells.
  489. * - Stop the Synopsys redriver Compliance Mode polling.
  490. * - Drop and reacquire the xHCI lock, in order to wait for port resume.
  491. */
  492. static u32 xhci_get_port_status(struct usb_hcd *hcd,
  493. struct xhci_bus_state *bus_state,
  494. __le32 __iomem **port_array,
  495. u16 wIndex, u32 raw_port_status,
  496. unsigned long flags)
  497. __releases(&xhci->lock)
  498. __acquires(&xhci->lock)
  499. {
  500. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  501. u32 status = 0;
  502. int slot_id;
  503. /* wPortChange bits */
  504. if (raw_port_status & PORT_CSC)
  505. status |= USB_PORT_STAT_C_CONNECTION << 16;
  506. if (raw_port_status & PORT_PEC)
  507. status |= USB_PORT_STAT_C_ENABLE << 16;
  508. if ((raw_port_status & PORT_OCC))
  509. status |= USB_PORT_STAT_C_OVERCURRENT << 16;
  510. if ((raw_port_status & PORT_RC))
  511. status |= USB_PORT_STAT_C_RESET << 16;
  512. /* USB3.0 only */
  513. if (hcd->speed == HCD_USB3) {
  514. if ((raw_port_status & PORT_PLC))
  515. status |= USB_PORT_STAT_C_LINK_STATE << 16;
  516. if ((raw_port_status & PORT_WRC))
  517. status |= USB_PORT_STAT_C_BH_RESET << 16;
  518. }
  519. if (hcd->speed != HCD_USB3) {
  520. if ((raw_port_status & PORT_PLS_MASK) == XDEV_U3
  521. && (raw_port_status & PORT_POWER))
  522. status |= USB_PORT_STAT_SUSPEND;
  523. }
  524. if ((raw_port_status & PORT_PLS_MASK) == XDEV_RESUME &&
  525. !DEV_SUPERSPEED(raw_port_status)) {
  526. if ((raw_port_status & PORT_RESET) ||
  527. !(raw_port_status & PORT_PE))
  528. return 0xffffffff;
  529. if (time_after_eq(jiffies,
  530. bus_state->resume_done[wIndex])) {
  531. int time_left;
  532. xhci_dbg(xhci, "Resume USB2 port %d\n",
  533. wIndex + 1);
  534. bus_state->resume_done[wIndex] = 0;
  535. clear_bit(wIndex, &bus_state->resuming_ports);
  536. set_bit(wIndex, &bus_state->rexit_ports);
  537. xhci_set_link_state(xhci, port_array, wIndex,
  538. XDEV_U0);
  539. spin_unlock_irqrestore(&xhci->lock, flags);
  540. time_left = wait_for_completion_timeout(
  541. &bus_state->rexit_done[wIndex],
  542. msecs_to_jiffies(
  543. XHCI_MAX_REXIT_TIMEOUT));
  544. spin_lock_irqsave(&xhci->lock, flags);
  545. if (time_left) {
  546. slot_id = xhci_find_slot_id_by_port(hcd,
  547. xhci, wIndex + 1);
  548. if (!slot_id) {
  549. xhci_dbg(xhci, "slot_id is zero\n");
  550. return 0xffffffff;
  551. }
  552. xhci_ring_device(xhci, slot_id);
  553. } else {
  554. int port_status = readl(port_array[wIndex]);
  555. xhci_warn(xhci, "Port resume took longer than %i msec, port status = 0x%x\n",
  556. XHCI_MAX_REXIT_TIMEOUT,
  557. port_status);
  558. status |= USB_PORT_STAT_SUSPEND;
  559. clear_bit(wIndex, &bus_state->rexit_ports);
  560. }
  561. bus_state->port_c_suspend |= 1 << wIndex;
  562. bus_state->suspended_ports &= ~(1 << wIndex);
  563. } else {
  564. /*
  565. * The resume has been signaling for less than
  566. * 20ms. Report the port status as SUSPEND,
  567. * let the usbcore check port status again
  568. * and clear resume signaling later.
  569. */
  570. status |= USB_PORT_STAT_SUSPEND;
  571. }
  572. }
  573. if ((raw_port_status & PORT_PLS_MASK) == XDEV_U0
  574. && (raw_port_status & PORT_POWER)
  575. && (bus_state->suspended_ports & (1 << wIndex))) {
  576. bus_state->suspended_ports &= ~(1 << wIndex);
  577. if (hcd->speed != HCD_USB3)
  578. bus_state->port_c_suspend |= 1 << wIndex;
  579. }
  580. if (raw_port_status & PORT_CONNECT) {
  581. status |= USB_PORT_STAT_CONNECTION;
  582. status |= xhci_port_speed(raw_port_status);
  583. }
  584. if (raw_port_status & PORT_PE)
  585. status |= USB_PORT_STAT_ENABLE;
  586. if (raw_port_status & PORT_OC)
  587. status |= USB_PORT_STAT_OVERCURRENT;
  588. if (raw_port_status & PORT_RESET)
  589. status |= USB_PORT_STAT_RESET;
  590. if (raw_port_status & PORT_POWER) {
  591. if (hcd->speed == HCD_USB3)
  592. status |= USB_SS_PORT_STAT_POWER;
  593. else
  594. status |= USB_PORT_STAT_POWER;
  595. }
  596. /* Update Port Link State */
  597. if (hcd->speed == HCD_USB3) {
  598. xhci_hub_report_usb3_link_state(&status, raw_port_status);
  599. /*
  600. * Verify if all USB3 Ports Have entered U0 already.
  601. * Delete Compliance Mode Timer if so.
  602. */
  603. xhci_del_comp_mod_timer(xhci, raw_port_status, wIndex);
  604. } else {
  605. xhci_hub_report_usb2_link_state(&status, raw_port_status);
  606. }
  607. if (bus_state->port_c_suspend & (1 << wIndex))
  608. status |= 1 << USB_PORT_FEAT_C_SUSPEND;
  609. return status;
  610. }
  611. int xhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
  612. u16 wIndex, char *buf, u16 wLength)
  613. {
  614. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  615. int max_ports;
  616. unsigned long flags;
  617. u32 temp, status;
  618. int retval = 0;
  619. __le32 __iomem **port_array;
  620. int slot_id;
  621. struct xhci_bus_state *bus_state;
  622. u16 link_state = 0;
  623. u16 wake_mask = 0;
  624. u16 timeout = 0;
  625. max_ports = xhci_get_ports(hcd, &port_array);
  626. bus_state = &xhci->bus_state[hcd_index(hcd)];
  627. spin_lock_irqsave(&xhci->lock, flags);
  628. switch (typeReq) {
  629. case GetHubStatus:
  630. /* No power source, over-current reported per port */
  631. memset(buf, 0, 4);
  632. break;
  633. case GetHubDescriptor:
  634. /* Check to make sure userspace is asking for the USB 3.0 hub
  635. * descriptor for the USB 3.0 roothub. If not, we stall the
  636. * endpoint, like external hubs do.
  637. */
  638. if (hcd->speed == HCD_USB3 &&
  639. (wLength < USB_DT_SS_HUB_SIZE ||
  640. wValue != (USB_DT_SS_HUB << 8))) {
  641. xhci_dbg(xhci, "Wrong hub descriptor type for "
  642. "USB 3.0 roothub.\n");
  643. goto error;
  644. }
  645. xhci_hub_descriptor(hcd, xhci,
  646. (struct usb_hub_descriptor *) buf);
  647. break;
  648. case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
  649. if ((wValue & 0xff00) != (USB_DT_BOS << 8))
  650. goto error;
  651. if (hcd->speed != HCD_USB3)
  652. goto error;
  653. /* Set the U1 and U2 exit latencies. */
  654. memcpy(buf, &usb_bos_descriptor,
  655. USB_DT_BOS_SIZE + USB_DT_USB_SS_CAP_SIZE);
  656. if ((xhci->quirks & XHCI_LPM_SUPPORT)) {
  657. temp = readl(&xhci->cap_regs->hcs_params3);
  658. buf[12] = HCS_U1_LATENCY(temp);
  659. put_unaligned_le16(HCS_U2_LATENCY(temp), &buf[13]);
  660. }
  661. /* Indicate whether the host has LTM support. */
  662. temp = readl(&xhci->cap_regs->hcc_params);
  663. if (HCC_LTC(temp))
  664. buf[8] |= USB_LTM_SUPPORT;
  665. spin_unlock_irqrestore(&xhci->lock, flags);
  666. return USB_DT_BOS_SIZE + USB_DT_USB_SS_CAP_SIZE;
  667. case GetPortStatus:
  668. if (!wIndex || wIndex > max_ports)
  669. goto error;
  670. wIndex--;
  671. temp = readl(port_array[wIndex]);
  672. if (temp == 0xffffffff) {
  673. retval = -ENODEV;
  674. break;
  675. }
  676. status = xhci_get_port_status(hcd, bus_state, port_array,
  677. wIndex, temp, flags);
  678. if (status == 0xffffffff)
  679. goto error;
  680. xhci_dbg(xhci, "get port status, actual port %d status = 0x%x\n",
  681. wIndex, temp);
  682. xhci_dbg(xhci, "Get port status returned 0x%x\n", status);
  683. put_unaligned(cpu_to_le32(status), (__le32 *) buf);
  684. break;
  685. case SetPortFeature:
  686. if (wValue == USB_PORT_FEAT_LINK_STATE)
  687. link_state = (wIndex & 0xff00) >> 3;
  688. if (wValue == USB_PORT_FEAT_REMOTE_WAKE_MASK)
  689. wake_mask = wIndex & 0xff00;
  690. /* The MSB of wIndex is the U1/U2 timeout */
  691. timeout = (wIndex & 0xff00) >> 8;
  692. wIndex &= 0xff;
  693. if (!wIndex || wIndex > max_ports)
  694. goto error;
  695. wIndex--;
  696. temp = readl(port_array[wIndex]);
  697. if (temp == 0xffffffff) {
  698. retval = -ENODEV;
  699. break;
  700. }
  701. temp = xhci_port_state_to_neutral(temp);
  702. /* FIXME: What new port features do we need to support? */
  703. switch (wValue) {
  704. case USB_PORT_FEAT_SUSPEND:
  705. temp = readl(port_array[wIndex]);
  706. if ((temp & PORT_PLS_MASK) != XDEV_U0) {
  707. /* Resume the port to U0 first */
  708. xhci_set_link_state(xhci, port_array, wIndex,
  709. XDEV_U0);
  710. spin_unlock_irqrestore(&xhci->lock, flags);
  711. msleep(10);
  712. spin_lock_irqsave(&xhci->lock, flags);
  713. }
  714. /* In spec software should not attempt to suspend
  715. * a port unless the port reports that it is in the
  716. * enabled (PED = ‘1’,PLS < ‘3’) state.
  717. */
  718. temp = readl(port_array[wIndex]);
  719. if ((temp & PORT_PE) == 0 || (temp & PORT_RESET)
  720. || (temp & PORT_PLS_MASK) >= XDEV_U3) {
  721. xhci_warn(xhci, "USB core suspending device "
  722. "not in U0/U1/U2.\n");
  723. goto error;
  724. }
  725. slot_id = xhci_find_slot_id_by_port(hcd, xhci,
  726. wIndex + 1);
  727. if (!slot_id) {
  728. xhci_warn(xhci, "slot_id is zero\n");
  729. goto error;
  730. }
  731. /* unlock to execute stop endpoint commands */
  732. spin_unlock_irqrestore(&xhci->lock, flags);
  733. xhci_stop_device(xhci, slot_id, 1);
  734. spin_lock_irqsave(&xhci->lock, flags);
  735. xhci_set_link_state(xhci, port_array, wIndex, XDEV_U3);
  736. spin_unlock_irqrestore(&xhci->lock, flags);
  737. msleep(10); /* wait device to enter */
  738. spin_lock_irqsave(&xhci->lock, flags);
  739. temp = readl(port_array[wIndex]);
  740. bus_state->suspended_ports |= 1 << wIndex;
  741. break;
  742. case USB_PORT_FEAT_LINK_STATE:
  743. temp = readl(port_array[wIndex]);
  744. /* Disable port */
  745. if (link_state == USB_SS_PORT_LS_SS_DISABLED) {
  746. xhci_dbg(xhci, "Disable port %d\n", wIndex);
  747. temp = xhci_port_state_to_neutral(temp);
  748. /*
  749. * Clear all change bits, so that we get a new
  750. * connection event.
  751. */
  752. temp |= PORT_CSC | PORT_PEC | PORT_WRC |
  753. PORT_OCC | PORT_RC | PORT_PLC |
  754. PORT_CEC;
  755. writel(temp | PORT_PE, port_array[wIndex]);
  756. temp = readl(port_array[wIndex]);
  757. break;
  758. }
  759. /* Put link in RxDetect (enable port) */
  760. if (link_state == USB_SS_PORT_LS_RX_DETECT) {
  761. xhci_dbg(xhci, "Enable port %d\n", wIndex);
  762. xhci_set_link_state(xhci, port_array, wIndex,
  763. link_state);
  764. temp = readl(port_array[wIndex]);
  765. break;
  766. }
  767. /* Software should not attempt to set
  768. * port link state above '3' (U3) and the port
  769. * must be enabled.
  770. */
  771. if ((temp & PORT_PE) == 0 ||
  772. (link_state > USB_SS_PORT_LS_U3)) {
  773. xhci_warn(xhci, "Cannot set link state.\n");
  774. goto error;
  775. }
  776. if (link_state == USB_SS_PORT_LS_U3) {
  777. slot_id = xhci_find_slot_id_by_port(hcd, xhci,
  778. wIndex + 1);
  779. if (slot_id) {
  780. /* unlock to execute stop endpoint
  781. * commands */
  782. spin_unlock_irqrestore(&xhci->lock,
  783. flags);
  784. xhci_stop_device(xhci, slot_id, 1);
  785. spin_lock_irqsave(&xhci->lock, flags);
  786. }
  787. }
  788. xhci_set_link_state(xhci, port_array, wIndex,
  789. link_state);
  790. spin_unlock_irqrestore(&xhci->lock, flags);
  791. msleep(20); /* wait device to enter */
  792. spin_lock_irqsave(&xhci->lock, flags);
  793. temp = readl(port_array[wIndex]);
  794. if (link_state == USB_SS_PORT_LS_U3)
  795. bus_state->suspended_ports |= 1 << wIndex;
  796. break;
  797. case USB_PORT_FEAT_POWER:
  798. /*
  799. * Turn on ports, even if there isn't per-port switching.
  800. * HC will report connect events even before this is set.
  801. * However, khubd will ignore the roothub events until
  802. * the roothub is registered.
  803. */
  804. writel(temp | PORT_POWER, port_array[wIndex]);
  805. temp = readl(port_array[wIndex]);
  806. xhci_dbg(xhci, "set port power, actual port %d status = 0x%x\n", wIndex, temp);
  807. spin_unlock_irqrestore(&xhci->lock, flags);
  808. temp = usb_acpi_power_manageable(hcd->self.root_hub,
  809. wIndex);
  810. if (temp)
  811. usb_acpi_set_power_state(hcd->self.root_hub,
  812. wIndex, true);
  813. spin_lock_irqsave(&xhci->lock, flags);
  814. break;
  815. case USB_PORT_FEAT_RESET:
  816. temp = (temp | PORT_RESET);
  817. writel(temp, port_array[wIndex]);
  818. temp = readl(port_array[wIndex]);
  819. xhci_dbg(xhci, "set port reset, actual port %d status = 0x%x\n", wIndex, temp);
  820. break;
  821. case USB_PORT_FEAT_REMOTE_WAKE_MASK:
  822. xhci_set_remote_wake_mask(xhci, port_array,
  823. wIndex, wake_mask);
  824. temp = readl(port_array[wIndex]);
  825. xhci_dbg(xhci, "set port remote wake mask, "
  826. "actual port %d status = 0x%x\n",
  827. wIndex, temp);
  828. break;
  829. case USB_PORT_FEAT_BH_PORT_RESET:
  830. temp |= PORT_WR;
  831. writel(temp, port_array[wIndex]);
  832. temp = readl(port_array[wIndex]);
  833. break;
  834. case USB_PORT_FEAT_U1_TIMEOUT:
  835. if (hcd->speed != HCD_USB3)
  836. goto error;
  837. temp = readl(port_array[wIndex] + PORTPMSC);
  838. temp &= ~PORT_U1_TIMEOUT_MASK;
  839. temp |= PORT_U1_TIMEOUT(timeout);
  840. writel(temp, port_array[wIndex] + PORTPMSC);
  841. break;
  842. case USB_PORT_FEAT_U2_TIMEOUT:
  843. if (hcd->speed != HCD_USB3)
  844. goto error;
  845. temp = readl(port_array[wIndex] + PORTPMSC);
  846. temp &= ~PORT_U2_TIMEOUT_MASK;
  847. temp |= PORT_U2_TIMEOUT(timeout);
  848. writel(temp, port_array[wIndex] + PORTPMSC);
  849. break;
  850. default:
  851. goto error;
  852. }
  853. /* unblock any posted writes */
  854. temp = readl(port_array[wIndex]);
  855. break;
  856. case ClearPortFeature:
  857. if (!wIndex || wIndex > max_ports)
  858. goto error;
  859. wIndex--;
  860. temp = readl(port_array[wIndex]);
  861. if (temp == 0xffffffff) {
  862. retval = -ENODEV;
  863. break;
  864. }
  865. /* FIXME: What new port features do we need to support? */
  866. temp = xhci_port_state_to_neutral(temp);
  867. switch (wValue) {
  868. case USB_PORT_FEAT_SUSPEND:
  869. temp = readl(port_array[wIndex]);
  870. xhci_dbg(xhci, "clear USB_PORT_FEAT_SUSPEND\n");
  871. xhci_dbg(xhci, "PORTSC %04x\n", temp);
  872. if (temp & PORT_RESET)
  873. goto error;
  874. if ((temp & PORT_PLS_MASK) == XDEV_U3) {
  875. if ((temp & PORT_PE) == 0)
  876. goto error;
  877. xhci_set_link_state(xhci, port_array, wIndex,
  878. XDEV_RESUME);
  879. spin_unlock_irqrestore(&xhci->lock, flags);
  880. msleep(20);
  881. spin_lock_irqsave(&xhci->lock, flags);
  882. xhci_set_link_state(xhci, port_array, wIndex,
  883. XDEV_U0);
  884. }
  885. bus_state->port_c_suspend |= 1 << wIndex;
  886. slot_id = xhci_find_slot_id_by_port(hcd, xhci,
  887. wIndex + 1);
  888. if (!slot_id) {
  889. xhci_dbg(xhci, "slot_id is zero\n");
  890. goto error;
  891. }
  892. xhci_ring_device(xhci, slot_id);
  893. break;
  894. case USB_PORT_FEAT_C_SUSPEND:
  895. bus_state->port_c_suspend &= ~(1 << wIndex);
  896. case USB_PORT_FEAT_C_RESET:
  897. case USB_PORT_FEAT_C_BH_PORT_RESET:
  898. case USB_PORT_FEAT_C_CONNECTION:
  899. case USB_PORT_FEAT_C_OVER_CURRENT:
  900. case USB_PORT_FEAT_C_ENABLE:
  901. case USB_PORT_FEAT_C_PORT_LINK_STATE:
  902. xhci_clear_port_change_bit(xhci, wValue, wIndex,
  903. port_array[wIndex], temp);
  904. break;
  905. case USB_PORT_FEAT_ENABLE:
  906. xhci_disable_port(hcd, xhci, wIndex,
  907. port_array[wIndex], temp);
  908. break;
  909. case USB_PORT_FEAT_POWER:
  910. writel(temp & ~PORT_POWER, port_array[wIndex]);
  911. spin_unlock_irqrestore(&xhci->lock, flags);
  912. temp = usb_acpi_power_manageable(hcd->self.root_hub,
  913. wIndex);
  914. if (temp)
  915. usb_acpi_set_power_state(hcd->self.root_hub,
  916. wIndex, false);
  917. spin_lock_irqsave(&xhci->lock, flags);
  918. break;
  919. default:
  920. goto error;
  921. }
  922. break;
  923. default:
  924. error:
  925. /* "stall" on error */
  926. retval = -EPIPE;
  927. }
  928. spin_unlock_irqrestore(&xhci->lock, flags);
  929. return retval;
  930. }
  931. /*
  932. * Returns 0 if the status hasn't changed, or the number of bytes in buf.
  933. * Ports are 0-indexed from the HCD point of view,
  934. * and 1-indexed from the USB core pointer of view.
  935. *
  936. * Note that the status change bits will be cleared as soon as a port status
  937. * change event is generated, so we use the saved status from that event.
  938. */
  939. int xhci_hub_status_data(struct usb_hcd *hcd, char *buf)
  940. {
  941. unsigned long flags;
  942. u32 temp, status;
  943. u32 mask;
  944. int i, retval;
  945. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  946. int max_ports;
  947. __le32 __iomem **port_array;
  948. struct xhci_bus_state *bus_state;
  949. bool reset_change = false;
  950. max_ports = xhci_get_ports(hcd, &port_array);
  951. bus_state = &xhci->bus_state[hcd_index(hcd)];
  952. /* Initial status is no changes */
  953. retval = (max_ports + 8) / 8;
  954. memset(buf, 0, retval);
  955. /*
  956. * Inform the usbcore about resume-in-progress by returning
  957. * a non-zero value even if there are no status changes.
  958. */
  959. status = bus_state->resuming_ports;
  960. mask = PORT_CSC | PORT_PEC | PORT_OCC | PORT_PLC | PORT_WRC;
  961. spin_lock_irqsave(&xhci->lock, flags);
  962. /* For each port, did anything change? If so, set that bit in buf. */
  963. for (i = 0; i < max_ports; i++) {
  964. temp = readl(port_array[i]);
  965. if (temp == 0xffffffff) {
  966. retval = -ENODEV;
  967. break;
  968. }
  969. if ((temp & mask) != 0 ||
  970. (bus_state->port_c_suspend & 1 << i) ||
  971. (bus_state->resume_done[i] && time_after_eq(
  972. jiffies, bus_state->resume_done[i]))) {
  973. buf[(i + 1) / 8] |= 1 << (i + 1) % 8;
  974. status = 1;
  975. }
  976. if ((temp & PORT_RC))
  977. reset_change = true;
  978. }
  979. if (!status && !reset_change) {
  980. xhci_dbg(xhci, "%s: stopping port polling.\n", __func__);
  981. clear_bit(HCD_FLAG_POLL_RH, &hcd->flags);
  982. }
  983. spin_unlock_irqrestore(&xhci->lock, flags);
  984. return status ? retval : 0;
  985. }
  986. #ifdef CONFIG_PM
  987. int xhci_bus_suspend(struct usb_hcd *hcd)
  988. {
  989. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  990. int max_ports, port_index;
  991. __le32 __iomem **port_array;
  992. struct xhci_bus_state *bus_state;
  993. unsigned long flags;
  994. max_ports = xhci_get_ports(hcd, &port_array);
  995. bus_state = &xhci->bus_state[hcd_index(hcd)];
  996. spin_lock_irqsave(&xhci->lock, flags);
  997. if (hcd->self.root_hub->do_remote_wakeup) {
  998. if (bus_state->resuming_ports) {
  999. spin_unlock_irqrestore(&xhci->lock, flags);
  1000. xhci_dbg(xhci, "suspend failed because "
  1001. "a port is resuming\n");
  1002. return -EBUSY;
  1003. }
  1004. }
  1005. port_index = max_ports;
  1006. bus_state->bus_suspended = 0;
  1007. while (port_index--) {
  1008. /* suspend the port if the port is not suspended */
  1009. u32 t1, t2;
  1010. int slot_id;
  1011. t1 = readl(port_array[port_index]);
  1012. t2 = xhci_port_state_to_neutral(t1);
  1013. if ((t1 & PORT_PE) && !(t1 & PORT_PLS_MASK)) {
  1014. xhci_dbg(xhci, "port %d not suspended\n", port_index);
  1015. slot_id = xhci_find_slot_id_by_port(hcd, xhci,
  1016. port_index + 1);
  1017. if (slot_id) {
  1018. spin_unlock_irqrestore(&xhci->lock, flags);
  1019. xhci_stop_device(xhci, slot_id, 1);
  1020. spin_lock_irqsave(&xhci->lock, flags);
  1021. }
  1022. t2 &= ~PORT_PLS_MASK;
  1023. t2 |= PORT_LINK_STROBE | XDEV_U3;
  1024. set_bit(port_index, &bus_state->bus_suspended);
  1025. }
  1026. /* USB core sets remote wake mask for USB 3.0 hubs,
  1027. * including the USB 3.0 roothub, but only if CONFIG_PM_RUNTIME
  1028. * is enabled, so also enable remote wake here.
  1029. */
  1030. if (hcd->self.root_hub->do_remote_wakeup
  1031. && device_may_wakeup(hcd->self.controller)) {
  1032. if (t1 & PORT_CONNECT) {
  1033. t2 |= PORT_WKOC_E | PORT_WKDISC_E;
  1034. t2 &= ~PORT_WKCONN_E;
  1035. } else {
  1036. t2 |= PORT_WKOC_E | PORT_WKCONN_E;
  1037. t2 &= ~PORT_WKDISC_E;
  1038. }
  1039. } else
  1040. t2 &= ~PORT_WAKE_BITS;
  1041. t1 = xhci_port_state_to_neutral(t1);
  1042. if (t1 != t2)
  1043. writel(t2, port_array[port_index]);
  1044. }
  1045. hcd->state = HC_STATE_SUSPENDED;
  1046. bus_state->next_statechange = jiffies + msecs_to_jiffies(10);
  1047. spin_unlock_irqrestore(&xhci->lock, flags);
  1048. return 0;
  1049. }
  1050. int xhci_bus_resume(struct usb_hcd *hcd)
  1051. {
  1052. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  1053. int max_ports, port_index;
  1054. __le32 __iomem **port_array;
  1055. struct xhci_bus_state *bus_state;
  1056. u32 temp;
  1057. unsigned long flags;
  1058. max_ports = xhci_get_ports(hcd, &port_array);
  1059. bus_state = &xhci->bus_state[hcd_index(hcd)];
  1060. if (time_before(jiffies, bus_state->next_statechange))
  1061. msleep(5);
  1062. spin_lock_irqsave(&xhci->lock, flags);
  1063. if (!HCD_HW_ACCESSIBLE(hcd)) {
  1064. spin_unlock_irqrestore(&xhci->lock, flags);
  1065. return -ESHUTDOWN;
  1066. }
  1067. /* delay the irqs */
  1068. temp = readl(&xhci->op_regs->command);
  1069. temp &= ~CMD_EIE;
  1070. writel(temp, &xhci->op_regs->command);
  1071. port_index = max_ports;
  1072. while (port_index--) {
  1073. /* Check whether need resume ports. If needed
  1074. resume port and disable remote wakeup */
  1075. u32 temp;
  1076. int slot_id;
  1077. temp = readl(port_array[port_index]);
  1078. if (DEV_SUPERSPEED(temp))
  1079. temp &= ~(PORT_RWC_BITS | PORT_CEC | PORT_WAKE_BITS);
  1080. else
  1081. temp &= ~(PORT_RWC_BITS | PORT_WAKE_BITS);
  1082. if (test_bit(port_index, &bus_state->bus_suspended) &&
  1083. (temp & PORT_PLS_MASK)) {
  1084. if (DEV_SUPERSPEED(temp)) {
  1085. xhci_set_link_state(xhci, port_array,
  1086. port_index, XDEV_U0);
  1087. } else {
  1088. xhci_set_link_state(xhci, port_array,
  1089. port_index, XDEV_RESUME);
  1090. spin_unlock_irqrestore(&xhci->lock, flags);
  1091. msleep(20);
  1092. spin_lock_irqsave(&xhci->lock, flags);
  1093. xhci_set_link_state(xhci, port_array,
  1094. port_index, XDEV_U0);
  1095. }
  1096. /* wait for the port to enter U0 and report port link
  1097. * state change.
  1098. */
  1099. spin_unlock_irqrestore(&xhci->lock, flags);
  1100. msleep(20);
  1101. spin_lock_irqsave(&xhci->lock, flags);
  1102. /* Clear PLC */
  1103. xhci_test_and_clear_bit(xhci, port_array, port_index,
  1104. PORT_PLC);
  1105. slot_id = xhci_find_slot_id_by_port(hcd,
  1106. xhci, port_index + 1);
  1107. if (slot_id)
  1108. xhci_ring_device(xhci, slot_id);
  1109. } else
  1110. writel(temp, port_array[port_index]);
  1111. }
  1112. (void) readl(&xhci->op_regs->command);
  1113. bus_state->next_statechange = jiffies + msecs_to_jiffies(5);
  1114. /* re-enable irqs */
  1115. temp = readl(&xhci->op_regs->command);
  1116. temp |= CMD_EIE;
  1117. writel(temp, &xhci->op_regs->command);
  1118. temp = readl(&xhci->op_regs->command);
  1119. spin_unlock_irqrestore(&xhci->lock, flags);
  1120. return 0;
  1121. }
  1122. #endif /* CONFIG_PM */