]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Bus/Usb/UsbMouseAbsolutePointerDxe/UsbMouseAbsolutePointer.c
MdeMdeModulePkg/Usb: Eliminated the use of Set_Idle request on usb modules.
[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
0309b719 4Copyright (c) 2004 - 2012, Intel Corporation. All rights reserved.<BR>\r
cd5ebaa0 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
0309b719 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
0309b719 529 EFI_USB_CONFIG_DESCRIPTOR ConfigDesc;\r
530 VOID *Buf;\r
531 UINT32 TransferResult;\r
532 UINT16 Total;\r
533 USB_DESC_HEAD *Head;\r
534 BOOLEAN Start;\r
09f72ae8 535\r
536 UsbIo = UsbMouseAbsolutePointerDev->UsbIo;\r
537\r
538 //\r
0309b719 539 // Get the current configuration descriptor. Note that it doesn't include other descriptors.\r
09f72ae8 540 //\r
0309b719 541 Status = UsbIo->UsbGetConfigDescriptor (\r
542 UsbIo,\r
543 &ConfigDesc\r
544 );\r
545 if (EFI_ERROR (Status)) {\r
546 return Status;\r
547 }\r
548\r
549 //\r
550 // By issuing Get_Descriptor(Configuration) request with total length, we get the Configuration descriptor,\r
551 // all Interface descriptors, all Endpoint descriptors, and the HID descriptor for each interface.\r
552 //\r
553 Buf = AllocateZeroPool (ConfigDesc.TotalLength);\r
554 if (Buf == NULL) {\r
555 return EFI_OUT_OF_RESOURCES;\r
556 }\r
557\r
558 Status = UsbGetDescriptor (\r
67b8a9ce 559 UsbIo,\r
0309b719 560 (UINT16)((USB_DESC_TYPE_CONFIG << 8) | (ConfigDesc.ConfigurationValue - 1)),\r
561 0,\r
562 ConfigDesc.TotalLength,\r
563 Buf,\r
564 &TransferResult\r
67b8a9ce 565 );\r
09f72ae8 566 if (EFI_ERROR (Status)) {\r
0309b719 567 FreePool (Buf);\r
09f72ae8 568 return Status;\r
569 }\r
570\r
0309b719 571 Total = 0;\r
572 Start = FALSE;\r
573 Head = (USB_DESC_HEAD *)Buf; \r
574 MouseHidDesc = NULL;\r
575\r
576 //\r
577 // Get HID descriptor from the receipt of Get_Descriptor(Configuration) request.\r
578 // This algorithm is based on the fact that the HID descriptor shall be interleaved\r
579 // between the interface and endpoint descriptors for HID interfaces.\r
580 //\r
581 while (Total < ConfigDesc.TotalLength) {\r
582 if (Head->Type == USB_DESC_TYPE_INTERFACE) {\r
583 if ((((USB_INTERFACE_DESCRIPTOR *)Head)->InterfaceNumber == UsbMouseAbsolutePointerDev->InterfaceDescriptor.InterfaceNumber) &&\r
584 (((USB_INTERFACE_DESCRIPTOR *)Head)->AlternateSetting == UsbMouseAbsolutePointerDev->InterfaceDescriptor.AlternateSetting)) {\r
585 Start = TRUE;\r
586 }\r
587 }\r
af3a71b8 588 if (Start && (Head->Type == USB_DESC_TYPE_ENDPOINT)) {\r
0309b719 589 break;\r
590 }\r
af3a71b8 591 if (Start && (Head->Type == USB_DESC_TYPE_HID)) {\r
0309b719 592 MouseHidDesc = (EFI_USB_HID_DESCRIPTOR *)Head;\r
593 break;\r
594 }\r
ab742719 595 Total = Total + (UINT16)Head->Len;\r
596 Head = (USB_DESC_HEAD*)((UINT8 *)Buf + Total);\r
0309b719 597 }\r
598\r
599 if (MouseHidDesc == NULL) {\r
600 FreePool (Buf);\r
601 return EFI_UNSUPPORTED;\r
602 }\r
603\r
09f72ae8 604 //\r
67b8a9ce 605 // Get report descriptor\r
09f72ae8 606 //\r
0309b719 607 if (MouseHidDesc->HidClassDesc[0].DescriptorType != USB_DESC_TYPE_REPORT) {\r
608 FreePool (Buf);\r
09f72ae8 609 return EFI_UNSUPPORTED;\r
610 }\r
611\r
0309b719 612 ReportDesc = AllocateZeroPool (MouseHidDesc->HidClassDesc[0].DescriptorLength);\r
67b8a9ce 613 ASSERT (ReportDesc != NULL);\r
09f72ae8 614\r
615 Status = UsbGetReportDescriptor (\r
67b8a9ce 616 UsbIo,\r
617 UsbMouseAbsolutePointerDev->InterfaceDescriptor.InterfaceNumber,\r
0309b719 618 MouseHidDesc->HidClassDesc[0].DescriptorLength,\r
67b8a9ce 619 ReportDesc\r
620 );\r
09f72ae8 621\r
622 if (EFI_ERROR (Status)) {\r
0309b719 623 FreePool (Buf);\r
67b8a9ce 624 FreePool (ReportDesc);\r
09f72ae8 625 return Status;\r
626 }\r
627\r
628 //\r
629 // Parse report descriptor\r
630 //\r
631 Status = ParseMouseReportDescriptor (\r
67b8a9ce 632 UsbMouseAbsolutePointerDev,\r
633 ReportDesc,\r
0309b719 634 MouseHidDesc->HidClassDesc[0].DescriptorLength\r
67b8a9ce 635 );\r
09f72ae8 636\r
637 if (EFI_ERROR (Status)) {\r
0309b719 638 FreePool (Buf);\r
67b8a9ce 639 FreePool (ReportDesc);\r
09f72ae8 640 return Status;\r
641 }\r
642\r
67b8a9ce 643 UsbMouseAbsolutePointerDev->Mode.AbsoluteMaxX = 1024;\r
644 UsbMouseAbsolutePointerDev->Mode.AbsoluteMaxY = 1024;\r
645 UsbMouseAbsolutePointerDev->Mode.AbsoluteMaxZ = 0;\r
646 UsbMouseAbsolutePointerDev->Mode.AbsoluteMinX = 0;\r
647 UsbMouseAbsolutePointerDev->Mode.AbsoluteMinY = 0;\r
648 UsbMouseAbsolutePointerDev->Mode.AbsoluteMinZ = 0;\r
649 UsbMouseAbsolutePointerDev->Mode.Attributes = 0x3;\r
09f72ae8 650 \r
651 //\r
67b8a9ce 652 // Set boot protocol for the USB mouse.\r
653 // This driver only supports boot protocol.\r
09f72ae8 654 //\r
655 UsbGetProtocolRequest (\r
656 UsbIo,\r
67b8a9ce 657 UsbMouseAbsolutePointerDev->InterfaceDescriptor.InterfaceNumber,\r
09f72ae8 658 &Protocol\r
659 );\r
09f72ae8 660 if (Protocol != BOOT_PROTOCOL) {\r
661 Status = UsbSetProtocolRequest (\r
67b8a9ce 662 UsbIo,\r
83040701 663 UsbMouseAbsolutePointerDev->InterfaceDescriptor.InterfaceNumber,\r
67b8a9ce 664 BOOT_PROTOCOL\r
665 );\r
09f72ae8 666\r
667 if (EFI_ERROR (Status)) {\r
0309b719 668 FreePool (Buf);\r
67b8a9ce 669 FreePool (ReportDesc);\r
670 return Status;\r
09f72ae8 671 }\r
672 }\r
673\r
0309b719 674 FreePool (Buf);\r
67b8a9ce 675 FreePool (ReportDesc);\r
09f72ae8 676\r
67b8a9ce 677 //\r
678 // Create event for delayed recovery, which deals with device error.\r
679 //\r
680 if (UsbMouseAbsolutePointerDev->DelayedRecoveryEvent != NULL) {\r
09f72ae8 681 gBS->CloseEvent (UsbMouseAbsolutePointerDev->DelayedRecoveryEvent);\r
682 UsbMouseAbsolutePointerDev->DelayedRecoveryEvent = 0;\r
683 }\r
684\r
67b8a9ce 685 gBS->CreateEvent (\r
686 EVT_TIMER | EVT_NOTIFY_SIGNAL,\r
687 TPL_NOTIFY,\r
688 USBMouseRecoveryHandler,\r
689 UsbMouseAbsolutePointerDev,\r
690 &UsbMouseAbsolutePointerDev->DelayedRecoveryEvent\r
691 );\r
09f72ae8 692\r
693 return EFI_SUCCESS;\r
694}\r
695\r
696\r
697/**\r
67b8a9ce 698 Handler function for USB mouse's asynchronous interrupt transfer.\r
699\r
700 This function is the handler function for USB mouse's asynchronous interrupt transfer\r
701 to manage the mouse. It parses data returned from asynchronous interrupt transfer, and\r
702 get button and movement state.\r
09f72ae8 703\r
67b8a9ce 704 @param Data A pointer to a buffer that is filled with key data which is\r
705 retrieved via asynchronous interrupt transfer.\r
706 @param DataLength Indicates the size of the data buffer.\r
707 @param Context Pointing to USB_KB_DEV instance.\r
708 @param Result Indicates the result of the asynchronous interrupt transfer.\r
09f72ae8 709\r
67b8a9ce 710 @retval EFI_SUCCESS Asynchronous interrupt transfer is handled successfully.\r
711 @retval EFI_DEVICE_ERROR Hardware error occurs.\r
09f72ae8 712\r
713**/\r
09f72ae8 714EFI_STATUS\r
715EFIAPI\r
67b8a9ce 716OnMouseInterruptComplete (\r
09f72ae8 717 IN VOID *Data,\r
718 IN UINTN DataLength,\r
719 IN VOID *Context,\r
720 IN UINT32 Result\r
721 )\r
722{\r
67b8a9ce 723 USB_MOUSE_ABSOLUTE_POINTER_DEV *UsbMouseAbsolutePointerDevice;\r
724 EFI_USB_IO_PROTOCOL *UsbIo;\r
725 UINT8 EndpointAddr;\r
726 UINT32 UsbResult;\r
09f72ae8 727\r
728 UsbMouseAbsolutePointerDevice = (USB_MOUSE_ABSOLUTE_POINTER_DEV *) Context;\r
67b8a9ce 729 UsbIo = UsbMouseAbsolutePointerDevice->UsbIo;\r
09f72ae8 730\r
731 if (Result != EFI_USB_NOERROR) {\r
732 //\r
733 // Some errors happen during the process\r
734 //\r
67b8a9ce 735 REPORT_STATUS_CODE_WITH_DEVICE_PATH (\r
09f72ae8 736 EFI_ERROR_CODE | EFI_ERROR_MINOR,\r
f9876ecf 737 (EFI_PERIPHERAL_MOUSE | EFI_P_EC_INPUT_ERROR),\r
67b8a9ce 738 UsbMouseAbsolutePointerDevice->DevicePath\r
09f72ae8 739 );\r
740\r
741 if ((Result & EFI_USB_ERR_STALL) == EFI_USB_ERR_STALL) {\r
67b8a9ce 742 EndpointAddr = UsbMouseAbsolutePointerDevice->IntEndpointDescriptor.EndpointAddress;\r
09f72ae8 743\r
744 UsbClearEndpointHalt (\r
745 UsbIo,\r
746 EndpointAddr,\r
747 &UsbResult\r
748 );\r
749 }\r
750\r
67b8a9ce 751 //\r
752 // Delete & Submit this interrupt again\r
753 // Handler of DelayedRecoveryEvent triggered by timer will re-submit the interrupt. \r
754 //\r
09f72ae8 755 UsbIo->UsbAsyncInterruptTransfer (\r
67b8a9ce 756 UsbIo,\r
757 UsbMouseAbsolutePointerDevice->IntEndpointDescriptor.EndpointAddress,\r
758 FALSE,\r
759 0,\r
760 0,\r
761 NULL,\r
762 NULL\r
763 );\r
764 //\r
765 // EFI_USB_INTERRUPT_DELAY is defined in USB standard for error handling.\r
766 //\r
09f72ae8 767 gBS->SetTimer (\r
67b8a9ce 768 UsbMouseAbsolutePointerDevice->DelayedRecoveryEvent,\r
769 TimerRelative,\r
770 EFI_USB_INTERRUPT_DELAY\r
771 );\r
09f72ae8 772 return EFI_DEVICE_ERROR;\r
773 }\r
774\r
67b8a9ce 775 //\r
776 // If no error and no data, just return EFI_SUCCESS.\r
777 //\r
09f72ae8 778 if (DataLength == 0 || Data == NULL) {\r
779 return EFI_SUCCESS;\r
780 }\r
781\r
67b8a9ce 782 UsbMouseAbsolutePointerDevice->StateChanged = TRUE;\r
783\r
09f72ae8 784 //\r
67b8a9ce 785 // Check mouse Data\r
786 // USB HID Specification specifies following data format:\r
787 // Byte Bits Description\r
788 // 0 0 Button 1\r
789 // 1 Button 2\r
790 // 2 Button 3\r
791 // 4 to 7 Device-specific\r
792 // 1 0 to 7 X displacement\r
793 // 2 0 to 7 Y displacement\r
794 // 3 to n 0 to 7 Device specific (optional)\r
09f72ae8 795 //\r
67b8a9ce 796 UsbMouseAbsolutePointerDevice->State.CurrentX += *((INT8 *) Data + 1);\r
797 UsbMouseAbsolutePointerDevice->State.CurrentY += *((INT8 *) Data + 2);\r
798 \r
09f72ae8 799 if (DataLength > 3) {\r
67b8a9ce 800 UsbMouseAbsolutePointerDevice->State.CurrentZ += *((INT8 *) Data + 3);\r
09f72ae8 801 }\r
67b8a9ce 802 UsbMouseAbsolutePointerDevice->State.ActiveButtons = *(UINT8 *) Data & (BIT0 | BIT1);\r
09f72ae8 803\r
804 return EFI_SUCCESS;\r
805}\r
806\r
807/**\r
67b8a9ce 808 Retrieves the current state of a pointer device.\r
09f72ae8 809\r
67b8a9ce 810 @param This A pointer to the EFI_ABSOLUTE_POINTER_PROTOCOL instance. \r
811 @param MouseState A pointer to the state information on the pointer device.\r
09f72ae8 812\r
67b8a9ce 813 @retval EFI_SUCCESS The state of the pointer device was returned in State.\r
814 @retval EFI_NOT_READY The state of the pointer device has not changed since the last call to\r
815 GetState(). \r
816 @retval EFI_DEVICE_ERROR A device error occurred while attempting to retrieve the pointer device's\r
817 current state. \r
818 @retval EFI_INVALID_PARAMETER State is NULL. \r
09f72ae8 819\r
820**/\r
09f72ae8 821EFI_STATUS\r
822EFIAPI\r
823GetMouseAbsolutePointerState (\r
824 IN EFI_ABSOLUTE_POINTER_PROTOCOL *This,\r
67b8a9ce 825 OUT EFI_ABSOLUTE_POINTER_STATE *State\r
09f72ae8 826 )\r
827{\r
828 USB_MOUSE_ABSOLUTE_POINTER_DEV *MouseAbsolutePointerDev;\r
829\r
67b8a9ce 830 if (State == NULL) {\r
831 return EFI_INVALID_PARAMETER;\r
09f72ae8 832 }\r
833\r
834 MouseAbsolutePointerDev = USB_MOUSE_ABSOLUTE_POINTER_DEV_FROM_MOUSE_PROTOCOL (This);\r
835\r
67b8a9ce 836 if (!MouseAbsolutePointerDev->StateChanged) {\r
09f72ae8 837 return EFI_NOT_READY;\r
838 }\r
839\r
67b8a9ce 840 //\r
841 // Retrieve mouse state from USB_MOUSE_ABSOLUTE_POINTER_DEV,\r
842 // which was filled by OnMouseInterruptComplete()\r
843 //\r
09f72ae8 844 CopyMem (\r
67b8a9ce 845 State,\r
846 &MouseAbsolutePointerDev->State,\r
09f72ae8 847 sizeof (EFI_ABSOLUTE_POINTER_STATE)\r
848 );\r
849\r
850 //\r
851 // Clear previous move state\r
852 //\r
67b8a9ce 853 MouseAbsolutePointerDev->State.CurrentX = 0;\r
854 MouseAbsolutePointerDev->State.CurrentY = 0;\r
855 MouseAbsolutePointerDev->State.CurrentZ = 0;\r
856 MouseAbsolutePointerDev->State.ActiveButtons = 0;\r
09f72ae8 857\r
67b8a9ce 858 MouseAbsolutePointerDev->StateChanged = FALSE;\r
09f72ae8 859\r
860 return EFI_SUCCESS;\r
861}\r
862\r
863\r
864/**\r
67b8a9ce 865 Resets the pointer device hardware.\r
09f72ae8 866\r
67b8a9ce 867 @param This A pointer to the EFI_ABSOLUTE_POINTER_PROTOCOL instance.\r
868 @param ExtendedVerification Indicates that the driver may perform a more exhaustive\r
869 verification operation of the device during reset.\r
09f72ae8 870\r
67b8a9ce 871 @retval EFI_SUCCESS The device was reset.\r
872 @retval EFI_DEVICE_ERROR The device is not functioning correctly and could not be reset.\r
09f72ae8 873\r
874**/\r
09f72ae8 875EFI_STATUS\r
876EFIAPI\r
877UsbMouseAbsolutePointerReset (\r
67b8a9ce 878 IN EFI_ABSOLUTE_POINTER_PROTOCOL *This,\r
09f72ae8 879 IN BOOLEAN ExtendedVerification\r
880 )\r
881{\r
882 USB_MOUSE_ABSOLUTE_POINTER_DEV *UsbMouseAbsolutePointerDevice;\r
883\r
884 UsbMouseAbsolutePointerDevice = USB_MOUSE_ABSOLUTE_POINTER_DEV_FROM_MOUSE_PROTOCOL (This);\r
885\r
67b8a9ce 886 REPORT_STATUS_CODE_WITH_DEVICE_PATH (\r
09f72ae8 887 EFI_PROGRESS_CODE,\r
f9876ecf 888 (EFI_PERIPHERAL_MOUSE | EFI_P_PC_RESET),\r
67b8a9ce 889 UsbMouseAbsolutePointerDevice->DevicePath\r
09f72ae8 890 );\r
891\r
67b8a9ce 892 //\r
893 // Clear mouse state.\r
894 //\r
09f72ae8 895 ZeroMem (\r
67b8a9ce 896 &UsbMouseAbsolutePointerDevice->State,\r
09f72ae8 897 sizeof (EFI_ABSOLUTE_POINTER_STATE)\r
898 );\r
67b8a9ce 899 UsbMouseAbsolutePointerDevice->StateChanged = FALSE;\r
09f72ae8 900\r
901 return EFI_SUCCESS;\r
902}\r
903\r
904/**\r
67b8a9ce 905 Event notification function for EFI_ABSOLUTE_POINTER_PROTOCOL.WaitForInput event.\r
09f72ae8 906\r
67b8a9ce 907 @param Event Event to be signaled when there's input from mouse.\r
908 @param Context Points to USB_MOUSE_ABSOLUTE_POINTER_DEV instance.\r
09f72ae8 909\r
910**/\r
09f72ae8 911VOID\r
912EFIAPI\r
913UsbMouseAbsolutePointerWaitForInput (\r
914 IN EFI_EVENT Event,\r
915 IN VOID *Context\r
916 )\r
917{\r
918 USB_MOUSE_ABSOLUTE_POINTER_DEV *UsbMouseAbsolutePointerDev;\r
919\r
920 UsbMouseAbsolutePointerDev = (USB_MOUSE_ABSOLUTE_POINTER_DEV *) Context;\r
921\r
922 //\r
67b8a9ce 923 // If there's input from mouse, signal the event.\r
09f72ae8 924 //\r
67b8a9ce 925 if (UsbMouseAbsolutePointerDev->StateChanged) {\r
09f72ae8 926 gBS->SignalEvent (Event);\r
927 }\r
928}\r
929\r
930/**\r
67b8a9ce 931 Handler for Delayed Recovery event.\r
09f72ae8 932\r
67b8a9ce 933 This function is the handler for Delayed Recovery event triggered\r
934 by timer.\r
935 After a device error occurs, the event would be triggered\r
936 with interval of EFI_USB_INTERRUPT_DELAY. EFI_USB_INTERRUPT_DELAY\r
937 is defined in USB standard for error handling.\r
09f72ae8 938\r
67b8a9ce 939 @param Event The Delayed Recovery event.\r
940 @param Context Points to the USB_MOUSE_ABSOLUTE_POINTER_DEV instance.\r
09f72ae8 941\r
942**/\r
943VOID\r
944EFIAPI\r
67b8a9ce 945USBMouseRecoveryHandler (\r
09f72ae8 946 IN EFI_EVENT Event,\r
947 IN VOID *Context\r
948 )\r
949{\r
950 USB_MOUSE_ABSOLUTE_POINTER_DEV *UsbMouseAbsolutePointerDev;\r
67b8a9ce 951 EFI_USB_IO_PROTOCOL *UsbIo;\r
09f72ae8 952\r
953 UsbMouseAbsolutePointerDev = (USB_MOUSE_ABSOLUTE_POINTER_DEV *) Context;\r
954\r
955 UsbIo = UsbMouseAbsolutePointerDev->UsbIo;\r
956\r
67b8a9ce 957 //\r
958 // Re-submit Asynchronous Interrupt Transfer for recovery.\r
959 //\r
09f72ae8 960 UsbIo->UsbAsyncInterruptTransfer (\r
67b8a9ce 961 UsbIo,\r
962 UsbMouseAbsolutePointerDev->IntEndpointDescriptor.EndpointAddress,\r
963 TRUE,\r
964 UsbMouseAbsolutePointerDev->IntEndpointDescriptor.Interval,\r
965 UsbMouseAbsolutePointerDev->IntEndpointDescriptor.MaxPacketSize,\r
966 OnMouseInterruptComplete,\r
967 UsbMouseAbsolutePointerDev\r
968 );\r
09f72ae8 969}\r