Subversion Repositories HelenOS-historic

Rev

Rev 1353 | Go to most recent revision | Details | 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
 
30
#include <arch/kbd.h>
31
#include <key_buffer.h>
32
#include <ipc.h>
33
 
34
#define SPECIAL		'?'
35
#define KEY_RELEASE	0x80
36
 
37
/**
38
 * These codes read from i8042 data register are silently ignored.
39
 */
40
#define IGNORE_CODE	0x7f
41
 
42
#define PRESSED_SHIFT		(1<<0)
43
#define PRESSED_CAPSLOCK	(1<<1)
44
#define LOCKED_CAPSLOCK		(1<<0)
45
 
46
/** Scancodes. */
47
#define SC_ESC		0x01
48
#define SC_BACKSPACE	0x0e
49
#define SC_LSHIFT	0x2a
50
#define SC_RSHIFT	0x36
51
#define SC_CAPSLOCK	0x3a
52
#define SC_SPEC_ESCAPE  0xe0
53
#define SC_LEFTARR      0x4b
54
#define SC_RIGHTARR     0x4d
55
#define SC_UPARR        0x48
56
#define SC_DOWNARR      0x50
57
#define SC_DELETE       0x53
58
#define SC_HOME         0x47
59
#define SC_END          0x4f
60
 
61
static volatile int keyflags;		/**< Tracking of multiple keypresses. */
62
static volatile int lockflags;		/**< Tracking of multiple keys lockings. */
63
 
64
/** Primary meaning of scancodes. */
65
static char sc_primary_map[] = {
66
	SPECIAL, /* 0x00 */
67
	SPECIAL, /* 0x01 - Esc */
68
	'1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=',
69
	'\b', /* 0x0e - Backspace */
70
	'\t', 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']', '\n',
71
	SPECIAL, /* 0x1d - LCtrl */
72
	'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', '\'',
73
	'`',
74
	SPECIAL, /* 0x2a - LShift */ 
75
	'\\',
76
	'z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '/',
77
	SPECIAL, /* 0x36 - RShift */
78
	'*',
79
	SPECIAL, /* 0x38 - LAlt */
80
	' ',
81
	SPECIAL, /* 0x3a - CapsLock */
82
	SPECIAL, /* 0x3b - F1 */
83
	SPECIAL, /* 0x3c - F2 */
84
	SPECIAL, /* 0x3d - F3 */
85
	SPECIAL, /* 0x3e - F4 */
86
	SPECIAL, /* 0x3f - F5 */
87
	SPECIAL, /* 0x40 - F6 */
88
	SPECIAL, /* 0x41 - F7 */
89
	SPECIAL, /* 0x42 - F8 */
90
	SPECIAL, /* 0x43 - F9 */
91
	SPECIAL, /* 0x44 - F10 */
92
	SPECIAL, /* 0x45 - NumLock */
93
	SPECIAL, /* 0x46 - ScrollLock */
94
	'7', '8', '9', '-',
95
	'4', '5', '6', '+',
96
	'1', '2', '3',
97
	'0', '.',
98
	SPECIAL, /* 0x54 - Alt-SysRq */
99
	SPECIAL, /* 0x55 - F11/F12/PF1/FN */
100
	SPECIAL, /* 0x56 - unlabelled key next to LAlt */
101
	SPECIAL, /* 0x57 - F11 */
102
	SPECIAL, /* 0x58 - F12 */
103
	SPECIAL, /* 0x59 */
104
	SPECIAL, /* 0x5a */
105
	SPECIAL, /* 0x5b */
106
	SPECIAL, /* 0x5c */
107
	SPECIAL, /* 0x5d */
108
	SPECIAL, /* 0x5e */
109
	SPECIAL, /* 0x5f */
110
	SPECIAL, /* 0x60 */
111
	SPECIAL, /* 0x61 */
112
	SPECIAL, /* 0x62 */
113
	SPECIAL, /* 0x63 */
114
	SPECIAL, /* 0x64 */
115
	SPECIAL, /* 0x65 */
116
	SPECIAL, /* 0x66 */
117
	SPECIAL, /* 0x67 */
118
	SPECIAL, /* 0x68 */
119
	SPECIAL, /* 0x69 */
120
	SPECIAL, /* 0x6a */
121
	SPECIAL, /* 0x6b */
122
	SPECIAL, /* 0x6c */
123
	SPECIAL, /* 0x6d */
124
	SPECIAL, /* 0x6e */
125
	SPECIAL, /* 0x6f */
126
	SPECIAL, /* 0x70 */
127
	SPECIAL, /* 0x71 */
128
	SPECIAL, /* 0x72 */
129
	SPECIAL, /* 0x73 */
130
	SPECIAL, /* 0x74 */
131
	SPECIAL, /* 0x75 */
132
	SPECIAL, /* 0x76 */
133
	SPECIAL, /* 0x77 */
134
	SPECIAL, /* 0x78 */
135
	SPECIAL, /* 0x79 */
136
	SPECIAL, /* 0x7a */
137
	SPECIAL, /* 0x7b */
138
	SPECIAL, /* 0x7c */
139
	SPECIAL, /* 0x7d */
140
	SPECIAL, /* 0x7e */
141
	SPECIAL, /* 0x7f */
142
};
143
 
144
/** Secondary meaning of scancodes. */
145
static char sc_secondary_map[] = {
146
	SPECIAL, /* 0x00 */
147
	SPECIAL, /* 0x01 - Esc */
148
	'!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+',
149
	SPECIAL, /* 0x0e - Backspace */
150
	'\t', 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', '{', '}', '\n',
151
	SPECIAL, /* 0x1d - LCtrl */
152
	'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':', '"',
153
	'~',
154
	SPECIAL, /* 0x2a - LShift */ 
155
	'|',
156
	'Z', 'X', 'C', 'V', 'B', 'N', 'M', '<', '>', '?',
157
	SPECIAL, /* 0x36 - RShift */
158
	'*',
159
	SPECIAL, /* 0x38 - LAlt */
160
	' ',
161
	SPECIAL, /* 0x3a - CapsLock */
162
	SPECIAL, /* 0x3b - F1 */
163
	SPECIAL, /* 0x3c - F2 */
164
	SPECIAL, /* 0x3d - F3 */
165
	SPECIAL, /* 0x3e - F4 */
166
	SPECIAL, /* 0x3f - F5 */
167
	SPECIAL, /* 0x40 - F6 */
168
	SPECIAL, /* 0x41 - F7 */
169
	SPECIAL, /* 0x42 - F8 */
170
	SPECIAL, /* 0x43 - F9 */
171
	SPECIAL, /* 0x44 - F10 */
172
	SPECIAL, /* 0x45 - NumLock */
173
	SPECIAL, /* 0x46 - ScrollLock */
174
	'7', '8', '9', '-',
175
	'4', '5', '6', '+',
176
	'1', '2', '3',
177
	'0', '.',
178
	SPECIAL, /* 0x54 - Alt-SysRq */
179
	SPECIAL, /* 0x55 - F11/F12/PF1/FN */
180
	SPECIAL, /* 0x56 - unlabelled key next to LAlt */
181
	SPECIAL, /* 0x57 - F11 */
182
	SPECIAL, /* 0x58 - F12 */
183
	SPECIAL, /* 0x59 */
184
	SPECIAL, /* 0x5a */
185
	SPECIAL, /* 0x5b */
186
	SPECIAL, /* 0x5c */
187
	SPECIAL, /* 0x5d */
188
	SPECIAL, /* 0x5e */
189
	SPECIAL, /* 0x5f */
190
	SPECIAL, /* 0x60 */
191
	SPECIAL, /* 0x61 */
192
	SPECIAL, /* 0x62 */
193
	SPECIAL, /* 0x63 */
194
	SPECIAL, /* 0x64 */
195
	SPECIAL, /* 0x65 */
196
	SPECIAL, /* 0x66 */
197
	SPECIAL, /* 0x67 */
198
	SPECIAL, /* 0x68 */
199
	SPECIAL, /* 0x69 */
200
	SPECIAL, /* 0x6a */
201
	SPECIAL, /* 0x6b */
202
	SPECIAL, /* 0x6c */
203
	SPECIAL, /* 0x6d */
204
	SPECIAL, /* 0x6e */
205
	SPECIAL, /* 0x6f */
206
	SPECIAL, /* 0x70 */
207
	SPECIAL, /* 0x71 */
208
	SPECIAL, /* 0x72 */
209
	SPECIAL, /* 0x73 */
210
	SPECIAL, /* 0x74 */
211
	SPECIAL, /* 0x75 */
212
	SPECIAL, /* 0x76 */
213
	SPECIAL, /* 0x77 */
214
	SPECIAL, /* 0x78 */
215
	SPECIAL, /* 0x79 */
216
	SPECIAL, /* 0x7a */
217
	SPECIAL, /* 0x7b */
218
	SPECIAL, /* 0x7c */
219
	SPECIAL, /* 0x7d */
220
	SPECIAL, /* 0x7e */
221
	SPECIAL, /* 0x7f */	
222
};
223
 
224
irq_cmd_t i8042_cmds[1] = {
225
	{ CMD_PORT_READ_1, (void *)0x60, 0 }
226
};
227
 
228
irq_code_t i8042_kbd = {
229
	1,
230
	i8042_cmds
231
};
232
 
233
static int key_released(unsigned char key)
234
{
235
	switch (key) {
236
		case SC_LSHIFT:
237
		case SC_RSHIFT:
238
			keyflags &= ~PRESSED_SHIFT;
239
			break;
240
		case SC_CAPSLOCK:
241
			keyflags &= ~PRESSED_CAPSLOCK;
242
			if (lockflags & LOCKED_CAPSLOCK)
243
				lockflags &= ~LOCKED_CAPSLOCK;
244
				else
245
				lockflags |= LOCKED_CAPSLOCK;
246
			break;
247
		default:
248
			break;
249
	}
250
}
251
 
252
static int key_pressed(unsigned char key)
253
{
254
	char *map = sc_primary_map;
255
	char ascii = sc_primary_map[key];
256
	char shift, capslock;
257
	char letter = 0;
258
 
259
	switch (key) {
260
		case SC_LSHIFT:
261
		case SC_RSHIFT:
262
		    	keyflags |= PRESSED_SHIFT;
263
			break;
264
		case SC_CAPSLOCK:
265
			keyflags |= PRESSED_CAPSLOCK;
266
			break;
267
		case SC_SPEC_ESCAPE:
268
			break;
269
		case SC_LEFTARR:
270
			if (key_buffer_available() >= 3) {
271
				key_buffer_push(0x1b);	
272
				key_buffer_push(0x5b);	
273
				key_buffer_push(0x44);	
274
			}
275
			break;
276
		case SC_RIGHTARR:
277
			if (key_buffer_available() >= 3) {
278
				key_buffer_push(0x1b);	
279
				key_buffer_push(0x5b);	
280
				key_buffer_push(0x43);	
281
			}
282
			break;
283
		case SC_UPARR:
284
			if (key_buffer_available() >= 3) {
285
				key_buffer_push(0x1b);	
286
				key_buffer_push(0x5b);	
287
				key_buffer_push(0x41);	
288
			}
289
			break;
290
		case SC_DOWNARR:
291
			if (key_buffer_available() >= 3) {
292
				key_buffer_push(0x1b);	
293
				key_buffer_push(0x5b);	
294
				key_buffer_push(0x42);	
295
			}
296
			break;
297
		case SC_HOME:
298
			if (key_buffer_available() >= 3) {
299
				key_buffer_push(0x1b);	
300
				key_buffer_push(0x4f);	
301
				key_buffer_push(0x48);	
302
			}
303
			break;
304
		case SC_END:
305
			if (key_buffer_available() >= 3) {
306
				key_buffer_push(0x1b);	
307
				key_buffer_push(0x4f);	
308
				key_buffer_push(0x46);	
309
			}
310
			break;
311
		case SC_DELETE:
312
			if (key_buffer_available() >= 4) {
313
				key_buffer_push(0x1b);	
314
				key_buffer_push(0x5b);	
315
				key_buffer_push(0x33);	
316
				key_buffer_push(0x7e);	
317
			}
318
			break;
319
		default:
320
		    	letter = ((ascii >= 'a') && (ascii <= 'z'));
321
			capslock = (keyflags & PRESSED_CAPSLOCK) || (lockflags & LOCKED_CAPSLOCK);
322
			shift = keyflags & PRESSED_SHIFT;
323
			if (letter && capslock)
324
				shift = !shift;
325
			if (shift)
326
				map = sc_secondary_map;
327
			key_buffer_push(map[key]);	
328
			break;
329
	}
330
}
331
 
332
/** Register uspace irq handler
333
 * @return 
334
 */
335
int kbd_arch_init(void)
336
{
337
	return !(ipc_register_irq(1, &i8042_kbd));
338
}
339
 
340
int kbd_arch_process(int scan_code)
341
{
342
	if (scan_code != IGNORE_CODE) {
343
		if (scan_code & KEY_RELEASE)
344
			key_released(scan_code ^ KEY_RELEASE);
345
		else
346
			key_pressed(scan_code);
347
	}
348
	return 	1;
349
}