xhci-hub.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021
  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->usb3_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. /* Test and clear port RWC bit */
  381. void xhci_test_and_clear_bit(struct xhci_hcd *xhci, __le32 __iomem **port_array,
  382. int port_id, u32 port_bit)
  383. {
  384. u32 temp;
  385. temp = xhci_readl(xhci, port_array[port_id]);
  386. if (temp & port_bit) {
  387. temp = xhci_port_state_to_neutral(temp);
  388. temp |= port_bit;
  389. xhci_writel(xhci, temp, port_array[port_id]);
  390. }
  391. }
  392. int xhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
  393. u16 wIndex, char *buf, u16 wLength)
  394. {
  395. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  396. int max_ports;
  397. unsigned long flags;
  398. u32 temp, status;
  399. int retval = 0;
  400. __le32 __iomem **port_array;
  401. int slot_id;
  402. struct xhci_bus_state *bus_state;
  403. u16 link_state = 0;
  404. max_ports = xhci_get_ports(hcd, &port_array);
  405. bus_state = &xhci->bus_state[hcd_index(hcd)];
  406. spin_lock_irqsave(&xhci->lock, flags);
  407. switch (typeReq) {
  408. case GetHubStatus:
  409. /* No power source, over-current reported per port */
  410. memset(buf, 0, 4);
  411. break;
  412. case GetHubDescriptor:
  413. /* Check to make sure userspace is asking for the USB 3.0 hub
  414. * descriptor for the USB 3.0 roothub. If not, we stall the
  415. * endpoint, like external hubs do.
  416. */
  417. if (hcd->speed == HCD_USB3 &&
  418. (wLength < USB_DT_SS_HUB_SIZE ||
  419. wValue != (USB_DT_SS_HUB << 8))) {
  420. xhci_dbg(xhci, "Wrong hub descriptor type for "
  421. "USB 3.0 roothub.\n");
  422. goto error;
  423. }
  424. xhci_hub_descriptor(hcd, xhci,
  425. (struct usb_hub_descriptor *) buf);
  426. break;
  427. case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
  428. if ((wValue & 0xff00) != (USB_DT_BOS << 8))
  429. goto error;
  430. if (hcd->speed != HCD_USB3)
  431. goto error;
  432. memcpy(buf, &usb_bos_descriptor,
  433. USB_DT_BOS_SIZE + USB_DT_USB_SS_CAP_SIZE);
  434. temp = xhci_readl(xhci, &xhci->cap_regs->hcs_params3);
  435. buf[12] = HCS_U1_LATENCY(temp);
  436. put_unaligned_le16(HCS_U2_LATENCY(temp), &buf[13]);
  437. spin_unlock_irqrestore(&xhci->lock, flags);
  438. return USB_DT_BOS_SIZE + USB_DT_USB_SS_CAP_SIZE;
  439. case GetPortStatus:
  440. if (!wIndex || wIndex > max_ports)
  441. goto error;
  442. wIndex--;
  443. status = 0;
  444. temp = xhci_readl(xhci, port_array[wIndex]);
  445. if (temp == 0xffffffff) {
  446. retval = -ENODEV;
  447. break;
  448. }
  449. xhci_dbg(xhci, "get port status, actual port %d status = 0x%x\n", wIndex, temp);
  450. /* wPortChange bits */
  451. if (temp & PORT_CSC)
  452. status |= USB_PORT_STAT_C_CONNECTION << 16;
  453. if (temp & PORT_PEC)
  454. status |= USB_PORT_STAT_C_ENABLE << 16;
  455. if ((temp & PORT_OCC))
  456. status |= USB_PORT_STAT_C_OVERCURRENT << 16;
  457. if ((temp & PORT_RC))
  458. status |= USB_PORT_STAT_C_RESET << 16;
  459. /* USB3.0 only */
  460. if (hcd->speed == HCD_USB3) {
  461. if ((temp & PORT_PLC))
  462. status |= USB_PORT_STAT_C_LINK_STATE << 16;
  463. if ((temp & PORT_WRC))
  464. status |= USB_PORT_STAT_C_BH_RESET << 16;
  465. }
  466. if (hcd->speed != HCD_USB3) {
  467. if ((temp & PORT_PLS_MASK) == XDEV_U3
  468. && (temp & PORT_POWER))
  469. status |= USB_PORT_STAT_SUSPEND;
  470. }
  471. if ((temp & PORT_PLS_MASK) == XDEV_RESUME &&
  472. !DEV_SUPERSPEED(temp)) {
  473. if ((temp & PORT_RESET) || !(temp & PORT_PE))
  474. goto error;
  475. if (time_after_eq(jiffies,
  476. bus_state->resume_done[wIndex])) {
  477. xhci_dbg(xhci, "Resume USB2 port %d\n",
  478. wIndex + 1);
  479. bus_state->resume_done[wIndex] = 0;
  480. xhci_set_link_state(xhci, port_array, wIndex,
  481. XDEV_U0);
  482. xhci_dbg(xhci, "set port %d resume\n",
  483. wIndex + 1);
  484. slot_id = xhci_find_slot_id_by_port(hcd, xhci,
  485. wIndex + 1);
  486. if (!slot_id) {
  487. xhci_dbg(xhci, "slot_id is zero\n");
  488. goto error;
  489. }
  490. xhci_ring_device(xhci, slot_id);
  491. bus_state->port_c_suspend |= 1 << wIndex;
  492. bus_state->suspended_ports &= ~(1 << wIndex);
  493. } else {
  494. /*
  495. * The resume has been signaling for less than
  496. * 20ms. Report the port status as SUSPEND,
  497. * let the usbcore check port status again
  498. * and clear resume signaling later.
  499. */
  500. status |= USB_PORT_STAT_SUSPEND;
  501. }
  502. }
  503. if ((temp & PORT_PLS_MASK) == XDEV_U0
  504. && (temp & PORT_POWER)
  505. && (bus_state->suspended_ports & (1 << wIndex))) {
  506. bus_state->suspended_ports &= ~(1 << wIndex);
  507. if (hcd->speed != HCD_USB3)
  508. bus_state->port_c_suspend |= 1 << wIndex;
  509. }
  510. if (temp & PORT_CONNECT) {
  511. status |= USB_PORT_STAT_CONNECTION;
  512. status |= xhci_port_speed(temp);
  513. }
  514. if (temp & PORT_PE)
  515. status |= USB_PORT_STAT_ENABLE;
  516. if (temp & PORT_OC)
  517. status |= USB_PORT_STAT_OVERCURRENT;
  518. if (temp & PORT_RESET)
  519. status |= USB_PORT_STAT_RESET;
  520. if (temp & PORT_POWER) {
  521. if (hcd->speed == HCD_USB3)
  522. status |= USB_SS_PORT_STAT_POWER;
  523. else
  524. status |= USB_PORT_STAT_POWER;
  525. }
  526. /* Port Link State */
  527. if (hcd->speed == HCD_USB3) {
  528. /* resume state is a xHCI internal state.
  529. * Do not report it to usb core.
  530. */
  531. if ((temp & PORT_PLS_MASK) != XDEV_RESUME)
  532. status |= (temp & PORT_PLS_MASK);
  533. }
  534. if (bus_state->port_c_suspend & (1 << wIndex))
  535. status |= 1 << USB_PORT_FEAT_C_SUSPEND;
  536. xhci_dbg(xhci, "Get port status returned 0x%x\n", status);
  537. put_unaligned(cpu_to_le32(status), (__le32 *) buf);
  538. break;
  539. case SetPortFeature:
  540. if (wValue == USB_PORT_FEAT_LINK_STATE)
  541. link_state = (wIndex & 0xff00) >> 3;
  542. wIndex &= 0xff;
  543. if (!wIndex || wIndex > max_ports)
  544. goto error;
  545. wIndex--;
  546. temp = xhci_readl(xhci, port_array[wIndex]);
  547. if (temp == 0xffffffff) {
  548. retval = -ENODEV;
  549. break;
  550. }
  551. temp = xhci_port_state_to_neutral(temp);
  552. /* FIXME: What new port features do we need to support? */
  553. switch (wValue) {
  554. case USB_PORT_FEAT_SUSPEND:
  555. temp = xhci_readl(xhci, port_array[wIndex]);
  556. if ((temp & PORT_PLS_MASK) != XDEV_U0) {
  557. /* Resume the port to U0 first */
  558. xhci_set_link_state(xhci, port_array, wIndex,
  559. XDEV_U0);
  560. spin_unlock_irqrestore(&xhci->lock, flags);
  561. msleep(10);
  562. spin_lock_irqsave(&xhci->lock, flags);
  563. }
  564. /* In spec software should not attempt to suspend
  565. * a port unless the port reports that it is in the
  566. * enabled (PED = ‘1’,PLS < ‘3’) state.
  567. */
  568. temp = xhci_readl(xhci, port_array[wIndex]);
  569. if ((temp & PORT_PE) == 0 || (temp & PORT_RESET)
  570. || (temp & PORT_PLS_MASK) >= XDEV_U3) {
  571. xhci_warn(xhci, "USB core suspending device "
  572. "not in U0/U1/U2.\n");
  573. goto error;
  574. }
  575. slot_id = xhci_find_slot_id_by_port(hcd, xhci,
  576. wIndex + 1);
  577. if (!slot_id) {
  578. xhci_warn(xhci, "slot_id is zero\n");
  579. goto error;
  580. }
  581. /* unlock to execute stop endpoint commands */
  582. spin_unlock_irqrestore(&xhci->lock, flags);
  583. xhci_stop_device(xhci, slot_id, 1);
  584. spin_lock_irqsave(&xhci->lock, flags);
  585. xhci_set_link_state(xhci, port_array, wIndex, XDEV_U3);
  586. spin_unlock_irqrestore(&xhci->lock, flags);
  587. msleep(10); /* wait device to enter */
  588. spin_lock_irqsave(&xhci->lock, flags);
  589. temp = xhci_readl(xhci, port_array[wIndex]);
  590. bus_state->suspended_ports |= 1 << wIndex;
  591. break;
  592. case USB_PORT_FEAT_LINK_STATE:
  593. temp = xhci_readl(xhci, port_array[wIndex]);
  594. /* Software should not attempt to set
  595. * port link state above '5' (Rx.Detect) and the port
  596. * must be enabled.
  597. */
  598. if ((temp & PORT_PE) == 0 ||
  599. (link_state > USB_SS_PORT_LS_RX_DETECT)) {
  600. xhci_warn(xhci, "Cannot set link state.\n");
  601. goto error;
  602. }
  603. if (link_state == USB_SS_PORT_LS_U3) {
  604. slot_id = xhci_find_slot_id_by_port(hcd, xhci,
  605. wIndex + 1);
  606. if (slot_id) {
  607. /* unlock to execute stop endpoint
  608. * commands */
  609. spin_unlock_irqrestore(&xhci->lock,
  610. flags);
  611. xhci_stop_device(xhci, slot_id, 1);
  612. spin_lock_irqsave(&xhci->lock, flags);
  613. }
  614. }
  615. xhci_set_link_state(xhci, port_array, wIndex,
  616. link_state);
  617. spin_unlock_irqrestore(&xhci->lock, flags);
  618. msleep(20); /* wait device to enter */
  619. spin_lock_irqsave(&xhci->lock, flags);
  620. temp = xhci_readl(xhci, port_array[wIndex]);
  621. if (link_state == USB_SS_PORT_LS_U3)
  622. bus_state->suspended_ports |= 1 << wIndex;
  623. break;
  624. case USB_PORT_FEAT_POWER:
  625. /*
  626. * Turn on ports, even if there isn't per-port switching.
  627. * HC will report connect events even before this is set.
  628. * However, khubd will ignore the roothub events until
  629. * the roothub is registered.
  630. */
  631. xhci_writel(xhci, temp | PORT_POWER,
  632. port_array[wIndex]);
  633. temp = xhci_readl(xhci, port_array[wIndex]);
  634. xhci_dbg(xhci, "set port power, actual port %d status = 0x%x\n", wIndex, temp);
  635. break;
  636. case USB_PORT_FEAT_RESET:
  637. temp = (temp | PORT_RESET);
  638. xhci_writel(xhci, temp, port_array[wIndex]);
  639. temp = xhci_readl(xhci, port_array[wIndex]);
  640. xhci_dbg(xhci, "set port reset, actual port %d status = 0x%x\n", wIndex, temp);
  641. break;
  642. case USB_PORT_FEAT_BH_PORT_RESET:
  643. temp |= PORT_WR;
  644. xhci_writel(xhci, temp, port_array[wIndex]);
  645. temp = xhci_readl(xhci, port_array[wIndex]);
  646. break;
  647. default:
  648. goto error;
  649. }
  650. /* unblock any posted writes */
  651. temp = xhci_readl(xhci, port_array[wIndex]);
  652. break;
  653. case ClearPortFeature:
  654. if (!wIndex || wIndex > max_ports)
  655. goto error;
  656. wIndex--;
  657. temp = xhci_readl(xhci, port_array[wIndex]);
  658. if (temp == 0xffffffff) {
  659. retval = -ENODEV;
  660. break;
  661. }
  662. /* FIXME: What new port features do we need to support? */
  663. temp = xhci_port_state_to_neutral(temp);
  664. switch (wValue) {
  665. case USB_PORT_FEAT_SUSPEND:
  666. temp = xhci_readl(xhci, port_array[wIndex]);
  667. xhci_dbg(xhci, "clear USB_PORT_FEAT_SUSPEND\n");
  668. xhci_dbg(xhci, "PORTSC %04x\n", temp);
  669. if (temp & PORT_RESET)
  670. goto error;
  671. if ((temp & PORT_PLS_MASK) == XDEV_U3) {
  672. if ((temp & PORT_PE) == 0)
  673. goto error;
  674. xhci_set_link_state(xhci, port_array, wIndex,
  675. XDEV_RESUME);
  676. spin_unlock_irqrestore(&xhci->lock, flags);
  677. msleep(20);
  678. spin_lock_irqsave(&xhci->lock, flags);
  679. xhci_set_link_state(xhci, port_array, wIndex,
  680. XDEV_U0);
  681. }
  682. bus_state->port_c_suspend |= 1 << wIndex;
  683. slot_id = xhci_find_slot_id_by_port(hcd, xhci,
  684. wIndex + 1);
  685. if (!slot_id) {
  686. xhci_dbg(xhci, "slot_id is zero\n");
  687. goto error;
  688. }
  689. xhci_ring_device(xhci, slot_id);
  690. break;
  691. case USB_PORT_FEAT_C_SUSPEND:
  692. bus_state->port_c_suspend &= ~(1 << wIndex);
  693. case USB_PORT_FEAT_C_RESET:
  694. case USB_PORT_FEAT_C_BH_PORT_RESET:
  695. case USB_PORT_FEAT_C_CONNECTION:
  696. case USB_PORT_FEAT_C_OVER_CURRENT:
  697. case USB_PORT_FEAT_C_ENABLE:
  698. case USB_PORT_FEAT_C_PORT_LINK_STATE:
  699. xhci_clear_port_change_bit(xhci, wValue, wIndex,
  700. port_array[wIndex], temp);
  701. break;
  702. case USB_PORT_FEAT_ENABLE:
  703. xhci_disable_port(hcd, xhci, wIndex,
  704. port_array[wIndex], temp);
  705. break;
  706. default:
  707. goto error;
  708. }
  709. break;
  710. default:
  711. error:
  712. /* "stall" on error */
  713. retval = -EPIPE;
  714. }
  715. spin_unlock_irqrestore(&xhci->lock, flags);
  716. return retval;
  717. }
  718. /*
  719. * Returns 0 if the status hasn't changed, or the number of bytes in buf.
  720. * Ports are 0-indexed from the HCD point of view,
  721. * and 1-indexed from the USB core pointer of view.
  722. *
  723. * Note that the status change bits will be cleared as soon as a port status
  724. * change event is generated, so we use the saved status from that event.
  725. */
  726. int xhci_hub_status_data(struct usb_hcd *hcd, char *buf)
  727. {
  728. unsigned long flags;
  729. u32 temp, status;
  730. u32 mask;
  731. int i, retval;
  732. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  733. int max_ports;
  734. __le32 __iomem **port_array;
  735. struct xhci_bus_state *bus_state;
  736. max_ports = xhci_get_ports(hcd, &port_array);
  737. bus_state = &xhci->bus_state[hcd_index(hcd)];
  738. /* Initial status is no changes */
  739. retval = (max_ports + 8) / 8;
  740. memset(buf, 0, retval);
  741. status = 0;
  742. mask = PORT_CSC | PORT_PEC | PORT_OCC | PORT_PLC | PORT_WRC;
  743. spin_lock_irqsave(&xhci->lock, flags);
  744. /* For each port, did anything change? If so, set that bit in buf. */
  745. for (i = 0; i < max_ports; i++) {
  746. temp = xhci_readl(xhci, port_array[i]);
  747. if (temp == 0xffffffff) {
  748. retval = -ENODEV;
  749. break;
  750. }
  751. if ((temp & mask) != 0 ||
  752. (bus_state->port_c_suspend & 1 << i) ||
  753. (bus_state->resume_done[i] && time_after_eq(
  754. jiffies, bus_state->resume_done[i]))) {
  755. buf[(i + 1) / 8] |= 1 << (i + 1) % 8;
  756. status = 1;
  757. }
  758. }
  759. spin_unlock_irqrestore(&xhci->lock, flags);
  760. return status ? retval : 0;
  761. }
  762. #ifdef CONFIG_PM
  763. int xhci_bus_suspend(struct usb_hcd *hcd)
  764. {
  765. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  766. int max_ports, port_index;
  767. __le32 __iomem **port_array;
  768. struct xhci_bus_state *bus_state;
  769. unsigned long flags;
  770. max_ports = xhci_get_ports(hcd, &port_array);
  771. bus_state = &xhci->bus_state[hcd_index(hcd)];
  772. spin_lock_irqsave(&xhci->lock, flags);
  773. if (hcd->self.root_hub->do_remote_wakeup) {
  774. port_index = max_ports;
  775. while (port_index--) {
  776. if (bus_state->resume_done[port_index] != 0) {
  777. spin_unlock_irqrestore(&xhci->lock, flags);
  778. xhci_dbg(xhci, "suspend failed because "
  779. "port %d is resuming\n",
  780. port_index + 1);
  781. return -EBUSY;
  782. }
  783. }
  784. }
  785. port_index = max_ports;
  786. bus_state->bus_suspended = 0;
  787. while (port_index--) {
  788. /* suspend the port if the port is not suspended */
  789. u32 t1, t2;
  790. int slot_id;
  791. t1 = xhci_readl(xhci, port_array[port_index]);
  792. t2 = xhci_port_state_to_neutral(t1);
  793. if ((t1 & PORT_PE) && !(t1 & PORT_PLS_MASK)) {
  794. xhci_dbg(xhci, "port %d not suspended\n", port_index);
  795. slot_id = xhci_find_slot_id_by_port(hcd, xhci,
  796. port_index + 1);
  797. if (slot_id) {
  798. spin_unlock_irqrestore(&xhci->lock, flags);
  799. xhci_stop_device(xhci, slot_id, 1);
  800. spin_lock_irqsave(&xhci->lock, flags);
  801. }
  802. t2 &= ~PORT_PLS_MASK;
  803. t2 |= PORT_LINK_STROBE | XDEV_U3;
  804. set_bit(port_index, &bus_state->bus_suspended);
  805. }
  806. if (hcd->self.root_hub->do_remote_wakeup) {
  807. if (t1 & PORT_CONNECT) {
  808. t2 |= PORT_WKOC_E | PORT_WKDISC_E;
  809. t2 &= ~PORT_WKCONN_E;
  810. } else {
  811. t2 |= PORT_WKOC_E | PORT_WKCONN_E;
  812. t2 &= ~PORT_WKDISC_E;
  813. }
  814. } else
  815. t2 &= ~PORT_WAKE_BITS;
  816. t1 = xhci_port_state_to_neutral(t1);
  817. if (t1 != t2)
  818. xhci_writel(xhci, t2, port_array[port_index]);
  819. if (hcd->speed != HCD_USB3) {
  820. /* enable remote wake up for USB 2.0 */
  821. __le32 __iomem *addr;
  822. u32 tmp;
  823. /* Add one to the port status register address to get
  824. * the port power control register address.
  825. */
  826. addr = port_array[port_index] + 1;
  827. tmp = xhci_readl(xhci, addr);
  828. tmp |= PORT_RWE;
  829. xhci_writel(xhci, tmp, addr);
  830. }
  831. }
  832. hcd->state = HC_STATE_SUSPENDED;
  833. bus_state->next_statechange = jiffies + msecs_to_jiffies(10);
  834. spin_unlock_irqrestore(&xhci->lock, flags);
  835. return 0;
  836. }
  837. int xhci_bus_resume(struct usb_hcd *hcd)
  838. {
  839. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  840. int max_ports, port_index;
  841. __le32 __iomem **port_array;
  842. struct xhci_bus_state *bus_state;
  843. u32 temp;
  844. unsigned long flags;
  845. max_ports = xhci_get_ports(hcd, &port_array);
  846. bus_state = &xhci->bus_state[hcd_index(hcd)];
  847. if (time_before(jiffies, bus_state->next_statechange))
  848. msleep(5);
  849. spin_lock_irqsave(&xhci->lock, flags);
  850. if (!HCD_HW_ACCESSIBLE(hcd)) {
  851. spin_unlock_irqrestore(&xhci->lock, flags);
  852. return -ESHUTDOWN;
  853. }
  854. /* delay the irqs */
  855. temp = xhci_readl(xhci, &xhci->op_regs->command);
  856. temp &= ~CMD_EIE;
  857. xhci_writel(xhci, temp, &xhci->op_regs->command);
  858. port_index = max_ports;
  859. while (port_index--) {
  860. /* Check whether need resume ports. If needed
  861. resume port and disable remote wakeup */
  862. u32 temp;
  863. int slot_id;
  864. temp = xhci_readl(xhci, port_array[port_index]);
  865. if (DEV_SUPERSPEED(temp))
  866. temp &= ~(PORT_RWC_BITS | PORT_CEC | PORT_WAKE_BITS);
  867. else
  868. temp &= ~(PORT_RWC_BITS | PORT_WAKE_BITS);
  869. if (test_bit(port_index, &bus_state->bus_suspended) &&
  870. (temp & PORT_PLS_MASK)) {
  871. if (DEV_SUPERSPEED(temp)) {
  872. xhci_set_link_state(xhci, port_array,
  873. port_index, XDEV_U0);
  874. } else {
  875. xhci_set_link_state(xhci, port_array,
  876. port_index, XDEV_RESUME);
  877. spin_unlock_irqrestore(&xhci->lock, flags);
  878. msleep(20);
  879. spin_lock_irqsave(&xhci->lock, flags);
  880. xhci_set_link_state(xhci, port_array,
  881. port_index, XDEV_U0);
  882. }
  883. /* wait for the port to enter U0 and report port link
  884. * state change.
  885. */
  886. spin_unlock_irqrestore(&xhci->lock, flags);
  887. msleep(20);
  888. spin_lock_irqsave(&xhci->lock, flags);
  889. /* Clear PLC */
  890. xhci_test_and_clear_bit(xhci, port_array, port_index,
  891. PORT_PLC);
  892. slot_id = xhci_find_slot_id_by_port(hcd,
  893. xhci, port_index + 1);
  894. if (slot_id)
  895. xhci_ring_device(xhci, slot_id);
  896. } else
  897. xhci_writel(xhci, temp, port_array[port_index]);
  898. if (hcd->speed != HCD_USB3) {
  899. /* disable remote wake up for USB 2.0 */
  900. __le32 __iomem *addr;
  901. u32 tmp;
  902. /* Add one to the port status register address to get
  903. * the port power control register address.
  904. */
  905. addr = port_array[port_index] + 1;
  906. tmp = xhci_readl(xhci, addr);
  907. tmp &= ~PORT_RWE;
  908. xhci_writel(xhci, tmp, addr);
  909. }
  910. }
  911. (void) xhci_readl(xhci, &xhci->op_regs->command);
  912. bus_state->next_statechange = jiffies + msecs_to_jiffies(5);
  913. /* re-enable irqs */
  914. temp = xhci_readl(xhci, &xhci->op_regs->command);
  915. temp |= CMD_EIE;
  916. xhci_writel(xhci, temp, &xhci->op_regs->command);
  917. temp = xhci_readl(xhci, &xhci->op_regs->command);
  918. spin_unlock_irqrestore(&xhci->lock, flags);
  919. return 0;
  920. }
  921. #endif /* CONFIG_PM */