1 /*!
2     \file    usbh_hid_core.h
3     \brief   header file for the usbh_hid_core.c
4 
5     \version 2020-08-04, V1.1.0, firmware for GD32VF103
6 */
7 
8 /*
9     Copyright (c) 2020, GigaDevice Semiconductor Inc.
10 
11     Redistribution and use in source and binary forms, with or without modification,
12 are permitted provided that the following conditions are met:
13 
14     1. Redistributions of source code must retain the above copyright notice, this
15        list of conditions and the following disclaimer.
16     2. Redistributions in binary form must reproduce the above copyright notice,
17        this list of conditions and the following disclaimer in the documentation
18        and/or other materials provided with the distribution.
19     3. Neither the name of the copyright holder nor the names of its contributors
20        may be used to endorse or promote products derived from this software without
21        specific prior written permission.
22 
23     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
27 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
29 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
30 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
32 OF SUCH DAMAGE.
33 */
34 
35 #ifndef __USBH_HID_CORE_H
36 #define __USBH_HID_CORE_H
37 
38 #include "usb_hid.h"
39 #include "usbh_enum.h"
40 #include "usbh_transc.h"
41 
42 #define HID_MIN_POLL                                    10U
43 #define HID_REPORT_SIZE                                 16U
44 #define HID_MAX_USAGE                                   10U
45 #define HID_MAX_NBR_REPORT_FMT                          10U
46 #define HID_QUEUE_SIZE                                  10U
47 
48 #define HID_ITEM_LONG                                   0xFEU
49 
50 #define HID_ITEM_TYPE_MAIN                              0x00U
51 #define HID_ITEM_TYPE_GLOBAL                            0x01U
52 #define HID_ITEM_TYPE_LOCAL                             0x02U
53 #define HID_ITEM_TYPE_RESERVED                          0x03U
54 
55 #define HID_MAIN_ITEM_TAG_INPUT                         0x08U
56 #define HID_MAIN_ITEM_TAG_OUTPUT                        0x09U
57 #define HID_MAIN_ITEM_TAG_COLLECTION                    0x0AU
58 #define HID_MAIN_ITEM_TAG_FEATURE                       0x0BU
59 #define HID_MAIN_ITEM_TAG_ENDCOLLECTION                 0x0CU
60 
61 #define HID_GLOBAL_ITEM_TAG_USAGE_PAGE                  0x00U
62 #define HID_GLOBAL_ITEM_TAG_LOG_MIN                     0x01U
63 #define HID_GLOBAL_ITEM_TAG_LOG_MAX                     0x02U
64 #define HID_GLOBAL_ITEM_TAG_PHY_MIN                     0x03U
65 #define HID_GLOBAL_ITEM_TAG_PHY_MAX                     0x04U
66 #define HID_GLOBAL_ITEM_TAG_UNIT_EXPONENT               0x05U
67 #define HID_GLOBAL_ITEM_TAG_UNIT                        0x06U
68 #define HID_GLOBAL_ITEM_TAG_REPORT_SIZE                 0x07U
69 #define HID_GLOBAL_ITEM_TAG_REPORT_ID                   0x08U
70 #define HID_GLOBAL_ITEM_TAG_REPORT_COUNT                0x09U
71 #define HID_GLOBAL_ITEM_TAG_PUSH                        0x0AU
72 #define HID_GLOBAL_ITEM_TAG_POP                         0x0BU
73 
74 #define HID_LOCAL_ITEM_TAG_USAGE                        0x00U
75 #define HID_LOCAL_ITEM_TAG_USAGE_MIN                    0x01U
76 #define HID_LOCAL_ITEM_TAG_USAGE_MAX                    0x02U
77 #define HID_LOCAL_ITEM_TAG_DESIGNATOR_INDEX             0x03U
78 #define HID_LOCAL_ITEM_TAG_DESIGNATOR_MIN               0x04U
79 #define HID_LOCAL_ITEM_TAG_DESIGNATOR_MAX               0x05U
80 #define HID_LOCAL_ITEM_TAG_STRING_INDEX                 0x07U
81 #define HID_LOCAL_ITEM_TAG_STRING_MIN                   0x08U
82 #define HID_LOCAL_ITEM_TAG_STRING_MAX                   0x09U
83 #define HID_LOCAL_ITEM_TAG_DELIMITER                    0x0AU
84 
85 #define USB_HID_DESC_SIZE                               9U
86 
87 /* states for HID state machine */
88 typedef enum {
89     HID_INIT = 0U,
90     HID_IDLE,
91     HID_SEND_DATA,
92     HID_BUSY,
93     HID_GET_DATA,
94     HID_SYNC,
95     HID_POLL,
96     HID_ERROR,
97 } hid_state;
98 
99 typedef enum {
100     HID_REQ_INIT = 0U,
101     HID_REQ_IDLE,
102     HID_REQ_GET_REPORT_DESC,
103     HID_REQ_GET_HID_DESC,
104     HID_REQ_SET_IDLE,
105     HID_REQ_SET_PROTOCOL,
106     HID_REQ_SET_REPORT,
107 } hid_ctlstate;
108 
109 typedef enum
110 {
111     HID_MOUSE    = 0x01U,
112     HID_KEYBOARD = 0x02U,
113     HID_UNKNOWN  = 0xFFU,
114 } hid_type;
115 
116 typedef struct _hid_report_data
117 {
118     uint8_t  ReportID;
119     uint8_t  ReportType;
120     uint16_t UsagePage;
121     uint32_t Usage[HID_MAX_USAGE];
122     uint32_t NbrUsage;
123     uint32_t UsageMin;
124     uint32_t UsageMax;
125     int32_t  LogMin;
126     int32_t  LogMax;
127     int32_t  PhyMin;
128     int32_t  PhyMax;
129     int32_t  UnitExp;
130     uint32_t Unit;
131     uint32_t ReportSize;
132     uint32_t ReportCnt;
133     uint32_t Flag;
134     uint32_t PhyUsage;
135     uint32_t AppUsage;
136     uint32_t LogUsage;
137 } hid_report_data;
138 
139 typedef struct _hid_report_ID
140 {
141     uint8_t  size;         /*!< report size return by the device ID */
142     uint8_t  reportID;     /*!< report ID */
143     uint8_t  type;         /*!< report type (INPUT/OUTPUT/FEATURE) */
144 } hid_report_ID;
145 
146 typedef struct  _hid_collection
147 {
148     uint32_t                usage;
149     uint8_t                 type;
150     struct _hid_collection  *next_ptr;
151 } hid_collection;
152 
153 typedef struct _hid_appcollection
154 {
155     uint32_t               usage;
156     uint8_t                type;
157     uint8_t                nbr_report_fmt;
158     hid_report_data        report_data[HID_MAX_NBR_REPORT_FMT];
159 } hid_appcollection;
160 
161 typedef struct
162 {
163      uint8_t  *buf;
164      uint16_t  head;
165      uint16_t  tail;
166      uint16_t  size;
167      uint8_t   lock;
168 } data_fifo;
169 
170 /* structure for HID process */
171 typedef struct _hid_process
172 {
173     uint8_t              pipe_in;
174     uint8_t              pipe_out;
175     uint8_t              ep_addr;
176     uint8_t              ep_in;
177     uint8_t              ep_out;
178     __IO uint8_t         data_ready;
179     uint8_t              *pdata;
180     uint16_t             len;
181     uint16_t             poll;
182 
183     __IO uint32_t        timer;
184 
185     data_fifo            fifo;
186     usb_desc_hid         hid_desc;
187     hid_report_data      hid_report;
188 
189     hid_state            state;
190     hid_ctlstate         ctl_state;
191     usbh_status          (*init)(usb_core_driver *pudev, usbh_host *puhost);
192     void                 (*machine)(usb_core_driver *pudev, usbh_host *puhost);
193 } usbh_hid_handler;
194 
195 extern usbh_class usbh_hid;
196 
197 /* function declarations */
198 /* set HID report */
199 usbh_status usbh_set_report (usb_core_driver *pudev,
200                              usbh_host *puhost,
201                              uint8_t  report_type,
202                              uint8_t  report_ID,
203                              uint8_t  report_len,
204                              uint8_t *report_buf);
205 /* read data from FIFO */
206 uint16_t usbh_hid_fifo_read (data_fifo *fifo, void *buf, uint16_t nbytes);
207 /* write data to FIFO */
208 uint16_t usbh_hid_fifo_write (data_fifo *fifo, void *buf, uint16_t nbytes);
209 /* initialize FIFO */
210 void usbh_hid_fifo_init (data_fifo *fifo, uint8_t *buf, uint16_t size);
211 
212 #endif /* __USBH_HID_CORE_H */
213