]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Bus/Pci/UhciDxe/Uhci.c
a4110b75a2a0323ded4afd68c1f871844d045a2c
[mirror_edk2.git] / MdeModulePkg / Bus / Pci / UhciDxe / Uhci.c
1 /** @file
2
3 The UHCI driver model and HC protocol routines.
4
5 Copyright (c) 2004 - 2008, Intel Corporation
6 All rights reserved. This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
10
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13
14 **/
15
16 #include "Uhci.h"
17
18
19 EFI_DRIVER_BINDING_PROTOCOL gUhciDriverBinding = {
20 UhciDriverBindingSupported,
21 UhciDriverBindingStart,
22 UhciDriverBindingStop,
23 0x20,
24 NULL,
25 NULL
26 };
27
28 /**
29 Provides software reset for the USB host controller according to UEFI 2.0 spec.
30
31 @param This A pointer to the EFI_USB2_HC_PROTOCOL instance.
32 @param Attributes A bit mask of the reset operation to perform. See
33 below for a list of the supported bit mask values.
34
35 @return EFI_SUCCESS The reset operation succeeded.
36 @return EFI_INVALID_PARAMETER Attributes is not valid.
37 @return EFI_UNSUPPORTED This type of reset is not currently supported.
38 @return EFI_DEVICE_ERROR Other errors.
39
40 **/
41 EFI_STATUS
42 EFIAPI
43 Uhci2Reset (
44 IN EFI_USB2_HC_PROTOCOL *This,
45 IN UINT16 Attributes
46 )
47 {
48 USB_HC_DEV *Uhc;
49 EFI_TPL OldTpl;
50
51 if ((Attributes == EFI_USB_HC_RESET_GLOBAL_WITH_DEBUG) ||
52 (Attributes == EFI_USB_HC_RESET_HOST_WITH_DEBUG)) {
53 return EFI_UNSUPPORTED;
54 }
55
56 Uhc = UHC_FROM_USB2_HC_PROTO (This);
57
58 OldTpl = gBS->RaiseTPL (UHCI_TPL);
59
60 switch (Attributes) {
61 case EFI_USB_HC_RESET_GLOBAL:
62 //
63 // Stop schedule and set the Global Reset bit in the command register
64 //
65 UhciStopHc (Uhc, UHC_GENERIC_TIMEOUT);
66 UhciSetRegBit (Uhc->PciIo, USBCMD_OFFSET, USBCMD_GRESET);
67
68 gBS->Stall (UHC_ROOT_PORT_RESET_STALL);
69
70 //
71 // Clear the Global Reset bit to zero.
72 //
73 UhciClearRegBit (Uhc->PciIo, USBCMD_OFFSET, USBCMD_GRESET);
74
75 gBS->Stall (UHC_ROOT_PORT_RECOVERY_STALL);
76 break;
77
78 case EFI_USB_HC_RESET_HOST_CONTROLLER:
79 //
80 // Stop schedule and set Host Controller Reset bit to 1
81 //
82 UhciStopHc (Uhc, UHC_GENERIC_TIMEOUT);
83 UhciSetRegBit (Uhc->PciIo, USBCMD_OFFSET, USBCMD_HCRESET);
84
85 gBS->Stall (UHC_ROOT_PORT_RECOVERY_STALL);
86 break;
87
88 default:
89 goto ON_INVAILD_PARAMETER;
90 }
91
92 //
93 // Delete all old transactions on the USB bus, then
94 // reinitialize the frame list
95 //
96 UhciFreeAllAsyncReq (Uhc);
97 UhciDestoryFrameList (Uhc);
98 UhciInitFrameList (Uhc);
99
100 gBS->RestoreTPL (OldTpl);
101
102 return EFI_SUCCESS;
103
104 ON_INVAILD_PARAMETER:
105
106 gBS->RestoreTPL (OldTpl);
107
108 return EFI_INVALID_PARAMETER;
109 }
110
111
112 /**
113 Retrieves current state of the USB host controller according to UEFI 2.0 spec.
114
115 @param This A pointer to the EFI_USB2_HC_PROTOCOL instance.
116 @param State Variable to receive current device state.
117
118 @return EFI_SUCCESS The state is returned.
119 @return EFI_INVALID_PARAMETER State is not valid.
120 @return EFI_DEVICE_ERROR Other errors.
121
122 **/
123 EFI_STATUS
124 EFIAPI
125 Uhci2GetState (
126 IN CONST EFI_USB2_HC_PROTOCOL *This,
127 OUT EFI_USB_HC_STATE *State
128 )
129 {
130 USB_HC_DEV *Uhc;
131 UINT16 UsbSts;
132 UINT16 UsbCmd;
133
134 if (State == NULL) {
135 return EFI_INVALID_PARAMETER;
136 }
137
138 Uhc = UHC_FROM_USB2_HC_PROTO (This);
139
140 UsbCmd = UhciReadReg (Uhc->PciIo, USBCMD_OFFSET);
141 UsbSts = UhciReadReg (Uhc->PciIo, USBSTS_OFFSET);
142
143 if ((UsbCmd & USBCMD_EGSM) !=0 ) {
144 *State = EfiUsbHcStateSuspend;
145
146 } else if ((UsbSts & USBSTS_HCH) != 0) {
147 *State = EfiUsbHcStateHalt;
148
149 } else {
150 *State = EfiUsbHcStateOperational;
151 }
152
153 return EFI_SUCCESS;
154 }
155
156
157 /**
158 Sets the USB host controller to a specific state according to UEFI 2.0 spec.
159
160 @param This A pointer to the EFI_USB2_HC_PROTOCOL instance.
161 @param State Indicates the state of the host controller that will
162 be set.
163
164 @return EFI_SUCCESS Host controller was successfully placed in the state.
165 @return EFI_INVALID_PARAMETER State is invalid.
166 @return EFI_DEVICE_ERROR Failed to set the state.
167
168 **/
169 EFI_STATUS
170 EFIAPI
171 Uhci2SetState (
172 IN EFI_USB2_HC_PROTOCOL *This,
173 IN EFI_USB_HC_STATE State
174 )
175 {
176 EFI_USB_HC_STATE CurState;
177 USB_HC_DEV *Uhc;
178 EFI_TPL OldTpl;
179 EFI_STATUS Status;
180 UINT16 UsbCmd;
181
182 Uhc = UHC_FROM_USB2_HC_PROTO (This);
183 Status = Uhci2GetState (This, &CurState);
184
185 if (EFI_ERROR (Status)) {
186 return EFI_DEVICE_ERROR;
187 }
188
189 if (CurState == State) {
190 return EFI_SUCCESS;
191 }
192
193 Status = EFI_SUCCESS;
194 OldTpl = gBS->RaiseTPL (UHCI_TPL);
195
196 switch (State) {
197 case EfiUsbHcStateHalt:
198 Status = UhciStopHc (Uhc, UHC_GENERIC_TIMEOUT);
199 break;
200
201 case EfiUsbHcStateOperational:
202 UsbCmd = UhciReadReg (Uhc->PciIo, USBCMD_OFFSET);
203
204 if (CurState == EfiUsbHcStateHalt) {
205 //
206 // Set Run/Stop bit to 1, also set the bandwidht reclamation
207 // point to 64 bytes
208 //
209 UsbCmd |= USBCMD_RS | USBCMD_MAXP;
210 UhciWriteReg (Uhc->PciIo, USBCMD_OFFSET, UsbCmd);
211
212 } else if (CurState == EfiUsbHcStateSuspend) {
213 //
214 // If FGR(Force Global Resume) bit is 0, set it
215 //
216 if ((UsbCmd & USBCMD_FGR) == 0) {
217 UsbCmd |= USBCMD_FGR;
218 UhciWriteReg (Uhc->PciIo, USBCMD_OFFSET, UsbCmd);
219 }
220
221 //
222 // wait 20ms to let resume complete (20ms is specified by UHCI spec)
223 //
224 gBS->Stall (UHC_FORCE_GLOBAL_RESUME_STALL);
225
226 //
227 // Write FGR bit to 0 and EGSM(Enter Global Suspend Mode) bit to 0
228 //
229 UsbCmd &= ~USBCMD_FGR;
230 UsbCmd &= ~USBCMD_EGSM;
231 UsbCmd |= USBCMD_RS;
232 UhciWriteReg (Uhc->PciIo, USBCMD_OFFSET, UsbCmd);
233 }
234
235 break;
236
237 case EfiUsbHcStateSuspend:
238 Status = Uhci2SetState (This, EfiUsbHcStateHalt);
239
240 if (EFI_ERROR (Status)) {
241 Status = EFI_DEVICE_ERROR;
242 goto ON_EXIT;
243 }
244
245 //
246 // Set Enter Global Suspend Mode bit to 1.
247 //
248 UsbCmd = UhciReadReg (Uhc->PciIo, USBCMD_OFFSET);
249 UsbCmd |= USBCMD_EGSM;
250 UhciWriteReg (Uhc->PciIo, USBCMD_OFFSET, UsbCmd);
251 break;
252
253 default:
254 Status = EFI_INVALID_PARAMETER;
255 break;
256 }
257
258 ON_EXIT:
259 gBS->RestoreTPL (OldTpl);
260 return Status;
261 }
262
263 /**
264 Retrieves capabilities of USB host controller according to UEFI 2.0 spec.
265
266 @param This A pointer to the EFI_USB2_HC_PROTOCOL instance.
267 @param MaxSpeed A pointer to the max speed USB host controller
268 supports.
269 @param PortNumber A pointer to the number of root hub ports.
270 @param Is64BitCapable A pointer to an integer to show whether USB host
271 controller supports 64-bit memory addressing.
272
273 @return EFI_SUCCESS capabilities were retrieved successfully.
274 @return EFI_INVALID_PARAMETER MaxSpeed or PortNumber or Is64BitCapable is NULL.
275 @return EFI_DEVICE_ERROR An error was encountered.
276
277 **/
278 EFI_STATUS
279 EFIAPI
280 Uhci2GetCapability (
281 IN EFI_USB2_HC_PROTOCOL *This,
282 OUT UINT8 *MaxSpeed,
283 OUT UINT8 *PortNumber,
284 OUT UINT8 *Is64BitCapable
285 )
286 {
287 USB_HC_DEV *Uhc;
288 UINT32 Offset;
289 UINT16 PortSC;
290 UINT32 Index;
291
292 Uhc = UHC_FROM_USB2_HC_PROTO (This);
293
294 if ((NULL == MaxSpeed) || (NULL == PortNumber) || (NULL == Is64BitCapable)) {
295 return EFI_INVALID_PARAMETER;
296 }
297
298 *MaxSpeed = EFI_USB_SPEED_FULL;
299 *Is64BitCapable = (UINT8) FALSE;
300
301 *PortNumber = 0;
302
303 for (Index = 0; Index < USB_MAX_ROOTHUB_PORT; Index++) {
304 Offset = USBPORTSC_OFFSET + Index * 2;
305 PortSC = UhciReadReg (Uhc->PciIo, Offset);
306
307 //
308 // Port status's bit 7 is reserved and always returns 1 if
309 // the port number is valid. Intel's UHCI (in EHCI controller)
310 // returns 0 in this bit if port number is invalid. Also, if
311 // PciIo IoRead returns error, 0xFFFF is returned to caller.
312 //
313 if (((PortSC & 0x80) == 0) || (PortSC == 0xFFFF)) {
314 break;
315 }
316 (*PortNumber)++;
317 }
318
319 Uhc->RootPorts = *PortNumber;
320
321 DEBUG ((EFI_D_INFO, "Uhci2GetCapability: %d ports\n", (UINT32)Uhc->RootPorts));
322 return EFI_SUCCESS;
323 }
324
325
326 /**
327 Retrieves the current status of a USB root hub port according to UEFI 2.0 spec.
328
329 @param This A pointer to the EFI_USB2_HC_PROTOCOL.
330 @param PortNumber The port to get status.
331 @param PortStatus A pointer to the current port status bits and port
332 status change bits.
333
334 @return EFI_SUCCESS status of the USB root hub port was returned in PortStatus.
335 @return EFI_INVALID_PARAMETER PortNumber is invalid.
336 @return EFI_DEVICE_ERROR Can't read register.
337
338 **/
339 EFI_STATUS
340 EFIAPI
341 Uhci2GetRootHubPortStatus (
342 IN CONST EFI_USB2_HC_PROTOCOL *This,
343 IN CONST UINT8 PortNumber,
344 OUT EFI_USB_PORT_STATUS *PortStatus
345 )
346 {
347 USB_HC_DEV *Uhc;
348 UINT32 Offset;
349 UINT16 PortSC;
350
351 Uhc = UHC_FROM_USB2_HC_PROTO (This);
352
353 if (PortStatus == NULL) {
354 return EFI_INVALID_PARAMETER;
355 }
356
357 if (PortNumber >= Uhc->RootPorts) {
358 return EFI_INVALID_PARAMETER;
359 }
360
361 Offset = USBPORTSC_OFFSET + PortNumber * 2;
362 PortStatus->PortStatus = 0;
363 PortStatus->PortChangeStatus = 0;
364
365 PortSC = UhciReadReg (Uhc->PciIo, Offset);
366
367 if ((PortSC & USBPORTSC_CCS) != 0) {
368 PortStatus->PortStatus |= USB_PORT_STAT_CONNECTION;
369 }
370
371 if ((PortSC & USBPORTSC_PED) != 0) {
372 PortStatus->PortStatus |= USB_PORT_STAT_ENABLE;
373 }
374
375 if ((PortSC & USBPORTSC_SUSP) != 0) {
376 DEBUG ((EFI_D_INFO, "Uhci2GetRootHubPortStatus: port %d is suspended\n", PortNumber));
377 PortStatus->PortStatus |= USB_PORT_STAT_SUSPEND;
378 }
379
380 if ((PortSC & USBPORTSC_PR) != 0) {
381 PortStatus->PortStatus |= USB_PORT_STAT_RESET;
382 }
383
384 if ((PortSC & USBPORTSC_LSDA) != 0) {
385 PortStatus->PortStatus |= USB_PORT_STAT_LOW_SPEED;
386 }
387
388 //
389 // CHC will always return one in port owner bit
390 //
391 PortStatus->PortStatus |= USB_PORT_STAT_OWNER;
392
393 if ((PortSC & USBPORTSC_CSC) != 0) {
394 PortStatus->PortChangeStatus |= USB_PORT_STAT_C_CONNECTION;
395 }
396
397 if ((PortSC & USBPORTSC_PEDC) != 0) {
398 PortStatus->PortChangeStatus |= USB_PORT_STAT_C_ENABLE;
399 }
400
401 return EFI_SUCCESS;
402 }
403
404
405 /**
406 Sets a feature for the specified root hub port according to UEFI 2.0 spec.
407
408 @param This A pointer to the EFI_USB2_HC_PROTOCOL.
409 @param PortNumber Specifies the root hub port whose feature is
410 requested to be set.
411 @param PortFeature Indicates the feature selector associated with the
412 feature set request.
413
414 @return EFI_SUCCESS PortFeature was set for the root port.
415 @return EFI_INVALID_PARAMETER PortNumber is invalid or PortFeature is invalid.
416 @return EFI_DEVICE_ERROR Can't read register.
417
418 **/
419 EFI_STATUS
420 EFIAPI
421 Uhci2SetRootHubPortFeature (
422 IN EFI_USB2_HC_PROTOCOL *This,
423 IN UINT8 PortNumber,
424 IN EFI_USB_PORT_FEATURE PortFeature
425 )
426 {
427 USB_HC_DEV *Uhc;
428 EFI_TPL OldTpl;
429 UINT32 Offset;
430 UINT16 PortSC;
431 UINT16 Command;
432
433 Uhc = UHC_FROM_USB2_HC_PROTO (This);
434
435 if (PortNumber >= Uhc->RootPorts) {
436 return EFI_INVALID_PARAMETER;
437 }
438
439 Offset = USBPORTSC_OFFSET + PortNumber * 2;
440
441 OldTpl = gBS->RaiseTPL (UHCI_TPL);
442 PortSC = UhciReadReg (Uhc->PciIo, Offset);
443
444 switch (PortFeature) {
445 case EfiUsbPortSuspend:
446 Command = UhciReadReg (Uhc->PciIo, USBCMD_OFFSET);
447 if ((Command & USBCMD_EGSM) == 0) {
448 //
449 // if global suspend is not active, can set port suspend
450 //
451 PortSC &= 0xfff5;
452 PortSC |= USBPORTSC_SUSP;
453 }
454 break;
455
456 case EfiUsbPortReset:
457 PortSC &= 0xfff5;
458 PortSC |= USBPORTSC_PR;
459 break;
460
461 case EfiUsbPortPower:
462 //
463 // No action
464 //
465 break;
466
467 case EfiUsbPortEnable:
468 PortSC &= 0xfff5;
469 PortSC |= USBPORTSC_PED;
470 break;
471
472 default:
473 gBS->RestoreTPL (OldTpl);
474 return EFI_INVALID_PARAMETER;
475 }
476
477 UhciWriteReg (Uhc->PciIo, Offset, PortSC);
478 gBS->RestoreTPL (OldTpl);
479
480 return EFI_SUCCESS;
481 }
482
483
484 /**
485 Clears a feature for the specified root hub port according to Uefi 2.0 spec.
486
487 @param This A pointer to the EFI_USB2_HC_PROTOCOL instance.
488 @param PortNumber Specifies the root hub port whose feature is
489 requested to be cleared.
490 @param PortFeature Indicates the feature selector associated with the
491 feature clear request.
492
493 @return EFI_SUCCESS PortFeature was cleared for the USB root hub port.
494 @return EFI_INVALID_PARAMETER PortNumber is invalid or PortFeature is invalid.
495 @return EFI_DEVICE_ERROR Can't read register.
496
497 **/
498 EFI_STATUS
499 EFIAPI
500 Uhci2ClearRootHubPortFeature (
501 IN EFI_USB2_HC_PROTOCOL *This,
502 IN UINT8 PortNumber,
503 IN EFI_USB_PORT_FEATURE PortFeature
504 )
505 {
506 USB_HC_DEV *Uhc;
507 EFI_TPL OldTpl;
508 UINT32 Offset;
509 UINT16 PortSC;
510
511 Uhc = UHC_FROM_USB2_HC_PROTO (This);
512
513 if (PortNumber >= Uhc->RootPorts) {
514 return EFI_INVALID_PARAMETER;
515 }
516
517 Offset = USBPORTSC_OFFSET + PortNumber * 2;
518
519 OldTpl = gBS->RaiseTPL (UHCI_TPL);
520 PortSC = UhciReadReg (Uhc->PciIo, Offset);
521
522 switch (PortFeature) {
523 case EfiUsbPortEnable:
524 PortSC &= 0xfff5;
525 PortSC &= ~USBPORTSC_PED;
526 break;
527
528 case EfiUsbPortSuspend:
529 //
530 // Cause a resume on the specified port if in suspend mode.
531 //
532 PortSC &= 0xfff5;
533 PortSC &= ~USBPORTSC_SUSP;
534 break;
535
536 case EfiUsbPortPower:
537 //
538 // No action
539 //
540 break;
541
542 case EfiUsbPortReset:
543 PortSC &= 0xfff5;
544 PortSC &= ~USBPORTSC_PR;
545 break;
546
547 case EfiUsbPortConnectChange:
548 PortSC &= 0xfff5;
549 PortSC |= USBPORTSC_CSC;
550 break;
551
552 case EfiUsbPortEnableChange:
553 PortSC &= 0xfff5;
554 PortSC |= USBPORTSC_PEDC;
555 break;
556
557 case EfiUsbPortSuspendChange:
558 //
559 // Root hub does not support this
560 //
561 break;
562
563 case EfiUsbPortOverCurrentChange:
564 //
565 // Root hub does not support this
566 //
567 break;
568
569 case EfiUsbPortResetChange:
570 //
571 // Root hub does not support this
572 //
573 break;
574
575 default:
576 gBS->RestoreTPL (OldTpl);
577 return EFI_INVALID_PARAMETER;
578 }
579
580 UhciWriteReg (Uhc->PciIo, Offset, PortSC);
581 gBS->RestoreTPL (OldTpl);
582
583 return EFI_SUCCESS;
584 }
585
586
587 /**
588 Submits control transfer to a target USB device accroding to UEFI 2.0 spec.
589
590 @param This A pointer to the EFI_USB2_HC_PROTOCOL instance.
591 @param DeviceAddress Target device address.
592 @param DeviceSpeed Device speed.
593 @param MaximumPacketLength Maximum packet size of the target endpoint.
594 @param Request USB device request to send.
595 @param TransferDirection Data direction of the Data stage in control transfer.
596 @param Data Data to transmit/receive in data stage.
597 @param DataLength Length of the data.
598 @param TimeOut Maximum time, in microseconds, for transfer to complete.
599 @param Translator Transaction translator to be used by this device.
600 @param TransferResult Variable to receive the transfer result.
601
602 @return EFI_SUCCESS The control transfer was completed successfully.
603 @return EFI_OUT_OF_RESOURCES Failed due to lack of resource.
604 @return EFI_INVALID_PARAMETER Some parameters are invalid.
605 @return EFI_TIMEOUT Failed due to timeout.
606 @return EFI_DEVICE_ERROR Failed due to host controller or device error.
607
608 **/
609 EFI_STATUS
610 EFIAPI
611 Uhci2ControlTransfer (
612 IN EFI_USB2_HC_PROTOCOL *This,
613 IN UINT8 DeviceAddress,
614 IN UINT8 DeviceSpeed,
615 IN UINTN MaximumPacketLength,
616 IN EFI_USB_DEVICE_REQUEST *Request,
617 IN EFI_USB_DATA_DIRECTION TransferDirection,
618 IN OUT VOID *Data,
619 IN OUT UINTN *DataLength,
620 IN UINTN TimeOut,
621 IN EFI_USB2_HC_TRANSACTION_TRANSLATOR *Translator,
622 OUT UINT32 *TransferResult
623 )
624 {
625 USB_HC_DEV *Uhc;
626 UHCI_TD_SW *TDs;
627 EFI_TPL OldTpl;
628 EFI_STATUS Status;
629 UHCI_QH_RESULT QhResult;
630 UINT8 PktId;
631 UINT8 *RequestPhy;
632 VOID *RequestMap;
633 UINT8 *DataPhy;
634 VOID *DataMap;
635 BOOLEAN IsSlowDevice;
636 UINTN TransferDataLength;
637
638 Uhc = UHC_FROM_USB2_HC_PROTO (This);
639 TDs = NULL;
640 DataPhy = NULL;
641 DataMap = NULL;
642 RequestPhy = NULL;
643 RequestMap = NULL;
644
645 IsSlowDevice = (BOOLEAN) ((EFI_USB_SPEED_LOW == DeviceSpeed) ? TRUE : FALSE);
646
647 //
648 // Parameters Checking
649 //
650 if (Request == NULL || TransferResult == NULL) {
651 return EFI_INVALID_PARAMETER;
652 }
653
654 if (IsSlowDevice && (MaximumPacketLength != 8)) {
655 return EFI_INVALID_PARAMETER;
656 }
657
658 if ((MaximumPacketLength != 8) && (MaximumPacketLength != 16) &&
659 (MaximumPacketLength != 32) && (MaximumPacketLength != 64)) {
660
661 return EFI_INVALID_PARAMETER;
662 }
663
664 if ((TransferDirection != EfiUsbNoData) && (Data == NULL || DataLength == NULL)) {
665 return EFI_INVALID_PARAMETER;
666 }
667
668 if (TransferDirection == EfiUsbNoData) {
669 TransferDataLength = 0;
670 } else {
671 TransferDataLength = *DataLength;
672 }
673
674 *TransferResult = EFI_USB_ERR_SYSTEM;
675 Status = EFI_DEVICE_ERROR;
676
677 //
678 // If errors exist that cause host controller halt,
679 // clear status then return EFI_DEVICE_ERROR.
680 //
681 UhciAckAllInterrupt (Uhc);
682
683 if (!UhciIsHcWorking (Uhc->PciIo)) {
684 return EFI_DEVICE_ERROR;
685 }
686
687 OldTpl = gBS->RaiseTPL (UHCI_TPL);
688
689 //
690 // Map the Request and data for bus master access,
691 // then create a list of TD for this transfer
692 //
693 Status = UhciMapUserRequest (Uhc, Request, &RequestPhy, &RequestMap);
694
695 if (EFI_ERROR (Status)) {
696 goto ON_EXIT;
697 }
698
699 Status = UhciMapUserData (Uhc, TransferDirection, Data, DataLength, &PktId, &DataPhy, &DataMap);
700
701 if (EFI_ERROR (Status)) {
702 Uhc->PciIo->Unmap (Uhc->PciIo, RequestMap);
703 goto ON_EXIT;
704 }
705
706 TDs = UhciCreateCtrlTds (
707 Uhc,
708 DeviceAddress,
709 PktId,
710 RequestPhy,
711 DataPhy,
712 TransferDataLength,
713 (UINT8) MaximumPacketLength,
714 IsSlowDevice
715 );
716
717 if (TDs == NULL) {
718 Status = EFI_OUT_OF_RESOURCES;
719 goto UNMAP_DATA;
720 }
721
722 //
723 // According to the speed of the end point, link
724 // the TD to corrosponding queue head, then check
725 // the execution result
726 //
727 UhciLinkTdToQh (Uhc->CtrlQh, TDs);
728 Status = UhciExecuteTransfer (Uhc, Uhc->CtrlQh, TDs, TimeOut, IsSlowDevice, &QhResult);
729 UhciUnlinkTdFromQh (Uhc->CtrlQh, TDs);
730
731 Uhc->PciIo->Flush (Uhc->PciIo);
732
733 *TransferResult = QhResult.Result;
734
735 if (DataLength != NULL) {
736 *DataLength = QhResult.Complete;
737 }
738
739 UhciDestoryTds (Uhc, TDs);
740
741 UNMAP_DATA:
742 Uhc->PciIo->Unmap (Uhc->PciIo, DataMap);
743 Uhc->PciIo->Unmap (Uhc->PciIo, RequestMap);
744
745 ON_EXIT:
746 gBS->RestoreTPL (OldTpl);
747 return Status;
748 }
749
750
751 /**
752 Submits bulk transfer to a bulk endpoint of a USB device.
753
754 @param This A pointer to the EFI_USB2_HC_PROTOCOL instance.
755 @param DeviceAddress Target device address.
756 @param EndPointAddress Endpoint number and direction.
757 @param DeviceSpeed Device speed.
758 @param MaximumPacketLength Maximum packet size of the target endpoint.
759 @param DataBuffersNumber Number of data buffers prepared for the transfer.
760 @param Data Array of pointers to the buffers of data.
761 @param DataLength On input, size of the data buffer, On output,
762 actually transferred data size.
763 @param DataToggle On input, data toggle to use; On output, next data toggle.
764 @param TimeOut Maximum time out, in microseconds.
765 @param Translator A pointr to the transaction translator data.
766 @param TransferResult Variable to receive transfer result.
767
768 @return EFI_SUCCESS The bulk transfer was completed successfully.
769 @return EFI_OUT_OF_RESOURCES Failed due to lack of resource.
770 @return EFI_INVALID_PARAMETER Some parameters are invalid.
771 @return EFI_TIMEOUT Failed due to timeout.
772 @return EFI_DEVICE_ERROR Failed due to host controller or device error.
773
774 **/
775 EFI_STATUS
776 EFIAPI
777 Uhci2BulkTransfer (
778 IN EFI_USB2_HC_PROTOCOL *This,
779 IN UINT8 DeviceAddress,
780 IN UINT8 EndPointAddress,
781 IN UINT8 DeviceSpeed,
782 IN UINTN MaximumPacketLength,
783 IN UINT8 DataBuffersNumber,
784 IN OUT VOID *Data[EFI_USB_MAX_BULK_BUFFER_NUM],
785 IN OUT UINTN *DataLength,
786 IN OUT UINT8 *DataToggle,
787 IN UINTN TimeOut,
788 IN EFI_USB2_HC_TRANSACTION_TRANSLATOR *Translator,
789 OUT UINT32 *TransferResult
790 )
791 {
792 EFI_USB_DATA_DIRECTION Direction;
793 EFI_TPL OldTpl;
794 USB_HC_DEV *Uhc;
795 UHCI_TD_SW *TDs;
796 UHCI_QH_SW *BulkQh;
797 UHCI_QH_RESULT QhResult;
798 EFI_STATUS Status;
799 UINT8 PktId;
800 UINT8 *DataPhy;
801 VOID *DataMap;
802
803 Uhc = UHC_FROM_USB2_HC_PROTO (This);
804 DataPhy = NULL;
805 DataMap = NULL;
806
807 if (DeviceSpeed == EFI_USB_SPEED_LOW) {
808 return EFI_INVALID_PARAMETER;
809 }
810
811 if ((DataLength == NULL) || (*DataLength == 0) || (Data == NULL) || (TransferResult == NULL)) {
812 return EFI_INVALID_PARAMETER;
813 }
814
815 if ((*DataToggle != 1) && (*DataToggle != 0)) {
816 return EFI_INVALID_PARAMETER;
817 }
818
819 if ((MaximumPacketLength != 8) && (MaximumPacketLength != 16) &&
820 (MaximumPacketLength != 32) && (MaximumPacketLength != 64)) {
821 return EFI_INVALID_PARAMETER;
822 }
823
824 *TransferResult = EFI_USB_ERR_SYSTEM;
825 Status = EFI_OUT_OF_RESOURCES;
826
827 //
828 // If has errors that cause host controller halt,
829 // then return EFI_DEVICE_ERROR directly.
830 //
831 UhciAckAllInterrupt (Uhc);
832
833 if (!UhciIsHcWorking (Uhc->PciIo)) {
834 return EFI_DEVICE_ERROR;
835 }
836
837 OldTpl = gBS->RaiseTPL (UHCI_TPL);
838
839 //
840 // Map the source data buffer for bus master access,
841 // then create a list of TDs
842 //
843 if ((EndPointAddress & 0x80) != 0) {
844 Direction = EfiUsbDataIn;
845 } else {
846 Direction = EfiUsbDataOut;
847 }
848
849 Status = UhciMapUserData (Uhc, Direction, *Data, DataLength, &PktId, &DataPhy, &DataMap);
850
851 if (EFI_ERROR (Status)) {
852 goto ON_EXIT;
853 }
854
855 Status = EFI_OUT_OF_RESOURCES;
856 TDs = UhciCreateBulkOrIntTds (
857 Uhc,
858 DeviceAddress,
859 EndPointAddress,
860 PktId,
861 DataPhy,
862 *DataLength,
863 DataToggle,
864 (UINT8) MaximumPacketLength,
865 FALSE
866 );
867
868 if (TDs == NULL) {
869 Uhc->PciIo->Unmap (Uhc->PciIo, DataMap);
870 goto ON_EXIT;
871 }
872
873
874 //
875 // Link the TDs to bulk queue head. According to the platfore
876 // defintion of UHCI_NO_BW_RECLAMATION, BulkQh is either configured
877 // to do full speed bandwidth reclamation or not.
878 //
879 BulkQh = Uhc->BulkQh;
880
881 UhciLinkTdToQh (BulkQh, TDs);
882 Status = UhciExecuteTransfer (Uhc, BulkQh, TDs, TimeOut, FALSE, &QhResult);
883 UhciUnlinkTdFromQh (BulkQh, TDs);
884
885 Uhc->PciIo->Flush (Uhc->PciIo);
886
887 *TransferResult = QhResult.Result;
888 *DataToggle = QhResult.NextToggle;
889 *DataLength = QhResult.Complete;
890
891 UhciDestoryTds (Uhc, TDs);
892 Uhc->PciIo->Unmap (Uhc->PciIo, DataMap);
893
894 ON_EXIT:
895 gBS->RestoreTPL (OldTpl);
896 return Status;
897 }
898
899
900 /**
901 Submits an asynchronous interrupt transfer to an
902 interrupt endpoint of a USB device according to UEFI 2.0 spec.
903
904 @param This A pointer to the EFI_USB2_HC_PROTOCOL instance.
905 @param DeviceAddress Target device address.
906 @param EndPointAddress Endpoint number and direction.
907 @param DeviceSpeed Device speed.
908 @param MaximumPacketLength Maximum packet size of the target endpoint.
909 @param IsNewTransfer If TRUE, submit a new transfer, if FALSE cancel old transfer.
910 @param DataToggle On input, data toggle to use; On output, next data toggle.
911 @param PollingInterval Interrupt poll rate in milliseconds.
912 @param DataLength On input, size of the data buffer, On output,
913 actually transferred data size.
914 @param Translator A pointr to the transaction translator data.
915 @param CallBackFunction Function to call periodically.
916 @param Context User context.
917
918 @return EFI_SUCCESS Transfer was submitted.
919 @return EFI_INVALID_PARAMETER Some parameters are invalid.
920 @return EFI_OUT_OF_RESOURCES Failed due to a lack of resources.
921 @return EFI_DEVICE_ERROR Can't read register.
922
923 **/
924 EFI_STATUS
925 EFIAPI
926 Uhci2AsyncInterruptTransfer (
927 IN EFI_USB2_HC_PROTOCOL *This,
928 IN UINT8 DeviceAddress,
929 IN UINT8 EndPointAddress,
930 IN UINT8 DeviceSpeed,
931 IN UINTN MaximumPacketLength,
932 IN BOOLEAN IsNewTransfer,
933 IN OUT UINT8 *DataToggle,
934 IN UINTN PollingInterval,
935 IN UINTN DataLength,
936 IN EFI_USB2_HC_TRANSACTION_TRANSLATOR *Translator,
937 IN EFI_ASYNC_USB_TRANSFER_CALLBACK CallBackFunction,
938 IN VOID *Context
939 )
940 {
941 USB_HC_DEV *Uhc;
942 BOOLEAN IsSlowDevice;
943 UHCI_QH_SW *Qh;
944 UHCI_TD_SW *IntTds;
945 EFI_TPL OldTpl;
946 EFI_STATUS Status;
947 UINT8 *DataPtr;
948 UINT8 *DataPhy;
949 VOID *DataMap;
950 UINT8 PktId;
951
952 Uhc = UHC_FROM_USB2_HC_PROTO (This);
953 Qh = NULL;
954 IntTds = NULL;
955 DataPtr = NULL;
956 DataPhy = NULL;
957 DataMap = NULL;
958
959 IsSlowDevice = (BOOLEAN) ((EFI_USB_SPEED_LOW == DeviceSpeed) ? TRUE : FALSE);
960
961 if ((EndPointAddress & 0x80) == 0) {
962 return EFI_INVALID_PARAMETER;
963 }
964
965 //
966 // Delete Async interrupt transfer request
967 //
968 if (!IsNewTransfer) {
969 OldTpl = gBS->RaiseTPL (UHCI_TPL);
970 Status = UhciRemoveAsyncReq (Uhc, DeviceAddress, EndPointAddress, DataToggle);
971
972 gBS->RestoreTPL (OldTpl);
973 return Status;
974 }
975
976 if (PollingInterval < 1 || PollingInterval > 255) {
977 return EFI_INVALID_PARAMETER;
978 }
979
980 if (DataLength == 0) {
981 return EFI_INVALID_PARAMETER;
982 }
983
984 if ((*DataToggle != 1) && (*DataToggle != 0)) {
985 return EFI_INVALID_PARAMETER;
986 }
987
988 //
989 // If has errors that cause host controller halt,
990 // then return EFI_DEVICE_ERROR directly.
991 //
992 UhciAckAllInterrupt (Uhc);
993
994 if (!UhciIsHcWorking (Uhc->PciIo)) {
995 return EFI_DEVICE_ERROR;
996 }
997
998 //
999 // Allocate and map source data buffer for bus master access.
1000 //
1001 DataPtr = AllocatePool (DataLength);
1002
1003 if (DataPtr == NULL) {
1004 return EFI_OUT_OF_RESOURCES;
1005 }
1006
1007 OldTpl = gBS->RaiseTPL (UHCI_TPL);
1008
1009 //
1010 // Map the user data then create a queue head and
1011 // list of TD for it.
1012 //
1013 Status = UhciMapUserData (
1014 Uhc,
1015 EfiUsbDataIn,
1016 DataPtr,
1017 &DataLength,
1018 &PktId,
1019 &DataPhy,
1020 &DataMap
1021 );
1022
1023 if (EFI_ERROR (Status)) {
1024 goto FREE_DATA;
1025 }
1026
1027 Qh = UhciCreateQh (Uhc, PollingInterval);
1028
1029 if (Qh == NULL) {
1030 Status = EFI_OUT_OF_RESOURCES;
1031 goto UNMAP_DATA;
1032 }
1033
1034 IntTds = UhciCreateBulkOrIntTds (
1035 Uhc,
1036 DeviceAddress,
1037 EndPointAddress,
1038 PktId,
1039 DataPhy,
1040 DataLength,
1041 DataToggle,
1042 (UINT8) MaximumPacketLength,
1043 IsSlowDevice
1044 );
1045
1046 if (IntTds == NULL) {
1047 Status = EFI_OUT_OF_RESOURCES;
1048 goto DESTORY_QH;
1049 }
1050
1051 UhciLinkTdToQh (Qh, IntTds);
1052
1053 //
1054 // Save QH-TD structures to async Interrupt transfer list,
1055 // for monitor interrupt transfer execution routine use.
1056 //
1057 Status = UhciCreateAsyncReq (
1058 Uhc,
1059 Qh,
1060 IntTds,
1061 DeviceAddress,
1062 EndPointAddress,
1063 DataLength,
1064 PollingInterval,
1065 DataMap,
1066 DataPtr,
1067 CallBackFunction,
1068 Context,
1069 IsSlowDevice
1070 );
1071
1072 if (EFI_ERROR (Status)) {
1073 goto DESTORY_QH;
1074 }
1075
1076 UhciLinkQhToFrameList (Uhc->FrameBase, Qh);
1077
1078 gBS->RestoreTPL (OldTpl);
1079 return EFI_SUCCESS;
1080
1081 DESTORY_QH:
1082 UsbHcFreeMem (Uhc->MemPool, Qh, sizeof (UHCI_QH_SW));
1083
1084 UNMAP_DATA:
1085 Uhc->PciIo->Unmap (Uhc->PciIo, DataMap);
1086
1087 FREE_DATA:
1088 gBS->FreePool (DataPtr);
1089 Uhc->PciIo->Flush (Uhc->PciIo);
1090
1091 gBS->RestoreTPL (OldTpl);
1092 return Status;
1093 }
1094
1095 /**
1096 Submits synchronous interrupt transfer to an interrupt endpoint
1097 of a USB device according to UEFI 2.0 spec.
1098
1099
1100 @param This A pointer to the EFI_USB2_HC_PROTOCOL instance.
1101 @param DeviceAddress Target device address.
1102 @param EndPointAddress Endpoint number and direction.
1103 @param DeviceSpeed Device speed.
1104 @param MaximumPacketLength Maximum packet size of the target endpoint.
1105 @param Data Array of pointers to the buffers of data.
1106 @param DataLength On input, size of the data buffer, On output,
1107 actually transferred data size.
1108 @param DataToggle On input, data toggle to use; On output, next data toggle.
1109 @param TimeOut Maximum time out, in microseconds.
1110 @param Translator A pointr to the transaction translator data.
1111 @param TransferResult Variable to receive transfer result.
1112
1113 @return EFI_SUCCESS The transfer was completed successfully.
1114 @return EFI_OUT_OF_RESOURCES Failed due to lack of resource.
1115 @return EFI_INVALID_PARAMETER Some parameters are invalid.
1116 @return EFI_TIMEOUT Failed due to timeout.
1117 @return EFI_DEVICE_ERROR Failed due to host controller or device error.
1118
1119 **/
1120 EFI_STATUS
1121 EFIAPI
1122 Uhci2SyncInterruptTransfer (
1123 IN EFI_USB2_HC_PROTOCOL *This,
1124 IN UINT8 DeviceAddress,
1125 IN UINT8 EndPointAddress,
1126 IN UINT8 DeviceSpeed,
1127 IN UINTN MaximumPacketLength,
1128 IN OUT VOID *Data,
1129 IN OUT UINTN *DataLength,
1130 IN OUT UINT8 *DataToggle,
1131 IN UINTN TimeOut,
1132 IN EFI_USB2_HC_TRANSACTION_TRANSLATOR *Translator,
1133 OUT UINT32 *TransferResult
1134 )
1135 {
1136 EFI_STATUS Status;
1137 USB_HC_DEV *Uhc;
1138 UHCI_TD_SW *TDs;
1139 UHCI_QH_RESULT QhResult;
1140 EFI_TPL OldTpl;
1141 UINT8 *DataPhy;
1142 VOID *DataMap;
1143 UINT8 PktId;
1144 BOOLEAN IsSlowDevice;
1145
1146 Uhc = UHC_FROM_USB2_HC_PROTO (This);
1147 DataPhy = NULL;
1148 DataMap = NULL;
1149 TDs = NULL;
1150
1151 if (DeviceSpeed == EFI_USB_SPEED_HIGH) {
1152 return EFI_INVALID_PARAMETER;
1153 }
1154
1155 IsSlowDevice = (BOOLEAN) ((EFI_USB_SPEED_LOW == DeviceSpeed) ? TRUE : FALSE);
1156
1157 if ((DataLength == NULL) || (Data == NULL) || (TransferResult == NULL)) {
1158 return EFI_INVALID_PARAMETER;
1159 }
1160
1161 if ((EndPointAddress & 0x80) == 0) {
1162 return EFI_INVALID_PARAMETER;
1163 }
1164
1165 if ((*DataToggle != 1) && (*DataToggle != 0)) {
1166 return EFI_INVALID_PARAMETER;
1167 }
1168
1169 if ((*DataLength == 0) || (MaximumPacketLength > 64)) {
1170 return EFI_INVALID_PARAMETER;
1171 }
1172
1173 if (IsSlowDevice && (MaximumPacketLength > 8)) {
1174 return EFI_INVALID_PARAMETER;
1175 }
1176
1177 *TransferResult = EFI_USB_ERR_SYSTEM;
1178 Status = EFI_DEVICE_ERROR;
1179
1180
1181 UhciAckAllInterrupt (Uhc);
1182
1183 if (!UhciIsHcWorking (Uhc->PciIo)) {
1184 return Status;
1185 }
1186
1187 OldTpl = gBS->RaiseTPL (UHCI_TPL);
1188
1189 //
1190 // Map the source data buffer for bus master access.
1191 // Create Tds list, then link it to the UHC's interrupt list
1192 //
1193 Status = UhciMapUserData (
1194 Uhc,
1195 EfiUsbDataIn,
1196 Data,
1197 DataLength,
1198 &PktId,
1199 &DataPhy,
1200 &DataMap
1201 );
1202
1203 if (EFI_ERROR (Status)) {
1204 goto ON_EXIT;
1205 }
1206
1207 TDs = UhciCreateBulkOrIntTds (
1208 Uhc,
1209 DeviceAddress,
1210 EndPointAddress,
1211 PktId,
1212 DataPhy,
1213 *DataLength,
1214 DataToggle,
1215 (UINT8) MaximumPacketLength,
1216 IsSlowDevice
1217 );
1218
1219 if (TDs == NULL) {
1220 Uhc->PciIo->Unmap (Uhc->PciIo, DataMap);
1221
1222 Status = EFI_OUT_OF_RESOURCES;
1223 goto ON_EXIT;
1224 }
1225
1226
1227 UhciLinkTdToQh (Uhc->SyncIntQh, TDs);
1228
1229 Status = UhciExecuteTransfer (Uhc, Uhc->SyncIntQh, TDs, TimeOut, IsSlowDevice, &QhResult);
1230
1231 UhciUnlinkTdFromQh (Uhc->SyncIntQh, TDs);
1232 Uhc->PciIo->Flush (Uhc->PciIo);
1233
1234 *TransferResult = QhResult.Result;
1235 *DataToggle = QhResult.NextToggle;
1236 *DataLength = QhResult.Complete;
1237
1238 UhciDestoryTds (Uhc, TDs);
1239 Uhc->PciIo->Unmap (Uhc->PciIo, DataMap);
1240
1241 ON_EXIT:
1242 gBS->RestoreTPL (OldTpl);
1243 return Status;
1244 }
1245
1246
1247 /**
1248 Submits isochronous transfer to a target USB device according to UEFI 2.0 spec.
1249
1250 @param This A pointer to the EFI_USB2_HC_PROTOCOL instance.
1251 @param DeviceAddress Target device address.
1252 @param EndPointAddress Endpoint number and direction.
1253 @param DeviceSpeed Device speed.
1254 @param MaximumPacketLength Maximum packet size of the target endpoint.
1255 @param DataBuffersNumber Number of data buffers prepared for the transfer.
1256 @param Data Array of pointers to the buffers of data.
1257 @param DataLength On input, size of the data buffer, On output,
1258 actually transferred data size.
1259 @param Translator A pointr to the transaction translator data.
1260 @param TransferResult Variable to receive transfer result.
1261
1262 @return EFI_UNSUPPORTED
1263
1264 **/
1265 EFI_STATUS
1266 EFIAPI
1267 Uhci2IsochronousTransfer (
1268 IN EFI_USB2_HC_PROTOCOL *This,
1269 IN UINT8 DeviceAddress,
1270 IN UINT8 EndPointAddress,
1271 IN UINT8 DeviceSpeed,
1272 IN UINTN MaximumPacketLength,
1273 IN UINT8 DataBuffersNumber,
1274 IN OUT VOID *Data[EFI_USB_MAX_ISO_BUFFER_NUM],
1275 IN UINTN DataLength,
1276 IN EFI_USB2_HC_TRANSACTION_TRANSLATOR *Translator,
1277 OUT UINT32 *TransferResult
1278 )
1279 {
1280 return EFI_UNSUPPORTED;
1281 }
1282
1283
1284 /**
1285 Submits Async isochronous transfer to a target USB device according to UEFI 2.0 spec.
1286
1287 @param This A pointer to the EFI_USB2_HC_PROTOCOL instance.
1288 @param DeviceAddress Target device address.
1289 @param EndPointAddress Endpoint number and direction.
1290 @param DeviceSpeed Device speed.
1291 @param MaximumPacketLength Maximum packet size of the target endpoint.
1292 @param DataBuffersNumber Number of data buffers prepared for the transfer.
1293 @param Data Array of pointers to the buffers of data.
1294 @param DataLength On input, size of the data buffer, On output,
1295 actually transferred data size.
1296 @param Translator A pointr to the transaction translator data.
1297 @param IsochronousCallBack Function to call when the transfer complete.
1298 @param Context Pass to the call back function as parameter.
1299
1300 @return EFI_UNSUPPORTED
1301
1302 **/
1303 EFI_STATUS
1304 EFIAPI
1305 Uhci2AsyncIsochronousTransfer (
1306 IN EFI_USB2_HC_PROTOCOL *This,
1307 IN UINT8 DeviceAddress,
1308 IN UINT8 EndPointAddress,
1309 IN UINT8 DeviceSpeed,
1310 IN UINTN MaximumPacketLength,
1311 IN UINT8 DataBuffersNumber,
1312 IN OUT VOID *Data[EFI_USB_MAX_ISO_BUFFER_NUM],
1313 IN UINTN DataLength,
1314 IN EFI_USB2_HC_TRANSACTION_TRANSLATOR *Translator,
1315 IN EFI_ASYNC_USB_TRANSFER_CALLBACK IsochronousCallBack,
1316 IN VOID *Context
1317 )
1318 {
1319 return EFI_UNSUPPORTED;
1320 }
1321
1322 /**
1323 Entry point for EFI drivers.
1324
1325 @param ImageHandle EFI_HANDLE.
1326 @param SystemTable EFI_SYSTEM_TABLE.
1327
1328 @retval EFI_SUCCESS Driver is successfully loaded.
1329 @return Others Failed.
1330
1331 **/
1332 EFI_STATUS
1333 EFIAPI
1334 UhciDriverEntryPoint (
1335 IN EFI_HANDLE ImageHandle,
1336 IN EFI_SYSTEM_TABLE *SystemTable
1337 )
1338 {
1339 return EfiLibInstallDriverBindingComponentName2 (
1340 ImageHandle,
1341 SystemTable,
1342 &gUhciDriverBinding,
1343 ImageHandle,
1344 &gUhciComponentName,
1345 &gUhciComponentName2
1346 );
1347 }
1348
1349
1350 /**
1351 Test to see if this driver supports ControllerHandle. Any
1352 ControllerHandle that has UsbHcProtocol installed will be supported.
1353
1354 @param This Protocol instance pointer.
1355 @param Controller Handle of device to test.
1356 @param RemainingDevicePath Not used.
1357
1358 @return EFI_SUCCESS This driver supports this device.
1359 @return EFI_UNSUPPORTED This driver does not support this device.
1360
1361 **/
1362 EFI_STATUS
1363 EFIAPI
1364 UhciDriverBindingSupported (
1365 IN EFI_DRIVER_BINDING_PROTOCOL *This,
1366 IN EFI_HANDLE Controller,
1367 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
1368 )
1369 {
1370 EFI_STATUS OpenStatus;
1371 EFI_STATUS Status;
1372 EFI_PCI_IO_PROTOCOL *PciIo;
1373 USB_CLASSC UsbClassCReg;
1374
1375 //
1376 // Test whether there is PCI IO Protocol attached on the controller handle.
1377 //
1378 OpenStatus = gBS->OpenProtocol (
1379 Controller,
1380 &gEfiPciIoProtocolGuid,
1381 (VOID **) &PciIo,
1382 This->DriverBindingHandle,
1383 Controller,
1384 EFI_OPEN_PROTOCOL_BY_DRIVER
1385 );
1386
1387 if (EFI_ERROR (OpenStatus)) {
1388 return OpenStatus;
1389 }
1390
1391 Status = PciIo->Pci.Read (
1392 PciIo,
1393 EfiPciIoWidthUint8,
1394 CLASSC_OFFSET,
1395 sizeof (USB_CLASSC) / sizeof (UINT8),
1396 &UsbClassCReg
1397 );
1398
1399 if (EFI_ERROR (Status)) {
1400 Status = EFI_UNSUPPORTED;
1401 goto ON_EXIT;
1402 }
1403
1404 //
1405 // Test whether the controller belongs to UHCI type
1406 //
1407 if ((UsbClassCReg.BaseCode != PCI_CLASS_SERIAL) ||
1408 (UsbClassCReg.SubClassCode != PCI_CLASS_SERIAL_USB) ||
1409 (UsbClassCReg.PI != PCI_CLASSC_PI_UHCI)
1410 ) {
1411
1412 Status = EFI_UNSUPPORTED;
1413 }
1414
1415 ON_EXIT:
1416 gBS->CloseProtocol (
1417 Controller,
1418 &gEfiPciIoProtocolGuid,
1419 This->DriverBindingHandle,
1420 Controller
1421 );
1422
1423 return Status;
1424
1425 }
1426
1427
1428 /**
1429 Allocate and initialize the empty UHCI device.
1430
1431 @param PciIo The PCIIO to use.
1432 @param OriginalPciAttributes The original PCI attributes.
1433
1434 @return Allocated UHCI device. If err, return NULL.
1435
1436 **/
1437 USB_HC_DEV *
1438 UhciAllocateDev (
1439 IN EFI_PCI_IO_PROTOCOL *PciIo,
1440 IN UINT64 OriginalPciAttributes
1441 )
1442 {
1443 USB_HC_DEV *Uhc;
1444 EFI_STATUS Status;
1445
1446 Uhc = AllocateZeroPool (sizeof (USB_HC_DEV));
1447
1448 if (Uhc == NULL) {
1449 return NULL;
1450 }
1451
1452 //
1453 // This driver supports both USB_HC_PROTOCOL and USB2_HC_PROTOCOL.
1454 // USB_HC_PROTOCOL is for EFI 1.1 backward compability.
1455 //
1456 Uhc->Signature = USB_HC_DEV_SIGNATURE;
1457 Uhc->Usb2Hc.GetCapability = Uhci2GetCapability;
1458 Uhc->Usb2Hc.Reset = Uhci2Reset;
1459 Uhc->Usb2Hc.GetState = Uhci2GetState;
1460 Uhc->Usb2Hc.SetState = Uhci2SetState;
1461 Uhc->Usb2Hc.ControlTransfer = Uhci2ControlTransfer;
1462 Uhc->Usb2Hc.BulkTransfer = Uhci2BulkTransfer;
1463 Uhc->Usb2Hc.AsyncInterruptTransfer = Uhci2AsyncInterruptTransfer;
1464 Uhc->Usb2Hc.SyncInterruptTransfer = Uhci2SyncInterruptTransfer;
1465 Uhc->Usb2Hc.IsochronousTransfer = Uhci2IsochronousTransfer;
1466 Uhc->Usb2Hc.AsyncIsochronousTransfer = Uhci2AsyncIsochronousTransfer;
1467 Uhc->Usb2Hc.GetRootHubPortStatus = Uhci2GetRootHubPortStatus;
1468 Uhc->Usb2Hc.SetRootHubPortFeature = Uhci2SetRootHubPortFeature;
1469 Uhc->Usb2Hc.ClearRootHubPortFeature = Uhci2ClearRootHubPortFeature;
1470 Uhc->Usb2Hc.MajorRevision = 0x1;
1471 Uhc->Usb2Hc.MinorRevision = 0x1;
1472
1473 Uhc->PciIo = PciIo;
1474 Uhc->OriginalPciAttributes = OriginalPciAttributes;
1475 Uhc->MemPool = UsbHcInitMemPool (PciIo, TRUE, 0);
1476
1477 if (Uhc->MemPool == NULL) {
1478 Status = EFI_OUT_OF_RESOURCES;
1479 goto ON_ERROR;
1480 }
1481
1482 InitializeListHead (&Uhc->AsyncIntList);
1483
1484 Status = gBS->CreateEvent (
1485 EVT_TIMER | EVT_NOTIFY_SIGNAL,
1486 TPL_CALLBACK,
1487 UhciMonitorAsyncReqList,
1488 Uhc,
1489 &Uhc->AsyncIntMonitor
1490 );
1491
1492 if (EFI_ERROR (Status)) {
1493 UsbHcFreeMemPool (Uhc->MemPool);
1494 goto ON_ERROR;
1495 }
1496
1497 return Uhc;
1498
1499 ON_ERROR:
1500 FreePool (Uhc);
1501 return NULL;
1502 }
1503
1504
1505 /**
1506 Free the UHCI device and release its associated resources.
1507
1508 @param Uhc The UHCI device to release.
1509
1510 **/
1511 VOID
1512 UhciFreeDev (
1513 IN USB_HC_DEV *Uhc
1514 )
1515 {
1516 if (Uhc->AsyncIntMonitor != NULL) {
1517 gBS->CloseEvent (Uhc->AsyncIntMonitor);
1518 }
1519
1520 if (Uhc->MemPool != NULL) {
1521 UsbHcFreeMemPool (Uhc->MemPool);
1522 }
1523
1524 if (Uhc->CtrlNameTable != NULL) {
1525 FreeUnicodeStringTable (Uhc->CtrlNameTable);
1526 }
1527
1528 FreePool (Uhc);
1529 }
1530
1531
1532 /**
1533 Uninstall all Uhci Interface.
1534
1535 @param Controller Controller handle.
1536 @param This Protocol instance pointer.
1537
1538 **/
1539 VOID
1540 UhciCleanDevUp (
1541 IN EFI_HANDLE Controller,
1542 IN EFI_USB2_HC_PROTOCOL *This
1543 )
1544 {
1545 USB_HC_DEV *Uhc;
1546
1547 //
1548 // Uninstall the USB_HC and USB_HC2 protocol, then disable the controller
1549 //
1550 Uhc = UHC_FROM_USB2_HC_PROTO (This);
1551 UhciStopHc (Uhc, UHC_GENERIC_TIMEOUT);
1552
1553 gBS->UninstallProtocolInterface (
1554 Controller,
1555 &gEfiUsb2HcProtocolGuid,
1556 &Uhc->Usb2Hc
1557 );
1558
1559 UhciFreeAllAsyncReq (Uhc);
1560 UhciDestoryFrameList (Uhc);
1561
1562 //
1563 // Restore original PCI attributes
1564 //
1565 Uhc->PciIo->Attributes (
1566 Uhc->PciIo,
1567 EfiPciIoAttributeOperationSet,
1568 Uhc->OriginalPciAttributes,
1569 NULL
1570 );
1571
1572 UhciFreeDev (Uhc);
1573 }
1574
1575
1576 /**
1577 Starting the Usb UHCI Driver.
1578
1579 @param This Protocol instance pointer.
1580 @param Controller Handle of device to test.
1581 @param RemainingDevicePath Not used.
1582
1583 @retval EFI_SUCCESS This driver supports this device.
1584 @retval EFI_UNSUPPORTED This driver does not support this device.
1585 @retval EFI_DEVICE_ERROR This driver cannot be started due to device Error.
1586 EFI_OUT_OF_RESOURCES- Failed due to resource shortage.
1587
1588 **/
1589 EFI_STATUS
1590 EFIAPI
1591 UhciDriverBindingStart (
1592 IN EFI_DRIVER_BINDING_PROTOCOL *This,
1593 IN EFI_HANDLE Controller,
1594 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
1595 )
1596 {
1597 EFI_STATUS Status;
1598 EFI_PCI_IO_PROTOCOL *PciIo;
1599 USB_HC_DEV *Uhc;
1600 UINT64 Supports;
1601 UINT64 OriginalPciAttributes;
1602 BOOLEAN PciAttributesSaved;
1603
1604 //
1605 // Open PCIIO, then enable the EHC device and turn off emulation
1606 //
1607 Uhc = NULL;
1608 Status = gBS->OpenProtocol (
1609 Controller,
1610 &gEfiPciIoProtocolGuid,
1611 (VOID **) &PciIo,
1612 This->DriverBindingHandle,
1613 Controller,
1614 EFI_OPEN_PROTOCOL_BY_DRIVER
1615 );
1616
1617 if (EFI_ERROR (Status)) {
1618 return Status;
1619 }
1620
1621 PciAttributesSaved = FALSE;
1622 //
1623 // Save original PCI attributes
1624 //
1625 Status = PciIo->Attributes (
1626 PciIo,
1627 EfiPciIoAttributeOperationGet,
1628 0,
1629 &OriginalPciAttributes
1630 );
1631
1632 if (EFI_ERROR (Status)) {
1633 goto CLOSE_PCIIO;
1634 }
1635 PciAttributesSaved = TRUE;
1636
1637 //
1638 // Robustnesss improvement such as for UoL
1639 // Default is not required.
1640 //
1641 if (FeaturePcdGet (PcdTurnOffUsbLegacySupport)) {
1642 UhciTurnOffUsbEmulation (PciIo);
1643 }
1644
1645 Status = PciIo->Attributes (
1646 PciIo,
1647 EfiPciIoAttributeOperationSupported,
1648 0,
1649 &Supports
1650 );
1651 if (!EFI_ERROR (Status)) {
1652 Supports &= EFI_PCI_DEVICE_ENABLE;
1653 Status = PciIo->Attributes (
1654 PciIo,
1655 EfiPciIoAttributeOperationEnable,
1656 Supports,
1657 NULL
1658 );
1659 }
1660
1661 if (EFI_ERROR (Status)) {
1662 goto CLOSE_PCIIO;
1663 }
1664
1665 Uhc = UhciAllocateDev (PciIo, OriginalPciAttributes);
1666
1667 if (Uhc == NULL) {
1668 Status = EFI_OUT_OF_RESOURCES;
1669 goto CLOSE_PCIIO;
1670 }
1671
1672 //
1673 // Allocate and Init Host Controller's Frame List Entry
1674 //
1675 Status = UhciInitFrameList (Uhc);
1676
1677 if (EFI_ERROR (Status)) {
1678 Status = EFI_OUT_OF_RESOURCES;
1679 goto FREE_UHC;
1680 }
1681
1682 Status = gBS->SetTimer (
1683 Uhc->AsyncIntMonitor,
1684 TimerPeriodic,
1685 UHC_ASYNC_POLL_INTERVAL
1686 );
1687
1688 if (EFI_ERROR (Status)) {
1689 goto FREE_UHC;
1690 }
1691
1692 //
1693 // Install USB2_HC_PROTOCOL
1694 //
1695 Status = gBS->InstallMultipleProtocolInterfaces (
1696 &Controller,
1697 &gEfiUsb2HcProtocolGuid,
1698 &Uhc->Usb2Hc,
1699 NULL
1700 );
1701
1702 if (EFI_ERROR (Status)) {
1703 goto FREE_UHC;
1704 }
1705
1706 //
1707 // Install the component name protocol
1708 //
1709 Uhc->CtrlNameTable = NULL;
1710
1711 AddUnicodeString2 (
1712 "eng",
1713 gUhciComponentName.SupportedLanguages,
1714 &Uhc->CtrlNameTable,
1715 L"Usb Universal Host Controller",
1716 TRUE
1717 );
1718 AddUnicodeString2 (
1719 "en",
1720 gUhciComponentName2.SupportedLanguages,
1721 &Uhc->CtrlNameTable,
1722 L"Usb Universal Host Controller",
1723 FALSE
1724 );
1725
1726
1727 //
1728 // Start the UHCI hardware, also set its reclamation point to 64 bytes
1729 //
1730 UhciWriteReg (Uhc->PciIo, USBCMD_OFFSET, USBCMD_RS | USBCMD_MAXP);
1731
1732 return EFI_SUCCESS;
1733
1734 FREE_UHC:
1735 UhciFreeDev (Uhc);
1736
1737 CLOSE_PCIIO:
1738 if (PciAttributesSaved) {
1739 //
1740 // Restore original PCI attributes
1741 //
1742 PciIo->Attributes (
1743 PciIo,
1744 EfiPciIoAttributeOperationSet,
1745 OriginalPciAttributes,
1746 NULL
1747 );
1748 }
1749
1750 gBS->CloseProtocol (
1751 Controller,
1752 &gEfiPciIoProtocolGuid,
1753 This->DriverBindingHandle,
1754 Controller
1755 );
1756
1757 return Status;
1758 }
1759
1760
1761 /**
1762 Stop this driver on ControllerHandle. Support stoping any child handles
1763 created by this driver.
1764
1765 @param This Protocol instance pointer.
1766 @param Controller Handle of device to stop driver on.
1767 @param NumberOfChildren Number of Children in the ChildHandleBuffer.
1768 @param ChildHandleBuffer List of handles for the children we need to stop.
1769
1770 @return EFI_SUCCESS
1771 @return others
1772
1773 **/
1774 EFI_STATUS
1775 EFIAPI
1776 UhciDriverBindingStop (
1777 IN EFI_DRIVER_BINDING_PROTOCOL *This,
1778 IN EFI_HANDLE Controller,
1779 IN UINTN NumberOfChildren,
1780 IN EFI_HANDLE *ChildHandleBuffer
1781 )
1782 {
1783 EFI_USB2_HC_PROTOCOL *Usb2Hc;
1784 EFI_STATUS Status;
1785
1786 Status = gBS->OpenProtocol (
1787 Controller,
1788 &gEfiUsb2HcProtocolGuid,
1789 (VOID **) &Usb2Hc,
1790 This->DriverBindingHandle,
1791 Controller,
1792 EFI_OPEN_PROTOCOL_GET_PROTOCOL
1793 );
1794
1795 //
1796 // Test whether the Controller handler passed in is a valid
1797 // Usb controller handle that should be supported, if not,
1798 // return the error status directly
1799 //
1800 if (EFI_ERROR (Status)) {
1801 return Status;
1802 }
1803
1804 UhciCleanDevUp (Controller, Usb2Hc);
1805
1806 gBS->CloseProtocol (
1807 Controller,
1808 &gEfiPciIoProtocolGuid,
1809 This->DriverBindingHandle,
1810 Controller
1811 );
1812
1813 return EFI_SUCCESS;
1814 }
1815