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