source: mainline/uspace/drv/hid/xtkbd/xtkbd.c@ 58168e0

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 58168e0 was fafb8e5, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 7 years ago

Mechanically lowercase IPC_SET_*/IPC_GET_*

  • Property mode set to 100644
File size: 10.3 KB
Line 
1/*
2 * Copyright (c) 2011 Jan Vesely
3 * Copyright (c) 2017 Jiri Svoboda
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/console.h>
42#include <ipc/kbdev.h>
43#include <abi/ipc/methods.h>
44#include "xtkbd.h"
45
46/** Scancode set 1 table. */
47static 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 [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};
154
155#define KBD_ACK 0xfa
156#define KBD_RESEND 0xfe
157#define KBD_SCANCODE_SET_EXTENDED 0xe0
158#define KBD_SCANCODE_SET_EXTENDED_SPECIAL 0xe1
159
160/** Scancode set 1 extended codes table */
161static const unsigned int scanmap_e0[] = {
162 [0x38] = KC_RALT,
163 [0x1d] = KC_RCTRL,
164
165 [0x37] = KC_SYSREQ,
166
167 [0x52] = KC_INSERT,
168 [0x47] = KC_HOME,
169 [0x49] = KC_PAGE_UP,
170
171 [0x53] = KC_DELETE,
172 [0x4f] = KC_END,
173 [0x51] = KC_PAGE_DOWN,
174
175 [0x48] = KC_UP,
176 [0x4b] = KC_LEFT,
177 [0x50] = KC_DOWN,
178 [0x4d] = KC_RIGHT,
179
180 [0x35] = KC_NSLASH,
181 [0x1c] = KC_NENTER
182};
183
184#define KBD_CMD_SET_LEDS 0xed
185
186enum led_indicators {
187 LI_SCROLL = 0x01,
188 LI_NUM = 0x02,
189 LI_CAPS = 0x04
190};
191
192static void push_event(async_sess_t *sess, kbd_event_type_t type,
193 unsigned int key)
194{
195 async_exch_t *exch = async_exchange_begin(sess);
196 async_msg_4(exch, KBDEV_EVENT, type, key, 0, 0);
197 async_exchange_end(exch);
198}
199
200/** Get data and parse scancodes.
201 *
202 * @param arg Pointer to xt_kbd_t structure.
203 *
204 * @return EIO on error.
205 *
206 */
207static errno_t polling(void *arg)
208{
209 xt_kbd_t *kbd = arg;
210 size_t nread;
211 errno_t rc;
212
213 while (true) {
214 const unsigned int *map = scanmap_simple;
215 size_t map_size = sizeof(scanmap_simple) / sizeof(unsigned int);
216
217 uint8_t code = 0;
218 rc = chardev_read(kbd->chardev, &code, 1, &nread, chardev_f_none);
219 if (rc != EOK)
220 return EIO;
221
222 /* Ignore AT command reply */
223 if ((code == KBD_ACK) || (code == KBD_RESEND))
224 continue;
225
226 /* Extended set */
227 if (code == KBD_SCANCODE_SET_EXTENDED) {
228 map = scanmap_e0;
229 map_size = sizeof(scanmap_e0) / sizeof(unsigned int);
230
231 rc = chardev_read(kbd->chardev, &code, 1, &nread,
232 chardev_f_none);
233 if (rc != EOK)
234 return EIO;
235
236 /* Handle really special keys */
237
238 if (code == 0x2a) { /* Print Screen */
239 rc = chardev_read(kbd->chardev, &code, 1, &nread,
240 chardev_f_none);
241 if (rc != EOK)
242 return EIO;
243
244 if (code != 0xe0)
245 continue;
246
247 rc = chardev_read(kbd->chardev, &code, 1, &nread,
248 chardev_f_none);
249 if (rc != EOK)
250 return EIO;
251
252 if (code == 0x37)
253 push_event(kbd->client_sess, KEY_PRESS, KC_PRTSCR);
254
255 continue;
256 }
257
258 if (code == 0x46) { /* Break */
259 rc = chardev_read(kbd->chardev, &code, 1, &nread,
260 chardev_f_none);
261 if (rc != EOK)
262 return EIO;
263
264 if (code != 0xe0)
265 continue;
266
267 rc = chardev_read(kbd->chardev, &code, 1, &nread,
268 chardev_f_none);
269 if (rc != EOK)
270 return EIO;
271
272 if (code == 0xc6)
273 push_event(kbd->client_sess, KEY_PRESS, KC_BREAK);
274
275 continue;
276 }
277 }
278
279 /* Extended special set */
280 if (code == KBD_SCANCODE_SET_EXTENDED_SPECIAL) {
281 rc = chardev_read(kbd->chardev, &code, 1, &nread,
282 chardev_f_none);
283 if (rc != EOK)
284 return EIO;
285
286 if (code != 0x1d)
287 continue;
288
289 rc = chardev_read(kbd->chardev, &code, 1, &nread,
290 chardev_f_none);
291 if (rc != EOK)
292 return EIO;
293
294 if (code != 0x45)
295 continue;
296
297 rc = chardev_read(kbd->chardev, &code, 1, &nread,
298 chardev_f_none);
299 if (rc != EOK)
300 return EIO;
301
302 if (code != 0xe1)
303 continue;
304
305 rc = chardev_read(kbd->chardev, &code, 1, &nread,
306 chardev_f_none);
307 if (rc != EOK)
308 return EIO;
309
310 if (code != 0x9d)
311 continue;
312
313 rc = chardev_read(kbd->chardev, &code, 1, &nread,
314 chardev_f_none);
315 if (rc != EOK)
316 return EIO;
317
318 if (code == 0xc5)
319 push_event(kbd->client_sess, KEY_PRESS, KC_PAUSE);
320
321 continue;
322 }
323
324 /* Bit 7 indicates press/release */
325 const kbd_event_type_t type =
326 (code & 0x80) ? KEY_RELEASE : KEY_PRESS;
327 code &= ~0x80;
328
329 const unsigned int key = (code < map_size) ? map[code] : 0;
330
331 if (key != 0)
332 push_event(kbd->client_sess, type, key);
333 else
334 ddf_msg(LVL_WARN, "Unknown scancode: %hhx", code);
335 }
336}
337
338/** Default handler for IPC methods not handled by DDF.
339 *
340 * @param fun Device function handling the call.
341 * @param icall Call data.
342 *
343 */
344static void default_connection_handler(ddf_fun_t *fun, ipc_call_t *icall)
345{
346 const sysarg_t method = ipc_get_imethod(icall);
347 xt_kbd_t *kbd = ddf_dev_data_get(ddf_fun_get_dev(fun));
348 unsigned mods;
349 async_sess_t *sess;
350
351 switch (method) {
352 case KBDEV_SET_IND:
353 /*
354 * XT keyboards do not support setting mods,
355 * assume AT keyboard with Scan Code Set 1.
356 */
357 mods = ipc_get_arg1(icall);
358 const uint8_t status = 0 |
359 ((mods & KM_CAPS_LOCK) ? LI_CAPS : 0) |
360 ((mods & KM_NUM_LOCK) ? LI_NUM : 0) |
361 ((mods & KM_SCROLL_LOCK) ? LI_SCROLL : 0);
362 uint8_t cmds[] = { KBD_CMD_SET_LEDS, status };
363
364 size_t nwr;
365 errno_t rc = chardev_write(kbd->chardev, &cmds[0], 1, &nwr);
366 if (rc != EOK) {
367 async_answer_0(icall, rc);
368 break;
369 }
370
371 rc = chardev_write(kbd->chardev, &cmds[1], 1, &nwr);
372 async_answer_0(icall, rc);
373 break;
374 case IPC_M_CONNECT_TO_ME:
375 /*
376 * This might be ugly but async_callback_receive_start makes no
377 * difference for incorrect call and malloc failure.
378 */
379 sess = async_callback_receive_start(EXCHANGE_SERIALIZE, icall);
380
381 /* Probably ENOMEM error, try again. */
382 if (sess == NULL) {
383 ddf_msg(LVL_WARN,
384 "Failed creating callback session");
385 async_answer_0(icall, EAGAIN);
386 break;
387 }
388
389 if (kbd->client_sess == NULL) {
390 kbd->client_sess = sess;
391 ddf_msg(LVL_DEBUG, "Set client session");
392 async_answer_0(icall, EOK);
393 } else {
394 ddf_msg(LVL_ERROR, "Client session already set");
395 async_answer_0(icall, ELIMIT);
396 }
397
398 break;
399 default:
400 ddf_msg(LVL_ERROR, "Unknown method: %d.", (int)method);
401 async_answer_0(icall, EINVAL);
402 break;
403 }
404}
405
406/** Keyboard function ops. */
407static ddf_dev_ops_t kbd_ops = {
408 .default_handler = default_connection_handler
409};
410
411/** Initialize keyboard driver structure.
412 *
413 * @param kbd Keyboard driver structure to initialize.
414 * @param dev DDF device structure.
415 *
416 * Connects to parent, creates keyboard function, starts polling fibril.
417 *
418 */
419errno_t xt_kbd_init(xt_kbd_t *kbd, ddf_dev_t *dev)
420{
421 async_sess_t *parent_sess;
422 bool bound = false;
423 errno_t rc;
424
425 kbd->client_sess = NULL;
426
427 parent_sess = ddf_dev_parent_sess_get(dev);
428 if (parent_sess == NULL) {
429 ddf_msg(LVL_ERROR, "Failed creating parent session.");
430 rc = EIO;
431 goto error;
432 }
433
434 rc = chardev_open(parent_sess, &kbd->chardev);
435 if (rc != EOK) {
436 ddf_msg(LVL_ERROR, "Failed opening character device.");
437 goto error;
438 }
439
440 kbd->kbd_fun = ddf_fun_create(dev, fun_exposed, "kbd");
441 if (kbd->kbd_fun == NULL) {
442 ddf_msg(LVL_ERROR, "Failed creating function 'kbd'.");
443 rc = ENOMEM;
444 goto error;
445 }
446
447 ddf_fun_set_ops(kbd->kbd_fun, &kbd_ops);
448
449 rc = ddf_fun_bind(kbd->kbd_fun);
450 if (rc != EOK) {
451 ddf_msg(LVL_ERROR, "Failed binding function 'kbd'.");
452 goto error;
453 }
454 bound = true;
455
456 rc = ddf_fun_add_to_category(kbd->kbd_fun, "keyboard");
457 if (rc != EOK) {
458 ddf_msg(LVL_ERROR, "Failed adding function 'kbd' to category "
459 "'keyboard'.");
460 goto error;
461 }
462
463 kbd->polling_fibril = fibril_create(polling, kbd);
464 if (kbd->polling_fibril == 0) {
465 ddf_msg(LVL_ERROR, "Failed creating polling fibril.");
466 rc = ENOMEM;
467 goto error;
468 }
469
470 fibril_add_ready(kbd->polling_fibril);
471 return EOK;
472error:
473 if (bound)
474 ddf_fun_unbind(kbd->kbd_fun);
475 if (kbd->kbd_fun != NULL)
476 ddf_fun_destroy(kbd->kbd_fun);
477 chardev_close(kbd->chardev);
478 return rc;
479}
Note: See TracBrowser for help on using the repository browser.