1 | /*
|
---|
2 | * Copyright (c) 2022 Jiri Svoboda
|
---|
3 | * Copyright (c) 2011 Jan Vesely
|
---|
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 | /** @addtogroup xtkbd
|
---|
31 | * @{
|
---|
32 | */
|
---|
33 | /** @file
|
---|
34 | * @brief XT keyboard driver
|
---|
35 | */
|
---|
36 |
|
---|
37 | #include <errno.h>
|
---|
38 | #include <ddf/log.h>
|
---|
39 | #include <io/keycode.h>
|
---|
40 | #include <io/chardev.h>
|
---|
41 | #include <io/kbd_event.h>
|
---|
42 | #include <ipc/kbdev.h>
|
---|
43 | #include <abi/ipc/methods.h>
|
---|
44 | #include "xtkbd.h"
|
---|
45 |
|
---|
46 | /** Scancode set 1 table. */
|
---|
47 | static const unsigned int scanmap_simple[] = {
|
---|
48 | [0x29] = KC_BACKTICK,
|
---|
49 |
|
---|
50 | [0x02] = KC_1,
|
---|
51 | [0x03] = KC_2,
|
---|
52 | [0x04] = KC_3,
|
---|
53 | [0x05] = KC_4,
|
---|
54 | [0x06] = KC_5,
|
---|
55 | [0x07] = KC_6,
|
---|
56 | [0x08] = KC_7,
|
---|
57 | [0x09] = KC_8,
|
---|
58 | [0x0a] = KC_9,
|
---|
59 | [0x0b] = KC_0,
|
---|
60 |
|
---|
61 | [0x0c] = KC_MINUS,
|
---|
62 | [0x0d] = KC_EQUALS,
|
---|
63 | [0x0e] = KC_BACKSPACE,
|
---|
64 |
|
---|
65 | [0x0f] = KC_TAB,
|
---|
66 |
|
---|
67 | [0x10] = KC_Q,
|
---|
68 | [0x11] = KC_W,
|
---|
69 | [0x12] = KC_E,
|
---|
70 | [0x13] = KC_R,
|
---|
71 | [0x14] = KC_T,
|
---|
72 | [0x15] = KC_Y,
|
---|
73 | [0x16] = KC_U,
|
---|
74 | [0x17] = KC_I,
|
---|
75 | [0x18] = KC_O,
|
---|
76 | [0x19] = KC_P,
|
---|
77 |
|
---|
78 | [0x1a] = KC_LBRACKET,
|
---|
79 | [0x1b] = KC_RBRACKET,
|
---|
80 |
|
---|
81 | [0x3a] = KC_CAPS_LOCK,
|
---|
82 |
|
---|
83 | [0x1e] = KC_A,
|
---|
84 | [0x1f] = KC_S,
|
---|
85 | [0x20] = KC_D,
|
---|
86 | [0x21] = KC_F,
|
---|
87 | [0x22] = KC_G,
|
---|
88 | [0x23] = KC_H,
|
---|
89 | [0x24] = KC_J,
|
---|
90 | [0x25] = KC_K,
|
---|
91 | [0x26] = KC_L,
|
---|
92 |
|
---|
93 | [0x27] = KC_SEMICOLON,
|
---|
94 | [0x28] = KC_QUOTE,
|
---|
95 | [0x2b] = KC_BACKSLASH,
|
---|
96 |
|
---|
97 | [0x2a] = KC_LSHIFT,
|
---|
98 |
|
---|
99 | [0x2c] = KC_Z,
|
---|
100 | [0x2d] = KC_X,
|
---|
101 | [0x2e] = KC_C,
|
---|
102 | [0x2f] = KC_V,
|
---|
103 | [0x30] = KC_B,
|
---|
104 | [0x31] = KC_N,
|
---|
105 | [0x32] = KC_M,
|
---|
106 |
|
---|
107 | [0x33] = KC_COMMA,
|
---|
108 | [0x34] = KC_PERIOD,
|
---|
109 | [0x35] = KC_SLASH,
|
---|
110 |
|
---|
111 | [0x36] = KC_RSHIFT,
|
---|
112 |
|
---|
113 | [0x1d] = KC_LCTRL,
|
---|
114 | [0x38] = KC_LALT,
|
---|
115 | [0x39] = KC_SPACE,
|
---|
116 |
|
---|
117 | [0x01] = KC_ESCAPE,
|
---|
118 |
|
---|
119 | [0x3b] = KC_F1,
|
---|
120 | [0x3c] = KC_F2,
|
---|
121 | [0x3d] = KC_F3,
|
---|
122 | [0x3e] = KC_F4,
|
---|
123 | [0x3f] = KC_F5,
|
---|
124 | [0x40] = KC_F6,
|
---|
125 | [0x41] = KC_F7,
|
---|
126 |
|
---|
127 | [0x42] = KC_F8,
|
---|
128 | [0x43] = KC_F9,
|
---|
129 | [0x44] = KC_F10,
|
---|
130 |
|
---|
131 | [0x57] = KC_F11,
|
---|
132 | [0x58] = KC_F12,
|
---|
133 |
|
---|
134 | [0x54] = KC_SYSREQ,
|
---|
135 | [0x46] = KC_SCROLL_LOCK,
|
---|
136 |
|
---|
137 | [0x1c] = KC_ENTER,
|
---|
138 |
|
---|
139 | [0x45] = KC_NUM_LOCK,
|
---|
140 | [0x37] = KC_NTIMES,
|
---|
141 | [0x4a] = KC_NMINUS,
|
---|
142 | [0x4e] = KC_NPLUS,
|
---|
143 | [0x47] = KC_N7,
|
---|
144 | [0x48] = KC_N8,
|
---|
145 | [0x49] = KC_N9,
|
---|
146 | [0x4b] = KC_N4,
|
---|
147 | [0x4c] = KC_N5,
|
---|
148 | [0x4d] = KC_N6,
|
---|
149 | [0x4f] = KC_N1,
|
---|
150 | [0x50] = KC_N2,
|
---|
151 | [0x51] = KC_N3,
|
---|
152 | [0x52] = KC_N0,
|
---|
153 | [0x53] = KC_NPERIOD
|
---|
154 | };
|
---|
155 |
|
---|
156 | #define KBD_ACK 0xfa
|
---|
157 | #define KBD_RESEND 0xfe
|
---|
158 | #define KBD_SCANCODE_SET_EXTENDED 0xe0
|
---|
159 | #define KBD_SCANCODE_SET_EXTENDED_SPECIAL 0xe1
|
---|
160 |
|
---|
161 | /** Scancode set 1 extended codes table */
|
---|
162 | static const unsigned int scanmap_e0[] = {
|
---|
163 | [0x38] = KC_RALT,
|
---|
164 | [0x1d] = KC_RCTRL,
|
---|
165 |
|
---|
166 | [0x37] = KC_PRTSCR,
|
---|
167 | [0x46] = KC_PAUSE,
|
---|
168 |
|
---|
169 | [0x52] = KC_INSERT,
|
---|
170 | [0x47] = KC_HOME,
|
---|
171 | [0x49] = KC_PAGE_UP,
|
---|
172 |
|
---|
173 | [0x53] = KC_DELETE,
|
---|
174 | [0x4f] = KC_END,
|
---|
175 | [0x51] = KC_PAGE_DOWN,
|
---|
176 |
|
---|
177 | [0x48] = KC_UP,
|
---|
178 | [0x4b] = KC_LEFT,
|
---|
179 | [0x50] = KC_DOWN,
|
---|
180 | [0x4d] = KC_RIGHT,
|
---|
181 |
|
---|
182 | [0x35] = KC_NSLASH,
|
---|
183 | [0x1c] = KC_NENTER
|
---|
184 | };
|
---|
185 |
|
---|
186 | #define KBD_CMD_SET_LEDS 0xed
|
---|
187 |
|
---|
188 | enum led_indicators {
|
---|
189 | LI_SCROLL = 0x01,
|
---|
190 | LI_NUM = 0x02,
|
---|
191 | LI_CAPS = 0x04
|
---|
192 | };
|
---|
193 |
|
---|
194 | static void push_event(async_sess_t *sess, kbd_event_type_t type,
|
---|
195 | unsigned int key)
|
---|
196 | {
|
---|
197 | async_exch_t *exch = async_exchange_begin(sess);
|
---|
198 | async_msg_4(exch, KBDEV_EVENT, type, key, 0, 0);
|
---|
199 | async_exchange_end(exch);
|
---|
200 | }
|
---|
201 |
|
---|
202 | /** Get one scancode byte from the keyboard.
|
---|
203 | *
|
---|
204 | * @param kbd Keyboard
|
---|
205 | * @param code Place to store scancode byte
|
---|
206 | * @return EOK on success or an error code
|
---|
207 | */
|
---|
208 | static errno_t xtkbd_get_code(xt_kbd_t *kbd, uint8_t *code)
|
---|
209 | {
|
---|
210 | errno_t rc;
|
---|
211 | size_t nread;
|
---|
212 |
|
---|
213 | *code = 0;
|
---|
214 | rc = chardev_read(kbd->chardev, code, 1, &nread, chardev_f_none);
|
---|
215 | if (rc != EOK) {
|
---|
216 | ddf_msg(LVL_WARN, "Error reading from keyboard device.");
|
---|
217 | return EIO;
|
---|
218 | }
|
---|
219 |
|
---|
220 | ddf_msg(LVL_DEBUG, "Read scancode: 0x%02hhx", *code);
|
---|
221 |
|
---|
222 | return EOK;
|
---|
223 | }
|
---|
224 |
|
---|
225 | /** Get data and parse scancodes.
|
---|
226 | *
|
---|
227 | * @param arg Pointer to xt_kbd_t structure.
|
---|
228 | *
|
---|
229 | * @return EIO on error.
|
---|
230 | *
|
---|
231 | */
|
---|
232 | static errno_t polling(void *arg)
|
---|
233 | {
|
---|
234 | kbd_event_type_t evtype;
|
---|
235 | unsigned int key;
|
---|
236 | size_t map_size;
|
---|
237 | xt_kbd_t *kbd = arg;
|
---|
238 | uint8_t code;
|
---|
239 | uint8_t xcode1;
|
---|
240 | uint8_t xcode2;
|
---|
241 | errno_t rc;
|
---|
242 |
|
---|
243 | while (true) {
|
---|
244 | rc = xtkbd_get_code(kbd, &code);
|
---|
245 | if (rc != EOK)
|
---|
246 | return EIO;
|
---|
247 |
|
---|
248 | /* Ignore AT command reply */
|
---|
249 | if ((code == KBD_ACK) || (code == KBD_RESEND))
|
---|
250 | continue;
|
---|
251 |
|
---|
252 | /* Extended set */
|
---|
253 | if (code == KBD_SCANCODE_SET_EXTENDED) {
|
---|
254 | rc = xtkbd_get_code(kbd, &xcode1);
|
---|
255 | if (rc != EOK)
|
---|
256 | return EIO;
|
---|
257 |
|
---|
258 | if ((xcode1 & 0x7f) == 0x2a) /* Fake shift */
|
---|
259 | continue;
|
---|
260 |
|
---|
261 | /* Bit 7 indicates press/release */
|
---|
262 | evtype = (xcode1 & 0x80) ? KEY_RELEASE : KEY_PRESS;
|
---|
263 |
|
---|
264 | map_size = sizeof(scanmap_e0) / sizeof(unsigned int);
|
---|
265 | key = (xcode1 & 0x7f) < map_size ?
|
---|
266 | scanmap_e0[xcode1 & 0x7f] : 0;
|
---|
267 |
|
---|
268 | if (key != 0)
|
---|
269 | push_event(kbd->client_sess, evtype, key);
|
---|
270 | else
|
---|
271 | ddf_msg(LVL_WARN, "Unknown scancode: e0 %hhx", xcode1);
|
---|
272 |
|
---|
273 | continue;
|
---|
274 | }
|
---|
275 |
|
---|
276 | /* Extended special set */
|
---|
277 | if (code == KBD_SCANCODE_SET_EXTENDED_SPECIAL) {
|
---|
278 | rc = xtkbd_get_code(kbd, &xcode1);
|
---|
279 | if (rc != EOK)
|
---|
280 | return EIO;
|
---|
281 |
|
---|
282 | rc = xtkbd_get_code(kbd, &xcode2);
|
---|
283 | if (rc != EOK)
|
---|
284 | return EIO;
|
---|
285 |
|
---|
286 | if ((xcode1 & 0x80) == (xcode2 & 0x80) &&
|
---|
287 | (xcode1 & 0x7f) == 0x1d && (xcode2 & 0x7f) == 0x45) {
|
---|
288 | /* Pause */
|
---|
289 | evtype = (xcode1 & 0x80) ? KEY_RELEASE :
|
---|
290 | KEY_PRESS;
|
---|
291 | push_event(kbd->client_sess, evtype, KC_PAUSE);
|
---|
292 | continue;
|
---|
293 | }
|
---|
294 |
|
---|
295 | ddf_msg(LVL_WARN, "Unknown scancode: e1 %hhx %hhx",
|
---|
296 | xcode1, xcode2);
|
---|
297 | continue;
|
---|
298 | }
|
---|
299 |
|
---|
300 | /* Bit 7 indicates press/release */
|
---|
301 | evtype = (code & 0x80) ? KEY_RELEASE : KEY_PRESS;
|
---|
302 |
|
---|
303 | map_size = sizeof(scanmap_simple) / sizeof(unsigned int);
|
---|
304 | key = (code & 0x7f) < map_size ?
|
---|
305 | scanmap_simple[code & 0x7f] : 0;
|
---|
306 |
|
---|
307 | if (key != 0)
|
---|
308 | push_event(kbd->client_sess, evtype, key);
|
---|
309 | else
|
---|
310 | ddf_msg(LVL_WARN, "Unknown scancode: %hhx", code);
|
---|
311 | }
|
---|
312 | }
|
---|
313 |
|
---|
314 | /** Default handler for IPC methods not handled by DDF.
|
---|
315 | *
|
---|
316 | * @param fun Device function handling the call.
|
---|
317 | * @param icall Call data.
|
---|
318 | *
|
---|
319 | */
|
---|
320 | static void default_connection_handler(ddf_fun_t *fun, ipc_call_t *icall)
|
---|
321 | {
|
---|
322 | const sysarg_t method = ipc_get_imethod(icall);
|
---|
323 | xt_kbd_t *kbd = ddf_dev_data_get(ddf_fun_get_dev(fun));
|
---|
324 | unsigned mods;
|
---|
325 | async_sess_t *sess;
|
---|
326 |
|
---|
327 | switch (method) {
|
---|
328 | case KBDEV_SET_IND:
|
---|
329 | /*
|
---|
330 | * XT keyboards do not support setting mods,
|
---|
331 | * assume AT keyboard with Scan Code Set 1.
|
---|
332 | */
|
---|
333 | mods = ipc_get_arg1(icall);
|
---|
334 | const uint8_t status = 0 |
|
---|
335 | ((mods & KM_CAPS_LOCK) ? LI_CAPS : 0) |
|
---|
336 | ((mods & KM_NUM_LOCK) ? LI_NUM : 0) |
|
---|
337 | ((mods & KM_SCROLL_LOCK) ? LI_SCROLL : 0);
|
---|
338 | uint8_t cmds[] = { KBD_CMD_SET_LEDS, status };
|
---|
339 |
|
---|
340 | size_t nwr;
|
---|
341 | errno_t rc = chardev_write(kbd->chardev, &cmds[0], 1, &nwr);
|
---|
342 | if (rc != EOK) {
|
---|
343 | async_answer_0(icall, rc);
|
---|
344 | break;
|
---|
345 | }
|
---|
346 |
|
---|
347 | rc = chardev_write(kbd->chardev, &cmds[1], 1, &nwr);
|
---|
348 | async_answer_0(icall, rc);
|
---|
349 | break;
|
---|
350 | case IPC_M_CONNECT_TO_ME:
|
---|
351 | /*
|
---|
352 | * This might be ugly but async_callback_receive_start makes no
|
---|
353 | * difference for incorrect call and malloc failure.
|
---|
354 | */
|
---|
355 | sess = async_callback_receive_start(EXCHANGE_SERIALIZE, icall);
|
---|
356 |
|
---|
357 | /* Probably ENOMEM error, try again. */
|
---|
358 | if (sess == NULL) {
|
---|
359 | ddf_msg(LVL_WARN,
|
---|
360 | "Failed creating callback session");
|
---|
361 | async_answer_0(icall, EAGAIN);
|
---|
362 | break;
|
---|
363 | }
|
---|
364 |
|
---|
365 | if (kbd->client_sess == NULL) {
|
---|
366 | kbd->client_sess = sess;
|
---|
367 | ddf_msg(LVL_DEBUG, "Set client session");
|
---|
368 | async_answer_0(icall, EOK);
|
---|
369 | } else {
|
---|
370 | ddf_msg(LVL_ERROR, "Client session already set");
|
---|
371 | async_answer_0(icall, ELIMIT);
|
---|
372 | }
|
---|
373 |
|
---|
374 | break;
|
---|
375 | default:
|
---|
376 | ddf_msg(LVL_ERROR, "Unknown method: %d.", (int)method);
|
---|
377 | async_answer_0(icall, EINVAL);
|
---|
378 | break;
|
---|
379 | }
|
---|
380 | }
|
---|
381 |
|
---|
382 | /** Keyboard function ops. */
|
---|
383 | static ddf_dev_ops_t kbd_ops = {
|
---|
384 | .default_handler = default_connection_handler
|
---|
385 | };
|
---|
386 |
|
---|
387 | /** Initialize keyboard driver structure.
|
---|
388 | *
|
---|
389 | * @param kbd Keyboard driver structure to initialize.
|
---|
390 | * @param dev DDF device structure.
|
---|
391 | *
|
---|
392 | * Connects to parent, creates keyboard function, starts polling fibril.
|
---|
393 | *
|
---|
394 | */
|
---|
395 | errno_t xt_kbd_init(xt_kbd_t *kbd, ddf_dev_t *dev)
|
---|
396 | {
|
---|
397 | async_sess_t *parent_sess;
|
---|
398 | bool bound = false;
|
---|
399 | errno_t rc;
|
---|
400 |
|
---|
401 | kbd->client_sess = NULL;
|
---|
402 |
|
---|
403 | parent_sess = ddf_dev_parent_sess_get(dev);
|
---|
404 | if (parent_sess == NULL) {
|
---|
405 | ddf_msg(LVL_ERROR, "Failed creating parent session.");
|
---|
406 | rc = EIO;
|
---|
407 | goto error;
|
---|
408 | }
|
---|
409 |
|
---|
410 | rc = chardev_open(parent_sess, &kbd->chardev);
|
---|
411 | if (rc != EOK) {
|
---|
412 | ddf_msg(LVL_ERROR, "Failed opening character device.");
|
---|
413 | goto error;
|
---|
414 | }
|
---|
415 |
|
---|
416 | kbd->kbd_fun = ddf_fun_create(dev, fun_exposed, "kbd");
|
---|
417 | if (kbd->kbd_fun == NULL) {
|
---|
418 | ddf_msg(LVL_ERROR, "Failed creating function 'kbd'.");
|
---|
419 | rc = ENOMEM;
|
---|
420 | goto error;
|
---|
421 | }
|
---|
422 |
|
---|
423 | ddf_fun_set_ops(kbd->kbd_fun, &kbd_ops);
|
---|
424 |
|
---|
425 | rc = ddf_fun_bind(kbd->kbd_fun);
|
---|
426 | if (rc != EOK) {
|
---|
427 | ddf_msg(LVL_ERROR, "Failed binding function 'kbd'.");
|
---|
428 | goto error;
|
---|
429 | }
|
---|
430 | bound = true;
|
---|
431 |
|
---|
432 | rc = ddf_fun_add_to_category(kbd->kbd_fun, "keyboard");
|
---|
433 | if (rc != EOK) {
|
---|
434 | ddf_msg(LVL_ERROR, "Failed adding function 'kbd' to category "
|
---|
435 | "'keyboard'.");
|
---|
436 | goto error;
|
---|
437 | }
|
---|
438 |
|
---|
439 | kbd->polling_fibril = fibril_create(polling, kbd);
|
---|
440 | if (kbd->polling_fibril == 0) {
|
---|
441 | ddf_msg(LVL_ERROR, "Failed creating polling fibril.");
|
---|
442 | rc = ENOMEM;
|
---|
443 | goto error;
|
---|
444 | }
|
---|
445 |
|
---|
446 | fibril_add_ready(kbd->polling_fibril);
|
---|
447 | return EOK;
|
---|
448 | error:
|
---|
449 | if (bound)
|
---|
450 | ddf_fun_unbind(kbd->kbd_fun);
|
---|
451 | if (kbd->kbd_fun != NULL)
|
---|
452 | ddf_fun_destroy(kbd->kbd_fun);
|
---|
453 | chardev_close(kbd->chardev);
|
---|
454 | return rc;
|
---|
455 | }
|
---|