Commit graph

800 commits

Author SHA1 Message Date
Johann Fischer b0d7d70834 usb: device_next: add initial BOS support
Use the same scheme as for string descriptors. Descriptors can be added
or removed using the existend interface.

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
2024-05-17 16:25:02 +01:00
Johann Fischer 29003ff264 usb: bos: cleanup Binary Device Object Store header
We could reuse the BOS header, but there are parts that are only needed
in the legacy device support or used internally and the tests. Move this
parts to the appropriate places.

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
2024-05-17 16:25:02 +01:00
Johann Fischer 4f17bc6051 usb: device_next: store bDescriptorType and bLength in descriptor node
It simplifies the macros to create the string descriptors. We also no
longer need placeholders for the SerialNumber descriptor when it is
generated from hwid. In the future, we can store other descriptor types
in the same list.

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
2024-05-17 16:25:02 +01:00
Johann Fischer 9b8796400a usb: device_next: rework SerialNumber generation from HWINFO
Do not use application-provided placeholder buffers. Generate
SerialNumber just before ASCII string is converted to UTF16LE. Make
dependency on HWINFO optional, but enabled by default.

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
2024-05-17 16:25:02 +01:00
Johann Fischer 48ca72326b usb: device_next: move string descriptor data to a separate structure
Move and rework string descriptor data to a separate structure so we can
share memory with another type. Also add description to the
USBD_DESC_STRING_DEFINE macro.

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
2024-05-17 16:25:02 +01:00
Johann Fischer 2f31ee63b5 usb: device_next: convert ASCII7 strings to UTF16LE on the fly
This is slower but should have no real effect. In the future we can
support more than ASCII7 format and store it in other memory areas.

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
2024-05-17 16:25:02 +01:00
Johann Fischer f110766072 usb: device_next: add few more string descriptor helpers
Add function to get string descriptor index and function
to remove linked descriptor from a device. This abstracts
it a bit so that the user does not need to know how it is
handled internally.

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
2024-05-17 16:25:02 +01:00
Johann Fischer 4c4ff20383 usb: device_next: prevent recursive logging loop
Apply the commit 21975231e2
("usb: device: cdc_acm: Prevent recursive logging loop")
to the new CDC ACM implementation.

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
2024-05-16 16:23:07 +01:00
Johann Fischer eb9b46c118 usb: device_next: lock scheduler in usbd_init()
Lock the scheduler to ensure that the context is not preempted before it
is fully initialized.

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
2024-05-15 17:29:24 +01:00
Johann Fischer c3ec41bdf8 usb: device_next: remove unused API function
Remove unused and not required usbd_ep_ctrl_buf_alloc().

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
2024-05-15 17:29:24 +01:00
Johann Fischer 4c360f38d1 usb: device_next: add controller can detect VBUS state change capability
There are controllers with capability to detect VBUS state change. This
can be used in a generic application to explicitly handle the VBUS
detected event after usbd_init() and for example allow the PMIC to
detect to what type of port device controller is connected.

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
2024-05-15 17:29:24 +01:00
Johann Fischer 48f2a4bc1a usb: device_next: remove initialized state checks in event processing
For the simple events, do not check whether the device driver and stack
are marked as initialized. USB device notification will reschedule
delivery if the stack is not yet marked initialized.

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
2024-05-15 17:29:24 +01:00
Johann Fischer 693191467d usb: device_next: add USB device context to message delivery callback
Add USB device context argument to message delivery callback.

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
2024-05-15 17:29:24 +01:00
Johann Fischer 22d574cb3c usb: device_next: tweak USB device notification support
Since no threads are waiting for the FIFO and we do not need any k_fifo
features, we can just use slist.

Some notifications (caused by interrupts), especially "VBUS ready", can
be triggered very quickly right after UDC initialization if the USB
controller is already connected. Depending on the global thread
configuration, messsage publishing work can be executed before the
context is marked as initialized. Use delayed work to reschedule
publishing when the context is not initialized.

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
2024-05-15 17:29:24 +01:00
Johann Fischer c0e8f0d96b usb: device_next: add initial HID device support
Add initial HID device support. Unlike the existing HID implementation,
the new implementation uses a devicetree to instantiate a HID device.
To the user, the HID device appears as a normal Zephyr RTOS device.

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
2024-05-14 18:24:45 -04:00
Johann Fischer 92fc5316a4 usb: device_next: allow Get Descriptor with recipient interface
If the recipient is not the device then it is probably a
class specific  request where wIndex is the interface
number or endpoing and not the language ID. e.g. HID
Class Get Descriptor request.

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
2024-05-14 18:24:45 -04:00
Johann Fischer 3eec2a56d5 usb: device: get rid of the partial initialization called by SYS_INIT
Part of the initialization is done in the static usb_device_init()
function called by SYS_INIT(). We can move part of it, which initialize
the descriptor and calls usb_set_config(), to usb_enable(). This allow
some usb_get_device_descriptor() dependencies called at application
desired point.

Also make sure that usb_fix_descriptor() is called only once.

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
2024-05-08 12:00:46 +02:00
Tomasz Moń 3850c92570 usb: device_next: Properly store enumeration speed
Add missing break inside switch statement to allow UDC context to
indicate that current operating speed is High-Speed. Due to missing
break statement, the stack always assumed Full-Speed.

Signed-off-by: Tomasz Moń <tomasz.mon@nordicsemi.no>
2024-05-07 15:20:13 -05:00
Fin Maaß 7d83a8a68a lib: smf: constant number of arguments for SMF_CREATE_STATE()
This sets the number of arguments for SMF_CREATE_STATE() to always
be the same, independent of the selected Kconfig options.

Signed-off-by: Fin Maaß <f.maass@vogl-electronic.com>
2024-05-07 17:46:21 +02:00
Tomasz Moń 2f24adbe0f usb: device: Do not claim to be USB 3.2 device
The bcdUSB value 0x0210 defined in USB 3.2 Specification indicates USB
3.2 device operating in one of the USB 2.0 modes. USB 2.0 Link Power
Management Addendum defines bcdUSB value 0x0201 to indicate that USB 2.0
device supports the request to read the BOS Descriptor.

The main difference between bcdUSB 0x0210 and 0x0201 is that the USB 3.2
device must support LPM, while USB 2.0 devices can (but are not required
to) support LPM.

The difference is respected by USB 3 Gen X Command Verifier (2.3.0.0)
Chapter 9 Tests [USB 2 devices], where the test behaves as follows:
  * For bcdUSB 0x0200:
      Checking Device Under Test for LPM L1 Compatibility...
      USB version of device is 2.00.
      DUT is NOT compatible with LPM.
      LPM is NOT required for DUT
      LPM is only supported in USB version 2.01 and above.

  * For bcdUSB 0x0201:
      Checking Device Under Test for LPM L1 Compatibility...
      USB version of device is 2.01.
      DUT IS compatible with LPM.
      LPM is NOT required for DUT
      USB 2.0 Extension Descriptor bmAttributes:
        LPM Capable = 0
        BESL and Alternate HIRD Supported = 0
        Baseline BESL Valid = 0
        Deep BESL Valid = 0
        Baseline BESL: 0d
        Deep BESL:  0d
      LPM is not supported

  * For bcdUSB 0x0210:
      Checking Device Under Test for LPM L1 Compatibility...
      USB version of device is 2.10.
      DUT IS compatible with LPM.
      LPM IS required for DUT
      USB 2.0 Extension Descriptor bmAttributes:
        LPM Capable = 0
        BESL and Alternate HIRD Supported = 0
        Baseline BESL Valid = 0
        Deep BESL Valid = 0
        Baseline BESL: 0d
        Deep BESL:  0d
      (USB: 9.6.2.1.6) Bit 1 in Attributes field of a USB 2.0 Extension
      descriptor returned in response to a GetDescriptor(BOS) request
      must be 1 for LS/FS/HS devices that support LPM L1.

The test fails when LPM bit is not set in USB 2.0 Extension Descriptor
only when bcdUSB is 0x0210. The test failure was incorrectly fixed in
commit 312429be3c ("usb: samples: Add Extension descriptor to webUSB
sample."). Properly fix the issue by changing bcdUSB to 0x0201 and
removing the false LPM support claim.

The false LPM claim was leading to device ceasing to work after some
time if there was no traffic from host to device (when the host is
likely to have executed the LPM L1 transition that was not properly
handled by the device).

Signed-off-by: Tomasz Moń <tomasz.mon@nordicsemi.no>
2024-05-06 14:54:23 +01:00
Johann Fischer 215b4ddf98 usb: device_next: rework USBD shell to take speed into account
Rework USBD shell to take speed arguments into account and revise
command help text. Rename some of the commands to better reflect what
they actually do.
Since most of the init code changes, rewrite the command to initialize
the default configuration in the shell to register all available
classes, but not to enable USB device support in a single command.
This will allow earlier notification in the future, if supported by the
controller, before the USB device controller is enabled.

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
2024-04-22 06:51:22 -07:00
Johann Fischer 0cfc15da2e usb: device_next: rename usbd_class_iter to usbd_class_node
For the last dozen commits, the role of usbd_class_node has actually
been taken over by usbd_class_iter. After cleaning up and merging
usbd_class_node and usbd_class_data, we can rename usbd_class_iter to
usbd_class_node to better reflect its role.

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
2024-04-22 06:51:22 -07:00
Johann Fischer 2bc55f17ec usb: device_next: rename usbd_class_node to usb_class_data
Since only usbd_class_node contains the class instance data, rename it
to usbd_class_data.

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
2024-04-22 06:51:22 -07:00
Johann Fischer 9acd1e877d usb: device_next: temporarily remove struct usbd_class_data
With the latest change, there are two structures that actually contain
class instance related data. Merge them into usbd_class_dnode, because
there is already a handy macro that can be used in class instances.

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
2024-04-22 06:51:22 -07:00
Johann Fischer cc36727b95 usb: device_next: use helpers to get priv and device context
Some places have been overlooked, finally get rid of it. Use helpers to
get instance priv and device context.

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
2024-04-22 06:51:22 -07:00
Tomasz Moń 5144d0f65f usb: device_next: Introduce speed specific configurations
USB High-Speed devices must be able to operate at both High-Speed and
Full-Speed. The USB specification allows the device to have different
configurations depending on connection speed. Modify the API to reflect
USB Specification requirements on what can (e.g. configurations) and
what cannot (e.g. VID, PID) be speed dependent.

While the class configurations for different speeds are completely
independent, the actual class instances are shared between operating
speeds (because only one speed can be active at a time). Classes are
free to provide different number of interfaces and/or endpoints for
different speeds. The endpoints are assigned for all operating speeds
during initialization.

Signed-off-by: Tomasz Moń <tomasz.mon@nordicsemi.no>
2024-04-22 06:51:22 -07:00
Tomasz Moń 99cd7ee991 usb: device_next: Fix uninitialized variable warning
Assign default value to prevent possible uninitialized variable warning.

Signed-off-by: Tomasz Moń <tomasz.mon@nordicsemi.no>
2024-04-22 06:51:22 -07:00
Tomasz Moń 2470821dc2 usb: device_next: Allow class to return NULL descriptor set
Simply skip over the class if it returns NULL descriptor set for given
speed.

Signed-off-by: Tomasz Moń <tomasz.mon@nordicsemi.no>
2024-04-22 06:51:22 -07:00
Tomasz Moń f411f801fe usb: device_next: Separate endpoint assignment from class data
Introduce usbd_class_iter for keeping endpoint assignment variables
and the single-linked list node. No functional changes right now, but
this paves the way for independent speed specific configurations.

Signed-off-by: Tomasz Moń <tomasz.mon@nordicsemi.no>
2024-04-22 06:51:22 -07:00
Tomasz Moń 2deab70f0f usb: device_next: Handle class requests the same regardless of debug
When device is not configured, any class or endpoint request must result
in request error. Do not try to find transfer owner because there cannot
be one.

Make sure that the actual handling code path does not depend on the
configured log level because it is absolutely not desired for log level
to affect actual handling.

Signed-off-by: Tomasz Moń <tomasz.mon@nordicsemi.no>
2024-04-22 06:51:22 -07:00
Tomasz Moń a2ce9b0151 usb: device_next: allow terminating descriptor set with NULL
Do not require nil descriptor at the end of descriptor set because it
serves no other purpose than a sentinel. Just end processing on first
NULL pointer within descriptor set.

Signed-off-by: Tomasz Moń <tomasz.mon@nordicsemi.no>
Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
2024-04-22 06:51:22 -07:00
Tomasz Moń 5e3a73de04 usb: device_next: uac2: Convert class to descriptor set
Convert to new descriptor interface, present only Full-Speed descriptors
to the USB stack.

Signed-off-by: Tomasz Moń <tomasz.mon@nordicsemi.no>
2024-04-22 06:51:22 -07:00
Tomasz Moń 52ce527c81 usb: device_next: uac2: add descriptor sets macros
Add necessary macros and convert UAC2 descriptor test from descriptor
blob to descriptor set. Currently there is only Full-Speed descriptor
set.

Signed-off-by: Tomasz Moń <tomasz.mon@nordicsemi.no>
2024-04-22 06:51:22 -07:00
Johann Fischer 42f7e1b97f usb: device_next: make HS support compliant with the USB2.0 specification
For specification-compliant high-speed support, we need to support
device quilifiers and other-speed-configuration descriptor requests. We
also need to store different configurations of the class/function
descriptors, which typically only affect the endpoint descriptors. With
this change, the stack expects class/function descriptors to be passed
as an array of struct usb_desc_header pointers to e.g. interface,
interface-specific, and endpoint descriptors, with the last element of
the array pointing to a nil descriptor. And also passed for a specific
speed, for now we support full and high speed configurations.

During instantiation, the class/function implementation must choose the
correct configuration in the full-speed and high-speed descriptor sets
for values such as maximum packet size and bInterval values of interrupt
and isochronous endpoints.

During initialization, the stack reads the highest speed supported by
the controller and uses it to get the appropriate descriptors set
from the instance. If the controller supports only full speed, the stack
configures the class/function descriptor for full speed only, if the
controller supports high speed, the stack configures the descriptors for
high speed only, and a class/function must update the full speed
descriptor during the init callback processing.

During device operation, the class/function implementation must check
the actual speed of the device and use the correct configuration, such
as the endpoint address or maximum packet size.

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
2024-04-22 06:51:22 -07:00
Johann Fischer 5cd964a744 usb: device_next: introduce and interface to get class/function descriptor
Introduce interface to get class/function descriptor for specific
speed.

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
2024-04-22 06:51:22 -07:00
Johann Fischer 3fed5559e9 usb: device_next: add helpers to get highest supported and actual speed
Save actual device speed in the device context and add helpers to get
highest supported and actual speed.

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
2024-04-22 06:51:22 -07:00
Johann Fischer fde4530055 usb: device_next: add helpers to get private data and device context
The class implementations should not access the members of the struct
usbd_class_node directly.

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
2024-04-22 06:51:22 -07:00
Johann Fischer 378a5f2bf7 usb: device_next: rework loopback interfaces
This implementation is WIP and has mostly been used to test interface
and endpoint configuration by the stack. For future changes, make the
interfaces more compliant with the USB specification.

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
2024-04-22 06:51:22 -07:00
Peter Mitsis 02cc127475 linker: subsys: Use Z_LINK_ITERABLE_SUBALIGN
Updates the linker script fragments under 'subsys' to use
Z_LINK_ITERABLE_SUBALIGN for the subalignment instead of
a hardcoded value of 4.

Signed-off-by: Peter Mitsis <peter.mitsis@intel.com>
2024-04-20 13:45:25 -04:00
Johann Fischer 567827add8 usb: device_next: use USB notification in CDC ACM implementation
Add message types for line coding and contol line state updates.
Add a publish message function that takes a pointer to a device
structure as payload, and use USB notification in the CDC ACM
implementation.

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
2024-03-22 10:10:42 +01:00
Johann Fischer 3aef852fc0 usb: device_next: implementation of USB device support notification
The implementation uses the system workqueue and a callback provided
and registered by the application. The application callback is called in
the context of the workqueue. Notification messages are stored in a
queue and delivered to the callback in sequence.

We cannot call application callback directly from the USB device stack
thread because the behavior of arbitrary code provided by the
application is unpredictable, and we do not want it to be executed in
the same context where all events from the device controller are
handled.

Nor can we use the ZBUS subsystem directly. ZBUS offloads responsibility
for defined behavior to the observers and application, and does not
provide any way for the publisher to enforce defined behavior and
execution context.
ZBUS listener would be called from the USB thread context and is not
acceptable. ZBUS subscriber does not provide delivery guarantee and
cached message can be overwritten. ZBUS message subscriber has
cumbersome global configuration and buffers that are too complicated to
handle from USB configuration, and even if we would use it, defined
behavior is not possible because of how listener and subscriber are
handled.

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
2024-03-22 10:10:42 +01:00
Johann Fischer 7e99579c6c usb: device_next: fix buffer count configuration in CDC ACM pool
Multiple CDC ACM instances can block each other because there are only
buffers for a single instance. Fix this by configuring the number of
buffers in the pool based on the number of CDC ACM instances.

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
2024-02-09 17:38:52 +00:00
Johann Fischer e3549ea28f usb: device_next: correct internal class API documentation
Correct documentation of the internal class API.

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
2024-02-09 10:52:16 -06:00
Tomasz Moń c83ad53442 usb: device_next: new USB Audio 2 implementation
Introduce new USB Audio 2 implementation written from scratch. Main goal
behind new implementation was to perform entity configuration with
devicetree bindings, hiding the descriptor complexity from application.

Initial implementation is working at Full-Speed only. High-Speed support
will come later, but even at Full-Speed only this is viable replacement
for old stack USB Audio 1 class (USB Audio 1 is limited to Full-Speed by
specification, i.e. it is explicitly forbidden for USB Audio 1 device to
work at High-Speed).

Implemented is only absolute minimum set of features required for basic
implicit and explicit feedback samples. Only one sample frequency is
currently supported. Optional interrupt notifications are not supported.

Signed-off-by: Tomasz Moń <tomasz.mon@nordicsemi.no>
2024-02-01 15:07:37 +01:00
Tomasz Moń 31de122ec0 usb: device_next: broadcast SOF to classes
Start of Frame is not relevant for most classes, but it is crucial for
isochronous operation. The most prominent example where SOF is necessary
is USB Audio class.

Signed-off-by: Tomasz Moń <tomasz.mon@nordicsemi.no>
2024-02-01 15:07:37 +01:00
Alexander Kaiser 476a224dfa usb: host: use uint16_t for Language ID
usbh_req_desc() truncates the descriptor id. This problem is most
visible with string descriptor requests, as only then can wIndex be
greater than 0xFF. In particular, this affects commonly used language
IDs such as English (United States), which is 0x0409.

Signed-off-by: Alexander Kaiser <akaiser@urbansky.com>
2024-02-01 10:29:00 +00:00
Roman Studenikin 260fc89643 drivers: use DT_INST_PROP over DT_INST_PROP_OR if possible
It might happens that DT(_INST)_PROP_OR is used with boolean properties.
For instance:

	.single_wire = DT_INST_PROP_OR(index, single_wire, false),	\
	.tx_rx_swap = DT_INST_PROP_OR(index, tx_rx_swap, false),	\

This is not required as boolean properties are generated with false
value when not present, so the _OR macro extension is superflous
and the above code can be replaced by:

	.single_wire = DT_INST_PROP(index, single_wire),		\
	.tx_rx_swap = DT_INST_PROP(index, tx_rx_swap),			\

Signed-off-by: Roman Studenikin <srv@meta.com>
2024-01-30 00:26:58 +00:00
Fabio Baltieri 1c9be802bc usbc: fix shadowed declaration build error
Fixes:

/__w/zephyr/zephyr/subsys/usb/usb_c/usbc_tc_src_states.c:260:21: error:
declaration of 'ret' shadows a previous local [-Werror=shadow]

Signed-off-by: Fabio Baltieri <fabiobaltieri@google.com>
2024-01-18 11:28:54 +00:00
Michał Barnaś 551c7654f5 usbc: integrate the PPC with the USB-C stack
Add calls to the PPC API that enables and disables the sink and source
paths in the appropriate USB-C stack states.

Signed-off-by: Michał Barnaś <mb@semihalf.com>
2024-01-18 10:53:53 +01:00
James Zipperer 273165c3bb usb: device: audio: fix compiler warning for usb audio microphone
audio_receive_cb is only used by headphones and headset, it is unused in
microphone-only configurations.  Gate compilation based on the device
tree.

Signed-off-by: James Zipperer <jzipperer@fb.com>
2024-01-10 15:08:06 +01:00