Commit graph

46 commits

Author SHA1 Message Date
Marek Pieta f6600040e4 usb: cdc_acm: Update log level only if UART log backend is enabled
Change updates log level only if UART log backend is enabled in Kconfig
configuration. The log level update is not needed in case logs are
provided over other backend (e.g. RTT).

Change affects both USB stacks.

Signed-off-by: Marek Pieta <Marek.Pieta@nordicsemi.no>
2024-06-21 08:45:22 -04:00
Fabio Baltieri 2c5b09cbda usb: device_next: rename the rate properties to use the correct unit
The polling properties are a period in us but are named as "-rate" right
now, which would imply that that's a frequency. Rename them to
"period-us" to make that unambiguous.

Signed-off-by: Fabio Baltieri <fabiobaltieri@google.com>
2024-06-13 08:03:49 -04:00
Johann Fischer af63e488f0 usb: device_next: hid: fix Get Report buffer handling
After the get_report() callback, we need to determine how many bytes the
HID device wrote to the report buffer. Use the callback return value to
do this, and modify the net_buf data length value if get_report was
successful.

Reported-by: Marek Pieta <Marek.Pieta@nordicsemi.no>
Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
2024-06-04 07:56:45 +02:00
Tomasz Moń 8d344cc9d8 usb: device_next: Rename usbd_contex to usbd_context
Add the missing "t" to struct usbd_contex. No functional changes.

Signed-off-by: Tomasz Moń <tomasz.mon@nordicsemi.no>
2024-06-03 06:43:20 -07:00
Jordan Yates 924db97724 usb: cdc_acm: add locks around ring_buf_put
The ring buffer API is explicitly not thread safe, with users needing to
implement their own locking. As `poll_out` and `fifo_fill` are operating
on the same ringbuffer, these locks are needed.

Signed-off-by: Jordan Yates <jordan@embeint.com>
2024-06-03 03:44:45 -07:00
Tomasz Moń 20983c7ced usb: device_next: uac2: Fix integer handling issues
Fix integer handling issues found out by Coverity Scan.

Coverity-CID: 363729
Coverity-CID: 363737

Signed-off-by: Tomasz Moń <tomasz.mon@nordicsemi.no>
2024-05-28 18:58:25 +02:00
Tomasz Moń c30ac4dfd8 usb: device_next: CDC ACM: Process TX on configuration enable
If CDC ACM uart side has TX enabled when the configuration gets enabled,
depending on fifo state following has to happen:
  * if the fifo is empty - trigger TX ready interrupt
  * if the fifo is not empty - queue TX data on IN endpoint

Signed-off-by: Tomasz Moń <tomasz.mon@nordicsemi.no>
2024-05-27 14:04:35 +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 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 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ń 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 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 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
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ń 3e7368829b usb: device_next: uac2: interface descriptor macros
Add macros to initialize full set of descriptors required for USB Audio
Class 2 instance. Descriptors start with Interface Association
Descriptor that binds together the AudioControl interface and all
AudioStreaming interfaces.

AudioStreaming interfaces have alternate setting 0 without any endpoints
and alternate setting 1 with isochronous endpoint with max packet size
calculated on maximum sample frequency, number of channels, subslot size
and clock synchronization (asynchronous endpoints require +1 sample).

Signed-off-by: Tomasz Moń <tomasz.mon@nordicsemi.no>
2023-12-15 14:24:44 +01:00
Tomasz Moń 4f1b5b11b2 usb: device_next: uac2: devicetree descriptor macros
Initial set of macros to generate class specific descriptors based on
devicetree nodes. Descriptors are generated as uint8_t array initializer
list because there is variable number of entities in between standard
USB interface and endpoint descriptors. Because the descriptors are
automatically generated there is no real point in trying to generate
corresponding C structures (especially that some entity descriptors have
variably sized fields in the middle of descriptor, e.g. Clock Selector).

Signed-off-by: Tomasz Moń <tomasz.mon@nordicsemi.no>
2023-12-15 14:24:44 +01:00
Johann Fischer ef72f73945 usb: device_next: loopback: fixup left over from early state
Class instance must not call usbd_ep_ctrl_enqueue().

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
2023-10-01 09:26:07 +03:00
Gerard Marull-Paretas 0aac6cfb75 usb: device_next: add missing init.h
Some files were using SYS_INIT, from init.h.

Signed-off-by: Gerard Marull-Paretas <gerard@teslabs.com>
2023-08-29 14:35:29 +01:00
Johann Fischer e6bfc7f868 usb: fix common misspellings in USB support
Fix common misspellings in USB device next and host.

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
2023-07-26 10:46:01 +02:00
Johann Fischer 32668dc7fd usb: device_next: allow string descriptor index to be updated
Support for multiple instances of a class implementation,
and the ability to register an instance to a configuration
at runtime, requires a mechanism to add a string descriptor
and update its index based on the total number of descriptors.

We also need to handle some special string descriptors like
Product or Serial Number provided by the application.
Marked as such by using specific macros, these descriptors
can be sorted out by the stack and the device descriptor
indexes are updated automatically.

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
2023-05-26 09:48:19 -04:00
Gerard Marull-Paretas dacb3dbfeb iterable_sections: move to specific header
Until now iterable sections APIs have been part of the toolchain
(common) headers. They are not strictly related to a toolchain, they
just rely on linker providing support for sections. Most files relied on
indirect includes to access the API, now, it is included as needed.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2023-05-22 10:42:30 +02:00
Tomasz Moń 7ba5ecf451 usb: device_next: report medium error on Mass Storage failed writes
End Write command with Medium Error / Write Error when underlying disk
write or sync fails.

Signed-off-by: Tomasz Moń <tomasz.mon@nordicsemi.no>
2023-05-17 11:45:45 +02:00
Tomasz Moń 3532c2563a usb: device_next: accept padded Mass Storage CDBs
Universal Serial Bus Mass Storage Specification For Bootability requires
that device shall accept Bulk-Only-Transport bCBWCBLength of 12 for all
commands documented in the specification. Allow padding for commands
documented in Bootability specification, but leave others intact.

This fixes Request Sense on Windows 11 which previously failed due to
bCBWCBLength being 12 instead of 6.

Signed-off-by: Tomasz Moń <tomasz.mon@nordicsemi.no>
2023-05-17 11:45:45 +02:00
Johann Fischer 71d9b0d5ff usb: device_next: add CDC ECM class implementation
Add CDC Ethernet Control Model class implementation for the
new experimental USB device support based on the existing
implementation subsys/usb/device/class/netusb/function_ecm.c.

The implementation forms virtual ethernet connection between
USB host and USB device and requires two corresponding MAC
addresses, one for the virtual interface on the device side,
and other for the host which is described by a string descriptor.
With upcoming changes it should also possible to use a real
ethernet controller as media access on the device side.

CDC ECM implementation supports multiple instances which are
specified using DT. The number of instances is limited by the
number of endpoints on the controller, two to three should usually
be possible.

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
2023-05-11 11:26:54 +02:00
Gerard Marull-Paretas a5fd0d184a init: remove the need for a dummy device pointer in SYS_INIT functions
The init infrastructure, found in `init.h`, is currently used by:

- `SYS_INIT`: to call functions before `main`
- `DEVICE_*`: to initialize devices

They are all sorted according to an initialization level + a priority.
`SYS_INIT` calls are really orthogonal to devices, however, the required
function signature requires a `const struct device *dev` as a first
argument. The only reason for that is because the same init machinery is
used by devices, so we have something like:

```c
struct init_entry {
	int (*init)(const struct device *dev);
	/* only set by DEVICE_*, otherwise NULL */
	const struct device *dev;
}
```

As a result, we end up with such weird/ugly pattern:

```c
static int my_init(const struct device *dev)
{
	/* always NULL! add ARG_UNUSED to avoid compiler warning */
	ARG_UNUSED(dev);
	...
}
```

This is really a result of poor internals isolation. This patch proposes
a to make init entries more flexible so that they can accept sytem
initialization calls like this:

```c
static int my_init(void)
{
	...
}
```

This is achieved using a union:

```c
union init_function {
	/* for SYS_INIT, used when init_entry.dev == NULL */
	int (*sys)(void);
	/* for DEVICE*, used when init_entry.dev != NULL */
	int (*dev)(const struct device *dev);
};

struct init_entry {
	/* stores init function (either for SYS_INIT or DEVICE*)
	union init_function init_fn;
	/* stores device pointer for DEVICE*, NULL for SYS_INIT. Allows
	 * to know which union entry to call.
	 */
	const struct device *dev;
}
```

This solution **does not increase ROM usage**, and allows to offer clean
public APIs for both SYS_INIT and DEVICE*. Note that however, init
machinery keeps a coupling with devices.

**NOTE**: This is a breaking change! All `SYS_INIT` functions will need
to be converted to the new signature. See the script offered in the
following commit.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>

init: convert SYS_INIT functions to the new signature

Conversion scripted using scripts/utils/migrate_sys_init.py.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>

manifest: update projects for SYS_INIT changes

Update modules with updated SYS_INIT calls:

- hal_ti
- lvgl
- sof
- TraceRecorderSource

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>

tests: devicetree: devices: adjust test

Adjust test according to the recently introduced SYS_INIT
infrastructure.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>

tests: kernel: threads: adjust SYS_INIT call

Adjust to the new signature: int (*init_fn)(void);

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2023-04-12 14:28:07 +00:00
Tomasz Moń 5bd0f5ea8b usb: device_next: msc: handle MODE SENSE(10) command
USB Mass Storage Specification for Bootability requires MODE SENSE(10)
command. MODE SENSE(6) and MODE SENSE(10) generally access the same data
but differ in the maximum allocated length and LLBAA support. However
there is no point in extracting common handling because there no mode
pages are supported now.

Fail MODE SENSE requests if asking for anything other than supported
page codes (to which the essentially hardcoded response is valid).

Signed-off-by: Tomasz Moń <tomasz.mon@nordicsemi.no>
2023-03-08 10:53:55 +01:00
Tomasz Moń 4c97dd546a usb: device_next: msc: STALL OUT when waiting for reset
Mass Storage Class allows device to either silently discard OUT endpoint
data after receiving invalid CBW or to STALL the OUT endpoint (and keep
it STALLed regardless of host clearing halt) until Reset Recovery.

Switch from silently discarding to STALL in order to allow host realize
that device waits for Reset Recovery without transferring complete OUT
data.

Signed-off-by: Tomasz Moń <tomasz.mon@nordicsemi.no>
2023-03-07 15:48:40 +01:00
Tomasz Moń 7527b38ff5 usb: device_next: fail Mass Storage requests with invalid length
Fail Mass Storage control requests if wLength is incorrect.

Signed-off-by: Tomasz Moń <tomasz.mon@nordicsemi.no>
2023-03-01 12:02:29 +01:00
Tomasz Moń 509c033c1d usb: device_next: new USB Mass Storage implementation
Introduce new USB Mass Storage Bulk-Only Transport implementation
written from scratch. Main goal behind new implementation was to
separate USB and SCSI parts as clearly as possible.

Limited set of SCSI disk commands is implemented in separate source code
file that is internal to USB device stack. While it should be possible
to use the SCSI implementation by other components there is currently no
other user besides USB MSC and therefore SCSI header is kept private.

Signed-off-by: Tomasz Moń <tomasz.mon@nordicsemi.no>
2023-02-24 15:19:20 +01:00
Johann Fischer c51744c6ee usb: device_next: use consistent nil descriptor name
Use a consistent nil_desc name for the descriptor that
terminates a class (function) descriptor.

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
2023-02-24 14:02:37 +01:00
Andrei Emeltchenko 8e84d840b5 usb: Fix build for 64 bit platforms
Use proper format for size_t type eliminating warnings of type:

...
error: format '%u' expects argument of type 'unsigned int', but
argument 2 has type 'size_t' {aka 'long unsigned int'}
[-Werror=format=]
...

Signed-off-by: Andrei Emeltchenko <andrei.emeltchenko@intel.com>
2023-02-23 17:51:31 +01:00
Johann Fischer a06d9769ce usb: device_next: update IAD during CDC ACM initialization
Set bFirstInterface of IAD and update CDC Union descriptor
during class initialization.

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
2023-01-26 09:37:03 -08:00
Johann Fischer 5c077d72b5 usb: device_next: add BT HCI USB transport layer
Add Bluetooth HCI USB transport layer implementation for the new
experimental USB device support based on the existing implementation
subsys/usb/device/class/bluetooth.c.

This implementation, unlike existing one, contains the interface
descriptors for isochronous endpoints. Since we do not support voice
channels, these are just there to avoid issues with Linux kernel btusb
driver when this implementation is part of a composite configuration.

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
2023-01-27 00:57:22 +09:00
Johann Fischer 98770caf2a usb: device_next: remove workaround for the UDC enqueue bug
Remove workaround for the UDC enqueue bug in experimental
CDC ACM implementation.

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
2023-01-11 17:44:50 +01:00
Johann Fischer e9bab613cc usb: device_next: rename Kconfig template instances count
The name and wording used in template can be misleading.
Use more accurate word instance instead of device since
an implementation does not necessarily have to implement
a device (in the sense of Zephyr device model), and if it
does then devicetree should be used to describe number of
instances as in the case of CDC ACM implementation.

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
2023-01-11 17:44:27 +01:00
Johann Fischer fcd21f10d5 usb: device_next: add experimental CDC ACM implementation
Add experimental CDC ACM implementation for new USB device stack.
It currently implements only UART IRQ API support and is WIP.

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
2022-12-02 12:55:18 +01:00
Johann Fischer cb8b9ad38a usb: add new experimental USB device stack
The device supprt brings support for multiple stack instances,
multiple configuration, asynchronous transfer model, ability to
change most of the properties of a device at runtime and
the composition of configuration and classes at runtime.

The stack requires new UDC driver API and is not compatible
with old driver API (usb_dc_). The classes (functions) of old
(current) USB device stack cannot be used with new ones and must
be ported.

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
2022-12-02 12:55:18 +01:00