/src/edk2/MdeModulePkg/Bus/Usb/UsbBusPei/PeiUsbLib.c
Line | Count | Source |
1 | | /** @file |
2 | | Common Library for PEI USB |
3 | | |
4 | | Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved. <BR> |
5 | | |
6 | | SPDX-License-Identifier: BSD-2-Clause-Patent |
7 | | |
8 | | **/ |
9 | | |
10 | | #include "UsbPeim.h" |
11 | | #include "PeiUsbLib.h" |
12 | | |
13 | | /** |
14 | | Get a given usb descriptor. |
15 | | |
16 | | @param PeiServices General-purpose services that are available to every PEIM. |
17 | | @param UsbIoPpi Indicates the PEI_USB_IO_PPI instance. |
18 | | @param Value Request Value. |
19 | | @param Index Request Index. |
20 | | @param DescriptorLength Request descriptor Length. |
21 | | @param Descriptor Request descriptor. |
22 | | |
23 | | |
24 | | @retval EFI_SUCCESS Usb descriptor is obtained successfully. |
25 | | @retval EFI_DEVICE_ERROR Cannot get the usb descriptor due to a hardware error. |
26 | | @retval Others Other failure occurs. |
27 | | |
28 | | **/ |
29 | | EFI_STATUS |
30 | | PeiUsbGetDescriptor ( |
31 | | IN EFI_PEI_SERVICES **PeiServices, |
32 | | IN PEI_USB_IO_PPI *UsbIoPpi, |
33 | | IN UINT16 Value, |
34 | | IN UINT16 Index, |
35 | | IN UINT16 DescriptorLength, |
36 | | OUT VOID *Descriptor |
37 | | ) |
38 | 67 | { |
39 | 67 | EFI_USB_DEVICE_REQUEST DevReq; |
40 | | |
41 | 67 | ASSERT (UsbIoPpi != NULL); |
42 | | |
43 | 67 | DevReq.RequestType = USB_DEV_GET_DESCRIPTOR_REQ_TYPE; |
44 | 67 | DevReq.Request = USB_DEV_GET_DESCRIPTOR; |
45 | 67 | DevReq.Value = Value; |
46 | 67 | DevReq.Index = Index; |
47 | 67 | DevReq.Length = DescriptorLength; |
48 | | |
49 | 67 | return UsbIoPpi->UsbControlTransfer ( |
50 | 67 | PeiServices, |
51 | 67 | UsbIoPpi, |
52 | 67 | &DevReq, |
53 | 67 | EfiUsbDataIn, |
54 | 67 | PcdGet32 (PcdUsbTransferTimeoutValue), |
55 | 67 | Descriptor, |
56 | 67 | DescriptorLength |
57 | 67 | ); |
58 | 67 | } |
59 | | |
60 | | /** |
61 | | Set a usb device with a specified address. |
62 | | |
63 | | @param PeiServices General-purpose services that are available to every PEIM. |
64 | | @param UsbIoPpi Indicates the PEI_USB_IO_PPI instance. |
65 | | @param AddressValue The address to assign. |
66 | | |
67 | | @retval EFI_SUCCESS Usb device address is set successfully. |
68 | | @retval EFI_DEVICE_ERROR Cannot set the usb address due to a hardware error. |
69 | | @retval Others Other failure occurs. |
70 | | |
71 | | **/ |
72 | | EFI_STATUS |
73 | | PeiUsbSetDeviceAddress ( |
74 | | IN EFI_PEI_SERVICES **PeiServices, |
75 | | IN PEI_USB_IO_PPI *UsbIoPpi, |
76 | | IN UINT16 AddressValue |
77 | | ) |
78 | 0 | { |
79 | 0 | EFI_USB_DEVICE_REQUEST DevReq; |
80 | |
|
81 | 0 | ASSERT (UsbIoPpi != NULL); |
82 | |
|
83 | 0 | DevReq.RequestType = USB_DEV_SET_ADDRESS_REQ_TYPE; |
84 | 0 | DevReq.Request = USB_DEV_SET_ADDRESS; |
85 | 0 | DevReq.Value = AddressValue; |
86 | 0 | DevReq.Index = 0; |
87 | 0 | DevReq.Length = 0; |
88 | |
|
89 | 0 | return UsbIoPpi->UsbControlTransfer ( |
90 | 0 | PeiServices, |
91 | 0 | UsbIoPpi, |
92 | 0 | &DevReq, |
93 | 0 | EfiUsbNoData, |
94 | 0 | PcdGet32 (PcdUsbTransferTimeoutValue), |
95 | 0 | NULL, |
96 | 0 | 0 |
97 | 0 | ); |
98 | 0 | } |
99 | | |
100 | | /** |
101 | | Configure a usb device to Configuration 1. |
102 | | |
103 | | @param PeiServices General-purpose services that are available to every PEIM. |
104 | | @param UsbIoPpi Indicates the PEI_USB_IO_PPI instance. |
105 | | |
106 | | @retval EFI_SUCCESS Usb device is set to use Configuration 1 successfully. |
107 | | @retval EFI_DEVICE_ERROR Cannot set the usb device due to a hardware error. |
108 | | @retval Others Other failure occurs. |
109 | | |
110 | | **/ |
111 | | EFI_STATUS |
112 | | PeiUsbSetConfiguration ( |
113 | | IN EFI_PEI_SERVICES **PeiServices, |
114 | | IN PEI_USB_IO_PPI *UsbIoPpi |
115 | | ) |
116 | 0 | { |
117 | 0 | EFI_USB_DEVICE_REQUEST DevReq; |
118 | |
|
119 | 0 | ZeroMem (&DevReq, sizeof (EFI_USB_DEVICE_REQUEST)); |
120 | |
|
121 | 0 | DevReq.RequestType = USB_DEV_SET_CONFIGURATION_REQ_TYPE; |
122 | 0 | DevReq.Request = USB_DEV_SET_CONFIGURATION; |
123 | 0 | DevReq.Value = 1; |
124 | |
|
125 | 0 | return UsbIoPpi->UsbControlTransfer ( |
126 | 0 | PeiServices, |
127 | 0 | UsbIoPpi, |
128 | 0 | &DevReq, |
129 | 0 | EfiUsbNoData, |
130 | 0 | PcdGet32 (PcdUsbTransferTimeoutValue), |
131 | 0 | NULL, |
132 | 0 | 0 |
133 | 0 | ); |
134 | 0 | } |
135 | | |
136 | | /** |
137 | | Judge if the port is connected with a usb device or not. |
138 | | |
139 | | @param PortStatus The usb port status gotten. |
140 | | |
141 | | @retval TRUE A usb device is connected with the port. |
142 | | @retval FALSE No usb device is connected with the port. |
143 | | |
144 | | **/ |
145 | | BOOLEAN |
146 | | IsPortConnect ( |
147 | | IN UINT16 PortStatus |
148 | | ) |
149 | 0 | { |
150 | | // |
151 | | // return the bit 0 value of PortStatus |
152 | | // |
153 | 0 | if ((PortStatus & USB_PORT_STAT_CONNECTION) != 0) { |
154 | 0 | return TRUE; |
155 | 0 | } else { |
156 | 0 | return FALSE; |
157 | 0 | } |
158 | 0 | } |
159 | | |
160 | | /** |
161 | | Get device speed according to port status. |
162 | | |
163 | | @param PortStatus The usb port status gotten. |
164 | | |
165 | | @return Device speed value. |
166 | | |
167 | | **/ |
168 | | UINTN |
169 | | PeiUsbGetDeviceSpeed ( |
170 | | IN UINT16 PortStatus |
171 | | ) |
172 | 0 | { |
173 | 0 | if ((PortStatus & USB_PORT_STAT_LOW_SPEED) != 0) { |
174 | 0 | return EFI_USB_SPEED_LOW; |
175 | 0 | } else if ((PortStatus & USB_PORT_STAT_HIGH_SPEED) != 0) { |
176 | 0 | return EFI_USB_SPEED_HIGH; |
177 | 0 | } else if ((PortStatus & USB_PORT_STAT_SUPER_SPEED) != 0) { |
178 | 0 | return EFI_USB_SPEED_SUPER; |
179 | 0 | } else { |
180 | 0 | return EFI_USB_SPEED_FULL; |
181 | 0 | } |
182 | 0 | } |