]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Bus/Usb/UsbBusDxe/UsbEnumer.c
MdeModulePkg/UsbBusDxe: Add UsbControlTransfer() error check
[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
1dac45f5
LE
443 DEBUG ((\r
444 DEBUG_WARN,\r
445 "UsbSelectConfig: failed to connect driver %r, ignored\n",\r
446 Status\r
447 ));\r
e237e7ae 448 }\r
449 }\r
450\r
451 Device->NumOfInterface = Index;\r
452\r
453 return EFI_SUCCESS;\r
454}\r
455\r
456\r
e237e7ae 457/**\r
458 Disconnect the USB interface with its driver.\r
459\r
8616fc4c 460 @param UsbIf The interface to disconnect driver from.\r
e237e7ae 461\r
e237e7ae 462**/\r
127884c5 463EFI_STATUS\r
e237e7ae 464UsbDisconnectDriver (\r
465 IN USB_INTERFACE *UsbIf\r
466 )\r
467{\r
468 EFI_TPL OldTpl;\r
a1b749d0 469 EFI_STATUS Status;\r
e237e7ae 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
127884c5 475 Status = EFI_SUCCESS;\r
e237e7ae 476 if (UsbIf->IsHub) {\r
127884c5 477 Status = UsbIf->HubApi->Release (UsbIf);\r
e237e7ae 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
a1b749d0 488 DEBUG ((EFI_D_INFO, "UsbDisconnectDriver: old TPL is %d, %p\n", (UINT32)OldTpl, UsbIf->Handle));\r
e237e7ae 489\r
490 gBS->RestoreTPL (TPL_CALLBACK);\r
491\r
a1b749d0 492 Status = gBS->DisconnectController (UsbIf->Handle, NULL, NULL);\r
127884c5
FT
493 if (!EFI_ERROR (Status)) {\r
494 UsbIf->IsManaged = FALSE;\r
495 }\r
496 \r
a1b749d0 497 DEBUG (( EFI_D_INFO, "UsbDisconnectDriver: TPL after disconnect is %d, %d\n", (UINT32)UsbGetCurrentTpl(), Status));\r
e237e7ae 498 ASSERT (UsbGetCurrentTpl () == TPL_CALLBACK);\r
499\r
500 gBS->RaiseTPL (OldTpl);\r
501 }\r
127884c5
FT
502 \r
503 return Status;\r
e237e7ae 504}\r
505\r
506\r
e237e7ae 507/**\r
8616fc4c 508 Remove the current device configuration.\r
e237e7ae 509\r
8616fc4c 510 @param Device The USB device to remove configuration from.\r
e237e7ae 511\r
e237e7ae 512**/\r
127884c5 513EFI_STATUS\r
e237e7ae 514UsbRemoveConfig (\r
515 IN USB_DEVICE *Device\r
516 )\r
517{\r
518 USB_INTERFACE *UsbIf;\r
519 UINTN Index;\r
127884c5
FT
520 EFI_STATUS Status;\r
521 EFI_STATUS ReturnStatus;\r
e237e7ae 522\r
523 //\r
524 // Remove each interface of the device\r
525 //\r
127884c5 526 ReturnStatus = EFI_SUCCESS;\r
a1b749d0 527 for (Index = 0; Index < Device->NumOfInterface; Index++) { \r
528 ASSERT (Index < USB_MAX_INTERFACE);\r
e237e7ae 529 UsbIf = Device->Interfaces[Index];\r
530\r
531 if (UsbIf == NULL) {\r
532 continue;\r
533 }\r
534\r
127884c5
FT
535 Status = UsbDisconnectDriver (UsbIf);\r
536 if (!EFI_ERROR (Status)) {\r
b659b503
RN
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
127884c5
FT
544 Device->Interfaces[Index] = NULL;\r
545 } else {\r
546 ReturnStatus = Status;\r
547 }\r
e237e7ae 548 }\r
549\r
550 Device->ActiveConfig = NULL;\r
127884c5 551 return ReturnStatus;\r
e237e7ae 552}\r
553\r
554\r
e237e7ae 555/**\r
556 Remove the device and all its children from the bus.\r
557\r
8616fc4c 558 @param Device The device to remove.\r
e237e7ae 559\r
8616fc4c 560 @retval EFI_SUCCESS The device is removed.\r
e237e7ae 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
127884c5 571 EFI_STATUS ReturnStatus;\r
92870c98 572 UINTN Index;\r
e237e7ae 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
127884c5 580 ReturnStatus = EFI_SUCCESS;\r
a9292c13 581 for (Index = 1; Index < Bus->MaxDevices; Index++) {\r
e237e7ae 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
127884c5 590 if (!EFI_ERROR (Status)) {\r
e237e7ae 591 Bus->Devices[Index] = NULL;\r
127884c5
FT
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
e237e7ae 596 }\r
597 }\r
598\r
127884c5
FT
599 if (EFI_ERROR (ReturnStatus)) {\r
600 return ReturnStatus;\r
601 }\r
e237e7ae 602\r
127884c5 603 Status = UsbRemoveConfig (Device);\r
e237e7ae 604\r
127884c5
FT
605 if (!EFI_ERROR (Status)) {\r
606 DEBUG (( EFI_D_INFO, "UsbRemoveDevice: device %d removed\n", Device->Address));\r
e237e7ae 607\r
127884c5
FT
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
e237e7ae 615}\r
616\r
617\r
618/**\r
8616fc4c 619 Find the child device on the hub's port.\r
e237e7ae 620\r
8616fc4c 621 @param HubIf The hub interface.\r
622 @param Port The port of the hub this child is connected to.\r
e237e7ae 623\r
8616fc4c 624 @return The device on the hub's port, or NULL if there is none.\r
e237e7ae 625\r
626**/\r
e237e7ae 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
a9292c13 642 for (Index = 1; Index < Bus->MaxDevices; Index++) {\r
e237e7ae 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
e237e7ae 656/**\r
657 Enumerate and configure the new device on the port of this HUB interface.\r
658\r
8616fc4c 659 @param HubIf The HUB that has the device connected.\r
660 @param Port The port index of the hub (started with zero).\r
bf4808d6 661 @param ResetIsNeeded The boolean to control whether skip the reset of the port.\r
e237e7ae 662\r
8616fc4c 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
e237e7ae 666\r
667**/\r
e237e7ae 668EFI_STATUS\r
669UsbEnumerateNewDev (\r
670 IN USB_INTERFACE *HubIf,\r
bf4808d6
FT
671 IN UINT8 Port,\r
672 IN BOOLEAN ResetIsNeeded\r
e237e7ae 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
92870c98 680 UINTN Address;\r
e237e7ae 681 UINT8 Config;\r
682 EFI_STATUS Status;\r
683\r
e237e7ae 684 Parent = HubIf->Device;\r
685 Bus = Parent->Bus;\r
a9292c13 686 HubApi = HubIf->HubApi; \r
687 Address = Bus->MaxDevices;\r
688\r
41e8ff27 689 gBS->Stall (USB_WAIT_PORT_STABLE_STALL);\r
690 \r
e237e7ae 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
bf4808d6
FT
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
e237e7ae 707 }\r
708\r
e237e7ae 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
d2577026 722 DEBUG ((EFI_D_ERROR, "UsbEnumerateNewDev: failed to get speed of port %d\n", Port));\r
e237e7ae 723 goto ON_ERROR;\r
724 }\r
725\r
92870c98 726 if (!USB_BIT_IS_SET (PortState.PortStatus, USB_PORT_STAT_CONNECTION)) {\r
0ecd7c4a 727 DEBUG ((EFI_D_ERROR, "UsbEnumerateNewDev: No device present at port %d\n", Port));\r
92870c98 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
e237e7ae 732 } else if (USB_BIT_IS_SET (PortState.PortStatus, USB_PORT_STAT_HIGH_SPEED)) {\r
92870c98 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
e237e7ae 738 } else {\r
92870c98 739 Child->Speed = EFI_USB_SPEED_FULL;\r
740 Child->MaxPacket0 = 8;\r
e237e7ae 741 }\r
742\r
d2577026 743 DEBUG (( EFI_D_INFO, "UsbEnumerateNewDev: device is of %d speed\n", Child->Speed));\r
e237e7ae 744\r
16d718a5 745 if (((Child->Speed == EFI_USB_SPEED_LOW) || (Child->Speed == EFI_USB_SPEED_FULL)) &&\r
746 (Parent->Speed == EFI_USB_SPEED_HIGH)) {\r
e237e7ae 747 //\r
16d718a5 748 // If the child is a low or full speed device, it is necessary to\r
b4c24e2d 749 // set the transaction translator. Port TT is 1-based.\r
750 // This is quite simple:\r
e237e7ae 751 // 1. if parent is of high speed, then parent is our translator\r
752 // 2. otherwise use parent's translator.\r
753 //\r
16d718a5 754 Child->Translator.TranslatorHubAddress = Parent->Address;\r
755 Child->Translator.TranslatorPortNumber = (UINT8) (Port + 1);\r
756 } else {\r
757 Child->Translator = Parent->Translator;\r
e237e7ae 758 }\r
16d718a5 759 DEBUG (( EFI_D_INFO, "UsbEnumerateNewDev: device uses translator (%d, %d)\n",\r
760 Child->Translator.TranslatorHubAddress,\r
761 Child->Translator.TranslatorPortNumber));\r
e237e7ae 762\r
763 //\r
764 // After port is reset, hub establishes a signal path between\r
ac644614 765 // the device and host (DEFALUT state). Device's registers are\r
e237e7ae 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
e237e7ae 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
a9292c13 775 ASSERT (Bus->MaxDevices <= 256);\r
776 for (Address = 1; Address < Bus->MaxDevices; Address++) {\r
e237e7ae 777 if (Bus->Devices[Address] == NULL) {\r
778 break;\r
779 }\r
780 }\r
781\r
a9292c13 782 if (Address >= Bus->MaxDevices) {\r
d2577026 783 DEBUG ((EFI_D_ERROR, "UsbEnumerateNewDev: address pool is full for port %d\n", Port));\r
e237e7ae 784\r
785 Status = EFI_ACCESS_DENIED;\r
786 goto ON_ERROR;\r
787 }\r
788\r
92870c98 789 Status = UsbSetAddress (Child, (UINT8)Address);\r
790 Child->Address = (UINT8)Address;\r
e237e7ae 791 Bus->Devices[Address] = Child;\r
e237e7ae 792\r
793 if (EFI_ERROR (Status)) {\r
d2577026 794 DEBUG ((EFI_D_ERROR, "UsbEnumerateNewDev: failed to set device address - %r\n", Status));\r
e237e7ae 795 goto ON_ERROR;\r
796 }\r
92870c98 797\r
41e8ff27 798 gBS->Stall (USB_SET_DEVICE_ADDRESS_STALL);\r
e237e7ae 799\r
d2577026 800 DEBUG ((EFI_D_INFO, "UsbEnumerateNewDev: device is now ADDRESSED at %d\n", Address));\r
e237e7ae 801\r
92870c98 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
e237e7ae 815 //\r
ac644614 816 // Host learns about the device's abilities by requesting device's\r
e237e7ae 817 // entire descriptions.\r
818 //\r
819 Status = UsbBuildDescTable (Child);\r
820\r
821 if (EFI_ERROR (Status)) {\r
d2577026 822 DEBUG ((EFI_D_ERROR, "UsbEnumerateNewDev: failed to build descriptor table - %r\n", Status));\r
e237e7ae 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
d2577026 834 DEBUG ((EFI_D_ERROR, "UsbEnumerateNewDev: failed to set configure %d - %r\n", Config, Status));\r
e237e7ae 835 goto ON_ERROR;\r
836 }\r
837\r
d2577026 838 DEBUG (( EFI_D_INFO, "UsbEnumerateNewDev: device %d is now in CONFIGED state\n", Address));\r
e237e7ae 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
d2577026 846 DEBUG ((EFI_D_ERROR, "UsbEnumerateNewDev: failed to create interfaces - %r\n", Status));\r
e237e7ae 847 goto ON_ERROR;\r
848 }\r
849\r
37623a5c 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
e237e7ae 858 return EFI_SUCCESS;\r
859\r
860ON_ERROR:\r
7a4d52ad 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
e237e7ae 875 return Status;\r
876}\r
877\r
878\r
e237e7ae 879/**\r
880 Process the events on the port.\r
881\r
8616fc4c 882 @param HubIf The HUB that has the device connected.\r
883 @param Port The port index of the hub (started with zero).\r
e237e7ae 884\r
8616fc4c 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
e237e7ae 888\r
889**/\r
e237e7ae 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
d2577026 910 DEBUG ((EFI_D_ERROR, "UsbEnumeratePort: failed to get state of port %d\n", Port));\r
e237e7ae 911 return Status;\r
912 }\r
913\r
92870c98 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
e237e7ae 919 return EFI_SUCCESS;\r
920 }\r
921\r
92870c98 922 DEBUG (( EFI_D_INFO, "UsbEnumeratePort: port %d state - %02x, change - %02x on %p\n",\r
a50f7c4c 923 Port, PortState.PortStatus, PortState.PortChangeStatus, HubIf));\r
e237e7ae 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
50fa1b3a 930 \r
931 if (USB_BIT_IS_SET (PortState.PortChangeStatus, USB_PORT_STAT_C_OVERCURRENT)) { \r
e237e7ae 932\r
50fa1b3a 933 if (USB_BIT_IS_SET (PortState.PortStatus, USB_PORT_STAT_OVERCURRENT)) {\r
934 //\r
41e8ff27 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
50fa1b3a 939 //\r
d2577026 940 DEBUG (( EFI_D_ERROR, "UsbEnumeratePort: Critical Over Current\n", Port));\r
50fa1b3a 941 return EFI_DEVICE_ERROR;\r
942 \r
41e8ff27 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
50fa1b3a 951 }\r
e237e7ae 952\r
50fa1b3a 953 if (USB_BIT_IS_SET (PortState.PortChangeStatus, USB_PORT_STAT_C_ENABLE)) { \r
e237e7ae 954 //\r
41e8ff27 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
e237e7ae 959 //\r
d2577026 960 DEBUG (( EFI_D_ERROR, "UsbEnumeratePort: 1.1 device Recovery Over Current\n", Port));\r
50fa1b3a 961 }\r
962 \r
963 if (USB_BIT_IS_SET (PortState.PortChangeStatus, USB_PORT_STAT_C_CONNECTION)) {\r
964 //\r
41e8ff27 965 // Case4:\r
966 // Device connected or disconnected normally. \r
50fa1b3a 967 //\r
9a95972e 968 DEBUG ((EFI_D_INFO, "UsbEnumeratePort: Device Connect/Disconnect Normally\n", Port));\r
50fa1b3a 969 }\r
e237e7ae 970\r
50fa1b3a 971 // \r
41e8ff27 972 // Following as the above cases, it's safety to remove and create again.\r
50fa1b3a 973 //\r
974 Child = UsbFindChild (HubIf, Port);\r
975 \r
976 if (Child != NULL) {\r
a1b749d0 977 DEBUG (( EFI_D_INFO, "UsbEnumeratePort: device at port %d removed from root hub %p\n", Port, HubIf));\r
50fa1b3a 978 UsbRemoveDevice (Child);\r
e237e7ae 979 }\r
50fa1b3a 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
d2577026 985 DEBUG (( EFI_D_INFO, "UsbEnumeratePort: new device connected at port %d\n", Port));\r
bf4808d6
FT
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
50fa1b3a 991 \r
992 } else {\r
d2577026 993 DEBUG (( EFI_D_INFO, "UsbEnumeratePort: device disconnected event on port %d\n", Port));\r
50fa1b3a 994 }\r
995 \r
e237e7ae 996 HubApi->ClearPortChange (HubIf, Port);\r
997 return Status;\r
998}\r
999\r
1000\r
1001/**\r
8616fc4c 1002 Enumerate all the changed hub ports.\r
e237e7ae 1003\r
8616fc4c 1004 @param Event The event that is triggered.\r
1005 @param Context The context to the event.\r
e237e7ae 1006\r
e237e7ae 1007**/\r
1008VOID\r
8616fc4c 1009EFIAPI\r
e237e7ae 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
127884c5
FT
1019 USB_DEVICE *Child;\r
1020 \r
ec30be9e 1021 ASSERT (Context != NULL);\r
e237e7ae 1022\r
1023 HubIf = (USB_INTERFACE *) Context;\r
1024\r
127884c5
FT
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
e237e7ae 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
8616fc4c 1060 Enumerate all the changed hub ports.\r
e237e7ae 1061\r
8616fc4c 1062 @param Event The event that is triggered.\r
1063 @param Context The context to the event.\r
e237e7ae 1064\r
e237e7ae 1065**/\r
1066VOID\r
eb1f5ab3 1067EFIAPI\r
e237e7ae 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
127884c5 1075 USB_DEVICE *Child;\r
e237e7ae 1076\r
1077 RootHub = (USB_INTERFACE *) Context;\r
1078\r
1079 for (Index = 0; Index < RootHub->NumOfPort; Index++) {\r
127884c5
FT
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
e237e7ae 1086 UsbEnumeratePort (RootHub, Index);\r
1087 }\r
1088}\r