xhci-hub.c 42 KB

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