Skip to content

drivers: usb: uhc: add hub topology fields and getters#102838

Closed
AidenHu wants to merge 4 commits intozephyrproject-rtos:mainfrom
nxp-upstream:add-usb-hub-related-fields
Closed

drivers: usb: uhc: add hub topology fields and getters#102838
AidenHu wants to merge 4 commits intozephyrproject-rtos:mainfrom
nxp-upstream:add-usb-hub-related-fields

Conversation

@AidenHu
Copy link
Copy Markdown
Contributor

@AidenHu AidenHu commented Jan 24, 2026

Add hub-related fields to struct usb_device under
CONFIG_USBH_HUB_CLASS:

  • hub_addr, hub_port
  • hs_hub_addr, hs_hub_port
  • level, total_think_time

Update MCUX UHC common code to return these values in hub-related queries.

@sonarqubecloud
Copy link
Copy Markdown

Comment thread include/zephyr/drivers/usb/uhc.h Outdated
Comment on lines +93 to +106
#ifdef CONFIG_USBH_HUB_CLASS
/** Device's first connected hub address (root hub = 0) */
uint8_t hub_addr;
/** Device's first connected hub's port */
uint8_t hub_port;
/** Device's first connected high-speed hub's address */
uint8_t hs_hub_addr;
/** Device's first connected high-speed hub's port */
uint8_t hs_hub_port;
/** Device's level (root device = 0) */
uint8_t level;
/** Device's first connected hub's total think time */
uint16_t total_think_time;
#endif
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

USBH_HUB_CLASS uses the wrong namespace and must not be used in the driver API. I cannot think of any reasonable reason to have this option or to make these properties conditional. There is no reason to have hub_addr/hub_port. Hub is just a device. And having something like usb_device *hub; uint8_t portnum; uint8_t level; should be sufficient. But the purpose of this commit is unclear.

Copy link
Copy Markdown
Contributor Author

@AidenHu AidenHu Jan 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jfischer-no
Thank you for the reply. Actually this PR's commit is from the usb: host: class: support for usb host video class #94085. We have to consider about the split transfer and MCUX USB controllers need to get these information to calculate bandwidth allocation.
Agree that USBH_HUB_CLASS naming seems not suitable, USBH_HUB_CLASS macro can be removed to avoid breaking namespace.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AidenHu I think @jfischer-no means you can try to add one usb_device *parent to this struct, then some information can be get through the parent.

@carlescufi carlescufi mentioned this pull request Feb 5, 2026
18 tasks
@zejiang0jason zejiang0jason removed their request for review March 17, 2026 07:30
@AidenHu AidenHu force-pushed the add-usb-hub-related-fields branch from 7124f2e to 523ca72 Compare April 1, 2026 08:21
@AidenHu
Copy link
Copy Markdown
Contributor Author

AidenHu commented Apr 1, 2026

@jfischer-no
I have updated the fields, but I have to keep the think time. Please help review the new update. Thanks.
total_think_time indicates total think time for the upstream hubs. If all of hub's itself think time is 32, take an example: host->hub1->hub2->hub3->device, then hub1's total_think_time is 32, hub2's total_think_time is 64, hub2's total_think_time is 96. For the end device, the total think time is wanted, so this is easy for getting with saving the total not the single.

josuah
josuah previously approved these changes Apr 3, 2026
Copy link
Copy Markdown
Contributor

@josuah josuah left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently, the new HUB-related fields are not populated by the stack.

However, the driver is not having a bug: it behaves the same way as before, where *infoValue was set to 0 already, so maybe this works without problem to merge the commits in this order.

Some optional improvement are proposed.

Comment thread drivers/usb/uhc/uhc_mcux_common.c Outdated
Comment thread drivers/usb/uhc/uhc_mcux_common.c Outdated
Comment thread drivers/usb/uhc/uhc_mcux_common.c Outdated
Comment thread drivers/usb/uhc/uhc_mcux_common.c
Copy link
Copy Markdown
Contributor

@josuah josuah left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some more complement proposition to be more generic...

Comment thread include/zephyr/drivers/usb/uhc.h Outdated
Comment thread include/zephyr/usb/usb_ch9.h Outdated
Comment on lines +142 to +143
#define USB_HUB_GET_THINK_TIME(wHubCharacteristics) \
(((((wHubCharacteristics) & 0x0060U) >> 5) + 1) << 3)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not technically Chapter 9 USB Device Framework but rather Chapter 11 Hub Specification. I am not sure if it should be added to usb_ch9.h or if a new header should be created.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My mistake, I advised wrong.

@AidenHu
Copy link
Copy Markdown
Contributor Author

AidenHu commented Apr 6, 2026

Thank you @tmon-nordic and @josuah

Currently I am thinking about location of USB_HUB_GET_THINK_TIME and the hub-related definitions/structures that have been moved to usbh_ch9.h. My plan is keeping the hub-related definitions/structures that have been moved to usbh_ch9.h to go back to usbh_hub.h, it is also the original implementation. For USB_HUB_GET_THINK_TIME, is it possible to put it in uhc.h?

@josuah
Copy link
Copy Markdown
Contributor

josuah commented Apr 6, 2026

For USB_HUB_GET_THINK_TIME is it possible to put it in uhc.h?

I just found this by chance:

/**
* @file
* @brief USB Hub Class device API header
*/
#ifndef ZEPHYR_INCLUDE_USB_CLASS_USB_HUB_H_
#define ZEPHYR_INCLUDE_USB_CLASS_USB_HUB_H_
/** USB Hub Class Feature Selectors defined in spec. Table 11-17 */
#define USB_HCFS_C_HUB_LOCAL_POWER 0x00
#define USB_HCFS_C_HUB_OVER_CURRENT 0x01
#define USB_HCFS_PORT_CONNECTION 0x00
#define USB_HCFS_PORT_ENABLE 0x01
#define USB_HCFS_PORT_SUSPEND 0x02
#define USB_HCFS_PORT_OVER_CURRENT 0x03
#define USB_HCFS_PORT_RESET 0x04
#define USB_HCFS_PORT_POWER 0x08
#define USB_HCFS_PORT_LOW_SPEED 0x09
#define USB_HCFS_C_PORT_CONNECTION 0x10
#define USB_HCFS_C_PORT_ENABLE 0x11
#define USB_HCFS_C_PORT_SUSPEND 0x12
#define USB_HCFS_C_PORT_OVER_CURRENT 0x13
#define USB_HCFS_C_PORT_RESET 0x14
#define USB_HCFS_PORT_TEST 0x15
#define USB_HCFS_PORT_INDICATOR 0x16
/** USB Hub Class Request Codes defined in spec. Table 11-16 */
#define USB_HCREQ_GET_STATUS 0x00
#define USB_HCREQ_CLEAR_FEATURE 0x01
#define USB_HCREQ_SET_FEATURE 0x03
#define USB_HCREQ_GET_DESCRIPTOR 0x06
#define USB_HCREQ_SET_DESCRIPTOR 0x07
#define USB_HCREQ_CLEAR_TT_BUFFER 0x08
#define USB_HCREQ_RESET_TT 0x09
#define USB_HCREQ_GET_TT_STATE 0x0A
#define USB_HCREQ_STOP_TT 0x0B
#endif /* ZEPHYR_INCLUDE_USB_CLASS_USB_HUB_H_ */

This already has chapter 11 tables, like Table 11-17.

That looks like reasonable to extend it.

it is also the original implementation

Yes my bad I did a mistake by advising usb_ch9.h, I missed the fact it was a different chapter.

@AidenHu
Copy link
Copy Markdown
Contributor Author

AidenHu commented Apr 7, 2026

define USB_HCFS_C_HUB_LOCAL_POWER 0x00

For USB_HUB_GET_THINK_TIME is it possible to put it in uhc.h?

I just found this by chance:

/**
* @file
* @brief USB Hub Class device API header
*/
#ifndef ZEPHYR_INCLUDE_USB_CLASS_USB_HUB_H_
#define ZEPHYR_INCLUDE_USB_CLASS_USB_HUB_H_
/** USB Hub Class Feature Selectors defined in spec. Table 11-17 */
#define USB_HCFS_C_HUB_LOCAL_POWER 0x00
#define USB_HCFS_C_HUB_OVER_CURRENT 0x01
#define USB_HCFS_PORT_CONNECTION 0x00
#define USB_HCFS_PORT_ENABLE 0x01
#define USB_HCFS_PORT_SUSPEND 0x02
#define USB_HCFS_PORT_OVER_CURRENT 0x03
#define USB_HCFS_PORT_RESET 0x04
#define USB_HCFS_PORT_POWER 0x08
#define USB_HCFS_PORT_LOW_SPEED 0x09
#define USB_HCFS_C_PORT_CONNECTION 0x10
#define USB_HCFS_C_PORT_ENABLE 0x11
#define USB_HCFS_C_PORT_SUSPEND 0x12
#define USB_HCFS_C_PORT_OVER_CURRENT 0x13
#define USB_HCFS_C_PORT_RESET 0x14
#define USB_HCFS_PORT_TEST 0x15
#define USB_HCFS_PORT_INDICATOR 0x16
/** USB Hub Class Request Codes defined in spec. Table 11-16 */
#define USB_HCREQ_GET_STATUS 0x00
#define USB_HCREQ_CLEAR_FEATURE 0x01
#define USB_HCREQ_SET_FEATURE 0x03
#define USB_HCREQ_GET_DESCRIPTOR 0x06
#define USB_HCREQ_SET_DESCRIPTOR 0x07
#define USB_HCREQ_CLEAR_TT_BUFFER 0x08
#define USB_HCREQ_RESET_TT 0x09
#define USB_HCREQ_GET_TT_STATE 0x0A
#define USB_HCREQ_STOP_TT 0x0B
#endif /* ZEPHYR_INCLUDE_USB_CLASS_USB_HUB_H_ */

This already has chapter 11 tables, like Table 11-17.

That looks like reasonable to extend it.

it is also the original implementation

Yes my bad I did a mistake by advising usb_ch9.h, I missed the fact it was a different chapter.

@josuah,
Never mind. Thank you for confirming.

@AidenHu AidenHu force-pushed the add-usb-hub-related-fields branch 2 times, most recently from 55dcfe9 to a6d3395 Compare April 8, 2026 06:23
@AidenHu AidenHu force-pushed the add-usb-hub-related-fields branch from a6d3395 to 89fe98f Compare April 8, 2026 06:28
@AidenHu
Copy link
Copy Markdown
Contributor Author

AidenHu commented Apr 9, 2026

@jfischer-no
Please help to review, thanks.

@AidenHu
Copy link
Copy Markdown
Contributor Author

AidenHu commented Apr 13, 2026

@josuah and @jfischer-no
Please help to review and see if it can be merged.

@mmahadevan108 mmahadevan108 requested review from jfischer-no, josuah and tmon-nordic and removed request for jfischer-no April 20, 2026 22:36
Comment thread include/zephyr/usb/class/usb_hub.h Outdated
#define USB_HCREQ_STOP_TT 0x0B

/**
* @brief Get Think Time field from wHubCharacteristics.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

* Get Think Time value from wHubCharacteristics field.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated.

Comment thread include/zephyr/usb/class/usb_hub.h Outdated
/**
* @brief Get Think Time field from wHubCharacteristics.
*
* @param wHubCharacteristics Hub characteristics value from hub descriptor.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

* @param wHubCharacteristics Hub descriptor field wHubCharacteristics

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated.

Comment thread include/zephyr/drivers/usb/uhc.h Outdated
Comment on lines +99 to +102
/** Pointer to the hub to which this device is connected */
struct usb_device *hub;
/** Device's hub wHubCharacteristics */
uint16_t hub_characteristics;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the whole wHubCharacteristics should not be exposed. Also using USB_HUB_GET_THINK_TIME() in the driver look a bit odd. Only think time seems to be relevant. Change it to

	/** Pointer to the hub to which this device is connected */
	struct usb_device *hub;
	/** Device's hub Think Time */
	uint16_t tt;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My bad for proposing to expose wHubCharacteristics instead of only the Think Time then!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My bad for proposing to expose wHubCharacteristics instead of only the Think Time then!

Never mind, this does not take much time.

Comment on lines +99 to +106
/** Pointer to the hub to which this device is connected */
struct usb_device *hub;
/** Device's hub wHubCharacteristics */
uint16_t hub_characteristics;
/** Device's hub port */
uint8_t hub_port;
/** Device's level (root device = 0) */
uint8_t level;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change in uhc.h does not seem to belong in this commit.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated.


if (ctx->root == NULL) {
ctx->root = udev;
udev->level = 1;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is not udev->level. This commit is just wrong.

host: core: set initialized value of level for first udev

What host? What core?

usb: host: set level value for the first USB device ?

Copy link
Copy Markdown
Contributor Author

@AidenHu AidenHu Apr 22, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. Adjust the commits' order and msg. Updated.

AidenHu added 3 commits April 22, 2026 12:18
This change extends the usb_device structure to keep track of
its hub relationship, including the parent hub device, hub
Think Time, port number, and topology level.

Signed-off-by: Aiden Hu <weiwei.hu@nxp.com>
when the first device is attached, its level should be
set as 1.

Signed-off-by: Aiden Hu <weiwei.hu@nxp.com>
This change updates usb_hub.h by adding missing USB Hub class
definitions from the specification, including hub class codes,
descriptor types, status and change bits, and related data
structures. The goal is to make the public header more complete
and easier to use for USB hub implementations.

Signed-off-by: Aiden Hu <weiwei.hu@nxp.com>
@AidenHu AidenHu force-pushed the add-usb-hub-related-fields branch from 89fe98f to 3ff302c Compare April 22, 2026 04:19
@AidenHu
Copy link
Copy Markdown
Contributor Author

AidenHu commented Apr 22, 2026

@jfischer-no
Thank you for the review. I have checked the comments you have raised. Besides, some additional updates are added from usb_hub_pr .
Please help to review again and see if it is ok to merge, thanks.

This change enhances USB_HostHelperGetPeripheralInformation()
so the MCUX USB host driver can correctly report hub-related
topology data, including the parent hub address, port number,
nearest high speed hub, HS hub port, hub think time and level.
The goal is to ensure proper device identification and routing,
especially when full speed or low speed devices are connected
behind multi level or high speed hubs.

Signed-off-by: Aiden Hu <weiwei.hu@nxp.com>
@AidenHu AidenHu force-pushed the add-usb-hub-related-fields branch from 3ff302c to 6f75d48 Compare April 22, 2026 07:25
@sonarqubecloud
Copy link
Copy Markdown

/** @} */

/**
* @name USB Hub Class Feature Selectors. See Table 11-17 of the specification.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is a preview of the doc after this is merged:

https://builds.zephyrproject.io/zephyr/pr/102838/docs/doxygen/html/usb__hub_8h.html

As you see here the "See Table 11-17 ..." is on the title, I would be tempted to split it to the next line:

/**
 * @name USB Hub Class Feature Selectors.
 * 
 * See Table 11-17 of the specification.
 */

Here and other locations.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, updated.

@AidenHu
Copy link
Copy Markdown
Contributor Author

AidenHu commented Apr 23, 2026

Close this pr, all of changes are in the main hub PR: #99735

@AidenHu AidenHu closed this Apr 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: USB Universal Serial Bus platform: NXP NXP

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants