-
Notifications
You must be signed in to change notification settings - Fork 14
Cherry-pick USB changes for release 3.3 #559
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
JIRA: RTOS-937 (cherry picked from commit 6669135)
JIRA: RTOS-937 (cherry picked from commit ae48f37)
JIRA: RTOS-937 (cherry picked from commit ff92025)
The root hub is now declared with bDeviceProtocol 0 instead of 1 to differentiate between root hubs and real TT layer 2 hubs in the usb stack JIRA: RTOS-937 (cherry picked from commit 2dc3047)
JIRA: RTOS-970 (cherry picked from commit c299eb8)
JIRA: RTOS-970 (cherry picked from commit 2dd11e3)
JIRA: RTOS-970 (cherry picked from commit 82b9234)
JIRA: RTOS-970 (cherry picked from commit fedd5cb)
JIRA: RTOS-970 (cherry picked from commit 8a8e572)
This fixes race conditions happening when there are multiple usb insertion/deletion/completion threads. Access to usbacm_common.devices struct was not guarded, which lead i.e. to multiple concurrent attempts of /dev/usbacm0 creation during insertion and possible double-free on destruction, since usbacm_freeAll was also called outside of critical section JIRA: RTOS-970 (cherry picked from commit 871c77b)
JIRA: RTOS-970 (cherry picked from commit 9cdf1e1)
Some real world sticks always fail few first commands but if REQUEST SENSE is done immediately afterwards, they will later work with no errors. Some others fail the first few READ commands, but recover after few retries. JIRA: RTOS-970 (cherry picked from commit 594cb68)
JIRA: RTOS-1024 (cherry picked from commit f160d1a)
JIRA: RTOS-1024 (cherry picked from commit b5f6620)
JIRA: RTOS-1024 (cherry picked from commit c144b16)
…e insertion Mutex was still locked when returning after error from umass_handleInsertion JIRA: RTOS-1024 (cherry picked from commit 6fd2aaa)
JIRA: RTOS-1024 (cherry picked from commit ae3e195)
JIRA: RTOS-1024 (cherry picked from commit 0631265)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hello @adamgreloch, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!
Summary of Changes
Hello team,
Gemini here, providing a summary for this pull request. This PR is a cherry-pick aimed at bringing significant USB-related changes, including a rework of the Mass Storage (UMASS) and CDC ACM (USBACM) drivers and the EHCI host controller driver, into the 3.3 release branch. It also incorporates additions related to a new USB event API and driver structure.
Highlights
- USB Driver Rework (UMASS & USBACM): The UMASS and USBACM drivers have been refactored to align with a new USB driver structure. This involves splitting the drivers into static libraries (
libusbdrv-umass,libusbdrv-usbacm) and separate binary server components (umass,usbacm). The server components handle driver registration and main execution flow, while the core driver logic resides in the libraries. - UMASS Driver Enhancements: The UMASS driver now includes definitions for the SCSI transparent command set, improved bulk transfer handling with retries and SCSI sense request support, and device management using an idtree instead of a linked list. It also introduces support for mounting filesystems on partitions, with specific compile-time support added for the ext2 filesystem.
- USBACM Driver Enhancements: The USBACM driver has been updated to support setting line coding parameters. Device management has been refined using a linked list with reference counting and a dedicated list for devices pending freeing.
- EHCI Host Controller Driver Updates: The EHCI driver has been refactored to work with a new HCD structure. Register access now explicitly uses the operational register base (
opbase). Interrupt handling has been improved, including clearing specific status bits and handling system errors. QTD allocation logic has been adjusted, and the PHY layer initialization has been updated for various architectures (imx6ull, imxrt106x, imxrt117x, ia32). A new ia32-specific PHY layer implementation has been added, including PCI configuration and BIOS ownership handling. - Target Makefile Updates: The Makefiles for the
armv7a7-imx6ull,armv7m7-imxrt106x, andia32-generictargets have been updated to include the newly introduced static libraries (libusbdrv-umass,libusbdrv-usbacm) in their default component lists.
Changelog
Click here to see the changelog
- _targets/Makefile.armv7a7-imx6ull
- Added
libusbdrv-umassandlibusbdrv-usbacmtoDEFAULT_COMPONENTS.
- Added
- _targets/Makefile.armv7m7-imxrt106x
- Added
libusbdrv-umassandlibusbdrv-usbacmtoDEFAULT_COMPONENTSfor imxrt117x and imxrt106x targets.
- Added
- _targets/Makefile.ia32-generic
- Added
libusbehci,umass, andlibusbdrv-umasstoDEFAULT_COMPONENTS.
- Added
- storage/umass/Makefile
- Updated copyright year to 2024.
- Split
umassinto a static librarylibusbdrv-umassand a binaryumass. - Added
srv.cto the sources for theumassbinary. - Added conditional dependency on
libext2for theia32target family.
- storage/umass/scsi.h
- New file: Added definitions for SCSI transparent command set structures (
scsi_cdb10_t,scsi_cdb6_t,scsi_sense_t,scsi_inquiry_t) and opcodes (SCSI_REQUEST_SENSE,SCSI_INQUIRY).
- New file: Added definitions for SCSI transparent command set structures (
- storage/umass/srv.c
- New file: Implemented the
umassdriver server entry point (main). - Added command line argument parsing for mounting as rootfs (
-r). - Pops the registered USB driver and runs the driver process.
- New file: Implemented the
- storage/umass/umass.c
- Updated copyright year to 2024 and added author Adam Greloch.
- Removed unused include
limits.h(line 17). - Added includes for
posix/idtree.h,libext2.h(conditional),umass.h, andscsi.h(lines 29-42). - Increased
UMASS_N_MSG_THREADSto 2 and addedUMASS_N_POOL_THREADS(4) (lines 44-45). - Added retry constants
UMASS_TRANSMIT_RETRIESandUMASS_INIT_RETRIES(lines 47-48). - Added logging and debug macros (lines 62-65).
- Defined function prototypes for device access, threads, and filesystem callbacks (lines 89-111).
- Added structures for filesystem registration (
umass_fs_t), partitions (umass_part_t), and request queue (umass_req_t) (lines 112-136). - Updated
umass_devstruct to useidnode_t, includeusb_driver_t *drv, and embedumass_part_t part(lines 140-155). Removed linked list pointers (prev,next),id,partOffset,partSize(lines 144, 153). - Updated
umass_commonstruct to useidtree_t devices, addrqueue,rlock,rcond,mount_root, andrbtree_t fss(lines 160-170). Updated stack sizes (lines 173-176). RemovedlastId(line 165). - Refactored
umass_transmitto_umass_transmitand added retry logic and SCSI sense request handling (lines 217-286). - Added SCSI specific functions
umass_scsiTest,umass_scsiRequestSense,umass_scsiInquiry, and_umass_scsiInit(lines 316-390). - Refactored
umass_checkto_umass_check(line 393). - Added filesystem registration function
umass_registerfs(conditional) and comparison functionumass_cmpfs(lines 185-211). - Added
umass_getfsfunction (lines 428-435). - Renamed
umass_readtoumass_readFromDevandumass_writetoumass_writeToDev(lines 458, 490). Added wrapper functionsumass_readandumass_writethat lookup device by id (lines 537, 549). - Refactored
umass_devFindto_umass_devFind(line 531). - Added mount related functions
umass_mountFromDevandumass_mount(lines 561, 607). - Added thread functions
umass_fsthrandumass_poolthrfor handling filesystem messages (lines 619, 656). - Updated
umass_msgthrto handlemtMountmessage type and use the new read/write functions (lines 721-733). - Refactored
umass_devAllocto_umass_devAllocusing idtree (line 759). Added_umass_devFree(line 751). - Added
umass_mountRootfunction (conditional) (line 789). - Refactored
umass_handleInsertionto takeusb_driver_tandusb_event_insertion_t, use new dev alloc/free, add SCSI init, and add root mount logic (lines 812-904). - Refactored
umass_handleDeletionto takeusb_driver_tand use idtree iteration (lines 907-932). - Removed the
mainfunction (lines 438-496). - Added
umass_init,umass_destroy,umass_driverstruct, andumass_registerconstructor (lines 941-1040).
- storage/umass/umass.h
- New file: Added common header for UMASS driver.
- Defined
umass_args_tstruct for driver arguments.
- tty/usbacm/Makefile
- Updated copyright year to 2024 and added author Adam Greloch.
- Split
usbacminto a static librarylibusbdrv-usbacmand a binaryusbacm. - Added
srv.cto the sources for theusbacmbinary.
- tty/usbacm/srv.c
- New file: Implemented the
usbacmdriver server entry point (main). - Pops the registered USB driver and runs the driver process.
- New file: Implemented the
- tty/usbacm/usbacm.c
- Updated copyright year to 2024 and added author Adam Greloch.
- Removed
USBACM_N_UMSG_THREADSdefine (lines 39-41). - Added
USBACM_SET_LINE_DEFAULT_RATEdefine (lines 55-57). - Updated
usbacm_devstruct to includeusb_driver_t *drvandusb_cdc_line_coding_t line(lines 97, 99). Removed linked list pointers (prev,next) (line 94). - Updated
usbacm_commonstruct to removeustackand addusbacm_dev_t *devicesToFree(lines 104, 110). - Updated
_usbacm_rxStopto usedev->drvforusb_transferAsync(line 156). - Added
usbacm_setLinefunction (lines 266-277). - Updated
usbacm_handleCompletionto takeusb_driver_t *drvand returnint(line 280). Useddrvforusb_transferAsync(line 329). - Updated
_usbacm_rxStartto usedev->drvforusb_transferAsync(lines 350, 356). - Updated
usbacm_writeto usedev->drvforusb_transferBulk(line 425). - Updated
usbacm_devAllocto add the device to the list prematurely and initializerfcnt(lines 454-456). - Updated
_usbacm_urbsAllocto usedev->drvforusb_urbAllocandusb_urbFree(lines 469, 475, 478, 482). - Updated
_usbacm_closeto usedev->drvforusb_urbFree(lines 530, 539). - Updated
usbacm_handleInsertionto takeusb_driver_t *drvandusb_event_insertion_t *event(line 662). Useddrvforusb_modeswitchHandle(line 675) andusb_open(lines 685, 699, 705, 712). Added call tousbacm_setLine(line 733). Populatedeventfields (lines 763-765). Added error handling and device removal on failure (lines 684-759). - Updated
usbacm_handleDeletionto takeusb_driver_t *drv(line 771). Usedusbacm_common.devicesToFree(line 797). Added mutex lock/unlock (lines 781, 807). - Removed
usbthrfunction (lines 744-775). - Removed the
mainfunction (lines 777-826). - Added
usbacm_init,usbacm_destroy,usbacm_driverstruct, andusbacm_registerconstructor (lines 813-878).
- usb/ehci/Makefile
- Updated copyright year to 2024 and added author Adam Greloch.
- Updated
LOCAL_SRCSfor the PHY file name to include target family (phy-$(TARGET_FAMILY)-$(TARGET_SUBFAMILY).c) (line 9). - Added conditional compilation flag
EHCI_IMXfor imx targets (lines 11-13).
- usb/ehci/ehci-hub.c
- Updated copyright year to 2024 and added author Adam Greloch.
- Added conditional compilation (
#ifdef EHCI_IMX) forbDeviceProtocolinhub_devDesc(lines 42-47). - Updated
ehci_resetPortto useehci->opbase(line 95), added more detailed reset logic for non-IMX targets (lines 112-131), and added debug logs (lines 98, 127, 130). - Updated
ehci_getPortStatusto useehci->opbase(line 153) and added conditional speed reporting for IMX (lines 187-195). - Updated
ehci_clearPortFeatureto useehci->opbase(line 232). - Updated
ehci_getDescto useehci->baseforhcsparamsaccess (line 350). - Updated
ehci_getHubStatusto useehci->opbase(line 368) and added debug logs (lines 369, 374). - Updated
ehci_roothubReqto useehci->opbaseforusbintraccess (lines 388-389).
- usb/ehci/ehci.c
- Updated copyright year to 2024 and added author Adam Greloch.
- Added include for
stdbool.h(line 26). - Added conditional
EHCI_PERIODIC_SIZEbased onEHCI_IMX(lines 40-44). - Added conditional
ehci_memDmbimplementation for IMX/non-IMX (lines 53-57). - Updated
ehci_startAsyncandehci_stopAsyncto useehci->opbase(lines 65, 68, 77, 79). - Added
ehci_qtdDumpdebug function (lines 174-199). - Updated
ehci_qtdAllocto initializebuf_hi(lines 227, 235, 247) and useEHCI_QH_NBUFS(lines 233, 246). - Updated
ehci_qhAllocto initializebufandbuf_hifields inqh->hw(lines 326-329). - Updated
ehci_irqHandlerto useehci->opbase(lines 551, 553, 559), clearUSBSTS_FRI(line 553), and removed theportscupdate (lines 508-509 in old code). - Added
ehci_printIrqdebug function (conditional) (lines 641-660). - Updated
ehci_irqThreadto print debug IRQ status (conditional) (lines 673-675), handleUSBSTS_SEI(lines 680-684), and clear status bits after handling (lines 681, 688, 695). - Updated
ehci_qtdAddto use1 - dtfor toggle bit logic (line 712). - Updated
ehci_transferEnqueuecondition for data stage (line 773). - Updated
ehci_initto uselog_errorfor error messages (lines 874, 879, 885, 893, 899, 905, 911, 917, 925). Added alignment check forhcd->base(lines 936-940). Setehci->base(line 943). Determined and setehci->opbasebased oncaplengthorEHCI_IMXdefine (lines 945-954). Updated interrupt handling setup (lines 956-962). Added conditional controller halt/wait for non-IMX (lines 969-974). Updated controller reset command and wait (lines 978-980). Updated host mode setting conditional for IMX/non-IMX (lines 982-989). Updated interrupt enable to includeUSBSTS_SEI(line 992). Added conditional frame list size setting for IMX (lines 997-1000). Updated controller run command and wait (lines 1003-1007). Added sleep after config flag (line 1013). Added debug log at the end (line 1017).
- usb/ehci/ehci.h
- Updated copyright year to 2024 and added author Adam Greloch.
- Added debug defines (
EHCI_DEBUG,EHCI_DEBUG_IRQ,EHCI_DEBUG_QTD) and logging macros (log_msg,log_error,log_debug) (lines 17-27). - Updated
EHCI_INTRMASKto includeUSBSTS_SEI(line 44). - Added new
USBCMDdefines (USBCMD_RUN,USBCMD_HCRESET,USBCMD_PSE,USBCMD_LRESET) (lines 46-51). - Added
HCCPARAMS_64BIT_ADDRSdefine (line 127). - Added conditional register enums based on
EHCI_IMX(lines 139-175). - Added
EHCI_QH_NBUFSdefine (line 191). - Updated
qtdstruct to includebuf_hiarray (line 187). - Updated
qhstruct to useEHCI_QH_NBUFSfor buffer arrays and includebuf_hi(lines 203-204). - Updated
ehci_tstruct to includevolatile uint32_t *baseandvolatile uint32_t *opbase(lines 243-244). Added a comment about stack alignment (line 229).
- usb/ehci/phy-armv7a7-imx6ull.c
- Updated
hcd_info_tstruct initialization to use a nestedphystruct for address and clock (lines 38-41). - Updated
phy_initClockandphy_disableClockto access clock viahcd->info->phy.clk(lines 91, 106). - Updated
phy_initto access PHY address viahcd->info->phy.addrfor mmap (line 127).
- Updated
- usb/ehci/phy-armv7m7-imxrt106x.c
- Updated
hcd_info_tstruct initialization to use a nestedphystruct for address and clock (lines 37-40). - Updated
phy_initto access PHY address viahcd->info->phy.addrand HCD address viahcd->info->hcdaddr(lines 107-108). Updated clock access to usehcd->info->phy.clk(line 110).
- Updated
- usb/ehci/phy-armv7m7-imxrt117x.c
- Updated
hcd_info_tstruct initialization to use a nestedphystruct for address and clock for both OTG1 and OTG2 (lines 43-46, 55-58). - Updated
phy_initto access clock viahcd->info->phy.clk(line 172) and PHY/HCD addresses viahcd->info->phy.addrandhcd->info->hcdaddr(lines 178-179).
- Updated
- usb/ehci/phy-ia32-generic.c
- New file: Implemented the EHCI PHY layer for ia32.
- Defined constants for EHCI PCI class/progif and HCCPARAMS EECP offset (lines 28-31).
- Implemented
hcd_getInfoto scan PCI devices for EHCI controllers and populatehcd_info_t(lines 34-97). - Implemented
phy_configto configure PCI device (busmaster, memory space, interrupts) and take controller ownership from BIOS if necessary (lines 100-155). - Implemented
phy_enableHighSpeedDisconnect(empty function) (lines 158-160). - Implemented
phy_initto mmap the HCD base address and callphy_config(lines 163-179).
Using Gemini Code Assist
The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.
Invoking Gemini
You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.
| Feature | Command | Description |
|---|---|---|
| Code Review | /gemini review |
Performs a code review for the current pull request in its current state. |
| Pull Request Summary | /gemini summary |
Provides a summary of the current pull request in its current state. |
| Comment | @gemini-code-assist | Responds in comments when explicitly tagged, both in issue comments and review comments. |
| Help | /gemini help |
Displays a list of available commands. |
Customization
To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.
Limitations & Feedback
Gemini Code Assist is currently in preview and may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments to provide feedback.
You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.
Footnotes
-
Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request introduces substantial and important updates to the USB stack, including a rework of umass and usbacm drivers to a new event API, and enhancements to the EHCI driver with added ia32 support. The changes are largely well-structured and aim for improved robustness and maintainability.
Key improvements include:
- Refactoring of
umassandusbacmdrivers to use a new USB driver registration and process model. - Enhanced error handling in
umass, including SCSI sense data retrieval and command retries. - More sophisticated threading model in
umassusing a request queue and worker pool. - EHCI driver updates to support ia32, including PCI device discovery and ownership handoff, and better distinction between capability and operational registers.
While the core logic appears to be a significant step forward, there are critical areas related to resource management (especially in driver deinitialization) and thread robustness that need to be addressed. These are crucial for the stability expected in a release branch.
Summary of Findings
- Resource Cleanup in Driver
destroyFunctions: Bothumass_destroy(instorage/umass/umass.c) andusbacm_destroy(intty/usbacm/usbacm.c) are stubs or marked as TODO. Their respectiveinitfunctions allocate resources (mutexes, condition variables, ports) and start threads. These must be properly cleaned up in thedestroyfunctions to prevent resource leaks and ensure driver stability, especially if drivers can be unloaded or restarted. This is considered a high-priority issue. - EHCI System Error Handling: In
usb/ehci/ehci.c, the handling forUSBSTS_SEI(System Error Interrupt) includes aTODO cleanup/reset after death. System errors can leave the controller in an undefined state, and robust handling (e.g., attempting a controller reset) is crucial for stability. This is a high-priority concern for a release branch. - Thread Behavior on
mallocFailure: Instorage/umass/umass.c, theumass_fsthrthread continues in a loop without delay ifmallocfails. This could lead to busy-waiting. Adding a short sleep would be beneficial. - Potentially Unused Port in
usbacm: Theusbacm_common.drvportintty/usbacm/usbacm.cappears to be a leftover from the previous driver model and might be unused. If confirmed, it should be removed.
Merge Readiness
This pull request introduces significant and valuable improvements to the USB stack. However, due to the critical nature of the identified issues related to resource management in driver destroy functions and EHCI system error handling, I recommend that these high-severity issues be addressed before merging into the 3.3 release branch. The medium-severity items should also be considered for improvement.
As an AI, I am not authorized to approve pull requests. Please ensure these changes are thoroughly tested and reviewed by other maintainers before merging.
JIRA: RTOS-970 (cherry picked from commit caa8ba1)
JIRA: RTOS-970 (cherry picked from commit c3c46a7)
JIRA: RTOS-970 (cherry picked from commit 3b4ef64)
JIRA: RTOS-970 (cherry picked from commit 53d5c1a)
JIRA: RTOS-970 (cherry picked from commit 2c5dc16)
JIRA: RTOS-970 (cherry picked from commit 7e1b9a5)
JIRA: RTOS-970 (cherry picked from commit 1b340e3)
There is no clear benefit from LIFO processing JIRA: RTOS-970 (cherry picked from commit 073b881)
|
Corrected regression on DTR and reverted the change that introduced the warning. |
anglov
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM +2

Description
Cherry-picks into the 3.3 release branch the following:
Minor device driver fixes:
USB rework, devinfo API addition:
Motivation and Context
Types of changes
How Has This Been Tested?
Checklist:
Special treatment
Backport USB rework and devinfo API addition to 3.3 release phoenix-rtos-usb#37