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