xhci-hub.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023
  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. /* Ugh, these should be #defines, FIXME */
  53. /* Using table 11-13 in USB 2.0 spec. */
  54. temp = 0;
  55. /* Bits 1:0 - support port power switching, or power always on */
  56. if (HCC_PPC(xhci->hcc_params))
  57. temp |= 0x0001;
  58. else
  59. temp |= 0x0002;
  60. /* Bit 2 - root hubs are not part of a compound device */
  61. /* Bits 4:3 - individual port over current protection */
  62. temp |= 0x0008;
  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 = 0x29;
  79. temp = 1 + (ports / 8);
  80. desc->bDescLength = 7 + 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 = xhci_readl(xhci, xhci->usb3_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 = 0x2a;
  125. desc->bDescLength = 12;
  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 = xhci_readl(xhci, xhci->usb3_ports[i]);
  135. if (portsc & PORT_DEV_REMOVE)
  136. port_removable |= 1 << (i + 1);
  137. }
  138. memset(&desc->u.ss.DeviceRemovable,
  139. (__force __u16) cpu_to_le16(port_removable),
  140. sizeof(__u16));
  141. }
  142. static void xhci_hub_descriptor(struct usb_hcd *hcd, struct xhci_hcd *xhci,
  143. struct usb_hub_descriptor *desc)
  144. {
  145. if (hcd->speed == HCD_USB3)
  146. xhci_usb3_hub_descriptor(hcd, xhci, desc);
  147. else
  148. xhci_usb2_hub_descriptor(hcd, xhci, desc);
  149. }
  150. static unsigned int xhci_port_speed(unsigned int port_status)
  151. {
  152. if (DEV_LOWSPEED(port_status))
  153. return USB_PORT_STAT_LOW_SPEED;
  154. if (DEV_HIGHSPEED(port_status))
  155. return USB_PORT_STAT_HIGH_SPEED;
  156. /*
  157. * FIXME: Yes, we should check for full speed, but the core uses that as
  158. * a default in portspeed() in usb/core/hub.c (which is the only place
  159. * USB_PORT_STAT_*_SPEED is used).
  160. */
  161. return 0;
  162. }
  163. /*
  164. * These bits are Read Only (RO) and should be saved and written to the
  165. * registers: 0, 3, 10:13, 30
  166. * connect status, over-current status, port speed, and device removable.
  167. * connect status and port speed are also sticky - meaning they're in
  168. * the AUX well and they aren't changed by a hot, warm, or cold reset.
  169. */
  170. #define XHCI_PORT_RO ((1<<0) | (1<<3) | (0xf<<10) | (1<<30))
  171. /*
  172. * These bits are RW; writing a 0 clears the bit, writing a 1 sets the bit:
  173. * bits 5:8, 9, 14:15, 25:27
  174. * link state, port power, port indicator state, "wake on" enable state
  175. */
  176. #define XHCI_PORT_RWS ((0xf<<5) | (1<<9) | (0x3<<14) | (0x7<<25))
  177. /*
  178. * These bits are RW; writing a 1 sets the bit, writing a 0 has no effect:
  179. * bit 4 (port reset)
  180. */
  181. #define XHCI_PORT_RW1S ((1<<4))
  182. /*
  183. * These bits are RW; writing a 1 clears the bit, writing a 0 has no effect:
  184. * bits 1, 17, 18, 19, 20, 21, 22, 23
  185. * port enable/disable, and
  186. * change bits: connect, PED, warm port reset changed (reserved zero for USB 2.0 ports),
  187. * over-current, reset, link state, and L1 change
  188. */
  189. #define XHCI_PORT_RW1CS ((1<<1) | (0x7f<<17))
  190. /*
  191. * Bit 16 is RW, and writing a '1' to it causes the link state control to be
  192. * latched in
  193. */
  194. #define XHCI_PORT_RW ((1<<16))
  195. /*
  196. * These bits are Reserved Zero (RsvdZ) and zero should be written to them:
  197. * bits 2, 24, 28:31
  198. */
  199. #define XHCI_PORT_RZ ((1<<2) | (1<<24) | (0xf<<28))
  200. /*
  201. * Given a port state, this function returns a value that would result in the
  202. * port being in the same state, if the value was written to the port status
  203. * control register.
  204. * Save Read Only (RO) bits and save read/write bits where
  205. * writing a 0 clears the bit and writing a 1 sets the bit (RWS).
  206. * For all other types (RW1S, RW1CS, RW, and RZ), writing a '0' has no effect.
  207. */
  208. u32 xhci_port_state_to_neutral(u32 state)
  209. {
  210. /* Save read-only status and port state */
  211. return (state & XHCI_PORT_RO) | (state & XHCI_PORT_RWS);
  212. }
  213. /*
  214. * find slot id based on port number.
  215. * @port: The one-based port number from one of the two split roothubs.
  216. */
  217. int xhci_find_slot_id_by_port(struct usb_hcd *hcd, struct xhci_hcd *xhci,
  218. u16 port)
  219. {
  220. int slot_id;
  221. int i;
  222. enum usb_device_speed speed;
  223. slot_id = 0;
  224. for (i = 0; i < MAX_HC_SLOTS; i++) {
  225. if (!xhci->devs[i])
  226. continue;
  227. speed = xhci->devs[i]->udev->speed;
  228. if (((speed == USB_SPEED_SUPER) == (hcd->speed == HCD_USB3))
  229. && xhci->devs[i]->fake_port == port) {
  230. slot_id = i;
  231. break;
  232. }
  233. }
  234. return slot_id;
  235. }
  236. /*
  237. * Stop device
  238. * It issues stop endpoint command for EP 0 to 30. And wait the last command
  239. * to complete.
  240. * suspend will set to 1, if suspend bit need to set in command.
  241. */
  242. static int xhci_stop_device(struct xhci_hcd *xhci, int slot_id, int suspend)
  243. {
  244. struct xhci_virt_device *virt_dev;
  245. struct xhci_command *cmd;
  246. unsigned long flags;
  247. int timeleft;
  248. int ret;
  249. int i;
  250. ret = 0;
  251. virt_dev = xhci->devs[slot_id];
  252. cmd = xhci_alloc_command(xhci, false, true, GFP_NOIO);
  253. if (!cmd) {
  254. xhci_dbg(xhci, "Couldn't allocate command structure.\n");
  255. return -ENOMEM;
  256. }
  257. spin_lock_irqsave(&xhci->lock, flags);
  258. for (i = LAST_EP_INDEX; i > 0; i--) {
  259. if (virt_dev->eps[i].ring && virt_dev->eps[i].ring->dequeue)
  260. xhci_queue_stop_endpoint(xhci, slot_id, i, suspend);
  261. }
  262. cmd->command_trb = xhci->cmd_ring->enqueue;
  263. list_add_tail(&cmd->cmd_list, &virt_dev->cmd_list);
  264. xhci_queue_stop_endpoint(xhci, slot_id, 0, suspend);
  265. xhci_ring_cmd_db(xhci);
  266. spin_unlock_irqrestore(&xhci->lock, flags);
  267. /* Wait for last stop endpoint command to finish */
  268. timeleft = wait_for_completion_interruptible_timeout(
  269. cmd->completion,
  270. USB_CTRL_SET_TIMEOUT);
  271. if (timeleft <= 0) {
  272. xhci_warn(xhci, "%s while waiting for stop endpoint command\n",
  273. timeleft == 0 ? "Timeout" : "Signal");
  274. spin_lock_irqsave(&xhci->lock, flags);
  275. /* The timeout might have raced with the event ring handler, so
  276. * only delete from the list if the item isn't poisoned.
  277. */
  278. if (cmd->cmd_list.next != LIST_POISON1)
  279. list_del(&cmd->cmd_list);
  280. spin_unlock_irqrestore(&xhci->lock, flags);
  281. ret = -ETIME;
  282. goto command_cleanup;
  283. }
  284. command_cleanup:
  285. xhci_free_command(xhci, cmd);
  286. return ret;
  287. }
  288. /*
  289. * Ring device, it rings the all doorbells unconditionally.
  290. */
  291. void xhci_ring_device(struct xhci_hcd *xhci, int slot_id)
  292. {
  293. int i;
  294. for (i = 0; i < LAST_EP_INDEX + 1; i++)
  295. if (xhci->devs[slot_id]->eps[i].ring &&
  296. xhci->devs[slot_id]->eps[i].ring->dequeue)
  297. xhci_ring_ep_doorbell(xhci, slot_id, i, 0);
  298. return;
  299. }
  300. static void xhci_disable_port(struct usb_hcd *hcd, struct xhci_hcd *xhci,
  301. u16 wIndex, __le32 __iomem *addr, u32 port_status)
  302. {
  303. /* Don't allow the USB core to disable SuperSpeed ports. */
  304. if (hcd->speed == HCD_USB3) {
  305. xhci_dbg(xhci, "Ignoring request to disable "
  306. "SuperSpeed port.\n");
  307. return;
  308. }
  309. /* Write 1 to disable the port */
  310. xhci_writel(xhci, port_status | PORT_PE, addr);
  311. port_status = xhci_readl(xhci, addr);
  312. xhci_dbg(xhci, "disable port, actual port %d status = 0x%x\n",
  313. wIndex, port_status);
  314. }
  315. static void xhci_clear_port_change_bit(struct xhci_hcd *xhci, u16 wValue,
  316. u16 wIndex, __le32 __iomem *addr, u32 port_status)
  317. {
  318. char *port_change_bit;
  319. u32 status;
  320. switch (wValue) {
  321. case USB_PORT_FEAT_C_RESET:
  322. status = PORT_RC;
  323. port_change_bit = "reset";
  324. break;
  325. case USB_PORT_FEAT_C_BH_PORT_RESET:
  326. status = PORT_WRC;
  327. port_change_bit = "warm(BH) reset";
  328. break;
  329. case USB_PORT_FEAT_C_CONNECTION:
  330. status = PORT_CSC;
  331. port_change_bit = "connect";
  332. break;
  333. case USB_PORT_FEAT_C_OVER_CURRENT:
  334. status = PORT_OCC;
  335. port_change_bit = "over-current";
  336. break;
  337. case USB_PORT_FEAT_C_ENABLE:
  338. status = PORT_PEC;
  339. port_change_bit = "enable/disable";
  340. break;
  341. case USB_PORT_FEAT_C_SUSPEND:
  342. status = PORT_PLC;
  343. port_change_bit = "suspend/resume";
  344. break;
  345. case USB_PORT_FEAT_C_PORT_LINK_STATE:
  346. status = PORT_PLC;
  347. port_change_bit = "link state";
  348. break;
  349. default:
  350. /* Should never happen */
  351. return;
  352. }
  353. /* Change bits are all write 1 to clear */
  354. xhci_writel(xhci, port_status | status, addr);
  355. port_status = xhci_readl(xhci, addr);
  356. xhci_dbg(xhci, "clear port %s change, actual port %d status = 0x%x\n",
  357. port_change_bit, wIndex, port_status);
  358. }
  359. static int xhci_get_ports(struct usb_hcd *hcd, __le32 __iomem ***port_array)
  360. {
  361. int max_ports;
  362. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  363. if (hcd->speed == HCD_USB3) {
  364. max_ports = xhci->num_usb3_ports;
  365. *port_array = xhci->usb3_ports;
  366. } else {
  367. max_ports = xhci->num_usb2_ports;
  368. *port_array = xhci->usb2_ports;
  369. }
  370. return max_ports;
  371. }
  372. void xhci_set_link_state(struct xhci_hcd *xhci, __le32 __iomem **port_array,
  373. int port_id, u32 link_state)
  374. {
  375. u32 temp;
  376. temp = xhci_readl(xhci, port_array[port_id]);
  377. temp = xhci_port_state_to_neutral(temp);
  378. temp &= ~PORT_PLS_MASK;
  379. temp |= PORT_LINK_STROBE | link_state;
  380. xhci_writel(xhci, temp, port_array[port_id]);
  381. }
  382. /* Test and clear port RWC bit */
  383. void xhci_test_and_clear_bit(struct xhci_hcd *xhci, __le32 __iomem **port_array,
  384. int port_id, u32 port_bit)
  385. {
  386. u32 temp;
  387. temp = xhci_readl(xhci, port_array[port_id]);
  388. if (temp & port_bit) {
  389. temp = xhci_port_state_to_neutral(temp);
  390. temp |= port_bit;
  391. xhci_writel(xhci, temp, port_array[port_id]);
  392. }
  393. }
  394. int xhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
  395. u16 wIndex, char *buf, u16 wLength)
  396. {
  397. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  398. int max_ports;
  399. unsigned long flags;
  400. u32 temp, status;
  401. int retval = 0;
  402. __le32 __iomem **port_array;
  403. int slot_id;
  404. struct xhci_bus_state *bus_state;
  405. u16 link_state = 0;
  406. max_ports = xhci_get_ports(hcd, &port_array);
  407. bus_state = &xhci->bus_state[hcd_index(hcd)];
  408. spin_lock_irqsave(&xhci->lock, flags);
  409. switch (typeReq) {
  410. case GetHubStatus:
  411. /* No power source, over-current reported per port */
  412. memset(buf, 0, 4);
  413. break;
  414. case GetHubDescriptor:
  415. /* Check to make sure userspace is asking for the USB 3.0 hub
  416. * descriptor for the USB 3.0 roothub. If not, we stall the
  417. * endpoint, like external hubs do.
  418. */
  419. if (hcd->speed == HCD_USB3 &&
  420. (wLength < USB_DT_SS_HUB_SIZE ||
  421. wValue != (USB_DT_SS_HUB << 8))) {
  422. xhci_dbg(xhci, "Wrong hub descriptor type for "
  423. "USB 3.0 roothub.\n");
  424. goto error;
  425. }
  426. xhci_hub_descriptor(hcd, xhci,
  427. (struct usb_hub_descriptor *) buf);
  428. break;
  429. case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
  430. if ((wValue & 0xff00) != (USB_DT_BOS << 8))
  431. goto error;
  432. if (hcd->speed != HCD_USB3)
  433. goto error;
  434. memcpy(buf, &usb_bos_descriptor,
  435. USB_DT_BOS_SIZE + USB_DT_USB_SS_CAP_SIZE);
  436. temp = xhci_readl(xhci, &xhci->cap_regs->hcs_params3);
  437. buf[12] = HCS_U1_LATENCY(temp);
  438. put_unaligned_le16(HCS_U2_LATENCY(temp), &buf[13]);
  439. spin_unlock_irqrestore(&xhci->lock, flags);
  440. return USB_DT_BOS_SIZE + USB_DT_USB_SS_CAP_SIZE;
  441. case GetPortStatus:
  442. if (!wIndex || wIndex > max_ports)
  443. goto error;
  444. wIndex--;
  445. status = 0;
  446. temp = xhci_readl(xhci, port_array[wIndex]);
  447. if (temp == 0xffffffff) {
  448. retval = -ENODEV;
  449. break;
  450. }
  451. xhci_dbg(xhci, "get port status, actual port %d status = 0x%x\n", wIndex, temp);
  452. /* wPortChange bits */
  453. if (temp & PORT_CSC)
  454. status |= USB_PORT_STAT_C_CONNECTION << 16;
  455. if (temp & PORT_PEC)
  456. status |= USB_PORT_STAT_C_ENABLE << 16;
  457. if ((temp & PORT_OCC))
  458. status |= USB_PORT_STAT_C_OVERCURRENT << 16;
  459. if ((temp & PORT_RC))
  460. status |= USB_PORT_STAT_C_RESET << 16;
  461. /* USB3.0 only */
  462. if (hcd->speed == HCD_USB3) {
  463. if ((temp & PORT_PLC))
  464. status |= USB_PORT_STAT_C_LINK_STATE << 16;
  465. if ((temp & PORT_WRC))
  466. status |= USB_PORT_STAT_C_BH_RESET << 16;
  467. }
  468. if (hcd->speed != HCD_USB3) {
  469. if ((temp & PORT_PLS_MASK) == XDEV_U3
  470. && (temp & PORT_POWER))
  471. status |= USB_PORT_STAT_SUSPEND;
  472. }
  473. if ((temp & PORT_PLS_MASK) == XDEV_RESUME &&
  474. !DEV_SUPERSPEED(temp)) {
  475. if ((temp & PORT_RESET) || !(temp & PORT_PE))
  476. goto error;
  477. if (time_after_eq(jiffies,
  478. bus_state->resume_done[wIndex])) {
  479. xhci_dbg(xhci, "Resume USB2 port %d\n",
  480. wIndex + 1);
  481. bus_state->resume_done[wIndex] = 0;
  482. xhci_set_link_state(xhci, port_array, wIndex,
  483. XDEV_U0);
  484. xhci_dbg(xhci, "set port %d resume\n",
  485. wIndex + 1);
  486. slot_id = xhci_find_slot_id_by_port(hcd, xhci,
  487. wIndex + 1);
  488. if (!slot_id) {
  489. xhci_dbg(xhci, "slot_id is zero\n");
  490. goto error;
  491. }
  492. xhci_ring_device(xhci, slot_id);
  493. bus_state->port_c_suspend |= 1 << wIndex;
  494. bus_state->suspended_ports &= ~(1 << wIndex);
  495. } else {
  496. /*
  497. * The resume has been signaling for less than
  498. * 20ms. Report the port status as SUSPEND,
  499. * let the usbcore check port status again
  500. * and clear resume signaling later.
  501. */
  502. status |= USB_PORT_STAT_SUSPEND;
  503. }
  504. }
  505. if ((temp & PORT_PLS_MASK) == XDEV_U0
  506. && (temp & PORT_POWER)
  507. && (bus_state->suspended_ports & (1 << wIndex))) {
  508. bus_state->suspended_ports &= ~(1 << wIndex);
  509. if (hcd->speed != HCD_USB3)
  510. bus_state->port_c_suspend |= 1 << wIndex;
  511. }
  512. if (temp & PORT_CONNECT) {
  513. status |= USB_PORT_STAT_CONNECTION;
  514. status |= xhci_port_speed(temp);
  515. }
  516. if (temp & PORT_PE)
  517. status |= USB_PORT_STAT_ENABLE;
  518. if (temp & PORT_OC)
  519. status |= USB_PORT_STAT_OVERCURRENT;
  520. if (temp & PORT_RESET)
  521. status |= USB_PORT_STAT_RESET;
  522. if (temp & PORT_POWER) {
  523. if (hcd->speed == HCD_USB3)
  524. status |= USB_SS_PORT_STAT_POWER;
  525. else
  526. status |= USB_PORT_STAT_POWER;
  527. }
  528. /* Port Link State */
  529. if (hcd->speed == HCD_USB3) {
  530. /* resume state is a xHCI internal state.
  531. * Do not report it to usb core.
  532. */
  533. if ((temp & PORT_PLS_MASK) != XDEV_RESUME)
  534. status |= (temp & PORT_PLS_MASK);
  535. }
  536. if (bus_state->port_c_suspend & (1 << wIndex))
  537. status |= 1 << USB_PORT_FEAT_C_SUSPEND;
  538. xhci_dbg(xhci, "Get port status returned 0x%x\n", status);
  539. put_unaligned(cpu_to_le32(status), (__le32 *) buf);
  540. break;
  541. case SetPortFeature:
  542. if (wValue == USB_PORT_FEAT_LINK_STATE)
  543. link_state = (wIndex & 0xff00) >> 3;
  544. wIndex &= 0xff;
  545. if (!wIndex || wIndex > max_ports)
  546. goto error;
  547. wIndex--;
  548. temp = xhci_readl(xhci, port_array[wIndex]);
  549. if (temp == 0xffffffff) {
  550. retval = -ENODEV;
  551. break;
  552. }
  553. temp = xhci_port_state_to_neutral(temp);
  554. /* FIXME: What new port features do we need to support? */
  555. switch (wValue) {
  556. case USB_PORT_FEAT_SUSPEND:
  557. temp = xhci_readl(xhci, port_array[wIndex]);
  558. if ((temp & PORT_PLS_MASK) != XDEV_U0) {
  559. /* Resume the port to U0 first */
  560. xhci_set_link_state(xhci, port_array, wIndex,
  561. XDEV_U0);
  562. spin_unlock_irqrestore(&xhci->lock, flags);
  563. msleep(10);
  564. spin_lock_irqsave(&xhci->lock, flags);
  565. }
  566. /* In spec software should not attempt to suspend
  567. * a port unless the port reports that it is in the
  568. * enabled (PED = ‘1’,PLS < ‘3’) state.
  569. */
  570. temp = xhci_readl(xhci, port_array[wIndex]);
  571. if ((temp & PORT_PE) == 0 || (temp & PORT_RESET)
  572. || (temp & PORT_PLS_MASK) >= XDEV_U3) {
  573. xhci_warn(xhci, "USB core suspending device "
  574. "not in U0/U1/U2.\n");
  575. goto error;
  576. }
  577. slot_id = xhci_find_slot_id_by_port(hcd, xhci,
  578. wIndex + 1);
  579. if (!slot_id) {
  580. xhci_warn(xhci, "slot_id is zero\n");
  581. goto error;
  582. }
  583. /* unlock to execute stop endpoint commands */
  584. spin_unlock_irqrestore(&xhci->lock, flags);
  585. xhci_stop_device(xhci, slot_id, 1);
  586. spin_lock_irqsave(&xhci->lock, flags);
  587. xhci_set_link_state(xhci, port_array, wIndex, XDEV_U3);
  588. spin_unlock_irqrestore(&xhci->lock, flags);
  589. msleep(10); /* wait device to enter */
  590. spin_lock_irqsave(&xhci->lock, flags);
  591. temp = xhci_readl(xhci, port_array[wIndex]);
  592. bus_state->suspended_ports |= 1 << wIndex;
  593. break;
  594. case USB_PORT_FEAT_LINK_STATE:
  595. temp = xhci_readl(xhci, port_array[wIndex]);
  596. /* Software should not attempt to set
  597. * port link state above '5' (Rx.Detect) and the port
  598. * must be enabled.
  599. */
  600. if ((temp & PORT_PE) == 0 ||
  601. (link_state > USB_SS_PORT_LS_RX_DETECT)) {
  602. xhci_warn(xhci, "Cannot set link state.\n");
  603. goto error;
  604. }
  605. if (link_state == USB_SS_PORT_LS_U3) {
  606. slot_id = xhci_find_slot_id_by_port(hcd, xhci,
  607. wIndex + 1);
  608. if (slot_id) {
  609. /* unlock to execute stop endpoint
  610. * commands */
  611. spin_unlock_irqrestore(&xhci->lock,
  612. flags);
  613. xhci_stop_device(xhci, slot_id, 1);
  614. spin_lock_irqsave(&xhci->lock, flags);
  615. }
  616. }
  617. xhci_set_link_state(xhci, port_array, wIndex,
  618. link_state);
  619. spin_unlock_irqrestore(&xhci->lock, flags);
  620. msleep(20); /* wait device to enter */
  621. spin_lock_irqsave(&xhci->lock, flags);
  622. temp = xhci_readl(xhci, port_array[wIndex]);
  623. if (link_state == USB_SS_PORT_LS_U3)
  624. bus_state->suspended_ports |= 1 << wIndex;
  625. break;
  626. case USB_PORT_FEAT_POWER:
  627. /*
  628. * Turn on ports, even if there isn't per-port switching.
  629. * HC will report connect events even before this is set.
  630. * However, khubd will ignore the roothub events until
  631. * the roothub is registered.
  632. */
  633. xhci_writel(xhci, temp | PORT_POWER,
  634. port_array[wIndex]);
  635. temp = xhci_readl(xhci, port_array[wIndex]);
  636. xhci_dbg(xhci, "set port power, actual port %d status = 0x%x\n", wIndex, temp);
  637. break;
  638. case USB_PORT_FEAT_RESET:
  639. temp = (temp | PORT_RESET);
  640. xhci_writel(xhci, temp, port_array[wIndex]);
  641. temp = xhci_readl(xhci, port_array[wIndex]);
  642. xhci_dbg(xhci, "set port reset, actual port %d status = 0x%x\n", wIndex, temp);
  643. break;
  644. case USB_PORT_FEAT_BH_PORT_RESET:
  645. temp |= PORT_WR;
  646. xhci_writel(xhci, temp, port_array[wIndex]);
  647. temp = xhci_readl(xhci, port_array[wIndex]);
  648. break;
  649. default:
  650. goto error;
  651. }
  652. /* unblock any posted writes */
  653. temp = xhci_readl(xhci, port_array[wIndex]);
  654. break;
  655. case ClearPortFeature:
  656. if (!wIndex || wIndex > max_ports)
  657. goto error;
  658. wIndex--;
  659. temp = xhci_readl(xhci, port_array[wIndex]);
  660. if (temp == 0xffffffff) {
  661. retval = -ENODEV;
  662. break;
  663. }
  664. /* FIXME: What new port features do we need to support? */
  665. temp = xhci_port_state_to_neutral(temp);
  666. switch (wValue) {
  667. case USB_PORT_FEAT_SUSPEND:
  668. temp = xhci_readl(xhci, port_array[wIndex]);
  669. xhci_dbg(xhci, "clear USB_PORT_FEAT_SUSPEND\n");
  670. xhci_dbg(xhci, "PORTSC %04x\n", temp);
  671. if (temp & PORT_RESET)
  672. goto error;
  673. if ((temp & PORT_PLS_MASK) == XDEV_U3) {
  674. if ((temp & PORT_PE) == 0)
  675. goto error;
  676. xhci_set_link_state(xhci, port_array, wIndex,
  677. XDEV_RESUME);
  678. spin_unlock_irqrestore(&xhci->lock, flags);
  679. msleep(20);
  680. spin_lock_irqsave(&xhci->lock, flags);
  681. xhci_set_link_state(xhci, port_array, wIndex,
  682. XDEV_U0);
  683. }
  684. bus_state->port_c_suspend |= 1 << wIndex;
  685. slot_id = xhci_find_slot_id_by_port(hcd, xhci,
  686. wIndex + 1);
  687. if (!slot_id) {
  688. xhci_dbg(xhci, "slot_id is zero\n");
  689. goto error;
  690. }
  691. xhci_ring_device(xhci, slot_id);
  692. break;
  693. case USB_PORT_FEAT_C_SUSPEND:
  694. bus_state->port_c_suspend &= ~(1 << wIndex);
  695. case USB_PORT_FEAT_C_RESET:
  696. case USB_PORT_FEAT_C_BH_PORT_RESET:
  697. case USB_PORT_FEAT_C_CONNECTION:
  698. case USB_PORT_FEAT_C_OVER_CURRENT:
  699. case USB_PORT_FEAT_C_ENABLE:
  700. case USB_PORT_FEAT_C_PORT_LINK_STATE:
  701. xhci_clear_port_change_bit(xhci, wValue, wIndex,
  702. port_array[wIndex], temp);
  703. break;
  704. case USB_PORT_FEAT_ENABLE:
  705. xhci_disable_port(hcd, xhci, wIndex,
  706. port_array[wIndex], temp);
  707. break;
  708. default:
  709. goto error;
  710. }
  711. break;
  712. default:
  713. error:
  714. /* "stall" on error */
  715. retval = -EPIPE;
  716. }
  717. spin_unlock_irqrestore(&xhci->lock, flags);
  718. return retval;
  719. }
  720. /*
  721. * Returns 0 if the status hasn't changed, or the number of bytes in buf.
  722. * Ports are 0-indexed from the HCD point of view,
  723. * and 1-indexed from the USB core pointer of view.
  724. *
  725. * Note that the status change bits will be cleared as soon as a port status
  726. * change event is generated, so we use the saved status from that event.
  727. */
  728. int xhci_hub_status_data(struct usb_hcd *hcd, char *buf)
  729. {
  730. unsigned long flags;
  731. u32 temp, status;
  732. u32 mask;
  733. int i, retval;
  734. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  735. int max_ports;
  736. __le32 __iomem **port_array;
  737. struct xhci_bus_state *bus_state;
  738. max_ports = xhci_get_ports(hcd, &port_array);
  739. bus_state = &xhci->bus_state[hcd_index(hcd)];
  740. /* Initial status is no changes */
  741. retval = (max_ports + 8) / 8;
  742. memset(buf, 0, retval);
  743. status = 0;
  744. mask = PORT_CSC | PORT_PEC | PORT_OCC | PORT_PLC | PORT_WRC;
  745. spin_lock_irqsave(&xhci->lock, flags);
  746. /* For each port, did anything change? If so, set that bit in buf. */
  747. for (i = 0; i < max_ports; i++) {
  748. temp = xhci_readl(xhci, port_array[i]);
  749. if (temp == 0xffffffff) {
  750. retval = -ENODEV;
  751. break;
  752. }
  753. if ((temp & mask) != 0 ||
  754. (bus_state->port_c_suspend & 1 << i) ||
  755. (bus_state->resume_done[i] && time_after_eq(
  756. jiffies, bus_state->resume_done[i]))) {
  757. buf[(i + 1) / 8] |= 1 << (i + 1) % 8;
  758. status = 1;
  759. }
  760. }
  761. spin_unlock_irqrestore(&xhci->lock, flags);
  762. return status ? retval : 0;
  763. }
  764. #ifdef CONFIG_PM
  765. int xhci_bus_suspend(struct usb_hcd *hcd)
  766. {
  767. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  768. int max_ports, port_index;
  769. __le32 __iomem **port_array;
  770. struct xhci_bus_state *bus_state;
  771. unsigned long flags;
  772. max_ports = xhci_get_ports(hcd, &port_array);
  773. bus_state = &xhci->bus_state[hcd_index(hcd)];
  774. spin_lock_irqsave(&xhci->lock, flags);
  775. if (hcd->self.root_hub->do_remote_wakeup) {
  776. port_index = max_ports;
  777. while (port_index--) {
  778. if (bus_state->resume_done[port_index] != 0) {
  779. spin_unlock_irqrestore(&xhci->lock, flags);
  780. xhci_dbg(xhci, "suspend failed because "
  781. "port %d is resuming\n",
  782. port_index + 1);
  783. return -EBUSY;
  784. }
  785. }
  786. }
  787. port_index = max_ports;
  788. bus_state->bus_suspended = 0;
  789. while (port_index--) {
  790. /* suspend the port if the port is not suspended */
  791. u32 t1, t2;
  792. int slot_id;
  793. t1 = xhci_readl(xhci, port_array[port_index]);
  794. t2 = xhci_port_state_to_neutral(t1);
  795. if ((t1 & PORT_PE) && !(t1 & PORT_PLS_MASK)) {
  796. xhci_dbg(xhci, "port %d not suspended\n", port_index);
  797. slot_id = xhci_find_slot_id_by_port(hcd, xhci,
  798. port_index + 1);
  799. if (slot_id) {
  800. spin_unlock_irqrestore(&xhci->lock, flags);
  801. xhci_stop_device(xhci, slot_id, 1);
  802. spin_lock_irqsave(&xhci->lock, flags);
  803. }
  804. t2 &= ~PORT_PLS_MASK;
  805. t2 |= PORT_LINK_STROBE | XDEV_U3;
  806. set_bit(port_index, &bus_state->bus_suspended);
  807. }
  808. if (hcd->self.root_hub->do_remote_wakeup) {
  809. if (t1 & PORT_CONNECT) {
  810. t2 |= PORT_WKOC_E | PORT_WKDISC_E;
  811. t2 &= ~PORT_WKCONN_E;
  812. } else {
  813. t2 |= PORT_WKOC_E | PORT_WKCONN_E;
  814. t2 &= ~PORT_WKDISC_E;
  815. }
  816. } else
  817. t2 &= ~PORT_WAKE_BITS;
  818. t1 = xhci_port_state_to_neutral(t1);
  819. if (t1 != t2)
  820. xhci_writel(xhci, t2, port_array[port_index]);
  821. if (hcd->speed != HCD_USB3) {
  822. /* enable remote wake up for USB 2.0 */
  823. __le32 __iomem *addr;
  824. u32 tmp;
  825. /* Add one to the port status register address to get
  826. * the port power control register address.
  827. */
  828. addr = port_array[port_index] + 1;
  829. tmp = xhci_readl(xhci, addr);
  830. tmp |= PORT_RWE;
  831. xhci_writel(xhci, tmp, addr);
  832. }
  833. }
  834. hcd->state = HC_STATE_SUSPENDED;
  835. bus_state->next_statechange = jiffies + msecs_to_jiffies(10);
  836. spin_unlock_irqrestore(&xhci->lock, flags);
  837. return 0;
  838. }
  839. int xhci_bus_resume(struct usb_hcd *hcd)
  840. {
  841. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  842. int max_ports, port_index;
  843. __le32 __iomem **port_array;
  844. struct xhci_bus_state *bus_state;
  845. u32 temp;
  846. unsigned long flags;
  847. max_ports = xhci_get_ports(hcd, &port_array);
  848. bus_state = &xhci->bus_state[hcd_index(hcd)];
  849. if (time_before(jiffies, bus_state->next_statechange))
  850. msleep(5);
  851. spin_lock_irqsave(&xhci->lock, flags);
  852. if (!HCD_HW_ACCESSIBLE(hcd)) {
  853. spin_unlock_irqrestore(&xhci->lock, flags);
  854. return -ESHUTDOWN;
  855. }
  856. /* delay the irqs */
  857. temp = xhci_readl(xhci, &xhci->op_regs->command);
  858. temp &= ~CMD_EIE;
  859. xhci_writel(xhci, temp, &xhci->op_regs->command);
  860. port_index = max_ports;
  861. while (port_index--) {
  862. /* Check whether need resume ports. If needed
  863. resume port and disable remote wakeup */
  864. u32 temp;
  865. int slot_id;
  866. temp = xhci_readl(xhci, port_array[port_index]);
  867. if (DEV_SUPERSPEED(temp))
  868. temp &= ~(PORT_RWC_BITS | PORT_CEC | PORT_WAKE_BITS);
  869. else
  870. temp &= ~(PORT_RWC_BITS | PORT_WAKE_BITS);
  871. if (test_bit(port_index, &bus_state->bus_suspended) &&
  872. (temp & PORT_PLS_MASK)) {
  873. if (DEV_SUPERSPEED(temp)) {
  874. xhci_set_link_state(xhci, port_array,
  875. port_index, XDEV_U0);
  876. } else {
  877. xhci_set_link_state(xhci, port_array,
  878. port_index, XDEV_RESUME);
  879. spin_unlock_irqrestore(&xhci->lock, flags);
  880. msleep(20);
  881. spin_lock_irqsave(&xhci->lock, flags);
  882. xhci_set_link_state(xhci, port_array,
  883. port_index, XDEV_U0);
  884. }
  885. /* wait for the port to enter U0 and report port link
  886. * state change.
  887. */
  888. spin_unlock_irqrestore(&xhci->lock, flags);
  889. msleep(20);
  890. spin_lock_irqsave(&xhci->lock, flags);
  891. /* Clear PLC */
  892. xhci_test_and_clear_bit(xhci, port_array, port_index,
  893. PORT_PLC);
  894. slot_id = xhci_find_slot_id_by_port(hcd,
  895. xhci, port_index + 1);
  896. if (slot_id)
  897. xhci_ring_device(xhci, slot_id);
  898. } else
  899. xhci_writel(xhci, temp, port_array[port_index]);
  900. if (hcd->speed != HCD_USB3) {
  901. /* disable remote wake up for USB 2.0 */
  902. __le32 __iomem *addr;
  903. u32 tmp;
  904. /* Add one to the port status register address to get
  905. * the port power control register address.
  906. */
  907. addr = port_array[port_index] + 1;
  908. tmp = xhci_readl(xhci, addr);
  909. tmp &= ~PORT_RWE;
  910. xhci_writel(xhci, tmp, addr);
  911. }
  912. }
  913. (void) xhci_readl(xhci, &xhci->op_regs->command);
  914. bus_state->next_statechange = jiffies + msecs_to_jiffies(5);
  915. /* re-enable irqs */
  916. temp = xhci_readl(xhci, &xhci->op_regs->command);
  917. temp |= CMD_EIE;
  918. xhci_writel(xhci, temp, &xhci->op_regs->command);
  919. temp = xhci_readl(xhci, &xhci->op_regs->command);
  920. spin_unlock_irqrestore(&xhci->lock, flags);
  921. return 0;
  922. }
  923. #endif /* CONFIG_PM */