Skip to content

Commit f230b55

Browse files
Josuah DemangeonAidenHu
andcommitted
usb: host: implement class filtering and descriptor browsing
Add utilities to filter a class and search the next descriptor of a given type. This can be used to in combination to seek device information from the USB descriptors and try to match a host class instance given a set of filters. Co-authored-by: Aiden Hu <[email protected]> Signed-off-by: Josuah Demangeon <[email protected]>
1 parent e9bb65f commit f230b55

File tree

5 files changed

+128
-15
lines changed

5 files changed

+128
-15
lines changed

include/zephyr/usb/usbh.h

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/*
2-
* Copyright (c) 2022 Nordic Semiconductor ASA
2+
* Copyright (c) 2025 Nordic Semiconductor ASA
3+
* Copyright 2025 NXP
34
*
45
* SPDX-License-Identifier: Apache-2.0
56
*/
@@ -72,18 +73,6 @@ struct usbh_context {
7273
.addr_ba = &ba_##device_name, \
7374
}
7475

75-
/**
76-
* @brief USB Class Code triple
77-
*/
78-
struct usbh_code_triple {
79-
/** Device Class Code */
80-
uint8_t dclass;
81-
/** Class Subclass Code */
82-
uint8_t sub;
83-
/** Class Protocol Code */
84-
uint8_t proto;
85-
};
86-
8776
struct usbh_class_data;
8877

8978
/**
@@ -124,8 +113,6 @@ struct usbh_class_data {
124113
const char *name;
125114
/** Pointer to USB host stack context structure */
126115
struct usbh_context *uhs_ctx;
127-
/** Class code supported by this instance */
128-
struct usbh_code_triple code;
129116
/** Pointer to host support class API */
130117
struct usbh_class_api *api;
131118
/** Pointer to private data */

subsys/usb/host/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ zephyr_library_sources(
99
usbh_ch9.c
1010
usbh_class.c
1111
usbh_core.c
12+
usbh_desc.c
1213
usbh_device.c
1314
)
1415

subsys/usb/host/usbh_class.h

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright (c) 2025 Nordic Semiconductor ASA
3+
* Copyright 2025 NXP
4+
*
5+
* SPDX-License-Identifier: Apache-2.0
6+
*/
7+
8+
#ifndef ZEPHYR_INCLUDE_USBD_CLASS_H
9+
#define ZEPHYR_INCLUDE_USBD_CLASS_H
10+
11+
#include <zephyr/usb/usbh.h>
12+
13+
/**
14+
* @brief Information about a device, which is relevant for matching a particular class.
15+
*/
16+
struct usbh_class_filter {
17+
/** Vendor ID */
18+
uint16_t vid;
19+
/** Product ID */
20+
uint16_t pid;
21+
/** Device Class Code */
22+
uint8_t dclass;
23+
/** Class Subclass Code */
24+
uint8_t sub;
25+
/** Class Protocol Code */
26+
uint8_t proto;
27+
/** Flags that tell which field to match */
28+
uint8_t flags;
29+
};
30+
31+
/** Match a device's vendor ID */
32+
#define USBH_CLASS_MATCH_VID BIT(1)
33+
34+
/** Match a device's product ID */
35+
#define USBH_CLASS_MATCH_PID BIT(2)
36+
37+
/** Match a class code */
38+
#define USBH_CLASS_MATCH_DCLASS BIT(3)
39+
40+
/** Match a subclass code */
41+
#define USBH_CLASS_MATCH_SUB BIT(4)
42+
43+
/** Match a protocol code */
44+
#define USBH_CLASS_MATCH_PROTO BIT(5)
45+
46+
/** Match a code triple */
47+
#define USBH_CLASS_MATCH_CODE_TRIPLE \
48+
(USBH_CLASS_MATCH_DCLASS | USBH_CLASS_MATCH_SUB | USBH_CLASS_MATCH_PROTO)
49+
50+
/**
51+
* @brief Match an USB host class (a driver) against a device descriptor.
52+
*
53+
* @param[in] filters Array of filter rules to match
54+
* @param[in] n_filters Number of rules in the array.
55+
* @param[in] desc USB Device descriptor to match against each rule
56+
* @retval true if the USB Device descriptor matches at least one rule.
57+
*/
58+
bool usbh_class_is_matching(struct usbh_class_filter *const filters, size_t n_filters,
59+
struct usb_device_descriptor *const desc);
60+
61+
#endif /* ZEPHYR_INCLUDE_USBD_CLASS_H */

subsys/usb/host/usbh_desc.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright (c) 2025 Nordic Semiconductor ASA
3+
* Copyright NXP
4+
*
5+
* SPDX-License-Identifier: Apache-2.0
6+
*/
7+
8+
#include "usbh_desc.h"
9+
10+
struct usb_desc_header *usbh_desc_get_by_type(const uint8_t *const start_addr,
11+
const uint8_t *const end_addr,
12+
uint32_t type_mask)
13+
{
14+
const uint8_t *curr_addr = start_addr;
15+
16+
while (curr_addr < end_addr) {
17+
struct usb_desc_header *desc = (void *)curr_addr;
18+
19+
if (desc->bLength == 0) {
20+
break;
21+
}
22+
23+
if ((BIT(desc->bDescriptorType) & type_mask) != 0) {
24+
return desc;
25+
}
26+
}
27+
28+
return NULL;
29+
}

subsys/usb/host/usbh_desc.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright (c) 2025 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
/**
8+
* @file
9+
* @brief Descriptor matching and searching utilities
10+
*
11+
* This file contains utilities to browse USB descriptors returned by a device
12+
* according to the USB Specification 2.0 Chapter 9.
13+
*/
14+
15+
#ifndef ZEPHYR_INCLUDE_USBH_DESC_H
16+
#define ZEPHYR_INCLUDE_USBH_DESC_H
17+
18+
#include <stdint.h>
19+
20+
#include <zephyr/usb/usbh.h>
21+
22+
/**
23+
* @brief Search the first descriptor matching the selected type(s).
24+
*
25+
* @param[in] start_addr Pointer to the beginning of the descriptor array; to search
26+
* @param[in] end_addr Pointer after the end of the descriptor array to search
27+
* @param[in] type_mask Bitmask of bDescriptorType values to match
28+
*
29+
* @return A pointer to the first descriptor matching
30+
*/
31+
struct usb_desc_header *usbh_desc_get_by_type(const uint8_t *const start_addr,
32+
const uint8_t *const end_addr,
33+
uint32_t type_mask);
34+
35+
#endif /* ZEPHYR_INCLUDE_USBH_DESC_H */

0 commit comments

Comments
 (0)