Add Internal.h to UefiUsbLib
[mirror_edk2.git] / MdePkg / Library / UefiUsbLib / UsbDxeLib.c
CommitLineData
ed838d0c 1/** @file\r
7bc232b2 2\r
ed838d0c 3Copyright (c) 2004 - 2007, Intel Corporation\r
4All rights reserved. This program and the accompanying materials\r
5are licensed and made available under the terms and conditions of the BSD License\r
6which accompanies this distribution. The full text of the license may be found at\r
7http://opensource.org/licenses/bsd-license.php\r
8\r
9THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
10WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
7bc232b2 11\r
7bc232b2
LG
12 Common Dxe Libarary for USB\r
13\r
ed838d0c 14**/\r
7bc232b2 15\r
dad203ec
A
16#include <UefiUsbLibInternal.h>\r
17\r
ed838d0c 18\r
19/**\r
20 Usb Get Descriptor\r
21\r
22 @param UsbIo EFI_USB_IO_PROTOCOL\r
23 @param Value Device Request Value\r
24 @param Index Device Request Index\r
25 @param DescriptorLength Descriptor Length\r
26 @param Descriptor Descriptor buffer to contain result\r
27 @param Status Transfer Status\r
28\r
29 @retval EFI_INVALID_PARAMETER Parameter is error\r
30 @retval EFI_SUCCESS Success\r
31 @retval EFI_TIMEOUT Device has no response\r
32\r
33**/\r
7bc232b2
LG
34EFI_STATUS\r
35UsbGetDescriptor (\r
36 IN EFI_USB_IO_PROTOCOL *UsbIo,\r
37 IN UINT16 Value,\r
38 IN UINT16 Index,\r
39 IN UINT16 DescriptorLength,\r
40 OUT VOID *Descriptor,\r
41 OUT UINT32 *Status\r
42 )\r
7bc232b2
LG
43{\r
44 EFI_USB_DEVICE_REQUEST DevReq;\r
45\r
46 if (UsbIo == NULL) {\r
47 return EFI_INVALID_PARAMETER;\r
48 }\r
49\r
50 ZeroMem (&DevReq, sizeof (EFI_USB_DEVICE_REQUEST));\r
51\r
52 DevReq.RequestType = USB_DEV_GET_DESCRIPTOR_REQ_TYPE;\r
ed838d0c 53 DevReq.Request = USB_REQ_GET_DESCRIPTOR;\r
7bc232b2
LG
54 DevReq.Value = Value;\r
55 DevReq.Index = Index;\r
56 DevReq.Length = DescriptorLength;\r
57\r
58 return UsbIo->UsbControlTransfer (\r
59 UsbIo,\r
60 &DevReq,\r
61 EfiUsbDataIn,\r
62 TIMEOUT_VALUE,\r
63 Descriptor,\r
64 DescriptorLength,\r
65 Status\r
66 );\r
67}\r
ed838d0c 68\r
69\r
70/**\r
71 Usb Set Descriptor\r
72\r
73 @param UsbIo EFI_USB_IO_PROTOCOL\r
74 @param Value Device Request Value\r
75 @param Index Device Request Index\r
76 @param DescriptorLength Descriptor Length\r
77 @param Descriptor Descriptor buffer to set\r
78 @param Status Transfer Status\r
79\r
80 @retval EFI_INVALID_PARAMETER Parameter is error\r
81 @retval EFI_SUCCESS Success\r
82 @retval EFI_TIMEOUT Device has no response\r
83\r
84**/\r
7bc232b2
LG
85EFI_STATUS\r
86UsbSetDescriptor (\r
87 IN EFI_USB_IO_PROTOCOL *UsbIo,\r
88 IN UINT16 Value,\r
89 IN UINT16 Index,\r
90 IN UINT16 DescriptorLength,\r
91 IN VOID *Descriptor,\r
92 OUT UINT32 *Status\r
93 )\r
7bc232b2
LG
94{\r
95 EFI_USB_DEVICE_REQUEST DevReq;\r
96\r
97 if (UsbIo == NULL) {\r
98 return EFI_INVALID_PARAMETER;\r
99 }\r
100\r
101 ZeroMem (&DevReq, sizeof (EFI_USB_DEVICE_REQUEST));\r
102\r
103 DevReq.RequestType = USB_DEV_SET_DESCRIPTOR_REQ_TYPE;\r
ed838d0c 104 DevReq.Request = USB_REQ_SET_DESCRIPTOR;\r
7bc232b2
LG
105 DevReq.Value = Value;\r
106 DevReq.Index = Index;\r
107 DevReq.Length = DescriptorLength;\r
108\r
109 return UsbIo->UsbControlTransfer (\r
110 UsbIo,\r
111 &DevReq,\r
112 EfiUsbDataOut,\r
113 TIMEOUT_VALUE,\r
114 Descriptor,\r
115 DescriptorLength,\r
116 Status\r
117 );\r
118}\r
119\r
ed838d0c 120\r
121/**\r
122 Usb Get Device Interface\r
123\r
124 @param UsbIo EFI_USB_IO_PROTOCOL\r
125 @param Index Interface index value\r
126 @param AltSetting Alternate setting\r
127 @param Status Trasnsfer status\r
128\r
129 @retval EFI_INVALID_PARAMETER Parameter is error\r
130 @retval EFI_SUCCESS Success\r
131 @retval EFI_TIMEOUT Device has no response\r
132\r
133**/\r
7bc232b2 134EFI_STATUS\r
ed838d0c 135UsbGetInterface (\r
7bc232b2
LG
136 IN EFI_USB_IO_PROTOCOL *UsbIo,\r
137 IN UINT16 Index,\r
138 OUT UINT8 *AltSetting,\r
139 OUT UINT32 *Status\r
140 )\r
7bc232b2
LG
141{\r
142 EFI_USB_DEVICE_REQUEST DevReq;\r
143\r
144 if (UsbIo == NULL) {\r
145 return EFI_INVALID_PARAMETER;\r
146 }\r
147\r
148 ZeroMem (&DevReq, sizeof (EFI_USB_DEVICE_REQUEST));\r
149\r
150 DevReq.RequestType = USB_DEV_GET_INTERFACE_REQ_TYPE;\r
ed838d0c 151 DevReq.Request = USB_REQ_GET_INTERFACE;\r
7bc232b2
LG
152 DevReq.Index = Index;\r
153 DevReq.Length = 1;\r
154\r
155 return UsbIo->UsbControlTransfer (\r
156 UsbIo,\r
157 &DevReq,\r
158 EfiUsbDataIn,\r
159 TIMEOUT_VALUE,\r
160 AltSetting,\r
161 1,\r
162 Status\r
163 );\r
164}\r
7bc232b2 165\r
7bc232b2 166\r
ed838d0c 167/**\r
7bc232b2
LG
168 Usb Set Device Interface\r
169\r
ed838d0c 170 @param UsbIo EFI_USB_IO_PROTOCOL\r
171 @param InterfaceNo Interface Number\r
172 @param AltSetting Alternate setting\r
173 @param Status Trasnsfer status\r
7bc232b2 174\r
ed838d0c 175 @retval EFI_INVALID_PARAMETER Parameter is error\r
176 @retval EFI_SUCCESS Success\r
177 @retval EFI_TIMEOUT Device has no response\r
7bc232b2 178\r
ed838d0c 179**/\r
180EFI_STATUS\r
181UsbSetInterface (\r
182 IN EFI_USB_IO_PROTOCOL *UsbIo,\r
183 IN UINT16 InterfaceNo,\r
184 IN UINT16 AltSetting,\r
185 OUT UINT32 *Status\r
186 )\r
7bc232b2
LG
187{\r
188 EFI_USB_DEVICE_REQUEST DevReq;\r
189\r
190 if (UsbIo == NULL) {\r
191 return EFI_INVALID_PARAMETER;\r
192 }\r
193\r
194 ZeroMem (&DevReq, sizeof (EFI_USB_DEVICE_REQUEST));\r
195\r
196 DevReq.RequestType = USB_DEV_SET_INTERFACE_REQ_TYPE;\r
ed838d0c 197 DevReq.Request = USB_REQ_SET_INTERFACE;\r
7bc232b2
LG
198 DevReq.Value = AltSetting;\r
199 DevReq.Index = InterfaceNo;\r
ed838d0c 200\r
7bc232b2
LG
201\r
202 return UsbIo->UsbControlTransfer (\r
203 UsbIo,\r
204 &DevReq,\r
205 EfiUsbNoData,\r
206 TIMEOUT_VALUE,\r
207 NULL,\r
208 0,\r
209 Status\r
210 );\r
211}\r
7bc232b2 212\r
7bc232b2 213\r
ed838d0c 214/**\r
7bc232b2
LG
215 Usb Get Device Configuration\r
216\r
ed838d0c 217 @param UsbIo EFI_USB_IO_PROTOCOL\r
218 @param ConfigValue Config Value\r
219 @param Status Transfer Status\r
7bc232b2 220\r
ed838d0c 221 @retval EFI_INVALID_PARAMETER Parameter is error\r
222 @retval EFI_SUCCESS Success\r
223 @retval EFI_TIMEOUT Device has no response\r
7bc232b2 224\r
ed838d0c 225**/\r
226EFI_STATUS\r
227UsbGetConfiguration (\r
228 IN EFI_USB_IO_PROTOCOL *UsbIo,\r
229 OUT UINT8 *ConfigValue,\r
230 OUT UINT32 *Status\r
231 )\r
7bc232b2
LG
232{\r
233 EFI_USB_DEVICE_REQUEST DevReq;\r
234\r
235 if (UsbIo == NULL) {\r
236 return EFI_INVALID_PARAMETER;\r
237 }\r
238\r
239 ZeroMem (&DevReq, sizeof (EFI_USB_DEVICE_REQUEST));\r
240\r
241 DevReq.RequestType = USB_DEV_GET_CONFIGURATION_REQ_TYPE;\r
ed838d0c 242 DevReq.Request = USB_REQ_GET_CONFIG;\r
7bc232b2
LG
243 DevReq.Length = 1;\r
244\r
245 return UsbIo->UsbControlTransfer (\r
246 UsbIo,\r
247 &DevReq,\r
248 EfiUsbDataIn,\r
249 TIMEOUT_VALUE,\r
250 ConfigValue,\r
251 1,\r
252 Status\r
253 );\r
254}\r
7bc232b2 255\r
7bc232b2 256\r
ed838d0c 257/**\r
7bc232b2
LG
258 Usb Set Device Configuration\r
259\r
ed838d0c 260 @param UsbIo EFI_USB_IO_PROTOCOL\r
261 @param Value Configuration Value to set\r
262 @param Status Transfer status\r
7bc232b2 263\r
ed838d0c 264 @retval EFI_INVALID_PARAMETER Parameter is error\r
265 @retval EFI_SUCCESS Success\r
266 @retval EFI_TIMEOUT Device has no response\r
7bc232b2 267\r
ed838d0c 268**/\r
269EFI_STATUS\r
270UsbSetConfiguration (\r
271 IN EFI_USB_IO_PROTOCOL *UsbIo,\r
272 IN UINT16 Value,\r
273 OUT UINT32 *Status\r
274 )\r
7bc232b2
LG
275{\r
276 EFI_USB_DEVICE_REQUEST DevReq;\r
277\r
278 if (UsbIo == NULL) {\r
279 return EFI_INVALID_PARAMETER;\r
280 }\r
281\r
282 ZeroMem (&DevReq, sizeof (EFI_USB_DEVICE_REQUEST));\r
283\r
284 DevReq.RequestType = USB_DEV_SET_CONFIGURATION_REQ_TYPE;\r
ed838d0c 285 DevReq.Request = USB_REQ_SET_CONFIG;\r
7bc232b2 286 DevReq.Value = Value;\r
ed838d0c 287\r
7bc232b2
LG
288 return UsbIo->UsbControlTransfer (\r
289 UsbIo,\r
290 &DevReq,\r
291 EfiUsbNoData,\r
292 TIMEOUT_VALUE,\r
293 NULL,\r
294 0,\r
295 Status\r
296 );\r
297}\r
7bc232b2 298\r
7bc232b2 299\r
ed838d0c 300/**\r
7bc232b2
LG
301 Usb Set Device Feature\r
302\r
ed838d0c 303 @param UsbIo EFI_USB_IO_PROTOCOL\r
304 @param Recipient Interface/Device/Endpoint\r
305 @param Value Request value\r
306 @param Target Request Index\r
307 @param Status Transfer status\r
7bc232b2 308\r
ed838d0c 309 @retval EFI_INVALID_PARAMETER Parameter is error\r
310 @retval EFI_SUCCESS Success\r
311 @retval EFI_TIMEOUT Device has no response\r
7bc232b2 312\r
ed838d0c 313**/\r
314EFI_STATUS\r
315UsbSetFeature (\r
316 IN EFI_USB_IO_PROTOCOL *UsbIo,\r
317 IN UINTN Recipient,\r
318 IN UINT16 Value,\r
319 IN UINT16 Target,\r
320 OUT UINT32 *Status\r
321 )\r
7bc232b2
LG
322{\r
323 EFI_USB_DEVICE_REQUEST DevReq;\r
324\r
325 if (UsbIo == NULL) {\r
326 return EFI_INVALID_PARAMETER;\r
327 }\r
328\r
329 ZeroMem (&DevReq, sizeof (EFI_USB_DEVICE_REQUEST));\r
330\r
331 switch (Recipient) {\r
332\r
ed838d0c 333 case USB_TARGET_DEVICE:\r
334 DevReq.RequestType = USB_DEV_SET_FEATURE_REQ_TYPE_D;\r
7bc232b2
LG
335 break;\r
336\r
ed838d0c 337 case USB_TARGET_INTERFACE:\r
338 DevReq.RequestType = USB_DEV_SET_FEATURE_REQ_TYPE_I;\r
7bc232b2
LG
339 break;\r
340\r
ed838d0c 341 case USB_TARGET_ENDPOINT:\r
342 DevReq.RequestType = USB_DEV_SET_FEATURE_REQ_TYPE_E;\r
7bc232b2
LG
343 break;\r
344 }\r
345 //\r
346 // Fill device request, see USB1.1 spec\r
347 //\r
ed838d0c 348 DevReq.Request = USB_REQ_SET_FEATURE;\r
7bc232b2
LG
349 DevReq.Value = Value;\r
350 DevReq.Index = Target;\r
351\r
352\r
353 return UsbIo->UsbControlTransfer (\r
354 UsbIo,\r
355 &DevReq,\r
356 EfiUsbNoData,\r
357 TIMEOUT_VALUE,\r
358 NULL,\r
359 0,\r
360 Status\r
361 );\r
362}\r
7bc232b2 363\r
7bc232b2 364\r
ed838d0c 365/**\r
7bc232b2
LG
366 Usb Clear Device Feature\r
367\r
ed838d0c 368 @param UsbIo EFI_USB_IO_PROTOCOL\r
369 @param Recipient Interface/Device/Endpoint\r
370 @param Value Request value\r
371 @param Target Request Index\r
372 @param Status Transfer status\r
7bc232b2 373\r
ed838d0c 374 @retval EFI_INVALID_PARAMETER Parameter is error\r
375 @retval EFI_SUCCESS Success\r
376 @retval EFI_TIMEOUT Device has no response\r
7bc232b2 377\r
ed838d0c 378**/\r
379EFI_STATUS\r
380UsbClearFeature (\r
381 IN EFI_USB_IO_PROTOCOL *UsbIo,\r
382 IN UINTN Recipient,\r
383 IN UINT16 Value,\r
384 IN UINT16 Target,\r
385 OUT UINT32 *Status\r
386 )\r
7bc232b2
LG
387{\r
388 EFI_USB_DEVICE_REQUEST DevReq;\r
389\r
390 if (UsbIo == NULL) {\r
391 return EFI_INVALID_PARAMETER;\r
392 }\r
393\r
394 ZeroMem (&DevReq, sizeof (EFI_USB_DEVICE_REQUEST));\r
395\r
396 switch (Recipient) {\r
397\r
ed838d0c 398 case USB_TARGET_DEVICE:\r
399 DevReq.RequestType = USB_DEV_CLEAR_FEATURE_REQ_TYPE_D;\r
7bc232b2
LG
400 break;\r
401\r
ed838d0c 402 case USB_TARGET_INTERFACE:\r
403 DevReq.RequestType = USB_DEV_CLEAR_FEATURE_REQ_TYPE_I;\r
7bc232b2
LG
404 break;\r
405\r
ed838d0c 406 case USB_TARGET_ENDPOINT:\r
407 DevReq.RequestType = USB_DEV_CLEAR_FEATURE_REQ_TYPE_E;\r
7bc232b2
LG
408 break;\r
409 }\r
410 //\r
411 // Fill device request, see USB1.1 spec\r
412 //\r
ed838d0c 413 DevReq.Request = USB_REQ_CLEAR_FEATURE;\r
7bc232b2
LG
414 DevReq.Value = Value;\r
415 DevReq.Index = Target;\r
416\r
417\r
418 return UsbIo->UsbControlTransfer (\r
419 UsbIo,\r
420 &DevReq,\r
421 EfiUsbNoData,\r
422 TIMEOUT_VALUE,\r
423 NULL,\r
424 0,\r
425 Status\r
426 );\r
427}\r
7bc232b2 428\r
7bc232b2 429\r
ed838d0c 430/**\r
7bc232b2
LG
431 Usb Get Device Status\r
432\r
ed838d0c 433 @param UsbIo EFI_USB_IO_PROTOCOL\r
434 @param Recipient Interface/Device/Endpoint\r
435 @param Target Request index\r
436 @param DevStatus Device status\r
437 @param Status Transfer status\r
7bc232b2 438\r
ed838d0c 439 @retval EFI_INVALID_PARAMETER Parameter is error\r
440 @retval EFI_SUCCESS Success\r
441 @retval EFI_TIMEOUT Device has no response\r
7bc232b2 442\r
ed838d0c 443**/\r
444EFI_STATUS\r
445UsbGetStatus (\r
446 IN EFI_USB_IO_PROTOCOL *UsbIo,\r
447 IN UINTN Recipient,\r
448 IN UINT16 Target,\r
449 OUT UINT16 *DevStatus,\r
450 OUT UINT32 *Status\r
451 )\r
7bc232b2
LG
452{\r
453 EFI_USB_DEVICE_REQUEST DevReq;\r
454\r
455 if (UsbIo == NULL) {\r
456 return EFI_INVALID_PARAMETER;\r
457 }\r
458\r
459 ZeroMem (&DevReq, sizeof (EFI_USB_DEVICE_REQUEST));\r
460\r
461 switch (Recipient) {\r
462\r
ed838d0c 463 case USB_TARGET_DEVICE:\r
464 DevReq.RequestType = USB_DEV_GET_STATUS_REQ_TYPE_D;\r
7bc232b2
LG
465 break;\r
466\r
ed838d0c 467 case USB_TARGET_INTERFACE:\r
468 DevReq.RequestType = USB_DEV_GET_STATUS_REQ_TYPE_I;\r
7bc232b2
LG
469 break;\r
470\r
ed838d0c 471 case USB_TARGET_ENDPOINT:\r
472 DevReq.RequestType = USB_DEV_GET_STATUS_REQ_TYPE_E;\r
7bc232b2
LG
473 break;\r
474 }\r
475 //\r
476 // Fill device request, see USB1.1 spec\r
477 //\r
ed838d0c 478 DevReq.Request = USB_REQ_GET_STATUS;\r
7bc232b2
LG
479 DevReq.Value = 0;\r
480 DevReq.Index = Target;\r
481 DevReq.Length = 2;\r
482\r
483 return UsbIo->UsbControlTransfer (\r
484 UsbIo,\r
485 &DevReq,\r
486 EfiUsbDataIn,\r
487 TIMEOUT_VALUE,\r
488 DevStatus,\r
489 2,\r
490 Status\r
491 );\r
492}\r
7bc232b2 493\r
7bc232b2 494\r
7bc232b2 495\r
ed838d0c 496/**\r
497 Clear endpoint stall\r
7bc232b2 498\r
ed838d0c 499 @param UsbIo EFI_USB_IO_PROTOCOL\r
500 @param EndpointNo Endpoint Number\r
501 @param Status Transfer Status\r
7bc232b2 502\r
ed838d0c 503 @retval EFI_NOT_FOUND Can't find the Endpoint\r
504 @retval EFI_DEVICE_ERROR Hardware error\r
505 @retval EFI_SUCCESS Success\r
7bc232b2 506\r
ed838d0c 507**/\r
7bc232b2
LG
508EFI_STATUS\r
509UsbClearEndpointHalt (\r
510 IN EFI_USB_IO_PROTOCOL *UsbIo,\r
511 IN UINT8 EndpointNo,\r
512 OUT UINT32 *Status\r
513 )\r
7bc232b2
LG
514{\r
515 EFI_STATUS Result;\r
516 EFI_USB_ENDPOINT_DESCRIPTOR EndpointDescriptor;\r
517 EFI_USB_INTERFACE_DESCRIPTOR InterfaceDescriptor;\r
518 UINT8 Index;\r
519\r
520 ZeroMem (&EndpointDescriptor, sizeof (EFI_USB_ENDPOINT_DESCRIPTOR));\r
521 //\r
522 // First seach the endpoint descriptor for that endpoint addr\r
523 //\r
524 Result = UsbIo->UsbGetInterfaceDescriptor (\r
525 UsbIo,\r
526 &InterfaceDescriptor\r
527 );\r
528 if (EFI_ERROR (Result)) {\r
529 return Result;\r
530 }\r
531\r
532 for (Index = 0; Index < InterfaceDescriptor.NumEndpoints; Index++) {\r
533 Result = UsbIo->UsbGetEndpointDescriptor (\r
534 UsbIo,\r
535 Index,\r
536 &EndpointDescriptor\r
537 );\r
538 if (EFI_ERROR (Result)) {\r
539 continue;\r
540 }\r
541\r
542 if (EndpointDescriptor.EndpointAddress == EndpointNo) {\r
543 break;\r
544 }\r
545 }\r
546\r
547 if (Index == InterfaceDescriptor.NumEndpoints) {\r
548 //\r
549 // No such endpoint\r
550 //\r
551 return EFI_NOT_FOUND;\r
552 }\r
553\r
ed838d0c 554 Result = UsbClearFeature (\r
7bc232b2 555 UsbIo,\r
ed838d0c 556 USB_TARGET_ENDPOINT,\r
7bc232b2
LG
557 EfiUsbEndpointHalt,\r
558 EndpointDescriptor.EndpointAddress,\r
559 Status\r
560 );\r
561\r
562 return Result;\r
563}\r