1 /*
2  * Copyright (c) 2018 Intel Corporation
3  * Copyright (c) 2018,2021 Nordic Semiconductor ASA
4  *
5  * SPDX-License-Identifier: Apache-2.0
6  */
7 
8 /**
9  * @file
10  * @brief USB HID Class device API header
11  */
12 
13 #ifndef ZEPHYR_INCLUDE_USB_HID_CLASS_DEVICE_H_
14 #define ZEPHYR_INCLUDE_USB_HID_CLASS_DEVICE_H_
15 
16 #include <zephyr/usb/class/hid.h>
17 
18 #ifdef __cplusplus
19 extern "C" {
20 #endif
21 
22 /**
23  * @brief usb_hid.h API
24  * @defgroup usb_hid_class USB HID class API
25  * @ingroup usb
26  * @{
27  */
28 
29 /**
30  * @defgroup usb_hid_device_api HID class USB specific definitions
31  * @{
32  */
33 
34 typedef int (*hid_cb_t)(const struct device *dev,
35 			struct usb_setup_packet *setup, int32_t *len,
36 			uint8_t **data);
37 typedef void (*hid_int_ready_callback)(const struct device *dev);
38 typedef void (*hid_protocol_cb_t)(const struct device *dev, uint8_t protocol);
39 typedef void (*hid_idle_cb_t)(const struct device *dev, uint16_t report_id);
40 
41 /**
42  * @brief USB HID device interface
43  */
44 struct hid_ops {
45 	hid_cb_t get_report;
46 	hid_cb_t set_report;
47 	hid_protocol_cb_t protocol_change;
48 	hid_idle_cb_t on_idle;
49 	/*
50 	 * int_in_ready is an optional callback that is called when
51 	 * the current interrupt IN transfer has completed.  This can
52 	 * be used to wait for the endpoint to go idle or to trigger
53 	 * the next transfer.
54 	 */
55 	hid_int_ready_callback int_in_ready;
56 #ifdef CONFIG_ENABLE_HID_INT_OUT_EP
57 	hid_int_ready_callback int_out_ready;
58 #endif
59 };
60 
61 /**
62  * @brief Register HID device
63  *
64  * @param[in]  dev          Pointer to USB HID device
65  * @param[in]  desc         Pointer to HID report descriptor
66  * @param[in]  size         Size of HID report descriptor
67  * @param[in]  op           Pointer to USB HID device interrupt struct
68  */
69 void usb_hid_register_device(const struct device *dev,
70 			     const uint8_t *desc,
71 			     size_t size,
72 			     const struct hid_ops *op);
73 
74 /**
75  * @brief Write to USB HID interrupt endpoint buffer
76  *
77  * @param[in]  dev          Pointer to USB HID device
78  * @param[in]  data         Pointer to data buffer
79  * @param[in]  data_len     Length of data to copy
80  * @param[out] bytes_ret    Bytes written to the EP buffer.
81  *
82  * @return 0 on success, negative errno code on fail.
83  */
84 int hid_int_ep_write(const struct device *dev,
85 		     const uint8_t *data,
86 		     uint32_t data_len,
87 		     uint32_t *bytes_ret);
88 
89 /**
90  * @brief Read from USB HID interrupt endpoint buffer
91  *
92  * @param[in]  dev          Pointer to USB HID device
93  * @param[in]  data         Pointer to data buffer
94  * @param[in]  max_data_len Max length of data to copy
95  * @param[out] ret_bytes    Number of bytes to copy.  If data is NULL and
96  *                          max_data_len is 0 the number of bytes
97  *                          available in the buffer will be returned.
98  *
99  * @return 0 on success, negative errno code on fail.
100  */
101 int hid_int_ep_read(const struct device *dev,
102 		    uint8_t *data,
103 		    uint32_t max_data_len,
104 		    uint32_t *ret_bytes);
105 
106 /**
107  * @brief Set USB HID class Protocol Code
108  *
109  * @details Should be called before usb_hid_init().
110  *
111  * @param[in]  dev          Pointer to USB HID device
112  * @param[in]  proto_code   Protocol Code to be used for bInterfaceProtocol
113  *
114  * @return 0 on success, negative errno code on fail.
115  */
116 int usb_hid_set_proto_code(const struct device *dev, uint8_t proto_code);
117 
118 /**
119  * @brief Initialize USB HID class support
120  *
121  * @param[in]  dev          Pointer to USB HID device
122  *
123  * @return 0 on success, negative errno code on fail.
124  */
125 int usb_hid_init(const struct device *dev);
126 
127 /**
128  * @}
129  */
130 
131 /**
132  * @}
133  */
134 
135 #ifdef __cplusplus
136 }
137 #endif
138 
139 #endif /* ZEPHYR_INCLUDE_USB_HID_CLASS_DEVICE_H_ */
140