asahi.lists.linux.dev archive mirror
 help / color / mirror / Atom feed
From: Janne Grunau via B4 Relay <devnull+j.jannau.net@kernel.org>
To: Bin Meng <bmeng.cn@gmail.com>, Marek Vasut <marex@denx.de>,
	 Tom Rini <trini@konsulko.com>, Simon Glass <sjg@chromium.org>,
	 Joe Hershberger <joe.hershberger@ni.com>
Cc: u-boot@lists.denx.de, asahi@lists.linux.dev,
	 Janne Grunau <j@jannau.net>
Subject: [PATCH v2 2/6] usb: xhci: Set up endpoints for the first 2 interfaces
Date: Sun, 17 Mar 2024 12:07:04 +0100	[thread overview]
Message-ID: <20240317-asahi-keyboards-v2-2-d3f4b8384f68@jannau.net> (raw)
In-Reply-To: <20240317-asahi-keyboards-v2-0-d3f4b8384f68@jannau.net>

From: Janne Grunau <j@jannau.net>

The xhci driver currently only does the necessary initialization for
endpoints found in the first interface descriptor. Apple USB keyboards
(released 2021) use the second interface descriptor for the HID keyboard
boot protocol. To allow USB drivers to use endpoints from other
interface descriptors the xhci driver needs to ensure these endpoints
are initialized as well.
Use USB_MAX_ACTIVE_INTERFACES to control how many interface descriptors
are considered during endpoint initialisation.
For now define it to 2 as that is sufficient for supporting the Apple
keyboards.

Reviewed-by: Marek Vasut <marex@denx.de>
Signed-off-by: Janne Grunau <j@jannau.net>
---
 drivers/usb/host/xhci.c | 31 +++++++++++++++++++------------
 include/usb.h           |  6 ++++++
 2 files changed, 25 insertions(+), 12 deletions(-)

diff --git a/drivers/usb/host/xhci.c b/drivers/usb/host/xhci.c
index 534c4b973f..741e186ee0 100644
--- a/drivers/usb/host/xhci.c
+++ b/drivers/usb/host/xhci.c
@@ -606,24 +606,28 @@ static int xhci_set_configuration(struct usb_device *udev)
 	int slot_id = udev->slot_id;
 	struct xhci_virt_device *virt_dev = ctrl->devs[slot_id];
 	struct usb_interface *ifdesc;
+	unsigned int ifnum;
+	unsigned int max_ifnum = min((unsigned int)USB_MAX_ACTIVE_INTERFACES,
+				     (unsigned int)udev->config.no_of_if);
 
 	out_ctx = virt_dev->out_ctx;
 	in_ctx = virt_dev->in_ctx;
 
-	num_of_ep = udev->config.if_desc[0].no_of_ep;
-	ifdesc = &udev->config.if_desc[0];
-
 	ctrl_ctx = xhci_get_input_control_ctx(in_ctx);
 	/* Initialize the input context control */
 	ctrl_ctx->add_flags = cpu_to_le32(SLOT_FLAG);
 	ctrl_ctx->drop_flags = 0;
 
-	/* EP_FLAG gives values 1 & 4 for EP1OUT and EP2IN */
-	for (cur_ep = 0; cur_ep < num_of_ep; cur_ep++) {
-		ep_flag = xhci_get_ep_index(&ifdesc->ep_desc[cur_ep]);
-		ctrl_ctx->add_flags |= cpu_to_le32(1 << (ep_flag + 1));
-		if (max_ep_flag < ep_flag)
-			max_ep_flag = ep_flag;
+	for (ifnum = 0; ifnum < max_ifnum; ifnum++) {
+		ifdesc = &udev->config.if_desc[ifnum];
+		num_of_ep = ifdesc->no_of_ep;
+		/* EP_FLAG gives values 1 & 4 for EP1OUT and EP2IN */
+		for (cur_ep = 0; cur_ep < num_of_ep; cur_ep++) {
+			ep_flag = xhci_get_ep_index(&ifdesc->ep_desc[cur_ep]);
+			ctrl_ctx->add_flags |= cpu_to_le32(1 << (ep_flag + 1));
+			if (max_ep_flag < ep_flag)
+				max_ep_flag = ep_flag;
+		}
 	}
 
 	xhci_inval_cache((uintptr_t)out_ctx->bytes, out_ctx->size);
@@ -637,9 +641,12 @@ static int xhci_set_configuration(struct usb_device *udev)
 	xhci_endpoint_copy(ctrl, in_ctx, out_ctx, 0);
 
 	/* filling up ep contexts */
-	err = xhci_init_ep_contexts_if(udev, ctrl, virt_dev, ifdesc);
-	if (err < 0)
-		return err;
+	for (ifnum = 0; ifnum < max_ifnum; ifnum++) {
+		ifdesc = &udev->config.if_desc[ifnum];
+		err = xhci_init_ep_contexts_if(udev, ctrl, virt_dev, ifdesc);
+		if (err < 0)
+			return err;
+	}
 
 	return xhci_configure_endpoints(udev, false);
 }
diff --git a/include/usb.h b/include/usb.h
index 09e3f0cb30..3aafdc8bfd 100644
--- a/include/usb.h
+++ b/include/usb.h
@@ -49,6 +49,12 @@ extern bool usb_started; /* flag for the started/stopped USB status */
  */
 #define USB_TIMEOUT_MS(pipe) (usb_pipebulk(pipe) ? 5000 : 1000)
 
+/*
+ * The xhcd hcd driver prepares only a limited number interfaces / endpoints.
+ * Define this limit so that drivers do not exceed it.
+ */
+#define USB_MAX_ACTIVE_INTERFACES	2
+
 /* device request (setup) */
 struct devrequest {
 	__u8	requesttype;

-- 
2.44.0


  parent reply	other threads:[~2024-03-17 11:07 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-03-17 11:07 [PATCH v2 0/6] USB keyboard improvements for asahi / desktop systems Janne Grunau via B4 Relay
2024-03-17 11:07 ` [PATCH v2 1/6] usb: xhci: refactor xhci_set_configuration Janne Grunau via B4 Relay
2024-03-17 11:07 ` Janne Grunau via B4 Relay [this message]
2024-03-17 11:07 ` [PATCH v2 3/6] usb: xhci: Abort transfers with unallocated rings Janne Grunau via B4 Relay
2024-03-17 16:06   ` Marek Vasut
2024-03-17 11:07 ` [PATCH v2 4/6] usb: Add environment based device blocklist Janne Grunau via B4 Relay
2024-03-17 11:34   ` Janne Grunau
2024-03-17 16:07     ` Marek Vasut
2024-03-17 16:18   ` Marek Vasut
2024-03-17 18:15     ` Janne Grunau
2024-03-18  5:06       ` Marek Vasut
2024-03-18  7:33         ` Janne Grunau
2024-03-18 14:17           ` Marek Vasut
2024-03-19 21:17             ` Janne Grunau
2024-03-21 23:47               ` Marek Vasut
2024-03-17 11:07 ` [PATCH v2 5/6] usb: kbd: support Apple Magic Keyboards (2021) Janne Grunau via B4 Relay
2024-03-17 16:20   ` Marek Vasut
2024-03-17 11:07 ` [PATCH v2 6/6] usb: kbd: Add probe quirk for Apple and Keychron keyboards Janne Grunau via B4 Relay
2024-03-17 16:21   ` Marek Vasut
2024-03-17 11:26 ` [PATCH v2 0/6] USB keyboard improvements for asahi / desktop systems Neal Gompa

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240317-asahi-keyboards-v2-2-d3f4b8384f68@jannau.net \
    --to=devnull+j.jannau.net@kernel.org \
    --cc=asahi@lists.linux.dev \
    --cc=bmeng.cn@gmail.com \
    --cc=j@jannau.net \
    --cc=joe.hershberger@ni.com \
    --cc=marex@denx.de \
    --cc=sjg@chromium.org \
    --cc=trini@konsulko.com \
    --cc=u-boot@lists.denx.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).