source: mainline/uspace/drv/hid/atkbd/atkbd.c@ 09ab0a9a

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 09ab0a9a was c8ea6eca, checked in by Jakub Jermar <jakub@…>, 7 years ago

Improve doxygen documentation

  • Property mode set to 100644
File size: 9.6 KB
Line 
1/*
2 * Copyright (c) 2017 Jiri Svoboda
3 * Copyright (c) 2011 Jan Vesely
4 * Copyright (c) 2009 Vineeth Pillai
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * - Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * - Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * - The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31/** @addtogroup atkbd
32 * @{
33 */
34/** @file
35 * @brief AT keyboard driver
36 */
37
38#include <errno.h>
39#include <ddf/log.h>
40#include <io/keycode.h>
41#include <io/chardev.h>
42#include <io/console.h>
43#include <ipc/kbdev.h>
44#include <abi/ipc/methods.h>
45#include "atkbd.h"
46
47#define AT_CAPS_SCAN_CODE 0x58
48#define AT_NUM_SCAN_CODE 0x77
49#define AT_SCROLL_SCAN_CODE 0x7E
50
51#define AT_KBD_ENABLE 0xF4
52#define AT_KBD_ACK 0xFA
53
54/* Set 2 scan codes (AT keyboard) */
55static const unsigned int scanmap_simple[] = {
56 [0x0e] = KC_BACKTICK,
57
58 [0x16] = KC_1,
59 [0x1e] = KC_2,
60 [0x26] = KC_3,
61 [0x25] = KC_4,
62 [0x2e] = KC_5,
63 [0x36] = KC_6,
64 [0x3d] = KC_7,
65 [0x3e] = KC_8,
66 [0x46] = KC_9,
67 [0x45] = KC_0,
68
69 [0x4e] = KC_MINUS,
70 [0x55] = KC_EQUALS,
71 [0x66] = KC_BACKSPACE,
72
73 [0x0d] = KC_TAB,
74
75 [0x15] = KC_Q,
76 [0x1d] = KC_W,
77 [0x24] = KC_E,
78 [0x2d] = KC_R,
79 [0x2c] = KC_T,
80 [0x35] = KC_Y,
81 [0x3c] = KC_U,
82 [0x43] = KC_I,
83 [0x44] = KC_O,
84 [0x4d] = KC_P,
85
86 [0x54] = KC_LBRACKET,
87 [0x5b] = KC_RBRACKET,
88
89 [0x58] = KC_CAPS_LOCK,
90
91 [0x1c] = KC_A,
92 [0x1b] = KC_S,
93 [0x23] = KC_D,
94 [0x2b] = KC_F,
95 [0x34] = KC_G,
96 [0x33] = KC_H,
97 [0x3b] = KC_J,
98 [0x42] = KC_K,
99 [0x4b] = KC_L,
100
101 [0x4c] = KC_SEMICOLON,
102 [0x52] = KC_QUOTE,
103 [0x5d] = KC_BACKSLASH,
104
105 [0x12] = KC_LSHIFT,
106
107 [0x1a] = KC_Z,
108 [0x22] = KC_X,
109 [0x21] = KC_C,
110 [0x2a] = KC_V,
111 [0x32] = KC_B,
112 [0x31] = KC_N,
113 [0x3a] = KC_M,
114
115 [0x41] = KC_COMMA,
116 [0x49] = KC_PERIOD,
117 [0x4a] = KC_SLASH,
118
119 [0x59] = KC_RSHIFT,
120
121 [0x14] = KC_LCTRL,
122 [0x11] = KC_LALT,
123 [0x29] = KC_SPACE,
124
125 [0x76] = KC_ESCAPE,
126
127 [0x05] = KC_F1,
128 [0x06] = KC_F2,
129 [0x04] = KC_F3,
130 [0x0c] = KC_F4,
131 [0x03] = KC_F5,
132 [0x0b] = KC_F6,
133 [0x02] = KC_F7,
134
135 [0x0a] = KC_F8,
136 [0x01] = KC_F9,
137 [0x09] = KC_F10,
138
139 [0x78] = KC_F11,
140 [0x07] = KC_F12,
141
142 [0x7e] = KC_SCROLL_LOCK,
143
144 [0x5a] = KC_ENTER,
145
146 [0x77] = KC_NUM_LOCK,
147 [0x7c] = KC_NTIMES,
148 [0x7b] = KC_NMINUS,
149 [0x79] = KC_NPLUS,
150 [0x6c] = KC_N7,
151 [0x75] = KC_N8,
152 [0x7d] = KC_N9,
153 [0x6b] = KC_N4,
154 [0x73] = KC_N5,
155 [0x74] = KC_N6,
156 [0x69] = KC_N1,
157 [0x72] = KC_N2,
158 [0x7a] = KC_N3,
159 [0x70] = KC_N0,
160 [0x71] = KC_NPERIOD,
161};
162
163#define KBD_SCANCODE_SET_EXTENDED 0xe0
164#define KBD_SCANCODE_SET_EXTENDED_SPECIAL 0xe1
165#define KBD_SCANCODE_KEY_RELEASE 0xf0
166
167static const unsigned int scanmap_e0[] = {
168 [0x65] = KC_RALT,
169 [0x59] = KC_RSHIFT,
170
171 [0x64] = KC_PRTSCR,
172
173 [0x70] = KC_INSERT,
174 [0x6c] = KC_HOME,
175 [0x7d] = KC_PAGE_UP,
176
177 [0x71] = KC_DELETE,
178 [0x69] = KC_END,
179 [0x7a] = KC_PAGE_DOWN,
180
181 [0x75] = KC_UP,
182 [0x6b] = KC_LEFT,
183 [0x72] = KC_DOWN,
184 [0x74] = KC_RIGHT,
185
186 [0x4a] = KC_NSLASH,
187 [0x5a] = KC_NENTER
188};
189
190static void push_event(async_sess_t *sess, kbd_event_type_t type,
191 unsigned int key)
192{
193 async_exch_t *exch = async_exchange_begin(sess);
194 async_msg_4(exch, KBDEV_EVENT, type, key, 0, 0);
195 async_exchange_end(exch);
196}
197
198/** Get data and parse scancodes.
199 *
200 * @param arg Pointer to at_kbd_t structure.
201 *
202 * @return EIO on error.
203 *
204 */
205static errno_t polling(void *arg)
206{
207 at_kbd_t *kbd = arg;
208 size_t nwr;
209 errno_t rc;
210
211 while (true) {
212 uint8_t code = 0;
213 rc = chardev_read(kbd->chardev, &code, 1, &nwr);
214 if (rc != EOK)
215 return EIO;
216
217 const unsigned int *map;
218 size_t map_size;
219
220 if (code == KBD_SCANCODE_SET_EXTENDED) {
221 map = scanmap_e0;
222 map_size = sizeof(scanmap_e0) / sizeof(unsigned int);
223
224 rc = chardev_read(kbd->chardev, &code, 1, &nwr);
225 if (rc != EOK)
226 return EIO;
227 } else if (code == KBD_SCANCODE_SET_EXTENDED_SPECIAL) {
228 rc = chardev_read(kbd->chardev, &code, 1, &nwr);
229 if (rc != EOK)
230 return EIO;
231 if (code != 0x14)
232 continue;
233
234 rc = chardev_read(kbd->chardev, &code, 1, &nwr);
235 if (rc != EOK)
236 return EIO;
237 if (code != 0x77)
238 continue;
239
240 rc = chardev_read(kbd->chardev, &code, 1, &nwr);
241 if (rc != EOK)
242 return EIO;
243 if (code != 0xe1)
244 continue;
245
246 rc = chardev_read(kbd->chardev, &code, 1, &nwr);
247 if (rc != EOK)
248 return EIO;
249 if (code != 0xf0)
250 continue;
251
252 rc = chardev_read(kbd->chardev, &code, 1, &nwr);
253 if (rc != EOK)
254 return EIO;
255 if (code != 0x14)
256 continue;
257
258 rc = chardev_read(kbd->chardev, &code, 1, &nwr);
259 if (rc != EOK)
260 return EIO;
261 if (code != 0xf0)
262 continue;
263
264 rc = chardev_read(kbd->chardev, &code, 1, &nwr);
265 if (rc != EOK)
266 return EIO;
267 if (code == 0x77)
268 push_event(kbd->client_sess, KEY_PRESS, KC_BREAK);
269
270 continue;
271 } else {
272 map = scanmap_simple;
273 map_size = sizeof(scanmap_simple) / sizeof(unsigned int);
274 }
275
276 kbd_event_type_t type;
277 if (code == KBD_SCANCODE_KEY_RELEASE) {
278 type = KEY_RELEASE;
279 rc = chardev_read(kbd->chardev, &code, 1, &nwr);
280 if (rc != EOK)
281 return EIO;
282 } else {
283 type = KEY_PRESS;
284 }
285
286 const unsigned int key = (code < map_size) ? map[code] : 0;
287
288 if (key != 0)
289 push_event(kbd->client_sess, type, key);
290 else
291 ddf_msg(LVL_WARN, "Unknown scancode: %hhx", code);
292 }
293}
294
295/** Default handler for IPC methods not handled by DDF.
296 *
297 * @param fun Device function handling the call.
298 * @param icall Call data.
299 *
300 */
301static void default_connection_handler(ddf_fun_t *fun, ipc_call_t *icall)
302{
303 const sysarg_t method = IPC_GET_IMETHOD(*icall);
304 at_kbd_t *kbd = ddf_dev_data_get(ddf_fun_get_dev(fun));
305 async_sess_t *sess;
306
307 switch (method) {
308 case KBDEV_SET_IND:
309 async_answer_0(icall, ENOTSUP);
310 break;
311 case IPC_M_CONNECT_TO_ME:
312 /*
313 * This might be ugly but async_callback_receive_start makes no
314 * difference for incorrect call and malloc failure.
315 */
316 sess = async_callback_receive_start(EXCHANGE_SERIALIZE, icall);
317
318 /* Probably ENOMEM error, try again. */
319 if (sess == NULL) {
320 ddf_msg(LVL_WARN,
321 "Failed creating callback session");
322 async_answer_0(icall, EAGAIN);
323 break;
324 }
325
326 if (kbd->client_sess == NULL) {
327 kbd->client_sess = sess;
328 ddf_msg(LVL_DEBUG, "Set client session");
329 async_answer_0(icall, EOK);
330 } else {
331 ddf_msg(LVL_ERROR, "Client session already set");
332 async_answer_0(icall, ELIMIT);
333 }
334
335 break;
336 default:
337 ddf_msg(LVL_ERROR, "Unknown method: %d.", (int)method);
338 async_answer_0(icall, EINVAL);
339 break;
340 }
341}
342
343/** Keyboard function ops. */
344static ddf_dev_ops_t kbd_ops = {
345 .default_handler = default_connection_handler
346};
347
348/** Initialize keyboard driver structure.
349 *
350 * @param kbd Keyboard driver structure to initialize.
351 * @param dev DDF device structure.
352 *
353 * Connects to parent, creates keyboard function, starts polling fibril.
354 *
355 */
356errno_t at_kbd_init(at_kbd_t *kbd, ddf_dev_t *dev)
357{
358 async_sess_t *parent_sess;
359 errno_t rc;
360
361 assert(kbd);
362 assert(dev);
363
364 kbd->client_sess = NULL;
365 parent_sess = ddf_dev_parent_sess_get(dev);
366 if (parent_sess == NULL) {
367 ddf_msg(LVL_ERROR, "Failed creating parent session.");
368 rc = EIO;
369 goto error;
370 }
371
372 rc = chardev_open(parent_sess, &kbd->chardev);
373 if (rc != EOK) {
374 ddf_msg(LVL_ERROR, "Failed opening character device.");
375 return EIO;
376 }
377
378 kbd->kbd_fun = ddf_fun_create(dev, fun_exposed, "kbd");
379 if (!kbd->kbd_fun) {
380 ddf_msg(LVL_ERROR, "Failed creating function 'kbd'.");
381 return ENOMEM;
382 }
383
384 ddf_fun_set_ops(kbd->kbd_fun, &kbd_ops);
385
386 errno_t ret = ddf_fun_bind(kbd->kbd_fun);
387 if (ret != EOK) {
388 ddf_msg(LVL_ERROR, "Failed binding function 'kbd'.");
389 ddf_fun_destroy(kbd->kbd_fun);
390 return EEXIST;
391 }
392
393 ret = ddf_fun_add_to_category(kbd->kbd_fun, "keyboard");
394 if (ret != EOK) {
395 ddf_msg(LVL_ERROR, "Failed adding function 'kbd' to category "
396 "'keyboard'.");
397 ddf_fun_unbind(kbd->kbd_fun);
398 ddf_fun_destroy(kbd->kbd_fun);
399 return ENOMEM;
400 }
401
402 kbd->polling_fibril = fibril_create(polling, kbd);
403 if (!kbd->polling_fibril) {
404 ddf_msg(LVL_ERROR, "Failed creating polling fibril.");
405 ddf_fun_unbind(kbd->kbd_fun);
406 ddf_fun_destroy(kbd->kbd_fun);
407 return ENOMEM;
408 }
409
410 uint8_t code = AT_KBD_ENABLE;
411 size_t bytes;
412 rc = chardev_write(kbd->chardev, &code, 1, &bytes);
413 if (rc != EOK) {
414 ddf_msg(LVL_ERROR, "Failed to enable keyboard.");
415 fibril_destroy(kbd->polling_fibril);
416 ddf_fun_unbind(kbd->kbd_fun);
417 ddf_fun_destroy(kbd->kbd_fun);
418 return EIO;
419 }
420 rc = chardev_read(kbd->chardev, &code, 1, &bytes);
421 if (rc != EOK || code != AT_KBD_ACK) {
422 ddf_msg(LVL_ERROR, "Failed to confirm keyboard enable: %hhx.",
423 code);
424 fibril_destroy(kbd->polling_fibril);
425 ddf_fun_unbind(kbd->kbd_fun);
426 ddf_fun_destroy(kbd->kbd_fun);
427 return EIO;
428 }
429
430 fibril_add_ready(kbd->polling_fibril);
431 return EOK;
432error:
433 chardev_close(kbd->chardev);
434 kbd->chardev = NULL;
435 return rc;
436}
Note: See TracBrowser for help on using the repository browser.