2 USB Mouse Driver that manages USB mouse and produces Simple Pointer Protocol.
4 Copyright (c) 2004 - 2008, Intel Corporation
5 All rights reserved. This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
17 EFI_DRIVER_BINDING_PROTOCOL gUsbMouseDriverBinding
= {
18 USBMouseDriverBindingSupported
,
19 USBMouseDriverBindingStart
,
20 USBMouseDriverBindingStop
,
27 Entrypoint of USB Mouse Driver.
29 This function is the entrypoint of USB Mouse Driver. It installs Driver Binding
30 Protocols together with Component Name Protocols.
32 @param ImageHandle The firmware allocated handle for the EFI image.
33 @param SystemTable A pointer to the EFI System Table.
35 @retval EFI_SUCCESS The entry point is executed successfully.
40 USBMouseDriverBindingEntryPoint (
41 IN EFI_HANDLE ImageHandle
,
42 IN EFI_SYSTEM_TABLE
*SystemTable
47 Status
= EfiLibInstallDriverBindingComponentName2 (
50 &gUsbMouseDriverBinding
,
52 &gUsbMouseComponentName
,
53 &gUsbMouseComponentName2
55 ASSERT_EFI_ERROR (Status
);
62 Check whether USB mouse driver supports this device.
64 @param This The USB mouse driver binding protocol.
65 @param Controller The controller handle to check.
66 @param RemainingDevicePath The remaining device path.
68 @retval EFI_SUCCESS The driver supports this controller.
69 @retval other This device isn't supported.
74 USBMouseDriverBindingSupported (
75 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
76 IN EFI_HANDLE Controller
,
77 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
81 EFI_USB_IO_PROTOCOL
*UsbIo
;
83 Status
= gBS
->OpenProtocol (
85 &gEfiUsbIoProtocolGuid
,
87 This
->DriverBindingHandle
,
89 EFI_OPEN_PROTOCOL_BY_DRIVER
91 if (EFI_ERROR (Status
)) {
96 // Use the USB I/O Protocol interface to check whether Controller is
97 // a mouse device that can be managed by this driver.
100 if (!IsUsbMouse (UsbIo
)) {
101 Status
= EFI_UNSUPPORTED
;
106 &gEfiUsbIoProtocolGuid
,
107 This
->DriverBindingHandle
,
116 Starts the mouse device with this driver.
118 This function consumes USB I/O Portocol, intializes USB mouse device,
119 installs Simple Pointer Protocol, and submits Asynchronous Interrupt
120 Transfer to manage the USB mouse device.
122 @param This The USB mouse driver binding instance.
123 @param Controller Handle of device to bind driver to.
124 @param RemainingDevicePath Optional parameter use to pick a specific child
127 @retval EFI_SUCCESS This driver supports this device.
128 @retval EFI_UNSUPPORTED This driver does not support this device.
129 @retval EFI_DEVICE_ERROR This driver cannot be started due to device Error.
130 @retval EFI_OUT_OF_RESOURCES Can't allocate memory resources.
131 @retval EFI_ALREADY_STARTED This driver has been started.
136 USBMouseDriverBindingStart (
137 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
138 IN EFI_HANDLE Controller
,
139 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
143 EFI_USB_IO_PROTOCOL
*UsbIo
;
144 USB_MOUSE_DEV
*UsbMouseDevice
;
145 UINT8 EndpointNumber
;
146 EFI_USB_ENDPOINT_DESCRIPTOR EndpointDescriptor
;
149 UINT8 PollingInterval
;
154 // Open USB I/O Protocol
156 Status
= gBS
->OpenProtocol (
158 &gEfiUsbIoProtocolGuid
,
160 This
->DriverBindingHandle
,
162 EFI_OPEN_PROTOCOL_BY_DRIVER
164 if (EFI_ERROR (Status
)) {
168 UsbMouseDevice
= AllocateZeroPool (sizeof (USB_MOUSE_DEV
));
169 ASSERT (UsbMouseDevice
!= NULL
);
171 UsbMouseDevice
->UsbIo
= UsbIo
;
172 UsbMouseDevice
->Signature
= USB_MOUSE_DEV_SIGNATURE
;
175 // Get the Device Path Protocol on Controller's handle
177 Status
= gBS
->OpenProtocol (
179 &gEfiDevicePathProtocolGuid
,
180 (VOID
**) &UsbMouseDevice
->DevicePath
,
181 This
->DriverBindingHandle
,
183 EFI_OPEN_PROTOCOL_GET_PROTOCOL
186 if (EFI_ERROR (Status
)) {
190 // Get interface & endpoint descriptor
192 UsbIo
->UsbGetInterfaceDescriptor (
194 &UsbMouseDevice
->InterfaceDescriptor
197 EndpointNumber
= UsbMouseDevice
->InterfaceDescriptor
.NumEndpoints
;
200 // Traverse endpoints to find interrupt endpoint
203 for (Index
= 0; Index
< EndpointNumber
; Index
++) {
204 UsbIo
->UsbGetEndpointDescriptor (
210 if ((EndpointDescriptor
.Attributes
& (BIT0
| BIT1
)) == USB_ENDPOINT_INTERRUPT
) {
212 // We only care interrupt endpoint here
214 CopyMem(&UsbMouseDevice
->IntEndpointDescriptor
, &EndpointDescriptor
, sizeof(EndpointDescriptor
));
222 // No interrupt endpoint found, then return unsupported.
224 Status
= EFI_UNSUPPORTED
;
228 Status
= InitializeUsbMouseDevice (UsbMouseDevice
);
229 if (EFI_ERROR (Status
)) {
231 // Fail to initialize USB mouse device.
233 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
234 EFI_ERROR_CODE
| EFI_ERROR_MINOR
,
235 PcdGet32 (PcdStatusCodeValueMouseInterfaceError
),
236 UsbMouseDevice
->DevicePath
243 // Initialize and install EFI Simple Pointer Protocol.
245 UsbMouseDevice
->SimplePointerProtocol
.GetState
= GetMouseState
;
246 UsbMouseDevice
->SimplePointerProtocol
.Reset
= UsbMouseReset
;
247 UsbMouseDevice
->SimplePointerProtocol
.Mode
= &UsbMouseDevice
->Mode
;
249 Status
= gBS
->CreateEvent (
252 UsbMouseWaitForInput
,
254 &((UsbMouseDevice
->SimplePointerProtocol
).WaitForInput
)
256 if (EFI_ERROR (Status
)) {
260 Status
= gBS
->InstallProtocolInterface (
262 &gEfiSimplePointerProtocolGuid
,
263 EFI_NATIVE_INTERFACE
,
264 &UsbMouseDevice
->SimplePointerProtocol
267 if (EFI_ERROR (Status
)) {
272 // The next step would be submitting Asynchronous Interrupt Transfer on this mouse device.
273 // After that we will be able to get key data from it. Thus this is deemed as
274 // the enable action of the mouse, so report status code accordingly.
276 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
278 PcdGet32 (PcdStatusCodeValueMouseEnable
),
279 UsbMouseDevice
->DevicePath
283 // Submit Asynchronous Interrupt Transfer to manage this device.
285 EndpointAddr
= UsbMouseDevice
->IntEndpointDescriptor
.EndpointAddress
;
286 PollingInterval
= UsbMouseDevice
->IntEndpointDescriptor
.Interval
;
287 PacketSize
= (UINT8
) (UsbMouseDevice
->IntEndpointDescriptor
.MaxPacketSize
);
289 Status
= UsbIo
->UsbAsyncInterruptTransfer (
295 OnMouseInterruptComplete
,
299 if (EFI_ERROR (Status
)) {
301 // If submit error, uninstall that interface
303 gBS
->UninstallProtocolInterface (
305 &gEfiSimplePointerProtocolGuid
,
306 &UsbMouseDevice
->SimplePointerProtocol
311 UsbMouseDevice
->ControllerNameTable
= NULL
;
314 gUsbMouseComponentName
.SupportedLanguages
,
315 &UsbMouseDevice
->ControllerNameTable
,
316 L
"Generic Usb Mouse",
321 gUsbMouseComponentName2
.SupportedLanguages
,
322 &UsbMouseDevice
->ControllerNameTable
,
323 L
"Generic Usb Mouse",
333 if (EFI_ERROR (Status
)) {
336 &gEfiUsbIoProtocolGuid
,
337 This
->DriverBindingHandle
,
341 if (UsbMouseDevice
!= NULL
) {
342 if ((UsbMouseDevice
->SimplePointerProtocol
).WaitForInput
!= NULL
) {
343 gBS
->CloseEvent ((UsbMouseDevice
->SimplePointerProtocol
).WaitForInput
);
346 FreePool (UsbMouseDevice
);
347 UsbMouseDevice
= NULL
;
356 Stop the USB mouse device handled by this driver.
358 @param This The USB mouse driver binding protocol.
359 @param Controller The controller to release.
360 @param NumberOfChildren The number of handles in ChildHandleBuffer.
361 @param ChildHandleBuffer The array of child handle.
363 @retval EFI_SUCCESS The device was stopped.
364 @retval EFI_UNSUPPORTED Simple Pointer Protocol is not installed on Controller.
365 @retval Others Fail to uninstall protocols attached on the device.
370 USBMouseDriverBindingStop (
371 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
372 IN EFI_HANDLE Controller
,
373 IN UINTN NumberOfChildren
,
374 IN EFI_HANDLE
*ChildHandleBuffer
378 USB_MOUSE_DEV
*UsbMouseDevice
;
379 EFI_SIMPLE_POINTER_PROTOCOL
*SimplePointerProtocol
;
380 EFI_USB_IO_PROTOCOL
*UsbIo
;
382 Status
= gBS
->OpenProtocol (
384 &gEfiSimplePointerProtocolGuid
,
385 (VOID
**) &SimplePointerProtocol
,
386 This
->DriverBindingHandle
,
388 EFI_OPEN_PROTOCOL_GET_PROTOCOL
391 if (EFI_ERROR (Status
)) {
392 return EFI_UNSUPPORTED
;
395 UsbMouseDevice
= USB_MOUSE_DEV_FROM_MOUSE_PROTOCOL (SimplePointerProtocol
);
397 UsbIo
= UsbMouseDevice
->UsbIo
;
400 // The key data input from this device will be disabled.
402 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
404 PcdGet32 (PcdStatusCodeValueMouseDisable
),
405 UsbMouseDevice
->DevicePath
409 // Delete the Asynchronous Interrupt Transfer from this device
411 UsbIo
->UsbAsyncInterruptTransfer (
413 UsbMouseDevice
->IntEndpointDescriptor
.EndpointAddress
,
415 UsbMouseDevice
->IntEndpointDescriptor
.Interval
,
421 Status
= gBS
->UninstallProtocolInterface (
423 &gEfiSimplePointerProtocolGuid
,
424 &UsbMouseDevice
->SimplePointerProtocol
426 if (EFI_ERROR (Status
)) {
432 &gEfiUsbIoProtocolGuid
,
433 This
->DriverBindingHandle
,
438 // Free all resources.
440 gBS
->CloseEvent (UsbMouseDevice
->SimplePointerProtocol
.WaitForInput
);
442 if (UsbMouseDevice
->DelayedRecoveryEvent
!= NULL
) {
443 gBS
->CloseEvent (UsbMouseDevice
->DelayedRecoveryEvent
);
444 UsbMouseDevice
->DelayedRecoveryEvent
= NULL
;
447 if (UsbMouseDevice
->ControllerNameTable
!= NULL
) {
448 FreeUnicodeStringTable (UsbMouseDevice
->ControllerNameTable
);
451 FreePool (UsbMouseDevice
);
459 Uses USB I/O to check whether the device is a USB mouse device.
461 @param UsbIo Pointer to a USB I/O protocol instance.
463 @retval TRUE Device is a USB mouse device.
464 @retval FALSE Device is a not USB mouse device.
469 IN EFI_USB_IO_PROTOCOL
*UsbIo
473 EFI_USB_INTERFACE_DESCRIPTOR InterfaceDescriptor
;
476 // Get the default interface descriptor
478 Status
= UsbIo
->UsbGetInterfaceDescriptor (
483 if (EFI_ERROR (Status
)) {
487 if ((InterfaceDescriptor
.InterfaceClass
== CLASS_HID
) &&
488 (InterfaceDescriptor
.InterfaceSubClass
== SUBCLASS_BOOT
) &&
489 (InterfaceDescriptor
.InterfaceProtocol
== PROTOCOL_MOUSE
)
499 Initialize the USB mouse device.
501 This function retrieves and parses HID report descriptor, and
502 initializes state of USB_MOUSE_DEV. Then it sets indefinite idle
503 rate for the device. Finally it creates event for delayed recovery,
504 which deals with device error.
506 @param UsbMouseDev Device instance to be initialized.
508 @retval EFI_SUCCESS USB mouse device successfully initialized..
509 @retval EFI_UNSUPPORTED HID descriptor type is not report descriptor.
510 @retval Other USB mouse device was not initialized successfully.
514 InitializeUsbMouseDevice (
515 IN OUT USB_MOUSE_DEV
*UsbMouseDev
518 EFI_USB_IO_PROTOCOL
*UsbIo
;
521 EFI_USB_HID_DESCRIPTOR MouseHidDesc
;
526 UsbIo
= UsbMouseDev
->UsbIo
;
529 // Get HID descriptor
531 Status
= UsbGetHidDescriptor (
533 UsbMouseDev
->InterfaceDescriptor
.InterfaceNumber
,
536 if (EFI_ERROR (Status
)) {
541 // Get report descriptor
543 if (MouseHidDesc
.HidClassDesc
[0].DescriptorType
!= USB_DESC_TYPE_REPORT
) {
544 return EFI_UNSUPPORTED
;
547 ReportDesc
= AllocateZeroPool (MouseHidDesc
.HidClassDesc
[0].DescriptorLength
);
548 ASSERT (ReportDesc
!= NULL
);
550 Status
= UsbGetReportDescriptor (
552 UsbMouseDev
->InterfaceDescriptor
.InterfaceNumber
,
553 MouseHidDesc
.HidClassDesc
[0].DescriptorLength
,
557 if (EFI_ERROR (Status
)) {
558 FreePool (ReportDesc
);
563 // Parse report descriptor
565 Status
= ParseMouseReportDescriptor (
568 MouseHidDesc
.HidClassDesc
[0].DescriptorLength
571 if (EFI_ERROR (Status
)) {
572 FreePool (ReportDesc
);
577 // Check the presence of left and right buttons,
578 // and initialize fields of EFI_SIMPLE_POINTER_MODE.
580 if (UsbMouseDev
->NumberOfButtons
>= 1) {
581 UsbMouseDev
->Mode
.LeftButton
= TRUE
;
583 if (UsbMouseDev
->NumberOfButtons
> 1) {
584 UsbMouseDev
->Mode
.RightButton
= TRUE
;
586 UsbMouseDev
->Mode
.ResolutionX
= 8;
587 UsbMouseDev
->Mode
.ResolutionY
= 8;
588 UsbMouseDev
->Mode
.ResolutionZ
= 0;
591 // Set boot protocol for the USB mouse.
592 // This driver only supports boot protocol.
594 UsbGetProtocolRequest (
596 UsbMouseDev
->InterfaceDescriptor
.InterfaceNumber
,
599 if (Protocol
!= BOOT_PROTOCOL
) {
600 Status
= UsbSetProtocolRequest (
606 if (EFI_ERROR (Status
)) {
607 FreePool (ReportDesc
);
613 // ReportId is zero, which means the idle rate applies to all input reports.
617 // Duration is zero, which means the duration is infinite.
618 // so the endpoint will inhibit reporting forever,
619 // and only reporting when a change is detected in the report data.
624 UsbMouseDev
->InterfaceDescriptor
.InterfaceNumber
,
629 FreePool (ReportDesc
);
632 // Create event for delayed recovery, which deals with device error.
634 if (UsbMouseDev
->DelayedRecoveryEvent
!= NULL
) {
635 gBS
->CloseEvent (UsbMouseDev
->DelayedRecoveryEvent
);
636 UsbMouseDev
->DelayedRecoveryEvent
= 0;
640 EVT_TIMER
| EVT_NOTIFY_SIGNAL
,
642 USBMouseRecoveryHandler
,
644 &UsbMouseDev
->DelayedRecoveryEvent
652 Handler function for USB mouse's asynchronous interrupt transfer.
654 This function is the handler function for USB mouse's asynchronous interrupt transfer
655 to manage the mouse. It parses data returned from asynchronous interrupt transfer, and
656 get button and movement state.
658 @param Data A pointer to a buffer that is filled with key data which is
659 retrieved via asynchronous interrupt transfer.
660 @param DataLength Indicates the size of the data buffer.
661 @param Context Pointing to USB_KB_DEV instance.
662 @param Result Indicates the result of the asynchronous interrupt transfer.
664 @retval EFI_SUCCESS Asynchronous interrupt transfer is handled successfully.
665 @retval EFI_DEVICE_ERROR Hardware error occurs.
670 OnMouseInterruptComplete (
677 USB_MOUSE_DEV
*UsbMouseDevice
;
678 EFI_USB_IO_PROTOCOL
*UsbIo
;
682 UsbMouseDevice
= (USB_MOUSE_DEV
*) Context
;
683 UsbIo
= UsbMouseDevice
->UsbIo
;
685 if (Result
!= EFI_USB_NOERROR
) {
687 // Some errors happen during the process
689 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
690 EFI_ERROR_CODE
| EFI_ERROR_MINOR
,
691 PcdGet32 (PcdStatusCodeValueMouseInputError
),
692 UsbMouseDevice
->DevicePath
695 if ((Result
& EFI_USB_ERR_STALL
) == EFI_USB_ERR_STALL
) {
696 EndpointAddr
= UsbMouseDevice
->IntEndpointDescriptor
.EndpointAddress
;
698 UsbClearEndpointHalt (
706 // Delete & Submit this interrupt again
707 // Handler of DelayedRecoveryEvent triggered by timer will re-submit the interrupt.
709 UsbIo
->UsbAsyncInterruptTransfer (
711 UsbMouseDevice
->IntEndpointDescriptor
.EndpointAddress
,
719 // EFI_USB_INTERRUPT_DELAY is defined in USB standard for error handling.
722 UsbMouseDevice
->DelayedRecoveryEvent
,
724 EFI_USB_INTERRUPT_DELAY
726 return EFI_DEVICE_ERROR
;
730 // If no error and no data, just return EFI_SUCCESS.
732 if (DataLength
== 0 || Data
== NULL
) {
736 UsbMouseDevice
->StateChanged
= TRUE
;
740 // USB HID Specification specifies following data format:
741 // Byte Bits Description
745 // 4 to 7 Device-specific
746 // 1 0 to 7 X displacement
747 // 2 0 to 7 Y displacement
748 // 3 to n 0 to 7 Device specific (optional)
750 UsbMouseDevice
->State
.LeftButton
= (BOOLEAN
) ((*(UINT8
*) Data
& BIT0
) != 0);
751 UsbMouseDevice
->State
.RightButton
= (BOOLEAN
) ((*(UINT8
*) Data
& BIT1
) != 0);
752 UsbMouseDevice
->State
.RelativeMovementX
+= *((INT8
*) Data
+ 1);
753 UsbMouseDevice
->State
.RelativeMovementY
+= *((INT8
*) Data
+ 2);
755 if (DataLength
> 3) {
756 UsbMouseDevice
->State
.RelativeMovementZ
+= *((INT8
*) Data
+ 3);
763 Retrieves the current state of a pointer device.
765 @param This A pointer to the EFI_SIMPLE_POINTER_PROTOCOL instance.
766 @param MouseState A pointer to the state information on the pointer device.
768 @retval EFI_SUCCESS The state of the pointer device was returned in State.
769 @retval EFI_NOT_READY The state of the pointer device has not changed since the last call to
771 @retval EFI_DEVICE_ERROR A device error occurred while attempting to retrieve the pointer device's
773 @retval EFI_INVALID_PARAMETER MouseState is NULL.
779 IN EFI_SIMPLE_POINTER_PROTOCOL
*This
,
780 OUT EFI_SIMPLE_POINTER_STATE
*MouseState
783 USB_MOUSE_DEV
*MouseDev
;
785 if (MouseState
== NULL
) {
786 return EFI_INVALID_PARAMETER
;
789 MouseDev
= USB_MOUSE_DEV_FROM_MOUSE_PROTOCOL (This
);
791 if (!MouseDev
->StateChanged
) {
792 return EFI_NOT_READY
;
796 // Retrieve mouse state from USB_MOUSE_DEV, which was filled by OnMouseInterruptComplete()
801 sizeof (EFI_SIMPLE_POINTER_STATE
)
805 // Clear previous move state
807 MouseDev
->State
.RelativeMovementX
= 0;
808 MouseDev
->State
.RelativeMovementY
= 0;
809 MouseDev
->State
.RelativeMovementZ
= 0;
811 MouseDev
->StateChanged
= FALSE
;
818 Resets the pointer device hardware.
820 @param This A pointer to the EFI_SIMPLE_POINTER_PROTOCOL instance.
821 @param ExtendedVerification Indicates that the driver may perform a more exhaustive
822 verification operation of the device during reset.
824 @retval EFI_SUCCESS The device was reset.
825 @retval EFI_DEVICE_ERROR The device is not functioning correctly and could not be reset.
831 IN EFI_SIMPLE_POINTER_PROTOCOL
*This
,
832 IN BOOLEAN ExtendedVerification
835 USB_MOUSE_DEV
*UsbMouseDevice
;
837 UsbMouseDevice
= USB_MOUSE_DEV_FROM_MOUSE_PROTOCOL (This
);
839 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
841 PcdGet32 (PcdStatusCodeValueMouseReset
),
842 UsbMouseDevice
->DevicePath
846 // Clear mouse state.
849 &UsbMouseDevice
->State
,
850 sizeof (EFI_SIMPLE_POINTER_STATE
)
852 UsbMouseDevice
->StateChanged
= FALSE
;
858 Event notification function for SIMPLE_POINTER.WaitForInput event.
860 @param Event Event to be signaled when there's input from mouse.
861 @param Context Points to USB_MOUSE_DEV instance.
866 UsbMouseWaitForInput (
871 USB_MOUSE_DEV
*UsbMouseDev
;
873 UsbMouseDev
= (USB_MOUSE_DEV
*) Context
;
876 // If there's input from mouse, signal the event.
878 if (UsbMouseDev
->StateChanged
) {
879 gBS
->SignalEvent (Event
);
884 Handler for Delayed Recovery event.
886 This function is the handler for Delayed Recovery event triggered
888 After a device error occurs, the event would be triggered
889 with interval of EFI_USB_INTERRUPT_DELAY. EFI_USB_INTERRUPT_DELAY
890 is defined in USB standard for error handling.
892 @param Event The Delayed Recovery event.
893 @param Context Points to the USB_MOUSE_DEV instance.
898 USBMouseRecoveryHandler (
903 USB_MOUSE_DEV
*UsbMouseDev
;
904 EFI_USB_IO_PROTOCOL
*UsbIo
;
906 UsbMouseDev
= (USB_MOUSE_DEV
*) Context
;
908 UsbIo
= UsbMouseDev
->UsbIo
;
911 // Re-submit Asynchronous Interrupt Transfer for recovery.
913 UsbIo
->UsbAsyncInterruptTransfer (
915 UsbMouseDev
->IntEndpointDescriptor
.EndpointAddress
,
917 UsbMouseDev
->IntEndpointDescriptor
.Interval
,
918 UsbMouseDev
->IntEndpointDescriptor
.MaxPacketSize
,
919 OnMouseInterruptComplete
,