178
178
return usb_get_intfdata(hdev->actconfig->interface[0]);
181
static int usb_device_supports_lpm(struct usb_device *udev)
183
/* USB 2.1 (and greater) devices indicate LPM support through
184
* their USB 2.0 Extended Capabilities BOS descriptor.
186
if (udev->speed == USB_SPEED_HIGH) {
187
if (udev->bos->ext_cap &&
189
le32_to_cpu(udev->bos->ext_cap->bmAttributes)))
194
/* All USB 3.0 must support LPM, but we need their max exit latency
195
* information from the SuperSpeed Extended Capabilities BOS descriptor.
197
if (!udev->bos->ss_cap) {
198
dev_warn(&udev->dev, "No LPM exit latency info found. "
199
"Power management will be impacted.\n");
202
if (udev->parent->lpm_capable)
205
dev_warn(&udev->dev, "Parent hub missing LPM exit latency info. "
206
"Power management will be impacted.\n");
211
* Set the Maximum Exit Latency (MEL) for the host to initiate a transition from
214
static void usb_set_lpm_mel(struct usb_device *udev,
215
struct usb3_lpm_parameters *udev_lpm_params,
216
unsigned int udev_exit_latency,
218
struct usb3_lpm_parameters *hub_lpm_params,
219
unsigned int hub_exit_latency)
221
unsigned int total_mel;
222
unsigned int device_mel;
223
unsigned int hub_mel;
226
* Calculate the time it takes to transition all links from the roothub
227
* to the parent hub into U0. The parent hub must then decode the
228
* packet (hub header decode latency) to figure out which port it was
231
* The Hub Header decode latency is expressed in 0.1us intervals (0x1
232
* means 0.1us). Multiply that by 100 to get nanoseconds.
234
total_mel = hub_lpm_params->mel +
235
(hub->descriptor->u.ss.bHubHdrDecLat * 100);
238
* How long will it take to transition the downstream hub's port into
239
* U0? The greater of either the hub exit latency or the device exit
242
* The BOS U1/U2 exit latencies are expressed in 1us intervals.
243
* Multiply that by 1000 to get nanoseconds.
245
device_mel = udev_exit_latency * 1000;
246
hub_mel = hub_exit_latency * 1000;
247
if (device_mel > hub_mel)
248
total_mel += device_mel;
250
total_mel += hub_mel;
252
udev_lpm_params->mel = total_mel;
256
* Set the maximum Device to Host Exit Latency (PEL) for the device to initiate
257
* a transition from either U1 or U2.
259
static void usb_set_lpm_pel(struct usb_device *udev,
260
struct usb3_lpm_parameters *udev_lpm_params,
261
unsigned int udev_exit_latency,
263
struct usb3_lpm_parameters *hub_lpm_params,
264
unsigned int hub_exit_latency,
265
unsigned int port_to_port_exit_latency)
267
unsigned int first_link_pel;
268
unsigned int hub_pel;
271
* First, the device sends an LFPS to transition the link between the
272
* device and the parent hub into U0. The exit latency is the bigger of
273
* the device exit latency or the hub exit latency.
275
if (udev_exit_latency > hub_exit_latency)
276
first_link_pel = udev_exit_latency * 1000;
278
first_link_pel = hub_exit_latency * 1000;
281
* When the hub starts to receive the LFPS, there is a slight delay for
282
* it to figure out that one of the ports is sending an LFPS. Then it
283
* will forward the LFPS to its upstream link. The exit latency is the
284
* delay, plus the PEL that we calculated for this hub.
286
hub_pel = port_to_port_exit_latency * 1000 + hub_lpm_params->pel;
289
* According to figure C-7 in the USB 3.0 spec, the PEL for this device
290
* is the greater of the two exit latencies.
292
if (first_link_pel > hub_pel)
293
udev_lpm_params->pel = first_link_pel;
295
udev_lpm_params->pel = hub_pel;
299
* Set the System Exit Latency (SEL) to indicate the total worst-case time from
300
* when a device initiates a transition to U0, until when it will receive the
301
* first packet from the host controller.
303
* Section C.1.5.1 describes the four components to this:
305
* - t2: time for the ERDY to make it from the device to the host.
306
* - t3: a host-specific delay to process the ERDY.
307
* - t4: time for the packet to make it from the host to the device.
309
* t3 is specific to both the xHCI host and the platform the host is integrated
310
* into. The Intel HW folks have said it's negligible, FIXME if a different
311
* vendor says otherwise.
313
static void usb_set_lpm_sel(struct usb_device *udev,
314
struct usb3_lpm_parameters *udev_lpm_params)
316
struct usb_device *parent;
317
unsigned int num_hubs;
318
unsigned int total_sel;
320
/* t1 = device PEL */
321
total_sel = udev_lpm_params->pel;
322
/* How many external hubs are in between the device & the root port. */
323
for (parent = udev->parent, num_hubs = 0; parent->parent;
324
parent = parent->parent)
326
/* t2 = 2.1us + 250ns * (num_hubs - 1) */
328
total_sel += 2100 + 250 * (num_hubs - 1);
330
/* t4 = 250ns * num_hubs */
331
total_sel += 250 * num_hubs;
333
udev_lpm_params->sel = total_sel;
336
static void usb_set_lpm_parameters(struct usb_device *udev)
339
unsigned int port_to_port_delay;
340
unsigned int udev_u1_del;
341
unsigned int udev_u2_del;
342
unsigned int hub_u1_del;
343
unsigned int hub_u2_del;
345
if (!udev->lpm_capable || udev->speed != USB_SPEED_SUPER)
348
hub = hdev_to_hub(udev->parent);
349
/* It doesn't take time to transition the roothub into U0, since it
350
* doesn't have an upstream link.
355
udev_u1_del = udev->bos->ss_cap->bU1devExitLat;
356
udev_u2_del = udev->bos->ss_cap->bU2DevExitLat;
357
hub_u1_del = udev->parent->bos->ss_cap->bU1devExitLat;
358
hub_u2_del = udev->parent->bos->ss_cap->bU2DevExitLat;
360
usb_set_lpm_mel(udev, &udev->u1_params, udev_u1_del,
361
hub, &udev->parent->u1_params, hub_u1_del);
363
usb_set_lpm_mel(udev, &udev->u2_params, udev_u2_del,
364
hub, &udev->parent->u2_params, hub_u2_del);
367
* Appendix C, section C.2.2.2, says that there is a slight delay from
368
* when the parent hub notices the downstream port is trying to
369
* transition to U0 to when the hub initiates a U0 transition on its
370
* upstream port. The section says the delays are tPort2PortU1EL and
371
* tPort2PortU2EL, but it doesn't define what they are.
373
* The hub chapter, sections 10.4.2.4 and 10.4.2.5 seem to be talking
374
* about the same delays. Use the maximum delay calculations from those
375
* sections. For U1, it's tHubPort2PortExitLat, which is 1us max. For
376
* U2, it's tHubPort2PortExitLat + U2DevExitLat - U1DevExitLat. I
377
* assume the device exit latencies they are talking about are the hub
380
* What do we do if the U2 exit latency is less than the U1 exit
381
* latency? It's possible, although not likely...
383
port_to_port_delay = 1;
385
usb_set_lpm_pel(udev, &udev->u1_params, udev_u1_del,
386
hub, &udev->parent->u1_params, hub_u1_del,
389
if (hub_u2_del > hub_u1_del)
390
port_to_port_delay = 1 + hub_u2_del - hub_u1_del;
392
port_to_port_delay = 1 + hub_u1_del;
394
usb_set_lpm_pel(udev, &udev->u2_params, udev_u2_del,
395
hub, &udev->parent->u2_params, hub_u2_del,
398
/* Now that we've got PEL, calculate SEL. */
399
usb_set_lpm_sel(udev, &udev->u1_params);
400
usb_set_lpm_sel(udev, &udev->u2_params);
181
403
/* USB 2.0 spec Section 11.24.4.5 */
182
404
static int get_hub_descriptor(struct usb_device *hdev, void *data)
2851
3150
EXPORT_SYMBOL_GPL(usb_root_hub_lost_power);
3152
static const char * const usb3_lpm_names[] = {
3160
* Send a Set SEL control transfer to the device, prior to enabling
3161
* device-initiated U1 or U2. This lets the device know the exit latencies from
3162
* the time the device initiates a U1 or U2 exit, to the time it will receive a
3163
* packet from the host.
3165
* This function will fail if the SEL or PEL values for udev are greater than
3166
* the maximum allowed values for the link state to be enabled.
3168
static int usb_req_set_sel(struct usb_device *udev, enum usb3_link_state state)
3170
struct usb_set_sel_req *sel_values;
3171
unsigned long long u1_sel;
3172
unsigned long long u1_pel;
3173
unsigned long long u2_sel;
3174
unsigned long long u2_pel;
3177
/* Convert SEL and PEL stored in ns to us */
3178
u1_sel = DIV_ROUND_UP(udev->u1_params.sel, 1000);
3179
u1_pel = DIV_ROUND_UP(udev->u1_params.pel, 1000);
3180
u2_sel = DIV_ROUND_UP(udev->u2_params.sel, 1000);
3181
u2_pel = DIV_ROUND_UP(udev->u2_params.pel, 1000);
3184
* Make sure that the calculated SEL and PEL values for the link
3185
* state we're enabling aren't bigger than the max SEL/PEL
3186
* value that will fit in the SET SEL control transfer.
3187
* Otherwise the device would get an incorrect idea of the exit
3188
* latency for the link state, and could start a device-initiated
3189
* U1/U2 when the exit latencies are too high.
3191
if ((state == USB3_LPM_U1 &&
3192
(u1_sel > USB3_LPM_MAX_U1_SEL_PEL ||
3193
u1_pel > USB3_LPM_MAX_U1_SEL_PEL)) ||
3194
(state == USB3_LPM_U2 &&
3195
(u2_sel > USB3_LPM_MAX_U2_SEL_PEL ||
3196
u2_pel > USB3_LPM_MAX_U2_SEL_PEL))) {
3197
dev_dbg(&udev->dev, "Device-initiated %s disabled due "
3198
"to long SEL %llu ms or PEL %llu ms\n",
3199
usb3_lpm_names[state], u1_sel, u1_pel);
3204
* If we're enabling device-initiated LPM for one link state,
3205
* but the other link state has a too high SEL or PEL value,
3206
* just set those values to the max in the Set SEL request.
3208
if (u1_sel > USB3_LPM_MAX_U1_SEL_PEL)
3209
u1_sel = USB3_LPM_MAX_U1_SEL_PEL;
3211
if (u1_pel > USB3_LPM_MAX_U1_SEL_PEL)
3212
u1_pel = USB3_LPM_MAX_U1_SEL_PEL;
3214
if (u2_sel > USB3_LPM_MAX_U2_SEL_PEL)
3215
u2_sel = USB3_LPM_MAX_U2_SEL_PEL;
3217
if (u2_pel > USB3_LPM_MAX_U2_SEL_PEL)
3218
u2_pel = USB3_LPM_MAX_U2_SEL_PEL;
3221
* usb_enable_lpm() can be called as part of a failed device reset,
3222
* which may be initiated by an error path of a mass storage driver.
3223
* Therefore, use GFP_NOIO.
3225
sel_values = kmalloc(sizeof *(sel_values), GFP_NOIO);
3229
sel_values->u1_sel = u1_sel;
3230
sel_values->u1_pel = u1_pel;
3231
sel_values->u2_sel = cpu_to_le16(u2_sel);
3232
sel_values->u2_pel = cpu_to_le16(u2_pel);
3234
ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
3238
sel_values, sizeof *(sel_values),
3239
USB_CTRL_SET_TIMEOUT);
3245
* Enable or disable device-initiated U1 or U2 transitions.
3247
static int usb_set_device_initiated_lpm(struct usb_device *udev,
3248
enum usb3_link_state state, bool enable)
3255
feature = USB_DEVICE_U1_ENABLE;
3258
feature = USB_DEVICE_U2_ENABLE;
3261
dev_warn(&udev->dev, "%s: Can't %s non-U1 or U2 state.\n",
3262
__func__, enable ? "enable" : "disable");
3266
if (udev->state != USB_STATE_CONFIGURED) {
3267
dev_dbg(&udev->dev, "%s: Can't %s %s state "
3268
"for unconfigured device.\n",
3269
__func__, enable ? "enable" : "disable",
3270
usb3_lpm_names[state]);
3276
* First, let the device know about the exit latencies
3277
* associated with the link state we're about to enable.
3279
ret = usb_req_set_sel(udev, state);
3281
dev_warn(&udev->dev, "Set SEL for device-initiated "
3282
"%s failed.\n", usb3_lpm_names[state]);
3286
* Now send the control transfer to enable device-initiated LPM
3287
* for either U1 or U2.
3289
ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
3290
USB_REQ_SET_FEATURE,
3294
USB_CTRL_SET_TIMEOUT);
3296
ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
3297
USB_REQ_CLEAR_FEATURE,
3301
USB_CTRL_SET_TIMEOUT);
3304
dev_warn(&udev->dev, "%s of device-initiated %s failed.\n",
3305
enable ? "Enable" : "Disable",
3306
usb3_lpm_names[state]);
3312
static int usb_set_lpm_timeout(struct usb_device *udev,
3313
enum usb3_link_state state, int timeout)
3320
feature = USB_PORT_FEAT_U1_TIMEOUT;
3323
feature = USB_PORT_FEAT_U2_TIMEOUT;
3326
dev_warn(&udev->dev, "%s: Can't set timeout for non-U1 or U2 state.\n",
3331
if (state == USB3_LPM_U1 && timeout > USB3_LPM_U1_MAX_TIMEOUT &&
3332
timeout != USB3_LPM_DEVICE_INITIATED) {
3333
dev_warn(&udev->dev, "Failed to set %s timeout to 0x%x, "
3334
"which is a reserved value.\n",
3335
usb3_lpm_names[state], timeout);
3339
ret = set_port_feature(udev->parent,
3340
USB_PORT_LPM_TIMEOUT(timeout) | udev->portnum,
3343
dev_warn(&udev->dev, "Failed to set %s timeout to 0x%x,"
3344
"error code %i\n", usb3_lpm_names[state],
3348
if (state == USB3_LPM_U1)
3349
udev->u1_params.timeout = timeout;
3351
udev->u2_params.timeout = timeout;
3356
* Enable the hub-initiated U1/U2 idle timeouts, and enable device-initiated
3359
* We will attempt to enable U1 or U2, but there are no guarantees that the
3360
* control transfers to set the hub timeout or enable device-initiated U1/U2
3361
* will be successful.
3363
* If we cannot set the parent hub U1/U2 timeout, we attempt to let the xHCI
3364
* driver know about it. If that call fails, it should be harmless, and just
3365
* take up more slightly more bus bandwidth for unnecessary U1/U2 exit latency.
3367
static void usb_enable_link_state(struct usb_hcd *hcd, struct usb_device *udev,
3368
enum usb3_link_state state)
3372
/* We allow the host controller to set the U1/U2 timeout internally
3373
* first, so that it can change its schedule to account for the
3374
* additional latency to send data to a device in a lower power
3377
timeout = hcd->driver->enable_usb3_lpm_timeout(hcd, udev, state);
3379
/* xHCI host controller doesn't want to enable this LPM state. */
3384
dev_warn(&udev->dev, "Could not enable %s link state, "
3385
"xHCI error %i.\n", usb3_lpm_names[state],
3390
if (usb_set_lpm_timeout(udev, state, timeout))
3391
/* If we can't set the parent hub U1/U2 timeout,
3392
* device-initiated LPM won't be allowed either, so let the xHCI
3393
* host know that this link state won't be enabled.
3395
hcd->driver->disable_usb3_lpm_timeout(hcd, udev, state);
3397
/* Only a configured device will accept the Set Feature U1/U2_ENABLE */
3398
else if (udev->actconfig)
3399
usb_set_device_initiated_lpm(udev, state, true);
3404
* Disable the hub-initiated U1/U2 idle timeouts, and disable device-initiated
3407
* If this function returns -EBUSY, the parent hub will still allow U1/U2 entry.
3408
* If zero is returned, the parent will not allow the link to go into U1/U2.
3410
* If zero is returned, device-initiated U1/U2 entry may still be enabled, but
3411
* it won't have an effect on the bus link state because the parent hub will
3412
* still disallow device-initiated U1/U2 entry.
3414
* If zero is returned, the xHCI host controller may still think U1/U2 entry is
3415
* possible. The result will be slightly more bus bandwidth will be taken up
3416
* (to account for U1/U2 exit latency), but it should be harmless.
3418
static int usb_disable_link_state(struct usb_hcd *hcd, struct usb_device *udev,
3419
enum usb3_link_state state)
3425
feature = USB_PORT_FEAT_U1_TIMEOUT;
3428
feature = USB_PORT_FEAT_U2_TIMEOUT;
3431
dev_warn(&udev->dev, "%s: Can't disable non-U1 or U2 state.\n",
3436
if (usb_set_lpm_timeout(udev, state, 0))
3439
usb_set_device_initiated_lpm(udev, state, false);
3441
if (hcd->driver->disable_usb3_lpm_timeout(hcd, udev, state))
3442
dev_warn(&udev->dev, "Could not disable xHCI %s timeout, "
3443
"bus schedule bandwidth may be impacted.\n",
3444
usb3_lpm_names[state]);
3449
* Disable hub-initiated and device-initiated U1 and U2 entry.
3450
* Caller must own the bandwidth_mutex.
3452
* This will call usb_enable_lpm() on failure, which will decrement
3453
* lpm_disable_count, and will re-enable LPM if lpm_disable_count reaches zero.
3455
int usb_disable_lpm(struct usb_device *udev)
3457
struct usb_hcd *hcd;
3459
if (!udev || !udev->parent ||
3460
udev->speed != USB_SPEED_SUPER ||
3464
hcd = bus_to_hcd(udev->bus);
3465
if (!hcd || !hcd->driver->disable_usb3_lpm_timeout)
3468
udev->lpm_disable_count++;
3469
if ((udev->u1_params.timeout == 0 && udev->u2_params.timeout == 0))
3472
/* If LPM is enabled, attempt to disable it. */
3473
if (usb_disable_link_state(hcd, udev, USB3_LPM_U1))
3475
if (usb_disable_link_state(hcd, udev, USB3_LPM_U2))
3481
usb_enable_lpm(udev);
3484
EXPORT_SYMBOL_GPL(usb_disable_lpm);
3486
/* Grab the bandwidth_mutex before calling usb_disable_lpm() */
3487
int usb_unlocked_disable_lpm(struct usb_device *udev)
3489
struct usb_hcd *hcd = bus_to_hcd(udev->bus);
3495
mutex_lock(hcd->bandwidth_mutex);
3496
ret = usb_disable_lpm(udev);
3497
mutex_unlock(hcd->bandwidth_mutex);
3501
EXPORT_SYMBOL_GPL(usb_unlocked_disable_lpm);
3504
* Attempt to enable device-initiated and hub-initiated U1 and U2 entry. The
3505
* xHCI host policy may prevent U1 or U2 from being enabled.
3507
* Other callers may have disabled link PM, so U1 and U2 entry will be disabled
3508
* until the lpm_disable_count drops to zero. Caller must own the
3511
void usb_enable_lpm(struct usb_device *udev)
3513
struct usb_hcd *hcd;
3515
if (!udev || !udev->parent ||
3516
udev->speed != USB_SPEED_SUPER ||
3520
udev->lpm_disable_count--;
3521
hcd = bus_to_hcd(udev->bus);
3522
/* Double check that we can both enable and disable LPM.
3523
* Device must be configured to accept set feature U1/U2 timeout.
3525
if (!hcd || !hcd->driver->enable_usb3_lpm_timeout ||
3526
!hcd->driver->disable_usb3_lpm_timeout)
3529
if (udev->lpm_disable_count > 0)
3532
usb_enable_link_state(hcd, udev, USB3_LPM_U1);
3533
usb_enable_link_state(hcd, udev, USB3_LPM_U2);
3535
EXPORT_SYMBOL_GPL(usb_enable_lpm);
3537
/* Grab the bandwidth_mutex before calling usb_enable_lpm() */
3538
void usb_unlocked_enable_lpm(struct usb_device *udev)
3540
struct usb_hcd *hcd = bus_to_hcd(udev->bus);
3545
mutex_lock(hcd->bandwidth_mutex);
3546
usb_enable_lpm(udev);
3547
mutex_unlock(hcd->bandwidth_mutex);
3549
EXPORT_SYMBOL_GPL(usb_unlocked_enable_lpm);
2853
3552
#else /* CONFIG_PM */
2855
3554
#define hub_suspend NULL
2856
3555
#define hub_resume NULL
2857
3556
#define hub_reset_resume NULL
3558
int usb_disable_lpm(struct usb_device *udev)
3562
EXPORT_SYMBOL_GPL(usb_disable_lpm);
3564
void usb_enable_lpm(struct usb_device *udev) { }
3565
EXPORT_SYMBOL_GPL(usb_enable_lpm);
3567
int usb_unlocked_disable_lpm(struct usb_device *udev)
3571
EXPORT_SYMBOL_GPL(usb_unlocked_disable_lpm);
3573
void usb_unlocked_enable_lpm(struct usb_device *udev) { }
3574
EXPORT_SYMBOL_GPL(usb_unlocked_enable_lpm);
3576
int usb_disable_ltm(struct usb_device *udev)
3580
EXPORT_SYMBOL_GPL(usb_disable_ltm);
3582
void usb_enable_ltm(struct usb_device *udev) { }
3583
EXPORT_SYMBOL_GPL(usb_enable_ltm);