source: mainline/uspace/drv/char/xtkbd/xtkbd.c@ 19e00de4

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 19e00de4 was ba551ba7, checked in by Jan Vesely <jano.vesely@…>, 14 years ago

xtkbd: Implement scancode parsing.

  • Property mode set to 100644
File size: 7.6 KB
Line 
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 <devman.h>
37#include <device/char_dev.h>
38#include <ddf/log.h>
39#include <io/keycode.h>
40#include <io/console.h>
41#include <ipc/kbdev.h>
42#include <abi/ipc/methods.h>
43
44#include "xtkbd.h"
45static const int scanmap_simple[] = {
46
47 [0x29] = KC_BACKTICK,
48
49 [0x02] = KC_1,
50 [0x03] = KC_2,
51 [0x04] = KC_3,
52 [0x05] = KC_4,
53 [0x06] = KC_5,
54 [0x07] = KC_6,
55 [0x08] = KC_7,
56 [0x09] = KC_8,
57 [0x0a] = KC_9,
58 [0x0b] = KC_0,
59
60 [0x0c] = KC_MINUS,
61 [0x0d] = KC_EQUALS,
62 [0x0e] = KC_BACKSPACE,
63
64 [0x0f] = KC_TAB,
65
66 [0x10] = KC_Q,
67 [0x11] = KC_W,
68 [0x12] = KC_E,
69 [0x13] = KC_R,
70 [0x14] = KC_T,
71 [0x15] = KC_Y,
72 [0x16] = KC_U,
73 [0x17] = KC_I,
74 [0x18] = KC_O,
75 [0x19] = KC_P,
76
77 [0x1a] = KC_LBRACKET,
78 [0x1b] = KC_RBRACKET,
79
80 [0x3a] = KC_CAPS_LOCK,
81
82 [0x1e] = KC_A,
83 [0x1f] = KC_S,
84 [0x20] = KC_D,
85 [0x21] = KC_F,
86 [0x22] = KC_G,
87 [0x23] = KC_H,
88 [0x24] = KC_J,
89 [0x25] = KC_K,
90 [0x26] = KC_L,
91
92 [0x27] = KC_SEMICOLON,
93 [0x28] = KC_QUOTE,
94 [0x2b] = KC_BACKSLASH,
95
96 [0x2a] = KC_LSHIFT,
97
98 [0x2c] = KC_Z,
99 [0x2d] = KC_X,
100 [0x2e] = KC_C,
101 [0x2f] = KC_V,
102 [0x30] = KC_B,
103 [0x31] = KC_N,
104 [0x32] = KC_M,
105
106 [0x33] = KC_COMMA,
107 [0x34] = KC_PERIOD,
108 [0x35] = KC_SLASH,
109
110 [0x36] = KC_RSHIFT,
111
112 [0x1d] = KC_LCTRL,
113 [0x38] = KC_LALT,
114 [0x39] = KC_SPACE,
115
116 [0x01] = KC_ESCAPE,
117
118 [0x3b] = KC_F1,
119 [0x3c] = KC_F2,
120 [0x3d] = KC_F3,
121 [0x3e] = KC_F4,
122 [0x3f] = KC_F5,
123 [0x40] = KC_F6,
124 [0x41] = KC_F7,
125
126 [0x42] = KC_F8,
127 [0x43] = KC_F9,
128 [0x44] = KC_F10,
129
130 [0x57] = KC_F11,
131 [0x58] = KC_F12,
132
133 [0x46] = KC_SCROLL_LOCK,
134
135 [0x1c] = KC_ENTER,
136
137 [0x45] = KC_NUM_LOCK,
138 [0x37] = KC_NTIMES,
139 [0x4a] = KC_NMINUS,
140 [0x4e] = KC_NPLUS,
141 [0x47] = KC_N7,
142 [0x48] = KC_N8,
143 [0x49] = KC_N9,
144 [0x4b] = KC_N4,
145 [0x4c] = KC_N5,
146 [0x4d] = KC_N6,
147 [0x4f] = KC_N1,
148 [0x50] = KC_N2,
149 [0x51] = KC_N3,
150 [0x52] = KC_N0,
151 [0x53] = KC_NPERIOD
152};
153/*----------------------------------------------------------------------------*/
154static const int scanmap_e0[] = {
155 [0x38] = KC_RALT,
156 [0x1d] = KC_RSHIFT,
157
158 [0x37] = KC_PRTSCR,
159
160 [0x52] = KC_INSERT,
161 [0x47] = KC_HOME,
162 [0x49] = KC_PAGE_UP,
163
164 [0x53] = KC_DELETE,
165 [0x4f] = KC_END,
166 [0x51] = KC_PAGE_DOWN,
167
168 [0x48] = KC_UP,
169 [0x4b] = KC_LEFT,
170 [0x50] = KC_DOWN,
171 [0x4d] = KC_RIGHT,
172
173 [0x35] = KC_NSLASH,
174 [0x1c] = KC_NENTER
175};
176/*----------------------------------------------------------------------------*/
177static int polling(void *);
178static void default_connection_handler(ddf_fun_t *fun,
179 ipc_callid_t icallid, ipc_call_t *icall);
180/*----------------------------------------------------------------------------*/
181static ddf_dev_ops_t kbd_ops = {
182 .default_handler = default_connection_handler
183};
184/*----------------------------------------------------------------------------*/
185int xt_kbd_init(xt_kbd_t *kbd, ddf_dev_t *dev)
186{
187 assert(kbd);
188 assert(dev);
189 kbd->input_sess = NULL;
190 kbd->parent_sess = devman_parent_device_connect(EXCHANGE_SERIALIZE,
191 dev->handle, IPC_FLAG_BLOCKING);
192 if (!kbd->parent_sess)
193 return ENOMEM;
194
195 kbd->kbd_fun = ddf_fun_create(dev, fun_exposed, "kbd");
196 if (!kbd->kbd_fun) {
197 async_hangup(kbd->parent_sess);
198 return ENOMEM;
199 }
200 kbd->kbd_fun->ops = &kbd_ops;
201 kbd->kbd_fun->driver_data = kbd;
202
203 int ret = ddf_fun_bind(kbd->kbd_fun);
204 if (ret != EOK) {
205 async_hangup(kbd->parent_sess);
206 kbd->kbd_fun->driver_data = NULL;
207 ddf_fun_destroy(kbd->kbd_fun);
208 return ENOMEM;
209 }
210
211 ret = ddf_fun_add_to_category(kbd->kbd_fun, "keyboard");
212 if (ret != EOK) {
213 async_hangup(kbd->parent_sess);
214 ddf_fun_unbind(kbd->kbd_fun);
215 kbd->kbd_fun->driver_data = NULL;
216 ddf_fun_destroy(kbd->kbd_fun);
217 return ENOMEM;
218 }
219
220 kbd->polling_fibril = fibril_create(polling, kbd);
221 if (!kbd->polling_fibril) {
222 async_hangup(kbd->parent_sess);
223 ddf_fun_unbind(kbd->kbd_fun);
224 kbd->kbd_fun->driver_data = NULL;
225 ddf_fun_destroy(kbd->kbd_fun);
226 return ENOMEM;
227 }
228 fibril_add_ready(kbd->polling_fibril);
229 return EOK;
230}
231/*----------------------------------------------------------------------------*/
232int polling(void *arg)
233{
234 assert(arg);
235 xt_kbd_t *kbd = arg;
236
237 assert(kbd->parent_sess);
238 while (1) {
239 const int *map = scanmap_simple;
240 size_t map_size = sizeof(scanmap_simple) / sizeof(int);
241
242 uint8_t code = 0;
243 ssize_t size = char_dev_read(kbd->parent_sess, &code, 1);
244
245 if (code == 0xe0) {
246 map = scanmap_e0;
247 map_size = sizeof(scanmap_e0) / sizeof(int);
248 size = char_dev_read(kbd->parent_sess, &code, 1);
249 }
250
251
252 kbd_event_type_t type;
253 if (code & 0x80) {
254 code &= ~0x80;
255 type = KEY_RELEASE;
256 } else {
257 type = KEY_PRESS;
258 }
259
260 if ((size_t) code >= map_size) {
261 ddf_msg(LVL_WARN,
262 "Unknown scancode: %hhx, size: %zd", code, size);
263 continue;
264 }
265
266 const unsigned key = map[code];
267 if (key != 0) {
268 async_exch_t *exch =
269 async_exchange_begin(kbd->input_sess);
270 if (!exch) {
271 ddf_msg(LVL_ERROR,
272 "Failed to create input exchange.");
273 continue;
274 }
275 async_msg_4(exch, KBDEV_EVENT, type, key, 0, 0);
276 async_exchange_end(exch);
277 }
278 }
279}
280/*----------------------------------------------------------------------------*/
281void default_connection_handler(ddf_fun_t *fun,
282 ipc_callid_t icallid, ipc_call_t *icall)
283{
284 if (fun == NULL || fun->driver_data == NULL) {
285 ddf_msg(LVL_ERROR, "%s: Missing parameter.\n", __FUNCTION__);
286 async_answer_0(icallid, EINVAL);
287 return;
288 }
289
290 const sysarg_t method = IPC_GET_IMETHOD(*icall);
291 xt_kbd_t *kbd = fun->driver_data;
292
293 switch (method) {
294 case KBDEV_SET_IND:
295 /* XT keyboards do not support setting mods */
296 async_answer_0(icallid, ENOTSUP);
297 break;
298 /* This might be ugly but async_callback_receive_start makes no
299 * difference for incorrect call and malloc failure. */
300 case IPC_M_CONNECT_TO_ME: {
301 async_sess_t *sess =
302 async_callback_receive_start(EXCHANGE_SERIALIZE, icall);
303 /* Probably ENOMEM error, try again. */
304 if (sess == NULL) {
305 ddf_msg(LVL_WARN,
306 "Failed to create start input session");
307 async_answer_0(icallid, EAGAIN);
308 break;
309 }
310 if (kbd->input_sess == NULL) {
311 kbd->input_sess = sess;
312 ddf_msg(LVL_DEBUG, "Set input session");
313 async_answer_0(icallid, EOK);
314 } else {
315 ddf_msg(LVL_ERROR, "Input session already set");
316 async_answer_0(icallid, ELIMIT);
317 }
318 break;
319 }
320 default:
321 ddf_msg(LVL_ERROR,
322 "Unknown method: %d.\n", (int)method);
323 async_answer_0(icallid, EINVAL);
324 break;
325 }
326}
Note: See TracBrowser for help on using the repository browser.