Comment out unused ECP library which no any module referenced,
[mirror_edk2.git] / MdeModulePkg / Bus / Usb / UsbBusDxe / usbbus.c
CommitLineData
e237e7ae 1/** @file\r
2\r
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
11\r
12 Module Name:\r
13\r
14 UsbBus.c\r
15\r
16 Abstract:\r
17\r
18 Usb Bus Driver Binding and Bus IO Protocol\r
19\r
20 Revision History\r
21\r
22\r
23**/\r
24\r
25#include "UsbBus.h"\r
26\r
27//\r
28// USB_BUS_PROTOCOL is only used to locate USB_BUS\r
29//\r
30EFI_GUID mUsbBusProtocolGuid = EFI_USB_BUS_PROTOCOL_GUID;\r
31\r
32\r
33/**\r
34 USB_IO function to execute a control transfer. This\r
35 function will execute the USB transfer. If transfer\r
36 successes, it will sync the internal state of USB bus\r
37 with device state.\r
38\r
39 @param This The USB_IO instance\r
40 @param Request The control transfer request\r
41 @param Direction Direction for data stage\r
42 @param Timeout The time to wait before timeout\r
43 @param Data The buffer holding the data\r
44 @param DataLength Then length of the data\r
45 @param UsbStatus USB result\r
46\r
47 @retval EFI_INVALID_PARAMETER The parameters are invalid\r
48 @retval EFI_SUCCESS The control transfer succeded.\r
49 @retval Others Failed to execute the transfer\r
50\r
51**/\r
52STATIC\r
53EFI_STATUS\r
54EFIAPI\r
55UsbIoControlTransfer (\r
56 IN EFI_USB_IO_PROTOCOL *This,\r
57 IN EFI_USB_DEVICE_REQUEST *Request,\r
58 IN EFI_USB_DATA_DIRECTION Direction,\r
59 IN UINT32 Timeout,\r
60 IN OUT VOID *Data, OPTIONAL\r
61 IN UINTN DataLength, OPTIONAL\r
62 OUT UINT32 *UsbStatus\r
63 )\r
64{\r
65 USB_DEVICE *Dev;\r
66 USB_INTERFACE *UsbIf;\r
67 USB_ENDPOINT_DESC *EpDesc;\r
68 EFI_TPL OldTpl;\r
69 EFI_STATUS Status;\r
70\r
71 if (UsbStatus == NULL) {\r
72 return EFI_INVALID_PARAMETER;\r
73 }\r
74\r
75 OldTpl = gBS->RaiseTPL (USB_BUS_TPL);\r
76\r
77 UsbIf = USB_INTERFACE_FROM_USBIO (This);\r
78 Dev = UsbIf->Device;\r
79\r
80 Status = UsbHcControlTransfer (\r
81 Dev->Bus,\r
82 Dev->Address,\r
83 Dev->Speed,\r
84 Dev->MaxPacket0,\r
85 Request,\r
86 Direction,\r
87 Data,\r
88 &DataLength,\r
89 (UINTN) Timeout,\r
90 &Dev->Translator,\r
91 UsbStatus\r
92 );\r
93\r
94 if (EFI_ERROR (Status) || (*UsbStatus != EFI_USB_NOERROR)) {\r
95 //\r
96 // Clear TT buffer when CTRL/BULK split transaction failes\r
97 // Clear the TRANSLATOR TT buffer, not parent's buffer\r
98 //\r
99 if (Dev->Translator.TranslatorHubAddress != 0) {\r
100 UsbHubCtrlClearTTBuffer (\r
101 Dev->Bus->Devices[Dev->Translator.TranslatorHubAddress],\r
102 Dev->Translator.TranslatorPortNumber,\r
103 Dev->Address,\r
104 0,\r
105 USB_ENDPOINT_CONTROL\r
106 );\r
107 }\r
108\r
109 goto ON_EXIT;\r
110 }\r
111\r
112 //\r
113 // Some control transfer will change the device's internal\r
114 // status, such as Set_Configuration and Set_Interface.\r
115 // We must synchronize the bus driver's status with that in\r
116 // device. We ignore the Set_Descriptor request because it's\r
117 // hardly used by any device, especially in pre-boot environment\r
118 //\r
119\r
120 //\r
121 // Reset the endpoint toggle when endpoint stall is cleared\r
122 //\r
123 if ((Request->Request == USB_REQ_CLEAR_FEATURE) &&\r
124 (Request->RequestType == USB_REQUEST_TYPE (EfiUsbNoData, USB_REQ_TYPE_STANDARD,\r
125 USB_TARGET_ENDPOINT)) &&\r
126 (Request->Value == USB_FEATURE_ENDPOINT_HALT)) {\r
127\r
128 EpDesc = UsbGetEndpointDesc (UsbIf, (UINT8) Request->Index);\r
129\r
130 if (EpDesc != NULL) {\r
131 EpDesc->Toggle = 0;\r
132 }\r
133 }\r
134\r
135 //\r
136 // Select a new configuration. This is a dangerous action. Upper driver\r
137 // should stop use its current UsbIo after calling this driver. The old\r
138 // UsbIo will be uninstalled and new UsbIo be installed. We can't use\r
139 // ReinstallProtocol since interfaces in different configuration may be\r
140 // completely irrellvant.\r
141 //\r
142 if ((Request->Request == USB_REQ_SET_CONFIG) &&\r
143 (Request->RequestType == USB_REQUEST_TYPE (EfiUsbNoData, USB_REQ_TYPE_STANDARD,\r
144 USB_TARGET_DEVICE))) {\r
145 //\r
146 // Don't re-create the USB interfaces if configuration isn't changed.\r
147 //\r
148 if ((Dev->ActiveConfig != NULL) &&\r
149 (Request->Value == Dev->ActiveConfig->Desc.ConfigurationValue)) {\r
150\r
151 goto ON_EXIT;\r
152 }\r
d2577026 153 DEBUG ((EFI_D_INFO, "UsbIoControlTransfer: configure changed!!! Do NOT use old UsbIo!!!\n"));\r
e237e7ae 154\r
155 if (Dev->ActiveConfig != NULL) {\r
156 UsbRemoveConfig (Dev);\r
157 }\r
158\r
159 if (Request->Value != 0) {\r
160 Status = UsbSelectConfig (Dev, (UINT8) Request->Value);\r
161 }\r
162\r
163 //\r
164 // Exit now, Old USB_IO is invalid now\r
165 //\r
166 goto ON_EXIT;\r
167 }\r
168\r
169 //\r
170 // A new alternative setting is selected for the interface.\r
171 // No need to reinstall UsbIo in this case because only\r
172 // underlying communication endpoints are changed. Functionality\r
173 // should remains the same.\r
174 //\r
175 if ((Request->Request == USB_REQ_SET_INTERFACE) &&\r
176 (Request->RequestType == USB_REQUEST_TYPE (EfiUsbNoData, USB_REQ_TYPE_STANDARD,\r
177 USB_TARGET_INTERFACE)) &&\r
178 (Request->Index == UsbIf->IfSetting->Desc.InterfaceNumber)) {\r
179\r
180 Status = UsbSelectSetting (UsbIf->IfDesc, (UINT8) Request->Value);\r
181\r
182 if (!EFI_ERROR (Status)) {\r
183 UsbIf->IfSetting = UsbIf->IfDesc->Settings[UsbIf->IfDesc->ActiveIndex];\r
184 }\r
185 }\r
186\r
187ON_EXIT:\r
188 gBS->RestoreTPL (OldTpl);\r
189 return Status;\r
190}\r
191\r
192\r
193/**\r
194 Execute a bulk transfer to the device endpoint\r
195\r
196 @param This The USB IO instance\r
197 @param Endpoint The device endpoint\r
198 @param Data The data to transfer\r
199 @param DataLength The length of the data to transfer\r
200 @param Timeout Time to wait before timeout\r
201 @param UsbStatus The result of USB transfer\r
202\r
203 @retval EFI_SUCCESS The bulk transfer is OK\r
204 @retval EFI_INVALID_PARAMETER Some parameters are invalid\r
205 @retval Others Failed to execute transfer, reason returned in\r
206 UsbStatus\r
207\r
208**/\r
209STATIC\r
210EFI_STATUS\r
211EFIAPI\r
212UsbIoBulkTransfer (\r
213 IN EFI_USB_IO_PROTOCOL *This,\r
214 IN UINT8 Endpoint,\r
215 IN OUT VOID *Data,\r
216 IN OUT UINTN *DataLength,\r
217 IN UINTN Timeout,\r
218 OUT UINT32 *UsbStatus\r
219 )\r
220{\r
221 USB_DEVICE *Dev;\r
222 USB_INTERFACE *UsbIf;\r
223 USB_ENDPOINT_DESC *EpDesc;\r
224 UINT8 BufNum;\r
225 UINT8 Toggle;\r
226 EFI_TPL OldTpl;\r
227 EFI_STATUS Status;\r
228\r
229 if ((USB_ENDPOINT_ADDR (Endpoint) == 0) || (USB_ENDPOINT_ADDR(Endpoint) > 15) ||\r
230 (UsbStatus == NULL)) {\r
231\r
232 return EFI_INVALID_PARAMETER;\r
233 }\r
234\r
235 OldTpl = gBS->RaiseTPL (USB_BUS_TPL);\r
236\r
237 UsbIf = USB_INTERFACE_FROM_USBIO (This);\r
238 Dev = UsbIf->Device;\r
239\r
240 EpDesc = UsbGetEndpointDesc (UsbIf, Endpoint);\r
241\r
242 if ((EpDesc == NULL) || (USB_ENDPOINT_TYPE (&EpDesc->Desc) != USB_ENDPOINT_BULK)) {\r
243 Status = EFI_INVALID_PARAMETER;\r
244 goto ON_EXIT;\r
245 }\r
246\r
247 BufNum = 1;\r
248 Toggle = EpDesc->Toggle;\r
249 Status = UsbHcBulkTransfer (\r
250 Dev->Bus,\r
251 Dev->Address,\r
252 Endpoint,\r
253 Dev->Speed,\r
254 EpDesc->Desc.MaxPacketSize,\r
255 BufNum,\r
256 &Data,\r
257 DataLength,\r
258 &Toggle,\r
259 Timeout,\r
260 &Dev->Translator,\r
261 UsbStatus\r
262 );\r
263\r
264 EpDesc->Toggle = Toggle;\r
265\r
266 if (EFI_ERROR (Status) || (*UsbStatus != EFI_USB_NOERROR)) {\r
267 //\r
268 // Clear TT buffer when CTRL/BULK split transaction failes.\r
269 // Clear the TRANSLATOR TT buffer, not parent's buffer\r
270 //\r
271 if (Dev->Translator.TranslatorHubAddress != 0) {\r
272 UsbHubCtrlClearTTBuffer (\r
273 Dev->Bus->Devices[Dev->Translator.TranslatorHubAddress],\r
274 Dev->Translator.TranslatorPortNumber,\r
275 Dev->Address,\r
276 0,\r
277 USB_ENDPOINT_BULK\r
278 );\r
279 }\r
280 }\r
281\r
282ON_EXIT:\r
283 gBS->RestoreTPL (OldTpl);\r
284 return Status;\r
285}\r
286\r
287\r
288/**\r
289 Execute a synchronous interrupt transfer\r
290\r
291 @param This The USB IO instance\r
292 @param Endpoint The device endpoint\r
293 @param Data The data to transfer\r
294 @param DataLength The length of the data to transfer\r
295 @param Timeout Time to wait before timeout\r
296 @param UsbStatus The result of USB transfer\r
297\r
298 @retval EFI_SUCCESS The synchronous interrupt transfer is OK\r
299 @retval EFI_INVALID_PARAMETER Some parameters are invalid\r
300 @retval Others Failed to execute transfer, reason returned in\r
301 UsbStatus\r
302\r
303**/\r
304STATIC\r
305EFI_STATUS\r
306EFIAPI\r
307UsbIoSyncInterruptTransfer (\r
308 IN EFI_USB_IO_PROTOCOL *This,\r
309 IN UINT8 Endpoint,\r
310 IN OUT VOID *Data,\r
311 IN OUT UINTN *DataLength,\r
312 IN UINTN Timeout,\r
313 OUT UINT32 *UsbStatus\r
314 )\r
315{\r
316 USB_DEVICE *Dev;\r
317 USB_INTERFACE *UsbIf;\r
318 USB_ENDPOINT_DESC *EpDesc;\r
319 EFI_TPL OldTpl;\r
320 UINT8 Toggle;\r
321 EFI_STATUS Status;\r
322\r
323 if ((USB_ENDPOINT_ADDR (Endpoint) == 0) || (USB_ENDPOINT_ADDR(Endpoint) > 15) ||\r
324 (UsbStatus == NULL)) {\r
325\r
326 return EFI_INVALID_PARAMETER;\r
327 }\r
328\r
329 OldTpl = gBS->RaiseTPL (USB_BUS_TPL);\r
330\r
331 UsbIf = USB_INTERFACE_FROM_USBIO (This);\r
332 Dev = UsbIf->Device;\r
333\r
334 EpDesc = UsbGetEndpointDesc (UsbIf, Endpoint);\r
335\r
336 if ((EpDesc == NULL) || (USB_ENDPOINT_TYPE (&EpDesc->Desc) != USB_ENDPOINT_INTERRUPT)) {\r
337 Status = EFI_INVALID_PARAMETER;\r
338 goto ON_EXIT;\r
339 }\r
340\r
341 Toggle = EpDesc->Toggle;\r
342 Status = UsbHcSyncInterruptTransfer (\r
343 Dev->Bus,\r
344 Dev->Address,\r
345 Endpoint,\r
346 Dev->Speed,\r
347 EpDesc->Desc.MaxPacketSize,\r
348 Data,\r
349 DataLength,\r
350 &Toggle,\r
351 Timeout,\r
352 &Dev->Translator,\r
353 UsbStatus\r
354 );\r
355\r
356 EpDesc->Toggle = Toggle;\r
357\r
358ON_EXIT:\r
359 gBS->RestoreTPL (OldTpl);\r
360 return Status;\r
361}\r
362\r
363\r
364/**\r
365 Queue a new asynchronous interrupt transfer, or remove the old\r
366 request if (IsNewTransfer == FALSE)\r
367\r
368 @param This The USB_IO instance\r
369 @param Endpoint The device endpoint\r
370 @param IsNewTransfer Whether this is a new request, if it's old, remove\r
371 the request\r
372 @param PollInterval The interval to poll the transfer result, (in ms)\r
373 @param DataLength The length of perodic data transfer\r
374 @param Callback The function to call periodicaly when transfer is\r
375 ready\r
376 @param Context The context to the callback\r
377\r
378 @retval EFI_SUCCESS New transfer is queued or old request is removed\r
379 @retval EFI_INVALID_PARAMETER Some parameters are invalid\r
380 @retval Others Failed to queue the new request or remove the old\r
381 request\r
382\r
383**/\r
384STATIC\r
385EFI_STATUS\r
386EFIAPI\r
387UsbIoAsyncInterruptTransfer (\r
388 IN EFI_USB_IO_PROTOCOL *This,\r
389 IN UINT8 Endpoint,\r
390 IN BOOLEAN IsNewTransfer,\r
391 IN UINTN PollInterval, OPTIONAL\r
392 IN UINTN DataLength, OPTIONAL\r
393 IN EFI_ASYNC_USB_TRANSFER_CALLBACK Callback, OPTIONAL\r
394 IN VOID *Context OPTIONAL\r
395 )\r
396{\r
397 USB_DEVICE *Dev;\r
398 USB_INTERFACE *UsbIf;\r
399 USB_ENDPOINT_DESC *EpDesc;\r
400 EFI_TPL OldTpl;\r
401 UINT8 Toggle;\r
402 EFI_STATUS Status;\r
403\r
404 if ((USB_ENDPOINT_ADDR (Endpoint) == 0) || (USB_ENDPOINT_ADDR (Endpoint) > 15)) {\r
405 return EFI_INVALID_PARAMETER;\r
406 }\r
407\r
408 OldTpl = gBS->RaiseTPL (USB_BUS_TPL);\r
409 UsbIf = USB_INTERFACE_FROM_USBIO (This);\r
410 Dev = UsbIf->Device;\r
411\r
412 EpDesc = UsbGetEndpointDesc (UsbIf, Endpoint);\r
413\r
414 if ((EpDesc == NULL) || (USB_ENDPOINT_TYPE (&EpDesc->Desc) != USB_ENDPOINT_INTERRUPT)) {\r
415 Status = EFI_INVALID_PARAMETER;\r
416 goto ON_EXIT;\r
417 }\r
418\r
419 Toggle = EpDesc->Toggle;\r
420 Status = UsbHcAsyncInterruptTransfer (\r
421 Dev->Bus,\r
422 Dev->Address,\r
423 Endpoint,\r
424 Dev->Speed,\r
425 EpDesc->Desc.MaxPacketSize,\r
426 IsNewTransfer,\r
427 &Toggle,\r
428 PollInterval,\r
429 DataLength,\r
430 &Dev->Translator,\r
431 Callback,\r
432 Context\r
433 );\r
434\r
435 EpDesc->Toggle = Toggle;\r
436\r
437ON_EXIT:\r
438 gBS->RestoreTPL (OldTpl);\r
439 return Status;\r
440}\r
441\r
442\r
443/**\r
444 Execute a synchronous isochronous transfer\r
445\r
446 @param This The USB IO instance\r
447 @param DeviceEndpoint The device endpoint\r
448 @param Data The data to transfer\r
449 @param DataLength The length of the data to transfer\r
450 @param UsbStatus The result of USB transfer\r
451\r
452 @retval EFI_UNSUPPORTED Currently isochronous transfer isn't supported\r
453\r
454**/\r
455STATIC\r
456EFI_STATUS\r
457EFIAPI\r
458UsbIoIsochronousTransfer (\r
459 IN EFI_USB_IO_PROTOCOL *This,\r
460 IN UINT8 DeviceEndpoint,\r
461 IN OUT VOID *Data,\r
462 IN UINTN DataLength,\r
463 OUT UINT32 *Status\r
464 )\r
465{\r
466 return EFI_UNSUPPORTED;\r
467}\r
468\r
469\r
470/**\r
471 Queue an asynchronous isochronous transfer\r
472\r
473 @param This The USB_IO instance\r
474 @param DeviceEndpoint The device endpoint\r
475 @param DataLength The length of perodic data transfer\r
476 @param IsochronousCallBack The function to call periodicaly when transfer is\r
477 ready\r
478 @param Context The context to the callback\r
479\r
480 @retval EFI_UNSUPPORTED Currently isochronous transfer isn't supported\r
481\r
482**/\r
483STATIC\r
484EFI_STATUS\r
485EFIAPI\r
486UsbIoAsyncIsochronousTransfer (\r
487 IN EFI_USB_IO_PROTOCOL *This,\r
488 IN UINT8 DeviceEndpoint,\r
489 IN OUT VOID *Data,\r
490 IN UINTN DataLength,\r
491 IN EFI_ASYNC_USB_TRANSFER_CALLBACK IsochronousCallBack,\r
492 IN VOID *Context OPTIONAL\r
493 )\r
494{\r
495 return EFI_UNSUPPORTED;\r
496}\r
497\r
498\r
499/**\r
500 Retrieve the device descriptor of the device\r
501\r
502 @param This The USB IO instance\r
503 @param Descriptor The variable to receive the device descriptor\r
504\r
505 @retval EFI_SUCCESS The device descriptor is returned\r
506 @retval EFI_INVALID_PARAMETER The parameter is invalid\r
507\r
508**/\r
509STATIC\r
510EFI_STATUS\r
511EFIAPI\r
512UsbIoGetDeviceDescriptor (\r
513 IN EFI_USB_IO_PROTOCOL *This,\r
514 OUT EFI_USB_DEVICE_DESCRIPTOR *Descriptor\r
515 )\r
516{\r
517 USB_DEVICE *Dev;\r
518 USB_INTERFACE *UsbIf;\r
519 EFI_TPL OldTpl;\r
520\r
521 if (Descriptor == NULL) {\r
522 return EFI_INVALID_PARAMETER;\r
523 }\r
524\r
525 OldTpl = gBS->RaiseTPL (USB_BUS_TPL);\r
526\r
527 UsbIf = USB_INTERFACE_FROM_USBIO (This);\r
528 Dev = UsbIf->Device;\r
529\r
530 CopyMem (Descriptor, &Dev->DevDesc->Desc, sizeof (EFI_USB_DEVICE_DESCRIPTOR));\r
531\r
532 gBS->RestoreTPL (OldTpl);\r
533 return EFI_SUCCESS;\r
534}\r
535\r
536\r
537/**\r
538 Return the configuration descriptor of the current active configuration\r
539\r
540 @param This The USB IO instance\r
541 @param Descriptor The USB configuration descriptor\r
542\r
543 @retval EFI_SUCCESS The active configuration descriptor is returned\r
544 @retval EFI_INVALID_PARAMETER Some parameter is invalid\r
545 @retval EFI_NOT_FOUND Currently no active configuration is selected.\r
546\r
547**/\r
548STATIC\r
549EFI_STATUS\r
550EFIAPI\r
551UsbIoGetActiveConfigDescriptor (\r
552 IN EFI_USB_IO_PROTOCOL *This,\r
553 OUT EFI_USB_CONFIG_DESCRIPTOR *Descriptor\r
554 )\r
555{\r
556 USB_DEVICE *Dev;\r
557 USB_INTERFACE *UsbIf;\r
558 EFI_STATUS Status;\r
559 EFI_TPL OldTpl;\r
560\r
561 if (Descriptor == NULL) {\r
562 return EFI_INVALID_PARAMETER;\r
563 }\r
564\r
565 Status = EFI_SUCCESS;\r
566 OldTpl = gBS->RaiseTPL (USB_BUS_TPL);\r
567\r
568 UsbIf = USB_INTERFACE_FROM_USBIO (This);\r
569 Dev = UsbIf->Device;\r
570\r
571 if (Dev->ActiveConfig == NULL) {\r
572 Status = EFI_NOT_FOUND;\r
573 goto ON_EXIT;\r
574 }\r
575\r
576 CopyMem (Descriptor, &(Dev->ActiveConfig->Desc), sizeof (EFI_USB_CONFIG_DESCRIPTOR));\r
577\r
578ON_EXIT:\r
579 gBS->RestoreTPL (OldTpl);\r
580 return Status;\r
581}\r
582\r
583\r
584/**\r
585 Retrieve the active interface setting descriptor for this USB IO instance\r
586\r
587 @param This The USB IO instance\r
588 @param Descriptor The variable to receive active interface setting\r
589\r
590 @retval EFI_SUCCESS The active interface setting is returned\r
591 @retval EFI_INVALID_PARAMETER Some parameter is invalid\r
592\r
593**/\r
594STATIC\r
595EFI_STATUS\r
596EFIAPI\r
597UsbIoGetInterfaceDescriptor (\r
598 IN EFI_USB_IO_PROTOCOL *This,\r
599 OUT EFI_USB_INTERFACE_DESCRIPTOR *Descriptor\r
600 )\r
601{\r
602 USB_INTERFACE *UsbIf;\r
603 EFI_TPL OldTpl;\r
604\r
605 if (Descriptor == NULL) {\r
606 return EFI_INVALID_PARAMETER;\r
607 }\r
608\r
609 OldTpl = gBS->RaiseTPL (USB_BUS_TPL);\r
610\r
611 UsbIf = USB_INTERFACE_FROM_USBIO (This);\r
612 CopyMem (Descriptor, &(UsbIf->IfSetting->Desc), sizeof (EFI_USB_INTERFACE_DESCRIPTOR));\r
613\r
614 gBS->RestoreTPL (OldTpl);\r
615 return EFI_SUCCESS;\r
616}\r
617\r
618\r
619/**\r
620 Retrieve the endpoint descriptor from this interface setting\r
621\r
622 @param This The USB IO instance\r
623 @param Index The index (start from zero) of the endpoint to\r
624 retrieve\r
625 @param Descriptor The variable to receive the descriptor\r
626\r
627 @retval EFI_SUCCESS The endpoint descriptor is returned\r
628 @retval EFI_INVALID_PARAMETER Some parameter is invalid\r
629\r
630**/\r
631STATIC\r
632EFI_STATUS\r
633EFIAPI\r
634UsbIoGetEndpointDescriptor (\r
635 IN EFI_USB_IO_PROTOCOL *This,\r
636 IN UINT8 Index,\r
637 OUT EFI_USB_ENDPOINT_DESCRIPTOR *Descriptor\r
638 )\r
639{\r
640 USB_INTERFACE *UsbIf;\r
641 EFI_TPL OldTpl;\r
642\r
643 OldTpl = gBS->RaiseTPL (USB_BUS_TPL);\r
644\r
645 UsbIf = USB_INTERFACE_FROM_USBIO (This);\r
646\r
50fa1b3a 647 if ((Descriptor == NULL) || (Index > 15)) {\r
e237e7ae 648 gBS->RestoreTPL (OldTpl);\r
649 return EFI_INVALID_PARAMETER;\r
650 }\r
651\r
50fa1b3a 652 if (Index >= UsbIf->IfSetting->Desc.NumEndpoints) {\r
653 gBS->RestoreTPL (OldTpl);\r
654 return EFI_NOT_FOUND;\r
655 }\r
656\r
e237e7ae 657 CopyMem (\r
658 Descriptor,\r
659 &(UsbIf->IfSetting->Endpoints[Index]->Desc),\r
660 sizeof (EFI_USB_ENDPOINT_DESCRIPTOR)\r
661 );\r
662\r
663 gBS->RestoreTPL (OldTpl);\r
664 return EFI_SUCCESS;\r
665}\r
666\r
667\r
668/**\r
669 Retrieve the supported language ID table from the device\r
670\r
671 @param This The USB IO instance\r
672 @param LangIDTable The table to return the language IDs\r
673 @param TableSize The number of supported languanges\r
674\r
675 @retval EFI_SUCCESS The language ID is return\r
676\r
677**/\r
678STATIC\r
679EFI_STATUS\r
680EFIAPI\r
681UsbIoGetSupportedLanguages (\r
682 IN EFI_USB_IO_PROTOCOL *This,\r
683 OUT UINT16 **LangIDTable,\r
684 OUT UINT16 *TableSize\r
685 )\r
686{\r
687 USB_DEVICE *Dev;\r
688 USB_INTERFACE *UsbIf;\r
689 EFI_TPL OldTpl;\r
690\r
691 OldTpl = gBS->RaiseTPL (USB_BUS_TPL);\r
692\r
693 UsbIf = USB_INTERFACE_FROM_USBIO (This);\r
694 Dev = UsbIf->Device;\r
695\r
696 *LangIDTable = Dev->LangId;\r
697 *TableSize = Dev->TotalLangId;\r
698\r
699 gBS->RestoreTPL (OldTpl);\r
700 return EFI_SUCCESS;\r
701}\r
702\r
703\r
704/**\r
705 Retrieve an indexed string in the language of LangID\r
706\r
707 @param This The USB IO instance\r
708 @param LangID The language ID of the string to retrieve\r
709 @param StringIndex The index of the string\r
710 @param String The variable to receive the string\r
711\r
712 @retval EFI_SUCCESS The string is returned\r
713 @retval EFI_NOT_FOUND No such string existed\r
714\r
715**/\r
716STATIC\r
717EFI_STATUS\r
718EFIAPI\r
719UsbIoGetStringDescriptor (\r
720 IN EFI_USB_IO_PROTOCOL *This,\r
721 IN UINT16 LangID,\r
722 IN UINT8 StringIndex,\r
723 OUT CHAR16 **String\r
724 )\r
725{\r
726 USB_DEVICE *Dev;\r
727 USB_INTERFACE *UsbIf;\r
728 EFI_USB_STRING_DESCRIPTOR *StrDesc;\r
729 EFI_TPL OldTpl;\r
730 UINT8 *Buf;\r
731 UINT8 Index;\r
732 EFI_STATUS Status;\r
733\r
734 if ((StringIndex == 0) || (LangID == 0)) {\r
735 return EFI_NOT_FOUND;\r
736 }\r
737\r
738 OldTpl = gBS->RaiseTPL (USB_BUS_TPL);\r
739\r
740 UsbIf = USB_INTERFACE_FROM_USBIO (This);\r
741 Dev = UsbIf->Device;\r
742\r
743 //\r
744 // Check whether language ID is supported\r
745 //\r
746 Status = EFI_NOT_FOUND;\r
747\r
748 for (Index = 0; Index < Dev->TotalLangId; Index++) {\r
749 if (Dev->LangId[Index] == LangID) {\r
750 break;\r
751 }\r
752 }\r
753\r
754 if (Index == Dev->TotalLangId) {\r
755 goto ON_EXIT;\r
756 }\r
757\r
758 //\r
759 // Retrieve the string descriptor then allocate a buffer\r
760 // to hold the string itself.\r
761 //\r
762 StrDesc = UsbGetOneString (Dev, StringIndex, LangID);\r
763\r
764 if (StrDesc == NULL) {\r
765 goto ON_EXIT;\r
766 }\r
767\r
768 if (StrDesc->Length <= 2) {\r
769 goto FREE_STR;\r
770 }\r
771\r
772 Buf = AllocateZeroPool (StrDesc->Length);\r
773\r
774 if (Buf == NULL) {\r
775 Status = EFI_OUT_OF_RESOURCES;\r
776 goto FREE_STR;\r
777 }\r
778\r
779 CopyMem (Buf, StrDesc->String, StrDesc->Length - 2);\r
780 *String = (CHAR16 *) Buf;\r
781 Status = EFI_SUCCESS;\r
782\r
783FREE_STR:\r
784 gBS->FreePool (StrDesc);\r
785\r
786ON_EXIT:\r
787 gBS->RestoreTPL (OldTpl);\r
788 return Status;\r
789}\r
790\r
791\r
792/**\r
793 Reset the device, then if that succeeds, reconfigure the\r
794 device with its address and current active configuration.\r
795\r
796 @param This The USB IO instance\r
797\r
798 @retval EFI_SUCCESS The device is reset and configured\r
799 @retval Others Failed to reset the device\r
800\r
801**/\r
802EFI_STATUS\r
803EFIAPI\r
804UsbIoPortReset (\r
805 IN EFI_USB_IO_PROTOCOL *This\r
806 )\r
807{\r
808 USB_INTERFACE *UsbIf;\r
809 USB_INTERFACE *HubIf;\r
810 USB_DEVICE *Dev;\r
811 UINT8 Address;\r
812 EFI_TPL OldTpl;\r
813 EFI_STATUS Status;\r
814\r
815 OldTpl = gBS->RaiseTPL (USB_BUS_TPL);\r
816\r
817 UsbIf = USB_INTERFACE_FROM_USBIO (This);\r
818 Dev = UsbIf->Device;\r
819\r
50fa1b3a 820 if (UsbIf->IsHub == TRUE) {\r
821 Status = EFI_INVALID_PARAMETER;\r
822 goto ON_EXIT;\r
823 }\r
824\r
e237e7ae 825 HubIf = Dev->ParentIf;\r
826 Status = HubIf->HubApi->ResetPort (HubIf, Dev->ParentPort);\r
827\r
828 if (EFI_ERROR (Status)) {\r
d2577026 829 DEBUG (( EFI_D_ERROR, "UsbIoPortReset: failed to reset hub port %d@hub %d, %r \n",\r
e237e7ae 830 Dev->ParentPort, Dev->ParentAddr, Status));\r
831\r
832 goto ON_EXIT;\r
833 }\r
834\r
835 //\r
836 // Reset the device to its current address. The device now has a\r
837 // address of ZERO, so need to set Dev->Address to zero first for\r
838 // host to communicate with the device\r
839 //\r
840 Address = Dev->Address;\r
841 Dev->Address = 0;\r
842 Status = UsbSetAddress (Dev, Address);\r
843\r
844 if (EFI_ERROR (Status)) {\r
d2577026 845 DEBUG (( EFI_D_ERROR, "UsbIoPortReset: failed to set address for device %d - %r\n",\r
e237e7ae 846 Address, Status));\r
847\r
848 goto ON_EXIT;\r
849 }\r
850\r
851 Dev->Address = Address;\r
852\r
853 //\r
854 // Reset the current active configure, after this device\r
855 // is in CONFIGURED state.\r
856 //\r
857 if (Dev->ActiveConfig != NULL) {\r
858 Status = UsbSetConfig (Dev, Dev->ActiveConfig->Desc.ConfigurationValue);\r
859\r
860 if (EFI_ERROR (Status)) {\r
d2577026 861 DEBUG (( EFI_D_ERROR, "UsbIoPortReset: failed to set configure for device %d - %r\n",\r
e237e7ae 862 Address, Status));\r
863 }\r
864 }\r
865\r
866ON_EXIT:\r
867 gBS->RestoreTPL (OldTpl);\r
868 return Status;\r
869}\r
870\r
871EFI_USB_IO_PROTOCOL mUsbIoProtocol = {\r
872 UsbIoControlTransfer,\r
873 UsbIoBulkTransfer,\r
874 UsbIoAsyncInterruptTransfer,\r
875 UsbIoSyncInterruptTransfer,\r
876 UsbIoIsochronousTransfer,\r
877 UsbIoAsyncIsochronousTransfer,\r
878 UsbIoGetDeviceDescriptor,\r
879 UsbIoGetActiveConfigDescriptor,\r
880 UsbIoGetInterfaceDescriptor,\r
881 UsbIoGetEndpointDescriptor,\r
882 UsbIoGetStringDescriptor,\r
883 UsbIoGetSupportedLanguages,\r
884 UsbIoPortReset\r
885};\r
886\r
e237e7ae 887\r
888EFI_STATUS\r
889EFIAPI\r
890UsbBusDriverEntryPoint (\r
891 IN EFI_HANDLE ImageHandle,\r
892 IN EFI_SYSTEM_TABLE *SystemTable\r
893 )\r
894/*++\r
895\r
896Routine Description:\r
897\r
898 The USB bus driver entry pointer\r
899\r
900Arguments:\r
901\r
902 ImageHandle - The driver image handle\r
903 SystemTable - The system table\r
904\r
905Returns:\r
906\r
907 EFI_SUCCESS - The component name protocol is installed\r
908 Others - Failed to init the usb driver\r
909\r
910--*/\r
911{\r
62b9bb55 912 return EfiLibInstallDriverBindingComponentName2 (\r
e237e7ae 913 ImageHandle,\r
914 SystemTable,\r
915 &mUsbBusDriverBinding,\r
916 ImageHandle,\r
917 &mUsbBusComponentName,\r
62b9bb55 918 &mUsbBusComponentName2\r
e237e7ae 919 );\r
920}\r
921\r
922\r
923/**\r
924 Check whether USB bus driver support this device\r
925\r
926 @param This The USB bus driver binding protocol\r
927 @param Controller The controller handle to test againist\r
928 @param RemainingDevicePath The remaining device path\r
929\r
930 @retval EFI_SUCCESS The bus supports this controller.\r
931 @retval EFI_UNSUPPORTED This device isn't supported\r
932\r
933**/\r
934EFI_STATUS\r
935EFIAPI\r
936UsbBusControllerDriverSupported (\r
937 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
938 IN EFI_HANDLE Controller,\r
939 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath\r
940 )\r
941{\r
942 EFI_DEV_PATH_PTR DevicePathNode;\r
943 EFI_DEVICE_PATH_PROTOCOL *ParentDevicePath;\r
944 EFI_USB2_HC_PROTOCOL *Usb2Hc;\r
945 EFI_USB_HC_PROTOCOL *UsbHc;\r
946 EFI_STATUS Status;\r
947\r
948 //\r
949 // Check whether device path is valid\r
950 //\r
951 if (RemainingDevicePath != NULL) {\r
952 DevicePathNode.DevPath = RemainingDevicePath;\r
953\r
954 if ((DevicePathNode.DevPath->Type != MESSAGING_DEVICE_PATH) ||\r
955 (DevicePathNode.DevPath->SubType != MSG_USB_DP) ||\r
956 (DevicePathNodeLength (DevicePathNode.DevPath) != sizeof (USB_DEVICE_PATH))) {\r
957\r
958 return EFI_UNSUPPORTED;\r
959 }\r
960 }\r
961\r
962 Status = gBS->OpenProtocol (\r
963 Controller,\r
964 &gEfiDevicePathProtocolGuid,\r
965 (VOID **) &ParentDevicePath,\r
966 This->DriverBindingHandle,\r
967 Controller,\r
968 EFI_OPEN_PROTOCOL_BY_DRIVER\r
969 );\r
970\r
971 if (Status == EFI_ALREADY_STARTED) {\r
972 return EFI_SUCCESS;\r
973 }\r
974\r
975 if (EFI_ERROR (Status)) {\r
976 return Status;\r
977 }\r
978\r
979 gBS->CloseProtocol (\r
980 Controller,\r
981 &gEfiDevicePathProtocolGuid,\r
982 This->DriverBindingHandle,\r
983 Controller\r
984 );\r
985\r
986 //\r
987 // Check whether USB_HC2 protocol is installed\r
988 //\r
989 Status = gBS->OpenProtocol (\r
990 Controller,\r
991 &gEfiUsb2HcProtocolGuid,\r
992 (VOID **) &Usb2Hc,\r
993 This->DriverBindingHandle,\r
994 Controller,\r
995 EFI_OPEN_PROTOCOL_BY_DRIVER\r
996 );\r
997\r
998 if (Status == EFI_ALREADY_STARTED) {\r
999 return EFI_SUCCESS;\r
1000 }\r
1001\r
1002 if (!EFI_ERROR (Status)) {\r
1003 gBS->CloseProtocol (\r
1004 Controller,\r
1005 &gEfiUsb2HcProtocolGuid,\r
1006 This->DriverBindingHandle,\r
1007 Controller\r
1008 );\r
1009\r
1010 return EFI_SUCCESS;\r
1011 }\r
1012\r
1013 //\r
1014 // If failed to open USB_HC2, fall back to USB_HC\r
1015 //\r
1016 Status = gBS->OpenProtocol (\r
1017 Controller,\r
1018 &gEfiUsbHcProtocolGuid,\r
1019 (VOID **) &UsbHc,\r
1020 This->DriverBindingHandle,\r
1021 Controller,\r
1022 EFI_OPEN_PROTOCOL_BY_DRIVER\r
1023 );\r
1024\r
1025 if (Status == EFI_ALREADY_STARTED) {\r
1026 return EFI_SUCCESS;\r
1027 }\r
1028\r
1029 if (!EFI_ERROR (Status)) {\r
1030 gBS->CloseProtocol (\r
1031 Controller,\r
1032 &gEfiUsbHcProtocolGuid,\r
1033 This->DriverBindingHandle,\r
1034 Controller\r
1035 );\r
1036 }\r
1037\r
1038 return Status;\r
1039}\r
1040\r
1041\r
1042/**\r
1043 Start to process the controller\r
1044\r
1045 @param This The USB bus driver binding instance\r
1046 @param Controller The controller to check\r
1047 @param RemainingDevicePath The remaining device patch\r
1048\r
1049 @retval EFI_SUCCESS The controller is controlled by the usb bus\r
1050 @retval EFI_ALREADY_STARTED The controller is already controlled by the usb\r
1051 bus\r
1052 @retval EFI_OUT_OF_RESOURCES Failed to allocate resources\r
1053\r
1054**/\r
1055EFI_STATUS\r
1056EFIAPI\r
1057UsbBusControllerDriverStart (\r
1058 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
1059 IN EFI_HANDLE Controller,\r
1060 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath\r
1061 )\r
1062{\r
1063 USB_BUS *UsbBus;\r
1064 USB_DEVICE *RootHub;\r
1065 USB_INTERFACE *RootIf;\r
1066 EFI_USB_BUS_PROTOCOL *UsbBusId;\r
1067 EFI_STATUS Status;\r
1068 EFI_STATUS Status2;\r
1069\r
1070 //\r
1071 // Locate the USB bus protocol, if it is found, USB bus\r
1072 // is already started on this controller.\r
1073 //\r
1074 Status = gBS->OpenProtocol (\r
1075 Controller,\r
1076 &mUsbBusProtocolGuid,\r
1077 (VOID **) &UsbBusId,\r
1078 This->DriverBindingHandle,\r
1079 Controller,\r
1080 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
1081 );\r
1082\r
1083 if (!EFI_ERROR (Status)) {\r
1084 return EFI_ALREADY_STARTED;\r
1085 }\r
1086\r
1087 UsbBus = AllocateZeroPool (sizeof (USB_BUS));\r
1088\r
1089 if (UsbBus == NULL) {\r
1090 return EFI_OUT_OF_RESOURCES;\r
1091 }\r
1092\r
1093 UsbBus->Signature = USB_BUS_SIGNATURE;\r
1094 UsbBus->HostHandle = Controller;\r
1095\r
1096 Status = gBS->OpenProtocol (\r
1097 Controller,\r
1098 &gEfiDevicePathProtocolGuid,\r
1099 (VOID **) &UsbBus->DevicePath,\r
1100 This->DriverBindingHandle,\r
1101 Controller,\r
1102 EFI_OPEN_PROTOCOL_BY_DRIVER\r
1103 );\r
1104\r
1105 if (EFI_ERROR (Status)) {\r
d2577026 1106 DEBUG (( EFI_D_ERROR, "UsbBusStart: Failed to open device path %r\n", Status));\r
e237e7ae 1107\r
1108 gBS->FreePool (UsbBus);\r
1109 return Status;\r
1110 }\r
1111\r
1112 //\r
1113 // Get USB_HC2/USB_HC host controller protocol (EHCI/UHCI).\r
1114 // This is for backward compatbility with EFI 1.x. In UEFI\r
1115 // 2.x, USB_HC2 replaces USB_HC. We will open both USB_HC2\r
1116 // and USB_HC because EHCI driver will install both protocols\r
1117 // (for the same reason). If we don't consume both of them,\r
1118 // the unconsumed one may be opened by others.\r
1119 //\r
1120 Status = gBS->OpenProtocol (\r
1121 Controller,\r
1122 &gEfiUsb2HcProtocolGuid,\r
1123 (VOID **) &(UsbBus->Usb2Hc),\r
1124 This->DriverBindingHandle,\r
1125 Controller,\r
1126 EFI_OPEN_PROTOCOL_BY_DRIVER\r
1127 );\r
1128\r
1129 Status2 = gBS->OpenProtocol (\r
1130 Controller,\r
1131 &gEfiUsbHcProtocolGuid,\r
1132 (VOID **) &(UsbBus->UsbHc),\r
1133 This->DriverBindingHandle,\r
1134 Controller,\r
1135 EFI_OPEN_PROTOCOL_BY_DRIVER\r
1136 );\r
1137\r
1138 if (EFI_ERROR (Status) && EFI_ERROR (Status2)) {\r
d2577026 1139 DEBUG (( EFI_D_ERROR, "UsbBusStart: Failed to open USB_HC/USB2_HC %r\n", Status));\r
e237e7ae 1140\r
1141 Status = EFI_DEVICE_ERROR;\r
1142 goto CLOSE_HC;\r
1143 }\r
1144\r
41e8ff27 1145 UsbHcReset (UsbBus, EFI_USB_HC_RESET_GLOBAL);\r
1146 UsbHcSetState (UsbBus, EfiUsbHcStateOperational);\r
1147\r
1148 //\r
1149 // Install an EFI_USB_BUS_PROTOCOL to host controler to identify it.\r
1150 //\r
1151 Status = gBS->InstallProtocolInterface (\r
1152 &Controller,\r
1153 &mUsbBusProtocolGuid,\r
1154 EFI_NATIVE_INTERFACE,\r
1155 &UsbBus->BusId\r
1156 );\r
1157\r
1158 if (EFI_ERROR (Status)) {\r
1159 DEBUG ((EFI_D_ERROR, "UsbBusStart: Failed to install bus protocol %r\n", Status));\r
1160 goto CLOSE_HC;\r
1161 }\r
1162\r
e237e7ae 1163 //\r
1164 // Create a fake usb device for root hub\r
1165 //\r
1166 RootHub = AllocateZeroPool (sizeof (USB_DEVICE));\r
1167\r
1168 if (RootHub == NULL) {\r
1169 Status = EFI_OUT_OF_RESOURCES;\r
41e8ff27 1170 goto UNINSTALL_USBBUS;\r
e237e7ae 1171 }\r
1172\r
1173 RootIf = AllocateZeroPool (sizeof (USB_INTERFACE));\r
1174\r
1175 if (RootIf == NULL) {\r
1176 gBS->FreePool (RootHub);\r
1177 Status = EFI_OUT_OF_RESOURCES;\r
41e8ff27 1178 goto FREE_ROOTHUB;\r
e237e7ae 1179 }\r
1180\r
1181 RootHub->Bus = UsbBus;\r
1182 RootHub->NumOfInterface = 1;\r
1183 RootHub->Interfaces[0] = RootIf;\r
1184 RootIf->Signature = USB_INTERFACE_SIGNATURE;\r
1185 RootIf->Device = RootHub;\r
1186 RootIf->DevicePath = UsbBus->DevicePath;\r
d2577026 1187 \r
e237e7ae 1188 Status = mUsbRootHubApi.Init (RootIf);\r
1189\r
1190 if (EFI_ERROR (Status)) {\r
d2577026 1191 DEBUG (( EFI_D_ERROR, "UsbBusStart: Failed to init root hub %r\n", Status));\r
e237e7ae 1192 goto FREE_ROOTHUB;\r
1193 }\r
1194\r
1195 UsbBus->Devices[0] = RootHub;\r
1196\r
d2577026 1197 DEBUG (( EFI_D_INFO, "UsbBusStart: usb bus started on %x, root hub %x\n", Controller, RootIf));\r
e237e7ae 1198 return EFI_SUCCESS;\r
41e8ff27 1199 \r
e237e7ae 1200FREE_ROOTHUB:\r
41e8ff27 1201 if (RootIf != NULL) {\r
1202 gBS->FreePool (RootIf);\r
1203 }\r
1204 if (RootHub != NULL) {\r
1205 gBS->FreePool (RootHub);\r
1206 }\r
1207 \r
1208UNINSTALL_USBBUS:\r
1209 gBS->UninstallProtocolInterface (Controller, &mUsbBusProtocolGuid, &UsbBus->BusId);\r
1210 \r
e237e7ae 1211CLOSE_HC:\r
1212 if (UsbBus->Usb2Hc != NULL) {\r
1213 gBS->CloseProtocol (\r
1214 Controller,\r
1215 &gEfiUsb2HcProtocolGuid,\r
1216 This->DriverBindingHandle,\r
1217 Controller\r
1218 );\r
1219 }\r
e237e7ae 1220 if (UsbBus->UsbHc != NULL) {\r
1221 gBS->CloseProtocol (\r
1222 Controller,\r
1223 &gEfiUsbHcProtocolGuid,\r
1224 This->DriverBindingHandle,\r
1225 Controller\r
1226 );\r
1227 }\r
e237e7ae 1228 gBS->CloseProtocol (\r
1229 Controller,\r
1230 &gEfiDevicePathProtocolGuid,\r
1231 This->DriverBindingHandle,\r
1232 Controller\r
1233 );\r
e237e7ae 1234 gBS->FreePool (UsbBus);\r
1235\r
d2577026 1236 DEBUG (( EFI_D_ERROR, "UsbBusStart: Failed to start bus driver %r\n", Status));\r
e237e7ae 1237 return Status;\r
1238}\r
1239\r
1240\r
1241/**\r
1242 Stop handle the controller by this USB bus driver\r
1243\r
1244 @param This The USB bus driver binding protocol\r
1245 @param Controller The controller to release\r
1246 @param NumberOfChildren The child of USB bus that opened controller\r
1247 BY_CHILD\r
1248 @param ChildHandleBuffer The array of child handle\r
1249\r
1250 @retval EFI_SUCCESS The controller or children are stopped\r
1251 @retval EFI_DEVICE_ERROR Failed to stop the driver\r
1252\r
1253**/\r
1254EFI_STATUS\r
1255EFIAPI\r
1256UsbBusControllerDriverStop (\r
1257 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
1258 IN EFI_HANDLE Controller,\r
1259 IN UINTN NumberOfChildren,\r
1260 IN EFI_HANDLE *ChildHandleBuffer\r
1261 )\r
1262{\r
1263 USB_BUS *Bus;\r
1264 USB_DEVICE *RootHub;\r
1265 USB_DEVICE *UsbDev;\r
1266 USB_INTERFACE *RootIf;\r
1267 USB_INTERFACE *UsbIf;\r
1268 EFI_USB_BUS_PROTOCOL *BusId;\r
1269 EFI_USB_IO_PROTOCOL *UsbIo;\r
1270 EFI_TPL OldTpl;\r
1271 UINTN Index;\r
1272 EFI_STATUS Status;\r
1273\r
1274 Status = EFI_SUCCESS;\r
1275\r
1276 if (NumberOfChildren > 0) {\r
1277 OldTpl = gBS->RaiseTPL (USB_BUS_TPL);\r
1278\r
1279 for (Index = 0; Index < NumberOfChildren; Index++) {\r
1280 Status = gBS->OpenProtocol (\r
1281 ChildHandleBuffer[Index],\r
1282 &gEfiUsbIoProtocolGuid,\r
1283 (VOID **) &UsbIo,\r
1284 This->DriverBindingHandle,\r
1285 Controller,\r
1286 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
1287 );\r
1288\r
1289 if (EFI_ERROR (Status)) {\r
1290 //\r
1291 // It is possible that the child has already been released:\r
1292 // 1. For combo device, free one device will release others.\r
1293 // 2. If a hub is released, all devices on its down facing\r
1294 // ports are released also.\r
1295 //\r
1296 continue;\r
1297 }\r
1298\r
1299 UsbIf = USB_INTERFACE_FROM_USBIO (UsbIo);\r
1300 UsbDev = UsbIf->Device;\r
1301\r
1302 UsbRemoveDevice (UsbDev);\r
1303 }\r
1304\r
1305 gBS->RestoreTPL (OldTpl);\r
1306 return EFI_SUCCESS;\r
1307 }\r
1308\r
d2577026 1309 DEBUG (( EFI_D_INFO, "UsbBusStop: usb bus stopped on %x\n", Controller));\r
e237e7ae 1310\r
1311 //\r
1312 // Locate USB_BUS for the current host controller\r
1313 //\r
1314 Status = gBS->OpenProtocol (\r
1315 Controller,\r
1316 &mUsbBusProtocolGuid,\r
1317 (VOID **) &BusId,\r
1318 This->DriverBindingHandle,\r
1319 Controller,\r
1320 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
1321 );\r
1322\r
1323 if (EFI_ERROR (Status)) {\r
1324 return Status;\r
1325 }\r
1326\r
1327 Bus = USB_BUS_FROM_THIS (BusId);\r
1328\r
1329 //\r
1330 // Stop the root hub, then free all the devices\r
1331 //\r
1332 OldTpl = gBS->RaiseTPL (USB_BUS_TPL);\r
1333 UsbHcSetState (Bus, EfiUsbHcStateHalt);\r
1334\r
1335 RootHub = Bus->Devices[0];\r
1336 RootIf = RootHub->Interfaces[0];\r
1337\r
1338 mUsbRootHubApi.Release (RootIf);\r
1339\r
1340 for (Index = 1; Index < USB_MAX_DEVICES; Index++) {\r
1341 if (Bus->Devices[Index] != NULL) {\r
1342 UsbRemoveDevice (Bus->Devices[Index]);\r
1343 }\r
1344 }\r
1345\r
1346 gBS->RestoreTPL (OldTpl);\r
1347\r
1348 gBS->FreePool (RootIf);\r
1349 gBS->FreePool (RootHub);\r
1350\r
1351 //\r
1352 // Uninstall the bus identifier and close USB_HC/USB2_HC protocols\r
1353 //\r
1354 gBS->UninstallProtocolInterface (Controller, &mUsbBusProtocolGuid, &Bus->BusId);\r
1355\r
1356 if (Bus->Usb2Hc != NULL) {\r
1357 gBS->CloseProtocol (\r
1358 Controller,\r
1359 &gEfiUsb2HcProtocolGuid,\r
1360 This->DriverBindingHandle,\r
1361 Controller\r
1362 );\r
1363 }\r
1364\r
1365 if (Bus->UsbHc != NULL) {\r
1366 gBS->CloseProtocol (\r
1367 Controller,\r
1368 &gEfiUsbHcProtocolGuid,\r
1369 This->DriverBindingHandle,\r
1370 Controller\r
1371 );\r
1372 }\r
1373\r
1374 gBS->CloseProtocol (\r
1375 Controller,\r
1376 &gEfiDevicePathProtocolGuid,\r
1377 This->DriverBindingHandle,\r
1378 Controller\r
1379 );\r
1380\r
1381 gBS->FreePool (Bus);\r
1382\r
1383 return Status;\r
1384}\r
1385\r
1386EFI_DRIVER_BINDING_PROTOCOL mUsbBusDriverBinding = {\r
1387 UsbBusControllerDriverSupported,\r
1388 UsbBusControllerDriverStart,\r
1389 UsbBusControllerDriverStop,\r
1390 0xa,\r
1391 NULL,\r
1392 NULL\r
1393};\r