Rev 1193 | Rev 1195 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1193 | Rev 1194 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * Copyright (C) 2001-2004 Jakub Jermar |
2 | * Copyright (C) 2001-2004 Jakub Jermar |
3 | * All rights reserved. |
3 | * All rights reserved. |
4 | * |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
7 | * are met: |
8 | * |
8 | * |
9 | * - Redistributions of source code must retain the above copyright |
9 | * - Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. |
10 | * notice, this list of conditions and the following disclaimer. |
11 | * - Redistributions in binary form must reproduce the above copyright |
11 | * - Redistributions in binary form must reproduce the above copyright |
12 | * notice, this list of conditions and the following disclaimer in the |
12 | * notice, this list of conditions and the following disclaimer in the |
13 | * documentation and/or other materials provided with the distribution. |
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 |
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. |
15 | * derived from this software without specific prior written permission. |
16 | * |
16 | * |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
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 |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
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 |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
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 |
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. |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | */ |
27 | */ |
28 | 28 | ||
29 | #include <genarch/i8042/i8042.h> |
29 | #include <genarch/i8042/i8042.h> |
30 | #include <arch/drivers/i8042.h> |
30 | #include <arch/drivers/i8042.h> |
31 | #include <arch/interrupt.h> |
31 | #include <arch/interrupt.h> |
32 | #include <cpu.h> |
32 | #include <cpu.h> |
33 | #include <arch/asm.h> |
33 | #include <arch/asm.h> |
34 | #include <arch.h> |
34 | #include <arch.h> |
35 | #include <print.h> |
35 | #include <print.h> |
36 | #include <synch/spinlock.h> |
36 | #include <synch/spinlock.h> |
37 | #include <typedefs.h> |
37 | #include <typedefs.h> |
38 | #include <console/chardev.h> |
38 | #include <console/chardev.h> |
39 | #include <console/console.h> |
39 | #include <console/console.h> |
40 | #include <macros.h> |
40 | #include <macros.h> |
41 | #include <interrupt.h> |
41 | #include <interrupt.h> |
42 | 42 | ||
43 | /** |
43 | /** |
44 | * i8042 processor driver. |
44 | * i8042 processor driver. |
45 | * It takes care of low-level keyboard functions. |
45 | * It takes care of low-level keyboard functions. |
46 | */ |
46 | */ |
47 | 47 | ||
48 | /** Keyboard commands. */ |
48 | /** Keyboard commands. */ |
49 | #define KBD_ENABLE 0xf4 |
49 | #define KBD_ENABLE 0xf4 |
50 | #define KBD_DISABLE 0xf5 |
50 | #define KBD_DISABLE 0xf5 |
51 | #define KBD_ACK 0xfa |
51 | #define KBD_ACK 0xfa |
52 | 52 | ||
53 | /* |
53 | /* |
54 | * 60 Write 8042 Command Byte: next data byte written to port 60h is |
54 | * 60 Write 8042 Command Byte: next data byte written to port 60h is |
55 | * placed in 8042 command register. Format: |
55 | * placed in 8042 command register. Format: |
56 | * |
56 | * |
57 | * |7|6|5|4|3|2|1|0|8042 Command Byte |
57 | * |7|6|5|4|3|2|1|0|8042 Command Byte |
58 | * | | | | | | | `---- 1=enable output register full interrupt |
58 | * | | | | | | | `---- 1=enable output register full interrupt |
59 | * | | | | | | `----- should be 0 |
59 | * | | | | | | `----- should be 0 |
60 | * | | | | | `------ 1=set status register system, 0=clear |
60 | * | | | | | `------ 1=set status register system, 0=clear |
61 | * | | | | `------- 1=override keyboard inhibit, 0=allow inhibit |
61 | * | | | | `------- 1=override keyboard inhibit, 0=allow inhibit |
62 | * | | | `-------- disable keyboard I/O by driving clock line low |
62 | * | | | `-------- disable keyboard I/O by driving clock line low |
63 | * | | `--------- disable auxiliary device, drives clock line low |
63 | * | | `--------- disable auxiliary device, drives clock line low |
64 | * | `---------- IBM scancode translation 0=AT, 1=PC/XT |
64 | * | `---------- IBM scancode translation 0=AT, 1=PC/XT |
65 | * `----------- reserved, should be 0 |
65 | * `----------- reserved, should be 0 |
66 | */ |
66 | */ |
67 | 67 | ||
68 | #define i8042_SET_COMMAND 0x60 |
68 | #define i8042_SET_COMMAND 0x60 |
69 | #define i8042_COMMAND 0x49 |
69 | #define i8042_COMMAND 0x49 |
70 | 70 | ||
71 | #define i8042_BUFFER_FULL_MASK 0x01 |
71 | #define i8042_BUFFER_FULL_MASK 0x01 |
72 | #define i8042_WAIT_MASK 0x02 |
72 | #define i8042_WAIT_MASK 0x02 |
73 | 73 | ||
74 | #define SPECIAL '?' |
74 | #define SPECIAL '?' |
75 | #define KEY_RELEASE 0x80 |
75 | #define KEY_RELEASE 0x80 |
76 | 76 | ||
77 | /** |
77 | /** |
78 | * These codes read from i8042 data register are silently ignored. |
78 | * These codes read from i8042 data register are silently ignored. |
79 | */ |
79 | */ |
80 | #define IGNORE_CODE 0x7f |
80 | #define IGNORE_CODE 0x7f |
81 | 81 | ||
82 | static void key_released(__u8 sc); |
82 | static void key_released(__u8 sc); |
83 | static void key_pressed(__u8 sc); |
83 | static void key_pressed(__u8 sc); |
84 | static char key_read(chardev_t *d); |
84 | static char key_read(chardev_t *d); |
85 | 85 | ||
86 | #define PRESSED_SHIFT (1<<0) |
86 | #define PRESSED_SHIFT (1<<0) |
87 | #define PRESSED_CAPSLOCK (1<<1) |
87 | #define PRESSED_CAPSLOCK (1<<1) |
88 | #define LOCKED_CAPSLOCK (1<<0) |
88 | #define LOCKED_CAPSLOCK (1<<0) |
89 | 89 | ||
90 | #define ACTIVE_READ_BUFF_SIZE 16 /* Must be power of 2 */ |
90 | #define ACTIVE_READ_BUFF_SIZE 16 /* Must be power of 2 */ |
91 | 91 | ||
92 | static __u8 active_read_buff[ACTIVE_READ_BUFF_SIZE]; |
92 | static __u8 active_read_buff[ACTIVE_READ_BUFF_SIZE]; |
93 | 93 | ||
94 | SPINLOCK_INITIALIZE(keylock); /**< keylock protects keyflags and lockflags. */ |
94 | SPINLOCK_INITIALIZE(keylock); /**< keylock protects keyflags and lockflags. */ |
95 | static volatile int keyflags; /**< Tracking of multiple keypresses. */ |
95 | static volatile int keyflags; /**< Tracking of multiple keypresses. */ |
96 | static volatile int lockflags; /**< Tracking of multiple keys lockings. */ |
96 | static volatile int lockflags; /**< Tracking of multiple keys lockings. */ |
97 | 97 | ||
98 | static void i8042_suspend(chardev_t *); |
98 | static void i8042_suspend(chardev_t *); |
99 | static void i8042_resume(chardev_t *); |
99 | static void i8042_resume(chardev_t *); |
100 | 100 | ||
101 | static chardev_t kbrd; |
101 | static chardev_t kbrd; |
102 | static chardev_operations_t ops = { |
102 | static chardev_operations_t ops = { |
103 | .suspend = i8042_suspend, |
103 | .suspend = i8042_suspend, |
104 | .resume = i8042_resume, |
104 | .resume = i8042_resume, |
105 | .read = key_read |
105 | .read = key_read |
106 | }; |
106 | }; |
107 | 107 | ||
108 | /** Primary meaning of scancodes. */ |
108 | /** Primary meaning of scancodes. */ |
109 | static char sc_primary_map[] = { |
109 | static char sc_primary_map[] = { |
110 | SPECIAL, /* 0x00 */ |
110 | SPECIAL, /* 0x00 */ |
111 | SPECIAL, /* 0x01 - Esc */ |
111 | SPECIAL, /* 0x01 - Esc */ |
112 | '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=', |
112 | '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=', |
113 | '\b', /* 0x0e - Backspace */ |
113 | '\b', /* 0x0e - Backspace */ |
114 | '\t', 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']', '\n', |
114 | '\t', 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']', '\n', |
115 | SPECIAL, /* 0x1d - LCtrl */ |
115 | SPECIAL, /* 0x1d - LCtrl */ |
116 | 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', '\'', |
116 | 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', '\'', |
117 | '`', |
117 | '`', |
118 | SPECIAL, /* 0x2a - LShift */ |
118 | SPECIAL, /* 0x2a - LShift */ |
119 | '\\', |
119 | '\\', |
120 | 'z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '/', |
120 | 'z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '/', |
121 | SPECIAL, /* 0x36 - RShift */ |
121 | SPECIAL, /* 0x36 - RShift */ |
122 | '*', |
122 | '*', |
123 | SPECIAL, /* 0x38 - LAlt */ |
123 | SPECIAL, /* 0x38 - LAlt */ |
124 | ' ', |
124 | ' ', |
125 | SPECIAL, /* 0x3a - CapsLock */ |
125 | SPECIAL, /* 0x3a - CapsLock */ |
126 | SPECIAL, /* 0x3b - F1 */ |
126 | SPECIAL, /* 0x3b - F1 */ |
127 | SPECIAL, /* 0x3c - F2 */ |
127 | SPECIAL, /* 0x3c - F2 */ |
128 | SPECIAL, /* 0x3d - F3 */ |
128 | SPECIAL, /* 0x3d - F3 */ |
129 | SPECIAL, /* 0x3e - F4 */ |
129 | SPECIAL, /* 0x3e - F4 */ |
130 | SPECIAL, /* 0x3f - F5 */ |
130 | SPECIAL, /* 0x3f - F5 */ |
131 | SPECIAL, /* 0x40 - F6 */ |
131 | SPECIAL, /* 0x40 - F6 */ |
132 | SPECIAL, /* 0x41 - F7 */ |
132 | SPECIAL, /* 0x41 - F7 */ |
133 | SPECIAL, /* 0x42 - F8 */ |
133 | SPECIAL, /* 0x42 - F8 */ |
134 | SPECIAL, /* 0x43 - F9 */ |
134 | SPECIAL, /* 0x43 - F9 */ |
135 | SPECIAL, /* 0x44 - F10 */ |
135 | SPECIAL, /* 0x44 - F10 */ |
136 | SPECIAL, /* 0x45 - NumLock */ |
136 | SPECIAL, /* 0x45 - NumLock */ |
137 | SPECIAL, /* 0x46 - ScrollLock */ |
137 | SPECIAL, /* 0x46 - ScrollLock */ |
138 | '7', '8', '9', '-', |
138 | '7', '8', '9', '-', |
139 | '4', '5', '6', '+', |
139 | '4', '5', '6', '+', |
140 | '1', '2', '3', |
140 | '1', '2', '3', |
141 | '0', '.', |
141 | '0', '.', |
142 | SPECIAL, /* 0x54 - Alt-SysRq */ |
142 | SPECIAL, /* 0x54 - Alt-SysRq */ |
143 | SPECIAL, /* 0x55 - F11/F12/PF1/FN */ |
143 | SPECIAL, /* 0x55 - F11/F12/PF1/FN */ |
144 | SPECIAL, /* 0x56 - unlabelled key next to LAlt */ |
144 | SPECIAL, /* 0x56 - unlabelled key next to LAlt */ |
145 | SPECIAL, /* 0x57 - F11 */ |
145 | SPECIAL, /* 0x57 - F11 */ |
146 | SPECIAL, /* 0x58 - F12 */ |
146 | SPECIAL, /* 0x58 - F12 */ |
147 | SPECIAL, /* 0x59 */ |
147 | SPECIAL, /* 0x59 */ |
148 | SPECIAL, /* 0x5a */ |
148 | SPECIAL, /* 0x5a */ |
149 | SPECIAL, /* 0x5b */ |
149 | SPECIAL, /* 0x5b */ |
150 | SPECIAL, /* 0x5c */ |
150 | SPECIAL, /* 0x5c */ |
151 | SPECIAL, /* 0x5d */ |
151 | SPECIAL, /* 0x5d */ |
152 | SPECIAL, /* 0x5e */ |
152 | SPECIAL, /* 0x5e */ |
153 | SPECIAL, /* 0x5f */ |
153 | SPECIAL, /* 0x5f */ |
154 | SPECIAL, /* 0x60 */ |
154 | SPECIAL, /* 0x60 */ |
155 | SPECIAL, /* 0x61 */ |
155 | SPECIAL, /* 0x61 */ |
156 | SPECIAL, /* 0x62 */ |
156 | SPECIAL, /* 0x62 */ |
157 | SPECIAL, /* 0x63 */ |
157 | SPECIAL, /* 0x63 */ |
158 | SPECIAL, /* 0x64 */ |
158 | SPECIAL, /* 0x64 */ |
159 | SPECIAL, /* 0x65 */ |
159 | SPECIAL, /* 0x65 */ |
160 | SPECIAL, /* 0x66 */ |
160 | SPECIAL, /* 0x66 */ |
161 | SPECIAL, /* 0x67 */ |
161 | SPECIAL, /* 0x67 */ |
162 | SPECIAL, /* 0x68 */ |
162 | SPECIAL, /* 0x68 */ |
163 | SPECIAL, /* 0x69 */ |
163 | SPECIAL, /* 0x69 */ |
164 | SPECIAL, /* 0x6a */ |
164 | SPECIAL, /* 0x6a */ |
165 | SPECIAL, /* 0x6b */ |
165 | SPECIAL, /* 0x6b */ |
166 | SPECIAL, /* 0x6c */ |
166 | SPECIAL, /* 0x6c */ |
167 | SPECIAL, /* 0x6d */ |
167 | SPECIAL, /* 0x6d */ |
168 | SPECIAL, /* 0x6e */ |
168 | SPECIAL, /* 0x6e */ |
169 | SPECIAL, /* 0x6f */ |
169 | SPECIAL, /* 0x6f */ |
170 | SPECIAL, /* 0x70 */ |
170 | SPECIAL, /* 0x70 */ |
171 | SPECIAL, /* 0x71 */ |
171 | SPECIAL, /* 0x71 */ |
172 | SPECIAL, /* 0x72 */ |
172 | SPECIAL, /* 0x72 */ |
173 | SPECIAL, /* 0x73 */ |
173 | SPECIAL, /* 0x73 */ |
174 | SPECIAL, /* 0x74 */ |
174 | SPECIAL, /* 0x74 */ |
175 | SPECIAL, /* 0x75 */ |
175 | SPECIAL, /* 0x75 */ |
176 | SPECIAL, /* 0x76 */ |
176 | SPECIAL, /* 0x76 */ |
177 | SPECIAL, /* 0x77 */ |
177 | SPECIAL, /* 0x77 */ |
178 | SPECIAL, /* 0x78 */ |
178 | SPECIAL, /* 0x78 */ |
179 | SPECIAL, /* 0x79 */ |
179 | SPECIAL, /* 0x79 */ |
180 | SPECIAL, /* 0x7a */ |
180 | SPECIAL, /* 0x7a */ |
181 | SPECIAL, /* 0x7b */ |
181 | SPECIAL, /* 0x7b */ |
182 | SPECIAL, /* 0x7c */ |
182 | SPECIAL, /* 0x7c */ |
183 | SPECIAL, /* 0x7d */ |
183 | SPECIAL, /* 0x7d */ |
184 | SPECIAL, /* 0x7e */ |
184 | SPECIAL, /* 0x7e */ |
185 | SPECIAL, /* 0x7f */ |
185 | SPECIAL, /* 0x7f */ |
186 | }; |
186 | }; |
187 | 187 | ||
188 | /** Secondary meaning of scancodes. */ |
188 | /** Secondary meaning of scancodes. */ |
189 | static char sc_secondary_map[] = { |
189 | static char sc_secondary_map[] = { |
190 | SPECIAL, /* 0x00 */ |
190 | SPECIAL, /* 0x00 */ |
191 | SPECIAL, /* 0x01 - Esc */ |
191 | SPECIAL, /* 0x01 - Esc */ |
192 | '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+', |
192 | '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+', |
193 | SPECIAL, /* 0x0e - Backspace */ |
193 | SPECIAL, /* 0x0e - Backspace */ |
194 | '\t', 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', '{', '}', '\n', |
194 | '\t', 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', '{', '}', '\n', |
195 | SPECIAL, /* 0x1d - LCtrl */ |
195 | SPECIAL, /* 0x1d - LCtrl */ |
196 | 'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':', '"', |
196 | 'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':', '"', |
197 | '~', |
197 | '~', |
198 | SPECIAL, /* 0x2a - LShift */ |
198 | SPECIAL, /* 0x2a - LShift */ |
199 | '|', |
199 | '|', |
200 | 'Z', 'X', 'C', 'V', 'B', 'N', 'M', '<', '>', '?', |
200 | 'Z', 'X', 'C', 'V', 'B', 'N', 'M', '<', '>', '?', |
201 | SPECIAL, /* 0x36 - RShift */ |
201 | SPECIAL, /* 0x36 - RShift */ |
202 | '*', |
202 | '*', |
203 | SPECIAL, /* 0x38 - LAlt */ |
203 | SPECIAL, /* 0x38 - LAlt */ |
204 | ' ', |
204 | ' ', |
205 | SPECIAL, /* 0x3a - CapsLock */ |
205 | SPECIAL, /* 0x3a - CapsLock */ |
206 | SPECIAL, /* 0x3b - F1 */ |
206 | SPECIAL, /* 0x3b - F1 */ |
207 | SPECIAL, /* 0x3c - F2 */ |
207 | SPECIAL, /* 0x3c - F2 */ |
208 | SPECIAL, /* 0x3d - F3 */ |
208 | SPECIAL, /* 0x3d - F3 */ |
209 | SPECIAL, /* 0x3e - F4 */ |
209 | SPECIAL, /* 0x3e - F4 */ |
210 | SPECIAL, /* 0x3f - F5 */ |
210 | SPECIAL, /* 0x3f - F5 */ |
211 | SPECIAL, /* 0x40 - F6 */ |
211 | SPECIAL, /* 0x40 - F6 */ |
212 | SPECIAL, /* 0x41 - F7 */ |
212 | SPECIAL, /* 0x41 - F7 */ |
213 | SPECIAL, /* 0x42 - F8 */ |
213 | SPECIAL, /* 0x42 - F8 */ |
214 | SPECIAL, /* 0x43 - F9 */ |
214 | SPECIAL, /* 0x43 - F9 */ |
215 | SPECIAL, /* 0x44 - F10 */ |
215 | SPECIAL, /* 0x44 - F10 */ |
216 | SPECIAL, /* 0x45 - NumLock */ |
216 | SPECIAL, /* 0x45 - NumLock */ |
217 | SPECIAL, /* 0x46 - ScrollLock */ |
217 | SPECIAL, /* 0x46 - ScrollLock */ |
218 | '7', '8', '9', '-', |
218 | '7', '8', '9', '-', |
219 | '4', '5', '6', '+', |
219 | '4', '5', '6', '+', |
220 | '1', '2', '3', |
220 | '1', '2', '3', |
221 | '0', '.', |
221 | '0', '.', |
222 | SPECIAL, /* 0x54 - Alt-SysRq */ |
222 | SPECIAL, /* 0x54 - Alt-SysRq */ |
223 | SPECIAL, /* 0x55 - F11/F12/PF1/FN */ |
223 | SPECIAL, /* 0x55 - F11/F12/PF1/FN */ |
224 | SPECIAL, /* 0x56 - unlabelled key next to LAlt */ |
224 | SPECIAL, /* 0x56 - unlabelled key next to LAlt */ |
225 | SPECIAL, /* 0x57 - F11 */ |
225 | SPECIAL, /* 0x57 - F11 */ |
226 | SPECIAL, /* 0x58 - F12 */ |
226 | SPECIAL, /* 0x58 - F12 */ |
227 | SPECIAL, /* 0x59 */ |
227 | SPECIAL, /* 0x59 */ |
228 | SPECIAL, /* 0x5a */ |
228 | SPECIAL, /* 0x5a */ |
229 | SPECIAL, /* 0x5b */ |
229 | SPECIAL, /* 0x5b */ |
230 | SPECIAL, /* 0x5c */ |
230 | SPECIAL, /* 0x5c */ |
231 | SPECIAL, /* 0x5d */ |
231 | SPECIAL, /* 0x5d */ |
232 | SPECIAL, /* 0x5e */ |
232 | SPECIAL, /* 0x5e */ |
233 | SPECIAL, /* 0x5f */ |
233 | SPECIAL, /* 0x5f */ |
234 | SPECIAL, /* 0x60 */ |
234 | SPECIAL, /* 0x60 */ |
235 | SPECIAL, /* 0x61 */ |
235 | SPECIAL, /* 0x61 */ |
236 | SPECIAL, /* 0x62 */ |
236 | SPECIAL, /* 0x62 */ |
237 | SPECIAL, /* 0x63 */ |
237 | SPECIAL, /* 0x63 */ |
238 | SPECIAL, /* 0x64 */ |
238 | SPECIAL, /* 0x64 */ |
239 | SPECIAL, /* 0x65 */ |
239 | SPECIAL, /* 0x65 */ |
240 | SPECIAL, /* 0x66 */ |
240 | SPECIAL, /* 0x66 */ |
241 | SPECIAL, /* 0x67 */ |
241 | SPECIAL, /* 0x67 */ |
242 | SPECIAL, /* 0x68 */ |
242 | SPECIAL, /* 0x68 */ |
243 | SPECIAL, /* 0x69 */ |
243 | SPECIAL, /* 0x69 */ |
244 | SPECIAL, /* 0x6a */ |
244 | SPECIAL, /* 0x6a */ |
245 | SPECIAL, /* 0x6b */ |
245 | SPECIAL, /* 0x6b */ |
246 | SPECIAL, /* 0x6c */ |
246 | SPECIAL, /* 0x6c */ |
247 | SPECIAL, /* 0x6d */ |
247 | SPECIAL, /* 0x6d */ |
248 | SPECIAL, /* 0x6e */ |
248 | SPECIAL, /* 0x6e */ |
249 | SPECIAL, /* 0x6f */ |
249 | SPECIAL, /* 0x6f */ |
250 | SPECIAL, /* 0x70 */ |
250 | SPECIAL, /* 0x70 */ |
251 | SPECIAL, /* 0x71 */ |
251 | SPECIAL, /* 0x71 */ |
252 | SPECIAL, /* 0x72 */ |
252 | SPECIAL, /* 0x72 */ |
253 | SPECIAL, /* 0x73 */ |
253 | SPECIAL, /* 0x73 */ |
254 | SPECIAL, /* 0x74 */ |
254 | SPECIAL, /* 0x74 */ |
255 | SPECIAL, /* 0x75 */ |
255 | SPECIAL, /* 0x75 */ |
256 | SPECIAL, /* 0x76 */ |
256 | SPECIAL, /* 0x76 */ |
257 | SPECIAL, /* 0x77 */ |
257 | SPECIAL, /* 0x77 */ |
258 | SPECIAL, /* 0x78 */ |
258 | SPECIAL, /* 0x78 */ |
259 | SPECIAL, /* 0x79 */ |
259 | SPECIAL, /* 0x79 */ |
260 | SPECIAL, /* 0x7a */ |
260 | SPECIAL, /* 0x7a */ |
261 | SPECIAL, /* 0x7b */ |
261 | SPECIAL, /* 0x7b */ |
262 | SPECIAL, /* 0x7c */ |
262 | SPECIAL, /* 0x7c */ |
263 | SPECIAL, /* 0x7d */ |
263 | SPECIAL, /* 0x7d */ |
264 | SPECIAL, /* 0x7e */ |
264 | SPECIAL, /* 0x7e */ |
265 | SPECIAL, /* 0x7f */ |
265 | SPECIAL, /* 0x7f */ |
266 | }; |
266 | }; |
267 | 267 | ||
268 | static void i8042_interrupt(int n, istate_t *istate); |
268 | static void i8042_interrupt(int n, istate_t *istate); |
269 | static void i8042_wait(void); |
269 | static void i8042_wait(void); |
270 | 270 | ||
271 | /** Initialize i8042. */ |
271 | /** Initialize i8042. */ |
272 | void i8042_init(void) |
272 | void i8042_init(void) |
273 | { |
273 | { |
274 | exc_register(VECTOR_KBD, "i8042_interrupt", (iroutine) i8042_interrupt); |
274 | exc_register(VECTOR_KBD, "i8042_interrupt", (iroutine) i8042_interrupt); |
275 | i8042_wait(); |
275 | i8042_wait(); |
276 | i8042_command_write(i8042_SET_COMMAND); |
276 | i8042_command_write(i8042_SET_COMMAND); |
277 | i8042_wait(); |
277 | i8042_wait(); |
278 | i8042_data_write(i8042_COMMAND); |
278 | i8042_data_write(i8042_COMMAND); |
279 | i8042_wait(); |
279 | i8042_wait(); |
280 | 280 | ||
281 | trap_virtual_enable_irqs(1<<IRQ_KBD); |
281 | trap_virtual_enable_irqs(1<<IRQ_KBD); |
282 | chardev_initialize("i8042_kbd", &kbrd, &ops); |
282 | chardev_initialize("i8042_kbd", &kbrd, &ops); |
283 | stdin = &kbrd; |
283 | stdin = &kbrd; |
- | 284 | /* |
|
- | 285 | * Clear input buffer |
|
- | 286 | */ |
|
284 | { |
287 | { |
285 | int a=0; |
288 | int a=0; |
286 | while(a<20) {i8042_data_read();a++;} /*Clear input buffer*/ |
289 | while((i8042_status_read()&i8042_BUFFER_FULL_MASK)&&(a<20)) { |
- | 290 | i8042_data_read(); |
|
- | 291 | a++; |
|
- | 292 | } |
|
287 | } |
293 | } |
288 | } |
294 | } |
289 | 295 | ||
290 | /** Process i8042 interrupt. |
296 | /** Process i8042 interrupt. |
291 | * |
297 | * |
292 | * @param n Interrupt vector. |
298 | * @param n Interrupt vector. |
293 | * @param stack Interrupted stack. |
299 | * @param stack Interrupted stack. |
294 | */ |
300 | */ |
295 | void i8042_interrupt(int n, istate_t *istate) |
301 | void i8042_interrupt(int n, istate_t *istate) |
296 | { |
302 | { |
297 | __u8 x; |
303 | __u8 x; |
298 | 304 | ||
299 | trap_virtual_eoi(); |
305 | trap_virtual_eoi(); |
300 | x = i8042_data_read(); |
306 | x = i8042_data_read(); |
301 | if (x & KEY_RELEASE) |
307 | if (x & KEY_RELEASE) |
302 | key_released(x ^ KEY_RELEASE); |
308 | key_released(x ^ KEY_RELEASE); |
303 | else |
309 | else |
304 | key_pressed(x); |
310 | key_pressed(x); |
305 | } |
311 | } |
306 | 312 | ||
307 | /** Wait until the controller reads its data. */ |
313 | /** Wait until the controller reads its data. */ |
308 | void i8042_wait(void) { |
314 | void i8042_wait(void) { |
309 | while (i8042_status_read() & i8042_WAIT_MASK) { |
315 | while (i8042_status_read() & i8042_WAIT_MASK) { |
310 | /* wait */ |
316 | /* wait */ |
311 | } |
317 | } |
312 | } |
318 | } |
313 | 319 | ||
314 | /** Process release of key. |
320 | /** Process release of key. |
315 | * |
321 | * |
316 | * @param sc Scancode of the key being released. |
322 | * @param sc Scancode of the key being released. |
317 | */ |
323 | */ |
318 | void key_released(__u8 sc) |
324 | void key_released(__u8 sc) |
319 | { |
325 | { |
320 | spinlock_lock(&keylock); |
326 | spinlock_lock(&keylock); |
321 | switch (sc) { |
327 | switch (sc) { |
322 | case SC_LSHIFT: |
328 | case SC_LSHIFT: |
323 | case SC_RSHIFT: |
329 | case SC_RSHIFT: |
324 | keyflags &= ~PRESSED_SHIFT; |
330 | keyflags &= ~PRESSED_SHIFT; |
325 | break; |
331 | break; |
326 | case SC_CAPSLOCK: |
332 | case SC_CAPSLOCK: |
327 | keyflags &= ~PRESSED_CAPSLOCK; |
333 | keyflags &= ~PRESSED_CAPSLOCK; |
328 | if (lockflags & LOCKED_CAPSLOCK) |
334 | if (lockflags & LOCKED_CAPSLOCK) |
329 | lockflags &= ~LOCKED_CAPSLOCK; |
335 | lockflags &= ~LOCKED_CAPSLOCK; |
330 | else |
336 | else |
331 | lockflags |= LOCKED_CAPSLOCK; |
337 | lockflags |= LOCKED_CAPSLOCK; |
332 | break; |
338 | break; |
333 | default: |
339 | default: |
334 | break; |
340 | break; |
335 | } |
341 | } |
336 | spinlock_unlock(&keylock); |
342 | spinlock_unlock(&keylock); |
337 | } |
343 | } |
338 | 344 | ||
339 | /** Process keypress. |
345 | /** Process keypress. |
340 | * |
346 | * |
341 | * @param sc Scancode of the key being pressed. |
347 | * @param sc Scancode of the key being pressed. |
342 | */ |
348 | */ |
343 | void key_pressed(__u8 sc) |
349 | void key_pressed(__u8 sc) |
344 | { |
350 | { |
345 | char *map = sc_primary_map; |
351 | char *map = sc_primary_map; |
346 | char ascii = sc_primary_map[sc]; |
352 | char ascii = sc_primary_map[sc]; |
347 | bool shift, capslock; |
353 | bool shift, capslock; |
348 | bool letter = false; |
354 | bool letter = false; |
349 | 355 | ||
350 | spinlock_lock(&keylock); |
356 | spinlock_lock(&keylock); |
351 | switch (sc) { |
357 | switch (sc) { |
352 | case SC_LSHIFT: |
358 | case SC_LSHIFT: |
353 | case SC_RSHIFT: |
359 | case SC_RSHIFT: |
354 | keyflags |= PRESSED_SHIFT; |
360 | keyflags |= PRESSED_SHIFT; |
355 | break; |
361 | break; |
356 | case SC_CAPSLOCK: |
362 | case SC_CAPSLOCK: |
357 | keyflags |= PRESSED_CAPSLOCK; |
363 | keyflags |= PRESSED_CAPSLOCK; |
358 | break; |
364 | break; |
359 | case SC_SPEC_ESCAPE: |
365 | case SC_SPEC_ESCAPE: |
360 | break; |
366 | break; |
361 | case SC_LEFTARR: |
367 | case SC_LEFTARR: |
362 | chardev_push_character(&kbrd, 0x1b); |
368 | chardev_push_character(&kbrd, 0x1b); |
363 | chardev_push_character(&kbrd, 0x5b); |
369 | chardev_push_character(&kbrd, 0x5b); |
364 | chardev_push_character(&kbrd, 0x44); |
370 | chardev_push_character(&kbrd, 0x44); |
365 | break; |
371 | break; |
366 | case SC_RIGHTARR: |
372 | case SC_RIGHTARR: |
367 | chardev_push_character(&kbrd, 0x1b); |
373 | chardev_push_character(&kbrd, 0x1b); |
368 | chardev_push_character(&kbrd, 0x5b); |
374 | chardev_push_character(&kbrd, 0x5b); |
369 | chardev_push_character(&kbrd, 0x43); |
375 | chardev_push_character(&kbrd, 0x43); |
370 | break; |
376 | break; |
371 | case SC_UPARR: |
377 | case SC_UPARR: |
372 | chardev_push_character(&kbrd, 0x1b); |
378 | chardev_push_character(&kbrd, 0x1b); |
373 | chardev_push_character(&kbrd, 0x5b); |
379 | chardev_push_character(&kbrd, 0x5b); |
374 | chardev_push_character(&kbrd, 0x41); |
380 | chardev_push_character(&kbrd, 0x41); |
375 | break; |
381 | break; |
376 | case SC_DOWNARR: |
382 | case SC_DOWNARR: |
377 | chardev_push_character(&kbrd, 0x1b); |
383 | chardev_push_character(&kbrd, 0x1b); |
378 | chardev_push_character(&kbrd, 0x5b); |
384 | chardev_push_character(&kbrd, 0x5b); |
379 | chardev_push_character(&kbrd, 0x42); |
385 | chardev_push_character(&kbrd, 0x42); |
380 | break; |
386 | break; |
381 | case SC_HOME: |
387 | case SC_HOME: |
382 | chardev_push_character(&kbrd, 0x1b); |
388 | chardev_push_character(&kbrd, 0x1b); |
383 | chardev_push_character(&kbrd, 0x4f); |
389 | chardev_push_character(&kbrd, 0x4f); |
384 | chardev_push_character(&kbrd, 0x48); |
390 | chardev_push_character(&kbrd, 0x48); |
385 | break; |
391 | break; |
386 | case SC_END: |
392 | case SC_END: |
387 | chardev_push_character(&kbrd, 0x1b); |
393 | chardev_push_character(&kbrd, 0x1b); |
388 | chardev_push_character(&kbrd, 0x4f); |
394 | chardev_push_character(&kbrd, 0x4f); |
389 | chardev_push_character(&kbrd, 0x46); |
395 | chardev_push_character(&kbrd, 0x46); |
390 | break; |
396 | break; |
391 | case SC_DELETE: |
397 | case SC_DELETE: |
392 | chardev_push_character(&kbrd, 0x1b); |
398 | chardev_push_character(&kbrd, 0x1b); |
393 | chardev_push_character(&kbrd, 0x5b); |
399 | chardev_push_character(&kbrd, 0x5b); |
394 | chardev_push_character(&kbrd, 0x33); |
400 | chardev_push_character(&kbrd, 0x33); |
395 | chardev_push_character(&kbrd, 0x7e); |
401 | chardev_push_character(&kbrd, 0x7e); |
396 | break; |
402 | break; |
397 | default: |
403 | default: |
398 | letter = is_lower(ascii); |
404 | letter = is_lower(ascii); |
399 | capslock = (keyflags & PRESSED_CAPSLOCK) || (lockflags & LOCKED_CAPSLOCK); |
405 | capslock = (keyflags & PRESSED_CAPSLOCK) || (lockflags & LOCKED_CAPSLOCK); |
400 | shift = keyflags & PRESSED_SHIFT; |
406 | shift = keyflags & PRESSED_SHIFT; |
401 | if (letter && capslock) |
407 | if (letter && capslock) |
402 | shift = !shift; |
408 | shift = !shift; |
403 | if (shift) |
409 | if (shift) |
404 | map = sc_secondary_map; |
410 | map = sc_secondary_map; |
405 | chardev_push_character(&kbrd, map[sc]); |
411 | chardev_push_character(&kbrd, map[sc]); |
406 | break; |
412 | break; |
407 | } |
413 | } |
408 | spinlock_unlock(&keylock); |
414 | spinlock_unlock(&keylock); |
409 | } |
415 | } |
410 | 416 | ||
411 | /* Called from getc(). */ |
417 | /* Called from getc(). */ |
412 | void i8042_resume(chardev_t *d) |
418 | void i8042_resume(chardev_t *d) |
413 | { |
419 | { |
414 | } |
420 | } |
415 | 421 | ||
416 | /* Called from getc(). */ |
422 | /* Called from getc(). */ |
417 | void i8042_suspend(chardev_t *d) |
423 | void i8042_suspend(chardev_t *d) |
418 | { |
424 | { |
419 | } |
425 | } |
420 | 426 | ||
421 | static __u8 active_read_buff_read(void) |
427 | static __u8 active_read_buff_read(void) |
422 | { |
428 | { |
423 | static int i=0; |
429 | static int i=0; |
424 | i &= (ACTIVE_READ_BUFF_SIZE-1); |
430 | i &= (ACTIVE_READ_BUFF_SIZE-1); |
425 | if(!active_read_buff[i]) { |
431 | if(!active_read_buff[i]) { |
426 | return 0; |
432 | return 0; |
427 | } |
433 | } |
428 | return active_read_buff[i++]; |
434 | return active_read_buff[i++]; |
429 | } |
435 | } |
430 | 436 | ||
431 | static void active_read_buff_write(__u8 ch) |
437 | static void active_read_buff_write(__u8 ch) |
432 | { |
438 | { |
433 | static int i=0; |
439 | static int i=0; |
434 | active_read_buff[i] = ch; |
440 | active_read_buff[i] = ch; |
435 | i++; |
441 | i++; |
436 | i &= (ACTIVE_READ_BUFF_SIZE-1); |
442 | i &= (ACTIVE_READ_BUFF_SIZE-1); |
437 | active_read_buff[i]=0; |
443 | active_read_buff[i]=0; |
438 | } |
444 | } |
439 | 445 | ||
440 | 446 | ||
441 | static void active_read_key_pressed(__u8 sc) |
447 | static void active_read_key_pressed(__u8 sc) |
442 | { |
448 | { |
443 | char *map = sc_primary_map; |
449 | char *map = sc_primary_map; |
444 | char ascii = sc_primary_map[sc]; |
450 | char ascii = sc_primary_map[sc]; |
445 | bool shift, capslock; |
451 | bool shift, capslock; |
446 | bool letter = false; |
452 | bool letter = false; |
447 | 453 | ||
448 | /*spinlock_lock(&keylock);*/ |
454 | /*spinlock_lock(&keylock);*/ |
449 | switch (sc) { |
455 | switch (sc) { |
450 | case SC_LSHIFT: |
456 | case SC_LSHIFT: |
451 | case SC_RSHIFT: |
457 | case SC_RSHIFT: |
452 | keyflags |= PRESSED_SHIFT; |
458 | keyflags |= PRESSED_SHIFT; |
453 | break; |
459 | break; |
454 | case SC_CAPSLOCK: |
460 | case SC_CAPSLOCK: |
455 | keyflags |= PRESSED_CAPSLOCK; |
461 | keyflags |= PRESSED_CAPSLOCK; |
456 | break; |
462 | break; |
457 | case SC_SPEC_ESCAPE: |
463 | case SC_SPEC_ESCAPE: |
458 | break; |
464 | break; |
459 | case SC_LEFTARR: |
465 | case SC_LEFTARR: |
460 | active_read_buff_write(0x1b); |
466 | active_read_buff_write(0x1b); |
461 | active_read_buff_write(0x5b); |
467 | active_read_buff_write(0x5b); |
462 | active_read_buff_write(0x44); |
468 | active_read_buff_write(0x44); |
463 | break; |
469 | break; |
464 | case SC_RIGHTARR: |
470 | case SC_RIGHTARR: |
465 | active_read_buff_write(0x1b); |
471 | active_read_buff_write(0x1b); |
466 | active_read_buff_write(0x5b); |
472 | active_read_buff_write(0x5b); |
467 | active_read_buff_write(0x43); |
473 | active_read_buff_write(0x43); |
468 | break; |
474 | break; |
469 | case SC_UPARR: |
475 | case SC_UPARR: |
470 | active_read_buff_write(0x1b); |
476 | active_read_buff_write(0x1b); |
471 | active_read_buff_write(0x5b); |
477 | active_read_buff_write(0x5b); |
472 | active_read_buff_write(0x41); |
478 | active_read_buff_write(0x41); |
473 | break; |
479 | break; |
474 | case SC_DOWNARR: |
480 | case SC_DOWNARR: |
475 | active_read_buff_write(0x1b); |
481 | active_read_buff_write(0x1b); |
476 | active_read_buff_write(0x5b); |
482 | active_read_buff_write(0x5b); |
477 | active_read_buff_write(0x42); |
483 | active_read_buff_write(0x42); |
478 | break; |
484 | break; |
479 | case SC_HOME: |
485 | case SC_HOME: |
480 | active_read_buff_write(0x1b); |
486 | active_read_buff_write(0x1b); |
481 | active_read_buff_write(0x4f); |
487 | active_read_buff_write(0x4f); |
482 | active_read_buff_write(0x48); |
488 | active_read_buff_write(0x48); |
483 | break; |
489 | break; |
484 | case SC_END: |
490 | case SC_END: |
485 | active_read_buff_write(0x1b); |
491 | active_read_buff_write(0x1b); |
486 | active_read_buff_write(0x4f); |
492 | active_read_buff_write(0x4f); |
487 | active_read_buff_write(0x46); |
493 | active_read_buff_write(0x46); |
488 | break; |
494 | break; |
489 | case SC_DELETE: |
495 | case SC_DELETE: |
490 | active_read_buff_write(0x1b); |
496 | active_read_buff_write(0x1b); |
491 | active_read_buff_write(0x5b); |
497 | active_read_buff_write(0x5b); |
492 | active_read_buff_write(0x33); |
498 | active_read_buff_write(0x33); |
493 | active_read_buff_write(0x7e); |
499 | active_read_buff_write(0x7e); |
494 | break; |
500 | break; |
495 | default: |
501 | default: |
496 | letter = is_lower(ascii); |
502 | letter = is_lower(ascii); |
497 | capslock = (keyflags & PRESSED_CAPSLOCK) || (lockflags & LOCKED_CAPSLOCK); |
503 | capslock = (keyflags & PRESSED_CAPSLOCK) || (lockflags & LOCKED_CAPSLOCK); |
498 | shift = keyflags & PRESSED_SHIFT; |
504 | shift = keyflags & PRESSED_SHIFT; |
499 | if (letter && capslock) |
505 | if (letter && capslock) |
500 | shift = !shift; |
506 | shift = !shift; |
501 | if (shift) |
507 | if (shift) |
502 | map = sc_secondary_map; |
508 | map = sc_secondary_map; |
503 | active_read_buff_write(map[sc]); |
509 | active_read_buff_write(map[sc]); |
504 | break; |
510 | break; |
505 | } |
511 | } |
506 | /*spinlock_unlock(&keylock);*/ |
512 | /*spinlock_unlock(&keylock);*/ |
507 | 513 | ||
508 | } |
514 | } |
509 | 515 | ||
510 | static char key_read(chardev_t *d) |
516 | static char key_read(chardev_t *d) |
511 | { |
517 | { |
512 | char ch; |
518 | char ch; |
513 | 519 | ||
514 | while(!(ch = active_read_buff_read())) { |
520 | while(!(ch = active_read_buff_read())) { |
515 | __u8 x; |
521 | __u8 x; |
516 | while (!((x=i8042_status_read() & i8042_BUFFER_FULL_MASK))) |
522 | while (!((x=i8042_status_read() & i8042_BUFFER_FULL_MASK))) |
517 | ; |
523 | ; |
518 | x = i8042_data_read(); |
524 | x = i8042_data_read(); |
519 | if (x != IGNORE_CODE) { |
525 | if (x != IGNORE_CODE) { |
520 | if (x & KEY_RELEASE) |
526 | if (x & KEY_RELEASE) |
521 | key_released(x ^ KEY_RELEASE); |
527 | key_released(x ^ KEY_RELEASE); |
522 | else |
528 | else |
523 | active_read_key_pressed(x); |
529 | active_read_key_pressed(x); |
524 | } |
530 | } |
525 | } |
531 | } |
526 | return ch; |
532 | return ch; |
527 | } |
533 | } |
528 | 534 | ||
529 | /** Poll for key press and release events. |
535 | /** Poll for key press and release events. |
530 | * |
536 | * |
531 | * This function can be used to implement keyboard polling. |
537 | * This function can be used to implement keyboard polling. |
532 | */ |
538 | */ |
533 | void i8042_poll(void) |
539 | void i8042_poll(void) |
534 | { |
540 | { |
535 | __u8 x; |
541 | __u8 x; |
536 | 542 | ||
537 | while (((x = i8042_status_read() & i8042_BUFFER_FULL_MASK))) { |
543 | while (((x = i8042_status_read() & i8042_BUFFER_FULL_MASK))) { |
538 | x = i8042_data_read(); |
544 | x = i8042_data_read(); |
539 | if (x != IGNORE_CODE) { |
545 | if (x != IGNORE_CODE) { |
540 | if (x & KEY_RELEASE) |
546 | if (x & KEY_RELEASE) |
541 | key_released(x ^ KEY_RELEASE); |
547 | key_released(x ^ KEY_RELEASE); |
542 | else |
548 | else |
543 | key_pressed(x); |
549 | key_pressed(x); |
544 | } |
550 | } |
545 | } |
551 | } |
546 | } |
552 | } |
547 | 553 |