Commit graph

944 commits

Author SHA1 Message Date
Johann Fischer
315e148864 usb: dfu: halt endpoint on flash API error or block mismatch
Halt endpoint on flash API error or block mismatch.

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
2021-08-03 19:05:44 -04:00
Johann Fischer
d6e2c8afa1 usb: dfu: check control request direction
Check control request direction and split class request handler
by request direction.

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
2021-08-03 19:05:44 -04:00
Johann Fischer
7aeed96e5d usb: msc: additionally check control request direction
Rework class control request handler and add control
request direction check.

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
2021-08-03 19:05:44 -04:00
Johann Fischer
4e6bf5b567 usb: bt_h4: return -ENOTSUP on vendor requests
This class does not handle any vendor request,
therefore just return -ENOTSUP on any vendor request.

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
2021-08-03 19:05:44 -04:00
Johann Fischer
4adde81b43 usb: bluetooth: additionally check control request direction
HCI commands are always directed to controller and should be
host-to-device class control requests.

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
2021-08-03 19:05:44 -04:00
Johann Fischer
78b9acf852 usb: audio: additionally check control request direction
Rework mute request handling and add checks for
control request direction.

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
2021-08-03 19:05:44 -04:00
Johann Fischer
37d8236be4 usb: rndis: return error on incorrect/unsupported request
Return ENOTSUP if request is unsupported and pass on the error
from encapsulated command/response functions.

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
2021-08-03 19:05:44 -04:00
Johann Fischer
a5493a5c71 usb: cdc_ecm: return error on incorrect/unsupported request
Control request handler from CDC ECM implementation does not
care at all about returning errors when a request is not supported.
Return EINVAL/ENOTSUP if request is incorrect/unsupported.

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
2021-08-03 19:05:44 -04:00
Johann Fischer
e9f9138997 usb: loopback: use class internal buffer to store request data
Use internal buffer to store request data and not
the data OUT stage buffer.

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
2021-08-03 19:05:44 -04:00
Johann Fischer
5698e17aa0 usb: cdc_acm: enhance verification of the class control requests
Rework CDC ACM class control request handling and
check the control transfer direction.

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
2021-08-03 19:05:44 -04:00
Johann Fischer
781c367730 usb: set data stage length variable to zero by default
Set data stage length variable to zero as a precaution
so that no trouble happens if control request handler
does not check the request values sufficiently.

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
2021-08-03 19:05:44 -04:00
Johann Fischer
ae074e81d4 usb: remove Kconfig option CONFIG_USB
The USB configuration option is actually a global switch
to enable USB drivers in general, but currently only
the device controller drivers are meant.

USB device controller drivers also have USB_DEVICE_DRIVER option.
Thus the option USB is actually redundant and can be replaced
by the self-explanatory option USB_DEVICE_DRIVER.
The name USB itself is not unique and should not be used as an
configuration option.

With these changes the option USB_DEVICE_DRIVER generally
enables USB device controller drivers. The option USB_DEVICE_STACK
enables USB device support. It is sufficient to enable only option
USB_DEVICE_STACK because it selects USB_DEVICE_DRIVER.

CONFIG_USB Kconfig option is temporary added to subsys/usb/Kconfig.
This is necessary to pass CI and will be removed again
when the USB configuration has been adapted in modules.

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
2021-08-03 19:00:12 -04:00
Johann Fischer
6408d40fd6 usb: use new USB framework header
Replace all macros and types with the new ones from
usb/usb_ch9.h header.

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
2021-08-03 13:20:07 +02:00
Aurelien Jarno
0cd3daacba usb: truncate the most significant part of the serial number
When the Hardware Information Driver (HWINFO) provides a device ID
longer than the configured USB serial number, truncate the most
significant part instead of the least significant part. The lower part
is usually having more entropy, e.g. on STM32.

Fixes #34550

Signed-off-by: Aurelien Jarno <aurelien@aurel32.net>
2021-07-24 21:26:06 -04:00
Johann Fischer
244f69d1df usb: audio: prevent unaligned memory access
Prevent unaligned memory access.

Fixes #36873

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
2021-07-20 16:56:26 +03:00
Johann Fischer
8b37c62b35 usb: remove useless len argument from usb_data_to_host()
The len argument always has the same value as
usb_dev.setup.wLength, which is also the last received
USB device request.

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
2021-07-19 04:01:22 -04:00
Johann Fischer
7ec174df0d usb: fix request hijack through Audio class workaround
Because it is not always possible to determine whether
a request is for Audio class or not, all requests are passed
to it. This can lead to the requests, to e.g. HID interface,
being hijacked by Audio class.
Ignore return value of Audio custom handler as it is not relevant here.

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
2021-06-17 12:41:33 +02:00
Johann Fischer
afecdc97ef usb: audio: rework custom request handler callback
Since the settings are stored in the core now,
we can remove REQ_GET_INTERFACE part from custom handler
and simplify the code. Return value is no longer relevant
since custom callback does not need to interfere with the
request handling process.

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
2021-06-17 12:41:33 +02:00
Johann Fischer
1e71cef151 usb: store alternate interface settings
Store alternate interface settings and
return alternate on GetInterface request.

Fixes: #24200

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
2021-06-17 12:41:33 +02:00
Johann Fischer
8999c30cee usb: hid: remove get_protocol/set_protocol from USB HID class API
The USB HID class API offers the possibility to register callbacks
for Get/SetIdle, Get/SetProtocol to the application.
Rules for these callbacks are neither obvious nor documented.

This patch remove this possibility to register Get/SetProtocol
and Get/SetIdle callbacks for the following reasons:

The possibility to call unknown application code while processing
control requests should be avoided or reduced to a minimum.

The Get/SetProtocol callbacks are redundant and do not provide any
additional value since the way to inform the application
about the change of the protocol exists via the callback
hid_protocol_cb_t protocol_change.

The core provides implementation to handle Get/SetIdle requests and
on idle reports. If this is not suitable in any way then the application
should implement everything itself.

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
2021-05-07 13:14:47 +02:00
Joakim Andersson
6483e12a8a Bluetooth: Refactor bluetooth buffer configuration for simplification
Refactor and simplify the bluetooth buffer configurations to improve the
easy of configurations and eliminate invalid ones.
By moving configurations out of host and controller specific
configurations and into a common one it becomes easier to configure
the host and controller separately as the same configurations can be
used as would be for a combined build.

All HCI configurations are now given exluding the matching HCI header,
which eases the configuration as the application don't have to know the
different header sizes.
The BT_RX_BUF_LEN is split into ACL and Event, as well as the suprising
use of Command size.
BT_L2CAP_RX_MTU is removed as the stack does not support reassembling of
HCI ACL data to larger L2CAP PDUs. The application will have to set
ACL RX size and account for the L2CAP PDU header itself.
BT_EATT_RX_MTU was removed as it is only used for setting a different
default value for another option which leads to the stuck kconfig symbol
problem.

The configurations can be updated according to the table below:

** New configuration         | ** Old configuration
All configurations
BT_BUF_ACL_RX_SIZE           | BT_L2CAP_RX_MTU + 4
BT_BUF_ACL_RX_SIZE           | BT_RX_BUF_LEN - 4
BT_BUF_EVT_RX_SIZE           | BT_RX_BUF_LEN - 2
BT_BUF_CMD_TX_SIZE           | BT_RX_BUF_LEN - 3
BT_BUF_CMD_TX_COUNT          | BT_HCI_CMD_COUNT
BT_BUF_EVT_RX_COUNT          | BT_RX_BUF_COUNT
BT_BUF_ACL_RX_COUNT          | BT_RX_BUF_COUNT
BT_BUF_ACL_RX_COUNT          | BT_ACL_RX_COUNT
BT_BUF_EVT_DISCARDABLE_SIZE  | BT_DISCARDABLE_BUF_SIZE - 2
BT_BUF_EVT_DISCARDABLE_COUNT | BT_DISCARDABLE_BUF_COUNT
Controller-build
BT_BUF_ACL_TX_SIZE           | BT_CTLR_TX_BUFFERS_SIZE
BT_BUF_ACL_TX_COUNT          | BT_CTLR_TX_BUFFER
HCI-bridge
BT_BUF_ACL_TX_SIZE           | BT_HCI_ACL_DATA_SIZE
BT_BUF_ACL_TX_COUNT          | 6

Fixed invalid configurations setting either BT_L2CAP_RX_MTU or
BT_CTLR_DATA_LENGTH_MAX larger than BT_RX_BUF_LEN could lead to buffer
overruns.

Fix advertising report max data length calculation.
This always used the BT_DISCARDABLE_BUF_SIZE macro but this feature
can be turned off and advertising reports will be allocated from the RX
buffer in that case. Also controller-build does not have this buffer
(in hci_raw.c). Also the wrong HCI header was used in the calculation,
HCI event header should have been used instead of HCI ACL header.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2021-05-06 14:56:18 +02:00
Aurelien Jarno
b61c23943c usb: netusb: rndis: fix a typo
asemble => assemble

Signed-off-by: Aurelien Jarno <aurelien@aurel32.net>
2021-05-01 14:17:39 -04:00
Jennifer Williams
a3c7e45a73 usb: usb_device: fix missing final else
set_endpoint() and reset_endpoint() were missing final
else statement in the if else if construct. This commit
adds a final else {} with assert to comply with coding
guideline 15.7.

Signed-off-by: Jennifer Williams <jennifer.m.williams@intel.com>
2021-04-27 17:31:59 -04:00
Gerard Marull-Paretas
d7da2a31f0 usb: remove usage of device_pm_control_nop
If device PM is not implemented just use NULL.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2021-04-27 16:28:49 -04:00
Trond Einar Snekvik
6976658719 usb: dfu: convert z_timeout_end_calc to sys_clock_timeout_end_calc
The z_timeout_end_calc function was replaced by
sys_clock_timeout_end_calc in #33302. A reference to the old function
snuck into master through #30015.

Signed-off-by: Trond Einar Snekvik <Trond.Einar.Snekvik@nordicsemi.no>
2021-04-26 13:40:29 +02:00
Peter Bigot
2ac596b91a usb: conversion of k_work API
Replace all existing deprecated API with the recommended alternative.

Be aware that this does not address architectural errors in the use
of the work API.

Fixes #34092

Signed-off-by: Peter Bigot <peter.bigot@nordicsemi.no>
Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
2021-04-15 12:29:34 -04:00
Dominik Ermel
f4b6231a9d usb/dfu: Pick DFU target image label from DTS
Instead of defining the string, that will be reported by DFU
as a name for DFU target, in code, pick it from the DTS label that
will be the DFU target.

Signed-off-by: Dominik Ermel <dominik.ermel@nordicsemi.no>
2021-03-30 09:01:20 -05:00
Dominik Ermel
73b29d68e7 usb/dfu: Removed unused NUMOF_ALTERNATE_SETTINGS
Alternative settings are determined by number of flash partitions
available for DFU from DTS.

Signed-off-by: Dominik Ermel <dominik.ermel@nordicsemi.no>
2021-03-30 09:01:20 -05:00
Johann Fischer
f205e94f9e include: move disk_access.h to storage
Zephyr already has a directory for storage API relevant headers.
Move disk_access.h header to include/storage where it fits better
structurally.

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
2021-03-23 12:16:01 +01:00
Josh Gao
16d2ab790c usb: dfu: give wait_for_usb_dfu an argument
Allow callers to wait for an arbitrary amount of time, instead of always
waiting for a compile-time fixed period.

Signed-off-by: Josh Gao <josh@jmgao.dev>
2021-03-22 12:55:56 +01:00
Johann Fischer
91ddb8c9fe usb: hid: allow boot interface Protocol Code to be set per device
Kconfig option USB_HID_PROTOCOL_CODE does not allow to set
boot interface protocol code for specific HID device but
only to set the same value for all device.
Add new API function to allow the application to set
Protocol Code per device. Deprecate USB_HID_PROTOCOL_CODE option.

Fixes: #32778

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
2021-03-19 09:51:21 -04:00
Johann Fischer
4516ad0bde usb: hid: rework idle report implementation
HID core implementation has support for Get/Set Idle requests and
on idle report processing. Basically it has little use.
Also, it has no users in the tree except samples/subsys/usb/hid,
which does not have it turned on.
There are several issues with the idle report implementation:
- Core calls hid_ops.on_idle callback with Report ID 0,
  but this ID is reserved and should not be used.
  Report descriptors do not use Report ID index zero.
- Calling hid_ops.on_idle with Report ID 0 cann only
  be turned off by calling SetIdle(Duration = 0, Report ID = 0),
  but not by SetIdle with Report ID not equal zero.
- GetIdle with Report Id not equal zero returns wrong
  value after SetIdle(Duration = n, Report ID = 0)
- It may happen that hid_ops.on_idle is called for
  different Report ID during one and the same SoF event.
  But there can be only one hid_int_ep_write() during a frame.

This patch fixes listed issues and revises Get/Set Idle request,
and hid_sof_handler() implementation.

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
2021-03-17 11:34:42 +01:00
Johann Fischer
5533c7ed21 usb: hid: clarify USB_HID_REPORTS configuration
Clarify USB_HID_REPORTS configuration.

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
2021-03-17 11:34:42 +01:00
James Harris
b10428163a kernel: sem: add K_SEM_MAX_LIMIT
Currently there is no way to distinguish between a caller
explicitly asking for a semaphore with a limit that
happens to be `UINT_MAX` and a semaphore that just
has a limit "as large as possible".

Add `K_SEM_MAX_LIMIT`, currently defined to `UINT_MAX`, and akin
to `K_FOREVER` versus just passing some very large wait time.

In addition, the `k_sem_*` APIs were type-confused, where
the internal data structure was `uint32_t`, but the APIs took
and returned `unsigned int`. This changes the underlying data
structure to also use `unsigned int`, as changing the APIs
would be a (potentially) breaking change.

These changes are backwards-compatible, but it is strongly suggested
to take a quick scan for `k_sem_init` and `K_SEM_DEFINE` calls with
`UINT_MAX` (or `UINT32_MAX`) and replace them with `K_SEM_MAX_LIMIT`
where appropriate.

Signed-off-by: James Harris <james.harris@intel.com>
2021-03-05 08:13:53 -06:00
Jacob Siverskog
c2afabcc7c usb: mass storage: add missing prompt to Kconfig
thread stack size was missing input prompt.

Signed-off-by: Jacob Siverskog <jacob@teenage.engineering>
2021-03-01 09:27:57 -05:00
Johann Fischer
6568f4ab99 usb: hid: remove usb_hid_descriptor from public header
usb_hid_descriptor struct is only used internally and
is not intended to be used by the USB HID device application.

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
2021-02-28 16:50:24 -05:00
Johann Fischer
879a5cfe48 usb: hid: replace deprecated macros
Replace deprecated macros.

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
2021-02-28 16:50:24 -05:00
Johann Fischer
9a3ebcccfa usb: hid: make config USB_DEVICE_HID menuconfig
This makes USB device support menu appear more tidy.

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
2021-02-19 14:39:26 -05:00
Johann Fischer
dac91c0bdc usb: move USB Loopback class configuration to its own Kconfig
Move USB Loopback class configuration to its own Kconfig

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
2021-02-19 14:39:26 -05:00
Johann Fischer
092a9de285 usb: move USB Bluetooth classes configuration to its own Kconfig
Move USB Bluetooth classes configuration to its own Kconfig.

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
2021-02-19 14:39:26 -05:00
Johann Fischer
12e32c2b70 usb: move USB MSC configuration to its own Kconfig file
Move USB MSC configuration to its own Kconfig file.

Align with other class designations and rename
mass_storage.c to msc.c

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
2021-02-19 14:39:26 -05:00
Johann Fischer
2feee774bb usb: move CDC ACM configuration to to its own Kconfig file
Move CDC ACM configuration to to its own Kconfig file.

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
2021-02-19 14:39:26 -05:00
Jacob Siverskog
3781d5f553 usb: mass storage: set thread stack size from Kconfig
allow user to select stack size depending on the situation.

Signed-off-by: Jacob Siverskog <jacob@teenage.engineering>
2021-02-17 11:49:52 +01:00
Jacob Siverskog
b4454f63ef usb: mass storage: use inquiry parameters from Kconfig
support custom inquiry vendor id, product id and revision.

Signed-off-by: Jacob Siverskog <jacob@teenage.engineering>
2021-02-14 18:31:18 -05:00
Johann Fischer
7ab50b66ce usb: bluetooth: fix OUT buffer handling
acl_read_cb does not handle incoming (ACL) data
if BT_CTLR_TX_BUFFER_SIZE is greater than
USB_MAX_FS_BULK_MPS - BT_HCI_ACL_HDR_SIZE.

Since the host adjusts the data according to
the BT_CTLR_TX_BUFFER_SIZE and does not use ZLP
we cannot start usb_transfer over the possible length of
the whole packet, with or without USB_TRANS_NO_ZLP flag.
But we can read the packet length from the header and
call net_buf_put() when the whole packet is received.

Fixes: #31922

Reported-by: Matias Karhumaa <matias.karhumaa@gmail.com>
Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
2021-02-14 16:38:56 -05:00
Johann Fischer
de6c583eee usb: fix BOS descriptor registration
Use BOS header structure directly we have access to
instead of casting extern const pointers.

Fixes: #30330

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
2021-02-02 21:29:23 -05:00
Jan Pohanka
cd7461495f usb: fix possible deadlock in usb_transfer_sync()
Syncing here is done with a semaphore declared on a function stack
and released in a callback. In case of a cancelled transfer (eg.
cable disconnect) the callback is never called.

The fix introduces a periodic check if a transfer is still valid.

Signed-off-by: Jan Pohanka <xhpohanka@gmail.com>
2021-01-26 07:21:13 -05:00
Kari Hulkko
22c0d0155a usb: fix for parallel transfer deadlock with usb_transfer_sync()
Parallel transfer to same endpoint is not supported and
may cause a deadlock. Adding a check to prevent starting
the transfer if tranfer is already ongoing on same endpoint.

Transfer status was not checked when accessing to transfer
from endpoint callback. Adding status check, to prevent
a double completion.

Fixes #30736

Signed-off-by: Kari Hulkko <kari.m.hulkko@gmail.com>
2021-01-24 10:08:27 -05:00
Peter Bigot
0496f8cc88 usb: cdc_acm: select features required for use
If UART_INTERRUPT_DRIVEN isn't selected the CDC ACM class driver won't
build.  Don't require the user's intervention to avoid this.

Signed-off-by: Peter Bigot <peter.bigot@nordicsemi.no>
2021-01-05 09:40:36 -06:00
Michael Rosen
1c89837e79 usb: dfu: add separate pid for dfu mode
Refactor the file structure for USB DFU class to facilitate
separate PID for USB DFU when in DFU mode. As required by USB DFU
1.1 Section 2, the PID in the USB device descriptor must be
different between the Run-time and DFU mode device descriptor to
avoid problems caused by the host OS caching the remaining
descriptors when switching to DFU mode, thus hiding the new
interface descriptors from applications on the host and reporting
the Run-time descriptors when the device is in DFU mode.

To avoid adding too much clutter to the root USB class Kconfig and
CMakeLists files, move the DFU class files into their own directory
with dedicated Kconfig and CMakeLists.txt.

Signed-off-by: Michael Rosen <michael.r.rosen@intel.com>
2020-12-20 13:04:45 -05:00