]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Bus/Usb/UsbBusDxe/UsbHub.c
Pass ECC check.
[mirror_edk2.git] / MdeModulePkg / Bus / Usb / UsbBusDxe / UsbHub.c
1 /** @file
2
3 Unified interface for RootHub and Hub.
4
5 Copyright (c) 2007, 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 UsbOnHubInterrupt (
520 IN VOID *Data,
521 IN UINTN DataLength,
522 IN VOID *Context,
523 IN UINT32 Result
524 )
525 {
526 USB_INTERFACE *HubIf;
527 EFI_USB_IO_PROTOCOL *UsbIo;
528 EFI_USB_ENDPOINT_DESCRIPTOR *EpDesc;
529 EFI_STATUS Status;
530
531 HubIf = (USB_INTERFACE *) Context;
532 UsbIo = &(HubIf->UsbIo);
533 EpDesc = &(HubIf->HubEp->Desc);
534
535 if (Result != EFI_USB_NOERROR) {
536 //
537 // If endpoint is stalled, clear the stall. Use UsbIo to access
538 // the control transfer so internal status are maintained.
539 //
540 if (USB_BIT_IS_SET (Result, EFI_USB_ERR_STALL)) {
541 UsbIoClearFeature (
542 UsbIo,
543 USB_TARGET_ENDPOINT,
544 USB_FEATURE_ENDPOINT_HALT,
545 EpDesc->EndpointAddress
546 );
547 }
548
549 //
550 // Delete and submit a new async interrupt
551 //
552 Status = UsbIo->UsbAsyncInterruptTransfer (
553 UsbIo,
554 EpDesc->EndpointAddress,
555 FALSE,
556 0,
557 0,
558 NULL,
559 NULL
560 );
561
562 if (EFI_ERROR (Status)) {
563 DEBUG (( EFI_D_ERROR, "UsbOnHubInterrupt: failed to remove async transfer - %r\n", Status));
564 return Status;
565 }
566
567 Status = UsbIo->UsbAsyncInterruptTransfer (
568 UsbIo,
569 EpDesc->EndpointAddress,
570 TRUE,
571 USB_HUB_POLL_INTERVAL,
572 HubIf->NumOfPort / 8 + 1,
573 UsbOnHubInterrupt,
574 HubIf
575 );
576
577 if (EFI_ERROR (Status)) {
578 DEBUG (( EFI_D_ERROR, "UsbOnHubInterrupt: failed to submit new async transfer - %r\n", Status));
579 }
580
581 return Status;
582 }
583
584 if ((DataLength == 0) || (Data == NULL)) {
585 return EFI_SUCCESS;
586 }
587
588 //
589 // OK, actually something is changed, save the change map
590 // then signal the HUB to do enumeration. This is a good
591 // practise since UsbOnHubInterrupt is called in the context
592 // of host contrller's AsyncInterrupt monitor.
593 //
594 HubIf->ChangeMap = AllocateZeroPool (DataLength);
595
596 if (HubIf->ChangeMap == NULL) {
597 return EFI_OUT_OF_RESOURCES;
598 }
599
600 CopyMem (HubIf->ChangeMap, Data, DataLength);
601 gBS->SignalEvent (HubIf->HubNotify);
602
603 return EFI_SUCCESS;
604 }
605
606
607
608
609 /**
610 Initialize the device for a non-root hub.
611
612 @param HubIf The USB hub interface.
613
614 @retval EFI_SUCCESS The hub is initialized.
615 @retval EFI_DEVICE_ERROR Failed to initialize the hub.
616
617 **/
618 EFI_STATUS
619 UsbHubInit (
620 IN USB_INTERFACE *HubIf
621 )
622 {
623 EFI_USB_HUB_DESCRIPTOR HubDesc;
624 USB_ENDPOINT_DESC *EpDesc;
625 USB_INTERFACE_SETTING *Setting;
626 EFI_USB_IO_PROTOCOL *UsbIo;
627 USB_DEVICE *HubDev;
628 EFI_STATUS Status;
629 UINT8 Index;
630 UINT8 NumEndpoints;
631
632 //
633 // Locate the interrupt endpoint for port change map
634 //
635 HubIf->IsHub = FALSE;
636 Setting = HubIf->IfSetting;
637 HubDev = HubIf->Device;
638 EpDesc = NULL;
639 NumEndpoints = Setting->Desc.NumEndpoints;
640
641 for (Index = 0; Index < NumEndpoints; Index++) {
642 ASSERT ((Setting->Endpoints != NULL) && (Setting->Endpoints[Index] != NULL));
643
644 EpDesc = Setting->Endpoints[Index];
645
646 if (USB_BIT_IS_SET (EpDesc->Desc.EndpointAddress, USB_ENDPOINT_DIR_IN) &&
647 (USB_ENDPOINT_TYPE (&EpDesc->Desc) == USB_ENDPOINT_INTERRUPT)) {
648 break;
649 }
650 }
651
652 if (Index == NumEndpoints) {
653 DEBUG (( EFI_D_ERROR, "UsbHubInit: no interrupt endpoint found for hub %d\n", HubDev->Address));
654 return EFI_DEVICE_ERROR;
655 }
656
657 Status = UsbHubReadDesc (HubDev, &HubDesc);
658
659 if (EFI_ERROR (Status)) {
660 DEBUG (( EFI_D_ERROR, "UsbHubInit: failed to read HUB descriptor %r\n", Status));
661 return Status;
662 }
663
664 HubIf->NumOfPort = HubDesc.NumPorts;
665
666 DEBUG (( EFI_D_INFO, "UsbHubInit: hub %d has %d ports\n", HubDev->Address,HubIf->NumOfPort));
667
668 //
669 // Create an event to enumerate the hub's port. On
670 //
671 Status = gBS->CreateEvent (
672 EVT_NOTIFY_SIGNAL,
673 TPL_CALLBACK,
674 UsbHubEnumeration,
675 HubIf,
676 &HubIf->HubNotify
677 );
678
679 if (EFI_ERROR (Status)) {
680 DEBUG (( EFI_D_ERROR, "UsbHubInit: failed to create signal for hub %d - %r\n",
681 HubDev->Address, Status));
682
683 return Status;
684 }
685
686 //
687 // Create AsyncInterrupt to query hub port change endpoint
688 // periodically. If the hub ports are changed, hub will return
689 // changed port map from the interrupt endpoint. The port map
690 // must be able to hold (HubIf->NumOfPort + 1) bits (one bit for
691 // host change status).
692 //
693 UsbIo = &HubIf->UsbIo;
694 Status = UsbIo->UsbAsyncInterruptTransfer (
695 UsbIo,
696 EpDesc->Desc.EndpointAddress,
697 TRUE,
698 USB_HUB_POLL_INTERVAL,
699 HubIf->NumOfPort / 8 + 1,
700 UsbOnHubInterrupt,
701 HubIf
702 );
703
704 if (EFI_ERROR (Status)) {
705 DEBUG (( EFI_D_ERROR, "UsbHubInit: failed to queue interrupt transfer for hub %d - %r\n",
706 HubDev->Address, Status));
707
708 gBS->CloseEvent (HubIf->HubNotify);
709 HubIf->HubNotify = NULL;
710
711 return Status;
712 }
713
714 //
715 // OK, set IsHub to TRUE. Now usb bus can handle this device
716 // as a working HUB. If failed eariler, bus driver will not
717 // recognize it as a hub. Other parts of the bus should be able
718 // to work.
719 //
720 HubIf->IsHub = TRUE;
721 HubIf->HubApi = &mUsbHubApi;
722 HubIf->HubEp = EpDesc;
723
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 gBS->Stall (HubDesc.PwrOn2PwrGood * USB_SET_PORT_POWER_STALL);
733 UsbHubAckHubStatus (HubIf->Device);
734
735 DEBUG (( EFI_D_INFO, "UsbHubInit: hub %d initialized\n", HubDev->Address));
736 return Status;
737 }
738
739
740
741 /**
742 Get the port status. This function is required to
743 ACK the port change bits although it will return
744 the port changes in PortState. Bus enumeration code
745 doesn't need to ACK the port change bits.
746
747 @param HubIf The hub interface.
748 @param Port The port of the hub to get state.
749 @param PortState Variable to return the port state.
750
751 @retval EFI_SUCCESS The port status is successfully returned.
752 @retval Others Failed to return the status.
753
754 **/
755 EFI_STATUS
756 UsbHubGetPortStatus (
757 IN USB_INTERFACE *HubIf,
758 IN UINT8 Port,
759 OUT EFI_USB_PORT_STATUS *PortState
760 )
761 {
762 EFI_STATUS Status;
763
764 Status = UsbHubCtrlGetPortStatus (HubIf->Device, Port, PortState);
765
766 return Status;
767 }
768
769
770
771 /**
772 Clear the port change status.
773
774 @param HubIf The hub interface.
775 @param Port The hub port.
776
777 **/
778 VOID
779 UsbHubClearPortChange (
780 IN USB_INTERFACE *HubIf,
781 IN UINT8 Port
782 )
783 {
784 EFI_USB_PORT_STATUS PortState;
785 USB_CHANGE_FEATURE_MAP *Map;
786 UINTN Index;
787 EFI_STATUS Status;
788
789 Status = UsbHubGetPortStatus (HubIf, Port, &PortState);
790
791 if (EFI_ERROR (Status)) {
792 return;
793 }
794
795 //
796 // OK, get the usb port status, now ACK the change bits.
797 // Don't return error when failed to clear the change bits.
798 // It may lead to extra port state report. USB bus should
799 // be able to handle this.
800 //
801 for (Index = 0; Index < USB_HUB_MAP_SIZE; Index++) {
802 Map = &mHubFeatureMap[Index];
803
804 if (USB_BIT_IS_SET (PortState.PortChangeStatus, Map->ChangedBit)) {
805 UsbHubCtrlClearPortFeature (HubIf->Device, Port, (UINT16) Map->Feature);
806 }
807 }
808 }
809
810
811
812 /**
813 Function to set the port feature for non-root hub.
814
815 @param HubIf The hub interface.
816 @param Port The port of the hub.
817 @param Feature The feature of the port to set.
818
819 @retval EFI_SUCCESS The hub port feature is set.
820 @retval Others Failed to set the port feature.
821
822 **/
823 EFI_STATUS
824 UsbHubSetPortFeature (
825 IN USB_INTERFACE *HubIf,
826 IN UINT8 Port,
827 IN EFI_USB_PORT_FEATURE Feature
828 )
829 {
830 EFI_STATUS Status;
831
832 Status = UsbHubCtrlSetPortFeature (HubIf->Device, Port, (UINT8) Feature);
833
834 return Status;
835 }
836
837
838 /**
839 Interface function to clear the port feature for non-root hub.
840
841 @param HubIf The hub interface.
842 @param Port The port of the hub to clear feature for.
843 @param Feature The feature to clear.
844
845 @retval EFI_SUCCESS The port feature is cleared.
846 @retval Others Failed to clear the port feature.
847
848 **/
849 EFI_STATUS
850 UsbHubClearPortFeature (
851 IN USB_INTERFACE *HubIf,
852 IN UINT8 Port,
853 IN EFI_USB_PORT_FEATURE Feature
854 )
855 {
856 EFI_STATUS Status;
857
858 Status = UsbHubCtrlClearPortFeature (HubIf->Device, Port, (UINT8) Feature);
859
860 return Status;
861 }
862
863
864 /**
865 Interface function to reset the port.
866
867 @param HubIf The hub interface.
868 @param Port The port to reset.
869
870 @retval EFI_SUCCESS The hub port is reset.
871 @retval EFI_TIMEOUT Failed to reset the port in time.
872 @retval Others Failed to reset the port.
873
874 **/
875 EFI_STATUS
876 UsbHubResetPort (
877 IN USB_INTERFACE *HubIf,
878 IN UINT8 Port
879 )
880 {
881 EFI_USB_PORT_STATUS PortState;
882 UINTN Index;
883 EFI_STATUS Status;
884
885 Status = UsbHubSetPortFeature (HubIf, Port, (EFI_USB_PORT_FEATURE) USB_HUB_PORT_RESET);
886
887 if (EFI_ERROR (Status)) {
888 return Status;
889 }
890
891 //
892 // Drive the reset signal for at least 10ms. Check USB 2.0 Spec
893 // section 7.1.7.5 for timing requirements.
894 //
895 gBS->Stall (USB_SET_PORT_RESET_STALL);
896
897 //
898 // USB hub will clear RESET bit if reset is actually finished.
899 //
900 ZeroMem (&PortState, sizeof (EFI_USB_PORT_STATUS));
901
902 for (Index = 0; Index < USB_WAIT_PORT_STS_CHANGE_LOOP; Index++) {
903 Status = UsbHubGetPortStatus (HubIf, Port, &PortState);
904
905 if (!EFI_ERROR (Status) &&
906 !USB_BIT_IS_SET (PortState.PortStatus, USB_PORT_STAT_RESET)) {
907
908 return EFI_SUCCESS;
909 }
910
911 gBS->Stall (USB_WAIT_PORT_STS_CHANGE_STALL);
912 }
913
914 return EFI_TIMEOUT;
915 }
916
917
918 /**
919 Release the hub's control of the interface.
920
921 @param HubIf The hub interface.
922
923 @retval EFI_SUCCESS The interface is release of hub control.
924
925 **/
926 EFI_STATUS
927 UsbHubRelease (
928 IN USB_INTERFACE *HubIf
929 )
930 {
931 EFI_USB_IO_PROTOCOL *UsbIo;
932 EFI_STATUS Status;
933
934 UsbIo = &HubIf->UsbIo;
935 Status = UsbIo->UsbAsyncInterruptTransfer (
936 UsbIo,
937 HubIf->HubEp->Desc.EndpointAddress,
938 FALSE,
939 USB_HUB_POLL_INTERVAL,
940 0,
941 NULL,
942 0
943 );
944
945 if (EFI_ERROR (Status)) {
946 return Status;
947 }
948
949 gBS->CloseEvent (HubIf->HubNotify);
950
951 HubIf->IsHub = FALSE;
952 HubIf->HubApi = NULL;
953 HubIf->HubEp = NULL;
954 HubIf->HubNotify = NULL;
955
956 DEBUG (( EFI_D_INFO, "UsbHubRelease: hub device %d released\n", HubIf->Device->Address));
957 return EFI_SUCCESS;
958 }
959
960
961
962 /**
963 Initialize the interface for root hub.
964
965 @param HubIf The root hub interface.
966
967 @retval EFI_SUCCESS The interface is initialized for root hub.
968 @retval Others Failed to initialize the hub.
969
970 **/
971 EFI_STATUS
972 UsbRootHubInit (
973 IN USB_INTERFACE *HubIf
974 )
975 {
976 EFI_STATUS Status;
977 UINT8 MaxSpeed;
978 UINT8 NumOfPort;
979 UINT8 Support64;
980
981 Status = UsbHcGetCapability (HubIf->Device->Bus, &MaxSpeed, &NumOfPort, &Support64);
982
983 if (EFI_ERROR (Status)) {
984 return Status;
985 }
986
987 DEBUG (( EFI_D_INFO, "UsbRootHubInit: root hub %p - max speed %d, %d ports\n",
988 HubIf, MaxSpeed, NumOfPort));
989
990 HubIf->IsHub = TRUE;
991 HubIf->HubApi = &mUsbRootHubApi;
992 HubIf->HubEp = NULL;
993 HubIf->MaxSpeed = MaxSpeed;
994 HubIf->NumOfPort = NumOfPort;
995 HubIf->HubNotify = NULL;
996
997 //
998 // Create a timer to poll root hub ports periodically
999 //
1000 Status = gBS->CreateEvent (
1001 EVT_TIMER | EVT_NOTIFY_SIGNAL,
1002 TPL_CALLBACK,
1003 UsbRootHubEnumeration,
1004 HubIf,
1005 &HubIf->HubNotify
1006 );
1007
1008 if (EFI_ERROR (Status)) {
1009 return Status;
1010 }
1011
1012 //
1013 // It should signal the event immediately here, or device detection
1014 // by bus enumeration might be delayed by the timer interval.
1015 //
1016 gBS->SignalEvent (HubIf->HubNotify);
1017
1018 Status = gBS->SetTimer (
1019 HubIf->HubNotify,
1020 TimerPeriodic,
1021 USB_ROOTHUB_POLL_INTERVAL
1022 );
1023
1024 if (EFI_ERROR (Status)) {
1025 gBS->CloseEvent (HubIf->HubNotify);
1026 }
1027
1028 return Status;
1029 }
1030
1031
1032 /**
1033 Get the port status. This function is required to
1034 ACK the port change bits although it will return
1035 the port changes in PortState. Bus enumeration code
1036 doesn't need to ACK the port change bits.
1037
1038 @param HubIf The root hub interface.
1039 @param Port The root hub port to get the state.
1040 @param PortState Variable to return the port state.
1041
1042 @retval EFI_SUCCESS The port state is returned.
1043 @retval Others Failed to retrieve the port state.
1044
1045 **/
1046 EFI_STATUS
1047 UsbRootHubGetPortStatus (
1048 IN USB_INTERFACE *HubIf,
1049 IN UINT8 Port,
1050 OUT EFI_USB_PORT_STATUS *PortState
1051 )
1052 {
1053 USB_BUS *Bus;
1054 EFI_STATUS Status;
1055
1056 Bus = HubIf->Device->Bus;
1057 Status = UsbHcGetRootHubPortStatus (Bus, Port, PortState);
1058
1059 return Status;
1060 }
1061
1062
1063 /**
1064 Clear the port change status.
1065
1066 @param HubIf The root hub interface.
1067 @param Port The root hub port.
1068
1069 **/
1070 VOID
1071 UsbRootHubClearPortChange (
1072 IN USB_INTERFACE *HubIf,
1073 IN UINT8 Port
1074 )
1075 {
1076 EFI_USB_PORT_STATUS PortState;
1077 USB_CHANGE_FEATURE_MAP *Map;
1078 UINTN Index;
1079 EFI_STATUS Status;
1080
1081 Status = UsbRootHubGetPortStatus (HubIf, Port, &PortState);
1082
1083 if (EFI_ERROR (Status)) {
1084 return;
1085 }
1086
1087 //
1088 // OK, get the usb port status, now ACK the change bits.
1089 // Don't return error when failed to clear the change bits.
1090 // It may lead to extra port state report. USB bus should
1091 // be able to handle this.
1092 //
1093 for (Index = 0; Index < USB_ROOT_HUB_MAP_SIZE; Index++) {
1094 Map = &mRootHubFeatureMap[Index];
1095
1096 if (USB_BIT_IS_SET (PortState.PortChangeStatus, Map->ChangedBit)) {
1097 UsbHcClearRootHubPortFeature (HubIf->Device->Bus, Port, (EFI_USB_PORT_FEATURE) Map->Feature);
1098 }
1099 }
1100 }
1101
1102
1103 /**
1104 Set the root hub port feature.
1105
1106 @param HubIf The Usb hub interface.
1107 @param Port The hub port.
1108 @param Feature The feature to set.
1109
1110 @retval EFI_SUCCESS The root hub port is set with the feature.
1111 @retval Others Failed to set the feature.
1112
1113 **/
1114 EFI_STATUS
1115 UsbRootHubSetPortFeature (
1116 IN USB_INTERFACE *HubIf,
1117 IN UINT8 Port,
1118 IN EFI_USB_PORT_FEATURE Feature
1119 )
1120 {
1121 EFI_STATUS Status;
1122
1123 Status = UsbHcSetRootHubPortFeature (HubIf->Device->Bus, Port, Feature);
1124
1125 return Status;
1126 }
1127
1128
1129 /**
1130 Clear the root hub port feature.
1131
1132 @param HubIf The root hub interface.
1133 @param Port The root hub port.
1134 @param Feature The feature to clear.
1135
1136 @retval EFI_SUCCESS The root hub port is cleared of the feature.
1137 @retval Others Failed to clear the feature.
1138
1139 **/
1140 EFI_STATUS
1141 UsbRootHubClearPortFeature (
1142 IN USB_INTERFACE *HubIf,
1143 IN UINT8 Port,
1144 IN EFI_USB_PORT_FEATURE Feature
1145 )
1146 {
1147 EFI_STATUS Status;
1148
1149 Status = UsbHcClearRootHubPortFeature (HubIf->Device->Bus, Port, Feature);
1150
1151 return Status;
1152 }
1153
1154
1155 /**
1156 Interface function to reset the root hub port.
1157
1158 @param RootIf The root hub interface.
1159 @param Port The port to reset.
1160
1161 @retval EFI_SUCCESS The hub port is reset.
1162 @retval EFI_TIMEOUT Failed to reset the port in time.
1163 @retval EFI_NOT_FOUND The low/full speed device connected to high speed.
1164 root hub is released to the companion UHCI.
1165 @retval Others Failed to reset the port.
1166
1167 **/
1168 EFI_STATUS
1169 UsbRootHubResetPort (
1170 IN USB_INTERFACE *RootIf,
1171 IN UINT8 Port
1172 )
1173 {
1174 USB_BUS *Bus;
1175 EFI_STATUS Status;
1176 EFI_USB_PORT_STATUS PortState;
1177 UINTN Index;
1178
1179 //
1180 // Notice: although EHCI requires that ENABLED bit be cleared
1181 // when reset the port, we don't need to care that here. It
1182 // should be handled in the EHCI driver.
1183 //
1184 Bus = RootIf->Device->Bus;
1185 Status = UsbHcSetRootHubPortFeature (Bus, Port, EfiUsbPortReset);
1186
1187 if (EFI_ERROR (Status)) {
1188 DEBUG (( EFI_D_ERROR, "UsbRootHubResetPort: failed to start reset on port %d\n", Port));
1189 return Status;
1190 }
1191
1192 //
1193 // Drive the reset signal for at least 50ms. Check USB 2.0 Spec
1194 // section 7.1.7.5 for timing requirements.
1195 //
1196 gBS->Stall (USB_SET_ROOT_PORT_RESET_STALL);
1197
1198 Status = UsbHcClearRootHubPortFeature (Bus, Port, EfiUsbPortReset);
1199
1200 if (EFI_ERROR (Status)) {
1201 DEBUG (( EFI_D_ERROR, "UsbRootHubResetPort: failed to clear reset on port %d\n", Port));
1202 return Status;
1203 }
1204
1205 gBS->Stall (USB_CLR_ROOT_PORT_RESET_STALL);
1206
1207 //
1208 // USB host controller won't clear the RESET bit until
1209 // reset is actually finished.
1210 //
1211 ZeroMem (&PortState, sizeof (EFI_USB_PORT_STATUS));
1212
1213 for (Index = 0; Index < USB_WAIT_PORT_STS_CHANGE_LOOP; Index++) {
1214 Status = UsbHcGetRootHubPortStatus (Bus, Port, &PortState);
1215
1216 if (EFI_ERROR (Status)) {
1217 return Status;
1218 }
1219
1220 if (!USB_BIT_IS_SET (PortState.PortStatus, USB_PORT_STAT_RESET)) {
1221 break;
1222 }
1223
1224 gBS->Stall (USB_WAIT_PORT_STS_CHANGE_STALL);
1225 }
1226
1227 if (Index == USB_WAIT_PORT_STS_CHANGE_LOOP) {
1228 DEBUG ((EFI_D_ERROR, "UsbRootHubResetPort: reset not finished in time on port %d\n", Port));
1229 return EFI_TIMEOUT;
1230 }
1231
1232 if (!USB_BIT_IS_SET (PortState.PortStatus, USB_PORT_STAT_ENABLE)) {
1233 //
1234 // OK, the port is reset. If root hub is of high speed and
1235 // the device is of low/full speed, release the ownership to
1236 // companion UHCI. If root hub is of full speed, it won't
1237 // automatically enable the port, we need to enable it manually.
1238 //
1239 if (RootIf->MaxSpeed == EFI_USB_SPEED_HIGH) {
1240 DEBUG (( EFI_D_ERROR, "UsbRootHubResetPort: release low/full speed device (%d) to UHCI\n", Port));
1241
1242 UsbRootHubSetPortFeature (RootIf, Port, EfiUsbPortOwner);
1243 return EFI_NOT_FOUND;
1244
1245 } else {
1246
1247 Status = UsbRootHubSetPortFeature (RootIf, Port, EfiUsbPortEnable);
1248
1249 if (EFI_ERROR (Status)) {
1250 DEBUG (( EFI_D_ERROR, "UsbRootHubResetPort: failed to enable port %d for UHCI\n", Port));
1251 return Status;
1252 }
1253
1254 gBS->Stall (USB_SET_ROOT_PORT_ENABLE_STALL);
1255 }
1256 }
1257
1258 return EFI_SUCCESS;
1259 }
1260
1261
1262 /**
1263 Release the root hub's control of the interface.
1264
1265 @param HubIf The root hub interface.
1266
1267 @retval EFI_SUCCESS The root hub's control of the interface is
1268 released.
1269
1270 **/
1271 EFI_STATUS
1272 UsbRootHubRelease (
1273 IN USB_INTERFACE *HubIf
1274 )
1275 {
1276 DEBUG (( EFI_D_INFO, "UsbRootHubRelease: root hub released for hub %p\n", HubIf));
1277
1278 gBS->SetTimer (HubIf->HubNotify, TimerCancel, USB_ROOTHUB_POLL_INTERVAL);
1279 gBS->CloseEvent (HubIf->HubNotify);
1280
1281 return EFI_SUCCESS;
1282 }
1283
1284 USB_HUB_API mUsbHubApi = {
1285 UsbHubInit,
1286 UsbHubGetPortStatus,
1287 UsbHubClearPortChange,
1288 UsbHubSetPortFeature,
1289 UsbHubClearPortFeature,
1290 UsbHubResetPort,
1291 UsbHubRelease
1292 };
1293
1294 USB_HUB_API mUsbRootHubApi = {
1295 UsbRootHubInit,
1296 UsbRootHubGetPortStatus,
1297 UsbRootHubClearPortChange,
1298 UsbRootHubSetPortFeature,
1299 UsbRootHubClearPortFeature,
1300 UsbRootHubResetPort,
1301 UsbRootHubRelease
1302 };