source: mainline/uspace/drv/char/xtkbd/xtkbd.c@ 1ff1ee1

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

xtkbd: Add support for setting LED indicators.

This breaks XT compatibility (XT protocol is unidirectional),
but was requested on ML in order to maintain feature parity with the old
driver.

  • Property mode set to 100644
File size: 8.3 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"
45/** Scancode set 1 table. */
46static const int scanmap_simple[] = {
47
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 SCANCODE_SET_EXTENDED 0xe0
156/** Scancode set 1 extended codes table */
157static const int scanmap_e0[] = {
158 [0x38] = KC_RALT,
159 [0x1d] = KC_RSHIFT,
160
161 [0x37] = KC_PRTSCR,
162
163 [0x52] = KC_INSERT,
164 [0x47] = KC_HOME,
165 [0x49] = KC_PAGE_UP,
166
167 [0x53] = KC_DELETE,
168 [0x4f] = KC_END,
169 [0x51] = KC_PAGE_DOWN,
170
171 [0x48] = KC_UP,
172 [0x4b] = KC_LEFT,
173 [0x50] = KC_DOWN,
174 [0x4d] = KC_RIGHT,
175
176 [0x35] = KC_NSLASH,
177 [0x1c] = KC_NENTER
178};
179
180#define KBD_CMD_SET_LEDS 0xed
181enum led_indicators {
182 LI_SCROLL = 0x01,
183 LI_NUM = 0x02,
184 LI_CAPS = 0x04,
185};
186
187static int polling(void *);
188static void default_connection_handler(ddf_fun_t *, ipc_callid_t, ipc_call_t *);
189
190/** Keyboard function ops. */
191static ddf_dev_ops_t kbd_ops = {
192 .default_handler = default_connection_handler
193};
194
195/** Initialize keyboard driver structure.
196 * @param kbd Keyboard driver structure to initialize.
197 * @param dev DDF device structure.
198 *
199 * Connects to parent, creates mouse function, starts polling fibril.
200 */
201int xt_kbd_init(xt_kbd_t *kbd, ddf_dev_t *dev)
202{
203 assert(kbd);
204 assert(dev);
205 kbd->input_sess = NULL;
206 kbd->parent_sess = devman_parent_device_connect(EXCHANGE_SERIALIZE,
207 dev->handle, IPC_FLAG_BLOCKING);
208 if (!kbd->parent_sess)
209 return ENOMEM;
210
211 kbd->kbd_fun = ddf_fun_create(dev, fun_exposed, "kbd");
212 if (!kbd->kbd_fun) {
213 async_hangup(kbd->parent_sess);
214 return ENOMEM;
215 }
216 kbd->kbd_fun->ops = &kbd_ops;
217 kbd->kbd_fun->driver_data = kbd;
218
219 int ret = ddf_fun_bind(kbd->kbd_fun);
220 if (ret != EOK) {
221 async_hangup(kbd->parent_sess);
222 kbd->kbd_fun->driver_data = NULL;
223 ddf_fun_destroy(kbd->kbd_fun);
224 return ENOMEM;
225 }
226
227 ret = ddf_fun_add_to_category(kbd->kbd_fun, "keyboard");
228 if (ret != EOK) {
229 async_hangup(kbd->parent_sess);
230 ddf_fun_unbind(kbd->kbd_fun);
231 kbd->kbd_fun->driver_data = NULL;
232 ddf_fun_destroy(kbd->kbd_fun);
233 return ENOMEM;
234 }
235
236 kbd->polling_fibril = fibril_create(polling, kbd);
237 if (!kbd->polling_fibril) {
238 async_hangup(kbd->parent_sess);
239 ddf_fun_unbind(kbd->kbd_fun);
240 kbd->kbd_fun->driver_data = NULL;
241 ddf_fun_destroy(kbd->kbd_fun);
242 return ENOMEM;
243 }
244 fibril_add_ready(kbd->polling_fibril);
245 return EOK;
246}
247
248/** Get data and parse scancodes.
249 * @param arg Pointer to xt_kbd_t structure.
250 * @return Never.
251 */
252int polling(void *arg)
253{
254 assert(arg);
255 const xt_kbd_t *kbd = arg;
256
257 assert(kbd->parent_sess);
258 while (1) {
259 const int *map = scanmap_simple;
260 size_t map_size = sizeof(scanmap_simple) / sizeof(int);
261
262 uint8_t code = 0;
263 ssize_t size = char_dev_read(kbd->parent_sess, &code, 1);
264
265 if (code == SCANCODE_SET_EXTENDED) {
266 map = scanmap_e0;
267 map_size = sizeof(scanmap_e0) / sizeof(int);
268 size = char_dev_read(kbd->parent_sess, &code, 1);
269 // TODO handle print screen
270 }
271
272 /* Invalid read. */
273 if (size != 1) {
274 continue;
275 }
276
277
278 /* Bit 7 indicates press/release */
279 const kbd_event_type_t type =
280 (code & 0x80) ? KEY_RELEASE : KEY_PRESS;
281 code &= ~0x80;
282
283 const unsigned key = (code < map_size) ? map[code] : 0;
284 if (key != 0) {
285 async_exch_t *exch =
286 async_exchange_begin(kbd->input_sess);
287 if (!exch) {
288 ddf_msg(LVL_ERROR,
289 "Failed to create input exchange.");
290 continue;
291 }
292 async_msg_4(exch, KBDEV_EVENT, type, key, 0, 0);
293 async_exchange_end(exch);
294 } else {
295 ddf_msg(LVL_WARN, "Unknown scancode: %hhx", code);
296 }
297 }
298}
299
300/** Default handler for IPC methods not handled by DDF.
301 *
302 * @param fun Device function handling the call.
303 * @param icallid Call id.
304 * @param icall Call data.
305 */
306void default_connection_handler(ddf_fun_t *fun,
307 ipc_callid_t icallid, ipc_call_t *icall)
308{
309 if (fun == NULL || fun->driver_data == NULL) {
310 ddf_msg(LVL_ERROR, "%s: Missing parameter.", __FUNCTION__);
311 async_answer_0(icallid, EINVAL);
312 return;
313 }
314
315 const sysarg_t method = IPC_GET_IMETHOD(*icall);
316 xt_kbd_t *kbd = fun->driver_data;
317
318 switch (method) {
319 case KBDEV_SET_IND: {
320 /* XT keyboards do not support setting mods,
321 * assume AT keyboard with Scan Code Set 1 */
322 const unsigned mods = IPC_GET_ARG1(*icall);
323 const uint8_t status = 0 |
324 ((mods & KM_CAPS_LOCK) ? LI_CAPS : 0) |
325 ((mods & KM_NUM_LOCK) ? LI_NUM : 0) |
326 ((mods & KM_SCROLL_LOCK) ? LI_SCROLL : 0);
327 uint8_t cmds[] = { KBD_CMD_SET_LEDS, status };
328 const ssize_t size =
329 char_dev_write(kbd->parent_sess, cmds, sizeof(cmds));
330 async_answer_0(icallid, size < 0 ? size : EOK);
331 break;
332 }
333 /* This might be ugly but async_callback_receive_start makes no
334 * difference for incorrect call and malloc failure. */
335 case IPC_M_CONNECT_TO_ME: {
336 async_sess_t *sess =
337 async_callback_receive_start(EXCHANGE_SERIALIZE, icall);
338 /* Probably ENOMEM error, try again. */
339 if (sess == NULL) {
340 ddf_msg(LVL_WARN,
341 "Failed to create start input session");
342 async_answer_0(icallid, EAGAIN);
343 break;
344 }
345 if (kbd->input_sess == NULL) {
346 kbd->input_sess = sess;
347 ddf_msg(LVL_DEBUG, "Set input session");
348 async_answer_0(icallid, EOK);
349 } else {
350 ddf_msg(LVL_ERROR, "Input session already set");
351 async_answer_0(icallid, ELIMIT);
352 }
353 break;
354 }
355 default:
356 ddf_msg(LVL_ERROR, "Unknown method: %d.", (int)method);
357 async_answer_0(icallid, EINVAL);
358 break;
359 }
360}
Note: See TracBrowser for help on using the repository browser.