xhci-hub.c 35 KB

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