summaryrefslogtreecommitdiff
path: root/Core/EM/KbcEmul/Legacykbd.c
blob: 3519098c7ccbaadf2463796fa07d556bb5a7db14 (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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
//**********************************************************************
//**********************************************************************
//**                                                                  **
//**        (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/Legacykbd.c 4     6/27/12 1:30a Jittenkumarp $
//
// $Revision: 4 $
//
// $Date: 6/27/12 1:30a $
//
//****************************************************************************
// Revision History
// ----------------
// $Log: /Alaska/SOURCE/Modules/KbcEmulation/ALASKA/KBCEMUL/Legacykbd.c $
// 
// 4     6/27/12 1:30a Jittenkumarp
// [TAG]  		EIP89838 
// [Category]  	Improvement
// [Description]  	KbcEmulation module can't changed NUM LOCK ,CAPS
// LOCK,Scroll lock LED.
// [Files]  		Legacykbd.c
// 
// 3     2/10/11 1:04a 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
// 
// 2     6/30/09 11:31a Rameshr
// Coding Standard and File header updated.
// 
// 1     12/14/07 10:26a Rameshraju
// Initial Check-in
//****************************************************************************

//<AMI_FHDR_START>
//****************************************************************************
// Name:        LegacyKbd.c
//
// Description: Legacy Keyboard functions
//
//****************************************************************************
//<AMI_FHDR_END>

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

void    LegacyKbd_initParams(LEGACYKBD* kbd);
void    LegacyKbd_read_codes(PS2SINK* ps2dev, UINT8 cmd );
void    LegacyKbd_read_option(PS2SINK* ps2dev, UINT8 cmd );
void    LegacyKbd_rw_scancode(PS2SINK* ps2dev, UINT8 cmd );
void    LegacyKbd_send_resp(LEGACYKBD* mouse, UINT8* data, int count );

BOOLEAN KBC_WaitForInputBufferToBeFree ();
BOOLEAN KBC_WaitForOutputBufferToBeFilled ();
UINT8   KBC_ReadDataByte ();
void    KBC_WriteSubCommandByte(UINT8   bCmd);
void    KBC_WriteCommandByte(UINT8  bCmd);

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

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

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:   Legacykbd_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 Legacykbd_scanning(PS2SINK* ps2dev, UINT8 cmd )
{
    LEGACYKBD* kbd = _CR(ps2dev,LEGACYKBD,sink);
    switch(cmd){
        case 0xFF: //reset
            LegacyKbd_initParams(kbd);
            LegacyKbd_send_resp(kbd, ResetResp,COUNTOF(ResetResp));
            break;
        case 0xFD:
        case 0xFC:
        case 0xFB:
            LegacyKbd_send_resp(kbd, ackResp,COUNTOF(ackResp));
            kbd->read_code_action = cmd;
            kbd->sink.onCommand = LegacyKbd_read_codes;
            break;
        case 0xFA:
        case 0xF9:
        case 0xF8:
        case 0xF7:
        case 0xF6:
        case 0xF5:  //Disable
        case 0xF4:  //Enable
            LegacyKbd_send_resp(kbd, ackResp,COUNTOF(ackResp));
            break;
        case 0xF3:  //Set Typeatic Rate/Delay
            LegacyKbd_send_resp(kbd, ackResp,COUNTOF(ackResp));
            kbd->sink.onCommand = LegacyKbd_read_option;
            kbd->option_ptr = &kbd->typematicRateDelay;
            break;
        case 0xF2:
            LegacyKbd_send_resp(kbd, IdResp,COUNTOF(IdResp));
            break;
        case 0xF0:
            LegacyKbd_send_resp(kbd, ackResp,COUNTOF(ackResp));
            kbd->sink.onCommand = LegacyKbd_rw_scancode;
            kbd->option_ptr = &kbd->scancodepage;
            break;

        case 0xEE:  //Echo
            LegacyKbd_send_resp(kbd, &cmd,1);
            break;

        case 0xED:  //LED
            LegacyKbd_send_resp(kbd, ackResp,COUNTOF(ackResp));
            kbd->sink.onCommand = LegacyKbd_read_option;
            kbd->option_ptr = &kbd->typematicRateDelay;
             CmdED = 1;
            break;
    }
}
//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:   LegacyKbd_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 LegacyKbd_read_codes(PS2SINK* ps2dev, UINT8 cmd )
{
    LEGACYKBD* kbd = _CR(ps2dev,LEGACYKBD,sink);
    if( cmd >= 0xED ){
        kbd->sink.onCommand = Legacykbd_scanning;
        Legacykbd_scanning(ps2dev,cmd);
    } else
        LegacyKbd_send_resp(kbd, ackResp,COUNTOF(ackResp));
}
//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:   LegacyKbd_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 LegacyKbd_read_option(PS2SINK* ps2dev, UINT8 cmd )
{
    LEGACYKBD* kbd = _CR(ps2dev,LEGACYKBD,sink);
    UINT8*   fPtr;

    kbd->sink.onCommand = Legacykbd_scanning;

    if (CmdED == 1) {
        fPtr = (UINT8*)(UINTN)0x417;
        *fPtr &= ~0x70;
        *fPtr |= ((cmd & 0x07) << 4);
        CmdED = 0;
        LegacyKbd_send_resp(kbd, ackResp,COUNTOF(ackResp));
    } else {
        if (cmd > 0xED) {
            Legacykbd_scanning(ps2dev,cmd);
        } else  {
            *(kbd->option_ptr) = cmd;
            LegacyKbd_send_resp(kbd, ackResp,COUNTOF(ackResp));
        }
    }
}
//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:   LegacyKbd_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 LegacyKbd_rw_scancode(PS2SINK* ps2dev, UINT8 cmd )
{
    LEGACYKBD* kbd = _CR(ps2dev,LEGACYKBD,sink);
    if(cmd != 0 )
        LegacyKbd_read_option(ps2dev,cmd);
    else {
        kbd->sink.onCommand = Legacykbd_scanning;
        ScanCodePageResp[1] = kbd->scancodepage;
        LegacyKbd_send_resp(kbd, ScanCodePageResp,COUNTOF(ScanCodePageResp));
    }
}
//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:   LegacyKbd_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 LegacyKbd_send_resp(LEGACYKBD* kbd, UINT8* data, int count )
{
    //
    //When there is no PS2 Keyboard present in the system, Emulation should send the responce byte for the keyboard command.
    //If PS2 Keyboard present, it will send the responce to the KBC. So Emulation code doesn't need to responce it.
    //
    if(!kbd->sink.present_ ){
        //
        // Emulation must provide the responce. Override any previos 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:   LegacyKbd_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 LegacyKbd_pumpQueue(PS2SINK* ps2dev)
{
    LEGACYKBD* kbd = _CR(ps2dev,LEGACYKBD,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:   LegacykbdInput_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 LegacykbdInput_Send(EFI_EMUL6064KBDINPUT_PROTOCOL* p, UINT8* buffer, UINT32 count)
{
    LEGACYKBD* kbd = _CR(p,LEGACYKBD,kbdInput_);
    UINTN c = 0;

    //
    // 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 -1;

    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;
    }

    //
    // Disable trap to access real harware
    //
    TrapEnable(FALSE);
    LegacyKbd_pumpQueue(&kbd->sink);
    //
    // Enable trap back and clear statuses
    //
    TrapEnable(TRUE);

    return EFI_SUCCESS;
}
//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:   LegacykbdInput_GetTranslation
//
// Description: KBC translation
//
// Input:       P   - pointer to the Emulation Protocol
//
// Output:     None
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS LegacykbdInput_GetTranslation(
        EFI_EMUL6064KBDINPUT_PROTOCOL* p,
        OUT KBC_KBDTRANSLATION* outTrans )
{
    LEGACYKBD* kbd = _CR(p,LEGACYKBD,kbdInput_);
    *outTrans = (kbd->sink.kbc->read_ccb(kbd->sink.kbc) & KBC_CCB_XLAT) != 0?
            KBC_KBDTRANS_PCXT : KBC_KBDTRANS_AT;
    return EFI_SUCCESS;
}


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

    //
    // Disable trap to access real harware
    //
    TrapEnable(FALSE);
    //
    // Wait for input buffer to be free
    //
    KBC_WaitForInputBufferToBeFree();
    KBC_WriteCommandByte(KBCCMD_LOCK);
    bSave = ByteReadIO(KBC_STATUS_REG);

    //
    // Data is pending. Read it in AL
    //
    if(bSave & KBC_STATUS_OBF)
    {
        bKBData = ByteReadIO(KBC_DATA_REG);
    }
    KBC_WriteCommandByte(0xAE);
    KBC_WriteSubCommandByte(0xED);
    KBC_ReadDataByte();
    KBC_WriteSubCommandByte(data);
    KBC_ReadDataByte();

    if(bSave & KBC_STATUS_OBF){
        KBC_WriteCommandByte(bSave & KBC_STATUS_AUXB? 0xD3: 0xD2);
        KBC_WriteSubCommandByte(bKBData);
    }
    //
    // Enable trap back and clear statuses
    //
    TrapEnable(TRUE);

    return EFI_SUCCESS;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:   LegacyKbd_initParams
//
// Description: Initialize the KBD data
//
// Input:       kbd   - Pointer to the KBD structure
//
// Output:      None
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
void LegacyKbd_initParams(LEGACYKBD* kbd)
{
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:   InitLegacyKbd
//
// Description: Initialize the KBD data
//
// Input:       kbc     - pointer to the KBC Structure
//              kbd     - Pointer to the KBD structure
//
// Output:      None
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
void InitLegacyKbd(KBC* kbc, LEGACYKBD* kbd )
{
    EFI_HANDLE hEmulationService = 0;
    kbd->qhead = kbd->qtail = kbd->queue;
    kbd->sink.kbc = kbc;
    kbd->sink.onCommand = Legacykbd_scanning;
    kbd->sink.onQueueFree = LegacyKbd_pumpQueue;
    kbd->kbdInput_.Send = LegacykbdInput_Send;
    kbd->kbdInput_.GetTranslation = LegacykbdInput_GetTranslation;
    kbd->kbdInput_.UpdateLEDState = LegacykbdInput_updateLED;
    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                      **
//**                                                                  **
//**********************************************************************
//**********************************************************************