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