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