]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Bus/Usb/UsbMouseDxe/MouseHid.c
modify coding style to pass ecc tool
[mirror_edk2.git] / MdeModulePkg / Bus / Usb / UsbMouseDxe / MouseHid.c
1 /** @file
2
3 Parse mouse hid descriptor.
4
5 Copyright (c) 2004 - 2008, Intel Corporation
6 All rights reserved. This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
10
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13
14 **/
15
16 #include "MouseHid.h"
17
18
19 /**
20 Get next item from report descriptor.
21
22 @param StartPos Start Position.
23 @param EndPos End Position.
24 @param HidItem HidItem to return.
25
26 @return Position.
27
28 **/
29 UINT8 *
30 GetNextItem (
31 IN UINT8 *StartPos,
32 IN UINT8 *EndPos,
33 OUT HID_ITEM *HidItem
34 )
35 {
36 UINT8 Temp;
37
38 if ((EndPos - StartPos) <= 0) {
39 return NULL;
40 }
41
42 Temp = *StartPos;
43 StartPos++;
44 //
45 // bit 2,3
46 //
47 HidItem->Type = (UINT8) ((Temp >> 2) & 0x03);
48 //
49 // bit 4-7
50 //
51 HidItem->Tag = (UINT8) ((Temp >> 4) & 0x0F);
52
53 if (HidItem->Tag == HID_ITEM_TAG_LONG) {
54 //
55 // Long Items are not supported by HID rev1.0,
56 // although we try to parse it.
57 //
58 HidItem->Format = HID_ITEM_FORMAT_LONG;
59
60 if ((EndPos - StartPos) >= 2) {
61 HidItem->Size = *StartPos++;
62 HidItem->Tag = *StartPos++;
63
64 if ((EndPos - StartPos) >= HidItem->Size) {
65 HidItem->Data.LongData = StartPos;
66 StartPos += HidItem->Size;
67 return StartPos;
68 }
69 }
70 } else {
71 HidItem->Format = HID_ITEM_FORMAT_SHORT;
72 //
73 // bit 0, 1
74 //
75 HidItem->Size = (UINT8) (Temp & 0x03);
76 switch (HidItem->Size) {
77
78 case 0:
79 //
80 // No data
81 //
82 return StartPos;
83
84 case 1:
85 //
86 // One byte data
87 //
88 if ((EndPos - StartPos) >= 1) {
89 HidItem->Data.U8 = *StartPos++;
90 return StartPos;
91 }
92
93 case 2:
94 //
95 // Two byte data
96 //
97 if ((EndPos - StartPos) >= 2) {
98 CopyMem (&HidItem->Data.U16, StartPos, sizeof (UINT16));
99 StartPos += 2;
100 return StartPos;
101 }
102
103 case 3:
104 //
105 // 4 byte data, adjust size
106 //
107 HidItem->Size++;
108 if ((EndPos - StartPos) >= 4) {
109 CopyMem (&HidItem->Data.U32, StartPos, sizeof (UINT32));
110 StartPos += 4;
111 return StartPos;
112 }
113 }
114 }
115
116 return NULL;
117 }
118
119
120 /**
121 Get item data from report descriptor.
122
123 @param HidItem The pointer to HID_ITEM.
124
125 @return The Data of HidItem.
126
127 **/
128 UINT32
129 GetItemData (
130 IN HID_ITEM *HidItem
131 )
132 {
133 //
134 // Get Data from HID_ITEM structure
135 //
136 switch (HidItem->Size) {
137
138 case 1:
139 return HidItem->Data.U8;
140
141 case 2:
142 return HidItem->Data.U16;
143
144 case 4:
145 return HidItem->Data.U32;
146 }
147
148 return 0;
149 }
150
151
152 /**
153 Parse local item from report descriptor.
154
155 @param UsbMouse The instance of USB_MOUSE_DEV
156 @param LocalItem The pointer to local hid item
157
158 **/
159 VOID
160 ParseLocalItem (
161 IN USB_MOUSE_DEV *UsbMouse,
162 IN HID_ITEM *LocalItem
163 )
164 {
165 UINT32 Data;
166
167 if (LocalItem->Size == 0) {
168 //
169 // No expected data for local item
170 //
171 return ;
172 }
173
174 Data = GetItemData (LocalItem);
175
176 switch (LocalItem->Tag) {
177
178 case HID_LOCAL_ITEM_TAG_DELIMITER:
179 //
180 // we don't support delimiter here
181 //
182 return ;
183
184 case HID_LOCAL_ITEM_TAG_USAGE:
185 return ;
186
187 case HID_LOCAL_ITEM_TAG_USAGE_MINIMUM:
188 if (UsbMouse->PrivateData.ButtonDetected) {
189 UsbMouse->PrivateData.ButtonMinIndex = (UINT8) Data;
190 }
191
192 return ;
193
194 case HID_LOCAL_ITEM_TAG_USAGE_MAXIMUM:
195 {
196 if (UsbMouse->PrivateData.ButtonDetected) {
197 UsbMouse->PrivateData.ButtonMaxIndex = (UINT8) Data;
198 }
199
200 return ;
201 }
202 }
203 }
204
205
206 /**
207 Parse global item from report descriptor.
208
209 @param UsbMouse The instance of USB_MOUSE_DEV
210 @param GlobalItem The pointer to global hid item
211
212 **/
213 VOID
214 ParseGlobalItem (
215 IN USB_MOUSE_DEV *UsbMouse,
216 IN HID_ITEM *GlobalItem
217 )
218 {
219 UINT8 UsagePage;
220
221 switch (GlobalItem->Tag) {
222 case HID_GLOBAL_ITEM_TAG_USAGE_PAGE:
223 {
224 UsagePage = (UINT8) GetItemData (GlobalItem);
225
226 //
227 // We only care Button Page here
228 //
229 if (UsagePage == 0x09) {
230 //
231 // Button Page
232 //
233 UsbMouse->PrivateData.ButtonDetected = TRUE;
234 return ;
235 }
236 break;
237 }
238
239 }
240 }
241
242
243 /**
244 Parse main item from report descriptor.
245
246 @param UsbMouse The instance of USB_MOUSE_DEV
247 @param MainItem Main hid item to parse
248
249 **/
250 VOID
251 ParseMainItem (
252 IN USB_MOUSE_DEV *UsbMouse,
253 IN HID_ITEM *MainItem
254 )
255 {
256 //
257 // we don't care any main items, just skip
258 //
259 return ;
260 }
261
262
263 /**
264 Parse hid item from report descriptor.
265
266 @param UsbMouse The instance of USB_MOUSE_DEV
267 @param HidItem The hid item to parse
268
269 **/
270 VOID
271 ParseHidItem (
272 IN USB_MOUSE_DEV *UsbMouse,
273 IN HID_ITEM *HidItem
274 )
275 {
276 switch (HidItem->Type) {
277
278 case HID_ITEM_TYPE_MAIN:
279 //
280 // For Main Item, parse main item
281 //
282 ParseMainItem (UsbMouse, HidItem);
283 break;
284
285 case HID_ITEM_TYPE_GLOBAL:
286 //
287 // For global Item, parse global item
288 //
289 ParseGlobalItem (UsbMouse, HidItem);
290 break;
291
292 case HID_ITEM_TYPE_LOCAL:
293 //
294 // For Local Item, parse local item
295 //
296 ParseLocalItem (UsbMouse, HidItem);
297 break;
298 }
299 }
300
301
302 /**
303 Parse Mouse Report Descriptor.
304
305 @param UsbMouse The instance of USB_MOUSE_DEV
306 @param ReportDescriptor Report descriptor to parse
307 @param ReportSize Report descriptor size
308
309 @retval EFI_DEVICE_ERROR Report descriptor error
310 @retval EFI_SUCCESS Parse descriptor success
311
312 **/
313 EFI_STATUS
314 ParseMouseReportDescriptor (
315 IN USB_MOUSE_DEV *UsbMouse,
316 IN UINT8 *ReportDescriptor,
317 IN UINTN ReportSize
318 )
319 {
320 UINT8 *DescriptorEnd;
321 UINT8 *Ptr;
322 HID_ITEM HidItem;
323
324 DescriptorEnd = ReportDescriptor + ReportSize;
325
326 Ptr = GetNextItem (ReportDescriptor, DescriptorEnd, &HidItem);
327
328 while (Ptr != NULL) {
329 if (HidItem.Format != HID_ITEM_FORMAT_SHORT) {
330 //
331 // Long Format Item is not supported at current HID revision
332 //
333 return EFI_DEVICE_ERROR;
334 }
335
336 ParseHidItem (UsbMouse, &HidItem);
337
338 Ptr = GetNextItem (Ptr, DescriptorEnd, &HidItem);
339 }
340
341 UsbMouse->NumberOfButtons = (UINT8) (UsbMouse->PrivateData.ButtonMaxIndex - UsbMouse->PrivateData.ButtonMinIndex + 1);
342 UsbMouse->XLogicMax = UsbMouse->YLogicMax = 127;
343 UsbMouse->XLogicMin = UsbMouse->YLogicMin = -127;
344
345 return EFI_SUCCESS;
346 }