Line data Source code
1 1 : /*
2 : * Copyright (c) 2022 Nordic Semiconductor ASA
3 : *
4 : * SPDX-License-Identifier: Apache-2.0
5 : */
6 :
7 : /**
8 : * @file
9 : * @brief New experimental USB device stack APIs and structures
10 : *
11 : * This file contains the USB device stack APIs and structures.
12 : */
13 :
14 : #ifndef ZEPHYR_INCLUDE_USBH_H_
15 : #define ZEPHYR_INCLUDE_USBH_H_
16 :
17 : #include <stdint.h>
18 : #include <zephyr/device.h>
19 : #include <zephyr/net_buf.h>
20 : #include <zephyr/sys/dlist.h>
21 : #include <zephyr/sys/bitarray.h>
22 : #include <zephyr/drivers/usb/uhc.h>
23 : #include <zephyr/sys/iterable_sections.h>
24 :
25 : #ifdef __cplusplus
26 : extern "C" {
27 : #endif
28 :
29 : /**
30 : * @brief USB HOST Core Layer API
31 : * @defgroup usb_host_core_api USB Host Core API
32 : * @ingroup usb
33 : * @{
34 : */
35 :
36 : /**
37 : * USB host support runtime context
38 : */
39 1 : struct usbh_context {
40 : /** Name of the USB device */
41 1 : const char *name;
42 : /** Access mutex */
43 1 : struct k_mutex mutex;
44 : /** Pointer to UHC device struct */
45 1 : const struct device *dev;
46 : /** USB device list */
47 1 : sys_dlist_t udevs;
48 : /** USB root device */
49 1 : struct usb_device *root;
50 : /** Allocated device addresses bit array */
51 1 : struct sys_bitarray *addr_ba;
52 : };
53 :
54 0 : #define USBH_CONTROLLER_DEFINE(device_name, uhc_dev) \
55 : SYS_BITARRAY_DEFINE_STATIC(ba_##device_name, 128); \
56 : static STRUCT_SECTION_ITERABLE(usbh_context, device_name) = { \
57 : .name = STRINGIFY(device_name), \
58 : .mutex = Z_MUTEX_INITIALIZER(device_name.mutex), \
59 : .dev = uhc_dev, \
60 : .addr_ba = &ba_##device_name, \
61 : }
62 :
63 : /**
64 : * @brief USB Class Code triple
65 : */
66 1 : struct usbh_code_triple {
67 : /** Device Class Code */
68 1 : uint8_t dclass;
69 : /** Class Subclass Code */
70 1 : uint8_t sub;
71 : /** Class Protocol Code */
72 1 : uint8_t proto;
73 : };
74 :
75 : /**
76 : * @brief USB host class data and class instance API
77 : */
78 1 : struct usbh_class_data {
79 : /** Class code supported by this instance */
80 1 : struct usbh_code_triple code;
81 :
82 : /** Initialization of the class implementation */
83 : /* int (*init)(struct usbh_context *const uhs_ctx); */
84 : /** Request completion event handler */
85 1 : int (*request)(struct usbh_context *const uhs_ctx,
86 : struct uhc_transfer *const xfer, int err);
87 : /** Device connected handler */
88 1 : int (*connected)(struct usbh_context *const uhs_ctx);
89 : /** Device removed handler */
90 1 : int (*removed)(struct usbh_context *const uhs_ctx);
91 : /** Bus remote wakeup handler */
92 1 : int (*rwup)(struct usbh_context *const uhs_ctx);
93 : /** Bus suspended handler */
94 1 : int (*suspended)(struct usbh_context *const uhs_ctx);
95 : /** Bus resumed handler */
96 1 : int (*resumed)(struct usbh_context *const uhs_ctx);
97 : };
98 :
99 : /**
100 : */
101 0 : #define USBH_DEFINE_CLASS(name) \
102 : static STRUCT_SECTION_ITERABLE(usbh_class_data, name)
103 :
104 :
105 : /**
106 : * @brief Initialize the USB host support;
107 : *
108 : * @param[in] uhs_ctx Pointer to USB host support context
109 : *
110 : * @return 0 on success, other values on fail.
111 : */
112 1 : int usbh_init(struct usbh_context *uhs_ctx);
113 :
114 : /**
115 : * @brief Enable the USB host support and class instances
116 : *
117 : * This function enables the USB host support.
118 : *
119 : * @param[in] uhs_ctx Pointer to USB host support context
120 : *
121 : * @return 0 on success, other values on fail.
122 : */
123 1 : int usbh_enable(struct usbh_context *uhs_ctx);
124 :
125 : /**
126 : * @brief Disable the USB host support
127 : *
128 : * This function disables the USB host support.
129 : *
130 : * @param[in] uhs_ctx Pointer to USB host support context
131 : *
132 : * @return 0 on success, other values on fail.
133 : */
134 1 : int usbh_disable(struct usbh_context *uhs_ctx);
135 :
136 : /**
137 : * @brief Shutdown the USB host support
138 : *
139 : * This function completely disables the USB host support.
140 : *
141 : * @param[in] uhs_ctx Pointer to USB host support context
142 : *
143 : * @return 0 on success, other values on fail.
144 : */
145 1 : int usbh_shutdown(struct usbh_context *const uhs_ctx);
146 :
147 : /**
148 : * @}
149 : */
150 :
151 : #ifdef __cplusplus
152 : }
153 : #endif
154 :
155 : #endif /* ZEPHYR_INCLUDE_USBH_H_ */
|