]>
Commit | Line | Data |
---|---|---|
4b1bf81c | 1 | /** @file\r |
2 | Common Libarary for PEI USB.\r | |
3 | \r | |
506560e7 | 4 | Copyright (c) 2006 - 2014, Intel Corporation. All rights reserved.<BR>\r |
4b1bf81c | 5 | \r |
6 | This program and the accompanying materials\r | |
7 | are licensed and made available under the terms and conditions\r | |
8 | of the BSD License which accompanies this distribution. The\r | |
9 | full text of the license may be found at\r | |
10 | http://opensource.org/licenses/bsd-license.php\r | |
11 | \r | |
12 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
13 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
14 | \r | |
15 | **/\r | |
16 | \r | |
17 | #include "UsbPeim.h"\r | |
18 | #include "PeiUsbLib.h"\r | |
19 | \r | |
20 | /**\r | |
21 | Get a given usb descriptor.\r | |
22 | \r | |
23 | @param PeiServices General-purpose services that are available to every PEIM.\r | |
24 | @param UsbIoPpi Indicates the PEI_USB_IO_PPI instance.\r | |
25 | @param Value Request Value.\r | |
26 | @param Index Request Index.\r | |
27 | @param DescriptorLength Request descriptor Length.\r | |
28 | @param Descriptor Request descriptor.\r | |
29 | \r | |
30 | \r | |
31 | @retval EFI_SUCCESS Usb descriptor is obtained successfully.\r | |
32 | @retval EFI_DEVICE_ERROR Cannot get the usb descriptor due to a hardware error.\r | |
33 | @retval Others Other failure occurs.\r | |
34 | \r | |
35 | **/\r | |
36 | EFI_STATUS\r | |
37 | PeiUsbGetDescriptor (\r | |
38 | IN EFI_PEI_SERVICES **PeiServices,\r | |
39 | IN PEI_USB_IO_PPI *UsbIoPpi,\r | |
40 | IN UINT16 Value,\r | |
41 | IN UINT16 Index,\r | |
42 | IN UINT16 DescriptorLength,\r | |
43 | OUT VOID *Descriptor\r | |
44 | )\r | |
45 | {\r | |
46 | EFI_USB_DEVICE_REQUEST DevReq;\r | |
47 | \r | |
48 | ASSERT (UsbIoPpi != NULL);\r | |
49 | \r | |
50 | DevReq.RequestType = USB_DEV_GET_DESCRIPTOR_REQ_TYPE;\r | |
51 | DevReq.Request = USB_DEV_GET_DESCRIPTOR;\r | |
52 | DevReq.Value = Value;\r | |
53 | DevReq.Index = Index;\r | |
54 | DevReq.Length = DescriptorLength;\r | |
55 | \r | |
56 | return UsbIoPpi->UsbControlTransfer (\r | |
57 | PeiServices,\r | |
58 | UsbIoPpi,\r | |
59 | &DevReq,\r | |
60 | EfiUsbDataIn,\r | |
61 | PcdGet32 (PcdUsbTransferTimeoutValue),\r | |
62 | Descriptor,\r | |
63 | DescriptorLength\r | |
64 | );\r | |
65 | }\r | |
66 | \r | |
67 | /**\r | |
68 | Set a usb device with a specified address.\r | |
69 | \r | |
70 | @param PeiServices General-purpose services that are available to every PEIM.\r | |
71 | @param UsbIoPpi Indicates the PEI_USB_IO_PPI instance.\r | |
72 | @param AddressValue The address to assign.\r | |
73 | \r | |
74 | @retval EFI_SUCCESS Usb device address is set successfully.\r | |
75 | @retval EFI_DEVICE_ERROR Cannot set the usb address due to a hardware error.\r | |
76 | @retval Others Other failure occurs.\r | |
77 | \r | |
78 | **/\r | |
79 | EFI_STATUS\r | |
80 | PeiUsbSetDeviceAddress (\r | |
81 | IN EFI_PEI_SERVICES **PeiServices,\r | |
82 | IN PEI_USB_IO_PPI *UsbIoPpi,\r | |
83 | IN UINT16 AddressValue\r | |
84 | )\r | |
85 | {\r | |
86 | EFI_USB_DEVICE_REQUEST DevReq;\r | |
87 | \r | |
88 | ASSERT (UsbIoPpi != NULL);\r | |
89 | \r | |
90 | DevReq.RequestType = USB_DEV_SET_ADDRESS_REQ_TYPE;\r | |
91 | DevReq.Request = USB_DEV_SET_ADDRESS;\r | |
92 | DevReq.Value = AddressValue;\r | |
93 | DevReq.Index = 0;\r | |
94 | DevReq.Length = 0;\r | |
95 | \r | |
96 | return UsbIoPpi->UsbControlTransfer (\r | |
97 | PeiServices,\r | |
98 | UsbIoPpi,\r | |
99 | &DevReq,\r | |
100 | EfiUsbNoData,\r | |
101 | PcdGet32 (PcdUsbTransferTimeoutValue),\r | |
102 | NULL,\r | |
103 | 0\r | |
104 | );\r | |
105 | }\r | |
106 | \r | |
107 | /**\r | |
108 | Clear a given usb feature.\r | |
109 | \r | |
110 | @param PeiServices General-purpose services that are available to every PEIM.\r | |
111 | @param UsbIoPpi Indicates the PEI_USB_IO_PPI instance.\r | |
112 | @param Recipient The recipient of ClearFeature Request, should be one of Device/Interface/Endpoint.\r | |
113 | @param Value Request Value.\r | |
114 | @param Target Request Index.\r | |
115 | \r | |
116 | @retval EFI_SUCCESS Usb feature is cleared successfully.\r | |
117 | @retval EFI_DEVICE_ERROR Cannot clear the usb feature due to a hardware error.\r | |
118 | @retval Others Other failure occurs.\r | |
119 | \r | |
120 | **/\r | |
121 | EFI_STATUS\r | |
122 | PeiUsbClearDeviceFeature (\r | |
123 | IN EFI_PEI_SERVICES **PeiServices,\r | |
124 | IN PEI_USB_IO_PPI *UsbIoPpi,\r | |
125 | IN EFI_USB_RECIPIENT Recipient,\r | |
126 | IN UINT16 Value,\r | |
127 | IN UINT16 Target\r | |
128 | )\r | |
129 | {\r | |
130 | EFI_USB_DEVICE_REQUEST DevReq;\r | |
131 | \r | |
132 | ASSERT (UsbIoPpi != NULL);\r | |
133 | \r | |
134 | switch (Recipient) {\r | |
135 | case EfiUsbDevice:\r | |
136 | DevReq.RequestType = USB_DEV_CLEAR_FEATURE_REQ_TYPE_D;\r | |
137 | break;\r | |
138 | \r | |
139 | case EfiUsbInterface:\r | |
140 | DevReq.RequestType = USB_DEV_CLEAR_FEATURE_REQ_TYPE_I;\r | |
141 | break;\r | |
142 | \r | |
143 | case EfiUsbEndpoint:\r | |
144 | DevReq.RequestType = USB_DEV_CLEAR_FEATURE_REQ_TYPE_E;\r | |
145 | break;\r | |
146 | }\r | |
147 | \r | |
148 | DevReq.Request = USB_DEV_CLEAR_FEATURE;\r | |
149 | DevReq.Value = Value;\r | |
150 | DevReq.Index = Target;\r | |
151 | DevReq.Length = 0;\r | |
152 | \r | |
153 | return UsbIoPpi->UsbControlTransfer (\r | |
154 | PeiServices,\r | |
155 | UsbIoPpi,\r | |
156 | &DevReq,\r | |
157 | EfiUsbNoData,\r | |
158 | PcdGet32 (PcdUsbTransferTimeoutValue),\r | |
159 | NULL,\r | |
160 | 0\r | |
161 | );\r | |
162 | }\r | |
163 | \r | |
164 | /**\r | |
165 | Configure a usb device to Configuration 1.\r | |
166 | \r | |
167 | @param PeiServices General-purpose services that are available to every PEIM.\r | |
168 | @param UsbIoPpi Indicates the PEI_USB_IO_PPI instance.\r | |
169 | \r | |
170 | @retval EFI_SUCCESS Usb device is set to use Configuration 1 successfully.\r | |
171 | @retval EFI_DEVICE_ERROR Cannot set the usb device due to a hardware error.\r | |
172 | @retval Others Other failure occurs.\r | |
173 | \r | |
174 | **/\r | |
175 | EFI_STATUS\r | |
176 | PeiUsbSetConfiguration (\r | |
177 | IN EFI_PEI_SERVICES **PeiServices,\r | |
178 | IN PEI_USB_IO_PPI *UsbIoPpi\r | |
179 | )\r | |
180 | {\r | |
181 | EFI_USB_DEVICE_REQUEST DevReq;\r | |
182 | ZeroMem (&DevReq, sizeof (EFI_USB_DEVICE_REQUEST));\r | |
183 | \r | |
184 | DevReq.RequestType = USB_DEV_SET_CONFIGURATION_REQ_TYPE;\r | |
185 | DevReq.Request = USB_DEV_SET_CONFIGURATION;\r | |
186 | DevReq.Value = 1;\r | |
187 | \r | |
188 | return UsbIoPpi->UsbControlTransfer (\r | |
189 | PeiServices,\r | |
190 | UsbIoPpi,\r | |
191 | &DevReq,\r | |
192 | EfiUsbNoData,\r | |
193 | PcdGet32 (PcdUsbTransferTimeoutValue),\r | |
194 | NULL,\r | |
195 | 0\r | |
196 | );\r | |
197 | }\r | |
198 | \r | |
199 | /**\r | |
200 | Clear Endpoint Halt.\r | |
201 | \r | |
202 | @param PeiServices General-purpose services that are available to every PEIM.\r | |
203 | @param UsbIoPpi Indicates the PEI_USB_IO_PPI instance.\r | |
204 | @param EndpointAddress The endpoint address.\r | |
205 | \r | |
206 | @retval EFI_SUCCESS Endpoint halt is cleared successfully.\r | |
207 | @retval EFI_DEVICE_ERROR Cannot clear the endpoint halt status due to a hardware error.\r | |
208 | @retval Others Other failure occurs.\r | |
209 | \r | |
210 | **/\r | |
211 | EFI_STATUS\r | |
212 | PeiUsbClearEndpointHalt (\r | |
213 | IN EFI_PEI_SERVICES **PeiServices,\r | |
214 | IN PEI_USB_IO_PPI *UsbIoPpi,\r | |
215 | IN UINT8 EndpointAddress\r | |
216 | )\r | |
217 | {\r | |
506560e7 SZ |
218 | EFI_STATUS Status;\r |
219 | EFI_USB_INTERFACE_DESCRIPTOR *InterfaceDesc;\r | |
220 | EFI_USB_ENDPOINT_DESCRIPTOR *EndpointDescriptor;\r | |
221 | UINT8 EndpointIndex;\r | |
4b1bf81c | 222 | \r |
4b1bf81c | 223 | \r |
506560e7 SZ |
224 | //\r |
225 | // Check its interface\r | |
226 | //\r | |
227 | Status = UsbIoPpi->UsbGetInterfaceDescriptor (\r | |
228 | PeiServices,\r | |
229 | UsbIoPpi,\r | |
230 | &InterfaceDesc\r | |
231 | );\r | |
232 | if (EFI_ERROR (Status)) {\r | |
233 | return Status;\r | |
234 | }\r | |
235 | for (EndpointIndex = 0; EndpointIndex < InterfaceDesc->NumEndpoints; EndpointIndex++) {\r | |
4b1bf81c | 236 | Status = UsbIoPpi->UsbGetEndpointDescriptor (PeiServices, UsbIoPpi, EndpointIndex, &EndpointDescriptor);\r |
237 | if (EFI_ERROR (Status)) {\r | |
238 | return EFI_INVALID_PARAMETER;\r | |
239 | }\r | |
240 | \r | |
241 | if (EndpointDescriptor->EndpointAddress == EndpointAddress) {\r | |
242 | break;\r | |
243 | }\r | |
4b1bf81c | 244 | }\r |
245 | \r | |
506560e7 | 246 | if (EndpointIndex == InterfaceDesc->NumEndpoints) {\r |
4b1bf81c | 247 | return EFI_INVALID_PARAMETER;\r |
248 | }\r | |
249 | \r | |
250 | Status = PeiUsbClearDeviceFeature (\r | |
251 | PeiServices,\r | |
252 | UsbIoPpi,\r | |
253 | EfiUsbEndpoint,\r | |
254 | EfiUsbEndpointHalt,\r | |
255 | EndpointAddress\r | |
256 | );\r | |
257 | \r | |
4b1bf81c | 258 | return Status;\r |
259 | }\r | |
260 | \r | |
261 | /**\r | |
262 | Judge if the port is connected with a usb device or not.\r | |
263 | \r | |
264 | @param PortStatus The usb port status gotten.\r | |
265 | \r | |
266 | @retval TRUE A usb device is connected with the port.\r | |
267 | @retval FALSE No usb device is connected with the port.\r | |
268 | \r | |
269 | **/\r | |
270 | BOOLEAN\r | |
271 | IsPortConnect (\r | |
272 | IN UINT16 PortStatus\r | |
273 | )\r | |
274 | {\r | |
275 | //\r | |
276 | // return the bit 0 value of PortStatus\r | |
277 | //\r | |
278 | if ((PortStatus & USB_PORT_STAT_CONNECTION) != 0) {\r | |
279 | return TRUE;\r | |
280 | } else {\r | |
281 | return FALSE;\r | |
282 | }\r | |
283 | }\r | |
284 | \r | |
285 | /**\r | |
286 | Judge if the port is connected with a low-speed usb device or not.\r | |
287 | \r | |
288 | @param PortStatus The usb port status gotten.\r | |
289 | \r | |
290 | @retval TRUE A low-speed usb device is connected with the port.\r | |
291 | @retval FALSE No low-speed usb device is connected with the port.\r | |
292 | \r | |
293 | **/\r | |
294 | BOOLEAN\r | |
295 | IsPortLowSpeedDeviceAttached (\r | |
296 | IN UINT16 PortStatus\r | |
297 | )\r | |
298 | {\r | |
299 | //\r | |
300 | // return the bit 9 value of PortStatus\r | |
301 | //\r | |
302 | if ((PortStatus & USB_PORT_STAT_LOW_SPEED) != 0) {\r | |
303 | return TRUE;\r | |
304 | } else {\r | |
305 | return FALSE;\r | |
306 | }\r | |
307 | }\r | |
308 | \r | |
309 | /**\r | |
310 | Judge if the port is in "connection change" status or not.\r | |
311 | \r | |
312 | @param PortChangeStatus The usb port change status gotten.\r | |
313 | \r | |
314 | @retval TRUE The port is in "connection change" status.\r | |
315 | @retval FALSE The port is NOT in "connection change" status.\r | |
316 | \r | |
317 | **/\r | |
318 | BOOLEAN\r | |
319 | IsPortConnectChange (\r | |
320 | IN UINT16 PortChangeStatus\r | |
321 | )\r | |
322 | {\r | |
323 | //\r | |
324 | // return the bit 0 value of PortChangeStatus\r | |
325 | //\r | |
326 | if ((PortChangeStatus & USB_PORT_STAT_C_CONNECTION) != 0) {\r | |
327 | return TRUE;\r | |
328 | } else {\r | |
329 | return FALSE;\r | |
330 | }\r | |
331 | }\r |