]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - MdeModulePkg/Bus/Usb/UsbBusDxe/UsbEnumer.c
Fixed build error reported by ICC and GCC
[mirror_edk2.git] / MdeModulePkg / Bus / Usb / UsbBusDxe / UsbEnumer.c
... / ...
CommitLineData
1/** @file\r
2\r
3 Usb bus enumeration support.\r
4\r
5Copyright (c) 2007 - 2011, Intel Corporation. All rights reserved.<BR>\r
6This 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
14**/\r
15\r
16#include "UsbBus.h"\r
17\r
18/**\r
19 Return the endpoint descriptor in this interface.\r
20\r
21 @param UsbIf The interface to search in.\r
22 @param EpAddr The address of the endpoint to return.\r
23\r
24 @return The endpoint descriptor or NULL.\r
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
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
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
52 Free the resource used by USB interface.\r
53\r
54 @param UsbIf The USB interface to free.\r
55\r
56**/\r
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
74 FreePool (UsbIf->DevicePath);\r
75 }\r
76\r
77 FreePool (UsbIf);\r
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
85 @param Device The device has the interface descriptor.\r
86 @param IfDesc The interface descriptor.\r
87\r
88 @return The created USB interface for the descriptor, or NULL.\r
89\r
90**/\r
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
111 ASSERT (IfDesc->ActiveIndex < USB_MAX_INTERFACE_SETTING);\r
112 UsbIf->IfSetting = IfDesc->Settings[IfDesc->ActiveIndex];\r
113\r
114 CopyMem (\r
115 &(UsbIf->UsbIo),\r
116 &mUsbIoProtocol,\r
117 sizeof (EFI_USB_IO_PROTOCOL)\r
118 );\r
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
136 DEBUG ((EFI_D_ERROR, "UsbCreateInterface: failed to create device path\n"));\r
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
152 DEBUG ((EFI_D_ERROR, "UsbCreateInterface: failed to install UsbIo - %r\n", Status));\r
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
171 DEBUG ((EFI_D_ERROR, "UsbCreateInterface: failed to open host for child - %r\n", Status));\r
172 goto ON_ERROR;\r
173 }\r
174\r
175 return UsbIf;\r
176\r
177ON_ERROR:\r
178 if (UsbIf->DevicePath != NULL) {\r
179 FreePool (UsbIf->DevicePath);\r
180 }\r
181\r
182 FreePool (UsbIf);\r
183 return NULL;\r
184}\r
185\r
186\r
187/**\r
188 Free the resource used by this USB device.\r
189\r
190 @param Device The USB device to free.\r
191\r
192**/\r
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
209 @param ParentIf The parent HUB interface.\r
210 @param ParentPort The port on the HUB this device is connected to.\r
211\r
212 @return Created USB device, Or NULL.\r
213\r
214**/\r
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 Device->Tier = (UINT8)(ParentIf->Device->Tier + 1);\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
243 create a USB interface for each separate interface descriptor.\r
244\r
245 @param UsbIf The interface to connect driver to.\r
246\r
247 @return EFI_SUCCESS Interface is managed by some driver.\r
248 @return Others Failed to locate a driver for this interface.\r
249\r
250**/\r
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
266 DEBUG ((EFI_D_INFO, "UsbConnectDriver: found a hub device\n"));\r
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
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
283 DEBUG ((EFI_D_INFO, "UsbConnectDriver: TPL before connect is %d, %p\n", (UINT32)OldTpl, UsbIf->Handle));\r
284\r
285 gBS->RestoreTPL (TPL_CALLBACK);\r
286\r
287 Status = gBS->ConnectController (UsbIf->Handle, NULL, NULL, TRUE);\r
288 UsbIf->IsManaged = (BOOLEAN)!EFI_ERROR (Status);\r
289\r
290 DEBUG ((EFI_D_INFO, "UsbConnectDriver: TPL after connect is %d\n", (UINT32)UsbGetCurrentTpl()));\r
291 ASSERT (UsbGetCurrentTpl () == TPL_CALLBACK);\r
292\r
293 gBS->RaiseTPL (OldTpl);\r
294 }\r
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
307 @param IfDesc The interface descriptor to set.\r
308 @param Alternate The alternate setting number to locate.\r
309\r
310 @retval EFI_NOT_FOUND There is no setting with this alternate index.\r
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
329 ASSERT (Index < USB_MAX_INTERFACE_SETTING);\r
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
343 ASSERT (Setting != NULL);\r
344 DEBUG ((EFI_D_INFO, "UsbSelectSetting: setting %d selected for interface %d\n",\r
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
362 @param Device The device to select configuration.\r
363 @param ConfigValue The index of the configuration ( != 0).\r
364\r
365 @retval EFI_NOT_FOUND There is no configuration with the index.\r
366 @retval EFI_OUT_OF_RESOURCES Failed to allocate resource.\r
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
403 DEBUG ((EFI_D_INFO, "UsbSelectConfig: config %d selected for device %d\n",\r
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
423 return EFI_OUT_OF_RESOURCES;\r
424 }\r
425\r
426 ASSERT (Index < USB_MAX_INTERFACE);\r
427 Device->Interfaces[Index] = UsbIf;\r
428\r
429 //\r
430 // Connect the device to drivers, if it failed, ignore\r
431 // the error. Don't let the unsupported interfaces to block\r
432 // the supported interfaces.\r
433 //\r
434 Status = UsbConnectDriver (UsbIf);\r
435\r
436 if (EFI_ERROR (Status)) {\r
437 DEBUG ((EFI_D_ERROR, "UsbSelectConfig: failed to connect driver %r, ignored\n", Status));\r
438 }\r
439 }\r
440\r
441 Device->NumOfInterface = Index;\r
442\r
443 return EFI_SUCCESS;\r
444}\r
445\r
446\r
447/**\r
448 Disconnect the USB interface with its driver.\r
449\r
450 @param UsbIf The interface to disconnect driver from.\r
451\r
452**/\r
453VOID\r
454UsbDisconnectDriver (\r
455 IN USB_INTERFACE *UsbIf\r
456 )\r
457{\r
458 EFI_TPL OldTpl;\r
459 EFI_STATUS Status;\r
460\r
461 //\r
462 // Release the hub if it's a hub controller, otherwise\r
463 // disconnect the driver if it is managed by other drivers.\r
464 //\r
465 if (UsbIf->IsHub) {\r
466 UsbIf->HubApi->Release (UsbIf);\r
467\r
468 } else if (UsbIf->IsManaged) {\r
469 //\r
470 // This function is called in both UsbIoControlTransfer and\r
471 // the timer callback in hub enumeration. So, at least it is\r
472 // called at TPL_CALLBACK. Some driver sitting on USB has\r
473 // twisted TPL used. It should be no problem for us to connect\r
474 // or disconnect at CALLBACK.\r
475 //\r
476 OldTpl = UsbGetCurrentTpl ();\r
477 DEBUG ((EFI_D_INFO, "UsbDisconnectDriver: old TPL is %d, %p\n", (UINT32)OldTpl, UsbIf->Handle));\r
478\r
479 gBS->RestoreTPL (TPL_CALLBACK);\r
480\r
481 Status = gBS->DisconnectController (UsbIf->Handle, NULL, NULL);\r
482 UsbIf->IsManaged = FALSE;\r
483\r
484 DEBUG (( EFI_D_INFO, "UsbDisconnectDriver: TPL after disconnect is %d, %d\n", (UINT32)UsbGetCurrentTpl(), Status));\r
485 ASSERT (UsbGetCurrentTpl () == TPL_CALLBACK);\r
486\r
487 gBS->RaiseTPL (OldTpl);\r
488 }\r
489}\r
490\r
491\r
492/**\r
493 Remove the current device configuration.\r
494\r
495 @param Device The USB device to remove configuration from.\r
496\r
497**/\r
498VOID\r
499UsbRemoveConfig (\r
500 IN USB_DEVICE *Device\r
501 )\r
502{\r
503 USB_INTERFACE *UsbIf;\r
504 UINTN Index;\r
505\r
506 //\r
507 // Remove each interface of the device\r
508 //\r
509 for (Index = 0; Index < Device->NumOfInterface; Index++) { \r
510 ASSERT (Index < USB_MAX_INTERFACE);\r
511 UsbIf = Device->Interfaces[Index];\r
512\r
513 if (UsbIf == NULL) {\r
514 continue;\r
515 }\r
516\r
517 UsbDisconnectDriver (UsbIf);\r
518 UsbFreeInterface (UsbIf);\r
519 Device->Interfaces[Index] = NULL;\r
520 }\r
521\r
522 Device->ActiveConfig = NULL;\r
523 Device->NumOfInterface = 0;\r
524}\r
525\r
526\r
527/**\r
528 Remove the device and all its children from the bus.\r
529\r
530 @param Device The device to remove.\r
531\r
532 @retval EFI_SUCCESS The device is removed.\r
533\r
534**/\r
535EFI_STATUS\r
536UsbRemoveDevice (\r
537 IN USB_DEVICE *Device\r
538 )\r
539{\r
540 USB_BUS *Bus;\r
541 USB_DEVICE *Child;\r
542 EFI_STATUS Status;\r
543 UINTN Index;\r
544\r
545 Bus = Device->Bus;\r
546\r
547 //\r
548 // Remove all the devices on its downstream ports. Search from devices[1].\r
549 // Devices[0] is the root hub.\r
550 //\r
551 for (Index = 1; Index < mMaxUsbDeviceNum; Index++) {\r
552 Child = Bus->Devices[Index];\r
553\r
554 if ((Child == NULL) || (Child->ParentAddr != Device->Address)) {\r
555 continue;\r
556 }\r
557\r
558 Status = UsbRemoveDevice (Child);\r
559\r
560 if (EFI_ERROR (Status)) {\r
561 DEBUG ((EFI_D_ERROR, "UsbRemoveDevice: failed to remove child, ignore error\n"));\r
562 Bus->Devices[Index] = NULL;\r
563 }\r
564 }\r
565\r
566 UsbRemoveConfig (Device);\r
567\r
568 DEBUG (( EFI_D_INFO, "UsbRemoveDevice: device %d removed\n", Device->Address));\r
569\r
570 ASSERT (Device->Address < mMaxUsbDeviceNum);\r
571 Bus->Devices[Device->Address] = NULL;\r
572 UsbFreeDevice (Device);\r
573\r
574 return EFI_SUCCESS;\r
575}\r
576\r
577\r
578/**\r
579 Find the child device on the hub's port.\r
580\r
581 @param HubIf The hub interface.\r
582 @param Port The port of the hub this child is connected to.\r
583\r
584 @return The device on the hub's port, or NULL if there is none.\r
585\r
586**/\r
587USB_DEVICE *\r
588UsbFindChild (\r
589 IN USB_INTERFACE *HubIf,\r
590 IN UINT8 Port\r
591 )\r
592{\r
593 USB_DEVICE *Device;\r
594 USB_BUS *Bus;\r
595 UINTN Index;\r
596\r
597 Bus = HubIf->Device->Bus;\r
598\r
599 //\r
600 // Start checking from device 1, device 0 is the root hub\r
601 //\r
602 for (Index = 1; Index < mMaxUsbDeviceNum; Index++) {\r
603 Device = Bus->Devices[Index];\r
604\r
605 if ((Device != NULL) && (Device->ParentAddr == HubIf->Device->Address) &&\r
606 (Device->ParentPort == Port)) {\r
607\r
608 return Device;\r
609 }\r
610 }\r
611\r
612 return NULL;\r
613}\r
614\r
615\r
616/**\r
617 Enumerate and configure the new device on the port of this HUB interface.\r
618\r
619 @param HubIf The HUB that has the device connected.\r
620 @param Port The port index of the hub (started with zero).\r
621\r
622 @retval EFI_SUCCESS The device is enumerated (added or removed).\r
623 @retval EFI_OUT_OF_RESOURCES Failed to allocate resource for the device.\r
624 @retval Others Failed to enumerate the device.\r
625\r
626**/\r
627EFI_STATUS\r
628UsbEnumerateNewDev (\r
629 IN USB_INTERFACE *HubIf,\r
630 IN UINT8 Port\r
631 )\r
632{\r
633 USB_BUS *Bus;\r
634 USB_HUB_API *HubApi;\r
635 USB_DEVICE *Child;\r
636 USB_DEVICE *Parent;\r
637 EFI_USB_PORT_STATUS PortState;\r
638 UINTN Address;\r
639 UINT8 Config;\r
640 EFI_STATUS Status;\r
641\r
642 Address = mMaxUsbDeviceNum;\r
643 Parent = HubIf->Device;\r
644 Bus = Parent->Bus;\r
645 HubApi = HubIf->HubApi;\r
646 \r
647 gBS->Stall (USB_WAIT_PORT_STABLE_STALL);\r
648 \r
649 //\r
650 // Hub resets the device for at least 10 milliseconds.\r
651 // Host learns device speed. If device is of low/full speed\r
652 // and the hub is a EHCI root hub, ResetPort will release\r
653 // the device to its companion UHCI and return an error.\r
654 //\r
655 Status = HubApi->ResetPort (HubIf, Port);\r
656\r
657 if (EFI_ERROR (Status)) {\r
658 DEBUG ((EFI_D_ERROR, "UsbEnumerateNewDev: failed to reset port %d - %r\n", Port, Status));\r
659\r
660 return Status;\r
661 }\r
662\r
663 DEBUG (( EFI_D_INFO, "UsbEnumerateNewDev: hub port %d is reset\n", Port));\r
664\r
665 Child = UsbCreateDevice (HubIf, Port);\r
666\r
667 if (Child == NULL) {\r
668 return EFI_OUT_OF_RESOURCES;\r
669 }\r
670\r
671 //\r
672 // OK, now identify the device speed. After reset, hub\r
673 // fully knows the actual device speed.\r
674 //\r
675 Status = HubApi->GetPortStatus (HubIf, Port, &PortState);\r
676\r
677 if (EFI_ERROR (Status)) {\r
678 DEBUG ((EFI_D_ERROR, "UsbEnumerateNewDev: failed to get speed of port %d\n", Port));\r
679 goto ON_ERROR;\r
680 }\r
681\r
682 if (!USB_BIT_IS_SET (PortState.PortStatus, USB_PORT_STAT_CONNECTION)) {\r
683 DEBUG ((EFI_D_ERROR, "UsbEnumerateNewDev: No device presented at port %d\n", Port));\r
684 goto ON_ERROR;\r
685 } else if (USB_BIT_IS_SET (PortState.PortStatus, USB_PORT_STAT_SUPER_SPEED)){\r
686 Child->Speed = EFI_USB_SPEED_SUPER;\r
687 Child->MaxPacket0 = 512;\r
688 } else if (USB_BIT_IS_SET (PortState.PortStatus, USB_PORT_STAT_HIGH_SPEED)) {\r
689 Child->Speed = EFI_USB_SPEED_HIGH;\r
690 Child->MaxPacket0 = 64;\r
691 } else if (USB_BIT_IS_SET (PortState.PortStatus, USB_PORT_STAT_LOW_SPEED)) {\r
692 Child->Speed = EFI_USB_SPEED_LOW;\r
693 Child->MaxPacket0 = 8;\r
694 } else {\r
695 Child->Speed = EFI_USB_SPEED_FULL;\r
696 Child->MaxPacket0 = 8;\r
697 }\r
698\r
699 DEBUG (( EFI_D_INFO, "UsbEnumerateNewDev: device is of %d speed\n", Child->Speed));\r
700\r
701 if (Child->Speed != EFI_USB_SPEED_HIGH) {\r
702 //\r
703 // If the child isn't a high speed device, it is necessary to\r
704 // set the transaction translator. Port TT is 1-based.\r
705 // This is quite simple:\r
706 // 1. if parent is of high speed, then parent is our translator\r
707 // 2. otherwise use parent's translator.\r
708 //\r
709 if (Parent->Speed == EFI_USB_SPEED_HIGH) {\r
710 Child->Translator.TranslatorHubAddress = Parent->Address;\r
711 Child->Translator.TranslatorPortNumber = (UINT8) (Port + 1);\r
712\r
713 } else {\r
714 Child->Translator = Parent->Translator;\r
715 }\r
716\r
717 DEBUG (( EFI_D_INFO, "UsbEnumerateNewDev: device uses translator (%d, %d)\n",\r
718 Child->Translator.TranslatorHubAddress,\r
719 Child->Translator.TranslatorPortNumber));\r
720 }\r
721\r
722 //\r
723 // After port is reset, hub establishes a signal path between\r
724 // the device and host (DEFALUT state). Device's registers are\r
725 // reset, use default address 0 (host enumerates one device at\r
726 // a time) , and ready to respond to control transfer at EP 0.\r
727 //\r
728\r
729 //\r
730 // Host assigns an address to the device. Device completes the\r
731 // status stage with default address, then switches to new address.\r
732 // ADDRESS state. Address zero is reserved for root hub.\r
733 //\r
734 for (Address = 1; Address < mMaxUsbDeviceNum; Address++) {\r
735 if (Bus->Devices[Address] == NULL) {\r
736 break;\r
737 }\r
738 }\r
739\r
740 if (Address == mMaxUsbDeviceNum) {\r
741 DEBUG ((EFI_D_ERROR, "UsbEnumerateNewDev: address pool is full for port %d\n", Port));\r
742\r
743 Status = EFI_ACCESS_DENIED;\r
744 goto ON_ERROR;\r
745 }\r
746\r
747 Status = UsbSetAddress (Child, (UINT8)Address);\r
748 Child->Address = (UINT8)Address;\r
749 Bus->Devices[Address] = Child;\r
750\r
751 if (EFI_ERROR (Status)) {\r
752 DEBUG ((EFI_D_ERROR, "UsbEnumerateNewDev: failed to set device address - %r\n", Status));\r
753 goto ON_ERROR;\r
754 }\r
755\r
756 gBS->Stall (USB_SET_DEVICE_ADDRESS_STALL);\r
757\r
758 DEBUG ((EFI_D_INFO, "UsbEnumerateNewDev: device is now ADDRESSED at %d\n", Address));\r
759\r
760 //\r
761 // Host sends a Get_Descriptor request to learn the max packet\r
762 // size of default pipe (only part of the device's descriptor).\r
763 //\r
764 Status = UsbGetMaxPacketSize0 (Child);\r
765\r
766 if (EFI_ERROR (Status)) {\r
767 DEBUG ((EFI_D_ERROR, "UsbEnumerateNewDev: failed to get max packet for EP 0 - %r\n", Status));\r
768 goto ON_ERROR;\r
769 }\r
770\r
771 DEBUG (( EFI_D_INFO, "UsbEnumerateNewDev: max packet size for EP 0 is %d\n", Child->MaxPacket0));\r
772\r
773 //\r
774 // Host learns about the device's abilities by requesting device's\r
775 // entire descriptions.\r
776 //\r
777 Status = UsbBuildDescTable (Child);\r
778\r
779 if (EFI_ERROR (Status)) {\r
780 DEBUG ((EFI_D_ERROR, "UsbEnumerateNewDev: failed to build descriptor table - %r\n", Status));\r
781 goto ON_ERROR;\r
782 }\r
783\r
784 //\r
785 // Select a default configuration: UEFI must set the configuration\r
786 // before the driver can connect to the device.\r
787 //\r
788 Config = Child->DevDesc->Configs[0]->Desc.ConfigurationValue;\r
789 Status = UsbSetConfig (Child, Config);\r
790\r
791 if (EFI_ERROR (Status)) {\r
792 DEBUG ((EFI_D_ERROR, "UsbEnumerateNewDev: failed to set configure %d - %r\n", Config, Status));\r
793 goto ON_ERROR;\r
794 }\r
795\r
796 DEBUG (( EFI_D_INFO, "UsbEnumerateNewDev: device %d is now in CONFIGED state\n", Address));\r
797\r
798 //\r
799 // Host assigns and loads a device driver.\r
800 //\r
801 Status = UsbSelectConfig (Child, Config);\r
802\r
803 if (EFI_ERROR (Status)) {\r
804 DEBUG ((EFI_D_ERROR, "UsbEnumerateNewDev: failed to create interfaces - %r\n", Status));\r
805 goto ON_ERROR;\r
806 }\r
807\r
808 return EFI_SUCCESS;\r
809\r
810ON_ERROR:\r
811 if (Address != mMaxUsbDeviceNum) {\r
812 Bus->Devices[Address] = NULL;\r
813 }\r
814\r
815 if (Child != NULL) {\r
816 UsbFreeDevice (Child);\r
817 }\r
818\r
819 return Status;\r
820}\r
821\r
822\r
823/**\r
824 Process the events on the port.\r
825\r
826 @param HubIf The HUB that has the device connected.\r
827 @param Port The port index of the hub (started with zero).\r
828\r
829 @retval EFI_SUCCESS The device is enumerated (added or removed).\r
830 @retval EFI_OUT_OF_RESOURCES Failed to allocate resource for the device.\r
831 @retval Others Failed to enumerate the device.\r
832\r
833**/\r
834EFI_STATUS\r
835UsbEnumeratePort (\r
836 IN USB_INTERFACE *HubIf,\r
837 IN UINT8 Port\r
838 )\r
839{\r
840 USB_HUB_API *HubApi;\r
841 USB_DEVICE *Child;\r
842 EFI_USB_PORT_STATUS PortState;\r
843 EFI_STATUS Status;\r
844\r
845 Child = NULL;\r
846 HubApi = HubIf->HubApi;\r
847\r
848 //\r
849 // Host learns of the new device by polling the hub for port changes.\r
850 //\r
851 Status = HubApi->GetPortStatus (HubIf, Port, &PortState);\r
852\r
853 if (EFI_ERROR (Status)) {\r
854 DEBUG ((EFI_D_ERROR, "UsbEnumeratePort: failed to get state of port %d\n", Port));\r
855 return Status;\r
856 }\r
857\r
858 //\r
859 // Only handle connection/enable/overcurrent/reset change.\r
860 // Usb super speed hub may report other changes, such as warm reset change. Ignore them.\r
861 //\r
862 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
863 return EFI_SUCCESS;\r
864 }\r
865\r
866 DEBUG (( EFI_D_INFO, "UsbEnumeratePort: port %d state - %02x, change - %02x on %p\n",\r
867 Port, PortState.PortChangeStatus, PortState.PortStatus, HubIf));\r
868\r
869 //\r
870 // This driver only process two kinds of events now: over current and\r
871 // connect/disconnect. Other three events are: ENABLE, SUSPEND, RESET.\r
872 // ENABLE/RESET is used to reset port. SUSPEND isn't supported.\r
873 //\r
874 \r
875 if (USB_BIT_IS_SET (PortState.PortChangeStatus, USB_PORT_STAT_C_OVERCURRENT)) { \r
876\r
877 if (USB_BIT_IS_SET (PortState.PortStatus, USB_PORT_STAT_OVERCURRENT)) {\r
878 //\r
879 // Case1:\r
880 // Both OverCurrent and OverCurrentChange set, means over current occurs, \r
881 // which probably is caused by short circuit. It has to wait system hardware\r
882 // to perform recovery.\r
883 //\r
884 DEBUG (( EFI_D_ERROR, "UsbEnumeratePort: Critical Over Current\n", Port));\r
885 return EFI_DEVICE_ERROR;\r
886 \r
887 } \r
888 //\r
889 // Case2:\r
890 // Only OverCurrentChange set, means system has been recoveried from \r
891 // over current. As a result, all ports are nearly power-off, so\r
892 // it's necessary to detach and enumerate all ports again. \r
893 //\r
894 DEBUG (( EFI_D_ERROR, "UsbEnumeratePort: 2.0 device Recovery Over Current\n", Port)); \r
895 }\r
896\r
897 if (USB_BIT_IS_SET (PortState.PortChangeStatus, USB_PORT_STAT_C_ENABLE)) { \r
898 //\r
899 // Case3:\r
900 // 1.1 roothub port reg doesn't reflect over-current state, while its counterpart\r
901 // on 2.0 roothub does. When over-current has influence on 1.1 device, the port \r
902 // would be disabled, so it's also necessary to detach and enumerate again.\r
903 //\r
904 DEBUG (( EFI_D_ERROR, "UsbEnumeratePort: 1.1 device Recovery Over Current\n", Port));\r
905 }\r
906 \r
907 if (USB_BIT_IS_SET (PortState.PortChangeStatus, USB_PORT_STAT_C_CONNECTION)) {\r
908 //\r
909 // Case4:\r
910 // Device connected or disconnected normally. \r
911 //\r
912 DEBUG ((EFI_D_ERROR, "UsbEnumeratePort: Device Connect/Discount Normally\n", Port));\r
913 }\r
914\r
915 // \r
916 // Following as the above cases, it's safety to remove and create again.\r
917 //\r
918 Child = UsbFindChild (HubIf, Port);\r
919 \r
920 if (Child != NULL) {\r
921 DEBUG (( EFI_D_INFO, "UsbEnumeratePort: device at port %d removed from root hub %p\n", Port, HubIf));\r
922 UsbRemoveDevice (Child);\r
923 }\r
924 \r
925 if (USB_BIT_IS_SET (PortState.PortStatus, USB_PORT_STAT_CONNECTION)) {\r
926 //\r
927 // Now, new device connected, enumerate and configure the device \r
928 //\r
929 DEBUG (( EFI_D_INFO, "UsbEnumeratePort: new device connected at port %d\n", Port));\r
930 Status = UsbEnumerateNewDev (HubIf, Port);\r
931 \r
932 } else {\r
933 DEBUG (( EFI_D_INFO, "UsbEnumeratePort: device disconnected event on port %d\n", Port));\r
934 }\r
935 \r
936 HubApi->ClearPortChange (HubIf, Port);\r
937 return Status;\r
938}\r
939\r
940\r
941/**\r
942 Enumerate all the changed hub ports.\r
943\r
944 @param Event The event that is triggered.\r
945 @param Context The context to the event.\r
946\r
947**/\r
948VOID\r
949EFIAPI\r
950UsbHubEnumeration (\r
951 IN EFI_EVENT Event,\r
952 IN VOID *Context\r
953 )\r
954{\r
955 USB_INTERFACE *HubIf;\r
956 UINT8 Byte;\r
957 UINT8 Bit;\r
958 UINT8 Index;\r
959\r
960 ASSERT (Context != NULL);\r
961\r
962 HubIf = (USB_INTERFACE *) Context;\r
963\r
964 if (HubIf->ChangeMap == NULL) {\r
965 return ;\r
966 }\r
967\r
968 //\r
969 // HUB starts its port index with 1.\r
970 //\r
971 Byte = 0;\r
972 Bit = 1;\r
973\r
974 for (Index = 0; Index < HubIf->NumOfPort; Index++) {\r
975 if (USB_BIT_IS_SET (HubIf->ChangeMap[Byte], USB_BIT (Bit))) {\r
976 UsbEnumeratePort (HubIf, Index);\r
977 }\r
978\r
979 USB_NEXT_BIT (Byte, Bit);\r
980 }\r
981\r
982 UsbHubAckHubStatus (HubIf->Device);\r
983\r
984 gBS->FreePool (HubIf->ChangeMap);\r
985 HubIf->ChangeMap = NULL;\r
986 return ;\r
987}\r
988\r
989\r
990/**\r
991 Enumerate all the changed hub ports.\r
992\r
993 @param Event The event that is triggered.\r
994 @param Context The context to the event.\r
995\r
996**/\r
997VOID\r
998EFIAPI\r
999UsbRootHubEnumeration (\r
1000 IN EFI_EVENT Event,\r
1001 IN VOID *Context\r
1002 )\r
1003{\r
1004 USB_INTERFACE *RootHub;\r
1005 UINT8 Index;\r
1006\r
1007 RootHub = (USB_INTERFACE *) Context;\r
1008\r
1009 for (Index = 0; Index < RootHub->NumOfPort; Index++) {\r
1010 UsbEnumeratePort (RootHub, Index);\r
1011 }\r
1012}\r