Skip to content

Add Host HID APIs to set/get protocol and and optional device HID changes protocol callback#244

Merged
fdesbiens merged 3 commits into
eclipse-threadx:devfrom
ayedm1:add_set_get_host_hid_api
Jun 4, 2026
Merged

Add Host HID APIs to set/get protocol and and optional device HID changes protocol callback#244
fdesbiens merged 3 commits into
eclipse-threadx:devfrom
ayedm1:add_set_get_host_hid_api

Conversation

@ayedm1
Copy link
Copy Markdown
Contributor

@ayedm1 ayedm1 commented Jan 18, 2026

This pull request adds host HID API functions to get and set the HID protocol (boot vs report mode) and introduces an optional callback for the device side when the protocol changes.

  • Added new host-side APIs ux_host_class_hid_protocol_set() and ux_host_class_hid_protocol_get() with error checking wrappers.
  • Added device-side optional callback ux_device_class_hid_set_protocol_callback that is invoked when the protocol changes
  • Added comprehensive test to verify the new protocol callback functionality

@ayedm1 ayedm1 requested review from ABOUSTM and rahmanih January 18, 2026 13:35
@ayedm1 ayedm1 moved this to In review in ThreadX Roadmap Jan 18, 2026
@ayedm1 ayedm1 added the feature New feature or enhancement request label Jan 18, 2026
@fdesbiens fdesbiens changed the base branch from master to dev February 5, 2026 21:36
Copy link
Copy Markdown
Contributor

@rahmanih rahmanih left a comment

Choose a reason for hiding this comment

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

IIRC, this PR implements 2 features in HID Device and Host Classes.
please split them in separate commits.

@ayedm1
Copy link
Copy Markdown
Contributor Author

ayedm1 commented Feb 9, 2026

IIRC, this PR implements 2 features in HID Device and Host Classes. please split them in separate commits.

in the simulator test, i test the callback with new apis

@rahmanih
Copy link
Copy Markdown
Contributor

the regression test updates can be made in a separate commit too.

@rahmanih rahmanih changed the title Add optional device hid changes protocol callback and host hid api to set/get protocol Add Host HID APIs to set/get protocol and and optional device HID changes protocol callback Feb 13, 2026
Comment thread common/usbx_host_classes/src/ux_host_class_hid_protocol_get.c Outdated
Comment thread common/usbx_host_classes/src/ux_host_class_hid_protocol_get.c Outdated
Comment thread common/usbx_host_classes/src/ux_host_class_hid_protocol_get.c Outdated
@ayedm1 ayedm1 force-pushed the add_set_get_host_hid_api branch 4 times, most recently from 0f90e31 to b024452 Compare February 24, 2026 22:31
@ayedm1 ayedm1 marked this pull request as draft February 24, 2026 22:43
@ayedm1 ayedm1 force-pushed the add_set_get_host_hid_api branch from b024452 to 6ea0ac4 Compare March 3, 2026 23:45
@ayedm1 ayedm1 marked this pull request as ready for review March 3, 2026 23:46
@ayedm1
Copy link
Copy Markdown
Contributor Author

ayedm1 commented Mar 3, 2026

How to test:

1- Update middleware
2- Open x-cube-azrtos-h7 HID Host RTOS and Standalone project
3- Copy content of attached app_usbx_host.c

@ayedm1 ayedm1 force-pushed the add_set_get_host_hid_api branch 2 times, most recently from ea1f53c to 9032773 Compare March 4, 2026 11:53
… set/get protocol

This pull request adds host HID API functions to get and set the HID protocol (boot vs report mode) and introduces an optional callback for the device side when the protocol changes.

- Added new host-side APIs ux_host_class_hid_protocol_set() and ux_host_class_hid_protocol_get() with error checking wrappers.
- Added device-side optional callback ux_device_class_hid_set_protocol_callback that is invoked when the protocol changes
- Added comprehensive test to verify the new protocol callback functionality
@ayedm1 ayedm1 force-pushed the add_set_get_host_hid_api branch from 9032773 to 63a0d44 Compare March 4, 2026 11:57
@ayedm1 ayedm1 requested a review from rahmanih March 5, 2026 15:28
@fdesbiens
Copy link
Copy Markdown
Contributor

@rahmanih: Please confirm whether I can merge this or not. Thanks.

Copy link
Copy Markdown
Contributor

@rahmanih rahmanih left a comment

Choose a reason for hiding this comment

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

LGTM 💯

@rahmanih
Copy link
Copy Markdown
Contributor

rahmanih commented Jun 2, 2026

@ABOUSTM Could you please provide your review.

Three bugs fixed in the new _ux_host_class_hid_protocol_get/set functions:

1. protocol_get: use a cache-safe allocated buffer for the DMA transfer
   instead of the caller's USHORT* directly. The caller's stack memory
   is not guaranteed to be DMA-safe on cache-incoherent embedded targets,
   and writing 1 byte into a USHORT* yields incorrect results on
   big-endian platforms. Follows the same pattern as ux_host_class_hid_idle_get.

2. Both functions: acquire ux_device_protection_semaphore in RTOS mode
   before submitting the transfer, in addition to the HID instance
   semaphore. All other control transfer functions (idle_get, idle_set)
   take both semaphores. Missing the device semaphore allowed concurrent
   callers to corrupt the shared transfer_request fields.

3. Both functions: add proper standalone (UX_HOST_STANDALONE) locking
   using UX_DISABLE/UX_RESTORE to atomically check and set
   UX_HOST_CLASS_HID_FLAG_LOCK and UX_DEVICE_FLAG_LOCK, and set
   UX_TRANSFER_FLAG_AUTO_DEVICE_UNLOCK + UX_TRANSFER_STATE_RESET on the
   transfer request before initiating. Follows the idle_get inline
   standalone pattern.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@fdesbiens
Copy link
Copy Markdown
Contributor

I am merging into dev now, and this will ship with our Q2 2026 release next week.

Please open a new PR if there are remaining issues with the code.

@fdesbiens fdesbiens merged commit 3592722 into eclipse-threadx:dev Jun 4, 2026
1 check passed
@github-project-automation github-project-automation Bot moved this from In review to Done in ThreadX Roadmap Jun 4, 2026
fdesbiens added a commit that referenced this pull request Jun 4, 2026
Two issues discovered while reviewing and fixing thread-safety issues in
the new ux_host_class_hid_protocol_get/set functions (PR #244).

1. idle_get.c: wrong operator acquires no lock in standalone mode

   Line 104 used '&= ~UX_HOST_CLASS_HID_FLAG_LOCK' (the release/clear
   operation) instead of '|= UX_HOST_CLASS_HID_FLAG_LOCK' (acquire/set).
   The preceding check correctly returns UX_BUSY when the flag is set,
   but the follow-on line then immediately clears it instead of setting it.
   The net effect is that the HID instance is never actually locked in
   UX_HOST_STANDALONE mode, making the mutual-exclusion check a no-op.

2. idle_set.c: standalone path used a blocking spin-loop

   The standalone branch called _ux_host_class_hid_idle_set_run() in a
   do/while loop, blocking the caller until the transfer completed. This
   is inconsistent with every other inline HID control-transfer function
   (idle_get, report_get, report_set, protocol_get, protocol_set) which
   all use the direct UX_DISABLE/UX_RESTORE atomic flag pattern and
   return UX_BUSY if the instance or device endpoint is already locked.

   Replaced with the same inline standalone locking pattern used by the
   other functions: atomic HID FLAG_LOCK acquire, atomic DEVICE_FLAG_LOCK
   acquire with AUTO_DEVICE_UNLOCK + UX_TRANSFER_STATE_RESET, and an
   AUTO_WAIT check at completion consistent with idle_get behavior.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature New feature or enhancement request

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

3 participants