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