1 | /*
|
---|
2 | * Copyright (c) 2011 Jiri Svoboda
|
---|
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 | /** @addtogroup kbd_ctl
|
---|
30 | * @ingroup input
|
---|
31 | * @{
|
---|
32 | */
|
---|
33 | /**
|
---|
34 | * @file
|
---|
35 | * @brief PC keyboard controller driver.
|
---|
36 | */
|
---|
37 |
|
---|
38 | #include <io/console.h>
|
---|
39 | #include <io/keycode.h>
|
---|
40 | #include "../gsp.h"
|
---|
41 | #include "../kbd.h"
|
---|
42 | #include "../kbd_port.h"
|
---|
43 | #include "../kbd_ctl.h"
|
---|
44 |
|
---|
45 | static void pc_ctl_parse(sysarg_t);
|
---|
46 | static int pc_ctl_init(kbd_dev_t *);
|
---|
47 | static void pc_ctl_set_ind(kbd_dev_t *, unsigned int);
|
---|
48 |
|
---|
49 | kbd_ctl_ops_t pc_ctl = {
|
---|
50 | .parse = pc_ctl_parse,
|
---|
51 | .init = pc_ctl_init,
|
---|
52 | .set_ind = pc_ctl_set_ind
|
---|
53 | };
|
---|
54 |
|
---|
55 | enum dec_state {
|
---|
56 | ds_s,
|
---|
57 | ds_e
|
---|
58 | };
|
---|
59 |
|
---|
60 | enum special_code {
|
---|
61 | SC_ACK = 0xfa,
|
---|
62 | SC_NAK = 0xfe
|
---|
63 | };
|
---|
64 |
|
---|
65 | enum lock_ind_bits {
|
---|
66 | LI_SCROLL = 0x01,
|
---|
67 | LI_NUM = 0x02,
|
---|
68 | LI_CAPS = 0x04
|
---|
69 | };
|
---|
70 |
|
---|
71 | enum kbd_command {
|
---|
72 | KBD_CMD_SET_LEDS = 0xed
|
---|
73 | };
|
---|
74 |
|
---|
75 | static enum dec_state ds;
|
---|
76 | static kbd_dev_t *kbd_dev;
|
---|
77 |
|
---|
78 | static int scanmap_simple[] = {
|
---|
79 |
|
---|
80 | [0x29] = KC_BACKTICK,
|
---|
81 |
|
---|
82 | [0x02] = KC_1,
|
---|
83 | [0x03] = KC_2,
|
---|
84 | [0x04] = KC_3,
|
---|
85 | [0x05] = KC_4,
|
---|
86 | [0x06] = KC_5,
|
---|
87 | [0x07] = KC_6,
|
---|
88 | [0x08] = KC_7,
|
---|
89 | [0x09] = KC_8,
|
---|
90 | [0x0a] = KC_9,
|
---|
91 | [0x0b] = KC_0,
|
---|
92 |
|
---|
93 | [0x0c] = KC_MINUS,
|
---|
94 | [0x0d] = KC_EQUALS,
|
---|
95 | [0x0e] = KC_BACKSPACE,
|
---|
96 |
|
---|
97 | [0x0f] = KC_TAB,
|
---|
98 |
|
---|
99 | [0x10] = KC_Q,
|
---|
100 | [0x11] = KC_W,
|
---|
101 | [0x12] = KC_E,
|
---|
102 | [0x13] = KC_R,
|
---|
103 | [0x14] = KC_T,
|
---|
104 | [0x15] = KC_Y,
|
---|
105 | [0x16] = KC_U,
|
---|
106 | [0x17] = KC_I,
|
---|
107 | [0x18] = KC_O,
|
---|
108 | [0x19] = KC_P,
|
---|
109 |
|
---|
110 | [0x1a] = KC_LBRACKET,
|
---|
111 | [0x1b] = KC_RBRACKET,
|
---|
112 |
|
---|
113 | [0x3a] = KC_CAPS_LOCK,
|
---|
114 |
|
---|
115 | [0x1e] = KC_A,
|
---|
116 | [0x1f] = KC_S,
|
---|
117 | [0x20] = KC_D,
|
---|
118 | [0x21] = KC_F,
|
---|
119 | [0x22] = KC_G,
|
---|
120 | [0x23] = KC_H,
|
---|
121 | [0x24] = KC_J,
|
---|
122 | [0x25] = KC_K,
|
---|
123 | [0x26] = KC_L,
|
---|
124 |
|
---|
125 | [0x27] = KC_SEMICOLON,
|
---|
126 | [0x28] = KC_QUOTE,
|
---|
127 | [0x2b] = KC_BACKSLASH,
|
---|
128 |
|
---|
129 | [0x2a] = KC_LSHIFT,
|
---|
130 |
|
---|
131 | [0x2c] = KC_Z,
|
---|
132 | [0x2d] = KC_X,
|
---|
133 | [0x2e] = KC_C,
|
---|
134 | [0x2f] = KC_V,
|
---|
135 | [0x30] = KC_B,
|
---|
136 | [0x31] = KC_N,
|
---|
137 | [0x32] = KC_M,
|
---|
138 |
|
---|
139 | [0x33] = KC_COMMA,
|
---|
140 | [0x34] = KC_PERIOD,
|
---|
141 | [0x35] = KC_SLASH,
|
---|
142 |
|
---|
143 | [0x36] = KC_RSHIFT,
|
---|
144 |
|
---|
145 | [0x1d] = KC_LCTRL,
|
---|
146 | [0x38] = KC_LALT,
|
---|
147 | [0x39] = KC_SPACE,
|
---|
148 |
|
---|
149 | [0x01] = KC_ESCAPE,
|
---|
150 |
|
---|
151 | [0x3b] = KC_F1,
|
---|
152 | [0x3c] = KC_F2,
|
---|
153 | [0x3d] = KC_F3,
|
---|
154 | [0x3e] = KC_F4,
|
---|
155 | [0x3f] = KC_F5,
|
---|
156 | [0x40] = KC_F6,
|
---|
157 | [0x41] = KC_F7,
|
---|
158 |
|
---|
159 | [0x42] = KC_F8,
|
---|
160 | [0x43] = KC_F9,
|
---|
161 | [0x44] = KC_F10,
|
---|
162 |
|
---|
163 | [0x57] = KC_F11,
|
---|
164 | [0x58] = KC_F12,
|
---|
165 |
|
---|
166 | [0x46] = KC_SCROLL_LOCK,
|
---|
167 |
|
---|
168 | [0x1c] = KC_ENTER,
|
---|
169 |
|
---|
170 | [0x45] = KC_NUM_LOCK,
|
---|
171 | [0x37] = KC_NTIMES,
|
---|
172 | [0x4a] = KC_NMINUS,
|
---|
173 | [0x4e] = KC_NPLUS,
|
---|
174 | [0x47] = KC_N7,
|
---|
175 | [0x48] = KC_N8,
|
---|
176 | [0x49] = KC_N9,
|
---|
177 | [0x4b] = KC_N4,
|
---|
178 | [0x4c] = KC_N5,
|
---|
179 | [0x4d] = KC_N6,
|
---|
180 | [0x4f] = KC_N1,
|
---|
181 | [0x50] = KC_N2,
|
---|
182 | [0x51] = KC_N3,
|
---|
183 | [0x52] = KC_N0,
|
---|
184 | [0x53] = KC_NPERIOD
|
---|
185 | };
|
---|
186 |
|
---|
187 | static int scanmap_e0[] = {
|
---|
188 | [0x38] = KC_RALT,
|
---|
189 | [0x1d] = KC_RSHIFT,
|
---|
190 |
|
---|
191 | [0x37] = KC_PRTSCR,
|
---|
192 |
|
---|
193 | [0x52] = KC_INSERT,
|
---|
194 | [0x47] = KC_HOME,
|
---|
195 | [0x49] = KC_PAGE_UP,
|
---|
196 |
|
---|
197 | [0x53] = KC_DELETE,
|
---|
198 | [0x4f] = KC_END,
|
---|
199 | [0x51] = KC_PAGE_DOWN,
|
---|
200 |
|
---|
201 | [0x48] = KC_UP,
|
---|
202 | [0x4b] = KC_LEFT,
|
---|
203 | [0x50] = KC_DOWN,
|
---|
204 | [0x4d] = KC_RIGHT,
|
---|
205 |
|
---|
206 | [0x35] = KC_NSLASH,
|
---|
207 | [0x1c] = KC_NENTER
|
---|
208 | };
|
---|
209 |
|
---|
210 | static int pc_ctl_init(kbd_dev_t *kdev)
|
---|
211 | {
|
---|
212 | kbd_dev = kdev;
|
---|
213 | ds = ds_s;
|
---|
214 | return 0;
|
---|
215 | }
|
---|
216 |
|
---|
217 | static void pc_ctl_parse(sysarg_t scancode)
|
---|
218 | {
|
---|
219 | kbd_event_type_t type;
|
---|
220 | unsigned int key;
|
---|
221 | int *map;
|
---|
222 | size_t map_length;
|
---|
223 |
|
---|
224 | /*
|
---|
225 | * ACK/NAK are returned as response to us sending a command.
|
---|
226 | * We are not interested in them.
|
---|
227 | */
|
---|
228 | if (scancode == SC_ACK || scancode == SC_NAK)
|
---|
229 | return;
|
---|
230 |
|
---|
231 | if (scancode == 0xe0) {
|
---|
232 | ds = ds_e;
|
---|
233 | return;
|
---|
234 | }
|
---|
235 |
|
---|
236 | switch (ds) {
|
---|
237 | case ds_s:
|
---|
238 | map = scanmap_simple;
|
---|
239 | map_length = sizeof(scanmap_simple) / sizeof(int);
|
---|
240 | break;
|
---|
241 | case ds_e:
|
---|
242 | map = scanmap_e0;
|
---|
243 | map_length = sizeof(scanmap_e0) / sizeof(int);
|
---|
244 | break;
|
---|
245 | default:
|
---|
246 | map = NULL;
|
---|
247 | map_length = 0;
|
---|
248 | }
|
---|
249 |
|
---|
250 | ds = ds_s;
|
---|
251 |
|
---|
252 | if (scancode & 0x80) {
|
---|
253 | scancode &= ~0x80;
|
---|
254 | type = KEY_RELEASE;
|
---|
255 | } else {
|
---|
256 | type = KEY_PRESS;
|
---|
257 | }
|
---|
258 |
|
---|
259 | if ((size_t) scancode >= map_length)
|
---|
260 | return;
|
---|
261 |
|
---|
262 | key = map[scancode];
|
---|
263 | if (key != 0)
|
---|
264 | kbd_push_event(kbd_dev, type, key);
|
---|
265 | }
|
---|
266 |
|
---|
267 | static void pc_ctl_set_ind(kbd_dev_t *kdev, unsigned mods)
|
---|
268 | {
|
---|
269 | uint8_t b;
|
---|
270 |
|
---|
271 | b = 0;
|
---|
272 | if ((mods & KM_CAPS_LOCK) != 0)
|
---|
273 | b = b | LI_CAPS;
|
---|
274 | if ((mods & KM_NUM_LOCK) != 0)
|
---|
275 | b = b | LI_NUM;
|
---|
276 | if ((mods & KM_SCROLL_LOCK) != 0)
|
---|
277 | b = b | LI_SCROLL;
|
---|
278 |
|
---|
279 | (*kbd_dev->port_ops->write)(KBD_CMD_SET_LEDS);
|
---|
280 | (*kbd_dev->port_ops->write)(b);
|
---|
281 | }
|
---|
282 |
|
---|
283 | /**
|
---|
284 | * @}
|
---|
285 | */
|
---|