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