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