1 /*
2  * Copyright (c) 2022 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef ZEPHYR_INCLUDE_USBD_DEVICE_H
8 #define ZEPHYR_INCLUDE_USBD_DEVICE_H
9 
10 #include <zephyr/drivers/usb/udc.h>
11 #include <zephyr/usb/usbd.h>
12 
13 /**
14  * @brief Get vendor request node
15  *
16  * Get vendor request node from internal vendor request list.
17  *
18  * @param[in] ctx    Pointer to USB device support context
19  * @param[in] code   Vendor request code
20  *
21  * @return pointer to vendor request node or NULL if not found.
22  */
23 struct usbd_vreq_node *usbd_device_get_vreq(struct usbd_context *const uds_ctx,
24 					    const uint8_t code);
25 
26 /**
27  * @brief Unregister all registered vendor request
28  *
29  * @param[in] uds_ctx Pointer to a device context
30  */
31 void usbd_device_unregister_all_vreq(struct usbd_context *const uds_ctx);
32 
33 /**
34  * @brief Get device descriptor bNumConfigurations value
35  *
36  * @param[in] uds_ctx Pointer to a device context
37  * @param[in] speed   Speed for which the bNumConfigurations should be returned
38  *
39  * @return bNumConfigurations value
40  */
usbd_get_num_configs(const struct usbd_context * const uds_ctx,const enum usbd_speed speed)41 static inline uint8_t usbd_get_num_configs(const struct usbd_context *const uds_ctx,
42 					   const enum usbd_speed speed)
43 {
44 	struct usb_device_descriptor *desc;
45 
46 	if (speed == USBD_SPEED_FS) {
47 		desc = uds_ctx->fs_desc;
48 	} else if (speed == USBD_SPEED_HS) {
49 		desc = uds_ctx->hs_desc;
50 	} else {
51 		return 0;
52 	}
53 
54 	return desc->bNumConfigurations;
55 }
56 
57 
58 /**
59  * @brief Set device descriptor bNumConfigurations value
60  *
61  * @param[in] uds_ctx Pointer to a device context
62  * @param[in] speed   Speed for which the bNumConfigurations should be set
63  * @param[in] value   new bNumConfigurations value
64  */
usbd_set_num_configs(struct usbd_context * const uds_ctx,const enum usbd_speed speed,const uint8_t value)65 static inline void usbd_set_num_configs(struct usbd_context *const uds_ctx,
66 					const enum usbd_speed speed,
67 					const uint8_t value)
68 {
69 	struct usb_device_descriptor *desc;
70 
71 	if (speed == USBD_SPEED_FS) {
72 		desc = uds_ctx->fs_desc;
73 	} else if (speed == USBD_SPEED_HS) {
74 		desc = uds_ctx->hs_desc;
75 	} else {
76 		return;
77 	}
78 
79 	desc->bNumConfigurations = value;
80 }
81 
82 /**
83  * @brief Check whether USB device is enabled
84  *
85  * @param[in] node Pointer to a device context
86  *
87  * @return true if USB device is in enabled, false otherwise
88  */
usbd_is_enabled(const struct usbd_context * const uds_ctx)89 static inline bool usbd_is_enabled(const struct usbd_context *const uds_ctx)
90 {
91 	return uds_ctx->status.enabled;
92 }
93 
94 /**
95  * @brief Check whether USB device is enabled
96  *
97  * @param[in] node Pointer to a device context
98  *
99  * @return true if USB device is in enabled, false otherwise
100  */
usbd_is_initialized(const struct usbd_context * const uds_ctx)101 static inline bool usbd_is_initialized(const struct usbd_context *const uds_ctx)
102 {
103 	return uds_ctx->status.initialized;
104 }
105 
106 /**
107  * @brief Set device suspended status
108  *
109  * @param[in] uds_ctx Pointer to a device context
110  * @param[in] value   new suspended value
111  */
usbd_status_suspended(struct usbd_context * const uds_ctx,const bool value)112 static inline void usbd_status_suspended(struct usbd_context *const uds_ctx,
113 					 const bool value)
114 {
115 	uds_ctx->status.suspended = value;
116 }
117 
118 /**
119  * @brief Lock USB device stack context
120  *
121  * @param[in] node Pointer to a device context
122  */
usbd_device_lock(struct usbd_context * const uds_ctx)123 static inline void usbd_device_lock(struct usbd_context *const uds_ctx)
124 {
125 	k_mutex_lock(&uds_ctx->mutex, K_FOREVER);
126 }
127 
128 /**
129  * @brief Lock USB device stack context
130  *
131  * @param[in] node Pointer to a device context
132  */
usbd_device_unlock(struct usbd_context * const uds_ctx)133 static inline void usbd_device_unlock(struct usbd_context *const uds_ctx)
134 {
135 	k_mutex_unlock(&uds_ctx->mutex);
136 }
137 
138 /**
139  * @brief Init USB device stack core
140  *
141  * @param[in] uds_ctx Pointer to a device context
142  *
143  * @return 0 on success, other values on fail.
144  */
145 int usbd_device_init_core(struct usbd_context *uds_ctx);
146 
147 /**
148  * @brief Shutdown USB device stack core
149  *
150  * @param[in] uds_ctx Pointer to a device context
151  *
152  * @return 0 on success, other values on fail.
153  */
154 int usbd_device_shutdown_core(struct usbd_context *const uds_ctx);
155 
156 #endif /* ZEPHYR_INCLUDE_USBD_DEVICE_H */
157