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) {
284 IsPortSuspendChange (
285 IN UINT16 PortChangeStatus
290 Tell if there is a Suspend Change Status in that port.
293 PortChangeStatus - The status value of that port.
302 // return the bit 2 value of PortChangeStatus
304 if ((PortChangeStatus
& USB_PORT_STAT_C_SUSPEND
) != 0) {
311 INTERFACE_DESC_LIST_ENTRY
*
312 FindInterfaceListEntry (
313 IN EFI_USB_IO_PROTOCOL
*This
318 Find Interface ListEntry.
321 This - EFI_USB_IO_PROTOCOL
324 INTERFACE_DESC_LIST_ENTRY pointer
328 USB_IO_CONTROLLER_DEVICE
*UsbIoController
;
329 USB_IO_DEVICE
*UsbIoDev
;
330 LIST_ENTRY
*InterfaceListHead
;
331 INTERFACE_DESC_LIST_ENTRY
*InterfaceListEntry
;
333 UsbIoController
= USB_IO_CONTROLLER_DEVICE_FROM_USB_IO_THIS (This
);
334 UsbIoDev
= UsbIoController
->UsbDevice
;
336 if (!UsbIoDev
->IsConfigured
) {
340 InterfaceListHead
= &UsbIoDev
->ActiveConfig
->InterfaceDescListHead
;
341 InterfaceListEntry
= (INTERFACE_DESC_LIST_ENTRY
*) (InterfaceListHead
->ForwardLink
);
344 // Loop all interface descriptor to get match one.
346 while (InterfaceListEntry
!= (INTERFACE_DESC_LIST_ENTRY
*) InterfaceListHead
) {
347 if (InterfaceListEntry
->InterfaceDescriptor
.InterfaceNumber
== UsbIoController
->InterfaceNumber
) {
348 return InterfaceListEntry
;
351 InterfaceListEntry
= (INTERFACE_DESC_LIST_ENTRY
*) InterfaceListEntry
->Link
.ForwardLink
;
357 ENDPOINT_DESC_LIST_ENTRY
*
358 FindEndPointListEntry (
359 IN EFI_USB_IO_PROTOCOL
*This
,
360 IN UINT8 EndPointAddress
365 Find EndPoint ListEntry.
368 This - EFI_USB_IO_PROTOCOL
369 EndPointAddress - Endpoint address.
372 ENDPOINT_DESC_LIST_ENTRY pointer
376 INTERFACE_DESC_LIST_ENTRY
*InterfaceListEntry
;
377 LIST_ENTRY
*EndpointDescListHead
;
378 ENDPOINT_DESC_LIST_ENTRY
*EndPointListEntry
;
380 InterfaceListEntry
= FindInterfaceListEntry (This
);
381 if (InterfaceListEntry
!= NULL
) {
382 EndpointDescListHead
= &InterfaceListEntry
->EndpointDescListHead
;
383 EndPointListEntry
= (ENDPOINT_DESC_LIST_ENTRY
*) (EndpointDescListHead
->ForwardLink
);
386 // Loop all interface descriptor to get match one.
388 while (EndPointListEntry
!= (ENDPOINT_DESC_LIST_ENTRY
*) EndpointDescListHead
) {
389 if (EndPointListEntry
->EndpointDescriptor
.EndpointAddress
== EndPointAddress
) {
390 return EndPointListEntry
;
393 EndPointListEntry
= (ENDPOINT_DESC_LIST_ENTRY
*) EndPointListEntry
->Link
.ForwardLink
;
402 IN EFI_USB_IO_PROTOCOL
*UsbIo
,
403 IN UINT8 EndpointAddr
,
404 OUT UINT8
*DataToggle
409 Get the datatoggle of a specified endpoint.
412 UsbIo - Given Usb Controller device.
413 EndpointAddr - Given Endpoint address.
414 DataToggle - The current data toggle of that endpoint
422 ENDPOINT_DESC_LIST_ENTRY
*EndpointListEntry
;
426 EndpointListEntry
= FindEndPointListEntry (UsbIo
, EndpointAddr
);
427 if (EndpointListEntry
== NULL
) {
431 *DataToggle
= (UINT8
) (EndpointListEntry
->Toggle
);
437 IN EFI_USB_IO_PROTOCOL
*UsbIo
,
438 IN UINT8 EndpointAddr
,
444 Set the datatoggle of a specified endpoint
447 UsbIo - Given Usb Controller device.
448 EndpointAddr - Given Endpoint address.
449 DataToggle - The current data toggle of that endpoint to be set
457 ENDPOINT_DESC_LIST_ENTRY
*EndpointListEntry
;
459 EndpointListEntry
= FindEndPointListEntry (UsbIo
, EndpointAddr
);
460 if (EndpointListEntry
== NULL
) {
464 EndpointListEntry
->Toggle
= DataToggle
;
469 GetDeviceEndPointMaxPacketLength (
470 IN EFI_USB_IO_PROTOCOL
*UsbIo
,
471 IN UINT8 EndpointAddr
,
472 OUT UINTN
*MaxPacketLength
477 Get the Max Packet Length of the speified Endpoint.
480 UsbIo - Given Usb Controller device.
481 EndpointAddr - Given Endpoint address.
482 MaxPacketLength - The max packet length of that endpoint
490 ENDPOINT_DESC_LIST_ENTRY
*EndpointListEntry
;
492 *MaxPacketLength
= 0;
494 EndpointListEntry
= FindEndPointListEntry (UsbIo
, EndpointAddr
);
495 if (EndpointListEntry
== NULL
) {
499 *MaxPacketLength
= (UINTN
) (EndpointListEntry
->EndpointDescriptor
.MaxPacketSize
);
506 UsbSetDeviceAddress (
507 IN EFI_USB_IO_PROTOCOL
*UsbIo
,
508 IN UINT16 AddressValue
,
515 Usb Set Device Address
519 UsbIo - EFI_USB_IO_PROTOCOL
520 AddressValue - Device address
521 Status - Transfer status
525 EFI_INVALID_PARAMETER - Parameter is error
526 EFI_SUCCESS - Success
527 EFI_TIMEOUT - Device has no response
532 EFI_USB_DEVICE_REQUEST DevReq
;
535 return EFI_INVALID_PARAMETER
;
538 ZeroMem (&DevReq
, sizeof (EFI_USB_DEVICE_REQUEST
));
540 DevReq
.RequestType
= USB_DEV_SET_ADDRESS_REQ_TYPE
;
541 DevReq
.Request
= USB_DEV_SET_ADDRESS
;
542 DevReq
.Value
= AddressValue
;
544 return UsbIo
->UsbControlTransfer (