]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Bus/Usb/UsbBusDxe/UsbEnumer.c
fixed comments missing.
[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
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
236 return Device;\r
237}\r
238\r
239\r
240/**\r
241 Connect the USB interface with its driver. EFI USB bus will\r
d17371e8 242 create a USB interface for each separate interface descriptor.\r
e237e7ae 243\r
8616fc4c 244 @param UsbIf The interface to connect driver to.\r
e237e7ae 245\r
8616fc4c 246 @return EFI_SUCCESS Interface is managed by some driver.\r
247 @return Others Failed to locate a driver for this interface.\r
e237e7ae 248\r
249**/\r
e237e7ae 250EFI_STATUS\r
251UsbConnectDriver (\r
252 IN USB_INTERFACE *UsbIf\r
253 )\r
254{\r
255 EFI_STATUS Status;\r
256 EFI_TPL OldTpl;\r
257\r
258 Status = EFI_SUCCESS;\r
259\r
260 //\r
261 // Hub is maintained by the USB bus driver. Otherwise try to\r
262 // connect drivers with this interface\r
263 //\r
264 if (UsbIsHubInterface (UsbIf)) {\r
d2577026 265 DEBUG ((EFI_D_INFO, "UsbConnectDriver: found a hub device\n"));\r
e237e7ae 266 Status = mUsbHubApi.Init (UsbIf);\r
267\r
268 } else {\r
269 //\r
270 // This function is called in both UsbIoControlTransfer and\r
271 // the timer callback in hub enumeration. So, at least it is\r
272 // called at TPL_CALLBACK. Some driver sitting on USB has\r
273 // twisted TPL used. It should be no problem for us to connect\r
274 // or disconnect at CALLBACK.\r
275 //\r
ecb575d9 276 \r
277 //\r
278 // Only recursively wanted usb child device\r
279 //\r
280 if (UsbBusIsWantedUsbIO (UsbIf->Device->Bus, UsbIf)) {\r
281 OldTpl = UsbGetCurrentTpl ();\r
7df7393f 282 DEBUG ((EFI_D_INFO, "UsbConnectDriver: TPL before connect is %d\n", (UINT32)OldTpl));\r
e237e7ae 283\r
ecb575d9 284 gBS->RestoreTPL (TPL_CALLBACK);\r
e237e7ae 285\r
ecb575d9 286 Status = gBS->ConnectController (UsbIf->Handle, NULL, NULL, TRUE);\r
287 UsbIf->IsManaged = (BOOLEAN)!EFI_ERROR (Status);\r
e237e7ae 288\r
7df7393f 289 DEBUG ((EFI_D_INFO, "UsbConnectDriver: TPL after connect is %d\n", (UINT32)UsbGetCurrentTpl()));\r
ecb575d9 290 ASSERT (UsbGetCurrentTpl () == TPL_CALLBACK);\r
e237e7ae 291\r
ecb575d9 292 gBS->RaiseTPL (OldTpl);\r
293 }\r
e237e7ae 294 }\r
295\r
296 return Status;\r
297}\r
298\r
299\r
300/**\r
301 Select an alternate setting for the interface.\r
302 Each interface can have several mutually exclusive\r
303 settings. Only one setting is active. It will\r
304 also reset its endpoints' toggle to zero.\r
305\r
8616fc4c 306 @param IfDesc The interface descriptor to set.\r
307 @param Alternate The alternate setting number to locate.\r
e237e7ae 308\r
8616fc4c 309 @retval EFI_NOT_FOUND There is no setting with this alternate index.\r
e237e7ae 310 @retval EFI_SUCCESS The interface is set to Alternate setting.\r
311\r
312**/\r
313EFI_STATUS\r
314UsbSelectSetting (\r
315 IN USB_INTERFACE_DESC *IfDesc,\r
316 IN UINT8 Alternate\r
317 )\r
318{\r
319 USB_INTERFACE_SETTING *Setting;\r
320 UINT8 Index;\r
321\r
322 //\r
323 // Locate the active alternate setting\r
324 //\r
325 Setting = NULL;\r
326\r
327 for (Index = 0; Index < IfDesc->NumOfSetting; Index++) {\r
328 Setting = IfDesc->Settings[Index];\r
329\r
330 if (Setting->Desc.AlternateSetting == Alternate) {\r
331 break;\r
332 }\r
333 }\r
334\r
335 if (Index == IfDesc->NumOfSetting) {\r
336 return EFI_NOT_FOUND;\r
337 }\r
338\r
339 IfDesc->ActiveIndex = Index;\r
340\r
d2577026 341 DEBUG ((EFI_D_INFO, "UsbSelectSetting: setting %d selected for interface %d\n",\r
e237e7ae 342 Alternate, Setting->Desc.InterfaceNumber));\r
343\r
344 //\r
345 // Reset the endpoint toggle to zero\r
346 //\r
347 for (Index = 0; Index < Setting->Desc.NumEndpoints; Index++) {\r
348 Setting->Endpoints[Index]->Toggle = 0;\r
349 }\r
350\r
351 return EFI_SUCCESS;\r
352}\r
353\r
354\r
355/**\r
356 Select a new configuration for the device. Each\r
357 device may support several configurations.\r
358\r
8616fc4c 359 @param Device The device to select configuration.\r
360 @param ConfigValue The index of the configuration ( != 0).\r
e237e7ae 361\r
8616fc4c 362 @retval EFI_NOT_FOUND There is no configuration with the index.\r
363 @retval EFI_OUT_OF_RESOURCES Failed to allocate resource.\r
e237e7ae 364 @retval EFI_SUCCESS The configuration is selected.\r
365\r
366**/\r
367EFI_STATUS\r
368UsbSelectConfig (\r
369 IN USB_DEVICE *Device,\r
370 IN UINT8 ConfigValue\r
371 )\r
372{\r
373 USB_DEVICE_DESC *DevDesc;\r
374 USB_CONFIG_DESC *ConfigDesc;\r
375 USB_INTERFACE_DESC *IfDesc;\r
376 USB_INTERFACE *UsbIf;\r
377 EFI_STATUS Status;\r
378 UINT8 Index;\r
379\r
380 //\r
381 // Locate the active config, then set the device's pointer\r
382 //\r
383 DevDesc = Device->DevDesc;\r
384 ConfigDesc = NULL;\r
385\r
386 for (Index = 0; Index < DevDesc->Desc.NumConfigurations; Index++) {\r
387 ConfigDesc = DevDesc->Configs[Index];\r
388\r
389 if (ConfigDesc->Desc.ConfigurationValue == ConfigValue) {\r
390 break;\r
391 }\r
392 }\r
393\r
394 if (Index == DevDesc->Desc.NumConfigurations) {\r
395 return EFI_NOT_FOUND;\r
396 }\r
397\r
398 Device->ActiveConfig = ConfigDesc;\r
399\r
d2577026 400 DEBUG ((EFI_D_INFO, "UsbSelectConfig: config %d selected for device %d\n",\r
e237e7ae 401 ConfigValue, Device->Address));\r
402\r
403 //\r
404 // Create interfaces for each USB interface descriptor.\r
405 //\r
406 for (Index = 0; Index < ConfigDesc->Desc.NumInterfaces; Index++) {\r
407 //\r
408 // First select the default interface setting, and reset\r
409 // the endpoint toggles to zero for its endpoints.\r
410 //\r
411 IfDesc = ConfigDesc->Interfaces[Index];\r
412 UsbSelectSetting (IfDesc, IfDesc->Settings[0]->Desc.AlternateSetting);\r
413\r
414 //\r
415 // Create a USB_INTERFACE and install USB_IO and other protocols\r
416 //\r
417 UsbIf = UsbCreateInterface (Device, ConfigDesc->Interfaces[Index]);\r
418\r
419 if (UsbIf == NULL) {\r
420 return EFI_OUT_OF_RESOURCES;\r
421 }\r
422\r
423 Device->Interfaces[Index] = UsbIf;\r
424\r
425 //\r
426 // Connect the device to drivers, if it failed, ignore\r
427 // the error. Don't let the unsupported interfaces to block\r
428 // the supported interfaces.\r
429 //\r
430 Status = UsbConnectDriver (UsbIf);\r
431\r
432 if (EFI_ERROR (Status)) {\r
d2577026 433 DEBUG ((EFI_D_ERROR, "UsbSelectConfig: failed to connect driver %r, ignored\n", Status));\r
e237e7ae 434 }\r
435 }\r
436\r
437 Device->NumOfInterface = Index;\r
438\r
439 return EFI_SUCCESS;\r
440}\r
441\r
442\r
e237e7ae 443/**\r
444 Disconnect the USB interface with its driver.\r
445\r
8616fc4c 446 @param UsbIf The interface to disconnect driver from.\r
e237e7ae 447\r
e237e7ae 448**/\r
e237e7ae 449VOID\r
450UsbDisconnectDriver (\r
451 IN USB_INTERFACE *UsbIf\r
452 )\r
453{\r
454 EFI_TPL OldTpl;\r
455\r
456 //\r
457 // Release the hub if it's a hub controller, otherwise\r
458 // disconnect the driver if it is managed by other drivers.\r
459 //\r
460 if (UsbIf->IsHub) {\r
461 UsbIf->HubApi->Release (UsbIf);\r
462\r
463 } else if (UsbIf->IsManaged) {\r
464 //\r
465 // This function is called in both UsbIoControlTransfer and\r
466 // the timer callback in hub enumeration. So, at least it is\r
467 // called at TPL_CALLBACK. Some driver sitting on USB has\r
468 // twisted TPL used. It should be no problem for us to connect\r
469 // or disconnect at CALLBACK.\r
470 //\r
471 OldTpl = UsbGetCurrentTpl ();\r
7df7393f 472 DEBUG ((EFI_D_INFO, "UsbDisconnectDriver: old TPL is %d\n", (UINT32)OldTpl));\r
e237e7ae 473\r
474 gBS->RestoreTPL (TPL_CALLBACK);\r
475\r
476 gBS->DisconnectController (UsbIf->Handle, NULL, NULL);\r
477 UsbIf->IsManaged = FALSE;\r
478\r
7df7393f 479 DEBUG (( EFI_D_INFO, "UsbDisconnectDriver: TPL after disconnect is %d\n", (UINT32)UsbGetCurrentTpl()));\r
e237e7ae 480 ASSERT (UsbGetCurrentTpl () == TPL_CALLBACK);\r
481\r
482 gBS->RaiseTPL (OldTpl);\r
483 }\r
484}\r
485\r
486\r
e237e7ae 487/**\r
8616fc4c 488 Remove the current device configuration.\r
e237e7ae 489\r
8616fc4c 490 @param Device The USB device to remove configuration from.\r
e237e7ae 491\r
e237e7ae 492**/\r
493VOID\r
494UsbRemoveConfig (\r
495 IN USB_DEVICE *Device\r
496 )\r
497{\r
498 USB_INTERFACE *UsbIf;\r
499 UINTN Index;\r
500\r
501 //\r
502 // Remove each interface of the device\r
503 //\r
504 for (Index = 0; Index < Device->NumOfInterface; Index++) {\r
505 UsbIf = Device->Interfaces[Index];\r
506\r
507 if (UsbIf == NULL) {\r
508 continue;\r
509 }\r
510\r
511 UsbDisconnectDriver (UsbIf);\r
512 UsbFreeInterface (UsbIf);\r
513 Device->Interfaces[Index] = NULL;\r
514 }\r
515\r
516 Device->ActiveConfig = NULL;\r
517 Device->NumOfInterface = 0;\r
518}\r
519\r
520\r
e237e7ae 521/**\r
522 Remove the device and all its children from the bus.\r
523\r
8616fc4c 524 @param Device The device to remove.\r
e237e7ae 525\r
8616fc4c 526 @retval EFI_SUCCESS The device is removed.\r
e237e7ae 527\r
528**/\r
529EFI_STATUS\r
530UsbRemoveDevice (\r
531 IN USB_DEVICE *Device\r
532 )\r
533{\r
534 USB_BUS *Bus;\r
535 USB_DEVICE *Child;\r
536 EFI_STATUS Status;\r
537 UINT8 Index;\r
538\r
539 Bus = Device->Bus;\r
540\r
541 //\r
542 // Remove all the devices on its downstream ports. Search from devices[1].\r
543 // Devices[0] is the root hub.\r
544 //\r
545 for (Index = 1; Index < USB_MAX_DEVICES; Index++) {\r
546 Child = Bus->Devices[Index];\r
547\r
548 if ((Child == NULL) || (Child->ParentAddr != Device->Address)) {\r
549 continue;\r
550 }\r
551\r
552 Status = UsbRemoveDevice (Child);\r
553\r
554 if (EFI_ERROR (Status)) {\r
d2577026 555 DEBUG ((EFI_D_ERROR, "UsbRemoveDevice: failed to remove child, ignore error\n"));\r
e237e7ae 556 Bus->Devices[Index] = NULL;\r
557 }\r
558 }\r
559\r
560 UsbRemoveConfig (Device);\r
561\r
d2577026 562 DEBUG (( EFI_D_INFO, "UsbRemoveDevice: device %d removed\n", Device->Address));\r
e237e7ae 563\r
564 Bus->Devices[Device->Address] = NULL;\r
565 UsbFreeDevice (Device);\r
566\r
567 return EFI_SUCCESS;\r
568}\r
569\r
570\r
571/**\r
8616fc4c 572 Find the child device on the hub's port.\r
e237e7ae 573\r
8616fc4c 574 @param HubIf The hub interface.\r
575 @param Port The port of the hub this child is connected to.\r
e237e7ae 576\r
8616fc4c 577 @return The device on the hub's port, or NULL if there is none.\r
e237e7ae 578\r
579**/\r
e237e7ae 580USB_DEVICE *\r
581UsbFindChild (\r
582 IN USB_INTERFACE *HubIf,\r
583 IN UINT8 Port\r
584 )\r
585{\r
586 USB_DEVICE *Device;\r
587 USB_BUS *Bus;\r
588 UINTN Index;\r
589\r
590 Bus = HubIf->Device->Bus;\r
591\r
592 //\r
593 // Start checking from device 1, device 0 is the root hub\r
594 //\r
595 for (Index = 1; Index < USB_MAX_DEVICES; Index++) {\r
596 Device = Bus->Devices[Index];\r
597\r
598 if ((Device != NULL) && (Device->ParentAddr == HubIf->Device->Address) &&\r
599 (Device->ParentPort == Port)) {\r
600\r
601 return Device;\r
602 }\r
603 }\r
604\r
605 return NULL;\r
606}\r
607\r
608\r
e237e7ae 609/**\r
610 Enumerate and configure the new device on the port of this HUB interface.\r
611\r
8616fc4c 612 @param HubIf The HUB that has the device connected.\r
613 @param Port The port index of the hub (started with zero).\r
e237e7ae 614\r
8616fc4c 615 @retval EFI_SUCCESS The device is enumerated (added or removed).\r
616 @retval EFI_OUT_OF_RESOURCES Failed to allocate resource for the device.\r
617 @retval Others Failed to enumerate the device.\r
e237e7ae 618\r
619**/\r
e237e7ae 620EFI_STATUS\r
621UsbEnumerateNewDev (\r
622 IN USB_INTERFACE *HubIf,\r
623 IN UINT8 Port\r
624 )\r
625{\r
626 USB_BUS *Bus;\r
627 USB_HUB_API *HubApi;\r
628 USB_DEVICE *Child;\r
629 USB_DEVICE *Parent;\r
630 EFI_USB_PORT_STATUS PortState;\r
631 UINT8 Address;\r
632 UINT8 Config;\r
633 EFI_STATUS Status;\r
634\r
635 Address = USB_MAX_DEVICES;\r
636 Parent = HubIf->Device;\r
637 Bus = Parent->Bus;\r
638 HubApi = HubIf->HubApi;\r
41e8ff27 639 \r
640 gBS->Stall (USB_WAIT_PORT_STABLE_STALL);\r
641 \r
e237e7ae 642 //\r
643 // Hub resets the device for at least 10 milliseconds.\r
644 // Host learns device speed. If device is of low/full speed\r
645 // and the hub is a EHCI root hub, ResetPort will release\r
646 // the device to its companion UHCI and return an error.\r
647 //\r
648 Status = HubApi->ResetPort (HubIf, Port);\r
649\r
650 if (EFI_ERROR (Status)) {\r
d2577026 651 DEBUG ((EFI_D_ERROR, "UsbEnumerateNewDev: failed to reset port %d - %r\n", Port, Status));\r
e237e7ae 652\r
653 return Status;\r
654 }\r
655\r
d2577026 656 DEBUG (( EFI_D_INFO, "UsbEnumerateNewDev: hub port %d is reset\n", Port));\r
e237e7ae 657\r
658 Child = UsbCreateDevice (HubIf, Port);\r
659\r
660 if (Child == NULL) {\r
661 return EFI_OUT_OF_RESOURCES;\r
662 }\r
663\r
664 //\r
665 // OK, now identify the device speed. After reset, hub\r
666 // fully knows the actual device speed.\r
667 //\r
668 Status = HubApi->GetPortStatus (HubIf, Port, &PortState);\r
669\r
670 if (EFI_ERROR (Status)) {\r
d2577026 671 DEBUG ((EFI_D_ERROR, "UsbEnumerateNewDev: failed to get speed of port %d\n", Port));\r
e237e7ae 672 goto ON_ERROR;\r
673 }\r
674\r
675 if (USB_BIT_IS_SET (PortState.PortStatus, USB_PORT_STAT_LOW_SPEED)) {\r
676 Child->Speed = EFI_USB_SPEED_LOW;\r
677\r
678 } else if (USB_BIT_IS_SET (PortState.PortStatus, USB_PORT_STAT_HIGH_SPEED)) {\r
679 Child->Speed = EFI_USB_SPEED_HIGH;\r
680\r
681 } else {\r
682 Child->Speed = EFI_USB_SPEED_FULL;\r
683 }\r
684\r
d2577026 685 DEBUG (( EFI_D_INFO, "UsbEnumerateNewDev: device is of %d speed\n", Child->Speed));\r
e237e7ae 686\r
687 if (Child->Speed != EFI_USB_SPEED_HIGH) {\r
688 //\r
689 // If the child isn't a high speed device, it is necessary to\r
b4c24e2d 690 // set the transaction translator. Port TT is 1-based.\r
691 // This is quite simple:\r
e237e7ae 692 // 1. if parent is of high speed, then parent is our translator\r
693 // 2. otherwise use parent's translator.\r
694 //\r
695 if (Parent->Speed == EFI_USB_SPEED_HIGH) {\r
696 Child->Translator.TranslatorHubAddress = Parent->Address;\r
faff3b47 697 Child->Translator.TranslatorPortNumber = (UINT8) (Port + 1);\r
e237e7ae 698\r
699 } else {\r
700 Child->Translator = Parent->Translator;\r
701 }\r
702\r
d2577026 703 DEBUG (( EFI_D_INFO, "UsbEnumerateNewDev: device uses translator (%d, %d)\n",\r
e237e7ae 704 Child->Translator.TranslatorHubAddress,\r
705 Child->Translator.TranslatorPortNumber));\r
706 }\r
707\r
708 //\r
709 // After port is reset, hub establishes a signal path between\r
ac644614 710 // the device and host (DEFALUT state). Device's registers are\r
e237e7ae 711 // reset, use default address 0 (host enumerates one device at\r
712 // a time) , and ready to respond to control transfer at EP 0.\r
713 //\r
714\r
715 //\r
716 // Host sends a Get_Descriptor request to learn the max packet\r
ac644614 717 // size of default pipe (only part of the device's descriptor).\r
e237e7ae 718 //\r
719 Status = UsbGetMaxPacketSize0 (Child);\r
720\r
721 if (EFI_ERROR (Status)) {\r
d2577026 722 DEBUG ((EFI_D_ERROR, "UsbEnumerateNewDev: failed to get max packet for EP 0 - %r\n", Status));\r
e237e7ae 723 goto ON_ERROR;\r
724 }\r
725\r
d2577026 726 DEBUG (( EFI_D_INFO, "UsbEnumerateNewDev: max packet size for EP 0 is %d\n", Child->MaxPacket0));\r
e237e7ae 727\r
728 //\r
729 // Host assigns an address to the device. Device completes the\r
730 // status stage with default address, then switches to new address.\r
731 // ADDRESS state. Address zero is reserved for root hub.\r
732 //\r
733 for (Address = 1; Address < USB_MAX_DEVICES; Address++) {\r
734 if (Bus->Devices[Address] == NULL) {\r
735 break;\r
736 }\r
737 }\r
738\r
739 if (Address == USB_MAX_DEVICES) {\r
d2577026 740 DEBUG ((EFI_D_ERROR, "UsbEnumerateNewDev: address pool is full for port %d\n", Port));\r
e237e7ae 741\r
742 Status = EFI_ACCESS_DENIED;\r
743 goto ON_ERROR;\r
744 }\r
745\r
746 Bus->Devices[Address] = Child;\r
747 Status = UsbSetAddress (Child, Address);\r
748 Child->Address = Address;\r
749\r
750 if (EFI_ERROR (Status)) {\r
d2577026 751 DEBUG ((EFI_D_ERROR, "UsbEnumerateNewDev: failed to set device address - %r\n", Status));\r
e237e7ae 752 goto ON_ERROR;\r
753 }\r
41e8ff27 754 \r
755 gBS->Stall (USB_SET_DEVICE_ADDRESS_STALL);\r
e237e7ae 756\r
d2577026 757 DEBUG ((EFI_D_INFO, "UsbEnumerateNewDev: device is now ADDRESSED at %d\n", Address));\r
e237e7ae 758\r
759 //\r
ac644614 760 // Host learns about the device's abilities by requesting device's\r
e237e7ae 761 // entire descriptions.\r
762 //\r
763 Status = UsbBuildDescTable (Child);\r
764\r
765 if (EFI_ERROR (Status)) {\r
d2577026 766 DEBUG ((EFI_D_ERROR, "UsbEnumerateNewDev: failed to build descriptor table - %r\n", Status));\r
e237e7ae 767 goto ON_ERROR;\r
768 }\r
769\r
770 //\r
771 // Select a default configuration: UEFI must set the configuration\r
772 // before the driver can connect to the device.\r
773 //\r
774 Config = Child->DevDesc->Configs[0]->Desc.ConfigurationValue;\r
775 Status = UsbSetConfig (Child, Config);\r
776\r
777 if (EFI_ERROR (Status)) {\r
d2577026 778 DEBUG ((EFI_D_ERROR, "UsbEnumerateNewDev: failed to set configure %d - %r\n", Config, Status));\r
e237e7ae 779 goto ON_ERROR;\r
780 }\r
781\r
d2577026 782 DEBUG (( EFI_D_INFO, "UsbEnumerateNewDev: device %d is now in CONFIGED state\n", Address));\r
e237e7ae 783\r
784 //\r
785 // Host assigns and loads a device driver.\r
786 //\r
787 Status = UsbSelectConfig (Child, Config);\r
788\r
789 if (EFI_ERROR (Status)) {\r
d2577026 790 DEBUG ((EFI_D_ERROR, "UsbEnumerateNewDev: failed to create interfaces - %r\n", Status));\r
e237e7ae 791 goto ON_ERROR;\r
792 }\r
793\r
794 return EFI_SUCCESS;\r
795\r
796ON_ERROR:\r
797 if (Address != USB_MAX_DEVICES) {\r
798 Bus->Devices[Address] = NULL;\r
799 }\r
800\r
801 if (Child != NULL) {\r
802 UsbFreeDevice (Child);\r
803 }\r
804\r
805 return Status;\r
806}\r
807\r
808\r
e237e7ae 809/**\r
810 Process the events on the port.\r
811\r
8616fc4c 812 @param HubIf The HUB that has the device connected.\r
813 @param Port The port index of the hub (started with zero).\r
e237e7ae 814\r
8616fc4c 815 @retval EFI_SUCCESS The device is enumerated (added or removed).\r
816 @retval EFI_OUT_OF_RESOURCES Failed to allocate resource for the device.\r
817 @retval Others Failed to enumerate the device.\r
e237e7ae 818\r
819**/\r
e237e7ae 820EFI_STATUS\r
821UsbEnumeratePort (\r
822 IN USB_INTERFACE *HubIf,\r
823 IN UINT8 Port\r
824 )\r
825{\r
826 USB_HUB_API *HubApi;\r
827 USB_DEVICE *Child;\r
828 EFI_USB_PORT_STATUS PortState;\r
829 EFI_STATUS Status;\r
830\r
831 Child = NULL;\r
832 HubApi = HubIf->HubApi;\r
833\r
834 //\r
835 // Host learns of the new device by polling the hub for port changes.\r
836 //\r
837 Status = HubApi->GetPortStatus (HubIf, Port, &PortState);\r
838\r
839 if (EFI_ERROR (Status)) {\r
d2577026 840 DEBUG ((EFI_D_ERROR, "UsbEnumeratePort: failed to get state of port %d\n", Port));\r
e237e7ae 841 return Status;\r
842 }\r
843\r
844 if (PortState.PortChangeStatus == 0) {\r
845 return EFI_SUCCESS;\r
846 }\r
847\r
d2577026 848 DEBUG (( EFI_D_INFO, "UsbEnumeratePort: port %d state - %x, change - %x\n",\r
e237e7ae 849 Port, PortState.PortStatus, PortState.PortChangeStatus));\r
850\r
851 //\r
852 // This driver only process two kinds of events now: over current and\r
853 // connect/disconnect. Other three events are: ENABLE, SUSPEND, RESET.\r
854 // ENABLE/RESET is used to reset port. SUSPEND isn't supported.\r
855 //\r
50fa1b3a 856 \r
857 if (USB_BIT_IS_SET (PortState.PortChangeStatus, USB_PORT_STAT_C_OVERCURRENT)) { \r
e237e7ae 858\r
50fa1b3a 859 if (USB_BIT_IS_SET (PortState.PortStatus, USB_PORT_STAT_OVERCURRENT)) {\r
860 //\r
41e8ff27 861 // Case1:\r
862 // Both OverCurrent and OverCurrentChange set, means over current occurs, \r
863 // which probably is caused by short circuit. It has to wait system hardware\r
864 // to perform recovery.\r
50fa1b3a 865 //\r
d2577026 866 DEBUG (( EFI_D_ERROR, "UsbEnumeratePort: Critical Over Current\n", Port));\r
50fa1b3a 867 return EFI_DEVICE_ERROR;\r
868 \r
41e8ff27 869 } \r
870 //\r
871 // Case2:\r
872 // Only OverCurrentChange set, means system has been recoveried from \r
873 // over current. As a result, all ports are nearly power-off, so\r
874 // it's necessary to detach and enumerate all ports again. \r
875 //\r
876 DEBUG (( EFI_D_ERROR, "UsbEnumeratePort: 2.0 device Recovery Over Current\n", Port)); \r
50fa1b3a 877 }\r
e237e7ae 878\r
50fa1b3a 879 if (USB_BIT_IS_SET (PortState.PortChangeStatus, USB_PORT_STAT_C_ENABLE)) { \r
e237e7ae 880 //\r
41e8ff27 881 // Case3:\r
882 // 1.1 roothub port reg doesn't reflect over-current state, while its counterpart\r
883 // on 2.0 roothub does. When over-current has influence on 1.1 device, the port \r
884 // would be disabled, so it's also necessary to detach and enumerate again.\r
e237e7ae 885 //\r
d2577026 886 DEBUG (( EFI_D_ERROR, "UsbEnumeratePort: 1.1 device Recovery Over Current\n", Port));\r
50fa1b3a 887 }\r
888 \r
889 if (USB_BIT_IS_SET (PortState.PortChangeStatus, USB_PORT_STAT_C_CONNECTION)) {\r
890 //\r
41e8ff27 891 // Case4:\r
892 // Device connected or disconnected normally. \r
50fa1b3a 893 //\r
41e8ff27 894 DEBUG ((EFI_D_ERROR, "UsbEnumeratePort: Device Connect/Discount Normally\n", Port));\r
50fa1b3a 895 }\r
e237e7ae 896\r
50fa1b3a 897 // \r
41e8ff27 898 // Following as the above cases, it's safety to remove and create again.\r
50fa1b3a 899 //\r
900 Child = UsbFindChild (HubIf, Port);\r
901 \r
902 if (Child != NULL) {\r
d2577026 903 DEBUG (( EFI_D_INFO, "UsbEnumeratePort: device at port %d removed from system\n", Port));\r
50fa1b3a 904 UsbRemoveDevice (Child);\r
e237e7ae 905 }\r
50fa1b3a 906 \r
907 if (USB_BIT_IS_SET (PortState.PortStatus, USB_PORT_STAT_CONNECTION)) {\r
908 //\r
909 // Now, new device connected, enumerate and configure the device \r
910 //\r
d2577026 911 DEBUG (( EFI_D_INFO, "UsbEnumeratePort: new device connected at port %d\n", Port));\r
50fa1b3a 912 Status = UsbEnumerateNewDev (HubIf, Port);\r
913 \r
914 } else {\r
d2577026 915 DEBUG (( EFI_D_INFO, "UsbEnumeratePort: device disconnected event on port %d\n", Port));\r
50fa1b3a 916 }\r
917 \r
e237e7ae 918 HubApi->ClearPortChange (HubIf, Port);\r
919 return Status;\r
920}\r
921\r
922\r
923/**\r
8616fc4c 924 Enumerate all the changed hub ports.\r
e237e7ae 925\r
8616fc4c 926 @param Event The event that is triggered.\r
927 @param Context The context to the event.\r
e237e7ae 928\r
e237e7ae 929**/\r
930VOID\r
8616fc4c 931EFIAPI\r
e237e7ae 932UsbHubEnumeration (\r
933 IN EFI_EVENT Event,\r
934 IN VOID *Context\r
935 )\r
936{\r
937 USB_INTERFACE *HubIf;\r
938 UINT8 Byte;\r
939 UINT8 Bit;\r
940 UINT8 Index;\r
941\r
ec30be9e 942 ASSERT (Context != NULL);\r
e237e7ae 943\r
944 HubIf = (USB_INTERFACE *) Context;\r
945\r
946 if (HubIf->ChangeMap == NULL) {\r
947 return ;\r
948 }\r
949\r
950 //\r
951 // HUB starts its port index with 1.\r
952 //\r
953 Byte = 0;\r
954 Bit = 1;\r
955\r
956 for (Index = 0; Index < HubIf->NumOfPort; Index++) {\r
957 if (USB_BIT_IS_SET (HubIf->ChangeMap[Byte], USB_BIT (Bit))) {\r
958 UsbEnumeratePort (HubIf, Index);\r
959 }\r
960\r
961 USB_NEXT_BIT (Byte, Bit);\r
962 }\r
963\r
964 UsbHubAckHubStatus (HubIf->Device);\r
965\r
966 gBS->FreePool (HubIf->ChangeMap);\r
967 HubIf->ChangeMap = NULL;\r
968 return ;\r
969}\r
970\r
971\r
972/**\r
8616fc4c 973 Enumerate all the changed hub ports.\r
e237e7ae 974\r
8616fc4c 975 @param Event The event that is triggered.\r
976 @param Context The context to the event.\r
e237e7ae 977\r
e237e7ae 978**/\r
979VOID\r
eb1f5ab3 980EFIAPI\r
e237e7ae 981UsbRootHubEnumeration (\r
982 IN EFI_EVENT Event,\r
983 IN VOID *Context\r
984 )\r
985{\r
986 USB_INTERFACE *RootHub;\r
987 UINT8 Index;\r
988\r
989 RootHub = (USB_INTERFACE *) Context;\r
990\r
991 for (Index = 0; Index < RootHub->NumOfPort; Index++) {\r
992 UsbEnumeratePort (RootHub, Index);\r
993 }\r
994}\r