1. Import UsbKbDxe and UsbMouseDxe into MdeModulePkg
[mirror_edk2.git] / MdePkg / Library / UefiUsbLib / UsbDxeLib.c
1 /** @file
2
3 Copyright (c) 2004 - 2007, 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 UsbDxeLib.c
15
16 Abstract:
17
18 Common Dxe Libarary for USB
19
20 Revision History
21
22
23 **/
24
25 //
26 // The package level header files this module uses
27 //
28 #include <PiDxe.h>
29 //
30 // The Library classes this module consumes
31 //
32 #include <Library/BaseMemoryLib.h>
33 #include <Library/UsbLib.h>
34
35 /**
36 Usb Get Descriptor
37
38 @param UsbIo EFI_USB_IO_PROTOCOL
39 @param Value Device Request Value
40 @param Index Device Request Index
41 @param DescriptorLength Descriptor Length
42 @param Descriptor Descriptor buffer to contain result
43 @param Status Transfer Status
44
45 @retval EFI_INVALID_PARAMETER Parameter is error
46 @retval EFI_SUCCESS Success
47 @retval EFI_TIMEOUT Device has no response
48
49 **/
50 EFI_STATUS
51 UsbGetDescriptor (
52 IN EFI_USB_IO_PROTOCOL *UsbIo,
53 IN UINT16 Value,
54 IN UINT16 Index,
55 IN UINT16 DescriptorLength,
56 OUT VOID *Descriptor,
57 OUT UINT32 *Status
58 )
59 {
60 EFI_USB_DEVICE_REQUEST DevReq;
61
62 if (UsbIo == NULL) {
63 return EFI_INVALID_PARAMETER;
64 }
65
66 ZeroMem (&DevReq, sizeof (EFI_USB_DEVICE_REQUEST));
67
68 DevReq.RequestType = USB_DEV_GET_DESCRIPTOR_REQ_TYPE;
69 DevReq.Request = USB_REQ_GET_DESCRIPTOR;
70 DevReq.Value = Value;
71 DevReq.Index = Index;
72 DevReq.Length = DescriptorLength;
73
74 return UsbIo->UsbControlTransfer (
75 UsbIo,
76 &DevReq,
77 EfiUsbDataIn,
78 TIMEOUT_VALUE,
79 Descriptor,
80 DescriptorLength,
81 Status
82 );
83 }
84
85
86 /**
87 Usb Set Descriptor
88
89 @param UsbIo EFI_USB_IO_PROTOCOL
90 @param Value Device Request Value
91 @param Index Device Request Index
92 @param DescriptorLength Descriptor Length
93 @param Descriptor Descriptor buffer to set
94 @param Status Transfer Status
95
96 @retval EFI_INVALID_PARAMETER Parameter is error
97 @retval EFI_SUCCESS Success
98 @retval EFI_TIMEOUT Device has no response
99
100 **/
101 EFI_STATUS
102 UsbSetDescriptor (
103 IN EFI_USB_IO_PROTOCOL *UsbIo,
104 IN UINT16 Value,
105 IN UINT16 Index,
106 IN UINT16 DescriptorLength,
107 IN VOID *Descriptor,
108 OUT UINT32 *Status
109 )
110 {
111 EFI_USB_DEVICE_REQUEST DevReq;
112
113 if (UsbIo == NULL) {
114 return EFI_INVALID_PARAMETER;
115 }
116
117 ZeroMem (&DevReq, sizeof (EFI_USB_DEVICE_REQUEST));
118
119 DevReq.RequestType = USB_DEV_SET_DESCRIPTOR_REQ_TYPE;
120 DevReq.Request = USB_REQ_SET_DESCRIPTOR;
121 DevReq.Value = Value;
122 DevReq.Index = Index;
123 DevReq.Length = DescriptorLength;
124
125 return UsbIo->UsbControlTransfer (
126 UsbIo,
127 &DevReq,
128 EfiUsbDataOut,
129 TIMEOUT_VALUE,
130 Descriptor,
131 DescriptorLength,
132 Status
133 );
134 }
135
136
137 /**
138 Usb Get Device Interface
139
140 @param UsbIo EFI_USB_IO_PROTOCOL
141 @param Index Interface index value
142 @param AltSetting Alternate setting
143 @param Status Trasnsfer status
144
145 @retval EFI_INVALID_PARAMETER Parameter is error
146 @retval EFI_SUCCESS Success
147 @retval EFI_TIMEOUT Device has no response
148
149 **/
150 EFI_STATUS
151 UsbGetInterface (
152 IN EFI_USB_IO_PROTOCOL *UsbIo,
153 IN UINT16 Index,
154 OUT UINT8 *AltSetting,
155 OUT UINT32 *Status
156 )
157 {
158 EFI_USB_DEVICE_REQUEST DevReq;
159
160 if (UsbIo == NULL) {
161 return EFI_INVALID_PARAMETER;
162 }
163
164 ZeroMem (&DevReq, sizeof (EFI_USB_DEVICE_REQUEST));
165
166 DevReq.RequestType = USB_DEV_GET_INTERFACE_REQ_TYPE;
167 DevReq.Request = USB_REQ_GET_INTERFACE;
168 DevReq.Index = Index;
169 DevReq.Length = 1;
170
171 return UsbIo->UsbControlTransfer (
172 UsbIo,
173 &DevReq,
174 EfiUsbDataIn,
175 TIMEOUT_VALUE,
176 AltSetting,
177 1,
178 Status
179 );
180 }
181
182
183 /**
184 Usb Set Device Interface
185
186 @param UsbIo EFI_USB_IO_PROTOCOL
187 @param InterfaceNo Interface Number
188 @param AltSetting Alternate setting
189 @param Status Trasnsfer status
190
191 @retval EFI_INVALID_PARAMETER Parameter is error
192 @retval EFI_SUCCESS Success
193 @retval EFI_TIMEOUT Device has no response
194
195 **/
196 EFI_STATUS
197 UsbSetInterface (
198 IN EFI_USB_IO_PROTOCOL *UsbIo,
199 IN UINT16 InterfaceNo,
200 IN UINT16 AltSetting,
201 OUT UINT32 *Status
202 )
203 {
204 EFI_USB_DEVICE_REQUEST DevReq;
205
206 if (UsbIo == NULL) {
207 return EFI_INVALID_PARAMETER;
208 }
209
210 ZeroMem (&DevReq, sizeof (EFI_USB_DEVICE_REQUEST));
211
212 DevReq.RequestType = USB_DEV_SET_INTERFACE_REQ_TYPE;
213 DevReq.Request = USB_REQ_SET_INTERFACE;
214 DevReq.Value = AltSetting;
215 DevReq.Index = InterfaceNo;
216
217
218 return UsbIo->UsbControlTransfer (
219 UsbIo,
220 &DevReq,
221 EfiUsbNoData,
222 TIMEOUT_VALUE,
223 NULL,
224 0,
225 Status
226 );
227 }
228
229
230 /**
231 Usb Get Device Configuration
232
233 @param UsbIo EFI_USB_IO_PROTOCOL
234 @param ConfigValue Config Value
235 @param Status Transfer Status
236
237 @retval EFI_INVALID_PARAMETER Parameter is error
238 @retval EFI_SUCCESS Success
239 @retval EFI_TIMEOUT Device has no response
240
241 **/
242 EFI_STATUS
243 UsbGetConfiguration (
244 IN EFI_USB_IO_PROTOCOL *UsbIo,
245 OUT UINT8 *ConfigValue,
246 OUT UINT32 *Status
247 )
248 {
249 EFI_USB_DEVICE_REQUEST DevReq;
250
251 if (UsbIo == NULL) {
252 return EFI_INVALID_PARAMETER;
253 }
254
255 ZeroMem (&DevReq, sizeof (EFI_USB_DEVICE_REQUEST));
256
257 DevReq.RequestType = USB_DEV_GET_CONFIGURATION_REQ_TYPE;
258 DevReq.Request = USB_REQ_GET_CONFIG;
259 DevReq.Length = 1;
260
261 return UsbIo->UsbControlTransfer (
262 UsbIo,
263 &DevReq,
264 EfiUsbDataIn,
265 TIMEOUT_VALUE,
266 ConfigValue,
267 1,
268 Status
269 );
270 }
271
272
273 /**
274 Usb Set Device Configuration
275
276 @param UsbIo EFI_USB_IO_PROTOCOL
277 @param Value Configuration Value to set
278 @param Status Transfer status
279
280 @retval EFI_INVALID_PARAMETER Parameter is error
281 @retval EFI_SUCCESS Success
282 @retval EFI_TIMEOUT Device has no response
283
284 **/
285 EFI_STATUS
286 UsbSetConfiguration (
287 IN EFI_USB_IO_PROTOCOL *UsbIo,
288 IN UINT16 Value,
289 OUT UINT32 *Status
290 )
291 {
292 EFI_USB_DEVICE_REQUEST DevReq;
293
294 if (UsbIo == NULL) {
295 return EFI_INVALID_PARAMETER;
296 }
297
298 ZeroMem (&DevReq, sizeof (EFI_USB_DEVICE_REQUEST));
299
300 DevReq.RequestType = USB_DEV_SET_CONFIGURATION_REQ_TYPE;
301 DevReq.Request = USB_REQ_SET_CONFIG;
302 DevReq.Value = Value;
303
304 return UsbIo->UsbControlTransfer (
305 UsbIo,
306 &DevReq,
307 EfiUsbNoData,
308 TIMEOUT_VALUE,
309 NULL,
310 0,
311 Status
312 );
313 }
314
315
316 /**
317 Usb Set Device Feature
318
319 @param UsbIo EFI_USB_IO_PROTOCOL
320 @param Recipient Interface/Device/Endpoint
321 @param Value Request value
322 @param Target Request Index
323 @param Status Transfer status
324
325 @retval EFI_INVALID_PARAMETER Parameter is error
326 @retval EFI_SUCCESS Success
327 @retval EFI_TIMEOUT Device has no response
328
329 **/
330 EFI_STATUS
331 UsbSetFeature (
332 IN EFI_USB_IO_PROTOCOL *UsbIo,
333 IN UINTN Recipient,
334 IN UINT16 Value,
335 IN UINT16 Target,
336 OUT UINT32 *Status
337 )
338 {
339 EFI_USB_DEVICE_REQUEST DevReq;
340
341 if (UsbIo == NULL) {
342 return EFI_INVALID_PARAMETER;
343 }
344
345 ZeroMem (&DevReq, sizeof (EFI_USB_DEVICE_REQUEST));
346
347 switch (Recipient) {
348
349 case USB_TARGET_DEVICE:
350 DevReq.RequestType = USB_DEV_SET_FEATURE_REQ_TYPE_D;
351 break;
352
353 case USB_TARGET_INTERFACE:
354 DevReq.RequestType = USB_DEV_SET_FEATURE_REQ_TYPE_I;
355 break;
356
357 case USB_TARGET_ENDPOINT:
358 DevReq.RequestType = USB_DEV_SET_FEATURE_REQ_TYPE_E;
359 break;
360 }
361 //
362 // Fill device request, see USB1.1 spec
363 //
364 DevReq.Request = USB_REQ_SET_FEATURE;
365 DevReq.Value = Value;
366 DevReq.Index = Target;
367
368
369 return UsbIo->UsbControlTransfer (
370 UsbIo,
371 &DevReq,
372 EfiUsbNoData,
373 TIMEOUT_VALUE,
374 NULL,
375 0,
376 Status
377 );
378 }
379
380
381 /**
382 Usb Clear Device Feature
383
384 @param UsbIo EFI_USB_IO_PROTOCOL
385 @param Recipient Interface/Device/Endpoint
386 @param Value Request value
387 @param Target Request Index
388 @param Status Transfer status
389
390 @retval EFI_INVALID_PARAMETER Parameter is error
391 @retval EFI_SUCCESS Success
392 @retval EFI_TIMEOUT Device has no response
393
394 **/
395 EFI_STATUS
396 UsbClearFeature (
397 IN EFI_USB_IO_PROTOCOL *UsbIo,
398 IN UINTN Recipient,
399 IN UINT16 Value,
400 IN UINT16 Target,
401 OUT UINT32 *Status
402 )
403 {
404 EFI_USB_DEVICE_REQUEST DevReq;
405
406 if (UsbIo == NULL) {
407 return EFI_INVALID_PARAMETER;
408 }
409
410 ZeroMem (&DevReq, sizeof (EFI_USB_DEVICE_REQUEST));
411
412 switch (Recipient) {
413
414 case USB_TARGET_DEVICE:
415 DevReq.RequestType = USB_DEV_CLEAR_FEATURE_REQ_TYPE_D;
416 break;
417
418 case USB_TARGET_INTERFACE:
419 DevReq.RequestType = USB_DEV_CLEAR_FEATURE_REQ_TYPE_I;
420 break;
421
422 case USB_TARGET_ENDPOINT:
423 DevReq.RequestType = USB_DEV_CLEAR_FEATURE_REQ_TYPE_E;
424 break;
425 }
426 //
427 // Fill device request, see USB1.1 spec
428 //
429 DevReq.Request = USB_REQ_CLEAR_FEATURE;
430 DevReq.Value = Value;
431 DevReq.Index = Target;
432
433
434 return UsbIo->UsbControlTransfer (
435 UsbIo,
436 &DevReq,
437 EfiUsbNoData,
438 TIMEOUT_VALUE,
439 NULL,
440 0,
441 Status
442 );
443 }
444
445
446 /**
447 Usb Get Device Status
448
449 @param UsbIo EFI_USB_IO_PROTOCOL
450 @param Recipient Interface/Device/Endpoint
451 @param Target Request index
452 @param DevStatus Device status
453 @param Status Transfer status
454
455 @retval EFI_INVALID_PARAMETER Parameter is error
456 @retval EFI_SUCCESS Success
457 @retval EFI_TIMEOUT Device has no response
458
459 **/
460 EFI_STATUS
461 UsbGetStatus (
462 IN EFI_USB_IO_PROTOCOL *UsbIo,
463 IN UINTN Recipient,
464 IN UINT16 Target,
465 OUT UINT16 *DevStatus,
466 OUT UINT32 *Status
467 )
468 {
469 EFI_USB_DEVICE_REQUEST DevReq;
470
471 if (UsbIo == NULL) {
472 return EFI_INVALID_PARAMETER;
473 }
474
475 ZeroMem (&DevReq, sizeof (EFI_USB_DEVICE_REQUEST));
476
477 switch (Recipient) {
478
479 case USB_TARGET_DEVICE:
480 DevReq.RequestType = USB_DEV_GET_STATUS_REQ_TYPE_D;
481 break;
482
483 case USB_TARGET_INTERFACE:
484 DevReq.RequestType = USB_DEV_GET_STATUS_REQ_TYPE_I;
485 break;
486
487 case USB_TARGET_ENDPOINT:
488 DevReq.RequestType = USB_DEV_GET_STATUS_REQ_TYPE_E;
489 break;
490 }
491 //
492 // Fill device request, see USB1.1 spec
493 //
494 DevReq.Request = USB_REQ_GET_STATUS;
495 DevReq.Value = 0;
496 DevReq.Index = Target;
497 DevReq.Length = 2;
498
499 return UsbIo->UsbControlTransfer (
500 UsbIo,
501 &DevReq,
502 EfiUsbDataIn,
503 TIMEOUT_VALUE,
504 DevStatus,
505 2,
506 Status
507 );
508 }
509
510
511
512 /**
513 Clear endpoint stall
514
515 @param UsbIo EFI_USB_IO_PROTOCOL
516 @param EndpointNo Endpoint Number
517 @param Status Transfer Status
518
519 @retval EFI_NOT_FOUND Can't find the Endpoint
520 @retval EFI_DEVICE_ERROR Hardware error
521 @retval EFI_SUCCESS Success
522
523 **/
524 EFI_STATUS
525 UsbClearEndpointHalt (
526 IN EFI_USB_IO_PROTOCOL *UsbIo,
527 IN UINT8 EndpointNo,
528 OUT UINT32 *Status
529 )
530 {
531 EFI_STATUS Result;
532 EFI_USB_ENDPOINT_DESCRIPTOR EndpointDescriptor;
533 EFI_USB_INTERFACE_DESCRIPTOR InterfaceDescriptor;
534 UINT8 Index;
535
536 ZeroMem (&EndpointDescriptor, sizeof (EFI_USB_ENDPOINT_DESCRIPTOR));
537 //
538 // First seach the endpoint descriptor for that endpoint addr
539 //
540 Result = UsbIo->UsbGetInterfaceDescriptor (
541 UsbIo,
542 &InterfaceDescriptor
543 );
544 if (EFI_ERROR (Result)) {
545 return Result;
546 }
547
548 for (Index = 0; Index < InterfaceDescriptor.NumEndpoints; Index++) {
549 Result = UsbIo->UsbGetEndpointDescriptor (
550 UsbIo,
551 Index,
552 &EndpointDescriptor
553 );
554 if (EFI_ERROR (Result)) {
555 continue;
556 }
557
558 if (EndpointDescriptor.EndpointAddress == EndpointNo) {
559 break;
560 }
561 }
562
563 if (Index == InterfaceDescriptor.NumEndpoints) {
564 //
565 // No such endpoint
566 //
567 return EFI_NOT_FOUND;
568 }
569
570 Result = UsbClearFeature (
571 UsbIo,
572 USB_TARGET_ENDPOINT,
573 EfiUsbEndpointHalt,
574 EndpointDescriptor.EndpointAddress,
575 Status
576 );
577
578 return Result;
579 }