xhci-hub.c 25 KB

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