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