]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Bus/Usb/UsbBusDxe/UsbBus.c
Fix doxygen issue:
[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
ecb575d9 871\r
872/**\r
873 Install Usb Bus Protocol on host controller, and start the Usb bus\r
874\r
875 @param This The USB bus driver binding instance\r
876 @param Controller The controller to check\r
877 @param RemainingDevicePath The remaining device patch\r
878\r
879 @retval EFI_SUCCESS The controller is controlled by the usb bus\r
880 @retval EFI_ALREADY_STARTED The controller is already controlled by the usb bus\r
881 @retval EFI_OUT_OF_RESOURCES Failed to allocate resources\r
882\r
883**/\r
884EFI_STATUS\r
885EFIAPI\r
886UsbBusBuildProtocol (\r
887 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
888 IN EFI_HANDLE Controller,\r
889 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath\r
890 )\r
891{\r
892 USB_BUS *UsbBus;\r
893 USB_DEVICE *RootHub;\r
894 USB_INTERFACE *RootIf;\r
895 EFI_STATUS Status;\r
896 EFI_STATUS Status2;\r
897\r
898 UsbBus = AllocateZeroPool (sizeof (USB_BUS));\r
899\r
900 if (UsbBus == NULL) {\r
901 return EFI_OUT_OF_RESOURCES;\r
902 }\r
903\r
904 UsbBus->Signature = USB_BUS_SIGNATURE;\r
905 UsbBus->HostHandle = Controller;\r
906\r
907 Status = gBS->OpenProtocol (\r
908 Controller,\r
909 &gEfiDevicePathProtocolGuid,\r
910 (VOID **) &UsbBus->DevicePath,\r
911 This->DriverBindingHandle,\r
912 Controller,\r
913 EFI_OPEN_PROTOCOL_BY_DRIVER\r
914 );\r
915\r
916 if (EFI_ERROR (Status)) {\r
917 DEBUG ((EFI_D_ERROR, "UsbBusStart: Failed to open device path %r\n", Status));\r
918\r
919 gBS->FreePool (UsbBus);\r
920 return Status;\r
921 }\r
922\r
923 //\r
924 // Get USB_HC2/USB_HC host controller protocol (EHCI/UHCI).\r
925 // This is for backward compatbility with EFI 1.x. In UEFI\r
926 // 2.x, USB_HC2 replaces USB_HC. We will open both USB_HC2\r
927 // and USB_HC because EHCI driver will install both protocols\r
928 // (for the same reason). If we don't consume both of them,\r
929 // the unconsumed one may be opened by others.\r
930 //\r
931 Status = gBS->OpenProtocol (\r
932 Controller,\r
933 &gEfiUsb2HcProtocolGuid,\r
934 (VOID **) &(UsbBus->Usb2Hc),\r
935 This->DriverBindingHandle,\r
936 Controller,\r
937 EFI_OPEN_PROTOCOL_BY_DRIVER\r
938 );\r
939\r
940 Status2 = gBS->OpenProtocol (\r
941 Controller,\r
942 &gEfiUsbHcProtocolGuid,\r
943 (VOID **) &(UsbBus->UsbHc),\r
944 This->DriverBindingHandle,\r
945 Controller,\r
946 EFI_OPEN_PROTOCOL_BY_DRIVER\r
947 );\r
948\r
949 if (EFI_ERROR (Status) && EFI_ERROR (Status2)) {\r
950 DEBUG ((EFI_D_ERROR, "UsbBusStart: Failed to open USB_HC/USB2_HC %r\n", Status));\r
951\r
952 Status = EFI_DEVICE_ERROR;\r
953 goto CLOSE_HC;\r
954 }\r
955\r
956 UsbHcReset (UsbBus, EFI_USB_HC_RESET_GLOBAL);\r
957 UsbHcSetState (UsbBus, EfiUsbHcStateOperational);\r
958\r
959 //\r
960 // Install an EFI_USB_BUS_PROTOCOL to host controler to identify it.\r
961 //\r
962 Status = gBS->InstallProtocolInterface (\r
963 &Controller,\r
964 &mUsbBusProtocolGuid,\r
965 EFI_NATIVE_INTERFACE,\r
966 &UsbBus->BusId\r
967 );\r
968\r
969 if (EFI_ERROR (Status)) {\r
970 DEBUG ((EFI_D_ERROR, "UsbBusStart: Failed to install bus protocol %r\n", Status));\r
971 goto CLOSE_HC;\r
972 }\r
973\r
974 //\r
975 // Initial the wanted child device path list, and add first RemainingDevicePath\r
976 //\r
977 InitializeListHead (&UsbBus->WantedUsbIoDPList);\r
978 Status = UsbBusAddWantedUsbIoDP (&UsbBus->BusId, RemainingDevicePath);\r
979 ASSERT (!EFI_ERROR (Status));\r
980 //\r
981 // Create a fake usb device for root hub\r
982 //\r
983 RootHub = AllocateZeroPool (sizeof (USB_DEVICE));\r
984\r
985 if (RootHub == NULL) {\r
986 Status = EFI_OUT_OF_RESOURCES;\r
987 goto UNINSTALL_USBBUS;\r
988 }\r
989\r
990 RootIf = AllocateZeroPool (sizeof (USB_INTERFACE));\r
991\r
992 if (RootIf == NULL) {\r
993 gBS->FreePool (RootHub);\r
994 Status = EFI_OUT_OF_RESOURCES;\r
995 goto FREE_ROOTHUB;\r
996 }\r
997\r
998 RootHub->Bus = UsbBus;\r
999 RootHub->NumOfInterface = 1;\r
1000 RootHub->Interfaces[0] = RootIf;\r
1001 RootIf->Signature = USB_INTERFACE_SIGNATURE;\r
1002 RootIf->Device = RootHub;\r
1003 RootIf->DevicePath = UsbBus->DevicePath;\r
1004\r
1005 Status = mUsbRootHubApi.Init (RootIf);\r
1006\r
1007 if (EFI_ERROR (Status)) {\r
1008 DEBUG ((EFI_D_ERROR, "UsbBusStart: Failed to init root hub %r\n", Status));\r
1009 goto FREE_ROOTHUB;\r
1010 }\r
1011\r
1012 UsbBus->Devices[0] = RootHub;\r
1013\r
1014 DEBUG ((EFI_D_INFO, "UsbBusStart: usb bus started on %x, root hub %x\n", Controller, RootIf));\r
1015 return EFI_SUCCESS;\r
1016\r
1017FREE_ROOTHUB:\r
1018 if (RootIf != NULL) {\r
1019 gBS->FreePool (RootIf);\r
1020 }\r
1021 if (RootHub != NULL) {\r
1022 gBS->FreePool (RootHub);\r
1023 }\r
1024\r
1025UNINSTALL_USBBUS:\r
1026 gBS->UninstallProtocolInterface (Controller, &mUsbBusProtocolGuid, &UsbBus->BusId);\r
1027\r
1028CLOSE_HC:\r
1029 if (UsbBus->Usb2Hc != NULL) {\r
1030 gBS->CloseProtocol (\r
1031 Controller,\r
1032 &gEfiUsb2HcProtocolGuid,\r
1033 This->DriverBindingHandle,\r
1034 Controller\r
1035 );\r
1036 }\r
1037 if (UsbBus->UsbHc != NULL) {\r
1038 gBS->CloseProtocol (\r
1039 Controller,\r
1040 &gEfiUsbHcProtocolGuid,\r
1041 This->DriverBindingHandle,\r
1042 Controller\r
1043 );\r
1044 }\r
1045 gBS->CloseProtocol (\r
1046 Controller,\r
1047 &gEfiDevicePathProtocolGuid,\r
1048 This->DriverBindingHandle,\r
1049 Controller\r
1050 );\r
1051 gBS->FreePool (UsbBus);\r
1052\r
1053 DEBUG ((EFI_D_ERROR, "UsbBusStart: Failed to start bus driver %r\n", Status));\r
1054 return Status;\r
1055}\r
1056\r
e237e7ae 1057EFI_USB_IO_PROTOCOL mUsbIoProtocol = {\r
1058 UsbIoControlTransfer,\r
1059 UsbIoBulkTransfer,\r
1060 UsbIoAsyncInterruptTransfer,\r
1061 UsbIoSyncInterruptTransfer,\r
1062 UsbIoIsochronousTransfer,\r
1063 UsbIoAsyncIsochronousTransfer,\r
1064 UsbIoGetDeviceDescriptor,\r
1065 UsbIoGetActiveConfigDescriptor,\r
1066 UsbIoGetInterfaceDescriptor,\r
1067 UsbIoGetEndpointDescriptor,\r
1068 UsbIoGetStringDescriptor,\r
1069 UsbIoGetSupportedLanguages,\r
1070 UsbIoPortReset\r
1071};\r
1072\r
e237e7ae 1073\r
1074EFI_STATUS\r
1075EFIAPI\r
1076UsbBusDriverEntryPoint (\r
1077 IN EFI_HANDLE ImageHandle,\r
1078 IN EFI_SYSTEM_TABLE *SystemTable\r
1079 )\r
1080/*++\r
1081\r
1082Routine Description:\r
1083\r
1084 The USB bus driver entry pointer\r
1085\r
1086Arguments:\r
1087\r
1088 ImageHandle - The driver image handle\r
1089 SystemTable - The system table\r
1090\r
1091Returns:\r
1092\r
1093 EFI_SUCCESS - The component name protocol is installed\r
1094 Others - Failed to init the usb driver\r
1095\r
1096--*/\r
1097{\r
62b9bb55 1098 return EfiLibInstallDriverBindingComponentName2 (\r
e237e7ae 1099 ImageHandle,\r
1100 SystemTable,\r
1101 &mUsbBusDriverBinding,\r
1102 ImageHandle,\r
1103 &mUsbBusComponentName,\r
62b9bb55 1104 &mUsbBusComponentName2\r
e237e7ae 1105 );\r
1106}\r
1107\r
1108\r
1109/**\r
1110 Check whether USB bus driver support this device\r
1111\r
1112 @param This The USB bus driver binding protocol\r
1113 @param Controller The controller handle to test againist\r
1114 @param RemainingDevicePath The remaining device path\r
1115\r
1116 @retval EFI_SUCCESS The bus supports this controller.\r
1117 @retval EFI_UNSUPPORTED This device isn't supported\r
1118\r
1119**/\r
1120EFI_STATUS\r
1121EFIAPI\r
1122UsbBusControllerDriverSupported (\r
1123 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
1124 IN EFI_HANDLE Controller,\r
1125 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath\r
1126 )\r
1127{\r
1128 EFI_DEV_PATH_PTR DevicePathNode;\r
1129 EFI_DEVICE_PATH_PROTOCOL *ParentDevicePath;\r
1130 EFI_USB2_HC_PROTOCOL *Usb2Hc;\r
1131 EFI_USB_HC_PROTOCOL *UsbHc;\r
1132 EFI_STATUS Status;\r
1133\r
1134 //\r
1135 // Check whether device path is valid\r
1136 //\r
1137 if (RemainingDevicePath != NULL) {\r
1138 DevicePathNode.DevPath = RemainingDevicePath;\r
1139\r
1140 if ((DevicePathNode.DevPath->Type != MESSAGING_DEVICE_PATH) ||\r
ecb575d9 1141 (DevicePathNode.DevPath->SubType != MSG_USB_DP &&\r
1142 DevicePathNode.DevPath->SubType != MSG_USB_CLASS_DP\r
1143 && DevicePathNode.DevPath->SubType != MSG_USB_WWID_DP\r
1144 )) {\r
e237e7ae 1145\r
1146 return EFI_UNSUPPORTED;\r
1147 }\r
1148 }\r
1149\r
1150 Status = gBS->OpenProtocol (\r
1151 Controller,\r
1152 &gEfiDevicePathProtocolGuid,\r
1153 (VOID **) &ParentDevicePath,\r
1154 This->DriverBindingHandle,\r
1155 Controller,\r
1156 EFI_OPEN_PROTOCOL_BY_DRIVER\r
1157 );\r
1158\r
1159 if (Status == EFI_ALREADY_STARTED) {\r
1160 return EFI_SUCCESS;\r
1161 }\r
1162\r
1163 if (EFI_ERROR (Status)) {\r
1164 return Status;\r
1165 }\r
1166\r
1167 gBS->CloseProtocol (\r
1168 Controller,\r
1169 &gEfiDevicePathProtocolGuid,\r
1170 This->DriverBindingHandle,\r
1171 Controller\r
1172 );\r
1173\r
1174 //\r
1175 // Check whether USB_HC2 protocol is installed\r
1176 //\r
1177 Status = gBS->OpenProtocol (\r
1178 Controller,\r
1179 &gEfiUsb2HcProtocolGuid,\r
1180 (VOID **) &Usb2Hc,\r
1181 This->DriverBindingHandle,\r
1182 Controller,\r
1183 EFI_OPEN_PROTOCOL_BY_DRIVER\r
1184 );\r
1185\r
1186 if (Status == EFI_ALREADY_STARTED) {\r
1187 return EFI_SUCCESS;\r
1188 }\r
1189\r
1190 if (!EFI_ERROR (Status)) {\r
1191 gBS->CloseProtocol (\r
1192 Controller,\r
1193 &gEfiUsb2HcProtocolGuid,\r
1194 This->DriverBindingHandle,\r
1195 Controller\r
1196 );\r
1197\r
1198 return EFI_SUCCESS;\r
1199 }\r
1200\r
1201 //\r
1202 // If failed to open USB_HC2, fall back to USB_HC\r
1203 //\r
1204 Status = gBS->OpenProtocol (\r
1205 Controller,\r
1206 &gEfiUsbHcProtocolGuid,\r
1207 (VOID **) &UsbHc,\r
1208 This->DriverBindingHandle,\r
1209 Controller,\r
1210 EFI_OPEN_PROTOCOL_BY_DRIVER\r
1211 );\r
1212\r
1213 if (Status == EFI_ALREADY_STARTED) {\r
1214 return EFI_SUCCESS;\r
1215 }\r
1216\r
1217 if (!EFI_ERROR (Status)) {\r
1218 gBS->CloseProtocol (\r
1219 Controller,\r
1220 &gEfiUsbHcProtocolGuid,\r
1221 This->DriverBindingHandle,\r
1222 Controller\r
1223 );\r
1224 }\r
1225\r
1226 return Status;\r
1227}\r
1228\r
1229\r
1230/**\r
1231 Start to process the controller\r
1232\r
1233 @param This The USB bus driver binding instance\r
1234 @param Controller The controller to check\r
1235 @param RemainingDevicePath The remaining device patch\r
1236\r
1237 @retval EFI_SUCCESS The controller is controlled by the usb bus\r
1238 @retval EFI_ALREADY_STARTED The controller is already controlled by the usb\r
1239 bus\r
1240 @retval EFI_OUT_OF_RESOURCES Failed to allocate resources\r
1241\r
1242**/\r
1243EFI_STATUS\r
1244EFIAPI\r
1245UsbBusControllerDriverStart (\r
1246 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
1247 IN EFI_HANDLE Controller,\r
1248 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath\r
1249 )\r
1250{\r
ecb575d9 1251 EFI_USB_BUS_PROTOCOL *UsbBusId;\r
1252 EFI_STATUS Status;\r
e237e7ae 1253\r
1254 //\r
1255 // Locate the USB bus protocol, if it is found, USB bus\r
1256 // is already started on this controller.\r
1257 //\r
1258 Status = gBS->OpenProtocol (\r
1259 Controller,\r
1260 &mUsbBusProtocolGuid,\r
1261 (VOID **) &UsbBusId,\r
1262 This->DriverBindingHandle,\r
1263 Controller,\r
1264 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
1265 );\r
1266\r
e237e7ae 1267 if (EFI_ERROR (Status)) {\r
ecb575d9 1268 //\r
1269 // If first start, build the bus execute enviorment and install bus protocol\r
1270 //\r
1271 Status = UsbBusBuildProtocol (This, Controller, RemainingDevicePath);\r
1272 if (EFI_ERROR (Status)) {\r
1273 return Status;\r
1274 }\r
1275 //\r
1276 // Try get the Usb Bus protocol interface again\r
1277 //\r
1278 Status = gBS->OpenProtocol (\r
1279 Controller,\r
1280 &mUsbBusProtocolGuid,\r
1281 (VOID **) &UsbBusId,\r
1282 This->DriverBindingHandle,\r
1283 Controller,\r
1284 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
1285 );\r
1286 ASSERT (!EFI_ERROR (Status));\r
1287 } else {\r
1288 //\r
1289 // USB Bus driver need to control the recursive connect policy of the bus, only those wanted\r
1290 // usb child device will be recursively connected.\r
1291 // The RemainingDevicePath indicate the child usb device which user want to fully recursively connecte this time.\r
1292 // All wanted usb child devices will be remembered by the usb bus driver itself.\r
1293 // If RemainingDevicePath == NULL, all the usb child devices in the usb bus are wanted devices.\r
1294 //\r
1295 // Save the passed in RemainingDevicePath this time\r
1296 //\r
1297 Status = UsbBusAddWantedUsbIoDP (UsbBusId, RemainingDevicePath);\r
1298 ASSERT (!EFI_ERROR (Status));\r
1299 //\r
1300 // Ensure all wanted child usb devices are fully recursively connected\r
1301 //\r
1302 Status = UsbBusRecursivelyConnectWantedUsbIo (UsbBusId);\r
1303 ASSERT (!EFI_ERROR (Status));\r
e237e7ae 1304 }\r
1305\r
e237e7ae 1306\r
e237e7ae 1307 return EFI_SUCCESS;\r
e237e7ae 1308}\r
1309\r
1310\r
1311/**\r
1312 Stop handle the controller by this USB bus driver\r
1313\r
1314 @param This The USB bus driver binding protocol\r
1315 @param Controller The controller to release\r
1316 @param NumberOfChildren The child of USB bus that opened controller\r
1317 BY_CHILD\r
1318 @param ChildHandleBuffer The array of child handle\r
1319\r
1320 @retval EFI_SUCCESS The controller or children are stopped\r
1321 @retval EFI_DEVICE_ERROR Failed to stop the driver\r
1322\r
1323**/\r
1324EFI_STATUS\r
1325EFIAPI\r
1326UsbBusControllerDriverStop (\r
1327 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
1328 IN EFI_HANDLE Controller,\r
1329 IN UINTN NumberOfChildren,\r
1330 IN EFI_HANDLE *ChildHandleBuffer\r
1331 )\r
1332{\r
1333 USB_BUS *Bus;\r
1334 USB_DEVICE *RootHub;\r
1335 USB_DEVICE *UsbDev;\r
1336 USB_INTERFACE *RootIf;\r
1337 USB_INTERFACE *UsbIf;\r
1338 EFI_USB_BUS_PROTOCOL *BusId;\r
1339 EFI_USB_IO_PROTOCOL *UsbIo;\r
1340 EFI_TPL OldTpl;\r
1341 UINTN Index;\r
1342 EFI_STATUS Status;\r
1343\r
1344 Status = EFI_SUCCESS;\r
1345\r
1346 if (NumberOfChildren > 0) {\r
ecb575d9 1347 //\r
1348 // BugBug: Raise TPL to callback level instead of USB_BUS_TPL to avoid TPL conflict\r
1349 //\r
1350 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
e237e7ae 1351\r
1352 for (Index = 0; Index < NumberOfChildren; Index++) {\r
1353 Status = gBS->OpenProtocol (\r
1354 ChildHandleBuffer[Index],\r
1355 &gEfiUsbIoProtocolGuid,\r
1356 (VOID **) &UsbIo,\r
1357 This->DriverBindingHandle,\r
1358 Controller,\r
1359 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
1360 );\r
1361\r
1362 if (EFI_ERROR (Status)) {\r
1363 //\r
1364 // It is possible that the child has already been released:\r
1365 // 1. For combo device, free one device will release others.\r
1366 // 2. If a hub is released, all devices on its down facing\r
1367 // ports are released also.\r
1368 //\r
1369 continue;\r
1370 }\r
1371\r
1372 UsbIf = USB_INTERFACE_FROM_USBIO (UsbIo);\r
1373 UsbDev = UsbIf->Device;\r
1374\r
1375 UsbRemoveDevice (UsbDev);\r
1376 }\r
1377\r
1378 gBS->RestoreTPL (OldTpl);\r
1379 return EFI_SUCCESS;\r
1380 }\r
1381\r
d2577026 1382 DEBUG (( EFI_D_INFO, "UsbBusStop: usb bus stopped on %x\n", Controller));\r
e237e7ae 1383\r
1384 //\r
1385 // Locate USB_BUS for the current host controller\r
1386 //\r
1387 Status = gBS->OpenProtocol (\r
1388 Controller,\r
1389 &mUsbBusProtocolGuid,\r
1390 (VOID **) &BusId,\r
1391 This->DriverBindingHandle,\r
1392 Controller,\r
1393 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
1394 );\r
1395\r
1396 if (EFI_ERROR (Status)) {\r
1397 return Status;\r
1398 }\r
1399\r
1400 Bus = USB_BUS_FROM_THIS (BusId);\r
1401\r
1402 //\r
1403 // Stop the root hub, then free all the devices\r
1404 //\r
ecb575d9 1405 // BugBug: Raise TPL to callback level instead of USB_BUS_TPL to avoid TPL conflict\r
1406 //\r
1407 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
e237e7ae 1408 UsbHcSetState (Bus, EfiUsbHcStateHalt);\r
1409\r
1410 RootHub = Bus->Devices[0];\r
1411 RootIf = RootHub->Interfaces[0];\r
1412\r
1413 mUsbRootHubApi.Release (RootIf);\r
1414\r
1415 for (Index = 1; Index < USB_MAX_DEVICES; Index++) {\r
1416 if (Bus->Devices[Index] != NULL) {\r
1417 UsbRemoveDevice (Bus->Devices[Index]);\r
1418 }\r
1419 }\r
1420\r
1421 gBS->RestoreTPL (OldTpl);\r
1422\r
1423 gBS->FreePool (RootIf);\r
1424 gBS->FreePool (RootHub);\r
ecb575d9 1425 Status = UsbBusFreeUsbDPList (&Bus->WantedUsbIoDPList);\r
1426 ASSERT (!EFI_ERROR (Status));\r
e237e7ae 1427\r
1428 //\r
1429 // Uninstall the bus identifier and close USB_HC/USB2_HC protocols\r
1430 //\r
1431 gBS->UninstallProtocolInterface (Controller, &mUsbBusProtocolGuid, &Bus->BusId);\r
1432\r
1433 if (Bus->Usb2Hc != NULL) {\r
1434 gBS->CloseProtocol (\r
1435 Controller,\r
1436 &gEfiUsb2HcProtocolGuid,\r
1437 This->DriverBindingHandle,\r
1438 Controller\r
1439 );\r
1440 }\r
1441\r
1442 if (Bus->UsbHc != NULL) {\r
1443 gBS->CloseProtocol (\r
1444 Controller,\r
1445 &gEfiUsbHcProtocolGuid,\r
1446 This->DriverBindingHandle,\r
1447 Controller\r
1448 );\r
1449 }\r
1450\r
1451 gBS->CloseProtocol (\r
1452 Controller,\r
1453 &gEfiDevicePathProtocolGuid,\r
1454 This->DriverBindingHandle,\r
1455 Controller\r
1456 );\r
1457\r
1458 gBS->FreePool (Bus);\r
1459\r
1460 return Status;\r
1461}\r
1462\r
1463EFI_DRIVER_BINDING_PROTOCOL mUsbBusDriverBinding = {\r
1464 UsbBusControllerDriverSupported,\r
1465 UsbBusControllerDriverStart,\r
1466 UsbBusControllerDriverStop,\r
1467 0xa,\r
1468 NULL,\r
1469 NULL\r
1470};\r