]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Bus/Usb/UsbMouseAbsolutePointerDxe/UsbMouseAbsolutePointer.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / MdeModulePkg / Bus / Usb / UsbMouseAbsolutePointerDxe / UsbMouseAbsolutePointer.c
1 /** @file
2 USB Mouse Driver that manages USB mouse and produces Absolute Pointer Protocol.
3
4 Copyright (c) 2004 - 2018, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
6
7 **/
8
9 #include "UsbMouseAbsolutePointer.h"
10
11 EFI_DRIVER_BINDING_PROTOCOL gUsbMouseAbsolutePointerDriverBinding = {
12 USBMouseAbsolutePointerDriverBindingSupported,
13 USBMouseAbsolutePointerDriverBindingStart,
14 USBMouseAbsolutePointerDriverBindingStop,
15 0x1,
16 NULL,
17 NULL
18 };
19
20 /**
21 Entrypoint of USB Mouse Absolute Pointer Driver.
22
23 This function is the entrypoint of USB Mouse Driver. It installs Driver Binding
24 Protocols together with Component Name Protocols.
25
26 @param ImageHandle The firmware allocated handle for the EFI image.
27 @param SystemTable A pointer to the EFI System Table.
28
29 @retval EFI_SUCCESS The entry point is executed successfully.
30
31 **/
32 EFI_STATUS
33 EFIAPI
34 USBMouseAbsolutePointerDriverBindingEntryPoint (
35 IN EFI_HANDLE ImageHandle,
36 IN EFI_SYSTEM_TABLE *SystemTable
37 )
38 {
39 EFI_STATUS Status;
40
41 Status = EfiLibInstallDriverBindingComponentName2 (
42 ImageHandle,
43 SystemTable,
44 &gUsbMouseAbsolutePointerDriverBinding,
45 ImageHandle,
46 &gUsbMouseAbsolutePointerComponentName,
47 &gUsbMouseAbsolutePointerComponentName2
48 );
49 ASSERT_EFI_ERROR (Status);
50
51 return EFI_SUCCESS;
52 }
53
54 /**
55 Check whether USB Mouse Absolute Pointer Driver supports this device.
56
57 @param This The driver binding protocol.
58 @param Controller The controller handle to check.
59 @param RemainingDevicePath The remaining device path.
60
61 @retval EFI_SUCCESS The driver supports this controller.
62 @retval other This device isn't supported.
63
64 **/
65 EFI_STATUS
66 EFIAPI
67 USBMouseAbsolutePointerDriverBindingSupported (
68 IN EFI_DRIVER_BINDING_PROTOCOL *This,
69 IN EFI_HANDLE Controller,
70 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
71 )
72 {
73 EFI_STATUS Status;
74 EFI_USB_IO_PROTOCOL *UsbIo;
75
76 Status = gBS->OpenProtocol (
77 Controller,
78 &gEfiUsbIoProtocolGuid,
79 (VOID **)&UsbIo,
80 This->DriverBindingHandle,
81 Controller,
82 EFI_OPEN_PROTOCOL_BY_DRIVER
83 );
84 if (EFI_ERROR (Status)) {
85 return Status;
86 }
87
88 //
89 // Use the USB I/O Protocol interface to check whether Controller is
90 // a mouse device that can be managed by this driver.
91 //
92 Status = EFI_SUCCESS;
93 if (!IsUsbMouse (UsbIo)) {
94 Status = EFI_UNSUPPORTED;
95 }
96
97 gBS->CloseProtocol (
98 Controller,
99 &gEfiUsbIoProtocolGuid,
100 This->DriverBindingHandle,
101 Controller
102 );
103
104 return Status;
105 }
106
107 /**
108 Starts the mouse device with this driver.
109
110 This function consumes USB I/O Protocol, initializes USB mouse device,
111 installs Absolute Pointer Protocol, and submits Asynchronous Interrupt
112 Transfer to manage the USB mouse device.
113
114 @param This The driver binding instance.
115 @param Controller Handle of device to bind driver to.
116 @param RemainingDevicePath Optional parameter use to pick a specific child
117 device to start.
118
119 @retval EFI_SUCCESS This driver supports this device.
120 @retval EFI_UNSUPPORTED This driver does not support this device.
121 @retval EFI_DEVICE_ERROR This driver cannot be started due to device Error.
122 @retval EFI_OUT_OF_RESOURCES Can't allocate memory resources.
123 @retval EFI_ALREADY_STARTED This driver has been started.
124
125 **/
126 EFI_STATUS
127 EFIAPI
128 USBMouseAbsolutePointerDriverBindingStart (
129 IN EFI_DRIVER_BINDING_PROTOCOL *This,
130 IN EFI_HANDLE Controller,
131 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
132 )
133 {
134 EFI_STATUS Status;
135 EFI_USB_IO_PROTOCOL *UsbIo;
136 USB_MOUSE_ABSOLUTE_POINTER_DEV *UsbMouseAbsolutePointerDevice;
137 UINT8 EndpointNumber;
138 EFI_USB_ENDPOINT_DESCRIPTOR EndpointDescriptor;
139 UINT8 Index;
140 UINT8 EndpointAddr;
141 UINT8 PollingInterval;
142 UINT8 PacketSize;
143 BOOLEAN Found;
144 EFI_TPL OldTpl;
145
146 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
147 //
148 // Open USB I/O Protocol
149 //
150 Status = gBS->OpenProtocol (
151 Controller,
152 &gEfiUsbIoProtocolGuid,
153 (VOID **)&UsbIo,
154 This->DriverBindingHandle,
155 Controller,
156 EFI_OPEN_PROTOCOL_BY_DRIVER
157 );
158 if (EFI_ERROR (Status)) {
159 goto ErrorExit1;
160 }
161
162 UsbMouseAbsolutePointerDevice = AllocateZeroPool (sizeof (USB_MOUSE_ABSOLUTE_POINTER_DEV));
163 ASSERT (UsbMouseAbsolutePointerDevice != NULL);
164
165 UsbMouseAbsolutePointerDevice->UsbIo = UsbIo;
166 UsbMouseAbsolutePointerDevice->Signature = USB_MOUSE_ABSOLUTE_POINTER_DEV_SIGNATURE;
167
168 //
169 // Get the Device Path Protocol on Controller's handle
170 //
171 Status = gBS->OpenProtocol (
172 Controller,
173 &gEfiDevicePathProtocolGuid,
174 (VOID **)&UsbMouseAbsolutePointerDevice->DevicePath,
175 This->DriverBindingHandle,
176 Controller,
177 EFI_OPEN_PROTOCOL_GET_PROTOCOL
178 );
179
180 if (EFI_ERROR (Status)) {
181 goto ErrorExit;
182 }
183
184 //
185 // Report Status Code here since USB mouse will be detected next.
186 //
187 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
188 EFI_PROGRESS_CODE,
189 (EFI_PERIPHERAL_MOUSE | EFI_P_PC_PRESENCE_DETECT),
190 UsbMouseAbsolutePointerDevice->DevicePath
191 );
192
193 //
194 // Get interface & endpoint descriptor
195 //
196 UsbIo->UsbGetInterfaceDescriptor (
197 UsbIo,
198 &UsbMouseAbsolutePointerDevice->InterfaceDescriptor
199 );
200
201 EndpointNumber = UsbMouseAbsolutePointerDevice->InterfaceDescriptor.NumEndpoints;
202
203 //
204 // Traverse endpoints to find interrupt endpoint IN
205 //
206 Found = FALSE;
207 for (Index = 0; Index < EndpointNumber; Index++) {
208 UsbIo->UsbGetEndpointDescriptor (
209 UsbIo,
210 Index,
211 &EndpointDescriptor
212 );
213
214 if (((EndpointDescriptor.Attributes & (BIT0 | BIT1)) == USB_ENDPOINT_INTERRUPT) &&
215 ((EndpointDescriptor.EndpointAddress & USB_ENDPOINT_DIR_IN) != 0))
216 {
217 //
218 // We only care interrupt endpoint here
219 //
220 CopyMem (&UsbMouseAbsolutePointerDevice->IntEndpointDescriptor, &EndpointDescriptor, sizeof (EndpointDescriptor));
221 Found = TRUE;
222 break;
223 }
224 }
225
226 if (!Found) {
227 //
228 // Report Status Code to indicate that there is no USB mouse
229 //
230 REPORT_STATUS_CODE (
231 EFI_ERROR_CODE | EFI_ERROR_MINOR,
232 (EFI_PERIPHERAL_MOUSE | EFI_P_EC_NOT_DETECTED)
233 );
234 //
235 // No interrupt endpoint found, then return unsupported.
236 //
237 Status = EFI_UNSUPPORTED;
238 goto ErrorExit;
239 }
240
241 //
242 // Report Status Code here since USB mouse has be detected.
243 //
244 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
245 EFI_PROGRESS_CODE,
246 (EFI_PERIPHERAL_MOUSE | EFI_P_PC_DETECTED),
247 UsbMouseAbsolutePointerDevice->DevicePath
248 );
249
250 Status = InitializeUsbMouseDevice (UsbMouseAbsolutePointerDevice);
251 if (EFI_ERROR (Status)) {
252 //
253 // Fail to initialize USB mouse device.
254 //
255 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
256 EFI_ERROR_CODE | EFI_ERROR_MINOR,
257 (EFI_PERIPHERAL_MOUSE | EFI_P_EC_INTERFACE_ERROR),
258 UsbMouseAbsolutePointerDevice->DevicePath
259 );
260
261 goto ErrorExit;
262 }
263
264 //
265 // Initialize and install EFI Absolute Pointer Protocol.
266 //
267 UsbMouseAbsolutePointerDevice->AbsolutePointerProtocol.GetState = GetMouseAbsolutePointerState;
268 UsbMouseAbsolutePointerDevice->AbsolutePointerProtocol.Reset = UsbMouseAbsolutePointerReset;
269 UsbMouseAbsolutePointerDevice->AbsolutePointerProtocol.Mode = &UsbMouseAbsolutePointerDevice->Mode;
270
271 Status = gBS->CreateEvent (
272 EVT_NOTIFY_WAIT,
273 TPL_NOTIFY,
274 UsbMouseAbsolutePointerWaitForInput,
275 UsbMouseAbsolutePointerDevice,
276 &((UsbMouseAbsolutePointerDevice->AbsolutePointerProtocol).WaitForInput)
277 );
278 if (EFI_ERROR (Status)) {
279 goto ErrorExit;
280 }
281
282 Status = gBS->InstallProtocolInterface (
283 &Controller,
284 &gEfiAbsolutePointerProtocolGuid,
285 EFI_NATIVE_INTERFACE,
286 &UsbMouseAbsolutePointerDevice->AbsolutePointerProtocol
287 );
288
289 if (EFI_ERROR (Status)) {
290 goto ErrorExit;
291 }
292
293 //
294 // The next step would be submitting Asynchronous Interrupt Transfer on this mouse device.
295 // After that we will be able to get key data from it. Thus this is deemed as
296 // the enable action of the mouse, so report status code accordingly.
297 //
298 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
299 EFI_PROGRESS_CODE,
300 (EFI_PERIPHERAL_MOUSE | EFI_P_PC_ENABLE),
301 UsbMouseAbsolutePointerDevice->DevicePath
302 );
303
304 //
305 // Submit Asynchronous Interrupt Transfer to manage this device.
306 //
307 EndpointAddr = UsbMouseAbsolutePointerDevice->IntEndpointDescriptor.EndpointAddress;
308 PollingInterval = UsbMouseAbsolutePointerDevice->IntEndpointDescriptor.Interval;
309 PacketSize = (UINT8)(UsbMouseAbsolutePointerDevice->IntEndpointDescriptor.MaxPacketSize);
310
311 Status = UsbIo->UsbAsyncInterruptTransfer (
312 UsbIo,
313 EndpointAddr,
314 TRUE,
315 PollingInterval,
316 PacketSize,
317 OnMouseInterruptComplete,
318 UsbMouseAbsolutePointerDevice
319 );
320
321 if (EFI_ERROR (Status)) {
322 //
323 // If submit error, uninstall that interface
324 //
325 gBS->UninstallProtocolInterface (
326 Controller,
327 &gEfiAbsolutePointerProtocolGuid,
328 &UsbMouseAbsolutePointerDevice->AbsolutePointerProtocol
329 );
330 goto ErrorExit;
331 }
332
333 UsbMouseAbsolutePointerDevice->ControllerNameTable = NULL;
334 AddUnicodeString2 (
335 "eng",
336 gUsbMouseAbsolutePointerComponentName.SupportedLanguages,
337 &UsbMouseAbsolutePointerDevice->ControllerNameTable,
338 L"Generic Usb Mouse Absolute Pointer",
339 TRUE
340 );
341 AddUnicodeString2 (
342 "en",
343 gUsbMouseAbsolutePointerComponentName2.SupportedLanguages,
344 &UsbMouseAbsolutePointerDevice->ControllerNameTable,
345 L"Generic Usb Mouse Absolute Pointer",
346 FALSE
347 );
348
349 gBS->RestoreTPL (OldTpl);
350 return EFI_SUCCESS;
351
352 //
353 // Error handler
354 //
355 ErrorExit:
356 if (EFI_ERROR (Status)) {
357 gBS->CloseProtocol (
358 Controller,
359 &gEfiUsbIoProtocolGuid,
360 This->DriverBindingHandle,
361 Controller
362 );
363
364 if (UsbMouseAbsolutePointerDevice != NULL) {
365 if ((UsbMouseAbsolutePointerDevice->AbsolutePointerProtocol).WaitForInput != NULL) {
366 gBS->CloseEvent ((UsbMouseAbsolutePointerDevice->AbsolutePointerProtocol).WaitForInput);
367 }
368
369 FreePool (UsbMouseAbsolutePointerDevice);
370 UsbMouseAbsolutePointerDevice = NULL;
371 }
372 }
373
374 ErrorExit1:
375 gBS->RestoreTPL (OldTpl);
376
377 return Status;
378 }
379
380 /**
381 Stop the USB mouse device handled by this driver.
382
383 @param This The driver binding protocol.
384 @param Controller The controller to release.
385 @param NumberOfChildren The number of handles in ChildHandleBuffer.
386 @param ChildHandleBuffer The array of child handle.
387
388 @retval EFI_SUCCESS The device was stopped.
389 @retval EFI_UNSUPPORTED Absolute Pointer Protocol is not installed on Controller.
390 @retval Others Fail to uninstall protocols attached on the device.
391
392 **/
393 EFI_STATUS
394 EFIAPI
395 USBMouseAbsolutePointerDriverBindingStop (
396 IN EFI_DRIVER_BINDING_PROTOCOL *This,
397 IN EFI_HANDLE Controller,
398 IN UINTN NumberOfChildren,
399 IN EFI_HANDLE *ChildHandleBuffer
400 )
401 {
402 EFI_STATUS Status;
403 USB_MOUSE_ABSOLUTE_POINTER_DEV *UsbMouseAbsolutePointerDevice;
404 EFI_ABSOLUTE_POINTER_PROTOCOL *AbsolutePointerProtocol;
405 EFI_USB_IO_PROTOCOL *UsbIo;
406
407 Status = gBS->OpenProtocol (
408 Controller,
409 &gEfiAbsolutePointerProtocolGuid,
410 (VOID **)&AbsolutePointerProtocol,
411 This->DriverBindingHandle,
412 Controller,
413 EFI_OPEN_PROTOCOL_GET_PROTOCOL
414 );
415
416 if (EFI_ERROR (Status)) {
417 return EFI_UNSUPPORTED;
418 }
419
420 UsbMouseAbsolutePointerDevice = USB_MOUSE_ABSOLUTE_POINTER_DEV_FROM_MOUSE_PROTOCOL (AbsolutePointerProtocol);
421
422 UsbIo = UsbMouseAbsolutePointerDevice->UsbIo;
423
424 //
425 // The key data input from this device will be disabled.
426 //
427 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
428 EFI_PROGRESS_CODE,
429 (EFI_PERIPHERAL_MOUSE | EFI_P_PC_DISABLE),
430 UsbMouseAbsolutePointerDevice->DevicePath
431 );
432
433 //
434 // Delete the Asynchronous Interrupt Transfer from this device
435 //
436 UsbIo->UsbAsyncInterruptTransfer (
437 UsbIo,
438 UsbMouseAbsolutePointerDevice->IntEndpointDescriptor.EndpointAddress,
439 FALSE,
440 UsbMouseAbsolutePointerDevice->IntEndpointDescriptor.Interval,
441 0,
442 NULL,
443 NULL
444 );
445
446 Status = gBS->UninstallProtocolInterface (
447 Controller,
448 &gEfiAbsolutePointerProtocolGuid,
449 &UsbMouseAbsolutePointerDevice->AbsolutePointerProtocol
450 );
451 if (EFI_ERROR (Status)) {
452 return Status;
453 }
454
455 gBS->CloseProtocol (
456 Controller,
457 &gEfiUsbIoProtocolGuid,
458 This->DriverBindingHandle,
459 Controller
460 );
461
462 //
463 // Free all resources.
464 //
465 gBS->CloseEvent (UsbMouseAbsolutePointerDevice->AbsolutePointerProtocol.WaitForInput);
466
467 if (UsbMouseAbsolutePointerDevice->DelayedRecoveryEvent != NULL) {
468 gBS->CloseEvent (UsbMouseAbsolutePointerDevice->DelayedRecoveryEvent);
469 UsbMouseAbsolutePointerDevice->DelayedRecoveryEvent = NULL;
470 }
471
472 if (UsbMouseAbsolutePointerDevice->ControllerNameTable != NULL) {
473 FreeUnicodeStringTable (UsbMouseAbsolutePointerDevice->ControllerNameTable);
474 }
475
476 FreePool (UsbMouseAbsolutePointerDevice);
477
478 return EFI_SUCCESS;
479 }
480
481 /**
482 Uses USB I/O to check whether the device is a USB mouse device.
483
484 @param UsbIo Pointer to a USB I/O protocol instance.
485
486 @retval TRUE Device is a USB mouse device.
487 @retval FALSE Device is a not USB mouse device.
488
489 **/
490 BOOLEAN
491 IsUsbMouse (
492 IN EFI_USB_IO_PROTOCOL *UsbIo
493 )
494 {
495 EFI_STATUS Status;
496 EFI_USB_INTERFACE_DESCRIPTOR InterfaceDescriptor;
497
498 //
499 // Get the default interface descriptor
500 //
501 Status = UsbIo->UsbGetInterfaceDescriptor (
502 UsbIo,
503 &InterfaceDescriptor
504 );
505
506 if (EFI_ERROR (Status)) {
507 return FALSE;
508 }
509
510 if ((InterfaceDescriptor.InterfaceClass == CLASS_HID) &&
511 (InterfaceDescriptor.InterfaceSubClass == SUBCLASS_BOOT) &&
512 (InterfaceDescriptor.InterfaceProtocol == PROTOCOL_MOUSE)
513 )
514 {
515 return TRUE;
516 }
517
518 return FALSE;
519 }
520
521 /**
522 Initialize the USB mouse device.
523
524 This function retrieves and parses HID report descriptor, and
525 initializes state of USB_MOUSE_ABSOLUTE_POINTER_DEV. Then it sets indefinite idle
526 rate for the device. Finally it creates event for delayed recovery,
527 which deals with device error.
528
529 @param UsbMouseAbsolutePointerDev Device instance to be initialized.
530
531 @retval EFI_SUCCESS USB mouse device successfully initialized.
532 @retval EFI_UNSUPPORTED HID descriptor type is not report descriptor.
533 @retval Other USB mouse device was not initialized successfully.
534
535 **/
536 EFI_STATUS
537 InitializeUsbMouseDevice (
538 IN USB_MOUSE_ABSOLUTE_POINTER_DEV *UsbMouseAbsolutePointerDev
539 )
540 {
541 EFI_USB_IO_PROTOCOL *UsbIo;
542 UINT8 Protocol;
543 EFI_STATUS Status;
544 EFI_USB_HID_DESCRIPTOR *MouseHidDesc;
545 UINT8 *ReportDesc;
546 EFI_USB_CONFIG_DESCRIPTOR ConfigDesc;
547 VOID *Buf;
548 UINT32 TransferResult;
549 UINT16 Total;
550 USB_DESC_HEAD *Head;
551 BOOLEAN Start;
552
553 UsbIo = UsbMouseAbsolutePointerDev->UsbIo;
554
555 //
556 // Get the current configuration descriptor. Note that it doesn't include other descriptors.
557 //
558 Status = UsbIo->UsbGetConfigDescriptor (
559 UsbIo,
560 &ConfigDesc
561 );
562 if (EFI_ERROR (Status)) {
563 return Status;
564 }
565
566 //
567 // By issuing Get_Descriptor(Configuration) request with total length, we get the Configuration descriptor,
568 // all Interface descriptors, all Endpoint descriptors, and the HID descriptor for each interface.
569 //
570 Buf = AllocateZeroPool (ConfigDesc.TotalLength);
571 if (Buf == NULL) {
572 return EFI_OUT_OF_RESOURCES;
573 }
574
575 Status = UsbGetDescriptor (
576 UsbIo,
577 (UINT16)((USB_DESC_TYPE_CONFIG << 8) | (ConfigDesc.ConfigurationValue - 1)),
578 0,
579 ConfigDesc.TotalLength,
580 Buf,
581 &TransferResult
582 );
583 if (EFI_ERROR (Status)) {
584 FreePool (Buf);
585 return Status;
586 }
587
588 Total = 0;
589 Start = FALSE;
590 Head = (USB_DESC_HEAD *)Buf;
591 MouseHidDesc = NULL;
592
593 //
594 // Get HID descriptor from the receipt of Get_Descriptor(Configuration) request.
595 // This algorithm is based on the fact that the HID descriptor shall be interleaved
596 // between the interface and endpoint descriptors for HID interfaces.
597 //
598 while (Total < ConfigDesc.TotalLength) {
599 if (Head->Type == USB_DESC_TYPE_INTERFACE) {
600 if ((((USB_INTERFACE_DESCRIPTOR *)Head)->InterfaceNumber == UsbMouseAbsolutePointerDev->InterfaceDescriptor.InterfaceNumber) &&
601 (((USB_INTERFACE_DESCRIPTOR *)Head)->AlternateSetting == UsbMouseAbsolutePointerDev->InterfaceDescriptor.AlternateSetting))
602 {
603 Start = TRUE;
604 }
605 }
606
607 if (Start && (Head->Type == USB_DESC_TYPE_ENDPOINT)) {
608 break;
609 }
610
611 if (Start && (Head->Type == USB_DESC_TYPE_HID)) {
612 MouseHidDesc = (EFI_USB_HID_DESCRIPTOR *)Head;
613 break;
614 }
615
616 Total = Total + (UINT16)Head->Len;
617 Head = (USB_DESC_HEAD *)((UINT8 *)Buf + Total);
618 }
619
620 if (MouseHidDesc == NULL) {
621 FreePool (Buf);
622 return EFI_UNSUPPORTED;
623 }
624
625 //
626 // Get report descriptor
627 //
628 if (MouseHidDesc->HidClassDesc[0].DescriptorType != USB_DESC_TYPE_REPORT) {
629 FreePool (Buf);
630 return EFI_UNSUPPORTED;
631 }
632
633 ReportDesc = AllocateZeroPool (MouseHidDesc->HidClassDesc[0].DescriptorLength);
634 ASSERT (ReportDesc != NULL);
635
636 Status = UsbGetReportDescriptor (
637 UsbIo,
638 UsbMouseAbsolutePointerDev->InterfaceDescriptor.InterfaceNumber,
639 MouseHidDesc->HidClassDesc[0].DescriptorLength,
640 ReportDesc
641 );
642
643 if (EFI_ERROR (Status)) {
644 FreePool (Buf);
645 FreePool (ReportDesc);
646 return Status;
647 }
648
649 //
650 // Parse report descriptor
651 //
652 Status = ParseMouseReportDescriptor (
653 UsbMouseAbsolutePointerDev,
654 ReportDesc,
655 MouseHidDesc->HidClassDesc[0].DescriptorLength
656 );
657
658 if (EFI_ERROR (Status)) {
659 FreePool (Buf);
660 FreePool (ReportDesc);
661 return Status;
662 }
663
664 UsbMouseAbsolutePointerDev->Mode.AbsoluteMaxX = 1024;
665 UsbMouseAbsolutePointerDev->Mode.AbsoluteMaxY = 1024;
666 UsbMouseAbsolutePointerDev->Mode.AbsoluteMaxZ = 0;
667 UsbMouseAbsolutePointerDev->Mode.AbsoluteMinX = 0;
668 UsbMouseAbsolutePointerDev->Mode.AbsoluteMinY = 0;
669 UsbMouseAbsolutePointerDev->Mode.AbsoluteMinZ = 0;
670 UsbMouseAbsolutePointerDev->Mode.Attributes = 0x3;
671
672 //
673 // Let the cursor's starting position is in the center of the screen.
674 //
675 UsbMouseAbsolutePointerDev->State.CurrentX =
676 DivU64x32 (UsbMouseAbsolutePointerDev->Mode.AbsoluteMaxX + UsbMouseAbsolutePointerDev->Mode.AbsoluteMinX, 2);
677 UsbMouseAbsolutePointerDev->State.CurrentY =
678 DivU64x32 (UsbMouseAbsolutePointerDev->Mode.AbsoluteMaxY + UsbMouseAbsolutePointerDev->Mode.AbsoluteMinY, 2);
679
680 //
681 // Set boot protocol for the USB mouse.
682 // This driver only supports boot protocol.
683 //
684 UsbGetProtocolRequest (
685 UsbIo,
686 UsbMouseAbsolutePointerDev->InterfaceDescriptor.InterfaceNumber,
687 &Protocol
688 );
689 if (Protocol != BOOT_PROTOCOL) {
690 Status = UsbSetProtocolRequest (
691 UsbIo,
692 UsbMouseAbsolutePointerDev->InterfaceDescriptor.InterfaceNumber,
693 BOOT_PROTOCOL
694 );
695
696 if (EFI_ERROR (Status)) {
697 FreePool (Buf);
698 FreePool (ReportDesc);
699 return Status;
700 }
701 }
702
703 FreePool (Buf);
704 FreePool (ReportDesc);
705
706 //
707 // Create event for delayed recovery, which deals with device error.
708 //
709 if (UsbMouseAbsolutePointerDev->DelayedRecoveryEvent != NULL) {
710 gBS->CloseEvent (UsbMouseAbsolutePointerDev->DelayedRecoveryEvent);
711 UsbMouseAbsolutePointerDev->DelayedRecoveryEvent = 0;
712 }
713
714 gBS->CreateEvent (
715 EVT_TIMER | EVT_NOTIFY_SIGNAL,
716 TPL_NOTIFY,
717 USBMouseRecoveryHandler,
718 UsbMouseAbsolutePointerDev,
719 &UsbMouseAbsolutePointerDev->DelayedRecoveryEvent
720 );
721
722 return EFI_SUCCESS;
723 }
724
725 /**
726 Handler function for USB mouse's asynchronous interrupt transfer.
727
728 This function is the handler function for USB mouse's asynchronous interrupt transfer
729 to manage the mouse. It parses data returned from asynchronous interrupt transfer, and
730 get button and movement state.
731
732 @param Data A pointer to a buffer that is filled with key data which is
733 retrieved via asynchronous interrupt transfer.
734 @param DataLength Indicates the size of the data buffer.
735 @param Context Pointing to USB_KB_DEV instance.
736 @param Result Indicates the result of the asynchronous interrupt transfer.
737
738 @retval EFI_SUCCESS Asynchronous interrupt transfer is handled successfully.
739 @retval EFI_DEVICE_ERROR Hardware error occurs.
740
741 **/
742 EFI_STATUS
743 EFIAPI
744 OnMouseInterruptComplete (
745 IN VOID *Data,
746 IN UINTN DataLength,
747 IN VOID *Context,
748 IN UINT32 Result
749 )
750 {
751 USB_MOUSE_ABSOLUTE_POINTER_DEV *UsbMouseAbsolutePointerDevice;
752 EFI_USB_IO_PROTOCOL *UsbIo;
753 UINT8 EndpointAddr;
754 UINT32 UsbResult;
755
756 UsbMouseAbsolutePointerDevice = (USB_MOUSE_ABSOLUTE_POINTER_DEV *)Context;
757 UsbIo = UsbMouseAbsolutePointerDevice->UsbIo;
758
759 if (Result != EFI_USB_NOERROR) {
760 //
761 // Some errors happen during the process
762 //
763 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
764 EFI_ERROR_CODE | EFI_ERROR_MINOR,
765 (EFI_PERIPHERAL_MOUSE | EFI_P_EC_INPUT_ERROR),
766 UsbMouseAbsolutePointerDevice->DevicePath
767 );
768
769 if ((Result & EFI_USB_ERR_STALL) == EFI_USB_ERR_STALL) {
770 EndpointAddr = UsbMouseAbsolutePointerDevice->IntEndpointDescriptor.EndpointAddress;
771
772 UsbClearEndpointHalt (
773 UsbIo,
774 EndpointAddr,
775 &UsbResult
776 );
777 }
778
779 //
780 // Delete & Submit this interrupt again
781 // Handler of DelayedRecoveryEvent triggered by timer will re-submit the interrupt.
782 //
783 UsbIo->UsbAsyncInterruptTransfer (
784 UsbIo,
785 UsbMouseAbsolutePointerDevice->IntEndpointDescriptor.EndpointAddress,
786 FALSE,
787 0,
788 0,
789 NULL,
790 NULL
791 );
792 //
793 // EFI_USB_INTERRUPT_DELAY is defined in USB standard for error handling.
794 //
795 gBS->SetTimer (
796 UsbMouseAbsolutePointerDevice->DelayedRecoveryEvent,
797 TimerRelative,
798 EFI_USB_INTERRUPT_DELAY
799 );
800 return EFI_DEVICE_ERROR;
801 }
802
803 //
804 // If no error and no data, just return EFI_SUCCESS.
805 //
806 if ((DataLength == 0) || (Data == NULL)) {
807 return EFI_SUCCESS;
808 }
809
810 //
811 // Check mouse Data
812 // USB HID Specification specifies following data format:
813 // Byte Bits Description
814 // 0 0 Button 1
815 // 1 Button 2
816 // 2 Button 3
817 // 4 to 7 Device-specific
818 // 1 0 to 7 X displacement
819 // 2 0 to 7 Y displacement
820 // 3 to n 0 to 7 Device specific (optional)
821 //
822 if (DataLength < 3) {
823 return EFI_DEVICE_ERROR;
824 }
825
826 UsbMouseAbsolutePointerDevice->StateChanged = TRUE;
827
828 UsbMouseAbsolutePointerDevice->State.ActiveButtons = *(UINT8 *)Data & (BIT0 | BIT1 | BIT2);
829
830 UsbMouseAbsolutePointerDevice->State.CurrentX =
831 MIN (
832 MAX (
833 (INT64)UsbMouseAbsolutePointerDevice->State.CurrentX + *((INT8 *)Data + 1),
834 (INT64)UsbMouseAbsolutePointerDevice->Mode.AbsoluteMinX
835 ),
836 (INT64)UsbMouseAbsolutePointerDevice->Mode.AbsoluteMaxX
837 );
838 UsbMouseAbsolutePointerDevice->State.CurrentY =
839 MIN (
840 MAX (
841 (INT64)UsbMouseAbsolutePointerDevice->State.CurrentY + *((INT8 *)Data + 2),
842 (INT64)UsbMouseAbsolutePointerDevice->Mode.AbsoluteMinY
843 ),
844 (INT64)UsbMouseAbsolutePointerDevice->Mode.AbsoluteMaxY
845 );
846 if (DataLength > 3) {
847 UsbMouseAbsolutePointerDevice->State.CurrentZ =
848 MIN (
849 MAX (
850 (INT64)UsbMouseAbsolutePointerDevice->State.CurrentZ + *((INT8 *)Data + 1),
851 (INT64)UsbMouseAbsolutePointerDevice->Mode.AbsoluteMinZ
852 ),
853 (INT64)UsbMouseAbsolutePointerDevice->Mode.AbsoluteMaxZ
854 );
855 }
856
857 return EFI_SUCCESS;
858 }
859
860 /**
861 Retrieves the current state of a pointer device.
862
863 @param This A pointer to the EFI_ABSOLUTE_POINTER_PROTOCOL instance.
864 @param MouseState A pointer to the state information on the pointer device.
865
866 @retval EFI_SUCCESS The state of the pointer device was returned in State.
867 @retval EFI_NOT_READY The state of the pointer device has not changed since the last call to
868 GetState().
869 @retval EFI_DEVICE_ERROR A device error occurred while attempting to retrieve the pointer device's
870 current state.
871 @retval EFI_INVALID_PARAMETER State is NULL.
872
873 **/
874 EFI_STATUS
875 EFIAPI
876 GetMouseAbsolutePointerState (
877 IN EFI_ABSOLUTE_POINTER_PROTOCOL *This,
878 OUT EFI_ABSOLUTE_POINTER_STATE *State
879 )
880 {
881 USB_MOUSE_ABSOLUTE_POINTER_DEV *MouseAbsolutePointerDev;
882
883 if (State == NULL) {
884 return EFI_INVALID_PARAMETER;
885 }
886
887 MouseAbsolutePointerDev = USB_MOUSE_ABSOLUTE_POINTER_DEV_FROM_MOUSE_PROTOCOL (This);
888
889 if (!MouseAbsolutePointerDev->StateChanged) {
890 return EFI_NOT_READY;
891 }
892
893 //
894 // Retrieve mouse state from USB_MOUSE_ABSOLUTE_POINTER_DEV,
895 // which was filled by OnMouseInterruptComplete()
896 //
897 CopyMem (
898 State,
899 &MouseAbsolutePointerDev->State,
900 sizeof (EFI_ABSOLUTE_POINTER_STATE)
901 );
902
903 MouseAbsolutePointerDev->StateChanged = FALSE;
904
905 return EFI_SUCCESS;
906 }
907
908 /**
909 Resets the pointer device hardware.
910
911 @param This A pointer to the EFI_ABSOLUTE_POINTER_PROTOCOL instance.
912 @param ExtendedVerification Indicates that the driver may perform a more exhaustive
913 verification operation of the device during reset.
914
915 @retval EFI_SUCCESS The device was reset.
916 @retval EFI_DEVICE_ERROR The device is not functioning correctly and could not be reset.
917
918 **/
919 EFI_STATUS
920 EFIAPI
921 UsbMouseAbsolutePointerReset (
922 IN EFI_ABSOLUTE_POINTER_PROTOCOL *This,
923 IN BOOLEAN ExtendedVerification
924 )
925 {
926 USB_MOUSE_ABSOLUTE_POINTER_DEV *UsbMouseAbsolutePointerDevice;
927
928 UsbMouseAbsolutePointerDevice = USB_MOUSE_ABSOLUTE_POINTER_DEV_FROM_MOUSE_PROTOCOL (This);
929
930 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
931 EFI_PROGRESS_CODE,
932 (EFI_PERIPHERAL_MOUSE | EFI_P_PC_RESET),
933 UsbMouseAbsolutePointerDevice->DevicePath
934 );
935
936 //
937 // Clear mouse state.
938 //
939 ZeroMem (
940 &UsbMouseAbsolutePointerDevice->State,
941 sizeof (EFI_ABSOLUTE_POINTER_STATE)
942 );
943
944 //
945 // Let the cursor's starting position is in the center of the screen.
946 //
947 UsbMouseAbsolutePointerDevice->State.CurrentX =
948 DivU64x32 (UsbMouseAbsolutePointerDevice->Mode.AbsoluteMaxX + UsbMouseAbsolutePointerDevice->Mode.AbsoluteMinX, 2);
949 UsbMouseAbsolutePointerDevice->State.CurrentY =
950 DivU64x32 (UsbMouseAbsolutePointerDevice->Mode.AbsoluteMaxY + UsbMouseAbsolutePointerDevice->Mode.AbsoluteMinY, 2);
951
952 UsbMouseAbsolutePointerDevice->StateChanged = FALSE;
953
954 return EFI_SUCCESS;
955 }
956
957 /**
958 Event notification function for EFI_ABSOLUTE_POINTER_PROTOCOL.WaitForInput event.
959
960 @param Event Event to be signaled when there's input from mouse.
961 @param Context Points to USB_MOUSE_ABSOLUTE_POINTER_DEV instance.
962
963 **/
964 VOID
965 EFIAPI
966 UsbMouseAbsolutePointerWaitForInput (
967 IN EFI_EVENT Event,
968 IN VOID *Context
969 )
970 {
971 USB_MOUSE_ABSOLUTE_POINTER_DEV *UsbMouseAbsolutePointerDev;
972
973 UsbMouseAbsolutePointerDev = (USB_MOUSE_ABSOLUTE_POINTER_DEV *)Context;
974
975 //
976 // If there's input from mouse, signal the event.
977 //
978 if (UsbMouseAbsolutePointerDev->StateChanged) {
979 gBS->SignalEvent (Event);
980 }
981 }
982
983 /**
984 Handler for Delayed Recovery event.
985
986 This function is the handler for Delayed Recovery event triggered
987 by timer.
988 After a device error occurs, the event would be triggered
989 with interval of EFI_USB_INTERRUPT_DELAY. EFI_USB_INTERRUPT_DELAY
990 is defined in USB standard for error handling.
991
992 @param Event The Delayed Recovery event.
993 @param Context Points to the USB_MOUSE_ABSOLUTE_POINTER_DEV instance.
994
995 **/
996 VOID
997 EFIAPI
998 USBMouseRecoveryHandler (
999 IN EFI_EVENT Event,
1000 IN VOID *Context
1001 )
1002 {
1003 USB_MOUSE_ABSOLUTE_POINTER_DEV *UsbMouseAbsolutePointerDev;
1004 EFI_USB_IO_PROTOCOL *UsbIo;
1005
1006 UsbMouseAbsolutePointerDev = (USB_MOUSE_ABSOLUTE_POINTER_DEV *)Context;
1007
1008 UsbIo = UsbMouseAbsolutePointerDev->UsbIo;
1009
1010 //
1011 // Re-submit Asynchronous Interrupt Transfer for recovery.
1012 //
1013 UsbIo->UsbAsyncInterruptTransfer (
1014 UsbIo,
1015 UsbMouseAbsolutePointerDev->IntEndpointDescriptor.EndpointAddress,
1016 TRUE,
1017 UsbMouseAbsolutePointerDev->IntEndpointDescriptor.Interval,
1018 UsbMouseAbsolutePointerDev->IntEndpointDescriptor.MaxPacketSize,
1019 OnMouseInterruptComplete,
1020 UsbMouseAbsolutePointerDev
1021 );
1022 }