1 From e9263b01e122059a6a8ba1aeed8ed7a56cf80028 Mon Sep 17 00:00:00 2001
2 From: Jonathan Bell <jonathan@raspberrypi.org>
3 Date: Tue, 11 Jun 2019 11:33:39 +0100
4 Subject: [PATCH] xhci: implement xhci_fixup_endpoint for interval
7 Must be called in a non-atomic context, after the endpoint
8 has been registered with the hardware via xhci_add_endpoint
9 and before the first URB is submitted for the endpoint.
11 Signed-off-by: Jonathan Bell <jonathan@raspberrypi.org>
13 drivers/usb/host/xhci.c | 104 ++++++++++++++++++++++++++++++++++++++++
14 1 file changed, 104 insertions(+)
16 --- a/drivers/usb/host/xhci.c
17 +++ b/drivers/usb/host/xhci.c
18 @@ -1618,6 +1618,109 @@ command_cleanup:
22 + * RPI: Fixup endpoint intervals when requested
23 + * - Check interval versus the (cached) endpoint context
24 + * - set the endpoint interval to the new value
25 + * - force an endpoint configure command
26 + * XXX: bandwidth is not recalculated. We should probably do that.
29 +static unsigned int xhci_get_endpoint_flag_from_index(unsigned int ep_index)
31 + return 1 << (ep_index + 1);
34 +static void xhci_fixup_endpoint(struct usb_hcd *hcd, struct usb_device *udev,
35 + struct usb_host_endpoint *ep, int interval)
37 + struct xhci_hcd *xhci;
38 + struct xhci_ep_ctx *ep_ctx_out, *ep_ctx_in;
39 + struct xhci_command *command;
40 + struct xhci_input_control_ctx *ctrl_ctx;
41 + struct xhci_virt_device *vdev;
45 + unsigned long flags;
48 + xhci = hcd_to_xhci(hcd);
49 + ep_index = xhci_get_endpoint_index(&ep->desc);
51 + /* FS/LS interval translations */
52 + if ((udev->speed == USB_SPEED_FULL ||
53 + udev->speed == USB_SPEED_LOW))
56 + mutex_lock(&xhci->mutex);
58 + spin_lock_irqsave(&xhci->lock, flags);
60 + vdev = xhci->devs[udev->slot_id];
61 + /* Get context-derived endpoint interval */
62 + ep_ctx_out = xhci_get_ep_ctx(xhci, vdev->out_ctx, ep_index);
63 + ep_ctx_in = xhci_get_ep_ctx(xhci, vdev->in_ctx, ep_index);
64 + xhci_interval = EP_INTERVAL_TO_UFRAMES(le32_to_cpu(ep_ctx_out->ep_info));
66 + if (interval == xhci_interval) {
67 + spin_unlock_irqrestore(&xhci->lock, flags);
68 + mutex_unlock(&xhci->mutex);
72 + xhci_dbg(xhci, "Fixup interval=%d xhci_interval=%d\n",
73 + interval, xhci_interval);
74 + command = xhci_alloc_command_with_ctx(xhci, true, GFP_ATOMIC);
76 + /* Failure here is benign, poll at the original rate */
77 + spin_unlock_irqrestore(&xhci->lock, flags);
78 + mutex_unlock(&xhci->mutex);
82 + /* xHCI uses exponents for intervals... */
83 + xhci_interval = fls(interval) - 1;
84 + xhci_interval = clamp_val(xhci_interval, 3, 10);
85 + ep_info_tmp = le32_to_cpu(ep_ctx_out->ep_info);
86 + ep_info_tmp &= ~EP_INTERVAL(255);
87 + ep_info_tmp |= EP_INTERVAL(xhci_interval);
89 + /* Keep the endpoint context up-to-date while issuing the command. */
90 + xhci_endpoint_copy(xhci, vdev->in_ctx,
91 + vdev->out_ctx, ep_index);
92 + ep_ctx_in->ep_info = cpu_to_le32(ep_info_tmp);
95 + * We need to drop the lock, so take an explicit copy
96 + * of the ep context.
98 + xhci_endpoint_copy(xhci, command->in_ctx, vdev->in_ctx, ep_index);
100 + ctrl_ctx = xhci_get_input_control_ctx(command->in_ctx);
103 + "%s: Could not get input context, bad type.\n",
105 + spin_unlock_irqrestore(&xhci->lock, flags);
106 + xhci_free_command(xhci, command);
107 + mutex_unlock(&xhci->mutex);
110 + ctrl_ctx->add_flags = xhci_get_endpoint_flag_from_index(ep_index);
111 + ctrl_ctx->drop_flags = 0;
113 + spin_unlock_irqrestore(&xhci->lock, flags);
115 + ret = xhci_configure_endpoint(xhci, udev, command,
118 + xhci_warn(xhci, "%s: Configure endpoint failed: %d\n",
120 + xhci_free_command(xhci, command);
121 + mutex_unlock(&xhci->mutex);
125 * non-error returns are a promise to giveback() the urb later
126 * we drop ownership so next owner (or urb unlink) can get it
128 @@ -5457,6 +5560,7 @@ static const struct hc_driver xhci_hc_dr
129 .endpoint_reset = xhci_endpoint_reset,
130 .check_bandwidth = xhci_check_bandwidth,
131 .reset_bandwidth = xhci_reset_bandwidth,
132 + .fixup_endpoint = xhci_fixup_endpoint,
133 .address_device = xhci_address_device,
134 .enable_device = xhci_enable_device,
135 .update_hub_device = xhci_update_hub_device,