source: mainline/uspace/drv/hid/atkbd/atkbd.c@ 15c5418

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 15c5418 was 15c5418, checked in by Jiri Svoboda <jiri@…>, 8 years ago

chardev_open, chardev_close.

  • Property mode set to 100644
File size: 9.0 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 drvkbd
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/* Set 2 scan codes (AT keyboard) */
52static const unsigned int scanmap_simple[] = {
53 [0x0e] = KC_BACKTICK,
54
55 [0x16] = KC_1,
56 [0x1e] = KC_2,
57 [0x26] = KC_3,
58 [0x25] = KC_4,
59 [0x2e] = KC_5,
60 [0x36] = KC_6,
61 [0x3d] = KC_7,
62 [0x3e] = KC_8,
63 [0x46] = KC_9,
64 [0x45] = KC_0,
65
66 [0x4e] = KC_MINUS,
67 [0x55] = KC_EQUALS,
68 [0x66] = KC_BACKSPACE,
69
70 [0x0d] = KC_TAB,
71
72 [0x15] = KC_Q,
73 [0x1d] = KC_W,
74 [0x24] = KC_E,
75 [0x2d] = KC_R,
76 [0x2c] = KC_T,
77 [0x35] = KC_Y,
78 [0x3c] = KC_U,
79 [0x43] = KC_I,
80 [0x44] = KC_O,
81 [0x4d] = KC_P,
82
83 [0x54] = KC_LBRACKET,
84 [0x5b] = KC_RBRACKET,
85
86 [0x58] = KC_CAPS_LOCK,
87
88 [0x1c] = KC_A,
89 [0x1b] = KC_S,
90 [0x23] = KC_D,
91 [0x2b] = KC_F,
92 [0x34] = KC_G,
93 [0x33] = KC_H,
94 [0x3b] = KC_J,
95 [0x42] = KC_K,
96 [0x4b] = KC_L,
97
98 [0x4c] = KC_SEMICOLON,
99 [0x52] = KC_QUOTE,
100 [0x5d] = KC_BACKSLASH,
101
102 [0x12] = KC_LSHIFT,
103
104 [0x1a] = KC_Z,
105 [0x22] = KC_X,
106 [0x21] = KC_C,
107 [0x2a] = KC_V,
108 [0x32] = KC_B,
109 [0x31] = KC_N,
110 [0x3a] = KC_M,
111
112 [0x41] = KC_COMMA,
113 [0x49] = KC_PERIOD,
114 [0x4a] = KC_SLASH,
115
116 [0x59] = KC_RSHIFT,
117
118 [0x14] = KC_LCTRL,
119 [0x11] = KC_LALT,
120 [0x29] = KC_SPACE,
121
122 [0x76] = KC_ESCAPE,
123
124 [0x05] = KC_F1,
125 [0x06] = KC_F2,
126 [0x04] = KC_F3,
127 [0x0c] = KC_F4,
128 [0x03] = KC_F5,
129 [0x0b] = KC_F6,
130 [0x02] = KC_F7,
131
132 [0x0a] = KC_F8,
133 [0x01] = KC_F9,
134 [0x09] = KC_F10,
135
136 [0x78] = KC_F11,
137 [0x07] = KC_F12,
138
139 [0x7e] = KC_SCROLL_LOCK,
140
141 [0x5a] = KC_ENTER,
142
143 [0x77] = KC_NUM_LOCK,
144 [0x7c] = KC_NTIMES,
145 [0x7b] = KC_NMINUS,
146 [0x79] = KC_NPLUS,
147 [0x6c] = KC_N7,
148 [0x75] = KC_N8,
149 [0x7d] = KC_N9,
150 [0x6b] = KC_N4,
151 [0x73] = KC_N5,
152 [0x74] = KC_N6,
153 [0x69] = KC_N1,
154 [0x72] = KC_N2,
155 [0x7a] = KC_N3,
156 [0x70] = KC_N0,
157 [0x71] = KC_NPERIOD,
158};
159
160#define KBD_SCANCODE_SET_EXTENDED 0xe0
161#define KBD_SCANCODE_SET_EXTENDED_SPECIAL 0xe1
162#define KBD_SCANCODE_KEY_RELEASE 0xf0
163
164static const unsigned int scanmap_e0[] = {
165 [0x65] = KC_RALT,
166 [0x59] = KC_RSHIFT,
167
168 [0x64] = KC_PRTSCR,
169
170 [0x70] = KC_INSERT,
171 [0x6c] = KC_HOME,
172 [0x7d] = KC_PAGE_UP,
173
174 [0x71] = KC_DELETE,
175 [0x69] = KC_END,
176 [0x7a] = KC_PAGE_DOWN,
177
178 [0x75] = KC_UP,
179 [0x6b] = KC_LEFT,
180 [0x72] = KC_DOWN,
181 [0x74] = KC_RIGHT,
182
183 [0x4a] = KC_NSLASH,
184 [0x5a] = KC_NENTER
185};
186
187static void push_event(async_sess_t *sess, kbd_event_type_t type,
188 unsigned int key)
189{
190 async_exch_t *exch = async_exchange_begin(sess);
191 async_msg_4(exch, KBDEV_EVENT, type, key, 0, 0);
192 async_exchange_end(exch);
193}
194
195/** Get data and parse scancodes.
196 *
197 * @param arg Pointer to at_kbd_t structure.
198 *
199 * @return EIO on error.
200 *
201 */
202static int polling(void *arg)
203{
204 at_kbd_t *kbd = arg;
205
206 while (true) {
207 uint8_t code = 0;
208 ssize_t size = chardev_read(kbd->chardev, &code, 1);
209 if (size != 1)
210 return EIO;
211
212 const unsigned int *map;
213 size_t map_size;
214
215 if (code == KBD_SCANCODE_SET_EXTENDED) {
216 map = scanmap_e0;
217 map_size = sizeof(scanmap_e0) / sizeof(unsigned int);
218
219 size = chardev_read(kbd->chardev, &code, 1);
220 if (size != 1)
221 return EIO;
222 } else if (code == KBD_SCANCODE_SET_EXTENDED_SPECIAL) {
223 size = chardev_read(kbd->chardev, &code, 1);
224 if (size != 1)
225 return EIO;
226 if (code != 0x14)
227 continue;
228
229 size = chardev_read(kbd->chardev, &code, 1);
230 if (size != 1)
231 return EIO;
232 if (code != 0x77)
233 continue;
234
235 size = chardev_read(kbd->chardev, &code, 1);
236 if (size != 1)
237 return EIO;
238 if (code != 0xe1)
239 continue;
240
241 size = chardev_read(kbd->chardev, &code, 1);
242 if (size != 1)
243 return EIO;
244 if (code != 0xf0)
245 continue;
246
247 size = chardev_read(kbd->chardev, &code, 1);
248 if (size != 1)
249 return EIO;
250 if (code != 0x14)
251 continue;
252
253 size = chardev_read(kbd->chardev, &code, 1);
254 if (size != 1)
255 return EIO;
256 if (code != 0xf0)
257 continue;
258
259 size = chardev_read(kbd->chardev, &code, 1);
260 if (size != 1)
261 return EIO;
262 if (code == 0x77)
263 push_event(kbd->client_sess, KEY_PRESS, KC_BREAK);
264
265 continue;
266 } else {
267 map = scanmap_simple;
268 map_size = sizeof(scanmap_simple) / sizeof(unsigned int);
269 }
270
271 kbd_event_type_t type;
272 if (code == KBD_SCANCODE_KEY_RELEASE) {
273 type = KEY_RELEASE;
274 size = chardev_read(kbd->chardev, &code, 1);
275 if (size != 1)
276 return EIO;
277 } else {
278 type = KEY_PRESS;
279 }
280
281 const unsigned int key = (code < map_size) ? map[code] : 0;
282
283 if (key != 0)
284 push_event(kbd->client_sess, type, key);
285 else
286 ddf_msg(LVL_WARN, "Unknown scancode: %hhx", code);
287 }
288}
289
290/** Default handler for IPC methods not handled by DDF.
291 *
292 * @param fun Device function handling the call.
293 * @param icallid Call id.
294 * @param icall Call data.
295 *
296 */
297static void default_connection_handler(ddf_fun_t *fun,
298 ipc_callid_t icallid, ipc_call_t *icall)
299{
300 const sysarg_t method = IPC_GET_IMETHOD(*icall);
301 at_kbd_t *kbd = ddf_dev_data_get(ddf_fun_get_dev(fun));
302
303 switch (method) {
304 case KBDEV_SET_IND: {
305 async_answer_0(icallid, ENOTSUP);
306 break;
307 }
308 /*
309 * This might be ugly but async_callback_receive_start makes no
310 * difference for incorrect call and malloc failure.
311 */
312 case IPC_M_CONNECT_TO_ME: {
313 async_sess_t *sess =
314 async_callback_receive_start(EXCHANGE_SERIALIZE, icall);
315
316 /* Probably ENOMEM error, try again. */
317 if (sess == NULL) {
318 ddf_msg(LVL_WARN,
319 "Failed creating callback session");
320 async_answer_0(icallid, EAGAIN);
321 break;
322 }
323
324 if (kbd->client_sess == NULL) {
325 kbd->client_sess = sess;
326 ddf_msg(LVL_DEBUG, "Set client session");
327 async_answer_0(icallid, EOK);
328 } else {
329 ddf_msg(LVL_ERROR, "Client session already set");
330 async_answer_0(icallid, ELIMIT);
331 }
332
333 break;
334 }
335 default:
336 ddf_msg(LVL_ERROR, "Unknown method: %d.", (int)method);
337 async_answer_0(icallid, EINVAL);
338 break;
339 }
340}
341
342/** Keyboard function ops. */
343static ddf_dev_ops_t kbd_ops = {
344 .default_handler = default_connection_handler
345};
346
347/** Initialize keyboard driver structure.
348 *
349 * @param kbd Keyboard driver structure to initialize.
350 * @param dev DDF device structure.
351 *
352 * Connects to parent, creates keyboard function, starts polling fibril.
353 *
354 */
355int at_kbd_init(at_kbd_t *kbd, ddf_dev_t *dev)
356{
357 async_sess_t *parent_sess;
358 int rc;
359
360 assert(kbd);
361 assert(dev);
362
363 kbd->client_sess = NULL;
364 parent_sess = ddf_dev_parent_sess_get(dev);
365 if (parent_sess == NULL) {
366 ddf_msg(LVL_ERROR, "Failed creating parent session.");
367 rc = EIO;
368 goto error;
369 }
370
371 rc = chardev_open(parent_sess, &kbd->chardev);
372 if (rc != EOK) {
373 ddf_msg(LVL_ERROR, "Failed opening character device.");
374 return EIO;
375 }
376
377 kbd->kbd_fun = ddf_fun_create(dev, fun_exposed, "kbd");
378 if (!kbd->kbd_fun) {
379 ddf_msg(LVL_ERROR, "Failed creating function 'kbd'.");
380 return ENOMEM;
381 }
382
383 ddf_fun_set_ops(kbd->kbd_fun, &kbd_ops);
384
385 int ret = ddf_fun_bind(kbd->kbd_fun);
386 if (ret != EOK) {
387 ddf_msg(LVL_ERROR, "Failed binding function 'kbd'.");
388 ddf_fun_destroy(kbd->kbd_fun);
389 return EEXIST;
390 }
391
392 ret = ddf_fun_add_to_category(kbd->kbd_fun, "keyboard");
393 if (ret != EOK) {
394 ddf_msg(LVL_ERROR, "Failed adding function 'kbd' to category "
395 "'keyboard'.");
396 ddf_fun_unbind(kbd->kbd_fun);
397 ddf_fun_destroy(kbd->kbd_fun);
398 return ENOMEM;
399 }
400
401 kbd->polling_fibril = fibril_create(polling, kbd);
402 if (!kbd->polling_fibril) {
403 ddf_msg(LVL_ERROR, "Failed creating polling fibril.");
404 ddf_fun_unbind(kbd->kbd_fun);
405 ddf_fun_destroy(kbd->kbd_fun);
406 return ENOMEM;
407 }
408
409 fibril_add_ready(kbd->polling_fibril);
410 return EOK;
411error:
412 chardev_close(kbd->chardev);
413 kbd->chardev = NULL;
414 return rc;
415}
Note: See TracBrowser for help on using the repository browser.