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