477 Commits
Author SHA1 Message Date
Justin Hammond 3b7e453012 drivers/usbhost: Take an xHCI command result from its completion event.
xhci_command() returned -ETIMEDOUT when a completion arrived without an
interrupt, although the fallback poll had already retrieved the event,
so callers unwound work the controller had completed.

Use the event's completion code whichever path retrieved it.

Assisted-by: Claude:claude-opus-5
Signed-off-by: Justin Hammond <justin@dynam.ac>
2026-08-17 10:10:47 -03:00
Justin Hammond 440d5f7f87 drivers/usbhost: Mask the write-one-to-clear PORTSC bits on reset.
Eight PORTSC bits are write-one-to-clear, so writing back a value just
read clears PED and every change bit that was set, disabling the port
being reset.  Mask them out using the new XHCI_PORTSC_RW1C.

The wait after reset also decided on its own counter rather than on the
port, reporting a timeout for a port that enabled on the last iteration.
Test PED, and report PORTSC when it does time out.

Assisted-by: Claude:claude-opus-5
Signed-off-by: Justin Hammond <justin@dynam.ac>
2026-08-17 10:10:47 -03:00
Justin Hammond c94fbeebcb drivers/usbhost: Stop an xHCI controller only when it is running.
xhci_ctrl_halt() wrote USBCMD zero unconditionally and then waited for
HCH.  A controller that was never started is already halted, so the wait
ran to its full length, and clearing the whole register also dropped
INTE and HSEE.

Test HCH first, clear only R/S when it is set, bound the wait with
XHCI_HALT_TIMEOUT_MS, and report USBCMD and USBSTS on failure.

Assisted-by: Claude:claude-opus-5
Signed-off-by: Justin Hammond <justin@dynam.ac>
2026-08-17 10:10:47 -03:00
Justin Hammond edb7d03467 drivers/usbhost: Allow an xHCI controller with no scratchpad buffers.
HCSPARAMS2 may report zero scratchpad buffers; QEMU's does.  The driver
sized the array from that count unconditionally and read the NULL from a
zero byte kmm_memalign() as -ENOMEM, so such a controller never started.

Skip the allocation when no_scratch is zero, leaving DCBAA[0] clear.

Assisted-by: Claude:claude-opus-5
Signed-off-by: Justin Hammond <justin@dynam.ac>
2026-08-17 10:10:47 -03:00
Justin Hammond 24132be56e drivers/usbhost: Read xHCI registers at the required access width.
xHCI requires aligned accesses of each register's own size, and a
controller may ignore narrower ones; QEMU's does.  volatile does not pin
the access width: GCC 16.1.0 at -Os narrows a 32-bit load feeding a
single bit test into a byte load, so polling USBSTS for HCH never
observes the halted state.

Launder each register value through a register with an empty asm, on
loads and stores both, so the access is the width the source specifies.

Assisted-by: Claude:claude-opus-5
Signed-off-by: Justin Hammond <justin@dynam.ac>
2026-08-17 10:10:47 -03:00
Justin Hammond b8e26b127e drivers/usbhost: Let the HID keyboard pick its interrupt pipe.
HIDKBD_NOGETREPORT reads keyboard reports with DRVR_ASYNCH(), and that
macro is only defined when USBHOST_ASYNCH is set.  The option selected
neither, so turning it on by itself fails at the call site with no hint
that a second option was meant to come with it.

Select it.  Every in-tree configuration that sets NOGETREPORT already
resolves USBHOST_ASYNCH: ci20:jumbo and sama5d3-xplained:bluetooth
through USBHOST_HUB, and the two linum-stm32h753bi configurations by
setting it directly.  No existing build changes.

The two that set it directly no longer can, since a selected symbol is
no longer settable, so savedefconfig drops the line.  Their defconfigs
are normalized here to keep them canonical.

Assisted-by: Claude:claude-opus-5
Signed-off-by: Justin Hammond <justin@dynam.ac>
2026-08-09 20:57:41 +08:00
Justin Hammond a023f61a38 drivers/usbhost: Do not unregister a HID keyboard that never registered.
usbhost_destroy() unregisters the keyboard unconditionally, and it runs
for a device that never got as far as being registered as well: an
enumeration that failed part way through, or a device unplugged while it
was still being set up.

The upper half does not tolerate that call.  It asserts that the lower
half carries the state keyboard_register() puts there, so a keyboard that
fails to come up takes the system down with an assertion rather than
being cleaned up and forgotten.  Seen on a low speed keyboard that
attaches and then does not finish enumerating.

The state the registration leaves behind is what says whether there is
anything to undo, so look at it first.

Assisted-by: Claude:claude-opus-5
Signed-off-by: Justin Hammond <justin@dynam.ac>
2026-08-09 20:57:41 +08:00
Justin Hammond d95d8c0fb1 usbhost: Report each device as it is enumerated.
A host that enumerates a device says nothing about it unless the whole of
CONFIG_DEBUG_USB_INFO is on, and then it says a great deal else besides.
The quietest case is the one that matters most: a device no class driver
claims produces no output at all, so a user with an unsupported device
sees exactly what a user with no device sees.

Add CONFIG_USBHOST_ANNOUNCE, reporting each device once, in the shape a
reader is likely to recognise from other systems: where it is, what it is,
its vendor, product and release, and the maker, product and serial number
it reports in its own string descriptors.  Those cost a control transfer
each, so they are read only where a report was asked for, and only once
the device is addressed.

The report is made after binding rather than from within it, because a
composite device never reaches the class lookup: usbhost_composite() is
tried first and binds it.  Whether a driver claimed the device is tracked
rather than read from the returned status, which the per interface loop
sets to OK whatever happened.

The port is given as the path from the root hub, and the path names the
bus, because a device on the first port of a hub and one on the first port
of a controller are otherwise reported identically.  struct
usbhost_roothubport_s gains that bus number for the purpose; a driver that
does not set it reports zero, which is the only bus it has.

Class codes are translated where a name is more use than a number, which
includes the HID boot protocols, so a keyboard is reported as a keyboard.

Default n, so no existing configuration changes.

Assisted-by: Claude:claude-opus-5
Signed-off-by: Justin Hammond <justin@dynam.ac>
2026-08-09 02:09:47 +08:00
Justin Hammond 8279a7e755 drivers/usbhost: Refuse to register the same class driver twice.
The registry is a singly linked list of static structures, so registering
one of them a second time does not add a second entry: it points that
entry's own link at itself, and the list stops having an end.

Nothing notices while every device that turns up matches something near
the head, because the search returns before it reaches the loop.  The
first device that matches nothing at all, meaning anything without a
class driver built in, walks the list to look for it and never comes back,
holding the registry lock.  On a multiprocessor the rest of the system
follows it down: every other processor that touches the registry spins,
and on the one measured here that included the console, so a board with a
USB keyboard and no keyboard driver came up and then answered nothing.

Registering twice is easy to do by accident.  drivers_initialize() calls
usbhost_drivers_initialize(), which registers every class the
configuration selected, and board code that also registers one, which
many boards do, gets a second call for free.

So look before linking, and treat a repeat registration as the no-op the
caller expected it to be.

Assisted-by: Claude:claude-opus-5
Signed-off-by: Justin Hammond <justin@dynam.ac>
2026-08-08 14:55:34 -03:00
Justin Hammond dd1b57577c drivers/usbhost: Correct the xHCI controller name in a trace string.
The control transfer trace entry names the controller "HXCI", so a log of
an enumeration reads as though a different controller were involved.  One
neighbouring entry also spells the port "RHport" where the rest of the
table spells it "RHPort".

Text only.  No trace identifier or argument changes.

Assisted-by: Claude:claude-opus-5
Signed-off-by: Justin Hammond <justin@dynam.ac>
2026-08-08 14:55:00 -03:00
Justin Hammond 4e7703792f drivers/usbhost: Define the xHCI endpoint-allocation trace.
The xHCI driver traces endpoint allocation with XHCI_VTRACE2_EPALLOC, but
that id is in neither the enumeration nor the string table, so the driver
does not compile once verbose tracing is turned on:

  usbhost_xhci_pci.c:3285: 'XHCI_VTRACE2_EPALLOC' undeclared

It builds today only because usbhost_vtrace2() collapses to a macro that
discards its arguments unless HAVE_USBHOST_TRACE_VERBOSE is defined, which
is what stops anyone finding this until they go looking for a trace.

Add the id and the string to match, keeping the two tables in step.

Assisted-by: Claude:claude-opus-5
Signed-off-by: Justin Hammond <justin@dynam.ac>
2026-08-08 14:55:00 -03:00
Justin Hammond 50f93a986a drivers/usbhost: Register the partitions on a mass storage device.
A drive that has been anywhere near another operating system almost
always carries a partition table rather than a filesystem starting at
sector zero, so the single block device this driver registers is usually
the one thing nobody can mount.  A USB stick written with an installer
image is a good example: sector zero holds a protective MBR, and what
somebody wants is the EFI system partition several gigabytes in.

Read the table and give each partition a block device of its own beside
the whole drive, named the way every other system names them.  The
parsing is already in the tree and understands both MBR and GPT; this
only calls it and registers what it finds.

The whole-drive node stays exactly where it was, for anyone who wants
the raw thing or whose drive really does hold a bare filesystem.

Assisted-by: Claude:claude-opus-5
Signed-off-by: Justin Hammond <justin@dynam.ac>
2026-08-08 14:40:52 -03:00
Jorge Guzman f0a870baab drivers/usbhost: report the HID keyboard through the keyboard upper half
The driver kept a character device, a ring buffer, a poll waiter list
and an encoder of its own, in parallel with everything the keyboard
upper half already provides.  A USB keyboard was therefore the one
keyboard an application could not read like any other.

Register with keyboard_register() and report with keyboard_event(),
which removes the private character device and the four hundred lines
that served it.  Special keys are reported with the SPEC event types
carrying a keycode, so an application no longer has to guess whether a
value in the character range is a character or an arrow key.

HIDKBD_ENCODED and HIDKBD_NODEBOUNCE go away with the code they guarded.
Encoding is now inherent to the event, and the previous report is no
longer an optimisation:  a HID keyboard reports the keys that are down
rather than the transitions, so it is what tells a new press from a key
still held, and what tells that a key has been released.

Reporting the modifiers as keys is new, so it is behind
HIDKBD_REPORT_MODIFIERS and off by default.

Signed-off-by: Jorge Guzman <jorge.gzm@gmail.com>
2026-07-31 11:02:20 -03:00
Ansh Rai 54b0066a69 libs/libc: Fix divide-by-zero in stat() with large filesystem block sizes
blksize_t is currently defined as int16_t, which overflows when a
filesystem reports a block size larger than 32767 bytes. This causes
st_blksize to become zero, leading to an integer divide-by-zero when
st_blocks is calculated in stat().

Widen blksize_t to int32_t to support larger filesystem block sizes.

Update nuttx_blksize_t in include/nuttx/fs/hostfs.h to keep it
consistent with include/sys/types.h.

struct geometry.geo_sectorsize (include/nuttx/fs/ioctl.h) is also
typed blksize_t, so every debug print of that field using a 16-bit
format specifier is updated to PRId32 to match the new width:
drivers/misc/ramdisk.c, drivers/mmcsd/mmcsd_spi.c, drivers/mtd/ftl.c,
fs/driver/fs_blockmerge.c, drivers/mtd/smart.c,
drivers/usbhost/usbhost_storage.c, drivers/mmcsd/mmcsd_sdio.c,
arch/arm/src/s32k1xx/s32k1xx_eeeprom.c,
arch/arm/src/lc823450/lc823450_mmcl.c.

Signed-off-by: Ansh Rai <anshrai331@gmail.com>
Signed-off-by: root <root@LAPTOP-9C7LKDC5.localdomain>
2026-07-16 23:37:22 +08:00
Catalin Visinescu 081e4c478a drivers/: Multiple Drivers Are Registered With World Writable - Part 2
MemBrowse Memory Report / changes-filter (push) Has been cancelled
MemBrowse Memory Report / load-targets (push) Has been cancelled
MemBrowse Memory Report / identical (push) Has been cancelled
MemBrowse Memory Report / analyze (push) Has been cancelled
Permissions (Part 2)

Description:

In kernel builds, any unprivileged process running on the NuttX
device can open /dev/efuse and attempt to read/write fuse content.
Reading the fuses may provide valuable information to an attacker
controlling the user process. The write operation, in extreme cases
where the fuse blocks are not locked, may brick the device.

DISCLAIMER: I tried to be strict with the settings, better to relax them
later if it's needed.

This is part of https://github.com/apache/nuttx/issues/19410

See https://github.com/apache/nuttx/issues/19410

Compiles ok.

Signed-off-by: Catalin Visinescu <catalin_visinescu@yahoo.com>
2026-07-15 15:27:28 +08:00
Lwazi Dube 2b0c59bcf4 drivers/usbhost_hidmouse: fix button detection in touchscreen example
Fix an issue where button presses were missed in the touchscreen
example due to incorrect packet processing.

Previously, the driver waited to accumulate a batch of packets but only
processed the first one, effectively discarding the rest. The driver
now reads and processes packets one at a time to ensure no input
events are lost.

Also fixed typo: usbhost_xythreshold does not exist.

Signed-off-by: Lwazi Dube <lwazeh@gmail.com>
2026-05-22 10:38:25 +08:00
daniellizewski fe67544d5f drivers/usbhost/usbhost_cdcecm.c: Converted CDC-ECM to lower half
Converted USB Host CDC-ECM to a lower half driver.
Removed existing full network driver callbacks and replaced with lower
half.

Signed-off-by: daniellizewski <daniellizewski@geotab.com>
2026-05-06 06:20:03 +08:00
daniellizewski 9b18160893 drivers/usbhost/usbhost_cdcecm.c: Added support for Host CDC-ECM
Added support for USB host to use an USB CDC-ECM device.
This class is used for usb-ethernet adapters as well as many modems.

Signed-off-by: daniellizewski <daniellizewski@geotab.com>
2026-05-06 06:20:03 +08:00
daniellizewski 761c17f1e0 drivers/usbhost/usbhost_enumerate.c: Allow selecting USB configuration
Added support for selecting a different USB configuration.
Certain USB devices offer different classes using different
configurations. This allows a board file to provide a callback
to select the proper configuration for a given USB device.

Signed-off-by: daniellizewski <daniellizewski@geotab.com>
2026-05-06 06:20:03 +08:00
daniellizewski 1632beaede arch/arm/src/stm32h5/stm32_usbdrdhost.c: Fixed Hub support
Fixed a few bugs in the initial stm32_usbdrdhost.c implementation
when using a USB hub. Fixed fault when freeing control endpoints.
Fixed crash when disconnecting devices from hubs due ot use after free.

Signed-off-by: daniellizewski <daniellizewski@geotab.com>
2026-04-19 14:24:40 +08:00
Piyush Patle 0dccc8ba21 include/debug.h: Move to include/nuttx/debug.h
debug.h is a NuttX-specific, non-POSIX header. Placing it in the
top-level include/ directory creates naming conflicts with external
projects that define their own debug.h.
This commit moves the canonical header to include/nuttx/debug.h,
following the NuttX convention for non-POSIX/non-standard headers,
and updates all in-tree references.

A backward-compatibility shim is left at include/debug.h that
emits a deprecation #warning and re-includes <nuttx/debug.h>,
allowing out-of-tree code to continue building while migrating.

Signed-off-by: Piyush Patle <piyushpatle228@gmail.com>
2026-04-07 07:50:06 -03:00
Peter Barada efdbdfe409 arch/arm/stm32*: Use PRIx32 format specifier where appropriate
Replace 'x' printf format specifier with PRIx32 where
corresponding value is uint32_t type.

Signed-off-by Peter Barada <peter.barada@gmail.com>
2026-04-01 13:48:58 +08:00
Huang Qi e3eeaefd6d style: Fix "the the" typo across the codebase.
Fix 269 occurrences of duplicate "the" word typo found in 209 files
across source code, header files, and configuration.

Signed-off-by: Huang Qi <huangqi3@xiaomi.com>
2026-03-23 11:07:49 +01:00
zhanghongyu a7567677a8 netdev_driver: add carrier_on to xxx_ifup where carrier_on is absent
since the judgment for network card selection was changed from IS_UP to
IS_RUNNING, drivers that lack carrier_on need to add the carrier_on
operation; otherwise, network access issues will occur.

Signed-off-by: zhanghongyu <zhanghongyu@xiaomi.com>
2026-02-02 13:23:05 +08:00
Alan Carvalho de Assis 0e5323370f drivers/usbhost_hidkbd: Fix small typo in usbhost_hidkbd.c
Build Documentation / build-html (push) Has been cancelled
There were missing quotes for key "backspace" for scancodes.
This issue was found by github user @StagiaireAbritek

He opened an PR in github to fix it:
https://github.com/apache/nuttx/pull/15917

However he decided to abandon and close the PR, but because it is
an important fix I decided submit it upstream.

Signed-off-by: Alan C. Assis <acassis@gmail.com>
2026-01-18 11:57:58 -05:00
zhanghongyu fa6c8579dd driver/net: replace net_lock with netdev_lock
modify the code of the adapted protocol stack to avoid deadlocks and the
logic that cannot be protected by locks after modification.

Signed-off-by: zhanghongyu <zhanghongyu@xiaomi.com>
2026-01-06 16:08:30 +08:00
yangsong8 a87ad98343 usbhost: cdcacm exit wq after copy all received data
Build Documentation / build-html (push) Has been cancelled
Fix issue: When the SIM CDCACM host receives data, the second packet
data reception is incorrect.

Signed-off-by: yangsong8 <yangsong8@xiaomi.com>
2025-12-14 10:37:53 -03:00
yangsong8 f2191a7e3b drivers/usbhost: fix build waring
usbhost/usbhost_hidkbd.c:1373:14: warning: unused variable ‘flags’ [-Wunused-variable]
 1373 |   irqstate_t flags;
      |              ^~~~~

Signed-off-by: yangsong8 <yangsong8@xiaomi.com>
2025-12-13 13:03:52 +08:00
yangsong8 2660df6581 drivers/usbhost: use small lock to protect usbhost kbd
replace critical_section with spinlock

Signed-off-by: yangsong8 <yangsong8@xiaomi.com>
2025-12-13 13:03:52 +08:00
yangsong8 25ec00c67c drivers/usbhost: improve usbhost hid kbd code
By passing parameters to the kthread_create process, reduce the global
variables and nxmutex lock.

Signed-off-by: yangsong8 <yangsong8@xiaomi.com>
2025-12-13 13:03:52 +08:00
yangsong8 a7af894cd3 driver/usbhost: use small lock to protect usbhost cdcmbim
replace critical_section with spinlock

Signed-off-by: yangsong8 <yangsong8@xiaomi.com>
2025-12-13 12:39:48 +08:00
yangsong8 23cec99002 drivers/usbhost: use small lock to protect usbhost cdcacm
replace critical_section with spinlock

Signed-off-by: yangsong8 <yangsong8@xiaomi.com>
2025-12-13 12:39:48 +08:00
yangsong8 62f7a8fa22 drivers/usbhost: use small lock to protect usbhost bthci
replace critical_section with spinlock

Signed-off-by: yangsong8 <yangsong8@xiaomi.com>
2025-12-13 12:39:48 +08:00
yangsong8 5571ec8fa2 drivers/usbhost: use small lock to protect usbhost msc
replace critical_section with spinlock

Signed-off-by: yangsong8 <yangsong8@xiaomi.com>
2025-12-13 12:39:48 +08:00
yangsong8 b9b62a9204 drivers/usbhost: Use small lock to protect usbhost common function
replace critical_section with spinlock

Signed-off-by: yangsong8 <yangsong8@xiaomi.com>
2025-12-13 12:39:48 +08:00
yangsong8 7907a5b1c3 driver/usbhost: Support obtaining USB3.0 device descriptors
usbhost supports USB 3.0 device descriptors.

Signed-off-by: yangsong8 <yangsong8@xiaomi.com>
2025-12-09 08:57:28 -05:00
Alan Carvalho de Assis 8cb1fd713e drivers/usbhost_hub: Add support to Multiple TT HS HUB
This is the case for USB2517 USB HUB. Also improved the driver to
avoid mistakes: initially I changed g_id[3], but the in register
there is another field where we need to pass this size again. So
it is better to use ARRAY_SIZE() macro to avoid mistakes.

Signed-off-by: Alan C. Assis <acassis@gmail.com>
2025-10-27 18:24:42 +08:00
chao an 87f134cfaa sched/sleep: replace all Signal-based sleep implement to Scheduled sleep
Nuttx currently has 2 types of sleep interfaces:

1. Signal-scheduled sleep: nxsig_sleep() / nxsig_usleep() / nxsig_nanosleep()
Weaknesses:
a. Signal-dependent: The signal-scheduled sleep method is bound to the signal framework, while some driver sleep operations do not depend on signals.
b. Timespec conversion: Signal-scheduled sleep involves timespec conversion, which has a significant impact on performance.

2. Busy sleep: up_mdelay() / up_udelay()
Weaknesses:
a. Does not actively trigger scheduling, occupy the CPU loading.

3. New interfaces: Scheduled sleep: nxsched_sleep() / nxsched_usleep() / nxsched_msleep() / nxsched_ticksleep()
Strengths:
a. Does not depend on the signal framework.
b. Tick-based, without additional computational overhead.

Currently, the Nuttx driver framework extensively uses nxsig_* interfaces. However, the driver does not need to rely on signals or timespec conversion.
Therefore, a new set of APIs is added to reduce dependencies on other modules.

(This PR also aims to make signals optional, further reducing the code size of Nuttx.)

Signed-off-by: chao an <anchao.archer@bytedance.com>
2025-10-17 14:05:02 +08:00
simbit18 db26ef1bd2 drivers/usbhost: Aligned Cmake with Make
Add:

- Bluetooth HCI Driver #11552

Signed-off-by: simbit18 <simbit18@gmail.com>
2025-10-09 18:20:36 -04:00
Lars Kruse 3ce85ca54e style: fix spelling in code comments and strings 2025-05-23 10:48:41 +08:00
p-szafonimateusz fab4f68b6f drivers/usbhost: add xHCI support
add xHCI PCI driver (usbhost).

Signed-off-by: p-szafonimateusz <p-szafonimateusz@xiaomi.com>
2025-05-15 21:58:57 +08:00
p-szafonimateusz 3dc1ac91d6 usbhost: add common usb host waiter and drivers initialization
add common usb host waiter and drivers initialization which is
required for xHCI support

Signed-off-by: p-szafonimateusz <p-szafonimateusz@xiaomi.com>
2025-05-15 21:58:57 +08:00
Lars Kruse 4568110d63 fix misspelled names in locally scoped code
These misspelled words are used in strictly local scopes.
Renaming these variables should not cause any problems.
2025-05-15 10:12:12 +08:00
yangsong8 7bad94a964 drivers/usbhost: recognize sim usb cdcacm composite device
In the USB interface descriptor of the cdcacm device, class:2 (CDC)
subclass:2 (ACM) protocol:0 (NONE) may appear. At this time, the
usb host does not match the correct protocol byte, which will cause
the device identification to fail.

Signed-off-by: yangsong8 <yangsong8@xiaomi.com>
2025-03-05 10:11:29 +01:00
Alin Jerpelea 35a4fc05d9 drivers/usbhost/hid_parser: migrate to SPDX identifier
Most tools used for compliance and SBOM generation use SPDX identifiers
This change brings us a step closer to an easy SBOM generation.

Signed-off-by: Alin Jerpelea <alin.jerpelea@sony.com>
2024-12-18 17:51:57 +08:00
Alin Jerpelea 286d37026c drivers: migrate to SPDX identifier
Most tools used for compliance and SBOM generation use SPDX identifiers
This change brings us a step closer to an easy SBOM generation.

Signed-off-by: Alin Jerpelea <alin.jerpelea@sony.com>
2024-11-06 18:02:25 +08:00
Petro Karashchenko d499ac9d58 nuttx: fix multiple 'FAR', 'CODE' and style issues
Signed-off-by: Petro Karashchenko <petro.karashchenko@gmail.com>
2024-08-25 19:22:15 +08:00
Petro Karashchenko 7b18f9d19f nuttx: add missing 'FAR' and fix style issues
Signed-off-by: Petro Karashchenko <petro.karashchenko@gmail.com>
2024-08-25 19:22:15 +08:00
Petro Karashchenko ebddc8d8ee drivers/usbhost: remove unused function
Signed-off-by: Petro Karashchenko <petro.karashchenko@gmail.com>
2024-08-25 19:22:15 +08:00
zhanghongyu 4e79741e7d usbdev: add ncm driver
The compilation and verification commands are shown below:
./tools/configure.sh sim:usbdev
make -j
sudo ./nuttx
nsh> conn 2
nsh> dhcpd_start eth1

Signed-off-by: zhanghongyu <zhanghongyu@xiaomi.com>
2024-08-21 02:01:01 +08:00