]> git.proxmox.com Git - mirror_edk2.git/blame - 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
CommitLineData
e237e7ae 1/** @file\r
2\r
8616fc4c 3 Usb bus enumeration support.\r
4\r
b659b503 5Copyright (c) 2007 - 2017, Intel Corporation. All rights reserved.<BR>\r
cd5ebaa0 6This program and the accompanying materials\r
e237e7ae 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
e237e7ae 14**/\r
15\r
16#include "UsbBus.h"\r
17\r
e237e7ae 18/**\r
8616fc4c 19 Return the endpoint descriptor in this interface.\r
e237e7ae 20\r
8616fc4c 21 @param UsbIf The interface to search in.\r
22 @param EpAddr The address of the endpoint to return.\r
e237e7ae 23\r
8616fc4c 24 @return The endpoint descriptor or NULL.\r
e237e7ae 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
d17371e8 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
e237e7ae 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
8616fc4c 52 Free the resource used by USB interface.\r
e237e7ae 53\r
8616fc4c 54 @param UsbIf The USB interface to free.\r
e237e7ae 55\r
b659b503
RN
56 @retval EFI_ACCESS_DENIED The interface is still occupied.\r
57 @retval EFI_SUCCESS The interface is freed.\r
e237e7ae 58**/\r
b659b503 59EFI_STATUS\r
e237e7ae 60UsbFreeInterface (\r
61 IN USB_INTERFACE *UsbIf\r
62 )\r
63{\r
b659b503 64 EFI_STATUS Status;\r
e237e7ae 65\r
b659b503 66 UsbCloseHostProtoByChild (UsbIf->Device->Bus, UsbIf->Handle);\r
e237e7ae 67\r
b659b503
RN
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
e237e7ae 81 }\r
b659b503 82 return Status;\r
e237e7ae 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
8616fc4c 90 @param Device The device has the interface descriptor.\r
91 @param IfDesc The interface descriptor.\r
e237e7ae 92\r
93 @return The created USB interface for the descriptor, or NULL.\r
94\r
95**/\r
e237e7ae 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
a1b749d0 116 ASSERT (IfDesc->ActiveIndex < USB_MAX_INTERFACE_SETTING);\r
e237e7ae 117 UsbIf->IfSetting = IfDesc->Settings[IfDesc->ActiveIndex];\r
e61d30b0 118\r
119 CopyMem (\r
120 &(UsbIf->UsbIo),\r
121 &mUsbIoProtocol,\r
122 sizeof (EFI_USB_IO_PROTOCOL)\r
123 );\r
e237e7ae 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
d2577026 141 DEBUG ((EFI_D_ERROR, "UsbCreateInterface: failed to create device path\n"));\r
e237e7ae 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
d2577026 157 DEBUG ((EFI_D_ERROR, "UsbCreateInterface: failed to install UsbIo - %r\n", Status));\r
e237e7ae 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
d2577026 176 DEBUG ((EFI_D_ERROR, "UsbCreateInterface: failed to open host for child - %r\n", Status));\r
e237e7ae 177 goto ON_ERROR;\r
178 }\r
179\r
180 return UsbIf;\r
181\r
182ON_ERROR:\r
8616fc4c 183 if (UsbIf->DevicePath != NULL) {\r
d17371e8 184 FreePool (UsbIf->DevicePath);\r
e237e7ae 185 }\r
186\r
d17371e8 187 FreePool (UsbIf);\r
e237e7ae 188 return NULL;\r
189}\r
190\r
191\r
192/**\r
8616fc4c 193 Free the resource used by this USB device.\r
e237e7ae 194\r
8616fc4c 195 @param Device The USB device to free.\r
e237e7ae 196\r
e237e7ae 197**/\r
e237e7ae 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
8616fc4c 214 @param ParentIf The parent HUB interface.\r
215 @param ParentPort The port on the HUB this device is connected to.\r
e237e7ae 216\r
8616fc4c 217 @return Created USB device, Or NULL.\r
e237e7ae 218\r
219**/\r
e237e7ae 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
ce9b5900 241 Device->Tier = (UINT8)(ParentIf->Device->Tier + 1);\r
e237e7ae 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
d17371e8 248 create a USB interface for each separate interface descriptor.\r
e237e7ae 249\r
8616fc4c 250 @param UsbIf The interface to connect driver to.\r
e237e7ae 251\r
8616fc4c 252 @return EFI_SUCCESS Interface is managed by some driver.\r
253 @return Others Failed to locate a driver for this interface.\r
e237e7ae 254\r
255**/\r
e237e7ae 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
d2577026 271 DEBUG ((EFI_D_INFO, "UsbConnectDriver: found a hub device\n"));\r
e237e7ae 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
ecb575d9 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
a1b749d0 288 DEBUG ((EFI_D_INFO, "UsbConnectDriver: TPL before connect is %d, %p\n", (UINT32)OldTpl, UsbIf->Handle));\r
e237e7ae 289\r
ecb575d9 290 gBS->RestoreTPL (TPL_CALLBACK);\r
e237e7ae 291\r
ecb575d9 292 Status = gBS->ConnectController (UsbIf->Handle, NULL, NULL, TRUE);\r
293 UsbIf->IsManaged = (BOOLEAN)!EFI_ERROR (Status);\r
e237e7ae 294\r
7df7393f 295 DEBUG ((EFI_D_INFO, "UsbConnectDriver: TPL after connect is %d\n", (UINT32)UsbGetCurrentTpl()));\r
ecb575d9 296 ASSERT (UsbGetCurrentTpl () == TPL_CALLBACK);\r
e237e7ae 297\r
ecb575d9 298 gBS->RaiseTPL (OldTpl);\r
299 }\r
e237e7ae 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
8616fc4c 312 @param IfDesc The interface descriptor to set.\r
313 @param Alternate The alternate setting number to locate.\r
e237e7ae 314\r
8616fc4c 315 @retval EFI_NOT_FOUND There is no setting with this alternate index.\r
e237e7ae 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
8d443a16 326 UINTN Index;\r
e237e7ae 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
a1b749d0 334 ASSERT (Index < USB_MAX_INTERFACE_SETTING);\r
e237e7ae 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
cb0b858d 348 ASSERT (Setting != NULL);\r
d2577026 349 DEBUG ((EFI_D_INFO, "UsbSelectSetting: setting %d selected for interface %d\n",\r
e237e7ae 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
8616fc4c 367 @param Device The device to select configuration.\r
368 @param ConfigValue The index of the configuration ( != 0).\r
e237e7ae 369\r
8616fc4c 370 @retval EFI_NOT_FOUND There is no configuration with the index.\r
371 @retval EFI_OUT_OF_RESOURCES Failed to allocate resource.\r
e237e7ae 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
d2577026 408 DEBUG ((EFI_D_INFO, "UsbSelectConfig: config %d selected for device %d\n",\r
e237e7ae 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
4d3d422d 428 Device->NumOfInterface = Index;\r
e237e7ae 429 return EFI_OUT_OF_RESOURCES;\r
430 }\r
431\r
a1b749d0 432 ASSERT (Index < USB_MAX_INTERFACE);\r
e237e7ae 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
d2577026 443 DEBUG ((EFI_D_ERROR, "UsbSelectConfig: failed to connect driver %r, ignored\n", Status));\r
e237e7ae 444 }\r
445 }\r
446\r
447 Device->NumOfInterface = Index;\r
448\r
449 return EFI_SUCCESS;\r
450}\r
451\r
452\r
e237e7ae 453/**\r
454 Disconnect the USB interface with its driver.\r
455\r
8616fc4c 456 @param UsbIf The interface to disconnect driver from.\r
e237e7ae 457\r
e237e7ae 458**/\r
127884c5 459EFI_STATUS\r
e237e7ae 460UsbDisconnectDriver (\r
461 IN USB_INTERFACE *UsbIf\r
462 )\r
463{\r
464 EFI_TPL OldTpl;\r
a1b749d0 465 EFI_STATUS Status;\r
e237e7ae 466\r
467 //\r
468 // Release the hub if it's a hub controller, otherwise\r
469 // disconnect the driver if it is managed by other drivers.\r
470 //\r
127884c5 471 Status = EFI_SUCCESS;\r
e237e7ae 472 if (UsbIf->IsHub) {\r
127884c5 473 Status = UsbIf->HubApi->Release (UsbIf);\r
e237e7ae 474\r
475 } else if (UsbIf->IsManaged) {\r
476 //\r
477 // This function is called in both UsbIoControlTransfer and\r
478 // the timer callback in hub enumeration. So, at least it is\r
479 // called at TPL_CALLBACK. Some driver sitting on USB has\r
480 // twisted TPL used. It should be no problem for us to connect\r
481 // or disconnect at CALLBACK.\r
482 //\r
483 OldTpl = UsbGetCurrentTpl ();\r
a1b749d0 484 DEBUG ((EFI_D_INFO, "UsbDisconnectDriver: old TPL is %d, %p\n", (UINT32)OldTpl, UsbIf->Handle));\r
e237e7ae 485\r
486 gBS->RestoreTPL (TPL_CALLBACK);\r
487\r
a1b749d0 488 Status = gBS->DisconnectController (UsbIf->Handle, NULL, NULL);\r
127884c5
FT
489 if (!EFI_ERROR (Status)) {\r
490 UsbIf->IsManaged = FALSE;\r
491 }\r
492 \r
a1b749d0 493 DEBUG (( EFI_D_INFO, "UsbDisconnectDriver: TPL after disconnect is %d, %d\n", (UINT32)UsbGetCurrentTpl(), Status));\r
e237e7ae 494 ASSERT (UsbGetCurrentTpl () == TPL_CALLBACK);\r
495\r
496 gBS->RaiseTPL (OldTpl);\r
497 }\r
127884c5
FT
498 \r
499 return Status;\r
e237e7ae 500}\r
501\r
502\r
e237e7ae 503/**\r
8616fc4c 504 Remove the current device configuration.\r
e237e7ae 505\r
8616fc4c 506 @param Device The USB device to remove configuration from.\r
e237e7ae 507\r
e237e7ae 508**/\r
127884c5 509EFI_STATUS\r
e237e7ae 510UsbRemoveConfig (\r
511 IN USB_DEVICE *Device\r
512 )\r
513{\r
514 USB_INTERFACE *UsbIf;\r
515 UINTN Index;\r
127884c5
FT
516 EFI_STATUS Status;\r
517 EFI_STATUS ReturnStatus;\r
e237e7ae 518\r
519 //\r
520 // Remove each interface of the device\r
521 //\r
127884c5 522 ReturnStatus = EFI_SUCCESS;\r
a1b749d0 523 for (Index = 0; Index < Device->NumOfInterface; Index++) { \r
524 ASSERT (Index < USB_MAX_INTERFACE);\r
e237e7ae 525 UsbIf = Device->Interfaces[Index];\r
526\r
527 if (UsbIf == NULL) {\r
528 continue;\r
529 }\r
530\r
127884c5
FT
531 Status = UsbDisconnectDriver (UsbIf);\r
532 if (!EFI_ERROR (Status)) {\r
b659b503
RN
533 Status = UsbFreeInterface (UsbIf);\r
534 if (EFI_ERROR (Status)) {\r
535 UsbConnectDriver (UsbIf);\r
536 }\r
537 }\r
538\r
539 if (!EFI_ERROR (Status)) {\r
127884c5
FT
540 Device->Interfaces[Index] = NULL;\r
541 } else {\r
542 ReturnStatus = Status;\r
543 }\r
e237e7ae 544 }\r
545\r
546 Device->ActiveConfig = NULL;\r
127884c5 547 return ReturnStatus;\r
e237e7ae 548}\r
549\r
550\r
e237e7ae 551/**\r
552 Remove the device and all its children from the bus.\r
553\r
8616fc4c 554 @param Device The device to remove.\r
e237e7ae 555\r
8616fc4c 556 @retval EFI_SUCCESS The device is removed.\r
e237e7ae 557\r
558**/\r
559EFI_STATUS\r
560UsbRemoveDevice (\r
561 IN USB_DEVICE *Device\r
562 )\r
563{\r
564 USB_BUS *Bus;\r
565 USB_DEVICE *Child;\r
566 EFI_STATUS Status;\r
127884c5 567 EFI_STATUS ReturnStatus;\r
92870c98 568 UINTN Index;\r
e237e7ae 569\r
570 Bus = Device->Bus;\r
571\r
572 //\r
573 // Remove all the devices on its downstream ports. Search from devices[1].\r
574 // Devices[0] is the root hub.\r
575 //\r
127884c5 576 ReturnStatus = EFI_SUCCESS;\r
a9292c13 577 for (Index = 1; Index < Bus->MaxDevices; Index++) {\r
e237e7ae 578 Child = Bus->Devices[Index];\r
579\r
580 if ((Child == NULL) || (Child->ParentAddr != Device->Address)) {\r
581 continue;\r
582 }\r
583\r
584 Status = UsbRemoveDevice (Child);\r
585\r
127884c5 586 if (!EFI_ERROR (Status)) {\r
e237e7ae 587 Bus->Devices[Index] = NULL;\r
127884c5
FT
588 } else {\r
589 Bus->Devices[Index]->DisconnectFail = TRUE;\r
590 ReturnStatus = Status;\r
591 DEBUG ((EFI_D_INFO, "UsbRemoveDevice: failed to remove child %p at parent %p\n", Child, Device));\r
e237e7ae 592 }\r
593 }\r
594\r
127884c5
FT
595 if (EFI_ERROR (ReturnStatus)) {\r
596 return ReturnStatus;\r
597 }\r
e237e7ae 598\r
127884c5 599 Status = UsbRemoveConfig (Device);\r
e237e7ae 600\r
127884c5
FT
601 if (!EFI_ERROR (Status)) {\r
602 DEBUG (( EFI_D_INFO, "UsbRemoveDevice: device %d removed\n", Device->Address));\r
e237e7ae 603\r
127884c5
FT
604 ASSERT (Device->Address < Bus->MaxDevices);\r
605 Bus->Devices[Device->Address] = NULL;\r
606 UsbFreeDevice (Device);\r
607 } else {\r
608 Bus->Devices[Device->Address]->DisconnectFail = TRUE;\r
609 }\r
610 return Status;\r
e237e7ae 611}\r
612\r
613\r
614/**\r
8616fc4c 615 Find the child device on the hub's port.\r
e237e7ae 616\r
8616fc4c 617 @param HubIf The hub interface.\r
618 @param Port The port of the hub this child is connected to.\r
e237e7ae 619\r
8616fc4c 620 @return The device on the hub's port, or NULL if there is none.\r
e237e7ae 621\r
622**/\r
e237e7ae 623USB_DEVICE *\r
624UsbFindChild (\r
625 IN USB_INTERFACE *HubIf,\r
626 IN UINT8 Port\r
627 )\r
628{\r
629 USB_DEVICE *Device;\r
630 USB_BUS *Bus;\r
631 UINTN Index;\r
632\r
633 Bus = HubIf->Device->Bus;\r
634\r
635 //\r
636 // Start checking from device 1, device 0 is the root hub\r
637 //\r
a9292c13 638 for (Index = 1; Index < Bus->MaxDevices; Index++) {\r
e237e7ae 639 Device = Bus->Devices[Index];\r
640\r
641 if ((Device != NULL) && (Device->ParentAddr == HubIf->Device->Address) &&\r
642 (Device->ParentPort == Port)) {\r
643\r
644 return Device;\r
645 }\r
646 }\r
647\r
648 return NULL;\r
649}\r
650\r
651\r
e237e7ae 652/**\r
653 Enumerate and configure the new device on the port of this HUB interface.\r
654\r
8616fc4c 655 @param HubIf The HUB that has the device connected.\r
656 @param Port The port index of the hub (started with zero).\r
bf4808d6 657 @param ResetIsNeeded The boolean to control whether skip the reset of the port.\r
e237e7ae 658\r
8616fc4c 659 @retval EFI_SUCCESS The device is enumerated (added or removed).\r
660 @retval EFI_OUT_OF_RESOURCES Failed to allocate resource for the device.\r
661 @retval Others Failed to enumerate the device.\r
e237e7ae 662\r
663**/\r
e237e7ae 664EFI_STATUS\r
665UsbEnumerateNewDev (\r
666 IN USB_INTERFACE *HubIf,\r
bf4808d6
FT
667 IN UINT8 Port,\r
668 IN BOOLEAN ResetIsNeeded\r
e237e7ae 669 )\r
670{\r
671 USB_BUS *Bus;\r
672 USB_HUB_API *HubApi;\r
673 USB_DEVICE *Child;\r
674 USB_DEVICE *Parent;\r
675 EFI_USB_PORT_STATUS PortState;\r
92870c98 676 UINTN Address;\r
e237e7ae 677 UINT8 Config;\r
678 EFI_STATUS Status;\r
679\r
e237e7ae 680 Parent = HubIf->Device;\r
681 Bus = Parent->Bus;\r
a9292c13 682 HubApi = HubIf->HubApi; \r
683 Address = Bus->MaxDevices;\r
684\r
41e8ff27 685 gBS->Stall (USB_WAIT_PORT_STABLE_STALL);\r
686 \r
e237e7ae 687 //\r
688 // Hub resets the device for at least 10 milliseconds.\r
689 // Host learns device speed. If device is of low/full speed\r
690 // and the hub is a EHCI root hub, ResetPort will release\r
691 // the device to its companion UHCI and return an error.\r
692 //\r
bf4808d6
FT
693 if (ResetIsNeeded) {\r
694 Status = HubApi->ResetPort (HubIf, Port);\r
695 if (EFI_ERROR (Status)) {\r
696 DEBUG ((EFI_D_ERROR, "UsbEnumerateNewDev: failed to reset port %d - %r\n", Port, Status));\r
697 \r
698 return Status;\r
699 }\r
700 DEBUG (( EFI_D_INFO, "UsbEnumerateNewDev: hub port %d is reset\n", Port));\r
701 } else {\r
702 DEBUG (( EFI_D_INFO, "UsbEnumerateNewDev: hub port %d reset is skipped\n", Port));\r
e237e7ae 703 }\r
704\r
e237e7ae 705 Child = UsbCreateDevice (HubIf, Port);\r
706\r
707 if (Child == NULL) {\r
708 return EFI_OUT_OF_RESOURCES;\r
709 }\r
710\r
711 //\r
712 // OK, now identify the device speed. After reset, hub\r
713 // fully knows the actual device speed.\r
714 //\r
715 Status = HubApi->GetPortStatus (HubIf, Port, &PortState);\r
716\r
717 if (EFI_ERROR (Status)) {\r
d2577026 718 DEBUG ((EFI_D_ERROR, "UsbEnumerateNewDev: failed to get speed of port %d\n", Port));\r
e237e7ae 719 goto ON_ERROR;\r
720 }\r
721\r
92870c98 722 if (!USB_BIT_IS_SET (PortState.PortStatus, USB_PORT_STAT_CONNECTION)) {\r
0ecd7c4a 723 DEBUG ((EFI_D_ERROR, "UsbEnumerateNewDev: No device present at port %d\n", Port));\r
92870c98 724 goto ON_ERROR;\r
725 } else if (USB_BIT_IS_SET (PortState.PortStatus, USB_PORT_STAT_SUPER_SPEED)){\r
726 Child->Speed = EFI_USB_SPEED_SUPER;\r
727 Child->MaxPacket0 = 512;\r
e237e7ae 728 } else if (USB_BIT_IS_SET (PortState.PortStatus, USB_PORT_STAT_HIGH_SPEED)) {\r
92870c98 729 Child->Speed = EFI_USB_SPEED_HIGH;\r
730 Child->MaxPacket0 = 64;\r
731 } else if (USB_BIT_IS_SET (PortState.PortStatus, USB_PORT_STAT_LOW_SPEED)) {\r
732 Child->Speed = EFI_USB_SPEED_LOW;\r
733 Child->MaxPacket0 = 8;\r
e237e7ae 734 } else {\r
92870c98 735 Child->Speed = EFI_USB_SPEED_FULL;\r
736 Child->MaxPacket0 = 8;\r
e237e7ae 737 }\r
738\r
d2577026 739 DEBUG (( EFI_D_INFO, "UsbEnumerateNewDev: device is of %d speed\n", Child->Speed));\r
e237e7ae 740\r
16d718a5 741 if (((Child->Speed == EFI_USB_SPEED_LOW) || (Child->Speed == EFI_USB_SPEED_FULL)) &&\r
742 (Parent->Speed == EFI_USB_SPEED_HIGH)) {\r
e237e7ae 743 //\r
16d718a5 744 // If the child is a low or full speed device, it is necessary to\r
b4c24e2d 745 // set the transaction translator. Port TT is 1-based.\r
746 // This is quite simple:\r
e237e7ae 747 // 1. if parent is of high speed, then parent is our translator\r
748 // 2. otherwise use parent's translator.\r
749 //\r
16d718a5 750 Child->Translator.TranslatorHubAddress = Parent->Address;\r
751 Child->Translator.TranslatorPortNumber = (UINT8) (Port + 1);\r
752 } else {\r
753 Child->Translator = Parent->Translator;\r
e237e7ae 754 }\r
16d718a5 755 DEBUG (( EFI_D_INFO, "UsbEnumerateNewDev: device uses translator (%d, %d)\n",\r
756 Child->Translator.TranslatorHubAddress,\r
757 Child->Translator.TranslatorPortNumber));\r
e237e7ae 758\r
759 //\r
760 // After port is reset, hub establishes a signal path between\r
ac644614 761 // the device and host (DEFALUT state). Device's registers are\r
e237e7ae 762 // reset, use default address 0 (host enumerates one device at\r
763 // a time) , and ready to respond to control transfer at EP 0.\r
764 //\r
765\r
e237e7ae 766 //\r
767 // Host assigns an address to the device. Device completes the\r
768 // status stage with default address, then switches to new address.\r
769 // ADDRESS state. Address zero is reserved for root hub.\r
770 //\r
a9292c13 771 ASSERT (Bus->MaxDevices <= 256);\r
772 for (Address = 1; Address < Bus->MaxDevices; Address++) {\r
e237e7ae 773 if (Bus->Devices[Address] == NULL) {\r
774 break;\r
775 }\r
776 }\r
777\r
a9292c13 778 if (Address >= Bus->MaxDevices) {\r
d2577026 779 DEBUG ((EFI_D_ERROR, "UsbEnumerateNewDev: address pool is full for port %d\n", Port));\r
e237e7ae 780\r
781 Status = EFI_ACCESS_DENIED;\r
782 goto ON_ERROR;\r
783 }\r
784\r
92870c98 785 Status = UsbSetAddress (Child, (UINT8)Address);\r
786 Child->Address = (UINT8)Address;\r
e237e7ae 787 Bus->Devices[Address] = Child;\r
e237e7ae 788\r
789 if (EFI_ERROR (Status)) {\r
d2577026 790 DEBUG ((EFI_D_ERROR, "UsbEnumerateNewDev: failed to set device address - %r\n", Status));\r
e237e7ae 791 goto ON_ERROR;\r
792 }\r
92870c98 793\r
41e8ff27 794 gBS->Stall (USB_SET_DEVICE_ADDRESS_STALL);\r
e237e7ae 795\r
d2577026 796 DEBUG ((EFI_D_INFO, "UsbEnumerateNewDev: device is now ADDRESSED at %d\n", Address));\r
e237e7ae 797\r
92870c98 798 //\r
799 // Host sends a Get_Descriptor request to learn the max packet\r
800 // size of default pipe (only part of the device's descriptor).\r
801 //\r
802 Status = UsbGetMaxPacketSize0 (Child);\r
803\r
804 if (EFI_ERROR (Status)) {\r
805 DEBUG ((EFI_D_ERROR, "UsbEnumerateNewDev: failed to get max packet for EP 0 - %r\n", Status));\r
806 goto ON_ERROR;\r
807 }\r
808\r
809 DEBUG (( EFI_D_INFO, "UsbEnumerateNewDev: max packet size for EP 0 is %d\n", Child->MaxPacket0));\r
810\r
e237e7ae 811 //\r
ac644614 812 // Host learns about the device's abilities by requesting device's\r
e237e7ae 813 // entire descriptions.\r
814 //\r
815 Status = UsbBuildDescTable (Child);\r
816\r
817 if (EFI_ERROR (Status)) {\r
d2577026 818 DEBUG ((EFI_D_ERROR, "UsbEnumerateNewDev: failed to build descriptor table - %r\n", Status));\r
e237e7ae 819 goto ON_ERROR;\r
820 }\r
821\r
822 //\r
823 // Select a default configuration: UEFI must set the configuration\r
824 // before the driver can connect to the device.\r
825 //\r
826 Config = Child->DevDesc->Configs[0]->Desc.ConfigurationValue;\r
827 Status = UsbSetConfig (Child, Config);\r
828\r
829 if (EFI_ERROR (Status)) {\r
d2577026 830 DEBUG ((EFI_D_ERROR, "UsbEnumerateNewDev: failed to set configure %d - %r\n", Config, Status));\r
e237e7ae 831 goto ON_ERROR;\r
832 }\r
833\r
d2577026 834 DEBUG (( EFI_D_INFO, "UsbEnumerateNewDev: device %d is now in CONFIGED state\n", Address));\r
e237e7ae 835\r
836 //\r
837 // Host assigns and loads a device driver.\r
838 //\r
839 Status = UsbSelectConfig (Child, Config);\r
840\r
841 if (EFI_ERROR (Status)) {\r
d2577026 842 DEBUG ((EFI_D_ERROR, "UsbEnumerateNewDev: failed to create interfaces - %r\n", Status));\r
e237e7ae 843 goto ON_ERROR;\r
844 }\r
845\r
37623a5c 846 //\r
847 // Report Status Code to indicate USB device has been detected by hotplug\r
848 //\r
849 REPORT_STATUS_CODE_WITH_DEVICE_PATH (\r
850 EFI_PROGRESS_CODE,\r
851 (EFI_IO_BUS_USB | EFI_IOB_PC_HOTPLUG),\r
852 Bus->DevicePath\r
853 );\r
e237e7ae 854 return EFI_SUCCESS;\r
855\r
856ON_ERROR:\r
7a4d52ad 857 //\r
858 // If reach here, it means the enumeration process on a given port is interrupted due to error.\r
859 // The s/w resources, including the assigned address(Address) and the allocated usb device data\r
860 // structure(Bus->Devices[Address]), will NOT be freed here. These resources will be freed when\r
861 // the device is unplugged from the port or DriverBindingStop() is invoked.\r
862 //\r
863 // This way is used to co-work with the lower layer EDKII UHCI/EHCI/XHCI host controller driver.\r
864 // It's mainly because to keep UEFI spec unchanged EDKII XHCI driver have to maintain a state machine\r
865 // to keep track of the mapping between actual address and request address. If the request address\r
866 // (Address) is freed here, the Address value will be used by next enumerated device. Then EDKII XHCI\r
867 // host controller driver will have wrong information, which will cause further transaction error.\r
868 //\r
869 // EDKII UHCI/EHCI doesn't get impacted as it's make sense to reserve s/w resource till it gets unplugged.\r
870 //\r
e237e7ae 871 return Status;\r
872}\r
873\r
874\r
e237e7ae 875/**\r
876 Process the events on the port.\r
877\r
8616fc4c 878 @param HubIf The HUB that has the device connected.\r
879 @param Port The port index of the hub (started with zero).\r
e237e7ae 880\r
8616fc4c 881 @retval EFI_SUCCESS The device is enumerated (added or removed).\r
882 @retval EFI_OUT_OF_RESOURCES Failed to allocate resource for the device.\r
883 @retval Others Failed to enumerate the device.\r
e237e7ae 884\r
885**/\r
e237e7ae 886EFI_STATUS\r
887UsbEnumeratePort (\r
888 IN USB_INTERFACE *HubIf,\r
889 IN UINT8 Port\r
890 )\r
891{\r
892 USB_HUB_API *HubApi;\r
893 USB_DEVICE *Child;\r
894 EFI_USB_PORT_STATUS PortState;\r
895 EFI_STATUS Status;\r
896\r
897 Child = NULL;\r
898 HubApi = HubIf->HubApi;\r
899\r
900 //\r
901 // Host learns of the new device by polling the hub for port changes.\r
902 //\r
903 Status = HubApi->GetPortStatus (HubIf, Port, &PortState);\r
904\r
905 if (EFI_ERROR (Status)) {\r
d2577026 906 DEBUG ((EFI_D_ERROR, "UsbEnumeratePort: failed to get state of port %d\n", Port));\r
e237e7ae 907 return Status;\r
908 }\r
909\r
92870c98 910 //\r
911 // Only handle connection/enable/overcurrent/reset change.\r
912 // Usb super speed hub may report other changes, such as warm reset change. Ignore them.\r
913 //\r
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) {\r
e237e7ae 915 return EFI_SUCCESS;\r
916 }\r
917\r
92870c98 918 DEBUG (( EFI_D_INFO, "UsbEnumeratePort: port %d state - %02x, change - %02x on %p\n",\r
a50f7c4c 919 Port, PortState.PortStatus, PortState.PortChangeStatus, HubIf));\r
e237e7ae 920\r
921 //\r
922 // This driver only process two kinds of events now: over current and\r
923 // connect/disconnect. Other three events are: ENABLE, SUSPEND, RESET.\r
924 // ENABLE/RESET is used to reset port. SUSPEND isn't supported.\r
925 //\r
50fa1b3a 926 \r
927 if (USB_BIT_IS_SET (PortState.PortChangeStatus, USB_PORT_STAT_C_OVERCURRENT)) { \r
e237e7ae 928\r
50fa1b3a 929 if (USB_BIT_IS_SET (PortState.PortStatus, USB_PORT_STAT_OVERCURRENT)) {\r
930 //\r
41e8ff27 931 // Case1:\r
932 // Both OverCurrent and OverCurrentChange set, means over current occurs, \r
933 // which probably is caused by short circuit. It has to wait system hardware\r
934 // to perform recovery.\r
50fa1b3a 935 //\r
d2577026 936 DEBUG (( EFI_D_ERROR, "UsbEnumeratePort: Critical Over Current\n", Port));\r
50fa1b3a 937 return EFI_DEVICE_ERROR;\r
938 \r
41e8ff27 939 } \r
940 //\r
941 // Case2:\r
942 // Only OverCurrentChange set, means system has been recoveried from \r
943 // over current. As a result, all ports are nearly power-off, so\r
944 // it's necessary to detach and enumerate all ports again. \r
945 //\r
946 DEBUG (( EFI_D_ERROR, "UsbEnumeratePort: 2.0 device Recovery Over Current\n", Port)); \r
50fa1b3a 947 }\r
e237e7ae 948\r
50fa1b3a 949 if (USB_BIT_IS_SET (PortState.PortChangeStatus, USB_PORT_STAT_C_ENABLE)) { \r
e237e7ae 950 //\r
41e8ff27 951 // Case3:\r
952 // 1.1 roothub port reg doesn't reflect over-current state, while its counterpart\r
953 // on 2.0 roothub does. When over-current has influence on 1.1 device, the port \r
954 // would be disabled, so it's also necessary to detach and enumerate again.\r
e237e7ae 955 //\r
d2577026 956 DEBUG (( EFI_D_ERROR, "UsbEnumeratePort: 1.1 device Recovery Over Current\n", Port));\r
50fa1b3a 957 }\r
958 \r
959 if (USB_BIT_IS_SET (PortState.PortChangeStatus, USB_PORT_STAT_C_CONNECTION)) {\r
960 //\r
41e8ff27 961 // Case4:\r
962 // Device connected or disconnected normally. \r
50fa1b3a 963 //\r
9a95972e 964 DEBUG ((EFI_D_INFO, "UsbEnumeratePort: Device Connect/Disconnect Normally\n", Port));\r
50fa1b3a 965 }\r
e237e7ae 966\r
50fa1b3a 967 // \r
41e8ff27 968 // Following as the above cases, it's safety to remove and create again.\r
50fa1b3a 969 //\r
970 Child = UsbFindChild (HubIf, Port);\r
971 \r
972 if (Child != NULL) {\r
a1b749d0 973 DEBUG (( EFI_D_INFO, "UsbEnumeratePort: device at port %d removed from root hub %p\n", Port, HubIf));\r
50fa1b3a 974 UsbRemoveDevice (Child);\r
e237e7ae 975 }\r
50fa1b3a 976 \r
977 if (USB_BIT_IS_SET (PortState.PortStatus, USB_PORT_STAT_CONNECTION)) {\r
978 //\r
979 // Now, new device connected, enumerate and configure the device \r
980 //\r
d2577026 981 DEBUG (( EFI_D_INFO, "UsbEnumeratePort: new device connected at port %d\n", Port));\r
bf4808d6
FT
982 if (USB_BIT_IS_SET (PortState.PortChangeStatus, USB_PORT_STAT_C_RESET)) {\r
983 Status = UsbEnumerateNewDev (HubIf, Port, FALSE);\r
984 } else {\r
985 Status = UsbEnumerateNewDev (HubIf, Port, TRUE);\r
986 }\r
50fa1b3a 987 \r
988 } else {\r
d2577026 989 DEBUG (( EFI_D_INFO, "UsbEnumeratePort: device disconnected event on port %d\n", Port));\r
50fa1b3a 990 }\r
991 \r
e237e7ae 992 HubApi->ClearPortChange (HubIf, Port);\r
993 return Status;\r
994}\r
995\r
996\r
997/**\r
8616fc4c 998 Enumerate all the changed hub ports.\r
e237e7ae 999\r
8616fc4c 1000 @param Event The event that is triggered.\r
1001 @param Context The context to the event.\r
e237e7ae 1002\r
e237e7ae 1003**/\r
1004VOID\r
8616fc4c 1005EFIAPI\r
e237e7ae 1006UsbHubEnumeration (\r
1007 IN EFI_EVENT Event,\r
1008 IN VOID *Context\r
1009 )\r
1010{\r
1011 USB_INTERFACE *HubIf;\r
1012 UINT8 Byte;\r
1013 UINT8 Bit;\r
1014 UINT8 Index;\r
127884c5
FT
1015 USB_DEVICE *Child;\r
1016 \r
ec30be9e 1017 ASSERT (Context != NULL);\r
e237e7ae 1018\r
1019 HubIf = (USB_INTERFACE *) Context;\r
1020\r
127884c5
FT
1021 for (Index = 0; Index < HubIf->NumOfPort; Index++) {\r
1022 Child = UsbFindChild (HubIf, Index);\r
1023 if ((Child != NULL) && (Child->DisconnectFail == TRUE)) {\r
1024 DEBUG (( EFI_D_INFO, "UsbEnumeratePort: The device disconnect fails at port %d from hub %p, try again\n", Index, HubIf));\r
1025 UsbRemoveDevice (Child);\r
1026 }\r
1027 }\r
1028\r
e237e7ae 1029 if (HubIf->ChangeMap == NULL) {\r
1030 return ;\r
1031 }\r
1032\r
1033 //\r
1034 // HUB starts its port index with 1.\r
1035 //\r
1036 Byte = 0;\r
1037 Bit = 1;\r
1038\r
1039 for (Index = 0; Index < HubIf->NumOfPort; Index++) {\r
1040 if (USB_BIT_IS_SET (HubIf->ChangeMap[Byte], USB_BIT (Bit))) {\r
1041 UsbEnumeratePort (HubIf, Index);\r
1042 }\r
1043\r
1044 USB_NEXT_BIT (Byte, Bit);\r
1045 }\r
1046\r
1047 UsbHubAckHubStatus (HubIf->Device);\r
1048\r
1049 gBS->FreePool (HubIf->ChangeMap);\r
1050 HubIf->ChangeMap = NULL;\r
1051 return ;\r
1052}\r
1053\r
1054\r
1055/**\r
8616fc4c 1056 Enumerate all the changed hub ports.\r
e237e7ae 1057\r
8616fc4c 1058 @param Event The event that is triggered.\r
1059 @param Context The context to the event.\r
e237e7ae 1060\r
e237e7ae 1061**/\r
1062VOID\r
eb1f5ab3 1063EFIAPI\r
e237e7ae 1064UsbRootHubEnumeration (\r
1065 IN EFI_EVENT Event,\r
1066 IN VOID *Context\r
1067 )\r
1068{\r
1069 USB_INTERFACE *RootHub;\r
1070 UINT8 Index;\r
127884c5 1071 USB_DEVICE *Child;\r
e237e7ae 1072\r
1073 RootHub = (USB_INTERFACE *) Context;\r
1074\r
1075 for (Index = 0; Index < RootHub->NumOfPort; Index++) {\r
127884c5
FT
1076 Child = UsbFindChild (RootHub, Index);\r
1077 if ((Child != NULL) && (Child->DisconnectFail == TRUE)) {\r
1078 DEBUG (( EFI_D_INFO, "UsbEnumeratePort: The device disconnect fails at port %d from root hub %p, try again\n", Index, RootHub));\r
1079 UsbRemoveDevice (Child);\r
1080 }\r
1081 \r
e237e7ae 1082 UsbEnumeratePort (RootHub, Index);\r
1083 }\r
1084}\r