a1019a0f2d228a301ff4b53b0d4632539279d9f0
[mirror_edk2.git] / MdePkg / Library / UefiUsbLib / hid.c
1 /*++
2
3 Copyright (c) 2006, Intel Corporation
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
8
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
11
12 Module Name:
13
14 hid.c
15
16 Abstract:
17
18 HID class request
19
20 Revision History
21
22 --*/
23
24 #include "UefiUsbLibInternal.h"
25
26 //
27 // Function to get HID descriptor
28 //
29 EFI_STATUS
30 UsbGetHidDescriptor (
31 IN EFI_USB_IO_PROTOCOL *UsbIo,
32 IN UINT8 InterfaceNum,
33 OUT EFI_USB_HID_DESCRIPTOR *HidDescriptor
34 )
35 /*++
36
37 Routine Description:
38 Get Hid Descriptor
39
40 Arguments:
41 UsbIo - EFI_USB_IO_PROTOCOL
42 InterfaceNum - Hid interface number
43 HidDescriptor - Caller allocated buffer to store Usb hid descriptor
44 if successfully returned.
45
46 Returns:
47 EFI_SUCCESS
48 EFI_DEVICE_ERROR
49 EFI_TIMEOUT
50
51 --*/
52 {
53 UINT32 Status;
54 EFI_STATUS Result;
55 EFI_USB_DEVICE_REQUEST Request;
56
57 Request.RequestType = 0x81;
58 Request.Request = 0x06;
59 Request.Value = (UINT16) (0x21 << 8);
60 Request.Index = InterfaceNum;
61 Request.Length = sizeof (EFI_USB_HID_DESCRIPTOR);
62
63 Result = UsbIo->UsbControlTransfer (
64 UsbIo,
65 &Request,
66 EfiUsbDataIn,
67 TIMEOUT_VALUE,
68 HidDescriptor,
69 sizeof (EFI_USB_HID_DESCRIPTOR),
70 &Status
71 );
72
73 return Result;
74
75 }
76 //
77 // Function to get Report Class descriptor
78 //
79 EFI_STATUS
80 UsbGetReportDescriptor (
81 IN EFI_USB_IO_PROTOCOL *UsbIo,
82 IN UINT8 InterfaceNum,
83 IN UINT16 DescriptorSize,
84 OUT UINT8 *DescriptorBuffer
85 )
86 /*++
87
88 Routine Description:
89 get Report Class descriptor
90
91 Arguments:
92 UsbIo - EFI_USB_IO_PROTOCOL.
93 InterfaceNum - Report interface number.
94 DescriptorSize - Length of DescriptorBuffer.
95 DescriptorBuffer - Caller allocated buffer to store Usb report descriptor
96 if successfully returned.
97
98 Returns:
99 EFI_SUCCESS
100 EFI_DEVICE_ERROR
101 EFI_TIMEOUT
102
103 --*/
104 {
105 UINT32 Status;
106 EFI_STATUS Result;
107 EFI_USB_DEVICE_REQUEST Request;
108
109 //
110 // Fill Device request packet
111 //
112 Request.RequestType = 0x81;
113 Request.Request = 0x06;
114 Request.Value = (UINT16) (0x22 << 8);
115 Request.Index = InterfaceNum;
116 Request.Length = DescriptorSize;
117
118 Result = UsbIo->UsbControlTransfer (
119 UsbIo,
120 &Request,
121 EfiUsbDataIn,
122 TIMEOUT_VALUE,
123 DescriptorBuffer,
124 DescriptorSize,
125 &Status
126 );
127
128 return Result;
129
130 }
131 //
132 // Following are HID class request
133 //
134 EFI_STATUS
135 UsbGetProtocolRequest (
136 IN EFI_USB_IO_PROTOCOL *UsbIo,
137 IN UINT8 Interface,
138 IN UINT8 *Protocol
139 )
140 /*++
141
142 Routine Description:
143 Get Hid Protocol Request
144
145 Arguments:
146 UsbIo - EFI_USB_IO_PROTOCOL
147 Interface - Which interface the caller wants to get protocol
148 Protocol - Protocol value returned.
149
150 Returns:
151 EFI_SUCCESS
152 EFI_DEVICE_ERROR
153 EFI_TIMEOUT
154
155 --*/
156 {
157 UINT32 Status;
158 EFI_STATUS Result;
159 EFI_USB_DEVICE_REQUEST Request;
160
161 //
162 // Fill Device request packet
163 //
164 Request.RequestType = 0xa1;
165 //
166 // 10100001b;
167 //
168 Request.Request = EFI_USB_GET_PROTOCOL_REQUEST;
169 Request.Value = 0;
170 Request.Index = Interface;
171 Request.Length = 1;
172
173 Result = UsbIo->UsbControlTransfer (
174 UsbIo,
175 &Request,
176 EfiUsbDataIn,
177 TIMEOUT_VALUE,
178 Protocol,
179 sizeof (UINT8),
180 &Status
181 );
182
183 return Result;
184 }
185
186
187 EFI_STATUS
188 UsbSetProtocolRequest (
189 IN EFI_USB_IO_PROTOCOL *UsbIo,
190 IN UINT8 Interface,
191 IN UINT8 Protocol
192 )
193 /*++
194
195 Routine Description:
196 Set Hid Protocol Request
197
198 Arguments:
199 UsbIo - EFI_USB_IO_PROTOCOL
200 Interface - Which interface the caller wants to set protocol
201 Protocol - Protocol value the caller wants to set.
202
203 Returns:
204 EFI_SUCCESS
205 EFI_DEVICE_ERROR
206 EFI_TIMEOUT
207
208 --*/
209 {
210 UINT32 Status;
211 EFI_STATUS Result;
212 EFI_USB_DEVICE_REQUEST Request;
213
214 //
215 // Fill Device request packet
216 //
217 Request.RequestType = 0x21;
218 //
219 // 00100001b;
220 //
221 Request.Request = EFI_USB_SET_PROTOCOL_REQUEST;
222 Request.Value = Protocol;
223 Request.Index = Interface;
224 Request.Length = 0;
225
226 Result = UsbIo->UsbControlTransfer (
227 UsbIo,
228 &Request,
229 EfiUsbNoData,
230 TIMEOUT_VALUE,
231 NULL,
232 0,
233 &Status
234 );
235 return Result;
236 }
237
238
239 EFI_STATUS
240 UsbSetIdleRequest (
241 IN EFI_USB_IO_PROTOCOL *UsbIo,
242 IN UINT8 Interface,
243 IN UINT8 ReportId,
244 IN UINT8 Duration
245 )
246 /*++
247
248 Routine Description:
249 Set Idel request.
250
251 Arguments:
252 UsbIo - EFI_USB_IO_PROTOCOL
253 Interface - Which interface the caller wants to set.
254 ReportId - Which report the caller wants to set.
255 Duration - Idle rate the caller wants to set.
256
257 Returns:
258 EFI_SUCCESS
259 EFI_DEVICE_ERROR
260 EFI_TIMEOUT
261
262 --*/
263 {
264 UINT32 Status;
265 EFI_STATUS Result;
266 EFI_USB_DEVICE_REQUEST Request;
267
268 //
269 // Fill Device request packet
270 //
271 Request.RequestType = 0x21;
272 //
273 // 00100001b;
274 //
275 Request.Request = EFI_USB_SET_IDLE_REQUEST;
276 Request.Value = (UINT16) ((Duration << 8) | ReportId);
277 Request.Index = Interface;
278 Request.Length = 0;
279
280 Result = UsbIo->UsbControlTransfer (
281 UsbIo,
282 &Request,
283 EfiUsbNoData,
284 TIMEOUT_VALUE,
285 NULL,
286 0,
287 &Status
288 );
289 return Result;
290 }
291
292 EFI_STATUS
293 UsbGetIdleRequest (
294 IN EFI_USB_IO_PROTOCOL *UsbIo,
295 IN UINT8 Interface,
296 IN UINT8 ReportId,
297 OUT UINT8 *Duration
298 )
299 /*++
300
301 Routine Description:
302 Get Idel request.
303
304 Arguments:
305 UsbIo - EFI_USB_IO_PROTOCOL
306 Interface - Which interface the caller wants to get.
307 ReportId - Which report the caller wants to get.
308 Duration - Idle rate the caller wants to get.
309
310 Returns:
311 EFI_SUCCESS
312 EFI_DEVICE_ERROR
313 EFI_TIMEOUT
314
315 --*/
316 {
317 UINT32 Status;
318 EFI_STATUS Result;
319 EFI_USB_DEVICE_REQUEST Request;
320
321 //
322 // Fill Device request packet
323 //
324 Request.RequestType = 0xa1;
325 //
326 // 10100001b;
327 //
328 Request.Request = EFI_USB_GET_IDLE_REQUEST;
329 Request.Value = ReportId;
330 Request.Index = Interface;
331 Request.Length = 1;
332
333 Result = UsbIo->UsbControlTransfer (
334 UsbIo,
335 &Request,
336 EfiUsbDataIn,
337 TIMEOUT_VALUE,
338 Duration,
339 1,
340 &Status
341 );
342
343 return Result;
344 }
345
346
347 EFI_STATUS
348 UsbSetReportRequest (
349 IN EFI_USB_IO_PROTOCOL *UsbIo,
350 IN UINT8 Interface,
351 IN UINT8 ReportId,
352 IN UINT8 ReportType,
353 IN UINT16 ReportLen,
354 IN UINT8 *Report
355 )
356 /*++
357
358 Routine Description:
359 Hid Set Report request.
360
361 Arguments:
362 UsbIo - EFI_USB_IO_PROTOCOL
363 Interface - Which interface the caller wants to set.
364 ReportId - Which report the caller wants to set.
365 ReportType - Type of report.
366 ReportLen - Length of report descriptor.
367 Report - Report Descriptor buffer.
368
369 Returns:
370 EFI_SUCCESS
371 EFI_DEVICE_ERROR
372 EFI_TIMEOUT
373
374 --*/
375 {
376 UINT32 Status;
377 EFI_STATUS Result;
378 EFI_USB_DEVICE_REQUEST Request;
379
380 //
381 // Fill Device request packet
382 //
383 Request.RequestType = 0x21;
384 //
385 // 00100001b;
386 //
387 Request.Request = EFI_USB_SET_REPORT_REQUEST;
388 Request.Value = (UINT16) ((ReportType << 8) | ReportId);
389 Request.Index = Interface;
390 Request.Length = ReportLen;
391
392 Result = UsbIo->UsbControlTransfer (
393 UsbIo,
394 &Request,
395 EfiUsbDataOut,
396 TIMEOUT_VALUE,
397 Report,
398 ReportLen,
399 &Status
400 );
401
402 return Result;
403 }
404
405 EFI_STATUS
406 UsbGetReportRequest (
407 IN EFI_USB_IO_PROTOCOL *UsbIo,
408 IN UINT8 Interface,
409 IN UINT8 ReportId,
410 IN UINT8 ReportType,
411 IN UINT16 ReportLen,
412 IN UINT8 *Report
413 )
414 /*++
415
416 Routine Description:
417 Hid Set Report request.
418
419 Arguments:
420 UsbIo - EFI_USB_IO_PROTOCOL
421 Interface - Which interface the caller wants to set.
422 ReportId - Which report the caller wants to set.
423 ReportType - Type of report.
424 ReportLen - Length of report descriptor.
425 Report - Caller allocated buffer to store Report Descriptor.
426
427 Returns:
428 EFI_SUCCESS
429 EFI_DEVICE_ERROR
430 EFI_TIMEOUT
431
432 --*/
433 {
434 UINT32 Status;
435 EFI_STATUS Result;
436 EFI_USB_DEVICE_REQUEST Request;
437
438 //
439 // Fill Device request packet
440 //
441 Request.RequestType = 0xa1;
442 //
443 // 10100001b;
444 //
445 Request.Request = EFI_USB_GET_REPORT_REQUEST;
446 Request.Value = (UINT16) ((ReportType << 8) | ReportId);
447 Request.Index = Interface;
448 Request.Length = ReportLen;
449
450 Result = UsbIo->UsbControlTransfer (
451 UsbIo,
452 &Request,
453 EfiUsbDataIn,
454 TIMEOUT_VALUE,
455 Report,
456 ReportLen,
457 &Status
458 );
459
460 return Result;
461 }