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
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.
12 Common Dxe Libarary for USB
17 #include <Library/BaseMemoryLib.h>
18 #include <Library/UsbLib.h>
23 @param UsbIo EFI_USB_IO_PROTOCOL
24 @param Value Device Request Value
25 @param Index Device Request Index
26 @param DescriptorLength Descriptor Length
27 @param Descriptor Descriptor buffer to contain result
28 @param Status Transfer Status
30 @retval EFI_INVALID_PARAMETER Parameter is error
31 @retval EFI_SUCCESS Success
32 @retval EFI_TIMEOUT Device has no response
37 IN EFI_USB_IO_PROTOCOL
*UsbIo
,
40 IN UINT16 DescriptorLength
,
45 EFI_USB_DEVICE_REQUEST DevReq
;
48 return EFI_INVALID_PARAMETER
;
51 ZeroMem (&DevReq
, sizeof (EFI_USB_DEVICE_REQUEST
));
53 DevReq
.RequestType
= USB_DEV_GET_DESCRIPTOR_REQ_TYPE
;
54 DevReq
.Request
= USB_REQ_GET_DESCRIPTOR
;
57 DevReq
.Length
= DescriptorLength
;
59 return UsbIo
->UsbControlTransfer (
74 @param UsbIo EFI_USB_IO_PROTOCOL
75 @param Value Device Request Value
76 @param Index Device Request Index
77 @param DescriptorLength Descriptor Length
78 @param Descriptor Descriptor buffer to set
79 @param Status Transfer Status
81 @retval EFI_INVALID_PARAMETER Parameter is error
82 @retval EFI_SUCCESS Success
83 @retval EFI_TIMEOUT Device has no response
88 IN EFI_USB_IO_PROTOCOL
*UsbIo
,
91 IN UINT16 DescriptorLength
,
96 EFI_USB_DEVICE_REQUEST DevReq
;
99 return EFI_INVALID_PARAMETER
;
102 ZeroMem (&DevReq
, sizeof (EFI_USB_DEVICE_REQUEST
));
104 DevReq
.RequestType
= USB_DEV_SET_DESCRIPTOR_REQ_TYPE
;
105 DevReq
.Request
= USB_REQ_SET_DESCRIPTOR
;
106 DevReq
.Value
= Value
;
107 DevReq
.Index
= Index
;
108 DevReq
.Length
= DescriptorLength
;
110 return UsbIo
->UsbControlTransfer (
123 Usb Get Device Interface
125 @param UsbIo EFI_USB_IO_PROTOCOL
126 @param Index Interface index value
127 @param AltSetting Alternate setting
128 @param Status Trasnsfer status
130 @retval EFI_INVALID_PARAMETER Parameter is error
131 @retval EFI_SUCCESS Success
132 @retval EFI_TIMEOUT Device has no response
137 IN EFI_USB_IO_PROTOCOL
*UsbIo
,
139 OUT UINT8
*AltSetting
,
143 EFI_USB_DEVICE_REQUEST DevReq
;
146 return EFI_INVALID_PARAMETER
;
149 ZeroMem (&DevReq
, sizeof (EFI_USB_DEVICE_REQUEST
));
151 DevReq
.RequestType
= USB_DEV_GET_INTERFACE_REQ_TYPE
;
152 DevReq
.Request
= USB_REQ_GET_INTERFACE
;
153 DevReq
.Index
= Index
;
156 return UsbIo
->UsbControlTransfer (
169 Usb Set Device Interface
171 @param UsbIo EFI_USB_IO_PROTOCOL
172 @param InterfaceNo Interface Number
173 @param AltSetting Alternate setting
174 @param Status Trasnsfer status
176 @retval EFI_INVALID_PARAMETER Parameter is error
177 @retval EFI_SUCCESS Success
178 @retval EFI_TIMEOUT Device has no response
183 IN EFI_USB_IO_PROTOCOL
*UsbIo
,
184 IN UINT16 InterfaceNo
,
185 IN UINT16 AltSetting
,
189 EFI_USB_DEVICE_REQUEST DevReq
;
192 return EFI_INVALID_PARAMETER
;
195 ZeroMem (&DevReq
, sizeof (EFI_USB_DEVICE_REQUEST
));
197 DevReq
.RequestType
= USB_DEV_SET_INTERFACE_REQ_TYPE
;
198 DevReq
.Request
= USB_REQ_SET_INTERFACE
;
199 DevReq
.Value
= AltSetting
;
200 DevReq
.Index
= InterfaceNo
;
203 return UsbIo
->UsbControlTransfer (
216 Usb Get Device Configuration
218 @param UsbIo EFI_USB_IO_PROTOCOL
219 @param ConfigValue Config Value
220 @param Status Transfer Status
222 @retval EFI_INVALID_PARAMETER Parameter is error
223 @retval EFI_SUCCESS Success
224 @retval EFI_TIMEOUT Device has no response
228 UsbGetConfiguration (
229 IN EFI_USB_IO_PROTOCOL
*UsbIo
,
230 OUT UINT8
*ConfigValue
,
234 EFI_USB_DEVICE_REQUEST DevReq
;
237 return EFI_INVALID_PARAMETER
;
240 ZeroMem (&DevReq
, sizeof (EFI_USB_DEVICE_REQUEST
));
242 DevReq
.RequestType
= USB_DEV_GET_CONFIGURATION_REQ_TYPE
;
243 DevReq
.Request
= USB_REQ_GET_CONFIG
;
246 return UsbIo
->UsbControlTransfer (
259 Usb Set Device Configuration
261 @param UsbIo EFI_USB_IO_PROTOCOL
262 @param Value Configuration Value to set
263 @param Status Transfer status
265 @retval EFI_INVALID_PARAMETER Parameter is error
266 @retval EFI_SUCCESS Success
267 @retval EFI_TIMEOUT Device has no response
271 UsbSetConfiguration (
272 IN EFI_USB_IO_PROTOCOL
*UsbIo
,
277 EFI_USB_DEVICE_REQUEST DevReq
;
280 return EFI_INVALID_PARAMETER
;
283 ZeroMem (&DevReq
, sizeof (EFI_USB_DEVICE_REQUEST
));
285 DevReq
.RequestType
= USB_DEV_SET_CONFIGURATION_REQ_TYPE
;
286 DevReq
.Request
= USB_REQ_SET_CONFIG
;
287 DevReq
.Value
= Value
;
289 return UsbIo
->UsbControlTransfer (
302 Usb Set Device Feature
304 @param UsbIo EFI_USB_IO_PROTOCOL
305 @param Recipient Interface/Device/Endpoint
306 @param Value Request value
307 @param Target Request Index
308 @param Status Transfer status
310 @retval EFI_INVALID_PARAMETER Parameter is error
311 @retval EFI_SUCCESS Success
312 @retval EFI_TIMEOUT Device has no response
317 IN EFI_USB_IO_PROTOCOL
*UsbIo
,
324 EFI_USB_DEVICE_REQUEST DevReq
;
327 return EFI_INVALID_PARAMETER
;
330 ZeroMem (&DevReq
, sizeof (EFI_USB_DEVICE_REQUEST
));
334 case USB_TARGET_DEVICE
:
335 DevReq
.RequestType
= USB_DEV_SET_FEATURE_REQ_TYPE_D
;
338 case USB_TARGET_INTERFACE
:
339 DevReq
.RequestType
= USB_DEV_SET_FEATURE_REQ_TYPE_I
;
342 case USB_TARGET_ENDPOINT
:
343 DevReq
.RequestType
= USB_DEV_SET_FEATURE_REQ_TYPE_E
;
347 // Fill device request, see USB1.1 spec
349 DevReq
.Request
= USB_REQ_SET_FEATURE
;
350 DevReq
.Value
= Value
;
351 DevReq
.Index
= Target
;
354 return UsbIo
->UsbControlTransfer (
367 Usb Clear Device Feature
369 @param UsbIo EFI_USB_IO_PROTOCOL
370 @param Recipient Interface/Device/Endpoint
371 @param Value Request value
372 @param Target Request Index
373 @param Status Transfer status
375 @retval EFI_INVALID_PARAMETER Parameter is error
376 @retval EFI_SUCCESS Success
377 @retval EFI_TIMEOUT Device has no response
382 IN EFI_USB_IO_PROTOCOL
*UsbIo
,
389 EFI_USB_DEVICE_REQUEST DevReq
;
392 return EFI_INVALID_PARAMETER
;
395 ZeroMem (&DevReq
, sizeof (EFI_USB_DEVICE_REQUEST
));
399 case USB_TARGET_DEVICE
:
400 DevReq
.RequestType
= USB_DEV_CLEAR_FEATURE_REQ_TYPE_D
;
403 case USB_TARGET_INTERFACE
:
404 DevReq
.RequestType
= USB_DEV_CLEAR_FEATURE_REQ_TYPE_I
;
407 case USB_TARGET_ENDPOINT
:
408 DevReq
.RequestType
= USB_DEV_CLEAR_FEATURE_REQ_TYPE_E
;
412 // Fill device request, see USB1.1 spec
414 DevReq
.Request
= USB_REQ_CLEAR_FEATURE
;
415 DevReq
.Value
= Value
;
416 DevReq
.Index
= Target
;
419 return UsbIo
->UsbControlTransfer (
432 Usb Get Device Status
434 @param UsbIo EFI_USB_IO_PROTOCOL
435 @param Recipient Interface/Device/Endpoint
436 @param Target Request index
437 @param DevStatus Device status
438 @param Status Transfer status
440 @retval EFI_INVALID_PARAMETER Parameter is error
441 @retval EFI_SUCCESS Success
442 @retval EFI_TIMEOUT Device has no response
447 IN EFI_USB_IO_PROTOCOL
*UsbIo
,
450 OUT UINT16
*DevStatus
,
454 EFI_USB_DEVICE_REQUEST DevReq
;
457 return EFI_INVALID_PARAMETER
;
460 ZeroMem (&DevReq
, sizeof (EFI_USB_DEVICE_REQUEST
));
464 case USB_TARGET_DEVICE
:
465 DevReq
.RequestType
= USB_DEV_GET_STATUS_REQ_TYPE_D
;
468 case USB_TARGET_INTERFACE
:
469 DevReq
.RequestType
= USB_DEV_GET_STATUS_REQ_TYPE_I
;
472 case USB_TARGET_ENDPOINT
:
473 DevReq
.RequestType
= USB_DEV_GET_STATUS_REQ_TYPE_E
;
477 // Fill device request, see USB1.1 spec
479 DevReq
.Request
= USB_REQ_GET_STATUS
;
481 DevReq
.Index
= Target
;
484 return UsbIo
->UsbControlTransfer (
500 @param UsbIo EFI_USB_IO_PROTOCOL
501 @param EndpointNo Endpoint Number
502 @param Status Transfer Status
504 @retval EFI_NOT_FOUND Can't find the Endpoint
505 @retval EFI_DEVICE_ERROR Hardware error
506 @retval EFI_SUCCESS Success
510 UsbClearEndpointHalt (
511 IN EFI_USB_IO_PROTOCOL
*UsbIo
,
517 EFI_USB_ENDPOINT_DESCRIPTOR EndpointDescriptor
;
518 EFI_USB_INTERFACE_DESCRIPTOR InterfaceDescriptor
;
521 ZeroMem (&EndpointDescriptor
, sizeof (EFI_USB_ENDPOINT_DESCRIPTOR
));
523 // First seach the endpoint descriptor for that endpoint addr
525 Result
= UsbIo
->UsbGetInterfaceDescriptor (
529 if (EFI_ERROR (Result
)) {
533 for (Index
= 0; Index
< InterfaceDescriptor
.NumEndpoints
; Index
++) {
534 Result
= UsbIo
->UsbGetEndpointDescriptor (
539 if (EFI_ERROR (Result
)) {
543 if (EndpointDescriptor
.EndpointAddress
== EndpointNo
) {
548 if (Index
== InterfaceDescriptor
.NumEndpoints
) {
552 return EFI_NOT_FOUND
;
555 Result
= UsbClearFeature (
559 EndpointDescriptor
.EndpointAddress
,