xhci-hub.c 37 KB

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