]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Bus/Pci/EhciDxe/Ehci.c
6cf4aefea668e596780eea7401147e7ea3135671
[mirror_edk2.git] / MdeModulePkg / Bus / Pci / EhciDxe / Ehci.c
1 /** @file
2 The Ehci controller driver.
3
4 EhciDxe driver is responsible for managing the behavior of EHCI controller.
5 It implements the interfaces of monitoring the status of all ports and transferring
6 Control, Bulk, Interrupt and Isochronous requests to Usb2.0 device.
7
8 Note that EhciDxe driver is enhanced to guarantee that the EHCI controller get attached
9 to the EHCI controller before the UHCI driver attaches to the companion UHCI controller.
10 This way avoids the control transfer on a shared port between EHCI and companion host
11 controller when UHCI gets attached earlier than EHCI and a USB 2.0 device inserts.
12
13 Copyright (c) 2006 - 2010, Intel Corporation
14 All rights reserved. This program and the accompanying materials
15 are licensed and made available under the terms and conditions of the BSD License
16 which accompanies this distribution. The full text of the license may be found at
17 http://opensource.org/licenses/bsd-license.php
18
19 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
20 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
21
22 **/
23
24
25 #include "Ehci.h"
26
27 //
28 // Two arrays used to translate the EHCI port state (change)
29 // to the UEFI protocol's port state (change).
30 //
31 USB_PORT_STATE_MAP mUsbPortStateMap[] = {
32 {PORTSC_CONN, USB_PORT_STAT_CONNECTION},
33 {PORTSC_ENABLED, USB_PORT_STAT_ENABLE},
34 {PORTSC_SUSPEND, USB_PORT_STAT_SUSPEND},
35 {PORTSC_OVERCUR, USB_PORT_STAT_OVERCURRENT},
36 {PORTSC_RESET, USB_PORT_STAT_RESET},
37 {PORTSC_POWER, USB_PORT_STAT_POWER},
38 {PORTSC_OWNER, USB_PORT_STAT_OWNER}
39 };
40
41 USB_PORT_STATE_MAP mUsbPortChangeMap[] = {
42 {PORTSC_CONN_CHANGE, USB_PORT_STAT_C_CONNECTION},
43 {PORTSC_ENABLE_CHANGE, USB_PORT_STAT_C_ENABLE},
44 {PORTSC_OVERCUR_CHANGE, USB_PORT_STAT_C_OVERCURRENT}
45 };
46
47 EFI_DRIVER_BINDING_PROTOCOL
48 gEhciDriverBinding = {
49 EhcDriverBindingSupported,
50 EhcDriverBindingStart,
51 EhcDriverBindingStop,
52 0x30,
53 NULL,
54 NULL
55 };
56
57 /**
58 Retrieves the capability of root hub ports.
59
60 @param This This EFI_USB_HC_PROTOCOL instance.
61 @param MaxSpeed Max speed supported by the controller.
62 @param PortNumber Number of the root hub ports.
63 @param Is64BitCapable Whether the controller supports 64-bit memory
64 addressing.
65
66 @retval EFI_SUCCESS Host controller capability were retrieved successfully.
67 @retval EFI_INVALID_PARAMETER Either of the three capability pointer is NULL.
68
69 **/
70 EFI_STATUS
71 EFIAPI
72 EhcGetCapability (
73 IN EFI_USB2_HC_PROTOCOL *This,
74 OUT UINT8 *MaxSpeed,
75 OUT UINT8 *PortNumber,
76 OUT UINT8 *Is64BitCapable
77 )
78 {
79 USB2_HC_DEV *Ehc;
80 EFI_TPL OldTpl;
81
82 if ((MaxSpeed == NULL) || (PortNumber == NULL) || (Is64BitCapable == NULL)) {
83 return EFI_INVALID_PARAMETER;
84 }
85
86 OldTpl = gBS->RaiseTPL (EHC_TPL);
87 Ehc = EHC_FROM_THIS (This);
88
89 *MaxSpeed = EFI_USB_SPEED_HIGH;
90 *PortNumber = (UINT8) (Ehc->HcStructParams & HCSP_NPORTS);
91 *Is64BitCapable = (UINT8) (Ehc->HcCapParams & HCCP_64BIT);
92
93 DEBUG ((EFI_D_INFO, "EhcGetCapability: %d ports, 64 bit %d\n", *PortNumber, *Is64BitCapable));
94
95 gBS->RestoreTPL (OldTpl);
96 return EFI_SUCCESS;
97 }
98
99
100 /**
101 Provides software reset for the USB host controller.
102
103 @param This This EFI_USB2_HC_PROTOCOL instance.
104 @param Attributes A bit mask of the reset operation to perform.
105
106 @retval EFI_SUCCESS The reset operation succeeded.
107 @retval EFI_INVALID_PARAMETER Attributes is not valid.
108 @retval EFI_UNSUPPOURTED The type of reset specified by Attributes is
109 not currently supported by the host controller.
110 @retval EFI_DEVICE_ERROR Host controller isn't halted to reset.
111
112 **/
113 EFI_STATUS
114 EFIAPI
115 EhcReset (
116 IN EFI_USB2_HC_PROTOCOL *This,
117 IN UINT16 Attributes
118 )
119 {
120 USB2_HC_DEV *Ehc;
121 EFI_TPL OldTpl;
122 EFI_STATUS Status;
123
124 OldTpl = gBS->RaiseTPL (EHC_TPL);
125 Ehc = EHC_FROM_THIS (This);
126
127 switch (Attributes) {
128 case EFI_USB_HC_RESET_GLOBAL:
129 //
130 // Flow through, same behavior as Host Controller Reset
131 //
132 case EFI_USB_HC_RESET_HOST_CONTROLLER:
133 //
134 // Host Controller must be Halt when Reset it
135 //
136 if (!EhcIsHalt (Ehc)) {
137 Status = EhcHaltHC (Ehc, EHC_GENERIC_TIMEOUT);
138
139 if (EFI_ERROR (Status)) {
140 Status = EFI_DEVICE_ERROR;
141 goto ON_EXIT;
142 }
143 }
144
145 //
146 // Clean up the asynchronous transfers, currently only
147 // interrupt supports asynchronous operation.
148 //
149 EhciDelAllAsyncIntTransfers (Ehc);
150 EhcAckAllInterrupt (Ehc);
151 EhcFreeSched (Ehc);
152
153 Status = EhcResetHC (Ehc, EHC_RESET_TIMEOUT);
154
155 if (EFI_ERROR (Status)) {
156 goto ON_EXIT;
157 }
158
159 Status = EhcInitHC (Ehc);
160 break;
161
162 case EFI_USB_HC_RESET_GLOBAL_WITH_DEBUG:
163 case EFI_USB_HC_RESET_HOST_WITH_DEBUG:
164 Status = EFI_UNSUPPORTED;
165 break;
166
167 default:
168 Status = EFI_INVALID_PARAMETER;
169 }
170
171 ON_EXIT:
172 DEBUG ((EFI_D_INFO, "EhcReset: exit status %r\n", Status));
173 gBS->RestoreTPL (OldTpl);
174 return Status;
175 }
176
177
178 /**
179 Retrieve the current state of the USB host controller.
180
181 @param This This EFI_USB2_HC_PROTOCOL instance.
182 @param State Variable to return the current host controller
183 state.
184
185 @retval EFI_SUCCESS Host controller state was returned in State.
186 @retval EFI_INVALID_PARAMETER State is NULL.
187 @retval EFI_DEVICE_ERROR An error was encountered while attempting to
188 retrieve the host controller's current state.
189
190 **/
191 EFI_STATUS
192 EFIAPI
193 EhcGetState (
194 IN CONST EFI_USB2_HC_PROTOCOL *This,
195 OUT EFI_USB_HC_STATE *State
196 )
197 {
198 EFI_TPL OldTpl;
199 USB2_HC_DEV *Ehc;
200
201 if (State == NULL) {
202 return EFI_INVALID_PARAMETER;
203 }
204
205 OldTpl = gBS->RaiseTPL (EHC_TPL);
206 Ehc = EHC_FROM_THIS (This);
207
208 if (EHC_REG_BIT_IS_SET (Ehc, EHC_USBSTS_OFFSET, USBSTS_HALT)) {
209 *State = EfiUsbHcStateHalt;
210 } else {
211 *State = EfiUsbHcStateOperational;
212 }
213
214 gBS->RestoreTPL (OldTpl);
215
216 DEBUG ((EFI_D_INFO, "EhcGetState: current state %d\n", *State));
217 return EFI_SUCCESS;
218 }
219
220
221 /**
222 Sets the USB host controller to a specific state.
223
224 @param This This EFI_USB2_HC_PROTOCOL instance.
225 @param State The state of the host controller that will be set.
226
227 @retval EFI_SUCCESS The USB host controller was successfully placed
228 in the state specified by State.
229 @retval EFI_INVALID_PARAMETER State is invalid.
230 @retval EFI_DEVICE_ERROR Failed to set the state due to device error.
231
232 **/
233 EFI_STATUS
234 EFIAPI
235 EhcSetState (
236 IN EFI_USB2_HC_PROTOCOL *This,
237 IN EFI_USB_HC_STATE State
238 )
239 {
240 USB2_HC_DEV *Ehc;
241 EFI_TPL OldTpl;
242 EFI_STATUS Status;
243 EFI_USB_HC_STATE CurState;
244
245 Status = EhcGetState (This, &CurState);
246
247 if (EFI_ERROR (Status)) {
248 return EFI_DEVICE_ERROR;
249 }
250
251 if (CurState == State) {
252 return EFI_SUCCESS;
253 }
254
255 OldTpl = gBS->RaiseTPL (EHC_TPL);
256 Ehc = EHC_FROM_THIS (This);
257
258 switch (State) {
259 case EfiUsbHcStateHalt:
260 Status = EhcHaltHC (Ehc, EHC_GENERIC_TIMEOUT);
261 break;
262
263 case EfiUsbHcStateOperational:
264 if (EHC_REG_BIT_IS_SET (Ehc, EHC_USBSTS_OFFSET, USBSTS_SYS_ERROR)) {
265 Status = EFI_DEVICE_ERROR;
266 break;
267 }
268
269 //
270 // Software must not write a one to this field unless the host controller
271 // is in the Halted state. Doing so will yield undefined results.
272 // refers to Spec[EHCI1.0-2.3.1]
273 //
274 if (!EHC_REG_BIT_IS_SET (Ehc, EHC_USBSTS_OFFSET, USBSTS_HALT)) {
275 Status = EFI_DEVICE_ERROR;
276 break;
277 }
278
279 Status = EhcRunHC (Ehc, EHC_GENERIC_TIMEOUT);
280 break;
281
282 case EfiUsbHcStateSuspend:
283 Status = EFI_UNSUPPORTED;
284 break;
285
286 default:
287 Status = EFI_INVALID_PARAMETER;
288 }
289
290 DEBUG ((EFI_D_INFO, "EhcSetState: exit status %r\n", Status));
291 gBS->RestoreTPL (OldTpl);
292 return Status;
293 }
294
295
296 /**
297 Retrieves the current status of a USB root hub port.
298
299 @param This This EFI_USB2_HC_PROTOCOL instance.
300 @param PortNumber The root hub port to retrieve the state from.
301 This value is zero-based.
302 @param PortStatus Variable to receive the port state.
303
304 @retval EFI_SUCCESS The status of the USB root hub port specified.
305 by PortNumber was returned in PortStatus.
306 @retval EFI_INVALID_PARAMETER PortNumber is invalid.
307 @retval EFI_DEVICE_ERROR Can't read register.
308
309 **/
310 EFI_STATUS
311 EFIAPI
312 EhcGetRootHubPortStatus (
313 IN CONST EFI_USB2_HC_PROTOCOL *This,
314 IN CONST UINT8 PortNumber,
315 OUT EFI_USB_PORT_STATUS *PortStatus
316 )
317 {
318 USB2_HC_DEV *Ehc;
319 EFI_TPL OldTpl;
320 UINT32 Offset;
321 UINT32 State;
322 UINT32 TotalPort;
323 UINTN Index;
324 UINTN MapSize;
325 EFI_STATUS Status;
326
327 if (PortStatus == NULL) {
328 return EFI_INVALID_PARAMETER;
329 }
330
331 OldTpl = gBS->RaiseTPL (EHC_TPL);
332
333 Ehc = EHC_FROM_THIS (This);
334 Status = EFI_SUCCESS;
335
336 TotalPort = (Ehc->HcStructParams & HCSP_NPORTS);
337
338 if (PortNumber >= TotalPort) {
339 Status = EFI_INVALID_PARAMETER;
340 goto ON_EXIT;
341 }
342
343 Offset = (UINT32) (EHC_PORT_STAT_OFFSET + (4 * PortNumber));
344 PortStatus->PortStatus = 0;
345 PortStatus->PortChangeStatus = 0;
346
347 State = EhcReadOpReg (Ehc, Offset);
348
349 //
350 // Identify device speed. If in K state, it is low speed.
351 // If the port is enabled after reset, the device is of
352 // high speed. The USB bus driver should retrieve the actual
353 // port speed after reset.
354 //
355 if (EHC_BIT_IS_SET (State, PORTSC_LINESTATE_K)) {
356 PortStatus->PortStatus |= USB_PORT_STAT_LOW_SPEED;
357
358 } else if (EHC_BIT_IS_SET (State, PORTSC_ENABLED)) {
359 PortStatus->PortStatus |= USB_PORT_STAT_HIGH_SPEED;
360 }
361
362 //
363 // Convert the EHCI port/port change state to UEFI status
364 //
365 MapSize = sizeof (mUsbPortStateMap) / sizeof (USB_PORT_STATE_MAP);
366
367 for (Index = 0; Index < MapSize; Index++) {
368 if (EHC_BIT_IS_SET (State, mUsbPortStateMap[Index].HwState)) {
369 PortStatus->PortStatus = (UINT16) (PortStatus->PortStatus | mUsbPortStateMap[Index].UefiState);
370 }
371 }
372
373 MapSize = sizeof (mUsbPortChangeMap) / sizeof (USB_PORT_STATE_MAP);
374
375 for (Index = 0; Index < MapSize; Index++) {
376 if (EHC_BIT_IS_SET (State, mUsbPortChangeMap[Index].HwState)) {
377 PortStatus->PortChangeStatus = (UINT16) (PortStatus->PortChangeStatus | mUsbPortChangeMap[Index].UefiState);
378 }
379 }
380
381 ON_EXIT:
382 gBS->RestoreTPL (OldTpl);
383 return Status;
384 }
385
386
387 /**
388 Sets a feature for the specified root hub port.
389
390 @param This This EFI_USB2_HC_PROTOCOL instance.
391 @param PortNumber Root hub port to set.
392 @param PortFeature Feature to set.
393
394 @retval EFI_SUCCESS The feature specified by PortFeature was set.
395 @retval EFI_INVALID_PARAMETER PortNumber is invalid or PortFeature is invalid.
396 @retval EFI_DEVICE_ERROR Can't read register.
397
398 **/
399 EFI_STATUS
400 EFIAPI
401 EhcSetRootHubPortFeature (
402 IN EFI_USB2_HC_PROTOCOL *This,
403 IN UINT8 PortNumber,
404 IN EFI_USB_PORT_FEATURE PortFeature
405 )
406 {
407 USB2_HC_DEV *Ehc;
408 EFI_TPL OldTpl;
409 UINT32 Offset;
410 UINT32 State;
411 UINT32 TotalPort;
412 EFI_STATUS Status;
413
414 OldTpl = gBS->RaiseTPL (EHC_TPL);
415 Ehc = EHC_FROM_THIS (This);
416 Status = EFI_SUCCESS;
417
418 TotalPort = (Ehc->HcStructParams & HCSP_NPORTS);
419
420 if (PortNumber >= TotalPort) {
421 Status = EFI_INVALID_PARAMETER;
422 goto ON_EXIT;
423 }
424
425 Offset = (UINT32) (EHC_PORT_STAT_OFFSET + (4 * PortNumber));
426 State = EhcReadOpReg (Ehc, Offset);
427
428 //
429 // Mask off the port status change bits, these bits are
430 // write clean bit
431 //
432 State &= ~PORTSC_CHANGE_MASK;
433
434 switch (PortFeature) {
435 case EfiUsbPortEnable:
436 //
437 // Sofeware can't set this bit, Port can only be enable by
438 // EHCI as a part of the reset and enable
439 //
440 State |= PORTSC_ENABLED;
441 EhcWriteOpReg (Ehc, Offset, State);
442 break;
443
444 case EfiUsbPortSuspend:
445 State |= PORTSC_SUSPEND;
446 EhcWriteOpReg (Ehc, Offset, State);
447 break;
448
449 case EfiUsbPortReset:
450 //
451 // Make sure Host Controller not halt before reset it
452 //
453 if (EhcIsHalt (Ehc)) {
454 Status = EhcRunHC (Ehc, EHC_GENERIC_TIMEOUT);
455
456 if (EFI_ERROR (Status)) {
457 DEBUG ((EFI_D_INFO, "EhcSetRootHubPortFeature :failed to start HC - %r\n", Status));
458 break;
459 }
460 }
461
462 //
463 // Set one to PortReset bit must also set zero to PortEnable bit
464 //
465 State |= PORTSC_RESET;
466 State &= ~PORTSC_ENABLED;
467 EhcWriteOpReg (Ehc, Offset, State);
468 break;
469
470 case EfiUsbPortPower:
471 //
472 // Not supported, ignore the operation
473 //
474 Status = EFI_SUCCESS;
475 break;
476
477 case EfiUsbPortOwner:
478 State |= PORTSC_OWNER;
479 EhcWriteOpReg (Ehc, Offset, State);
480 break;
481
482 default:
483 Status = EFI_INVALID_PARAMETER;
484 }
485
486 ON_EXIT:
487 DEBUG ((EFI_D_INFO, "EhcSetRootHubPortFeature: exit status %r\n", Status));
488
489 gBS->RestoreTPL (OldTpl);
490 return Status;
491 }
492
493
494 /**
495 Clears a feature for the specified root hub port.
496
497 @param This A pointer to the EFI_USB2_HC_PROTOCOL instance.
498 @param PortNumber Specifies the root hub port whose feature is
499 requested to be cleared.
500 @param PortFeature Indicates the feature selector associated with the
501 feature clear request.
502
503 @retval EFI_SUCCESS The feature specified by PortFeature was cleared
504 for the USB root hub port specified by PortNumber.
505 @retval EFI_INVALID_PARAMETER PortNumber is invalid or PortFeature is invalid.
506 @retval EFI_DEVICE_ERROR Can't read register.
507
508 **/
509 EFI_STATUS
510 EFIAPI
511 EhcClearRootHubPortFeature (
512 IN EFI_USB2_HC_PROTOCOL *This,
513 IN UINT8 PortNumber,
514 IN EFI_USB_PORT_FEATURE PortFeature
515 )
516 {
517 USB2_HC_DEV *Ehc;
518 EFI_TPL OldTpl;
519 UINT32 Offset;
520 UINT32 State;
521 UINT32 TotalPort;
522 EFI_STATUS Status;
523
524 OldTpl = gBS->RaiseTPL (EHC_TPL);
525 Ehc = EHC_FROM_THIS (This);
526 Status = EFI_SUCCESS;
527
528 TotalPort = (Ehc->HcStructParams & HCSP_NPORTS);
529
530 if (PortNumber >= TotalPort) {
531 Status = EFI_INVALID_PARAMETER;
532 goto ON_EXIT;
533 }
534
535 Offset = EHC_PORT_STAT_OFFSET + (4 * PortNumber);
536 State = EhcReadOpReg (Ehc, Offset);
537 State &= ~PORTSC_CHANGE_MASK;
538
539 switch (PortFeature) {
540 case EfiUsbPortEnable:
541 //
542 // Clear PORT_ENABLE feature means disable port.
543 //
544 State &= ~PORTSC_ENABLED;
545 EhcWriteOpReg (Ehc, Offset, State);
546 break;
547
548 case EfiUsbPortSuspend:
549 //
550 // A write of zero to this bit is ignored by the host
551 // controller. The host controller will unconditionally
552 // set this bit to a zero when:
553 // 1. software sets the Forct Port Resume bit to a zero from a one.
554 // 2. software sets the Port Reset bit to a one frome a zero.
555 //
556 State &= ~PORSTSC_RESUME;
557 EhcWriteOpReg (Ehc, Offset, State);
558 break;
559
560 case EfiUsbPortReset:
561 //
562 // Clear PORT_RESET means clear the reset signal.
563 //
564 State &= ~PORTSC_RESET;
565 EhcWriteOpReg (Ehc, Offset, State);
566 break;
567
568 case EfiUsbPortOwner:
569 //
570 // Clear port owner means this port owned by EHC
571 //
572 State &= ~PORTSC_OWNER;
573 EhcWriteOpReg (Ehc, Offset, State);
574 break;
575
576 case EfiUsbPortConnectChange:
577 //
578 // Clear connect status change
579 //
580 State |= PORTSC_CONN_CHANGE;
581 EhcWriteOpReg (Ehc, Offset, State);
582 break;
583
584 case EfiUsbPortEnableChange:
585 //
586 // Clear enable status change
587 //
588 State |= PORTSC_ENABLE_CHANGE;
589 EhcWriteOpReg (Ehc, Offset, State);
590 break;
591
592 case EfiUsbPortOverCurrentChange:
593 //
594 // Clear PortOverCurrent change
595 //
596 State |= PORTSC_OVERCUR_CHANGE;
597 EhcWriteOpReg (Ehc, Offset, State);
598 break;
599
600 case EfiUsbPortPower:
601 case EfiUsbPortSuspendChange:
602 case EfiUsbPortResetChange:
603 //
604 // Not supported or not related operation
605 //
606 break;
607
608 default:
609 Status = EFI_INVALID_PARAMETER;
610 break;
611 }
612
613 ON_EXIT:
614 DEBUG ((EFI_D_INFO, "EhcClearRootHubPortFeature: exit status %r\n", Status));
615 gBS->RestoreTPL (OldTpl);
616 return Status;
617 }
618
619
620 /**
621 Submits control transfer to a target USB device.
622
623 @param This This EFI_USB2_HC_PROTOCOL instance.
624 @param DeviceAddress The target device address.
625 @param DeviceSpeed Target device speed.
626 @param MaximumPacketLength Maximum packet size the default control transfer
627 endpoint is capable of sending or receiving.
628 @param Request USB device request to send.
629 @param TransferDirection Specifies the data direction for the data stage
630 @param Data Data buffer to be transmitted or received from USB
631 device.
632 @param DataLength The size (in bytes) of the data buffer.
633 @param TimeOut Indicates the maximum timeout, in millisecond.
634 @param Translator Transaction translator to be used by this device.
635 @param TransferResult Return the result of this control transfer.
636
637 @retval EFI_SUCCESS Transfer was completed successfully.
638 @retval EFI_OUT_OF_RESOURCES The transfer failed due to lack of resources.
639 @retval EFI_INVALID_PARAMETER Some parameters are invalid.
640 @retval EFI_TIMEOUT Transfer failed due to timeout.
641 @retval EFI_DEVICE_ERROR Transfer failed due to host controller or device error.
642
643 **/
644 EFI_STATUS
645 EFIAPI
646 EhcControlTransfer (
647 IN EFI_USB2_HC_PROTOCOL *This,
648 IN UINT8 DeviceAddress,
649 IN UINT8 DeviceSpeed,
650 IN UINTN MaximumPacketLength,
651 IN EFI_USB_DEVICE_REQUEST *Request,
652 IN EFI_USB_DATA_DIRECTION TransferDirection,
653 IN OUT VOID *Data,
654 IN OUT UINTN *DataLength,
655 IN UINTN TimeOut,
656 IN EFI_USB2_HC_TRANSACTION_TRANSLATOR *Translator,
657 OUT UINT32 *TransferResult
658 )
659 {
660 USB2_HC_DEV *Ehc;
661 URB *Urb;
662 EFI_TPL OldTpl;
663 UINT8 Endpoint;
664 EFI_STATUS Status;
665
666 //
667 // Validate parameters
668 //
669 if ((Request == NULL) || (TransferResult == NULL)) {
670 return EFI_INVALID_PARAMETER;
671 }
672
673 if ((TransferDirection != EfiUsbDataIn) &&
674 (TransferDirection != EfiUsbDataOut) &&
675 (TransferDirection != EfiUsbNoData)) {
676 return EFI_INVALID_PARAMETER;
677 }
678
679 if ((TransferDirection == EfiUsbNoData) &&
680 ((Data != NULL) || (*DataLength != 0))) {
681 return EFI_INVALID_PARAMETER;
682 }
683
684 if ((TransferDirection != EfiUsbNoData) &&
685 ((Data == NULL) || (*DataLength == 0))) {
686 return EFI_INVALID_PARAMETER;
687 }
688
689 if ((MaximumPacketLength != 8) && (MaximumPacketLength != 16) &&
690 (MaximumPacketLength != 32) && (MaximumPacketLength != 64)) {
691 return EFI_INVALID_PARAMETER;
692 }
693
694 if ((DeviceSpeed == EFI_USB_SPEED_LOW) && (MaximumPacketLength != 8)) {
695 return EFI_INVALID_PARAMETER;
696 }
697
698 OldTpl = gBS->RaiseTPL (EHC_TPL);
699 Ehc = EHC_FROM_THIS (This);
700
701 Status = EFI_DEVICE_ERROR;
702 *TransferResult = EFI_USB_ERR_SYSTEM;
703
704 if (EhcIsHalt (Ehc) || EhcIsSysError (Ehc)) {
705 DEBUG ((EFI_D_ERROR, "EhcControlTransfer: HC halted at entrance\n"));
706
707 EhcAckAllInterrupt (Ehc);
708 goto ON_EXIT;
709 }
710
711 EhcAckAllInterrupt (Ehc);
712
713 //
714 // Create a new URB, insert it into the asynchronous
715 // schedule list, then poll the execution status.
716 //
717 //
718 // Encode the direction in address, although default control
719 // endpoint is bidirectional. EhcCreateUrb expects this
720 // combination of Ep addr and its direction.
721 //
722 Endpoint = (UINT8) (0 | ((TransferDirection == EfiUsbDataIn) ? 0x80 : 0));
723 Urb = EhcCreateUrb (
724 Ehc,
725 DeviceAddress,
726 Endpoint,
727 DeviceSpeed,
728 0,
729 MaximumPacketLength,
730 Translator,
731 EHC_CTRL_TRANSFER,
732 Request,
733 Data,
734 *DataLength,
735 NULL,
736 NULL,
737 1
738 );
739
740 if (Urb == NULL) {
741 DEBUG ((EFI_D_ERROR, "EhcControlTransfer: failed to create URB"));
742
743 Status = EFI_OUT_OF_RESOURCES;
744 goto ON_EXIT;
745 }
746
747 EhcLinkQhToAsync (Ehc, Urb->Qh);
748 Status = EhcExecTransfer (Ehc, Urb, TimeOut);
749 EhcUnlinkQhFromAsync (Ehc, Urb->Qh);
750
751 //
752 // Get the status from URB. The result is updated in EhcCheckUrbResult
753 // which is called by EhcExecTransfer
754 //
755 *TransferResult = Urb->Result;
756 *DataLength = Urb->Completed;
757
758 if (*TransferResult == EFI_USB_NOERROR) {
759 Status = EFI_SUCCESS;
760 }
761
762 EhcAckAllInterrupt (Ehc);
763 EhcFreeUrb (Ehc, Urb);
764
765 ON_EXIT:
766 Ehc->PciIo->Flush (Ehc->PciIo);
767 gBS->RestoreTPL (OldTpl);
768
769 if (EFI_ERROR (Status)) {
770 DEBUG ((EFI_D_ERROR, "EhcControlTransfer: error - %r, transfer - %x\n", Status, *TransferResult));
771 }
772
773 return Status;
774 }
775
776
777 /**
778 Submits bulk transfer to a bulk endpoint of a USB device.
779
780 @param This This EFI_USB2_HC_PROTOCOL instance.
781 @param DeviceAddress Target device address.
782 @param EndPointAddress Endpoint number and its direction in bit 7.
783 @param DeviceSpeed Device speed, Low speed device doesn't support bulk
784 transfer.
785 @param MaximumPacketLength Maximum packet size the endpoint is capable of
786 sending or receiving.
787 @param DataBuffersNumber Number of data buffers prepared for the transfer.
788 @param Data Array of pointers to the buffers of data to transmit
789 from or receive into.
790 @param DataLength The lenght of the data buffer.
791 @param DataToggle On input, the initial data toggle for the transfer;
792 On output, it is updated to to next data toggle to
793 use of the subsequent bulk transfer.
794 @param TimeOut Indicates the maximum time, in millisecond, which
795 the transfer is allowed to complete.
796 @param Translator A pointr to the transaction translator data.
797 @param TransferResult A pointer to the detailed result information of the
798 bulk transfer.
799
800 @retval EFI_SUCCESS The transfer was completed successfully.
801 @retval EFI_OUT_OF_RESOURCES The transfer failed due to lack of resource.
802 @retval EFI_INVALID_PARAMETER Some parameters are invalid.
803 @retval EFI_TIMEOUT The transfer failed due to timeout.
804 @retval EFI_DEVICE_ERROR The transfer failed due to host controller error.
805
806 **/
807 EFI_STATUS
808 EFIAPI
809 EhcBulkTransfer (
810 IN EFI_USB2_HC_PROTOCOL *This,
811 IN UINT8 DeviceAddress,
812 IN UINT8 EndPointAddress,
813 IN UINT8 DeviceSpeed,
814 IN UINTN MaximumPacketLength,
815 IN UINT8 DataBuffersNumber,
816 IN OUT VOID *Data[EFI_USB_MAX_BULK_BUFFER_NUM],
817 IN OUT UINTN *DataLength,
818 IN OUT UINT8 *DataToggle,
819 IN UINTN TimeOut,
820 IN EFI_USB2_HC_TRANSACTION_TRANSLATOR *Translator,
821 OUT UINT32 *TransferResult
822 )
823 {
824 USB2_HC_DEV *Ehc;
825 URB *Urb;
826 EFI_TPL OldTpl;
827 EFI_STATUS Status;
828
829 //
830 // Validate the parameters
831 //
832 if ((DataLength == NULL) || (*DataLength == 0) ||
833 (Data == NULL) || (Data[0] == NULL) || (TransferResult == NULL)) {
834 return EFI_INVALID_PARAMETER;
835 }
836
837 if ((*DataToggle != 0) && (*DataToggle != 1)) {
838 return EFI_INVALID_PARAMETER;
839 }
840
841 if ((DeviceSpeed == EFI_USB_SPEED_LOW) ||
842 ((DeviceSpeed == EFI_USB_SPEED_FULL) && (MaximumPacketLength > 64)) ||
843 ((EFI_USB_SPEED_HIGH == DeviceSpeed) && (MaximumPacketLength > 512))) {
844 return EFI_INVALID_PARAMETER;
845 }
846
847 OldTpl = gBS->RaiseTPL (EHC_TPL);
848 Ehc = EHC_FROM_THIS (This);
849
850 *TransferResult = EFI_USB_ERR_SYSTEM;
851 Status = EFI_DEVICE_ERROR;
852
853 if (EhcIsHalt (Ehc) || EhcIsSysError (Ehc)) {
854 DEBUG ((EFI_D_ERROR, "EhcBulkTransfer: HC is halted\n"));
855
856 EhcAckAllInterrupt (Ehc);
857 goto ON_EXIT;
858 }
859
860 EhcAckAllInterrupt (Ehc);
861
862 //
863 // Create a new URB, insert it into the asynchronous
864 // schedule list, then poll the execution status.
865 //
866 Urb = EhcCreateUrb (
867 Ehc,
868 DeviceAddress,
869 EndPointAddress,
870 DeviceSpeed,
871 *DataToggle,
872 MaximumPacketLength,
873 Translator,
874 EHC_BULK_TRANSFER,
875 NULL,
876 Data[0],
877 *DataLength,
878 NULL,
879 NULL,
880 1
881 );
882
883 if (Urb == NULL) {
884 DEBUG ((EFI_D_ERROR, "EhcBulkTransfer: failed to create URB\n"));
885
886 Status = EFI_OUT_OF_RESOURCES;
887 goto ON_EXIT;
888 }
889
890 EhcLinkQhToAsync (Ehc, Urb->Qh);
891 Status = EhcExecTransfer (Ehc, Urb, TimeOut);
892 EhcUnlinkQhFromAsync (Ehc, Urb->Qh);
893
894 *TransferResult = Urb->Result;
895 *DataLength = Urb->Completed;
896 *DataToggle = Urb->DataToggle;
897
898 if (*TransferResult == EFI_USB_NOERROR) {
899 Status = EFI_SUCCESS;
900 }
901
902 EhcAckAllInterrupt (Ehc);
903 EhcFreeUrb (Ehc, Urb);
904
905 ON_EXIT:
906 Ehc->PciIo->Flush (Ehc->PciIo);
907 gBS->RestoreTPL (OldTpl);
908
909 if (EFI_ERROR (Status)) {
910 DEBUG ((EFI_D_ERROR, "EhcBulkTransfer: error - %r, transfer - %x\n", Status, *TransferResult));
911 }
912
913 return Status;
914 }
915
916
917 /**
918 Submits an asynchronous interrupt transfer to an
919 interrupt endpoint of a USB device.
920
921 @param This This EFI_USB2_HC_PROTOCOL instance.
922 @param DeviceAddress Target device address.
923 @param EndPointAddress Endpoint number and its direction encoded in bit 7
924 @param DeviceSpeed Indicates device speed.
925 @param MaximumPacketLength Maximum packet size the target endpoint is capable
926 @param IsNewTransfer If TRUE, to submit an new asynchronous interrupt
927 transfer If FALSE, to remove the specified
928 asynchronous interrupt.
929 @param DataToggle On input, the initial data toggle to use; on output,
930 it is updated to indicate the next data toggle.
931 @param PollingInterval The he interval, in milliseconds, that the transfer
932 is polled.
933 @param DataLength The length of data to receive at the rate specified
934 by PollingInterval.
935 @param Translator Transaction translator to use.
936 @param CallBackFunction Function to call at the rate specified by
937 PollingInterval.
938 @param Context Context to CallBackFunction.
939
940 @retval EFI_SUCCESS The request has been successfully submitted or canceled.
941 @retval EFI_INVALID_PARAMETER Some parameters are invalid.
942 @retval EFI_OUT_OF_RESOURCES The request failed due to a lack of resources.
943 @retval EFI_DEVICE_ERROR The transfer failed due to host controller error.
944
945 **/
946 EFI_STATUS
947 EFIAPI
948 EhcAsyncInterruptTransfer (
949 IN EFI_USB2_HC_PROTOCOL * This,
950 IN UINT8 DeviceAddress,
951 IN UINT8 EndPointAddress,
952 IN UINT8 DeviceSpeed,
953 IN UINTN MaximumPacketLength,
954 IN BOOLEAN IsNewTransfer,
955 IN OUT UINT8 *DataToggle,
956 IN UINTN PollingInterval,
957 IN UINTN DataLength,
958 IN EFI_USB2_HC_TRANSACTION_TRANSLATOR * Translator,
959 IN EFI_ASYNC_USB_TRANSFER_CALLBACK CallBackFunction,
960 IN VOID *Context OPTIONAL
961 )
962 {
963 USB2_HC_DEV *Ehc;
964 URB *Urb;
965 EFI_TPL OldTpl;
966 EFI_STATUS Status;
967 UINT8 *Data;
968
969 //
970 // Validate parameters
971 //
972 if (!(EndPointAddress >= 0x01 && EndPointAddress <= 0x0F)
973 && !(EndPointAddress >= 0x81 && EndPointAddress <= 0x8F)) {
974 return EFI_INVALID_PARAMETER;
975 }
976
977 if (IsNewTransfer) {
978 if (DataLength == 0) {
979 return EFI_INVALID_PARAMETER;
980 }
981
982 if ((*DataToggle != 1) && (*DataToggle != 0)) {
983 return EFI_INVALID_PARAMETER;
984 }
985
986 if ((PollingInterval > 255) || (PollingInterval < 1)) {
987 return EFI_INVALID_PARAMETER;
988 }
989 }
990
991 OldTpl = gBS->RaiseTPL (EHC_TPL);
992 Ehc = EHC_FROM_THIS (This);
993
994 //
995 // Delete Async interrupt transfer request. DataToggle will return
996 // the next data toggle to use.
997 //
998 if (!IsNewTransfer) {
999 Status = EhciDelAsyncIntTransfer (Ehc, DeviceAddress, EndPointAddress, DataToggle);
1000
1001 DEBUG ((EFI_D_INFO, "EhcAsyncInterruptTransfer: remove old transfer - %r\n", Status));
1002 goto ON_EXIT;
1003 }
1004
1005 Status = EFI_SUCCESS;
1006
1007 if (EhcIsHalt (Ehc) || EhcIsSysError (Ehc)) {
1008 DEBUG ((EFI_D_ERROR, "EhcAsyncInterruptTransfer: HC is halt\n"));
1009 EhcAckAllInterrupt (Ehc);
1010
1011 Status = EFI_DEVICE_ERROR;
1012 goto ON_EXIT;
1013 }
1014
1015 EhcAckAllInterrupt (Ehc);
1016
1017 Data = AllocatePool (DataLength);
1018
1019 if (Data == NULL) {
1020 DEBUG ((EFI_D_ERROR, "EhcAsyncInterruptTransfer: failed to allocate buffer\n"));
1021
1022 Status = EFI_OUT_OF_RESOURCES;
1023 goto ON_EXIT;
1024 }
1025
1026 Urb = EhcCreateUrb (
1027 Ehc,
1028 DeviceAddress,
1029 EndPointAddress,
1030 DeviceSpeed,
1031 *DataToggle,
1032 MaximumPacketLength,
1033 Translator,
1034 EHC_INT_TRANSFER_ASYNC,
1035 NULL,
1036 Data,
1037 DataLength,
1038 CallBackFunction,
1039 Context,
1040 PollingInterval
1041 );
1042
1043 if (Urb == NULL) {
1044 DEBUG ((EFI_D_ERROR, "EhcAsyncInterruptTransfer: failed to create URB\n"));
1045
1046 gBS->FreePool (Data);
1047 Status = EFI_OUT_OF_RESOURCES;
1048 goto ON_EXIT;
1049 }
1050
1051 //
1052 // New asynchronous transfer must inserted to the head.
1053 // Check the comments in EhcMoniteAsyncRequests
1054 //
1055 EhcLinkQhToPeriod (Ehc, Urb->Qh);
1056 InsertHeadList (&Ehc->AsyncIntTransfers, &Urb->UrbList);
1057
1058 ON_EXIT:
1059 Ehc->PciIo->Flush (Ehc->PciIo);
1060 gBS->RestoreTPL (OldTpl);
1061
1062 return Status;
1063 }
1064
1065
1066 /**
1067 Submits synchronous interrupt transfer to an interrupt endpoint
1068 of a USB device.
1069
1070 @param This This EFI_USB2_HC_PROTOCOL instance.
1071 @param DeviceAddress Target device address.
1072 @param EndPointAddress Endpoint number and its direction encoded in bit 7
1073 @param DeviceSpeed Indicates device speed.
1074 @param MaximumPacketLength Maximum packet size the target endpoint is capable
1075 of sending or receiving.
1076 @param Data Buffer of data that will be transmitted to USB
1077 device or received from USB device.
1078 @param DataLength On input, the size, in bytes, of the data buffer; On
1079 output, the number of bytes transferred.
1080 @param DataToggle On input, the initial data toggle to use; on output,
1081 it is updated to indicate the next data toggle.
1082 @param TimeOut Maximum time, in second, to complete.
1083 @param Translator Transaction translator to use.
1084 @param TransferResult Variable to receive the transfer result.
1085
1086 @return EFI_SUCCESS The transfer was completed successfully.
1087 @return EFI_OUT_OF_RESOURCES The transfer failed due to lack of resource.
1088 @return EFI_INVALID_PARAMETER Some parameters are invalid.
1089 @return EFI_TIMEOUT The transfer failed due to timeout.
1090 @return EFI_DEVICE_ERROR The failed due to host controller or device error
1091
1092 **/
1093 EFI_STATUS
1094 EFIAPI
1095 EhcSyncInterruptTransfer (
1096 IN EFI_USB2_HC_PROTOCOL *This,
1097 IN UINT8 DeviceAddress,
1098 IN UINT8 EndPointAddress,
1099 IN UINT8 DeviceSpeed,
1100 IN UINTN MaximumPacketLength,
1101 IN OUT VOID *Data,
1102 IN OUT UINTN *DataLength,
1103 IN OUT UINT8 *DataToggle,
1104 IN UINTN TimeOut,
1105 IN EFI_USB2_HC_TRANSACTION_TRANSLATOR *Translator,
1106 OUT UINT32 *TransferResult
1107 )
1108 {
1109 USB2_HC_DEV *Ehc;
1110 EFI_TPL OldTpl;
1111 URB *Urb;
1112 EFI_STATUS Status;
1113
1114 //
1115 // Validates parameters
1116 //
1117 if ((DataLength == NULL) || (*DataLength == 0) ||
1118 (Data == NULL) || (TransferResult == NULL)) {
1119 return EFI_INVALID_PARAMETER;
1120 }
1121
1122 if (!(EndPointAddress >= 0x01 && EndPointAddress <= 0x0F)
1123 && !(EndPointAddress >= 0x81 && EndPointAddress <= 0x8F)) {
1124 return EFI_INVALID_PARAMETER;
1125 }
1126
1127 if ((*DataToggle != 1) && (*DataToggle != 0)) {
1128 return EFI_INVALID_PARAMETER;
1129 }
1130
1131 if (((DeviceSpeed == EFI_USB_SPEED_LOW) && (MaximumPacketLength != 8)) ||
1132 ((DeviceSpeed == EFI_USB_SPEED_FULL) && (MaximumPacketLength > 64)) ||
1133 ((DeviceSpeed == EFI_USB_SPEED_HIGH) && (MaximumPacketLength > 3072))) {
1134 return EFI_INVALID_PARAMETER;
1135 }
1136
1137 OldTpl = gBS->RaiseTPL (EHC_TPL);
1138 Ehc = EHC_FROM_THIS (This);
1139
1140 *TransferResult = EFI_USB_ERR_SYSTEM;
1141 Status = EFI_DEVICE_ERROR;
1142
1143 if (EhcIsHalt (Ehc) || EhcIsSysError (Ehc)) {
1144 DEBUG ((EFI_D_ERROR, "EhcSyncInterruptTransfer: HC is halt\n"));
1145
1146 EhcAckAllInterrupt (Ehc);
1147 goto ON_EXIT;
1148 }
1149
1150 EhcAckAllInterrupt (Ehc);
1151
1152 Urb = EhcCreateUrb (
1153 Ehc,
1154 DeviceAddress,
1155 EndPointAddress,
1156 DeviceSpeed,
1157 *DataToggle,
1158 MaximumPacketLength,
1159 Translator,
1160 EHC_INT_TRANSFER_SYNC,
1161 NULL,
1162 Data,
1163 *DataLength,
1164 NULL,
1165 NULL,
1166 1
1167 );
1168
1169 if (Urb == NULL) {
1170 DEBUG ((EFI_D_ERROR, "EhcSyncInterruptTransfer: failed to create URB\n"));
1171
1172 Status = EFI_OUT_OF_RESOURCES;
1173 goto ON_EXIT;
1174 }
1175
1176 EhcLinkQhToPeriod (Ehc, Urb->Qh);
1177 Status = EhcExecTransfer (Ehc, Urb, TimeOut);
1178 EhcUnlinkQhFromPeriod (Ehc, Urb->Qh);
1179
1180 *TransferResult = Urb->Result;
1181 *DataLength = Urb->Completed;
1182 *DataToggle = Urb->DataToggle;
1183
1184 if (*TransferResult == EFI_USB_NOERROR) {
1185 Status = EFI_SUCCESS;
1186 }
1187
1188 ON_EXIT:
1189 Ehc->PciIo->Flush (Ehc->PciIo);
1190 gBS->RestoreTPL (OldTpl);
1191
1192 if (EFI_ERROR (Status)) {
1193 DEBUG ((EFI_D_ERROR, "EhcSyncInterruptTransfer: error - %r, transfer - %x\n", Status, *TransferResult));
1194 }
1195
1196 return Status;
1197 }
1198
1199
1200 /**
1201 Submits isochronous transfer to a target USB device.
1202
1203 @param This This EFI_USB2_HC_PROTOCOL instance.
1204 @param DeviceAddress Target device address.
1205 @param EndPointAddress End point address with its direction.
1206 @param DeviceSpeed Device speed, Low speed device doesn't support this
1207 type.
1208 @param MaximumPacketLength Maximum packet size that the endpoint is capable of
1209 sending or receiving.
1210 @param DataBuffersNumber Number of data buffers prepared for the transfer.
1211 @param Data Array of pointers to the buffers of data that will
1212 be transmitted to USB device or received from USB
1213 device.
1214 @param DataLength The size, in bytes, of the data buffer.
1215 @param Translator Transaction translator to use.
1216 @param TransferResult Variable to receive the transfer result.
1217
1218 @return EFI_UNSUPPORTED Isochronous transfer is unsupported.
1219
1220 **/
1221 EFI_STATUS
1222 EFIAPI
1223 EhcIsochronousTransfer (
1224 IN EFI_USB2_HC_PROTOCOL *This,
1225 IN UINT8 DeviceAddress,
1226 IN UINT8 EndPointAddress,
1227 IN UINT8 DeviceSpeed,
1228 IN UINTN MaximumPacketLength,
1229 IN UINT8 DataBuffersNumber,
1230 IN OUT VOID *Data[EFI_USB_MAX_ISO_BUFFER_NUM],
1231 IN UINTN DataLength,
1232 IN EFI_USB2_HC_TRANSACTION_TRANSLATOR *Translator,
1233 OUT UINT32 *TransferResult
1234 )
1235 {
1236 return EFI_UNSUPPORTED;
1237 }
1238
1239
1240 /**
1241 Submits Async isochronous transfer to a target USB device.
1242
1243 @param This This EFI_USB2_HC_PROTOCOL instance.
1244 @param DeviceAddress Target device address.
1245 @param EndPointAddress End point address with its direction.
1246 @param DeviceSpeed Device speed, Low speed device doesn't support this
1247 type.
1248 @param MaximumPacketLength Maximum packet size that the endpoint is capable of
1249 sending or receiving.
1250 @param DataBuffersNumber Number of data buffers prepared for the transfer.
1251 @param Data Array of pointers to the buffers of data that will
1252 be transmitted to USB device or received from USB
1253 device.
1254 @param DataLength The size, in bytes, of the data buffer.
1255 @param Translator Transaction translator to use.
1256 @param IsochronousCallBack Function to be called when the transfer complete.
1257 @param Context Context passed to the call back function as
1258 parameter.
1259
1260 @return EFI_UNSUPPORTED Isochronous transfer isn't supported.
1261
1262 **/
1263 EFI_STATUS
1264 EFIAPI
1265 EhcAsyncIsochronousTransfer (
1266 IN EFI_USB2_HC_PROTOCOL *This,
1267 IN UINT8 DeviceAddress,
1268 IN UINT8 EndPointAddress,
1269 IN UINT8 DeviceSpeed,
1270 IN UINTN MaximumPacketLength,
1271 IN UINT8 DataBuffersNumber,
1272 IN OUT VOID *Data[EFI_USB_MAX_ISO_BUFFER_NUM],
1273 IN UINTN DataLength,
1274 IN EFI_USB2_HC_TRANSACTION_TRANSLATOR *Translator,
1275 IN EFI_ASYNC_USB_TRANSFER_CALLBACK IsochronousCallBack,
1276 IN VOID *Context
1277 )
1278 {
1279 return EFI_UNSUPPORTED;
1280 }
1281
1282 /**
1283 Entry point for EFI drivers.
1284
1285 @param ImageHandle EFI_HANDLE.
1286 @param SystemTable EFI_SYSTEM_TABLE.
1287
1288 @return EFI_SUCCESS Success.
1289 EFI_DEVICE_ERROR Fail.
1290
1291 **/
1292 EFI_STATUS
1293 EFIAPI
1294 EhcDriverEntryPoint (
1295 IN EFI_HANDLE ImageHandle,
1296 IN EFI_SYSTEM_TABLE *SystemTable
1297 )
1298 {
1299 return EfiLibInstallDriverBindingComponentName2 (
1300 ImageHandle,
1301 SystemTable,
1302 &gEhciDriverBinding,
1303 ImageHandle,
1304 &gEhciComponentName,
1305 &gEhciComponentName2
1306 );
1307 }
1308
1309
1310 /**
1311 Test to see if this driver supports ControllerHandle. Any
1312 ControllerHandle that has Usb2HcProtocol installed will
1313 be supported.
1314
1315 @param This Protocol instance pointer.
1316 @param Controller Handle of device to test.
1317 @param RemainingDevicePath Not used.
1318
1319 @return EFI_SUCCESS This driver supports this device.
1320 @return EFI_UNSUPPORTED This driver does not support this device.
1321
1322 **/
1323 EFI_STATUS
1324 EFIAPI
1325 EhcDriverBindingSupported (
1326 IN EFI_DRIVER_BINDING_PROTOCOL *This,
1327 IN EFI_HANDLE Controller,
1328 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
1329 )
1330 {
1331 EFI_STATUS Status;
1332 EFI_PCI_IO_PROTOCOL *PciIo;
1333 USB_CLASSC UsbClassCReg;
1334
1335 //
1336 // Test whether there is PCI IO Protocol attached on the controller handle.
1337 //
1338 Status = gBS->OpenProtocol (
1339 Controller,
1340 &gEfiPciIoProtocolGuid,
1341 (VOID **) &PciIo,
1342 This->DriverBindingHandle,
1343 Controller,
1344 EFI_OPEN_PROTOCOL_BY_DRIVER
1345 );
1346
1347 if (EFI_ERROR (Status)) {
1348 return EFI_UNSUPPORTED;
1349 }
1350
1351 Status = PciIo->Pci.Read (
1352 PciIo,
1353 EfiPciIoWidthUint8,
1354 PCI_CLASSCODE_OFFSET,
1355 sizeof (USB_CLASSC) / sizeof (UINT8),
1356 &UsbClassCReg
1357 );
1358
1359 if (EFI_ERROR (Status)) {
1360 Status = EFI_UNSUPPORTED;
1361 goto ON_EXIT;
1362 }
1363
1364 //
1365 // Test whether the controller belongs to Ehci type
1366 //
1367 if ((UsbClassCReg.BaseCode != PCI_CLASS_SERIAL) || (UsbClassCReg.SubClassCode != PCI_CLASS_SERIAL_USB)
1368 || ((UsbClassCReg.ProgInterface != PCI_IF_EHCI) && (UsbClassCReg.ProgInterface !=PCI_IF_UHCI))) {
1369
1370 Status = EFI_UNSUPPORTED;
1371 }
1372
1373 ON_EXIT:
1374 gBS->CloseProtocol (
1375 Controller,
1376 &gEfiPciIoProtocolGuid,
1377 This->DriverBindingHandle,
1378 Controller
1379 );
1380
1381 return Status;
1382 }
1383
1384
1385 /**
1386 Create and initialize a USB2_HC_DEV.
1387
1388 @param PciIo The PciIo on this device.
1389 @param OriginalPciAttributes Original PCI attributes.
1390
1391 @return The allocated and initialized USB2_HC_DEV structure if created,
1392 otherwise NULL.
1393
1394 **/
1395 USB2_HC_DEV *
1396 EhcCreateUsb2Hc (
1397 IN EFI_PCI_IO_PROTOCOL *PciIo,
1398 IN UINT64 OriginalPciAttributes
1399 )
1400 {
1401 USB2_HC_DEV *Ehc;
1402 EFI_STATUS Status;
1403
1404 Ehc = AllocateZeroPool (sizeof (USB2_HC_DEV));
1405
1406 if (Ehc == NULL) {
1407 return NULL;
1408 }
1409
1410 //
1411 // Init EFI_USB2_HC_PROTOCOL interface and private data structure
1412 //
1413 Ehc->Signature = USB2_HC_DEV_SIGNATURE;
1414
1415 Ehc->Usb2Hc.GetCapability = EhcGetCapability;
1416 Ehc->Usb2Hc.Reset = EhcReset;
1417 Ehc->Usb2Hc.GetState = EhcGetState;
1418 Ehc->Usb2Hc.SetState = EhcSetState;
1419 Ehc->Usb2Hc.ControlTransfer = EhcControlTransfer;
1420 Ehc->Usb2Hc.BulkTransfer = EhcBulkTransfer;
1421 Ehc->Usb2Hc.AsyncInterruptTransfer = EhcAsyncInterruptTransfer;
1422 Ehc->Usb2Hc.SyncInterruptTransfer = EhcSyncInterruptTransfer;
1423 Ehc->Usb2Hc.IsochronousTransfer = EhcIsochronousTransfer;
1424 Ehc->Usb2Hc.AsyncIsochronousTransfer = EhcAsyncIsochronousTransfer;
1425 Ehc->Usb2Hc.GetRootHubPortStatus = EhcGetRootHubPortStatus;
1426 Ehc->Usb2Hc.SetRootHubPortFeature = EhcSetRootHubPortFeature;
1427 Ehc->Usb2Hc.ClearRootHubPortFeature = EhcClearRootHubPortFeature;
1428 Ehc->Usb2Hc.MajorRevision = 0x2;
1429 Ehc->Usb2Hc.MinorRevision = 0x0;
1430
1431 Ehc->PciIo = PciIo;
1432 Ehc->OriginalPciAttributes = OriginalPciAttributes;
1433
1434 InitializeListHead (&Ehc->AsyncIntTransfers);
1435
1436 Ehc->HcStructParams = EhcReadCapRegister (Ehc, EHC_HCSPARAMS_OFFSET);
1437 Ehc->HcCapParams = EhcReadCapRegister (Ehc, EHC_HCCPARAMS_OFFSET);
1438 Ehc->CapLen = EhcReadCapRegister (Ehc, EHC_CAPLENGTH_OFFSET) & 0x0FF;
1439
1440 DEBUG ((EFI_D_INFO, "EhcCreateUsb2Hc: capability length %d\n", Ehc->CapLen));
1441
1442 //
1443 // Create AsyncRequest Polling Timer
1444 //
1445 Status = gBS->CreateEvent (
1446 EVT_TIMER | EVT_NOTIFY_SIGNAL,
1447 TPL_CALLBACK,
1448 EhcMonitorAsyncRequests,
1449 Ehc,
1450 &Ehc->PollTimer
1451 );
1452
1453 if (EFI_ERROR (Status)) {
1454 gBS->FreePool (Ehc);
1455 return NULL;
1456 }
1457
1458 return Ehc;
1459 }
1460
1461 /**
1462 One notified function to stop the Host Controller when gBS->ExitBootServices() called.
1463
1464 @param Event Pointer to this event
1465 @param Context Event hanlder private data
1466
1467 **/
1468 VOID
1469 EFIAPI
1470 EhcExitBootService (
1471 EFI_EVENT Event,
1472 VOID *Context
1473 )
1474
1475 {
1476 USB2_HC_DEV *Ehc;
1477
1478 Ehc = (USB2_HC_DEV *) Context;
1479
1480 //
1481 // Stop the Host Controller
1482 //
1483 EhcHaltHC (Ehc, EHC_GENERIC_TIMEOUT);
1484
1485 return;
1486 }
1487
1488
1489 /**
1490 Starting the Usb EHCI Driver.
1491
1492 @param This Protocol instance pointer.
1493 @param Controller Handle of device to test.
1494 @param RemainingDevicePath Not used.
1495
1496 @return EFI_SUCCESS supports this device.
1497 @return EFI_UNSUPPORTED do not support this device.
1498 @return EFI_DEVICE_ERROR cannot be started due to device Error.
1499 @return EFI_OUT_OF_RESOURCES cannot allocate resources.
1500
1501 **/
1502 EFI_STATUS
1503 EFIAPI
1504 EhcDriverBindingStart (
1505 IN EFI_DRIVER_BINDING_PROTOCOL *This,
1506 IN EFI_HANDLE Controller,
1507 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
1508 )
1509 {
1510 EFI_STATUS Status;
1511 USB2_HC_DEV *Ehc;
1512 EFI_PCI_IO_PROTOCOL *PciIo;
1513 EFI_PCI_IO_PROTOCOL *Instance;
1514 UINT64 Supports;
1515 UINT64 OriginalPciAttributes;
1516 BOOLEAN PciAttributesSaved;
1517 USB_CLASSC UsbClassCReg;
1518 EFI_HANDLE *HandleBuffer;
1519 UINTN NumberOfHandles;
1520 UINTN Index;
1521 UINTN UhciSegmentNumber;
1522 UINTN UhciBusNumber;
1523 UINTN UhciDeviceNumber;
1524 UINTN UhciFunctionNumber;
1525 UINTN EhciSegmentNumber;
1526 UINTN EhciBusNumber;
1527 UINTN EhciDeviceNumber;
1528 UINTN EhciFunctionNumber;
1529
1530 //
1531 // Open the PciIo Protocol, then enable the USB host controller
1532 //
1533 Status = gBS->OpenProtocol (
1534 Controller,
1535 &gEfiPciIoProtocolGuid,
1536 (VOID **) &PciIo,
1537 This->DriverBindingHandle,
1538 Controller,
1539 EFI_OPEN_PROTOCOL_BY_DRIVER
1540 );
1541
1542 if (EFI_ERROR (Status)) {
1543 return Status;
1544 }
1545
1546 PciAttributesSaved = FALSE;
1547 //
1548 // Save original PCI attributes
1549 //
1550 Status = PciIo->Attributes (
1551 PciIo,
1552 EfiPciIoAttributeOperationGet,
1553 0,
1554 &OriginalPciAttributes
1555 );
1556
1557 if (EFI_ERROR (Status)) {
1558 goto CLOSE_PCIIO;
1559 }
1560 PciAttributesSaved = TRUE;
1561
1562 Status = PciIo->Attributes (
1563 PciIo,
1564 EfiPciIoAttributeOperationSupported,
1565 0,
1566 &Supports
1567 );
1568 if (!EFI_ERROR (Status)) {
1569 Supports &= EFI_PCI_DEVICE_ENABLE;
1570 Status = PciIo->Attributes (
1571 PciIo,
1572 EfiPciIoAttributeOperationEnable,
1573 Supports,
1574 NULL
1575 );
1576 }
1577
1578 if (EFI_ERROR (Status)) {
1579 DEBUG ((EFI_D_ERROR, "EhcDriverBindingStart: failed to enable controller\n"));
1580 goto CLOSE_PCIIO;
1581 }
1582
1583 //
1584 // Get the Pci device class code.
1585 //
1586 Status = PciIo->Pci.Read (
1587 PciIo,
1588 EfiPciIoWidthUint8,
1589 PCI_CLASSCODE_OFFSET,
1590 sizeof (USB_CLASSC) / sizeof (UINT8),
1591 &UsbClassCReg
1592 );
1593
1594 if (EFI_ERROR (Status)) {
1595 Status = EFI_UNSUPPORTED;
1596 goto CLOSE_PCIIO;
1597 }
1598 //
1599 // determine if the device is UHCI host controller or not. If yes, then find out the
1600 // companion usb ehci host controller and force EHCI driver get attached to it before
1601 // UHCI driver attaches to UHCI host controller.
1602 //
1603 if ((UsbClassCReg.ProgInterface == PCI_IF_UHCI) &&
1604 (UsbClassCReg.BaseCode == PCI_CLASS_SERIAL) &&
1605 (UsbClassCReg.SubClassCode == PCI_CLASS_SERIAL_USB)) {
1606 Status = PciIo->GetLocation (
1607 PciIo,
1608 &UhciSegmentNumber,
1609 &UhciBusNumber,
1610 &UhciDeviceNumber,
1611 &UhciFunctionNumber
1612 );
1613 if (EFI_ERROR (Status)) {
1614 goto CLOSE_PCIIO;
1615 }
1616
1617 Status = gBS->LocateHandleBuffer (
1618 ByProtocol,
1619 &gEfiPciIoProtocolGuid,
1620 NULL,
1621 &NumberOfHandles,
1622 &HandleBuffer
1623 );
1624 if (EFI_ERROR (Status)) {
1625 goto CLOSE_PCIIO;
1626 }
1627
1628 for (Index = 0; Index < NumberOfHandles; Index++) {
1629 //
1630 // Get the device path on this handle
1631 //
1632 Status = gBS->HandleProtocol (
1633 HandleBuffer[Index],
1634 &gEfiPciIoProtocolGuid,
1635 (VOID **)&Instance
1636 );
1637 ASSERT_EFI_ERROR (Status);
1638
1639 Status = Instance->Pci.Read (
1640 Instance,
1641 EfiPciIoWidthUint8,
1642 PCI_CLASSCODE_OFFSET,
1643 sizeof (USB_CLASSC) / sizeof (UINT8),
1644 &UsbClassCReg
1645 );
1646
1647 if (EFI_ERROR (Status)) {
1648 Status = EFI_UNSUPPORTED;
1649 goto CLOSE_PCIIO;
1650 }
1651
1652 if ((UsbClassCReg.ProgInterface == PCI_IF_EHCI) &&
1653 (UsbClassCReg.BaseCode == PCI_CLASS_SERIAL) &&
1654 (UsbClassCReg.SubClassCode == PCI_CLASS_SERIAL_USB)) {
1655 Status = Instance->GetLocation (
1656 Instance,
1657 &EhciSegmentNumber,
1658 &EhciBusNumber,
1659 &EhciDeviceNumber,
1660 &EhciFunctionNumber
1661 );
1662 if (EFI_ERROR (Status)) {
1663 goto CLOSE_PCIIO;
1664 }
1665 //
1666 // Currently, the judgment on the companion usb host controller is through the
1667 // same bus number, which may vary on different platform.
1668 //
1669 if (EhciBusNumber == UhciBusNumber) {
1670 gBS->CloseProtocol (
1671 Controller,
1672 &gEfiPciIoProtocolGuid,
1673 This->DriverBindingHandle,
1674 Controller
1675 );
1676 EhcDriverBindingStart(This, HandleBuffer[Index], NULL);
1677 }
1678 }
1679 }
1680 Status = EFI_NOT_FOUND;
1681 goto CLOSE_PCIIO;
1682 }
1683
1684 //
1685 // Create then install USB2_HC_PROTOCOL
1686 //
1687 Ehc = EhcCreateUsb2Hc (PciIo, OriginalPciAttributes);
1688
1689 if (Ehc == NULL) {
1690 DEBUG ((EFI_D_ERROR, "EhcDriverBindingStart: failed to create USB2_HC\n"));
1691
1692 Status = EFI_OUT_OF_RESOURCES;
1693 goto CLOSE_PCIIO;
1694 }
1695
1696 Status = gBS->InstallProtocolInterface (
1697 &Controller,
1698 &gEfiUsb2HcProtocolGuid,
1699 EFI_NATIVE_INTERFACE,
1700 &Ehc->Usb2Hc
1701 );
1702
1703 if (EFI_ERROR (Status)) {
1704 DEBUG ((EFI_D_ERROR, "EhcDriverBindingStart: failed to install USB2_HC Protocol\n"));
1705 goto FREE_POOL;
1706 }
1707
1708 //
1709 // Robustnesss improvement such as for Duet platform
1710 // Default is not required.
1711 //
1712 if (FeaturePcdGet (PcdTurnOffUsbLegacySupport)) {
1713 EhcClearLegacySupport (Ehc);
1714 }
1715 EhcResetHC (Ehc, EHC_RESET_TIMEOUT);
1716
1717 Status = EhcInitHC (Ehc);
1718
1719 if (EFI_ERROR (Status)) {
1720 DEBUG ((EFI_D_ERROR, "EhcDriverBindingStart: failed to init host controller\n"));
1721 goto UNINSTALL_USBHC;
1722 }
1723
1724 //
1725 // Start the asynchronous interrupt monitor
1726 //
1727 Status = gBS->SetTimer (Ehc->PollTimer, TimerPeriodic, EHC_ASYNC_POLL_INTERVAL);
1728
1729 if (EFI_ERROR (Status)) {
1730 DEBUG ((EFI_D_ERROR, "EhcDriverBindingStart: failed to start async interrupt monitor\n"));
1731
1732 EhcHaltHC (Ehc, EHC_GENERIC_TIMEOUT);
1733 goto UNINSTALL_USBHC;
1734 }
1735
1736 //
1737 // Create event to stop the HC when exit boot service.
1738 //
1739 Status = gBS->CreateEventEx (
1740 EVT_NOTIFY_SIGNAL,
1741 TPL_NOTIFY,
1742 EhcExitBootService,
1743 Ehc,
1744 &gEfiEventExitBootServicesGuid,
1745 &Ehc->ExitBootServiceEvent
1746 );
1747 if (EFI_ERROR (Status)) {
1748 goto UNINSTALL_USBHC;
1749 }
1750
1751 //
1752 // Install the component name protocol, don't fail the start
1753 // because of something for display.
1754 //
1755 AddUnicodeString2 (
1756 "eng",
1757 gEhciComponentName.SupportedLanguages,
1758 &Ehc->ControllerNameTable,
1759 L"Enhanced Host Controller (USB 2.0)",
1760 TRUE
1761 );
1762 AddUnicodeString2 (
1763 "en",
1764 gEhciComponentName2.SupportedLanguages,
1765 &Ehc->ControllerNameTable,
1766 L"Enhanced Host Controller (USB 2.0)",
1767 FALSE
1768 );
1769
1770
1771 DEBUG ((EFI_D_INFO, "EhcDriverBindingStart: EHCI started for controller @ %p\n", Controller));
1772 return EFI_SUCCESS;
1773
1774 UNINSTALL_USBHC:
1775 gBS->UninstallProtocolInterface (
1776 Controller,
1777 &gEfiUsb2HcProtocolGuid,
1778 &Ehc->Usb2Hc
1779 );
1780
1781 FREE_POOL:
1782 EhcFreeSched (Ehc);
1783 gBS->CloseEvent (Ehc->PollTimer);
1784 gBS->FreePool (Ehc);
1785
1786 CLOSE_PCIIO:
1787 if (PciAttributesSaved) {
1788 //
1789 // Restore original PCI attributes
1790 //
1791 PciIo->Attributes (
1792 PciIo,
1793 EfiPciIoAttributeOperationSet,
1794 OriginalPciAttributes,
1795 NULL
1796 );
1797 }
1798
1799 gBS->CloseProtocol (
1800 Controller,
1801 &gEfiPciIoProtocolGuid,
1802 This->DriverBindingHandle,
1803 Controller
1804 );
1805
1806 return Status;
1807 }
1808
1809
1810 /**
1811 Stop this driver on ControllerHandle. Support stoping any child handles
1812 created by this driver.
1813
1814 @param This Protocol instance pointer.
1815 @param Controller Handle of device to stop driver on.
1816 @param NumberOfChildren Number of Children in the ChildHandleBuffer.
1817 @param ChildHandleBuffer List of handles for the children we need to stop.
1818
1819 @return EFI_SUCCESS Success.
1820 @return EFI_DEVICE_ERROR Fail.
1821
1822 **/
1823 EFI_STATUS
1824 EFIAPI
1825 EhcDriverBindingStop (
1826 IN EFI_DRIVER_BINDING_PROTOCOL *This,
1827 IN EFI_HANDLE Controller,
1828 IN UINTN NumberOfChildren,
1829 IN EFI_HANDLE *ChildHandleBuffer
1830 )
1831 {
1832 EFI_STATUS Status;
1833 EFI_USB2_HC_PROTOCOL *Usb2Hc;
1834 EFI_PCI_IO_PROTOCOL *PciIo;
1835 USB2_HC_DEV *Ehc;
1836
1837 //
1838 // Test whether the Controller handler passed in is a valid
1839 // Usb controller handle that should be supported, if not,
1840 // return the error status directly
1841 //
1842 Status = gBS->OpenProtocol (
1843 Controller,
1844 &gEfiUsb2HcProtocolGuid,
1845 (VOID **) &Usb2Hc,
1846 This->DriverBindingHandle,
1847 Controller,
1848 EFI_OPEN_PROTOCOL_GET_PROTOCOL
1849 );
1850
1851 if (EFI_ERROR (Status)) {
1852 return Status;
1853 }
1854
1855 Ehc = EHC_FROM_THIS (Usb2Hc);
1856 PciIo = Ehc->PciIo;
1857
1858 //
1859 // Stop AsyncRequest Polling timer then stop the EHCI driver
1860 // and uninstall the EHCI protocl.
1861 //
1862 gBS->SetTimer (Ehc->PollTimer, TimerCancel, EHC_ASYNC_POLL_INTERVAL);
1863 EhcHaltHC (Ehc, EHC_GENERIC_TIMEOUT);
1864
1865 Status = gBS->UninstallProtocolInterface (
1866 Controller,
1867 &gEfiUsb2HcProtocolGuid,
1868 Usb2Hc
1869 );
1870
1871 if (EFI_ERROR (Status)) {
1872 return Status;
1873 }
1874
1875 if (Ehc->PollTimer != NULL) {
1876 gBS->CloseEvent (Ehc->PollTimer);
1877 }
1878
1879 if (Ehc->ExitBootServiceEvent != NULL) {
1880 gBS->CloseEvent (Ehc->ExitBootServiceEvent);
1881 }
1882
1883 EhcFreeSched (Ehc);
1884
1885 if (Ehc->ControllerNameTable != NULL) {
1886 FreeUnicodeStringTable (Ehc->ControllerNameTable);
1887 }
1888
1889 //
1890 // Disable routing of all ports to EHCI controller, so all ports are
1891 // routed back to the UHCI controller.
1892 //
1893 EhcClearOpRegBit (Ehc, EHC_CONFIG_FLAG_OFFSET, CONFIGFLAG_ROUTE_EHC);
1894
1895 //
1896 // Restore original PCI attributes
1897 //
1898 PciIo->Attributes (
1899 PciIo,
1900 EfiPciIoAttributeOperationSet,
1901 Ehc->OriginalPciAttributes,
1902 NULL
1903 );
1904
1905 gBS->CloseProtocol (
1906 Controller,
1907 &gEfiPciIoProtocolGuid,
1908 This->DriverBindingHandle,
1909 Controller
1910 );
1911
1912 FreePool (Ehc);
1913
1914 return EFI_SUCCESS;
1915 }
1916