Skip to contentSkip to conversation

usb: xhci: Fix bNbrPorts overwritten by another controller

PATCH In review Beibei Yang 2 participants 7 messages Jun 27 – Jun 30

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 usb: xhci: Fix bNbrPorts overwritten by another controller

In a system with multiple XHCI controllers, the global descriptor.hub (bNbrPorts) can be overwritten by a later initialized controller, causing the root hub of an earlier controller to report wrong port numbers.

Fix this by dynamically setting bNbrPorts from the current controller's hcsparams1 in the USB_DT_HUB request handler, instead of relying on a stale global value.

drivers/usb/host/xhci.c +6 -0
index dbeb88a..f7984de 100644
@@ -942,6 +942,12 @@ static int xhci_submit_root(struct usb_device *udev, unsigned long pipe,
 		case USB_DT_HUB:
 		case USB_DT_SS_HUB:
 			debug("USB_DT_HUB config\n");
+			/* Dynamically set bNbrPorts from this controller's
+			* hcsparams1, because the global descriptor.hub
+			* may have been overwritten by another controller
+			* that initialized after this one. */
+			descriptor.hub.bNbrPorts =
+				HCS_MAX_PORTS(xhci_readl(&hccr->cr_hcsparams1));
 			srcptr = &descriptor.hub;
 			srclen = 0x8;
 			break;
#1 lore raw
Quoted 8 lines · Beibei Yang

In a system with multiple XHCI controllers, the global descriptor.hub (bNbrPorts) can be overwritten by a later initialized controller, causing the root hub of an earlier controller to report wrong port numbers.

Fix this by dynamically setting bNbrPorts from the current controller's hcsparams1 in the USB_DT_HUB request handler, instead of relying on a stale global value.

How about having one descriptor per controller ?

This is probably easily doable in xhci_lowlevel_init() with some ctrl->hub_desc = kmemdup(descriptor.hub,...); and related tweaks.

What do you think ?

author
#2 lore raw

My patch just fix set a correct bNbrPorts value for per XHCI controller, but this code modification is less. Yes, your advice having one descriptor per controller is more properly, this can solve all other potential problems.

Quoted 16 lines · Marek Vasut
Quoted 8 lines · Beibei Yang

In a system with multiple XHCI controllers, the global descriptor.hub (bNbrPorts) can be overwritten by a later initialized controller, causing the root hub of an earlier controller to report wrong port numbers.

Fix this by dynamically setting bNbrPorts from the current controller's hcsparams1 in the USB_DT_HUB request handler, instead of relying on a stale global value.

How about having one descriptor per controller ?

This is probably easily doable in xhci_lowlevel_init() with some ctrl->hub_desc = kmemdup(descriptor.hub,...); and related tweaks.

What do you think ?

#3 lore raw
In reply to Marek Vasut

For the struct descriptor, only the hub part (bNbrPorts and wHubCharacteristics) is different for each controller; the other five members (device, config, interface, endpoint,ep_companion)are identical for all XHCI root hubs.

My previous patch only fixed the dynamic reading of bNbrPorts, but wHubCharacteristics is also different for each controller and will be overridden by the last controller.

New patch as below for referrence.

usb/host/xhci.c (working copy) renamed from usb/host/xhci.c (revision 10446) +9 -8
Index: drivers/usb/host/xhci.c
===================================================================
@@ -954,13 +954,7 @@
               case USB_DT_HUB:
               case USB_DT_SS_HUB:
                       debug("USB_DT_HUB config\n");
-                       srcptr = &descriptor.hub;
+                       srcptr = &ctrl->hub_desc;
                       srclen = 0x8;
                       break;
               default:
@@ -1221,20 +1215,21 @@
               return -ENOMEM;
 
       reg = xhci_readl(&hccr->cr_hcsparams1);
-       descriptor.hub.bNbrPorts = ((reg & HCS_MAX_PORTS_MASK) >>
-                                               HCS_MAX_PORTS_SHIFT);
-       printf("Register %x NbrPorts %d\n", reg, descriptor.hub.bNbrPorts);
+       ctrl->hub_desc = descriptor.hub;
+       ctrl->hub_desc.bNbrPorts = ((reg & HCS_MAX_PORTS_MASK) >>
+                                       HCS_MAX_PORTS_SHIFT);
+       printf("Register %x NbrPorts %d\n", reg, ctrl->hub_desc.bNbrPorts);
 
       /* Port Indicators */
       reg = xhci_readl(&hccr->cr_hccparams);
       if (HCS_INDICATOR(reg))
-               put_unaligned(get_unaligned(&descriptor.hub.wHubCharacteristics)
-                               | 0x80, &descriptor.hub.wHubCharacteristics);
+               put_unaligned(get_unaligned(&ctrl->hub_desc.wHubCharacteristics)
+                               | 0x80, &ctrl->hub_desc.wHubCharacteristics);
 
       /* Port Power Control */
       if (HCC_PPC(reg))
-               put_unaligned(get_unaligned(&descriptor.hub.wHubCharacteristics)
-                               | 0x01, &descriptor.hub.wHubCharacteristics);
+               put_unaligned(get_unaligned(&ctrl->hub_desc.wHubCharacteristics)
+                               | 0x01, &ctrl->hub_desc.wHubCharacteristics);
 
       if (xhci_start(hcor)) {
               xhci_reset(hcor);
usb/xhci.h (working copy) renamed from usb/xhci.h (revision 10446) +0 -0
Index: include/usb/xhci.h
===================================================================
@@ -21,6 +21,7 @@

#include <asm/io.h>
#include <linux/list.h>
#include <linux/compat.h>
+#include <usb.h>

#define MAX_EP_CTX_NUM 31
#define XHCI_ALIGNMENT 64
@@ -1229,6 +1230,7 @@

       struct xhci_erst_entry entry[ERST_NUM_SEGS];
       struct xhci_scratchpad *scratchpad;
       struct xhci_virt_device *devs[MAX_HC_SLOTS];
+       struct usb_hub_descriptor hub_desc;
       int rootdev;
};

---- Replied Message ----

| From | Marek Vasut<[email protected]> |
| Date | 06/28/2026 04:10 |
| To | Beibei Yang<[email protected]>、[email protected] |
| Cc | [email protected] |
| Subject | Re: [PATCH] usb: xhci: Fix bNbrPorts overwritten by another controller |
Quoted 8 lines · Beibei Yang

In a system with multiple XHCI controllers, the global descriptor.hub (bNbrPorts) can be overwritten by a later initialized controller, causing the root hub of an earlier controller to report wrong port numbers.

Fix this by dynamically setting bNbrPorts from the current controller's hcsparams1 in the USB_DT_HUB request handler, instead of relying on a stale global value.

How about having one descriptor per controller ?

This is probably easily doable in xhci_lowlevel_init() with some ctrl->hub_desc = kmemdup(descriptor.hub,...); and related tweaks.

What do you think ?

#4 lore raw
Quoted 5 lines · On 6/29/26 8:25 AM, m13738176232 wrote:

For the struct descriptor, only the hub part (bNbrPorts and wHubCharacteristics) is different for each controller; the other five members (device, config, interface, endpoint,ep_companion)are identical for all XHCI root hubs.

My previous patch only fixed the dynamic reading of bNbrPorts, but wHubCharacteristics is also different for each controller and will be overridden by the last controller.

New patch as below for referrence.

Please send this as a V2 properly. Thank you !

author
#5 lore raw

New patch as attachment, thank you.

Quoted 7 lines · Marek Vasut
Quoted 5 lines · On 6/29/26 8:25 AM, m13738176232 wrote:

For the struct descriptor, only the hub part (bNbrPorts and wHubCharacteristics) is different for each controller; the other five members (device, config, interface, endpoint,ep_companion)are identical for all XHCI root hubs.

My previous patch only fixed the dynamic reading of bNbrPorts, but wHubCharacteristics is also different for each controller and will be overridden by the last controller.

New patch as below for referrence.

Please send this as a V2 properly. Thank you !

From: Beibei Yang <[email protected]>
Subject: [PATCH] usb: xhci: Fix bNbrPorts and wHubCharacteristics overwritten by another controller

For the struct descriptor, only the hub part (bNbrPorts and wHubCharacteristics) is different for each controller, the other five members (device, config, interface, endpoint,ep_companion)are identical for all XHCI root hubs.

drivers/usb/host/xhci.c +9 -7
index dbeb88a..1469b3d 100644
@@ -942,7 +942,7 @@ static int xhci_submit_root(struct usb_device *udev, unsigned long pipe,
 		case USB_DT_HUB:
 		case USB_DT_SS_HUB:
 			debug("USB_DT_HUB config\n");
-			srcptr = &descriptor.hub;
+			srcptr = &ctrl->hub_desc;
 			srclen = 0x8;
 			break;
 		default:
@@ -1203,19 +1203,21 @@ static int xhci_lowlevel_init(struct xhci_ctrl *ctrl)
 		return -ENOMEM;
 
 	reg = xhci_readl(&hccr->cr_hcsparams1);
-	descriptor.hub.bNbrPorts = HCS_MAX_PORTS(reg);
-	printf("Register %x NbrPorts %d\n", reg, descriptor.hub.bNbrPorts);
+	ctrl->hub_desc = descriptor.hub;
+	ctrl->hub_desc.bNbrPorts = ((reg & HCS_MAX_PORTS_MASK) >>
+		HCS_MAX_PORTS_SHIFT);
+	printf("Register %x NbrPorts %d\n", reg, ctrl->hub_desc.bNbrPorts);
 
 	/* Port Indicators */
 	reg = xhci_readl(&hccr->cr_hccparams);
 	if (HCS_INDICATOR(reg))
-		put_unaligned(get_unaligned(&descriptor.hub.wHubCharacteristics)
-				| 0x80, &descriptor.hub.wHubCharacteristics);
+		put_unaligned(get_unaligned(&ctrl->hub_desc.wHubCharacteristics)
+				| 0x80, &ctrl->hub_desc.wHubCharacteristics);
 
 	/* Port Power Control */
 	if (HCC_PPC(reg))
-		put_unaligned(get_unaligned(&descriptor.hub.wHubCharacteristics)
-				| 0x01, &descriptor.hub.wHubCharacteristics);
+		put_unaligned(get_unaligned(&ctrl->hub_desc.wHubCharacteristics)
+				| 0x01, &ctrl->hub_desc.wHubCharacteristics);
 
 	if (xhci_start(hcor)) {
 		xhci_reset(hcor);
include/usb/xhci.h +2 -0
index ea4cf3f..9feb3aa 100644
@@ -22,6 +22,7 @@
 #include <asm/io.h>
 #include <linux/list.h>
 #include <linux/compat.h>
+#include <usb.h>
 
 #define MAX_EP_CTX_NUM		31
 #define XHCI_ALIGNMENT		64
@@ -1214,6 +1215,7 @@ struct xhci_ctrl {
 	struct xhci_erst_entry entry[ERST_NUM_SEGS];
 	struct xhci_scratchpad *scratchpad;
 	struct xhci_virt_device *devs[MAX_HC_SLOTS];
+	struct usb_hub_descriptor hub_desc;
 	int rootdev;
 	u16 hci_version;
 	u32 quirks;