Subversion Repositories HelenOS-historic

Rev

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

Rev Author Line No. Line
1 jermar 1
/*
2
 * Copyright (C) 2001-2004 Jakub Jermar
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 *
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
13
 *   documentation and/or other materials provided with the distribution.
14
 * - The name of the author may not be used to endorse or promote products
15
 *   derived from this software without specific prior written permission.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
28
 
29
#include <arch/i8042.h>
30
#include <arch/i8259.h>
31
#include <arch/interrupt.h>
32
#include <cpu.h>
33
#include <arch/asm.h>
34
#include <arch.h>
35
#include <print.h>
508 jermar 36
#include <synch/spinlock.h>
37
#include <typedefs.h>
511 jermar 38
#include <console/chardev.h>
39
#include <console/console.h>
517 jermar 40
#include <macros.h>
576 palkovsky 41
#include <interrupt.h>
1 jermar 42
 
508 jermar 43
/**
1 jermar 44
 * i8042 processor driver.
508 jermar 45
 * It takes care of low-level keyboard functions.
1 jermar 46
 */
47
 
512 jermar 48
#define i8042_DATA		0x60
49
#define i8042_STATUS		0x64
50
 
51
/** Keyboard commands. */
52
#define KBD_ENABLE	0xf4
53
#define KBD_DISABLE	0xf5
54
#define KBD_ACK		0xfa
55
 
508 jermar 56
#define SPECIAL		'?'
57
#define KEY_RELEASE	0x80
58
 
59
static void key_released(__u8 sc);
60
static void key_pressed(__u8 sc);
61
 
62
#define PRESSED_SHIFT		(1<<0)
63
#define PRESSED_CAPSLOCK	(1<<1)
64
#define LOCKED_CAPSLOCK		(1<<0)
65
 
509 jermar 66
static spinlock_t keylock;		/**< keylock protects keyflags and lockflags. */
508 jermar 67
static volatile int keyflags;		/**< Tracking of multiple keypresses. */
509 jermar 68
static volatile int lockflags;		/**< Tracking of multiple keys lockings. */
508 jermar 69
 
575 palkovsky 70
static void i8042_suspend(chardev_t *);
71
static void i8042_resume(chardev_t *);
511 jermar 72
 
73
static chardev_t kbrd;
74
static chardev_operations_t ops = {
75
	.suspend = i8042_suspend,
76
	.resume = i8042_resume
77
};
78
 
508 jermar 79
/** Primary meaning of scancodes. */
80
static char sc_primary_map[] = {
81
	SPECIAL, /* 0x00 */
82
	SPECIAL, /* 0x01 - Esc */
83
	'1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=',
588 palkovsky 84
	'\b', /* 0x0e - Backspace */
508 jermar 85
	'\t', 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']', '\n',
86
	SPECIAL, /* 0x1d - LCtrl */
87
	'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', '\'',
88
	'`',
89
	SPECIAL, /* 0x2a - LShift */ 
90
	'\\',
91
	'z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '/',
92
	SPECIAL, /* 0x36 - RShift */
93
	'*',
94
	SPECIAL, /* 0x38 - LAlt */
95
	' ',
96
	SPECIAL, /* 0x3a - CapsLock */
97
	SPECIAL, /* 0x3b - F1 */
98
	SPECIAL, /* 0x3c - F2 */
99
	SPECIAL, /* 0x3d - F3 */
100
	SPECIAL, /* 0x3e - F4 */
101
	SPECIAL, /* 0x3f - F5 */
102
	SPECIAL, /* 0x40 - F6 */
103
	SPECIAL, /* 0x41 - F7 */
104
	SPECIAL, /* 0x42 - F8 */
105
	SPECIAL, /* 0x43 - F9 */
106
	SPECIAL, /* 0x44 - F10 */
107
	SPECIAL, /* 0x45 - NumLock */
108
	SPECIAL, /* 0x46 - ScrollLock */
109
	'7', '8', '9', '-',
110
	'4', '5', '6', '+',
111
	'1', '2', '3',
112
	'0', '.',
113
	SPECIAL, /* 0x54 - Alt-SysRq */
114
	SPECIAL, /* 0x55 - F11/F12/PF1/FN */
115
	SPECIAL, /* 0x56 - unlabelled key next to LAlt */
116
	SPECIAL, /* 0x57 - F11 */
117
	SPECIAL, /* 0x58 - F12 */
118
	SPECIAL, /* 0x59 */
119
	SPECIAL, /* 0x5a */
120
	SPECIAL, /* 0x5b */
121
	SPECIAL, /* 0x5c */
122
	SPECIAL, /* 0x5d */
123
	SPECIAL, /* 0x5e */
124
	SPECIAL, /* 0x5f */
125
	SPECIAL, /* 0x60 */
126
	SPECIAL, /* 0x61 */
127
	SPECIAL, /* 0x62 */
128
	SPECIAL, /* 0x63 */
129
	SPECIAL, /* 0x64 */
130
	SPECIAL, /* 0x65 */
131
	SPECIAL, /* 0x66 */
132
	SPECIAL, /* 0x67 */
133
	SPECIAL, /* 0x68 */
134
	SPECIAL, /* 0x69 */
135
	SPECIAL, /* 0x6a */
136
	SPECIAL, /* 0x6b */
137
	SPECIAL, /* 0x6c */
138
	SPECIAL, /* 0x6d */
139
	SPECIAL, /* 0x6e */
140
	SPECIAL, /* 0x6f */
141
	SPECIAL, /* 0x70 */
142
	SPECIAL, /* 0x71 */
143
	SPECIAL, /* 0x72 */
144
	SPECIAL, /* 0x73 */
145
	SPECIAL, /* 0x74 */
146
	SPECIAL, /* 0x75 */
147
	SPECIAL, /* 0x76 */
148
	SPECIAL, /* 0x77 */
149
	SPECIAL, /* 0x78 */
150
	SPECIAL, /* 0x79 */
151
	SPECIAL, /* 0x7a */
152
	SPECIAL, /* 0x7b */
153
	SPECIAL, /* 0x7c */
154
	SPECIAL, /* 0x7d */
155
	SPECIAL, /* 0x7e */
156
	SPECIAL, /* 0x7f */
157
};
158
 
159
/** Secondary meaning of scancodes. */
160
static char sc_secondary_map[] = {
161
	SPECIAL, /* 0x00 */
162
	SPECIAL, /* 0x01 - Esc */
163
	'!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+',
164
	SPECIAL, /* 0x0e - Backspace */
165
	'\t', 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', '{', '}', '\n',
166
	SPECIAL, /* 0x1d - LCtrl */
167
	'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':', '"',
168
	'~',
169
	SPECIAL, /* 0x2a - LShift */ 
170
	'|',
171
	'Z', 'X', 'C', 'V', 'B', 'N', 'M', '<', '>', '?',
172
	SPECIAL, /* 0x36 - RShift */
173
	'*',
174
	SPECIAL, /* 0x38 - LAlt */
175
	' ',
176
	SPECIAL, /* 0x3a - CapsLock */
177
	SPECIAL, /* 0x3b - F1 */
178
	SPECIAL, /* 0x3c - F2 */
179
	SPECIAL, /* 0x3d - F3 */
180
	SPECIAL, /* 0x3e - F4 */
181
	SPECIAL, /* 0x3f - F5 */
182
	SPECIAL, /* 0x40 - F6 */
183
	SPECIAL, /* 0x41 - F7 */
184
	SPECIAL, /* 0x42 - F8 */
185
	SPECIAL, /* 0x43 - F9 */
186
	SPECIAL, /* 0x44 - F10 */
187
	SPECIAL, /* 0x45 - NumLock */
188
	SPECIAL, /* 0x46 - ScrollLock */
189
	'7', '8', '9', '-',
190
	'4', '5', '6', '+',
191
	'1', '2', '3',
192
	'0', '.',
193
	SPECIAL, /* 0x54 - Alt-SysRq */
194
	SPECIAL, /* 0x55 - F11/F12/PF1/FN */
195
	SPECIAL, /* 0x56 - unlabelled key next to LAlt */
196
	SPECIAL, /* 0x57 - F11 */
197
	SPECIAL, /* 0x58 - F12 */
198
	SPECIAL, /* 0x59 */
199
	SPECIAL, /* 0x5a */
200
	SPECIAL, /* 0x5b */
201
	SPECIAL, /* 0x5c */
202
	SPECIAL, /* 0x5d */
203
	SPECIAL, /* 0x5e */
204
	SPECIAL, /* 0x5f */
205
	SPECIAL, /* 0x60 */
206
	SPECIAL, /* 0x61 */
207
	SPECIAL, /* 0x62 */
208
	SPECIAL, /* 0x63 */
209
	SPECIAL, /* 0x64 */
210
	SPECIAL, /* 0x65 */
211
	SPECIAL, /* 0x66 */
212
	SPECIAL, /* 0x67 */
213
	SPECIAL, /* 0x68 */
214
	SPECIAL, /* 0x69 */
215
	SPECIAL, /* 0x6a */
216
	SPECIAL, /* 0x6b */
217
	SPECIAL, /* 0x6c */
218
	SPECIAL, /* 0x6d */
219
	SPECIAL, /* 0x6e */
220
	SPECIAL, /* 0x6f */
221
	SPECIAL, /* 0x70 */
222
	SPECIAL, /* 0x71 */
223
	SPECIAL, /* 0x72 */
224
	SPECIAL, /* 0x73 */
225
	SPECIAL, /* 0x74 */
226
	SPECIAL, /* 0x75 */
227
	SPECIAL, /* 0x76 */
228
	SPECIAL, /* 0x77 */
229
	SPECIAL, /* 0x78 */
230
	SPECIAL, /* 0x79 */
231
	SPECIAL, /* 0x7a */
232
	SPECIAL, /* 0x7b */
233
	SPECIAL, /* 0x7c */
234
	SPECIAL, /* 0x7d */
235
	SPECIAL, /* 0x7e */
236
	SPECIAL, /* 0x7f */	
237
};
238
 
576 palkovsky 239
static void i8042_interrupt(int n, void *stack);
240
 
508 jermar 241
/** Initialize i8042. */
1 jermar 242
void i8042_init(void)
243
{
576 palkovsky 244
	exc_register(VECTOR_KBD, "i8042_interrupt", i8042_interrupt);
512 jermar 245
	trap_virtual_enable_irqs(1<<IRQ_KBD);
552 palkovsky 246
	spinlock_initialize(&keylock, "i8042_lock");
575 palkovsky 247
	chardev_initialize("i8042_kbd", &kbrd, &ops);
511 jermar 248
	stdin = &kbrd;
1 jermar 249
}
250
 
508 jermar 251
/** Process i8042 interrupt.
252
 *
253
 * @param n Interrupt vector.
254
 * @param stack Interrupted stack.
255
 */
576 palkovsky 256
void i8042_interrupt(int n, void *stack)
1 jermar 257
{
258
	__u8 x;
259
 
260
	trap_virtual_eoi();
512 jermar 261
	x = inb(i8042_DATA);
508 jermar 262
	if (x & KEY_RELEASE)
263
		key_released(x ^ KEY_RELEASE);
264
	else
265
		key_pressed(x);
1 jermar 266
}
508 jermar 267
 
268
/** Process release of key.
269
 *
270
 * @param sc Scancode of the key being released.
271
 */
272
void key_released(__u8 sc)
273
{
274
	spinlock_lock(&keylock);
275
	switch (sc) {
276
	    case SC_LSHIFT:
277
	    case SC_RSHIFT:
278
		keyflags &= ~PRESSED_SHIFT;
279
		break;
280
	    case SC_CAPSLOCK:
281
		keyflags &= ~PRESSED_CAPSLOCK;
282
		if (lockflags & LOCKED_CAPSLOCK)
283
			lockflags &= ~LOCKED_CAPSLOCK;
284
		else
285
			lockflags |= LOCKED_CAPSLOCK;
286
		break;
287
	    default:
288
		break;
289
	}
290
	spinlock_unlock(&keylock);
291
}
292
 
293
/** Process keypress.
294
 *
295
 * @param sc Scancode of the key being pressed.
296
 */
297
void key_pressed(__u8 sc)
298
{
299
	char *map = sc_primary_map;
300
	char ascii = sc_primary_map[sc];
301
	bool shift, capslock;
302
	bool letter = false;
303
 
304
	spinlock_lock(&keylock);
305
	switch (sc) {
601 palkovsky 306
	case SC_LSHIFT:
307
	case SC_RSHIFT:
508 jermar 308
	    	keyflags |= PRESSED_SHIFT;
309
		break;
601 palkovsky 310
	case SC_CAPSLOCK:
508 jermar 311
		keyflags |= PRESSED_CAPSLOCK;
312
		break;
601 palkovsky 313
	case SC_SPEC_ESCAPE:
314
		break;
315
	case SC_LEFTARR:
316
		chardev_push_character(&kbrd, 0x1b);
317
		chardev_push_character(&kbrd, 0x5b);
318
		chardev_push_character(&kbrd, 0x44);
319
		break;
320
	case SC_RIGHTARR:
321
		chardev_push_character(&kbrd, 0x1b);
322
		chardev_push_character(&kbrd, 0x5b);
323
		chardev_push_character(&kbrd, 0x43);
324
		break;
325
	case SC_UPARR:
326
		chardev_push_character(&kbrd, 0x1b);
327
		chardev_push_character(&kbrd, 0x5b);
328
		chardev_push_character(&kbrd, 0x41);
329
		break;
330
	case SC_DOWNARR:
331
		chardev_push_character(&kbrd, 0x1b);
332
		chardev_push_character(&kbrd, 0x5b);
333
		chardev_push_character(&kbrd, 0x42);
334
		break;
335
	default:
517 jermar 336
	    	letter = is_lower(ascii);
508 jermar 337
		capslock = (keyflags & PRESSED_CAPSLOCK) || (lockflags & LOCKED_CAPSLOCK);
338
		shift = keyflags & PRESSED_SHIFT;
339
		if (letter && capslock)
340
			shift = !shift;
341
		if (shift)
342
			map = sc_secondary_map;
511 jermar 343
		chardev_push_character(&kbrd, map[sc]);
508 jermar 344
		break;
345
	}
346
	spinlock_unlock(&keylock);
347
}
511 jermar 348
 
349
/* Called from getc(). */
575 palkovsky 350
void i8042_resume(chardev_t *d)
511 jermar 351
{
352
}
353
 
354
/* Called from getc(). */
575 palkovsky 355
void i8042_suspend(chardev_t *d)
511 jermar 356
{
357
}