1 /*
2  * Copyright (c) 2022 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef ZEPHYR_INCLUDE_USBD_CLASS_H
8 #define ZEPHYR_INCLUDE_USBD_CLASS_H
9 
10 #include <zephyr/usb/usbd.h>
11 
12 /**
13  * @brief Handle endpoint transfer result
14  *
15  * @param[in] uds_ctx Pointer to device context
16  * @param[in] buf     Pointer to UDC request buffer
17  * @param[in] err     Transfer status
18  *
19  * @return usbd_class_request() return value
20  */
21 int usbd_class_handle_xfer(struct usbd_context *const uds_ctx,
22 			   struct net_buf *const buf,
23 			   const int err);
24 
25 /**
26  * @brief Calculate the length of the class function descriptor
27  *
28  * Calculate the length of the class instance function descriptor.
29  * Calculated length does not include any string descriptors that may be
30  * used by the class instance.
31  *
32  * @param[in] c_data Pointer to a class data
33  * @param[in] speed Speed-dependent descriptor selector
34  *
35  * @return Length of the class descriptor
36  */
37 size_t usbd_class_desc_len(struct usbd_class_data *const c_data,
38 			   const enum usbd_speed speed);
39 
40 /**
41  * @brief Get class context by bInterfaceNumber value
42  *
43  * The function searches the class instance list for the interface number.
44  *
45  * @param[in] uds_ctx Pointer to device context
46  * @param[in] inum    Interface number
47  *
48  * @return Class c_nd pointer or NULL
49  */
50 struct usbd_class_node *usbd_class_get_by_iface(struct usbd_context *uds_ctx,
51 						uint8_t i_n);
52 
53 /**
54  * @brief Get class context by configuration and interface number
55  *
56  * @param[in] uds_ctx Pointer to device context
57  * @param[in] speed   Speed the configuration number refers to
58  * @param[in] cnum    Configuration number
59  * @param[in] inum    Interface number
60  *
61  * @return Class c_nd pointer or NULL
62  */
63 struct usbd_class_node *usbd_class_get_by_config(struct usbd_context *uds_ctx,
64 						 const enum usbd_speed speed,
65 						 uint8_t cnum,
66 						 uint8_t inum);
67 
68 /**
69  * @brief Get class context by endpoint address
70  *
71  * The function searches the class instance list for the endpoint address.
72  *
73  * @param[in] uds_ctx Pointer to device context
74  * @param[in] ep      Endpoint address
75  *
76  * @return Class c_nd pointer or NULL
77  */
78 struct usbd_class_node *usbd_class_get_by_ep(struct usbd_context *uds_ctx,
79 					     uint8_t ep);
80 
81 /**
82  * @brief Get class context by request (bRequest)
83  *
84  * The function searches the class instance list and
85  * compares the vendor request table with request value.
86  * The function is only used if the request type is Vendor and
87  * request recipient is Device.
88  * Accordingly only the first class instance can be found.
89  *
90  * @param[in] uds_ctx Pointer to device context
91  * @param[in] request bRequest value
92  *
93  * @return Class c_nd pointer or NULL
94  */
95 struct usbd_class_node *usbd_class_get_by_req(struct usbd_context *uds_ctx,
96 					      uint8_t request);
97 
98 /**
99  * @brief Remove all registered class instances from a configuration
100  *
101  * @param[in] uds_ctx Pointer to device context
102  * @param[in] speed   Speed the configuration number applies to
103  * @param[in] cfg     Configuration number (bConfigurationValue)
104  *
105  * @return 0 on success, other values on fail.
106  */
107 int usbd_class_remove_all(struct usbd_context *const uds_ctx,
108 			  const enum usbd_speed speed,
109 			  const uint8_t cfg);
110 
111 #endif /* ZEPHYR_INCLUDE_USBD_CLASS_H */
112