xhci-hub.c 28 KB

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