]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Bus/Usb/UsbBusDxe/UsbEnumer.c
MdeModulePkg/UsbBus: Fix system hang when failed to uninstall UsbIo
[mirror_edk2.git] / MdeModulePkg / Bus / Usb / UsbBusDxe / UsbEnumer.c
1 /** @file
2
3 Usb bus enumeration support.
4
5 Copyright (c) 2007 - 2017, Intel Corporation. All rights reserved.<BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
10
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13
14 **/
15
16 #include "UsbBus.h"
17
18 /**
19 Return the endpoint descriptor in this interface.
20
21 @param UsbIf The interface to search in.
22 @param EpAddr The address of the endpoint to return.
23
24 @return The endpoint descriptor or NULL.
25
26 **/
27 USB_ENDPOINT_DESC *
28 UsbGetEndpointDesc (
29 IN USB_INTERFACE *UsbIf,
30 IN UINT8 EpAddr
31 )
32 {
33 USB_ENDPOINT_DESC *EpDesc;
34 UINT8 Index;
35 UINT8 NumEndpoints;
36
37 NumEndpoints = UsbIf->IfSetting->Desc.NumEndpoints;
38
39 for (Index = 0; Index < NumEndpoints; Index++) {
40 EpDesc = UsbIf->IfSetting->Endpoints[Index];
41
42 if (EpDesc->Desc.EndpointAddress == EpAddr) {
43 return EpDesc;
44 }
45 }
46
47 return NULL;
48 }
49
50
51 /**
52 Free the resource used by USB interface.
53
54 @param UsbIf The USB interface to free.
55
56 @retval EFI_ACCESS_DENIED The interface is still occupied.
57 @retval EFI_SUCCESS The interface is freed.
58 **/
59 EFI_STATUS
60 UsbFreeInterface (
61 IN USB_INTERFACE *UsbIf
62 )
63 {
64 EFI_STATUS Status;
65
66 UsbCloseHostProtoByChild (UsbIf->Device->Bus, UsbIf->Handle);
67
68 Status = gBS->UninstallMultipleProtocolInterfaces (
69 UsbIf->Handle,
70 &gEfiDevicePathProtocolGuid, UsbIf->DevicePath,
71 &gEfiUsbIoProtocolGuid, &UsbIf->UsbIo,
72 NULL
73 );
74 if (!EFI_ERROR (Status)) {
75 if (UsbIf->DevicePath != NULL) {
76 FreePool (UsbIf->DevicePath);
77 }
78 FreePool (UsbIf);
79 } else {
80 UsbOpenHostProtoByChild (UsbIf->Device->Bus, UsbIf->Handle);
81 }
82 return Status;
83 }
84
85
86 /**
87 Create an interface for the descriptor IfDesc. Each
88 device's configuration can have several interfaces.
89
90 @param Device The device has the interface descriptor.
91 @param IfDesc The interface descriptor.
92
93 @return The created USB interface for the descriptor, or NULL.
94
95 **/
96 USB_INTERFACE *
97 UsbCreateInterface (
98 IN USB_DEVICE *Device,
99 IN USB_INTERFACE_DESC *IfDesc
100 )
101 {
102 USB_DEVICE_PATH UsbNode;
103 USB_INTERFACE *UsbIf;
104 USB_INTERFACE *HubIf;
105 EFI_STATUS Status;
106
107 UsbIf = AllocateZeroPool (sizeof (USB_INTERFACE));
108
109 if (UsbIf == NULL) {
110 return NULL;
111 }
112
113 UsbIf->Signature = USB_INTERFACE_SIGNATURE;
114 UsbIf->Device = Device;
115 UsbIf->IfDesc = IfDesc;
116 ASSERT (IfDesc->ActiveIndex < USB_MAX_INTERFACE_SETTING);
117 UsbIf->IfSetting = IfDesc->Settings[IfDesc->ActiveIndex];
118
119 CopyMem (
120 &(UsbIf->UsbIo),
121 &mUsbIoProtocol,
122 sizeof (EFI_USB_IO_PROTOCOL)
123 );
124
125 //
126 // Install protocols for USBIO and device path
127 //
128 UsbNode.Header.Type = MESSAGING_DEVICE_PATH;
129 UsbNode.Header.SubType = MSG_USB_DP;
130 UsbNode.ParentPortNumber = Device->ParentPort;
131 UsbNode.InterfaceNumber = UsbIf->IfSetting->Desc.InterfaceNumber;
132
133 SetDevicePathNodeLength (&UsbNode.Header, sizeof (UsbNode));
134
135 HubIf = Device->ParentIf;
136 ASSERT (HubIf != NULL);
137
138 UsbIf->DevicePath = AppendDevicePathNode (HubIf->DevicePath, &UsbNode.Header);
139
140 if (UsbIf->DevicePath == NULL) {
141 DEBUG ((EFI_D_ERROR, "UsbCreateInterface: failed to create device path\n"));
142
143 Status = EFI_OUT_OF_RESOURCES;
144 goto ON_ERROR;
145 }
146
147 Status = gBS->InstallMultipleProtocolInterfaces (
148 &UsbIf->Handle,
149 &gEfiDevicePathProtocolGuid,
150 UsbIf->DevicePath,
151 &gEfiUsbIoProtocolGuid,
152 &UsbIf->UsbIo,
153 NULL
154 );
155
156 if (EFI_ERROR (Status)) {
157 DEBUG ((EFI_D_ERROR, "UsbCreateInterface: failed to install UsbIo - %r\n", Status));
158 goto ON_ERROR;
159 }
160
161 //
162 // Open USB Host Controller Protocol by Child
163 //
164 Status = UsbOpenHostProtoByChild (Device->Bus, UsbIf->Handle);
165
166 if (EFI_ERROR (Status)) {
167 gBS->UninstallMultipleProtocolInterfaces (
168 &UsbIf->Handle,
169 &gEfiDevicePathProtocolGuid,
170 UsbIf->DevicePath,
171 &gEfiUsbIoProtocolGuid,
172 &UsbIf->UsbIo,
173 NULL
174 );
175
176 DEBUG ((EFI_D_ERROR, "UsbCreateInterface: failed to open host for child - %r\n", Status));
177 goto ON_ERROR;
178 }
179
180 return UsbIf;
181
182 ON_ERROR:
183 if (UsbIf->DevicePath != NULL) {
184 FreePool (UsbIf->DevicePath);
185 }
186
187 FreePool (UsbIf);
188 return NULL;
189 }
190
191
192 /**
193 Free the resource used by this USB device.
194
195 @param Device The USB device to free.
196
197 **/
198 VOID
199 UsbFreeDevice (
200 IN USB_DEVICE *Device
201 )
202 {
203 if (Device->DevDesc != NULL) {
204 UsbFreeDevDesc (Device->DevDesc);
205 }
206
207 gBS->FreePool (Device);
208 }
209
210
211 /**
212 Create a device which is on the parent's ParentPort port.
213
214 @param ParentIf The parent HUB interface.
215 @param ParentPort The port on the HUB this device is connected to.
216
217 @return Created USB device, Or NULL.
218
219 **/
220 USB_DEVICE *
221 UsbCreateDevice (
222 IN USB_INTERFACE *ParentIf,
223 IN UINT8 ParentPort
224 )
225 {
226 USB_DEVICE *Device;
227
228 ASSERT (ParentIf != NULL);
229
230 Device = AllocateZeroPool (sizeof (USB_DEVICE));
231
232 if (Device == NULL) {
233 return NULL;
234 }
235
236 Device->Bus = ParentIf->Device->Bus;
237 Device->MaxPacket0 = 8;
238 Device->ParentAddr = ParentIf->Device->Address;
239 Device->ParentIf = ParentIf;
240 Device->ParentPort = ParentPort;
241 Device->Tier = (UINT8)(ParentIf->Device->Tier + 1);
242 return Device;
243 }
244
245
246 /**
247 Connect the USB interface with its driver. EFI USB bus will
248 create a USB interface for each separate interface descriptor.
249
250 @param UsbIf The interface to connect driver to.
251
252 @return EFI_SUCCESS Interface is managed by some driver.
253 @return Others Failed to locate a driver for this interface.
254
255 **/
256 EFI_STATUS
257 UsbConnectDriver (
258 IN USB_INTERFACE *UsbIf
259 )
260 {
261 EFI_STATUS Status;
262 EFI_TPL OldTpl;
263
264 Status = EFI_SUCCESS;
265
266 //
267 // Hub is maintained by the USB bus driver. Otherwise try to
268 // connect drivers with this interface
269 //
270 if (UsbIsHubInterface (UsbIf)) {
271 DEBUG ((EFI_D_INFO, "UsbConnectDriver: found a hub device\n"));
272 Status = mUsbHubApi.Init (UsbIf);
273
274 } else {
275 //
276 // This function is called in both UsbIoControlTransfer and
277 // the timer callback in hub enumeration. So, at least it is
278 // called at TPL_CALLBACK. Some driver sitting on USB has
279 // twisted TPL used. It should be no problem for us to connect
280 // or disconnect at CALLBACK.
281 //
282
283 //
284 // Only recursively wanted usb child device
285 //
286 if (UsbBusIsWantedUsbIO (UsbIf->Device->Bus, UsbIf)) {
287 OldTpl = UsbGetCurrentTpl ();
288 DEBUG ((EFI_D_INFO, "UsbConnectDriver: TPL before connect is %d, %p\n", (UINT32)OldTpl, UsbIf->Handle));
289
290 gBS->RestoreTPL (TPL_CALLBACK);
291
292 Status = gBS->ConnectController (UsbIf->Handle, NULL, NULL, TRUE);
293 UsbIf->IsManaged = (BOOLEAN)!EFI_ERROR (Status);
294
295 DEBUG ((EFI_D_INFO, "UsbConnectDriver: TPL after connect is %d\n", (UINT32)UsbGetCurrentTpl()));
296 ASSERT (UsbGetCurrentTpl () == TPL_CALLBACK);
297
298 gBS->RaiseTPL (OldTpl);
299 }
300 }
301
302 return Status;
303 }
304
305
306 /**
307 Select an alternate setting for the interface.
308 Each interface can have several mutually exclusive
309 settings. Only one setting is active. It will
310 also reset its endpoints' toggle to zero.
311
312 @param IfDesc The interface descriptor to set.
313 @param Alternate The alternate setting number to locate.
314
315 @retval EFI_NOT_FOUND There is no setting with this alternate index.
316 @retval EFI_SUCCESS The interface is set to Alternate setting.
317
318 **/
319 EFI_STATUS
320 UsbSelectSetting (
321 IN USB_INTERFACE_DESC *IfDesc,
322 IN UINT8 Alternate
323 )
324 {
325 USB_INTERFACE_SETTING *Setting;
326 UINTN Index;
327
328 //
329 // Locate the active alternate setting
330 //
331 Setting = NULL;
332
333 for (Index = 0; Index < IfDesc->NumOfSetting; Index++) {
334 ASSERT (Index < USB_MAX_INTERFACE_SETTING);
335 Setting = IfDesc->Settings[Index];
336
337 if (Setting->Desc.AlternateSetting == Alternate) {
338 break;
339 }
340 }
341
342 if (Index == IfDesc->NumOfSetting) {
343 return EFI_NOT_FOUND;
344 }
345
346 IfDesc->ActiveIndex = Index;
347
348 ASSERT (Setting != NULL);
349 DEBUG ((EFI_D_INFO, "UsbSelectSetting: setting %d selected for interface %d\n",
350 Alternate, Setting->Desc.InterfaceNumber));
351
352 //
353 // Reset the endpoint toggle to zero
354 //
355 for (Index = 0; Index < Setting->Desc.NumEndpoints; Index++) {
356 Setting->Endpoints[Index]->Toggle = 0;
357 }
358
359 return EFI_SUCCESS;
360 }
361
362
363 /**
364 Select a new configuration for the device. Each
365 device may support several configurations.
366
367 @param Device The device to select configuration.
368 @param ConfigValue The index of the configuration ( != 0).
369
370 @retval EFI_NOT_FOUND There is no configuration with the index.
371 @retval EFI_OUT_OF_RESOURCES Failed to allocate resource.
372 @retval EFI_SUCCESS The configuration is selected.
373
374 **/
375 EFI_STATUS
376 UsbSelectConfig (
377 IN USB_DEVICE *Device,
378 IN UINT8 ConfigValue
379 )
380 {
381 USB_DEVICE_DESC *DevDesc;
382 USB_CONFIG_DESC *ConfigDesc;
383 USB_INTERFACE_DESC *IfDesc;
384 USB_INTERFACE *UsbIf;
385 EFI_STATUS Status;
386 UINT8 Index;
387
388 //
389 // Locate the active config, then set the device's pointer
390 //
391 DevDesc = Device->DevDesc;
392 ConfigDesc = NULL;
393
394 for (Index = 0; Index < DevDesc->Desc.NumConfigurations; Index++) {
395 ConfigDesc = DevDesc->Configs[Index];
396
397 if (ConfigDesc->Desc.ConfigurationValue == ConfigValue) {
398 break;
399 }
400 }
401
402 if (Index == DevDesc->Desc.NumConfigurations) {
403 return EFI_NOT_FOUND;
404 }
405
406 Device->ActiveConfig = ConfigDesc;
407
408 DEBUG ((EFI_D_INFO, "UsbSelectConfig: config %d selected for device %d\n",
409 ConfigValue, Device->Address));
410
411 //
412 // Create interfaces for each USB interface descriptor.
413 //
414 for (Index = 0; Index < ConfigDesc->Desc.NumInterfaces; Index++) {
415 //
416 // First select the default interface setting, and reset
417 // the endpoint toggles to zero for its endpoints.
418 //
419 IfDesc = ConfigDesc->Interfaces[Index];
420 UsbSelectSetting (IfDesc, IfDesc->Settings[0]->Desc.AlternateSetting);
421
422 //
423 // Create a USB_INTERFACE and install USB_IO and other protocols
424 //
425 UsbIf = UsbCreateInterface (Device, ConfigDesc->Interfaces[Index]);
426
427 if (UsbIf == NULL) {
428 Device->NumOfInterface = Index;
429 return EFI_OUT_OF_RESOURCES;
430 }
431
432 ASSERT (Index < USB_MAX_INTERFACE);
433 Device->Interfaces[Index] = UsbIf;
434
435 //
436 // Connect the device to drivers, if it failed, ignore
437 // the error. Don't let the unsupported interfaces to block
438 // the supported interfaces.
439 //
440 Status = UsbConnectDriver (UsbIf);
441
442 if (EFI_ERROR (Status)) {
443 DEBUG ((EFI_D_ERROR, "UsbSelectConfig: failed to connect driver %r, ignored\n", Status));
444 }
445 }
446
447 Device->NumOfInterface = Index;
448
449 return EFI_SUCCESS;
450 }
451
452
453 /**
454 Disconnect the USB interface with its driver.
455
456 @param UsbIf The interface to disconnect driver from.
457
458 **/
459 EFI_STATUS
460 UsbDisconnectDriver (
461 IN USB_INTERFACE *UsbIf
462 )
463 {
464 EFI_TPL OldTpl;
465 EFI_STATUS Status;
466
467 //
468 // Release the hub if it's a hub controller, otherwise
469 // disconnect the driver if it is managed by other drivers.
470 //
471 Status = EFI_SUCCESS;
472 if (UsbIf->IsHub) {
473 Status = UsbIf->HubApi->Release (UsbIf);
474
475 } else if (UsbIf->IsManaged) {
476 //
477 // This function is called in both UsbIoControlTransfer and
478 // the timer callback in hub enumeration. So, at least it is
479 // called at TPL_CALLBACK. Some driver sitting on USB has
480 // twisted TPL used. It should be no problem for us to connect
481 // or disconnect at CALLBACK.
482 //
483 OldTpl = UsbGetCurrentTpl ();
484 DEBUG ((EFI_D_INFO, "UsbDisconnectDriver: old TPL is %d, %p\n", (UINT32)OldTpl, UsbIf->Handle));
485
486 gBS->RestoreTPL (TPL_CALLBACK);
487
488 Status = gBS->DisconnectController (UsbIf->Handle, NULL, NULL);
489 if (!EFI_ERROR (Status)) {
490 UsbIf->IsManaged = FALSE;
491 }
492
493 DEBUG (( EFI_D_INFO, "UsbDisconnectDriver: TPL after disconnect is %d, %d\n", (UINT32)UsbGetCurrentTpl(), Status));
494 ASSERT (UsbGetCurrentTpl () == TPL_CALLBACK);
495
496 gBS->RaiseTPL (OldTpl);
497 }
498
499 return Status;
500 }
501
502
503 /**
504 Remove the current device configuration.
505
506 @param Device The USB device to remove configuration from.
507
508 **/
509 EFI_STATUS
510 UsbRemoveConfig (
511 IN USB_DEVICE *Device
512 )
513 {
514 USB_INTERFACE *UsbIf;
515 UINTN Index;
516 EFI_STATUS Status;
517 EFI_STATUS ReturnStatus;
518
519 //
520 // Remove each interface of the device
521 //
522 ReturnStatus = EFI_SUCCESS;
523 for (Index = 0; Index < Device->NumOfInterface; Index++) {
524 ASSERT (Index < USB_MAX_INTERFACE);
525 UsbIf = Device->Interfaces[Index];
526
527 if (UsbIf == NULL) {
528 continue;
529 }
530
531 Status = UsbDisconnectDriver (UsbIf);
532 if (!EFI_ERROR (Status)) {
533 Status = UsbFreeInterface (UsbIf);
534 if (EFI_ERROR (Status)) {
535 UsbConnectDriver (UsbIf);
536 }
537 }
538
539 if (!EFI_ERROR (Status)) {
540 Device->Interfaces[Index] = NULL;
541 } else {
542 ReturnStatus = Status;
543 }
544 }
545
546 Device->ActiveConfig = NULL;
547 return ReturnStatus;
548 }
549
550
551 /**
552 Remove the device and all its children from the bus.
553
554 @param Device The device to remove.
555
556 @retval EFI_SUCCESS The device is removed.
557
558 **/
559 EFI_STATUS
560 UsbRemoveDevice (
561 IN USB_DEVICE *Device
562 )
563 {
564 USB_BUS *Bus;
565 USB_DEVICE *Child;
566 EFI_STATUS Status;
567 EFI_STATUS ReturnStatus;
568 UINTN Index;
569
570 Bus = Device->Bus;
571
572 //
573 // Remove all the devices on its downstream ports. Search from devices[1].
574 // Devices[0] is the root hub.
575 //
576 ReturnStatus = EFI_SUCCESS;
577 for (Index = 1; Index < Bus->MaxDevices; Index++) {
578 Child = Bus->Devices[Index];
579
580 if ((Child == NULL) || (Child->ParentAddr != Device->Address)) {
581 continue;
582 }
583
584 Status = UsbRemoveDevice (Child);
585
586 if (!EFI_ERROR (Status)) {
587 Bus->Devices[Index] = NULL;
588 } else {
589 Bus->Devices[Index]->DisconnectFail = TRUE;
590 ReturnStatus = Status;
591 DEBUG ((EFI_D_INFO, "UsbRemoveDevice: failed to remove child %p at parent %p\n", Child, Device));
592 }
593 }
594
595 if (EFI_ERROR (ReturnStatus)) {
596 return ReturnStatus;
597 }
598
599 Status = UsbRemoveConfig (Device);
600
601 if (!EFI_ERROR (Status)) {
602 DEBUG (( EFI_D_INFO, "UsbRemoveDevice: device %d removed\n", Device->Address));
603
604 ASSERT (Device->Address < Bus->MaxDevices);
605 Bus->Devices[Device->Address] = NULL;
606 UsbFreeDevice (Device);
607 } else {
608 Bus->Devices[Device->Address]->DisconnectFail = TRUE;
609 }
610 return Status;
611 }
612
613
614 /**
615 Find the child device on the hub's port.
616
617 @param HubIf The hub interface.
618 @param Port The port of the hub this child is connected to.
619
620 @return The device on the hub's port, or NULL if there is none.
621
622 **/
623 USB_DEVICE *
624 UsbFindChild (
625 IN USB_INTERFACE *HubIf,
626 IN UINT8 Port
627 )
628 {
629 USB_DEVICE *Device;
630 USB_BUS *Bus;
631 UINTN Index;
632
633 Bus = HubIf->Device->Bus;
634
635 //
636 // Start checking from device 1, device 0 is the root hub
637 //
638 for (Index = 1; Index < Bus->MaxDevices; Index++) {
639 Device = Bus->Devices[Index];
640
641 if ((Device != NULL) && (Device->ParentAddr == HubIf->Device->Address) &&
642 (Device->ParentPort == Port)) {
643
644 return Device;
645 }
646 }
647
648 return NULL;
649 }
650
651
652 /**
653 Enumerate and configure the new device on the port of this HUB interface.
654
655 @param HubIf The HUB that has the device connected.
656 @param Port The port index of the hub (started with zero).
657 @param ResetIsNeeded The boolean to control whether skip the reset of the port.
658
659 @retval EFI_SUCCESS The device is enumerated (added or removed).
660 @retval EFI_OUT_OF_RESOURCES Failed to allocate resource for the device.
661 @retval Others Failed to enumerate the device.
662
663 **/
664 EFI_STATUS
665 UsbEnumerateNewDev (
666 IN USB_INTERFACE *HubIf,
667 IN UINT8 Port,
668 IN BOOLEAN ResetIsNeeded
669 )
670 {
671 USB_BUS *Bus;
672 USB_HUB_API *HubApi;
673 USB_DEVICE *Child;
674 USB_DEVICE *Parent;
675 EFI_USB_PORT_STATUS PortState;
676 UINTN Address;
677 UINT8 Config;
678 EFI_STATUS Status;
679
680 Parent = HubIf->Device;
681 Bus = Parent->Bus;
682 HubApi = HubIf->HubApi;
683 Address = Bus->MaxDevices;
684
685 gBS->Stall (USB_WAIT_PORT_STABLE_STALL);
686
687 //
688 // Hub resets the device for at least 10 milliseconds.
689 // Host learns device speed. If device is of low/full speed
690 // and the hub is a EHCI root hub, ResetPort will release
691 // the device to its companion UHCI and return an error.
692 //
693 if (ResetIsNeeded) {
694 Status = HubApi->ResetPort (HubIf, Port);
695 if (EFI_ERROR (Status)) {
696 DEBUG ((EFI_D_ERROR, "UsbEnumerateNewDev: failed to reset port %d - %r\n", Port, Status));
697
698 return Status;
699 }
700 DEBUG (( EFI_D_INFO, "UsbEnumerateNewDev: hub port %d is reset\n", Port));
701 } else {
702 DEBUG (( EFI_D_INFO, "UsbEnumerateNewDev: hub port %d reset is skipped\n", Port));
703 }
704
705 Child = UsbCreateDevice (HubIf, Port);
706
707 if (Child == NULL) {
708 return EFI_OUT_OF_RESOURCES;
709 }
710
711 //
712 // OK, now identify the device speed. After reset, hub
713 // fully knows the actual device speed.
714 //
715 Status = HubApi->GetPortStatus (HubIf, Port, &PortState);
716
717 if (EFI_ERROR (Status)) {
718 DEBUG ((EFI_D_ERROR, "UsbEnumerateNewDev: failed to get speed of port %d\n", Port));
719 goto ON_ERROR;
720 }
721
722 if (!USB_BIT_IS_SET (PortState.PortStatus, USB_PORT_STAT_CONNECTION)) {
723 DEBUG ((EFI_D_ERROR, "UsbEnumerateNewDev: No device present at port %d\n", Port));
724 goto ON_ERROR;
725 } else if (USB_BIT_IS_SET (PortState.PortStatus, USB_PORT_STAT_SUPER_SPEED)){
726 Child->Speed = EFI_USB_SPEED_SUPER;
727 Child->MaxPacket0 = 512;
728 } else if (USB_BIT_IS_SET (PortState.PortStatus, USB_PORT_STAT_HIGH_SPEED)) {
729 Child->Speed = EFI_USB_SPEED_HIGH;
730 Child->MaxPacket0 = 64;
731 } else if (USB_BIT_IS_SET (PortState.PortStatus, USB_PORT_STAT_LOW_SPEED)) {
732 Child->Speed = EFI_USB_SPEED_LOW;
733 Child->MaxPacket0 = 8;
734 } else {
735 Child->Speed = EFI_USB_SPEED_FULL;
736 Child->MaxPacket0 = 8;
737 }
738
739 DEBUG (( EFI_D_INFO, "UsbEnumerateNewDev: device is of %d speed\n", Child->Speed));
740
741 if (((Child->Speed == EFI_USB_SPEED_LOW) || (Child->Speed == EFI_USB_SPEED_FULL)) &&
742 (Parent->Speed == EFI_USB_SPEED_HIGH)) {
743 //
744 // If the child is a low or full speed device, it is necessary to
745 // set the transaction translator. Port TT is 1-based.
746 // This is quite simple:
747 // 1. if parent is of high speed, then parent is our translator
748 // 2. otherwise use parent's translator.
749 //
750 Child->Translator.TranslatorHubAddress = Parent->Address;
751 Child->Translator.TranslatorPortNumber = (UINT8) (Port + 1);
752 } else {
753 Child->Translator = Parent->Translator;
754 }
755 DEBUG (( EFI_D_INFO, "UsbEnumerateNewDev: device uses translator (%d, %d)\n",
756 Child->Translator.TranslatorHubAddress,
757 Child->Translator.TranslatorPortNumber));
758
759 //
760 // After port is reset, hub establishes a signal path between
761 // the device and host (DEFALUT state). Device's registers are
762 // reset, use default address 0 (host enumerates one device at
763 // a time) , and ready to respond to control transfer at EP 0.
764 //
765
766 //
767 // Host assigns an address to the device. Device completes the
768 // status stage with default address, then switches to new address.
769 // ADDRESS state. Address zero is reserved for root hub.
770 //
771 ASSERT (Bus->MaxDevices <= 256);
772 for (Address = 1; Address < Bus->MaxDevices; Address++) {
773 if (Bus->Devices[Address] == NULL) {
774 break;
775 }
776 }
777
778 if (Address >= Bus->MaxDevices) {
779 DEBUG ((EFI_D_ERROR, "UsbEnumerateNewDev: address pool is full for port %d\n", Port));
780
781 Status = EFI_ACCESS_DENIED;
782 goto ON_ERROR;
783 }
784
785 Status = UsbSetAddress (Child, (UINT8)Address);
786 Child->Address = (UINT8)Address;
787 Bus->Devices[Address] = Child;
788
789 if (EFI_ERROR (Status)) {
790 DEBUG ((EFI_D_ERROR, "UsbEnumerateNewDev: failed to set device address - %r\n", Status));
791 goto ON_ERROR;
792 }
793
794 gBS->Stall (USB_SET_DEVICE_ADDRESS_STALL);
795
796 DEBUG ((EFI_D_INFO, "UsbEnumerateNewDev: device is now ADDRESSED at %d\n", Address));
797
798 //
799 // Host sends a Get_Descriptor request to learn the max packet
800 // size of default pipe (only part of the device's descriptor).
801 //
802 Status = UsbGetMaxPacketSize0 (Child);
803
804 if (EFI_ERROR (Status)) {
805 DEBUG ((EFI_D_ERROR, "UsbEnumerateNewDev: failed to get max packet for EP 0 - %r\n", Status));
806 goto ON_ERROR;
807 }
808
809 DEBUG (( EFI_D_INFO, "UsbEnumerateNewDev: max packet size for EP 0 is %d\n", Child->MaxPacket0));
810
811 //
812 // Host learns about the device's abilities by requesting device's
813 // entire descriptions.
814 //
815 Status = UsbBuildDescTable (Child);
816
817 if (EFI_ERROR (Status)) {
818 DEBUG ((EFI_D_ERROR, "UsbEnumerateNewDev: failed to build descriptor table - %r\n", Status));
819 goto ON_ERROR;
820 }
821
822 //
823 // Select a default configuration: UEFI must set the configuration
824 // before the driver can connect to the device.
825 //
826 Config = Child->DevDesc->Configs[0]->Desc.ConfigurationValue;
827 Status = UsbSetConfig (Child, Config);
828
829 if (EFI_ERROR (Status)) {
830 DEBUG ((EFI_D_ERROR, "UsbEnumerateNewDev: failed to set configure %d - %r\n", Config, Status));
831 goto ON_ERROR;
832 }
833
834 DEBUG (( EFI_D_INFO, "UsbEnumerateNewDev: device %d is now in CONFIGED state\n", Address));
835
836 //
837 // Host assigns and loads a device driver.
838 //
839 Status = UsbSelectConfig (Child, Config);
840
841 if (EFI_ERROR (Status)) {
842 DEBUG ((EFI_D_ERROR, "UsbEnumerateNewDev: failed to create interfaces - %r\n", Status));
843 goto ON_ERROR;
844 }
845
846 //
847 // Report Status Code to indicate USB device has been detected by hotplug
848 //
849 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
850 EFI_PROGRESS_CODE,
851 (EFI_IO_BUS_USB | EFI_IOB_PC_HOTPLUG),
852 Bus->DevicePath
853 );
854 return EFI_SUCCESS;
855
856 ON_ERROR:
857 //
858 // If reach here, it means the enumeration process on a given port is interrupted due to error.
859 // The s/w resources, including the assigned address(Address) and the allocated usb device data
860 // structure(Bus->Devices[Address]), will NOT be freed here. These resources will be freed when
861 // the device is unplugged from the port or DriverBindingStop() is invoked.
862 //
863 // This way is used to co-work with the lower layer EDKII UHCI/EHCI/XHCI host controller driver.
864 // It's mainly because to keep UEFI spec unchanged EDKII XHCI driver have to maintain a state machine
865 // to keep track of the mapping between actual address and request address. If the request address
866 // (Address) is freed here, the Address value will be used by next enumerated device. Then EDKII XHCI
867 // host controller driver will have wrong information, which will cause further transaction error.
868 //
869 // EDKII UHCI/EHCI doesn't get impacted as it's make sense to reserve s/w resource till it gets unplugged.
870 //
871 return Status;
872 }
873
874
875 /**
876 Process the events on the port.
877
878 @param HubIf The HUB that has the device connected.
879 @param Port The port index of the hub (started with zero).
880
881 @retval EFI_SUCCESS The device is enumerated (added or removed).
882 @retval EFI_OUT_OF_RESOURCES Failed to allocate resource for the device.
883 @retval Others Failed to enumerate the device.
884
885 **/
886 EFI_STATUS
887 UsbEnumeratePort (
888 IN USB_INTERFACE *HubIf,
889 IN UINT8 Port
890 )
891 {
892 USB_HUB_API *HubApi;
893 USB_DEVICE *Child;
894 EFI_USB_PORT_STATUS PortState;
895 EFI_STATUS Status;
896
897 Child = NULL;
898 HubApi = HubIf->HubApi;
899
900 //
901 // Host learns of the new device by polling the hub for port changes.
902 //
903 Status = HubApi->GetPortStatus (HubIf, Port, &PortState);
904
905 if (EFI_ERROR (Status)) {
906 DEBUG ((EFI_D_ERROR, "UsbEnumeratePort: failed to get state of port %d\n", Port));
907 return Status;
908 }
909
910 //
911 // Only handle connection/enable/overcurrent/reset change.
912 // Usb super speed hub may report other changes, such as warm reset change. Ignore them.
913 //
914 if ((PortState.PortChangeStatus & (USB_PORT_STAT_C_CONNECTION | USB_PORT_STAT_C_ENABLE | USB_PORT_STAT_C_OVERCURRENT | USB_PORT_STAT_C_RESET)) == 0) {
915 return EFI_SUCCESS;
916 }
917
918 DEBUG (( EFI_D_INFO, "UsbEnumeratePort: port %d state - %02x, change - %02x on %p\n",
919 Port, PortState.PortStatus, PortState.PortChangeStatus, HubIf));
920
921 //
922 // This driver only process two kinds of events now: over current and
923 // connect/disconnect. Other three events are: ENABLE, SUSPEND, RESET.
924 // ENABLE/RESET is used to reset port. SUSPEND isn't supported.
925 //
926
927 if (USB_BIT_IS_SET (PortState.PortChangeStatus, USB_PORT_STAT_C_OVERCURRENT)) {
928
929 if (USB_BIT_IS_SET (PortState.PortStatus, USB_PORT_STAT_OVERCURRENT)) {
930 //
931 // Case1:
932 // Both OverCurrent and OverCurrentChange set, means over current occurs,
933 // which probably is caused by short circuit. It has to wait system hardware
934 // to perform recovery.
935 //
936 DEBUG (( EFI_D_ERROR, "UsbEnumeratePort: Critical Over Current\n", Port));
937 return EFI_DEVICE_ERROR;
938
939 }
940 //
941 // Case2:
942 // Only OverCurrentChange set, means system has been recoveried from
943 // over current. As a result, all ports are nearly power-off, so
944 // it's necessary to detach and enumerate all ports again.
945 //
946 DEBUG (( EFI_D_ERROR, "UsbEnumeratePort: 2.0 device Recovery Over Current\n", Port));
947 }
948
949 if (USB_BIT_IS_SET (PortState.PortChangeStatus, USB_PORT_STAT_C_ENABLE)) {
950 //
951 // Case3:
952 // 1.1 roothub port reg doesn't reflect over-current state, while its counterpart
953 // on 2.0 roothub does. When over-current has influence on 1.1 device, the port
954 // would be disabled, so it's also necessary to detach and enumerate again.
955 //
956 DEBUG (( EFI_D_ERROR, "UsbEnumeratePort: 1.1 device Recovery Over Current\n", Port));
957 }
958
959 if (USB_BIT_IS_SET (PortState.PortChangeStatus, USB_PORT_STAT_C_CONNECTION)) {
960 //
961 // Case4:
962 // Device connected or disconnected normally.
963 //
964 DEBUG ((EFI_D_INFO, "UsbEnumeratePort: Device Connect/Disconnect Normally\n", Port));
965 }
966
967 //
968 // Following as the above cases, it's safety to remove and create again.
969 //
970 Child = UsbFindChild (HubIf, Port);
971
972 if (Child != NULL) {
973 DEBUG (( EFI_D_INFO, "UsbEnumeratePort: device at port %d removed from root hub %p\n", Port, HubIf));
974 UsbRemoveDevice (Child);
975 }
976
977 if (USB_BIT_IS_SET (PortState.PortStatus, USB_PORT_STAT_CONNECTION)) {
978 //
979 // Now, new device connected, enumerate and configure the device
980 //
981 DEBUG (( EFI_D_INFO, "UsbEnumeratePort: new device connected at port %d\n", Port));
982 if (USB_BIT_IS_SET (PortState.PortChangeStatus, USB_PORT_STAT_C_RESET)) {
983 Status = UsbEnumerateNewDev (HubIf, Port, FALSE);
984 } else {
985 Status = UsbEnumerateNewDev (HubIf, Port, TRUE);
986 }
987
988 } else {
989 DEBUG (( EFI_D_INFO, "UsbEnumeratePort: device disconnected event on port %d\n", Port));
990 }
991
992 HubApi->ClearPortChange (HubIf, Port);
993 return Status;
994 }
995
996
997 /**
998 Enumerate all the changed hub ports.
999
1000 @param Event The event that is triggered.
1001 @param Context The context to the event.
1002
1003 **/
1004 VOID
1005 EFIAPI
1006 UsbHubEnumeration (
1007 IN EFI_EVENT Event,
1008 IN VOID *Context
1009 )
1010 {
1011 USB_INTERFACE *HubIf;
1012 UINT8 Byte;
1013 UINT8 Bit;
1014 UINT8 Index;
1015 USB_DEVICE *Child;
1016
1017 ASSERT (Context != NULL);
1018
1019 HubIf = (USB_INTERFACE *) Context;
1020
1021 for (Index = 0; Index < HubIf->NumOfPort; Index++) {
1022 Child = UsbFindChild (HubIf, Index);
1023 if ((Child != NULL) && (Child->DisconnectFail == TRUE)) {
1024 DEBUG (( EFI_D_INFO, "UsbEnumeratePort: The device disconnect fails at port %d from hub %p, try again\n", Index, HubIf));
1025 UsbRemoveDevice (Child);
1026 }
1027 }
1028
1029 if (HubIf->ChangeMap == NULL) {
1030 return ;
1031 }
1032
1033 //
1034 // HUB starts its port index with 1.
1035 //
1036 Byte = 0;
1037 Bit = 1;
1038
1039 for (Index = 0; Index < HubIf->NumOfPort; Index++) {
1040 if (USB_BIT_IS_SET (HubIf->ChangeMap[Byte], USB_BIT (Bit))) {
1041 UsbEnumeratePort (HubIf, Index);
1042 }
1043
1044 USB_NEXT_BIT (Byte, Bit);
1045 }
1046
1047 UsbHubAckHubStatus (HubIf->Device);
1048
1049 gBS->FreePool (HubIf->ChangeMap);
1050 HubIf->ChangeMap = NULL;
1051 return ;
1052 }
1053
1054
1055 /**
1056 Enumerate all the changed hub ports.
1057
1058 @param Event The event that is triggered.
1059 @param Context The context to the event.
1060
1061 **/
1062 VOID
1063 EFIAPI
1064 UsbRootHubEnumeration (
1065 IN EFI_EVENT Event,
1066 IN VOID *Context
1067 )
1068 {
1069 USB_INTERFACE *RootHub;
1070 UINT8 Index;
1071 USB_DEVICE *Child;
1072
1073 RootHub = (USB_INTERFACE *) Context;
1074
1075 for (Index = 0; Index < RootHub->NumOfPort; Index++) {
1076 Child = UsbFindChild (RootHub, Index);
1077 if ((Child != NULL) && (Child->DisconnectFail == TRUE)) {
1078 DEBUG (( EFI_D_INFO, "UsbEnumeratePort: The device disconnect fails at port %d from root hub %p, try again\n", Index, RootHub));
1079 UsbRemoveDevice (Child);
1080 }
1081
1082 UsbEnumeratePort (RootHub, Index);
1083 }
1084 }