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