Zephyr API Documentation  3.6.0
A Scalable Open Source RTOS
3.6.0
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
usbd.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2022 Nordic Semiconductor ASA
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
14#ifndef ZEPHYR_INCLUDE_USBD_H_
15#define ZEPHYR_INCLUDE_USBD_H_
16
17#include <zephyr/device.h>
18#include <zephyr/usb/usb_ch9.h>
19#include <zephyr/net/buf.h>
21#include <zephyr/sys/slist.h>
22#include <zephyr/logging/log.h>
24
25#ifdef __cplusplus
26extern "C" {
27#endif
28
36/*
37 * The USB Unicode bString is encoded in UTF16LE, which means it takes up
38 * twice the amount of bytes than the same string encoded in ASCII7.
39 * Use this macro to determine the length of the bString array.
40 *
41 * bString length without null character:
42 * bString_length = (sizeof(initializer_string) - 1) * 2
43 * or:
44 * bString_length = sizeof(initializer_string) * 2 - 2
45 */
46#define USB_BSTRING_LENGTH(s) (sizeof(s) * 2 - 2)
47
48/*
49 * The length of the string descriptor (bLength) is calculated from the
50 * size of the two octets bLength and bDescriptorType plus the
51 * length of the UTF16LE string:
52 *
53 * bLength = 2 + bString_length
54 * bLength = 2 + sizeof(initializer_string) * 2 - 2
55 * bLength = sizeof(initializer_string) * 2
56 * Use this macro to determine the bLength of the string descriptor.
57 */
58#define USB_STRING_DESCRIPTOR_LENGTH(s) (sizeof(s) * 2)
59
60/* Used internally to keep descriptors in order */
67};
68
79 unsigned int idx : 8;
81 unsigned int utype : 8;
83 unsigned int utf16le : 1;
85 unsigned int custom_sn : 1;
87 void *desc;
88};
89
102 void *desc;
105};
106
107/* TODO: Kconfig option USBD_NUMOF_INTERFACES_MAX? */
108#define USBD_NUMOF_INTERFACES_MAX 16U
109
120};
121
122
141};
142
148 unsigned int initialized : 1;
150 unsigned int enabled : 1;
152 unsigned int suspended : 1;
154 unsigned int rwup : 1;
155};
156
165 const char *name;
169 const struct device *dev;
179 void *desc;
180};
181
187 const uint8_t *reqs;
190};
191
193#define USBD_CCTX_REGISTERED 0
194
195struct usbd_class_node;
196
202 void (*feature_halt)(struct usbd_class_node *const node,
203 uint8_t ep, bool halted);
204
206 void (*update)(struct usbd_class_node *const node,
207 uint8_t iface, uint8_t alternate);
208
210 int (*control_to_dev)(struct usbd_class_node *const node,
211 const struct usb_setup_packet *const setup,
212 const struct net_buf *const buf);
213
215 int (*control_to_host)(struct usbd_class_node *const node,
216 const struct usb_setup_packet *const setup,
217 struct net_buf *const buf);
218
220 int (*request)(struct usbd_class_node *const node,
221 struct net_buf *buf, int err);
222
224 void (*suspended)(struct usbd_class_node *const node);
225
227 void (*resumed)(struct usbd_class_node *const node);
228
230 void (*sof)(struct usbd_class_node *const node);
231
233 void (*enable)(struct usbd_class_node *const node);
234
236 void (*disable)(struct usbd_class_node *const node);
237
239 int (*init)(struct usbd_class_node *const node);
240
242 void (*shutdown)(struct usbd_class_node *const node);
243};
244
254 void *desc;
270 void *priv;
271};
272
277 const char *name;
279 const struct usbd_class_api *api;
282};
283
284#define USBD_DEVICE_DEFINE(device_name, uhc_dev, vid, pid) \
285 static struct usb_device_descriptor \
286 desc_##device_name = { \
287 .bLength = sizeof(struct usb_device_descriptor), \
288 .bDescriptorType = USB_DESC_DEVICE, \
289 .bcdUSB = sys_cpu_to_le16(USB_SRN_2_0), \
290 .bDeviceClass = USB_BCC_MISCELLANEOUS, \
291 .bDeviceSubClass = 2, \
292 .bDeviceProtocol = 1, \
293 .bMaxPacketSize0 = USB_CONTROL_EP_MPS, \
294 .idVendor = vid, \
295 .idProduct = pid, \
296 .bcdDevice = sys_cpu_to_le16(USB_BCD_DRN), \
297 .iManufacturer = 0, \
298 .iProduct = 0, \
299 .iSerialNumber = 0, \
300 .bNumConfigurations = 0, \
301 }; \
302 static STRUCT_SECTION_ITERABLE(usbd_contex, device_name) = { \
303 .name = STRINGIFY(device_name), \
304 .dev = uhc_dev, \
305 .desc = &desc_##device_name, \
306 }
307
308#define USBD_CONFIGURATION_DEFINE(name, attrib, power) \
309 static struct usb_cfg_descriptor \
310 cfg_desc_##name = { \
311 .bLength = sizeof(struct usb_cfg_descriptor), \
312 .bDescriptorType = USB_DESC_CONFIGURATION, \
313 .wTotalLength = 0, \
314 .bNumInterfaces = 0, \
315 .bConfigurationValue = 1, \
316 .iConfiguration = 0, \
317 .bmAttributes = USB_SCD_RESERVED | (attrib), \
318 .bMaxPower = (power), \
319 }; \
320 BUILD_ASSERT((power) < 256, "Too much power"); \
321 static struct usbd_config_node name = { \
322 .desc = &cfg_desc_##name, \
323 }
324
338#define USBD_DESC_LANG_DEFINE(name) \
339 static struct usb_string_descriptor \
340 string_desc_##name = { \
341 .bLength = sizeof(struct usb_string_descriptor), \
342 .bDescriptorType = USB_DESC_STRING, \
343 .bString = sys_cpu_to_le16(0x0409), \
344 }; \
345 static struct usbd_desc_node name = { \
346 .idx = 0, \
347 .utype = USBD_DUT_STRING_LANG, \
348 .desc = &string_desc_##name, \
349 }
350
351#define USBD_DESC_STRING_DEFINE(d_name, d_string, d_utype) \
352 struct usb_string_descriptor_##d_name { \
353 uint8_t bLength; \
354 uint8_t bDescriptorType; \
355 uint8_t bString[USB_BSTRING_LENGTH(d_string)]; \
356 } __packed; \
357 static struct usb_string_descriptor_##d_name \
358 string_desc_##d_name = { \
359 .bLength = USB_STRING_DESCRIPTOR_LENGTH(d_string), \
360 .bDescriptorType = USB_DESC_STRING, \
361 .bString = d_string, \
362 }; \
363 static struct usbd_desc_node d_name = { \
364 .utype = d_utype, \
365 .desc = &string_desc_##d_name, \
366 }
367
379#define USBD_DESC_MANUFACTURER_DEFINE(d_name, d_string) \
380 USBD_DESC_STRING_DEFINE(d_name, d_string, USBD_DUT_STRING_MANUFACTURER)
381
393#define USBD_DESC_PRODUCT_DEFINE(d_name, d_string) \
394 USBD_DESC_STRING_DEFINE(d_name, d_string, USBD_DUT_STRING_PRODUCT)
395
408#define USBD_DESC_SERIAL_NUMBER_DEFINE(d_name, d_string) \
409 USBD_DESC_STRING_DEFINE(d_name, d_string, USBD_DUT_STRING_SERIAL_NUMBER)
410
411#define USBD_DEFINE_CLASS(class_name, class_api, class_data) \
412 static STRUCT_SECTION_ITERABLE(usbd_class_node, class_name) = { \
413 .name = STRINGIFY(class_name), \
414 .api = class_api, \
415 .data = class_data, \
416 }
417
423#define VENDOR_REQ_DEFINE(_reqs, _len) \
424 { \
425 .reqs = (const uint8_t *)(_reqs), \
426 .len = (_len), \
427 }
428
433#define USBD_VENDOR_REQ(_reqs...) \
434 VENDOR_REQ_DEFINE(((uint8_t []) { _reqs }), \
435 sizeof((uint8_t []) { _reqs }))
436
437
449 struct usbd_desc_node *dn);
450
460 struct usbd_config_node *cd);
461
483 const char *name,
484 uint8_t cfg);
485
500 const char *name,
501 uint8_t cfg);
502
517
528
539
550
560
570
580
592struct net_buf *usbd_ep_ctrl_buf_alloc(struct usbd_contex *const uds_ctx,
593 const uint8_t ep, const size_t size);
594
606struct net_buf *usbd_ep_buf_alloc(const struct usbd_class_node *const c_nd,
607 const uint8_t ep, const size_t size);
608
619int usbd_ep_ctrl_enqueue(struct usbd_contex *const uds_ctx,
620 struct net_buf *const buf);
621
632int usbd_ep_enqueue(const struct usbd_class_node *const c_nd,
633 struct net_buf *const buf);
634
643int usbd_ep_dequeue(struct usbd_contex *uds_ctx, const uint8_t ep);
644
655int usbd_ep_buf_free(struct usbd_contex *uds_ctx, struct net_buf *buf);
656
664bool usbd_is_suspended(struct usbd_contex *uds_ctx);
665
671int usbd_wakeup_request(struct usbd_contex *uds_ctx);
672
681int usbd_device_set_bcd(struct usbd_contex *const uds_ctx,
682 const uint16_t bcd);
683
692int usbd_device_set_vid(struct usbd_contex *const uds_ctx,
693 const uint16_t vid);
694
703int usbd_device_set_pid(struct usbd_contex *const uds_ctx,
704 const uint16_t pid);
705
716int usbd_device_set_code_triple(struct usbd_contex *const uds_ctx,
717 const uint8_t base_class,
718 const uint8_t subclass, const uint8_t protocol);
719
729int usbd_config_attrib_rwup(struct usbd_contex *const uds_ctx,
730 const uint8_t cfg, const bool enable);
731
741int usbd_config_attrib_self(struct usbd_contex *const uds_ctx,
742 const uint8_t cfg, const bool enable);
743
753int usbd_config_maxpower(struct usbd_contex *const uds_ctx,
754 const uint8_t cfg, const uint8_t power);
759#ifdef __cplusplus
760}
761#endif
762
763#endif /* ZEPHYR_INCLUDE_USBD_H_ */
long atomic_t
Definition: atomic_types.h:15
struct _dnode sys_dnode_t
Doubly-linked list node structure.
Definition: dlist.h:55
struct _dnode sys_dlist_t
Doubly-linked list structure.
Definition: dlist.h:51
struct _slist sys_slist_t
Single-linked list structure.
Definition: slist.h:49
struct _snode sys_snode_t
Single-linked list node structure.
Definition: slist.h:39
int usbd_add_descriptor(struct usbd_contex *uds_ctx, struct usbd_desc_node *dn)
Add common USB descriptor.
int usbd_ep_set_halt(struct usbd_contex *uds_ctx, uint8_t ep)
Halt endpoint.
int usbd_ep_dequeue(struct usbd_contex *uds_ctx, const uint8_t ep)
Remove all USB device controller requests from endpoint queue.
int usbd_ep_clear_halt(struct usbd_contex *uds_ctx, uint8_t ep)
Clear endpoint halt.
int usbd_ep_enqueue(const struct usbd_class_node *const c_nd, struct net_buf *const buf)
Queue USB device request.
int usbd_ep_buf_free(struct usbd_contex *uds_ctx, struct net_buf *buf)
Free USB device request buffer.
int usbd_register_class(struct usbd_contex *uds_ctx, const char *name, uint8_t cfg)
Register an USB class instance.
int usbd_wakeup_request(struct usbd_contex *uds_ctx)
Initiate the USB remote wakeup (TBD)
int usbd_ep_ctrl_enqueue(struct usbd_contex *const uds_ctx, struct net_buf *const buf)
Queue USB device control request.
struct net_buf * usbd_ep_ctrl_buf_alloc(struct usbd_contex *const uds_ctx, const uint8_t ep, const size_t size)
Allocate buffer for USB device control request.
int usbd_config_attrib_rwup(struct usbd_contex *const uds_ctx, const uint8_t cfg, const bool enable)
Setup USB device configuration attribute Remote Wakeup.
int usbd_device_set_vid(struct usbd_contex *const uds_ctx, const uint16_t vid)
Set USB device descriptor value idVendor.
int usbd_config_attrib_self(struct usbd_contex *const uds_ctx, const uint8_t cfg, const bool enable)
Setup USB device configuration attribute Self-powered.
bool usbd_is_suspended(struct usbd_contex *uds_ctx)
Checks whether the USB device controller is suspended.
bool usbd_ep_is_halted(struct usbd_contex *uds_ctx, uint8_t ep)
Checks whether the endpoint is halted.
struct net_buf * usbd_ep_buf_alloc(const struct usbd_class_node *const c_nd, const uint8_t ep, const size_t size)
Allocate buffer for USB device request.
usbd_ch9_state
USB device support middle layer runtime state.
Definition: usbd.h:116
int usbd_device_set_bcd(struct usbd_contex *const uds_ctx, const uint16_t bcd)
Set USB device descriptor value bcdUSB.
int usbd_disable(struct usbd_contex *uds_ctx)
Disable the USB device support.
int usbd_shutdown(struct usbd_contex *const uds_ctx)
Shutdown the USB device support.
usbd_desc_usage_type
Definition: usbd.h:61
int usbd_enable(struct usbd_contex *uds_ctx)
Enable the USB device support and registered class instances.
int usbd_config_maxpower(struct usbd_contex *const uds_ctx, const uint8_t cfg, const uint8_t power)
Setup USB device configuration power consumption.
int usbd_device_set_pid(struct usbd_contex *const uds_ctx, const uint16_t pid)
Set USB device descriptor value idProduct.
int usbd_device_set_code_triple(struct usbd_contex *const uds_ctx, const uint8_t base_class, const uint8_t subclass, const uint8_t protocol)
Set USB device descriptor code triple Base Class, SubClass, and Protocol.
int usbd_unregister_class(struct usbd_contex *uds_ctx, const char *name, uint8_t cfg)
Unregister an USB class instance.
int usbd_add_configuration(struct usbd_contex *uds_ctx, struct usbd_config_node *cd)
Add a USB device configuration.
int usbd_init(struct usbd_contex *uds_ctx)
Initialize USB device.
#define USBD_NUMOF_INTERFACES_MAX
Definition: usbd.h:108
@ USBD_STATE_CONFIGURED
Definition: usbd.h:119
@ USBD_STATE_DEFAULT
Definition: usbd.h:117
@ USBD_STATE_ADDRESS
Definition: usbd.h:118
@ USBD_DUT_STRING_MANUFACTURER
Definition: usbd.h:63
@ USBD_DUT_STRING_PRODUCT
Definition: usbd.h:64
@ USBD_DUT_STRING_LANG
Definition: usbd.h:62
@ USBD_DUT_STRING_INTERFACE
Definition: usbd.h:66
@ USBD_DUT_STRING_SERIAL_NUMBER
Definition: usbd.h:65
Buffer management.
__UINT32_TYPE__ uint32_t
Definition: stdint.h:90
__UINT8_TYPE__ uint8_t
Definition: stdint.h:88
__UINT16_TYPE__ uint16_t
Definition: stdint.h:89
Runtime device structure (in ROM) per driver instance.
Definition: device.h:387
Mutex Structure.
Definition: kernel.h:2900
Network buffer representation.
Definition: buf.h:910
uint16_t size
Amount of data that this buffer can store.
Definition: buf.h:942
USB Setup Data packet defined in spec.
Definition: usb_ch9.h:39
Vendor Requests Table.
Definition: usbd.h:185
const uint8_t * reqs
Array of vendor requests supported by the class.
Definition: usbd.h:187
uint8_t len
Length of the array.
Definition: usbd.h:189
USB device support middle layer runtime data.
Definition: usbd.h:126
int ctrl_type
Control type, internally used for stage verification.
Definition: usbd.h:130
uint8_t configuration
USB device stack selected configuration.
Definition: usbd.h:136
uint32_t ep_halt
Halted endpoints bitmap.
Definition: usbd.h:134
bool post_status
Post status stage work required, e.g.
Definition: usbd.h:138
enum usbd_ch9_state state
Protocol state of the USB device stack.
Definition: usbd.h:132
struct usb_setup_packet setup
Setup packet, up-to-date for the respective control request.
Definition: usbd.h:128
uint8_t alternate[16U]
Array to track interfaces alternate settings.
Definition: usbd.h:140
USB device support class instance API.
Definition: usbd.h:200
void(* enable)(struct usbd_class_node *const node)
Class associated configuration is selected.
Definition: usbd.h:233
void(* shutdown)(struct usbd_class_node *const node)
Shutdown of the class implementation.
Definition: usbd.h:242
int(* init)(struct usbd_class_node *const node)
Initialization of the class implementation.
Definition: usbd.h:239
void(* resumed)(struct usbd_class_node *const node)
USB power management handler resumed.
Definition: usbd.h:227
void(* suspended)(struct usbd_class_node *const node)
USB power management handler suspended.
Definition: usbd.h:224
void(* disable)(struct usbd_class_node *const node)
Class associated configuration is disabled.
Definition: usbd.h:236
int(* control_to_dev)(struct usbd_class_node *const node, const struct usb_setup_packet *const setup, const struct net_buf *const buf)
USB control request handler to device.
Definition: usbd.h:210
int(* control_to_host)(struct usbd_class_node *const node, const struct usb_setup_packet *const setup, struct net_buf *const buf)
USB control request handler to host.
Definition: usbd.h:215
void(* feature_halt)(struct usbd_class_node *const node, uint8_t ep, bool halted)
Feature halt state update handler.
Definition: usbd.h:202
void(* sof)(struct usbd_class_node *const node)
Start of Frame.
Definition: usbd.h:230
void(* update)(struct usbd_class_node *const node, uint8_t iface, uint8_t alternate)
Configuration update handler.
Definition: usbd.h:206
int(* request)(struct usbd_class_node *const node, struct net_buf *buf, int err)
Endpoint request completion event handler.
Definition: usbd.h:220
USB device support class data.
Definition: usbd.h:248
void * priv
Pointer to private data.
Definition: usbd.h:270
const struct usbd_cctx_vendor_req * v_reqs
Supported vendor request table, can be NULL.
Definition: usbd.h:256
atomic_t state
Variable to store the state of the class instance.
Definition: usbd.h:268
void * desc
Pointer to a class implementation descriptor that should end with a nil descriptor (bLength = 0 and b...
Definition: usbd.h:254
uint32_t ep_active
Bitmap of the enabled endpoints of the instance.
Definition: usbd.h:264
uint32_t iface_bm
Bitmap of the bInterfaceNumbers of the class instance.
Definition: usbd.h:266
uint32_t ep_assigned
Bitmap of all endpoints assigned to the instance.
Definition: usbd.h:260
struct usbd_contex * uds_ctx
Pointer to USB device stack context structure.
Definition: usbd.h:250
Definition: usbd.h:273
const char * name
Name of the USB device class instance.
Definition: usbd.h:277
sys_snode_t node
Node information for the slist.
Definition: usbd.h:275
const struct usbd_class_api * api
Pointer to device support class API.
Definition: usbd.h:279
struct usbd_class_data * data
Pointer to USB device support class data.
Definition: usbd.h:281
Device configuration node.
Definition: usbd.h:98
sys_snode_t node
slist node struct
Definition: usbd.h:100
sys_slist_t class_list
List of registered classes (functions)
Definition: usbd.h:104
void * desc
Pointer to configuration descriptor.
Definition: usbd.h:102
USB device support runtime context.
Definition: usbd.h:163
sys_dlist_t descriptors
slist to manage descriptors like string, bos
Definition: usbd.h:173
struct usbd_ch9_data ch9_data
Middle layer runtime data.
Definition: usbd.h:171
const char * name
Name of the USB device.
Definition: usbd.h:165
sys_slist_t configs
slist to manage device configurations
Definition: usbd.h:175
const struct device * dev
Pointer to UDC device.
Definition: usbd.h:169
struct k_mutex mutex
Access mutex.
Definition: usbd.h:167
void * desc
Pointer to device descriptor.
Definition: usbd.h:179
struct usbd_status status
Status of the USB device support.
Definition: usbd.h:177
Descriptor node.
Definition: usbd.h:75
unsigned int idx
Descriptor index, required for string descriptors.
Definition: usbd.h:79
unsigned int utf16le
If not set, string descriptor must be converted to UTF16LE.
Definition: usbd.h:83
unsigned int custom_sn
If not set, device stack obtains SN using the hwinfo API.
Definition: usbd.h:85
void * desc
Pointer to a descriptor.
Definition: usbd.h:87
unsigned int utype
Descriptor usage type (not bDescriptorType)
Definition: usbd.h:81
sys_dnode_t node
slist node struct
Definition: usbd.h:77
USB device support status.
Definition: usbd.h:146
unsigned int rwup
USB remote wake-up feature is enabled.
Definition: usbd.h:154
unsigned int enabled
USB device support is enabled.
Definition: usbd.h:150
unsigned int initialized
USB device support is initialized.
Definition: usbd.h:148
unsigned int suspended
USB device is suspended.
Definition: usbd.h:152
Byte order helpers.
USB Chapter 9 structures and definitions.