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
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.
17 Helper functions for USB
26 // Following APIs are used to query Port Status
35 Tell if there is a device connected to that port according to
39 PortStatus - The status value of that port.
48 // return the bit 0 value of PortStatus
50 if ((PortStatus
& USB_PORT_STAT_CONNECTION
) != 0) {
64 Tell if Port is enabled.
67 PortStatus - The status value of that port.
71 FALSE - Port is disable
76 // return the bit 1 value of PortStatus
78 if ((PortStatus
& USB_PORT_STAT_ENABLE
) != 0) {
92 Tell if the port is being reset.
95 PortStatus - The status value of that port.
104 // return the bit 4 value of PortStatus
106 if ((PortStatus
& USB_PORT_STAT_RESET
) != 0) {
120 Tell if there is power applied to that port.
123 PortStatus - The status value of that port.
132 // return the bit 8 value of PortStatus
134 if ((PortStatus
& USB_PORT_STAT_POWER
) != 0) {
142 IsPortLowSpeedDeviceAttached (
148 Tell if the connected device is a low device.
151 PortStatus - The status value of that port.
160 // return the bit 9 value of PortStatus
162 if ((PortStatus
& USB_PORT_STAT_LOW_SPEED
) != 0) {
176 Tell if the port is suspend.
179 PortStatus - The status value of that port.
188 // return the bit 2 value of PortStatus
190 if ((PortStatus
& USB_PORT_STAT_SUSPEND
) != 0) {
197 // Following APIs are used to query Port Change Status
200 IsPortConnectChange (
201 IN UINT16 PortChangeStatus
206 Tell if there is a Connect Change status in that port.
209 PortChangeStatus - The status value of that port.
218 // return the bit 0 value of PortChangeStatus
220 if ((PortChangeStatus
& USB_PORT_STAT_C_CONNECTION
) != 0) {
228 IsPortEnableDisableChange (
229 IN UINT16 PortChangeStatus
234 Tell if there is a Enable/Disable change in that port.
237 PortChangeStatus - The status value of that port.
246 // return the bit 1 value of PortChangeStatus
248 if ((PortChangeStatus
& USB_PORT_STAT_C_ENABLE
) != 0) {
257 IN UINT16 PortChangeStatus
262 Tell if there is a Port Reset Change status in that port.
265 PortChangeStatus - The status value of that port.
274 // return the bit 4 value of PortChangeStatus
276 if ((PortChangeStatus
& USB_PORT_STAT_C_RESET
) != 0) {
285 IsPortSuspendChange (
286 IN UINT16 PortChangeStatus
291 Tell if there is a Suspend Change Status in that port.
294 PortChangeStatus - The status value of that port.
303 // return the bit 2 value of PortChangeStatus
305 if ((PortChangeStatus
& USB_PORT_STAT_C_SUSPEND
) != 0) {
313 INTERFACE_DESC_LIST_ENTRY
*
314 FindInterfaceListEntry (
315 IN EFI_USB_IO_PROTOCOL
*This
320 Find Interface ListEntry.
323 This - EFI_USB_IO_PROTOCOL
326 INTERFACE_DESC_LIST_ENTRY pointer
330 USB_IO_CONTROLLER_DEVICE
*UsbIoController
;
331 USB_IO_DEVICE
*UsbIoDev
;
332 LIST_ENTRY
*InterfaceListHead
;
333 INTERFACE_DESC_LIST_ENTRY
*InterfaceListEntry
;
335 UsbIoController
= USB_IO_CONTROLLER_DEVICE_FROM_USB_IO_THIS (This
);
336 UsbIoDev
= UsbIoController
->UsbDevice
;
338 if (!UsbIoDev
->IsConfigured
) {
342 InterfaceListHead
= &UsbIoDev
->ActiveConfig
->InterfaceDescListHead
;
343 InterfaceListEntry
= (INTERFACE_DESC_LIST_ENTRY
*) (InterfaceListHead
->ForwardLink
);
346 // Loop all interface descriptor to get match one.
348 while (InterfaceListEntry
!= (INTERFACE_DESC_LIST_ENTRY
*) InterfaceListHead
) {
349 if (InterfaceListEntry
->InterfaceDescriptor
.InterfaceNumber
== UsbIoController
->InterfaceNumber
) {
350 return InterfaceListEntry
;
353 InterfaceListEntry
= (INTERFACE_DESC_LIST_ENTRY
*) InterfaceListEntry
->Link
.ForwardLink
;
359 ENDPOINT_DESC_LIST_ENTRY
*
360 FindEndPointListEntry (
361 IN EFI_USB_IO_PROTOCOL
*This
,
362 IN UINT8 EndPointAddress
367 Find EndPoint ListEntry.
370 This - EFI_USB_IO_PROTOCOL
371 EndpointAddr - Endpoint address.
374 ENDPOINT_DESC_LIST_ENTRY pointer
378 INTERFACE_DESC_LIST_ENTRY
*InterfaceListEntry
;
379 LIST_ENTRY
*EndpointDescListHead
;
380 ENDPOINT_DESC_LIST_ENTRY
*EndPointListEntry
;
382 InterfaceListEntry
= FindInterfaceListEntry (This
);
383 if (InterfaceListEntry
!= NULL
) {
384 EndpointDescListHead
= &InterfaceListEntry
->EndpointDescListHead
;
385 EndPointListEntry
= (ENDPOINT_DESC_LIST_ENTRY
*) (EndpointDescListHead
->ForwardLink
);
388 // Loop all interface descriptor to get match one.
390 while (EndPointListEntry
!= (ENDPOINT_DESC_LIST_ENTRY
*) EndpointDescListHead
) {
391 if (EndPointListEntry
->EndpointDescriptor
.EndpointAddress
== EndPointAddress
) {
392 return EndPointListEntry
;
395 EndPointListEntry
= (ENDPOINT_DESC_LIST_ENTRY
*) EndPointListEntry
->Link
.ForwardLink
;
404 IN EFI_USB_IO_PROTOCOL
*UsbIo
,
405 IN UINT8 EndpointAddr
,
406 OUT UINT8
*DataToggle
411 Get the datatoggle of a specified endpoint.
414 UsbIo - Given Usb Controller device.
415 EndpointAddr - Given Endpoint address.
416 DataToggle - The current data toggle of that endpoint
424 ENDPOINT_DESC_LIST_ENTRY
*EndpointListEntry
;
428 EndpointListEntry
= FindEndPointListEntry (UsbIo
, EndpointAddr
);
429 if (EndpointListEntry
== NULL
) {
433 *DataToggle
= (UINT8
) (EndpointListEntry
->Toggle
);
439 IN EFI_USB_IO_PROTOCOL
*UsbIo
,
440 IN UINT8 EndpointAddr
,
446 Set the datatoggle of a specified endpoint
449 UsbIo - Given Usb Controller device.
450 EndpointAddr - Given Endpoint address.
451 DataToggle - The current data toggle of that endpoint to be set
459 ENDPOINT_DESC_LIST_ENTRY
*EndpointListEntry
;
461 EndpointListEntry
= FindEndPointListEntry (UsbIo
, EndpointAddr
);
462 if (EndpointListEntry
== NULL
) {
466 EndpointListEntry
->Toggle
= DataToggle
;
471 GetDeviceEndPointMaxPacketLength (
472 IN EFI_USB_IO_PROTOCOL
*UsbIo
,
473 IN UINT8 EndpointAddr
,
474 OUT UINT8
*MaxPacketLength
479 Get the Max Packet Length of the speified Endpoint.
482 UsbIo - Given Usb Controller device.
483 EndpointAddr - Given Endpoint address.
484 MaxPacketLength - The max packet length of that endpoint
492 ENDPOINT_DESC_LIST_ENTRY
*EndpointListEntry
;
494 *MaxPacketLength
= 0;
496 EndpointListEntry
= FindEndPointListEntry (UsbIo
, EndpointAddr
);
497 if (EndpointListEntry
== NULL
) {
501 *MaxPacketLength
= (UINT8
) (EndpointListEntry
->EndpointDescriptor
.MaxPacketSize
);
508 UsbSetDeviceAddress (
509 IN EFI_USB_IO_PROTOCOL
*UsbIo
,
510 IN UINT16 AddressValue
,
517 Usb Set Device Address
521 UsbIo - EFI_USB_IO_PROTOCOL
522 AddressValue - Device address
523 Status - Transfer status
527 EFI_INVALID_PARAMETER - Parameter is error
528 EFI_SUCCESS - Success
529 EFI_TIMEOUT - Device has no response
534 EFI_USB_DEVICE_REQUEST DevReq
;
537 return EFI_INVALID_PARAMETER
;
540 ZeroMem (&DevReq
, sizeof (EFI_USB_DEVICE_REQUEST
));
542 DevReq
.RequestType
= USB_DEV_SET_ADDRESS_REQ_TYPE
;
543 DevReq
.Request
= USB_DEV_SET_ADDRESS
;
544 DevReq
.Value
= AddressValue
;
546 return UsbIo
->UsbControlTransfer (