Subversion Repositories HelenOS

Rev

Rev 1787 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1787 Rev 1874
Line 25... Line 25...
25
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
 */
28
 */
29
 
29
 
30
/** @addtogroup kbdia32 ia32
30
/** @addtogroup kbdia32
31
 * @brief   HelenOS ia32 / amd64 arch dependent parts of uspace keyboard handler.
31
 * @brief   Scancodes for PC keyboards.
32
 * @ingroup  kbd
-
 
33
 * @{
32
 * @{
34
 */
33
 */
35
/** @file
34
/** @file
36
 * @ingroup kbdamd64
35
 * @ingroup kbdamd64
37
 */
36
 */
38
 
37
 
39
#include <arch/kbd.h>
38
#include <genarch/scanc.h>
40
#include <ipc/ipc.h>
-
 
41
#include <unistd.h>
-
 
42
#include <kbd.h>
-
 
43
#include <keys.h>
-
 
44
 
-
 
45
/* Interesting bits for status register */
-
 
46
#define i8042_OUTPUT_FULL  0x1
-
 
47
#define i8042_INPUT_FULL   0x2
-
 
48
#define i8042_MOUSE_DATA   0x20
-
 
49
 
-
 
50
/* Command constants */
-
 
51
#define i8042_CMD_KBD 0x60
-
 
52
#define i8042_CMD_MOUSE  0xd4
-
 
53
 
-
 
54
/* Keyboard cmd byte */
-
 
55
#define i8042_KBD_IE        0x1
-
 
56
#define i8042_MOUSE_IE      0x2
-
 
57
#define i8042_KBD_DISABLE   0x10
-
 
58
#define i8042_MOUSE_DISABLE 0x20
-
 
59
#define i8042_KBD_TRANSLATE 0x40
-
 
60
 
-
 
61
/* Mouse constants */
-
 
62
#define MOUSE_OUT_INIT  0xf4
-
 
63
#define MOUSE_ACK       0xfa
-
 
64
 
-
 
65
 
-
 
66
#define SPECIAL     255
-
 
67
#define KEY_RELEASE 0x80
-
 
68
 
-
 
69
/**
-
 
70
 * These codes read from i8042 data register are silently ignored.
-
 
71
 */
-
 
72
#define IGNORE_CODE 0x7f
-
 
73
 
-
 
74
#define PRESSED_SHIFT       (1<<0)
-
 
75
#define PRESSED_CAPSLOCK    (1<<1)
-
 
76
#define LOCKED_CAPSLOCK     (1<<0)
-
 
77
 
-
 
78
/** Scancodes. */
-
 
79
#define SC_ESC      0x01
-
 
80
#define SC_BACKSPACE    0x0e
-
 
81
#define SC_LSHIFT   0x2a
-
 
82
#define SC_RSHIFT   0x36
-
 
83
#define SC_CAPSLOCK 0x3a
-
 
84
#define SC_SPEC_ESCAPE  0xe0
-
 
85
#define SC_LEFTARR      0x4b
-
 
86
#define SC_RIGHTARR     0x4d
-
 
87
#define SC_UPARR        0x48
-
 
88
#define SC_DOWNARR      0x50
-
 
89
#define SC_DELETE       0x53
-
 
90
#define SC_HOME         0x47
-
 
91
#define SC_END          0x4f
-
 
92
 
-
 
93
#define FUNCTION_KEYS 0x100
-
 
94
 
-
 
95
static volatile int keyflags;       /**< Tracking of multiple keypresses. */
-
 
96
static volatile int lockflags;      /**< Tracking of multiple keys lockings. */
-
 
97
 
39
 
98
/** Primary meaning of scancodes. */
40
/** Primary meaning of scancodes. */
99
static int sc_primary_map[] = {
41
int sc_primary_map[] = {
100
    SPECIAL, /* 0x00 */
42
    SPECIAL, /* 0x00 */
101
    SPECIAL, /* 0x01 - Esc */
43
    SPECIAL, /* 0x01 - Esc */
102
    '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=',
44
    '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=',
103
    '\b', /* 0x0e - Backspace */
45
    '\b', /* 0x0e - Backspace */
104
    '\t', 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']', '\n',
46
    '\t', 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']', '\n',
Line 174... Line 116...
174
    SPECIAL, /* 0x7e */
116
    SPECIAL, /* 0x7e */
175
    SPECIAL, /* 0x7f */
117
    SPECIAL, /* 0x7f */
176
};
118
};
177
 
119
 
178
/** Secondary meaning of scancodes. */
120
/** Secondary meaning of scancodes. */
179
static int sc_secondary_map[] = {
121
int sc_secondary_map[] = {
180
    SPECIAL, /* 0x00 */
122
    SPECIAL, /* 0x00 */
181
    0x1b, /* 0x01 - Esc */
123
    0x1b, /* 0x01 - Esc */
182
    '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+',
124
    '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+',
183
    SPECIAL, /* 0x0e - Backspace */
125
    SPECIAL, /* 0x0e - Backspace */
184
    '\t', 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', '{', '}', '\n',
126
    '\t', 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', '{', '}', '\n',
Line 253... Line 195...
253
    SPECIAL, /* 0x7d */
195
    SPECIAL, /* 0x7d */
254
    SPECIAL, /* 0x7e */
196
    SPECIAL, /* 0x7e */
255
    SPECIAL, /* 0x7f */
197
    SPECIAL, /* 0x7f */
256
};
198
};
257
 
199
 
258
irq_cmd_t i8042_cmds[2] = {
-
 
259
    { CMD_PORT_READ_1, (void *)0x64, 0, 1 },
-
 
260
    { CMD_PORT_READ_1, (void *)0x60, 0, 2 }
-
 
261
};
-
 
262
 
-
 
263
irq_code_t i8042_kbd = {
-
 
264
    2,
-
 
265
    i8042_cmds
-
 
266
};
-
 
267
 
-
 
268
static void key_released(keybuffer_t *keybuffer, unsigned char key)
-
 
269
{
-
 
270
    switch (key) {
-
 
271
        case SC_LSHIFT:
-
 
272
        case SC_RSHIFT:
-
 
273
            keyflags &= ~PRESSED_SHIFT;
-
 
274
            break;
-
 
275
        case SC_CAPSLOCK:
-
 
276
            keyflags &= ~PRESSED_CAPSLOCK;
-
 
277
            if (lockflags & LOCKED_CAPSLOCK)
-
 
278
                lockflags &= ~LOCKED_CAPSLOCK;
-
 
279
                else
-
 
280
                lockflags |= LOCKED_CAPSLOCK;
-
 
281
            break;
-
 
282
        default:
-
 
283
            break;
-
 
284
    }
-
 
285
}
-
 
286
 
-
 
287
static void key_pressed(keybuffer_t *keybuffer, unsigned char key)
-
 
288
{
-
 
289
    int *map = sc_primary_map;
-
 
290
    int ascii = sc_primary_map[key];
-
 
291
    int shift, capslock;
-
 
292
    int letter = 0;
-
 
293
 
-
 
294
    static int esc_count=0;
-
 
295
 
-
 
296
   
-
 
297
    if ( key == SC_ESC ) {
-
 
298
        esc_count++;
-
 
299
        if ( esc_count == 3 ) {
-
 
300
            __SYSCALL0(SYS_DEBUG_ENABLE_CONSOLE);
-
 
301
        }  
-
 
302
    } else {
-
 
303
        esc_count=0;
-
 
304
    }
-
 
305
   
-
 
306
   
-
 
307
 
-
 
308
    switch (key) {
-
 
309
        case SC_LSHIFT:
-
 
310
        case SC_RSHIFT:
-
 
311
                keyflags |= PRESSED_SHIFT;
-
 
312
            break;
-
 
313
        case SC_CAPSLOCK:
-
 
314
            keyflags |= PRESSED_CAPSLOCK;
-
 
315
            break;
-
 
316
        case SC_SPEC_ESCAPE:
-
 
317
            break;
-
 
318
    /*  case SC_LEFTARR:
-
 
319
            if (keybuffer_available(keybuffer) >= 3) {
-
 
320
                keybuffer_push(keybuffer, 0x1b);   
-
 
321
                keybuffer_push(keybuffer, 0x5b);   
-
 
322
                keybuffer_push(keybuffer, 0x44);   
-
 
323
            }
-
 
324
            break;
-
 
325
        case SC_RIGHTARR:
-
 
326
            if (keybuffer_available(keybuffer) >= 3) {
-
 
327
                keybuffer_push(keybuffer, 0x1b);   
-
 
328
                keybuffer_push(keybuffer, 0x5b);   
-
 
329
                keybuffer_push(keybuffer, 0x43);   
-
 
330
            }
-
 
331
            break;
-
 
332
        case SC_UPARR:
-
 
333
            if (keybuffer_available(keybuffer) >= 3) {
-
 
334
                keybuffer_push(keybuffer, 0x1b);   
-
 
335
                keybuffer_push(keybuffer, 0x5b);   
-
 
336
                keybuffer_push(keybuffer, 0x41);   
-
 
337
            }
-
 
338
            break;
-
 
339
        case SC_DOWNARR:
-
 
340
            if (keybuffer_available(keybuffer) >= 3) {
-
 
341
                keybuffer_push(keybuffer, 0x1b);   
-
 
342
                keybuffer_push(keybuffer, 0x5b);   
-
 
343
                keybuffer_push(keybuffer, 0x42);   
-
 
344
            }
-
 
345
            break;
-
 
346
        case SC_HOME:
-
 
347
            if (keybuffer_available(keybuffer) >= 3) {
-
 
348
                keybuffer_push(keybuffer, 0x1b);   
-
 
349
                keybuffer_push(keybuffer, 0x4f);   
-
 
350
                keybuffer_push(keybuffer, 0x48);   
-
 
351
            }
-
 
352
            break;
-
 
353
        case SC_END:
-
 
354
            if (keybuffer_available(keybuffer) >= 3) {
-
 
355
                keybuffer_push(keybuffer, 0x1b);   
-
 
356
                keybuffer_push(keybuffer, 0x4f);   
-
 
357
                keybuffer_push(keybuffer, 0x46);   
-
 
358
            }
-
 
359
            break;
-
 
360
        case SC_DELETE:
-
 
361
            if (keybuffer_available(keybuffer) >= 4) {
-
 
362
                keybuffer_push(keybuffer, 0x1b);   
-
 
363
                keybuffer_push(keybuffer, 0x5b);   
-
 
364
                keybuffer_push(keybuffer, 0x33);   
-
 
365
                keybuffer_push(keybuffer, 0x7e);   
-
 
366
            }
-
 
367
            break;
-
 
368
    */  default:
-
 
369
                letter = ((ascii >= 'a') && (ascii <= 'z'));
-
 
370
            capslock = (keyflags & PRESSED_CAPSLOCK) || (lockflags & LOCKED_CAPSLOCK);
-
 
371
            shift = keyflags & PRESSED_SHIFT;
-
 
372
            if (letter && capslock)
-
 
373
                shift = !shift;
-
 
374
            if (shift)
-
 
375
                map = sc_secondary_map;
-
 
376
            if (map[key] != SPECIAL)
-
 
377
                keybuffer_push(keybuffer, map[key]);   
-
 
378
            break;
-
 
379
    }
-
 
380
}
-
 
381
 
-
 
382
 
-
 
383
static void wait_ready(void) {
-
 
384
    while (i8042_status_read() & i8042_INPUT_FULL)
-
 
385
        ;
-
 
386
}
-
 
387
 
-
 
388
/** Register uspace irq handler
-
 
389
 * @return
-
 
390
 */
-
 
391
int kbd_arch_init(void)
-
 
392
{
-
 
393
    int i;
-
 
394
    int mouseenabled = 0;
-
 
395
 
-
 
396
    iospace_enable(task_get_id(),(void *)i8042_DATA, 5);
-
 
397
    /* Disable kbd, enable mouse */
-
 
398
    i8042_command_write(i8042_CMD_KBD);
-
 
399
    wait_ready();
-
 
400
    i8042_command_write(i8042_CMD_KBD);
-
 
401
    wait_ready();
-
 
402
    i8042_data_write(i8042_KBD_DISABLE);
-
 
403
    wait_ready();
-
 
404
 
-
 
405
    /* Flush all current IO */
-
 
406
    while (i8042_status_read() & i8042_OUTPUT_FULL)
-
 
407
        i8042_data_read();
-
 
408
    /* Initialize mouse */
-
 
409
    i8042_command_write(i8042_CMD_MOUSE);
-
 
410
    wait_ready();
-
 
411
    i8042_data_write(MOUSE_OUT_INIT);
-
 
412
    wait_ready();
-
 
413
   
-
 
414
    int mouseanswer = 0;
-
 
415
    for (i=0;i < 1000; i++) {
-
 
416
        int status = i8042_status_read();
-
 
417
        if (status & i8042_OUTPUT_FULL) {
-
 
418
            int data = i8042_data_read();
-
 
419
            if (status & i8042_MOUSE_DATA) {
-
 
420
                mouseanswer = data;
-
 
421
                break;
-
 
422
            }
-
 
423
        }
-
 
424
        usleep(1000);
-
 
425
    }
-
 
426
    if (mouseanswer == MOUSE_ACK) {
-
 
427
        /* enable mouse */
-
 
428
        mouseenabled = 1;
-
 
429
 
-
 
430
        ipc_register_irq(MOUSE_IRQ, &i8042_kbd);
-
 
431
    }
-
 
432
    /* Enable kbd */
-
 
433
    ipc_register_irq(KBD_IRQ, &i8042_kbd);
-
 
434
    /* Register for irq restart */
-
 
435
    ipc_register_irq(IPC_IRQ_KBDRESTART, NULL);
-
 
436
 
-
 
437
    int newcontrol = i8042_KBD_IE | i8042_KBD_TRANSLATE;
-
 
438
    if (mouseenabled)
-
 
439
        newcontrol |= i8042_MOUSE_IE;
-
 
440
   
-
 
441
    i8042_command_write(i8042_CMD_KBD);
-
 
442
    wait_ready();
-
 
443
    i8042_data_write(newcontrol);
-
 
444
    wait_ready();
-
 
445
   
-
 
446
    return 0;
-
 
447
}
-
 
448
 
-
 
449
/** Process keyboard & mouse events */
-
 
450
int kbd_arch_process(keybuffer_t *keybuffer, ipc_call_t *call)
-
 
451
{
-
 
452
    int status = IPC_GET_ARG1(*call);
-
 
453
 
-
 
454
    if (IPC_GET_METHOD(*call) == IPC_IRQ_KBDRESTART) {
-
 
455
        kbd_arch_init();
-
 
456
        return 1;
-
 
457
    }
-
 
458
 
-
 
459
    if ((status & i8042_MOUSE_DATA))
-
 
460
        return 0;
-
 
461
   
-
 
462
    int scan_code = IPC_GET_ARG2(*call);
-
 
463
   
-
 
464
    if (scan_code != IGNORE_CODE) {
-
 
465
        if (scan_code & KEY_RELEASE)
-
 
466
            key_released(keybuffer, scan_code ^ KEY_RELEASE);
-
 
467
        else
-
 
468
            key_pressed(keybuffer, scan_code);
-
 
469
    }
-
 
470
    return  1;
-
 
471
}
-
 
472
 
-
 
473
/**
200
/**
474
 * @}
201
 * @}
475
 */
202
 */
476
 
-