1 /*
2  * Copyright (c) 2022 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef ZEPHYR_INCLUDE_USBD_CH9_H
8 #define ZEPHYR_INCLUDE_USBD_CH9_H
9 
10 #include <zephyr/usb/usbd.h>
11 
12 /**
13  * @brief Check whether USB device is in default state.
14  *
15  * @param[in] node Pointer to a device context
16  *
17  * @return true if USB device is in default state, false otherwise
18  */
usbd_state_is_default(const struct usbd_contex * const uds_ctx)19 static inline bool usbd_state_is_default(const struct usbd_contex *const uds_ctx)
20 {
21 	return (uds_ctx->ch9_data.state == USBD_STATE_DEFAULT) ?  true : false;
22 }
23 
24 /**
25  * @brief Check whether USB device is in address state.
26  *
27  * @param[in] node Pointer to a device context
28  *
29  * @return true if USB device is in address state, false otherwise
30  */
usbd_state_is_address(const struct usbd_contex * const uds_ctx)31 static inline bool usbd_state_is_address(const struct usbd_contex *const uds_ctx)
32 {
33 	return (uds_ctx->ch9_data.state == USBD_STATE_ADDRESS) ?  true : false;
34 }
35 
36 /**
37  * @brief Check whether USB device is in configured state.
38  *
39  * @param[in] node Pointer to a device context
40  *
41  * @return true if USB device is in configured state, false otherwise
42  */
usbd_state_is_configured(const struct usbd_contex * const uds_ctx)43 static inline bool usbd_state_is_configured(const struct usbd_contex *const uds_ctx)
44 {
45 	return (uds_ctx->ch9_data.state == USBD_STATE_CONFIGURED) ?  true : false;
46 }
47 
48 /**
49  * @brief Get current configuration value
50  *
51  * @param[in] uds_ctx Pointer to a device context
52  *
53  * @return current configuration value
54  */
usbd_get_config_value(const struct usbd_contex * const uds_ctx)55 static inline uint8_t usbd_get_config_value(const struct usbd_contex *const uds_ctx)
56 {
57 	return uds_ctx->ch9_data.configuration;
58 }
59 
60 /**
61  * @brief Set current configuration value
62  *
63  * @param[in] uds_ctx Pointer to a device context
64  * @param[in] value   New configuration value
65  */
usbd_set_config_value(struct usbd_contex * const uds_ctx,const uint8_t value)66 static inline void usbd_set_config_value(struct usbd_contex *const uds_ctx,
67 					  const uint8_t value)
68 {
69 	uds_ctx->ch9_data.configuration = value;
70 }
71 
72 /**
73  * @brief Get interface alternate value
74  *
75  * @param[in] uds_ctx Pointer to a device context
76  * @param[in] iface   Interface number
77  * @param[out] alt    Alternate value
78  *
79  * @return 0 on success, other values on fail.
80  */
usbd_get_alt_value(const struct usbd_contex * const uds_ctx,const uint8_t iface,uint8_t * const alt)81 static inline int usbd_get_alt_value(const struct usbd_contex *const uds_ctx,
82 				     const uint8_t iface,
83 				     uint8_t *const alt)
84 {
85 	if (iface >= USBD_NUMOF_INTERFACES_MAX) {
86 		return -ENOENT;
87 	}
88 
89 	*alt = uds_ctx->ch9_data.alternate[iface];
90 
91 	return 0;
92 }
93 
94 /**
95  * @brief Set interface alternate value
96  *
97  * @param[in] uds_ctx Pointer to a device context
98  * @param[in] iface   Interface number
99  * @param[out] alt    Alternate value
100  *
101  * @return 0 on success, other values on fail.
102  */
usbd_set_alt_value(struct usbd_contex * const uds_ctx,const uint8_t iface,const uint8_t alt)103 static inline int usbd_set_alt_value(struct usbd_contex *const uds_ctx,
104 				     const uint8_t iface,
105 				     const uint8_t alt)
106 {
107 	if (iface >= USBD_NUMOF_INTERFACES_MAX) {
108 		return -ENOENT;
109 	}
110 
111 	uds_ctx->ch9_data.alternate[iface] = alt;
112 
113 	return 0;
114 }
115 
116 /**
117  * @brief Get pointer to last received setup packet
118  *
119  * @param[in] uds_ctx Pointer to a device context
120  *
121  * @return Pointer to last received setup packet
122  */
123 static inline struct usb_setup_packet *
usbd_get_setup_pkt(struct usbd_contex * const uds_ctx)124 usbd_get_setup_pkt(struct usbd_contex *const uds_ctx)
125 {
126 	return &uds_ctx->ch9_data.setup;
127 }
128 
129 /**
130  * @brief Handle control endpoint transfer result
131  *
132  * @param[in] uds_ctx Pointer to a device context
133  * @param[in] buf     Pointer to UDC request buffer
134  * @param[in] err     Transfer status
135  *
136  * @return 0 on success, other values on fail.
137  */
138 int usbd_handle_ctrl_xfer(struct usbd_contex *uds_ctx,
139 			  struct net_buf *buf, int err);
140 
141 /**
142  * @brief Initialize control pipe to default values
143  *
144  * @param[in] uds_ctx Pointer to a device context
145  *
146  * @return 0 on success, other values on fail.
147  */
148 int usbd_init_control_pipe(struct usbd_contex *uds_ctx);
149 
150 
151 #endif /* ZEPHYR_INCLUDE_USBD_CH9_H */
152