source: mainline/uspace/drv/hid/xtkbd/xtkbd.c

Last change on this file was a635535, checked in by Jiri Svoboda <jiri@…>, 20 months ago

Move console/con_srv out of libc into a separate library

  • Property mode set to 100644
File size: 10.0 KB
RevLine 
[5b0de4c]1/*
[bb4ba92]2 * Copyright (c) 2022 Jiri Svoboda
[5b0de4c]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 */
[c072a29]29
[c8ea6eca]30/** @addtogroup xtkbd
[5b0de4c]31 * @{
32 */
33/** @file
[c072a29]34 * @brief XT keyboard driver
[5b0de4c]35 */
36
37#include <errno.h>
38#include <ddf/log.h>
[ba551ba7]39#include <io/keycode.h>
[75751db6]40#include <io/chardev.h>
[a635535]41#include <io/kbd_event.h>
[65ceb4b]42#include <ipc/kbdev.h>
43#include <abi/ipc/methods.h>
[5b0de4c]44#include "xtkbd.h"
[ecc6323]45
[a84ddf0]46/** Scancode set 1 table. */
[c072a29]47static const unsigned int scanmap_simple[] = {
[ba551ba7]48 [0x29] = KC_BACKTICK,
[a35b458]49
[ba551ba7]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,
[a35b458]60
[ba551ba7]61 [0x0c] = KC_MINUS,
62 [0x0d] = KC_EQUALS,
63 [0x0e] = KC_BACKSPACE,
[a35b458]64
[ba551ba7]65 [0x0f] = KC_TAB,
[a35b458]66
[ba551ba7]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,
[a35b458]77
[ba551ba7]78 [0x1a] = KC_LBRACKET,
79 [0x1b] = KC_RBRACKET,
[a35b458]80
[ba551ba7]81 [0x3a] = KC_CAPS_LOCK,
[a35b458]82
[ba551ba7]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,
[a35b458]92
[ba551ba7]93 [0x27] = KC_SEMICOLON,
94 [0x28] = KC_QUOTE,
95 [0x2b] = KC_BACKSLASH,
[a35b458]96
[ba551ba7]97 [0x2a] = KC_LSHIFT,
[a35b458]98
[ba551ba7]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,
[a35b458]106
[ba551ba7]107 [0x33] = KC_COMMA,
108 [0x34] = KC_PERIOD,
109 [0x35] = KC_SLASH,
[a35b458]110
[ba551ba7]111 [0x36] = KC_RSHIFT,
[a35b458]112
[ba551ba7]113 [0x1d] = KC_LCTRL,
114 [0x38] = KC_LALT,
115 [0x39] = KC_SPACE,
[a35b458]116
[ba551ba7]117 [0x01] = KC_ESCAPE,
[a35b458]118
[ba551ba7]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,
[a35b458]126
[ba551ba7]127 [0x42] = KC_F8,
128 [0x43] = KC_F9,
129 [0x44] = KC_F10,
[a35b458]130
[ba551ba7]131 [0x57] = KC_F11,
132 [0x58] = KC_F12,
[a35b458]133
[9e9d9bc6]134 [0x54] = KC_SYSREQ,
[ba551ba7]135 [0x46] = KC_SCROLL_LOCK,
[a35b458]136
[ba551ba7]137 [0x1c] = KC_ENTER,
[a35b458]138
[ba551ba7]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};
[8bb9540]155
[c072a29]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
[a84ddf0]161/** Scancode set 1 extended codes table */
[c072a29]162static const unsigned int scanmap_e0[] = {
[ba551ba7]163 [0x38] = KC_RALT,
[3dcc9d8]164 [0x1d] = KC_RCTRL,
[a35b458]165
[9e9d9bc6]166 [0x37] = KC_PRTSCR,
167 [0x46] = KC_PAUSE,
[a35b458]168
[ba551ba7]169 [0x52] = KC_INSERT,
170 [0x47] = KC_HOME,
171 [0x49] = KC_PAGE_UP,
[a35b458]172
[ba551ba7]173 [0x53] = KC_DELETE,
174 [0x4f] = KC_END,
175 [0x51] = KC_PAGE_DOWN,
[a35b458]176
[ba551ba7]177 [0x48] = KC_UP,
178 [0x4b] = KC_LEFT,
179 [0x50] = KC_DOWN,
180 [0x4d] = KC_RIGHT,
[a35b458]181
[ba551ba7]182 [0x35] = KC_NSLASH,
183 [0x1c] = KC_NENTER
184};
[8bb9540]185
[c072a29]186#define KBD_CMD_SET_LEDS 0xed
[8bb9540]187
[c072a29]188enum led_indicators {
189 LI_SCROLL = 0x01,
190 LI_NUM = 0x02,
191 LI_CAPS = 0x04
[65ceb4b]192};
[8bb9540]193
[c072a29]194static void push_event(async_sess_t *sess, kbd_event_type_t type,
195 unsigned int key)
[5b0de4c]196{
[c072a29]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);
[5b0de4c]200}
[8bb9540]201
[bb4ba92]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 */
208static 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
[a84ddf0]225/** Get data and parse scancodes.
[c072a29]226 *
[da2d75b]227 * @param arg Pointer to xt_kbd_t structure.
[c072a29]228 *
[312e5ff]229 * @return EIO on error.
[c072a29]230 *
[a84ddf0]231 */
[b7fd2a0]232static errno_t polling(void *arg)
[5b0de4c]233{
[9e9d9bc6]234 kbd_event_type_t evtype;
235 unsigned int key;
236 size_t map_size;
[15c5418]237 xt_kbd_t *kbd = arg;
[9e9d9bc6]238 uint8_t code;
239 uint8_t xcode1;
240 uint8_t xcode2;
[b7fd2a0]241 errno_t rc;
[a35b458]242
[c072a29]243 while (true) {
[bb4ba92]244 rc = xtkbd_get_code(kbd, &code);
[5d50c419]245 if (rc != EOK)
[312e5ff]246 return EIO;
[a35b458]247
[c072a29]248 /* Ignore AT command reply */
249 if ((code == KBD_ACK) || (code == KBD_RESEND))
[ecc6323]250 continue;
[a35b458]251
[c072a29]252 /* Extended set */
[ecc6323]253 if (code == KBD_SCANCODE_SET_EXTENDED) {
[9e9d9bc6]254 rc = xtkbd_get_code(kbd, &xcode1);
[5d50c419]255 if (rc != EOK)
[312e5ff]256 return EIO;
[a35b458]257
[9e9d9bc6]258 if ((xcode1 & 0x7f) == 0x2a) /* Fake shift */
[c072a29]259 continue;
[a35b458]260
[9e9d9bc6]261 /* Bit 7 indicates press/release */
262 evtype = (xcode1 & 0x80) ? KEY_RELEASE : KEY_PRESS;
[a35b458]263
[9e9d9bc6]264 map_size = sizeof(scanmap_e0) / sizeof(unsigned int);
265 key = (xcode1 & 0x7f) < map_size ?
266 scanmap_e0[xcode1 & 0x7f] : 0;
[a35b458]267
[9e9d9bc6]268 if (key != 0)
269 push_event(kbd->client_sess, evtype, key);
270 else
271 ddf_msg(LVL_WARN, "Unknown scancode: e0 %hhx", xcode1);
[a35b458]272
[9e9d9bc6]273 continue;
[ba551ba7]274 }
[a35b458]275
[c072a29]276 /* Extended special set */
277 if (code == KBD_SCANCODE_SET_EXTENDED_SPECIAL) {
[9e9d9bc6]278 rc = xtkbd_get_code(kbd, &xcode1);
[5d50c419]279 if (rc != EOK)
[c072a29]280 return EIO;
[a35b458]281
[9e9d9bc6]282 rc = xtkbd_get_code(kbd, &xcode2);
[5d50c419]283 if (rc != EOK)
[c072a29]284 return EIO;
[a35b458]285
[9e9d9bc6]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);
[c072a29]292 continue;
[9e9d9bc6]293 }
[a35b458]294
[9e9d9bc6]295 ddf_msg(LVL_WARN, "Unknown scancode: e1 %hhx %hhx",
296 xcode1, xcode2);
[c072a29]297 continue;
298 }
[a35b458]299
[bff90ba]300 /* Bit 7 indicates press/release */
[9e9d9bc6]301 evtype = (code & 0x80) ? KEY_RELEASE : KEY_PRESS;
[a35b458]302
[9e9d9bc6]303 map_size = sizeof(scanmap_simple) / sizeof(unsigned int);
304 key = (code & 0x7f) < map_size ?
305 scanmap_simple[code & 0x7f] : 0;
[a35b458]306
[c072a29]307 if (key != 0)
[9e9d9bc6]308 push_event(kbd->client_sess, evtype, key);
[c072a29]309 else
[bff90ba]310 ddf_msg(LVL_WARN, "Unknown scancode: %hhx", code);
[5b0de4c]311 }
312}
[8bb9540]313
[a84ddf0]314/** Default handler for IPC methods not handled by DDF.
315 *
[984a9ba]316 * @param fun Device function handling the call.
317 * @param icall Call data.
[c072a29]318 *
[a84ddf0]319 */
[984a9ba]320static void default_connection_handler(ddf_fun_t *fun, ipc_call_t *icall)
[65ceb4b]321{
[fafb8e5]322 const sysarg_t method = ipc_get_imethod(icall);
[56fd7cf]323 xt_kbd_t *kbd = ddf_dev_data_get(ddf_fun_get_dev(fun));
[338d54a7]324 unsigned mods;
325 async_sess_t *sess;
[65ceb4b]326
327 switch (method) {
[338d54a7]328 case KBDEV_SET_IND:
[c072a29]329 /*
330 * XT keyboards do not support setting mods,
331 * assume AT keyboard with Scan Code Set 1.
332 */
[fafb8e5]333 mods = ipc_get_arg1(icall);
[1ff1ee1]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 };
[a35b458]339
[5d50c419]340 size_t nwr;
[b7fd2a0]341 errno_t rc = chardev_write(kbd->chardev, &cmds[0], 1, &nwr);
[c657bd7]342 if (rc != EOK) {
[984a9ba]343 async_answer_0(icall, rc);
[c657bd7]344 break;
345 }
[5d50c419]346
[c657bd7]347 rc = chardev_write(kbd->chardev, &cmds[1], 1, &nwr);
[984a9ba]348 async_answer_0(icall, rc);
[65ceb4b]349 break;
[338d54a7]350 case IPC_M_CONNECT_TO_ME:
[6ff23ff]351 /*
352 * This might be ugly but async_callback_receive_start makes no
353 * difference for incorrect call and malloc failure.
354 */
[338d54a7]355 sess = async_callback_receive_start(EXCHANGE_SERIALIZE, icall);
[a35b458]356
[65ceb4b]357 /* Probably ENOMEM error, try again. */
358 if (sess == NULL) {
359 ddf_msg(LVL_WARN,
[e98fe28c]360 "Failed creating callback session");
[984a9ba]361 async_answer_0(icall, EAGAIN);
[65ceb4b]362 break;
363 }
[a35b458]364
[e98fe28c]365 if (kbd->client_sess == NULL) {
366 kbd->client_sess = sess;
367 ddf_msg(LVL_DEBUG, "Set client session");
[984a9ba]368 async_answer_0(icall, EOK);
[65ceb4b]369 } else {
[e98fe28c]370 ddf_msg(LVL_ERROR, "Client session already set");
[984a9ba]371 async_answer_0(icall, ELIMIT);
[65ceb4b]372 }
[a35b458]373
[65ceb4b]374 break;
375 default:
[c072a29]376 ddf_msg(LVL_ERROR, "Unknown method: %d.", (int)method);
[984a9ba]377 async_answer_0(icall, EINVAL);
[c072a29]378 break;
379 }
380}
381
382/** Keyboard function ops. */
383static 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 */
[b7fd2a0]395errno_t xt_kbd_init(xt_kbd_t *kbd, ddf_dev_t *dev)
[c072a29]396{
[15c5418]397 async_sess_t *parent_sess;
398 bool bound = false;
[b7fd2a0]399 errno_t rc;
[a35b458]400
[c072a29]401 kbd->client_sess = NULL;
[a35b458]402
[15c5418]403 parent_sess = ddf_dev_parent_sess_get(dev);
404 if (parent_sess == NULL) {
[c072a29]405 ddf_msg(LVL_ERROR, "Failed creating parent session.");
[15c5418]406 rc = EIO;
407 goto error;
408 }
[a35b458]409
[15c5418]410 rc = chardev_open(parent_sess, &kbd->chardev);
411 if (rc != EOK) {
412 ddf_msg(LVL_ERROR, "Failed opening character device.");
413 goto error;
[c072a29]414 }
[a35b458]415
[c072a29]416 kbd->kbd_fun = ddf_fun_create(dev, fun_exposed, "kbd");
[15c5418]417 if (kbd->kbd_fun == NULL) {
[c072a29]418 ddf_msg(LVL_ERROR, "Failed creating function 'kbd'.");
[15c5418]419 rc = ENOMEM;
420 goto error;
[c072a29]421 }
[a35b458]422
[c072a29]423 ddf_fun_set_ops(kbd->kbd_fun, &kbd_ops);
[a35b458]424
[15c5418]425 rc = ddf_fun_bind(kbd->kbd_fun);
426 if (rc != EOK) {
[c072a29]427 ddf_msg(LVL_ERROR, "Failed binding function 'kbd'.");
[15c5418]428 goto error;
[65ceb4b]429 }
[0bd66f5]430 bound = true;
[a35b458]431
[15c5418]432 rc = ddf_fun_add_to_category(kbd->kbd_fun, "keyboard");
433 if (rc != EOK) {
[c072a29]434 ddf_msg(LVL_ERROR, "Failed adding function 'kbd' to category "
435 "'keyboard'.");
[15c5418]436 goto error;
[c072a29]437 }
[a35b458]438
[c072a29]439 kbd->polling_fibril = fibril_create(polling, kbd);
[15c5418]440 if (kbd->polling_fibril == 0) {
[c072a29]441 ddf_msg(LVL_ERROR, "Failed creating polling fibril.");
[15c5418]442 rc = ENOMEM;
443 goto error;
[c072a29]444 }
[a35b458]445
[c072a29]446 fibril_add_ready(kbd->polling_fibril);
447 return EOK;
[15c5418]448error:
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;
[65ceb4b]455}
Note: See TracBrowser for help on using the repository browser.