@@ -61,6 +61,8 @@ extern "C" {
6161 */
6262#define USB_STRING_DESCRIPTOR_LENGTH (s ) (sizeof(s) * 2)
6363
64+ struct usbd_context ;
65+
6466/** Used internally to keep descriptors in order
6567 * @cond INTERNAL_HIDDEN
6668 */
@@ -92,6 +94,54 @@ struct usbd_str_desc_data {
9294 unsigned int use_hwinfo : 1 ;
9395};
9496
97+ /**
98+ * USBD vendor request node
99+ *
100+ * Vendor request node is identified by the vendor code and is used to register
101+ * callbacks to handle the vendor request with the receiving device.
102+ * When the device stack receives a request with type Vendor and recipient
103+ * Device, and bRequest value equal to the vendor request code, it will call
104+ * the vendor callbacks depending on the direction of the request.
105+ *
106+ * Example callback code fragment:
107+ *
108+ * @code{.c}
109+ * static int foo_to_host_cb(const struct usbd_context *const ctx,
110+ * const struct usb_setup_packet *const setup,
111+ * struct net_buf *const buf)
112+ * {
113+ * if (setup->wIndex == WEBUSB_REQ_GET_URL) {
114+ * uint8_t index = USB_GET_DESCRIPTOR_INDEX(setup->wValue);
115+ *
116+ * if (index != SAMPLE_WEBUSB_LANDING_PAGE) {
117+ * return -ENOTSUP;
118+ * }
119+ *
120+ * net_buf_add_mem(buf, &webusb_origin_url,
121+ * MIN(net_buf_tailroom(buf), sizeof(webusb_origin_url)));
122+ *
123+ * return 0;
124+ * }
125+ *
126+ * return -ENOTSUP;
127+ * }
128+ * @endcode
129+ */
130+ struct usbd_vreq_node {
131+ /** Node information for the dlist */
132+ sys_dnode_t node ;
133+ /** Vendor code (bRequest value) */
134+ const uint8_t code ;
135+ /** Vendor request callback for device-to-host direction */
136+ int (* to_host )(const struct usbd_context * const ctx ,
137+ const struct usb_setup_packet * const setup ,
138+ struct net_buf * const buf );
139+ /** Vendor request callback for host-to-device direction */
140+ int (* to_dev )(const struct usbd_context * const ctx ,
141+ const struct usb_setup_packet * const setup ,
142+ const struct net_buf * const buf );
143+ };
144+
95145/**
96146 * USBD BOS Device Capability descriptor data
97147 */
@@ -204,8 +254,6 @@ struct usbd_status {
204254 enum usbd_speed speed : 2 ;
205255};
206256
207- struct usbd_context ;
208-
209257/**
210258 * @brief Callback type definition for USB device message delivery
211259 *
@@ -243,6 +291,8 @@ struct usbd_context {
243291 sys_slist_t fs_configs ;
244292 /** slist to manage High-Speed device configurations */
245293 sys_slist_t hs_configs ;
294+ /** dlist to manage vendor requests with recipient device */
295+ sys_dlist_t vreqs ;
246296 /** Status of the USB device support */
247297 struct usbd_status status ;
248298 /** Pointer to Full-Speed device descriptor */
@@ -618,6 +668,21 @@ static inline void *usbd_class_get_private(const struct usbd_class_data *const c
618668 .bDescriptorType = USB_DESC_BOS, \
619669 }
620670
671+ /**
672+ * @brief Define a vendor request with recipient device
673+ *
674+ * @param name Vendor request identifier
675+ * @param vcode Vendor request code
676+ * @param vto_host Vendor callback for to-host direction request
677+ * @param vto_dev Vendor callback for to-device direction request
678+ */
679+ #define USBD_VREQUEST_DEFINE (name , vcode , vto_host , vto_dev ) \
680+ static struct usbd_vreq_node name = { \
681+ .code = vcode, \
682+ .to_host = vto_host, \
683+ .to_dev = vto_dev, \
684+ }
685+
621686/**
622687 * @brief Define USB device support class data
623688 *
@@ -1088,6 +1153,20 @@ int usbd_config_maxpower(struct usbd_context *const uds_ctx,
10881153 */
10891154bool usbd_can_detect_vbus (struct usbd_context * const uds_ctx );
10901155
1156+ /**
1157+ * @brief Register an USB vendor request with recipient device
1158+ *
1159+ * The vendor request with the recipient device applies to all configurations
1160+ * within the device.
1161+ *
1162+ * @param[in] uds_ctx Pointer to USB device support context
1163+ * @param[in] vreq_nd Pointer to vendor request node
1164+ *
1165+ * @return 0 on success, other values on fail.
1166+ */
1167+ int usbd_device_register_vreq (struct usbd_context * const uds_ctx ,
1168+ struct usbd_vreq_node * const vreq_nd );
1169+
10911170/**
10921171 * @}
10931172 */
0 commit comments