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