@@ -59,6 +59,8 @@ extern "C" {
5959 */
6060#define USB_STRING_DESCRIPTOR_LENGTH (s ) (sizeof(s) * 2)
6161
62+ struct usbd_context ;
63+
6264/** Used internally to keep descriptors in order
6365 * @cond INTERNAL_HIDDEN
6466 */
@@ -89,6 +91,54 @@ struct usbd_str_desc_data {
8991 unsigned int use_hwinfo : 1 ;
9092};
9193
94+ /**
95+ * USBD vendor request node
96+ *
97+ * Vendor request node is identified by the vendor code and is used to register
98+ * callbacks to handle the vendor request with the receiving device.
99+ * When the device stack receives a request with type Vendor and recipient
100+ * Device, and bRequest value equal to the vendor request code, it will call
101+ * the vendor callbacks depending on the direction of the request.
102+ *
103+ * Example callback code fragment:
104+ *
105+ * @code{.c}
106+ * static int foo_to_host_cb(const struct usbd_context *const ctx,
107+ * const struct usb_setup_packet *const setup,
108+ * struct net_buf *const buf)
109+ * {
110+ * if (setup->wIndex == WEBUSB_REQ_GET_URL) {
111+ * uint8_t index = USB_GET_DESCRIPTOR_INDEX(setup->wValue);
112+ *
113+ * if (index != SAMPLE_WEBUSB_LANDING_PAGE) {
114+ * return -ENOTSUP;
115+ * }
116+ *
117+ * net_buf_add_mem(buf, &webusb_origin_url,
118+ * MIN(net_buf_tailroom(buf), sizeof(webusb_origin_url)));
119+ *
120+ * return 0;
121+ * }
122+ *
123+ * return -ENOTSUP;
124+ * }
125+ * @endcode
126+ */
127+ struct usbd_vreq_node {
128+ /** Node information for the dlist */
129+ sys_dnode_t node ;
130+ /** Vendor code (bRequest value) */
131+ const uint8_t code ;
132+ /** Vendor request callback for device-to-host direction */
133+ int (* to_host )(const struct usbd_context * const ctx ,
134+ const struct usb_setup_packet * const setup ,
135+ struct net_buf * const buf );
136+ /** Vendor request callback for host-to-device direction */
137+ int (* to_dev )(const struct usbd_context * const ctx ,
138+ const struct usb_setup_packet * const setup ,
139+ const struct net_buf * const buf );
140+ };
141+
92142/**
93143 * USBD BOS Device Capability descriptor data
94144 */
@@ -199,8 +249,6 @@ struct usbd_status {
199249 enum usbd_speed speed : 2 ;
200250};
201251
202- struct usbd_context ;
203-
204252/**
205253 * @brief Callback type definition for USB device message delivery
206254 *
@@ -238,6 +286,8 @@ struct usbd_context {
238286 sys_slist_t fs_configs ;
239287 /** slist to manage High-Speed device configurations */
240288 sys_slist_t hs_configs ;
289+ /** dlist to manage vendor requests with recipient device */
290+ sys_dlist_t vreqs ;
241291 /** Status of the USB device support */
242292 struct usbd_status status ;
243293 /** Pointer to Full-Speed device descriptor */
@@ -597,6 +647,21 @@ static inline void *usbd_class_get_private(const struct usbd_class_data *const c
597647 .bDescriptorType = USB_DESC_BOS, \
598648 }
599649
650+ /**
651+ * @brief Define a vendor request with recipient device
652+ *
653+ * @param name Vendor request identifier
654+ * @param vcode Vendor request code
655+ * @param vto_host Vendor callback for to-host direction request
656+ * @param vto_dev Vendor callback for to-device direction request
657+ */
658+ #define USBD_VREQUEST_DEFINE (name , vcode , vto_host , vto_dev ) \
659+ static struct usbd_vreq_node name = { \
660+ .code = vcode, \
661+ .to_host = vto_host, \
662+ .to_dev = vto_dev, \
663+ }
664+
600665/**
601666 * @brief Define USB device support class data
602667 *
@@ -1056,6 +1121,20 @@ int usbd_config_maxpower(struct usbd_context *const uds_ctx,
10561121 */
10571122bool usbd_can_detect_vbus (struct usbd_context * const uds_ctx );
10581123
1124+ /**
1125+ * @brief Register an USB vendor request with recipient device
1126+ *
1127+ * The vendor request with the recipient device applies to all configurations
1128+ * within the device.
1129+ *
1130+ * @param[in] uds_ctx Pointer to USB device support context
1131+ * @param[in] vreq_nd Pointer to vendor request node
1132+ *
1133+ * @return 0 on success, other values on fail.
1134+ */
1135+ int usbd_device_register_vreq (struct usbd_context * const uds_ctx ,
1136+ struct usbd_vreq_node * const vreq_nd );
1137+
10591138/**
10601139 * @}
10611140 */
0 commit comments