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