summaryrefslogtreecommitdiff
path: root/Core/EM/KbcEmul/VirtualKbd.c
blob: 48c4e35ec386c48c818cabb7625215f43c1c1dd5 (plain)
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
//**********************************************************************
//**********************************************************************
//**                                                                  **
//**        (C)Copyright 1985-2011, American Megatrends, Inc.         **
//**                                                                  **
//**                       All Rights Reserved.                       **
//**                                                                  **
//**         5555 Oakbrook Pkwy, Suite 200, Norcross, GA 30093        **
//**                                                                  **
//**                       Phone: (770)-246-8600                      **
//**                                                                  **
//**********************************************************************
//**********************************************************************
// $Header: /Alaska/SOURCE/Modules/KbcEmulation/ALASKA/KBCEMUL/VirtualKbd.c 6     2/10/11 1:07a Rameshr $
//
// $Revision: 6 $
//
// $Date: 2/10/11 1:07a $
//
//****************************************************************************
// Revision History
// ----------------
// $Log: /Alaska/SOURCE/Modules/KbcEmulation/ALASKA/KBCEMUL/VirtualKbd.c $
// 
// 6     2/10/11 1:07a Rameshr
// [TAG]  		EIP53687
// [Category]  	Improvement
// [Description]  	AMI headers update for KbcEmulation Module 
// [Files]  		KbcEmul.mak,KbcEmul.dxs,KbcEmul.c,KbcEmul.h,KbcEmulLib.h,Kbc
// EmulLib.c,Kbc.h,KbcDevEmul.h,Kbccore.c,Legacykbc.c,Legacykbd.c,LegacyMo
// use.c,VirtualKbc.c,VirtualKbd.c,VirtualMouse.c,Ap4x.h,Ap4x.c,KbcUhci.c,
// KbcUhci.h,KbcEmulIrq.c, KbcOhci.c, Kbcohci.h
// 
// 5     2/19/10 3:50p Davidd
// Corrected arrow keys not working properly during Netware 6.5
// installation - EIP 28411
// 
// 4     6/30/09 11:31a Rameshr
// Coding Standard and File header updated.
// 
// 3     10/30/08 10:47a Rameshraju
// Keyboard & Mouse enable/disable command handled
// 
// 2     5/01/08 10:43a Rameshraju
// Keyboard command D2 support added
// 
// 1     12/14/07 10:26a Rameshraju
// Initial Check-in
//****************************************************************************

//<AMI_FHDR_START>
//****************************************************************************
// Name:        VirtualKbd.c
//
// Description: Virtual keyboard functions
//****************************************************************************
//<AMI_FHDR_END>

#include "KbcEmul.h"
#include "KbcDevEmul.h"

#define COUNTOF(x) (sizeof(x)/sizeof(x[0]))

void vkbd_initParams(VIRTKBD* kbd);
void vkbd_read_codes(PS2SINK* ps2dev, UINT8 cmd );
void vkbd_read_option(PS2SINK* ps2dev, UINT8 cmd );
void vkbd_rw_scancode(PS2SINK* ps2dev, UINT8 cmd );
void vkbd_send_resp(VIRTKBD* mouse, UINT8* data, int count );

static UINT8 ackResp[] = {0xFA};
static UINT8 ResetResp[] = {0xFA,0xAA};
static UINT8 IdResp[] = {0xFA,0xAB, 0x41};
static UINT8 ScanCodePageResp[] = {0xFA, 0x00};
EFI_EMUL6064KBDINPUT_PROTOCOL   *gKbdProtocol=NULL;
static UINT8 CmdED = 0;

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:   kbd_scanning
//
// Description: Handle commands sent from KBC to PS2 Keyboard. Keyboard is scanning or idle, waiting for command or key
//
// Input:       ps2dev  - pointer to the KBD structure
//              Cmd     -   Data from the kbc buffer
//
// Output:      None
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
void kbd_scanning(PS2SINK* ps2dev, UINT8 cmd )
{
    VIRTKBD* kbd = _CR(ps2dev,VIRTKBD,sink);

    switch(cmd){
        //
        //Reset
        //
        case 0xFF:
            vkbd_initParams(kbd);
            vkbd_send_resp(kbd, ResetResp,COUNTOF(ResetResp));
            break;
        case 0xFD:
        case 0xFC:
        case 0xFB:
            vkbd_send_resp(kbd, ackResp,COUNTOF(ackResp));
            kbd->read_code_action = cmd;
            kbd->sink.onCommand = vkbd_read_codes;
            break;
        case 0xFA:
        case 0xF9:
        case 0xF8:
        case 0xF7:
        case 0xF6:
        case 0xF5:  
        case 0xF4:  //Enable
            kbd->Enabled=TRUE;
            vkbd_send_resp(kbd, ackResp,COUNTOF(ackResp));
            break;
        //
        //Set Typeatic Rate/Delay
        //
        case 0xF3:
            vkbd_send_resp(kbd, ackResp,COUNTOF(ackResp));
            kbd->sink.onCommand = vkbd_read_option;
            kbd->option_ptr = &kbd->typematicRateDelay;
            break;
        case 0xF2:
            vkbd_send_resp(kbd, IdResp,COUNTOF(IdResp));
            break;
        case 0xF0:
            vkbd_send_resp(kbd, ackResp,COUNTOF(ackResp));
            kbd->sink.onCommand = vkbd_rw_scancode;
            kbd->option_ptr = &kbd->scancodepage;
            break;
        //
        //Ehco
        //
        case 0xEE:
            vkbd_send_resp(kbd, &cmd, 1);
            break;

        //
        //Led
        //
        case 0xED:
            vkbd_send_resp(kbd, ackResp, COUNTOF(ackResp));
            kbd->sink.onCommand = vkbd_read_option;
            kbd->option_ptr = &kbd->typematicRateDelay;
            CmdED = 1;
            break;
    }
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:   vkbd_read_codes
//
// Description: Keyboard Read Codes.
//
// Input:       ps2dev  - pointer to the KBD structure
//              Cmd     - Data from the kbc buffer
//
// Output:      None
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
void vkbd_read_codes(PS2SINK* ps2dev, UINT8 cmd )
{
    VIRTKBD* kbd = _CR(ps2dev,VIRTKBD,sink);
    if( cmd >= 0xED ){
        kbd->sink.onCommand = kbd_scanning;
        kbd_scanning(ps2dev,cmd);
    } else
        vkbd_send_resp(kbd, ackResp,COUNTOF(ackResp));
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:   vkbd_read_option
//
// Description: Keyboard Read option.
//
// Input:       ps2dev  - pointer to the KBD structure
//              Cmd     - Data from the kbc buffer
//
// Output:      None
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
void vkbd_read_option(PS2SINK* ps2dev, UINT8 cmd )
{
    VIRTKBD* kbd = _CR(ps2dev,VIRTKBD,sink);
    UINT8*   fPtr;

    kbd->sink.onCommand = kbd_scanning;

    if (CmdED == 1) {
        fPtr = (UINT8*)(UINTN)0x417;
        *fPtr &= ~0x70;
        *fPtr |= ((cmd & 0x07) << 4);
        CmdED = 0;
        vkbd_send_resp(kbd, ackResp,COUNTOF(ackResp));
    }
    else {
        if (cmd > 0xED) {
            kbd_scanning(ps2dev,cmd);
        } else  {
            *(kbd->option_ptr) = cmd;
            vkbd_send_resp(kbd, ackResp,COUNTOF(ackResp));
        }
    }
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:   vkbd_rw_scancode
//
// Description: Keyboard Read write Scan code.
//
// Input:       ps2dev  - pointer to the KBD structure
//              Cmd     - Data from the kbc buffer
//
// Output:      None
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
void vkbd_rw_scancode(PS2SINK* ps2dev, UINT8 cmd )
{
    VIRTKBD* kbd = _CR(ps2dev,VIRTKBD,sink);
    if(cmd != 0 )
        vkbd_read_option(ps2dev,cmd);
    else {
        kbd->sink.onCommand = kbd_scanning;
        ScanCodePageResp[1] = kbd->scancodepage;
        vkbd_send_resp(kbd, ScanCodePageResp,COUNTOF(ScanCodePageResp));
    }
}
//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:   vkbd_send_resp
//
// Description: Place the Responce bytes in the keyboard buffer.
//
// Input:       ps2dev      - pointer to the KBD structure
//              Data        -   Pointer to the Data
//              count       - Length of the data
//
// Output:      None
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
void vkbd_send_resp(VIRTKBD* kbd, UINT8* data, int count )
{
    if( kbd->sink.present_ ){
        //
        // Emulation must provide the responce. Override any pervious kbd data queued to KBC
        //
        kbd->qtail = kbd->qhead = kbd->queue;
        for( ; count > 0; --count ){
            *kbd->qtail++ = *data++;
        }

        //
        // push the first byte to the KBC
        //
        if( kbd->qhead != kbd->qtail &&
            kbd->sink.kbc->send_outb1(kbd->sink.kbc,
            PS2DEV_KBD,TRUE,*kbd->qhead))
        {
            if(++kbd->qhead >=  kbd->queue+KBDQUEUE_SIZE)
                kbd->qhead = kbd->queue;
        }
    }
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:   vkbd_pumpQueue
//
// Description: KBC has empty queue; Legacy I/O trapping is being processed.If Kbd has data to push into KBC,
//              now is a good time to do it
//
// Input:       ps2dev  - pointer to the KBD structure
//
// Output:      None
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>

void vkbd_pumpQueue(PS2SINK* ps2dev)
{
    VIRTKBD* kbd = _CR(ps2dev,VIRTKBD,sink);
    //
    //send the Keyboard data to the KBC
    //
    if( kbd->qhead != kbd->qtail &&
        ps2dev->kbc->send_outb1(ps2dev->kbc,PS2DEV_KBD,FALSE,*kbd->qhead))
    {
        if(++kbd->qhead >=  kbd->queue+KBDQUEUE_SIZE)
            kbd->qhead = kbd->queue;
    }

}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:   kbdInput_Send
//
// Description: Keyboard Input source sends data. Put the data into the buffer, attempt to send the first byte.
//              The call comes outside of Trapping Leg access. Check that Trapping status will not be overriden
//
// Input:       P       - pointer to the Emulation Protocol
//              Buffer  - Data buffer
//              Count   - Buffer Length
//
// Output:      None
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS kbdInput_Send(EFI_EMUL6064KBDINPUT_PROTOCOL* p, UINT8* buffer, UINT32 count)
{
    VIRTKBD* kbd = _CR(p,VIRTKBD,kbdInput_);
    UINTN c = 0;

    //
    //Keyboard is disabled by KBC command 
    //
    if(!kbd->Enabled) {
        return EFI_DEVICE_ERROR;
    }

    //
    // count of free items in the queue
    //
    if( kbd->qtail >= kbd->qhead )
        c = KBDQUEUE_SIZE - (kbd->qtail - kbd->qhead);
    else
        c = kbd->qhead - kbd->qtail;

    if(c < count )
        return EFI_NOT_AVAILABLE_YET;

    for( ; count > 0; --count ){
        *kbd->qtail++ = *buffer++;
        if(kbd->qtail >= kbd->queue+KBDQUEUE_SIZE)
            kbd->qtail = kbd->queue;
    }
    //
    //Place the first data into the KBC buffer
    //
    vkbd_pumpQueue(&kbd->sink);

    return EFI_SUCCESS;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:   kbdInput_GetTranslation
//
// Description: KBC translation
//
// Input:       P   - pointer to the Emulation Protocol
//
// Output:     None
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS kbdInput_GetTranslation(
        EFI_EMUL6064KBDINPUT_PROTOCOL* p,
        OUT KBC_KBDTRANSLATION* outTrans )
{
    *outTrans=KBC_KBDTRANS_PCXT;
    return EFI_SUCCESS;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:   kbdInput_updateLED
//
// Description: Update the keyboard LED's
//
// Input:       P       - pointer to the Emulation Protocol
//              Data    - LED data
//
// Output:      None
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS kbdInput_updateLED(
        EFI_EMUL6064KBDINPUT_PROTOCOL* p,
        IN UINT8 data )
{
    return EFI_SUCCESS;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:   vkbd_initParams
//
// Description: Initialize the KBD data
//
// Input:       kbd   - Pointer to the KBD structure
//
// Output:      None
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
void vkbd_initParams(VIRTKBD* Kbd)
{
    Kbd->Enabled = TRUE;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:   InitVirtualKbd
//
// Description: Initialize the KBD data
//
// Input:       kbc     - pointer to the KBC Structure
//              kbd     - Pointer to the KBD structure
//
// Output:      None
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
void InitVirtualKbd(KBC* kbc, VIRTKBD* kbd )
{
    EFI_HANDLE hEmulationService = 0;
    kbd->qhead = kbd->qtail = kbd->queue;
    kbd->sink.kbc = kbc;
    kbd->sink.onCommand = kbd_scanning;
    kbd->sink.onQueueFree = vkbd_pumpQueue;
    kbd->kbdInput_.Send = kbdInput_Send;
    kbd->kbdInput_.GetTranslation = kbdInput_GetTranslation;
    kbd->kbdInput_.UpdateLEDState = kbdInput_updateLED;
    gKbdProtocol=&kbd->kbdInput_;
    vkbd_initParams(kbd);
    VERIFY_EFI_ERROR(
        gBS->InstallProtocolInterface(&hEmulationService,
        &gEmul6064KbdInputProtocolGuid,EFI_NATIVE_INTERFACE,(void*)&kbd->kbdInput_));
}

//**********************************************************************
//**********************************************************************
//**                                                                  **
//**        (C)Copyright 1985-2011, American Megatrends, Inc.         **
//**                                                                  **
//**                       All Rights Reserved.                       **
//**                                                                  **
//**         5555 Oakbrook Pkwy, Suite 200, Norcross, GA 30093        **
//**                                                                  **
//**                       Phone: (770)-246-8600                      **
//**                                                                  **
//**********************************************************************
//**********************************************************************