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
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
|
/*++
Copyright (c) 2006 - 2007, Intel Corporation
All rights reserved. This program and the accompanying materials
are licensed and made available under the terms and conditions of the BSD License
which accompanies this distribution. The full text of the license may be found at
http://opensource.org/licenses/bsd-license.php
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
Module Name:
Ps2Mouse.c
Abstract:
PS/2 Mouse driver. Routines that interacts with callers,
conforming to EFI driver model
--*/
//
// Include common header file for this module.
//
#include "CommonHeader.h"
#include "Ps2Mouse.h"
#include "CommPs2.h"
//
// DriverBinding Protocol Instance
//
EFI_DRIVER_BINDING_PROTOCOL gPS2MouseDriver = {
PS2MouseDriverSupported,
PS2MouseDriverStart,
PS2MouseDriverStop,
0xa,
NULL,
NULL
};
EFI_STATUS
EFIAPI
PS2MouseDriverSupported (
IN EFI_DRIVER_BINDING_PROTOCOL *This,
IN EFI_HANDLE Controller,
IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
)
/*++
Routine Description:
ControllerDriver Protocol Method
Arguments:
Returns:
--*/
// GC_TODO: This - add argument and description to function comment
// GC_TODO: Controller - add argument and description to function comment
// GC_TODO: RemainingDevicePath - add argument and description to function comment
{
EFI_STATUS Status;
EFI_ISA_IO_PROTOCOL *IsaIo;
Status = EFI_SUCCESS;
//
// Open the IO Abstraction(s) needed to perform the supported test
//
Status = gBS->OpenProtocol (
Controller,
&gEfiIsaIoProtocolGuid,
(VOID **) &IsaIo,
This->DriverBindingHandle,
Controller,
EFI_OPEN_PROTOCOL_BY_DRIVER
);
if (EFI_ERROR (Status)) {
return Status;
}
//
// Use the ISA I/O Protocol to see if Controller is the Keyboard controller
//
switch (IsaIo->ResourceList->Device.HID) {
case EISA_PNP_ID (0xF03):
//
// Microsoft PS/2 style mouse
//
case EISA_PNP_ID (0xF13):
//
// PS/2 Port for PS/2-style Mice
//
break;
case EISA_PNP_ID (0x303):
//
// IBM Enhanced (101/102-key, PS/2 mouse support)
//
if (IsaIo->ResourceList->Device.UID == 1) {
break;
}
default:
Status = EFI_UNSUPPORTED;
break;
}
//
// Close the I/O Abstraction(s) used to perform the supported test
//
gBS->CloseProtocol (
Controller,
&gEfiIsaIoProtocolGuid,
This->DriverBindingHandle,
Controller
);
return Status;
}
EFI_STATUS
EFIAPI
PS2MouseDriverStart (
IN EFI_DRIVER_BINDING_PROTOCOL *This,
IN EFI_HANDLE Controller,
IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
)
/*++
Routine Description:
Start protocol interfaces for the mouse device handles.
Arguments:
This - Protocol instance pointer.
Controller - Handle of device to bind driver to.
RemainingDevicePath - Not used.
Returns:
EFI_SUCCESS - This driver is added to DeviceHandle.
other - Errors occurred.
--*/
{
EFI_STATUS Status;
EFI_STATUS EmptyStatus;
EFI_ISA_IO_PROTOCOL *IsaIo;
PS2_MOUSE_DEV *MouseDev;
UINT8 Data;
EFI_TPL OldTpl;
EFI_STATUS_CODE_VALUE StatusCode;
EFI_DEVICE_PATH_PROTOCOL *ParentDevicePath;
StatusCode = 0;
MouseDev = NULL;
IsaIo = NULL;
//
// Open the device path protocol
//
Status = gBS->OpenProtocol (
Controller,
&gEfiDevicePathProtocolGuid,
(VOID **) &ParentDevicePath,
This->DriverBindingHandle,
Controller,
EFI_OPEN_PROTOCOL_BY_DRIVER
);
if (EFI_ERROR (Status)) {
return Status;
}
//
// Report that the keyboard is being enabled
//
REPORT_STATUS_CODE_WITH_DEVICE_PATH (
EFI_PROGRESS_CODE,
EFI_PERIPHERAL_MOUSE | EFI_P_PC_ENABLE,
ParentDevicePath
);
//
// Get the ISA I/O Protocol on Controller's handle
//
Status = gBS->OpenProtocol (
Controller,
&gEfiIsaIoProtocolGuid,
(VOID **) &IsaIo,
This->DriverBindingHandle,
Controller,
EFI_OPEN_PROTOCOL_BY_DRIVER
);
if (EFI_ERROR (Status)) {
gBS->CloseProtocol (
Controller,
&gEfiDevicePathProtocolGuid,
This->DriverBindingHandle,
Controller
);
return EFI_INVALID_PARAMETER;
}
//
// Raise TPL to avoid keyboard operation impact
//
OldTpl = gBS->RaiseTPL (TPL_NOTIFY);
//
// Allocate private data
//
MouseDev = AllocateZeroPool (sizeof (PS2_MOUSE_DEV));
if (MouseDev == NULL) {
Status = EFI_OUT_OF_RESOURCES;
goto ErrorExit;
}
//
// Setup the device instance
//
MouseDev->Signature = PS2_MOUSE_DEV_SIGNATURE;
MouseDev->Handle = Controller;
MouseDev->SampleRate = SSR_20;
MouseDev->Resolution = CMR4;
MouseDev->Scaling = SF1;
MouseDev->DataPackageSize = 3;
MouseDev->IsaIo = IsaIo;
MouseDev->DevicePath = ParentDevicePath;
//
// Resolution = 4 counts/mm
//
MouseDev->Mode.ResolutionX = 4;
MouseDev->Mode.ResolutionY = 4;
MouseDev->Mode.LeftButton = TRUE;
MouseDev->Mode.RightButton = TRUE;
MouseDev->SimplePointerProtocol.Reset = MouseReset;
MouseDev->SimplePointerProtocol.GetState = MouseGetState;
MouseDev->SimplePointerProtocol.Mode = &(MouseDev->Mode);
//
// Initialize keyboard controller if necessary
//
IsaIo->Io.Read (IsaIo, EfiIsaIoWidthUint8, KBC_CMD_STS_PORT, 1, &Data);
if ((Data & KBC_SYSF) != KBC_SYSF) {
Status = KbcSelfTest (IsaIo);
if (EFI_ERROR (Status)) {
StatusCode = EFI_PERIPHERAL_MOUSE | EFI_P_EC_CONTROLLER_ERROR;
goto ErrorExit;
}
}
KbcEnableAux (IsaIo);
REPORT_STATUS_CODE_WITH_DEVICE_PATH (
EFI_PROGRESS_CODE,
EFI_PERIPHERAL_MOUSE | EFI_P_PC_PRESENCE_DETECT,
ParentDevicePath
);
//
// Reset the mouse
//
Status = MouseDev->SimplePointerProtocol.Reset (&MouseDev->SimplePointerProtocol, TRUE);
if (EFI_ERROR (Status)) {
//
// mouse not connected
//
Status = EFI_SUCCESS;
StatusCode = EFI_PERIPHERAL_MOUSE | EFI_P_EC_NOT_DETECTED;
goto ErrorExit;
}
//
// Setup the WaitForKey event
//
Status = gBS->CreateEvent (
EVT_NOTIFY_WAIT,
TPL_NOTIFY,
MouseWaitForInput,
MouseDev,
&((MouseDev->SimplePointerProtocol).WaitForInput)
);
if (EFI_ERROR (Status)) {
Status = EFI_OUT_OF_RESOURCES;
goto ErrorExit;
}
//
// Setup a periodic timer, used to poll mouse state
//
Status = gBS->CreateEvent (
EVT_TIMER | EVT_NOTIFY_SIGNAL,
TPL_NOTIFY,
PollMouse,
MouseDev,
&MouseDev->TimerEvent
);
if (EFI_ERROR (Status)) {
Status = EFI_OUT_OF_RESOURCES;
goto ErrorExit;
}
//
// Start timer to poll mouse (100 samples per second)
//
Status = gBS->SetTimer (MouseDev->TimerEvent, TimerPeriodic, 100000);
if (EFI_ERROR (Status)) {
Status = EFI_OUT_OF_RESOURCES;
goto ErrorExit;
}
MouseDev->ControllerNameTable = NULL;
AddUnicodeString (
"eng",
gPs2MouseComponentName.SupportedLanguages,
&MouseDev->ControllerNameTable,
L"PS/2 Mouse Device"
);
//
// Install protocol interfaces for the mouse device.
//
Status = gBS->InstallMultipleProtocolInterfaces (
&Controller,
&gEfiSimplePointerProtocolGuid,
&MouseDev->SimplePointerProtocol,
NULL
);
if (EFI_ERROR (Status)) {
goto ErrorExit;
}
gBS->RestoreTPL (OldTpl);
return Status;
ErrorExit:
KbcDisableAux (IsaIo);
if (StatusCode != 0) {
REPORT_STATUS_CODE_WITH_DEVICE_PATH (
EFI_ERROR_CODE | EFI_ERROR_MINOR,
StatusCode,
ParentDevicePath
);
}
if ((MouseDev != NULL) && (MouseDev->SimplePointerProtocol.WaitForInput != NULL)) {
gBS->CloseEvent (MouseDev->SimplePointerProtocol.WaitForInput);
}
if ((MouseDev != NULL) && (MouseDev->TimerEvent != NULL)) {
gBS->CloseEvent (MouseDev->TimerEvent);
}
if ((MouseDev != NULL) && (MouseDev->ControllerNameTable != NULL)) {
FreeUnicodeStringTable (MouseDev->ControllerNameTable);
}
//
// Since there will be no timer handler for mouse input any more,
// exhaust input data just in case there is still mouse data left
//
EmptyStatus = EFI_SUCCESS;
while (!EFI_ERROR (EmptyStatus)) {
EmptyStatus = In8042Data (IsaIo, &Data);
}
if (MouseDev != NULL) {
gBS->FreePool (MouseDev);
}
gBS->CloseProtocol (
Controller,
&gEfiDevicePathProtocolGuid,
This->DriverBindingHandle,
Controller
);
gBS->CloseProtocol (
Controller,
&gEfiIsaIoProtocolGuid,
This->DriverBindingHandle,
Controller
);
gBS->RestoreTPL (OldTpl);
return Status;
}
EFI_STATUS
EFIAPI
PS2MouseDriverStop (
IN EFI_DRIVER_BINDING_PROTOCOL *This,
IN EFI_HANDLE Controller,
IN UINTN NumberOfChildren,
IN EFI_HANDLE *ChildHandleBuffer
)
/*++
Routine Description:
Arguments:
Returns:
--*/
// GC_TODO: This - add argument and description to function comment
// GC_TODO: Controller - add argument and description to function comment
// GC_TODO: NumberOfChildren - add argument and description to function comment
// GC_TODO: ChildHandleBuffer - add argument and description to function comment
// GC_TODO: EFI_SUCCESS - add return value to function comment
// GC_TODO: EFI_SUCCESS - add return value to function comment
{
EFI_STATUS Status;
EFI_SIMPLE_POINTER_PROTOCOL *SimplePointerProtocol;
PS2_MOUSE_DEV *MouseDev;
UINT8 Data;
Status = gBS->OpenProtocol (
Controller,
&gEfiSimplePointerProtocolGuid,
(VOID **) &SimplePointerProtocol,
This->DriverBindingHandle,
Controller,
EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
if (EFI_ERROR (Status)) {
return EFI_SUCCESS;
}
MouseDev = PS2_MOUSE_DEV_FROM_THIS (SimplePointerProtocol);
//
// Report that the keyboard is being disabled
//
REPORT_STATUS_CODE_WITH_DEVICE_PATH (
EFI_PROGRESS_CODE,
EFI_PERIPHERAL_MOUSE | EFI_P_PC_DISABLE,
MouseDev->DevicePath
);
Status = gBS->UninstallProtocolInterface (
Controller,
&gEfiSimplePointerProtocolGuid,
&MouseDev->SimplePointerProtocol
);
if (EFI_ERROR (Status)) {
return Status;
}
//
// Disable mouse on keyboard controller
//
KbcDisableAux (MouseDev->IsaIo);
//
// Cancel mouse data polling timer, close timer event
//
gBS->SetTimer (MouseDev->TimerEvent, TimerCancel, 0);
gBS->CloseEvent (MouseDev->TimerEvent);
//
// Since there will be no timer handler for mouse input any more,
// exhaust input data just in case there is still mouse data left
//
Status = EFI_SUCCESS;
while (!EFI_ERROR (Status)) {
Status = In8042Data (MouseDev->IsaIo, &Data);
}
gBS->CloseEvent (MouseDev->SimplePointerProtocol.WaitForInput);
FreeUnicodeStringTable (MouseDev->ControllerNameTable);
gBS->FreePool (MouseDev);
gBS->CloseProtocol (
Controller,
&gEfiDevicePathProtocolGuid,
This->DriverBindingHandle,
Controller
);
gBS->CloseProtocol (
Controller,
&gEfiIsaIoProtocolGuid,
This->DriverBindingHandle,
Controller
);
return EFI_SUCCESS;
}
EFI_STATUS
EFIAPI
MouseReset (
IN EFI_SIMPLE_POINTER_PROTOCOL *This,
IN BOOLEAN ExtendedVerification
)
/*++
Routine Description:
Reset the Mouse and do BAT test for it, if ExtendedVerification isTRUE and there is a mouse device connectted to system
Arguments:
This - Pointer of simple pointer Protocol.
ExtendedVerification - Whether configure mouse parameters. True: do; FALSE: skip.
Returns:
EFI_SUCCESS - The command byte is written successfully.
EFI_DEVICE_ERROR - Errors occurred during reseting keyboard.
--*/
{
EFI_STATUS Status;
PS2_MOUSE_DEV *MouseDev;
EFI_TPL OldTpl;
BOOLEAN KeyboardEnable;
UINT8 Data;
MouseDev = PS2_MOUSE_DEV_FROM_THIS (This);
//
// Report reset progress code
//
REPORT_STATUS_CODE_WITH_DEVICE_PATH (
EFI_PROGRESS_CODE,
EFI_PERIPHERAL_MOUSE | EFI_P_PC_RESET,
MouseDev->DevicePath
);
KeyboardEnable = FALSE;
//
// Raise TPL to avoid keyboard operation impact
//
OldTpl = gBS->RaiseTPL (TPL_NOTIFY);
ZeroMem (&MouseDev->State, sizeof (EFI_SIMPLE_POINTER_STATE));
MouseDev->StateChanged = FALSE;
//
// Exhaust input data
//
Status = EFI_SUCCESS;
while (!EFI_ERROR (Status)) {
Status = In8042Data (MouseDev->IsaIo, &Data);
}
CheckKbStatus (MouseDev->IsaIo, &KeyboardEnable);
KbcDisableKb (MouseDev->IsaIo);
MouseDev->IsaIo->Io.Read (MouseDev->IsaIo, EfiIsaIoWidthUint8, KBC_CMD_STS_PORT, 1, &Data);
//
// if there's data block on KBC data port, read it out
//
if ((Data & KBC_OUTB) == KBC_OUTB) {
MouseDev->IsaIo->Io.Read (MouseDev->IsaIo, EfiIsaIoWidthUint8, KBC_DATA_PORT, 1, &Data);
}
Status = EFI_SUCCESS;
//
// The PS2 mouse driver reset behavior is always successfully return no matter wheater or not there is mouse connected to system.
// This behavior is needed by performance speed. The following mouse command only succeessfully finish when mouse device is
// connected to system, so if PS2 mouse device not connect to system or user not ask for, we skip the mouse configuration and enabling
//
if (ExtendedVerification && CheckMouseConnect (MouseDev)) {
//
// Send mouse reset command and set mouse default configure
//
Status = PS2MouseReset (MouseDev->IsaIo);
if (EFI_ERROR (Status)) {
Status = EFI_DEVICE_ERROR;
goto Exit;
}
Status = PS2MouseSetSampleRate (MouseDev->IsaIo, MouseDev->SampleRate);
if (EFI_ERROR (Status)) {
Status = EFI_DEVICE_ERROR;
goto Exit;
}
Status = PS2MouseSetResolution (MouseDev->IsaIo, MouseDev->Resolution);
if (EFI_ERROR (Status)) {
Status = EFI_DEVICE_ERROR;
goto Exit;
}
Status = PS2MouseSetScaling (MouseDev->IsaIo, MouseDev->Scaling);
if (EFI_ERROR (Status)) {
Status = EFI_DEVICE_ERROR;
goto Exit;
}
Status = PS2MouseEnable (MouseDev->IsaIo);
if (EFI_ERROR (Status)) {
Status = EFI_DEVICE_ERROR;
goto Exit;
}
}
Exit:
gBS->RestoreTPL (OldTpl);
if (KeyboardEnable) {
KbcEnableKb (MouseDev->IsaIo);
}
return Status;
}
BOOLEAN
CheckMouseConnect (
IN PS2_MOUSE_DEV *MouseDev
)
/*++
Routine Description:
Check whether there is Ps/2 mouse device in system
Arguments:
PS2_MOUSE_DEV - Mouse Private Data Structure
Returns:
TRUE - Keyboard in System.
FALSE - Keyboard not in System.
--*/
{
EFI_STATUS Status;
Status = PS2MouseEnable (MouseDev->IsaIo);
if (!EFI_ERROR (Status)) {
return TRUE;
}
return FALSE;
}
EFI_STATUS
EFIAPI
MouseGetState (
IN EFI_SIMPLE_POINTER_PROTOCOL *This,
IN OUT EFI_SIMPLE_POINTER_STATE *State
)
/*++
Routine Description:
GC_TODO: Add function description
Arguments:
This - GC_TODO: add argument description
State - GC_TODO: add argument description
Returns:
EFI_INVALID_PARAMETER - GC_TODO: Add description for return value
EFI_NOT_READY - GC_TODO: Add description for return value
EFI_SUCCESS - GC_TODO: Add description for return value
--*/
{
PS2_MOUSE_DEV *MouseDev;
EFI_TPL OldTpl;
MouseDev = PS2_MOUSE_DEV_FROM_THIS (This);
if (State == NULL) {
return EFI_INVALID_PARAMETER;
}
if (!MouseDev->StateChanged) {
return EFI_NOT_READY;
}
OldTpl = gBS->RaiseTPL (TPL_NOTIFY);
CopyMem (State, &(MouseDev->State), sizeof (EFI_SIMPLE_POINTER_STATE));
//
// clear mouse state
//
MouseDev->State.RelativeMovementX = 0;
MouseDev->State.RelativeMovementY = 0;
MouseDev->State.RelativeMovementZ = 0;
MouseDev->StateChanged = FALSE;
gBS->RestoreTPL (OldTpl);
return EFI_SUCCESS;
}
VOID
EFIAPI
MouseWaitForInput (
IN EFI_EVENT Event,
IN VOID *Context
)
/*++
Routine Description:
Event notification function for SIMPLE_POINTER.WaitForInput event
Signal the event if there is input from mouse
Arguments:
Returns:
--*/
// GC_TODO: Event - add argument and description to function comment
// GC_TODO: Context - add argument and description to function comment
{
PS2_MOUSE_DEV *MouseDev;
MouseDev = (PS2_MOUSE_DEV *) Context;
//
// Someone is waiting on the mouse event, if there's
// input from mouse, signal the event
//
if (MouseDev->StateChanged) {
gBS->SignalEvent (Event);
}
}
VOID
EFIAPI
PollMouse (
IN EFI_EVENT Event,
IN VOID *Context
)
/*++
Routine Description:
Event notification function for TimerEvent event
If mouse device is connected to system, try to get the mouse packet data
Arguments:
Event - TimerEvent in PS2_MOUSE_DEV
Context - Pointer to PS2_MOUSE_DEV structure
Returns:
None
--*/
{
PS2_MOUSE_DEV *MouseDev;
MouseDev = (PS2_MOUSE_DEV *) Context;
//
// Polling mouse packet data
//
PS2MouseGetPacket (MouseDev);
}
|