1 /*
2 ********************************************************************************
3 *                                USB Hid Driver
4 *
5 *                (c) Copyright 2006-2010, All winners Co,Ld.
6 *                        All Right Reserved
7 *
8 * FileName      :  HidSpec.h
9 *
10 * Author        :  Javen
11 *
12 * Date          :  2010.06.02
13 *
14 * Description   :  Hid 协议相关
15 *
16 * Others        :  NULL
17 *
18 * History:
19 *       <time>      <version >      <author>        <desc>
20 *      2010.06.02      1.0           Javen          build this file
21 *
22 ********************************************************************************
23 */
24 #ifndef  __HIDPUB_H__
25 #define  __HIDPUB_H__
26 #define __packed    __attribute__((__packed__))
27 
28 //----------------------------------------------------------------------
29 // Hid Class descriptor
30 //----------------------------------------------------------------------
31 /* Sub Class */
32 #define  USB_HID_SUBCLASS_NONE              0x00
33 #define  USB_HID_SUBCLASS_BOOT_INTERFACE    0x01
34 
35 /* Protocol */
36 #define  USB_HID_PROTOCOL_NONE              0x00
37 #define  USB_HID_PROTOCOL_KEYBOARD          0x01
38 #define  USB_HID_PROTOCOL_MOUSE             0x02
39 
40 /* HID class requests */
41 #define  USB_HID_HID_REQ_GET_REPORT         0x01
42 #define  USB_HID_REQ_GET_IDLE               0x02
43 #define  USB_HID_REQ_GET_PROTOCOL           0x03
44 #define  USB_HID_REQ_SET_REPORT             0x09
45 #define  USB_HID_REQ_SET_IDLE               0x0A
46 #define  USB_HID_REQ_SET_PROTOCOL           0x0B
47 
48 /* HID class descriptor types */
49 #define  USB_HID_DT_HID                     (USB_TYPE_CLASS | 0x01)
50 #define  USB_HID_DT_REPORT                  (USB_TYPE_CLASS | 0x02)
51 #define  USB_HID_DT_PHYSICAL                (USB_TYPE_CLASS | 0x03)
52 
53 #define  USB_HID_MAX_DESCRIPTOR_SIZE        4096
54 
55 /* hid spec 1.11. page 68 */
56 typedef struct _usbHidClassDescriptor{
57     unsigned char  bDescriptorType;     /* Report descriptor type               */
58     unsigned short wDescriptorLength;   /* Total length of report descriptor    */
59 }__packed usbHidClassDescriptor_t;
60 
61 
62 typedef struct _usbHidDescriptor{
63     unsigned char  bLength;                 /* size of this descriptor                          */
64     unsigned char  bDescriptorType;         /* Hid decriptor type                               */
65     unsigned short bcdHID;                  /* Hid Class Specification release number in BCD    */
66     unsigned char  bCountryCode;                /* Hardware target country                          */
67     unsigned char  bNumDescriptors;         /* Number of HID class descriptor to follow         */
68 
69     usbHidClassDescriptor_t desc[1];    /* report descriptor */
70 }__packed usbHidDescriptor_t;
71 
72 /* Hid Report type */
73 #define  USB_HID_REPORT_INPUT           0x00
74 #define  USB_HID_REPORT_OUTPUT          0x01
75 #define  USB_HID_REPORT_FEATURE         0x02
76 
77 
78 
79 //----------------------------------------------------------------------
80 // Hid spec
81 //----------------------------------------------------------------------
82 
83 /*
84  * This is the local environment. It is persistent up the next main-item.
85  */
86 #define USB_HID_MAX_USAGES                          8192
87 #define USB_HID_DEFAULT_NUM_COLLECTIONS             16
88 
89 #define USB_HID_REPORT_TYPES        3
90 
91 #define USB_HID_MIN_BUFFER_SIZE     64      /* make sure there is at least a packet size of space */
92 #define USB_HID_MAX_BUFFER_SIZE     4096    /* 4kb */
93 #define USB_HID_CONTROL_FIFO_SIZE   256     /* to init devices with >100 reports */
94 #define USB_HID_OUTPUT_FIFO_SIZE    64
95 
96 
97 /* HID report item format */
98 #define USB_HID_ITEM_FORMAT_SHORT                   0
99 #define USB_HID_ITEM_FORMAT_LONG                    1
100 
101 /* Special tag indicating long items */
102 #define USB_HID_ITEM_TAG_LONG                       15
103 
104 /* HID report descriptor item type (prefix bit 2,3) */
105 #define USB_HID_ITEM_TYPE_MAIN                      0
106 #define USB_HID_ITEM_TYPE_GLOBAL                    1
107 #define USB_HID_ITEM_TYPE_LOCAL                     2
108 #define USB_HID_ITEM_TYPE_RESERVED                  3
109 
110 /* HID report descriptor main item tags */
111 #define USB_HID_MAIN_ITEM_TAG_INPUT                 8
112 #define USB_HID_MAIN_ITEM_TAG_OUTPUT                9
113 #define USB_HID_MAIN_ITEM_TAG_FEATURE               11
114 #define USB_HID_MAIN_ITEM_TAG_BEGIN_COLLECTION      10
115 #define USB_HID_MAIN_ITEM_TAG_END_COLLECTION        12
116 
117 /* HID report descriptor main item contents */
118 #define USB_HID_MAIN_ITEM_CONSTANT                  0x001
119 #define USB_HID_MAIN_ITEM_VARIABLE                  0x002
120 #define USB_HID_MAIN_ITEM_RELATIVE                  0x004
121 #define USB_HID_MAIN_ITEM_WRAP                      0x008
122 #define USB_HID_MAIN_ITEM_NONLINEAR                 0x010
123 #define USB_HID_MAIN_ITEM_NO_PREFERRED              0x020
124 #define USB_HID_MAIN_ITEM_NULL_STATE                0x040
125 #define USB_HID_MAIN_ITEM_VOLATILE                  0x080
126 #define USB_HID_MAIN_ITEM_BUFFERED_BYTE             0x100
127 
128 /* HID report descriptor collection item types */
129 #define USB_HID_COLLECTION_PHYSICAL                 0x00
130 #define USB_HID_COLLECTION_APPLICATION              0x01
131 #define USB_HID_COLLECTION_LOGICAL                  0x02
132 #define USB_HID_COLLECTION_REPORT                   0x03
133 #define USB_HID_COLLECTION_NAMED_ARRAY              0x04
134 #define USB_HID_COLLECTION_USAGE_SWITCH             0x05
135 #define USB_HID_COLLECTION_USAGE_MODIFIER           0x06
136 
137 /* HID report descriptor global item tags */
138 #define USB_HID_GLOBAL_ITEM_TAG_USAGE_PAGE          0
139 #define USB_HID_GLOBAL_ITEM_TAG_LOGICAL_MINIMUM     1
140 #define USB_HID_GLOBAL_ITEM_TAG_LOGICAL_MAXIMUM     2
141 #define USB_HID_GLOBAL_ITEM_TAG_PHYSICAL_MINIMUM    3
142 #define USB_HID_GLOBAL_ITEM_TAG_PHYSICAL_MAXIMUM    4
143 #define USB_HID_GLOBAL_ITEM_TAG_UNIT_EXPONENT       5
144 #define USB_HID_GLOBAL_ITEM_TAG_UNIT                6
145 #define USB_HID_GLOBAL_ITEM_TAG_REPORT_SIZE         7
146 #define USB_HID_GLOBAL_ITEM_TAG_REPORT_ID           8
147 #define USB_HID_GLOBAL_ITEM_TAG_REPORT_COUNT        9
148 #define USB_HID_GLOBAL_ITEM_TAG_PUSH                10
149 #define USB_HID_GLOBAL_ITEM_TAG_POP                 11
150 
151 /* HID report descriptor local item tags */
152 #define USB_HID_LOCAL_ITEM_TAG_USAGE                0
153 #define USB_HID_LOCAL_ITEM_TAG_USAGE_MINIMUM        1
154 #define USB_HID_LOCAL_ITEM_TAG_USAGE_MAXIMUM        2
155 #define USB_HID_LOCAL_ITEM_TAG_DESIGNATOR_INDEX     3
156 #define USB_HID_LOCAL_ITEM_TAG_DESIGNATOR_MINIMUM   4
157 #define USB_HID_LOCAL_ITEM_TAG_DESIGNATOR_MAXIMUM   5
158 #define USB_HID_LOCAL_ITEM_TAG_STRING_INDEX         7
159 #define USB_HID_LOCAL_ITEM_TAG_STRING_MINIMUM       8
160 #define USB_HID_LOCAL_ITEM_TAG_STRING_MAXIMUM       9
161 #define USB_HID_LOCAL_ITEM_TAG_DELIMITER            10
162 
163 /*
164  * We parse each description item into this structure. Short items data
165  * values are expanded to 32-bit signed int, long items contain a pointer
166  * into the data area.
167  */
168 typedef struct _usbHidItem{
169     unsigned int  format;           /* Short items or long items                                */
170 
171     unsigned char      size;            /* Numeric expression specifying size of data               */
172     unsigned char      type;            /* Numeric expression identifying type of item              */
173     unsigned char      tag;         /* Numeric expression specifying the function of the item   */
174 
175     union {
176         unsigned char   Data_u8;        /* 无符号8位数 */
177         char   Data_s8;     /* 有符号8位数 */
178         unsigned short  Data_u16;
179         short  Data_s16;
180         unsigned int  Data_u32;
181         int  Data_s32;
182         unsigned char  *longdata;
183     } data;
184 }usbHidItem_t;
185 
186 /* Usage Page. <<usb hid usage tables>> page14, table1 */
187 #define  USB_HID_USAGE_PAGE_UNDEFINED                       0x00
188 
189 #define  USB_HID_USAGE_PAGE_GENERIC_DESKTOP_CONTROLS        0x01
190 #define  USB_HID_USAGE_PAGE_SIMULATION_CONTROLS             0x02
191 #define  USB_HID_USAGE_PAGE_VR_CONTROLS                     0x03
192 #define  USB_HID_USAGE_PAGE_SPORT_CONTROLS                  0x04
193 #define  USB_HID_USAGE_PAGE_GAME_CONTROLS                   0x05
194 #define  USB_HID_USAGE_PAGE_GENERIC_DEVICE_CONTROLS         0x06
195 #define  USB_HID_USAGE_PAGE_KEYBOARD_KEYPAD                 0x07
196 #define  USB_HID_USAGE_PAGE_LEDS                            0x08
197 #define  USB_HID_USAGE_PAGE_BUTTON                          0x09
198 #define  USB_HID_USAGE_PAGE_ORDINAL                         0x0A
199 #define  USB_HID_USAGE_PAGE_TELEPHONE                       0x0B
200 #define  USB_HID_USAGE_PAGE_CONSUMER                        0x0C
201 #define  USB_HID_USAGE_PAGE_DIGITIZER                       0x0D
202 #define  USB_HID_USAGE_PAGE_PID_PAGE                        0x0F
203 #define  USB_HID_USAGE_PAGE_UNICODE                         0x10
204 #define  USB_HID_USAGE_PAGE_ALPHANUMERIC_DISPLAY            0x14
205 #define  USB_HID_USAGE_PAGE_MEDICAL_INSTRUMENTS             0x40
206 #define  USB_HID_USAGE_PAGE_MONITOR_PAGES                   0x80  //???????
207 #define  USB_HID_USAGE_PAGE_POWER_PAGES                     0x84  //???????
208 #define  USB_HID_USAGE_PAGE_BAR_CODE_SCANNER_PAGE           0x8C
209 #define  USB_HID_USAGE_PAGE_SCALE_PAGE                      0x8D
210 #define  USB_HID_USAGE_PAGE_MEGNETIC_STRIPE_READING_DEVICE  0x8E
211 #define  USB_HID_USAGE_PAGE_RESEVERED_POINT_OF_SALE_PAGES   0x8F
212 #define  USB_HID_USAGE_PAGE_CAMERA_CONTROL_PAGE             0x90
213 #define  USB_HID_USAGE_PAGE_ARCADE_PAGE                     0x91
214 
215 #define  USB_HID_USAGE_PAGE_VENDOR_DEFINED                  0xFF00
216 
217 /* Usage Types. <<usb hid usage tables>> page17~21 */
218 #define  USB_HID_USAGE_TYPES_UNDEFINED                  0x00
219 
220 #define  USB_HID_USAGE_TYPES_CONTROL_UNDEFINED          0x01
221 #define  USB_HID_USAGE_TYPES_CONTROL_LINER              0x02
222 #define  USB_HID_USAGE_TYPES_CONTROL_ON_OFF             0x03
223 #define  USB_HID_USAGE_TYPES_CONTROL_MOMENTARY          0x04
224 #define  USB_HID_USAGE_TYPES_CONTROL_ONE_SHOT           0x05
225 
226 #define  USB_HID_USAGE_TYPES_DATA_UNDEFINED             0x06
227 #define  USB_HID_USAGE_TYPES_DATA_SELECTOR              0x07
228 #define  USB_HID_USAGE_TYPES_DATA_STATIC_VALUE          0x08
229 #define  USB_HID_USAGE_TYPES_DATA_STATIC_FLAG           0x09
230 #define  USB_HID_USAGE_TYPES_DATA_DYNAMIC_VALUE         0x0A
231 #define  USB_HID_USAGE_TYPES_DATA_DYNAMIC_FLAG          0x0B
232 
233 #define  USB_HID_USAGE_TYPES_COLLECTION_UNDEFINED       0x0C
234 #define  USB_HID_USAGE_TYPES_COLLECTION_NAMED_ARRAY     0x0D
235 #define  USB_HID_USAGE_TYPES_COLLECTION_APPLICATION     0x0E
236 #define  USB_HID_USAGE_TYPES_COLLECTION_LOGICAL         0x0F
237 #define  USB_HID_USAGE_TYPES_COLLECTION_PHYSICAL        0x10
238 #define  USB_HID_USAGE_TYPES_COLLECTION_USAGE_SWITCH    0x11
239 #define  USB_HID_USAGE_TYPES_COLLECTION_USAGE_MODIFIER  0x12
240 
241 /* Generic DeskTop Page. <<usb hid usage tables>> page126 */
242 #define  USB_HID_GENERIC_DESKTOP_PAGE_UNDEFINED                 0x00
243 #define  USB_HID_GENERIC_DESKTOP_PAGE_PONITER                   0x01
244 #define  USB_HID_GENERIC_DESKTOP_PAGE_MOUSE                     0x02
245 
246 #define  USB_HID_GENERIC_DESKTOP_PAGE_JOYSTICK                  0x04
247 #define  USB_HID_GENERIC_DESKTOP_PAGE_GAMEPAD                   0x05
248 #define  USB_HID_GENERIC_DESKTOP_PAGE_KEYBOARD                  0x06
249 #define  USB_HID_GENERIC_DESKTOP_PAGE_KEYPAD                    0x07
250 #define  USB_HID_GENERIC_DESKTOP_PAGE_MULTIAXIS_CONTROLLER      0x08
251 #define  USB_HID_GENERIC_DESKTOP_PAGE_TABLET_PC_SYSTEM_CONTROLS 0x09
252 
253 #define  USB_HID_GENERIC_DESKTOP_PAGE_X                         0x30
254 #define  USB_HID_GENERIC_DESKTOP_PAGE_Y                         0x31
255 #define  USB_HID_GENERIC_DESKTOP_PAGE_Z                         0x32
256 #define  USB_HID_GENERIC_DESKTOP_PAGE_RX                        0x33
257 #define  USB_HID_GENERIC_DESKTOP_PAGE_RY                        0x34
258 #define  USB_HID_GENERIC_DESKTOP_PAGE_RZ                        0x35
259 #define  USB_HID_GENERIC_DESKTOP_PAGE_SLIDER                    0x36
260 #define  USB_HID_GENERIC_DESKTOP_PAGE_DIAL                      0x37
261 #define  USB_HID_GENERIC_DESKTOP_PAGE_WHEEL                     0x38
262 #define  USB_HID_GENERIC_DESKTOP_PAGE_HAT_SWITCH                0x39
263 #define  USB_HID_GENERIC_DESKTOP_PAGE_COUNTED_BUFFER            0x3A
264 #define  USB_HID_GENERIC_DESKTOP_PAGE_BYTE_COUNT                0x3B
265 #define  USB_HID_GENERIC_DESKTOP_PAGE_MOTION_WAKEUP             0x3C
266 #define  USB_HID_GENERIC_DESKTOP_PAGE_START                     0x3D
267 #define  USB_HID_GENERIC_DESKTOP_PAGE_SELECT                    0x3E
268 
269 #define  USB_HID_GENERIC_DESKTOP_PAGE_VX                        0x40
270 #define  USB_HID_GENERIC_DESKTOP_PAGE_VY                        0x41
271 #define  USB_HID_GENERIC_DESKTOP_PAGE_VZ                        0x42
272 #define  USB_HID_GENERIC_DESKTOP_PAGE_VBRX                      0x43
273 #define  USB_HID_GENERIC_DESKTOP_PAGE_VBRY                      0x44
274 #define  USB_HID_GENERIC_DESKTOP_PAGE_VBRZ                      0x45
275 #define  USB_HID_GENERIC_DESKTOP_PAGE_VNO                       0x46
276 #define  USB_HID_GENERIC_DESKTOP_PAGE_FEATURE_NOTIFICATION      0x47
277 
278 
279 /* Global Items. <<Device Class Definition for Human Interface Devices (HID)>> page35 */
280 typedef struct _usbHidGlobalItems{
281     unsigned int UsagePage;         /* 用途类页                     */
282 
283     int LogicalMinimum;     /* 最小逻辑值                    */
284     int LogicalMaximum;     /* 最大逻辑值                    */
285     int PhysicalMinimum;        /* 最小物理值                    */
286     int PhysicalMaximum;        /* 最大物理值                    */
287     int UnitExponent;           /* 单位指数                     */
288     unsigned int Unit;                  /* 单位                       */
289     unsigned int ReportSize;            /* 报告大小                     */
290     unsigned int ReportId;              /* 报告ID                         */
291     unsigned int ReportCount;           /* 报告的个数                    */
292     unsigned int Push;                  /* 项目状态表暂存到stack        */
293     unsigned int Pop;                   /* 取回stack最顶层的项目状态表     */
294 
295     unsigned int Reserved;
296 }__packed usbHidGlobalItems_t;
297 
298 /* Local Items. <<Device Class Definition for Human Interface Devices (HID)>> page39 */
299 typedef __packed struct _usbHidLocalItems{
300     unsigned int usage[USB_HID_MAX_USAGES];                 /* usage array */
301     unsigned int collection_index[USB_HID_MAX_USAGES];  /* collection index array */
302     unsigned int usage_index;
303     unsigned int usage_minimum;
304     unsigned int delimiter_depth;
305     unsigned int delimiter_branch;
306 }usbHidLocalItems_t;
307 
308 /* Collection Items */
309 typedef struct _usbHidCollectionItems{
310     unsigned int Type;      /* type of collection */
311     unsigned int Usage; /* 用途类页 */
312     unsigned int Level; /* the level of collection items */
313 }__packed usbHidCollectionItems_t;
314 
315 typedef struct _usbHidUsage{
316     unsigned int hid;               /* hid usage code, 高16为usage page, 低16位usage */
317     unsigned int collection_index;  /* index into collection array  */
318 
319     /* hidinput data */
320     unsigned short code;                /* input driver code            */
321     unsigned char  type;                /* input driver type            */
322     char  hat_min;          /* hat switch fun               */
323     char  hat_max;          /* ditto                        */
324     char  hat_dir;          /* ditto                        */
325 }usbHidUsage_t;
326 
327 #endif   //__HIDPUB_H__
328 
329 
330