Skip to content

Commit 5cb0667

Browse files
committed
usb: host: class: implement usb host video class
Add USB host video class driver. Do some improvements for usbh.h accordingly. Add dts binding for usb host video class. Signed-off-by: Aiden Hu <[email protected]>
1 parent 9b5aa6b commit 5cb0667

File tree

8 files changed

+4507
-9
lines changed

8 files changed

+4507
-9
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Copyright 2025 NXP
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
description: |
5+
USB Video Class (UVC) host instance.
6+
7+
Each UVC instance added to the USB Host Controller (UHC) node will be visible
8+
as a new camera from the host point of view.
9+
10+
compatible: "zephyr,uvc-host"
11+
12+
include: base.yaml

include/zephyr/usb/usbh.h

Lines changed: 95 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,16 @@
2626
extern "C" {
2727
#endif
2828

29+
/**
30+
* @brief Match flags for USB device identification
31+
*/
32+
#define USBH_MATCH_DEVICE (1U << 0) /* Match device class code */
33+
#define USBH_MATCH_INTFACE (1U << 1) /* Match interface code */
34+
35+
/* device signal value definitions */
36+
#define USBH_DEVICE_CONNECTED 1
37+
#define USBH_DEVICE_DISCONNECTED 2
38+
2939
/**
3040
* @brief USB HOST Core Layer API
3141
* @defgroup usb_host_core_api USB Host Core API
@@ -49,6 +59,8 @@ struct usbh_contex {
4959
struct usb_device *root;
5060
/** Allocated device addresses bit array */
5161
struct sys_bitarray *addr_ba;
62+
/** Registered classes */
63+
sys_slist_t registered_classes;
5264
};
5365

5466
#define USBH_CONTROLLER_DEFINE(device_name, uhc_dev) \
@@ -73,21 +85,60 @@ struct usbh_code_triple {
7385
};
7486

7587
/**
76-
* @brief USB host class data and class instance API
88+
* @brief USB device code table for device matching
89+
*/
90+
struct usbh_device_code_table {
91+
/** Match type for device identification */
92+
uint32_t match_type;
93+
/** Vendor ID */
94+
uint16_t vid;
95+
/** Product ID */
96+
uint16_t pid;
97+
/** device's class code, subclass code, protocol code. */
98+
struct usbh_code_triple device_code;
99+
/** USB interface class code */
100+
uint8_t interface_class_code;
101+
/** USB interface subclass code */
102+
uint8_t interface_subclass_code;
103+
/** USB interface protocol code */
104+
uint8_t interface_protocol_code;
105+
};
106+
107+
/**
108+
* @brief USB host speed
109+
*/
110+
enum usbh_speed {
111+
/** Host supports or is connected to a full speed bus */
112+
USBH_SPEED_FS,
113+
/** Host supports or is connected to a high speed bus */
114+
USBH_SPEED_HS,
115+
/** Host supports or is connected to a super speed bus */
116+
USBH_SPEED_SS,
117+
};
118+
119+
/**
120+
* @brief USB HOST Core Layer API
121+
* @defgroup usb_host_core_api USB Host Core API
122+
* @ingroup usb
123+
* @{
77124
*/
78-
struct usbh_class_data {
79-
/** Class code supported by this instance */
80-
struct usbh_code_triple code;
81125

126+
/**
127+
* @brief USB host class data and class instance API
128+
*/
129+
struct usbh_class_api {
82130
/** Initialization of the class implementation */
83-
/* int (*init)(struct usbh_contex *const uhs_ctx); */
131+
int (*init)(struct usbh_contex *const uhs_ctx,
132+
struct usbh_class_data *cdata);
84133
/** Request completion event handler */
85134
int (*request)(struct usbh_contex *const uhs_ctx,
86135
struct uhc_transfer *const xfer, int err);
87136
/** Device connected handler */
88-
int (*connected)(struct usbh_contex *const uhs_ctx);
137+
int (*connected)(struct usb_device *udev,
138+
void *desc_start_addr, void *desc_end_addr, struct usbh_class_data *cdata);
89139
/** Device removed handler */
90-
int (*removed)(struct usbh_contex *const uhs_ctx);
140+
int (*removed)(struct usbh_contex *const uhs_ctx,
141+
struct usbh_class_data *cdata);
91142
/** Bus remote wakeup handler */
92143
int (*rwup)(struct usbh_contex *const uhs_ctx);
93144
/** Bus suspended handler */
@@ -97,11 +148,46 @@ struct usbh_class_data {
97148
};
98149

99150
/**
151+
* @brief USB host class data and class instance API
100152
*/
101-
#define USBH_DEFINE_CLASS(name) \
102-
static STRUCT_SECTION_ITERABLE(usbh_class_data, name)
153+
struct usbh_class_data {
154+
/** System linked list node for registered classes */
155+
sys_snode_t node;
156+
/** Name of the USB host class instance */
157+
const char *name;
158+
/** Pointer to host support class API */
159+
const struct usbh_class_api *api;
160+
/** Pointer to private data */
161+
void *priv;
162+
/** Pointer to device code table for class matching */
163+
const struct usbh_device_code_table *device_code_table;
164+
/** Number of items in device code table */
165+
uint8_t table_items_count;
166+
/** Flag indicating if class has been matched to a device */
167+
uint8_t class_matched;
168+
};
103169

104170

171+
/**
172+
* @brief Define USB host support class data
173+
*
174+
* Macro defines class (function) data, as well as corresponding node
175+
* structures used internally by the stack.
176+
*
177+
* @param class_name Class name
178+
* @param class_api Pointer to struct usbd_class_api
179+
* @param class_priv Class private data
180+
*/
181+
#define USBH_DEFINE_CLASS(class_name, class_api, class_priv, code_table, items_count) \
182+
static STRUCT_SECTION_ITERABLE(usbh_class_data, class_name) = { \
183+
.name = STRINGIFY(class_name), \
184+
.api = class_api, \
185+
.priv = class_priv, \
186+
.device_code_table = code_table, \
187+
.table_items_count = items_count, \
188+
.class_matched = 0, \
189+
};
190+
105191
/**
106192
* @brief Initialize the USB host support;
107193
*

subsys/usb/host/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ zephyr_library_sources_ifdef(
1616
usbh_shell.c
1717
)
1818

19+
zephyr_library_sources_ifdef(
20+
CONFIG_USBH_VIDEO_CLASS
21+
class/usbh_uvc.c
22+
)
23+
1924
zephyr_library_sources_ifdef(
2025
CONFIG_USBIP
2126
usbip.c

subsys/usb/host/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,5 +54,6 @@ config USBH_MAX_UHC_MSG
5454
Maximum number of USB host controller events that can be queued.
5555

5656
rsource "Kconfig.usbip"
57+
rsource "class/Kconfig"
5758

5859
endif # USB_HOST_STACK

subsys/usb/host/class/Kconfig

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#
2+
# Copyright 2025 NXP
3+
#
4+
# SPDX-License-Identifier: Apache-2.0
5+
#
6+
7+
rsource "Kconfig.uvc_host"
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#
2+
# Copyright 2025 NXP
3+
#
4+
# SPDX-License-Identifier: Apache-2.0
5+
#
6+
7+
config USBH_VIDEO_CLASS
8+
bool "USB Host Video Class implementation"
9+
depends on DT_HAS_ZEPHYR_UVC_HOST_ENABLED
10+
help
11+
USB Host Video Class (UVC) implementation.
12+
This allows the system to act as a USB host and communicate
13+
with USB video devices such as webcams and other UVC-compliant
14+
video capture devices.
15+
16+
if USBH_VIDEO_CLASS
17+
18+
config USBH_VIDEO_NUM_BUFS
19+
int "Max number of buffers the UVC class can allocate"
20+
default 16
21+
help
22+
Control the number of buffer UVC can allocate in parallel.
23+
The default is a compromise to allow enough concurrent buffers
24+
but not too much memory usage.
25+
26+
module = USBH_VIDEO
27+
module-str = usbh uvc
28+
default-count = 1
29+
source "subsys/logging/Kconfig.template.log_config"
30+
31+
endif # USBH_VIDEO_CLASS

0 commit comments

Comments
 (0)