]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Bus/Usb/UsbMouseDxe/UsbMouse.c
Update the copyright notice format
[mirror_edk2.git] / MdeModulePkg / Bus / Usb / UsbMouseDxe / UsbMouse.c
1 /** @file
2 USB Mouse Driver that manages USB mouse and produces Simple Pointer Protocol.
3
4 Copyright (c) 2004 - 2010, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 **/
14
15 #include "UsbMouse.h"
16
17 EFI_DRIVER_BINDING_PROTOCOL gUsbMouseDriverBinding = {
18 USBMouseDriverBindingSupported,
19 USBMouseDriverBindingStart,
20 USBMouseDriverBindingStop,
21 0xa,
22 NULL,
23 NULL
24 };
25
26 /**
27 Entrypoint of USB Mouse Driver.
28
29 This function is the entrypoint of USB Mouse Driver. It installs Driver Binding
30 Protocols together with Component Name Protocols.
31
32 @param ImageHandle The firmware allocated handle for the EFI image.
33 @param SystemTable A pointer to the EFI System Table.
34
35 @retval EFI_SUCCESS The entry point is executed successfully.
36
37 **/
38 EFI_STATUS
39 EFIAPI
40 USBMouseDriverBindingEntryPoint (
41 IN EFI_HANDLE ImageHandle,
42 IN EFI_SYSTEM_TABLE *SystemTable
43 )
44 {
45 EFI_STATUS Status;
46
47 Status = EfiLibInstallDriverBindingComponentName2 (
48 ImageHandle,
49 SystemTable,
50 &gUsbMouseDriverBinding,
51 ImageHandle,
52 &gUsbMouseComponentName,
53 &gUsbMouseComponentName2
54 );
55 ASSERT_EFI_ERROR (Status);
56
57 return EFI_SUCCESS;
58 }
59
60
61 /**
62 Check whether USB mouse driver supports this device.
63
64 @param This The USB mouse driver binding protocol.
65 @param Controller The controller handle to check.
66 @param RemainingDevicePath The remaining device path.
67
68 @retval EFI_SUCCESS The driver supports this controller.
69 @retval other This device isn't supported.
70
71 **/
72 EFI_STATUS
73 EFIAPI
74 USBMouseDriverBindingSupported (
75 IN EFI_DRIVER_BINDING_PROTOCOL *This,
76 IN EFI_HANDLE Controller,
77 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
78 )
79 {
80 EFI_STATUS Status;
81 EFI_USB_IO_PROTOCOL *UsbIo;
82
83 Status = gBS->OpenProtocol (
84 Controller,
85 &gEfiUsbIoProtocolGuid,
86 (VOID **) &UsbIo,
87 This->DriverBindingHandle,
88 Controller,
89 EFI_OPEN_PROTOCOL_BY_DRIVER
90 );
91 if (EFI_ERROR (Status)) {
92 return Status;
93 }
94
95 //
96 // Use the USB I/O Protocol interface to check whether Controller is
97 // a mouse device that can be managed by this driver.
98 //
99 Status = EFI_SUCCESS;
100 if (!IsUsbMouse (UsbIo)) {
101 Status = EFI_UNSUPPORTED;
102 }
103
104 gBS->CloseProtocol (
105 Controller,
106 &gEfiUsbIoProtocolGuid,
107 This->DriverBindingHandle,
108 Controller
109 );
110
111 return Status;
112 }
113
114
115 /**
116 Starts the mouse device with this driver.
117
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.
121
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
125 device to start.
126
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.
132
133 **/
134 EFI_STATUS
135 EFIAPI
136 USBMouseDriverBindingStart (
137 IN EFI_DRIVER_BINDING_PROTOCOL *This,
138 IN EFI_HANDLE Controller,
139 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
140 )
141 {
142 EFI_STATUS Status;
143 EFI_USB_IO_PROTOCOL *UsbIo;
144 USB_MOUSE_DEV *UsbMouseDevice;
145 UINT8 EndpointNumber;
146 EFI_USB_ENDPOINT_DESCRIPTOR EndpointDescriptor;
147 UINT8 Index;
148 UINT8 EndpointAddr;
149 UINT8 PollingInterval;
150 UINT8 PacketSize;
151 BOOLEAN Found;
152
153 //
154 // Open USB I/O Protocol
155 //
156 Status = gBS->OpenProtocol (
157 Controller,
158 &gEfiUsbIoProtocolGuid,
159 (VOID **) &UsbIo,
160 This->DriverBindingHandle,
161 Controller,
162 EFI_OPEN_PROTOCOL_BY_DRIVER
163 );
164 if (EFI_ERROR (Status)) {
165 return Status;
166 }
167
168 UsbMouseDevice = AllocateZeroPool (sizeof (USB_MOUSE_DEV));
169 ASSERT (UsbMouseDevice != NULL);
170
171 UsbMouseDevice->UsbIo = UsbIo;
172 UsbMouseDevice->Signature = USB_MOUSE_DEV_SIGNATURE;
173
174 //
175 // Get the Device Path Protocol on Controller's handle
176 //
177 Status = gBS->OpenProtocol (
178 Controller,
179 &gEfiDevicePathProtocolGuid,
180 (VOID **) &UsbMouseDevice->DevicePath,
181 This->DriverBindingHandle,
182 Controller,
183 EFI_OPEN_PROTOCOL_GET_PROTOCOL
184 );
185
186 if (EFI_ERROR (Status)) {
187 goto ErrorExit;
188 }
189 //
190 // Get interface & endpoint descriptor
191 //
192 UsbIo->UsbGetInterfaceDescriptor (
193 UsbIo,
194 &UsbMouseDevice->InterfaceDescriptor
195 );
196
197 EndpointNumber = UsbMouseDevice->InterfaceDescriptor.NumEndpoints;
198
199 //
200 // Traverse endpoints to find interrupt endpoint
201 //
202 Found = FALSE;
203 for (Index = 0; Index < EndpointNumber; Index++) {
204 UsbIo->UsbGetEndpointDescriptor (
205 UsbIo,
206 Index,
207 &EndpointDescriptor
208 );
209
210 if ((EndpointDescriptor.Attributes & (BIT0 | BIT1)) == USB_ENDPOINT_INTERRUPT) {
211 //
212 // We only care interrupt endpoint here
213 //
214 CopyMem(&UsbMouseDevice->IntEndpointDescriptor, &EndpointDescriptor, sizeof(EndpointDescriptor));
215 Found = TRUE;
216 break;
217 }
218 }
219
220 if (!Found) {
221 //
222 // No interrupt endpoint found, then return unsupported.
223 //
224 Status = EFI_UNSUPPORTED;
225 goto ErrorExit;
226 }
227
228 Status = InitializeUsbMouseDevice (UsbMouseDevice);
229 if (EFI_ERROR (Status)) {
230 //
231 // Fail to initialize USB mouse device.
232 //
233 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
234 EFI_ERROR_CODE | EFI_ERROR_MINOR,
235 (EFI_PERIPHERAL_MOUSE | EFI_P_EC_INTERFACE_ERROR),
236 UsbMouseDevice->DevicePath
237 );
238
239 goto ErrorExit;
240 }
241
242 //
243 // Initialize and install EFI Simple Pointer Protocol.
244 //
245 UsbMouseDevice->SimplePointerProtocol.GetState = GetMouseState;
246 UsbMouseDevice->SimplePointerProtocol.Reset = UsbMouseReset;
247 UsbMouseDevice->SimplePointerProtocol.Mode = &UsbMouseDevice->Mode;
248
249 Status = gBS->CreateEvent (
250 EVT_NOTIFY_WAIT,
251 TPL_NOTIFY,
252 UsbMouseWaitForInput,
253 UsbMouseDevice,
254 &((UsbMouseDevice->SimplePointerProtocol).WaitForInput)
255 );
256 if (EFI_ERROR (Status)) {
257 goto ErrorExit;
258 }
259
260 Status = gBS->InstallProtocolInterface (
261 &Controller,
262 &gEfiSimplePointerProtocolGuid,
263 EFI_NATIVE_INTERFACE,
264 &UsbMouseDevice->SimplePointerProtocol
265 );
266
267 if (EFI_ERROR (Status)) {
268 goto ErrorExit;
269 }
270
271 //
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.
275 //
276 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
277 EFI_PROGRESS_CODE,
278 (EFI_PERIPHERAL_MOUSE | EFI_P_PC_ENABLE),
279 UsbMouseDevice->DevicePath
280 );
281
282 //
283 // Submit Asynchronous Interrupt Transfer to manage this device.
284 //
285 EndpointAddr = UsbMouseDevice->IntEndpointDescriptor.EndpointAddress;
286 PollingInterval = UsbMouseDevice->IntEndpointDescriptor.Interval;
287 PacketSize = (UINT8) (UsbMouseDevice->IntEndpointDescriptor.MaxPacketSize);
288
289 Status = UsbIo->UsbAsyncInterruptTransfer (
290 UsbIo,
291 EndpointAddr,
292 TRUE,
293 PollingInterval,
294 PacketSize,
295 OnMouseInterruptComplete,
296 UsbMouseDevice
297 );
298
299 if (EFI_ERROR (Status)) {
300 //
301 // If submit error, uninstall that interface
302 //
303 gBS->UninstallProtocolInterface (
304 Controller,
305 &gEfiSimplePointerProtocolGuid,
306 &UsbMouseDevice->SimplePointerProtocol
307 );
308 goto ErrorExit;
309 }
310
311 UsbMouseDevice->ControllerNameTable = NULL;
312 AddUnicodeString2 (
313 "eng",
314 gUsbMouseComponentName.SupportedLanguages,
315 &UsbMouseDevice->ControllerNameTable,
316 L"Generic Usb Mouse",
317 TRUE
318 );
319 AddUnicodeString2 (
320 "en",
321 gUsbMouseComponentName2.SupportedLanguages,
322 &UsbMouseDevice->ControllerNameTable,
323 L"Generic Usb Mouse",
324 FALSE
325 );
326
327 return EFI_SUCCESS;
328
329 //
330 // Error handler
331 //
332 ErrorExit:
333 if (EFI_ERROR (Status)) {
334 gBS->CloseProtocol (
335 Controller,
336 &gEfiUsbIoProtocolGuid,
337 This->DriverBindingHandle,
338 Controller
339 );
340
341 if (UsbMouseDevice != NULL) {
342 if ((UsbMouseDevice->SimplePointerProtocol).WaitForInput != NULL) {
343 gBS->CloseEvent ((UsbMouseDevice->SimplePointerProtocol).WaitForInput);
344 }
345
346 FreePool (UsbMouseDevice);
347 UsbMouseDevice = NULL;
348 }
349 }
350
351 return Status;
352 }
353
354
355 /**
356 Stop the USB mouse device handled by this driver.
357
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.
362
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.
366
367 **/
368 EFI_STATUS
369 EFIAPI
370 USBMouseDriverBindingStop (
371 IN EFI_DRIVER_BINDING_PROTOCOL *This,
372 IN EFI_HANDLE Controller,
373 IN UINTN NumberOfChildren,
374 IN EFI_HANDLE *ChildHandleBuffer
375 )
376 {
377 EFI_STATUS Status;
378 USB_MOUSE_DEV *UsbMouseDevice;
379 EFI_SIMPLE_POINTER_PROTOCOL *SimplePointerProtocol;
380 EFI_USB_IO_PROTOCOL *UsbIo;
381
382 Status = gBS->OpenProtocol (
383 Controller,
384 &gEfiSimplePointerProtocolGuid,
385 (VOID **) &SimplePointerProtocol,
386 This->DriverBindingHandle,
387 Controller,
388 EFI_OPEN_PROTOCOL_GET_PROTOCOL
389 );
390
391 if (EFI_ERROR (Status)) {
392 return EFI_UNSUPPORTED;
393 }
394
395 UsbMouseDevice = USB_MOUSE_DEV_FROM_MOUSE_PROTOCOL (SimplePointerProtocol);
396
397 UsbIo = UsbMouseDevice->UsbIo;
398
399 //
400 // The key data input from this device will be disabled.
401 //
402 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
403 EFI_PROGRESS_CODE,
404 (EFI_PERIPHERAL_MOUSE | EFI_P_PC_DISABLE),
405 UsbMouseDevice->DevicePath
406 );
407
408 //
409 // Delete the Asynchronous Interrupt Transfer from this device
410 //
411 UsbIo->UsbAsyncInterruptTransfer (
412 UsbIo,
413 UsbMouseDevice->IntEndpointDescriptor.EndpointAddress,
414 FALSE,
415 UsbMouseDevice->IntEndpointDescriptor.Interval,
416 0,
417 NULL,
418 NULL
419 );
420
421 Status = gBS->UninstallProtocolInterface (
422 Controller,
423 &gEfiSimplePointerProtocolGuid,
424 &UsbMouseDevice->SimplePointerProtocol
425 );
426 if (EFI_ERROR (Status)) {
427 return Status;
428 }
429
430 gBS->CloseProtocol (
431 Controller,
432 &gEfiUsbIoProtocolGuid,
433 This->DriverBindingHandle,
434 Controller
435 );
436
437 //
438 // Free all resources.
439 //
440 gBS->CloseEvent (UsbMouseDevice->SimplePointerProtocol.WaitForInput);
441
442 if (UsbMouseDevice->DelayedRecoveryEvent != NULL) {
443 gBS->CloseEvent (UsbMouseDevice->DelayedRecoveryEvent);
444 UsbMouseDevice->DelayedRecoveryEvent = NULL;
445 }
446
447 if (UsbMouseDevice->ControllerNameTable != NULL) {
448 FreeUnicodeStringTable (UsbMouseDevice->ControllerNameTable);
449 }
450
451 FreePool (UsbMouseDevice);
452
453 return EFI_SUCCESS;
454
455 }
456
457
458 /**
459 Uses USB I/O to check whether the device is a USB mouse device.
460
461 @param UsbIo Pointer to a USB I/O protocol instance.
462
463 @retval TRUE Device is a USB mouse device.
464 @retval FALSE Device is a not USB mouse device.
465
466 **/
467 BOOLEAN
468 IsUsbMouse (
469 IN EFI_USB_IO_PROTOCOL *UsbIo
470 )
471 {
472 EFI_STATUS Status;
473 EFI_USB_INTERFACE_DESCRIPTOR InterfaceDescriptor;
474
475 //
476 // Get the default interface descriptor
477 //
478 Status = UsbIo->UsbGetInterfaceDescriptor (
479 UsbIo,
480 &InterfaceDescriptor
481 );
482
483 if (EFI_ERROR (Status)) {
484 return FALSE;
485 }
486
487 if ((InterfaceDescriptor.InterfaceClass == CLASS_HID) &&
488 (InterfaceDescriptor.InterfaceSubClass == SUBCLASS_BOOT) &&
489 (InterfaceDescriptor.InterfaceProtocol == PROTOCOL_MOUSE)
490 ) {
491 return TRUE;
492 }
493
494 return FALSE;
495 }
496
497
498 /**
499 Initialize the USB mouse device.
500
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.
505
506 @param UsbMouseDev Device instance to be initialized.
507
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.
511
512 **/
513 EFI_STATUS
514 InitializeUsbMouseDevice (
515 IN OUT USB_MOUSE_DEV *UsbMouseDev
516 )
517 {
518 EFI_USB_IO_PROTOCOL *UsbIo;
519 UINT8 Protocol;
520 EFI_STATUS Status;
521 EFI_USB_HID_DESCRIPTOR MouseHidDesc;
522 UINT8 *ReportDesc;
523 UINT8 ReportId;
524 UINT8 Duration;
525
526 UsbIo = UsbMouseDev->UsbIo;
527
528 //
529 // Get HID descriptor
530 //
531 Status = UsbGetHidDescriptor (
532 UsbIo,
533 UsbMouseDev->InterfaceDescriptor.InterfaceNumber,
534 &MouseHidDesc
535 );
536 if (EFI_ERROR (Status)) {
537 return Status;
538 }
539
540 //
541 // Get report descriptor
542 //
543 if (MouseHidDesc.HidClassDesc[0].DescriptorType != USB_DESC_TYPE_REPORT) {
544 return EFI_UNSUPPORTED;
545 }
546
547 ReportDesc = AllocateZeroPool (MouseHidDesc.HidClassDesc[0].DescriptorLength);
548 ASSERT (ReportDesc != NULL);
549
550 Status = UsbGetReportDescriptor (
551 UsbIo,
552 UsbMouseDev->InterfaceDescriptor.InterfaceNumber,
553 MouseHidDesc.HidClassDesc[0].DescriptorLength,
554 ReportDesc
555 );
556
557 if (EFI_ERROR (Status)) {
558 FreePool (ReportDesc);
559 return Status;
560 }
561
562 //
563 // Parse report descriptor
564 //
565 Status = ParseMouseReportDescriptor (
566 UsbMouseDev,
567 ReportDesc,
568 MouseHidDesc.HidClassDesc[0].DescriptorLength
569 );
570
571 if (EFI_ERROR (Status)) {
572 FreePool (ReportDesc);
573 return Status;
574 }
575
576 //
577 // Check the presence of left and right buttons,
578 // and initialize fields of EFI_SIMPLE_POINTER_MODE.
579 //
580 if (UsbMouseDev->NumberOfButtons >= 1) {
581 UsbMouseDev->Mode.LeftButton = TRUE;
582 }
583 if (UsbMouseDev->NumberOfButtons > 1) {
584 UsbMouseDev->Mode.RightButton = TRUE;
585 }
586 UsbMouseDev->Mode.ResolutionX = 8;
587 UsbMouseDev->Mode.ResolutionY = 8;
588 UsbMouseDev->Mode.ResolutionZ = 0;
589
590 //
591 // Set boot protocol for the USB mouse.
592 // This driver only supports boot protocol.
593 //
594 UsbGetProtocolRequest (
595 UsbIo,
596 UsbMouseDev->InterfaceDescriptor.InterfaceNumber,
597 &Protocol
598 );
599 if (Protocol != BOOT_PROTOCOL) {
600 Status = UsbSetProtocolRequest (
601 UsbIo,
602 0,
603 BOOT_PROTOCOL
604 );
605
606 if (EFI_ERROR (Status)) {
607 FreePool (ReportDesc);
608 return Status;
609 }
610 }
611
612 //
613 // ReportId is zero, which means the idle rate applies to all input reports.
614 //
615 ReportId = 0;
616 //
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.
620 //
621 Duration = 0;
622 UsbSetIdleRequest (
623 UsbIo,
624 UsbMouseDev->InterfaceDescriptor.InterfaceNumber,
625 ReportId,
626 Duration
627 );
628
629 FreePool (ReportDesc);
630
631 //
632 // Create event for delayed recovery, which deals with device error.
633 //
634 if (UsbMouseDev->DelayedRecoveryEvent != NULL) {
635 gBS->CloseEvent (UsbMouseDev->DelayedRecoveryEvent);
636 UsbMouseDev->DelayedRecoveryEvent = 0;
637 }
638
639 gBS->CreateEvent (
640 EVT_TIMER | EVT_NOTIFY_SIGNAL,
641 TPL_NOTIFY,
642 USBMouseRecoveryHandler,
643 UsbMouseDev,
644 &UsbMouseDev->DelayedRecoveryEvent
645 );
646
647 return EFI_SUCCESS;
648 }
649
650
651 /**
652 Handler function for USB mouse's asynchronous interrupt transfer.
653
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.
657
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.
663
664 @retval EFI_SUCCESS Asynchronous interrupt transfer is handled successfully.
665 @retval EFI_DEVICE_ERROR Hardware error occurs.
666
667 **/
668 EFI_STATUS
669 EFIAPI
670 OnMouseInterruptComplete (
671 IN VOID *Data,
672 IN UINTN DataLength,
673 IN VOID *Context,
674 IN UINT32 Result
675 )
676 {
677 USB_MOUSE_DEV *UsbMouseDevice;
678 EFI_USB_IO_PROTOCOL *UsbIo;
679 UINT8 EndpointAddr;
680 UINT32 UsbResult;
681
682 UsbMouseDevice = (USB_MOUSE_DEV *) Context;
683 UsbIo = UsbMouseDevice->UsbIo;
684
685 if (Result != EFI_USB_NOERROR) {
686 //
687 // Some errors happen during the process
688 //
689 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
690 EFI_ERROR_CODE | EFI_ERROR_MINOR,
691 (EFI_PERIPHERAL_MOUSE | EFI_P_EC_INPUT_ERROR),
692 UsbMouseDevice->DevicePath
693 );
694
695 if ((Result & EFI_USB_ERR_STALL) == EFI_USB_ERR_STALL) {
696 EndpointAddr = UsbMouseDevice->IntEndpointDescriptor.EndpointAddress;
697
698 UsbClearEndpointHalt (
699 UsbIo,
700 EndpointAddr,
701 &UsbResult
702 );
703 }
704
705 //
706 // Delete & Submit this interrupt again
707 // Handler of DelayedRecoveryEvent triggered by timer will re-submit the interrupt.
708 //
709 UsbIo->UsbAsyncInterruptTransfer (
710 UsbIo,
711 UsbMouseDevice->IntEndpointDescriptor.EndpointAddress,
712 FALSE,
713 0,
714 0,
715 NULL,
716 NULL
717 );
718 //
719 // EFI_USB_INTERRUPT_DELAY is defined in USB standard for error handling.
720 //
721 gBS->SetTimer (
722 UsbMouseDevice->DelayedRecoveryEvent,
723 TimerRelative,
724 EFI_USB_INTERRUPT_DELAY
725 );
726 return EFI_DEVICE_ERROR;
727 }
728
729 //
730 // If no error and no data, just return EFI_SUCCESS.
731 //
732 if (DataLength == 0 || Data == NULL) {
733 return EFI_SUCCESS;
734 }
735
736 UsbMouseDevice->StateChanged = TRUE;
737
738 //
739 // Check mouse Data
740 // USB HID Specification specifies following data format:
741 // Byte Bits Description
742 // 0 0 Button 1
743 // 1 Button 2
744 // 2 Button 3
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)
749 //
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);
754
755 if (DataLength > 3) {
756 UsbMouseDevice->State.RelativeMovementZ += *((INT8 *) Data + 3);
757 }
758
759 return EFI_SUCCESS;
760 }
761
762 /**
763 Retrieves the current state of a pointer device.
764
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.
767
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
770 GetState().
771 @retval EFI_DEVICE_ERROR A device error occurred while attempting to retrieve the pointer device's
772 current state.
773 @retval EFI_INVALID_PARAMETER MouseState is NULL.
774
775 **/
776 EFI_STATUS
777 EFIAPI
778 GetMouseState (
779 IN EFI_SIMPLE_POINTER_PROTOCOL *This,
780 OUT EFI_SIMPLE_POINTER_STATE *MouseState
781 )
782 {
783 USB_MOUSE_DEV *MouseDev;
784
785 if (MouseState == NULL) {
786 return EFI_INVALID_PARAMETER;
787 }
788
789 MouseDev = USB_MOUSE_DEV_FROM_MOUSE_PROTOCOL (This);
790
791 if (!MouseDev->StateChanged) {
792 return EFI_NOT_READY;
793 }
794
795 //
796 // Retrieve mouse state from USB_MOUSE_DEV, which was filled by OnMouseInterruptComplete()
797 //
798 CopyMem (
799 MouseState,
800 &MouseDev->State,
801 sizeof (EFI_SIMPLE_POINTER_STATE)
802 );
803
804 //
805 // Clear previous move state
806 //
807 MouseDev->State.RelativeMovementX = 0;
808 MouseDev->State.RelativeMovementY = 0;
809 MouseDev->State.RelativeMovementZ = 0;
810
811 MouseDev->StateChanged = FALSE;
812
813 return EFI_SUCCESS;
814 }
815
816
817 /**
818 Resets the pointer device hardware.
819
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.
823
824 @retval EFI_SUCCESS The device was reset.
825 @retval EFI_DEVICE_ERROR The device is not functioning correctly and could not be reset.
826
827 **/
828 EFI_STATUS
829 EFIAPI
830 UsbMouseReset (
831 IN EFI_SIMPLE_POINTER_PROTOCOL *This,
832 IN BOOLEAN ExtendedVerification
833 )
834 {
835 USB_MOUSE_DEV *UsbMouseDevice;
836
837 UsbMouseDevice = USB_MOUSE_DEV_FROM_MOUSE_PROTOCOL (This);
838
839 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
840 EFI_PROGRESS_CODE,
841 (EFI_PERIPHERAL_MOUSE | EFI_P_PC_RESET),
842 UsbMouseDevice->DevicePath
843 );
844
845 //
846 // Clear mouse state.
847 //
848 ZeroMem (
849 &UsbMouseDevice->State,
850 sizeof (EFI_SIMPLE_POINTER_STATE)
851 );
852 UsbMouseDevice->StateChanged = FALSE;
853
854 return EFI_SUCCESS;
855 }
856
857 /**
858 Event notification function for EFI_SIMPLE_POINTER_PROTOCOL.WaitForInput event.
859
860 @param Event Event to be signaled when there's input from mouse.
861 @param Context Points to USB_MOUSE_DEV instance.
862
863 **/
864 VOID
865 EFIAPI
866 UsbMouseWaitForInput (
867 IN EFI_EVENT Event,
868 IN VOID *Context
869 )
870 {
871 USB_MOUSE_DEV *UsbMouseDev;
872
873 UsbMouseDev = (USB_MOUSE_DEV *) Context;
874
875 //
876 // If there's input from mouse, signal the event.
877 //
878 if (UsbMouseDev->StateChanged) {
879 gBS->SignalEvent (Event);
880 }
881 }
882
883 /**
884 Handler for Delayed Recovery event.
885
886 This function is the handler for Delayed Recovery event triggered
887 by timer.
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.
891
892 @param Event The Delayed Recovery event.
893 @param Context Points to the USB_MOUSE_DEV instance.
894
895 **/
896 VOID
897 EFIAPI
898 USBMouseRecoveryHandler (
899 IN EFI_EVENT Event,
900 IN VOID *Context
901 )
902 {
903 USB_MOUSE_DEV *UsbMouseDev;
904 EFI_USB_IO_PROTOCOL *UsbIo;
905
906 UsbMouseDev = (USB_MOUSE_DEV *) Context;
907
908 UsbIo = UsbMouseDev->UsbIo;
909
910 //
911 // Re-submit Asynchronous Interrupt Transfer for recovery.
912 //
913 UsbIo->UsbAsyncInterruptTransfer (
914 UsbIo,
915 UsbMouseDev->IntEndpointDescriptor.EndpointAddress,
916 TRUE,
917 UsbMouseDev->IntEndpointDescriptor.Interval,
918 UsbMouseDev->IntEndpointDescriptor.MaxPacketSize,
919 OnMouseInterruptComplete,
920 UsbMouseDev
921 );
922 }