xhci-hub.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348
  1. /*
  2. * xHCI host controller driver
  3. *
  4. * Copyright (C) 2008 Intel Corp.
  5. *
  6. * Author: Sarah Sharp
  7. * Some code borrowed from the Linux EHCI driver.
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  15. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  16. * for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software Foundation,
  20. * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. */
  22. #include <linux/gfp.h>
  23. #include <linux/slab.h>
  24. #include <asm/unaligned.h>
  25. #include "xhci.h"
  26. #define PORT_WAKE_BITS (PORT_WKOC_E | PORT_WKDISC_E | PORT_WKCONN_E)
  27. #define PORT_RWC_BITS (PORT_CSC | PORT_PEC | PORT_WRC | PORT_OCC | \
  28. PORT_RC | PORT_PLC | PORT_PE)
  29. /* usb 1.1 root hub device descriptor */
  30. static u8 usb_bos_descriptor [] = {
  31. USB_DT_BOS_SIZE, /* __u8 bLength, 5 bytes */
  32. USB_DT_BOS, /* __u8 bDescriptorType */
  33. 0x0F, 0x00, /* __le16 wTotalLength, 15 bytes */
  34. 0x1, /* __u8 bNumDeviceCaps */
  35. /* First device capability */
  36. USB_DT_USB_SS_CAP_SIZE, /* __u8 bLength, 10 bytes */
  37. USB_DT_DEVICE_CAPABILITY, /* Device Capability */
  38. USB_SS_CAP_TYPE, /* bDevCapabilityType, SUPERSPEED_USB */
  39. 0x00, /* bmAttributes, LTM off by default */
  40. USB_5GBPS_OPERATION, 0x00, /* wSpeedsSupported, 5Gbps only */
  41. 0x03, /* bFunctionalitySupport,
  42. USB 3.0 speed only */
  43. 0x00, /* bU1DevExitLat, set later. */
  44. 0x00, 0x00 /* __le16 bU2DevExitLat, set later. */
  45. };
  46. static void xhci_common_hub_descriptor(struct xhci_hcd *xhci,
  47. struct usb_hub_descriptor *desc, int ports)
  48. {
  49. u16 temp;
  50. desc->bPwrOn2PwrGood = 10; /* xhci section 5.4.9 says 20ms max */
  51. desc->bHubContrCurrent = 0;
  52. desc->bNbrPorts = ports;
  53. temp = 0;
  54. /* Bits 1:0 - support per-port power switching, or power always on */
  55. if (HCC_PPC(xhci->hcc_params))
  56. temp |= HUB_CHAR_INDV_PORT_LPSM;
  57. else
  58. temp |= HUB_CHAR_NO_LPSM;
  59. /* Bit 2 - root hubs are not part of a compound device */
  60. /* Bits 4:3 - individual port over current protection */
  61. temp |= HUB_CHAR_INDV_PORT_OCPM;
  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 = USB_DT_HUB;
  78. temp = 1 + (ports / 8);
  79. desc->bDescLength = USB_DT_HUB_NONVAR_SIZE + 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->usb2_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 = USB_DT_SS_HUB;
  124. desc->bDescLength = USB_DT_SS_HUB_SIZE;
  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_find_next_enqueue(xhci->cmd_ring);
  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. if (xhci->quirks & XHCI_PORTSC_DELAY)
  311. ndelay(100);
  312. port_status = xhci_readl(xhci, addr);
  313. xhci_dbg(xhci, "disable port, actual port %d status = 0x%x\n",
  314. wIndex, port_status);
  315. }
  316. static void xhci_clear_port_change_bit(struct xhci_hcd *xhci, u16 wValue,
  317. u16 wIndex, __le32 __iomem *addr, u32 port_status)
  318. {
  319. char *port_change_bit;
  320. u32 status;
  321. switch (wValue) {
  322. case USB_PORT_FEAT_C_RESET:
  323. status = PORT_RC;
  324. port_change_bit = "reset";
  325. break;
  326. case USB_PORT_FEAT_C_BH_PORT_RESET:
  327. status = PORT_WRC;
  328. port_change_bit = "warm(BH) reset";
  329. break;
  330. case USB_PORT_FEAT_C_CONNECTION:
  331. status = PORT_CSC;
  332. port_change_bit = "connect";
  333. break;
  334. case USB_PORT_FEAT_C_OVER_CURRENT:
  335. status = PORT_OCC;
  336. port_change_bit = "over-current";
  337. break;
  338. case USB_PORT_FEAT_C_ENABLE:
  339. status = PORT_PEC;
  340. port_change_bit = "enable/disable";
  341. break;
  342. case USB_PORT_FEAT_C_SUSPEND:
  343. status = PORT_PLC;
  344. port_change_bit = "suspend/resume";
  345. break;
  346. case USB_PORT_FEAT_C_PORT_LINK_STATE:
  347. status = PORT_PLC;
  348. port_change_bit = "link state";
  349. break;
  350. case USB_PORT_FEAT_C_PORT_CONFIG_ERROR:
  351. status = PORT_CEC;
  352. port_change_bit = "config error";
  353. break;
  354. default:
  355. /* Should never happen */
  356. return;
  357. }
  358. /* Change bits are all write 1 to clear */
  359. xhci_writel(xhci, port_status | status, addr);
  360. if (xhci->quirks & XHCI_PORTSC_DELAY)
  361. ndelay(100);
  362. port_status = xhci_readl(xhci, addr);
  363. xhci_dbg(xhci, "clear port %s change, actual port %d status = 0x%x\n",
  364. port_change_bit, wIndex, port_status);
  365. }
  366. static int xhci_get_ports(struct usb_hcd *hcd, __le32 __iomem ***port_array)
  367. {
  368. int max_ports;
  369. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  370. if (hcd->speed == HCD_USB3) {
  371. max_ports = xhci->num_usb3_ports;
  372. *port_array = xhci->usb3_ports;
  373. } else {
  374. max_ports = xhci->num_usb2_ports;
  375. *port_array = xhci->usb2_ports;
  376. }
  377. return max_ports;
  378. }
  379. void xhci_set_link_state(struct xhci_hcd *xhci, __le32 __iomem **port_array,
  380. int port_id, u32 link_state)
  381. {
  382. u32 temp;
  383. temp = xhci_readl(xhci, port_array[port_id]);
  384. temp = xhci_port_state_to_neutral(temp);
  385. temp &= ~PORT_PLS_MASK;
  386. temp |= PORT_LINK_STROBE | link_state;
  387. xhci_writel(xhci, temp, port_array[port_id]);
  388. if (xhci->quirks & XHCI_PORTSC_DELAY)
  389. ndelay(100);
  390. }
  391. void xhci_set_remote_wake_mask(struct xhci_hcd *xhci,
  392. __le32 __iomem **port_array, int port_id, u16 wake_mask)
  393. {
  394. u32 temp;
  395. temp = xhci_readl(xhci, port_array[port_id]);
  396. temp = xhci_port_state_to_neutral(temp);
  397. if (wake_mask & USB_PORT_FEAT_REMOTE_WAKE_CONNECT)
  398. temp |= PORT_WKCONN_E;
  399. else
  400. temp &= ~PORT_WKCONN_E;
  401. if (wake_mask & USB_PORT_FEAT_REMOTE_WAKE_DISCONNECT)
  402. temp |= PORT_WKDISC_E;
  403. else
  404. temp &= ~PORT_WKDISC_E;
  405. if (wake_mask & USB_PORT_FEAT_REMOTE_WAKE_OVER_CURRENT)
  406. temp |= PORT_WKOC_E;
  407. else
  408. temp &= ~PORT_WKOC_E;
  409. xhci_writel(xhci, temp, port_array[port_id]);
  410. if (xhci->quirks & XHCI_PORTSC_DELAY)
  411. ndelay(100);
  412. }
  413. /* Test and clear port RWC bit */
  414. void xhci_test_and_clear_bit(struct xhci_hcd *xhci, __le32 __iomem **port_array,
  415. int port_id, u32 port_bit)
  416. {
  417. u32 temp;
  418. temp = xhci_readl(xhci, port_array[port_id]);
  419. if (temp & port_bit) {
  420. temp = xhci_port_state_to_neutral(temp);
  421. temp |= port_bit;
  422. xhci_writel(xhci, temp, port_array[port_id]);
  423. if (xhci->quirks & XHCI_PORTSC_DELAY)
  424. ndelay(100);
  425. }
  426. }
  427. static void xhci_single_step_completion(struct urb *urb)
  428. {
  429. struct completion *done = urb->context;
  430. complete(done);
  431. }
  432. /*
  433. * Allocate a URB and initialize the various fields of it.
  434. * This API is used by the single_step_set_feature test of
  435. * EHSET where IN packet of the GetDescriptor request is
  436. * sent 15secs after the SETUP packet.
  437. * Return NULL if failed.
  438. */
  439. static struct urb *xhci_request_single_step_set_feature_urb(
  440. struct usb_device *udev,
  441. void *dr,
  442. void *buf,
  443. struct completion *done)
  444. {
  445. struct urb *urb;
  446. struct usb_hcd *hcd = bus_to_hcd(udev->bus);
  447. struct usb_host_endpoint *ep;
  448. urb = usb_alloc_urb(0, GFP_KERNEL);
  449. if (!urb)
  450. return NULL;
  451. urb->pipe = usb_rcvctrlpipe(udev, 0);
  452. ep = udev->ep_in[usb_pipeendpoint(urb->pipe)];
  453. if (!ep) {
  454. usb_free_urb(urb);
  455. return NULL;
  456. }
  457. /*
  458. * Initialize the various URB fields as these are used by the HCD
  459. * driver to queue it and as well as when completion happens.
  460. */
  461. urb->ep = ep;
  462. urb->dev = udev;
  463. urb->setup_packet = dr;
  464. urb->transfer_buffer = buf;
  465. urb->transfer_buffer_length = USB_DT_DEVICE_SIZE;
  466. urb->complete = xhci_single_step_completion;
  467. urb->status = -EINPROGRESS;
  468. urb->actual_length = 0;
  469. urb->transfer_flags = URB_DIR_IN;
  470. usb_get_urb(urb);
  471. atomic_inc(&urb->use_count);
  472. atomic_inc(&urb->dev->urbnum);
  473. usb_hcd_map_urb_for_dma(hcd, urb, GFP_KERNEL);
  474. urb->context = done;
  475. return urb;
  476. }
  477. /*
  478. * This function implements the USB_PORT_FEAT_TEST handling of the
  479. * SINGLE_STEP_SET_FEATURE test mode as defined in the Embedded
  480. * High-Speed Electrical Test (EHSET) specification. This simply
  481. * issues a GetDescriptor control transfer, with an inserted 15-second
  482. * delay after the end of the SETUP stage and before the IN token of
  483. * the DATA stage is set. The idea is that this gives the test operator
  484. * enough time to configure the oscilloscope to perform a measurement
  485. * of the response time between the DATA and ACK packets that follow.
  486. */
  487. static int xhci_ehset_single_step_set_feature(struct usb_hcd *hcd, int port)
  488. {
  489. int retval = -ENOMEM;
  490. struct usb_ctrlrequest *dr;
  491. struct urb *urb;
  492. struct usb_device *udev;
  493. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  494. struct usb_device_descriptor *buf;
  495. unsigned long flags;
  496. DECLARE_COMPLETION_ONSTACK(done);
  497. /* Obtain udev of the rhub's child port */
  498. udev = hcd->self.root_hub->children[port];
  499. if (!udev) {
  500. xhci_err(xhci, "No device attached to the RootHub\n");
  501. return -ENODEV;
  502. }
  503. buf = kmalloc(USB_DT_DEVICE_SIZE, GFP_KERNEL);
  504. if (!buf)
  505. return -ENOMEM;
  506. dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
  507. if (!dr) {
  508. kfree(buf);
  509. return -ENOMEM;
  510. }
  511. /* Fill Setup packet for GetDescriptor */
  512. dr->bRequestType = USB_DIR_IN;
  513. dr->bRequest = USB_REQ_GET_DESCRIPTOR;
  514. dr->wValue = cpu_to_le16(USB_DT_DEVICE << 8);
  515. dr->wIndex = 0;
  516. dr->wLength = cpu_to_le16(USB_DT_DEVICE_SIZE);
  517. urb = xhci_request_single_step_set_feature_urb(udev, dr, buf, &done);
  518. if (!urb)
  519. goto cleanup;
  520. /* Now complete just the SETUP stage */
  521. spin_lock_irqsave(&xhci->lock, flags);
  522. retval = xhci_submit_single_step_set_feature(hcd, urb, 1);
  523. spin_unlock_irqrestore(&xhci->lock, flags);
  524. if (retval)
  525. goto out1;
  526. if (!wait_for_completion_timeout(&done, msecs_to_jiffies(2000))) {
  527. usb_kill_urb(urb);
  528. retval = -ETIMEDOUT;
  529. xhci_err(xhci, "%s SETUP stage timed out on ep0\n", __func__);
  530. goto out1;
  531. }
  532. /* Sleep for 15 seconds; HC will send SOFs during this period */
  533. msleep(15 * 1000);
  534. /* Complete remaining DATA and status stages. Re-use same URB */
  535. urb->status = -EINPROGRESS;
  536. usb_get_urb(urb);
  537. atomic_inc(&urb->use_count);
  538. atomic_inc(&urb->dev->urbnum);
  539. spin_lock_irqsave(&xhci->lock, flags);
  540. retval = xhci_submit_single_step_set_feature(hcd, urb, 0);
  541. spin_unlock_irqrestore(&xhci->lock, flags);
  542. if (!retval && !wait_for_completion_timeout(&done,
  543. msecs_to_jiffies(2000))) {
  544. usb_kill_urb(urb);
  545. retval = -ETIMEDOUT;
  546. xhci_err(xhci, "%s IN stage timed out on ep0\n", __func__);
  547. }
  548. out1:
  549. usb_free_urb(urb);
  550. cleanup:
  551. kfree(dr);
  552. kfree(buf);
  553. return retval;
  554. }
  555. /* Updates Link Status for super Speed port */
  556. static void xhci_hub_report_link_state(struct xhci_hcd *xhci,
  557. u32 *status, u32 status_reg)
  558. {
  559. u32 pls = status_reg & PORT_PLS_MASK;
  560. /* resume state is a xHCI internal state.
  561. * Do not report it to usb core, instead, pretend to be U3,
  562. * thus usb core knows it's not ready for transfer
  563. */
  564. if (pls == XDEV_RESUME) {
  565. *status |= USB_SS_PORT_LS_U3;
  566. return;
  567. }
  568. /* When the CAS bit is set then warm reset
  569. * should be performed on port
  570. */
  571. if (status_reg & PORT_CAS) {
  572. /* The CAS bit can be set while the port is
  573. * in any link state.
  574. * Only roothubs have CAS bit, so we
  575. * pretend to be in compliance mode
  576. * unless we're already in compliance
  577. * or the inactive state.
  578. */
  579. if (pls != USB_SS_PORT_LS_COMP_MOD &&
  580. pls != USB_SS_PORT_LS_SS_INACTIVE) {
  581. pls = USB_SS_PORT_LS_COMP_MOD;
  582. }
  583. /* Return also connection bit -
  584. * hub state machine resets port
  585. * when this bit is set.
  586. */
  587. pls |= USB_PORT_STAT_CONNECTION;
  588. } else {
  589. /*
  590. * If CAS bit isn't set but the Port is already at
  591. * Compliance Mode, fake a connection so the USB core
  592. * notices the Compliance state and resets the port.
  593. * This resolves an issue generated by the SN65LVPE502CP
  594. * in which sometimes the port enters compliance mode
  595. * caused by a delay on the host-device negotiation.
  596. */
  597. if ((xhci->quirks & XHCI_COMP_MODE_QUIRK) &&
  598. (pls == USB_SS_PORT_LS_COMP_MOD))
  599. pls |= USB_PORT_STAT_CONNECTION;
  600. }
  601. /* update status field */
  602. *status |= pls;
  603. }
  604. /*
  605. * Function for Compliance Mode Quirk.
  606. *
  607. * This Function verifies if all xhc USB3 ports have entered U0, if so,
  608. * the compliance mode timer is deleted. A port won't enter
  609. * compliance mode if it has previously entered U0.
  610. */
  611. void xhci_del_comp_mod_timer(struct xhci_hcd *xhci, u32 status, u16 wIndex)
  612. {
  613. u32 all_ports_seen_u0 = ((1 << xhci->num_usb3_ports)-1);
  614. bool port_in_u0 = ((status & PORT_PLS_MASK) == XDEV_U0);
  615. if (!(xhci->quirks & XHCI_COMP_MODE_QUIRK))
  616. return;
  617. if ((xhci->port_status_u0 != all_ports_seen_u0) && port_in_u0) {
  618. xhci->port_status_u0 |= 1 << wIndex;
  619. if (xhci->port_status_u0 == all_ports_seen_u0) {
  620. del_timer_sync(&xhci->comp_mode_recovery_timer);
  621. xhci_dbg(xhci, "All USB3 ports have entered U0 already!\n");
  622. xhci_dbg(xhci, "Compliance Mode Recovery Timer Deleted.\n");
  623. }
  624. }
  625. }
  626. int xhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
  627. u16 wIndex, char *buf, u16 wLength)
  628. {
  629. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  630. int max_ports;
  631. unsigned long flags;
  632. u32 temp, status;
  633. int retval = 0;
  634. __le32 __iomem **port_array;
  635. int slot_id;
  636. struct xhci_bus_state *bus_state;
  637. u16 link_state = 0;
  638. u16 wake_mask = 0;
  639. u16 test_mode = 0;
  640. max_ports = xhci_get_ports(hcd, &port_array);
  641. bus_state = &xhci->bus_state[hcd_index(hcd)];
  642. spin_lock_irqsave(&xhci->lock, flags);
  643. switch (typeReq) {
  644. case GetHubStatus:
  645. /* No power source, over-current reported per port */
  646. memset(buf, 0, 4);
  647. break;
  648. case GetHubDescriptor:
  649. /* Check to make sure userspace is asking for the USB 3.0 hub
  650. * descriptor for the USB 3.0 roothub. If not, we stall the
  651. * endpoint, like external hubs do.
  652. */
  653. if (hcd->speed == HCD_USB3 &&
  654. (wLength < USB_DT_SS_HUB_SIZE ||
  655. wValue != (USB_DT_SS_HUB << 8))) {
  656. xhci_dbg(xhci, "Wrong hub descriptor type for "
  657. "USB 3.0 roothub.\n");
  658. goto error;
  659. }
  660. xhci_hub_descriptor(hcd, xhci,
  661. (struct usb_hub_descriptor *) buf);
  662. break;
  663. case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
  664. if ((wValue & 0xff00) != (USB_DT_BOS << 8))
  665. goto error;
  666. if (hcd->speed != HCD_USB3)
  667. goto error;
  668. memcpy(buf, &usb_bos_descriptor,
  669. USB_DT_BOS_SIZE + USB_DT_USB_SS_CAP_SIZE);
  670. temp = xhci_readl(xhci, &xhci->cap_regs->hcs_params3);
  671. buf[12] = HCS_U1_LATENCY(temp);
  672. put_unaligned_le16(HCS_U2_LATENCY(temp), &buf[13]);
  673. spin_unlock_irqrestore(&xhci->lock, flags);
  674. return USB_DT_BOS_SIZE + USB_DT_USB_SS_CAP_SIZE;
  675. case GetPortStatus:
  676. if (!wIndex || wIndex > max_ports)
  677. goto error;
  678. wIndex--;
  679. status = 0;
  680. temp = xhci_readl(xhci, port_array[wIndex]);
  681. if (temp == 0xffffffff) {
  682. retval = -ENODEV;
  683. break;
  684. }
  685. xhci_dbg(xhci, "get port status, actual port %d status = 0x%x\n", wIndex, temp);
  686. /* wPortChange bits */
  687. if (temp & PORT_CSC)
  688. status |= USB_PORT_STAT_C_CONNECTION << 16;
  689. if (temp & PORT_PEC)
  690. status |= USB_PORT_STAT_C_ENABLE << 16;
  691. if ((temp & PORT_OCC))
  692. status |= USB_PORT_STAT_C_OVERCURRENT << 16;
  693. if ((temp & PORT_RC))
  694. status |= USB_PORT_STAT_C_RESET << 16;
  695. /* USB3.0 only */
  696. if (hcd->speed == HCD_USB3) {
  697. if ((temp & PORT_PLC))
  698. status |= USB_PORT_STAT_C_LINK_STATE << 16;
  699. if ((temp & PORT_WRC))
  700. status |= USB_PORT_STAT_C_BH_RESET << 16;
  701. if ((temp & PORT_CEC))
  702. status |= USB_PORT_STAT_C_CONFIG_ERROR << 16;
  703. }
  704. if (hcd->speed != HCD_USB3) {
  705. if ((temp & PORT_PLS_MASK) == XDEV_U3
  706. && (temp & PORT_POWER))
  707. status |= USB_PORT_STAT_SUSPEND;
  708. }
  709. if ((temp & PORT_PLS_MASK) == XDEV_RESUME &&
  710. !DEV_SUPERSPEED(temp)) {
  711. if ((temp & PORT_RESET) || !(temp & PORT_PE))
  712. goto error;
  713. if (time_after_eq(jiffies,
  714. bus_state->resume_done[wIndex])) {
  715. xhci_dbg(xhci, "Resume USB2 port %d\n",
  716. wIndex + 1);
  717. bus_state->resume_done[wIndex] = 0;
  718. clear_bit(wIndex, &bus_state->resuming_ports);
  719. xhci_set_link_state(xhci, port_array, wIndex,
  720. XDEV_U0);
  721. xhci_dbg(xhci, "set port %d resume\n",
  722. wIndex + 1);
  723. slot_id = xhci_find_slot_id_by_port(hcd, xhci,
  724. wIndex + 1);
  725. if (!slot_id) {
  726. xhci_dbg(xhci, "slot_id is zero\n");
  727. goto error;
  728. }
  729. xhci_ring_device(xhci, slot_id);
  730. bus_state->port_c_suspend |= 1 << wIndex;
  731. bus_state->suspended_ports &= ~(1 << wIndex);
  732. } else {
  733. /*
  734. * The resume has been signaling for less than
  735. * 20ms. Report the port status as SUSPEND,
  736. * let the usbcore check port status again
  737. * and clear resume signaling later.
  738. */
  739. status |= USB_PORT_STAT_SUSPEND;
  740. }
  741. }
  742. if ((temp & PORT_PLS_MASK) == XDEV_U0
  743. && (temp & PORT_POWER)
  744. && (bus_state->suspended_ports & (1 << wIndex))) {
  745. bus_state->suspended_ports &= ~(1 << wIndex);
  746. if (hcd->speed != HCD_USB3)
  747. bus_state->port_c_suspend |= 1 << wIndex;
  748. }
  749. if (temp & PORT_CONNECT) {
  750. status |= USB_PORT_STAT_CONNECTION;
  751. status |= xhci_port_speed(temp);
  752. }
  753. if (temp & PORT_PE)
  754. status |= USB_PORT_STAT_ENABLE;
  755. if (temp & PORT_OC)
  756. status |= USB_PORT_STAT_OVERCURRENT;
  757. if (temp & PORT_RESET)
  758. status |= USB_PORT_STAT_RESET;
  759. if (temp & PORT_POWER) {
  760. if (hcd->speed == HCD_USB3)
  761. status |= USB_SS_PORT_STAT_POWER;
  762. else
  763. status |= USB_PORT_STAT_POWER;
  764. }
  765. /* Update Port Link State for super speed ports*/
  766. if (hcd->speed == HCD_USB3) {
  767. xhci_hub_report_link_state(xhci, &status, temp);
  768. /*
  769. * Verify if all USB3 Ports Have entered U0 already.
  770. * Delete Compliance Mode Timer if so.
  771. */
  772. xhci_del_comp_mod_timer(xhci, temp, wIndex);
  773. }
  774. if (bus_state->port_c_suspend & (1 << wIndex))
  775. status |= 1 << USB_PORT_FEAT_C_SUSPEND;
  776. xhci_dbg(xhci, "Get port status returned 0x%x\n", status);
  777. put_unaligned(cpu_to_le32(status), (__le32 *) buf);
  778. break;
  779. case SetPortFeature:
  780. /* The MSB of wIndex is the TEST Mode */
  781. test_mode = (wIndex & 0xff00) >> 8;
  782. if (wValue == USB_PORT_FEAT_LINK_STATE)
  783. link_state = (wIndex & 0xff00) >> 3;
  784. if (wValue == USB_PORT_FEAT_REMOTE_WAKE_MASK)
  785. wake_mask = wIndex & 0xff00;
  786. wIndex &= 0xff;
  787. if (!wIndex || wIndex > max_ports)
  788. goto error;
  789. wIndex--;
  790. temp = xhci_readl(xhci, port_array[wIndex]);
  791. if (temp == 0xffffffff) {
  792. retval = -ENODEV;
  793. break;
  794. }
  795. temp = xhci_port_state_to_neutral(temp);
  796. /* FIXME: What new port features do we need to support? */
  797. switch (wValue) {
  798. case USB_PORT_FEAT_SUSPEND:
  799. temp = xhci_readl(xhci, port_array[wIndex]);
  800. if ((temp & PORT_PLS_MASK) != XDEV_U0) {
  801. /* Resume the port to U0 first */
  802. xhci_set_link_state(xhci, port_array, wIndex,
  803. XDEV_U0);
  804. spin_unlock_irqrestore(&xhci->lock, flags);
  805. msleep(10);
  806. spin_lock_irqsave(&xhci->lock, flags);
  807. }
  808. /* In spec software should not attempt to suspend
  809. * a port unless the port reports that it is in the
  810. * enabled (PED = ‘1’,PLS < ‘3’) state.
  811. */
  812. temp = xhci_readl(xhci, port_array[wIndex]);
  813. if ((temp & PORT_PE) == 0 || (temp & PORT_RESET)
  814. || (temp & PORT_PLS_MASK) >= XDEV_U3) {
  815. xhci_warn(xhci, "USB core suspending device "
  816. "not in U0/U1/U2.\n");
  817. goto error;
  818. }
  819. slot_id = xhci_find_slot_id_by_port(hcd, xhci,
  820. wIndex + 1);
  821. if (!slot_id) {
  822. xhci_warn(xhci, "slot_id is zero\n");
  823. goto error;
  824. }
  825. /* unlock to execute stop endpoint commands */
  826. spin_unlock_irqrestore(&xhci->lock, flags);
  827. xhci_stop_device(xhci, slot_id, 1);
  828. spin_lock_irqsave(&xhci->lock, flags);
  829. xhci_set_link_state(xhci, port_array, wIndex, XDEV_U3);
  830. spin_unlock_irqrestore(&xhci->lock, flags);
  831. msleep(10); /* wait device to enter */
  832. spin_lock_irqsave(&xhci->lock, flags);
  833. temp = xhci_readl(xhci, port_array[wIndex]);
  834. bus_state->suspended_ports |= 1 << wIndex;
  835. break;
  836. case USB_PORT_FEAT_LINK_STATE:
  837. temp = xhci_readl(xhci, port_array[wIndex]);
  838. /* Disable port */
  839. if (link_state == USB_SS_PORT_LS_SS_DISABLED) {
  840. xhci_dbg(xhci, "Disable port %d\n", wIndex);
  841. temp = xhci_port_state_to_neutral(temp);
  842. /*
  843. * Clear all change bits, so that we get a new
  844. * connection event.
  845. */
  846. temp |= PORT_CSC | PORT_PEC | PORT_WRC |
  847. PORT_OCC | PORT_RC | PORT_PLC |
  848. PORT_CEC;
  849. xhci_writel(xhci, temp | PORT_PE,
  850. port_array[wIndex]);
  851. temp = xhci_readl(xhci, port_array[wIndex]);
  852. break;
  853. }
  854. /* Put link in RxDetect (enable port) */
  855. if (link_state == USB_SS_PORT_LS_RX_DETECT) {
  856. xhci_dbg(xhci, "Enable port %d\n", wIndex);
  857. xhci_set_link_state(xhci, port_array, wIndex,
  858. link_state);
  859. temp = xhci_readl(xhci, port_array[wIndex]);
  860. break;
  861. }
  862. /* Software should not attempt to set
  863. * port link state above '3' (U3) and the port
  864. * must be enabled.
  865. */
  866. if ((temp & PORT_PE) == 0 ||
  867. (link_state > USB_SS_PORT_LS_U3)) {
  868. xhci_warn(xhci, "Cannot set link state.\n");
  869. goto error;
  870. }
  871. if (link_state == USB_SS_PORT_LS_U3) {
  872. slot_id = xhci_find_slot_id_by_port(hcd, xhci,
  873. wIndex + 1);
  874. if (slot_id) {
  875. /* unlock to execute stop endpoint
  876. * commands */
  877. spin_unlock_irqrestore(&xhci->lock,
  878. flags);
  879. xhci_stop_device(xhci, slot_id, 1);
  880. spin_lock_irqsave(&xhci->lock, flags);
  881. }
  882. }
  883. xhci_set_link_state(xhci, port_array, wIndex,
  884. link_state);
  885. spin_unlock_irqrestore(&xhci->lock, flags);
  886. msleep(20); /* wait device to enter */
  887. spin_lock_irqsave(&xhci->lock, flags);
  888. temp = xhci_readl(xhci, port_array[wIndex]);
  889. if (link_state == USB_SS_PORT_LS_U3)
  890. bus_state->suspended_ports |= 1 << wIndex;
  891. break;
  892. case USB_PORT_FEAT_POWER:
  893. /*
  894. * Turn on ports, even if there isn't per-port switching.
  895. * HC will report connect events even before this is set.
  896. * However, khubd will ignore the roothub events until
  897. * the roothub is registered.
  898. */
  899. xhci_writel(xhci, temp | PORT_POWER,
  900. port_array[wIndex]);
  901. if (xhci->quirks & XHCI_PORTSC_DELAY)
  902. ndelay(100);
  903. temp = xhci_readl(xhci, port_array[wIndex]);
  904. xhci_dbg(xhci, "set port power, actual port %d status = 0x%x\n", wIndex, temp);
  905. break;
  906. case USB_PORT_FEAT_RESET:
  907. temp = (temp | PORT_RESET);
  908. xhci_writel(xhci, temp, port_array[wIndex]);
  909. if (xhci->quirks & XHCI_PORTSC_DELAY)
  910. ndelay(100);
  911. temp = xhci_readl(xhci, port_array[wIndex]);
  912. xhci_dbg(xhci, "set port reset, actual port %d status = 0x%x\n", wIndex, temp);
  913. break;
  914. case USB_PORT_FEAT_REMOTE_WAKE_MASK:
  915. xhci_set_remote_wake_mask(xhci, port_array,
  916. wIndex, wake_mask);
  917. temp = xhci_readl(xhci, port_array[wIndex]);
  918. xhci_dbg(xhci, "set port remote wake mask, "
  919. "actual port %d status = 0x%x\n",
  920. wIndex, temp);
  921. break;
  922. case USB_PORT_FEAT_BH_PORT_RESET:
  923. temp |= PORT_WR;
  924. xhci_writel(xhci, temp, port_array[wIndex]);
  925. if (xhci->quirks & XHCI_PORTSC_DELAY)
  926. ndelay(100);
  927. temp = xhci_readl(xhci, port_array[wIndex]);
  928. break;
  929. case USB_PORT_FEAT_TEST:
  930. slot_id = xhci_find_slot_id_by_port(hcd, xhci,
  931. wIndex + 1);
  932. if (test_mode && test_mode <= 5) {
  933. /* unlock to execute stop endpoint commands */
  934. spin_unlock_irqrestore(&xhci->lock, flags);
  935. xhci_stop_device(xhci, slot_id, 1);
  936. spin_lock_irqsave(&xhci->lock, flags);
  937. xhci_halt(xhci);
  938. temp = xhci_readl(xhci, port_array[wIndex] + 1);
  939. temp |= test_mode << 28;
  940. xhci_writel(xhci, temp, port_array[wIndex] + 1);
  941. } else if (test_mode == 6) {
  942. spin_unlock_irqrestore(&xhci->lock, flags);
  943. retval = xhci_ehset_single_step_set_feature(hcd,
  944. wIndex);
  945. spin_lock_irqsave(&xhci->lock, flags);
  946. } else {
  947. goto error;
  948. }
  949. break;
  950. default:
  951. goto error;
  952. }
  953. /* unblock any posted writes */
  954. temp = xhci_readl(xhci, port_array[wIndex]);
  955. break;
  956. case ClearPortFeature:
  957. if (!wIndex || wIndex > max_ports)
  958. goto error;
  959. wIndex--;
  960. temp = xhci_readl(xhci, port_array[wIndex]);
  961. if (temp == 0xffffffff) {
  962. retval = -ENODEV;
  963. break;
  964. }
  965. /* FIXME: What new port features do we need to support? */
  966. temp = xhci_port_state_to_neutral(temp);
  967. switch (wValue) {
  968. case USB_PORT_FEAT_SUSPEND:
  969. temp = xhci_readl(xhci, port_array[wIndex]);
  970. xhci_dbg(xhci, "clear USB_PORT_FEAT_SUSPEND\n");
  971. xhci_dbg(xhci, "PORTSC %04x\n", temp);
  972. if (temp & PORT_RESET)
  973. goto error;
  974. if ((temp & PORT_PLS_MASK) == XDEV_U3) {
  975. if ((temp & PORT_PE) == 0)
  976. goto error;
  977. xhci_set_link_state(xhci, port_array, wIndex,
  978. XDEV_RESUME);
  979. spin_unlock_irqrestore(&xhci->lock, flags);
  980. msleep(20);
  981. spin_lock_irqsave(&xhci->lock, flags);
  982. xhci_set_link_state(xhci, port_array, wIndex,
  983. XDEV_U0);
  984. }
  985. bus_state->port_c_suspend |= 1 << wIndex;
  986. slot_id = xhci_find_slot_id_by_port(hcd, xhci,
  987. wIndex + 1);
  988. if (!slot_id) {
  989. xhci_dbg(xhci, "slot_id is zero\n");
  990. goto error;
  991. }
  992. xhci_ring_device(xhci, slot_id);
  993. break;
  994. case USB_PORT_FEAT_C_SUSPEND:
  995. bus_state->port_c_suspend &= ~(1 << wIndex);
  996. case USB_PORT_FEAT_C_RESET:
  997. case USB_PORT_FEAT_C_BH_PORT_RESET:
  998. case USB_PORT_FEAT_C_CONNECTION:
  999. case USB_PORT_FEAT_C_OVER_CURRENT:
  1000. case USB_PORT_FEAT_C_ENABLE:
  1001. case USB_PORT_FEAT_C_PORT_LINK_STATE:
  1002. case USB_PORT_FEAT_C_PORT_CONFIG_ERROR:
  1003. xhci_clear_port_change_bit(xhci, wValue, wIndex,
  1004. port_array[wIndex], temp);
  1005. break;
  1006. case USB_PORT_FEAT_ENABLE:
  1007. xhci_disable_port(hcd, xhci, wIndex,
  1008. port_array[wIndex], temp);
  1009. break;
  1010. default:
  1011. goto error;
  1012. }
  1013. break;
  1014. default:
  1015. error:
  1016. /* "stall" on error */
  1017. retval = -EPIPE;
  1018. }
  1019. spin_unlock_irqrestore(&xhci->lock, flags);
  1020. return retval;
  1021. }
  1022. /*
  1023. * Returns 0 if the status hasn't changed, or the number of bytes in buf.
  1024. * Ports are 0-indexed from the HCD point of view,
  1025. * and 1-indexed from the USB core pointer of view.
  1026. *
  1027. * Note that the status change bits will be cleared as soon as a port status
  1028. * change event is generated, so we use the saved status from that event.
  1029. */
  1030. int xhci_hub_status_data(struct usb_hcd *hcd, char *buf)
  1031. {
  1032. unsigned long flags;
  1033. u32 temp, status;
  1034. u32 mask;
  1035. int i, retval;
  1036. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  1037. int max_ports;
  1038. __le32 __iomem **port_array;
  1039. struct xhci_bus_state *bus_state;
  1040. bool reset_change = false;
  1041. max_ports = xhci_get_ports(hcd, &port_array);
  1042. bus_state = &xhci->bus_state[hcd_index(hcd)];
  1043. /* Initial status is no changes */
  1044. retval = (max_ports + 8) / 8;
  1045. memset(buf, 0, retval);
  1046. /*
  1047. * Inform the usbcore about resume-in-progress by returning
  1048. * a non-zero value even if there are no status changes.
  1049. */
  1050. status = bus_state->resuming_ports;
  1051. mask = PORT_CSC | PORT_PEC | PORT_OCC | PORT_PLC | PORT_WRC | PORT_CEC;
  1052. spin_lock_irqsave(&xhci->lock, flags);
  1053. /* For each port, did anything change? If so, set that bit in buf. */
  1054. for (i = 0; i < max_ports; i++) {
  1055. temp = xhci_readl(xhci, port_array[i]);
  1056. if (temp == 0xffffffff) {
  1057. retval = -ENODEV;
  1058. break;
  1059. }
  1060. if ((temp & mask) != 0 ||
  1061. (bus_state->port_c_suspend & 1 << i) ||
  1062. (bus_state->resume_done[i] && time_after_eq(
  1063. jiffies, bus_state->resume_done[i]))) {
  1064. buf[(i + 1) / 8] |= 1 << (i + 1) % 8;
  1065. status = 1;
  1066. }
  1067. if ((temp & PORT_RC))
  1068. reset_change = true;
  1069. }
  1070. if (!status && !reset_change) {
  1071. xhci_dbg(xhci, "%s: stopping port polling.\n", __func__);
  1072. clear_bit(HCD_FLAG_POLL_RH, &hcd->flags);
  1073. }
  1074. spin_unlock_irqrestore(&xhci->lock, flags);
  1075. return status ? retval : 0;
  1076. }
  1077. #ifdef CONFIG_PM
  1078. int xhci_bus_suspend(struct usb_hcd *hcd)
  1079. {
  1080. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  1081. int max_ports, port_index;
  1082. __le32 __iomem **port_array;
  1083. struct xhci_bus_state *bus_state;
  1084. unsigned long flags;
  1085. max_ports = xhci_get_ports(hcd, &port_array);
  1086. bus_state = &xhci->bus_state[hcd_index(hcd)];
  1087. spin_lock_irqsave(&xhci->lock, flags);
  1088. if (hcd->self.root_hub->do_remote_wakeup) {
  1089. if (bus_state->resuming_ports || /* USB2 */
  1090. bus_state->port_remote_wakeup) { /* USB3 */
  1091. spin_unlock_irqrestore(&xhci->lock, flags);
  1092. xhci_dbg(xhci, "suspend failed because a port is resuming\n");
  1093. return -EBUSY;
  1094. }
  1095. }
  1096. port_index = max_ports;
  1097. bus_state->bus_suspended = 0;
  1098. while (port_index--) {
  1099. /* suspend the port if the port is not suspended */
  1100. u32 t1, t2;
  1101. int slot_id;
  1102. t1 = xhci_readl(xhci, port_array[port_index]);
  1103. t2 = xhci_port_state_to_neutral(t1);
  1104. if ((t1 & PORT_PE) && !(t1 & PORT_PLS_MASK)) {
  1105. xhci_dbg(xhci, "port %d not suspended\n", port_index);
  1106. slot_id = xhci_find_slot_id_by_port(hcd, xhci,
  1107. port_index + 1);
  1108. if (slot_id) {
  1109. spin_unlock_irqrestore(&xhci->lock, flags);
  1110. xhci_stop_device(xhci, slot_id, 1);
  1111. spin_lock_irqsave(&xhci->lock, flags);
  1112. }
  1113. t2 &= ~PORT_PLS_MASK;
  1114. t2 |= PORT_LINK_STROBE | XDEV_U3;
  1115. set_bit(port_index, &bus_state->bus_suspended);
  1116. }
  1117. /* USB core sets remote wake mask for USB 3.0 hubs,
  1118. * including the USB 3.0 roothub, but only if CONFIG_USB_SUSPEND
  1119. * is enabled, so also enable remote wake here.
  1120. */
  1121. if (hcd->self.root_hub->do_remote_wakeup) {
  1122. if (t1 & PORT_CONNECT) {
  1123. t2 |= PORT_WKOC_E | PORT_WKDISC_E;
  1124. t2 &= ~PORT_WKCONN_E;
  1125. } else {
  1126. t2 |= PORT_WKOC_E | PORT_WKCONN_E;
  1127. t2 &= ~PORT_WKDISC_E;
  1128. }
  1129. } else
  1130. t2 &= ~PORT_WAKE_BITS;
  1131. t1 = xhci_port_state_to_neutral(t1);
  1132. if (t1 != t2) {
  1133. xhci_writel(xhci, t2, port_array[port_index]);
  1134. if (xhci->quirks & XHCI_PORTSC_DELAY)
  1135. ndelay(100);
  1136. }
  1137. }
  1138. hcd->state = HC_STATE_SUSPENDED;
  1139. bus_state->next_statechange = jiffies + msecs_to_jiffies(10);
  1140. spin_unlock_irqrestore(&xhci->lock, flags);
  1141. return 0;
  1142. }
  1143. int xhci_bus_resume(struct usb_hcd *hcd)
  1144. {
  1145. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  1146. int max_ports, port_index;
  1147. __le32 __iomem **port_array;
  1148. struct xhci_bus_state *bus_state;
  1149. u32 temp;
  1150. unsigned long flags;
  1151. max_ports = xhci_get_ports(hcd, &port_array);
  1152. bus_state = &xhci->bus_state[hcd_index(hcd)];
  1153. if (time_before(jiffies, bus_state->next_statechange))
  1154. msleep(5);
  1155. spin_lock_irqsave(&xhci->lock, flags);
  1156. if (!HCD_HW_ACCESSIBLE(hcd)) {
  1157. spin_unlock_irqrestore(&xhci->lock, flags);
  1158. return -ESHUTDOWN;
  1159. }
  1160. /* delay the irqs */
  1161. temp = xhci_readl(xhci, &xhci->op_regs->command);
  1162. temp &= ~CMD_EIE;
  1163. xhci_writel(xhci, temp, &xhci->op_regs->command);
  1164. port_index = max_ports;
  1165. while (port_index--) {
  1166. /* Check whether need resume ports. If needed
  1167. resume port and disable remote wakeup */
  1168. u32 temp;
  1169. int slot_id;
  1170. temp = xhci_readl(xhci, port_array[port_index]);
  1171. if (DEV_SUPERSPEED(temp))
  1172. temp &= ~(PORT_RWC_BITS | PORT_CEC | PORT_WAKE_BITS);
  1173. else
  1174. temp &= ~(PORT_RWC_BITS | PORT_WAKE_BITS);
  1175. if (test_bit(port_index, &bus_state->bus_suspended) &&
  1176. (temp & PORT_PLS_MASK)) {
  1177. if (DEV_SUPERSPEED(temp)) {
  1178. xhci_set_link_state(xhci, port_array,
  1179. port_index, XDEV_U0);
  1180. } else {
  1181. xhci_set_link_state(xhci, port_array,
  1182. port_index, XDEV_RESUME);
  1183. spin_unlock_irqrestore(&xhci->lock, flags);
  1184. msleep(20);
  1185. spin_lock_irqsave(&xhci->lock, flags);
  1186. xhci_set_link_state(xhci, port_array,
  1187. port_index, XDEV_U0);
  1188. }
  1189. /* wait for the port to enter U0 and report port link
  1190. * state change.
  1191. */
  1192. spin_unlock_irqrestore(&xhci->lock, flags);
  1193. msleep(20);
  1194. spin_lock_irqsave(&xhci->lock, flags);
  1195. /* Clear PLC */
  1196. xhci_test_and_clear_bit(xhci, port_array, port_index,
  1197. PORT_PLC);
  1198. slot_id = xhci_find_slot_id_by_port(hcd,
  1199. xhci, port_index + 1);
  1200. if (slot_id)
  1201. xhci_ring_device(xhci, slot_id);
  1202. } else {
  1203. xhci_writel(xhci, temp, port_array[port_index]);
  1204. if (xhci->quirks & XHCI_PORTSC_DELAY)
  1205. ndelay(100);
  1206. }
  1207. }
  1208. (void) xhci_readl(xhci, &xhci->op_regs->command);
  1209. bus_state->next_statechange = jiffies + msecs_to_jiffies(5);
  1210. /* re-enable irqs */
  1211. temp = xhci_readl(xhci, &xhci->op_regs->command);
  1212. temp |= CMD_EIE;
  1213. xhci_writel(xhci, temp, &xhci->op_regs->command);
  1214. temp = xhci_readl(xhci, &xhci->op_regs->command);
  1215. spin_unlock_irqrestore(&xhci->lock, flags);
  1216. return 0;
  1217. }
  1218. #endif /* CONFIG_PM */