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