]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Bus/Pci/XhciDxe/Xhci.c
Enabling usb3.0 XHCI support.
[mirror_edk2.git] / MdeModulePkg / Bus / Pci / XhciDxe / Xhci.c
1 /** @file
2 The XHCI controller driver.
3
4 Copyright (c) 2011, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 **/
14
15 #include "Xhci.h"
16
17 //
18 // The device context array which supports up to 255 devices, entry 0 is reserved and should not be used.
19 //
20 USB_DEV_CONTEXT UsbDevContext[256];
21
22 //
23 // Two arrays used to translate the XHCI port state (change)
24 // to the UEFI protocol's port state (change).
25 //
26 USB_PORT_STATE_MAP mUsbPortStateMap[] = {
27 {XHC_PORTSC_CCS, USB_PORT_STAT_CONNECTION},
28 {XHC_PORTSC_PED, USB_PORT_STAT_ENABLE},
29 {XHC_PORTSC_OCA, USB_PORT_STAT_OVERCURRENT},
30 {XHC_PORTSC_RESET, USB_PORT_STAT_RESET}
31 };
32
33 USB_PORT_STATE_MAP mUsbPortChangeMap[] = {
34 {XHC_PORTSC_CSC, USB_PORT_STAT_C_CONNECTION},
35 {XHC_PORTSC_PEC, USB_PORT_STAT_C_ENABLE},
36 {XHC_PORTSC_OCC, USB_PORT_STAT_C_OVERCURRENT},
37 {XHC_PORTSC_PRC, USB_PORT_STAT_C_RESET}
38 };
39
40 EFI_DRIVER_BINDING_PROTOCOL gXhciDriverBinding = {
41 XhcDriverBindingSupported,
42 XhcDriverBindingStart,
43 XhcDriverBindingStop,
44 0x30,
45 NULL,
46 NULL
47 };
48
49 /**
50 Retrieves the capability of root hub ports.
51
52 @param This The EFI_USB2_HC_PROTOCOL instance.
53 @param MaxSpeed Max speed supported by the controller.
54 @param PortNumber Number of the root hub ports.
55 @param Is64BitCapable Whether the controller supports 64-bit memory
56 addressing.
57
58 @retval EFI_SUCCESS Host controller capability were retrieved successfully.
59 @retval EFI_INVALID_PARAMETER Either of the three capability pointer is NULL.
60
61 **/
62 EFI_STATUS
63 EFIAPI
64 XhcGetCapability (
65 IN EFI_USB2_HC_PROTOCOL *This,
66 OUT UINT8 *MaxSpeed,
67 OUT UINT8 *PortNumber,
68 OUT UINT8 *Is64BitCapable
69 )
70 {
71 USB_XHCI_DEV *Xhc;
72 EFI_TPL OldTpl;
73
74 if ((MaxSpeed == NULL) || (PortNumber == NULL) || (Is64BitCapable == NULL)) {
75 return EFI_INVALID_PARAMETER;
76 }
77
78 OldTpl = gBS->RaiseTPL (XHC_TPL);
79
80 Xhc = XHC_FROM_THIS (This);
81 *MaxSpeed = EFI_USB_SPEED_SUPER;
82 *PortNumber = (UINT8) (Xhc->HcSParams1.Data.MaxPorts);
83 *Is64BitCapable = (UINT8) (Xhc->HcCParams.Data.Ac64);
84 DEBUG ((EFI_D_INFO, "XhcGetCapability: %d ports, 64 bit %d\n", *PortNumber, *Is64BitCapable));
85
86 gBS->RestoreTPL (OldTpl);
87
88 return EFI_SUCCESS;
89 }
90
91
92 /**
93 Provides software reset for the USB host controller.
94
95 @param This This EFI_USB2_HC_PROTOCOL instance.
96 @param Attributes A bit mask of the reset operation to perform.
97
98 @retval EFI_SUCCESS The reset operation succeeded.
99 @retval EFI_INVALID_PARAMETER Attributes is not valid.
100 @retval EFI_UNSUPPOURTED The type of reset specified by Attributes is
101 not currently supported by the host controller.
102 @retval EFI_DEVICE_ERROR Host controller isn't halted to reset.
103
104 **/
105 EFI_STATUS
106 EFIAPI
107 XhcReset (
108 IN EFI_USB2_HC_PROTOCOL *This,
109 IN UINT16 Attributes
110 )
111 {
112 USB_XHCI_DEV *Xhc;
113 EFI_STATUS Status;
114 EFI_TPL OldTpl;
115
116 OldTpl = gBS->RaiseTPL (XHC_TPL);
117
118 Xhc = XHC_FROM_THIS (This);
119
120 switch (Attributes) {
121 case EFI_USB_HC_RESET_GLOBAL:
122 //
123 // Flow through, same behavior as Host Controller Reset
124 //
125 case EFI_USB_HC_RESET_HOST_CONTROLLER:
126 //
127 // Host Controller must be Halt when Reset it
128 //
129 if (!XhcIsHalt (Xhc)) {
130 Status = XhcHaltHC (Xhc, XHC_GENERIC_TIMEOUT);
131
132 if (EFI_ERROR (Status)) {
133 Status = EFI_DEVICE_ERROR;
134 goto ON_EXIT;
135 }
136 }
137
138 Status = XhcResetHC (Xhc, XHC_RESET_TIMEOUT);
139 ASSERT (!(XHC_REG_BIT_IS_SET (Xhc, XHC_USBSTS_OFFSET, XHC_USBSTS_CNR)));
140
141 if (EFI_ERROR (Status)) {
142 goto ON_EXIT;
143 }
144 //
145 // Clean up the asynchronous transfers, currently only
146 // interrupt supports asynchronous operation.
147 //
148 XhciDelAllAsyncIntTransfers (Xhc);
149 XhcFreeSched (Xhc);
150
151 XhcInitSched (Xhc);
152 break;
153
154 case EFI_USB_HC_RESET_GLOBAL_WITH_DEBUG:
155 case EFI_USB_HC_RESET_HOST_WITH_DEBUG:
156 Status = EFI_UNSUPPORTED;
157 break;
158
159 default:
160 Status = EFI_INVALID_PARAMETER;
161 }
162
163 ON_EXIT:
164 DEBUG ((EFI_D_INFO, "XhcReset: status %r\n", Status));
165 gBS->RestoreTPL (OldTpl);
166
167 return Status;
168 }
169
170
171 /**
172 Retrieve the current state of the USB host controller.
173
174 @param This This EFI_USB2_HC_PROTOCOL instance.
175 @param State Variable to return the current host controller
176 state.
177
178 @retval EFI_SUCCESS Host controller state was returned in State.
179 @retval EFI_INVALID_PARAMETER State is NULL.
180 @retval EFI_DEVICE_ERROR An error was encountered while attempting to
181 retrieve the host controller's current state.
182
183 **/
184 EFI_STATUS
185 EFIAPI
186 XhcGetState (
187 IN EFI_USB2_HC_PROTOCOL *This,
188 OUT EFI_USB_HC_STATE *State
189 )
190 {
191 USB_XHCI_DEV *Xhc;
192 EFI_TPL OldTpl;
193
194 if (State == NULL) {
195 return EFI_INVALID_PARAMETER;
196 }
197
198 OldTpl = gBS->RaiseTPL (XHC_TPL);
199
200 Xhc = XHC_FROM_THIS (This);
201
202 if (XHC_REG_BIT_IS_SET (Xhc, XHC_USBSTS_OFFSET, XHC_USBSTS_HALT)) {
203 *State = EfiUsbHcStateHalt;
204 } else {
205 *State = EfiUsbHcStateOperational;
206 }
207
208 DEBUG ((EFI_D_INFO, "XhcGetState: current state %d\n", *State));
209 gBS->RestoreTPL (OldTpl);
210
211 return EFI_SUCCESS;
212 }
213
214 /**
215 Sets the USB host controller to a specific state.
216
217 @param This This EFI_USB2_HC_PROTOCOL instance.
218 @param State The state of the host controller that will be set.
219
220 @retval EFI_SUCCESS The USB host controller was successfully placed
221 in the state specified by State.
222 @retval EFI_INVALID_PARAMETER State is invalid.
223 @retval EFI_DEVICE_ERROR Failed to set the state due to device error.
224
225 **/
226 EFI_STATUS
227 EFIAPI
228 XhcSetState (
229 IN EFI_USB2_HC_PROTOCOL *This,
230 IN EFI_USB_HC_STATE State
231 )
232 {
233 USB_XHCI_DEV *Xhc;
234 EFI_STATUS Status;
235 EFI_USB_HC_STATE CurState;
236 EFI_TPL OldTpl;
237
238 Status = XhcGetState (This, &CurState);
239
240 if (EFI_ERROR (Status)) {
241 return EFI_DEVICE_ERROR;
242 }
243
244 if (CurState == State) {
245 return EFI_SUCCESS;
246 }
247
248 OldTpl = gBS->RaiseTPL (XHC_TPL);
249
250 Xhc = XHC_FROM_THIS (This);
251
252 switch (State) {
253 case EfiUsbHcStateHalt:
254 Status = XhcHaltHC (Xhc, XHC_GENERIC_TIMEOUT);
255 break;
256
257 case EfiUsbHcStateOperational:
258 if (XHC_REG_BIT_IS_SET (Xhc, XHC_USBSTS_OFFSET, XHC_USBSTS_HSE)) {
259 Status = EFI_DEVICE_ERROR;
260 break;
261 }
262
263 //
264 // Software must not write a one to this field unless the host controller
265 // is in the Halted state. Doing so will yield undefined results.
266 // refers to Spec[XHCI1.0-2.3.1]
267 //
268 if (!XHC_REG_BIT_IS_SET (Xhc, XHC_USBSTS_OFFSET, XHC_USBSTS_HALT)) {
269 Status = EFI_DEVICE_ERROR;
270 break;
271 }
272
273 Status = XhcRunHC (Xhc, XHC_GENERIC_TIMEOUT);
274 break;
275
276 case EfiUsbHcStateSuspend:
277 Status = EFI_UNSUPPORTED;
278 break;
279
280 default:
281 Status = EFI_INVALID_PARAMETER;
282 }
283
284 DEBUG ((EFI_D_INFO, "XhcSetState: status %r\n", Status));
285 gBS->RestoreTPL (OldTpl);
286
287 return Status;
288 }
289
290 /**
291 Retrieves the current status of a USB root hub port.
292
293 @param This This EFI_USB2_HC_PROTOCOL instance.
294 @param PortNumber The root hub port to retrieve the state from.
295 This value is zero-based.
296 @param PortStatus Variable to receive the port state.
297
298 @retval EFI_SUCCESS The status of the USB root hub port specified.
299 by PortNumber was returned in PortStatus.
300 @retval EFI_INVALID_PARAMETER PortNumber is invalid.
301 @retval EFI_DEVICE_ERROR Can't read register.
302
303 **/
304 EFI_STATUS
305 EFIAPI
306 XhcGetRootHubPortStatus (
307 IN EFI_USB2_HC_PROTOCOL *This,
308 IN UINT8 PortNumber,
309 OUT EFI_USB_PORT_STATUS *PortStatus
310 )
311 {
312 USB_XHCI_DEV *Xhc;
313 UINT32 Offset;
314 UINT32 State;
315 UINT32 TotalPort;
316 UINTN Index;
317 UINTN MapSize;
318 EFI_STATUS Status;
319 USB_DEV_ROUTE ParentRouteChart;
320 EFI_TPL OldTpl;
321
322 if (PortStatus == NULL) {
323 return EFI_INVALID_PARAMETER;
324 }
325
326 OldTpl = gBS->RaiseTPL (XHC_TPL);
327
328 Xhc = XHC_FROM_THIS (This);
329 Status = EFI_SUCCESS;
330
331 TotalPort = Xhc->HcSParams1.Data.MaxPorts;
332
333 if (PortNumber >= TotalPort) {
334 Status = EFI_INVALID_PARAMETER;
335 goto ON_EXIT;
336 }
337
338 Offset = (UINT32) (XHC_PORTSC_OFFSET + (0x10 * PortNumber));
339 PortStatus->PortStatus = 0;
340 PortStatus->PortChangeStatus = 0;
341
342 State = XhcReadOpReg (Xhc, Offset);
343
344 //
345 // According to XHCI 1.0 spec, bit 10~13 of the root port status register identifies the speed of the attached device.
346 //
347 switch ((State & XHC_PORTSC_PS) >> 10) {
348 case 2:
349 PortStatus->PortStatus |= USB_PORT_STAT_LOW_SPEED;
350 break;
351
352 case 3:
353 PortStatus->PortStatus |= USB_PORT_STAT_HIGH_SPEED;
354 break;
355
356 case 4:
357 PortStatus->PortStatus |= USB_PORT_STAT_SUPER_SPEED;
358 break;
359
360 default:
361 break;
362 }
363
364 //
365 // Convert the XHCI port/port change state to UEFI status
366 //
367 MapSize = sizeof (mUsbPortStateMap) / sizeof (USB_PORT_STATE_MAP);
368
369 for (Index = 0; Index < MapSize; Index++) {
370 if (XHC_BIT_IS_SET (State, mUsbPortStateMap[Index].HwState)) {
371 PortStatus->PortStatus = (UINT16) (PortStatus->PortStatus | mUsbPortStateMap[Index].UefiState);
372 }
373 }
374 //
375 // Bit5~8 reflects its current link state.
376 //
377 if ((State & XHC_PORTSC_PLS) >> 5 == 3) {
378 PortStatus->PortStatus |= USB_PORT_STAT_SUSPEND;
379 }
380
381 MapSize = sizeof (mUsbPortChangeMap) / sizeof (USB_PORT_STATE_MAP);
382
383 for (Index = 0; Index < MapSize; Index++) {
384 if (XHC_BIT_IS_SET (State, mUsbPortChangeMap[Index].HwState)) {
385 PortStatus->PortChangeStatus = (UINT16) (PortStatus->PortChangeStatus | mUsbPortChangeMap[Index].UefiState);
386 }
387 }
388
389 //
390 // Poll the root port status register to enable/disable corresponding device slot if there is a device attached/detached.
391 // For those devices behind hub, we get its attach/detach event by hooking Get_Port_Status request at control transfer for those hub.
392 //
393 ParentRouteChart.Dword = 0;
394 XhcPollPortStatusChange (Xhc, ParentRouteChart, PortNumber, PortStatus);
395
396 ON_EXIT:
397 gBS->RestoreTPL (OldTpl);
398 return Status;
399 }
400
401
402 /**
403 Sets a feature for the specified root hub port.
404
405 @param This This EFI_USB2_HC_PROTOCOL instance.
406 @param PortNumber Root hub port to set.
407 @param PortFeature Feature to set.
408
409 @retval EFI_SUCCESS The feature specified by PortFeature was set.
410 @retval EFI_INVALID_PARAMETER PortNumber is invalid or PortFeature is invalid.
411 @retval EFI_DEVICE_ERROR Can't read register.
412
413 **/
414 EFI_STATUS
415 EFIAPI
416 XhcSetRootHubPortFeature (
417 IN EFI_USB2_HC_PROTOCOL *This,
418 IN UINT8 PortNumber,
419 IN EFI_USB_PORT_FEATURE PortFeature
420 )
421 {
422 USB_XHCI_DEV *Xhc;
423 UINT32 Offset;
424 UINT32 State;
425 UINT32 TotalPort;
426 UINT8 SlotId;
427 USB_DEV_ROUTE RouteChart;
428 EFI_STATUS Status;
429 EFI_TPL OldTpl;
430
431 OldTpl = gBS->RaiseTPL (XHC_TPL);
432
433 Xhc = XHC_FROM_THIS (This);
434 Status = EFI_SUCCESS;
435
436 TotalPort = (Xhc->HcSParams1.Data.MaxPorts);
437
438 if (PortNumber >= TotalPort) {
439 Status = EFI_INVALID_PARAMETER;
440 goto ON_EXIT;
441 }
442
443 Offset = (UINT32) (XHC_PORTSC_OFFSET + (0x10 * PortNumber));
444 State = XhcReadOpReg (Xhc, Offset);
445
446 //
447 // Mask off the port status change bits, these bits are
448 // write clean bit
449 //
450 State &= ~ (BIT1 | BIT17 | BIT18 | BIT19 | BIT20 | BIT21 | BIT22 | BIT23);
451
452 switch (PortFeature) {
453 case EfiUsbPortEnable:
454 //
455 // Ports may only be enabled by the xHC. Software cannot enable a port by writing a '1' to this flag.
456 // A port may be disabled by software writing a '1' to this flag.
457 //
458 Status = EFI_SUCCESS;
459 break;
460
461 case EfiUsbPortSuspend:
462 State |= XHC_PORTSC_LWS;
463 XhcWriteOpReg (Xhc, Offset, State);
464 State &= ~XHC_PORTSC_PLS;
465 State |= (3 << 5) ;
466 XhcWriteOpReg (Xhc, Offset, State);
467 break;
468
469 case EfiUsbPortReset:
470 DEBUG ((EFI_D_INFO, "XhcUsbPortReset!\n"));
471 //
472 // Make sure Host Controller not halt before reset it
473 //
474 if (XhcIsHalt (Xhc)) {
475 Status = XhcRunHC (Xhc, XHC_GENERIC_TIMEOUT);
476
477 if (EFI_ERROR (Status)) {
478 DEBUG ((EFI_D_INFO, "XhcSetRootHubPortFeature :failed to start HC - %r\n", Status));
479 break;
480 }
481 }
482
483 RouteChart.Field.RouteString = 0;
484 RouteChart.Field.RootPortNum = PortNumber + 1;
485 RouteChart.Field.TierNum = 1;
486 //
487 // BUGBUG: If the port reset operation happens after the usb super speed device is enabled,
488 // The subsequent configuration, such as getting device descriptor, will fail.
489 // So here a workaround is introduced to skip the reset operation if the device is enabled.
490 //
491 SlotId = XhcRouteStringToSlotId (RouteChart);
492 if (SlotId == 0) {
493 //
494 // 4.3.1 Resetting a Root Hub Port
495 // 1) Write the PORTSC register with the Port Reset (PR) bit set to '1'.
496 //
497 State |= XHC_PORTSC_RESET;
498 XhcWriteOpReg (Xhc, Offset, State);
499 XhcWaitOpRegBit(Xhc, Offset, XHC_PORTSC_PRC, TRUE, XHC_GENERIC_TIMEOUT);
500 }
501 break;
502
503 case EfiUsbPortPower:
504 //
505 // Not supported, ignore the operation
506 //
507 Status = EFI_SUCCESS;
508 break;
509
510 case EfiUsbPortOwner:
511 //
512 // XHCI root hub port don't has the owner bit, ignore the operation
513 //
514 Status = EFI_SUCCESS;
515 break;
516
517 default:
518 Status = EFI_INVALID_PARAMETER;
519 }
520
521 ON_EXIT:
522 DEBUG ((EFI_D_INFO, "XhcSetRootHubPortFeature: status %r\n", Status));
523 gBS->RestoreTPL (OldTpl);
524
525 return Status;
526 }
527
528
529 /**
530 Clears a feature for the specified root hub port.
531
532 @param This A pointer to the EFI_USB2_HC_PROTOCOL instance.
533 @param PortNumber Specifies the root hub port whose feature is
534 requested to be cleared.
535 @param PortFeature Indicates the feature selector associated with the
536 feature clear request.
537
538 @retval EFI_SUCCESS The feature specified by PortFeature was cleared
539 for the USB root hub port specified by PortNumber.
540 @retval EFI_INVALID_PARAMETER PortNumber is invalid or PortFeature is invalid.
541 @retval EFI_DEVICE_ERROR Can't read register.
542
543 **/
544 EFI_STATUS
545 EFIAPI
546 XhcClearRootHubPortFeature (
547 IN EFI_USB2_HC_PROTOCOL *This,
548 IN UINT8 PortNumber,
549 IN EFI_USB_PORT_FEATURE PortFeature
550 )
551 {
552 USB_XHCI_DEV *Xhc;
553 UINT32 Offset;
554 UINT32 State;
555 UINT32 TotalPort;
556 EFI_STATUS Status;
557 EFI_TPL OldTpl;
558
559 OldTpl = gBS->RaiseTPL (XHC_TPL);
560
561 Xhc = XHC_FROM_THIS (This);
562 Status = EFI_SUCCESS;
563
564 TotalPort = (Xhc->HcSParams1.Data.MaxPorts);
565
566 if (PortNumber >= TotalPort) {
567 Status = EFI_INVALID_PARAMETER;
568 goto ON_EXIT;
569 }
570
571 Offset = XHC_PORTSC_OFFSET + (0x10 * PortNumber);
572
573 //
574 // Mask off the port status change bits, these bits are
575 // write clean bit
576 //
577 State = XhcReadOpReg (Xhc, Offset);
578 State &= ~ (BIT1 | BIT17 | BIT18 | BIT19 | BIT20 | BIT21 | BIT22 | BIT23);
579
580 switch (PortFeature) {
581 case EfiUsbPortEnable:
582 //
583 // Ports may only be enabled by the xHC. Software cannot enable a port by writing a '1' to this flag.
584 // A port may be disabled by software writing a '1' to this flag.
585 //
586 State |= XHC_PORTSC_PED;
587 State &= ~XHC_PORTSC_RESET;
588 XhcWriteOpReg (Xhc, Offset, State);
589 break;
590
591 case EfiUsbPortSuspend:
592 State |= XHC_PORTSC_LWS;
593 XhcWriteOpReg (Xhc, Offset, State);
594 State &= ~XHC_PORTSC_PLS;
595 XhcWriteOpReg (Xhc, Offset, State);
596 break;
597
598 case EfiUsbPortReset:
599 //
600 // PORTSC_RESET BIT(4) bit is RW1S attribute, which means Write-1-to-set status:
601 // Register bits indicate status when read, a clear bit may be set by
602 // writing a '1'. Writing a '0' to RW1S bits has no effect.
603 //
604 break;
605
606 case EfiUsbPortOwner:
607 //
608 // XHCI root hub port don't has the owner bit, ignore the operation
609 //
610 break;
611
612 case EfiUsbPortConnectChange:
613 //
614 // Clear connect status change
615 //
616 State |= XHC_PORTSC_CSC;
617 XhcWriteOpReg (Xhc, Offset, State);
618 break;
619
620 case EfiUsbPortEnableChange:
621 //
622 // Clear enable status change
623 //
624 State |= XHC_PORTSC_PEC;
625 XhcWriteOpReg (Xhc, Offset, State);
626 break;
627
628 case EfiUsbPortOverCurrentChange:
629 //
630 // Clear PortOverCurrent change
631 //
632 State |= XHC_PORTSC_OCC;
633 XhcWriteOpReg (Xhc, Offset, State);
634 break;
635
636 case EfiUsbPortResetChange:
637 //
638 // Clear Port Reset change
639 //
640 State |= XHC_PORTSC_PRC;
641 XhcWriteOpReg (Xhc, Offset, State);
642 break;
643
644 case EfiUsbPortPower:
645 case EfiUsbPortSuspendChange:
646 //
647 // Not supported or not related operation
648 //
649 break;
650
651 default:
652 Status = EFI_INVALID_PARAMETER;
653 break;
654 }
655
656 ON_EXIT:
657 DEBUG ((EFI_D_INFO, "XhcClearRootHubPortFeature: status %r\n", Status));
658 gBS->RestoreTPL (OldTpl);
659
660 return Status;
661 }
662
663
664 /**
665 Submits control transfer to a target USB device.
666
667 @param This This EFI_USB2_HC_PROTOCOL instance.
668 @param DeviceAddress The target device address.
669 @param DeviceSpeed Target device speed.
670 @param MaximumPacketLength Maximum packet size the default control transfer
671 endpoint is capable of sending or receiving.
672 @param Request USB device request to send.
673 @param TransferDirection Specifies the data direction for the data stage
674 @param Data Data buffer to be transmitted or received from USB
675 device.
676 @param DataLength The size (in bytes) of the data buffer.
677 @param TimeOut Indicates the maximum timeout, in millisecond.
678 @param Translator Transaction translator to be used by this device.
679 @param TransferResult Return the result of this control transfer.
680
681 @retval EFI_SUCCESS Transfer was completed successfully.
682 @retval EFI_OUT_OF_RESOURCES The transfer failed due to lack of resources.
683 @retval EFI_INVALID_PARAMETER Some parameters are invalid.
684 @retval EFI_TIMEOUT Transfer failed due to timeout.
685 @retval EFI_DEVICE_ERROR Transfer failed due to host controller or device error.
686
687 **/
688 EFI_STATUS
689 EFIAPI
690 XhcControlTransfer (
691 IN EFI_USB2_HC_PROTOCOL *This,
692 IN UINT8 DeviceAddress,
693 IN UINT8 DeviceSpeed,
694 IN UINTN MaximumPacketLength,
695 IN EFI_USB_DEVICE_REQUEST *Request,
696 IN EFI_USB_DATA_DIRECTION TransferDirection,
697 IN OUT VOID *Data,
698 IN OUT UINTN *DataLength,
699 IN UINTN TimeOut,
700 IN EFI_USB2_HC_TRANSACTION_TRANSLATOR *Translator,
701 OUT UINT32 *TransferResult
702 )
703 {
704 USB_XHCI_DEV *Xhc;
705 URB *Urb;
706 UINT8 Endpoint;
707 UINT8 Index;
708 UINT8 XhciDevAddr;
709 UINT8 DescriptorType;
710 UINT8 SlotId;
711 UINT8 TTT;
712 UINT8 MTT;
713 UINT32 MaxPacket0;
714 EFI_USB_HUB_DESCRIPTOR *HubDesc;
715 EFI_TPL OldTpl;
716 EFI_STATUS Status;
717 EFI_STATUS RecoveryStatus;
718 UINTN MapSize;
719 EFI_USB_PORT_STATUS PortStatus;
720 UINT32 State;
721
722 //
723 // Validate parameters
724 //
725 if ((Request == NULL) || (TransferResult == NULL)) {
726 return EFI_INVALID_PARAMETER;
727 }
728
729 if ((TransferDirection != EfiUsbDataIn) &&
730 (TransferDirection != EfiUsbDataOut) &&
731 (TransferDirection != EfiUsbNoData)) {
732 return EFI_INVALID_PARAMETER;
733 }
734
735 if ((TransferDirection == EfiUsbNoData) &&
736 ((Data != NULL) || (*DataLength != 0))) {
737 return EFI_INVALID_PARAMETER;
738 }
739
740 if ((TransferDirection != EfiUsbNoData) &&
741 ((Data == NULL) || (*DataLength == 0))) {
742 return EFI_INVALID_PARAMETER;
743 }
744
745 if ((MaximumPacketLength != 8) && (MaximumPacketLength != 16) &&
746 (MaximumPacketLength != 32) && (MaximumPacketLength != 64) &&
747 (MaximumPacketLength != 512)
748 ) {
749 return EFI_INVALID_PARAMETER;
750 }
751
752 if ((DeviceSpeed == EFI_USB_SPEED_LOW) && (MaximumPacketLength != 8)) {
753 return EFI_INVALID_PARAMETER;
754 }
755
756 if ((DeviceSpeed == EFI_USB_SPEED_SUPER) && (MaximumPacketLength != 512)) {
757 return EFI_INVALID_PARAMETER;
758 }
759
760 OldTpl = gBS->RaiseTPL (XHC_TPL);
761
762 Xhc = XHC_FROM_THIS (This);
763
764 Status = EFI_DEVICE_ERROR;
765 *TransferResult = EFI_USB_ERR_SYSTEM;
766
767 if (XhcIsHalt (Xhc) || XhcIsSysError (Xhc)) {
768 DEBUG ((EFI_D_ERROR, "XhcControlTransfer: HC halted at entrance\n"));
769 goto ON_EXIT;
770 }
771
772 //
773 // Check if the device is still enabled before every transaction.
774 //
775 SlotId = XhcBusDevAddrToSlotId (DeviceAddress);
776 if (SlotId == 0) {
777 goto ON_EXIT;
778 }
779
780 //
781 // Acquire the actual device address assigned by XHCI's Address_Device cmd.
782 //
783 XhciDevAddr = UsbDevContext[SlotId].XhciDevAddr;
784
785 //
786 // Hook the Set_Address request from UsbBus.
787 // According to XHCI 1.0 spec, the Set_Address request is replaced by XHCI's Address_Device cmd.
788 //
789 if (Request->Request == USB_REQ_SET_ADDRESS) {
790 //
791 // Reset the BusDevAddr field of all disabled entries in UsbDevContext array firstly.
792 // This way is used to clean the history to avoid using wrong device address by XhcAsyncInterruptTransfer().
793 //
794 for (Index = 0; Index < 255; Index++) {
795 if (!UsbDevContext[Index + 1].Enabled &&
796 (UsbDevContext[Index + 1].SlotId != 0) &&
797 (UsbDevContext[Index + 1].BusDevAddr == (UINT8)Request->Value)) {
798 UsbDevContext[Index + 1].BusDevAddr = 0;
799 }
800 }
801 //
802 // The actual device address has been assigned by XHCI during initializing the device slot.
803 // So we just need establish the mapping relationship between the device address requested from UsbBus
804 // and the actual device address assigned by XHCI. The the following invocations through EFI_USB2_HC_PROTOCOL interface
805 // can find out the actual device address by it.
806 //
807 UsbDevContext[SlotId].BusDevAddr = (UINT8)Request->Value;
808 Status = EFI_SUCCESS;
809 goto ON_EXIT;
810 }
811
812 //
813 // BUGBUG: If the port reset operation happens after the usb super speed device is enabled,
814 // The subsequent configuration, such as getting device descriptor, will fail.
815 // So here a workaround is introduced to skip the reset operation if the device is enabled.
816 //
817 if ((Request->Request == USB_REQ_SET_FEATURE) &&
818 (Request->Value == EfiUsbPortReset)) {
819 if (DeviceSpeed == EFI_USB_SPEED_SUPER) {
820 Status = EFI_SUCCESS;
821 goto ON_EXIT;
822 }
823 }
824
825 //
826 // Create a new URB, insert it into the asynchronous
827 // schedule list, then poll the execution status.
828 // Note that we encode the direction in address although default control
829 // endpoint is bidirectional. XhcCreateUrb expects this
830 // combination of Ep addr and its direction.
831 //
832 Endpoint = 0 | ((TransferDirection == EfiUsbDataIn) ? 0x80 : 0);
833 Urb = XhcCreateUrb (
834 Xhc,
835 XhciDevAddr,
836 Endpoint,
837 DeviceSpeed,
838 MaximumPacketLength,
839 XHC_CTRL_TRANSFER,
840 Request,
841 Data,
842 *DataLength,
843 NULL,
844 NULL
845 );
846
847 if (Urb == NULL) {
848 DEBUG ((EFI_D_ERROR, "XhcControlTransfer: failed to create URB"));
849 Status = EFI_OUT_OF_RESOURCES;
850 goto ON_EXIT;
851 }
852 ASSERT (Urb->EvtRing == &Xhc->CtrlTrEventRing);
853 Status = XhcExecTransfer (Xhc, FALSE, Urb, TimeOut);
854
855 //
856 // Get the status from URB. The result is updated in XhcCheckUrbResult
857 // which is called by XhcExecTransfer
858 //
859 *TransferResult = Urb->Result;
860 *DataLength = Urb->Completed;
861
862 if (*TransferResult == EFI_USB_NOERROR) {
863 Status = EFI_SUCCESS;
864 } else if ((*TransferResult == EFI_USB_ERR_STALL) ||
865 (*TransferResult == EFI_USB_ERR_TIMEOUT)) {
866 RecoveryStatus = XhcRecoverHaltedEndpoint(Xhc, Urb);
867 ASSERT_EFI_ERROR (RecoveryStatus);
868 goto FREE_URB;
869 }
870
871 //
872 // Hook Get_Descriptor request from UsbBus as we need evaluate context and configure endpoint.
873 // Hook Get_Status request form UsbBus as we need trace device attach/detach event happened at hub.
874 // Hook Set_Config request from UsbBus as we need configure device endpoint.
875 //
876 if (Request->Request == USB_REQ_GET_DESCRIPTOR) {
877 DescriptorType = (UINT8)(Request->Value >> 8);
878 if ((DescriptorType == USB_DESC_TYPE_DEVICE) && (*DataLength == sizeof (EFI_USB_DEVICE_DESCRIPTOR))) {
879 //
880 // Store a copy of device scriptor as hub device need this info to configure endpoint.
881 //
882 CopyMem (&UsbDevContext[SlotId].DevDesc, Data, *DataLength);
883 if (UsbDevContext[SlotId].DevDesc.BcdUSB == 0x0300) {
884 //
885 // If it's a usb3.0 device, then its max packet size is a 2^n.
886 //
887 MaxPacket0 = 1 << UsbDevContext[SlotId].DevDesc.MaxPacketSize0;
888 } else {
889 MaxPacket0 = UsbDevContext[SlotId].DevDesc.MaxPacketSize0;
890 }
891 UsbDevContext[SlotId].ConfDesc = AllocateZeroPool (UsbDevContext[SlotId].DevDesc.NumConfigurations * sizeof (EFI_USB_CONFIG_DESCRIPTOR *));
892 Status = XhcEvaluateContext (Xhc, SlotId, MaxPacket0);
893 ASSERT_EFI_ERROR (Status);
894 } else if ((DescriptorType == USB_DESC_TYPE_CONFIG) && (*DataLength == ((UINT16 *)Data)[1])) {
895 //
896 // Get configuration value from request, Store the configuration descriptor for Configure_Endpoint cmd.
897 //
898 Index = (UINT8)Request->Value;
899 ASSERT (Index < UsbDevContext[SlotId].DevDesc.NumConfigurations);
900 UsbDevContext[SlotId].ConfDesc[Index] = AllocateZeroPool(*DataLength);
901 CopyMem (UsbDevContext[SlotId].ConfDesc[Index], Data, *DataLength);
902 } else if (((DescriptorType == USB_DESC_TYPE_HUB) ||
903 (DescriptorType == USB_DESC_TYPE_HUB_SUPER_SPEED))) {
904 HubDesc = (EFI_USB_HUB_DESCRIPTOR *)Data;
905 //
906 // The bit 5,6 of HubCharacter field of Hub Descriptor is TTT.
907 //
908 TTT = (UINT8)((HubDesc->HubCharacter & (BIT5 | BIT6)) >> 5);
909 if (UsbDevContext[SlotId].DevDesc.DeviceProtocol == 2) {
910 //
911 // BUGBUG: Don't support multi-TT feature for super speed hub.
912 //
913 MTT = 1;
914 ASSERT (FALSE);
915 } else {
916 MTT = 0;
917 }
918
919 Status = XhcConfigHubContext (
920 Xhc,
921 SlotId,
922 HubDesc->NumPorts,
923 TTT,
924 MTT
925 );
926 }
927 } else if (Request->Request == USB_REQ_SET_CONFIG) {
928 //
929 // Hook Set_Config request from UsbBus as we need configure device endpoint.
930 //
931 for (Index = 0; Index < UsbDevContext[SlotId].DevDesc.NumConfigurations; Index++) {
932 if (UsbDevContext[SlotId].ConfDesc[Index]->ConfigurationValue == (UINT8)Request->Value) {
933 XhcSetConfigCmd (Xhc, SlotId, DeviceSpeed, UsbDevContext[SlotId].ConfDesc[Index]);
934 break;
935 }
936 }
937 } else if (Request->Request == USB_REQ_GET_STATUS) {
938 //
939 // Hook Get_Status request from UsbBus to keep track of the port status change.
940 //
941 State = *(UINT32 *)Data;
942 PortStatus.PortStatus = 0;
943 PortStatus.PortChangeStatus = 0;
944
945 if (DeviceSpeed == EFI_USB_SPEED_SUPER) {
946 //
947 // For super speed hub, its bit10~12 presents the attached device speed.
948 //
949 if ((*(UINT32 *)Data & XHC_PORTSC_PS) >> 10 == 0) {
950 PortStatus.PortStatus |= USB_PORT_STAT_SUPER_SPEED;
951 }
952 } else if (DeviceSpeed == EFI_USB_SPEED_HIGH) {
953 //
954 // For high speed hub, its bit9~10 presents the attached device speed.
955 //
956 if (XHC_BIT_IS_SET (*(UINT32 *)Data, BIT9)) {
957 PortStatus.PortStatus |= USB_PORT_STAT_LOW_SPEED;
958 } else if (XHC_BIT_IS_SET (*(UINT32 *)Data, BIT10)) {
959 PortStatus.PortStatus |= USB_PORT_STAT_HIGH_SPEED;
960 }
961 } else {
962 ASSERT (0);
963 }
964
965 //
966 // Convert the XHCI port/port change state to UEFI status
967 //
968 MapSize = sizeof (mUsbPortStateMap) / sizeof (USB_PORT_STATE_MAP);
969 for (Index = 0; Index < MapSize; Index++) {
970 if (XHC_BIT_IS_SET (*(UINT32 *)Data, mUsbPortStateMap[Index].HwState)) {
971 PortStatus.PortStatus = (UINT16) (PortStatus.PortStatus | mUsbPortStateMap[Index].UefiState);
972 }
973 }
974 MapSize = sizeof (mUsbPortChangeMap) / sizeof (USB_PORT_STATE_MAP);
975
976 for (Index = 0; Index < MapSize; Index++) {
977 if (XHC_BIT_IS_SET (*(UINT32 *)Data, mUsbPortChangeMap[Index].HwState)) {
978 PortStatus.PortChangeStatus = (UINT16) (PortStatus.PortChangeStatus | mUsbPortChangeMap[Index].UefiState);
979 }
980 }
981
982 XhcPollPortStatusChange (Xhc, UsbDevContext[SlotId].RouteString, (UINT8)Request->Index, &PortStatus);
983 }
984
985 FREE_URB:
986 FreePool (Urb);
987
988 ON_EXIT:
989
990 if (EFI_ERROR (Status)) {
991 DEBUG ((EFI_D_ERROR, "XhcControlTransfer: error - %r, transfer - %x\n", Status, *TransferResult));
992 }
993
994 gBS->RestoreTPL (OldTpl);
995
996 return Status;
997 }
998
999
1000 /**
1001 Submits bulk transfer to a bulk endpoint of a USB device.
1002
1003 @param This This EFI_USB2_HC_PROTOCOL instance.
1004 @param DeviceAddress Target device address.
1005 @param EndPointAddress Endpoint number and its direction in bit 7.
1006 @param DeviceSpeed Device speed, Low speed device doesn't support bulk
1007 transfer.
1008 @param MaximumPacketLength Maximum packet size the endpoint is capable of
1009 sending or receiving.
1010 @param DataBuffersNumber Number of data buffers prepared for the transfer.
1011 @param Data Array of pointers to the buffers of data to transmit
1012 from or receive into.
1013 @param DataLength The lenght of the data buffer.
1014 @param DataToggle On input, the initial data toggle for the transfer;
1015 On output, it is updated to to next data toggle to
1016 use of the subsequent bulk transfer.
1017 @param TimeOut Indicates the maximum time, in millisecond, which
1018 the transfer is allowed to complete.
1019 @param Translator A pointr to the transaction translator data.
1020 @param TransferResult A pointer to the detailed result information of the
1021 bulk transfer.
1022
1023 @retval EFI_SUCCESS The transfer was completed successfully.
1024 @retval EFI_OUT_OF_RESOURCES The transfer failed due to lack of resource.
1025 @retval EFI_INVALID_PARAMETER Some parameters are invalid.
1026 @retval EFI_TIMEOUT The transfer failed due to timeout.
1027 @retval EFI_DEVICE_ERROR The transfer failed due to host controller error.
1028
1029 **/
1030 EFI_STATUS
1031 EFIAPI
1032 XhcBulkTransfer (
1033 IN EFI_USB2_HC_PROTOCOL *This,
1034 IN UINT8 DeviceAddress,
1035 IN UINT8 EndPointAddress,
1036 IN UINT8 DeviceSpeed,
1037 IN UINTN MaximumPacketLength,
1038 IN UINT8 DataBuffersNumber,
1039 IN OUT VOID *Data[EFI_USB_MAX_BULK_BUFFER_NUM],
1040 IN OUT UINTN *DataLength,
1041 IN OUT UINT8 *DataToggle,
1042 IN UINTN TimeOut,
1043 IN EFI_USB2_HC_TRANSACTION_TRANSLATOR *Translator,
1044 OUT UINT32 *TransferResult
1045 )
1046 {
1047 USB_XHCI_DEV *Xhc;
1048 URB *Urb;
1049 UINT8 XhciDevAddr;
1050 UINT8 SlotId;
1051 EFI_STATUS Status;
1052 EFI_STATUS RecoveryStatus;
1053 EFI_TPL OldTpl;
1054
1055 //
1056 // Validate the parameters
1057 //
1058 if ((DataLength == NULL) || (*DataLength == 0) ||
1059 (Data == NULL) || (Data[0] == NULL) || (TransferResult == NULL)) {
1060 return EFI_INVALID_PARAMETER;
1061 }
1062
1063 if ((*DataToggle != 0) && (*DataToggle != 1)) {
1064 return EFI_INVALID_PARAMETER;
1065 }
1066
1067 if ((DeviceSpeed == EFI_USB_SPEED_LOW) ||
1068 ((DeviceSpeed == EFI_USB_SPEED_FULL) && (MaximumPacketLength > 64)) ||
1069 ((EFI_USB_SPEED_HIGH == DeviceSpeed) && (MaximumPacketLength > 512)) ||
1070 ((EFI_USB_SPEED_SUPER == DeviceSpeed) && (MaximumPacketLength > 1024))) {
1071 return EFI_INVALID_PARAMETER;
1072 }
1073
1074 OldTpl = gBS->RaiseTPL (XHC_TPL);
1075
1076 Xhc = XHC_FROM_THIS (This);
1077
1078 *TransferResult = EFI_USB_ERR_SYSTEM;
1079 Status = EFI_DEVICE_ERROR;
1080
1081 if (XhcIsHalt (Xhc) || XhcIsSysError (Xhc)) {
1082 DEBUG ((EFI_D_ERROR, "XhcBulkTransfer: HC is halted\n"));
1083 goto ON_EXIT;
1084 }
1085
1086 //
1087 // Check if the device is still enabled before every transaction.
1088 //
1089 SlotId = XhcBusDevAddrToSlotId (DeviceAddress);
1090 if (SlotId == 0) {
1091 goto ON_EXIT;
1092 }
1093
1094 //
1095 // Acquire the actual device address assigned by XHCI's Address_Device cmd.
1096 //
1097 XhciDevAddr = UsbDevContext[SlotId].XhciDevAddr;
1098
1099 //
1100 // Create a new URB, insert it into the asynchronous
1101 // schedule list, then poll the execution status.
1102 //
1103 Urb = XhcCreateUrb (
1104 Xhc,
1105 XhciDevAddr,
1106 EndPointAddress,
1107 DeviceSpeed,
1108 MaximumPacketLength,
1109 XHC_BULK_TRANSFER,
1110 NULL,
1111 Data[0],
1112 *DataLength,
1113 NULL,
1114 NULL
1115 );
1116
1117 if (Urb == NULL) {
1118 DEBUG ((EFI_D_ERROR, "XhcBulkTransfer: failed to create URB\n"));
1119 Status = EFI_OUT_OF_RESOURCES;
1120 goto ON_EXIT;
1121 }
1122
1123 ASSERT (Urb->EvtRing == &Xhc->BulkTrEventRing);
1124
1125 Status = XhcExecTransfer (Xhc, FALSE, Urb, TimeOut);
1126
1127 *TransferResult = Urb->Result;
1128 *DataLength = Urb->Completed;
1129
1130 if (*TransferResult == EFI_USB_NOERROR) {
1131 Status = EFI_SUCCESS;
1132 } else if ((*TransferResult == EFI_USB_ERR_STALL) ||
1133 (*TransferResult == EFI_USB_ERR_TIMEOUT)) {
1134 RecoveryStatus = XhcRecoverHaltedEndpoint(Xhc, Urb);
1135 ASSERT_EFI_ERROR (RecoveryStatus);
1136 }
1137
1138 FreePool (Urb);
1139
1140 ON_EXIT:
1141
1142 if (EFI_ERROR (Status)) {
1143 DEBUG ((EFI_D_ERROR, "XhcBulkTransfer: error - %r, transfer - %x\n", Status, *TransferResult));
1144 }
1145 gBS->RestoreTPL (OldTpl);
1146
1147 return Status;
1148 }
1149
1150 /**
1151 Submits an asynchronous interrupt transfer to an
1152 interrupt endpoint of a USB device.
1153
1154 @param This This EFI_USB2_HC_PROTOCOL instance.
1155 @param DeviceAddress Target device address.
1156 @param EndPointAddress Endpoint number and its direction encoded in bit 7
1157 @param DeviceSpeed Indicates device speed.
1158 @param MaximumPacketLength Maximum packet size the target endpoint is capable
1159 @param IsNewTransfer If TRUE, to submit an new asynchronous interrupt
1160 transfer If FALSE, to remove the specified
1161 asynchronous interrupt.
1162 @param DataToggle On input, the initial data toggle to use; on output,
1163 it is updated to indicate the next data toggle.
1164 @param PollingInterval The he interval, in milliseconds, that the transfer
1165 is polled.
1166 @param DataLength The length of data to receive at the rate specified
1167 by PollingInterval.
1168 @param Translator Transaction translator to use.
1169 @param CallBackFunction Function to call at the rate specified by
1170 PollingInterval.
1171 @param Context Context to CallBackFunction.
1172
1173 @retval EFI_SUCCESS The request has been successfully submitted or canceled.
1174 @retval EFI_INVALID_PARAMETER Some parameters are invalid.
1175 @retval EFI_OUT_OF_RESOURCES The request failed due to a lack of resources.
1176 @retval EFI_DEVICE_ERROR The transfer failed due to host controller error.
1177
1178 **/
1179 EFI_STATUS
1180 EFIAPI
1181 XhcAsyncInterruptTransfer (
1182 IN EFI_USB2_HC_PROTOCOL *This,
1183 IN UINT8 DeviceAddress,
1184 IN UINT8 EndPointAddress,
1185 IN UINT8 DeviceSpeed,
1186 IN UINTN MaximumPacketLength,
1187 IN BOOLEAN IsNewTransfer,
1188 IN OUT UINT8 *DataToggle,
1189 IN UINTN PollingInterval,
1190 IN UINTN DataLength,
1191 IN EFI_USB2_HC_TRANSACTION_TRANSLATOR *Translator,
1192 IN EFI_ASYNC_USB_TRANSFER_CALLBACK CallBackFunction,
1193 IN VOID *Context OPTIONAL
1194 )
1195 {
1196 USB_XHCI_DEV *Xhc;
1197 URB *Urb;
1198 EFI_STATUS Status;
1199 UINT8 XhciDevAddr;
1200 UINT8 SlotId;
1201 UINT8 Index;
1202 UINT8 *Data;
1203 EFI_TPL OldTpl;
1204
1205 //
1206 // Validate parameters
1207 //
1208 if (!XHCI_IS_DATAIN (EndPointAddress)) {
1209 return EFI_INVALID_PARAMETER;
1210 }
1211
1212 if (IsNewTransfer) {
1213 if (DataLength == 0) {
1214 return EFI_INVALID_PARAMETER;
1215 }
1216
1217 if ((*DataToggle != 1) && (*DataToggle != 0)) {
1218 return EFI_INVALID_PARAMETER;
1219 }
1220
1221 if ((PollingInterval > 255) || (PollingInterval < 1)) {
1222 return EFI_INVALID_PARAMETER;
1223 }
1224 }
1225
1226 OldTpl = gBS->RaiseTPL (XHC_TPL);
1227
1228 Xhc = XHC_FROM_THIS (This);
1229
1230 //
1231 // Delete Async interrupt transfer request.
1232 //
1233 if (!IsNewTransfer) {
1234 //
1235 // The delete request may happen after device is detached.
1236 //
1237 for (Index = 0; Index < 255; Index++) {
1238 if ((UsbDevContext[Index + 1].SlotId != 0) &&
1239 (UsbDevContext[Index + 1].BusDevAddr == DeviceAddress)) {
1240 break;
1241 }
1242 }
1243
1244 if (Index == 255) {
1245 Status = EFI_INVALID_PARAMETER;
1246 goto ON_EXIT;
1247 }
1248
1249 //
1250 // Acquire the actual device address assigned by XHCI's Address_Device cmd.
1251 //
1252 XhciDevAddr = UsbDevContext[Index + 1].XhciDevAddr;
1253
1254 Status = XhciDelAsyncIntTransfer (Xhc, XhciDevAddr, EndPointAddress);
1255 DEBUG ((EFI_D_INFO, "XhcAsyncInterruptTransfer: remove old transfer, Status = %r\n", Status));
1256 goto ON_EXIT;
1257 }
1258
1259 Status = EFI_SUCCESS;
1260
1261 if (XhcIsHalt (Xhc) || XhcIsSysError (Xhc)) {
1262 DEBUG ((EFI_D_ERROR, "XhcAsyncInterruptTransfer: HC is halt\n"));
1263 Status = EFI_DEVICE_ERROR;
1264 goto ON_EXIT;
1265 }
1266
1267 //
1268 // Check if the device is still enabled before every transaction.
1269 //
1270 SlotId = XhcBusDevAddrToSlotId (DeviceAddress);
1271 if (SlotId == 0) {
1272 goto ON_EXIT;
1273 }
1274
1275 //
1276 // Acquire the actual device address assigned by XHCI's Address_Device cmd.
1277 //
1278 XhciDevAddr = UsbDevContext[SlotId].XhciDevAddr;
1279
1280 Data = AllocatePool (DataLength);
1281
1282 if (Data == NULL) {
1283 DEBUG ((EFI_D_ERROR, "XhcAsyncInterruptTransfer: failed to allocate buffer\n"));
1284 Status = EFI_OUT_OF_RESOURCES;
1285 goto ON_EXIT;
1286 }
1287
1288 Urb = XhcCreateUrb (
1289 Xhc,
1290 XhciDevAddr,
1291 EndPointAddress,
1292 DeviceSpeed,
1293 MaximumPacketLength,
1294 XHC_INT_TRANSFER_ASYNC,
1295 NULL,
1296 Data,
1297 DataLength,
1298 CallBackFunction,
1299 Context
1300 );
1301
1302 if (Urb == NULL) {
1303 DEBUG ((EFI_D_ERROR, "XhcAsyncInterruptTransfer: failed to create URB\n"));
1304 FreePool (Data);
1305 Status = EFI_OUT_OF_RESOURCES;
1306 goto ON_EXIT;
1307 }
1308
1309 ASSERT (Urb->EvtRing == &Xhc->AsynIntTrEventRing);
1310
1311 InsertHeadList (&Xhc->AsyncIntTransfers, &Urb->UrbList);
1312 //
1313 // Ring the doorbell
1314 //
1315 Status = RingIntTransferDoorBell (Xhc, Urb);
1316
1317 ON_EXIT:
1318 gBS->RestoreTPL (OldTpl);
1319
1320 return Status;
1321 }
1322
1323
1324 /**
1325 Submits synchronous interrupt transfer to an interrupt endpoint
1326 of a USB device.
1327
1328 @param This This EFI_USB2_HC_PROTOCOL instance.
1329 @param DeviceAddress Target device address.
1330 @param EndPointAddress Endpoint number and its direction encoded in bit 7
1331 @param DeviceSpeed Indicates device speed.
1332 @param MaximumPacketLength Maximum packet size the target endpoint is capable
1333 of sending or receiving.
1334 @param Data Buffer of data that will be transmitted to USB
1335 device or received from USB device.
1336 @param DataLength On input, the size, in bytes, of the data buffer; On
1337 output, the number of bytes transferred.
1338 @param DataToggle On input, the initial data toggle to use; on output,
1339 it is updated to indicate the next data toggle.
1340 @param TimeOut Maximum time, in second, to complete.
1341 @param Translator Transaction translator to use.
1342 @param TransferResult Variable to receive the transfer result.
1343
1344 @return EFI_SUCCESS The transfer was completed successfully.
1345 @return EFI_OUT_OF_RESOURCES The transfer failed due to lack of resource.
1346 @return EFI_INVALID_PARAMETER Some parameters are invalid.
1347 @return EFI_TIMEOUT The transfer failed due to timeout.
1348 @return EFI_DEVICE_ERROR The failed due to host controller or device error
1349
1350 **/
1351 EFI_STATUS
1352 EFIAPI
1353 XhcSyncInterruptTransfer (
1354 IN EFI_USB2_HC_PROTOCOL *This,
1355 IN UINT8 DeviceAddress,
1356 IN UINT8 EndPointAddress,
1357 IN UINT8 DeviceSpeed,
1358 IN UINTN MaximumPacketLength,
1359 IN OUT VOID *Data,
1360 IN OUT UINTN *DataLength,
1361 IN OUT UINT8 *DataToggle,
1362 IN UINTN TimeOut,
1363 IN EFI_USB2_HC_TRANSACTION_TRANSLATOR *Translator,
1364 OUT UINT32 *TransferResult
1365 )
1366 {
1367 USB_XHCI_DEV *Xhc;
1368 URB *Urb;
1369 UINT8 XhciDevAddr;
1370 UINT8 SlotId;
1371 EFI_STATUS Status;
1372 EFI_STATUS RecoveryStatus;
1373 EFI_TPL OldTpl;
1374
1375 //
1376 // Validates parameters
1377 //
1378 if ((DataLength == NULL) || (*DataLength == 0) ||
1379 (Data == NULL) || (TransferResult == NULL)) {
1380 return EFI_INVALID_PARAMETER;
1381 }
1382
1383 if (!XHCI_IS_DATAIN (EndPointAddress)) {
1384 return EFI_INVALID_PARAMETER;
1385 }
1386
1387 if ((*DataToggle != 1) && (*DataToggle != 0)) {
1388 return EFI_INVALID_PARAMETER;
1389 }
1390
1391 if (((DeviceSpeed == EFI_USB_SPEED_LOW) && (MaximumPacketLength != 8)) ||
1392 ((DeviceSpeed == EFI_USB_SPEED_FULL) && (MaximumPacketLength > 64)) ||
1393 ((DeviceSpeed == EFI_USB_SPEED_HIGH) && (MaximumPacketLength > 3072))) {
1394 return EFI_INVALID_PARAMETER;
1395 }
1396
1397 OldTpl = gBS->RaiseTPL (XHC_TPL);
1398
1399 Xhc = XHC_FROM_THIS (This);
1400
1401 *TransferResult = EFI_USB_ERR_SYSTEM;
1402 Status = EFI_DEVICE_ERROR;
1403
1404 if (XhcIsHalt (Xhc) || XhcIsSysError (Xhc)) {
1405 DEBUG ((EFI_D_ERROR, "EhcSyncInterruptTransfer: HC is halt\n"));
1406 goto ON_EXIT;
1407 }
1408
1409 //
1410 // Check if the device is still enabled before every transaction.
1411 //
1412 SlotId = XhcBusDevAddrToSlotId (DeviceAddress);
1413 if (SlotId == 0) {
1414 goto ON_EXIT;
1415 }
1416
1417 //
1418 // Acquire the actual device address assigned by XHCI's Address_Device cmd.
1419 //
1420 XhciDevAddr = UsbDevContext[SlotId].XhciDevAddr;
1421
1422 Urb = XhcCreateUrb (
1423 Xhc,
1424 XhciDevAddr,
1425 EndPointAddress,
1426 DeviceSpeed,
1427 MaximumPacketLength,
1428 XHC_INT_TRANSFER_SYNC,
1429 NULL,
1430 Data,
1431 *DataLength,
1432 NULL,
1433 NULL
1434 );
1435
1436 if (Urb == NULL) {
1437 DEBUG ((EFI_D_ERROR, "XhcSyncInterruptTransfer: failed to create URB\n"));
1438 Status = EFI_OUT_OF_RESOURCES;
1439 goto ON_EXIT;
1440 }
1441
1442 Status = XhcExecTransfer (Xhc, FALSE, Urb, TimeOut);
1443
1444 *TransferResult = Urb->Result;
1445 *DataLength = Urb->Completed;
1446
1447 if (*TransferResult == EFI_USB_NOERROR) {
1448 Status = EFI_SUCCESS;
1449 } else if ((*TransferResult == EFI_USB_ERR_STALL) ||
1450 (*TransferResult == EFI_USB_ERR_TIMEOUT)) {
1451 RecoveryStatus = XhcRecoverHaltedEndpoint(Xhc, Urb);
1452 ASSERT_EFI_ERROR (RecoveryStatus);
1453 }
1454
1455 FreePool (Urb);
1456
1457 ON_EXIT:
1458 if (EFI_ERROR (Status)) {
1459 DEBUG ((EFI_D_ERROR, "XhcSyncInterruptTransfer: error - %r, transfer - %x\n", Status, *TransferResult));
1460 }
1461 gBS->RestoreTPL (OldTpl);
1462
1463 return Status;
1464 }
1465
1466
1467 /**
1468 Submits isochronous transfer to a target USB device.
1469
1470 @param This This EFI_USB2_HC_PROTOCOL instance.
1471 @param DeviceAddress Target device address.
1472 @param EndPointAddress End point address with its direction.
1473 @param DeviceSpeed Device speed, Low speed device doesn't support this
1474 type.
1475 @param MaximumPacketLength Maximum packet size that the endpoint is capable of
1476 sending or receiving.
1477 @param DataBuffersNumber Number of data buffers prepared for the transfer.
1478 @param Data Array of pointers to the buffers of data that will
1479 be transmitted to USB device or received from USB
1480 device.
1481 @param DataLength The size, in bytes, of the data buffer.
1482 @param Translator Transaction translator to use.
1483 @param TransferResult Variable to receive the transfer result.
1484
1485 @return EFI_UNSUPPORTED Isochronous transfer is unsupported.
1486
1487 **/
1488 EFI_STATUS
1489 EFIAPI
1490 XhcIsochronousTransfer (
1491 IN EFI_USB2_HC_PROTOCOL *This,
1492 IN UINT8 DeviceAddress,
1493 IN UINT8 EndPointAddress,
1494 IN UINT8 DeviceSpeed,
1495 IN UINTN MaximumPacketLength,
1496 IN UINT8 DataBuffersNumber,
1497 IN OUT VOID *Data[EFI_USB_MAX_ISO_BUFFER_NUM],
1498 IN UINTN DataLength,
1499 IN EFI_USB2_HC_TRANSACTION_TRANSLATOR *Translator,
1500 OUT UINT32 *TransferResult
1501 )
1502 {
1503 return EFI_UNSUPPORTED;
1504 }
1505
1506
1507 /**
1508 Submits Async isochronous transfer to a target USB device.
1509
1510 @param This This EFI_USB2_HC_PROTOCOL instance.
1511 @param DeviceAddress Target device address.
1512 @param EndPointAddress End point address with its direction.
1513 @param DeviceSpeed Device speed, Low speed device doesn't support this
1514 type.
1515 @param MaximumPacketLength Maximum packet size that the endpoint is capable of
1516 sending or receiving.
1517 @param DataBuffersNumber Number of data buffers prepared for the transfer.
1518 @param Data Array of pointers to the buffers of data that will
1519 be transmitted to USB device or received from USB
1520 device.
1521 @param DataLength The size, in bytes, of the data buffer.
1522 @param Translator Transaction translator to use.
1523 @param IsochronousCallBack Function to be called when the transfer complete.
1524 @param Context Context passed to the call back function as
1525 parameter.
1526
1527 @return EFI_UNSUPPORTED Isochronous transfer isn't supported.
1528
1529 **/
1530 EFI_STATUS
1531 EFIAPI
1532 XhcAsyncIsochronousTransfer (
1533 IN EFI_USB2_HC_PROTOCOL *This,
1534 IN UINT8 DeviceAddress,
1535 IN UINT8 EndPointAddress,
1536 IN UINT8 DeviceSpeed,
1537 IN UINTN MaximumPacketLength,
1538 IN UINT8 DataBuffersNumber,
1539 IN OUT VOID *Data[EFI_USB_MAX_ISO_BUFFER_NUM],
1540 IN UINTN DataLength,
1541 IN EFI_USB2_HC_TRANSACTION_TRANSLATOR *Translator,
1542 IN EFI_ASYNC_USB_TRANSFER_CALLBACK IsochronousCallBack,
1543 IN VOID *Context
1544 )
1545 {
1546 return EFI_UNSUPPORTED;
1547 }
1548
1549 /**
1550 Entry point for EFI drivers.
1551
1552 @param ImageHandle EFI_HANDLE.
1553 @param SystemTable EFI_SYSTEM_TABLE.
1554
1555 @retval EFI_SUCCESS Success.
1556 @retval Others Fail.
1557
1558 **/
1559 EFI_STATUS
1560 EFIAPI
1561 XhcDriverEntryPoint (
1562 IN EFI_HANDLE ImageHandle,
1563 IN EFI_SYSTEM_TABLE *SystemTable
1564 )
1565 {
1566 return EfiLibInstallDriverBindingComponentName2 (
1567 ImageHandle,
1568 SystemTable,
1569 &gXhciDriverBinding,
1570 ImageHandle,
1571 &gXhciComponentName,
1572 &gXhciComponentName2
1573 );
1574 }
1575
1576
1577 /**
1578 Test to see if this driver supports ControllerHandle. Any
1579 ControllerHandle that has Usb2HcProtocol installed will
1580 be supported.
1581
1582 @param This Protocol instance pointer.
1583 @param Controller Handle of device to test.
1584 @param RemainingDevicePath Not used.
1585
1586 @return EFI_SUCCESS This driver supports this device.
1587 @return EFI_UNSUPPORTED This driver does not support this device.
1588
1589 **/
1590 EFI_STATUS
1591 EFIAPI
1592 XhcDriverBindingSupported (
1593 IN EFI_DRIVER_BINDING_PROTOCOL *This,
1594 IN EFI_HANDLE Controller,
1595 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
1596 )
1597 {
1598 EFI_STATUS Status;
1599 EFI_PCI_IO_PROTOCOL *PciIo;
1600 USB_CLASSC UsbClassCReg;
1601
1602 //
1603 // Test whether there is PCI IO Protocol attached on the controller handle.
1604 //
1605 Status = gBS->OpenProtocol (
1606 Controller,
1607 &gEfiPciIoProtocolGuid,
1608 (VOID **) &PciIo,
1609 This->DriverBindingHandle,
1610 Controller,
1611 EFI_OPEN_PROTOCOL_BY_DRIVER
1612 );
1613
1614 if (EFI_ERROR (Status)) {
1615 return EFI_UNSUPPORTED;
1616 }
1617
1618 Status = PciIo->Pci.Read (
1619 PciIo,
1620 EfiPciIoWidthUint8,
1621 PCI_CLASSCODE_OFFSET,
1622 sizeof (USB_CLASSC) / sizeof (UINT8),
1623 &UsbClassCReg
1624 );
1625
1626 if (EFI_ERROR (Status)) {
1627 Status = EFI_UNSUPPORTED;
1628 goto ON_EXIT;
1629 }
1630
1631 //
1632 // Test whether the controller belongs to Xhci type
1633 //
1634 if ((UsbClassCReg.BaseCode != PCI_CLASS_SERIAL) ||
1635 (UsbClassCReg.SubClassCode != PCI_CLASS_SERIAL_USB) ||
1636 (UsbClassCReg.ProgInterface != PCI_IF_XHCI)) {
1637 Status = EFI_UNSUPPORTED;
1638 }
1639
1640 ON_EXIT:
1641 gBS->CloseProtocol (
1642 Controller,
1643 &gEfiPciIoProtocolGuid,
1644 This->DriverBindingHandle,
1645 Controller
1646 );
1647
1648 return Status;
1649 }
1650
1651 /**
1652 Create and initialize a USB_XHCI_DEV.
1653
1654 @param PciIo The PciIo on this device.
1655 @param OriginalPciAttributes Original PCI attributes.
1656
1657 @return The allocated and initialized USB_XHCI_DEV structure if created,
1658 otherwise NULL.
1659
1660 **/
1661 USB_XHCI_DEV*
1662 XhcCreateUsbHc (
1663 IN EFI_PCI_IO_PROTOCOL *PciIo,
1664 IN UINT64 OriginalPciAttributes
1665 )
1666 {
1667 USB_XHCI_DEV *Xhc;
1668 EFI_STATUS Status;
1669 UINT32 PageSize;
1670 UINT16 ExtCapReg;
1671
1672 ZeroMem (UsbDevContext, sizeof (UsbDevContext));
1673
1674 Xhc = AllocateZeroPool (sizeof (USB_XHCI_DEV));
1675
1676 if (Xhc == NULL) {
1677 return NULL;
1678 }
1679
1680 //
1681 // Init EFI_USB2_HC_PROTOCOL interface and private data structure
1682 //
1683 Xhc->Signature = USB_XHCI_DEV_SIGNATURE;
1684
1685 Xhc->Usb2Hc.GetCapability = XhcGetCapability;
1686 Xhc->Usb2Hc.Reset = XhcReset;
1687 Xhc->Usb2Hc.GetState = XhcGetState;
1688 Xhc->Usb2Hc.SetState = XhcSetState;
1689 Xhc->Usb2Hc.ControlTransfer = XhcControlTransfer;
1690 Xhc->Usb2Hc.BulkTransfer = XhcBulkTransfer;
1691 Xhc->Usb2Hc.AsyncInterruptTransfer = XhcAsyncInterruptTransfer;
1692 Xhc->Usb2Hc.SyncInterruptTransfer = XhcSyncInterruptTransfer;
1693 Xhc->Usb2Hc.IsochronousTransfer = XhcIsochronousTransfer;
1694 Xhc->Usb2Hc.AsyncIsochronousTransfer = XhcAsyncIsochronousTransfer;
1695 Xhc->Usb2Hc.GetRootHubPortStatus = XhcGetRootHubPortStatus;
1696 Xhc->Usb2Hc.SetRootHubPortFeature = XhcSetRootHubPortFeature;
1697 Xhc->Usb2Hc.ClearRootHubPortFeature = XhcClearRootHubPortFeature;
1698 Xhc->Usb2Hc.MajorRevision = 0x3;
1699 Xhc->Usb2Hc.MinorRevision = 0x0;
1700
1701 Xhc->PciIo = PciIo;
1702 Xhc->OriginalPciAttributes = OriginalPciAttributes;
1703
1704 InitializeListHead (&Xhc->AsyncIntTransfers);
1705
1706 //
1707 // Be caution that the Offset passed to XhcReadCapReg() should be Dword align
1708 //
1709 Xhc->CapLength = XhcReadCapReg8 (Xhc, XHC_CAPLENGTH_OFFSET);
1710 Xhc->HcSParams1.Dword = XhcReadCapReg (Xhc, XHC_HCSPARAMS1_OFFSET);
1711 Xhc->HcSParams2.Dword = XhcReadCapReg (Xhc, XHC_HCSPARAMS2_OFFSET);
1712 Xhc->HcCParams.Dword = XhcReadCapReg (Xhc, XHC_HCCPARAMS_OFFSET);
1713 Xhc->DBOff = XhcReadCapReg (Xhc, XHC_DBOFF_OFFSET);
1714 Xhc->RTSOff = XhcReadCapReg (Xhc, XHC_RTSOFF_OFFSET);
1715
1716 //
1717 // This PageSize field defines the page size supported by the xHC implementation.
1718 // This xHC supports a page size of 2^(n+12) if bit n is Set. For example,
1719 // if bit 0 is Set, the xHC supports 4k byte page sizes.
1720 //
1721 PageSize = XhcReadOpReg(Xhc, XHC_PAGESIZE_OFFSET) & XHC_PAGESIZE_MASK;
1722 Xhc->PageSize = 1 << (HighBitSet32(PageSize) + 12);
1723 ASSERT (Xhc->PageSize == 0x1000);
1724
1725 ExtCapReg = (UINT16) (Xhc->HcCParams.Data.ExtCapReg);
1726 Xhc->ExtCapRegBase = ExtCapReg << 2;
1727 Xhc->UsbLegSupOffset = XhcGetLegSupCapAddr (Xhc);
1728
1729 DEBUG ((EFI_D_INFO, "XhcCreateUsb3Hc: capability length 0x%x\n", Xhc->CapLength));
1730 DEBUG ((EFI_D_INFO, "XhcCreateUsb3Hc: HcSParams1 0x%x\n", Xhc->HcSParams1));
1731 DEBUG ((EFI_D_INFO, "XhcCreateUsb3Hc: HcSParams2 0x%x\n", Xhc->HcSParams2));
1732 DEBUG ((EFI_D_INFO, "XhcCreateUsb3Hc: HcCParams 0x%x\n", Xhc->HcCParams));
1733 DEBUG ((EFI_D_INFO, "XhcCreateUsb3Hc: DBOff 0x%x\n", Xhc->DBOff));
1734 DEBUG ((EFI_D_INFO, "XhcCreateUsb3Hc: RTSOff 0x%x\n", Xhc->RTSOff));
1735 DEBUG ((EFI_D_INFO, "XhcCreateUsb3Hc: Xhc->UsbLegSupOffset 0x%x\n", Xhc->UsbLegSupOffset));
1736
1737 //
1738 // Create AsyncRequest Polling Timer
1739 //
1740 Status = gBS->CreateEvent (
1741 EVT_TIMER | EVT_NOTIFY_SIGNAL,
1742 TPL_CALLBACK,
1743 XhcMonitorAsyncRequests,
1744 Xhc,
1745 &Xhc->PollTimer
1746 );
1747
1748 if (EFI_ERROR (Status)) {
1749 goto ON_ERROR;
1750 }
1751
1752 return Xhc;
1753
1754 ON_ERROR:
1755 FreePool (Xhc);
1756 return NULL;
1757 }
1758
1759 /**
1760 One notified function to stop the Host Controller when gBS->ExitBootServices() called.
1761
1762 @param Event Pointer to this event
1763 @param Context Event hanlder private data
1764
1765 **/
1766 VOID
1767 EFIAPI
1768 XhcExitBootService (
1769 EFI_EVENT Event,
1770 VOID *Context
1771 )
1772
1773 {
1774 USB_XHCI_DEV *Xhc;
1775 EFI_PCI_IO_PROTOCOL *PciIo;
1776
1777 Xhc = (USB_XHCI_DEV*) Context;
1778 PciIo = Xhc->PciIo;
1779
1780 //
1781 // Stop AsyncRequest Polling timer then stop the XHCI driver
1782 // and uninstall the XHCI protocl.
1783 //
1784 gBS->SetTimer (Xhc->PollTimer, TimerCancel, XHC_ASYNC_POLL_INTERVAL);
1785 XhcHaltHC (Xhc, XHC_GENERIC_TIMEOUT);
1786
1787 if (Xhc->PollTimer != NULL) {
1788 gBS->CloseEvent (Xhc->PollTimer);
1789 }
1790
1791 //
1792 // Restore original PCI attributes
1793 //
1794 PciIo->Attributes (
1795 PciIo,
1796 EfiPciIoAttributeOperationSet,
1797 Xhc->OriginalPciAttributes,
1798 NULL
1799 );
1800
1801 XhcClearBiosOwnership (Xhc);
1802 }
1803
1804 /**
1805 Starting the Usb XHCI Driver.
1806
1807 @param This Protocol instance pointer.
1808 @param Controller Handle of device to test.
1809 @param RemainingDevicePath Not used.
1810
1811 @return EFI_SUCCESS supports this device.
1812 @return EFI_UNSUPPORTED do not support this device.
1813 @return EFI_DEVICE_ERROR cannot be started due to device Error.
1814 @return EFI_OUT_OF_RESOURCES cannot allocate resources.
1815
1816 **/
1817 EFI_STATUS
1818 EFIAPI
1819 XhcDriverBindingStart (
1820 IN EFI_DRIVER_BINDING_PROTOCOL *This,
1821 IN EFI_HANDLE Controller,
1822 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
1823 )
1824 {
1825 EFI_STATUS Status;
1826 EFI_PCI_IO_PROTOCOL *PciIo;
1827 UINT64 Supports;
1828 UINT64 OriginalPciAttributes;
1829 BOOLEAN PciAttributesSaved;
1830 USB_XHCI_DEV *Xhc;
1831
1832 //
1833 // Open the PciIo Protocol, then enable the USB host controller
1834 //
1835 Status = gBS->OpenProtocol (
1836 Controller,
1837 &gEfiPciIoProtocolGuid,
1838 (VOID **) &PciIo,
1839 This->DriverBindingHandle,
1840 Controller,
1841 EFI_OPEN_PROTOCOL_BY_DRIVER
1842 );
1843
1844 if (EFI_ERROR (Status)) {
1845 return Status;
1846 }
1847
1848 PciAttributesSaved = FALSE;
1849 //
1850 // Save original PCI attributes
1851 //
1852 Status = PciIo->Attributes (
1853 PciIo,
1854 EfiPciIoAttributeOperationGet,
1855 0,
1856 &OriginalPciAttributes
1857 );
1858
1859 if (EFI_ERROR (Status)) {
1860 goto CLOSE_PCIIO;
1861 }
1862 PciAttributesSaved = TRUE;
1863
1864 Status = PciIo->Attributes (
1865 PciIo,
1866 EfiPciIoAttributeOperationSupported,
1867 0,
1868 &Supports
1869 );
1870 if (!EFI_ERROR (Status)) {
1871 Supports &= EFI_PCI_DEVICE_ENABLE;
1872 Status = PciIo->Attributes (
1873 PciIo,
1874 EfiPciIoAttributeOperationEnable,
1875 Supports,
1876 NULL
1877 );
1878 }
1879
1880 if (EFI_ERROR (Status)) {
1881 DEBUG ((EFI_D_ERROR, "XhcDriverBindingStart: failed to enable controller\n"));
1882 goto CLOSE_PCIIO;
1883 }
1884
1885 //
1886 // Create then install USB2_HC_PROTOCOL
1887 //
1888 Xhc = XhcCreateUsbHc (PciIo, OriginalPciAttributes);
1889
1890 if (Xhc == NULL) {
1891 DEBUG ((EFI_D_ERROR, "XhcDriverBindingStart: failed to create USB2_HC\n"));
1892 return EFI_OUT_OF_RESOURCES;
1893 }
1894
1895 XhcSetBiosOwnership (Xhc);
1896
1897 XhcResetHC (Xhc, XHC_RESET_TIMEOUT);
1898 ASSERT (XhcIsHalt (Xhc));
1899
1900 //
1901 // After Chip Hardware Reset wait until the Controller Not Ready (CNR) flag
1902 // in the USBSTS is '0' before writing any xHC Operational or Runtime registers.
1903 //
1904 ASSERT (!(XHC_REG_BIT_IS_SET (Xhc, XHC_USBSTS_OFFSET, XHC_USBSTS_CNR)));
1905
1906 //
1907 // Initialize the schedule
1908 //
1909 XhcInitSched (Xhc);
1910
1911 //
1912 // Start the Host Controller
1913 //
1914 XhcRunHC(Xhc, XHC_GENERIC_TIMEOUT);
1915
1916 //
1917 // Start the asynchronous interrupt monitor
1918 //
1919 Status = gBS->SetTimer (Xhc->PollTimer, TimerPeriodic, XHC_ASYNC_POLL_INTERVAL);
1920 if (EFI_ERROR (Status)) {
1921 DEBUG ((EFI_D_ERROR, "XhcDriverBindingStart: failed to start async interrupt monitor\n"));
1922 XhcHaltHC (Xhc, XHC_GENERIC_TIMEOUT);
1923 goto FREE_POOL;
1924 }
1925
1926 //
1927 // Create event to stop the HC when exit boot service.
1928 //
1929 Status = gBS->CreateEventEx (
1930 EVT_NOTIFY_SIGNAL,
1931 TPL_NOTIFY,
1932 XhcExitBootService,
1933 Xhc,
1934 &gEfiEventExitBootServicesGuid,
1935 &Xhc->ExitBootServiceEvent
1936 );
1937 if (EFI_ERROR (Status)) {
1938 goto FREE_POOL;
1939 }
1940
1941 //
1942 // Install the component name protocol, don't fail the start
1943 // because of something for display.
1944 //
1945 AddUnicodeString2 (
1946 "eng",
1947 gXhciComponentName.SupportedLanguages,
1948 &Xhc->ControllerNameTable,
1949 L"eXtensible Host Controller (USB 3.0)",
1950 TRUE
1951 );
1952 AddUnicodeString2 (
1953 "en",
1954 gXhciComponentName2.SupportedLanguages,
1955 &Xhc->ControllerNameTable,
1956 L"eXtensible Host Controller (USB 3.0)",
1957 FALSE
1958 );
1959
1960 Status = gBS->InstallProtocolInterface (
1961 &Controller,
1962 &gEfiUsb2HcProtocolGuid,
1963 EFI_NATIVE_INTERFACE,
1964 &Xhc->Usb2Hc
1965 );
1966 if (EFI_ERROR (Status)) {
1967 DEBUG ((EFI_D_ERROR, "XhcDriverBindingStart: failed to install USB2_HC Protocol\n"));
1968 goto FREE_POOL;
1969 }
1970
1971 DEBUG ((EFI_D_INFO, "XhcDriverBindingStart: XHCI started for controller @ %x\n", Controller));
1972 return EFI_SUCCESS;
1973
1974 FREE_POOL:
1975 gBS->CloseEvent (Xhc->PollTimer);
1976 XhcFreeSched (Xhc);
1977 FreePool (Xhc);
1978
1979 CLOSE_PCIIO:
1980 if (PciAttributesSaved) {
1981 //
1982 // Restore original PCI attributes
1983 //
1984 PciIo->Attributes (
1985 PciIo,
1986 EfiPciIoAttributeOperationSet,
1987 OriginalPciAttributes,
1988 NULL
1989 );
1990 }
1991
1992 gBS->CloseProtocol (
1993 Controller,
1994 &gEfiPciIoProtocolGuid,
1995 This->DriverBindingHandle,
1996 Controller
1997 );
1998
1999 return Status;
2000 }
2001
2002
2003 /**
2004 Stop this driver on ControllerHandle. Support stoping any child handles
2005 created by this driver.
2006
2007 @param This Protocol instance pointer.
2008 @param Controller Handle of device to stop driver on.
2009 @param NumberOfChildren Number of Children in the ChildHandleBuffer.
2010 @param ChildHandleBuffer List of handles for the children we need to stop.
2011
2012 @return EFI_SUCCESS Success.
2013 @return EFI_DEVICE_ERROR Fail.
2014
2015 **/
2016 EFI_STATUS
2017 EFIAPI
2018 XhcDriverBindingStop (
2019 IN EFI_DRIVER_BINDING_PROTOCOL *This,
2020 IN EFI_HANDLE Controller,
2021 IN UINTN NumberOfChildren,
2022 IN EFI_HANDLE *ChildHandleBuffer
2023 )
2024 {
2025 EFI_STATUS Status;
2026 EFI_USB2_HC_PROTOCOL *Usb2Hc;
2027 EFI_PCI_IO_PROTOCOL *PciIo;
2028 USB_XHCI_DEV *Xhc;
2029
2030 //
2031 // Test whether the Controller handler passed in is a valid
2032 // Usb controller handle that should be supported, if not,
2033 // return the error status directly
2034 //
2035 Status = gBS->OpenProtocol (
2036 Controller,
2037 &gEfiUsb2HcProtocolGuid,
2038 (VOID **) &Usb2Hc,
2039 This->DriverBindingHandle,
2040 Controller,
2041 EFI_OPEN_PROTOCOL_GET_PROTOCOL
2042 );
2043
2044 if (EFI_ERROR (Status)) {
2045 return Status;
2046 }
2047
2048 Xhc = XHC_FROM_THIS (Usb2Hc);
2049 PciIo = Xhc->PciIo;
2050
2051 //
2052 // Stop AsyncRequest Polling timer then stop the XHCI driver
2053 // and uninstall the XHCI protocl.
2054 //
2055 gBS->SetTimer (Xhc->PollTimer, TimerCancel, XHC_ASYNC_POLL_INTERVAL);
2056 XhcHaltHC (Xhc, XHC_GENERIC_TIMEOUT);
2057 XhcClearBiosOwnership (Xhc);
2058
2059 Status = gBS->UninstallProtocolInterface (
2060 Controller,
2061 &gEfiUsb2HcProtocolGuid,
2062 Usb2Hc
2063 );
2064
2065 if (EFI_ERROR (Status)) {
2066 return Status;
2067 }
2068
2069 if (Xhc->PollTimer != NULL) {
2070 gBS->CloseEvent (Xhc->PollTimer);
2071 }
2072
2073 if (Xhc->ExitBootServiceEvent != NULL) {
2074 gBS->CloseEvent (Xhc->ExitBootServiceEvent);
2075 }
2076
2077 XhciDelAllAsyncIntTransfers (Xhc);
2078 XhcFreeSched (Xhc);
2079
2080 if (Xhc->ControllerNameTable) {
2081 FreeUnicodeStringTable (Xhc->ControllerNameTable);
2082 }
2083
2084 //
2085 // Restore original PCI attributes
2086 //
2087 PciIo->Attributes (
2088 PciIo,
2089 EfiPciIoAttributeOperationSet,
2090 Xhc->OriginalPciAttributes,
2091 NULL
2092 );
2093
2094 gBS->CloseProtocol (
2095 Controller,
2096 &gEfiPciIoProtocolGuid,
2097 This->DriverBindingHandle,
2098 Controller
2099 );
2100
2101 FreePool (Xhc);
2102
2103 return EFI_SUCCESS;
2104 }
2105