Zephyr Project API  3.3.0
A Scalable Open Source RTOS
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>
23
24#ifdef __cplusplus
25extern "C" {
26#endif
27
28/*
29 * The USB Unicode bString is encoded in UTF16LE, which means it takes up
30 * twice the amount of bytes than the same string encoded in ASCII7.
31 * Use this macro to determine the length of the bString array.
32 *
33 * bString length without null character:
34 * bString_length = (sizeof(initializer_string) - 1) * 2
35 * or:
36 * bString_length = sizeof(initializer_string) * 2 - 2
37 */
38#define USB_BSTRING_LENGTH(s) (sizeof(s) * 2 - 2)
39
40/*
41 * The length of the string descriptor (bLength) is calculated from the
42 * size of the two octets bLength and bDescriptorType plus the
43 * length of the UTF16LE string:
44 *
45 * bLength = 2 + bString_length
46 * bLength = 2 + sizeof(initializer_string) * 2 - 2
47 * bLength = sizeof(initializer_string) * 2
48 * Use this macro to determine the bLength of the string descriptor.
49 */
50#define USB_STRING_DESCRIPTOR_LENGTH(s) (sizeof(s) * 2)
51
52#define USBD_DESC_MANUFACTURER_IDX 1
53#define USBD_DESC_PRODUCT_IDX 2
54#define USBD_DESC_SERIAL_NUMBER_IDX 3
55
68 void *desc;
69};
70
83 void *desc;
86};
87
88/* TODO: Kconfig option USBD_NUMOF_INTERFACES_MAX? */
89#define USBD_NUMOF_INTERFACES_MAX 16U
90
101};
102
103
122};
123
129 unsigned int initialized : 1;
131 unsigned int enabled : 1;
133 unsigned int suspended : 1;
135 unsigned int rwup : 1;
136};
137
146 const char *name;
150 const struct device *dev;
160 void *desc;
161};
162
168 const uint8_t *reqs;
171};
172
174#define USBD_CCTX_REGISTERED 0
175
176struct usbd_class_node;
177
183 void (*update)(struct usbd_class_node *const node,
184 uint8_t iface, uint8_t alternate);
185
187 int (*control_to_dev)(struct usbd_class_node *const node,
188 const struct usb_setup_packet *const setup,
189 const struct net_buf *const buf);
190
193 const struct usb_setup_packet *const setup,
194 struct net_buf *const buf);
195
197 int (*request)(struct usbd_class_node *const node,
198 struct net_buf *buf, int err);
199
201 void (*suspended)(struct usbd_class_node *const node);
202
204 void (*resumed)(struct usbd_class_node *const node);
205
207 void (*enable)(struct usbd_class_node *const node);
208
210 void (*disable)(struct usbd_class_node *const node);
211
213 int (*init)(struct usbd_class_node *const node);
214
216 int (*shutdown)(struct usbd_class_node *const node);
217};
218
226 void *desc;
242 void *priv;
243};
244
249 const char *name;
251 const struct usbd_class_api *api;
254};
255
263#define USBD_DEVICE_DEFINE(device_name, uhc_dev, vid, pid) \
264 static struct usb_device_descriptor \
265 desc_##device_name = { \
266 .bLength = sizeof(struct usb_device_descriptor), \
267 .bDescriptorType = USB_DESC_DEVICE, \
268 .bcdUSB = sys_cpu_to_le16(USB_SRN_2_0), \
269 .bDeviceClass = USB_BCC_MISCELLANEOUS, \
270 .bDeviceSubClass = 2, \
271 .bDeviceProtocol = 1, \
272 .bMaxPacketSize0 = USB_CONTROL_EP_MPS, \
273 .idVendor = vid, \
274 .idProduct = pid, \
275 .bcdDevice = sys_cpu_to_le16(USB_BCD_DRN), \
276 .iManufacturer = 0, \
277 .iProduct = 0, \
278 .iSerialNumber = 0, \
279 .bNumConfigurations = 0, \
280 }; \
281 static STRUCT_SECTION_ITERABLE(usbd_contex, device_name) = { \
282 .name = STRINGIFY(device_name), \
283 .dev = uhc_dev, \
284 .desc = &desc_##device_name, \
285 }
286
287#define USBD_CONFIGURATION_DEFINE(name, attrib, power) \
288 static struct usb_cfg_descriptor \
289 cfg_desc_##name = { \
290 .bLength = sizeof(struct usb_cfg_descriptor), \
291 .bDescriptorType = USB_DESC_CONFIGURATION, \
292 .wTotalLength = 0, \
293 .bNumInterfaces = 0, \
294 .bConfigurationValue = 1, \
295 .iConfiguration = 0, \
296 .bmAttributes = USB_SCD_RESERVED | (attrib), \
297 .bMaxPower = (power), \
298 }; \
299 BUILD_ASSERT((power) < 256, "Too much power"); \
300 static struct usbd_config_node name = { \
301 .desc = &cfg_desc_##name, \
302 }
303
304
305#define USBD_DESC_LANG_DEFINE(name) \
306 static struct usb_string_descriptor \
307 string_desc_##name = { \
308 .bLength = sizeof(struct usb_string_descriptor), \
309 .bDescriptorType = USB_DESC_STRING, \
310 .bString = sys_cpu_to_le16(0x0409), \
311 }; \
312 static struct usbd_desc_node name = { \
313 .idx = 0, \
314 .desc = &string_desc_##name, \
315 }
316
317#define USBD_DESC_STRING_DEFINE(d_name, d_string, d_idx) \
318 struct usb_string_descriptor_##d_name { \
319 uint8_t bLength; \
320 uint8_t bDescriptorType; \
321 uint8_t bString[USB_BSTRING_LENGTH(d_string)]; \
322 } __packed; \
323 static struct usb_string_descriptor_##d_name \
324 string_desc_##d_name = { \
325 .bLength = USB_STRING_DESCRIPTOR_LENGTH(d_string), \
326 .bDescriptorType = USB_DESC_STRING, \
327 .bString = d_string, \
328 }; \
329 BUILD_ASSERT(d_idx != 0, "Index 0 is not allowed"); \
330 static struct usbd_desc_node d_name = { \
331 .idx = d_idx, \
332 .desc = &string_desc_##d_name, \
333 }
334
335#define USBD_DEFINE_CLASS(class_name, class_api, class_data) \
336 static STRUCT_SECTION_ITERABLE(usbd_class_node, class_name) = { \
337 .name = STRINGIFY(class_name), \
338 .api = class_api, \
339 .data = class_data, \
340 }
341
347#define VENDOR_REQ_DEFINE(_reqs, _len) \
348 { \
349 .reqs = (const uint8_t *)(_reqs), \
350 .len = (_len), \
351 }
352
357#define USBD_VENDOR_REQ(_reqs...) \
358 VENDOR_REQ_DEFINE(((uint8_t []) { _reqs }), \
359 sizeof((uint8_t []) { _reqs }))
360
361
373 struct usbd_desc_node *dn);
374
384 struct usbd_config_node *cd);
385
407 const char *name,
408 uint8_t cfg);
409
424 const char *name,
425 uint8_t cfg);
426
441
452
463
474
484
494
504
516struct net_buf *usbd_ep_ctrl_buf_alloc(struct usbd_contex *const uds_ctx,
517 const uint8_t ep, const size_t size);
518
530struct net_buf *usbd_ep_buf_alloc(const struct usbd_class_node *const c_nd,
531 const uint8_t ep, const size_t size);
532
543int usbd_ep_ctrl_enqueue(struct usbd_contex *const uds_ctx,
544 struct net_buf *const buf);
545
556int usbd_ep_enqueue(const struct usbd_class_node *const c_nd,
557 struct net_buf *const buf);
558
567int usbd_ep_dequeue(struct usbd_contex *uds_ctx, const uint8_t ep);
568
579int usbd_ep_buf_free(struct usbd_contex *uds_ctx, struct net_buf *buf);
580
588bool usbd_is_suspended(struct usbd_contex *uds_ctx);
589
595int usbd_wakeup_request(struct usbd_contex *uds_ctx);
596
605int usbd_device_set_bcd(struct usbd_contex *const uds_ctx,
606 const uint16_t bcd);
607
616int usbd_device_set_vid(struct usbd_contex *const uds_ctx,
617 const uint16_t vid);
618
627int usbd_device_set_pid(struct usbd_contex *const uds_ctx,
628 const uint16_t pid);
629
638int usbd_device_set_class(struct usbd_contex *const uds_ctx,
639 const uint8_t value);
640
649int usbd_device_set_subclass(struct usbd_contex *const uds_ctx,
650 const uint8_t value);
651
660int usbd_device_set_proto(struct usbd_contex *const uds_ctx,
661 const uint8_t value);
662
672int usbd_config_attrib_rwup(struct usbd_contex *const uds_ctx,
673 const uint8_t cfg, const bool enable);
674
684int usbd_config_attrib_self(struct usbd_contex *const uds_ctx,
685 const uint8_t cfg, const bool enable);
686
696int usbd_config_maxpower(struct usbd_contex *const uds_ctx,
697 const uint8_t cfg, const uint8_t power);
702#ifdef __cplusplus
703}
704#endif
705
706#endif /* ZEPHYR_INCLUDE_USBD_H_ */
long atomic_t
Definition: atomic.h:22
Byte order helpers.
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_class(struct usbd_contex *const uds_ctx, const uint8_t value)
Set USB device descriptor value bDeviceClass.
int usbd_device_set_proto(struct usbd_contex *const uds_ctx, const uint8_t value)
Set USB device descriptor value bDeviceProtocol.
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.
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.
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_subclass(struct usbd_contex *const uds_ctx, const uint8_t value)
Set USB device descriptor value bDeviceSubClass.
int usbd_device_set_pid(struct usbd_contex *const uds_ctx, const uint16_t pid)
Set USB device descriptor value idProduct.
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.
Buffer management.
Single-linked list implementation.
struct _slist sys_slist_t
Definition: slist.h:40
struct _snode sys_snode_t
Definition: slist.h:33
__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:378
Definition: kernel.h:2764
Network buffer representation.
Definition: buf.h:905
uint16_t size
Definition: buf.h:937
Definition: usb_ch9.h:39
Vendor Requests Table.
Definition: usbd.h:166
const uint8_t * reqs
Definition: usbd.h:168
uint8_t len
Definition: usbd.h:170
Definition: usbd.h:107
int ctrl_type
Definition: usbd.h:111
uint8_t configuration
Definition: usbd.h:117
uint32_t ep_halt
Definition: usbd.h:115
enum usbd_ch9_state state
Definition: usbd.h:113
struct usb_setup_packet setup
Definition: usbd.h:109
uint8_t alternate[16U]
Definition: usbd.h:121
bool new_address
Definition: usbd.h:119
USB device support class instance API.
Definition: usbd.h:181
void(* enable)(struct usbd_class_node *const node)
Definition: usbd.h:207
int(* init)(struct usbd_class_node *const node)
Definition: usbd.h:213
void(* resumed)(struct usbd_class_node *const node)
Definition: usbd.h:204
void(* suspended)(struct usbd_class_node *const node)
Definition: usbd.h:201
void(* disable)(struct usbd_class_node *const node)
Definition: usbd.h:210
int(* control_to_dev)(struct usbd_class_node *const node, const struct usb_setup_packet *const setup, const struct net_buf *const buf)
Definition: usbd.h:187
int(* control_to_host)(struct usbd_class_node *const node, const struct usb_setup_packet *const setup, struct net_buf *const buf)
Definition: usbd.h:192
void(* update)(struct usbd_class_node *const node, uint8_t iface, uint8_t alternate)
Definition: usbd.h:183
int(* shutdown)(struct usbd_class_node *const node)
Definition: usbd.h:216
int(* request)(struct usbd_class_node *const node, struct net_buf *buf, int err)
Definition: usbd.h:197
USB device support class data.
Definition: usbd.h:222
void * priv
Definition: usbd.h:242
const struct usbd_cctx_vendor_req * v_reqs
Definition: usbd.h:228
atomic_t state
Definition: usbd.h:240
void * desc
Definition: usbd.h:226
uint32_t ep_active
Definition: usbd.h:236
uint32_t iface_bm
Definition: usbd.h:238
uint32_t ep_assigned
Definition: usbd.h:232
struct usbd_contex * uds_ctx
Definition: usbd.h:224
Definition: usbd.h:245
const char * name
Definition: usbd.h:249
sys_snode_t node
Definition: usbd.h:247
const struct usbd_class_api * api
Definition: usbd.h:251
struct usbd_class_data * data
Definition: usbd.h:253
Definition: usbd.h:79
sys_snode_t node
Definition: usbd.h:81
sys_slist_t class_list
Definition: usbd.h:85
void * desc
Definition: usbd.h:83
Definition: usbd.h:144
struct usbd_ch9_data ch9_data
Definition: usbd.h:152
sys_slist_t descriptors
Definition: usbd.h:154
const char * name
Definition: usbd.h:146
sys_slist_t configs
Definition: usbd.h:156
const struct device * dev
Definition: usbd.h:150
struct k_mutex mutex
Definition: usbd.h:148
void * desc
Definition: usbd.h:160
struct usbd_status status
Definition: usbd.h:158
Definition: usbd.h:62
void * desc
Definition: usbd.h:68
sys_snode_t node
Definition: usbd.h:64
uint32_t idx
Definition: usbd.h:66
Definition: usbd.h:127
unsigned int rwup
Definition: usbd.h:135
unsigned int enabled
Definition: usbd.h:131
unsigned int initialized
Definition: usbd.h:129
unsigned int suspended
Definition: usbd.h:133
USB Chapter 9 structures and definitions.
usbd_ch9_state
Definition: usbd.h:97
@ USBD_STATE_CONFIGURED
Definition: usbd.h:100
@ USBD_STATE_DEFAULT
Definition: usbd.h:98
@ USBD_STATE_ADDRESS
Definition: usbd.h:99
#define USBD_NUMOF_INTERFACES_MAX
Definition: usbd.h:89