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