xhci-hub.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163
  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 1.1 root hub device descriptor */
  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. memset(&desc->u.ss.DeviceRemovable,
  137. (__force __u16) cpu_to_le16(port_removable),
  138. sizeof(__u16));
  139. }
  140. static void xhci_hub_descriptor(struct usb_hcd *hcd, struct xhci_hcd *xhci,
  141. struct usb_hub_descriptor *desc)
  142. {
  143. if (hcd->speed == HCD_USB3)
  144. xhci_usb3_hub_descriptor(hcd, xhci, desc);
  145. else
  146. xhci_usb2_hub_descriptor(hcd, xhci, desc);
  147. }
  148. static unsigned int xhci_port_speed(unsigned int port_status)
  149. {
  150. if (DEV_LOWSPEED(port_status))
  151. return USB_PORT_STAT_LOW_SPEED;
  152. if (DEV_HIGHSPEED(port_status))
  153. return USB_PORT_STAT_HIGH_SPEED;
  154. /*
  155. * FIXME: Yes, we should check for full speed, but the core uses that as
  156. * a default in portspeed() in usb/core/hub.c (which is the only place
  157. * USB_PORT_STAT_*_SPEED is used).
  158. */
  159. return 0;
  160. }
  161. /*
  162. * These bits are Read Only (RO) and should be saved and written to the
  163. * registers: 0, 3, 10:13, 30
  164. * connect status, over-current status, port speed, and device removable.
  165. * connect status and port speed are also sticky - meaning they're in
  166. * the AUX well and they aren't changed by a hot, warm, or cold reset.
  167. */
  168. #define XHCI_PORT_RO ((1<<0) | (1<<3) | (0xf<<10) | (1<<30))
  169. /*
  170. * These bits are RW; writing a 0 clears the bit, writing a 1 sets the bit:
  171. * bits 5:8, 9, 14:15, 25:27
  172. * link state, port power, port indicator state, "wake on" enable state
  173. */
  174. #define XHCI_PORT_RWS ((0xf<<5) | (1<<9) | (0x3<<14) | (0x7<<25))
  175. /*
  176. * These bits are RW; writing a 1 sets the bit, writing a 0 has no effect:
  177. * bit 4 (port reset)
  178. */
  179. #define XHCI_PORT_RW1S ((1<<4))
  180. /*
  181. * These bits are RW; writing a 1 clears the bit, writing a 0 has no effect:
  182. * bits 1, 17, 18, 19, 20, 21, 22, 23
  183. * port enable/disable, and
  184. * change bits: connect, PED, warm port reset changed (reserved zero for USB 2.0 ports),
  185. * over-current, reset, link state, and L1 change
  186. */
  187. #define XHCI_PORT_RW1CS ((1<<1) | (0x7f<<17))
  188. /*
  189. * Bit 16 is RW, and writing a '1' to it causes the link state control to be
  190. * latched in
  191. */
  192. #define XHCI_PORT_RW ((1<<16))
  193. /*
  194. * These bits are Reserved Zero (RsvdZ) and zero should be written to them:
  195. * bits 2, 24, 28:31
  196. */
  197. #define XHCI_PORT_RZ ((1<<2) | (1<<24) | (0xf<<28))
  198. /*
  199. * Given a port state, this function returns a value that would result in the
  200. * port being in the same state, if the value was written to the port status
  201. * control register.
  202. * Save Read Only (RO) bits and save read/write bits where
  203. * writing a 0 clears the bit and writing a 1 sets the bit (RWS).
  204. * For all other types (RW1S, RW1CS, RW, and RZ), writing a '0' has no effect.
  205. */
  206. u32 xhci_port_state_to_neutral(u32 state)
  207. {
  208. /* Save read-only status and port state */
  209. return (state & XHCI_PORT_RO) | (state & XHCI_PORT_RWS);
  210. }
  211. /*
  212. * find slot id based on port number.
  213. * @port: The one-based port number from one of the two split roothubs.
  214. */
  215. int xhci_find_slot_id_by_port(struct usb_hcd *hcd, struct xhci_hcd *xhci,
  216. u16 port)
  217. {
  218. int slot_id;
  219. int i;
  220. enum usb_device_speed speed;
  221. slot_id = 0;
  222. for (i = 0; i < MAX_HC_SLOTS; i++) {
  223. if (!xhci->devs[i])
  224. continue;
  225. speed = xhci->devs[i]->udev->speed;
  226. if (((speed == USB_SPEED_SUPER) == (hcd->speed == HCD_USB3))
  227. && xhci->devs[i]->fake_port == port) {
  228. slot_id = i;
  229. break;
  230. }
  231. }
  232. return slot_id;
  233. }
  234. /*
  235. * Stop device
  236. * It issues stop endpoint command for EP 0 to 30. And wait the last command
  237. * to complete.
  238. * suspend will set to 1, if suspend bit need to set in command.
  239. */
  240. static int xhci_stop_device(struct xhci_hcd *xhci, int slot_id, int suspend)
  241. {
  242. struct xhci_virt_device *virt_dev;
  243. struct xhci_command *cmd;
  244. unsigned long flags;
  245. int timeleft;
  246. int ret;
  247. int i;
  248. ret = 0;
  249. virt_dev = xhci->devs[slot_id];
  250. cmd = xhci_alloc_command(xhci, false, true, GFP_NOIO);
  251. if (!cmd) {
  252. xhci_dbg(xhci, "Couldn't allocate command structure.\n");
  253. return -ENOMEM;
  254. }
  255. spin_lock_irqsave(&xhci->lock, flags);
  256. for (i = LAST_EP_INDEX; i > 0; i--) {
  257. if (virt_dev->eps[i].ring && virt_dev->eps[i].ring->dequeue)
  258. xhci_queue_stop_endpoint(xhci, slot_id, i, suspend);
  259. }
  260. cmd->command_trb = xhci->cmd_ring->enqueue;
  261. list_add_tail(&cmd->cmd_list, &virt_dev->cmd_list);
  262. xhci_queue_stop_endpoint(xhci, slot_id, 0, suspend);
  263. xhci_ring_cmd_db(xhci);
  264. spin_unlock_irqrestore(&xhci->lock, flags);
  265. /* Wait for last stop endpoint command to finish */
  266. timeleft = wait_for_completion_interruptible_timeout(
  267. cmd->completion,
  268. USB_CTRL_SET_TIMEOUT);
  269. if (timeleft <= 0) {
  270. xhci_warn(xhci, "%s while waiting for stop endpoint command\n",
  271. timeleft == 0 ? "Timeout" : "Signal");
  272. spin_lock_irqsave(&xhci->lock, flags);
  273. /* The timeout might have raced with the event ring handler, so
  274. * only delete from the list if the item isn't poisoned.
  275. */
  276. if (cmd->cmd_list.next != LIST_POISON1)
  277. list_del(&cmd->cmd_list);
  278. spin_unlock_irqrestore(&xhci->lock, flags);
  279. ret = -ETIME;
  280. goto command_cleanup;
  281. }
  282. command_cleanup:
  283. xhci_free_command(xhci, cmd);
  284. return ret;
  285. }
  286. /*
  287. * Ring device, it rings the all doorbells unconditionally.
  288. */
  289. void xhci_ring_device(struct xhci_hcd *xhci, int slot_id)
  290. {
  291. int i;
  292. for (i = 0; i < LAST_EP_INDEX + 1; i++)
  293. if (xhci->devs[slot_id]->eps[i].ring &&
  294. xhci->devs[slot_id]->eps[i].ring->dequeue)
  295. xhci_ring_ep_doorbell(xhci, slot_id, i, 0);
  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. xhci_writel(xhci, port_status | PORT_PE, addr);
  309. port_status = xhci_readl(xhci, 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. xhci_writel(xhci, port_status | status, addr);
  353. port_status = xhci_readl(xhci, 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 = xhci_readl(xhci, 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. xhci_writel(xhci, temp, port_array[port_id]);
  379. }
  380. 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 = xhci_readl(xhci, 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. xhci_writel(xhci, 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 = xhci_readl(xhci, port_array[port_id]);
  406. if (temp & port_bit) {
  407. temp = xhci_port_state_to_neutral(temp);
  408. temp |= port_bit;
  409. xhci_writel(xhci, temp, port_array[port_id]);
  410. }
  411. }
  412. /* Updates Link Status for super Speed port */
  413. static void xhci_hub_report_link_state(u32 *status, u32 status_reg)
  414. {
  415. u32 pls = status_reg & PORT_PLS_MASK;
  416. /* resume state is a xHCI internal state.
  417. * Do not report it to usb core.
  418. */
  419. if (pls == XDEV_RESUME)
  420. return;
  421. /* When the CAS bit is set then warm reset
  422. * should be performed on port
  423. */
  424. if (status_reg & PORT_CAS) {
  425. /* The CAS bit can be set while the port is
  426. * in any link state.
  427. * Only roothubs have CAS bit, so we
  428. * pretend to be in compliance mode
  429. * unless we're already in compliance
  430. * or the inactive state.
  431. */
  432. if (pls != USB_SS_PORT_LS_COMP_MOD &&
  433. pls != USB_SS_PORT_LS_SS_INACTIVE) {
  434. pls = USB_SS_PORT_LS_COMP_MOD;
  435. }
  436. /* Return also connection bit -
  437. * hub state machine resets port
  438. * when this bit is set.
  439. */
  440. pls |= USB_PORT_STAT_CONNECTION;
  441. } else {
  442. /*
  443. * If CAS bit isn't set but the Port is already at
  444. * Compliance Mode, fake a connection so the USB core
  445. * notices the Compliance state and resets the port.
  446. * This resolves an issue generated by the SN65LVPE502CP
  447. * in which sometimes the port enters compliance mode
  448. * caused by a delay on the host-device negotiation.
  449. */
  450. if (pls == USB_SS_PORT_LS_COMP_MOD)
  451. pls |= USB_PORT_STAT_CONNECTION;
  452. }
  453. /* update status field */
  454. *status |= pls;
  455. }
  456. /*
  457. * Function for Compliance Mode Quirk.
  458. *
  459. * This Function verifies if all xhc USB3 ports have entered U0, if so,
  460. * the compliance mode timer is deleted. A port won't enter
  461. * compliance mode if it has previously entered U0.
  462. */
  463. void xhci_del_comp_mod_timer(struct xhci_hcd *xhci, u32 status, u16 wIndex)
  464. {
  465. u32 all_ports_seen_u0 = ((1 << xhci->num_usb3_ports)-1);
  466. bool port_in_u0 = ((status & PORT_PLS_MASK) == XDEV_U0);
  467. if (!(xhci->quirks & XHCI_COMP_MODE_QUIRK))
  468. return;
  469. if ((xhci->port_status_u0 != all_ports_seen_u0) && port_in_u0) {
  470. xhci->port_status_u0 |= 1 << wIndex;
  471. if (xhci->port_status_u0 == all_ports_seen_u0) {
  472. del_timer_sync(&xhci->comp_mode_recovery_timer);
  473. xhci_dbg(xhci, "All USB3 ports have entered U0 already!\n");
  474. xhci_dbg(xhci, "Compliance Mode Recovery Timer Deleted.\n");
  475. }
  476. }
  477. }
  478. int xhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
  479. u16 wIndex, char *buf, u16 wLength)
  480. {
  481. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  482. int max_ports;
  483. unsigned long flags;
  484. u32 temp, status;
  485. int retval = 0;
  486. __le32 __iomem **port_array;
  487. int slot_id;
  488. struct xhci_bus_state *bus_state;
  489. u16 link_state = 0;
  490. u16 wake_mask = 0;
  491. u16 timeout = 0;
  492. max_ports = xhci_get_ports(hcd, &port_array);
  493. bus_state = &xhci->bus_state[hcd_index(hcd)];
  494. spin_lock_irqsave(&xhci->lock, flags);
  495. switch (typeReq) {
  496. case GetHubStatus:
  497. /* No power source, over-current reported per port */
  498. memset(buf, 0, 4);
  499. break;
  500. case GetHubDescriptor:
  501. /* Check to make sure userspace is asking for the USB 3.0 hub
  502. * descriptor for the USB 3.0 roothub. If not, we stall the
  503. * endpoint, like external hubs do.
  504. */
  505. if (hcd->speed == HCD_USB3 &&
  506. (wLength < USB_DT_SS_HUB_SIZE ||
  507. wValue != (USB_DT_SS_HUB << 8))) {
  508. xhci_dbg(xhci, "Wrong hub descriptor type for "
  509. "USB 3.0 roothub.\n");
  510. goto error;
  511. }
  512. xhci_hub_descriptor(hcd, xhci,
  513. (struct usb_hub_descriptor *) buf);
  514. break;
  515. case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
  516. if ((wValue & 0xff00) != (USB_DT_BOS << 8))
  517. goto error;
  518. if (hcd->speed != HCD_USB3)
  519. goto error;
  520. /* Set the U1 and U2 exit latencies. */
  521. memcpy(buf, &usb_bos_descriptor,
  522. USB_DT_BOS_SIZE + USB_DT_USB_SS_CAP_SIZE);
  523. temp = xhci_readl(xhci, &xhci->cap_regs->hcs_params3);
  524. buf[12] = HCS_U1_LATENCY(temp);
  525. put_unaligned_le16(HCS_U2_LATENCY(temp), &buf[13]);
  526. /* Indicate whether the host has LTM support. */
  527. temp = xhci_readl(xhci, &xhci->cap_regs->hcc_params);
  528. if (HCC_LTC(temp))
  529. buf[8] |= USB_LTM_SUPPORT;
  530. spin_unlock_irqrestore(&xhci->lock, flags);
  531. return USB_DT_BOS_SIZE + USB_DT_USB_SS_CAP_SIZE;
  532. case GetPortStatus:
  533. if (!wIndex || wIndex > max_ports)
  534. goto error;
  535. wIndex--;
  536. status = 0;
  537. temp = xhci_readl(xhci, port_array[wIndex]);
  538. if (temp == 0xffffffff) {
  539. retval = -ENODEV;
  540. break;
  541. }
  542. xhci_dbg(xhci, "get port status, actual port %d status = 0x%x\n", wIndex, temp);
  543. /* wPortChange bits */
  544. if (temp & PORT_CSC)
  545. status |= USB_PORT_STAT_C_CONNECTION << 16;
  546. if (temp & PORT_PEC)
  547. status |= USB_PORT_STAT_C_ENABLE << 16;
  548. if ((temp & PORT_OCC))
  549. status |= USB_PORT_STAT_C_OVERCURRENT << 16;
  550. if ((temp & PORT_RC))
  551. status |= USB_PORT_STAT_C_RESET << 16;
  552. /* USB3.0 only */
  553. if (hcd->speed == HCD_USB3) {
  554. if ((temp & PORT_PLC))
  555. status |= USB_PORT_STAT_C_LINK_STATE << 16;
  556. if ((temp & PORT_WRC))
  557. status |= USB_PORT_STAT_C_BH_RESET << 16;
  558. }
  559. if (hcd->speed != HCD_USB3) {
  560. if ((temp & PORT_PLS_MASK) == XDEV_U3
  561. && (temp & PORT_POWER))
  562. status |= USB_PORT_STAT_SUSPEND;
  563. }
  564. if ((temp & PORT_PLS_MASK) == XDEV_RESUME &&
  565. !DEV_SUPERSPEED(temp)) {
  566. if ((temp & PORT_RESET) || !(temp & PORT_PE))
  567. goto error;
  568. if (time_after_eq(jiffies,
  569. bus_state->resume_done[wIndex])) {
  570. xhci_dbg(xhci, "Resume USB2 port %d\n",
  571. wIndex + 1);
  572. bus_state->resume_done[wIndex] = 0;
  573. clear_bit(wIndex, &bus_state->resuming_ports);
  574. xhci_set_link_state(xhci, port_array, wIndex,
  575. XDEV_U0);
  576. xhci_dbg(xhci, "set port %d resume\n",
  577. wIndex + 1);
  578. slot_id = xhci_find_slot_id_by_port(hcd, xhci,
  579. wIndex + 1);
  580. if (!slot_id) {
  581. xhci_dbg(xhci, "slot_id is zero\n");
  582. goto error;
  583. }
  584. xhci_ring_device(xhci, slot_id);
  585. bus_state->port_c_suspend |= 1 << wIndex;
  586. bus_state->suspended_ports &= ~(1 << wIndex);
  587. } else {
  588. /*
  589. * The resume has been signaling for less than
  590. * 20ms. Report the port status as SUSPEND,
  591. * let the usbcore check port status again
  592. * and clear resume signaling later.
  593. */
  594. status |= USB_PORT_STAT_SUSPEND;
  595. }
  596. }
  597. if ((temp & PORT_PLS_MASK) == XDEV_U0
  598. && (temp & PORT_POWER)
  599. && (bus_state->suspended_ports & (1 << wIndex))) {
  600. bus_state->suspended_ports &= ~(1 << wIndex);
  601. if (hcd->speed != HCD_USB3)
  602. bus_state->port_c_suspend |= 1 << wIndex;
  603. }
  604. if (temp & PORT_CONNECT) {
  605. status |= USB_PORT_STAT_CONNECTION;
  606. status |= xhci_port_speed(temp);
  607. }
  608. if (temp & PORT_PE)
  609. status |= USB_PORT_STAT_ENABLE;
  610. if (temp & PORT_OC)
  611. status |= USB_PORT_STAT_OVERCURRENT;
  612. if (temp & PORT_RESET)
  613. status |= USB_PORT_STAT_RESET;
  614. if (temp & PORT_POWER) {
  615. if (hcd->speed == HCD_USB3)
  616. status |= USB_SS_PORT_STAT_POWER;
  617. else
  618. status |= USB_PORT_STAT_POWER;
  619. }
  620. /* Update Port Link State for super speed ports*/
  621. if (hcd->speed == HCD_USB3) {
  622. xhci_hub_report_link_state(&status, temp);
  623. /*
  624. * Verify if all USB3 Ports Have entered U0 already.
  625. * Delete Compliance Mode Timer if so.
  626. */
  627. xhci_del_comp_mod_timer(xhci, temp, wIndex);
  628. }
  629. if (bus_state->port_c_suspend & (1 << wIndex))
  630. status |= 1 << USB_PORT_FEAT_C_SUSPEND;
  631. xhci_dbg(xhci, "Get port status returned 0x%x\n", status);
  632. put_unaligned(cpu_to_le32(status), (__le32 *) buf);
  633. break;
  634. case SetPortFeature:
  635. if (wValue == USB_PORT_FEAT_LINK_STATE)
  636. link_state = (wIndex & 0xff00) >> 3;
  637. if (wValue == USB_PORT_FEAT_REMOTE_WAKE_MASK)
  638. wake_mask = wIndex & 0xff00;
  639. /* The MSB of wIndex is the U1/U2 timeout */
  640. timeout = (wIndex & 0xff00) >> 8;
  641. wIndex &= 0xff;
  642. if (!wIndex || wIndex > max_ports)
  643. goto error;
  644. wIndex--;
  645. temp = xhci_readl(xhci, port_array[wIndex]);
  646. if (temp == 0xffffffff) {
  647. retval = -ENODEV;
  648. break;
  649. }
  650. temp = xhci_port_state_to_neutral(temp);
  651. /* FIXME: What new port features do we need to support? */
  652. switch (wValue) {
  653. case USB_PORT_FEAT_SUSPEND:
  654. temp = xhci_readl(xhci, port_array[wIndex]);
  655. if ((temp & PORT_PLS_MASK) != XDEV_U0) {
  656. /* Resume the port to U0 first */
  657. xhci_set_link_state(xhci, port_array, wIndex,
  658. XDEV_U0);
  659. spin_unlock_irqrestore(&xhci->lock, flags);
  660. msleep(10);
  661. spin_lock_irqsave(&xhci->lock, flags);
  662. }
  663. /* In spec software should not attempt to suspend
  664. * a port unless the port reports that it is in the
  665. * enabled (PED = ‘1’,PLS < ‘3’) state.
  666. */
  667. temp = xhci_readl(xhci, port_array[wIndex]);
  668. if ((temp & PORT_PE) == 0 || (temp & PORT_RESET)
  669. || (temp & PORT_PLS_MASK) >= XDEV_U3) {
  670. xhci_warn(xhci, "USB core suspending device "
  671. "not in U0/U1/U2.\n");
  672. goto error;
  673. }
  674. slot_id = xhci_find_slot_id_by_port(hcd, xhci,
  675. wIndex + 1);
  676. if (!slot_id) {
  677. xhci_warn(xhci, "slot_id is zero\n");
  678. goto error;
  679. }
  680. /* unlock to execute stop endpoint commands */
  681. spin_unlock_irqrestore(&xhci->lock, flags);
  682. xhci_stop_device(xhci, slot_id, 1);
  683. spin_lock_irqsave(&xhci->lock, flags);
  684. xhci_set_link_state(xhci, port_array, wIndex, XDEV_U3);
  685. spin_unlock_irqrestore(&xhci->lock, flags);
  686. msleep(10); /* wait device to enter */
  687. spin_lock_irqsave(&xhci->lock, flags);
  688. temp = xhci_readl(xhci, port_array[wIndex]);
  689. bus_state->suspended_ports |= 1 << wIndex;
  690. break;
  691. case USB_PORT_FEAT_LINK_STATE:
  692. temp = xhci_readl(xhci, port_array[wIndex]);
  693. /* Software should not attempt to set
  694. * port link state above '5' (Rx.Detect) and the port
  695. * must be enabled.
  696. */
  697. if ((temp & PORT_PE) == 0 ||
  698. (link_state > USB_SS_PORT_LS_RX_DETECT)) {
  699. xhci_warn(xhci, "Cannot set link state.\n");
  700. goto error;
  701. }
  702. if (link_state == USB_SS_PORT_LS_U3) {
  703. slot_id = xhci_find_slot_id_by_port(hcd, xhci,
  704. wIndex + 1);
  705. if (slot_id) {
  706. /* unlock to execute stop endpoint
  707. * commands */
  708. spin_unlock_irqrestore(&xhci->lock,
  709. flags);
  710. xhci_stop_device(xhci, slot_id, 1);
  711. spin_lock_irqsave(&xhci->lock, flags);
  712. }
  713. }
  714. xhci_set_link_state(xhci, port_array, wIndex,
  715. link_state);
  716. spin_unlock_irqrestore(&xhci->lock, flags);
  717. msleep(20); /* wait device to enter */
  718. spin_lock_irqsave(&xhci->lock, flags);
  719. temp = xhci_readl(xhci, port_array[wIndex]);
  720. if (link_state == USB_SS_PORT_LS_U3)
  721. bus_state->suspended_ports |= 1 << wIndex;
  722. break;
  723. case USB_PORT_FEAT_POWER:
  724. /*
  725. * Turn on ports, even if there isn't per-port switching.
  726. * HC will report connect events even before this is set.
  727. * However, khubd will ignore the roothub events until
  728. * the roothub is registered.
  729. */
  730. xhci_writel(xhci, temp | PORT_POWER,
  731. port_array[wIndex]);
  732. temp = xhci_readl(xhci, port_array[wIndex]);
  733. xhci_dbg(xhci, "set port power, actual port %d status = 0x%x\n", wIndex, temp);
  734. break;
  735. case USB_PORT_FEAT_RESET:
  736. temp = (temp | PORT_RESET);
  737. xhci_writel(xhci, temp, port_array[wIndex]);
  738. temp = xhci_readl(xhci, port_array[wIndex]);
  739. xhci_dbg(xhci, "set port reset, actual port %d status = 0x%x\n", wIndex, temp);
  740. break;
  741. case USB_PORT_FEAT_REMOTE_WAKE_MASK:
  742. xhci_set_remote_wake_mask(xhci, port_array,
  743. wIndex, wake_mask);
  744. temp = xhci_readl(xhci, port_array[wIndex]);
  745. xhci_dbg(xhci, "set port remote wake mask, "
  746. "actual port %d status = 0x%x\n",
  747. wIndex, temp);
  748. break;
  749. case USB_PORT_FEAT_BH_PORT_RESET:
  750. temp |= PORT_WR;
  751. xhci_writel(xhci, temp, port_array[wIndex]);
  752. temp = xhci_readl(xhci, port_array[wIndex]);
  753. break;
  754. case USB_PORT_FEAT_U1_TIMEOUT:
  755. if (hcd->speed != HCD_USB3)
  756. goto error;
  757. temp = xhci_readl(xhci, port_array[wIndex] + 1);
  758. temp &= ~PORT_U1_TIMEOUT_MASK;
  759. temp |= PORT_U1_TIMEOUT(timeout);
  760. xhci_writel(xhci, temp, port_array[wIndex] + 1);
  761. break;
  762. case USB_PORT_FEAT_U2_TIMEOUT:
  763. if (hcd->speed != HCD_USB3)
  764. goto error;
  765. temp = xhci_readl(xhci, port_array[wIndex] + 1);
  766. temp &= ~PORT_U2_TIMEOUT_MASK;
  767. temp |= PORT_U2_TIMEOUT(timeout);
  768. xhci_writel(xhci, temp, port_array[wIndex] + 1);
  769. break;
  770. default:
  771. goto error;
  772. }
  773. /* unblock any posted writes */
  774. temp = xhci_readl(xhci, port_array[wIndex]);
  775. break;
  776. case ClearPortFeature:
  777. if (!wIndex || wIndex > max_ports)
  778. goto error;
  779. wIndex--;
  780. temp = xhci_readl(xhci, port_array[wIndex]);
  781. if (temp == 0xffffffff) {
  782. retval = -ENODEV;
  783. break;
  784. }
  785. /* FIXME: What new port features do we need to support? */
  786. temp = xhci_port_state_to_neutral(temp);
  787. switch (wValue) {
  788. case USB_PORT_FEAT_SUSPEND:
  789. temp = xhci_readl(xhci, port_array[wIndex]);
  790. xhci_dbg(xhci, "clear USB_PORT_FEAT_SUSPEND\n");
  791. xhci_dbg(xhci, "PORTSC %04x\n", temp);
  792. if (temp & PORT_RESET)
  793. goto error;
  794. if ((temp & PORT_PLS_MASK) == XDEV_U3) {
  795. if ((temp & PORT_PE) == 0)
  796. goto error;
  797. xhci_set_link_state(xhci, port_array, wIndex,
  798. XDEV_RESUME);
  799. spin_unlock_irqrestore(&xhci->lock, flags);
  800. msleep(20);
  801. spin_lock_irqsave(&xhci->lock, flags);
  802. xhci_set_link_state(xhci, port_array, wIndex,
  803. XDEV_U0);
  804. }
  805. bus_state->port_c_suspend |= 1 << wIndex;
  806. slot_id = xhci_find_slot_id_by_port(hcd, xhci,
  807. wIndex + 1);
  808. if (!slot_id) {
  809. xhci_dbg(xhci, "slot_id is zero\n");
  810. goto error;
  811. }
  812. xhci_ring_device(xhci, slot_id);
  813. break;
  814. case USB_PORT_FEAT_C_SUSPEND:
  815. bus_state->port_c_suspend &= ~(1 << wIndex);
  816. case USB_PORT_FEAT_C_RESET:
  817. case USB_PORT_FEAT_C_BH_PORT_RESET:
  818. case USB_PORT_FEAT_C_CONNECTION:
  819. case USB_PORT_FEAT_C_OVER_CURRENT:
  820. case USB_PORT_FEAT_C_ENABLE:
  821. case USB_PORT_FEAT_C_PORT_LINK_STATE:
  822. xhci_clear_port_change_bit(xhci, wValue, wIndex,
  823. port_array[wIndex], temp);
  824. break;
  825. case USB_PORT_FEAT_ENABLE:
  826. xhci_disable_port(hcd, xhci, wIndex,
  827. port_array[wIndex], temp);
  828. break;
  829. default:
  830. goto error;
  831. }
  832. break;
  833. default:
  834. error:
  835. /* "stall" on error */
  836. retval = -EPIPE;
  837. }
  838. spin_unlock_irqrestore(&xhci->lock, flags);
  839. return retval;
  840. }
  841. /*
  842. * Returns 0 if the status hasn't changed, or the number of bytes in buf.
  843. * Ports are 0-indexed from the HCD point of view,
  844. * and 1-indexed from the USB core pointer of view.
  845. *
  846. * Note that the status change bits will be cleared as soon as a port status
  847. * change event is generated, so we use the saved status from that event.
  848. */
  849. int xhci_hub_status_data(struct usb_hcd *hcd, char *buf)
  850. {
  851. unsigned long flags;
  852. u32 temp, status;
  853. u32 mask;
  854. int i, retval;
  855. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  856. int max_ports;
  857. __le32 __iomem **port_array;
  858. struct xhci_bus_state *bus_state;
  859. max_ports = xhci_get_ports(hcd, &port_array);
  860. bus_state = &xhci->bus_state[hcd_index(hcd)];
  861. /* Initial status is no changes */
  862. retval = (max_ports + 8) / 8;
  863. memset(buf, 0, retval);
  864. /*
  865. * Inform the usbcore about resume-in-progress by returning
  866. * a non-zero value even if there are no status changes.
  867. */
  868. status = bus_state->resuming_ports;
  869. mask = PORT_CSC | PORT_PEC | PORT_OCC | PORT_PLC | PORT_WRC;
  870. spin_lock_irqsave(&xhci->lock, flags);
  871. /* For each port, did anything change? If so, set that bit in buf. */
  872. for (i = 0; i < max_ports; i++) {
  873. temp = xhci_readl(xhci, port_array[i]);
  874. if (temp == 0xffffffff) {
  875. retval = -ENODEV;
  876. break;
  877. }
  878. if ((temp & mask) != 0 ||
  879. (bus_state->port_c_suspend & 1 << i) ||
  880. (bus_state->resume_done[i] && time_after_eq(
  881. jiffies, bus_state->resume_done[i]))) {
  882. buf[(i + 1) / 8] |= 1 << (i + 1) % 8;
  883. status = 1;
  884. }
  885. }
  886. spin_unlock_irqrestore(&xhci->lock, flags);
  887. return status ? retval : 0;
  888. }
  889. #ifdef CONFIG_PM
  890. int xhci_bus_suspend(struct usb_hcd *hcd)
  891. {
  892. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  893. int max_ports, port_index;
  894. __le32 __iomem **port_array;
  895. struct xhci_bus_state *bus_state;
  896. unsigned long flags;
  897. max_ports = xhci_get_ports(hcd, &port_array);
  898. bus_state = &xhci->bus_state[hcd_index(hcd)];
  899. spin_lock_irqsave(&xhci->lock, flags);
  900. if (hcd->self.root_hub->do_remote_wakeup) {
  901. if (bus_state->resuming_ports) {
  902. spin_unlock_irqrestore(&xhci->lock, flags);
  903. xhci_dbg(xhci, "suspend failed because "
  904. "a port is resuming\n");
  905. return -EBUSY;
  906. }
  907. }
  908. port_index = max_ports;
  909. bus_state->bus_suspended = 0;
  910. while (port_index--) {
  911. /* suspend the port if the port is not suspended */
  912. u32 t1, t2;
  913. int slot_id;
  914. t1 = xhci_readl(xhci, port_array[port_index]);
  915. t2 = xhci_port_state_to_neutral(t1);
  916. if ((t1 & PORT_PE) && !(t1 & PORT_PLS_MASK)) {
  917. xhci_dbg(xhci, "port %d not suspended\n", port_index);
  918. slot_id = xhci_find_slot_id_by_port(hcd, xhci,
  919. port_index + 1);
  920. if (slot_id) {
  921. spin_unlock_irqrestore(&xhci->lock, flags);
  922. xhci_stop_device(xhci, slot_id, 1);
  923. spin_lock_irqsave(&xhci->lock, flags);
  924. }
  925. t2 &= ~PORT_PLS_MASK;
  926. t2 |= PORT_LINK_STROBE | XDEV_U3;
  927. set_bit(port_index, &bus_state->bus_suspended);
  928. }
  929. /* USB core sets remote wake mask for USB 3.0 hubs,
  930. * including the USB 3.0 roothub, but only if CONFIG_USB_SUSPEND
  931. * is enabled, so also enable remote wake here.
  932. */
  933. if (hcd->self.root_hub->do_remote_wakeup) {
  934. if (t1 & PORT_CONNECT) {
  935. t2 |= PORT_WKOC_E | PORT_WKDISC_E;
  936. t2 &= ~PORT_WKCONN_E;
  937. } else {
  938. t2 |= PORT_WKOC_E | PORT_WKCONN_E;
  939. t2 &= ~PORT_WKDISC_E;
  940. }
  941. } else
  942. t2 &= ~PORT_WAKE_BITS;
  943. t1 = xhci_port_state_to_neutral(t1);
  944. if (t1 != t2)
  945. xhci_writel(xhci, t2, port_array[port_index]);
  946. if (hcd->speed != HCD_USB3) {
  947. /* enable remote wake up for USB 2.0 */
  948. __le32 __iomem *addr;
  949. u32 tmp;
  950. /* Add one to the port status register address to get
  951. * the port power control register address.
  952. */
  953. addr = port_array[port_index] + 1;
  954. tmp = xhci_readl(xhci, addr);
  955. tmp |= PORT_RWE;
  956. xhci_writel(xhci, tmp, addr);
  957. }
  958. }
  959. hcd->state = HC_STATE_SUSPENDED;
  960. bus_state->next_statechange = jiffies + msecs_to_jiffies(10);
  961. spin_unlock_irqrestore(&xhci->lock, flags);
  962. return 0;
  963. }
  964. int xhci_bus_resume(struct usb_hcd *hcd)
  965. {
  966. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  967. int max_ports, port_index;
  968. __le32 __iomem **port_array;
  969. struct xhci_bus_state *bus_state;
  970. u32 temp;
  971. unsigned long flags;
  972. max_ports = xhci_get_ports(hcd, &port_array);
  973. bus_state = &xhci->bus_state[hcd_index(hcd)];
  974. if (time_before(jiffies, bus_state->next_statechange))
  975. msleep(5);
  976. spin_lock_irqsave(&xhci->lock, flags);
  977. if (!HCD_HW_ACCESSIBLE(hcd)) {
  978. spin_unlock_irqrestore(&xhci->lock, flags);
  979. return -ESHUTDOWN;
  980. }
  981. /* delay the irqs */
  982. temp = xhci_readl(xhci, &xhci->op_regs->command);
  983. temp &= ~CMD_EIE;
  984. xhci_writel(xhci, temp, &xhci->op_regs->command);
  985. port_index = max_ports;
  986. while (port_index--) {
  987. /* Check whether need resume ports. If needed
  988. resume port and disable remote wakeup */
  989. u32 temp;
  990. int slot_id;
  991. temp = xhci_readl(xhci, port_array[port_index]);
  992. if (DEV_SUPERSPEED(temp))
  993. temp &= ~(PORT_RWC_BITS | PORT_CEC | PORT_WAKE_BITS);
  994. else
  995. temp &= ~(PORT_RWC_BITS | PORT_WAKE_BITS);
  996. if (test_bit(port_index, &bus_state->bus_suspended) &&
  997. (temp & PORT_PLS_MASK)) {
  998. if (DEV_SUPERSPEED(temp)) {
  999. xhci_set_link_state(xhci, port_array,
  1000. port_index, XDEV_U0);
  1001. } else {
  1002. xhci_set_link_state(xhci, port_array,
  1003. port_index, XDEV_RESUME);
  1004. spin_unlock_irqrestore(&xhci->lock, flags);
  1005. msleep(20);
  1006. spin_lock_irqsave(&xhci->lock, flags);
  1007. xhci_set_link_state(xhci, port_array,
  1008. port_index, XDEV_U0);
  1009. }
  1010. /* wait for the port to enter U0 and report port link
  1011. * state change.
  1012. */
  1013. spin_unlock_irqrestore(&xhci->lock, flags);
  1014. msleep(20);
  1015. spin_lock_irqsave(&xhci->lock, flags);
  1016. /* Clear PLC */
  1017. xhci_test_and_clear_bit(xhci, port_array, port_index,
  1018. PORT_PLC);
  1019. slot_id = xhci_find_slot_id_by_port(hcd,
  1020. xhci, port_index + 1);
  1021. if (slot_id)
  1022. xhci_ring_device(xhci, slot_id);
  1023. } else
  1024. xhci_writel(xhci, temp, port_array[port_index]);
  1025. if (hcd->speed != HCD_USB3) {
  1026. /* disable remote wake up for USB 2.0 */
  1027. __le32 __iomem *addr;
  1028. u32 tmp;
  1029. /* Add one to the port status register address to get
  1030. * the port power control register address.
  1031. */
  1032. addr = port_array[port_index] + 1;
  1033. tmp = xhci_readl(xhci, addr);
  1034. tmp &= ~PORT_RWE;
  1035. xhci_writel(xhci, tmp, addr);
  1036. }
  1037. }
  1038. (void) xhci_readl(xhci, &xhci->op_regs->command);
  1039. bus_state->next_statechange = jiffies + msecs_to_jiffies(5);
  1040. /* re-enable irqs */
  1041. temp = xhci_readl(xhci, &xhci->op_regs->command);
  1042. temp |= CMD_EIE;
  1043. xhci_writel(xhci, temp, &xhci->op_regs->command);
  1044. temp = xhci_readl(xhci, &xhci->op_regs->command);
  1045. spin_unlock_irqrestore(&xhci->lock, flags);
  1046. return 0;
  1047. }
  1048. #endif /* CONFIG_PM */