xhci-hub.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258
  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 <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 = readl(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 = readl(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 ret;
  245. int i;
  246. ret = 0;
  247. virt_dev = xhci->devs[slot_id];
  248. cmd = xhci_alloc_command(xhci, false, true, GFP_NOIO);
  249. if (!cmd) {
  250. xhci_dbg(xhci, "Couldn't allocate command structure.\n");
  251. return -ENOMEM;
  252. }
  253. spin_lock_irqsave(&xhci->lock, flags);
  254. for (i = LAST_EP_INDEX; i > 0; i--) {
  255. if (virt_dev->eps[i].ring && virt_dev->eps[i].ring->dequeue) {
  256. struct xhci_command *command;
  257. command = xhci_alloc_command(xhci, false, false,
  258. GFP_NOWAIT);
  259. if (!command) {
  260. spin_unlock_irqrestore(&xhci->lock, flags);
  261. xhci_free_command(xhci, cmd);
  262. return -ENOMEM;
  263. }
  264. xhci_queue_stop_endpoint(xhci, command, slot_id, i,
  265. suspend);
  266. }
  267. }
  268. xhci_queue_stop_endpoint(xhci, cmd, slot_id, 0, suspend);
  269. xhci_ring_cmd_db(xhci);
  270. spin_unlock_irqrestore(&xhci->lock, flags);
  271. /* Wait for last stop endpoint command to finish */
  272. wait_for_completion(cmd->completion);
  273. if (cmd->status == COMP_CMD_ABORT || cmd->status == COMP_CMD_STOP) {
  274. xhci_warn(xhci, "Timeout while waiting for stop endpoint command\n");
  275. ret = -ETIME;
  276. }
  277. xhci_free_command(xhci, cmd);
  278. return ret;
  279. }
  280. /*
  281. * Ring device, it rings the all doorbells unconditionally.
  282. */
  283. void xhci_ring_device(struct xhci_hcd *xhci, int slot_id)
  284. {
  285. int i, s;
  286. struct xhci_virt_ep *ep;
  287. for (i = 0; i < LAST_EP_INDEX + 1; i++) {
  288. ep = &xhci->devs[slot_id]->eps[i];
  289. if (ep->ep_state & EP_HAS_STREAMS) {
  290. for (s = 1; s < ep->stream_info->num_streams; s++)
  291. xhci_ring_ep_doorbell(xhci, slot_id, i, s);
  292. } else if (ep->ring && ep->ring->dequeue) {
  293. xhci_ring_ep_doorbell(xhci, slot_id, i, 0);
  294. }
  295. }
  296. return;
  297. }
  298. static void xhci_disable_port(struct usb_hcd *hcd, struct xhci_hcd *xhci,
  299. u16 wIndex, __le32 __iomem *addr, u32 port_status)
  300. {
  301. /* Don't allow the USB core to disable SuperSpeed ports. */
  302. if (hcd->speed == HCD_USB3) {
  303. xhci_dbg(xhci, "Ignoring request to disable "
  304. "SuperSpeed port.\n");
  305. return;
  306. }
  307. /* Write 1 to disable the port */
  308. writel(port_status | PORT_PE, addr);
  309. port_status = readl(addr);
  310. xhci_dbg(xhci, "disable port, actual port %d status = 0x%x\n",
  311. wIndex, port_status);
  312. }
  313. static void xhci_clear_port_change_bit(struct xhci_hcd *xhci, u16 wValue,
  314. u16 wIndex, __le32 __iomem *addr, u32 port_status)
  315. {
  316. char *port_change_bit;
  317. u32 status;
  318. switch (wValue) {
  319. case USB_PORT_FEAT_C_RESET:
  320. status = PORT_RC;
  321. port_change_bit = "reset";
  322. break;
  323. case USB_PORT_FEAT_C_BH_PORT_RESET:
  324. status = PORT_WRC;
  325. port_change_bit = "warm(BH) reset";
  326. break;
  327. case USB_PORT_FEAT_C_CONNECTION:
  328. status = PORT_CSC;
  329. port_change_bit = "connect";
  330. break;
  331. case USB_PORT_FEAT_C_OVER_CURRENT:
  332. status = PORT_OCC;
  333. port_change_bit = "over-current";
  334. break;
  335. case USB_PORT_FEAT_C_ENABLE:
  336. status = PORT_PEC;
  337. port_change_bit = "enable/disable";
  338. break;
  339. case USB_PORT_FEAT_C_SUSPEND:
  340. status = PORT_PLC;
  341. port_change_bit = "suspend/resume";
  342. break;
  343. case USB_PORT_FEAT_C_PORT_LINK_STATE:
  344. status = PORT_PLC;
  345. port_change_bit = "link state";
  346. break;
  347. default:
  348. /* Should never happen */
  349. return;
  350. }
  351. /* Change bits are all write 1 to clear */
  352. writel(port_status | status, addr);
  353. port_status = readl(addr);
  354. xhci_dbg(xhci, "clear port %s change, actual port %d status = 0x%x\n",
  355. port_change_bit, wIndex, port_status);
  356. }
  357. static int xhci_get_ports(struct usb_hcd *hcd, __le32 __iomem ***port_array)
  358. {
  359. int max_ports;
  360. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  361. if (hcd->speed == HCD_USB3) {
  362. max_ports = xhci->num_usb3_ports;
  363. *port_array = xhci->usb3_ports;
  364. } else {
  365. max_ports = xhci->num_usb2_ports;
  366. *port_array = xhci->usb2_ports;
  367. }
  368. return max_ports;
  369. }
  370. void xhci_set_link_state(struct xhci_hcd *xhci, __le32 __iomem **port_array,
  371. int port_id, u32 link_state)
  372. {
  373. u32 temp;
  374. temp = readl(port_array[port_id]);
  375. temp = xhci_port_state_to_neutral(temp);
  376. temp &= ~PORT_PLS_MASK;
  377. temp |= PORT_LINK_STROBE | link_state;
  378. writel(temp, port_array[port_id]);
  379. }
  380. static void xhci_set_remote_wake_mask(struct xhci_hcd *xhci,
  381. __le32 __iomem **port_array, int port_id, u16 wake_mask)
  382. {
  383. u32 temp;
  384. temp = readl(port_array[port_id]);
  385. temp = xhci_port_state_to_neutral(temp);
  386. if (wake_mask & USB_PORT_FEAT_REMOTE_WAKE_CONNECT)
  387. temp |= PORT_WKCONN_E;
  388. else
  389. temp &= ~PORT_WKCONN_E;
  390. if (wake_mask & USB_PORT_FEAT_REMOTE_WAKE_DISCONNECT)
  391. temp |= PORT_WKDISC_E;
  392. else
  393. temp &= ~PORT_WKDISC_E;
  394. if (wake_mask & USB_PORT_FEAT_REMOTE_WAKE_OVER_CURRENT)
  395. temp |= PORT_WKOC_E;
  396. else
  397. temp &= ~PORT_WKOC_E;
  398. writel(temp, port_array[port_id]);
  399. }
  400. /* Test and clear port RWC bit */
  401. void xhci_test_and_clear_bit(struct xhci_hcd *xhci, __le32 __iomem **port_array,
  402. int port_id, u32 port_bit)
  403. {
  404. u32 temp;
  405. temp = readl(port_array[port_id]);
  406. if (temp & port_bit) {
  407. temp = xhci_port_state_to_neutral(temp);
  408. temp |= port_bit;
  409. writel(temp, port_array[port_id]);
  410. }
  411. }
  412. /* Updates Link Status for USB 2.1 port */
  413. static void xhci_hub_report_usb2_link_state(u32 *status, u32 status_reg)
  414. {
  415. if ((status_reg & PORT_PLS_MASK) == XDEV_U2)
  416. *status |= USB_PORT_STAT_L1;
  417. }
  418. /* Updates Link Status for super Speed port */
  419. static void xhci_hub_report_usb3_link_state(struct xhci_hcd *xhci,
  420. u32 *status, u32 status_reg)
  421. {
  422. u32 pls = status_reg & PORT_PLS_MASK;
  423. /* resume state is a xHCI internal state.
  424. * Do not report it to usb core.
  425. */
  426. if (pls == XDEV_RESUME)
  427. return;
  428. /* When the CAS bit is set then warm reset
  429. * should be performed on port
  430. */
  431. if (status_reg & PORT_CAS) {
  432. /* The CAS bit can be set while the port is
  433. * in any link state.
  434. * Only roothubs have CAS bit, so we
  435. * pretend to be in compliance mode
  436. * unless we're already in compliance
  437. * or the inactive state.
  438. */
  439. if (pls != USB_SS_PORT_LS_COMP_MOD &&
  440. pls != USB_SS_PORT_LS_SS_INACTIVE) {
  441. pls = USB_SS_PORT_LS_COMP_MOD;
  442. }
  443. /* Return also connection bit -
  444. * hub state machine resets port
  445. * when this bit is set.
  446. */
  447. pls |= USB_PORT_STAT_CONNECTION;
  448. } else {
  449. /*
  450. * If CAS bit isn't set but the Port is already at
  451. * Compliance Mode, fake a connection so the USB core
  452. * notices the Compliance state and resets the port.
  453. * This resolves an issue generated by the SN65LVPE502CP
  454. * in which sometimes the port enters compliance mode
  455. * caused by a delay on the host-device negotiation.
  456. */
  457. if ((xhci->quirks & XHCI_COMP_MODE_QUIRK) &&
  458. (pls == USB_SS_PORT_LS_COMP_MOD))
  459. pls |= USB_PORT_STAT_CONNECTION;
  460. }
  461. /* update status field */
  462. *status |= pls;
  463. }
  464. /*
  465. * Function for Compliance Mode Quirk.
  466. *
  467. * This Function verifies if all xhc USB3 ports have entered U0, if so,
  468. * the compliance mode timer is deleted. A port won't enter
  469. * compliance mode if it has previously entered U0.
  470. */
  471. static void xhci_del_comp_mod_timer(struct xhci_hcd *xhci, u32 status,
  472. u16 wIndex)
  473. {
  474. u32 all_ports_seen_u0 = ((1 << xhci->num_usb3_ports)-1);
  475. bool port_in_u0 = ((status & PORT_PLS_MASK) == XDEV_U0);
  476. if (!(xhci->quirks & XHCI_COMP_MODE_QUIRK))
  477. return;
  478. if ((xhci->port_status_u0 != all_ports_seen_u0) && port_in_u0) {
  479. xhci->port_status_u0 |= 1 << wIndex;
  480. if (xhci->port_status_u0 == all_ports_seen_u0) {
  481. del_timer_sync(&xhci->comp_mode_recovery_timer);
  482. xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
  483. "All USB3 ports have entered U0 already!");
  484. xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
  485. "Compliance Mode Recovery Timer Deleted.");
  486. }
  487. }
  488. }
  489. /*
  490. * Converts a raw xHCI port status into the format that external USB 2.0 or USB
  491. * 3.0 hubs use.
  492. *
  493. * Possible side effects:
  494. * - Mark a port as being done with device resume,
  495. * and ring the endpoint doorbells.
  496. * - Stop the Synopsys redriver Compliance Mode polling.
  497. * - Drop and reacquire the xHCI lock, in order to wait for port resume.
  498. */
  499. static u32 xhci_get_port_status(struct usb_hcd *hcd,
  500. struct xhci_bus_state *bus_state,
  501. __le32 __iomem **port_array,
  502. u16 wIndex, u32 raw_port_status,
  503. unsigned long flags)
  504. __releases(&xhci->lock)
  505. __acquires(&xhci->lock)
  506. {
  507. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  508. u32 status = 0;
  509. int slot_id;
  510. /* wPortChange bits */
  511. if (raw_port_status & PORT_CSC)
  512. status |= USB_PORT_STAT_C_CONNECTION << 16;
  513. if (raw_port_status & PORT_PEC)
  514. status |= USB_PORT_STAT_C_ENABLE << 16;
  515. if ((raw_port_status & PORT_OCC))
  516. status |= USB_PORT_STAT_C_OVERCURRENT << 16;
  517. if ((raw_port_status & PORT_RC))
  518. status |= USB_PORT_STAT_C_RESET << 16;
  519. /* USB3.0 only */
  520. if (hcd->speed == HCD_USB3) {
  521. if ((raw_port_status & PORT_PLC))
  522. status |= USB_PORT_STAT_C_LINK_STATE << 16;
  523. if ((raw_port_status & PORT_WRC))
  524. status |= USB_PORT_STAT_C_BH_RESET << 16;
  525. }
  526. if (hcd->speed != HCD_USB3) {
  527. if ((raw_port_status & PORT_PLS_MASK) == XDEV_U3
  528. && (raw_port_status & PORT_POWER))
  529. status |= USB_PORT_STAT_SUSPEND;
  530. }
  531. if ((raw_port_status & PORT_PLS_MASK) == XDEV_RESUME &&
  532. !DEV_SUPERSPEED(raw_port_status)) {
  533. if ((raw_port_status & PORT_RESET) ||
  534. !(raw_port_status & PORT_PE))
  535. return 0xffffffff;
  536. if (time_after_eq(jiffies,
  537. bus_state->resume_done[wIndex])) {
  538. int time_left;
  539. xhci_dbg(xhci, "Resume USB2 port %d\n",
  540. wIndex + 1);
  541. bus_state->resume_done[wIndex] = 0;
  542. clear_bit(wIndex, &bus_state->resuming_ports);
  543. set_bit(wIndex, &bus_state->rexit_ports);
  544. xhci_set_link_state(xhci, port_array, wIndex,
  545. XDEV_U0);
  546. spin_unlock_irqrestore(&xhci->lock, flags);
  547. time_left = wait_for_completion_timeout(
  548. &bus_state->rexit_done[wIndex],
  549. msecs_to_jiffies(
  550. XHCI_MAX_REXIT_TIMEOUT));
  551. spin_lock_irqsave(&xhci->lock, flags);
  552. if (time_left) {
  553. slot_id = xhci_find_slot_id_by_port(hcd,
  554. xhci, wIndex + 1);
  555. if (!slot_id) {
  556. xhci_dbg(xhci, "slot_id is zero\n");
  557. return 0xffffffff;
  558. }
  559. xhci_ring_device(xhci, slot_id);
  560. } else {
  561. int port_status = readl(port_array[wIndex]);
  562. xhci_warn(xhci, "Port resume took longer than %i msec, port status = 0x%x\n",
  563. XHCI_MAX_REXIT_TIMEOUT,
  564. port_status);
  565. status |= USB_PORT_STAT_SUSPEND;
  566. clear_bit(wIndex, &bus_state->rexit_ports);
  567. }
  568. bus_state->port_c_suspend |= 1 << wIndex;
  569. bus_state->suspended_ports &= ~(1 << wIndex);
  570. } else {
  571. /*
  572. * The resume has been signaling for less than
  573. * 20ms. Report the port status as SUSPEND,
  574. * let the usbcore check port status again
  575. * and clear resume signaling later.
  576. */
  577. status |= USB_PORT_STAT_SUSPEND;
  578. }
  579. }
  580. if ((raw_port_status & PORT_PLS_MASK) == XDEV_U0
  581. && (raw_port_status & PORT_POWER)
  582. && (bus_state->suspended_ports & (1 << wIndex))) {
  583. bus_state->suspended_ports &= ~(1 << wIndex);
  584. if (hcd->speed != HCD_USB3)
  585. bus_state->port_c_suspend |= 1 << wIndex;
  586. }
  587. if (raw_port_status & PORT_CONNECT) {
  588. status |= USB_PORT_STAT_CONNECTION;
  589. status |= xhci_port_speed(raw_port_status);
  590. }
  591. if (raw_port_status & PORT_PE)
  592. status |= USB_PORT_STAT_ENABLE;
  593. if (raw_port_status & PORT_OC)
  594. status |= USB_PORT_STAT_OVERCURRENT;
  595. if (raw_port_status & PORT_RESET)
  596. status |= USB_PORT_STAT_RESET;
  597. if (raw_port_status & PORT_POWER) {
  598. if (hcd->speed == HCD_USB3)
  599. status |= USB_SS_PORT_STAT_POWER;
  600. else
  601. status |= USB_PORT_STAT_POWER;
  602. }
  603. /* Update Port Link State */
  604. if (hcd->speed == HCD_USB3) {
  605. xhci_hub_report_usb3_link_state(xhci, &status, raw_port_status);
  606. /*
  607. * Verify if all USB3 Ports Have entered U0 already.
  608. * Delete Compliance Mode Timer if so.
  609. */
  610. xhci_del_comp_mod_timer(xhci, raw_port_status, wIndex);
  611. } else {
  612. xhci_hub_report_usb2_link_state(&status, raw_port_status);
  613. }
  614. if (bus_state->port_c_suspend & (1 << wIndex))
  615. status |= 1 << USB_PORT_FEAT_C_SUSPEND;
  616. return status;
  617. }
  618. int xhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
  619. u16 wIndex, char *buf, u16 wLength)
  620. {
  621. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  622. int max_ports;
  623. unsigned long flags;
  624. u32 temp, status;
  625. int retval = 0;
  626. __le32 __iomem **port_array;
  627. int slot_id;
  628. struct xhci_bus_state *bus_state;
  629. u16 link_state = 0;
  630. u16 wake_mask = 0;
  631. u16 timeout = 0;
  632. max_ports = xhci_get_ports(hcd, &port_array);
  633. bus_state = &xhci->bus_state[hcd_index(hcd)];
  634. spin_lock_irqsave(&xhci->lock, flags);
  635. switch (typeReq) {
  636. case GetHubStatus:
  637. /* No power source, over-current reported per port */
  638. memset(buf, 0, 4);
  639. break;
  640. case GetHubDescriptor:
  641. /* Check to make sure userspace is asking for the USB 3.0 hub
  642. * descriptor for the USB 3.0 roothub. If not, we stall the
  643. * endpoint, like external hubs do.
  644. */
  645. if (hcd->speed == HCD_USB3 &&
  646. (wLength < USB_DT_SS_HUB_SIZE ||
  647. wValue != (USB_DT_SS_HUB << 8))) {
  648. xhci_dbg(xhci, "Wrong hub descriptor type for "
  649. "USB 3.0 roothub.\n");
  650. goto error;
  651. }
  652. xhci_hub_descriptor(hcd, xhci,
  653. (struct usb_hub_descriptor *) buf);
  654. break;
  655. case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
  656. if ((wValue & 0xff00) != (USB_DT_BOS << 8))
  657. goto error;
  658. if (hcd->speed != HCD_USB3)
  659. goto error;
  660. /* Set the U1 and U2 exit latencies. */
  661. memcpy(buf, &usb_bos_descriptor,
  662. USB_DT_BOS_SIZE + USB_DT_USB_SS_CAP_SIZE);
  663. if ((xhci->quirks & XHCI_LPM_SUPPORT)) {
  664. temp = readl(&xhci->cap_regs->hcs_params3);
  665. buf[12] = HCS_U1_LATENCY(temp);
  666. put_unaligned_le16(HCS_U2_LATENCY(temp), &buf[13]);
  667. }
  668. /* Indicate whether the host has LTM support. */
  669. temp = readl(&xhci->cap_regs->hcc_params);
  670. if (HCC_LTC(temp))
  671. buf[8] |= USB_LTM_SUPPORT;
  672. spin_unlock_irqrestore(&xhci->lock, flags);
  673. return USB_DT_BOS_SIZE + USB_DT_USB_SS_CAP_SIZE;
  674. case GetPortStatus:
  675. if (!wIndex || wIndex > max_ports)
  676. goto error;
  677. wIndex--;
  678. temp = readl(port_array[wIndex]);
  679. if (temp == 0xffffffff) {
  680. retval = -ENODEV;
  681. break;
  682. }
  683. status = xhci_get_port_status(hcd, bus_state, port_array,
  684. wIndex, temp, flags);
  685. if (status == 0xffffffff)
  686. goto error;
  687. xhci_dbg(xhci, "get port status, actual port %d status = 0x%x\n",
  688. wIndex, temp);
  689. xhci_dbg(xhci, "Get port status returned 0x%x\n", status);
  690. put_unaligned(cpu_to_le32(status), (__le32 *) buf);
  691. break;
  692. case SetPortFeature:
  693. if (wValue == USB_PORT_FEAT_LINK_STATE)
  694. link_state = (wIndex & 0xff00) >> 3;
  695. if (wValue == USB_PORT_FEAT_REMOTE_WAKE_MASK)
  696. wake_mask = wIndex & 0xff00;
  697. /* The MSB of wIndex is the U1/U2 timeout */
  698. timeout = (wIndex & 0xff00) >> 8;
  699. wIndex &= 0xff;
  700. if (!wIndex || wIndex > max_ports)
  701. goto error;
  702. wIndex--;
  703. temp = readl(port_array[wIndex]);
  704. if (temp == 0xffffffff) {
  705. retval = -ENODEV;
  706. break;
  707. }
  708. temp = xhci_port_state_to_neutral(temp);
  709. /* FIXME: What new port features do we need to support? */
  710. switch (wValue) {
  711. case USB_PORT_FEAT_SUSPEND:
  712. temp = readl(port_array[wIndex]);
  713. if ((temp & PORT_PLS_MASK) != XDEV_U0) {
  714. /* Resume the port to U0 first */
  715. xhci_set_link_state(xhci, port_array, wIndex,
  716. XDEV_U0);
  717. spin_unlock_irqrestore(&xhci->lock, flags);
  718. msleep(10);
  719. spin_lock_irqsave(&xhci->lock, flags);
  720. }
  721. /* In spec software should not attempt to suspend
  722. * a port unless the port reports that it is in the
  723. * enabled (PED = ‘1’,PLS < ‘3’) state.
  724. */
  725. temp = readl(port_array[wIndex]);
  726. if ((temp & PORT_PE) == 0 || (temp & PORT_RESET)
  727. || (temp & PORT_PLS_MASK) >= XDEV_U3) {
  728. xhci_warn(xhci, "USB core suspending device "
  729. "not in U0/U1/U2.\n");
  730. goto error;
  731. }
  732. slot_id = xhci_find_slot_id_by_port(hcd, xhci,
  733. wIndex + 1);
  734. if (!slot_id) {
  735. xhci_warn(xhci, "slot_id is zero\n");
  736. goto error;
  737. }
  738. /* unlock to execute stop endpoint commands */
  739. spin_unlock_irqrestore(&xhci->lock, flags);
  740. xhci_stop_device(xhci, slot_id, 1);
  741. spin_lock_irqsave(&xhci->lock, flags);
  742. xhci_set_link_state(xhci, port_array, wIndex, XDEV_U3);
  743. spin_unlock_irqrestore(&xhci->lock, flags);
  744. msleep(10); /* wait device to enter */
  745. spin_lock_irqsave(&xhci->lock, flags);
  746. temp = readl(port_array[wIndex]);
  747. bus_state->suspended_ports |= 1 << wIndex;
  748. break;
  749. case USB_PORT_FEAT_LINK_STATE:
  750. temp = readl(port_array[wIndex]);
  751. /* Disable port */
  752. if (link_state == USB_SS_PORT_LS_SS_DISABLED) {
  753. xhci_dbg(xhci, "Disable port %d\n", wIndex);
  754. temp = xhci_port_state_to_neutral(temp);
  755. /*
  756. * Clear all change bits, so that we get a new
  757. * connection event.
  758. */
  759. temp |= PORT_CSC | PORT_PEC | PORT_WRC |
  760. PORT_OCC | PORT_RC | PORT_PLC |
  761. PORT_CEC;
  762. writel(temp | PORT_PE, port_array[wIndex]);
  763. temp = readl(port_array[wIndex]);
  764. break;
  765. }
  766. /* Put link in RxDetect (enable port) */
  767. if (link_state == USB_SS_PORT_LS_RX_DETECT) {
  768. xhci_dbg(xhci, "Enable port %d\n", wIndex);
  769. xhci_set_link_state(xhci, port_array, wIndex,
  770. link_state);
  771. temp = readl(port_array[wIndex]);
  772. break;
  773. }
  774. /* Software should not attempt to set
  775. * port link state above '3' (U3) and the port
  776. * must be enabled.
  777. */
  778. if ((temp & PORT_PE) == 0 ||
  779. (link_state > USB_SS_PORT_LS_U3)) {
  780. xhci_warn(xhci, "Cannot set link state.\n");
  781. goto error;
  782. }
  783. if (link_state == USB_SS_PORT_LS_U3) {
  784. slot_id = xhci_find_slot_id_by_port(hcd, xhci,
  785. wIndex + 1);
  786. if (slot_id) {
  787. /* unlock to execute stop endpoint
  788. * commands */
  789. spin_unlock_irqrestore(&xhci->lock,
  790. flags);
  791. xhci_stop_device(xhci, slot_id, 1);
  792. spin_lock_irqsave(&xhci->lock, flags);
  793. }
  794. }
  795. xhci_set_link_state(xhci, port_array, wIndex,
  796. link_state);
  797. spin_unlock_irqrestore(&xhci->lock, flags);
  798. msleep(20); /* wait device to enter */
  799. spin_lock_irqsave(&xhci->lock, flags);
  800. temp = readl(port_array[wIndex]);
  801. if (link_state == USB_SS_PORT_LS_U3)
  802. bus_state->suspended_ports |= 1 << wIndex;
  803. break;
  804. case USB_PORT_FEAT_POWER:
  805. /*
  806. * Turn on ports, even if there isn't per-port switching.
  807. * HC will report connect events even before this is set.
  808. * However, hub_wq will ignore the roothub events until
  809. * the roothub is registered.
  810. */
  811. writel(temp | PORT_POWER, port_array[wIndex]);
  812. temp = readl(port_array[wIndex]);
  813. xhci_dbg(xhci, "set port power, actual port %d status = 0x%x\n", wIndex, temp);
  814. spin_unlock_irqrestore(&xhci->lock, flags);
  815. temp = usb_acpi_power_manageable(hcd->self.root_hub,
  816. wIndex);
  817. if (temp)
  818. usb_acpi_set_power_state(hcd->self.root_hub,
  819. wIndex, true);
  820. spin_lock_irqsave(&xhci->lock, flags);
  821. break;
  822. case USB_PORT_FEAT_RESET:
  823. temp = (temp | PORT_RESET);
  824. writel(temp, port_array[wIndex]);
  825. temp = readl(port_array[wIndex]);
  826. xhci_dbg(xhci, "set port reset, actual port %d status = 0x%x\n", wIndex, temp);
  827. break;
  828. case USB_PORT_FEAT_REMOTE_WAKE_MASK:
  829. xhci_set_remote_wake_mask(xhci, port_array,
  830. wIndex, wake_mask);
  831. temp = readl(port_array[wIndex]);
  832. xhci_dbg(xhci, "set port remote wake mask, "
  833. "actual port %d status = 0x%x\n",
  834. wIndex, temp);
  835. break;
  836. case USB_PORT_FEAT_BH_PORT_RESET:
  837. temp |= PORT_WR;
  838. writel(temp, port_array[wIndex]);
  839. temp = readl(port_array[wIndex]);
  840. break;
  841. case USB_PORT_FEAT_U1_TIMEOUT:
  842. if (hcd->speed != HCD_USB3)
  843. goto error;
  844. temp = readl(port_array[wIndex] + PORTPMSC);
  845. temp &= ~PORT_U1_TIMEOUT_MASK;
  846. temp |= PORT_U1_TIMEOUT(timeout);
  847. writel(temp, port_array[wIndex] + PORTPMSC);
  848. break;
  849. case USB_PORT_FEAT_U2_TIMEOUT:
  850. if (hcd->speed != HCD_USB3)
  851. goto error;
  852. temp = readl(port_array[wIndex] + PORTPMSC);
  853. temp &= ~PORT_U2_TIMEOUT_MASK;
  854. temp |= PORT_U2_TIMEOUT(timeout);
  855. writel(temp, port_array[wIndex] + PORTPMSC);
  856. break;
  857. default:
  858. goto error;
  859. }
  860. /* unblock any posted writes */
  861. temp = readl(port_array[wIndex]);
  862. break;
  863. case ClearPortFeature:
  864. if (!wIndex || wIndex > max_ports)
  865. goto error;
  866. wIndex--;
  867. temp = readl(port_array[wIndex]);
  868. if (temp == 0xffffffff) {
  869. retval = -ENODEV;
  870. break;
  871. }
  872. /* FIXME: What new port features do we need to support? */
  873. temp = xhci_port_state_to_neutral(temp);
  874. switch (wValue) {
  875. case USB_PORT_FEAT_SUSPEND:
  876. temp = readl(port_array[wIndex]);
  877. xhci_dbg(xhci, "clear USB_PORT_FEAT_SUSPEND\n");
  878. xhci_dbg(xhci, "PORTSC %04x\n", temp);
  879. if (temp & PORT_RESET)
  880. goto error;
  881. if ((temp & PORT_PLS_MASK) == XDEV_U3) {
  882. if ((temp & PORT_PE) == 0)
  883. goto error;
  884. xhci_set_link_state(xhci, port_array, wIndex,
  885. XDEV_RESUME);
  886. spin_unlock_irqrestore(&xhci->lock, flags);
  887. msleep(20);
  888. spin_lock_irqsave(&xhci->lock, flags);
  889. xhci_set_link_state(xhci, port_array, wIndex,
  890. XDEV_U0);
  891. }
  892. bus_state->port_c_suspend |= 1 << wIndex;
  893. slot_id = xhci_find_slot_id_by_port(hcd, xhci,
  894. wIndex + 1);
  895. if (!slot_id) {
  896. xhci_dbg(xhci, "slot_id is zero\n");
  897. goto error;
  898. }
  899. xhci_ring_device(xhci, slot_id);
  900. break;
  901. case USB_PORT_FEAT_C_SUSPEND:
  902. bus_state->port_c_suspend &= ~(1 << wIndex);
  903. case USB_PORT_FEAT_C_RESET:
  904. case USB_PORT_FEAT_C_BH_PORT_RESET:
  905. case USB_PORT_FEAT_C_CONNECTION:
  906. case USB_PORT_FEAT_C_OVER_CURRENT:
  907. case USB_PORT_FEAT_C_ENABLE:
  908. case USB_PORT_FEAT_C_PORT_LINK_STATE:
  909. xhci_clear_port_change_bit(xhci, wValue, wIndex,
  910. port_array[wIndex], temp);
  911. break;
  912. case USB_PORT_FEAT_ENABLE:
  913. xhci_disable_port(hcd, xhci, wIndex,
  914. port_array[wIndex], temp);
  915. break;
  916. case USB_PORT_FEAT_POWER:
  917. writel(temp & ~PORT_POWER, port_array[wIndex]);
  918. spin_unlock_irqrestore(&xhci->lock, flags);
  919. temp = usb_acpi_power_manageable(hcd->self.root_hub,
  920. wIndex);
  921. if (temp)
  922. usb_acpi_set_power_state(hcd->self.root_hub,
  923. wIndex, false);
  924. spin_lock_irqsave(&xhci->lock, flags);
  925. break;
  926. default:
  927. goto error;
  928. }
  929. break;
  930. default:
  931. error:
  932. /* "stall" on error */
  933. retval = -EPIPE;
  934. }
  935. spin_unlock_irqrestore(&xhci->lock, flags);
  936. return retval;
  937. }
  938. /*
  939. * Returns 0 if the status hasn't changed, or the number of bytes in buf.
  940. * Ports are 0-indexed from the HCD point of view,
  941. * and 1-indexed from the USB core pointer of view.
  942. *
  943. * Note that the status change bits will be cleared as soon as a port status
  944. * change event is generated, so we use the saved status from that event.
  945. */
  946. int xhci_hub_status_data(struct usb_hcd *hcd, char *buf)
  947. {
  948. unsigned long flags;
  949. u32 temp, status;
  950. u32 mask;
  951. int i, retval;
  952. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  953. int max_ports;
  954. __le32 __iomem **port_array;
  955. struct xhci_bus_state *bus_state;
  956. bool reset_change = false;
  957. max_ports = xhci_get_ports(hcd, &port_array);
  958. bus_state = &xhci->bus_state[hcd_index(hcd)];
  959. /* Initial status is no changes */
  960. retval = (max_ports + 8) / 8;
  961. memset(buf, 0, retval);
  962. /*
  963. * Inform the usbcore about resume-in-progress by returning
  964. * a non-zero value even if there are no status changes.
  965. */
  966. status = bus_state->resuming_ports;
  967. mask = PORT_CSC | PORT_PEC | PORT_OCC | PORT_PLC | PORT_WRC;
  968. spin_lock_irqsave(&xhci->lock, flags);
  969. /* For each port, did anything change? If so, set that bit in buf. */
  970. for (i = 0; i < max_ports; i++) {
  971. temp = readl(port_array[i]);
  972. if (temp == 0xffffffff) {
  973. retval = -ENODEV;
  974. break;
  975. }
  976. if ((temp & mask) != 0 ||
  977. (bus_state->port_c_suspend & 1 << i) ||
  978. (bus_state->resume_done[i] && time_after_eq(
  979. jiffies, bus_state->resume_done[i]))) {
  980. buf[(i + 1) / 8] |= 1 << (i + 1) % 8;
  981. status = 1;
  982. }
  983. if ((temp & PORT_RC))
  984. reset_change = true;
  985. }
  986. if (!status && !reset_change) {
  987. xhci_dbg(xhci, "%s: stopping port polling.\n", __func__);
  988. clear_bit(HCD_FLAG_POLL_RH, &hcd->flags);
  989. }
  990. spin_unlock_irqrestore(&xhci->lock, flags);
  991. return status ? retval : 0;
  992. }
  993. #ifdef CONFIG_PM
  994. int xhci_bus_suspend(struct usb_hcd *hcd)
  995. {
  996. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  997. int max_ports, port_index;
  998. __le32 __iomem **port_array;
  999. struct xhci_bus_state *bus_state;
  1000. unsigned long flags;
  1001. max_ports = xhci_get_ports(hcd, &port_array);
  1002. bus_state = &xhci->bus_state[hcd_index(hcd)];
  1003. spin_lock_irqsave(&xhci->lock, flags);
  1004. if (hcd->self.root_hub->do_remote_wakeup) {
  1005. if (bus_state->resuming_ports) {
  1006. spin_unlock_irqrestore(&xhci->lock, flags);
  1007. xhci_dbg(xhci, "suspend failed because "
  1008. "a port is resuming\n");
  1009. return -EBUSY;
  1010. }
  1011. }
  1012. port_index = max_ports;
  1013. bus_state->bus_suspended = 0;
  1014. while (port_index--) {
  1015. /* suspend the port if the port is not suspended */
  1016. u32 t1, t2;
  1017. int slot_id;
  1018. t1 = readl(port_array[port_index]);
  1019. t2 = xhci_port_state_to_neutral(t1);
  1020. if ((t1 & PORT_PE) && !(t1 & PORT_PLS_MASK)) {
  1021. xhci_dbg(xhci, "port %d not suspended\n", port_index);
  1022. slot_id = xhci_find_slot_id_by_port(hcd, xhci,
  1023. port_index + 1);
  1024. if (slot_id) {
  1025. spin_unlock_irqrestore(&xhci->lock, flags);
  1026. xhci_stop_device(xhci, slot_id, 1);
  1027. spin_lock_irqsave(&xhci->lock, flags);
  1028. }
  1029. t2 &= ~PORT_PLS_MASK;
  1030. t2 |= PORT_LINK_STROBE | XDEV_U3;
  1031. set_bit(port_index, &bus_state->bus_suspended);
  1032. }
  1033. /* USB core sets remote wake mask for USB 3.0 hubs,
  1034. * including the USB 3.0 roothub, but only if CONFIG_PM
  1035. * is enabled, so also enable remote wake here.
  1036. */
  1037. if (hcd->self.root_hub->do_remote_wakeup) {
  1038. if (t1 & PORT_CONNECT) {
  1039. t2 |= PORT_WKOC_E | PORT_WKDISC_E;
  1040. t2 &= ~PORT_WKCONN_E;
  1041. } else {
  1042. t2 |= PORT_WKOC_E | PORT_WKCONN_E;
  1043. t2 &= ~PORT_WKDISC_E;
  1044. }
  1045. } else
  1046. t2 &= ~PORT_WAKE_BITS;
  1047. t1 = xhci_port_state_to_neutral(t1);
  1048. if (t1 != t2)
  1049. writel(t2, port_array[port_index]);
  1050. }
  1051. hcd->state = HC_STATE_SUSPENDED;
  1052. bus_state->next_statechange = jiffies + msecs_to_jiffies(10);
  1053. spin_unlock_irqrestore(&xhci->lock, flags);
  1054. return 0;
  1055. }
  1056. int xhci_bus_resume(struct usb_hcd *hcd)
  1057. {
  1058. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  1059. int max_ports, port_index;
  1060. __le32 __iomem **port_array;
  1061. struct xhci_bus_state *bus_state;
  1062. u32 temp;
  1063. unsigned long flags;
  1064. max_ports = xhci_get_ports(hcd, &port_array);
  1065. bus_state = &xhci->bus_state[hcd_index(hcd)];
  1066. if (time_before(jiffies, bus_state->next_statechange))
  1067. msleep(5);
  1068. spin_lock_irqsave(&xhci->lock, flags);
  1069. if (!HCD_HW_ACCESSIBLE(hcd)) {
  1070. spin_unlock_irqrestore(&xhci->lock, flags);
  1071. return -ESHUTDOWN;
  1072. }
  1073. /* delay the irqs */
  1074. temp = readl(&xhci->op_regs->command);
  1075. temp &= ~CMD_EIE;
  1076. writel(temp, &xhci->op_regs->command);
  1077. port_index = max_ports;
  1078. while (port_index--) {
  1079. /* Check whether need resume ports. If needed
  1080. resume port and disable remote wakeup */
  1081. u32 temp;
  1082. int slot_id;
  1083. temp = readl(port_array[port_index]);
  1084. if (DEV_SUPERSPEED(temp))
  1085. temp &= ~(PORT_RWC_BITS | PORT_CEC | PORT_WAKE_BITS);
  1086. else
  1087. temp &= ~(PORT_RWC_BITS | PORT_WAKE_BITS);
  1088. if (test_bit(port_index, &bus_state->bus_suspended) &&
  1089. (temp & PORT_PLS_MASK)) {
  1090. if (DEV_SUPERSPEED(temp)) {
  1091. xhci_set_link_state(xhci, port_array,
  1092. port_index, XDEV_U0);
  1093. } else {
  1094. xhci_set_link_state(xhci, port_array,
  1095. port_index, XDEV_RESUME);
  1096. spin_unlock_irqrestore(&xhci->lock, flags);
  1097. msleep(20);
  1098. spin_lock_irqsave(&xhci->lock, flags);
  1099. xhci_set_link_state(xhci, port_array,
  1100. port_index, XDEV_U0);
  1101. }
  1102. /* wait for the port to enter U0 and report port link
  1103. * state change.
  1104. */
  1105. spin_unlock_irqrestore(&xhci->lock, flags);
  1106. msleep(20);
  1107. spin_lock_irqsave(&xhci->lock, flags);
  1108. /* Clear PLC */
  1109. xhci_test_and_clear_bit(xhci, port_array, port_index,
  1110. PORT_PLC);
  1111. slot_id = xhci_find_slot_id_by_port(hcd,
  1112. xhci, port_index + 1);
  1113. if (slot_id)
  1114. xhci_ring_device(xhci, slot_id);
  1115. } else
  1116. writel(temp, port_array[port_index]);
  1117. }
  1118. (void) readl(&xhci->op_regs->command);
  1119. bus_state->next_statechange = jiffies + msecs_to_jiffies(5);
  1120. /* re-enable irqs */
  1121. temp = readl(&xhci->op_regs->command);
  1122. temp |= CMD_EIE;
  1123. writel(temp, &xhci->op_regs->command);
  1124. temp = readl(&xhci->op_regs->command);
  1125. spin_unlock_irqrestore(&xhci->lock, flags);
  1126. return 0;
  1127. }
  1128. #endif /* CONFIG_PM */