[5b0de4c] | 1 | /*
|
---|
| 2 | * Copyright (c) 2011 Jan Vesely
|
---|
| 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 | /** @addtogroup drvkbd
|
---|
| 29 | * @{
|
---|
| 30 | */
|
---|
| 31 | /** @file
|
---|
| 32 | * @brief XT keyboard driver;
|
---|
| 33 | */
|
---|
| 34 |
|
---|
| 35 | #include <errno.h>
|
---|
| 36 | #include <ddf/log.h>
|
---|
[ba551ba7] | 37 | #include <io/keycode.h>
|
---|
| 38 | #include <io/console.h>
|
---|
[65ceb4b] | 39 | #include <ipc/kbdev.h>
|
---|
| 40 | #include <abi/ipc/methods.h>
|
---|
[5b0de4c] | 41 |
|
---|
[bd87ae0] | 42 | #include "chardev.h"
|
---|
[5b0de4c] | 43 | #include "xtkbd.h"
|
---|
[ecc6323] | 44 |
|
---|
[a84ddf0] | 45 | /** Scancode set 1 table. */
|
---|
[ba551ba7] | 46 | static const int scanmap_simple[] = {
|
---|
[5b0de4c] | 47 |
|
---|
[ba551ba7] | 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 | [0x46] = KC_SCROLL_LOCK,
|
---|
| 135 |
|
---|
| 136 | [0x1c] = KC_ENTER,
|
---|
| 137 |
|
---|
| 138 | [0x45] = KC_NUM_LOCK,
|
---|
| 139 | [0x37] = KC_NTIMES,
|
---|
| 140 | [0x4a] = KC_NMINUS,
|
---|
| 141 | [0x4e] = KC_NPLUS,
|
---|
| 142 | [0x47] = KC_N7,
|
---|
| 143 | [0x48] = KC_N8,
|
---|
| 144 | [0x49] = KC_N9,
|
---|
| 145 | [0x4b] = KC_N4,
|
---|
| 146 | [0x4c] = KC_N5,
|
---|
| 147 | [0x4d] = KC_N6,
|
---|
| 148 | [0x4f] = KC_N1,
|
---|
| 149 | [0x50] = KC_N2,
|
---|
| 150 | [0x51] = KC_N3,
|
---|
| 151 | [0x52] = KC_N0,
|
---|
| 152 | [0x53] = KC_NPERIOD
|
---|
| 153 | };
|
---|
[8bb9540] | 154 |
|
---|
[ecc6323] | 155 | #define KBD_ACK 0xfa
|
---|
| 156 | #define KBD_RESEND 0xfe
|
---|
| 157 | #define KBD_SCANCODE_SET_EXTENDED 0xe0
|
---|
[a84ddf0] | 158 | /** Scancode set 1 extended codes table */
|
---|
[ba551ba7] | 159 | static const int scanmap_e0[] = {
|
---|
| 160 | [0x38] = KC_RALT,
|
---|
| 161 | [0x1d] = KC_RSHIFT,
|
---|
| 162 |
|
---|
| 163 | [0x37] = KC_PRTSCR,
|
---|
| 164 |
|
---|
| 165 | [0x52] = KC_INSERT,
|
---|
| 166 | [0x47] = KC_HOME,
|
---|
| 167 | [0x49] = KC_PAGE_UP,
|
---|
| 168 |
|
---|
| 169 | [0x53] = KC_DELETE,
|
---|
| 170 | [0x4f] = KC_END,
|
---|
| 171 | [0x51] = KC_PAGE_DOWN,
|
---|
| 172 |
|
---|
| 173 | [0x48] = KC_UP,
|
---|
| 174 | [0x4b] = KC_LEFT,
|
---|
| 175 | [0x50] = KC_DOWN,
|
---|
| 176 | [0x4d] = KC_RIGHT,
|
---|
| 177 |
|
---|
| 178 | [0x35] = KC_NSLASH,
|
---|
| 179 | [0x1c] = KC_NENTER
|
---|
| 180 | };
|
---|
[8bb9540] | 181 |
|
---|
[1ff1ee1] | 182 | #define KBD_CMD_SET_LEDS 0xed
|
---|
| 183 | enum led_indicators {
|
---|
| 184 | LI_SCROLL = 0x01,
|
---|
| 185 | LI_NUM = 0x02,
|
---|
| 186 | LI_CAPS = 0x04,
|
---|
| 187 | };
|
---|
| 188 |
|
---|
[5b0de4c] | 189 | static int polling(void *);
|
---|
[bff90ba] | 190 | static void default_connection_handler(ddf_fun_t *, ipc_callid_t, ipc_call_t *);
|
---|
[8bb9540] | 191 |
|
---|
[a84ddf0] | 192 | /** Keyboard function ops. */
|
---|
[65ceb4b] | 193 | static ddf_dev_ops_t kbd_ops = {
|
---|
| 194 | .default_handler = default_connection_handler
|
---|
| 195 | };
|
---|
[8bb9540] | 196 |
|
---|
[da2d75b] | 197 | /** Initialize keyboard driver structure.
|
---|
| 198 | * @param kbd Keyboard driver structure to initialize.
|
---|
| 199 | * @param dev DDF device structure.
|
---|
| 200 | *
|
---|
| 201 | * Connects to parent, creates mouse function, starts polling fibril.
|
---|
| 202 | */
|
---|
[5b0de4c] | 203 | int xt_kbd_init(xt_kbd_t *kbd, ddf_dev_t *dev)
|
---|
| 204 | {
|
---|
| 205 | assert(kbd);
|
---|
| 206 | assert(dev);
|
---|
[e98fe28c] | 207 | kbd->client_sess = NULL;
|
---|
[56fd7cf] | 208 | kbd->parent_sess = ddf_dev_parent_sess_create(dev, EXCHANGE_SERIALIZE);
|
---|
[5b0de4c] | 209 | if (!kbd->parent_sess)
|
---|
| 210 | return ENOMEM;
|
---|
| 211 |
|
---|
| 212 | kbd->kbd_fun = ddf_fun_create(dev, fun_exposed, "kbd");
|
---|
| 213 | if (!kbd->kbd_fun) {
|
---|
| 214 | return ENOMEM;
|
---|
| 215 | }
|
---|
[56fd7cf] | 216 | ddf_fun_set_ops(kbd->kbd_fun, &kbd_ops);
|
---|
[65ceb4b] | 217 |
|
---|
| 218 | int ret = ddf_fun_bind(kbd->kbd_fun);
|
---|
| 219 | if (ret != EOK) {
|
---|
| 220 | ddf_fun_destroy(kbd->kbd_fun);
|
---|
| 221 | return ENOMEM;
|
---|
| 222 | }
|
---|
[5b0de4c] | 223 |
|
---|
[65ceb4b] | 224 | ret = ddf_fun_add_to_category(kbd->kbd_fun, "keyboard");
|
---|
[5b0de4c] | 225 | if (ret != EOK) {
|
---|
[65ceb4b] | 226 | ddf_fun_unbind(kbd->kbd_fun);
|
---|
[5b0de4c] | 227 | ddf_fun_destroy(kbd->kbd_fun);
|
---|
| 228 | return ENOMEM;
|
---|
| 229 | }
|
---|
| 230 |
|
---|
| 231 | kbd->polling_fibril = fibril_create(polling, kbd);
|
---|
| 232 | if (!kbd->polling_fibril) {
|
---|
| 233 | ddf_fun_unbind(kbd->kbd_fun);
|
---|
| 234 | ddf_fun_destroy(kbd->kbd_fun);
|
---|
| 235 | return ENOMEM;
|
---|
| 236 | }
|
---|
| 237 | fibril_add_ready(kbd->polling_fibril);
|
---|
| 238 | return EOK;
|
---|
| 239 | }
|
---|
[8bb9540] | 240 |
|
---|
[a84ddf0] | 241 | /** Get data and parse scancodes.
|
---|
[da2d75b] | 242 | * @param arg Pointer to xt_kbd_t structure.
|
---|
[a84ddf0] | 243 | * @return Never.
|
---|
| 244 | */
|
---|
[5b0de4c] | 245 | int polling(void *arg)
|
---|
| 246 | {
|
---|
| 247 | assert(arg);
|
---|
[bff90ba] | 248 | const xt_kbd_t *kbd = arg;
|
---|
[5b0de4c] | 249 |
|
---|
| 250 | assert(kbd->parent_sess);
|
---|
[bd87ae0] | 251 | async_exch_t *parent_exch = async_exchange_begin(kbd->parent_sess);
|
---|
[5b0de4c] | 252 | while (1) {
|
---|
[bd87ae0] | 253 | if (!parent_exch)
|
---|
| 254 | parent_exch = async_exchange_begin(kbd->parent_sess);
|
---|
| 255 |
|
---|
[ba551ba7] | 256 | const int *map = scanmap_simple;
|
---|
| 257 | size_t map_size = sizeof(scanmap_simple) / sizeof(int);
|
---|
| 258 |
|
---|
[5b0de4c] | 259 | uint8_t code = 0;
|
---|
[bd87ae0] | 260 | ssize_t size = chardev_read(parent_exch, &code, 1);
|
---|
[ba551ba7] | 261 |
|
---|
[ecc6323] | 262 | /** Ignore AT command reply */
|
---|
| 263 | if (code == KBD_ACK || code == KBD_RESEND) {
|
---|
| 264 | continue;
|
---|
| 265 | }
|
---|
| 266 |
|
---|
| 267 | if (code == KBD_SCANCODE_SET_EXTENDED) {
|
---|
[ba551ba7] | 268 | map = scanmap_e0;
|
---|
| 269 | map_size = sizeof(scanmap_e0) / sizeof(int);
|
---|
[bd87ae0] | 270 | size = chardev_read(parent_exch, &code, 1);
|
---|
[bff90ba] | 271 | // TODO handle print screen
|
---|
[ba551ba7] | 272 | }
|
---|
| 273 |
|
---|
[bff90ba] | 274 | /* Invalid read. */
|
---|
| 275 | if (size != 1) {
|
---|
[ba551ba7] | 276 | continue;
|
---|
| 277 | }
|
---|
| 278 |
|
---|
[bff90ba] | 279 |
|
---|
| 280 | /* Bit 7 indicates press/release */
|
---|
| 281 | const kbd_event_type_t type =
|
---|
| 282 | (code & 0x80) ? KEY_RELEASE : KEY_PRESS;
|
---|
| 283 | code &= ~0x80;
|
---|
| 284 |
|
---|
| 285 | const unsigned key = (code < map_size) ? map[code] : 0;
|
---|
[ba551ba7] | 286 | if (key != 0) {
|
---|
| 287 | async_exch_t *exch =
|
---|
[e98fe28c] | 288 | async_exchange_begin(kbd->client_sess);
|
---|
[ba551ba7] | 289 | if (!exch) {
|
---|
| 290 | ddf_msg(LVL_ERROR,
|
---|
[e98fe28c] | 291 | "Failed creating exchange.");
|
---|
[ba551ba7] | 292 | continue;
|
---|
| 293 | }
|
---|
| 294 | async_msg_4(exch, KBDEV_EVENT, type, key, 0, 0);
|
---|
| 295 | async_exchange_end(exch);
|
---|
[bff90ba] | 296 | } else {
|
---|
| 297 | ddf_msg(LVL_WARN, "Unknown scancode: %hhx", code);
|
---|
[ba551ba7] | 298 | }
|
---|
[5b0de4c] | 299 | }
|
---|
| 300 | }
|
---|
[8bb9540] | 301 |
|
---|
[a84ddf0] | 302 | /** Default handler for IPC methods not handled by DDF.
|
---|
| 303 | *
|
---|
| 304 | * @param fun Device function handling the call.
|
---|
| 305 | * @param icallid Call id.
|
---|
| 306 | * @param icall Call data.
|
---|
| 307 | */
|
---|
[65ceb4b] | 308 | void default_connection_handler(ddf_fun_t *fun,
|
---|
| 309 | ipc_callid_t icallid, ipc_call_t *icall)
|
---|
| 310 | {
|
---|
| 311 | const sysarg_t method = IPC_GET_IMETHOD(*icall);
|
---|
[56fd7cf] | 312 | xt_kbd_t *kbd = ddf_dev_data_get(ddf_fun_get_dev(fun));
|
---|
[65ceb4b] | 313 |
|
---|
| 314 | switch (method) {
|
---|
[1ff1ee1] | 315 | case KBDEV_SET_IND: {
|
---|
| 316 | /* XT keyboards do not support setting mods,
|
---|
| 317 | * assume AT keyboard with Scan Code Set 1 */
|
---|
| 318 | const unsigned mods = IPC_GET_ARG1(*icall);
|
---|
| 319 | const uint8_t status = 0 |
|
---|
| 320 | ((mods & KM_CAPS_LOCK) ? LI_CAPS : 0) |
|
---|
| 321 | ((mods & KM_NUM_LOCK) ? LI_NUM : 0) |
|
---|
| 322 | ((mods & KM_SCROLL_LOCK) ? LI_SCROLL : 0);
|
---|
| 323 | uint8_t cmds[] = { KBD_CMD_SET_LEDS, status };
|
---|
[bd87ae0] | 324 | async_exch_t *exch = async_exchange_begin(kbd->parent_sess);
|
---|
| 325 | const ssize_t size = chardev_write(exch, cmds, sizeof(cmds));
|
---|
| 326 | async_exchange_end(exch);
|
---|
[1ff1ee1] | 327 | async_answer_0(icallid, size < 0 ? size : EOK);
|
---|
[65ceb4b] | 328 | break;
|
---|
[1ff1ee1] | 329 | }
|
---|
[65ceb4b] | 330 | /* This might be ugly but async_callback_receive_start makes no
|
---|
| 331 | * difference for incorrect call and malloc failure. */
|
---|
| 332 | case IPC_M_CONNECT_TO_ME: {
|
---|
| 333 | async_sess_t *sess =
|
---|
| 334 | async_callback_receive_start(EXCHANGE_SERIALIZE, icall);
|
---|
| 335 | /* Probably ENOMEM error, try again. */
|
---|
| 336 | if (sess == NULL) {
|
---|
| 337 | ddf_msg(LVL_WARN,
|
---|
[e98fe28c] | 338 | "Failed creating callback session");
|
---|
[65ceb4b] | 339 | async_answer_0(icallid, EAGAIN);
|
---|
| 340 | break;
|
---|
| 341 | }
|
---|
[e98fe28c] | 342 | if (kbd->client_sess == NULL) {
|
---|
| 343 | kbd->client_sess = sess;
|
---|
| 344 | ddf_msg(LVL_DEBUG, "Set client session");
|
---|
[65ceb4b] | 345 | async_answer_0(icallid, EOK);
|
---|
| 346 | } else {
|
---|
[e98fe28c] | 347 | ddf_msg(LVL_ERROR, "Client session already set");
|
---|
[65ceb4b] | 348 | async_answer_0(icallid, ELIMIT);
|
---|
| 349 | }
|
---|
| 350 | break;
|
---|
| 351 | }
|
---|
| 352 | default:
|
---|
[9b56a8dd] | 353 | ddf_msg(LVL_ERROR, "Unknown method: %d.", (int)method);
|
---|
[65ceb4b] | 354 | async_answer_0(icallid, EINVAL);
|
---|
| 355 | break;
|
---|
| 356 | }
|
---|
| 357 | }
|
---|