hc
2024-03-25 edb30157bad0c0001c32b854271ace01d3b9a16a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
/** @file
  Virtual Keyboard driver.
 
  Copyright (c) 2012 - 2020, Intel Corporation. All rights reserved.<BR>
  SPDX-License-Identifier: BSD-2-Clause-Patent
 
**/
 
#include "VirtualKeyboard.h"
 
UINT32         mOrigConOutRow      = 0;
UINT32         mOrigSetupConOutRow = 0;
EFI_HII_HANDLE mHiiHandle          = NULL;
/**
  Verify the controller type
 
  This routine determines if the pointer and GOP are available.
 
  This routine is called by the UEFI driver framework during connect
  processing.
 
  @param[in] This                 Protocol instance pointer.
  @param[in] Controller           Handle of device to test.
  @param[in] RemainingDevicePath  Not used.
 
  @retval EFI_SUCCESS             This driver supports this device.
  @retval other                   This driver does not support this device.
 
**/
EFI_STATUS
EFIAPI
VirtualKeyboardDriverSupported (
  IN EFI_DRIVER_BINDING_PROTOCOL *This,
  IN EFI_HANDLE                  Controller,
  IN EFI_DEVICE_PATH_PROTOCOL    *RemainingDevicePath
  )
{
  EFI_GRAPHICS_OUTPUT_PROTOCOL  *GraphicsOutput;
  EFI_STATUS                    Status;
 
  DEBUG ((DEBUG_VK_ROUTINE_ENTRY_EXIT, "VirtualKeyboardDriverSupported Start\n"));
 
  //
  // Verify that the driver is not already started
  //
  Status = gBS->OpenProtocol (
                  Controller,
                  &gEfiCallerIdGuid,
                  NULL,
                  This->DriverBindingHandle,
                  Controller,
                  EFI_OPEN_PROTOCOL_TEST_PROTOCOL
                  );
  if (!EFI_ERROR (Status)) {
    Status = EFI_ALREADY_STARTED;
    DEBUG ((DEBUG_VK_ROUTINE_ENTRY_EXIT, "ERROR - VK has already initialized\n"));
    goto Error;
  }
 
  //
  // Determine if the pointer protocol is available.
  // This should be installed in touch driver.
  //
  Status = gBS->OpenProtocol (
                  Controller,
                  &gEfiAbsolutePointerProtocolGuid,
                  NULL,
                  This->DriverBindingHandle,
                  Controller,
                  EFI_OPEN_PROTOCOL_TEST_PROTOCOL
                  );
  if (EFI_ERROR (Status)) {
    DEBUG ((DEBUG_VK_ROUTINE_ENTRY_EXIT, "ERROR - VK Absolute pointer protocol not found\n"));
    goto Error;
  }
 
  Status = gBS->OpenProtocol (
                  Controller,
                  &gEdkiiTouchPanelGuid,
                  NULL,
                  This->DriverBindingHandle,
                  Controller,
                  EFI_OPEN_PROTOCOL_TEST_PROTOCOL
                  );
  if (EFI_ERROR (Status)) {
    DEBUG ((DEBUG_VK_ROUTINE_ENTRY_EXIT, "ERROR - VK Touch Panel Guid not found\n"));
    goto Error;
  }
 
  //
  // Determine if the graphics output protocol is available
  //
  Status = gBS->HandleProtocol (
                  gST->ConsoleOutHandle,
                  &gEfiGraphicsOutputProtocolGuid,
                  (VOID **)&GraphicsOutput
                  );
  if (EFI_ERROR (Status)) {
    DEBUG ((DEBUG_VK_ROUTINE_ENTRY_EXIT, "ERROR - VK Graphics output protocol not found\n"));
    goto Error;
  }
 
  DEBUG ((DEBUG_VK_ROUTINE_ENTRY_EXIT | DEBUG_INFO, "VirtualKeyboardDriverSupported Success, Status: %r\n", Status));
  goto End;
 
Error:
  DEBUG ((DEBUG_VK_ROUTINE_ENTRY_EXIT, "VirtualKeyboardDriverSupported Failed, Status: %r\n", Status));
 
End:
  DEBUG ((DEBUG_VK_ROUTINE_ENTRY_EXIT, "VirtualKeyboardDriverSupported End\n"));
  return Status;
}
 
/**
  Connect to the controller
 
  This routine initializes an instance of the virutal keyboard driver for this
  controller.
 
  This routine is called by the UEFI driver framework during connect
  processing if the controller passes the tests in I2cBusDriverSupported.
 
  @param[in] This                 Protocol instance pointer.
  @param[in] Controller           Handle of device to work with.
  @param[in] RemainingDevicePath  Not used, always produce all possible children.
 
  @retval EFI_SUCCESS             This driver is added to Controller.
  @retval other                   This driver does not support this device.
 
**/
EFI_STATUS
EFIAPI
VirtualKeyboardDriverStart (
  IN EFI_DRIVER_BINDING_PROTOCOL *This,
  IN EFI_HANDLE                  Controller,
  IN EFI_DEVICE_PATH_PROTOCOL    *RemainingDevicePath
  )
{
  VK_CONTEXT *VkContext;
  EFI_STATUS Status;
 
  DEBUG ((DEBUG_VK_ROUTINE_ENTRY_EXIT | DEBUG_INFO, "VirtualKeyboardDriverStart Start\n"));
 
  VkContext = AllocateZeroPool (sizeof (VK_CONTEXT));
  if (VkContext == NULL) {
    DEBUG ((DEBUG_VK_ROUTINE_ENTRY_EXIT | DEBUG_ERROR, "ERROR - No memory for virtual keyboard driver\n"));
    Status = EFI_OUT_OF_RESOURCES;
    ASSERT_EFI_ERROR (Status);
    goto Error;
  }
 
  Status = gBS->OpenProtocol (
                  Controller,
                  &gEfiAbsolutePointerProtocolGuid,
                  (VOID**) &VkContext->AbsolutePointer,
                  This->DriverBindingHandle,
                  Controller,
                  EFI_OPEN_PROTOCOL_BY_DRIVER
                  );
  if (EFI_ERROR (Status)) {
    DEBUG ((DEBUG_VK_ROUTINE_ENTRY_EXIT | DEBUG_ERROR, "ERROR - Failed to open absolute pointer protocol, Status: %r\n", Status));
    goto Error;
  }
 
  Status = gBS->OpenProtocol (
                  Controller,
                  &gEdkiiTouchPanelGuid,
                  NULL,
                  This->DriverBindingHandle,
                  Controller,
                  EFI_OPEN_PROTOCOL_TEST_PROTOCOL
                  );
  if (EFI_ERROR (Status)) {
    DEBUG ((DEBUG_VK_ROUTINE_ENTRY_EXIT | DEBUG_ERROR, "ERROR - TouchPanel GUID not found, Status: %r\n", Status));
    goto Error;
  }
 
  Status = gBS->HandleProtocol (
                  gST->ConsoleOutHandle,
                  &gEfiGraphicsOutputProtocolGuid,
                  (VOID**) &VkContext->GraphicsOutput
                  );
  if (EFI_ERROR (Status)) {
    DEBUG ((DEBUG_VK_ROUTINE_ENTRY_EXIT | DEBUG_ERROR, "ERROR - Graphics output protocol not available, Status: %r\n", Status));
    goto Error;
  }
 
  VkContext->HiiHandle = mHiiHandle;
  Status = VkApiStart (VkContext, Controller);
  if (EFI_ERROR (Status)) {
    DEBUG ((DEBUG_VK_ROUTINE_ENTRY_EXIT | DEBUG_ERROR, "ERROR - Failed to VkApiStart, Status: %r\n", Status));
    goto Error;
  }
 
  Status = gBS->InstallMultipleProtocolInterfaces (
                  &Controller,
                  &gEfiCallerIdGuid,
                  VkContext,
                  &gEfiSimpleTextInProtocolGuid,
                  &VkContext->SimpleTextIn,
                  &gEfiSimpleTextInputExProtocolGuid,
                  &VkContext->SimpleTextInEx,
                  &gEfiConsoleInDeviceGuid,
                  NULL,
                  NULL
                  );
  if (EFI_ERROR (Status)) {
    DEBUG ((DEBUG_VK_ROUTINE_ENTRY_EXIT | DEBUG_ERROR, "ERROR - Failed to install VK protocols, Status: %r\n", Status));
    ASSERT_EFI_ERROR (Status);
    goto Error;
  }
 
  DEBUG ((DEBUG_VK_ROUTINE_ENTRY_EXIT | DEBUG_INFO, "VirtualKeyboardDriverStart Success, Status: %r\n", Status));
  goto End;
 
Error:
  if (VkContext != NULL) {
    FreePool (VkContext);
  }
 
  gBS->CloseProtocol (
         Controller,
         &gEfiAbsolutePointerProtocolGuid,
         This->DriverBindingHandle,
         Controller
         );
 
  //
  // Restore setting if connect device fail.
  //
  PcdSet32S (PcdConOutRow, mOrigConOutRow);
  PcdSet32S (PcdSetupConOutRow, mOrigSetupConOutRow);
 
  DEBUG ((DEBUG_VK_ROUTINE_ENTRY_EXIT, "VirtualKeyboardDriverStart Failed, Status: %r\n", Status));
 
End:
  DEBUG ((DEBUG_VK_ROUTINE_ENTRY_EXIT, "VirtualKeyboardDriverStart End\n"));
 
  return Status;
}
 
/**
  Disconnect from the controller.
 
  This routine disconnects from the controller.
 
  This routine is called by DriverUnload when the I2C bus driver
  is being unloaded.
 
  @param[in] This                 Protocol instance pointer.
  @param[in] Controller           Handle of device to stop driver on.
  @param[in] NumberOfChildren     How many children need to be stopped.
  @param[in] ChildHandleBuffer    Not used.
 
  @retval EFI_SUCCESS             This driver is removed Controller.
  @retval EFI_DEVICE_ERROR        The device could not be stopped due to a device error.
  @retval other                   This driver was not removed from this device.
 
**/
EFI_STATUS
EFIAPI
VirtualKeyboardDriverStop (
  IN  EFI_DRIVER_BINDING_PROTOCOL *This,
  IN  EFI_HANDLE                  Controller,
  IN  UINTN                       NumberOfChildren,
  IN  EFI_HANDLE                  *ChildHandleBuffer
  )
{
  VK_CONTEXT                          *VkContext;
  EFI_STATUS                          Status;
 
  DEBUG ((DEBUG_VK_ROUTINE_ENTRY_EXIT, "VirtualKeyboardDriverStop Start\n"));
 
  Status = gBS->OpenProtocol (
                  Controller,
                  &gEfiCallerIdGuid,
                  (VOID**)&VkContext,
                  This->DriverBindingHandle,
                  Controller,
                  EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE
                  );
  if (EFI_ERROR (Status)) {
    Status = EFI_SUCCESS;
    goto Success;
  }
 
  //
  // Done with the driver protocol
  //
  Status = gBS->CloseProtocol (
                  Controller,
                  &gEfiCallerIdGuid,
                  This->DriverBindingHandle,
                  Controller
                  );
  if (EFI_ERROR (Status)) {
    DEBUG ((DEBUG_VK_ROUTINE_ENTRY_EXIT, "ERROR - Failed to close the VK protocol, Status: %r\n", Status));
    goto Error;
  }
 
  //
  // Remove ConsoleIn protocols first to close the link in ConSplitter
  //
  Status = gBS->OpenProtocol (
                  Controller,
                  &gEfiConsoleInDeviceGuid,
                  NULL,
                  This->DriverBindingHandle,
                  Controller,
                  EFI_OPEN_PROTOCOL_TEST_PROTOCOL
                  );
  if (EFI_ERROR (Status)) {
    DEBUG ((DEBUG_VK_ROUTINE_ENTRY_EXIT, "ERROR - Failed to uninstall the protocols, Status: %r\n", Status));
    goto Error;
  }
 
  //
  // Remove the remaining protocols
  //
  Status = gBS->UninstallMultipleProtocolInterfaces (
                  Controller,
                  &gEfiCallerIdGuid,
                  VkContext,
                  &gEfiSimpleTextInProtocolGuid,
                  &VkContext->SimpleTextIn,
                  &gEfiSimpleTextInputExProtocolGuid,
                  &VkContext->SimpleTextInEx,
                  &gEfiConsoleInDeviceGuid,
                  NULL,
                  NULL
                  );
  if (EFI_ERROR (Status)) {
    DEBUG ((DEBUG_VK_ROUTINE_ENTRY_EXIT, "ERROR - Failed to uninstall the protocols, Status: %r\n", Status));
    goto Error;
  }
 
  //
  // Stop the driver
  //
  VkApiStop (VkContext);
 
  //
  // Release the pointer protocol upon failure
  //
  Status = gBS->CloseProtocol (
                  Controller,
                  &gEfiAbsolutePointerProtocolGuid,
                  This->DriverBindingHandle,
                  Controller
                  );
  if (EFI_ERROR (Status)) {
    DEBUG ((DEBUG_VK_ROUTINE_ENTRY_EXIT, "ERROR - Failed to close absolute pointer protocol, Status: %r\n", Status));
    goto Error;
  }
 
  //
  // Release VkContext
  //
  if (VkContext != NULL) {
    FreePool (VkContext);
  }
 
Success:
  DEBUG ((DEBUG_VK_ROUTINE_ENTRY_EXIT, "VirtualKeyboardDriverStop Success, Status: %r\n", Status));
  goto End;
 
Error:
  DEBUG ((DEBUG_VK_ROUTINE_ENTRY_EXIT, "VirtualKeyboardDriverStop Failed, Status: %r\n", Status));
 
End:
  DEBUG ((DEBUG_VK_ROUTINE_ENTRY_EXIT, "VirtualKeyboardDriverStop End\n"));
  return Status;
}
 
/**
  Unloads an image.
 
  @param[in] ImageHandle        Handle that identifies the image to be unloaded.
 
  @retval EFI_SUCCESS           The image has been unloaded.
  @retval EFI_INVALID_PARAMETER ImageHandle is not a valid image handle.
 
**/
EFI_STATUS
EFIAPI
VirtualKeyboardDriverUnload (
  IN EFI_HANDLE ImageHandle
  )
{
  EFI_STATUS                 Status;
  EFI_HANDLE                 *HandleBuffer;
  UINTN                      HandleCount;
  UINTN                      Index;
 
  DEBUG ((DEBUG_VK_ROUTINE_ENTRY_EXIT, "VirtualKeyboardDriverUnload Start\n"));
 
  //
  // Retrieve array of all handles in the handle database
  //
  Status = gBS->LocateHandleBuffer (
                  AllHandles,
                  NULL,
                  NULL,
                  &HandleCount,
                  &HandleBuffer
                  );
  if (EFI_ERROR (Status)) {
    DEBUG ((DEBUG_VK_ROUTINE_ENTRY_EXIT, "ERROR - Failed to locate handle buffer, Status: %r\n", Status));
    ASSERT_EFI_ERROR (Status);
    goto Error;
  }
 
  //
  // Disconnect the current driver from handles in the handle database
  //
  for (Index = 0; Index < HandleCount; Index++) {
    gBS->DisconnectController (HandleBuffer[Index], ImageHandle, NULL);
  }
 
  //
  // Free the array of handles
  //
  if (HandleBuffer != NULL) {
    FreePool (HandleBuffer);
  }
 
  //
  // Uninstall protocols installed in the driver entry point
  //
  Status = gBS->UninstallMultipleProtocolInterfaces (
                  ImageHandle,
                  &gEfiDriverBindingProtocolGuid,
                  &gVirtualKeyboardDriverBinding,
                  &gEfiComponentNameProtocolGuid,
                  &gVirtualKeyboardComponentName,
                  &gEfiComponentName2ProtocolGuid,
                  &gVirtualKeyboardComponentName2,
                  NULL
                  );
  if (EFI_ERROR (Status)) {
    ASSERT_EFI_ERROR (Status);
    goto Error;
  }
 
  HiiRemovePackages (mHiiHandle);
 
  DEBUG ((DEBUG_VK_ROUTINE_ENTRY_EXIT, "VirtualKeyboardDriverUnload Success, Status: %r\n", Status));
  goto End;
 
Error:
  DEBUG ((DEBUG_VK_ROUTINE_ENTRY_EXIT, "VirtualKeyboardDriverUnload Failed, Status: %r\n", Status));
 
End:
  DEBUG ((DEBUG_VK_ROUTINE_ENTRY_EXIT, "VirtualKeyboardDriverUnload End\n"));
  return Status;
}
 
EFI_DRIVER_BINDING_PROTOCOL gVirtualKeyboardDriverBinding = {
  VirtualKeyboardDriverSupported,
  VirtualKeyboardDriverStart,
  VirtualKeyboardDriverStop,
  0x10,
  NULL,
  NULL
};
 
/**
  This is the declaration of an EFI image entry point. This entry point is
  the same for UEFI Applications, UEFI OS Loaders, and UEFI Drivers including
  both device drivers and bus drivers.
 
  @param  ImageHandle           The firmware allocated handle for the UEFI image.
  @param  SystemTable           A pointer to the EFI System Table.
 
  @retval EFI_SUCCESS           The operation completed successfully.
  @retval Others                An unexpected error occurred.
 
**/
EFI_STATUS
EFIAPI
VirtualKeyboardDriverEntryPoint (
  IN EFI_HANDLE       ImageHandle,
  IN EFI_SYSTEM_TABLE *SystemTable
  )
{
  EFI_STATUS                     Status;
 
  DEBUG ((DEBUG_VK_ROUTINE_ENTRY_EXIT, "VirtualKeyboardDriverEntryPoint Start\n"));
  //
  // Install UEFI Driver Model protocol(s).
  //
  Status = EfiLibInstallDriverBindingComponentName2 (
             ImageHandle,
             SystemTable,
             &gVirtualKeyboardDriverBinding,
             ImageHandle,
             &gVirtualKeyboardComponentName,
             &gVirtualKeyboardComponentName2
             );
  ASSERT_EFI_ERROR (Status);
 
  mHiiHandle = HiiAddPackages (
                 &gEfiCallerIdGuid,
                 NULL,
                 VirtualKeyboardDxeImages,
                 NULL
                 );
  ASSERT (mHiiHandle != NULL);
 
  mOrigConOutRow      = PcdGet32 (PcdConOutRow);
  mOrigSetupConOutRow = PcdGet32 (PcdSetupConOutRow);
 
  DEBUG ((DEBUG_VK_ROUTINE_ENTRY_EXIT, "VirtualKeyboardDriverEntryPoint End\n"));
  return Status;
}