mfd: wm8350-i2c: Make sure the i2c regmap functions are compiled
[linux/fpc-iii.git] / drivers / usb / host / xhci-hub.c
blob8a79270ca44dc17b88f540d23965fe5dd87c7587
1 /*
2 * xHCI host controller driver
4 * Copyright (C) 2008 Intel Corp.
6 * Author: Sarah Sharp
7 * Some code borrowed from the Linux EHCI driver.
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.
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.
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.
23 #include <linux/gfp.h>
24 #include <asm/unaligned.h>
26 #include "xhci.h"
27 #include "xhci-trace.h"
29 #define PORT_WAKE_BITS (PORT_WKOC_E | PORT_WKDISC_E | PORT_WKCONN_E)
30 #define PORT_RWC_BITS (PORT_CSC | PORT_PEC | PORT_WRC | PORT_OCC | \
31 PORT_RC | PORT_PLC | PORT_PE)
33 /* USB 3.0 BOS descriptor and a capability descriptor, combined */
34 static u8 usb_bos_descriptor [] = {
35 USB_DT_BOS_SIZE, /* __u8 bLength, 5 bytes */
36 USB_DT_BOS, /* __u8 bDescriptorType */
37 0x0F, 0x00, /* __le16 wTotalLength, 15 bytes */
38 0x1, /* __u8 bNumDeviceCaps */
39 /* First device capability */
40 USB_DT_USB_SS_CAP_SIZE, /* __u8 bLength, 10 bytes */
41 USB_DT_DEVICE_CAPABILITY, /* Device Capability */
42 USB_SS_CAP_TYPE, /* bDevCapabilityType, SUPERSPEED_USB */
43 0x00, /* bmAttributes, LTM off by default */
44 USB_5GBPS_OPERATION, 0x00, /* wSpeedsSupported, 5Gbps only */
45 0x03, /* bFunctionalitySupport,
46 USB 3.0 speed only */
47 0x00, /* bU1DevExitLat, set later. */
48 0x00, 0x00 /* __le16 bU2DevExitLat, set later. */
52 static void xhci_common_hub_descriptor(struct xhci_hcd *xhci,
53 struct usb_hub_descriptor *desc, int ports)
55 u16 temp;
57 desc->bPwrOn2PwrGood = 10; /* xhci section 5.4.9 says 20ms max */
58 desc->bHubContrCurrent = 0;
60 desc->bNbrPorts = ports;
61 temp = 0;
62 /* Bits 1:0 - support per-port power switching, or power always on */
63 if (HCC_PPC(xhci->hcc_params))
64 temp |= HUB_CHAR_INDV_PORT_LPSM;
65 else
66 temp |= HUB_CHAR_NO_LPSM;
67 /* Bit 2 - root hubs are not part of a compound device */
68 /* Bits 4:3 - individual port over current protection */
69 temp |= HUB_CHAR_INDV_PORT_OCPM;
70 /* Bits 6:5 - no TTs in root ports */
71 /* Bit 7 - no port indicators */
72 desc->wHubCharacteristics = cpu_to_le16(temp);
75 /* Fill in the USB 2.0 roothub descriptor */
76 static void xhci_usb2_hub_descriptor(struct usb_hcd *hcd, struct xhci_hcd *xhci,
77 struct usb_hub_descriptor *desc)
79 int ports;
80 u16 temp;
81 __u8 port_removable[(USB_MAXCHILDREN + 1 + 7) / 8];
82 u32 portsc;
83 unsigned int i;
85 ports = xhci->num_usb2_ports;
87 xhci_common_hub_descriptor(xhci, desc, ports);
88 desc->bDescriptorType = USB_DT_HUB;
89 temp = 1 + (ports / 8);
90 desc->bDescLength = USB_DT_HUB_NONVAR_SIZE + 2 * temp;
92 /* The Device Removable bits are reported on a byte granularity.
93 * If the port doesn't exist within that byte, the bit is set to 0.
95 memset(port_removable, 0, sizeof(port_removable));
96 for (i = 0; i < ports; i++) {
97 portsc = xhci_readl(xhci, xhci->usb2_ports[i]);
98 /* If a device is removable, PORTSC reports a 0, same as in the
99 * hub descriptor DeviceRemovable bits.
101 if (portsc & PORT_DEV_REMOVE)
102 /* This math is hairy because bit 0 of DeviceRemovable
103 * is reserved, and bit 1 is for port 1, etc.
105 port_removable[(i + 1) / 8] |= 1 << ((i + 1) % 8);
108 /* ch11.h defines a hub descriptor that has room for USB_MAXCHILDREN
109 * ports on it. The USB 2.0 specification says that there are two
110 * variable length fields at the end of the hub descriptor:
111 * DeviceRemovable and PortPwrCtrlMask. But since we can have less than
112 * USB_MAXCHILDREN ports, we may need to use the DeviceRemovable array
113 * to set PortPwrCtrlMask bits. PortPwrCtrlMask must always be set to
114 * 0xFF, so we initialize the both arrays (DeviceRemovable and
115 * PortPwrCtrlMask) to 0xFF. Then we set the DeviceRemovable for each
116 * set of ports that actually exist.
118 memset(desc->u.hs.DeviceRemovable, 0xff,
119 sizeof(desc->u.hs.DeviceRemovable));
120 memset(desc->u.hs.PortPwrCtrlMask, 0xff,
121 sizeof(desc->u.hs.PortPwrCtrlMask));
123 for (i = 0; i < (ports + 1 + 7) / 8; i++)
124 memset(&desc->u.hs.DeviceRemovable[i], port_removable[i],
125 sizeof(__u8));
128 /* Fill in the USB 3.0 roothub descriptor */
129 static void xhci_usb3_hub_descriptor(struct usb_hcd *hcd, struct xhci_hcd *xhci,
130 struct usb_hub_descriptor *desc)
132 int ports;
133 u16 port_removable;
134 u32 portsc;
135 unsigned int i;
137 ports = xhci->num_usb3_ports;
138 xhci_common_hub_descriptor(xhci, desc, ports);
139 desc->bDescriptorType = USB_DT_SS_HUB;
140 desc->bDescLength = USB_DT_SS_HUB_SIZE;
142 /* header decode latency should be zero for roothubs,
143 * see section 4.23.5.2.
145 desc->u.ss.bHubHdrDecLat = 0;
146 desc->u.ss.wHubDelay = 0;
148 port_removable = 0;
149 /* bit 0 is reserved, bit 1 is for port 1, etc. */
150 for (i = 0; i < ports; i++) {
151 portsc = xhci_readl(xhci, xhci->usb3_ports[i]);
152 if (portsc & PORT_DEV_REMOVE)
153 port_removable |= 1 << (i + 1);
156 desc->u.ss.DeviceRemovable = cpu_to_le16(port_removable);
159 static void xhci_hub_descriptor(struct usb_hcd *hcd, struct xhci_hcd *xhci,
160 struct usb_hub_descriptor *desc)
163 if (hcd->speed == HCD_USB3)
164 xhci_usb3_hub_descriptor(hcd, xhci, desc);
165 else
166 xhci_usb2_hub_descriptor(hcd, xhci, desc);
170 static unsigned int xhci_port_speed(unsigned int port_status)
172 if (DEV_LOWSPEED(port_status))
173 return USB_PORT_STAT_LOW_SPEED;
174 if (DEV_HIGHSPEED(port_status))
175 return USB_PORT_STAT_HIGH_SPEED;
177 * FIXME: Yes, we should check for full speed, but the core uses that as
178 * a default in portspeed() in usb/core/hub.c (which is the only place
179 * USB_PORT_STAT_*_SPEED is used).
181 return 0;
185 * These bits are Read Only (RO) and should be saved and written to the
186 * registers: 0, 3, 10:13, 30
187 * connect status, over-current status, port speed, and device removable.
188 * connect status and port speed are also sticky - meaning they're in
189 * the AUX well and they aren't changed by a hot, warm, or cold reset.
191 #define XHCI_PORT_RO ((1<<0) | (1<<3) | (0xf<<10) | (1<<30))
193 * These bits are RW; writing a 0 clears the bit, writing a 1 sets the bit:
194 * bits 5:8, 9, 14:15, 25:27
195 * link state, port power, port indicator state, "wake on" enable state
197 #define XHCI_PORT_RWS ((0xf<<5) | (1<<9) | (0x3<<14) | (0x7<<25))
199 * These bits are RW; writing a 1 sets the bit, writing a 0 has no effect:
200 * bit 4 (port reset)
202 #define XHCI_PORT_RW1S ((1<<4))
204 * These bits are RW; writing a 1 clears the bit, writing a 0 has no effect:
205 * bits 1, 17, 18, 19, 20, 21, 22, 23
206 * port enable/disable, and
207 * change bits: connect, PED, warm port reset changed (reserved zero for USB 2.0 ports),
208 * over-current, reset, link state, and L1 change
210 #define XHCI_PORT_RW1CS ((1<<1) | (0x7f<<17))
212 * Bit 16 is RW, and writing a '1' to it causes the link state control to be
213 * latched in
215 #define XHCI_PORT_RW ((1<<16))
217 * These bits are Reserved Zero (RsvdZ) and zero should be written to them:
218 * bits 2, 24, 28:31
220 #define XHCI_PORT_RZ ((1<<2) | (1<<24) | (0xf<<28))
223 * Given a port state, this function returns a value that would result in the
224 * port being in the same state, if the value was written to the port status
225 * control register.
226 * Save Read Only (RO) bits and save read/write bits where
227 * writing a 0 clears the bit and writing a 1 sets the bit (RWS).
228 * For all other types (RW1S, RW1CS, RW, and RZ), writing a '0' has no effect.
230 u32 xhci_port_state_to_neutral(u32 state)
232 /* Save read-only status and port state */
233 return (state & XHCI_PORT_RO) | (state & XHCI_PORT_RWS);
237 * find slot id based on port number.
238 * @port: The one-based port number from one of the two split roothubs.
240 int xhci_find_slot_id_by_port(struct usb_hcd *hcd, struct xhci_hcd *xhci,
241 u16 port)
243 int slot_id;
244 int i;
245 enum usb_device_speed speed;
247 slot_id = 0;
248 for (i = 0; i < MAX_HC_SLOTS; i++) {
249 if (!xhci->devs[i])
250 continue;
251 speed = xhci->devs[i]->udev->speed;
252 if (((speed == USB_SPEED_SUPER) == (hcd->speed == HCD_USB3))
253 && xhci->devs[i]->fake_port == port) {
254 slot_id = i;
255 break;
259 return slot_id;
263 * Stop device
264 * It issues stop endpoint command for EP 0 to 30. And wait the last command
265 * to complete.
266 * suspend will set to 1, if suspend bit need to set in command.
268 static int xhci_stop_device(struct xhci_hcd *xhci, int slot_id, int suspend)
270 struct xhci_virt_device *virt_dev;
271 struct xhci_command *cmd;
272 unsigned long flags;
273 int timeleft;
274 int ret;
275 int i;
277 ret = 0;
278 virt_dev = xhci->devs[slot_id];
279 if (!virt_dev)
280 return -ENODEV;
282 cmd = xhci_alloc_command(xhci, false, true, GFP_NOIO);
283 if (!cmd) {
284 xhci_dbg(xhci, "Couldn't allocate command structure.\n");
285 return -ENOMEM;
288 spin_lock_irqsave(&xhci->lock, flags);
289 for (i = LAST_EP_INDEX; i > 0; i--) {
290 if (virt_dev->eps[i].ring && virt_dev->eps[i].ring->dequeue)
291 xhci_queue_stop_endpoint(xhci, slot_id, i, suspend);
293 cmd->command_trb = xhci_find_next_enqueue(xhci->cmd_ring);
294 list_add_tail(&cmd->cmd_list, &virt_dev->cmd_list);
295 xhci_queue_stop_endpoint(xhci, slot_id, 0, suspend);
296 xhci_ring_cmd_db(xhci);
297 spin_unlock_irqrestore(&xhci->lock, flags);
299 /* Wait for last stop endpoint command to finish */
300 timeleft = wait_for_completion_interruptible_timeout(
301 cmd->completion,
302 USB_CTRL_SET_TIMEOUT);
303 if (timeleft <= 0) {
304 xhci_warn(xhci, "%s while waiting for stop endpoint command\n",
305 timeleft == 0 ? "Timeout" : "Signal");
306 spin_lock_irqsave(&xhci->lock, flags);
307 /* The timeout might have raced with the event ring handler, so
308 * only delete from the list if the item isn't poisoned.
310 if (cmd->cmd_list.next != LIST_POISON1)
311 list_del(&cmd->cmd_list);
312 spin_unlock_irqrestore(&xhci->lock, flags);
313 ret = -ETIME;
314 goto command_cleanup;
317 command_cleanup:
318 xhci_free_command(xhci, cmd);
319 return ret;
323 * Ring device, it rings the all doorbells unconditionally.
325 void xhci_ring_device(struct xhci_hcd *xhci, int slot_id)
327 int i;
329 for (i = 0; i < LAST_EP_INDEX + 1; i++)
330 if (xhci->devs[slot_id]->eps[i].ring &&
331 xhci->devs[slot_id]->eps[i].ring->dequeue)
332 xhci_ring_ep_doorbell(xhci, slot_id, i, 0);
334 return;
337 static void xhci_disable_port(struct usb_hcd *hcd, struct xhci_hcd *xhci,
338 u16 wIndex, __le32 __iomem *addr, u32 port_status)
340 /* Don't allow the USB core to disable SuperSpeed ports. */
341 if (hcd->speed == HCD_USB3) {
342 xhci_dbg(xhci, "Ignoring request to disable "
343 "SuperSpeed port.\n");
344 return;
347 /* Write 1 to disable the port */
348 xhci_writel(xhci, port_status | PORT_PE, addr);
349 port_status = xhci_readl(xhci, addr);
350 xhci_dbg(xhci, "disable port, actual port %d status = 0x%x\n",
351 wIndex, port_status);
354 static void xhci_clear_port_change_bit(struct xhci_hcd *xhci, u16 wValue,
355 u16 wIndex, __le32 __iomem *addr, u32 port_status)
357 char *port_change_bit;
358 u32 status;
360 switch (wValue) {
361 case USB_PORT_FEAT_C_RESET:
362 status = PORT_RC;
363 port_change_bit = "reset";
364 break;
365 case USB_PORT_FEAT_C_BH_PORT_RESET:
366 status = PORT_WRC;
367 port_change_bit = "warm(BH) reset";
368 break;
369 case USB_PORT_FEAT_C_CONNECTION:
370 status = PORT_CSC;
371 port_change_bit = "connect";
372 break;
373 case USB_PORT_FEAT_C_OVER_CURRENT:
374 status = PORT_OCC;
375 port_change_bit = "over-current";
376 break;
377 case USB_PORT_FEAT_C_ENABLE:
378 status = PORT_PEC;
379 port_change_bit = "enable/disable";
380 break;
381 case USB_PORT_FEAT_C_SUSPEND:
382 status = PORT_PLC;
383 port_change_bit = "suspend/resume";
384 break;
385 case USB_PORT_FEAT_C_PORT_LINK_STATE:
386 status = PORT_PLC;
387 port_change_bit = "link state";
388 break;
389 case USB_PORT_FEAT_C_PORT_CONFIG_ERROR:
390 status = PORT_CEC;
391 port_change_bit = "config error";
392 break;
393 default:
394 /* Should never happen */
395 return;
397 /* Change bits are all write 1 to clear */
398 xhci_writel(xhci, port_status | status, addr);
399 port_status = xhci_readl(xhci, addr);
400 xhci_dbg(xhci, "clear port %s change, actual port %d status = 0x%x\n",
401 port_change_bit, wIndex, port_status);
404 static int xhci_get_ports(struct usb_hcd *hcd, __le32 __iomem ***port_array)
406 int max_ports;
407 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
409 if (hcd->speed == HCD_USB3) {
410 max_ports = xhci->num_usb3_ports;
411 *port_array = xhci->usb3_ports;
412 } else {
413 max_ports = xhci->num_usb2_ports;
414 *port_array = xhci->usb2_ports;
417 return max_ports;
420 void xhci_set_link_state(struct xhci_hcd *xhci, __le32 __iomem **port_array,
421 int port_id, u32 link_state)
423 u32 temp;
425 temp = xhci_readl(xhci, port_array[port_id]);
426 temp = xhci_port_state_to_neutral(temp);
427 temp &= ~PORT_PLS_MASK;
428 temp |= PORT_LINK_STROBE | link_state;
429 xhci_writel(xhci, temp, port_array[port_id]);
432 static void xhci_set_remote_wake_mask(struct xhci_hcd *xhci,
433 __le32 __iomem **port_array, int port_id, u16 wake_mask)
435 u32 temp;
437 temp = xhci_readl(xhci, port_array[port_id]);
438 temp = xhci_port_state_to_neutral(temp);
440 if (wake_mask & USB_PORT_FEAT_REMOTE_WAKE_CONNECT)
441 temp |= PORT_WKCONN_E;
442 else
443 temp &= ~PORT_WKCONN_E;
445 if (wake_mask & USB_PORT_FEAT_REMOTE_WAKE_DISCONNECT)
446 temp |= PORT_WKDISC_E;
447 else
448 temp &= ~PORT_WKDISC_E;
450 if (wake_mask & USB_PORT_FEAT_REMOTE_WAKE_OVER_CURRENT)
451 temp |= PORT_WKOC_E;
452 else
453 temp &= ~PORT_WKOC_E;
455 xhci_writel(xhci, temp, port_array[port_id]);
458 /* Test and clear port RWC bit */
459 void xhci_test_and_clear_bit(struct xhci_hcd *xhci, __le32 __iomem **port_array,
460 int port_id, u32 port_bit)
462 u32 temp;
464 temp = xhci_readl(xhci, port_array[port_id]);
465 if (temp & port_bit) {
466 temp = xhci_port_state_to_neutral(temp);
467 temp |= port_bit;
468 xhci_writel(xhci, temp, port_array[port_id]);
472 /* Updates Link Status for USB 2.1 port */
473 static void xhci_hub_report_usb2_link_state(u32 *status, u32 status_reg)
475 if ((status_reg & PORT_PLS_MASK) == XDEV_U2)
476 *status |= USB_PORT_STAT_L1;
479 /* Updates Link Status for super Speed port */
480 static void xhci_hub_report_usb3_link_state(struct xhci_hcd *xhci,
481 u32 *status, u32 status_reg)
483 u32 pls = status_reg & PORT_PLS_MASK;
485 /* resume state is a xHCI internal state.
486 * Do not report it to usb core, instead, pretend to be U3,
487 * thus usb core knows it's not ready for transfer
489 if (pls == XDEV_RESUME) {
490 *status |= USB_SS_PORT_LS_U3;
491 return;
494 /* When the CAS bit is set then warm reset
495 * should be performed on port
497 if (status_reg & PORT_CAS) {
498 /* The CAS bit can be set while the port is
499 * in any link state.
500 * Only roothubs have CAS bit, so we
501 * pretend to be in compliance mode
502 * unless we're already in compliance
503 * or the inactive state.
505 if (pls != USB_SS_PORT_LS_COMP_MOD &&
506 pls != USB_SS_PORT_LS_SS_INACTIVE) {
507 pls = USB_SS_PORT_LS_COMP_MOD;
509 /* Return also connection bit -
510 * hub state machine resets port
511 * when this bit is set.
513 pls |= USB_PORT_STAT_CONNECTION;
514 } else {
516 * If CAS bit isn't set but the Port is already at
517 * Compliance Mode, fake a connection so the USB core
518 * notices the Compliance state and resets the port.
519 * This resolves an issue generated by the SN65LVPE502CP
520 * in which sometimes the port enters compliance mode
521 * caused by a delay on the host-device negotiation.
523 if ((xhci->quirks & XHCI_COMP_MODE_QUIRK) &&
524 (pls == USB_SS_PORT_LS_COMP_MOD))
525 pls |= USB_PORT_STAT_CONNECTION;
528 /* update status field */
529 *status |= pls;
533 * Function for Compliance Mode Quirk.
535 * This Function verifies if all xhc USB3 ports have entered U0, if so,
536 * the compliance mode timer is deleted. A port won't enter
537 * compliance mode if it has previously entered U0.
539 void xhci_del_comp_mod_timer(struct xhci_hcd *xhci, u32 status, u16 wIndex)
541 u32 all_ports_seen_u0 = ((1 << xhci->num_usb3_ports)-1);
542 bool port_in_u0 = ((status & PORT_PLS_MASK) == XDEV_U0);
544 if (!(xhci->quirks & XHCI_COMP_MODE_QUIRK))
545 return;
547 if ((xhci->port_status_u0 != all_ports_seen_u0) && port_in_u0) {
548 xhci->port_status_u0 |= 1 << wIndex;
549 if (xhci->port_status_u0 == all_ports_seen_u0) {
550 del_timer_sync(&xhci->comp_mode_recovery_timer);
551 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
552 "All USB3 ports have entered U0 already!");
553 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
554 "Compliance Mode Recovery Timer Deleted.");
560 * Converts a raw xHCI port status into the format that external USB 2.0 or USB
561 * 3.0 hubs use.
563 * Possible side effects:
564 * - Mark a port as being done with device resume,
565 * and ring the endpoint doorbells.
566 * - Stop the Synopsys redriver Compliance Mode polling.
567 * - Drop and reacquire the xHCI lock, in order to wait for port resume.
569 static u32 xhci_get_port_status(struct usb_hcd *hcd,
570 struct xhci_bus_state *bus_state,
571 __le32 __iomem **port_array,
572 u16 wIndex, u32 raw_port_status,
573 unsigned long flags)
574 __releases(&xhci->lock)
575 __acquires(&xhci->lock)
577 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
578 u32 status = 0;
579 int slot_id;
581 /* wPortChange bits */
582 if (raw_port_status & PORT_CSC)
583 status |= USB_PORT_STAT_C_CONNECTION << 16;
584 if (raw_port_status & PORT_PEC)
585 status |= USB_PORT_STAT_C_ENABLE << 16;
586 if ((raw_port_status & PORT_OCC))
587 status |= USB_PORT_STAT_C_OVERCURRENT << 16;
588 if ((raw_port_status & PORT_RC))
589 status |= USB_PORT_STAT_C_RESET << 16;
590 /* USB3.0 only */
591 if (hcd->speed == HCD_USB3) {
592 /* Port link change with port in resume state should not be
593 * reported to usbcore, as this is an internal state to be
594 * handled by xhci driver. Reporting PLC to usbcore may
595 * cause usbcore clearing PLC first and port change event
596 * irq won't be generated.
598 if ((raw_port_status & PORT_PLC) &&
599 (raw_port_status & PORT_PLS_MASK) != XDEV_RESUME)
600 status |= USB_PORT_STAT_C_LINK_STATE << 16;
601 if ((raw_port_status & PORT_WRC))
602 status |= USB_PORT_STAT_C_BH_RESET << 16;
603 if ((raw_port_status & PORT_CEC))
604 status |= USB_PORT_STAT_C_CONFIG_ERROR << 16;
607 if (hcd->speed != HCD_USB3) {
608 if ((raw_port_status & PORT_PLS_MASK) == XDEV_U3
609 && (raw_port_status & PORT_POWER))
610 status |= USB_PORT_STAT_SUSPEND;
612 if ((raw_port_status & PORT_PLS_MASK) == XDEV_RESUME &&
613 !DEV_SUPERSPEED(raw_port_status)) {
614 if ((raw_port_status & PORT_RESET) ||
615 !(raw_port_status & PORT_PE))
616 return 0xffffffff;
617 /* did port event handler already start resume timing? */
618 if (!bus_state->resume_done[wIndex]) {
619 /* If not, maybe we are in a host initated resume? */
620 if (test_bit(wIndex, &bus_state->resuming_ports)) {
621 /* Host initated resume doesn't time the resume
622 * signalling using resume_done[].
623 * It manually sets RESUME state, sleeps 20ms
624 * and sets U0 state. This should probably be
625 * changed, but not right now.
627 } else {
628 /* port resume was discovered now and here,
629 * start resume timing
631 unsigned long timeout = jiffies +
632 msecs_to_jiffies(USB_RESUME_TIMEOUT);
634 set_bit(wIndex, &bus_state->resuming_ports);
635 bus_state->resume_done[wIndex] = timeout;
636 mod_timer(&hcd->rh_timer, timeout);
638 /* Has resume been signalled for USB_RESUME_TIME yet? */
639 } else if (time_after_eq(jiffies,
640 bus_state->resume_done[wIndex])) {
641 int time_left;
643 xhci_dbg(xhci, "Resume USB2 port %d\n",
644 wIndex + 1);
645 bus_state->resume_done[wIndex] = 0;
646 clear_bit(wIndex, &bus_state->resuming_ports);
648 set_bit(wIndex, &bus_state->rexit_ports);
649 xhci_set_link_state(xhci, port_array, wIndex,
650 XDEV_U0);
652 spin_unlock_irqrestore(&xhci->lock, flags);
653 time_left = wait_for_completion_timeout(
654 &bus_state->rexit_done[wIndex],
655 msecs_to_jiffies(
656 XHCI_MAX_REXIT_TIMEOUT));
657 spin_lock_irqsave(&xhci->lock, flags);
659 if (time_left) {
660 slot_id = xhci_find_slot_id_by_port(hcd,
661 xhci, wIndex + 1);
662 if (!slot_id) {
663 xhci_dbg(xhci, "slot_id is zero\n");
664 return 0xffffffff;
666 xhci_ring_device(xhci, slot_id);
667 } else {
668 int port_status = xhci_readl(xhci,
669 port_array[wIndex]);
670 xhci_warn(xhci, "Port resume took longer than %i msec, port status = 0x%x\n",
671 XHCI_MAX_REXIT_TIMEOUT,
672 port_status);
673 status |= USB_PORT_STAT_SUSPEND;
674 clear_bit(wIndex, &bus_state->rexit_ports);
677 bus_state->port_c_suspend |= 1 << wIndex;
678 bus_state->suspended_ports &= ~(1 << wIndex);
679 } else {
681 * The resume has been signaling for less than
682 * USB_RESUME_TIME. Report the port status as SUSPEND,
683 * let the usbcore check port status again and clear
684 * resume signaling later.
686 status |= USB_PORT_STAT_SUSPEND;
690 * Clear stale usb2 resume signalling variables in case port changed
691 * state during resume signalling. For example on error
693 if ((bus_state->resume_done[wIndex] ||
694 test_bit(wIndex, &bus_state->resuming_ports)) &&
695 (raw_port_status & PORT_PLS_MASK) != XDEV_U3 &&
696 (raw_port_status & PORT_PLS_MASK) != XDEV_RESUME) {
697 bus_state->resume_done[wIndex] = 0;
698 clear_bit(wIndex, &bus_state->resuming_ports);
700 if ((raw_port_status & PORT_PLS_MASK) == XDEV_U0
701 && (raw_port_status & PORT_POWER)
702 && (bus_state->suspended_ports & (1 << wIndex))) {
703 bus_state->suspended_ports &= ~(1 << wIndex);
704 if (hcd->speed != HCD_USB3)
705 bus_state->port_c_suspend |= 1 << wIndex;
707 if (raw_port_status & PORT_CONNECT) {
708 status |= USB_PORT_STAT_CONNECTION;
709 status |= xhci_port_speed(raw_port_status);
711 if (raw_port_status & PORT_PE)
712 status |= USB_PORT_STAT_ENABLE;
713 if (raw_port_status & PORT_OC)
714 status |= USB_PORT_STAT_OVERCURRENT;
715 if (raw_port_status & PORT_RESET)
716 status |= USB_PORT_STAT_RESET;
717 if (raw_port_status & PORT_POWER) {
718 if (hcd->speed == HCD_USB3)
719 status |= USB_SS_PORT_STAT_POWER;
720 else
721 status |= USB_PORT_STAT_POWER;
723 /* Update Port Link State */
724 if (hcd->speed == HCD_USB3) {
725 xhci_hub_report_usb3_link_state(xhci, &status, raw_port_status);
727 * Verify if all USB3 Ports Have entered U0 already.
728 * Delete Compliance Mode Timer if so.
730 xhci_del_comp_mod_timer(xhci, raw_port_status, wIndex);
731 } else {
732 xhci_hub_report_usb2_link_state(&status, raw_port_status);
734 if (bus_state->port_c_suspend & (1 << wIndex))
735 status |= 1 << USB_PORT_FEAT_C_SUSPEND;
737 return status;
740 int xhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
741 u16 wIndex, char *buf, u16 wLength)
743 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
744 int max_ports;
745 unsigned long flags;
746 u32 temp, status;
747 int retval = 0;
748 __le32 __iomem **port_array;
749 int slot_id;
750 struct xhci_bus_state *bus_state;
751 u16 link_state = 0;
752 u16 wake_mask = 0;
753 u16 timeout = 0;
755 max_ports = xhci_get_ports(hcd, &port_array);
756 bus_state = &xhci->bus_state[hcd_index(hcd)];
758 spin_lock_irqsave(&xhci->lock, flags);
759 switch (typeReq) {
760 case GetHubStatus:
761 /* No power source, over-current reported per port */
762 memset(buf, 0, 4);
763 break;
764 case GetHubDescriptor:
765 /* Check to make sure userspace is asking for the USB 3.0 hub
766 * descriptor for the USB 3.0 roothub. If not, we stall the
767 * endpoint, like external hubs do.
769 if (hcd->speed == HCD_USB3 &&
770 (wLength < USB_DT_SS_HUB_SIZE ||
771 wValue != (USB_DT_SS_HUB << 8))) {
772 xhci_dbg(xhci, "Wrong hub descriptor type for "
773 "USB 3.0 roothub.\n");
774 goto error;
776 xhci_hub_descriptor(hcd, xhci,
777 (struct usb_hub_descriptor *) buf);
778 break;
779 case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
780 if ((wValue & 0xff00) != (USB_DT_BOS << 8))
781 goto error;
783 if (hcd->speed != HCD_USB3)
784 goto error;
786 /* Set the U1 and U2 exit latencies. */
787 memcpy(buf, &usb_bos_descriptor,
788 USB_DT_BOS_SIZE + USB_DT_USB_SS_CAP_SIZE);
789 temp = xhci_readl(xhci, &xhci->cap_regs->hcs_params3);
790 buf[12] = HCS_U1_LATENCY(temp);
791 put_unaligned_le16(HCS_U2_LATENCY(temp), &buf[13]);
793 /* Indicate whether the host has LTM support. */
794 temp = xhci_readl(xhci, &xhci->cap_regs->hcc_params);
795 if (HCC_LTC(temp))
796 buf[8] |= USB_LTM_SUPPORT;
798 spin_unlock_irqrestore(&xhci->lock, flags);
799 return USB_DT_BOS_SIZE + USB_DT_USB_SS_CAP_SIZE;
800 case GetPortStatus:
801 if (!wIndex || wIndex > max_ports)
802 goto error;
803 wIndex--;
804 temp = xhci_readl(xhci, port_array[wIndex]);
805 if (temp == 0xffffffff) {
806 retval = -ENODEV;
807 break;
809 status = xhci_get_port_status(hcd, bus_state, port_array,
810 wIndex, temp, flags);
811 if (status == 0xffffffff)
812 goto error;
814 xhci_dbg(xhci, "get port status, actual port %d status = 0x%x\n",
815 wIndex, temp);
816 xhci_dbg(xhci, "Get port status returned 0x%x\n", status);
818 put_unaligned(cpu_to_le32(status), (__le32 *) buf);
819 break;
820 case SetPortFeature:
821 if (wValue == USB_PORT_FEAT_LINK_STATE)
822 link_state = (wIndex & 0xff00) >> 3;
823 if (wValue == USB_PORT_FEAT_REMOTE_WAKE_MASK)
824 wake_mask = wIndex & 0xff00;
825 /* The MSB of wIndex is the U1/U2 timeout */
826 timeout = (wIndex & 0xff00) >> 8;
827 wIndex &= 0xff;
828 if (!wIndex || wIndex > max_ports)
829 goto error;
830 wIndex--;
831 temp = xhci_readl(xhci, port_array[wIndex]);
832 if (temp == 0xffffffff) {
833 retval = -ENODEV;
834 break;
836 temp = xhci_port_state_to_neutral(temp);
837 /* FIXME: What new port features do we need to support? */
838 switch (wValue) {
839 case USB_PORT_FEAT_SUSPEND:
840 temp = xhci_readl(xhci, port_array[wIndex]);
841 if ((temp & PORT_PLS_MASK) != XDEV_U0) {
842 /* Resume the port to U0 first */
843 xhci_set_link_state(xhci, port_array, wIndex,
844 XDEV_U0);
845 spin_unlock_irqrestore(&xhci->lock, flags);
846 msleep(10);
847 spin_lock_irqsave(&xhci->lock, flags);
849 /* In spec software should not attempt to suspend
850 * a port unless the port reports that it is in the
851 * enabled (PED = ‘1’,PLS < ‘3’) state.
853 temp = xhci_readl(xhci, port_array[wIndex]);
854 if ((temp & PORT_PE) == 0 || (temp & PORT_RESET)
855 || (temp & PORT_PLS_MASK) >= XDEV_U3) {
856 xhci_warn(xhci, "USB core suspending device "
857 "not in U0/U1/U2.\n");
858 goto error;
861 slot_id = xhci_find_slot_id_by_port(hcd, xhci,
862 wIndex + 1);
863 if (!slot_id) {
864 xhci_warn(xhci, "slot_id is zero\n");
865 goto error;
867 /* unlock to execute stop endpoint commands */
868 spin_unlock_irqrestore(&xhci->lock, flags);
869 xhci_stop_device(xhci, slot_id, 1);
870 spin_lock_irqsave(&xhci->lock, flags);
872 xhci_set_link_state(xhci, port_array, wIndex, XDEV_U3);
874 spin_unlock_irqrestore(&xhci->lock, flags);
875 msleep(10); /* wait device to enter */
876 spin_lock_irqsave(&xhci->lock, flags);
878 temp = xhci_readl(xhci, port_array[wIndex]);
879 bus_state->suspended_ports |= 1 << wIndex;
880 break;
881 case USB_PORT_FEAT_LINK_STATE:
882 temp = xhci_readl(xhci, port_array[wIndex]);
884 /* Disable port */
885 if (link_state == USB_SS_PORT_LS_SS_DISABLED) {
886 xhci_dbg(xhci, "Disable port %d\n", wIndex);
887 temp = xhci_port_state_to_neutral(temp);
889 * Clear all change bits, so that we get a new
890 * connection event.
892 temp |= PORT_CSC | PORT_PEC | PORT_WRC |
893 PORT_OCC | PORT_RC | PORT_PLC |
894 PORT_CEC;
895 xhci_writel(xhci, temp | PORT_PE,
896 port_array[wIndex]);
897 temp = xhci_readl(xhci, port_array[wIndex]);
898 break;
901 /* Put link in RxDetect (enable port) */
902 if (link_state == USB_SS_PORT_LS_RX_DETECT) {
903 xhci_dbg(xhci, "Enable port %d\n", wIndex);
904 xhci_set_link_state(xhci, port_array, wIndex,
905 link_state);
906 temp = xhci_readl(xhci, port_array[wIndex]);
907 break;
910 /* Software should not attempt to set
911 * port link state above '3' (U3) and the port
912 * must be enabled.
914 if ((temp & PORT_PE) == 0 ||
915 (link_state > USB_SS_PORT_LS_U3)) {
916 xhci_warn(xhci, "Cannot set link state.\n");
917 goto error;
920 if (link_state == USB_SS_PORT_LS_U3) {
921 slot_id = xhci_find_slot_id_by_port(hcd, xhci,
922 wIndex + 1);
923 if (slot_id) {
924 /* unlock to execute stop endpoint
925 * commands */
926 spin_unlock_irqrestore(&xhci->lock,
927 flags);
928 xhci_stop_device(xhci, slot_id, 1);
929 spin_lock_irqsave(&xhci->lock, flags);
933 xhci_set_link_state(xhci, port_array, wIndex,
934 link_state);
936 spin_unlock_irqrestore(&xhci->lock, flags);
937 msleep(20); /* wait device to enter */
938 spin_lock_irqsave(&xhci->lock, flags);
940 temp = xhci_readl(xhci, port_array[wIndex]);
941 if (link_state == USB_SS_PORT_LS_U3)
942 bus_state->suspended_ports |= 1 << wIndex;
943 break;
944 case USB_PORT_FEAT_POWER:
946 * Turn on ports, even if there isn't per-port switching.
947 * HC will report connect events even before this is set.
948 * However, khubd will ignore the roothub events until
949 * the roothub is registered.
951 xhci_writel(xhci, temp | PORT_POWER,
952 port_array[wIndex]);
954 temp = xhci_readl(xhci, port_array[wIndex]);
955 xhci_dbg(xhci, "set port power, actual port %d status = 0x%x\n", wIndex, temp);
957 spin_unlock_irqrestore(&xhci->lock, flags);
958 temp = usb_acpi_power_manageable(hcd->self.root_hub,
959 wIndex);
960 if (temp)
961 usb_acpi_set_power_state(hcd->self.root_hub,
962 wIndex, true);
963 spin_lock_irqsave(&xhci->lock, flags);
964 break;
965 case USB_PORT_FEAT_RESET:
966 temp = (temp | PORT_RESET);
967 xhci_writel(xhci, temp, port_array[wIndex]);
969 temp = xhci_readl(xhci, port_array[wIndex]);
970 xhci_dbg(xhci, "set port reset, actual port %d status = 0x%x\n", wIndex, temp);
971 break;
972 case USB_PORT_FEAT_REMOTE_WAKE_MASK:
973 xhci_set_remote_wake_mask(xhci, port_array,
974 wIndex, wake_mask);
975 temp = xhci_readl(xhci, port_array[wIndex]);
976 xhci_dbg(xhci, "set port remote wake mask, "
977 "actual port %d status = 0x%x\n",
978 wIndex, temp);
979 break;
980 case USB_PORT_FEAT_BH_PORT_RESET:
981 temp |= PORT_WR;
982 xhci_writel(xhci, temp, port_array[wIndex]);
984 temp = xhci_readl(xhci, port_array[wIndex]);
985 break;
986 case USB_PORT_FEAT_U1_TIMEOUT:
987 if (hcd->speed != HCD_USB3)
988 goto error;
989 temp = xhci_readl(xhci, port_array[wIndex] + PORTPMSC);
990 temp &= ~PORT_U1_TIMEOUT_MASK;
991 temp |= PORT_U1_TIMEOUT(timeout);
992 xhci_writel(xhci, temp, port_array[wIndex] + PORTPMSC);
993 break;
994 case USB_PORT_FEAT_U2_TIMEOUT:
995 if (hcd->speed != HCD_USB3)
996 goto error;
997 temp = xhci_readl(xhci, port_array[wIndex] + PORTPMSC);
998 temp &= ~PORT_U2_TIMEOUT_MASK;
999 temp |= PORT_U2_TIMEOUT(timeout);
1000 xhci_writel(xhci, temp, port_array[wIndex] + PORTPMSC);
1001 break;
1002 default:
1003 goto error;
1005 /* unblock any posted writes */
1006 temp = xhci_readl(xhci, port_array[wIndex]);
1007 break;
1008 case ClearPortFeature:
1009 if (!wIndex || wIndex > max_ports)
1010 goto error;
1011 wIndex--;
1012 temp = xhci_readl(xhci, port_array[wIndex]);
1013 if (temp == 0xffffffff) {
1014 retval = -ENODEV;
1015 break;
1017 /* FIXME: What new port features do we need to support? */
1018 temp = xhci_port_state_to_neutral(temp);
1019 switch (wValue) {
1020 case USB_PORT_FEAT_SUSPEND:
1021 temp = xhci_readl(xhci, port_array[wIndex]);
1022 xhci_dbg(xhci, "clear USB_PORT_FEAT_SUSPEND\n");
1023 xhci_dbg(xhci, "PORTSC %04x\n", temp);
1024 if (temp & PORT_RESET)
1025 goto error;
1026 if ((temp & PORT_PLS_MASK) == XDEV_U3) {
1027 if ((temp & PORT_PE) == 0)
1028 goto error;
1030 set_bit(wIndex, &bus_state->resuming_ports);
1031 xhci_set_link_state(xhci, port_array, wIndex,
1032 XDEV_RESUME);
1033 spin_unlock_irqrestore(&xhci->lock, flags);
1034 msleep(20);
1035 spin_lock_irqsave(&xhci->lock, flags);
1036 xhci_set_link_state(xhci, port_array, wIndex,
1037 XDEV_U0);
1038 clear_bit(wIndex, &bus_state->resuming_ports);
1040 bus_state->port_c_suspend |= 1 << wIndex;
1042 slot_id = xhci_find_slot_id_by_port(hcd, xhci,
1043 wIndex + 1);
1044 if (!slot_id) {
1045 xhci_dbg(xhci, "slot_id is zero\n");
1046 goto error;
1048 xhci_ring_device(xhci, slot_id);
1049 break;
1050 case USB_PORT_FEAT_C_SUSPEND:
1051 bus_state->port_c_suspend &= ~(1 << wIndex);
1052 case USB_PORT_FEAT_C_RESET:
1053 case USB_PORT_FEAT_C_BH_PORT_RESET:
1054 case USB_PORT_FEAT_C_CONNECTION:
1055 case USB_PORT_FEAT_C_OVER_CURRENT:
1056 case USB_PORT_FEAT_C_ENABLE:
1057 case USB_PORT_FEAT_C_PORT_LINK_STATE:
1058 case USB_PORT_FEAT_C_PORT_CONFIG_ERROR:
1059 xhci_clear_port_change_bit(xhci, wValue, wIndex,
1060 port_array[wIndex], temp);
1061 break;
1062 case USB_PORT_FEAT_ENABLE:
1063 xhci_disable_port(hcd, xhci, wIndex,
1064 port_array[wIndex], temp);
1065 break;
1066 case USB_PORT_FEAT_POWER:
1067 xhci_writel(xhci, temp & ~PORT_POWER,
1068 port_array[wIndex]);
1070 spin_unlock_irqrestore(&xhci->lock, flags);
1071 temp = usb_acpi_power_manageable(hcd->self.root_hub,
1072 wIndex);
1073 if (temp)
1074 usb_acpi_set_power_state(hcd->self.root_hub,
1075 wIndex, false);
1076 spin_lock_irqsave(&xhci->lock, flags);
1077 break;
1078 default:
1079 goto error;
1081 break;
1082 default:
1083 error:
1084 /* "stall" on error */
1085 retval = -EPIPE;
1087 spin_unlock_irqrestore(&xhci->lock, flags);
1088 return retval;
1092 * Returns 0 if the status hasn't changed, or the number of bytes in buf.
1093 * Ports are 0-indexed from the HCD point of view,
1094 * and 1-indexed from the USB core pointer of view.
1096 * Note that the status change bits will be cleared as soon as a port status
1097 * change event is generated, so we use the saved status from that event.
1099 int xhci_hub_status_data(struct usb_hcd *hcd, char *buf)
1101 unsigned long flags;
1102 u32 temp, status;
1103 u32 mask;
1104 int i, retval;
1105 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
1106 int max_ports;
1107 __le32 __iomem **port_array;
1108 struct xhci_bus_state *bus_state;
1109 bool reset_change = false;
1111 max_ports = xhci_get_ports(hcd, &port_array);
1112 bus_state = &xhci->bus_state[hcd_index(hcd)];
1114 /* Initial status is no changes */
1115 retval = (max_ports + 8) / 8;
1116 memset(buf, 0, retval);
1119 * Inform the usbcore about resume-in-progress by returning
1120 * a non-zero value even if there are no status changes.
1122 status = bus_state->resuming_ports;
1124 mask = PORT_CSC | PORT_PEC | PORT_OCC | PORT_PLC | PORT_WRC | PORT_CEC;
1126 spin_lock_irqsave(&xhci->lock, flags);
1127 /* For each port, did anything change? If so, set that bit in buf. */
1128 for (i = 0; i < max_ports; i++) {
1129 temp = xhci_readl(xhci, port_array[i]);
1130 if (temp == 0xffffffff) {
1131 retval = -ENODEV;
1132 break;
1134 if ((temp & mask) != 0 ||
1135 (bus_state->port_c_suspend & 1 << i) ||
1136 (bus_state->resume_done[i] && time_after_eq(
1137 jiffies, bus_state->resume_done[i]))) {
1138 buf[(i + 1) / 8] |= 1 << (i + 1) % 8;
1139 status = 1;
1141 if ((temp & PORT_RC))
1142 reset_change = true;
1144 if (!status && !reset_change) {
1145 xhci_dbg(xhci, "%s: stopping port polling.\n", __func__);
1146 clear_bit(HCD_FLAG_POLL_RH, &hcd->flags);
1148 spin_unlock_irqrestore(&xhci->lock, flags);
1149 return status ? retval : 0;
1152 #ifdef CONFIG_PM
1154 int xhci_bus_suspend(struct usb_hcd *hcd)
1156 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
1157 int max_ports, port_index;
1158 __le32 __iomem **port_array;
1159 struct xhci_bus_state *bus_state;
1160 unsigned long flags;
1162 max_ports = xhci_get_ports(hcd, &port_array);
1163 bus_state = &xhci->bus_state[hcd_index(hcd)];
1165 spin_lock_irqsave(&xhci->lock, flags);
1167 if (hcd->self.root_hub->do_remote_wakeup) {
1168 if (bus_state->resuming_ports || /* USB2 */
1169 bus_state->port_remote_wakeup) { /* USB3 */
1170 spin_unlock_irqrestore(&xhci->lock, flags);
1171 xhci_dbg(xhci, "suspend failed because a port is resuming\n");
1172 return -EBUSY;
1176 port_index = max_ports;
1177 bus_state->bus_suspended = 0;
1178 while (port_index--) {
1179 /* suspend the port if the port is not suspended */
1180 u32 t1, t2;
1181 int slot_id;
1183 t1 = xhci_readl(xhci, port_array[port_index]);
1184 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);
1195 t2 &= ~PORT_PLS_MASK;
1196 t2 |= PORT_LINK_STROBE | XDEV_U3;
1197 set_bit(port_index, &bus_state->bus_suspended);
1199 /* USB core sets remote wake mask for USB 3.0 hubs,
1200 * including the USB 3.0 roothub, but only if CONFIG_PM_RUNTIME
1201 * is enabled, so also enable remote wake here.
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;
1211 } else
1212 t2 &= ~PORT_WAKE_BITS;
1214 t1 = xhci_port_state_to_neutral(t1);
1215 if (t1 != t2)
1216 xhci_writel(xhci, t2, port_array[port_index]);
1218 hcd->state = HC_STATE_SUSPENDED;
1219 bus_state->next_statechange = jiffies + msecs_to_jiffies(10);
1220 spin_unlock_irqrestore(&xhci->lock, flags);
1221 return 0;
1224 int xhci_bus_resume(struct usb_hcd *hcd)
1226 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
1227 int max_ports, port_index;
1228 __le32 __iomem **port_array;
1229 struct xhci_bus_state *bus_state;
1230 u32 temp;
1231 unsigned long flags;
1233 max_ports = xhci_get_ports(hcd, &port_array);
1234 bus_state = &xhci->bus_state[hcd_index(hcd)];
1236 if (time_before(jiffies, bus_state->next_statechange))
1237 msleep(5);
1239 spin_lock_irqsave(&xhci->lock, flags);
1240 if (!HCD_HW_ACCESSIBLE(hcd)) {
1241 spin_unlock_irqrestore(&xhci->lock, flags);
1242 return -ESHUTDOWN;
1245 /* delay the irqs */
1246 temp = xhci_readl(xhci, &xhci->op_regs->command);
1247 temp &= ~CMD_EIE;
1248 xhci_writel(xhci, temp, &xhci->op_regs->command);
1250 port_index = max_ports;
1251 while (port_index--) {
1252 /* Check whether need resume ports. If needed
1253 resume port and disable remote wakeup */
1254 u32 temp;
1255 int slot_id;
1257 temp = xhci_readl(xhci, port_array[port_index]);
1258 if (DEV_SUPERSPEED(temp))
1259 temp &= ~(PORT_RWC_BITS | PORT_CEC | PORT_WAKE_BITS);
1260 else
1261 temp &= ~(PORT_RWC_BITS | PORT_WAKE_BITS);
1262 if (test_bit(port_index, &bus_state->bus_suspended) &&
1263 (temp & PORT_PLS_MASK)) {
1264 if (DEV_SUPERSPEED(temp)) {
1265 xhci_set_link_state(xhci, port_array,
1266 port_index, XDEV_U0);
1267 } else {
1268 xhci_set_link_state(xhci, port_array,
1269 port_index, XDEV_RESUME);
1271 spin_unlock_irqrestore(&xhci->lock, flags);
1272 msleep(20);
1273 spin_lock_irqsave(&xhci->lock, flags);
1275 xhci_set_link_state(xhci, port_array,
1276 port_index, XDEV_U0);
1278 /* wait for the port to enter U0 and report port link
1279 * state change.
1281 spin_unlock_irqrestore(&xhci->lock, flags);
1282 msleep(20);
1283 spin_lock_irqsave(&xhci->lock, flags);
1285 /* Clear PLC */
1286 xhci_test_and_clear_bit(xhci, port_array, port_index,
1287 PORT_PLC);
1289 slot_id = xhci_find_slot_id_by_port(hcd,
1290 xhci, port_index + 1);
1291 if (slot_id)
1292 xhci_ring_device(xhci, slot_id);
1293 } else
1294 xhci_writel(xhci, temp, port_array[port_index]);
1297 (void) xhci_readl(xhci, &xhci->op_regs->command);
1299 bus_state->next_statechange = jiffies + msecs_to_jiffies(5);
1300 /* re-enable irqs */
1301 temp = xhci_readl(xhci, &xhci->op_regs->command);
1302 temp |= CMD_EIE;
1303 xhci_writel(xhci, temp, &xhci->op_regs->command);
1304 temp = xhci_readl(xhci, &xhci->op_regs->command);
1306 spin_unlock_irqrestore(&xhci->lock, flags);
1307 return 0;
1310 #endif /* CONFIG_PM */