950-0139-xhci-implement-xhci_fixup_endpoint-for-interval-adju.patch 4.1 KB

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