]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Bus/Usb/UsbBusDxe/UsbHub.c
MdeModulePkg: Clean up source files
[mirror_edk2.git] / MdeModulePkg / Bus / Usb / UsbBusDxe / UsbHub.c
1 /** @file
2
3 Unified interface for RootHub and Hub.
4
5 Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.<BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
10
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13
14 **/
15
16 #include "UsbBus.h"
17
18 //
19 // Array that maps the change bit to feature value which is
20 // used to clear these change bit. USB HUB API will clear
21 // these change bit automatically. For non-root hub, these
22 // bits determine whether hub will report the port in changed
23 // bit maps.
24 //
25 USB_CHANGE_FEATURE_MAP mHubFeatureMap[] = {
26 {USB_PORT_STAT_C_CONNECTION, EfiUsbPortConnectChange},
27 {USB_PORT_STAT_C_ENABLE, EfiUsbPortEnableChange},
28 {USB_PORT_STAT_C_SUSPEND, EfiUsbPortSuspendChange},
29 {USB_PORT_STAT_C_OVERCURRENT, EfiUsbPortOverCurrentChange},
30 {USB_PORT_STAT_C_RESET, EfiUsbPortResetChange}
31 };
32
33 USB_CHANGE_FEATURE_MAP mRootHubFeatureMap[] = {
34 {USB_PORT_STAT_C_CONNECTION, EfiUsbPortConnectChange},
35 {USB_PORT_STAT_C_ENABLE, EfiUsbPortEnableChange},
36 {USB_PORT_STAT_C_SUSPEND, EfiUsbPortSuspendChange},
37 {USB_PORT_STAT_C_OVERCURRENT, EfiUsbPortOverCurrentChange},
38 {USB_PORT_STAT_C_RESET, EfiUsbPortResetChange},
39 };
40
41 //
42 // USB hub class specific requests. Although USB hub
43 // is related to an interface, these requests are sent
44 // to the control endpoint of the device.
45 //
46 /**
47 USB hub control transfer to set the hub depth.
48
49 @param HubDev The device of the hub.
50 @param Depth The depth to set.
51
52 @retval EFI_SUCCESS Depth of the hub is set.
53 @retval Others Failed to set the depth.
54
55 **/
56 EFI_STATUS
57 UsbHubCtrlSetHubDepth (
58 IN USB_DEVICE *HubDev,
59 IN UINT16 Depth
60 )
61 {
62 EFI_STATUS Status;
63
64 Status = UsbCtrlRequest (
65 HubDev,
66 EfiUsbNoData,
67 USB_REQ_TYPE_CLASS,
68 USB_HUB_TARGET_HUB,
69 USB_HUB_REQ_SET_DEPTH,
70 Depth,
71 0,
72 NULL,
73 0
74 );
75
76 return Status;
77 }
78
79 /**
80 USB hub control transfer to clear the hub feature.
81
82 @param HubDev The device of the hub.
83 @param Feature The feature to clear.
84
85 @retval EFI_SUCCESS Feature of the hub is cleared.
86 @retval Others Failed to clear the feature.
87
88 **/
89 EFI_STATUS
90 UsbHubCtrlClearHubFeature (
91 IN USB_DEVICE *HubDev,
92 IN UINT16 Feature
93 )
94 {
95 EFI_STATUS Status;
96
97 Status = UsbCtrlRequest (
98 HubDev,
99 EfiUsbNoData,
100 USB_REQ_TYPE_CLASS,
101 USB_HUB_TARGET_HUB,
102 USB_HUB_REQ_CLEAR_FEATURE,
103 Feature,
104 0,
105 NULL,
106 0
107 );
108
109 return Status;
110 }
111
112
113 /**
114 Clear the feature of the device's port.
115
116 @param HubDev The hub device.
117 @param Port The port to clear feature.
118 @param Feature The feature to clear.
119
120 @retval EFI_SUCCESS The feature of the port is cleared.
121 @retval Others Failed to clear the feature.
122
123 **/
124 EFI_STATUS
125 UsbHubCtrlClearPortFeature (
126 IN USB_DEVICE *HubDev,
127 IN UINT8 Port,
128 IN UINT16 Feature
129 )
130 {
131 EFI_STATUS Status;
132
133 //
134 // In USB bus, all the port index starts from 0. But HUB
135 // indexes its port from 1. So, port number is added one.
136 //
137 Status = UsbCtrlRequest (
138 HubDev,
139 EfiUsbNoData,
140 USB_REQ_TYPE_CLASS,
141 USB_HUB_TARGET_PORT,
142 USB_HUB_REQ_CLEAR_FEATURE,
143 Feature,
144 (UINT16) (Port + 1),
145 NULL,
146 0
147 );
148
149 return Status;
150 }
151
152
153 /**
154 Clear the transaction translate buffer if full/low
155 speed control/bulk transfer failed and the transfer
156 uses this hub as translator.Remember to clear the TT
157 buffer of transaction translator, not that of the
158 parent.
159
160 @param HubDev The hub device.
161 @param Port The port of the hub.
162 @param DevAddr Address of the failed transaction.
163 @param EpNum The endpoint number of the failed transaction.
164 @param EpType The type of failed transaction.
165
166 @retval EFI_SUCCESS The TT buffer is cleared.
167 @retval Others Failed to clear the TT buffer.
168
169 **/
170 EFI_STATUS
171 UsbHubCtrlClearTTBuffer (
172 IN USB_DEVICE *HubDev,
173 IN UINT8 Port,
174 IN UINT16 DevAddr,
175 IN UINT16 EpNum,
176 IN UINT16 EpType
177 )
178 {
179 EFI_STATUS Status;
180 UINT16 Value;
181
182 //
183 // Check USB2.0 spec page 424 for wValue's encoding
184 //
185 Value = (UINT16) ((EpNum & 0x0F) | (DevAddr << 4) |
186 ((EpType & 0x03) << 11) | ((EpNum & 0x80) << 15));
187
188 Status = UsbCtrlRequest (
189 HubDev,
190 EfiUsbNoData,
191 USB_REQ_TYPE_CLASS,
192 USB_HUB_TARGET_PORT,
193 USB_HUB_REQ_CLEAR_TT,
194 Value,
195 (UINT16) (Port + 1),
196 NULL,
197 0
198 );
199
200 return Status;
201 }
202
203 /**
204 Usb hub control transfer to get the (super speed) hub descriptor.
205
206 @param HubDev The hub device.
207 @param Buf The buffer to hold the descriptor.
208 @param Len The length to retrieve.
209
210 @retval EFI_SUCCESS The hub descriptor is retrieved.
211 @retval Others Failed to retrieve the hub descriptor.
212
213 **/
214 EFI_STATUS
215 UsbHubCtrlGetHubDesc (
216 IN USB_DEVICE *HubDev,
217 OUT VOID *Buf,
218 IN UINTN Len
219 )
220 {
221 EFI_STATUS Status;
222 UINT8 DescType;
223
224 DescType = (HubDev->Speed == EFI_USB_SPEED_SUPER) ?
225 USB_DESC_TYPE_HUB_SUPER_SPEED :
226 USB_DESC_TYPE_HUB;
227
228 Status = UsbCtrlRequest (
229 HubDev,
230 EfiUsbDataIn,
231 USB_REQ_TYPE_CLASS,
232 USB_HUB_TARGET_HUB,
233 USB_HUB_REQ_GET_DESC,
234 (UINT16) (DescType << 8),
235 0,
236 Buf,
237 Len
238 );
239
240 return Status;
241 }
242
243
244 /**
245 Usb hub control transfer to get the hub status.
246
247 @param HubDev The hub device.
248 @param State The variable to return the status.
249
250 @retval EFI_SUCCESS The hub status is returned in State.
251 @retval Others Failed to get the hub status.
252
253 **/
254 EFI_STATUS
255 UsbHubCtrlGetHubStatus (
256 IN USB_DEVICE *HubDev,
257 OUT UINT32 *State
258 )
259 {
260 EFI_STATUS Status;
261
262 Status = UsbCtrlRequest (
263 HubDev,
264 EfiUsbDataIn,
265 USB_REQ_TYPE_CLASS,
266 USB_HUB_TARGET_HUB,
267 USB_HUB_REQ_GET_STATUS,
268 0,
269 0,
270 State,
271 4
272 );
273
274 return Status;
275 }
276
277
278 /**
279 Usb hub control transfer to get the port status.
280
281 @param HubDev The hub device.
282 @param Port The port of the hub.
283 @param State Variable to return the hub port state.
284
285 @retval EFI_SUCCESS The port state is returned in State.
286 @retval Others Failed to retrieve the port state.
287
288 **/
289 EFI_STATUS
290 UsbHubCtrlGetPortStatus (
291 IN USB_DEVICE *HubDev,
292 IN UINT8 Port,
293 OUT VOID *State
294 )
295 {
296 EFI_STATUS Status;
297
298 //
299 // In USB bus, all the port index starts from 0. But HUB
300 // indexes its port from 1. So, port number is added one.
301 // No need to convert the hub bit to UEFI definition, they
302 // are the same
303 //
304 Status = UsbCtrlRequest (
305 HubDev,
306 EfiUsbDataIn,
307 USB_REQ_TYPE_CLASS,
308 USB_HUB_TARGET_PORT,
309 USB_HUB_REQ_GET_STATUS,
310 0,
311 (UINT16) (Port + 1),
312 State,
313 4
314 );
315
316 return Status;
317 }
318
319
320 /**
321 Usb hub control transfer to reset the TT (Transaction Transaltor).
322
323 @param HubDev The hub device.
324 @param Port The port of the hub.
325
326 @retval EFI_SUCCESS The TT of the hub is reset.
327 @retval Others Failed to reset the port.
328
329 **/
330 EFI_STATUS
331 UsbHubCtrlResetTT (
332 IN USB_DEVICE *HubDev,
333 IN UINT8 Port
334 )
335 {
336 EFI_STATUS Status;
337
338 Status = UsbCtrlRequest (
339 HubDev,
340 EfiUsbNoData,
341 USB_REQ_TYPE_CLASS,
342 USB_HUB_TARGET_HUB,
343 USB_HUB_REQ_RESET_TT,
344 0,
345 (UINT16) (Port + 1),
346 NULL,
347 0
348 );
349
350 return Status;
351 }
352
353
354 /**
355 Usb hub control transfer to set the hub feature.
356
357 @param HubDev The hub device.
358 @param Feature The feature to set.
359
360 @retval EFI_SUCESS The feature is set for the hub.
361 @retval Others Failed to set the feature.
362
363 **/
364 EFI_STATUS
365 UsbHubCtrlSetHubFeature (
366 IN USB_DEVICE *HubDev,
367 IN UINT8 Feature
368 )
369 {
370 EFI_STATUS Status;
371
372 Status = UsbCtrlRequest (
373 HubDev,
374 EfiUsbNoData,
375 USB_REQ_TYPE_CLASS,
376 USB_HUB_TARGET_HUB,
377 USB_HUB_REQ_SET_FEATURE,
378 Feature,
379 0,
380 NULL,
381 0
382 );
383
384 return Status;
385 }
386
387
388 /**
389 Usb hub control transfer to set the port feature.
390
391 @param HubDev The Usb hub device.
392 @param Port The Usb port to set feature for.
393 @param Feature The feature to set.
394
395 @retval EFI_SUCCESS The feature is set for the port.
396 @retval Others Failed to set the feature.
397
398 **/
399 EFI_STATUS
400 UsbHubCtrlSetPortFeature (
401 IN USB_DEVICE *HubDev,
402 IN UINT8 Port,
403 IN UINT8 Feature
404 )
405 {
406 EFI_STATUS Status;
407
408 //
409 // In USB bus, all the port index starts from 0. But HUB
410 // indexes its port from 1. So, port number is added one.
411 //
412 Status = UsbCtrlRequest (
413 HubDev,
414 EfiUsbNoData,
415 USB_REQ_TYPE_CLASS,
416 USB_HUB_TARGET_PORT,
417 USB_HUB_REQ_SET_FEATURE,
418 Feature,
419 (UINT16) (Port + 1),
420 NULL,
421 0
422 );
423
424 return Status;
425 }
426
427
428 /**
429 Read the whole usb hub descriptor. It is necessary
430 to do it in two steps because hub descriptor is of
431 variable length.
432
433 @param HubDev The hub device.
434 @param HubDesc The variable to return the descriptor.
435
436 @retval EFI_SUCCESS The hub descriptor is read.
437 @retval Others Failed to read the hub descriptor.
438
439 **/
440 EFI_STATUS
441 UsbHubReadDesc (
442 IN USB_DEVICE *HubDev,
443 OUT EFI_USB_HUB_DESCRIPTOR *HubDesc
444 )
445 {
446 EFI_STATUS Status;
447
448 //
449 // First get the hub descriptor length
450 //
451 Status = UsbHubCtrlGetHubDesc (HubDev, HubDesc, 2);
452
453 if (EFI_ERROR (Status)) {
454 return Status;
455 }
456
457 //
458 // Get the whole hub descriptor
459 //
460 return UsbHubCtrlGetHubDesc (HubDev, HubDesc, HubDesc->Length);
461 }
462
463
464
465 /**
466 Ack the hub change bits. If these bits are not ACKed, Hub will
467 always return changed bit map from its interrupt endpoint.
468
469 @param HubDev The hub device.
470
471 @retval EFI_SUCCESS The hub change status is ACKed.
472 @retval Others Failed to ACK the hub status.
473
474 **/
475 EFI_STATUS
476 UsbHubAckHubStatus (
477 IN USB_DEVICE *HubDev
478 )
479 {
480 EFI_USB_PORT_STATUS HubState;
481 EFI_STATUS Status;
482
483 Status = UsbHubCtrlGetHubStatus (HubDev, (UINT32 *) &HubState);
484
485 if (EFI_ERROR (Status)) {
486 return Status;
487 }
488
489 if (USB_BIT_IS_SET (HubState.PortChangeStatus, USB_HUB_STAT_C_LOCAL_POWER)) {
490 UsbHubCtrlClearHubFeature (HubDev, USB_HUB_C_HUB_LOCAL_POWER);
491 }
492
493 if (USB_BIT_IS_SET (HubState.PortChangeStatus, USB_HUB_STAT_C_OVER_CURRENT)) {
494 UsbHubCtrlClearHubFeature (HubDev, USB_HUB_C_HUB_OVER_CURRENT);
495 }
496
497 return EFI_SUCCESS;
498 }
499
500
501 /**
502 Test whether the interface is a hub interface.
503
504 @param UsbIf The interface to test.
505
506 @retval TRUE The interface is a hub interface.
507 @retval FALSE The interface isn't a hub interface.
508
509 **/
510 BOOLEAN
511 UsbIsHubInterface (
512 IN USB_INTERFACE *UsbIf
513 )
514 {
515 EFI_USB_INTERFACE_DESCRIPTOR *Setting;
516
517 //
518 // If the hub is a high-speed hub with multiple TT,
519 // the hub will has a default setting of single TT.
520 //
521 Setting = &UsbIf->IfSetting->Desc;
522
523 if ((Setting->InterfaceClass == USB_HUB_CLASS_CODE) &&
524 (Setting->InterfaceSubClass == USB_HUB_SUBCLASS_CODE)) {
525
526 return TRUE;
527 }
528
529 return FALSE;
530 }
531
532
533 /**
534 The callback function to the USB hub status change
535 interrupt endpoint. It is called periodically by
536 the underlying host controller.
537
538 @param Data The data read.
539 @param DataLength The length of the data read.
540 @param Context The context.
541 @param Result The result of the last interrupt transfer.
542
543 @retval EFI_SUCCESS The process is OK.
544 @retval EFI_OUT_OF_RESOURCES Failed to allocate resource.
545
546 **/
547 EFI_STATUS
548 EFIAPI
549 UsbOnHubInterrupt (
550 IN VOID *Data,
551 IN UINTN DataLength,
552 IN VOID *Context,
553 IN UINT32 Result
554 )
555 {
556 USB_INTERFACE *HubIf;
557 EFI_USB_IO_PROTOCOL *UsbIo;
558 EFI_USB_ENDPOINT_DESCRIPTOR *EpDesc;
559 EFI_STATUS Status;
560
561 HubIf = (USB_INTERFACE *) Context;
562 UsbIo = &(HubIf->UsbIo);
563 EpDesc = &(HubIf->HubEp->Desc);
564
565 if (Result != EFI_USB_NOERROR) {
566 //
567 // If endpoint is stalled, clear the stall. Use UsbIo to access
568 // the control transfer so internal status are maintained.
569 //
570 if (USB_BIT_IS_SET (Result, EFI_USB_ERR_STALL)) {
571 UsbIoClearFeature (
572 UsbIo,
573 USB_TARGET_ENDPOINT,
574 USB_FEATURE_ENDPOINT_HALT,
575 EpDesc->EndpointAddress
576 );
577 }
578
579 //
580 // Delete and submit a new async interrupt
581 //
582 Status = UsbIo->UsbAsyncInterruptTransfer (
583 UsbIo,
584 EpDesc->EndpointAddress,
585 FALSE,
586 0,
587 0,
588 NULL,
589 NULL
590 );
591
592 if (EFI_ERROR (Status)) {
593 DEBUG (( EFI_D_ERROR, "UsbOnHubInterrupt: failed to remove async transfer - %r\n", Status));
594 return Status;
595 }
596
597 Status = UsbIo->UsbAsyncInterruptTransfer (
598 UsbIo,
599 EpDesc->EndpointAddress,
600 TRUE,
601 USB_HUB_POLL_INTERVAL,
602 HubIf->NumOfPort / 8 + 1,
603 UsbOnHubInterrupt,
604 HubIf
605 );
606
607 if (EFI_ERROR (Status)) {
608 DEBUG (( EFI_D_ERROR, "UsbOnHubInterrupt: failed to submit new async transfer - %r\n", Status));
609 }
610
611 return Status;
612 }
613
614 if ((DataLength == 0) || (Data == NULL)) {
615 return EFI_SUCCESS;
616 }
617
618 //
619 // OK, actually something is changed, save the change map
620 // then signal the HUB to do enumeration. This is a good
621 // practise since UsbOnHubInterrupt is called in the context
622 // of host contrller's AsyncInterrupt monitor.
623 //
624 HubIf->ChangeMap = AllocateZeroPool (DataLength);
625
626 if (HubIf->ChangeMap == NULL) {
627 return EFI_OUT_OF_RESOURCES;
628 }
629
630 CopyMem (HubIf->ChangeMap, Data, DataLength);
631 gBS->SignalEvent (HubIf->HubNotify);
632
633 return EFI_SUCCESS;
634 }
635
636
637
638
639 /**
640 Initialize the device for a non-root hub.
641
642 @param HubIf The USB hub interface.
643
644 @retval EFI_SUCCESS The hub is initialized.
645 @retval EFI_DEVICE_ERROR Failed to initialize the hub.
646
647 **/
648 EFI_STATUS
649 UsbHubInit (
650 IN USB_INTERFACE *HubIf
651 )
652 {
653 UINT8 HubDescBuffer[256];
654 EFI_USB_HUB_DESCRIPTOR *HubDesc;
655 USB_ENDPOINT_DESC *EpDesc;
656 USB_INTERFACE_SETTING *Setting;
657 EFI_USB_IO_PROTOCOL *UsbIo;
658 USB_DEVICE *HubDev;
659 EFI_STATUS Status;
660 UINT8 Index;
661 UINT8 NumEndpoints;
662 UINT16 Depth;
663
664 //
665 // Locate the interrupt endpoint for port change map
666 //
667 HubIf->IsHub = FALSE;
668 Setting = HubIf->IfSetting;
669 HubDev = HubIf->Device;
670 EpDesc = NULL;
671 NumEndpoints = Setting->Desc.NumEndpoints;
672
673 for (Index = 0; Index < NumEndpoints; Index++) {
674 ASSERT ((Setting->Endpoints != NULL) && (Setting->Endpoints[Index] != NULL));
675
676 EpDesc = Setting->Endpoints[Index];
677
678 if (USB_BIT_IS_SET (EpDesc->Desc.EndpointAddress, USB_ENDPOINT_DIR_IN) &&
679 (USB_ENDPOINT_TYPE (&EpDesc->Desc) == USB_ENDPOINT_INTERRUPT)) {
680 break;
681 }
682 }
683
684 if (Index == NumEndpoints) {
685 DEBUG (( EFI_D_ERROR, "UsbHubInit: no interrupt endpoint found for hub %d\n", HubDev->Address));
686 return EFI_DEVICE_ERROR;
687 }
688
689 //
690 // The length field of descriptor is UINT8 type, so the buffer
691 // with 256 bytes is enough to hold the descriptor data.
692 //
693 HubDesc = (EFI_USB_HUB_DESCRIPTOR *) HubDescBuffer;
694 Status = UsbHubReadDesc (HubDev, HubDesc);
695
696 if (EFI_ERROR (Status)) {
697 DEBUG (( EFI_D_ERROR, "UsbHubInit: failed to read HUB descriptor %r\n", Status));
698 return Status;
699 }
700
701 HubIf->NumOfPort = HubDesc->NumPorts;
702
703 DEBUG (( EFI_D_INFO, "UsbHubInit: hub %d has %d ports\n", HubDev->Address,HubIf->NumOfPort));
704
705 //
706 // OK, set IsHub to TRUE. Now usb bus can handle this device
707 // as a working HUB. If failed eariler, bus driver will not
708 // recognize it as a hub. Other parts of the bus should be able
709 // to work.
710 //
711 HubIf->IsHub = TRUE;
712 HubIf->HubApi = &mUsbHubApi;
713 HubIf->HubEp = EpDesc;
714
715 if (HubIf->Device->Speed == EFI_USB_SPEED_SUPER) {
716 Depth = (UINT16)(HubIf->Device->Tier - 1);
717 DEBUG ((EFI_D_INFO, "UsbHubInit: Set Hub Depth as 0x%x\n", Depth));
718 UsbHubCtrlSetHubDepth (HubIf->Device, Depth);
719
720 for (Index = 0; Index < HubDesc->NumPorts; Index++) {
721 UsbHubCtrlSetPortFeature (HubIf->Device, Index, USB_HUB_PORT_REMOTE_WAKE_MASK);
722 }
723 } else {
724 //
725 // Feed power to all the hub ports. It should be ok
726 // for both gang/individual powered hubs.
727 //
728 for (Index = 0; Index < HubDesc->NumPorts; Index++) {
729 UsbHubCtrlSetPortFeature (HubIf->Device, Index, (EFI_USB_PORT_FEATURE) USB_HUB_PORT_POWER);
730 }
731
732 //
733 // Update for the usb hub has no power on delay requirement
734 //
735 if (HubDesc->PwrOn2PwrGood > 0) {
736 gBS->Stall (HubDesc->PwrOn2PwrGood * USB_SET_PORT_POWER_STALL);
737 }
738 UsbHubAckHubStatus (HubIf->Device);
739 }
740
741 //
742 // Create an event to enumerate the hub's port. On
743 //
744 Status = gBS->CreateEvent (
745 EVT_NOTIFY_SIGNAL,
746 TPL_CALLBACK,
747 UsbHubEnumeration,
748 HubIf,
749 &HubIf->HubNotify
750 );
751
752 if (EFI_ERROR (Status)) {
753 DEBUG (( EFI_D_ERROR, "UsbHubInit: failed to create signal for hub %d - %r\n",
754 HubDev->Address, Status));
755
756 return Status;
757 }
758
759 //
760 // Create AsyncInterrupt to query hub port change endpoint
761 // periodically. If the hub ports are changed, hub will return
762 // changed port map from the interrupt endpoint. The port map
763 // must be able to hold (HubIf->NumOfPort + 1) bits (one bit for
764 // host change status).
765 //
766 UsbIo = &HubIf->UsbIo;
767 Status = UsbIo->UsbAsyncInterruptTransfer (
768 UsbIo,
769 EpDesc->Desc.EndpointAddress,
770 TRUE,
771 USB_HUB_POLL_INTERVAL,
772 HubIf->NumOfPort / 8 + 1,
773 UsbOnHubInterrupt,
774 HubIf
775 );
776
777 if (EFI_ERROR (Status)) {
778 DEBUG (( EFI_D_ERROR, "UsbHubInit: failed to queue interrupt transfer for hub %d - %r\n",
779 HubDev->Address, Status));
780
781 gBS->CloseEvent (HubIf->HubNotify);
782 HubIf->HubNotify = NULL;
783
784 return Status;
785 }
786
787 DEBUG (( EFI_D_INFO, "UsbHubInit: hub %d initialized\n", HubDev->Address));
788 return Status;
789 }
790
791
792
793 /**
794 Get the port status. This function is required to
795 ACK the port change bits although it will return
796 the port changes in PortState. Bus enumeration code
797 doesn't need to ACK the port change bits.
798
799 @param HubIf The hub interface.
800 @param Port The port of the hub to get state.
801 @param PortState Variable to return the port state.
802
803 @retval EFI_SUCCESS The port status is successfully returned.
804 @retval Others Failed to return the status.
805
806 **/
807 EFI_STATUS
808 UsbHubGetPortStatus (
809 IN USB_INTERFACE *HubIf,
810 IN UINT8 Port,
811 OUT EFI_USB_PORT_STATUS *PortState
812 )
813 {
814 EFI_STATUS Status;
815
816 Status = UsbHubCtrlGetPortStatus (HubIf->Device, Port, PortState);
817
818 return Status;
819 }
820
821
822
823 /**
824 Clear the port change status.
825
826 @param HubIf The hub interface.
827 @param Port The hub port.
828
829 **/
830 VOID
831 UsbHubClearPortChange (
832 IN USB_INTERFACE *HubIf,
833 IN UINT8 Port
834 )
835 {
836 EFI_USB_PORT_STATUS PortState;
837 USB_CHANGE_FEATURE_MAP *Map;
838 UINTN Index;
839 EFI_STATUS Status;
840
841 Status = UsbHubGetPortStatus (HubIf, Port, &PortState);
842
843 if (EFI_ERROR (Status)) {
844 return;
845 }
846
847 //
848 // OK, get the usb port status, now ACK the change bits.
849 // Don't return error when failed to clear the change bits.
850 // It may lead to extra port state report. USB bus should
851 // be able to handle this.
852 //
853 for (Index = 0; Index < ARRAY_SIZE (mHubFeatureMap); Index++) {
854 Map = &mHubFeatureMap[Index];
855
856 if (USB_BIT_IS_SET (PortState.PortChangeStatus, Map->ChangedBit)) {
857 UsbHubCtrlClearPortFeature (HubIf->Device, Port, (UINT16) Map->Feature);
858 }
859 }
860 }
861
862
863
864 /**
865 Function to set the port feature for non-root hub.
866
867 @param HubIf The hub interface.
868 @param Port The port of the hub.
869 @param Feature The feature of the port to set.
870
871 @retval EFI_SUCCESS The hub port feature is set.
872 @retval Others Failed to set the port feature.
873
874 **/
875 EFI_STATUS
876 UsbHubSetPortFeature (
877 IN USB_INTERFACE *HubIf,
878 IN UINT8 Port,
879 IN EFI_USB_PORT_FEATURE Feature
880 )
881 {
882 EFI_STATUS Status;
883
884 Status = UsbHubCtrlSetPortFeature (HubIf->Device, Port, (UINT8) Feature);
885
886 return Status;
887 }
888
889
890 /**
891 Interface function to clear the port feature for non-root hub.
892
893 @param HubIf The hub interface.
894 @param Port The port of the hub to clear feature for.
895 @param Feature The feature to clear.
896
897 @retval EFI_SUCCESS The port feature is cleared.
898 @retval Others Failed to clear the port feature.
899
900 **/
901 EFI_STATUS
902 UsbHubClearPortFeature (
903 IN USB_INTERFACE *HubIf,
904 IN UINT8 Port,
905 IN EFI_USB_PORT_FEATURE Feature
906 )
907 {
908 EFI_STATUS Status;
909
910 Status = UsbHubCtrlClearPortFeature (HubIf->Device, Port, (UINT8) Feature);
911
912 return Status;
913 }
914
915
916 /**
917 Interface function to reset the port.
918
919 @param HubIf The hub interface.
920 @param Port The port to reset.
921
922 @retval EFI_SUCCESS The hub port is reset.
923 @retval EFI_TIMEOUT Failed to reset the port in time.
924 @retval Others Failed to reset the port.
925
926 **/
927 EFI_STATUS
928 UsbHubResetPort (
929 IN USB_INTERFACE *HubIf,
930 IN UINT8 Port
931 )
932 {
933 EFI_USB_PORT_STATUS PortState;
934 UINTN Index;
935 EFI_STATUS Status;
936
937 Status = UsbHubSetPortFeature (HubIf, Port, (EFI_USB_PORT_FEATURE) USB_HUB_PORT_RESET);
938
939 if (EFI_ERROR (Status)) {
940 return Status;
941 }
942
943 //
944 // Drive the reset signal for worst 20ms. Check USB 2.0 Spec
945 // section 7.1.7.5 for timing requirements.
946 //
947 gBS->Stall (USB_SET_PORT_RESET_STALL);
948
949 //
950 // Check USB_PORT_STAT_C_RESET bit to see if the resetting state is done.
951 //
952 ZeroMem (&PortState, sizeof (EFI_USB_PORT_STATUS));
953
954 for (Index = 0; Index < USB_WAIT_PORT_STS_CHANGE_LOOP; Index++) {
955 Status = UsbHubGetPortStatus (HubIf, Port, &PortState);
956
957 if (EFI_ERROR (Status)) {
958 return Status;
959 }
960
961 if (!EFI_ERROR (Status) &&
962 USB_BIT_IS_SET (PortState.PortChangeStatus, USB_PORT_STAT_C_RESET)) {
963 gBS->Stall (USB_SET_PORT_RECOVERY_STALL);
964 return EFI_SUCCESS;
965 }
966
967 gBS->Stall (USB_WAIT_PORT_STS_CHANGE_STALL);
968 }
969
970 return EFI_TIMEOUT;
971 }
972
973
974 /**
975 Release the hub's control of the interface.
976
977 @param HubIf The hub interface.
978
979 @retval EFI_SUCCESS The interface is release of hub control.
980
981 **/
982 EFI_STATUS
983 UsbHubRelease (
984 IN USB_INTERFACE *HubIf
985 )
986 {
987 EFI_USB_IO_PROTOCOL *UsbIo;
988 EFI_STATUS Status;
989
990 UsbIo = &HubIf->UsbIo;
991 Status = UsbIo->UsbAsyncInterruptTransfer (
992 UsbIo,
993 HubIf->HubEp->Desc.EndpointAddress,
994 FALSE,
995 USB_HUB_POLL_INTERVAL,
996 0,
997 NULL,
998 0
999 );
1000
1001 if (EFI_ERROR (Status)) {
1002 return Status;
1003 }
1004
1005 gBS->CloseEvent (HubIf->HubNotify);
1006
1007 HubIf->IsHub = FALSE;
1008 HubIf->HubApi = NULL;
1009 HubIf->HubEp = NULL;
1010 HubIf->HubNotify = NULL;
1011
1012 DEBUG (( EFI_D_INFO, "UsbHubRelease: hub device %d released\n", HubIf->Device->Address));
1013 return EFI_SUCCESS;
1014 }
1015
1016
1017
1018 /**
1019 Initialize the interface for root hub.
1020
1021 @param HubIf The root hub interface.
1022
1023 @retval EFI_SUCCESS The interface is initialized for root hub.
1024 @retval Others Failed to initialize the hub.
1025
1026 **/
1027 EFI_STATUS
1028 UsbRootHubInit (
1029 IN USB_INTERFACE *HubIf
1030 )
1031 {
1032 EFI_STATUS Status;
1033 UINT8 MaxSpeed;
1034 UINT8 NumOfPort;
1035 UINT8 Support64;
1036
1037 Status = UsbHcGetCapability (HubIf->Device->Bus, &MaxSpeed, &NumOfPort, &Support64);
1038
1039 if (EFI_ERROR (Status)) {
1040 return Status;
1041 }
1042
1043 DEBUG (( EFI_D_INFO, "UsbRootHubInit: root hub %p - max speed %d, %d ports\n",
1044 HubIf, MaxSpeed, NumOfPort));
1045
1046 HubIf->IsHub = TRUE;
1047 HubIf->HubApi = &mUsbRootHubApi;
1048 HubIf->HubEp = NULL;
1049 HubIf->MaxSpeed = MaxSpeed;
1050 HubIf->NumOfPort = NumOfPort;
1051 HubIf->HubNotify = NULL;
1052
1053 //
1054 // Create a timer to poll root hub ports periodically
1055 //
1056 Status = gBS->CreateEvent (
1057 EVT_TIMER | EVT_NOTIFY_SIGNAL,
1058 TPL_CALLBACK,
1059 UsbRootHubEnumeration,
1060 HubIf,
1061 &HubIf->HubNotify
1062 );
1063
1064 if (EFI_ERROR (Status)) {
1065 return Status;
1066 }
1067
1068 //
1069 // It should signal the event immediately here, or device detection
1070 // by bus enumeration might be delayed by the timer interval.
1071 //
1072 gBS->SignalEvent (HubIf->HubNotify);
1073
1074 Status = gBS->SetTimer (
1075 HubIf->HubNotify,
1076 TimerPeriodic,
1077 USB_ROOTHUB_POLL_INTERVAL
1078 );
1079
1080 if (EFI_ERROR (Status)) {
1081 gBS->CloseEvent (HubIf->HubNotify);
1082 }
1083
1084 return Status;
1085 }
1086
1087
1088 /**
1089 Get the port status. This function is required to
1090 ACK the port change bits although it will return
1091 the port changes in PortState. Bus enumeration code
1092 doesn't need to ACK the port change bits.
1093
1094 @param HubIf The root hub interface.
1095 @param Port The root hub port to get the state.
1096 @param PortState Variable to return the port state.
1097
1098 @retval EFI_SUCCESS The port state is returned.
1099 @retval Others Failed to retrieve the port state.
1100
1101 **/
1102 EFI_STATUS
1103 UsbRootHubGetPortStatus (
1104 IN USB_INTERFACE *HubIf,
1105 IN UINT8 Port,
1106 OUT EFI_USB_PORT_STATUS *PortState
1107 )
1108 {
1109 USB_BUS *Bus;
1110 EFI_STATUS Status;
1111
1112 Bus = HubIf->Device->Bus;
1113 Status = UsbHcGetRootHubPortStatus (Bus, Port, PortState);
1114
1115 return Status;
1116 }
1117
1118
1119 /**
1120 Clear the port change status.
1121
1122 @param HubIf The root hub interface.
1123 @param Port The root hub port.
1124
1125 **/
1126 VOID
1127 UsbRootHubClearPortChange (
1128 IN USB_INTERFACE *HubIf,
1129 IN UINT8 Port
1130 )
1131 {
1132 EFI_USB_PORT_STATUS PortState;
1133 USB_CHANGE_FEATURE_MAP *Map;
1134 UINTN Index;
1135 EFI_STATUS Status;
1136
1137 Status = UsbRootHubGetPortStatus (HubIf, Port, &PortState);
1138
1139 if (EFI_ERROR (Status)) {
1140 return;
1141 }
1142
1143 //
1144 // OK, get the usb port status, now ACK the change bits.
1145 // Don't return error when failed to clear the change bits.
1146 // It may lead to extra port state report. USB bus should
1147 // be able to handle this.
1148 //
1149 for (Index = 0; Index < ARRAY_SIZE (mRootHubFeatureMap); Index++) {
1150 Map = &mRootHubFeatureMap[Index];
1151
1152 if (USB_BIT_IS_SET (PortState.PortChangeStatus, Map->ChangedBit)) {
1153 UsbHcClearRootHubPortFeature (HubIf->Device->Bus, Port, (EFI_USB_PORT_FEATURE) Map->Feature);
1154 }
1155 }
1156 }
1157
1158
1159 /**
1160 Set the root hub port feature.
1161
1162 @param HubIf The Usb hub interface.
1163 @param Port The hub port.
1164 @param Feature The feature to set.
1165
1166 @retval EFI_SUCCESS The root hub port is set with the feature.
1167 @retval Others Failed to set the feature.
1168
1169 **/
1170 EFI_STATUS
1171 UsbRootHubSetPortFeature (
1172 IN USB_INTERFACE *HubIf,
1173 IN UINT8 Port,
1174 IN EFI_USB_PORT_FEATURE Feature
1175 )
1176 {
1177 EFI_STATUS Status;
1178
1179 Status = UsbHcSetRootHubPortFeature (HubIf->Device->Bus, Port, Feature);
1180
1181 return Status;
1182 }
1183
1184
1185 /**
1186 Clear the root hub port feature.
1187
1188 @param HubIf The root hub interface.
1189 @param Port The root hub port.
1190 @param Feature The feature to clear.
1191
1192 @retval EFI_SUCCESS The root hub port is cleared of the feature.
1193 @retval Others Failed to clear the feature.
1194
1195 **/
1196 EFI_STATUS
1197 UsbRootHubClearPortFeature (
1198 IN USB_INTERFACE *HubIf,
1199 IN UINT8 Port,
1200 IN EFI_USB_PORT_FEATURE Feature
1201 )
1202 {
1203 EFI_STATUS Status;
1204
1205 Status = UsbHcClearRootHubPortFeature (HubIf->Device->Bus, Port, Feature);
1206
1207 return Status;
1208 }
1209
1210
1211 /**
1212 Interface function to reset the root hub port.
1213
1214 @param RootIf The root hub interface.
1215 @param Port The port to reset.
1216
1217 @retval EFI_SUCCESS The hub port is reset.
1218 @retval EFI_TIMEOUT Failed to reset the port in time.
1219 @retval EFI_NOT_FOUND The low/full speed device connected to high speed.
1220 root hub is released to the companion UHCI.
1221 @retval Others Failed to reset the port.
1222
1223 **/
1224 EFI_STATUS
1225 UsbRootHubResetPort (
1226 IN USB_INTERFACE *RootIf,
1227 IN UINT8 Port
1228 )
1229 {
1230 USB_BUS *Bus;
1231 EFI_STATUS Status;
1232 EFI_USB_PORT_STATUS PortState;
1233 UINTN Index;
1234
1235 //
1236 // Notice: although EHCI requires that ENABLED bit be cleared
1237 // when reset the port, we don't need to care that here. It
1238 // should be handled in the EHCI driver.
1239 //
1240 Bus = RootIf->Device->Bus;
1241
1242 Status = UsbHcSetRootHubPortFeature (Bus, Port, EfiUsbPortReset);
1243
1244 if (EFI_ERROR (Status)) {
1245 DEBUG (( EFI_D_ERROR, "UsbRootHubResetPort: failed to start reset on port %d\n", Port));
1246 return Status;
1247 }
1248
1249 //
1250 // Drive the reset signal for at least 50ms. Check USB 2.0 Spec
1251 // section 7.1.7.5 for timing requirements.
1252 //
1253 gBS->Stall (USB_SET_ROOT_PORT_RESET_STALL);
1254
1255 Status = UsbHcClearRootHubPortFeature (Bus, Port, EfiUsbPortReset);
1256
1257 if (EFI_ERROR (Status)) {
1258 DEBUG (( EFI_D_ERROR, "UsbRootHubResetPort: failed to clear reset on port %d\n", Port));
1259 return Status;
1260 }
1261
1262 gBS->Stall (USB_CLR_ROOT_PORT_RESET_STALL);
1263
1264 //
1265 // USB host controller won't clear the RESET bit until
1266 // reset is actually finished.
1267 //
1268 ZeroMem (&PortState, sizeof (EFI_USB_PORT_STATUS));
1269
1270 for (Index = 0; Index < USB_WAIT_PORT_STS_CHANGE_LOOP; Index++) {
1271 Status = UsbHcGetRootHubPortStatus (Bus, Port, &PortState);
1272
1273 if (EFI_ERROR (Status)) {
1274 return Status;
1275 }
1276
1277 if (!USB_BIT_IS_SET (PortState.PortStatus, USB_PORT_STAT_RESET)) {
1278 break;
1279 }
1280
1281 gBS->Stall (USB_WAIT_PORT_STS_CHANGE_STALL);
1282 }
1283
1284 if (Index == USB_WAIT_PORT_STS_CHANGE_LOOP) {
1285 DEBUG ((EFI_D_ERROR, "UsbRootHubResetPort: reset not finished in time on port %d\n", Port));
1286 return EFI_TIMEOUT;
1287 }
1288
1289 if (!USB_BIT_IS_SET (PortState.PortStatus, USB_PORT_STAT_ENABLE)) {
1290 //
1291 // OK, the port is reset. If root hub is of high speed and
1292 // the device is of low/full speed, release the ownership to
1293 // companion UHCI. If root hub is of full speed, it won't
1294 // automatically enable the port, we need to enable it manually.
1295 //
1296 if (RootIf->MaxSpeed == EFI_USB_SPEED_HIGH) {
1297 DEBUG (( EFI_D_ERROR, "UsbRootHubResetPort: release low/full speed device (%d) to UHCI\n", Port));
1298
1299 UsbRootHubSetPortFeature (RootIf, Port, EfiUsbPortOwner);
1300 return EFI_NOT_FOUND;
1301
1302 } else {
1303
1304 Status = UsbRootHubSetPortFeature (RootIf, Port, EfiUsbPortEnable);
1305
1306 if (EFI_ERROR (Status)) {
1307 DEBUG (( EFI_D_ERROR, "UsbRootHubResetPort: failed to enable port %d for UHCI\n", Port));
1308 return Status;
1309 }
1310
1311 gBS->Stall (USB_SET_ROOT_PORT_ENABLE_STALL);
1312 }
1313 }
1314
1315 return EFI_SUCCESS;
1316 }
1317
1318
1319 /**
1320 Release the root hub's control of the interface.
1321
1322 @param HubIf The root hub interface.
1323
1324 @retval EFI_SUCCESS The root hub's control of the interface is
1325 released.
1326
1327 **/
1328 EFI_STATUS
1329 UsbRootHubRelease (
1330 IN USB_INTERFACE *HubIf
1331 )
1332 {
1333 DEBUG (( EFI_D_INFO, "UsbRootHubRelease: root hub released for hub %p\n", HubIf));
1334
1335 gBS->SetTimer (HubIf->HubNotify, TimerCancel, USB_ROOTHUB_POLL_INTERVAL);
1336 gBS->CloseEvent (HubIf->HubNotify);
1337
1338 return EFI_SUCCESS;
1339 }
1340
1341 USB_HUB_API mUsbHubApi = {
1342 UsbHubInit,
1343 UsbHubGetPortStatus,
1344 UsbHubClearPortChange,
1345 UsbHubSetPortFeature,
1346 UsbHubClearPortFeature,
1347 UsbHubResetPort,
1348 UsbHubRelease
1349 };
1350
1351 USB_HUB_API mUsbRootHubApi = {
1352 UsbRootHubInit,
1353 UsbRootHubGetPortStatus,
1354 UsbRootHubClearPortChange,
1355 UsbRootHubSetPortFeature,
1356 UsbRootHubClearPortFeature,
1357 UsbRootHubResetPort,
1358 UsbRootHubRelease
1359 };