Subversion Repositories HelenOS-historic

Rev

Rev 1694 | Rev 1713 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1339 cejka 1
/*
2
 * Copyright (C) 2001-2004 Jakub Jermar
3
 * Copyright (C) 2006 Josef Cejka
4
 * All rights reserved.
5
 *
6
 * Redistribution and use in source and binary forms, with or without
7
 * modification, are permitted provided that the following conditions
8
 * are met:
9
 *
10
 * - Redistributions of source code must retain the above copyright
11
 *   notice, this list of conditions and the following disclaimer.
12
 * - Redistributions in binary form must reproduce the above copyright
13
 *   notice, this list of conditions and the following disclaimer in the
14
 *   documentation and/or other materials provided with the distribution.
15
 * - The name of the author may not be used to endorse or promote products
16
 *   derived from this software without specific prior written permission.
17
 *
18
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
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
27
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
 */
29
 
1649 cejka 30
/** @addtogroup kbdia32 ia32
1657 cejka 31
 * @brief   HelenOS ia32 / amd64 arch dependent parts of uspace keyboard handler.
1649 cejka 32
 * @ingroup  kbd
33
 * @{
34
 */
35
/** @file
1657 cejka 36
 * @ingroup kbdamd64
1649 cejka 37
 */
38
 
1339 cejka 39
#include <arch/kbd.h>
1353 jermar 40
#include <ipc/ipc.h>
1694 palkovsky 41
#include <unistd.h>
42
#include <kbd.h>
43
#include <keys.h>
1339 cejka 44
 
1694 palkovsky 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
 
1603 vana 66
#define SPECIAL     255
1339 cejka 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
 
1560 vana 93
#define FUNCTION_KEYS 0x100
94
 
1339 cejka 95
static volatile int keyflags;       /**< Tracking of multiple keypresses. */
96
static volatile int lockflags;      /**< Tracking of multiple keys lockings. */
97
 
98
/** Primary meaning of scancodes. */
1560 vana 99
static int sc_primary_map[] = {
1339 cejka 100
    SPECIAL, /* 0x00 */
101
    SPECIAL, /* 0x01 - Esc */
102
    '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=',
103
    '\b', /* 0x0e - Backspace */
104
    '\t', 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']', '\n',
105
    SPECIAL, /* 0x1d - LCtrl */
106
    'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', '\'',
107
    '`',
108
    SPECIAL, /* 0x2a - LShift */
109
    '\\',
110
    'z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '/',
111
    SPECIAL, /* 0x36 - RShift */
112
    '*',
113
    SPECIAL, /* 0x38 - LAlt */
114
    ' ',
115
    SPECIAL, /* 0x3a - CapsLock */
1560 vana 116
    (FUNCTION_KEYS | 1), /* 0x3b - F1 */
117
    (FUNCTION_KEYS | 2), /* 0x3c - F2 */
118
    (FUNCTION_KEYS | 3), /* 0x3d - F3 */
119
    (FUNCTION_KEYS | 4), /* 0x3e - F4 */
120
    (FUNCTION_KEYS | 5), /* 0x3f - F5 */
121
    (FUNCTION_KEYS | 6), /* 0x40 - F6 */
122
    (FUNCTION_KEYS | 7), /* 0x41 - F7 */
123
    (FUNCTION_KEYS | 8), /* 0x42 - F8 */
124
    (FUNCTION_KEYS | 9), /* 0x43 - F9 */
125
    (FUNCTION_KEYS | 10), /* 0x44 - F10 */
1339 cejka 126
    SPECIAL, /* 0x45 - NumLock */
127
    SPECIAL, /* 0x46 - ScrollLock */
128
    '7', '8', '9', '-',
129
    '4', '5', '6', '+',
130
    '1', '2', '3',
131
    '0', '.',
132
    SPECIAL, /* 0x54 - Alt-SysRq */
133
    SPECIAL, /* 0x55 - F11/F12/PF1/FN */
134
    SPECIAL, /* 0x56 - unlabelled key next to LAlt */
1566 cejka 135
    (FUNCTION_KEYS | 11), /* 0x57 - F11 */
136
    (FUNCTION_KEYS | 12), /* 0x58 - F12 */
1339 cejka 137
    SPECIAL, /* 0x59 */
138
    SPECIAL, /* 0x5a */
139
    SPECIAL, /* 0x5b */
140
    SPECIAL, /* 0x5c */
141
    SPECIAL, /* 0x5d */
142
    SPECIAL, /* 0x5e */
143
    SPECIAL, /* 0x5f */
144
    SPECIAL, /* 0x60 */
145
    SPECIAL, /* 0x61 */
146
    SPECIAL, /* 0x62 */
147
    SPECIAL, /* 0x63 */
148
    SPECIAL, /* 0x64 */
149
    SPECIAL, /* 0x65 */
150
    SPECIAL, /* 0x66 */
151
    SPECIAL, /* 0x67 */
152
    SPECIAL, /* 0x68 */
153
    SPECIAL, /* 0x69 */
154
    SPECIAL, /* 0x6a */
155
    SPECIAL, /* 0x6b */
156
    SPECIAL, /* 0x6c */
157
    SPECIAL, /* 0x6d */
158
    SPECIAL, /* 0x6e */
159
    SPECIAL, /* 0x6f */
160
    SPECIAL, /* 0x70 */
161
    SPECIAL, /* 0x71 */
162
    SPECIAL, /* 0x72 */
163
    SPECIAL, /* 0x73 */
164
    SPECIAL, /* 0x74 */
165
    SPECIAL, /* 0x75 */
166
    SPECIAL, /* 0x76 */
167
    SPECIAL, /* 0x77 */
168
    SPECIAL, /* 0x78 */
169
    SPECIAL, /* 0x79 */
170
    SPECIAL, /* 0x7a */
171
    SPECIAL, /* 0x7b */
172
    SPECIAL, /* 0x7c */
173
    SPECIAL, /* 0x7d */
174
    SPECIAL, /* 0x7e */
175
    SPECIAL, /* 0x7f */
176
};
177
 
178
/** Secondary meaning of scancodes. */
1566 cejka 179
static int sc_secondary_map[] = {
1339 cejka 180
    SPECIAL, /* 0x00 */
1644 vana 181
    0x1b, /* 0x01 - Esc */
1339 cejka 182
    '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+',
183
    SPECIAL, /* 0x0e - Backspace */
184
    '\t', 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', '{', '}', '\n',
185
    SPECIAL, /* 0x1d - LCtrl */
186
    'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':', '"',
187
    '~',
188
    SPECIAL, /* 0x2a - LShift */
189
    '|',
190
    'Z', 'X', 'C', 'V', 'B', 'N', 'M', '<', '>', '?',
191
    SPECIAL, /* 0x36 - RShift */
192
    '*',
193
    SPECIAL, /* 0x38 - LAlt */
194
    ' ',
195
    SPECIAL, /* 0x3a - CapsLock */
1504 cejka 196
    SPECIAL, /* 0x3b - F1 */
197
    SPECIAL, /* 0x3c - F2 */
198
    SPECIAL, /* 0x3d - F3 */
199
    SPECIAL, /* 0x3e - F4 */
200
    SPECIAL, /* 0x3f - F5 */
201
    SPECIAL, /* 0x40 - F6 */
202
    SPECIAL, /* 0x41 - F7 */
203
    SPECIAL, /* 0x42 - F8 */
204
    SPECIAL, /* 0x43 - F9 */
205
    SPECIAL, /* 0x44 - F10 */
1339 cejka 206
    SPECIAL, /* 0x45 - NumLock */
207
    SPECIAL, /* 0x46 - ScrollLock */
208
    '7', '8', '9', '-',
209
    '4', '5', '6', '+',
210
    '1', '2', '3',
211
    '0', '.',
212
    SPECIAL, /* 0x54 - Alt-SysRq */
213
    SPECIAL, /* 0x55 - F11/F12/PF1/FN */
214
    SPECIAL, /* 0x56 - unlabelled key next to LAlt */
215
    SPECIAL, /* 0x57 - F11 */
216
    SPECIAL, /* 0x58 - F12 */
217
    SPECIAL, /* 0x59 */
218
    SPECIAL, /* 0x5a */
219
    SPECIAL, /* 0x5b */
220
    SPECIAL, /* 0x5c */
221
    SPECIAL, /* 0x5d */
222
    SPECIAL, /* 0x5e */
223
    SPECIAL, /* 0x5f */
224
    SPECIAL, /* 0x60 */
225
    SPECIAL, /* 0x61 */
226
    SPECIAL, /* 0x62 */
227
    SPECIAL, /* 0x63 */
228
    SPECIAL, /* 0x64 */
229
    SPECIAL, /* 0x65 */
230
    SPECIAL, /* 0x66 */
231
    SPECIAL, /* 0x67 */
232
    SPECIAL, /* 0x68 */
233
    SPECIAL, /* 0x69 */
234
    SPECIAL, /* 0x6a */
235
    SPECIAL, /* 0x6b */
236
    SPECIAL, /* 0x6c */
237
    SPECIAL, /* 0x6d */
238
    SPECIAL, /* 0x6e */
239
    SPECIAL, /* 0x6f */
240
    SPECIAL, /* 0x70 */
241
    SPECIAL, /* 0x71 */
242
    SPECIAL, /* 0x72 */
243
    SPECIAL, /* 0x73 */
244
    SPECIAL, /* 0x74 */
245
    SPECIAL, /* 0x75 */
246
    SPECIAL, /* 0x76 */
247
    SPECIAL, /* 0x77 */
248
    SPECIAL, /* 0x78 */
249
    SPECIAL, /* 0x79 */
250
    SPECIAL, /* 0x7a */
251
    SPECIAL, /* 0x7b */
252
    SPECIAL, /* 0x7c */
253
    SPECIAL, /* 0x7d */
254
    SPECIAL, /* 0x7e */
255
    SPECIAL, /* 0x7f */
256
};
257
 
1694 palkovsky 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 }
1339 cejka 261
};
262
 
263
irq_code_t i8042_kbd = {
1694 palkovsky 264
    2,
1339 cejka 265
    i8042_cmds
266
};
267
 
1451 cejka 268
static int key_released(keybuffer_t *keybuffer, unsigned char key)
1339 cejka 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
 
1451 cejka 287
static int key_pressed(keybuffer_t *keybuffer, unsigned char key)
1339 cejka 288
{
1560 vana 289
    int *map = sc_primary_map;
290
    int ascii = sc_primary_map[key];
291
    int shift, capslock;
292
    int letter = 0;
1339 cejka 293
 
1644 vana 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
 
1339 cejka 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;
1504 cejka 318
    /*  case SC_LEFTARR:
1451 cejka 319
            if (keybuffer_available(keybuffer) >= 3) {
320
                keybuffer_push(keybuffer, 0x1b);   
321
                keybuffer_push(keybuffer, 0x5b);   
322
                keybuffer_push(keybuffer, 0x44);   
1339 cejka 323
            }
324
            break;
325
        case SC_RIGHTARR:
1451 cejka 326
            if (keybuffer_available(keybuffer) >= 3) {
327
                keybuffer_push(keybuffer, 0x1b);   
328
                keybuffer_push(keybuffer, 0x5b);   
329
                keybuffer_push(keybuffer, 0x43);   
1339 cejka 330
            }
331
            break;
332
        case SC_UPARR:
1451 cejka 333
            if (keybuffer_available(keybuffer) >= 3) {
334
                keybuffer_push(keybuffer, 0x1b);   
335
                keybuffer_push(keybuffer, 0x5b);   
336
                keybuffer_push(keybuffer, 0x41);   
1339 cejka 337
            }
338
            break;
339
        case SC_DOWNARR:
1451 cejka 340
            if (keybuffer_available(keybuffer) >= 3) {
341
                keybuffer_push(keybuffer, 0x1b);   
342
                keybuffer_push(keybuffer, 0x5b);   
343
                keybuffer_push(keybuffer, 0x42);   
1339 cejka 344
            }
345
            break;
346
        case SC_HOME:
1451 cejka 347
            if (keybuffer_available(keybuffer) >= 3) {
348
                keybuffer_push(keybuffer, 0x1b);   
349
                keybuffer_push(keybuffer, 0x4f);   
350
                keybuffer_push(keybuffer, 0x48);   
1339 cejka 351
            }
352
            break;
353
        case SC_END:
1451 cejka 354
            if (keybuffer_available(keybuffer) >= 3) {
355
                keybuffer_push(keybuffer, 0x1b);   
356
                keybuffer_push(keybuffer, 0x4f);   
357
                keybuffer_push(keybuffer, 0x46);   
1339 cejka 358
            }
359
            break;
360
        case SC_DELETE:
1451 cejka 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);   
1339 cejka 366
            }
367
            break;
1504 cejka 368
    */  default:
1339 cejka 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;
1504 cejka 376
            if (map[key] != SPECIAL)
377
                keybuffer_push(keybuffer, map[key]);   
1339 cejka 378
            break;
379
    }
380
}
381
 
1694 palkovsky 382
 
383
static void wait_ready(void) {
384
    while (i8042_status_read() & i8042_INPUT_FULL)
385
        ;
386
}
387
 
1339 cejka 388
/** Register uspace irq handler
389
 * @return
390
 */
391
int kbd_arch_init(void)
392
{
1694 palkovsky 393
    int rc1, 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
 
435
    int newcontrol = i8042_KBD_IE | i8042_KBD_TRANSLATE;
436
    if (mouseenabled)
437
        newcontrol |= i8042_MOUSE_IE;
438
 
439
    i8042_command_write(i8042_CMD_KBD);
440
    wait_ready();
441
    i8042_data_write(newcontrol);
442
    wait_ready();
443
 
444
    return 0;
1339 cejka 445
}
446
 
1694 palkovsky 447
/** Process keyboard & mouse events */
448
int kbd_arch_process(keybuffer_t *keybuffer, ipc_call_t *call)
1339 cejka 449
{
1694 palkovsky 450
    int status = IPC_GET_ARG1(*call);
451
 
1707 palkovsky 452
    if ((status & i8042_MOUSE_DATA))
453
        return 0;
454
 
455
    int scan_code = IPC_GET_ARG2(*call);
456
 
457
    if (scan_code != IGNORE_CODE) {
458
        if (scan_code & KEY_RELEASE)
459
            key_released(keybuffer, scan_code ^ KEY_RELEASE);
460
        else
461
            key_pressed(keybuffer, scan_code);
1339 cejka 462
    }
463
    return  1;
464
}
1649 cejka 465
 
466
/**
467
 * @}
468
 */
469