xhci-hub.c 35 KB

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