Rev 3877 | Rev 4092 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 3877 | Rev 4042 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * Copyright (c) 2006 Jakub Jermar |
2 | * Copyright (c) 2009 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 | /** @addtogroup genarch |
29 | /** @addtogroup genarch |
30 | * @{ |
30 | * @{ |
31 | */ |
31 | */ |
32 | /** |
32 | /** |
33 | * @file |
33 | * @file |
34 | * @brief Key processing. |
34 | * @brief Keyboard processing. |
35 | */ |
35 | */ |
36 | 36 | ||
37 | #include <genarch/kbd/key.h> |
37 | #include <genarch/kbrd/kbrd.h> |
38 | #include <genarch/kbd/scanc.h> |
38 | #include <genarch/kbrd/scanc.h> |
- | 39 | ||
39 | #ifdef CONFIG_I8042 |
40 | #ifdef CONFIG_PC_KBD |
40 | #include <genarch/kbd/scanc_pc.h> |
41 | #include <genarch/kbrd/scanc_pc.h> |
41 | #endif |
42 | #endif |
42 | 43 | ||
43 | #if (defined(US)) || (defined(US3)) |
44 | #ifdef CONFIG_SUN_KBD |
44 | #include <genarch/kbd/scanc_sun.h> |
45 | #include <genarch/kbrd/scanc_sun.h> |
45 | #endif |
46 | #endif |
46 | 47 | ||
47 | #include <synch/spinlock.h> |
48 | #include <synch/spinlock.h> |
48 | #include <console/chardev.h> |
49 | #include <console/chardev.h> |
- | 50 | #include <console/console.h> |
|
- | 51 | #include <proc/thread.h> |
|
- | 52 | #include <arch.h> |
|
49 | #include <macros.h> |
53 | #include <macros.h> |
50 | 54 | ||
- | 55 | #ifdef CONFIG_SUN_KBD |
|
- | 56 | # define IGNORE_CODE 0x7f |
|
- | 57 | #endif |
|
- | 58 | ||
- | 59 | #define KEY_RELEASE 0x80 |
|
- | 60 | ||
51 | #define PRESSED_SHIFT (1<<0) |
61 | #define PRESSED_SHIFT (1 << 0) |
52 | #define PRESSED_CAPSLOCK (1<<1) |
62 | #define PRESSED_CAPSLOCK (1 << 1) |
53 | #define LOCKED_CAPSLOCK (1<<0) |
63 | #define LOCKED_CAPSLOCK (1 << 0) |
- | 64 | ||
- | 65 | chardev_t kbrdin; |
|
- | 66 | static chardev_t *kbdout; |
|
54 | 67 | ||
55 | #define ACTIVE_READ_BUFF_SIZE 16 /* Must be power of 2 */ |
68 | static void kbrdin_suspend(chardev_t *d) |
- | 69 | { |
|
- | 70 | } |
|
56 | 71 | ||
57 | chardev_t kbrd; |
72 | static void kbrdin_resume(chardev_t *d) |
- | 73 | { |
|
- | 74 | } |
|
58 | 75 | ||
59 | static uint8_t active_read_buff[ACTIVE_READ_BUFF_SIZE]; |
76 | chardev_operations_t kbrdin_ops = { |
- | 77 | .suspend = kbrdin_suspend, |
|
- | 78 | .resume = kbrdin_resume, |
|
- | 79 | }; |
|
60 | 80 | ||
61 | SPINLOCK_INITIALIZE(keylock); /**< keylock protects keyflags and lockflags. */ |
81 | SPINLOCK_INITIALIZE(keylock); /**< keylock protects keyflags and lockflags. */ |
62 | static volatile int keyflags; /**< Tracking of multiple keypresses. */ |
82 | static volatile int keyflags; /**< Tracking of multiple keypresses. */ |
63 | static volatile int lockflags; /**< Tracking of multiple keys lockings. */ |
83 | static volatile int lockflags; /**< Tracking of multiple keys lockings. */ |
64 | 84 | ||
- | 85 | static void key_released(uint8_t); |
|
- | 86 | static void key_pressed(uint8_t); |
|
- | 87 | ||
- | 88 | static void kkbrd(void *arg) |
|
- | 89 | { |
|
- | 90 | chardev_t *in = (chardev_t *) arg; |
|
- | 91 | uint8_t sc; |
|
- | 92 | ||
- | 93 | while (1) { |
|
- | 94 | sc = _getc(in); |
|
- | 95 | ||
- | 96 | #ifdef CONFIG_SUN_KBD |
|
- | 97 | if (sc == IGNORE_CODE) |
|
- | 98 | continue; |
|
- | 99 | #endif |
|
- | 100 | ||
- | 101 | if (sc & KEY_RELEASE) |
|
- | 102 | key_released(sc ^ KEY_RELEASE); |
|
- | 103 | else |
|
- | 104 | key_pressed(sc); |
|
- | 105 | } |
|
- | 106 | } |
|
- | 107 | ||
- | 108 | ||
- | 109 | void kbrd_init(chardev_t *devout) |
|
- | 110 | { |
|
- | 111 | thread_t *t; |
|
- | 112 | ||
- | 113 | chardev_initialize("kbrd", &kbrdin, &kbrdin_ops); |
|
- | 114 | kbdout = devout; |
|
- | 115 | ||
- | 116 | t = thread_create(kkbrd, &kbrdin, TASK, 0, "kkbrd", false); |
|
- | 117 | ASSERT(t); |
|
- | 118 | thread_ready(t); |
|
- | 119 | } |
|
- | 120 | ||
65 | /** Process release of key. |
121 | /** Process release of key. |
66 | * |
122 | * |
67 | * @param sc Scancode of the key being released. |
123 | * @param sc Scancode of the key being released. |
68 | */ |
124 | */ |
69 | void key_released(uint8_t sc) |
125 | void key_released(uint8_t sc) |
70 | { |
126 | { |
71 | spinlock_lock(&keylock); |
127 | spinlock_lock(&keylock); |
72 | switch (sc) { |
128 | switch (sc) { |
73 | case SC_LSHIFT: |
129 | case SC_LSHIFT: |
74 | case SC_RSHIFT: |
130 | case SC_RSHIFT: |
75 | keyflags &= ~PRESSED_SHIFT; |
131 | keyflags &= ~PRESSED_SHIFT; |
76 | break; |
132 | break; |
77 | case SC_CAPSLOCK: |
133 | case SC_CAPSLOCK: |
78 | keyflags &= ~PRESSED_CAPSLOCK; |
134 | keyflags &= ~PRESSED_CAPSLOCK; |
79 | if (lockflags & LOCKED_CAPSLOCK) |
135 | if (lockflags & LOCKED_CAPSLOCK) |
80 | lockflags &= ~LOCKED_CAPSLOCK; |
136 | lockflags &= ~LOCKED_CAPSLOCK; |
81 | else |
137 | else |
82 | lockflags |= LOCKED_CAPSLOCK; |
138 | lockflags |= LOCKED_CAPSLOCK; |
83 | break; |
139 | break; |
84 | default: |
140 | default: |
85 | break; |
141 | break; |
86 | } |
142 | } |
87 | spinlock_unlock(&keylock); |
143 | spinlock_unlock(&keylock); |
88 | } |
144 | } |
89 | 145 | ||
90 | /** Process keypress. |
146 | /** Process keypress. |
91 | * |
147 | * |
92 | * @param sc Scancode of the key being pressed. |
148 | * @param sc Scancode of the key being pressed. |
93 | */ |
149 | */ |
94 | void key_pressed(uint8_t sc) |
150 | void key_pressed(uint8_t sc) |
95 | { |
151 | { |
96 | char *map = sc_primary_map; |
152 | char *map = sc_primary_map; |
97 | char ascii = sc_primary_map[sc]; |
153 | char ascii = sc_primary_map[sc]; |
98 | bool shift, capslock; |
154 | bool shift, capslock; |
99 | bool letter = false; |
155 | bool letter = false; |
100 | 156 | ||
101 | spinlock_lock(&keylock); |
157 | spinlock_lock(&keylock); |
102 | switch (sc) { |
158 | switch (sc) { |
103 | case SC_LSHIFT: |
159 | case SC_LSHIFT: |
104 | case SC_RSHIFT: |
160 | case SC_RSHIFT: |
105 | keyflags |= PRESSED_SHIFT; |
161 | keyflags |= PRESSED_SHIFT; |
106 | break; |
162 | break; |
107 | case SC_CAPSLOCK: |
163 | case SC_CAPSLOCK: |
108 | keyflags |= PRESSED_CAPSLOCK; |
164 | keyflags |= PRESSED_CAPSLOCK; |
109 | break; |
165 | break; |
110 | case SC_SPEC_ESCAPE: |
166 | case SC_SPEC_ESCAPE: |
111 | break; |
167 | break; |
112 | case SC_LEFTARR: |
168 | case SC_LEFTARR: |
113 | chardev_push_character(&kbrd, 0x1b); |
169 | chardev_push_character(kbdout, 0x1b); |
114 | chardev_push_character(&kbrd, 0x5b); |
170 | chardev_push_character(kbdout, 0x5b); |
115 | chardev_push_character(&kbrd, 0x44); |
171 | chardev_push_character(kbdout, 0x44); |
116 | break; |
172 | break; |
117 | case SC_RIGHTARR: |
173 | case SC_RIGHTARR: |
118 | chardev_push_character(&kbrd, 0x1b); |
174 | chardev_push_character(kbdout, 0x1b); |
119 | chardev_push_character(&kbrd, 0x5b); |
175 | chardev_push_character(kbdout, 0x5b); |
120 | chardev_push_character(&kbrd, 0x43); |
176 | chardev_push_character(kbdout, 0x43); |
121 | break; |
177 | break; |
122 | case SC_UPARR: |
178 | case SC_UPARR: |
123 | chardev_push_character(&kbrd, 0x1b); |
179 | chardev_push_character(kbdout, 0x1b); |
124 | chardev_push_character(&kbrd, 0x5b); |
180 | chardev_push_character(kbdout, 0x5b); |
125 | chardev_push_character(&kbrd, 0x41); |
181 | chardev_push_character(kbdout, 0x41); |
126 | break; |
182 | break; |
127 | case SC_DOWNARR: |
183 | case SC_DOWNARR: |
128 | chardev_push_character(&kbrd, 0x1b); |
184 | chardev_push_character(kbdout, 0x1b); |
129 | chardev_push_character(&kbrd, 0x5b); |
185 | chardev_push_character(kbdout, 0x5b); |
130 | chardev_push_character(&kbrd, 0x42); |
186 | chardev_push_character(kbdout, 0x42); |
131 | break; |
187 | break; |
132 | case SC_HOME: |
188 | case SC_HOME: |
133 | chardev_push_character(&kbrd, 0x1b); |
189 | chardev_push_character(kbdout, 0x1b); |
134 | chardev_push_character(&kbrd, 0x4f); |
190 | chardev_push_character(kbdout, 0x4f); |
135 | chardev_push_character(&kbrd, 0x48); |
191 | chardev_push_character(kbdout, 0x48); |
136 | break; |
192 | break; |
137 | case SC_END: |
193 | case SC_END: |
138 | chardev_push_character(&kbrd, 0x1b); |
194 | chardev_push_character(kbdout, 0x1b); |
139 | chardev_push_character(&kbrd, 0x4f); |
195 | chardev_push_character(kbdout, 0x4f); |
140 | chardev_push_character(&kbrd, 0x46); |
196 | chardev_push_character(kbdout, 0x46); |
141 | break; |
197 | break; |
142 | case SC_DELETE: |
198 | case SC_DELETE: |
143 | chardev_push_character(&kbrd, 0x1b); |
199 | chardev_push_character(kbdout, 0x1b); |
144 | chardev_push_character(&kbrd, 0x5b); |
200 | chardev_push_character(kbdout, 0x5b); |
145 | chardev_push_character(&kbrd, 0x33); |
201 | chardev_push_character(kbdout, 0x33); |
146 | chardev_push_character(&kbrd, 0x7e); |
202 | chardev_push_character(kbdout, 0x7e); |
147 | break; |
203 | break; |
148 | default: |
204 | default: |
149 | letter = islower(ascii); |
205 | letter = islower(ascii); |
150 | capslock = (keyflags & PRESSED_CAPSLOCK) || |
206 | capslock = (keyflags & PRESSED_CAPSLOCK) || |
151 | (lockflags & LOCKED_CAPSLOCK); |
207 | (lockflags & LOCKED_CAPSLOCK); |
152 | shift = keyflags & PRESSED_SHIFT; |
208 | shift = keyflags & PRESSED_SHIFT; |
153 | if (letter && capslock) |
209 | if (letter && capslock) |
154 | shift = !shift; |
210 | shift = !shift; |
155 | if (shift) |
211 | if (shift) |
156 | map = sc_secondary_map; |
212 | map = sc_secondary_map; |
157 | chardev_push_character(&kbrd, map[sc]); |
213 | chardev_push_character(kbdout, map[sc]); |
158 | break; |
214 | break; |
159 | } |
215 | } |
160 | spinlock_unlock(&keylock); |
216 | spinlock_unlock(&keylock); |
161 | } |
217 | } |
162 | - | ||
163 | uint8_t active_read_buff_read(void) |
- | |
164 | { |
- | |
165 | static int i=0; |
- | |
166 | i &= (ACTIVE_READ_BUFF_SIZE-1); |
- | |
167 | if(!active_read_buff[i]) { |
- | |
168 | return 0; |
- | |
169 | } |
- | |
170 | return active_read_buff[i++]; |
- | |
171 | } |
- | |
172 | - | ||
173 | void active_read_buff_write(uint8_t ch) |
- | |
174 | { |
- | |
175 | static int i=0; |
- | |
176 | active_read_buff[i] = ch; |
- | |
177 | i++; |
- | |
178 | i &= (ACTIVE_READ_BUFF_SIZE-1); |
- | |
179 | active_read_buff[i]=0; |
- | |
180 | } |
- | |
181 | - | ||
182 | - | ||
183 | void active_read_key_pressed(uint8_t sc) |
- | |
184 | { |
- | |
185 | char *map = sc_primary_map; |
- | |
186 | char ascii = sc_primary_map[sc]; |
- | |
187 | bool shift, capslock; |
- | |
188 | bool letter = false; |
- | |
189 | - | ||
190 | /*spinlock_lock(&keylock);*/ |
- | |
191 | switch (sc) { |
- | |
192 | case SC_LSHIFT: |
- | |
193 | case SC_RSHIFT: |
- | |
194 | keyflags |= PRESSED_SHIFT; |
- | |
195 | break; |
- | |
196 | case SC_CAPSLOCK: |
- | |
197 | keyflags |= PRESSED_CAPSLOCK; |
- | |
198 | break; |
- | |
199 | case SC_SPEC_ESCAPE: |
- | |
200 | break; |
- | |
201 | case SC_LEFTARR: |
- | |
202 | active_read_buff_write(0x1b); |
- | |
203 | active_read_buff_write(0x5b); |
- | |
204 | active_read_buff_write(0x44); |
- | |
205 | break; |
- | |
206 | case SC_RIGHTARR: |
- | |
207 | active_read_buff_write(0x1b); |
- | |
208 | active_read_buff_write(0x5b); |
- | |
209 | active_read_buff_write(0x43); |
- | |
210 | break; |
- | |
211 | case SC_UPARR: |
- | |
212 | active_read_buff_write(0x1b); |
- | |
213 | active_read_buff_write(0x5b); |
- | |
214 | active_read_buff_write(0x41); |
- | |
215 | break; |
- | |
216 | case SC_DOWNARR: |
- | |
217 | active_read_buff_write(0x1b); |
- | |
218 | active_read_buff_write(0x5b); |
- | |
219 | active_read_buff_write(0x42); |
- | |
220 | break; |
- | |
221 | case SC_HOME: |
- | |
222 | active_read_buff_write(0x1b); |
- | |
223 | active_read_buff_write(0x4f); |
- | |
224 | active_read_buff_write(0x48); |
- | |
225 | break; |
- | |
226 | case SC_END: |
- | |
227 | active_read_buff_write(0x1b); |
- | |
228 | active_read_buff_write(0x4f); |
- | |
229 | active_read_buff_write(0x46); |
- | |
230 | break; |
- | |
231 | case SC_DELETE: |
- | |
232 | active_read_buff_write(0x1b); |
- | |
233 | active_read_buff_write(0x5b); |
- | |
234 | active_read_buff_write(0x33); |
- | |
235 | active_read_buff_write(0x7e); |
- | |
236 | break; |
- | |
237 | default: |
- | |
238 | letter = islower(ascii); |
- | |
239 | capslock = (keyflags & PRESSED_CAPSLOCK) || |
- | |
240 | (lockflags & LOCKED_CAPSLOCK); |
- | |
241 | shift = keyflags & PRESSED_SHIFT; |
- | |
242 | if (letter && capslock) |
- | |
243 | shift = !shift; |
- | |
244 | if (shift) |
- | |
245 | map = sc_secondary_map; |
- | |
246 | active_read_buff_write(map[sc]); |
- | |
247 | break; |
- | |
248 | } |
- | |
249 | /*spinlock_unlock(&keylock);*/ |
- | |
250 | - | ||
251 | } |
- | |
252 | 218 | ||
253 | /** @} |
219 | /** @} |
254 | */ |
220 | */ |
255 | 221 |