950-0152-xhci-implement-xhci_fixup_endpoint-for-interval-adju.patch 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. From e9263b01e122059a6a8ba1aeed8ed7a56cf80028 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 | 104 ++++++++++++++++++++++++++++++++++++++++
  12. 1 file changed, 104 insertions(+)
  13. --- a/drivers/usb/host/xhci.c
  14. +++ b/drivers/usb/host/xhci.c
  15. @@ -1622,6 +1622,109 @@ 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. +
  25. +static unsigned int xhci_get_endpoint_flag_from_index(unsigned int ep_index)
  26. +{
  27. + return 1 << (ep_index + 1);
  28. +}
  29. +
  30. +static void xhci_fixup_endpoint(struct usb_hcd *hcd, struct usb_device *udev,
  31. + struct usb_host_endpoint *ep, int interval)
  32. +{
  33. + struct xhci_hcd *xhci;
  34. + struct xhci_ep_ctx *ep_ctx_out, *ep_ctx_in;
  35. + struct xhci_command *command;
  36. + struct xhci_input_control_ctx *ctrl_ctx;
  37. + struct xhci_virt_device *vdev;
  38. + int xhci_interval;
  39. + int ret;
  40. + int ep_index;
  41. + unsigned long flags;
  42. + u32 ep_info_tmp;
  43. +
  44. + xhci = hcd_to_xhci(hcd);
  45. + ep_index = xhci_get_endpoint_index(&ep->desc);
  46. +
  47. + /* FS/LS interval translations */
  48. + if ((udev->speed == USB_SPEED_FULL ||
  49. + udev->speed == USB_SPEED_LOW))
  50. + interval *= 8;
  51. +
  52. + mutex_lock(&xhci->mutex);
  53. +
  54. + spin_lock_irqsave(&xhci->lock, flags);
  55. +
  56. + vdev = xhci->devs[udev->slot_id];
  57. + /* Get context-derived endpoint interval */
  58. + ep_ctx_out = xhci_get_ep_ctx(xhci, vdev->out_ctx, ep_index);
  59. + ep_ctx_in = xhci_get_ep_ctx(xhci, vdev->in_ctx, ep_index);
  60. + xhci_interval = EP_INTERVAL_TO_UFRAMES(le32_to_cpu(ep_ctx_out->ep_info));
  61. +
  62. + if (interval == xhci_interval) {
  63. + spin_unlock_irqrestore(&xhci->lock, flags);
  64. + mutex_unlock(&xhci->mutex);
  65. + return;
  66. + }
  67. +
  68. + xhci_dbg(xhci, "Fixup interval=%d xhci_interval=%d\n",
  69. + interval, xhci_interval);
  70. + command = xhci_alloc_command_with_ctx(xhci, true, GFP_ATOMIC);
  71. + if (!command) {
  72. + /* Failure here is benign, poll at the original rate */
  73. + spin_unlock_irqrestore(&xhci->lock, flags);
  74. + mutex_unlock(&xhci->mutex);
  75. + return;
  76. + }
  77. +
  78. + /* xHCI uses exponents for intervals... */
  79. + xhci_interval = fls(interval) - 1;
  80. + xhci_interval = clamp_val(xhci_interval, 3, 10);
  81. + ep_info_tmp = le32_to_cpu(ep_ctx_out->ep_info);
  82. + ep_info_tmp &= ~EP_INTERVAL(255);
  83. + ep_info_tmp |= EP_INTERVAL(xhci_interval);
  84. +
  85. + /* Keep the endpoint context up-to-date while issuing the command. */
  86. + xhci_endpoint_copy(xhci, vdev->in_ctx,
  87. + vdev->out_ctx, ep_index);
  88. + ep_ctx_in->ep_info = cpu_to_le32(ep_info_tmp);
  89. +
  90. + /*
  91. + * We need to drop the lock, so take an explicit copy
  92. + * of the ep context.
  93. + */
  94. + xhci_endpoint_copy(xhci, command->in_ctx, vdev->in_ctx, ep_index);
  95. +
  96. + ctrl_ctx = xhci_get_input_control_ctx(command->in_ctx);
  97. + if (!ctrl_ctx) {
  98. + xhci_warn(xhci,
  99. + "%s: Could not get input context, bad type.\n",
  100. + __func__);
  101. + spin_unlock_irqrestore(&xhci->lock, flags);
  102. + xhci_free_command(xhci, command);
  103. + mutex_unlock(&xhci->mutex);
  104. + return;
  105. + }
  106. + ctrl_ctx->add_flags = xhci_get_endpoint_flag_from_index(ep_index);
  107. + ctrl_ctx->drop_flags = 0;
  108. +
  109. + spin_unlock_irqrestore(&xhci->lock, flags);
  110. +
  111. + ret = xhci_configure_endpoint(xhci, udev, command,
  112. + false, false);
  113. + if (ret)
  114. + xhci_warn(xhci, "%s: Configure endpoint failed: %d\n",
  115. + __func__, ret);
  116. + xhci_free_command(xhci, command);
  117. + mutex_unlock(&xhci->mutex);
  118. +}
  119. +
  120. +/*
  121. * non-error returns are a promise to giveback() the urb later
  122. * we drop ownership so next owner (or urb unlink) can get it
  123. */
  124. @@ -5461,6 +5564,7 @@ static const struct hc_driver xhci_hc_dr
  125. .endpoint_reset = xhci_endpoint_reset,
  126. .check_bandwidth = xhci_check_bandwidth,
  127. .reset_bandwidth = xhci_reset_bandwidth,
  128. + .fixup_endpoint = xhci_fixup_endpoint,
  129. .address_device = xhci_address_device,
  130. .enable_device = xhci_enable_device,
  131. .update_hub_device = xhci_update_hub_device,