]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Bus/Usb/UsbBusDxe/UsbHub.c
Global variables have been moved backward ahead of functions.
[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 retrive 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
631 //
632 // Locate the interrupt endpoint for port change map
633 //
634 HubIf->IsHub = FALSE;
635 Setting = HubIf->IfSetting;
636 HubDev = HubIf->Device;
637 EpDesc = NULL;
638
639 for (Index = 0; Index < Setting->Desc.NumEndpoints; Index++) {
640 ASSERT ((Setting->Endpoints != NULL) && (Setting->Endpoints[Index] != NULL));
641
642 EpDesc = Setting->Endpoints[Index];
643
644 if (USB_BIT_IS_SET (EpDesc->Desc.EndpointAddress, USB_ENDPOINT_DIR_IN) &&
645 (USB_ENDPOINT_TYPE (&EpDesc->Desc) == USB_ENDPOINT_INTERRUPT)) {
646 break;
647 }
648 }
649
650 if (Index == Setting->Desc.NumEndpoints) {
651 DEBUG (( EFI_D_ERROR, "UsbHubInit: no interrupt endpoint found for hub %d\n", HubDev->Address));
652 return EFI_DEVICE_ERROR;
653 }
654
655 Status = UsbHubReadDesc (HubDev, &HubDesc);
656
657 if (EFI_ERROR (Status)) {
658 DEBUG (( EFI_D_ERROR, "UsbHubInit: failed to read HUB descriptor %r\n", Status));
659 return Status;
660 }
661
662 HubIf->NumOfPort = HubDesc.NumPorts;
663
664 DEBUG (( EFI_D_INFO, "UsbHubInit: hub %d has %d ports\n", HubDev->Address,HubIf->NumOfPort));
665
666 //
667 // Create an event to enumerate the hub's port. On
668 //
669 Status = gBS->CreateEvent (
670 EVT_NOTIFY_SIGNAL,
671 TPL_CALLBACK,
672 UsbHubEnumeration,
673 HubIf,
674 &HubIf->HubNotify
675 );
676
677 if (EFI_ERROR (Status)) {
678 DEBUG (( EFI_D_ERROR, "UsbHubInit: failed to create signal for hub %d - %r\n",
679 HubDev->Address, Status));
680
681 return Status;
682 }
683
684 //
685 // Create AsyncInterrupt to query hub port change endpoint
686 // periodically. If the hub ports are changed, hub will return
687 // changed port map from the interrupt endpoint. The port map
688 // must be able to hold (HubIf->NumOfPort + 1) bits (one bit for
689 // host change status).
690 //
691 UsbIo = &HubIf->UsbIo;
692 Status = UsbIo->UsbAsyncInterruptTransfer (
693 UsbIo,
694 EpDesc->Desc.EndpointAddress,
695 TRUE,
696 USB_HUB_POLL_INTERVAL,
697 HubIf->NumOfPort / 8 + 1,
698 UsbOnHubInterrupt,
699 HubIf
700 );
701
702 if (EFI_ERROR (Status)) {
703 DEBUG (( EFI_D_ERROR, "UsbHubInit: failed to queue interrupt transfer for hub %d - %r\n",
704 HubDev->Address, Status));
705
706 gBS->CloseEvent (HubIf->HubNotify);
707 HubIf->HubNotify = NULL;
708
709 return Status;
710 }
711
712 //
713 // OK, set IsHub to TRUE. Now usb bus can handle this device
714 // as a working HUB. If failed eariler, bus driver will not
715 // recognize it as a hub. Other parts of the bus should be able
716 // to work.
717 //
718 HubIf->IsHub = TRUE;
719 HubIf->HubApi = &mUsbHubApi;
720 HubIf->HubEp = EpDesc;
721
722 //
723 // Feed power to all the hub ports. It should be ok
724 // for both gang/individual powered hubs.
725 //
726 for (Index = 0; Index < HubDesc.NumPorts; Index++) {
727 UsbHubCtrlSetPortFeature (HubIf->Device, Index, (EFI_USB_PORT_FEATURE) USB_HUB_PORT_POWER);
728 }
729
730 gBS->Stall (HubDesc.PwrOn2PwrGood * USB_SET_PORT_POWER_STALL);
731 UsbHubAckHubStatus (HubIf->Device);
732
733 DEBUG (( EFI_D_INFO, "UsbHubInit: hub %d initialized\n", HubDev->Address));
734 return Status;
735 }
736
737
738
739 /**
740 Get the port status. This function is required to
741 ACK the port change bits although it will return
742 the port changes in PortState. Bus enumeration code
743 doesn't need to ACK the port change bits.
744
745 @param HubIf The hub interface.
746 @param Port The port of the hub to get state.
747 @param PortState Variable to return the port state.
748
749 @retval EFI_SUCCESS The port status is successfully returned.
750 @retval Others Failed to return the status.
751
752 **/
753 EFI_STATUS
754 UsbHubGetPortStatus (
755 IN USB_INTERFACE *HubIf,
756 IN UINT8 Port,
757 OUT EFI_USB_PORT_STATUS *PortState
758 )
759 {
760 EFI_STATUS Status;
761
762 Status = UsbHubCtrlGetPortStatus (HubIf->Device, Port, PortState);
763
764 return Status;
765 }
766
767
768
769 /**
770 Clear the port change status.
771
772 @param HubIf The hub interface.
773 @param Port The hub port.
774
775 @return None.
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 funtion 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 initialied 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 @return None.
1070
1071 **/
1072 VOID
1073 UsbRootHubClearPortChange (
1074 IN USB_INTERFACE *HubIf,
1075 IN UINT8 Port
1076 )
1077 {
1078 EFI_USB_PORT_STATUS PortState;
1079 USB_CHANGE_FEATURE_MAP *Map;
1080 UINTN Index;
1081 EFI_STATUS Status;
1082
1083 Status = UsbRootHubGetPortStatus (HubIf, Port, &PortState);
1084
1085 if (EFI_ERROR (Status)) {
1086 return;
1087 }
1088
1089 //
1090 // OK, get the usb port status, now ACK the change bits.
1091 // Don't return error when failed to clear the change bits.
1092 // It may lead to extra port state report. USB bus should
1093 // be able to handle this.
1094 //
1095 for (Index = 0; Index < USB_ROOT_HUB_MAP_SIZE; Index++) {
1096 Map = &mRootHubFeatureMap[Index];
1097
1098 if (USB_BIT_IS_SET (PortState.PortChangeStatus, Map->ChangedBit)) {
1099 UsbHcClearRootHubPortFeature (HubIf->Device->Bus, Port, (EFI_USB_PORT_FEATURE) Map->Feature);
1100 }
1101 }
1102 }
1103
1104
1105 /**
1106 Set the root hub port feature.
1107
1108 @param HubIf The Usb hub interface.
1109 @param Port The hub port.
1110 @param Feature The feature to set.
1111
1112 @retval EFI_SUCCESS The root hub port is set with the feature.
1113 @retval Others Failed to set the feature.
1114
1115 **/
1116 EFI_STATUS
1117 UsbRootHubSetPortFeature (
1118 IN USB_INTERFACE *HubIf,
1119 IN UINT8 Port,
1120 IN EFI_USB_PORT_FEATURE Feature
1121 )
1122 {
1123 EFI_STATUS Status;
1124
1125 Status = UsbHcSetRootHubPortFeature (HubIf->Device->Bus, Port, Feature);
1126
1127 return Status;
1128 }
1129
1130
1131 /**
1132 Clear the root hub port feature.
1133
1134 @param HubIf The root hub interface.
1135 @param Port The root hub port.
1136 @param Feature The feature to clear.
1137
1138 @retval EFI_SUCCESS The root hub port is cleared of the feature.
1139 @retval Others Failed to clear the feature.
1140
1141 **/
1142 EFI_STATUS
1143 UsbRootHubClearPortFeature (
1144 IN USB_INTERFACE *HubIf,
1145 IN UINT8 Port,
1146 IN EFI_USB_PORT_FEATURE Feature
1147 )
1148 {
1149 EFI_STATUS Status;
1150
1151 Status = UsbHcClearRootHubPortFeature (HubIf->Device->Bus, Port, Feature);
1152
1153 return Status;
1154 }
1155
1156
1157 /**
1158 Interface funtion to reset the root hub port.
1159
1160 @param RootIf The root hub interface.
1161 @param Port The port to reset.
1162
1163 @retval EFI_SUCCESS The hub port is reset.
1164 @retval EFI_TIMEOUT Failed to reset the port in time.
1165 @retval EFI_NOT_FOUND The low/full speed device connected to high speed.
1166 root hub is released to the companion UHCI.
1167 @retval Others Failed to reset the port.
1168
1169 **/
1170 EFI_STATUS
1171 UsbRootHubResetPort (
1172 IN USB_INTERFACE *RootIf,
1173 IN UINT8 Port
1174 )
1175 {
1176 USB_BUS *Bus;
1177 EFI_STATUS Status;
1178 EFI_USB_PORT_STATUS PortState;
1179 UINTN Index;
1180
1181 //
1182 // Notice: although EHCI requires that ENABLED bit be cleared
1183 // when reset the port, we don't need to care that here. It
1184 // should be handled in the EHCI driver.
1185 //
1186 Bus = RootIf->Device->Bus;
1187 Status = UsbHcSetRootHubPortFeature (Bus, Port, EfiUsbPortReset);
1188
1189 if (EFI_ERROR (Status)) {
1190 DEBUG (( EFI_D_ERROR, "UsbRootHubResetPort: failed to start reset on port %d\n", Port));
1191 return Status;
1192 }
1193
1194 //
1195 // Drive the reset signal for at least 50ms. Check USB 2.0 Spec
1196 // section 7.1.7.5 for timing requirements.
1197 //
1198 gBS->Stall (USB_SET_ROOT_PORT_RESET_STALL);
1199
1200 Status = UsbHcClearRootHubPortFeature (Bus, Port, EfiUsbPortReset);
1201
1202 if (EFI_ERROR (Status)) {
1203 DEBUG (( EFI_D_ERROR, "UsbRootHubResetPort: failed to clear reset on port %d\n", Port));
1204 return Status;
1205 }
1206
1207 gBS->Stall (USB_CLR_ROOT_PORT_RESET_STALL);
1208
1209 //
1210 // USB host controller won't clear the RESET bit until
1211 // reset is actually finished.
1212 //
1213 ZeroMem (&PortState, sizeof (EFI_USB_PORT_STATUS));
1214
1215 for (Index = 0; Index < USB_WAIT_PORT_STS_CHANGE_LOOP; Index++) {
1216 Status = UsbHcGetRootHubPortStatus (Bus, Port, &PortState);
1217
1218 if (EFI_ERROR (Status)) {
1219 return Status;
1220 }
1221
1222 if (!USB_BIT_IS_SET (PortState.PortStatus, USB_PORT_STAT_RESET)) {
1223 break;
1224 }
1225
1226 gBS->Stall (USB_WAIT_PORT_STS_CHANGE_STALL);
1227 }
1228
1229 if (Index == USB_WAIT_PORT_STS_CHANGE_LOOP) {
1230 DEBUG ((EFI_D_ERROR, "UsbRootHubResetPort: reset not finished in time on port %d\n", Port));
1231 return EFI_TIMEOUT;
1232 }
1233
1234 if (!USB_BIT_IS_SET (PortState.PortStatus, USB_PORT_STAT_ENABLE)) {
1235 //
1236 // OK, the port is reset. If root hub is of high speed and
1237 // the device is of low/full speed, release the ownership to
1238 // companion UHCI. If root hub is of full speed, it won't
1239 // automatically enable the port, we need to enable it manually.
1240 //
1241 if (RootIf->MaxSpeed == EFI_USB_SPEED_HIGH) {
1242 DEBUG (( EFI_D_ERROR, "UsbRootHubResetPort: release low/full speed device (%d) to UHCI\n", Port));
1243
1244 UsbRootHubSetPortFeature (RootIf, Port, EfiUsbPortOwner);
1245 return EFI_NOT_FOUND;
1246
1247 } else {
1248
1249 Status = UsbRootHubSetPortFeature (RootIf, Port, EfiUsbPortEnable);
1250
1251 if (EFI_ERROR (Status)) {
1252 DEBUG (( EFI_D_ERROR, "UsbRootHubResetPort: failed to enable port %d for UHCI\n", Port));
1253 return Status;
1254 }
1255
1256 gBS->Stall (USB_SET_ROOT_PORT_ENABLE_STALL);
1257 }
1258 }
1259
1260 return EFI_SUCCESS;
1261 }
1262
1263
1264 /**
1265 Release the root hub's control of the interface.
1266
1267 @param HubIf The root hub interface.
1268
1269 @retval EFI_SUCCESS The root hub's control of the interface is
1270 released.
1271
1272 **/
1273 EFI_STATUS
1274 UsbRootHubRelease (
1275 IN USB_INTERFACE *HubIf
1276 )
1277 {
1278 DEBUG (( EFI_D_INFO, "UsbRootHubRelease: root hub released for hub %p\n", HubIf));
1279
1280 gBS->SetTimer (HubIf->HubNotify, TimerCancel, USB_ROOTHUB_POLL_INTERVAL);
1281 gBS->CloseEvent (HubIf->HubNotify);
1282
1283 return EFI_SUCCESS;
1284 }
1285
1286 USB_HUB_API mUsbHubApi = {
1287 UsbHubInit,
1288 UsbHubGetPortStatus,
1289 UsbHubClearPortChange,
1290 UsbHubSetPortFeature,
1291 UsbHubClearPortFeature,
1292 UsbHubResetPort,
1293 UsbHubRelease
1294 };
1295
1296 USB_HUB_API mUsbRootHubApi = {
1297 UsbRootHubInit,
1298 UsbRootHubGetPortStatus,
1299 UsbRootHubClearPortChange,
1300 UsbRootHubSetPortFeature,
1301 UsbRootHubClearPortFeature,
1302 UsbRootHubResetPort,
1303 UsbRootHubRelease
1304 };