summaryrefslogtreecommitdiffstats
path: root/private/ntos/fw/mips/fwsignal.c
blob: e4610721225a4ddd410c48bcf5e85cb9684e79f6 (plain) (blame)
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
/*++

Copyright (c) 1991  Microsoft Corporation

Module Name:

    fwsignal.c

Abstract:

    This module implements the ARC firmware Signal Handling Functions.

Author:

    Lluis Abello (lluis) 24-Sep-1991

--*/

#include "fwp.h"
#include "iodevice.h"
#ifdef DUO
#include "duoint.h"
#else
#include "jazzint.h"
#endif

//
// Define Signal types
//

typedef
VOID
(*SIGNALHANDLER)(
    IN LONG
    );

#define SIGINT  (0)
#define SIGDefault (SIGNALHANDLER)(0)
#define SIGIgnore (SIGNALHANDLER)(1)

typedef
SIGNALHANDLER
(*PARC_SIGNAL_ROUTINE) (
    IN LONG Sig,
    IN SIGNALHANDLER Handler
    );

typedef struct _R4000_CAUSE_REGISTER {
    ULONG Zero1:2;
    ULONG ExcCode:5;
    ULONG Zero2:1;
    ULONG IP:8;
    ULONG Zero3:12;
    ULONG CE:2;
    ULONG Zero4:1;
    ULONG BD:1;
} R4000_CAUSE_REGISTER, *PR4000_CAUSE_REGISTER;

VOID
SIGINTIgnore(
    IN LONG Sig
    );

extern KEYBOARD_BUFFER KbdBuffer;
extern PVOID MonitorExceptionHandler;
volatile BOOLEAN DeviceInterruptFlag;
SIGNALHANDLER SIGINTHandler = (SIGNALHANDLER)SIGINTIgnore;

//
// Keyboard static variables.
//

BOOLEAN KbdCtrl = FALSE;
BOOLEAN Scan0xE0 = FALSE;

//                             1111 1 1111122222222 2 2333333333344 4 44444445555 5 5 55
// Character #         234567890123 4 5678901234567 8 9012345678901 2 34567890123 4 5 67
PCHAR NormalLookup =  "1234567890-=\b\0qwertyuiop[]\n\0asdfghjkl;'`\0\\zxcvbnm,./\0\0\0 ";
PCHAR ShiftedLookup = "!@#$%^&*()_+\b\0QWERTYUIOP{}\n\0ASDFGHJKL:\"~\0\|ZXCVBNM<>?\0\0\0 ";

extern BOOLEAN FwLeftShift;
extern BOOLEAN FwRightShift;
extern BOOLEAN FwControl;
extern BOOLEAN FwAlt;
extern BOOLEAN FwCapsLock;


VOID
SIGINTIgnore(
    IN LONG Sig
    )

/*++

Routine Description:

    This routine implements the Signal Ignore. It gets called when
    a SIGINT signal raises and SIGIgnore was associated.

Arguments:

    ??

Return Value:

    None.

--*/

{
    return;
}


SIGNALHANDLER
FwSignal(
    IN LONG Sig,
    IN SIGNALHANDLER Handler
    )

/*++

Routine Description:

    This routine implements the ARC firmware signal routine.
    It associates the supplied Handler with the Signal.

Arguments:

    Sig  -  Signal number (Only SIGINT is defined).

    Handler - Handler to call when the signal rises.

Return Value:

    Returns the address of any previous handler routine associated
    with this signal.

--*/

{
    SIGNALHANDLER  Tmp;
    Tmp=SIGINTHandler;
    if (Handler == SIGDefault) {
        SIGINTHandler = (SIGNALHANDLER)MonitorExceptionHandler;
        return Tmp;
    }
    if (Handler == SIGIgnore) {
        SIGINTHandler = (SIGNALHANDLER)SIGINTIgnore;
        return Tmp;
    }
    SIGINTHandler =  Handler;
    return Tmp;
}



ARC_STATUS
FwWaitForDeviceInterrupt(
    USHORT  InterruptMask,
    ULONG Timeout
    )

/*++

Routine Description:

    This routine is called to wait for an specific I/O device interrupt.
    It sets a boolean to FALSE
    enables the device interrupt in the interrupt enable register
    Polls the boolean until becomes true.
    Returns to the caller.

    When the Inerrupt handler detects an interrupt which is not
    the keyboard interrupt sets the boolean to true and disables all
    the I/O device interrupts except the keyboard in the interrupt
    enable register.

    To guarantee that the interrupt received is the expected one
    the argument InterruptMask must have only one bit set.
    It's not possible to wait for a keyboard interrupt.


Arguments:

    InterruptMask -  Mask to enable the device interrupt in the
                     interrupt enable register.

    Timeout - a timeout value in seconds.  Note that a timeout of 0 gives an
              actual timeout of between 0 and 1, a timeout of 1 gives an actual
              timeout of between 1 and 2, and so on.

Return Value:

    ESUCCESS if the interrupt occurs, EIO if a timeout occurs.  It doesn't
    return until the requested interrupt occurs.

--*/

{
    ULONG Time1, Time2;

    //
    // set flag to FALSE
    //

    DeviceInterruptFlag = FALSE;

    //
    // Enable requested interrupt plus keyboard interrupt.
    //

    InterruptMask |= (1 << (KEYBOARD_VECTOR - DEVICE_VECTORS - 1));
    WRITE_REGISTER_USHORT(&((PINTERRUPT_REGISTERS)INTERRUPT_VIRTUAL_BASE)->Enable,
                          InterruptMask);
    //
    // wait until flag becomes TRUE.
    //

    Time1 = FwGetRelativeTime();
    while (DeviceInterruptFlag == FALSE) {
        Time2 = FwGetRelativeTime();
        if ((Time2 - Time1) > (Timeout + 1)) {
            return(EIO);
        }
    }
    return ESUCCESS;
}


VOID
StoreKeyboardChar(
    IN UCHAR Character
    )
/*++

Routine Description:

    This routine stores the given character into the circular
    buffer if there is enough room. Otherwise the character is lost.

Arguments:

    Character - Supplies the translated scan code to store into the buffer

Return Value:

    None.

--*/
{
    //
    // Store scan code in buffer if there is room.
    //
    if (((KbdBuffer.WriteIndex+1) % KBD_BUFFER_SIZE) != KbdBuffer.ReadIndex) {
        KbdBuffer.WriteIndex = (KbdBuffer.WriteIndex+1) % KBD_BUFFER_SIZE;
        KbdBuffer.Buffer[KbdBuffer.WriteIndex] = Character;
    }
}


VOID
TranslateScanCode(
    IN UCHAR   Scan
    )

/*++

Routine Description:

    This routine translates the given keyboard scan code into an
    ASCII character and puts it in the circular buffer.

Arguments:

    Scan - Supplies the scan code read from the keyboard.

Return Value:

    None.

--*/

{
    UCHAR FwControlCharacter=0;
    UCHAR FwFunctionCharacter;
    BOOLEAN MakeCode;
    UCHAR Char;

    //
    // Check 0xE0, which introduces a two key sequence.
    //

    if (Scan == 0xE0) {
        Scan0xE0 = TRUE;
        return;
    }
    if (Scan0xE0 == TRUE) {
        //
        // Check for PrintScrn (used as SysRq, also found in its true Alt
        // form below).
        //
        if (Scan == KEY_PRINT_SCREEN) {
            StoreKeyboardChar(ASCII_SYSRQ);
            Scan0xE0 = FALSE;
            return;
        }
    }

    //
    // Look for scan codes that indicate shift, control, or alt keys.  Bit 7
    // of scan code indicates upward or downward keypress.
    //
    MakeCode = !(Scan & 0x80);
    switch (Scan & 0x7F) {

        case KEY_LEFT_SHIFT:
            FwLeftShift = MakeCode;
            return;

        case KEY_RIGHT_SHIFT:
            FwRightShift = MakeCode;
            return;

        case KEY_CONTROL:
            FwControl = MakeCode;
            return;

        case KEY_ALT:
            FwAlt = MakeCode;
            return;

        default:
            break;

    }

    //
    // The rest of the keys only do something on make.
    //

    if (MakeCode) {

        //
        // Check for control keys.
        //

        switch (Scan) {

            case KEY_UP_ARROW:
                FwControlCharacter = 'A';
                break;

            case KEY_DOWN_ARROW:
                FwControlCharacter = 'B';
                break;

            case KEY_RIGHT_ARROW:
                FwControlCharacter = 'C';
                break;

            case KEY_LEFT_ARROW:
                FwControlCharacter = 'D';
                break;

            case KEY_HOME:
                FwControlCharacter = 'H';
                break;

            case KEY_END:
                FwControlCharacter = 'K';
                break;

            case KEY_PAGE_UP:
                FwControlCharacter = '?';
                break;

            case KEY_PAGE_DOWN:
                FwControlCharacter = '/';
                break;

            case KEY_INSERT:
                FwControlCharacter = '@';
                break;

            case KEY_DELETE:
                FwControlCharacter = 'P';
                break;

            case KEY_SYS_REQUEST:
                StoreKeyboardChar(ASCII_SYSRQ);
                return;

            case KEY_ESC:
                StoreKeyboardChar(ASCII_ESC);
                return;

            case KEY_CAPS_LOCK:
                FwCapsLock = !FwCapsLock;
                return;

            case KEY_F1:
                FwControlCharacter = 'O';
                FwFunctionCharacter = 'P';
                break;

            case KEY_F2:
                FwControlCharacter = 'O';
                FwFunctionCharacter = 'Q';
                break;

            case KEY_F3:
                FwControlCharacter = 'O';
                FwFunctionCharacter = 'w';
                break;

            case KEY_F4:
                FwControlCharacter = 'O';
                FwFunctionCharacter = 'x';
                break;

            case KEY_F5:
                FwControlCharacter = 'O';
                FwFunctionCharacter = 't';
                break;

            case KEY_F6:
                FwControlCharacter = 'O';
                FwFunctionCharacter = 'u';
                break;

            case KEY_F7:
                FwControlCharacter = 'O';
                FwFunctionCharacter = 'q';
                break;

            case KEY_F8:
                FwControlCharacter = 'O';
                FwFunctionCharacter = 'r';
                break;

            case KEY_F9:
                FwControlCharacter = 'O';
                FwFunctionCharacter = 'p';
                break;

            case KEY_F10:
                FwControlCharacter = 'O';
                FwFunctionCharacter = 'M';
                break;

//            case KEY_F11:
//                FwControlCharacter = 'O';
//                FwFunctionCharacter = 'A';
//                break;
//
//            case KEY_F12:
//                FwControlCharacter = 'O';
//                FwFunctionCharacter = 'B';
//                break;

            default:
                Char = 0;

                //
                // Check to see if the scan code corresponds to an ASCII
                // character.
                //

                if (((Scan >= 16) && (Scan <= 25)) ||
                    ((Scan >= 30) && (Scan <= 38)) ||
                    ((Scan >= 44) && (Scan <= 50))) {
                    if (((FwLeftShift || FwRightShift) && !FwCapsLock) ||
                        (!(FwLeftShift || FwRightShift) && FwCapsLock)) {
                        Char = ShiftedLookup[Scan - 2];
                    } else {
                        Char = NormalLookup[Scan - 2];
                    }
                } else {
                    if ((Scan > 1) && (Scan < 58)) {

                        //
                        // Its ASCII but not alpha, so don't shift on CapsLock.
                        //

                        if (FwLeftShift || FwRightShift) {
                            Char = ShiftedLookup[Scan - 2];
                        } else {
                            Char = NormalLookup[Scan - 2];
                        }
                    }
                }

                //
                // If a character, store it in buffer.
                //

                if (Char) {
                    StoreKeyboardChar(Char);
                    return;
                }
                break;
        }
        if (FwControlCharacter) {
            StoreKeyboardChar(ASCII_CSI);
            StoreKeyboardChar(FwControlCharacter);
            if (FwControlCharacter == 'O') {
               StoreKeyboardChar(FwFunctionCharacter);
            }
            return;
        }
    }
}


BOOLEAN
FwInterruptHandler(
    IN R4000_CAUSE_REGISTER Cause
    )

/*++

Routine Description:

    This routine is called when an interrupt occurs.
    Only device interrupts are handled.
    In a Keyboard interrupt, it reads the scan code and puts it
    in a buffer. If a Ctrl C sequence is detected it raises SIGINT.

Arguments:

    Cause - R4000 cause register.

Return Value:

    FALSE  If the exception could not be handled.
    TRUE   Otherwise.

--*/

{

    UCHAR   IntP;
    UCHAR   ScanCode;
    volatile USHORT TmpShort;

    //
    // Ignore EISA and TIMER interrupts.  TEMPTEMP
    //

    if (!(Cause.IP & ~((1 << 4) | (1 << 6)))) {
        return(TRUE);
    }

    if (Cause.IP != (1 << 3)) {             // if not device interrupt
        return FALSE;                       // exit
    }
    IntP = READ_REGISTER_UCHAR(INTERRUPT_SOURCE);

    //
    // if the interrupt is not from the keyboard,
    // set the Boolean to tell that a device interrupt ocurred and
    // disable all I/O device interrupts except keyboard.
    //
    if (IntP != KEYBOARD_DEVICE) {
        DeviceInterruptFlag = TRUE;
        WRITE_REGISTER_USHORT(&((PINTERRUPT_REGISTERS)INTERRUPT_VIRTUAL_BASE)->Enable,
                              (1 << (KEYBOARD_VECTOR - DEVICE_VECTORS - 1)));

        //
        // Read register to synchronize write.
        //
        TmpShort = READ_REGISTER_USHORT(&((PINTERRUPT_REGISTERS)INTERRUPT_VIRTUAL_BASE)->Enable);
        return  TRUE;
    }

    //
    // Wait 10 microseconds in case we have a slow keyboard controller that posts the interrupt
    // before the character is ready.
    //

    FwStallExecution(10);

    //
    //  Handle the keyboard interrupt.
    //
    while (READ_REGISTER_UCHAR(&KEYBOARD_READ->Status) & KBD_OBF_MASK == 0) {
    }
    ScanCode = READ_REGISTER_UCHAR(&KEYBOARD_READ->Data);

    switch (ScanCode) {

        //
        // Ctrl Make
        //
    case    0x1D: KbdCtrl = TRUE;
                  break;
        //
        // Ctrl Break
        //
    case    0x9D: KbdCtrl = FALSE;
                  break;
        //
        // C
        //
    case    0x2E: if (KbdCtrl) {
                    (SIGINTHandler)(SIGINT);
                  }
                  break;
    }

    //
    // Translate the scan code.
    //
    TranslateScanCode(ScanCode);
    return TRUE;
}