xhci-hub.c 29 KB

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