~ubuntu-branches/ubuntu/saucy/linux-ti-omap4/saucy-proposed

« back to all changes in this revision

Viewing changes to drivers/usb/core/hub.c

  • Committer: Package Import Robot
  • Author(s): Paolo Pisati, Paolo Pisati, Stefan Bader, Upstream Kernel Changes
  • Date: 2012-08-15 17:17:43 UTC
  • Revision ID: package-import@ubuntu.com-20120815171743-h5wnuf51xe7pvdid
Tags: 3.5.0-207.13
[ Paolo Pisati ]

* Start new release

[ Stefan Bader ]

* (config) Enable getabis to use local package copies

[ Upstream Kernel Changes ]

* fixup: gargabe collect iva_seq[0|1] init
* [Config] enable all SND_OMAP_SOC_*s
* fixup: cm2xxx_3xxx.o is needed for omap2_cm_read|write_reg
* fixup: add some snd_soc_dai* helper functions
* fixup: s/snd_soc_dpcm_params/snd_soc_dpcm/g
* fixup: typo, no_host_mode and useless SDP4430 init
* fixup: enable again aess hwmod

Show diffs side-by-side

added added

removed removed

Lines of Context:
82
82
        u8                      indicator[USB_MAXCHILDREN];
83
83
        struct delayed_work     leds;
84
84
        struct delayed_work     init_work;
85
 
        void                    **port_owners;
 
85
        struct dev_state        **port_owners;
86
86
};
87
87
 
88
88
static inline int hub_is_superspeed(struct usb_device *hdev)
178
178
        return usb_get_intfdata(hdev->actconfig->interface[0]);
179
179
}
180
180
 
 
181
static int usb_device_supports_lpm(struct usb_device *udev)
 
182
{
 
183
        /* USB 2.1 (and greater) devices indicate LPM support through
 
184
         * their USB 2.0 Extended Capabilities BOS descriptor.
 
185
         */
 
186
        if (udev->speed == USB_SPEED_HIGH) {
 
187
                if (udev->bos->ext_cap &&
 
188
                        (USB_LPM_SUPPORT &
 
189
                         le32_to_cpu(udev->bos->ext_cap->bmAttributes)))
 
190
                        return 1;
 
191
                return 0;
 
192
        }
 
193
 
 
194
        /* All USB 3.0 must support LPM, but we need their max exit latency
 
195
         * information from the SuperSpeed Extended Capabilities BOS descriptor.
 
196
         */
 
197
        if (!udev->bos->ss_cap) {
 
198
                dev_warn(&udev->dev, "No LPM exit latency info found.  "
 
199
                                "Power management will be impacted.\n");
 
200
                return 0;
 
201
        }
 
202
        if (udev->parent->lpm_capable)
 
203
                return 1;
 
204
 
 
205
        dev_warn(&udev->dev, "Parent hub missing LPM exit latency info.  "
 
206
                        "Power management will be impacted.\n");
 
207
        return 0;
 
208
}
 
209
 
 
210
/*
 
211
 * Set the Maximum Exit Latency (MEL) for the host to initiate a transition from
 
212
 * either U1 or U2.
 
213
 */
 
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,
 
217
                struct usb_hub *hub,
 
218
                struct usb3_lpm_parameters *hub_lpm_params,
 
219
                unsigned int hub_exit_latency)
 
220
{
 
221
        unsigned int total_mel;
 
222
        unsigned int device_mel;
 
223
        unsigned int hub_mel;
 
224
 
 
225
        /*
 
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
 
229
         * bound for.
 
230
         *
 
231
         * The Hub Header decode latency is expressed in 0.1us intervals (0x1
 
232
         * means 0.1us).  Multiply that by 100 to get nanoseconds.
 
233
         */
 
234
        total_mel = hub_lpm_params->mel +
 
235
                (hub->descriptor->u.ss.bHubHdrDecLat * 100);
 
236
 
 
237
        /*
 
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
 
240
         * latency.
 
241
         *
 
242
         * The BOS U1/U2 exit latencies are expressed in 1us intervals.
 
243
         * Multiply that by 1000 to get nanoseconds.
 
244
         */
 
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;
 
249
        else
 
250
                total_mel += hub_mel;
 
251
 
 
252
        udev_lpm_params->mel = total_mel;
 
253
}
 
254
 
 
255
/*
 
256
 * Set the maximum Device to Host Exit Latency (PEL) for the device to initiate
 
257
 * a transition from either U1 or U2.
 
258
 */
 
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,
 
262
                struct usb_hub *hub,
 
263
                struct usb3_lpm_parameters *hub_lpm_params,
 
264
                unsigned int hub_exit_latency,
 
265
                unsigned int port_to_port_exit_latency)
 
266
{
 
267
        unsigned int first_link_pel;
 
268
        unsigned int hub_pel;
 
269
 
 
270
        /*
 
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.
 
274
         */
 
275
        if (udev_exit_latency > hub_exit_latency)
 
276
                first_link_pel = udev_exit_latency * 1000;
 
277
        else
 
278
                first_link_pel = hub_exit_latency * 1000;
 
279
 
 
280
        /*
 
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.
 
285
         */
 
286
        hub_pel = port_to_port_exit_latency * 1000 + hub_lpm_params->pel;
 
287
 
 
288
        /*
 
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.
 
291
         */
 
292
        if (first_link_pel > hub_pel)
 
293
                udev_lpm_params->pel = first_link_pel;
 
294
        else
 
295
                udev_lpm_params->pel = hub_pel;
 
296
}
 
297
 
 
298
/*
 
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.
 
302
 *
 
303
 * Section C.1.5.1 describes the four components to this:
 
304
 *  - t1: device PEL
 
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.
 
308
 *
 
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.
 
312
 */
 
313
static void usb_set_lpm_sel(struct usb_device *udev,
 
314
                struct usb3_lpm_parameters *udev_lpm_params)
 
315
{
 
316
        struct usb_device *parent;
 
317
        unsigned int num_hubs;
 
318
        unsigned int total_sel;
 
319
 
 
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)
 
325
                num_hubs++;
 
326
        /* t2 = 2.1us + 250ns * (num_hubs - 1) */
 
327
        if (num_hubs > 0)
 
328
                total_sel += 2100 + 250 * (num_hubs - 1);
 
329
 
 
330
        /* t4 = 250ns * num_hubs */
 
331
        total_sel += 250 * num_hubs;
 
332
 
 
333
        udev_lpm_params->sel = total_sel;
 
334
}
 
335
 
 
336
static void usb_set_lpm_parameters(struct usb_device *udev)
 
337
{
 
338
        struct usb_hub *hub;
 
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;
 
344
 
 
345
        if (!udev->lpm_capable || udev->speed != USB_SPEED_SUPER)
 
346
                return;
 
347
 
 
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.
 
351
         */
 
352
        if (!hub)
 
353
                return;
 
354
 
 
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;
 
359
 
 
360
        usb_set_lpm_mel(udev, &udev->u1_params, udev_u1_del,
 
361
                        hub, &udev->parent->u1_params, hub_u1_del);
 
362
 
 
363
        usb_set_lpm_mel(udev, &udev->u2_params, udev_u2_del,
 
364
                        hub, &udev->parent->u2_params, hub_u2_del);
 
365
 
 
366
        /*
 
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.
 
372
         *
 
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
 
378
         * exit latencies.
 
379
         *
 
380
         * What do we do if the U2 exit latency is less than the U1 exit
 
381
         * latency?  It's possible, although not likely...
 
382
         */
 
383
        port_to_port_delay = 1;
 
384
 
 
385
        usb_set_lpm_pel(udev, &udev->u1_params, udev_u1_del,
 
386
                        hub, &udev->parent->u1_params, hub_u1_del,
 
387
                        port_to_port_delay);
 
388
 
 
389
        if (hub_u2_del > hub_u1_del)
 
390
                port_to_port_delay = 1 + hub_u2_del - hub_u1_del;
 
391
        else
 
392
                port_to_port_delay = 1 + hub_u1_del;
 
393
 
 
394
        usb_set_lpm_pel(udev, &udev->u2_params, udev_u2_del,
 
395
                        hub, &udev->parent->u2_params, hub_u2_del,
 
396
                        port_to_port_delay);
 
397
 
 
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);
 
401
}
 
402
 
181
403
/* USB 2.0 spec Section 11.24.4.5 */
182
404
static int get_hub_descriptor(struct usb_device *hdev, void *data)
183
405
{
1050
1272
 
1051
1273
        hdev->children = kzalloc(hdev->maxchild *
1052
1274
                                sizeof(struct usb_device *), GFP_KERNEL);
1053
 
        hub->port_owners = kzalloc(hdev->maxchild * sizeof(void *), GFP_KERNEL);
 
1275
        hub->port_owners = kzalloc(hdev->maxchild * sizeof(struct dev_state *),
 
1276
                                GFP_KERNEL);
1054
1277
        if (!hdev->children || !hub->port_owners) {
1055
1278
                ret = -ENOMEM;
1056
1279
                goto fail;
1428
1651
 * to one of these "claimed" ports, the program will "own" the device.
1429
1652
 */
1430
1653
static int find_port_owner(struct usb_device *hdev, unsigned port1,
1431
 
                void ***ppowner)
 
1654
                struct dev_state ***ppowner)
1432
1655
{
1433
1656
        if (hdev->state == USB_STATE_NOTATTACHED)
1434
1657
                return -ENODEV;
1443
1666
}
1444
1667
 
1445
1668
/* In the following three functions, the caller must hold hdev's lock */
1446
 
int usb_hub_claim_port(struct usb_device *hdev, unsigned port1, void *owner)
 
1669
int usb_hub_claim_port(struct usb_device *hdev, unsigned port1,
 
1670
                       struct dev_state *owner)
1447
1671
{
1448
1672
        int rc;
1449
 
        void **powner;
 
1673
        struct dev_state **powner;
1450
1674
 
1451
1675
        rc = find_port_owner(hdev, port1, &powner);
1452
1676
        if (rc)
1457
1681
        return rc;
1458
1682
}
1459
1683
 
1460
 
int usb_hub_release_port(struct usb_device *hdev, unsigned port1, void *owner)
 
1684
int usb_hub_release_port(struct usb_device *hdev, unsigned port1,
 
1685
                         struct dev_state *owner)
1461
1686
{
1462
1687
        int rc;
1463
 
        void **powner;
 
1688
        struct dev_state **powner;
1464
1689
 
1465
1690
        rc = find_port_owner(hdev, port1, &powner);
1466
1691
        if (rc)
1471
1696
        return rc;
1472
1697
}
1473
1698
 
1474
 
void usb_hub_release_all_ports(struct usb_device *hdev, void *owner)
 
1699
void usb_hub_release_all_ports(struct usb_device *hdev, struct dev_state *owner)
1475
1700
{
1476
1701
        int n;
1477
 
        void **powner;
 
1702
        struct dev_state **powner;
1478
1703
 
1479
1704
        n = find_port_owner(hdev, 1, &powner);
1480
1705
        if (n == 0) {
2124
2349
static int hub_port_reset(struct usb_hub *hub, int port1,
2125
2350
                        struct usb_device *udev, unsigned int delay, bool warm);
2126
2351
 
2127
 
/* Is a USB 3.0 port in the Inactive state? */
2128
 
static bool hub_port_inactive(struct usb_hub *hub, u16 portstatus)
 
2352
/* Is a USB 3.0 port in the Inactive or Complinance Mode state?
 
2353
 * Port worm reset is required to recover
 
2354
 */
 
2355
static bool hub_port_warm_reset_required(struct usb_hub *hub, u16 portstatus)
2129
2356
{
2130
2357
        return hub_is_superspeed(hub->hdev) &&
2131
 
                (portstatus & USB_PORT_STAT_LINK_STATE) ==
2132
 
                USB_SS_PORT_LS_SS_INACTIVE;
 
2358
                (((portstatus & USB_PORT_STAT_LINK_STATE) ==
 
2359
                  USB_SS_PORT_LS_SS_INACTIVE) ||
 
2360
                 ((portstatus & USB_PORT_STAT_LINK_STATE) ==
 
2361
                  USB_SS_PORT_LS_COMP_MOD)) ;
2133
2362
}
2134
2363
 
2135
2364
static int hub_port_wait_reset(struct usb_hub *hub, int port1,
2165
2394
                         *
2166
2395
                         * See https://bugzilla.kernel.org/show_bug.cgi?id=41752
2167
2396
                         */
2168
 
                        if (hub_port_inactive(hub, portstatus)) {
 
2397
                        if (hub_port_warm_reset_required(hub, portstatus)) {
2169
2398
                                int ret;
2170
2399
 
2171
2400
                                if ((portchange & USB_PORT_STAT_C_CONNECTION))
2407
2636
        return status;
2408
2637
}
2409
2638
 
 
2639
static bool usb_device_supports_ltm(struct usb_device *udev)
 
2640
{
 
2641
        if (udev->speed != USB_SPEED_SUPER || !udev->bos || !udev->bos->ss_cap)
 
2642
                return false;
 
2643
        return udev->bos->ss_cap->bmAttributes & USB_LTM_SUPPORT;
 
2644
}
 
2645
 
 
2646
int usb_disable_ltm(struct usb_device *udev)
 
2647
{
 
2648
        struct usb_hcd *hcd = bus_to_hcd(udev->bus);
 
2649
 
 
2650
        /* Check if the roothub and device supports LTM. */
 
2651
        if (!usb_device_supports_ltm(hcd->self.root_hub) ||
 
2652
                        !usb_device_supports_ltm(udev))
 
2653
                return 0;
 
2654
 
 
2655
        /* Clear Feature LTM Enable can only be sent if the device is
 
2656
         * configured.
 
2657
         */
 
2658
        if (!udev->actconfig)
 
2659
                return 0;
 
2660
 
 
2661
        return usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
 
2662
                        USB_REQ_CLEAR_FEATURE, USB_RECIP_DEVICE,
 
2663
                        USB_DEVICE_LTM_ENABLE, 0, NULL, 0,
 
2664
                        USB_CTRL_SET_TIMEOUT);
 
2665
}
 
2666
EXPORT_SYMBOL_GPL(usb_disable_ltm);
 
2667
 
 
2668
void usb_enable_ltm(struct usb_device *udev)
 
2669
{
 
2670
        struct usb_hcd *hcd = bus_to_hcd(udev->bus);
 
2671
 
 
2672
        /* Check if the roothub and device supports LTM. */
 
2673
        if (!usb_device_supports_ltm(hcd->self.root_hub) ||
 
2674
                        !usb_device_supports_ltm(udev))
 
2675
                return;
 
2676
 
 
2677
        /* Set Feature LTM Enable can only be sent if the device is
 
2678
         * configured.
 
2679
         */
 
2680
        if (!udev->actconfig)
 
2681
                return;
 
2682
 
 
2683
        usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
 
2684
                        USB_REQ_SET_FEATURE, USB_RECIP_DEVICE,
 
2685
                        USB_DEVICE_LTM_ENABLE, 0, NULL, 0,
 
2686
                        USB_CTRL_SET_TIMEOUT);
 
2687
}
 
2688
EXPORT_SYMBOL_GPL(usb_enable_ltm);
 
2689
 
2410
2690
#ifdef  CONFIG_USB_SUSPEND
2411
2691
 
2412
2692
/*
2502
2782
        if (udev->usb2_hw_lpm_enabled == 1)
2503
2783
                usb_set_usb2_hardware_lpm(udev, 0);
2504
2784
 
 
2785
        if (usb_disable_ltm(udev)) {
 
2786
                dev_err(&udev->dev, "%s Failed to disable LTM before suspend\n.",
 
2787
                                __func__);
 
2788
                return -ENOMEM;
 
2789
        }
 
2790
        if (usb_unlocked_disable_lpm(udev)) {
 
2791
                dev_err(&udev->dev, "%s Failed to disable LPM before suspend\n.",
 
2792
                                __func__);
 
2793
                return -ENOMEM;
 
2794
        }
 
2795
 
2505
2796
        /* see 7.1.7.6 */
2506
2797
        if (hub_is_superspeed(hub->hdev))
2507
2798
                status = set_port_feature(hub->hdev,
2525
2816
                if (udev->usb2_hw_lpm_capable == 1)
2526
2817
                        usb_set_usb2_hardware_lpm(udev, 1);
2527
2818
 
 
2819
                /* Try to enable USB3 LTM and LPM again */
 
2820
                usb_enable_ltm(udev);
 
2821
                usb_unlocked_enable_lpm(udev);
 
2822
 
2528
2823
                /* System sleep transitions should never fail */
2529
2824
                if (!PMSG_IS_AUTO(msg))
2530
2825
                        status = 0;
2722
3017
                /* Try to enable USB2 hardware LPM */
2723
3018
                if (udev->usb2_hw_lpm_capable == 1)
2724
3019
                        usb_set_usb2_hardware_lpm(udev, 1);
 
3020
 
 
3021
                /* Try to enable USB3 LTM and LPM */
 
3022
                usb_enable_ltm(udev);
 
3023
                usb_unlocked_enable_lpm(udev);
2725
3024
        }
2726
3025
 
2727
3026
        return status;
2850
3149
}
2851
3150
EXPORT_SYMBOL_GPL(usb_root_hub_lost_power);
2852
3151
 
 
3152
static const char * const usb3_lpm_names[]  = {
 
3153
        "U0",
 
3154
        "U1",
 
3155
        "U2",
 
3156
        "U3",
 
3157
};
 
3158
 
 
3159
/*
 
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.
 
3164
 *
 
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.
 
3167
 */
 
3168
static int usb_req_set_sel(struct usb_device *udev, enum usb3_link_state state)
 
3169
{
 
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;
 
3175
        int ret;
 
3176
 
 
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);
 
3182
 
 
3183
        /*
 
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.
 
3190
         */
 
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);
 
3200
                return -EINVAL;
 
3201
        }
 
3202
 
 
3203
        /*
 
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.
 
3207
         */
 
3208
        if (u1_sel > USB3_LPM_MAX_U1_SEL_PEL)
 
3209
                u1_sel = USB3_LPM_MAX_U1_SEL_PEL;
 
3210
 
 
3211
        if (u1_pel > USB3_LPM_MAX_U1_SEL_PEL)
 
3212
                u1_pel = USB3_LPM_MAX_U1_SEL_PEL;
 
3213
 
 
3214
        if (u2_sel > USB3_LPM_MAX_U2_SEL_PEL)
 
3215
                u2_sel = USB3_LPM_MAX_U2_SEL_PEL;
 
3216
 
 
3217
        if (u2_pel > USB3_LPM_MAX_U2_SEL_PEL)
 
3218
                u2_pel = USB3_LPM_MAX_U2_SEL_PEL;
 
3219
 
 
3220
        /*
 
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.
 
3224
         */
 
3225
        sel_values = kmalloc(sizeof *(sel_values), GFP_NOIO);
 
3226
        if (!sel_values)
 
3227
                return -ENOMEM;
 
3228
 
 
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);
 
3233
 
 
3234
        ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
 
3235
                        USB_REQ_SET_SEL,
 
3236
                        USB_RECIP_DEVICE,
 
3237
                        0, 0,
 
3238
                        sel_values, sizeof *(sel_values),
 
3239
                        USB_CTRL_SET_TIMEOUT);
 
3240
        kfree(sel_values);
 
3241
        return ret;
 
3242
}
 
3243
 
 
3244
/*
 
3245
 * Enable or disable device-initiated U1 or U2 transitions.
 
3246
 */
 
3247
static int usb_set_device_initiated_lpm(struct usb_device *udev,
 
3248
                enum usb3_link_state state, bool enable)
 
3249
{
 
3250
        int ret;
 
3251
        int feature;
 
3252
 
 
3253
        switch (state) {
 
3254
        case USB3_LPM_U1:
 
3255
                feature = USB_DEVICE_U1_ENABLE;
 
3256
                break;
 
3257
        case USB3_LPM_U2:
 
3258
                feature = USB_DEVICE_U2_ENABLE;
 
3259
                break;
 
3260
        default:
 
3261
                dev_warn(&udev->dev, "%s: Can't %s non-U1 or U2 state.\n",
 
3262
                                __func__, enable ? "enable" : "disable");
 
3263
                return -EINVAL;
 
3264
        }
 
3265
 
 
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]);
 
3271
                return 0;
 
3272
        }
 
3273
 
 
3274
        if (enable) {
 
3275
                /*
 
3276
                 * First, let the device know about the exit latencies
 
3277
                 * associated with the link state we're about to enable.
 
3278
                 */
 
3279
                ret = usb_req_set_sel(udev, state);
 
3280
                if (ret < 0) {
 
3281
                        dev_warn(&udev->dev, "Set SEL for device-initiated "
 
3282
                                        "%s failed.\n", usb3_lpm_names[state]);
 
3283
                        return -EBUSY;
 
3284
                }
 
3285
                /*
 
3286
                 * Now send the control transfer to enable device-initiated LPM
 
3287
                 * for either U1 or U2.
 
3288
                 */
 
3289
                ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
 
3290
                                USB_REQ_SET_FEATURE,
 
3291
                                USB_RECIP_DEVICE,
 
3292
                                feature,
 
3293
                                0, NULL, 0,
 
3294
                                USB_CTRL_SET_TIMEOUT);
 
3295
        } else {
 
3296
                ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
 
3297
                                USB_REQ_CLEAR_FEATURE,
 
3298
                                USB_RECIP_DEVICE,
 
3299
                                feature,
 
3300
                                0, NULL, 0,
 
3301
                                USB_CTRL_SET_TIMEOUT);
 
3302
        }
 
3303
        if (ret < 0) {
 
3304
                dev_warn(&udev->dev, "%s of device-initiated %s failed.\n",
 
3305
                                enable ? "Enable" : "Disable",
 
3306
                                usb3_lpm_names[state]);
 
3307
                return -EBUSY;
 
3308
        }
 
3309
        return 0;
 
3310
}
 
3311
 
 
3312
static int usb_set_lpm_timeout(struct usb_device *udev,
 
3313
                enum usb3_link_state state, int timeout)
 
3314
{
 
3315
        int ret;
 
3316
        int feature;
 
3317
 
 
3318
        switch (state) {
 
3319
        case USB3_LPM_U1:
 
3320
                feature = USB_PORT_FEAT_U1_TIMEOUT;
 
3321
                break;
 
3322
        case USB3_LPM_U2:
 
3323
                feature = USB_PORT_FEAT_U2_TIMEOUT;
 
3324
                break;
 
3325
        default:
 
3326
                dev_warn(&udev->dev, "%s: Can't set timeout for non-U1 or U2 state.\n",
 
3327
                                __func__);
 
3328
                return -EINVAL;
 
3329
        }
 
3330
 
 
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);
 
3336
                return -EINVAL;
 
3337
        }
 
3338
 
 
3339
        ret = set_port_feature(udev->parent,
 
3340
                        USB_PORT_LPM_TIMEOUT(timeout) | udev->portnum,
 
3341
                        feature);
 
3342
        if (ret < 0) {
 
3343
                dev_warn(&udev->dev, "Failed to set %s timeout to 0x%x,"
 
3344
                                "error code %i\n", usb3_lpm_names[state],
 
3345
                                timeout, ret);
 
3346
                return -EBUSY;
 
3347
        }
 
3348
        if (state == USB3_LPM_U1)
 
3349
                udev->u1_params.timeout = timeout;
 
3350
        else
 
3351
                udev->u2_params.timeout = timeout;
 
3352
        return 0;
 
3353
}
 
3354
 
 
3355
/*
 
3356
 * Enable the hub-initiated U1/U2 idle timeouts, and enable device-initiated
 
3357
 * U1/U2 entry.
 
3358
 *
 
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.
 
3362
 *
 
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.
 
3366
 */
 
3367
static void usb_enable_link_state(struct usb_hcd *hcd, struct usb_device *udev,
 
3368
                enum usb3_link_state state)
 
3369
{
 
3370
        int timeout;
 
3371
 
 
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
 
3375
         * link state.
 
3376
         */
 
3377
        timeout = hcd->driver->enable_usb3_lpm_timeout(hcd, udev, state);
 
3378
 
 
3379
        /* xHCI host controller doesn't want to enable this LPM state. */
 
3380
        if (timeout == 0)
 
3381
                return;
 
3382
 
 
3383
        if (timeout < 0) {
 
3384
                dev_warn(&udev->dev, "Could not enable %s link state, "
 
3385
                                "xHCI error %i.\n", usb3_lpm_names[state],
 
3386
                                timeout);
 
3387
                return;
 
3388
        }
 
3389
 
 
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.
 
3394
                 */
 
3395
                hcd->driver->disable_usb3_lpm_timeout(hcd, udev, state);
 
3396
 
 
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);
 
3400
 
 
3401
}
 
3402
 
 
3403
/*
 
3404
 * Disable the hub-initiated U1/U2 idle timeouts, and disable device-initiated
 
3405
 * U1/U2 entry.
 
3406
 *
 
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.
 
3409
 *
 
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.
 
3413
 *
 
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.
 
3417
 */
 
3418
static int usb_disable_link_state(struct usb_hcd *hcd, struct usb_device *udev,
 
3419
                enum usb3_link_state state)
 
3420
{
 
3421
        int feature;
 
3422
 
 
3423
        switch (state) {
 
3424
        case USB3_LPM_U1:
 
3425
                feature = USB_PORT_FEAT_U1_TIMEOUT;
 
3426
                break;
 
3427
        case USB3_LPM_U2:
 
3428
                feature = USB_PORT_FEAT_U2_TIMEOUT;
 
3429
                break;
 
3430
        default:
 
3431
                dev_warn(&udev->dev, "%s: Can't disable non-U1 or U2 state.\n",
 
3432
                                __func__);
 
3433
                return -EINVAL;
 
3434
        }
 
3435
 
 
3436
        if (usb_set_lpm_timeout(udev, state, 0))
 
3437
                return -EBUSY;
 
3438
 
 
3439
        usb_set_device_initiated_lpm(udev, state, false);
 
3440
 
 
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]);
 
3445
        return 0;
 
3446
}
 
3447
 
 
3448
/*
 
3449
 * Disable hub-initiated and device-initiated U1 and U2 entry.
 
3450
 * Caller must own the bandwidth_mutex.
 
3451
 *
 
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.
 
3454
 */
 
3455
int usb_disable_lpm(struct usb_device *udev)
 
3456
{
 
3457
        struct usb_hcd *hcd;
 
3458
 
 
3459
        if (!udev || !udev->parent ||
 
3460
                        udev->speed != USB_SPEED_SUPER ||
 
3461
                        !udev->lpm_capable)
 
3462
                return 0;
 
3463
 
 
3464
        hcd = bus_to_hcd(udev->bus);
 
3465
        if (!hcd || !hcd->driver->disable_usb3_lpm_timeout)
 
3466
                return 0;
 
3467
 
 
3468
        udev->lpm_disable_count++;
 
3469
        if ((udev->u1_params.timeout == 0 && udev->u2_params.timeout == 0))
 
3470
                return 0;
 
3471
 
 
3472
        /* If LPM is enabled, attempt to disable it. */
 
3473
        if (usb_disable_link_state(hcd, udev, USB3_LPM_U1))
 
3474
                goto enable_lpm;
 
3475
        if (usb_disable_link_state(hcd, udev, USB3_LPM_U2))
 
3476
                goto enable_lpm;
 
3477
 
 
3478
        return 0;
 
3479
 
 
3480
enable_lpm:
 
3481
        usb_enable_lpm(udev);
 
3482
        return -EBUSY;
 
3483
}
 
3484
EXPORT_SYMBOL_GPL(usb_disable_lpm);
 
3485
 
 
3486
/* Grab the bandwidth_mutex before calling usb_disable_lpm() */
 
3487
int usb_unlocked_disable_lpm(struct usb_device *udev)
 
3488
{
 
3489
        struct usb_hcd *hcd = bus_to_hcd(udev->bus);
 
3490
        int ret;
 
3491
 
 
3492
        if (!hcd)
 
3493
                return -EINVAL;
 
3494
 
 
3495
        mutex_lock(hcd->bandwidth_mutex);
 
3496
        ret = usb_disable_lpm(udev);
 
3497
        mutex_unlock(hcd->bandwidth_mutex);
 
3498
 
 
3499
        return ret;
 
3500
}
 
3501
EXPORT_SYMBOL_GPL(usb_unlocked_disable_lpm);
 
3502
 
 
3503
/*
 
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.
 
3506
 *
 
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
 
3509
 * bandwidth_mutex.
 
3510
 */
 
3511
void usb_enable_lpm(struct usb_device *udev)
 
3512
{
 
3513
        struct usb_hcd *hcd;
 
3514
 
 
3515
        if (!udev || !udev->parent ||
 
3516
                        udev->speed != USB_SPEED_SUPER ||
 
3517
                        !udev->lpm_capable)
 
3518
                return;
 
3519
 
 
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.
 
3524
         */
 
3525
        if (!hcd || !hcd->driver->enable_usb3_lpm_timeout ||
 
3526
                        !hcd->driver->disable_usb3_lpm_timeout)
 
3527
                return;
 
3528
 
 
3529
        if (udev->lpm_disable_count > 0)
 
3530
                return;
 
3531
 
 
3532
        usb_enable_link_state(hcd, udev, USB3_LPM_U1);
 
3533
        usb_enable_link_state(hcd, udev, USB3_LPM_U2);
 
3534
}
 
3535
EXPORT_SYMBOL_GPL(usb_enable_lpm);
 
3536
 
 
3537
/* Grab the bandwidth_mutex before calling usb_enable_lpm() */
 
3538
void usb_unlocked_enable_lpm(struct usb_device *udev)
 
3539
{
 
3540
        struct usb_hcd *hcd = bus_to_hcd(udev->bus);
 
3541
 
 
3542
        if (!hcd)
 
3543
                return;
 
3544
 
 
3545
        mutex_lock(hcd->bandwidth_mutex);
 
3546
        usb_enable_lpm(udev);
 
3547
        mutex_unlock(hcd->bandwidth_mutex);
 
3548
}
 
3549
EXPORT_SYMBOL_GPL(usb_unlocked_enable_lpm);
 
3550
 
 
3551
 
2853
3552
#else   /* CONFIG_PM */
2854
3553
 
2855
3554
#define hub_suspend             NULL
2856
3555
#define hub_resume              NULL
2857
3556
#define hub_reset_resume        NULL
 
3557
 
 
3558
int usb_disable_lpm(struct usb_device *udev)
 
3559
{
 
3560
        return 0;
 
3561
}
 
3562
EXPORT_SYMBOL_GPL(usb_disable_lpm);
 
3563
 
 
3564
void usb_enable_lpm(struct usb_device *udev) { }
 
3565
EXPORT_SYMBOL_GPL(usb_enable_lpm);
 
3566
 
 
3567
int usb_unlocked_disable_lpm(struct usb_device *udev)
 
3568
{
 
3569
        return 0;
 
3570
}
 
3571
EXPORT_SYMBOL_GPL(usb_unlocked_disable_lpm);
 
3572
 
 
3573
void usb_unlocked_enable_lpm(struct usb_device *udev) { }
 
3574
EXPORT_SYMBOL_GPL(usb_unlocked_enable_lpm);
 
3575
 
 
3576
int usb_disable_ltm(struct usb_device *udev)
 
3577
{
 
3578
        return 0;
 
3579
}
 
3580
EXPORT_SYMBOL_GPL(usb_disable_ltm);
 
3581
 
 
3582
void usb_enable_ltm(struct usb_device *udev) { }
 
3583
EXPORT_SYMBOL_GPL(usb_enable_ltm);
2858
3584
#endif
2859
3585
 
2860
3586
 
3234
3960
        if (udev->wusb == 0 && le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0201) {
3235
3961
                retval = usb_get_bos_descriptor(udev);
3236
3962
                if (!retval) {
3237
 
                        if (udev->bos->ext_cap && (USB_LPM_SUPPORT &
3238
 
                                le32_to_cpu(udev->bos->ext_cap->bmAttributes)))
3239
 
                                        udev->lpm_capable = 1;
 
3963
                        udev->lpm_capable = usb_device_supports_lpm(udev);
 
3964
                        usb_set_lpm_parameters(udev);
3240
3965
                }
3241
3966
        }
3242
3967
 
3779
4504
                        /* Warm reset a USB3 protocol port if it's in
3780
4505
                         * SS.Inactive state.
3781
4506
                         */
3782
 
                        if (hub_is_superspeed(hub->hdev) &&
3783
 
                                (portstatus & USB_PORT_STAT_LINK_STATE)
3784
 
                                        == USB_SS_PORT_LS_SS_INACTIVE) {
 
4507
                        if (hub_port_warm_reset_required(hub, portstatus)) {
3785
4508
                                dev_dbg(hub_dev, "warm reset port %d\n", i);
3786
4509
                                hub_port_reset(hub, i, NULL,
3787
4510
                                                HUB_BH_RESET_TIME, true);
4041
4764
        }
4042
4765
        parent_hub = hdev_to_hub(parent_hdev);
4043
4766
 
 
4767
        /* Disable LPM and LTM while we reset the device and reinstall the alt
 
4768
         * settings.  Device-initiated LPM settings, and system exit latency
 
4769
         * settings are cleared when the device is reset, so we have to set
 
4770
         * them up again.
 
4771
         */
 
4772
        ret = usb_unlocked_disable_lpm(udev);
 
4773
        if (ret) {
 
4774
                dev_err(&udev->dev, "%s Failed to disable LPM\n.", __func__);
 
4775
                goto re_enumerate;
 
4776
        }
 
4777
        ret = usb_disable_ltm(udev);
 
4778
        if (ret) {
 
4779
                dev_err(&udev->dev, "%s Failed to disable LTM\n.",
 
4780
                                __func__);
 
4781
                goto re_enumerate;
 
4782
        }
 
4783
 
4044
4784
        set_bit(port1, parent_hub->busy_bits);
4045
4785
        for (i = 0; i < SET_CONFIG_TRIES; ++i) {
4046
4786
 
4127
4867
        }
4128
4868
 
4129
4869
done:
 
4870
        /* Now that the alt settings are re-installed, enable LTM and LPM. */
 
4871
        usb_unlocked_enable_lpm(udev);
 
4872
        usb_enable_ltm(udev);
4130
4873
        return 0;
4131
4874
 
4132
4875
re_enumerate:
 
4876
        /* LPM state doesn't matter when we're about to destroy the device. */
4133
4877
        hub_port_logical_disconnect(parent_hub, port1);
4134
4878
        return -ENODEV;
4135
4879
}