]> git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/UefiUsbLib/hid.c
3156f6314309577fe519b3b43039b8f6cb56d78f
[mirror_edk2.git] / MdePkg / Library / UefiUsbLib / hid.c
1 /** @file
2
3 Copyright (c) 2004, 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 <PiDxe.h>
25 #include <Library/UsbLib.h>
26
27 //
28 // Function to get HID descriptor
29 //
30
31 /**
32 Get Hid Descriptor
33
34 @param UsbIo EFI_USB_IO_PROTOCOL
35 @param InterfaceNum Hid interface number
36 @param HidDescriptor Caller allocated buffer to store Usb hid descriptor if
37 successfully returned.
38
39 @return EFI_SUCCESS
40 @return EFI_DEVICE_ERROR
41 @return EFI_TIMEOUT
42
43 **/
44 EFI_STATUS
45 UsbGetHidDescriptor (
46 IN EFI_USB_IO_PROTOCOL *UsbIo,
47 IN UINT8 InterfaceNum,
48 OUT EFI_USB_HID_DESCRIPTOR *HidDescriptor
49 )
50 {
51 UINT32 Status;
52 EFI_STATUS Result;
53 EFI_USB_DEVICE_REQUEST Request;
54
55 Request.RequestType = 0x81;
56 Request.Request = 0x06;
57 Request.Value = (UINT16) (0x21 << 8);
58 Request.Index = InterfaceNum;
59 Request.Length = sizeof (EFI_USB_HID_DESCRIPTOR);
60
61 Result = UsbIo->UsbControlTransfer (
62 UsbIo,
63 &Request,
64 EfiUsbDataIn,
65 TIMEOUT_VALUE,
66 HidDescriptor,
67 sizeof (EFI_USB_HID_DESCRIPTOR),
68 &Status
69 );
70
71 return Result;
72
73 }
74 //
75 // Function to get Report Class descriptor
76 //
77
78 /**
79 get Report Class descriptor
80
81 @param UsbIo EFI_USB_IO_PROTOCOL.
82 @param InterfaceNum Report interface number.
83 @param DescriptorSize Length of DescriptorBuffer.
84 @param DescriptorBuffer Caller allocated buffer to store Usb report descriptor
85 if successfully returned.
86
87 @return EFI_SUCCESS
88 @return EFI_DEVICE_ERROR
89 @return EFI_TIMEOUT
90
91 **/
92 EFI_STATUS
93 UsbGetReportDescriptor (
94 IN EFI_USB_IO_PROTOCOL *UsbIo,
95 IN UINT8 InterfaceNum,
96 IN UINT16 DescriptorSize,
97 OUT UINT8 *DescriptorBuffer
98 )
99 {
100 UINT32 Status;
101 EFI_STATUS Result;
102 EFI_USB_DEVICE_REQUEST Request;
103
104 //
105 // Fill Device request packet
106 //
107 Request.RequestType = 0x81;
108 Request.Request = 0x06;
109 Request.Value = (UINT16) (0x22 << 8);
110 Request.Index = InterfaceNum;
111 Request.Length = DescriptorSize;
112
113 Result = UsbIo->UsbControlTransfer (
114 UsbIo,
115 &Request,
116 EfiUsbDataIn,
117 TIMEOUT_VALUE,
118 DescriptorBuffer,
119 DescriptorSize,
120 &Status
121 );
122
123 return Result;
124
125 }
126 //
127 // Following are HID class request
128 //
129
130 /**
131 Get Hid Protocol Request
132
133 @param UsbIo EFI_USB_IO_PROTOCOL
134 @param Interface Which interface the caller wants to get protocol
135 @param Protocol Protocol value returned.
136
137 @return EFI_SUCCESS
138 @return EFI_DEVICE_ERROR
139 @return EFI_TIMEOUT
140
141 **/
142 EFI_STATUS
143 UsbGetProtocolRequest (
144 IN EFI_USB_IO_PROTOCOL *UsbIo,
145 IN UINT8 Interface,
146 IN UINT8 *Protocol
147 )
148 {
149 UINT32 Status;
150 EFI_STATUS Result;
151 EFI_USB_DEVICE_REQUEST Request;
152
153 //
154 // Fill Device request packet
155 //
156 Request.RequestType = 0xa1;
157 //
158 // 10100001b;
159 //
160 Request.Request = EFI_USB_GET_PROTOCOL_REQUEST;
161 Request.Value = 0;
162 Request.Index = Interface;
163 Request.Length = 1;
164
165 Result = UsbIo->UsbControlTransfer (
166 UsbIo,
167 &Request,
168 EfiUsbDataIn,
169 TIMEOUT_VALUE,
170 Protocol,
171 sizeof (UINT8),
172 &Status
173 );
174
175 return Result;
176 }
177
178
179
180 /**
181 Set Hid Protocol Request
182
183 @param UsbIo EFI_USB_IO_PROTOCOL
184 @param Interface Which interface the caller wants to set protocol
185 @param Protocol Protocol value the caller wants to set.
186
187 @return EFI_SUCCESS
188 @return EFI_DEVICE_ERROR
189 @return EFI_TIMEOUT
190
191 **/
192 EFI_STATUS
193 UsbSetProtocolRequest (
194 IN EFI_USB_IO_PROTOCOL *UsbIo,
195 IN UINT8 Interface,
196 IN UINT8 Protocol
197 )
198 {
199 UINT32 Status;
200 EFI_STATUS Result;
201 EFI_USB_DEVICE_REQUEST Request;
202
203 //
204 // Fill Device request packet
205 //
206 Request.RequestType = 0x21;
207 //
208 // 00100001b;
209 //
210 Request.Request = EFI_USB_SET_PROTOCOL_REQUEST;
211 Request.Value = Protocol;
212 Request.Index = Interface;
213 Request.Length = 0;
214
215 Result = UsbIo->UsbControlTransfer (
216 UsbIo,
217 &Request,
218 EfiUsbNoData,
219 TIMEOUT_VALUE,
220 NULL,
221 0,
222 &Status
223 );
224 return Result;
225 }
226
227
228
229 /**
230 Set Idel request.
231
232 @param UsbIo EFI_USB_IO_PROTOCOL
233 @param Interface Which interface the caller wants to set.
234 @param ReportId Which report the caller wants to set.
235 @param Duration Idle rate the caller wants to set.
236
237 @return EFI_SUCCESS
238 @return EFI_DEVICE_ERROR
239 @return EFI_TIMEOUT
240
241 **/
242 EFI_STATUS
243 UsbSetIdleRequest (
244 IN EFI_USB_IO_PROTOCOL *UsbIo,
245 IN UINT8 Interface,
246 IN UINT8 ReportId,
247 IN UINT8 Duration
248 )
249 {
250 UINT32 Status;
251 EFI_STATUS Result;
252 EFI_USB_DEVICE_REQUEST Request;
253
254 //
255 // Fill Device request packet
256 //
257 Request.RequestType = 0x21;
258 //
259 // 00100001b;
260 //
261 Request.Request = EFI_USB_SET_IDLE_REQUEST;
262 Request.Value = (UINT16) ((Duration << 8) | ReportId);
263 Request.Index = Interface;
264 Request.Length = 0;
265
266 Result = UsbIo->UsbControlTransfer (
267 UsbIo,
268 &Request,
269 EfiUsbNoData,
270 TIMEOUT_VALUE,
271 NULL,
272 0,
273 &Status
274 );
275 return Result;
276 }
277
278
279 /**
280 Get Idel request.
281
282 @param UsbIo EFI_USB_IO_PROTOCOL
283 @param Interface Which interface the caller wants to get.
284 @param ReportId Which report the caller wants to get.
285 @param Duration Idle rate the caller wants to get.
286
287 @return EFI_SUCCESS
288 @return EFI_DEVICE_ERROR
289 @return EFI_TIMEOUT
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 UINT32 Status;
301 EFI_STATUS Result;
302 EFI_USB_DEVICE_REQUEST Request;
303
304 //
305 // Fill Device request packet
306 //
307 Request.RequestType = 0xa1;
308 //
309 // 10100001b;
310 //
311 Request.Request = EFI_USB_GET_IDLE_REQUEST;
312 Request.Value = ReportId;
313 Request.Index = Interface;
314 Request.Length = 1;
315
316 Result = UsbIo->UsbControlTransfer (
317 UsbIo,
318 &Request,
319 EfiUsbDataIn,
320 TIMEOUT_VALUE,
321 Duration,
322 1,
323 &Status
324 );
325
326 return Result;
327 }
328
329
330
331 /**
332 Hid Set Report request.
333
334 @param UsbIo EFI_USB_IO_PROTOCOL
335 @param Interface Which interface the caller wants to set.
336 @param ReportId Which report the caller wants to set.
337 @param ReportType Type of report.
338 @param ReportLen Length of report descriptor.
339 @param Report Report Descriptor buffer.
340
341 @return EFI_SUCCESS
342 @return EFI_DEVICE_ERROR
343 @return EFI_TIMEOUT
344
345 **/
346 EFI_STATUS
347 UsbSetReportRequest (
348 IN EFI_USB_IO_PROTOCOL *UsbIo,
349 IN UINT8 Interface,
350 IN UINT8 ReportId,
351 IN UINT8 ReportType,
352 IN UINT16 ReportLen,
353 IN UINT8 *Report
354 )
355 {
356 UINT32 Status;
357 EFI_STATUS Result;
358 EFI_USB_DEVICE_REQUEST Request;
359
360 //
361 // Fill Device request packet
362 //
363 Request.RequestType = 0x21;
364 //
365 // 00100001b;
366 //
367 Request.Request = EFI_USB_SET_REPORT_REQUEST;
368 Request.Value = (UINT16) ((ReportType << 8) | ReportId);
369 Request.Index = Interface;
370 Request.Length = ReportLen;
371
372 Result = UsbIo->UsbControlTransfer (
373 UsbIo,
374 &Request,
375 EfiUsbDataOut,
376 TIMEOUT_VALUE,
377 Report,
378 ReportLen,
379 &Status
380 );
381
382 return Result;
383 }
384
385
386 /**
387 Hid Set Report request.
388
389 @param UsbIo EFI_USB_IO_PROTOCOL
390 @param Interface Which interface the caller wants to set.
391 @param ReportId Which report the caller wants to set.
392 @param ReportType Type of report.
393 @param ReportLen Length of report descriptor.
394 @param Report Caller allocated buffer to store Report Descriptor.
395
396 @return EFI_SUCCESS
397 @return EFI_DEVICE_ERROR
398 @return EFI_TIMEOUT
399
400 **/
401 EFI_STATUS
402 UsbGetReportRequest (
403 IN EFI_USB_IO_PROTOCOL *UsbIo,
404 IN UINT8 Interface,
405 IN UINT8 ReportId,
406 IN UINT8 ReportType,
407 IN UINT16 ReportLen,
408 IN UINT8 *Report
409 )
410 {
411 UINT32 Status;
412 EFI_STATUS Result;
413 EFI_USB_DEVICE_REQUEST Request;
414
415 //
416 // Fill Device request packet
417 //
418 Request.RequestType = 0xa1;
419 //
420 // 10100001b;
421 //
422 Request.Request = EFI_USB_GET_REPORT_REQUEST;
423 Request.Value = (UINT16) ((ReportType << 8) | ReportId);
424 Request.Index = Interface;
425 Request.Length = ReportLen;
426
427 Result = UsbIo->UsbControlTransfer (
428 UsbIo,
429 &Request,
430 EfiUsbDataIn,
431 TIMEOUT_VALUE,
432 Report,
433 ReportLen,
434 &Status
435 );
436
437 return Result;
438 }