xhci-hub.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531
  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/slab.h>
  23. #include <asm/unaligned.h>
  24. #include "xhci.h"
  25. #include "xhci-trace.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 3 BOS descriptor and a capability descriptors, combined.
  30. * Fields will be adjusted and added later in xhci_create_usb3_bos_desc()
  31. */
  32. static u8 usb_bos_descriptor [] = {
  33. USB_DT_BOS_SIZE, /* __u8 bLength, 5 bytes */
  34. USB_DT_BOS, /* __u8 bDescriptorType */
  35. 0x0F, 0x00, /* __le16 wTotalLength, 15 bytes */
  36. 0x1, /* __u8 bNumDeviceCaps */
  37. /* First device capability, SuperSpeed */
  38. USB_DT_USB_SS_CAP_SIZE, /* __u8 bLength, 10 bytes */
  39. USB_DT_DEVICE_CAPABILITY, /* Device Capability */
  40. USB_SS_CAP_TYPE, /* bDevCapabilityType, SUPERSPEED_USB */
  41. 0x00, /* bmAttributes, LTM off by default */
  42. USB_5GBPS_OPERATION, 0x00, /* wSpeedsSupported, 5Gbps only */
  43. 0x03, /* bFunctionalitySupport,
  44. USB 3.0 speed only */
  45. 0x00, /* bU1DevExitLat, set later. */
  46. 0x00, 0x00, /* __le16 bU2DevExitLat, set later. */
  47. /* Second device capability, SuperSpeedPlus */
  48. 0x0c, /* bLength 12, will be adjusted later */
  49. USB_DT_DEVICE_CAPABILITY, /* Device Capability */
  50. USB_SSP_CAP_TYPE, /* bDevCapabilityType SUPERSPEED_PLUS */
  51. 0x00, /* bReserved 0 */
  52. 0x00, 0x00, 0x00, 0x00, /* bmAttributes, get from xhci psic */
  53. 0x00, 0x00, /* wFunctionalitySupport */
  54. 0x00, 0x00, /* wReserved 0 */
  55. /* Sublink Speed Attributes are added in xhci_create_usb3_bos_desc() */
  56. };
  57. static int xhci_create_usb3_bos_desc(struct xhci_hcd *xhci, char *buf,
  58. u16 wLength)
  59. {
  60. int i, ssa_count;
  61. u32 temp;
  62. u16 desc_size, ssp_cap_size, ssa_size = 0;
  63. bool usb3_1 = false;
  64. desc_size = USB_DT_BOS_SIZE + USB_DT_USB_SS_CAP_SIZE;
  65. ssp_cap_size = sizeof(usb_bos_descriptor) - desc_size;
  66. /* does xhci support USB 3.1 Enhanced SuperSpeed */
  67. if (xhci->usb3_rhub.min_rev >= 0x01 && xhci->usb3_rhub.psi_uid_count) {
  68. /* two SSA entries for each unique PSI ID, one RX and one TX */
  69. ssa_count = xhci->usb3_rhub.psi_uid_count * 2;
  70. ssa_size = ssa_count * sizeof(u32);
  71. desc_size += ssp_cap_size;
  72. usb3_1 = true;
  73. }
  74. memcpy(buf, &usb_bos_descriptor, min(desc_size, wLength));
  75. if (usb3_1) {
  76. /* modify bos descriptor bNumDeviceCaps and wTotalLength */
  77. buf[4] += 1;
  78. put_unaligned_le16(desc_size + ssa_size, &buf[2]);
  79. }
  80. if (wLength < USB_DT_BOS_SIZE + USB_DT_USB_SS_CAP_SIZE)
  81. return wLength;
  82. /* Indicate whether the host has LTM support. */
  83. temp = readl(&xhci->cap_regs->hcc_params);
  84. if (HCC_LTC(temp))
  85. buf[8] |= USB_LTM_SUPPORT;
  86. /* Set the U1 and U2 exit latencies. */
  87. if ((xhci->quirks & XHCI_LPM_SUPPORT)) {
  88. temp = readl(&xhci->cap_regs->hcs_params3);
  89. buf[12] = HCS_U1_LATENCY(temp);
  90. put_unaligned_le16(HCS_U2_LATENCY(temp), &buf[13]);
  91. }
  92. if (usb3_1) {
  93. u32 ssp_cap_base, bm_attrib, psi;
  94. int offset;
  95. ssp_cap_base = USB_DT_BOS_SIZE + USB_DT_USB_SS_CAP_SIZE;
  96. if (wLength < desc_size)
  97. return wLength;
  98. buf[ssp_cap_base] = ssp_cap_size + ssa_size;
  99. /* attribute count SSAC bits 4:0 and ID count SSIC bits 8:5 */
  100. bm_attrib = (ssa_count - 1) & 0x1f;
  101. bm_attrib |= (xhci->usb3_rhub.psi_uid_count - 1) << 5;
  102. put_unaligned_le32(bm_attrib, &buf[ssp_cap_base + 4]);
  103. if (wLength < desc_size + ssa_size)
  104. return wLength;
  105. /*
  106. * Create the Sublink Speed Attributes (SSA) array.
  107. * The xhci PSI field and USB 3.1 SSA fields are very similar,
  108. * but link type bits 7:6 differ for values 01b and 10b.
  109. * xhci has also only one PSI entry for a symmetric link when
  110. * USB 3.1 requires two SSA entries (RX and TX) for every link
  111. */
  112. offset = desc_size;
  113. for (i = 0; i < xhci->usb3_rhub.psi_count; i++) {
  114. psi = xhci->usb3_rhub.psi[i];
  115. psi &= ~USB_SSP_SUBLINK_SPEED_RSVD;
  116. if ((psi & PLT_MASK) == PLT_SYM) {
  117. /* Symmetric, create SSA RX and TX from one PSI entry */
  118. put_unaligned_le32(psi, &buf[offset]);
  119. psi |= 1 << 7; /* turn entry to TX */
  120. offset += 4;
  121. if (offset >= desc_size + ssa_size)
  122. return desc_size + ssa_size;
  123. } else if ((psi & PLT_MASK) == PLT_ASYM_RX) {
  124. /* Asymetric RX, flip bits 7:6 for SSA */
  125. psi ^= PLT_MASK;
  126. }
  127. put_unaligned_le32(psi, &buf[offset]);
  128. offset += 4;
  129. if (offset >= desc_size + ssa_size)
  130. return desc_size + ssa_size;
  131. }
  132. }
  133. /* ssa_size is 0 for other than usb 3.1 hosts */
  134. return desc_size + ssa_size;
  135. }
  136. static void xhci_common_hub_descriptor(struct xhci_hcd *xhci,
  137. struct usb_hub_descriptor *desc, int ports)
  138. {
  139. u16 temp;
  140. desc->bPwrOn2PwrGood = 10; /* xhci section 5.4.9 says 20ms max */
  141. desc->bHubContrCurrent = 0;
  142. desc->bNbrPorts = ports;
  143. temp = 0;
  144. /* Bits 1:0 - support per-port power switching, or power always on */
  145. if (HCC_PPC(xhci->hcc_params))
  146. temp |= HUB_CHAR_INDV_PORT_LPSM;
  147. else
  148. temp |= HUB_CHAR_NO_LPSM;
  149. /* Bit 2 - root hubs are not part of a compound device */
  150. /* Bits 4:3 - individual port over current protection */
  151. temp |= HUB_CHAR_INDV_PORT_OCPM;
  152. /* Bits 6:5 - no TTs in root ports */
  153. /* Bit 7 - no port indicators */
  154. desc->wHubCharacteristics = cpu_to_le16(temp);
  155. }
  156. /* Fill in the USB 2.0 roothub descriptor */
  157. static void xhci_usb2_hub_descriptor(struct usb_hcd *hcd, struct xhci_hcd *xhci,
  158. struct usb_hub_descriptor *desc)
  159. {
  160. int ports;
  161. u16 temp;
  162. __u8 port_removable[(USB_MAXCHILDREN + 1 + 7) / 8];
  163. u32 portsc;
  164. unsigned int i;
  165. ports = xhci->num_usb2_ports;
  166. xhci_common_hub_descriptor(xhci, desc, ports);
  167. desc->bDescriptorType = USB_DT_HUB;
  168. temp = 1 + (ports / 8);
  169. desc->bDescLength = USB_DT_HUB_NONVAR_SIZE + 2 * temp;
  170. /* The Device Removable bits are reported on a byte granularity.
  171. * If the port doesn't exist within that byte, the bit is set to 0.
  172. */
  173. memset(port_removable, 0, sizeof(port_removable));
  174. for (i = 0; i < ports; i++) {
  175. portsc = readl(xhci->usb2_ports[i]);
  176. /* If a device is removable, PORTSC reports a 0, same as in the
  177. * hub descriptor DeviceRemovable bits.
  178. */
  179. if (portsc & PORT_DEV_REMOVE)
  180. /* This math is hairy because bit 0 of DeviceRemovable
  181. * is reserved, and bit 1 is for port 1, etc.
  182. */
  183. port_removable[(i + 1) / 8] |= 1 << ((i + 1) % 8);
  184. }
  185. /* ch11.h defines a hub descriptor that has room for USB_MAXCHILDREN
  186. * ports on it. The USB 2.0 specification says that there are two
  187. * variable length fields at the end of the hub descriptor:
  188. * DeviceRemovable and PortPwrCtrlMask. But since we can have less than
  189. * USB_MAXCHILDREN ports, we may need to use the DeviceRemovable array
  190. * to set PortPwrCtrlMask bits. PortPwrCtrlMask must always be set to
  191. * 0xFF, so we initialize the both arrays (DeviceRemovable and
  192. * PortPwrCtrlMask) to 0xFF. Then we set the DeviceRemovable for each
  193. * set of ports that actually exist.
  194. */
  195. memset(desc->u.hs.DeviceRemovable, 0xff,
  196. sizeof(desc->u.hs.DeviceRemovable));
  197. memset(desc->u.hs.PortPwrCtrlMask, 0xff,
  198. sizeof(desc->u.hs.PortPwrCtrlMask));
  199. for (i = 0; i < (ports + 1 + 7) / 8; i++)
  200. memset(&desc->u.hs.DeviceRemovable[i], port_removable[i],
  201. sizeof(__u8));
  202. }
  203. /* Fill in the USB 3.0 roothub descriptor */
  204. static void xhci_usb3_hub_descriptor(struct usb_hcd *hcd, struct xhci_hcd *xhci,
  205. struct usb_hub_descriptor *desc)
  206. {
  207. int ports;
  208. u16 port_removable;
  209. u32 portsc;
  210. unsigned int i;
  211. ports = xhci->num_usb3_ports;
  212. xhci_common_hub_descriptor(xhci, desc, ports);
  213. desc->bDescriptorType = USB_DT_SS_HUB;
  214. desc->bDescLength = USB_DT_SS_HUB_SIZE;
  215. /* header decode latency should be zero for roothubs,
  216. * see section 4.23.5.2.
  217. */
  218. desc->u.ss.bHubHdrDecLat = 0;
  219. desc->u.ss.wHubDelay = 0;
  220. port_removable = 0;
  221. /* bit 0 is reserved, bit 1 is for port 1, etc. */
  222. for (i = 0; i < ports; i++) {
  223. portsc = readl(xhci->usb3_ports[i]);
  224. if (portsc & PORT_DEV_REMOVE)
  225. port_removable |= 1 << (i + 1);
  226. }
  227. desc->u.ss.DeviceRemovable = cpu_to_le16(port_removable);
  228. }
  229. static void xhci_hub_descriptor(struct usb_hcd *hcd, struct xhci_hcd *xhci,
  230. struct usb_hub_descriptor *desc)
  231. {
  232. if (hcd->speed >= HCD_USB3)
  233. xhci_usb3_hub_descriptor(hcd, xhci, desc);
  234. else
  235. xhci_usb2_hub_descriptor(hcd, xhci, desc);
  236. }
  237. static unsigned int xhci_port_speed(unsigned int port_status)
  238. {
  239. if (DEV_LOWSPEED(port_status))
  240. return USB_PORT_STAT_LOW_SPEED;
  241. if (DEV_HIGHSPEED(port_status))
  242. return USB_PORT_STAT_HIGH_SPEED;
  243. /*
  244. * FIXME: Yes, we should check for full speed, but the core uses that as
  245. * a default in portspeed() in usb/core/hub.c (which is the only place
  246. * USB_PORT_STAT_*_SPEED is used).
  247. */
  248. return 0;
  249. }
  250. /*
  251. * These bits are Read Only (RO) and should be saved and written to the
  252. * registers: 0, 3, 10:13, 30
  253. * connect status, over-current status, port speed, and device removable.
  254. * connect status and port speed are also sticky - meaning they're in
  255. * the AUX well and they aren't changed by a hot, warm, or cold reset.
  256. */
  257. #define XHCI_PORT_RO ((1<<0) | (1<<3) | (0xf<<10) | (1<<30))
  258. /*
  259. * These bits are RW; writing a 0 clears the bit, writing a 1 sets the bit:
  260. * bits 5:8, 9, 14:15, 25:27
  261. * link state, port power, port indicator state, "wake on" enable state
  262. */
  263. #define XHCI_PORT_RWS ((0xf<<5) | (1<<9) | (0x3<<14) | (0x7<<25))
  264. /*
  265. * These bits are RW; writing a 1 sets the bit, writing a 0 has no effect:
  266. * bit 4 (port reset)
  267. */
  268. #define XHCI_PORT_RW1S ((1<<4))
  269. /*
  270. * These bits are RW; writing a 1 clears the bit, writing a 0 has no effect:
  271. * bits 1, 17, 18, 19, 20, 21, 22, 23
  272. * port enable/disable, and
  273. * change bits: connect, PED, warm port reset changed (reserved zero for USB 2.0 ports),
  274. * over-current, reset, link state, and L1 change
  275. */
  276. #define XHCI_PORT_RW1CS ((1<<1) | (0x7f<<17))
  277. /*
  278. * Bit 16 is RW, and writing a '1' to it causes the link state control to be
  279. * latched in
  280. */
  281. #define XHCI_PORT_RW ((1<<16))
  282. /*
  283. * These bits are Reserved Zero (RsvdZ) and zero should be written to them:
  284. * bits 2, 24, 28:31
  285. */
  286. #define XHCI_PORT_RZ ((1<<2) | (1<<24) | (0xf<<28))
  287. /*
  288. * Given a port state, this function returns a value that would result in the
  289. * port being in the same state, if the value was written to the port status
  290. * control register.
  291. * Save Read Only (RO) bits and save read/write bits where
  292. * writing a 0 clears the bit and writing a 1 sets the bit (RWS).
  293. * For all other types (RW1S, RW1CS, RW, and RZ), writing a '0' has no effect.
  294. */
  295. u32 xhci_port_state_to_neutral(u32 state)
  296. {
  297. /* Save read-only status and port state */
  298. return (state & XHCI_PORT_RO) | (state & XHCI_PORT_RWS);
  299. }
  300. /*
  301. * find slot id based on port number.
  302. * @port: The one-based port number from one of the two split roothubs.
  303. */
  304. int xhci_find_slot_id_by_port(struct usb_hcd *hcd, struct xhci_hcd *xhci,
  305. u16 port)
  306. {
  307. int slot_id;
  308. int i;
  309. enum usb_device_speed speed;
  310. slot_id = 0;
  311. for (i = 0; i < MAX_HC_SLOTS; i++) {
  312. if (!xhci->devs[i] || !xhci->devs[i]->udev)
  313. continue;
  314. speed = xhci->devs[i]->udev->speed;
  315. if (((speed >= USB_SPEED_SUPER) == (hcd->speed >= HCD_USB3))
  316. && xhci->devs[i]->fake_port == port) {
  317. slot_id = i;
  318. break;
  319. }
  320. }
  321. return slot_id;
  322. }
  323. /*
  324. * Stop device
  325. * It issues stop endpoint command for EP 0 to 30. And wait the last command
  326. * to complete.
  327. * suspend will set to 1, if suspend bit need to set in command.
  328. */
  329. static int xhci_stop_device(struct xhci_hcd *xhci, int slot_id, int suspend)
  330. {
  331. struct xhci_virt_device *virt_dev;
  332. struct xhci_command *cmd;
  333. unsigned long flags;
  334. int ret;
  335. int i;
  336. ret = 0;
  337. virt_dev = xhci->devs[slot_id];
  338. if (!virt_dev)
  339. return -ENODEV;
  340. cmd = xhci_alloc_command(xhci, false, true, GFP_NOIO);
  341. if (!cmd) {
  342. xhci_dbg(xhci, "Couldn't allocate command structure.\n");
  343. return -ENOMEM;
  344. }
  345. spin_lock_irqsave(&xhci->lock, flags);
  346. for (i = LAST_EP_INDEX; i > 0; i--) {
  347. if (virt_dev->eps[i].ring && virt_dev->eps[i].ring->dequeue) {
  348. struct xhci_command *command;
  349. command = xhci_alloc_command(xhci, false, false,
  350. GFP_NOWAIT);
  351. if (!command) {
  352. spin_unlock_irqrestore(&xhci->lock, flags);
  353. ret = -ENOMEM;
  354. goto cmd_cleanup;
  355. }
  356. ret = xhci_queue_stop_endpoint(xhci, command, slot_id,
  357. i, suspend);
  358. if (ret) {
  359. spin_unlock_irqrestore(&xhci->lock, flags);
  360. xhci_free_command(xhci, command);
  361. goto cmd_cleanup;
  362. }
  363. }
  364. }
  365. ret = xhci_queue_stop_endpoint(xhci, cmd, slot_id, 0, suspend);
  366. if (ret) {
  367. spin_unlock_irqrestore(&xhci->lock, flags);
  368. goto cmd_cleanup;
  369. }
  370. xhci_ring_cmd_db(xhci);
  371. spin_unlock_irqrestore(&xhci->lock, flags);
  372. /* Wait for last stop endpoint command to finish */
  373. wait_for_completion(cmd->completion);
  374. if (cmd->status == COMP_CMD_ABORT || cmd->status == COMP_CMD_STOP) {
  375. xhci_warn(xhci, "Timeout while waiting for stop endpoint command\n");
  376. ret = -ETIME;
  377. }
  378. cmd_cleanup:
  379. xhci_free_command(xhci, cmd);
  380. return ret;
  381. }
  382. /*
  383. * Ring device, it rings the all doorbells unconditionally.
  384. */
  385. void xhci_ring_device(struct xhci_hcd *xhci, int slot_id)
  386. {
  387. int i, s;
  388. struct xhci_virt_ep *ep;
  389. for (i = 0; i < LAST_EP_INDEX + 1; i++) {
  390. ep = &xhci->devs[slot_id]->eps[i];
  391. if (ep->ep_state & EP_HAS_STREAMS) {
  392. for (s = 1; s < ep->stream_info->num_streams; s++)
  393. xhci_ring_ep_doorbell(xhci, slot_id, i, s);
  394. } else if (ep->ring && ep->ring->dequeue) {
  395. xhci_ring_ep_doorbell(xhci, slot_id, i, 0);
  396. }
  397. }
  398. return;
  399. }
  400. static void xhci_disable_port(struct usb_hcd *hcd, struct xhci_hcd *xhci,
  401. u16 wIndex, __le32 __iomem *addr, u32 port_status)
  402. {
  403. /* Don't allow the USB core to disable SuperSpeed ports. */
  404. if (hcd->speed >= HCD_USB3) {
  405. xhci_dbg(xhci, "Ignoring request to disable "
  406. "SuperSpeed port.\n");
  407. return;
  408. }
  409. /* Write 1 to disable the port */
  410. writel(port_status | PORT_PE, addr);
  411. port_status = readl(addr);
  412. xhci_dbg(xhci, "disable port, actual port %d status = 0x%x\n",
  413. wIndex, port_status);
  414. }
  415. static void xhci_clear_port_change_bit(struct xhci_hcd *xhci, u16 wValue,
  416. u16 wIndex, __le32 __iomem *addr, u32 port_status)
  417. {
  418. char *port_change_bit;
  419. u32 status;
  420. switch (wValue) {
  421. case USB_PORT_FEAT_C_RESET:
  422. status = PORT_RC;
  423. port_change_bit = "reset";
  424. break;
  425. case USB_PORT_FEAT_C_BH_PORT_RESET:
  426. status = PORT_WRC;
  427. port_change_bit = "warm(BH) reset";
  428. break;
  429. case USB_PORT_FEAT_C_CONNECTION:
  430. status = PORT_CSC;
  431. port_change_bit = "connect";
  432. break;
  433. case USB_PORT_FEAT_C_OVER_CURRENT:
  434. status = PORT_OCC;
  435. port_change_bit = "over-current";
  436. break;
  437. case USB_PORT_FEAT_C_ENABLE:
  438. status = PORT_PEC;
  439. port_change_bit = "enable/disable";
  440. break;
  441. case USB_PORT_FEAT_C_SUSPEND:
  442. status = PORT_PLC;
  443. port_change_bit = "suspend/resume";
  444. break;
  445. case USB_PORT_FEAT_C_PORT_LINK_STATE:
  446. status = PORT_PLC;
  447. port_change_bit = "link state";
  448. break;
  449. case USB_PORT_FEAT_C_PORT_CONFIG_ERROR:
  450. status = PORT_CEC;
  451. port_change_bit = "config error";
  452. break;
  453. default:
  454. /* Should never happen */
  455. return;
  456. }
  457. /* Change bits are all write 1 to clear */
  458. writel(port_status | status, addr);
  459. port_status = readl(addr);
  460. xhci_dbg(xhci, "clear port %s change, actual port %d status = 0x%x\n",
  461. port_change_bit, wIndex, port_status);
  462. }
  463. static int xhci_get_ports(struct usb_hcd *hcd, __le32 __iomem ***port_array)
  464. {
  465. int max_ports;
  466. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  467. if (hcd->speed >= HCD_USB3) {
  468. max_ports = xhci->num_usb3_ports;
  469. *port_array = xhci->usb3_ports;
  470. } else {
  471. max_ports = xhci->num_usb2_ports;
  472. *port_array = xhci->usb2_ports;
  473. }
  474. return max_ports;
  475. }
  476. void xhci_set_link_state(struct xhci_hcd *xhci, __le32 __iomem **port_array,
  477. int port_id, u32 link_state)
  478. {
  479. u32 temp;
  480. temp = readl(port_array[port_id]);
  481. temp = xhci_port_state_to_neutral(temp);
  482. temp &= ~PORT_PLS_MASK;
  483. temp |= PORT_LINK_STROBE | link_state;
  484. writel(temp, port_array[port_id]);
  485. }
  486. static void xhci_set_remote_wake_mask(struct xhci_hcd *xhci,
  487. __le32 __iomem **port_array, int port_id, u16 wake_mask)
  488. {
  489. u32 temp;
  490. temp = readl(port_array[port_id]);
  491. temp = xhci_port_state_to_neutral(temp);
  492. if (wake_mask & USB_PORT_FEAT_REMOTE_WAKE_CONNECT)
  493. temp |= PORT_WKCONN_E;
  494. else
  495. temp &= ~PORT_WKCONN_E;
  496. if (wake_mask & USB_PORT_FEAT_REMOTE_WAKE_DISCONNECT)
  497. temp |= PORT_WKDISC_E;
  498. else
  499. temp &= ~PORT_WKDISC_E;
  500. if (wake_mask & USB_PORT_FEAT_REMOTE_WAKE_OVER_CURRENT)
  501. temp |= PORT_WKOC_E;
  502. else
  503. temp &= ~PORT_WKOC_E;
  504. writel(temp, port_array[port_id]);
  505. }
  506. /* Test and clear port RWC bit */
  507. void xhci_test_and_clear_bit(struct xhci_hcd *xhci, __le32 __iomem **port_array,
  508. int port_id, u32 port_bit)
  509. {
  510. u32 temp;
  511. temp = readl(port_array[port_id]);
  512. if (temp & port_bit) {
  513. temp = xhci_port_state_to_neutral(temp);
  514. temp |= port_bit;
  515. writel(temp, port_array[port_id]);
  516. }
  517. }
  518. /* Updates Link Status for USB 2.1 port */
  519. static void xhci_hub_report_usb2_link_state(u32 *status, u32 status_reg)
  520. {
  521. if ((status_reg & PORT_PLS_MASK) == XDEV_U2)
  522. *status |= USB_PORT_STAT_L1;
  523. }
  524. /* Updates Link Status for super Speed port */
  525. static void xhci_hub_report_usb3_link_state(struct xhci_hcd *xhci,
  526. u32 *status, u32 status_reg)
  527. {
  528. u32 pls = status_reg & PORT_PLS_MASK;
  529. /* resume state is a xHCI internal state.
  530. * Do not report it to usb core, instead, pretend to be U3,
  531. * thus usb core knows it's not ready for transfer
  532. */
  533. if (pls == XDEV_RESUME) {
  534. *status |= USB_SS_PORT_LS_U3;
  535. return;
  536. }
  537. /* When the CAS bit is set then warm reset
  538. * should be performed on port
  539. */
  540. if (status_reg & PORT_CAS) {
  541. /* The CAS bit can be set while the port is
  542. * in any link state.
  543. * Only roothubs have CAS bit, so we
  544. * pretend to be in compliance mode
  545. * unless we're already in compliance
  546. * or the inactive state.
  547. */
  548. if (pls != USB_SS_PORT_LS_COMP_MOD &&
  549. pls != USB_SS_PORT_LS_SS_INACTIVE) {
  550. pls = USB_SS_PORT_LS_COMP_MOD;
  551. }
  552. /* Return also connection bit -
  553. * hub state machine resets port
  554. * when this bit is set.
  555. */
  556. pls |= USB_PORT_STAT_CONNECTION;
  557. } else {
  558. /*
  559. * If CAS bit isn't set but the Port is already at
  560. * Compliance Mode, fake a connection so the USB core
  561. * notices the Compliance state and resets the port.
  562. * This resolves an issue generated by the SN65LVPE502CP
  563. * in which sometimes the port enters compliance mode
  564. * caused by a delay on the host-device negotiation.
  565. */
  566. if ((xhci->quirks & XHCI_COMP_MODE_QUIRK) &&
  567. (pls == USB_SS_PORT_LS_COMP_MOD))
  568. pls |= USB_PORT_STAT_CONNECTION;
  569. }
  570. /* update status field */
  571. *status |= pls;
  572. }
  573. /*
  574. * Function for Compliance Mode Quirk.
  575. *
  576. * This Function verifies if all xhc USB3 ports have entered U0, if so,
  577. * the compliance mode timer is deleted. A port won't enter
  578. * compliance mode if it has previously entered U0.
  579. */
  580. static void xhci_del_comp_mod_timer(struct xhci_hcd *xhci, u32 status,
  581. u16 wIndex)
  582. {
  583. u32 all_ports_seen_u0 = ((1 << xhci->num_usb3_ports)-1);
  584. bool port_in_u0 = ((status & PORT_PLS_MASK) == XDEV_U0);
  585. if (!(xhci->quirks & XHCI_COMP_MODE_QUIRK))
  586. return;
  587. if ((xhci->port_status_u0 != all_ports_seen_u0) && port_in_u0) {
  588. xhci->port_status_u0 |= 1 << wIndex;
  589. if (xhci->port_status_u0 == all_ports_seen_u0) {
  590. del_timer_sync(&xhci->comp_mode_recovery_timer);
  591. xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
  592. "All USB3 ports have entered U0 already!");
  593. xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
  594. "Compliance Mode Recovery Timer Deleted.");
  595. }
  596. }
  597. }
  598. static u32 xhci_get_ext_port_status(u32 raw_port_status, u32 port_li)
  599. {
  600. u32 ext_stat = 0;
  601. int speed_id;
  602. /* only support rx and tx lane counts of 1 in usb3.1 spec */
  603. speed_id = DEV_PORT_SPEED(raw_port_status);
  604. ext_stat |= speed_id; /* bits 3:0, RX speed id */
  605. ext_stat |= speed_id << 4; /* bits 7:4, TX speed id */
  606. ext_stat |= PORT_RX_LANES(port_li) << 8; /* bits 11:8 Rx lane count */
  607. ext_stat |= PORT_TX_LANES(port_li) << 12; /* bits 15:12 Tx lane count */
  608. return ext_stat;
  609. }
  610. /*
  611. * Converts a raw xHCI port status into the format that external USB 2.0 or USB
  612. * 3.0 hubs use.
  613. *
  614. * Possible side effects:
  615. * - Mark a port as being done with device resume,
  616. * and ring the endpoint doorbells.
  617. * - Stop the Synopsys redriver Compliance Mode polling.
  618. * - Drop and reacquire the xHCI lock, in order to wait for port resume.
  619. */
  620. static u32 xhci_get_port_status(struct usb_hcd *hcd,
  621. struct xhci_bus_state *bus_state,
  622. __le32 __iomem **port_array,
  623. u16 wIndex, u32 raw_port_status,
  624. unsigned long flags)
  625. __releases(&xhci->lock)
  626. __acquires(&xhci->lock)
  627. {
  628. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  629. u32 status = 0;
  630. int slot_id;
  631. /* wPortChange bits */
  632. if (raw_port_status & PORT_CSC)
  633. status |= USB_PORT_STAT_C_CONNECTION << 16;
  634. if (raw_port_status & PORT_PEC)
  635. status |= USB_PORT_STAT_C_ENABLE << 16;
  636. if ((raw_port_status & PORT_OCC))
  637. status |= USB_PORT_STAT_C_OVERCURRENT << 16;
  638. if ((raw_port_status & PORT_RC))
  639. status |= USB_PORT_STAT_C_RESET << 16;
  640. /* USB3.0 only */
  641. if (hcd->speed >= HCD_USB3) {
  642. /* Port link change with port in resume state should not be
  643. * reported to usbcore, as this is an internal state to be
  644. * handled by xhci driver. Reporting PLC to usbcore may
  645. * cause usbcore clearing PLC first and port change event
  646. * irq won't be generated.
  647. */
  648. if ((raw_port_status & PORT_PLC) &&
  649. (raw_port_status & PORT_PLS_MASK) != XDEV_RESUME)
  650. status |= USB_PORT_STAT_C_LINK_STATE << 16;
  651. if ((raw_port_status & PORT_WRC))
  652. status |= USB_PORT_STAT_C_BH_RESET << 16;
  653. if ((raw_port_status & PORT_CEC))
  654. status |= USB_PORT_STAT_C_CONFIG_ERROR << 16;
  655. }
  656. if (hcd->speed < HCD_USB3) {
  657. if ((raw_port_status & PORT_PLS_MASK) == XDEV_U3
  658. && (raw_port_status & PORT_POWER))
  659. status |= USB_PORT_STAT_SUSPEND;
  660. }
  661. if ((raw_port_status & PORT_PLS_MASK) == XDEV_RESUME &&
  662. !DEV_SUPERSPEED_ANY(raw_port_status) && hcd->speed < HCD_USB3) {
  663. if ((raw_port_status & PORT_RESET) ||
  664. !(raw_port_status & PORT_PE))
  665. return 0xffffffff;
  666. /* did port event handler already start resume timing? */
  667. if (!bus_state->resume_done[wIndex]) {
  668. /* If not, maybe we are in a host initated resume? */
  669. if (test_bit(wIndex, &bus_state->resuming_ports)) {
  670. /* Host initated resume doesn't time the resume
  671. * signalling using resume_done[].
  672. * It manually sets RESUME state, sleeps 20ms
  673. * and sets U0 state. This should probably be
  674. * changed, but not right now.
  675. */
  676. } else {
  677. /* port resume was discovered now and here,
  678. * start resume timing
  679. */
  680. unsigned long timeout = jiffies +
  681. msecs_to_jiffies(USB_RESUME_TIMEOUT);
  682. set_bit(wIndex, &bus_state->resuming_ports);
  683. bus_state->resume_done[wIndex] = timeout;
  684. mod_timer(&hcd->rh_timer, timeout);
  685. }
  686. /* Has resume been signalled for USB_RESUME_TIME yet? */
  687. } else if (time_after_eq(jiffies,
  688. bus_state->resume_done[wIndex])) {
  689. int time_left;
  690. xhci_dbg(xhci, "Resume USB2 port %d\n",
  691. wIndex + 1);
  692. bus_state->resume_done[wIndex] = 0;
  693. clear_bit(wIndex, &bus_state->resuming_ports);
  694. set_bit(wIndex, &bus_state->rexit_ports);
  695. xhci_test_and_clear_bit(xhci, port_array, wIndex,
  696. PORT_PLC);
  697. xhci_set_link_state(xhci, port_array, wIndex,
  698. XDEV_U0);
  699. spin_unlock_irqrestore(&xhci->lock, flags);
  700. time_left = wait_for_completion_timeout(
  701. &bus_state->rexit_done[wIndex],
  702. msecs_to_jiffies(
  703. XHCI_MAX_REXIT_TIMEOUT_MS));
  704. spin_lock_irqsave(&xhci->lock, flags);
  705. if (time_left) {
  706. slot_id = xhci_find_slot_id_by_port(hcd,
  707. xhci, wIndex + 1);
  708. if (!slot_id) {
  709. xhci_dbg(xhci, "slot_id is zero\n");
  710. return 0xffffffff;
  711. }
  712. xhci_ring_device(xhci, slot_id);
  713. } else {
  714. int port_status = readl(port_array[wIndex]);
  715. xhci_warn(xhci, "Port resume took longer than %i msec, port status = 0x%x\n",
  716. XHCI_MAX_REXIT_TIMEOUT_MS,
  717. port_status);
  718. status |= USB_PORT_STAT_SUSPEND;
  719. clear_bit(wIndex, &bus_state->rexit_ports);
  720. }
  721. bus_state->port_c_suspend |= 1 << wIndex;
  722. bus_state->suspended_ports &= ~(1 << wIndex);
  723. } else {
  724. /*
  725. * The resume has been signaling for less than
  726. * USB_RESUME_TIME. Report the port status as SUSPEND,
  727. * let the usbcore check port status again and clear
  728. * resume signaling later.
  729. */
  730. status |= USB_PORT_STAT_SUSPEND;
  731. }
  732. }
  733. /*
  734. * Clear stale usb2 resume signalling variables in case port changed
  735. * state during resume signalling. For example on error
  736. */
  737. if ((bus_state->resume_done[wIndex] ||
  738. test_bit(wIndex, &bus_state->resuming_ports)) &&
  739. (raw_port_status & PORT_PLS_MASK) != XDEV_U3 &&
  740. (raw_port_status & PORT_PLS_MASK) != XDEV_RESUME) {
  741. bus_state->resume_done[wIndex] = 0;
  742. clear_bit(wIndex, &bus_state->resuming_ports);
  743. }
  744. if ((raw_port_status & PORT_PLS_MASK) == XDEV_U0 &&
  745. (raw_port_status & PORT_POWER)) {
  746. if (bus_state->suspended_ports & (1 << wIndex)) {
  747. bus_state->suspended_ports &= ~(1 << wIndex);
  748. if (hcd->speed < HCD_USB3)
  749. bus_state->port_c_suspend |= 1 << wIndex;
  750. }
  751. bus_state->resume_done[wIndex] = 0;
  752. clear_bit(wIndex, &bus_state->resuming_ports);
  753. }
  754. if (raw_port_status & PORT_CONNECT) {
  755. status |= USB_PORT_STAT_CONNECTION;
  756. status |= xhci_port_speed(raw_port_status);
  757. }
  758. if (raw_port_status & PORT_PE)
  759. status |= USB_PORT_STAT_ENABLE;
  760. if (raw_port_status & PORT_OC)
  761. status |= USB_PORT_STAT_OVERCURRENT;
  762. if (raw_port_status & PORT_RESET)
  763. status |= USB_PORT_STAT_RESET;
  764. if (raw_port_status & PORT_POWER) {
  765. if (hcd->speed >= HCD_USB3)
  766. status |= USB_SS_PORT_STAT_POWER;
  767. else
  768. status |= USB_PORT_STAT_POWER;
  769. }
  770. /* Update Port Link State */
  771. if (hcd->speed >= HCD_USB3) {
  772. xhci_hub_report_usb3_link_state(xhci, &status, raw_port_status);
  773. /*
  774. * Verify if all USB3 Ports Have entered U0 already.
  775. * Delete Compliance Mode Timer if so.
  776. */
  777. xhci_del_comp_mod_timer(xhci, raw_port_status, wIndex);
  778. } else {
  779. xhci_hub_report_usb2_link_state(&status, raw_port_status);
  780. }
  781. if (bus_state->port_c_suspend & (1 << wIndex))
  782. status |= 1 << USB_PORT_FEAT_C_SUSPEND;
  783. return status;
  784. }
  785. int xhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
  786. u16 wIndex, char *buf, u16 wLength)
  787. {
  788. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  789. int max_ports;
  790. unsigned long flags;
  791. u32 temp, status;
  792. int retval = 0;
  793. __le32 __iomem **port_array;
  794. int slot_id;
  795. struct xhci_bus_state *bus_state;
  796. u16 link_state = 0;
  797. u16 wake_mask = 0;
  798. u16 timeout = 0;
  799. max_ports = xhci_get_ports(hcd, &port_array);
  800. bus_state = &xhci->bus_state[hcd_index(hcd)];
  801. spin_lock_irqsave(&xhci->lock, flags);
  802. switch (typeReq) {
  803. case GetHubStatus:
  804. /* No power source, over-current reported per port */
  805. memset(buf, 0, 4);
  806. break;
  807. case GetHubDescriptor:
  808. /* Check to make sure userspace is asking for the USB 3.0 hub
  809. * descriptor for the USB 3.0 roothub. If not, we stall the
  810. * endpoint, like external hubs do.
  811. */
  812. if (hcd->speed >= HCD_USB3 &&
  813. (wLength < USB_DT_SS_HUB_SIZE ||
  814. wValue != (USB_DT_SS_HUB << 8))) {
  815. xhci_dbg(xhci, "Wrong hub descriptor type for "
  816. "USB 3.0 roothub.\n");
  817. goto error;
  818. }
  819. xhci_hub_descriptor(hcd, xhci,
  820. (struct usb_hub_descriptor *) buf);
  821. break;
  822. case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
  823. if ((wValue & 0xff00) != (USB_DT_BOS << 8))
  824. goto error;
  825. if (hcd->speed < HCD_USB3)
  826. goto error;
  827. retval = xhci_create_usb3_bos_desc(xhci, buf, wLength);
  828. spin_unlock_irqrestore(&xhci->lock, flags);
  829. return retval;
  830. case GetPortStatus:
  831. if (!wIndex || wIndex > max_ports)
  832. goto error;
  833. wIndex--;
  834. temp = readl(port_array[wIndex]);
  835. if (temp == 0xffffffff) {
  836. retval = -ENODEV;
  837. break;
  838. }
  839. status = xhci_get_port_status(hcd, bus_state, port_array,
  840. wIndex, temp, flags);
  841. if (status == 0xffffffff)
  842. goto error;
  843. xhci_dbg(xhci, "get port status, actual port %d status = 0x%x\n",
  844. wIndex, temp);
  845. xhci_dbg(xhci, "Get port status returned 0x%x\n", status);
  846. put_unaligned(cpu_to_le32(status), (__le32 *) buf);
  847. /* if USB 3.1 extended port status return additional 4 bytes */
  848. if (wValue == 0x02) {
  849. u32 port_li;
  850. if (hcd->speed < HCD_USB31 || wLength != 8) {
  851. xhci_err(xhci, "get ext port status invalid parameter\n");
  852. retval = -EINVAL;
  853. break;
  854. }
  855. port_li = readl(port_array[wIndex] + PORTLI);
  856. status = xhci_get_ext_port_status(temp, port_li);
  857. put_unaligned_le32(cpu_to_le32(status), &buf[4]);
  858. }
  859. break;
  860. case SetPortFeature:
  861. if (wValue == USB_PORT_FEAT_LINK_STATE)
  862. link_state = (wIndex & 0xff00) >> 3;
  863. if (wValue == USB_PORT_FEAT_REMOTE_WAKE_MASK)
  864. wake_mask = wIndex & 0xff00;
  865. /* The MSB of wIndex is the U1/U2 timeout */
  866. timeout = (wIndex & 0xff00) >> 8;
  867. wIndex &= 0xff;
  868. if (!wIndex || wIndex > max_ports)
  869. goto error;
  870. wIndex--;
  871. temp = readl(port_array[wIndex]);
  872. if (temp == 0xffffffff) {
  873. retval = -ENODEV;
  874. break;
  875. }
  876. temp = xhci_port_state_to_neutral(temp);
  877. /* FIXME: What new port features do we need to support? */
  878. switch (wValue) {
  879. case USB_PORT_FEAT_SUSPEND:
  880. temp = readl(port_array[wIndex]);
  881. if ((temp & PORT_PLS_MASK) != XDEV_U0) {
  882. /* Resume the port to U0 first */
  883. xhci_set_link_state(xhci, port_array, wIndex,
  884. XDEV_U0);
  885. spin_unlock_irqrestore(&xhci->lock, flags);
  886. msleep(10);
  887. spin_lock_irqsave(&xhci->lock, flags);
  888. }
  889. /* In spec software should not attempt to suspend
  890. * a port unless the port reports that it is in the
  891. * enabled (PED = ‘1’,PLS < ‘3’) state.
  892. */
  893. temp = readl(port_array[wIndex]);
  894. if ((temp & PORT_PE) == 0 || (temp & PORT_RESET)
  895. || (temp & PORT_PLS_MASK) >= XDEV_U3) {
  896. xhci_warn(xhci, "USB core suspending device "
  897. "not in U0/U1/U2.\n");
  898. goto error;
  899. }
  900. slot_id = xhci_find_slot_id_by_port(hcd, xhci,
  901. wIndex + 1);
  902. if (!slot_id) {
  903. xhci_warn(xhci, "slot_id is zero\n");
  904. goto error;
  905. }
  906. /* unlock to execute stop endpoint commands */
  907. spin_unlock_irqrestore(&xhci->lock, flags);
  908. xhci_stop_device(xhci, slot_id, 1);
  909. spin_lock_irqsave(&xhci->lock, flags);
  910. xhci_set_link_state(xhci, port_array, wIndex, XDEV_U3);
  911. spin_unlock_irqrestore(&xhci->lock, flags);
  912. msleep(10); /* wait device to enter */
  913. spin_lock_irqsave(&xhci->lock, flags);
  914. temp = readl(port_array[wIndex]);
  915. bus_state->suspended_ports |= 1 << wIndex;
  916. break;
  917. case USB_PORT_FEAT_LINK_STATE:
  918. temp = readl(port_array[wIndex]);
  919. /* Disable port */
  920. if (link_state == USB_SS_PORT_LS_SS_DISABLED) {
  921. xhci_dbg(xhci, "Disable port %d\n", wIndex);
  922. temp = xhci_port_state_to_neutral(temp);
  923. /*
  924. * Clear all change bits, so that we get a new
  925. * connection event.
  926. */
  927. temp |= PORT_CSC | PORT_PEC | PORT_WRC |
  928. PORT_OCC | PORT_RC | PORT_PLC |
  929. PORT_CEC;
  930. writel(temp | PORT_PE, port_array[wIndex]);
  931. temp = readl(port_array[wIndex]);
  932. break;
  933. }
  934. /* Put link in RxDetect (enable port) */
  935. if (link_state == USB_SS_PORT_LS_RX_DETECT) {
  936. xhci_dbg(xhci, "Enable port %d\n", wIndex);
  937. xhci_set_link_state(xhci, port_array, wIndex,
  938. link_state);
  939. temp = readl(port_array[wIndex]);
  940. break;
  941. }
  942. /* Port must be enabled */
  943. if (!(temp & PORT_PE)) {
  944. retval = -ENODEV;
  945. break;
  946. }
  947. /* Can't set port link state above '3' (U3) */
  948. if (link_state > USB_SS_PORT_LS_U3) {
  949. xhci_warn(xhci, "Cannot set port %d link state %d\n",
  950. wIndex, link_state);
  951. goto error;
  952. }
  953. if (link_state == USB_SS_PORT_LS_U3) {
  954. slot_id = xhci_find_slot_id_by_port(hcd, xhci,
  955. wIndex + 1);
  956. if (slot_id) {
  957. /* unlock to execute stop endpoint
  958. * commands */
  959. spin_unlock_irqrestore(&xhci->lock,
  960. flags);
  961. xhci_stop_device(xhci, slot_id, 1);
  962. spin_lock_irqsave(&xhci->lock, flags);
  963. }
  964. }
  965. xhci_set_link_state(xhci, port_array, wIndex,
  966. link_state);
  967. spin_unlock_irqrestore(&xhci->lock, flags);
  968. msleep(20); /* wait device to enter */
  969. spin_lock_irqsave(&xhci->lock, flags);
  970. temp = readl(port_array[wIndex]);
  971. if (link_state == USB_SS_PORT_LS_U3)
  972. bus_state->suspended_ports |= 1 << wIndex;
  973. break;
  974. case USB_PORT_FEAT_POWER:
  975. /*
  976. * Turn on ports, even if there isn't per-port switching.
  977. * HC will report connect events even before this is set.
  978. * However, hub_wq will ignore the roothub events until
  979. * the roothub is registered.
  980. */
  981. writel(temp | PORT_POWER, port_array[wIndex]);
  982. temp = readl(port_array[wIndex]);
  983. xhci_dbg(xhci, "set port power, actual port %d status = 0x%x\n", wIndex, temp);
  984. spin_unlock_irqrestore(&xhci->lock, flags);
  985. temp = usb_acpi_power_manageable(hcd->self.root_hub,
  986. wIndex);
  987. if (temp)
  988. usb_acpi_set_power_state(hcd->self.root_hub,
  989. wIndex, true);
  990. spin_lock_irqsave(&xhci->lock, flags);
  991. break;
  992. case USB_PORT_FEAT_RESET:
  993. temp = (temp | PORT_RESET);
  994. writel(temp, port_array[wIndex]);
  995. temp = readl(port_array[wIndex]);
  996. xhci_dbg(xhci, "set port reset, actual port %d status = 0x%x\n", wIndex, temp);
  997. break;
  998. case USB_PORT_FEAT_REMOTE_WAKE_MASK:
  999. xhci_set_remote_wake_mask(xhci, port_array,
  1000. wIndex, wake_mask);
  1001. temp = readl(port_array[wIndex]);
  1002. xhci_dbg(xhci, "set port remote wake mask, "
  1003. "actual port %d status = 0x%x\n",
  1004. wIndex, temp);
  1005. break;
  1006. case USB_PORT_FEAT_BH_PORT_RESET:
  1007. temp |= PORT_WR;
  1008. writel(temp, port_array[wIndex]);
  1009. temp = readl(port_array[wIndex]);
  1010. break;
  1011. case USB_PORT_FEAT_U1_TIMEOUT:
  1012. if (hcd->speed < HCD_USB3)
  1013. goto error;
  1014. temp = readl(port_array[wIndex] + PORTPMSC);
  1015. temp &= ~PORT_U1_TIMEOUT_MASK;
  1016. temp |= PORT_U1_TIMEOUT(timeout);
  1017. writel(temp, port_array[wIndex] + PORTPMSC);
  1018. break;
  1019. case USB_PORT_FEAT_U2_TIMEOUT:
  1020. if (hcd->speed < HCD_USB3)
  1021. goto error;
  1022. temp = readl(port_array[wIndex] + PORTPMSC);
  1023. temp &= ~PORT_U2_TIMEOUT_MASK;
  1024. temp |= PORT_U2_TIMEOUT(timeout);
  1025. writel(temp, port_array[wIndex] + PORTPMSC);
  1026. break;
  1027. default:
  1028. goto error;
  1029. }
  1030. /* unblock any posted writes */
  1031. temp = readl(port_array[wIndex]);
  1032. break;
  1033. case ClearPortFeature:
  1034. if (!wIndex || wIndex > max_ports)
  1035. goto error;
  1036. wIndex--;
  1037. temp = readl(port_array[wIndex]);
  1038. if (temp == 0xffffffff) {
  1039. retval = -ENODEV;
  1040. break;
  1041. }
  1042. /* FIXME: What new port features do we need to support? */
  1043. temp = xhci_port_state_to_neutral(temp);
  1044. switch (wValue) {
  1045. case USB_PORT_FEAT_SUSPEND:
  1046. temp = readl(port_array[wIndex]);
  1047. xhci_dbg(xhci, "clear USB_PORT_FEAT_SUSPEND\n");
  1048. xhci_dbg(xhci, "PORTSC %04x\n", temp);
  1049. if (temp & PORT_RESET)
  1050. goto error;
  1051. if ((temp & PORT_PLS_MASK) == XDEV_U3) {
  1052. if ((temp & PORT_PE) == 0)
  1053. goto error;
  1054. set_bit(wIndex, &bus_state->resuming_ports);
  1055. xhci_set_link_state(xhci, port_array, wIndex,
  1056. XDEV_RESUME);
  1057. spin_unlock_irqrestore(&xhci->lock, flags);
  1058. msleep(USB_RESUME_TIMEOUT);
  1059. spin_lock_irqsave(&xhci->lock, flags);
  1060. xhci_set_link_state(xhci, port_array, wIndex,
  1061. XDEV_U0);
  1062. clear_bit(wIndex, &bus_state->resuming_ports);
  1063. }
  1064. bus_state->port_c_suspend |= 1 << wIndex;
  1065. slot_id = xhci_find_slot_id_by_port(hcd, xhci,
  1066. wIndex + 1);
  1067. if (!slot_id) {
  1068. xhci_dbg(xhci, "slot_id is zero\n");
  1069. goto error;
  1070. }
  1071. xhci_ring_device(xhci, slot_id);
  1072. break;
  1073. case USB_PORT_FEAT_C_SUSPEND:
  1074. bus_state->port_c_suspend &= ~(1 << wIndex);
  1075. case USB_PORT_FEAT_C_RESET:
  1076. case USB_PORT_FEAT_C_BH_PORT_RESET:
  1077. case USB_PORT_FEAT_C_CONNECTION:
  1078. case USB_PORT_FEAT_C_OVER_CURRENT:
  1079. case USB_PORT_FEAT_C_ENABLE:
  1080. case USB_PORT_FEAT_C_PORT_LINK_STATE:
  1081. case USB_PORT_FEAT_C_PORT_CONFIG_ERROR:
  1082. xhci_clear_port_change_bit(xhci, wValue, wIndex,
  1083. port_array[wIndex], temp);
  1084. break;
  1085. case USB_PORT_FEAT_ENABLE:
  1086. xhci_disable_port(hcd, xhci, wIndex,
  1087. port_array[wIndex], temp);
  1088. break;
  1089. case USB_PORT_FEAT_POWER:
  1090. writel(temp & ~PORT_POWER, port_array[wIndex]);
  1091. spin_unlock_irqrestore(&xhci->lock, flags);
  1092. temp = usb_acpi_power_manageable(hcd->self.root_hub,
  1093. wIndex);
  1094. if (temp)
  1095. usb_acpi_set_power_state(hcd->self.root_hub,
  1096. wIndex, false);
  1097. spin_lock_irqsave(&xhci->lock, flags);
  1098. break;
  1099. default:
  1100. goto error;
  1101. }
  1102. break;
  1103. default:
  1104. error:
  1105. /* "stall" on error */
  1106. retval = -EPIPE;
  1107. }
  1108. spin_unlock_irqrestore(&xhci->lock, flags);
  1109. return retval;
  1110. }
  1111. /*
  1112. * Returns 0 if the status hasn't changed, or the number of bytes in buf.
  1113. * Ports are 0-indexed from the HCD point of view,
  1114. * and 1-indexed from the USB core pointer of view.
  1115. *
  1116. * Note that the status change bits will be cleared as soon as a port status
  1117. * change event is generated, so we use the saved status from that event.
  1118. */
  1119. int xhci_hub_status_data(struct usb_hcd *hcd, char *buf)
  1120. {
  1121. unsigned long flags;
  1122. u32 temp, status;
  1123. u32 mask;
  1124. int i, retval;
  1125. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  1126. int max_ports;
  1127. __le32 __iomem **port_array;
  1128. struct xhci_bus_state *bus_state;
  1129. bool reset_change = false;
  1130. max_ports = xhci_get_ports(hcd, &port_array);
  1131. bus_state = &xhci->bus_state[hcd_index(hcd)];
  1132. /* Initial status is no changes */
  1133. retval = (max_ports + 8) / 8;
  1134. memset(buf, 0, retval);
  1135. /*
  1136. * Inform the usbcore about resume-in-progress by returning
  1137. * a non-zero value even if there are no status changes.
  1138. */
  1139. status = bus_state->resuming_ports;
  1140. mask = PORT_CSC | PORT_PEC | PORT_OCC | PORT_PLC | PORT_WRC | PORT_CEC;
  1141. spin_lock_irqsave(&xhci->lock, flags);
  1142. /* For each port, did anything change? If so, set that bit in buf. */
  1143. for (i = 0; i < max_ports; i++) {
  1144. temp = readl(port_array[i]);
  1145. if (temp == 0xffffffff) {
  1146. retval = -ENODEV;
  1147. break;
  1148. }
  1149. if ((temp & mask) != 0 ||
  1150. (bus_state->port_c_suspend & 1 << i) ||
  1151. (bus_state->resume_done[i] && time_after_eq(
  1152. jiffies, bus_state->resume_done[i]))) {
  1153. buf[(i + 1) / 8] |= 1 << (i + 1) % 8;
  1154. status = 1;
  1155. }
  1156. if ((temp & PORT_RC))
  1157. reset_change = true;
  1158. }
  1159. if (!status && !reset_change) {
  1160. xhci_dbg(xhci, "%s: stopping port polling.\n", __func__);
  1161. clear_bit(HCD_FLAG_POLL_RH, &hcd->flags);
  1162. }
  1163. spin_unlock_irqrestore(&xhci->lock, flags);
  1164. return status ? retval : 0;
  1165. }
  1166. #ifdef CONFIG_PM
  1167. int xhci_bus_suspend(struct usb_hcd *hcd)
  1168. {
  1169. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  1170. int max_ports, port_index;
  1171. __le32 __iomem **port_array;
  1172. struct xhci_bus_state *bus_state;
  1173. unsigned long flags;
  1174. u32 portsc_buf[USB_MAXCHILDREN];
  1175. bool wake_enabled;
  1176. max_ports = xhci_get_ports(hcd, &port_array);
  1177. bus_state = &xhci->bus_state[hcd_index(hcd)];
  1178. wake_enabled = hcd->self.root_hub->do_remote_wakeup;
  1179. spin_lock_irqsave(&xhci->lock, flags);
  1180. if (wake_enabled) {
  1181. if (bus_state->resuming_ports || /* USB2 */
  1182. bus_state->port_remote_wakeup) { /* USB3 */
  1183. spin_unlock_irqrestore(&xhci->lock, flags);
  1184. xhci_dbg(xhci, "suspend failed because a port is resuming\n");
  1185. return -EBUSY;
  1186. }
  1187. }
  1188. /*
  1189. * Prepare ports for suspend, but don't write anything before all ports
  1190. * are checked and we know bus suspend can proceed
  1191. */
  1192. bus_state->bus_suspended = 0;
  1193. port_index = max_ports;
  1194. while (port_index--) {
  1195. u32 t1, t2;
  1196. t1 = readl(port_array[port_index]);
  1197. t2 = xhci_port_state_to_neutral(t1);
  1198. portsc_buf[port_index] = 0;
  1199. /* Bail out if a USB3 port has a new device in link training */
  1200. if ((hcd->speed >= HCD_USB3) &&
  1201. (t1 & PORT_PLS_MASK) == XDEV_POLLING) {
  1202. bus_state->bus_suspended = 0;
  1203. spin_unlock_irqrestore(&xhci->lock, flags);
  1204. xhci_dbg(xhci, "Bus suspend bailout, port in polling\n");
  1205. return -EBUSY;
  1206. }
  1207. /* suspend ports in U0, or bail out for new connect changes */
  1208. if ((t1 & PORT_PE) && (t1 & PORT_PLS_MASK) == XDEV_U0) {
  1209. if ((t1 & PORT_CSC) && wake_enabled) {
  1210. bus_state->bus_suspended = 0;
  1211. spin_unlock_irqrestore(&xhci->lock, flags);
  1212. xhci_dbg(xhci, "Bus suspend bailout, port connect change\n");
  1213. return -EBUSY;
  1214. }
  1215. xhci_dbg(xhci, "port %d not suspended\n", port_index);
  1216. t2 &= ~PORT_PLS_MASK;
  1217. t2 |= PORT_LINK_STROBE | XDEV_U3;
  1218. set_bit(port_index, &bus_state->bus_suspended);
  1219. }
  1220. /* USB core sets remote wake mask for USB 3.0 hubs,
  1221. * including the USB 3.0 roothub, but only if CONFIG_PM
  1222. * is enabled, so also enable remote wake here.
  1223. */
  1224. if (wake_enabled) {
  1225. if (t1 & PORT_CONNECT) {
  1226. t2 |= PORT_WKOC_E | PORT_WKDISC_E;
  1227. t2 &= ~PORT_WKCONN_E;
  1228. } else {
  1229. t2 |= PORT_WKOC_E | PORT_WKCONN_E;
  1230. t2 &= ~PORT_WKDISC_E;
  1231. }
  1232. } else
  1233. t2 &= ~PORT_WAKE_BITS;
  1234. t1 = xhci_port_state_to_neutral(t1);
  1235. if (t1 != t2)
  1236. portsc_buf[port_index] = t2;
  1237. }
  1238. /* write port settings, stopping and suspending ports if needed */
  1239. port_index = max_ports;
  1240. while (port_index--) {
  1241. if (!portsc_buf[port_index])
  1242. continue;
  1243. if (test_bit(port_index, &bus_state->bus_suspended)) {
  1244. int slot_id;
  1245. slot_id = xhci_find_slot_id_by_port(hcd, xhci,
  1246. port_index + 1);
  1247. if (slot_id) {
  1248. spin_unlock_irqrestore(&xhci->lock, flags);
  1249. xhci_stop_device(xhci, slot_id, 1);
  1250. spin_lock_irqsave(&xhci->lock, flags);
  1251. }
  1252. }
  1253. writel(portsc_buf[port_index], port_array[port_index]);
  1254. }
  1255. hcd->state = HC_STATE_SUSPENDED;
  1256. bus_state->next_statechange = jiffies + msecs_to_jiffies(10);
  1257. spin_unlock_irqrestore(&xhci->lock, flags);
  1258. return 0;
  1259. }
  1260. /*
  1261. * Workaround for missing Cold Attach Status (CAS) if device re-plugged in S3.
  1262. * warm reset a USB3 device stuck in polling or compliance mode after resume.
  1263. * See Intel 100/c230 series PCH specification update Doc #332692-006 Errata #8
  1264. */
  1265. static bool xhci_port_missing_cas_quirk(int port_index,
  1266. __le32 __iomem **port_array)
  1267. {
  1268. u32 portsc;
  1269. portsc = readl(port_array[port_index]);
  1270. /* if any of these are set we are not stuck */
  1271. if (portsc & (PORT_CONNECT | PORT_CAS))
  1272. return false;
  1273. if (((portsc & PORT_PLS_MASK) != XDEV_POLLING) &&
  1274. ((portsc & PORT_PLS_MASK) != XDEV_COMP_MODE))
  1275. return false;
  1276. /* clear wakeup/change bits, and do a warm port reset */
  1277. portsc &= ~(PORT_RWC_BITS | PORT_CEC | PORT_WAKE_BITS);
  1278. portsc |= PORT_WR;
  1279. writel(portsc, port_array[port_index]);
  1280. /* flush write */
  1281. readl(port_array[port_index]);
  1282. return true;
  1283. }
  1284. int xhci_bus_resume(struct usb_hcd *hcd)
  1285. {
  1286. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  1287. int max_ports, port_index;
  1288. __le32 __iomem **port_array;
  1289. struct xhci_bus_state *bus_state;
  1290. u32 temp;
  1291. unsigned long flags;
  1292. unsigned long port_was_suspended = 0;
  1293. bool need_usb2_u3_exit = false;
  1294. int slot_id;
  1295. int sret;
  1296. max_ports = xhci_get_ports(hcd, &port_array);
  1297. bus_state = &xhci->bus_state[hcd_index(hcd)];
  1298. if (time_before(jiffies, bus_state->next_statechange))
  1299. msleep(5);
  1300. spin_lock_irqsave(&xhci->lock, flags);
  1301. if (!HCD_HW_ACCESSIBLE(hcd)) {
  1302. spin_unlock_irqrestore(&xhci->lock, flags);
  1303. return -ESHUTDOWN;
  1304. }
  1305. /* delay the irqs */
  1306. temp = readl(&xhci->op_regs->command);
  1307. temp &= ~CMD_EIE;
  1308. writel(temp, &xhci->op_regs->command);
  1309. port_index = max_ports;
  1310. while (port_index--) {
  1311. /* Check whether need resume ports. If needed
  1312. resume port and disable remote wakeup */
  1313. u32 temp;
  1314. temp = readl(port_array[port_index]);
  1315. /* warm reset CAS limited ports stuck in polling/compliance */
  1316. if ((xhci->quirks & XHCI_MISSING_CAS) &&
  1317. (hcd->speed >= HCD_USB3) &&
  1318. xhci_port_missing_cas_quirk(port_index, port_array)) {
  1319. xhci_dbg(xhci, "reset stuck port %d\n", port_index);
  1320. continue;
  1321. }
  1322. if (DEV_SUPERSPEED_ANY(temp))
  1323. temp &= ~(PORT_RWC_BITS | PORT_CEC | PORT_WAKE_BITS);
  1324. else
  1325. temp &= ~(PORT_RWC_BITS | PORT_WAKE_BITS);
  1326. if (test_bit(port_index, &bus_state->bus_suspended) &&
  1327. (temp & PORT_PLS_MASK)) {
  1328. set_bit(port_index, &port_was_suspended);
  1329. if (!DEV_SUPERSPEED_ANY(temp)) {
  1330. xhci_set_link_state(xhci, port_array,
  1331. port_index, XDEV_RESUME);
  1332. need_usb2_u3_exit = true;
  1333. }
  1334. } else
  1335. writel(temp, port_array[port_index]);
  1336. }
  1337. if (need_usb2_u3_exit) {
  1338. spin_unlock_irqrestore(&xhci->lock, flags);
  1339. msleep(USB_RESUME_TIMEOUT);
  1340. spin_lock_irqsave(&xhci->lock, flags);
  1341. }
  1342. port_index = max_ports;
  1343. while (port_index--) {
  1344. if (!(port_was_suspended & BIT(port_index)))
  1345. continue;
  1346. /* Clear PLC to poll it later after XDEV_U0 */
  1347. xhci_test_and_clear_bit(xhci, port_array, port_index, PORT_PLC);
  1348. xhci_set_link_state(xhci, port_array, port_index, XDEV_U0);
  1349. }
  1350. port_index = max_ports;
  1351. while (port_index--) {
  1352. if (!(port_was_suspended & BIT(port_index)))
  1353. continue;
  1354. /* Poll and Clear PLC */
  1355. sret = xhci_handshake(port_array[port_index], PORT_PLC,
  1356. PORT_PLC, 10 * 1000);
  1357. if (sret)
  1358. xhci_warn(xhci, "port %d resume PLC timeout\n",
  1359. port_index);
  1360. xhci_test_and_clear_bit(xhci, port_array, port_index, PORT_PLC);
  1361. slot_id = xhci_find_slot_id_by_port(hcd, xhci, port_index + 1);
  1362. if (slot_id)
  1363. xhci_ring_device(xhci, slot_id);
  1364. }
  1365. (void) readl(&xhci->op_regs->command);
  1366. bus_state->next_statechange = jiffies + msecs_to_jiffies(5);
  1367. /* re-enable irqs */
  1368. temp = readl(&xhci->op_regs->command);
  1369. temp |= CMD_EIE;
  1370. writel(temp, &xhci->op_regs->command);
  1371. temp = readl(&xhci->op_regs->command);
  1372. spin_unlock_irqrestore(&xhci->lock, flags);
  1373. return 0;
  1374. }
  1375. #endif /* CONFIG_PM */