1 | /*
|
---|
2 | * Copyright (c) 2011 Lubos Slovak
|
---|
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 |
|
---|
29 | /** @addtogroup drvusbhid
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /**
|
---|
33 | * @file
|
---|
34 | * USB HID keyboard device structure and API.
|
---|
35 | */
|
---|
36 |
|
---|
37 | #include <errno.h>
|
---|
38 | #include <str_error.h>
|
---|
39 | #include <fibril.h>
|
---|
40 | #include <stdio.h>
|
---|
41 |
|
---|
42 | #include <io/keycode.h>
|
---|
43 | #include <ipc/kbd.h>
|
---|
44 | #include <async.h>
|
---|
45 |
|
---|
46 | #include <usb/usb.h>
|
---|
47 | #include <usb/classes/hid.h>
|
---|
48 | #include <usb/pipes.h>
|
---|
49 | #include <usb/debug.h>
|
---|
50 | #include <usb/classes/hidparser.h>
|
---|
51 | #include <usb/classes/classes.h>
|
---|
52 |
|
---|
53 | #include "kbddev.h"
|
---|
54 | #include "hiddev.h"
|
---|
55 | #include "hidreq.h"
|
---|
56 | #include "layout.h"
|
---|
57 | #include "conv.h"
|
---|
58 |
|
---|
59 | /*----------------------------------------------------------------------------*/
|
---|
60 |
|
---|
61 | static const unsigned DEFAULT_ACTIVE_MODS = KM_NUM_LOCK;
|
---|
62 | static const size_t BOOTP_REPORT_SIZE = 6;
|
---|
63 | static const size_t BOOTP_BUFFER_SIZE = 8;
|
---|
64 | static const size_t BOOTP_BUFFER_OUT_SIZE = 1;
|
---|
65 | static const uint8_t BOOTP_ERROR_ROLLOVER = 1;
|
---|
66 | static const uint8_t IDLE_RATE = 0;
|
---|
67 |
|
---|
68 | /** Keyboard polling endpoint description for boot protocol class. */
|
---|
69 | static usb_endpoint_description_t poll_endpoint_description = {
|
---|
70 | .transfer_type = USB_TRANSFER_INTERRUPT,
|
---|
71 | .direction = USB_DIRECTION_IN,
|
---|
72 | .interface_class = USB_CLASS_HID,
|
---|
73 | .interface_subclass = USB_HID_SUBCLASS_BOOT,
|
---|
74 | .interface_protocol = USB_HID_PROTOCOL_KEYBOARD,
|
---|
75 | .flags = 0
|
---|
76 | };
|
---|
77 |
|
---|
78 | /*----------------------------------------------------------------------------*/
|
---|
79 | /* Keyboard layouts */
|
---|
80 | /*----------------------------------------------------------------------------*/
|
---|
81 |
|
---|
82 | #define NUM_LAYOUTS 3
|
---|
83 |
|
---|
84 | static layout_op_t *layout[NUM_LAYOUTS] = {
|
---|
85 | &us_qwerty_op,
|
---|
86 | &us_dvorak_op,
|
---|
87 | &cz_op
|
---|
88 | };
|
---|
89 |
|
---|
90 | static int active_layout = 0;
|
---|
91 |
|
---|
92 | /*----------------------------------------------------------------------------*/
|
---|
93 | /* Modifier constants */
|
---|
94 | /*----------------------------------------------------------------------------*/
|
---|
95 |
|
---|
96 | static const keycode_t usbhid_modifiers_keycodes[USB_HID_MOD_COUNT] = {
|
---|
97 | KC_LCTRL, /* USB_HID_MOD_LCTRL */
|
---|
98 | KC_LSHIFT, /* USB_HID_MOD_LSHIFT */
|
---|
99 | KC_LALT, /* USB_HID_MOD_LALT */
|
---|
100 | 0, /* USB_HID_MOD_LGUI */
|
---|
101 | KC_RCTRL, /* USB_HID_MOD_RCTRL */
|
---|
102 | KC_RSHIFT, /* USB_HID_MOD_RSHIFT */
|
---|
103 | KC_RALT, /* USB_HID_MOD_RALT */
|
---|
104 | 0, /* USB_HID_MOD_RGUI */
|
---|
105 | };
|
---|
106 |
|
---|
107 | /*----------------------------------------------------------------------------*/
|
---|
108 | /* IPC method handler */
|
---|
109 | /*----------------------------------------------------------------------------*/
|
---|
110 |
|
---|
111 | static void default_connection_handler(ddf_fun_t *, ipc_callid_t, ipc_call_t *);
|
---|
112 | static ddf_dev_ops_t keyboard_ops = {
|
---|
113 | .default_handler = default_connection_handler
|
---|
114 | };
|
---|
115 |
|
---|
116 | /** Default handler for IPC methods not handled by DDF.
|
---|
117 | *
|
---|
118 | * @param dev Device handling the call.
|
---|
119 | * @param icallid Call id.
|
---|
120 | * @param icall Call data.
|
---|
121 | */
|
---|
122 | void default_connection_handler(ddf_fun_t *fun,
|
---|
123 | ipc_callid_t icallid, ipc_call_t *icall)
|
---|
124 | {
|
---|
125 | sysarg_t method = IPC_GET_IMETHOD(*icall);
|
---|
126 |
|
---|
127 | usbhid_kbd_t *kbd_dev = (usbhid_kbd_t *)fun->driver_data;
|
---|
128 | assert(kbd_dev != NULL);
|
---|
129 |
|
---|
130 | if (method == IPC_M_CONNECT_TO_ME) {
|
---|
131 | int callback = IPC_GET_ARG5(*icall);
|
---|
132 |
|
---|
133 | if (kbd_dev->console_phone != -1) {
|
---|
134 | async_answer_0(icallid, ELIMIT);
|
---|
135 | return;
|
---|
136 | }
|
---|
137 |
|
---|
138 | kbd_dev->console_phone = callback;
|
---|
139 | async_answer_0(icallid, EOK);
|
---|
140 | return;
|
---|
141 | }
|
---|
142 |
|
---|
143 | async_answer_0(icallid, EINVAL);
|
---|
144 | }
|
---|
145 |
|
---|
146 | /*----------------------------------------------------------------------------*/
|
---|
147 | /* Key processing functions */
|
---|
148 | /*----------------------------------------------------------------------------*/
|
---|
149 |
|
---|
150 | static void usbhid_kbd_set_led(usbhid_kbd_t *kbd_dev)
|
---|
151 | {
|
---|
152 | uint8_t buffer[BOOTP_BUFFER_OUT_SIZE];
|
---|
153 | int rc= 0;
|
---|
154 |
|
---|
155 | memset(buffer, 0, BOOTP_BUFFER_OUT_SIZE);
|
---|
156 | uint8_t leds = 0;
|
---|
157 |
|
---|
158 | if (kbd_dev->mods & KM_NUM_LOCK) {
|
---|
159 | leds |= USB_HID_LED_NUM_LOCK;
|
---|
160 | }
|
---|
161 |
|
---|
162 | if (kbd_dev->mods & KM_CAPS_LOCK) {
|
---|
163 | leds |= USB_HID_LED_CAPS_LOCK;
|
---|
164 | }
|
---|
165 |
|
---|
166 | if (kbd_dev->mods & KM_SCROLL_LOCK) {
|
---|
167 | leds |= USB_HID_LED_SCROLL_LOCK;
|
---|
168 | }
|
---|
169 |
|
---|
170 | // TODO: COMPOSE and KANA
|
---|
171 |
|
---|
172 | usb_log_debug("Creating output report.\n");
|
---|
173 | usb_log_debug("Leds: 0x%x\n", leds);
|
---|
174 | if ((rc = usb_hid_boot_keyboard_output_report(
|
---|
175 | leds, buffer, BOOTP_BUFFER_OUT_SIZE)) != EOK) {
|
---|
176 | usb_log_warning("Error composing output report to the keyboard:"
|
---|
177 | "%s.\n", str_error(rc));
|
---|
178 | return;
|
---|
179 | }
|
---|
180 |
|
---|
181 | usb_log_debug("Output report buffer: %s\n",
|
---|
182 | usb_debug_str_buffer(buffer, BOOTP_BUFFER_OUT_SIZE, 0));
|
---|
183 |
|
---|
184 | assert(kbd_dev->hid_dev != NULL);
|
---|
185 | assert(kbd_dev->hid_dev->initialized);
|
---|
186 | usbhid_req_set_report(kbd_dev->hid_dev, USB_HID_REPORT_TYPE_OUTPUT,
|
---|
187 | buffer, BOOTP_BUFFER_OUT_SIZE);
|
---|
188 | }
|
---|
189 |
|
---|
190 | /*----------------------------------------------------------------------------*/
|
---|
191 |
|
---|
192 | static void usbhid_kbd_push_ev(usbhid_kbd_t *kbd_dev, int type,
|
---|
193 | unsigned int key)
|
---|
194 | {
|
---|
195 | console_event_t ev;
|
---|
196 | unsigned mod_mask;
|
---|
197 |
|
---|
198 | // TODO: replace by our own parsing?? or are the key codes identical??
|
---|
199 | switch (key) {
|
---|
200 | case KC_LCTRL: mod_mask = KM_LCTRL; break;
|
---|
201 | case KC_RCTRL: mod_mask = KM_RCTRL; break;
|
---|
202 | case KC_LSHIFT: mod_mask = KM_LSHIFT; break;
|
---|
203 | case KC_RSHIFT: mod_mask = KM_RSHIFT; break;
|
---|
204 | case KC_LALT: mod_mask = KM_LALT; break;
|
---|
205 | case KC_RALT: mod_mask = KM_RALT; break;
|
---|
206 | default: mod_mask = 0; break;
|
---|
207 | }
|
---|
208 |
|
---|
209 | if (mod_mask != 0) {
|
---|
210 | if (type == KEY_PRESS)
|
---|
211 | kbd_dev->mods = kbd_dev->mods | mod_mask;
|
---|
212 | else
|
---|
213 | kbd_dev->mods = kbd_dev->mods & ~mod_mask;
|
---|
214 | }
|
---|
215 |
|
---|
216 | switch (key) {
|
---|
217 | case KC_CAPS_LOCK: mod_mask = KM_CAPS_LOCK; break;
|
---|
218 | case KC_NUM_LOCK: mod_mask = KM_NUM_LOCK; break;
|
---|
219 | case KC_SCROLL_LOCK: mod_mask = KM_SCROLL_LOCK; break;
|
---|
220 | default: mod_mask = 0; break;
|
---|
221 | }
|
---|
222 |
|
---|
223 | if (mod_mask != 0) {
|
---|
224 | if (type == KEY_PRESS) {
|
---|
225 | /*
|
---|
226 | * Only change lock state on transition from released
|
---|
227 | * to pressed. This prevents autorepeat from messing
|
---|
228 | * up the lock state.
|
---|
229 | */
|
---|
230 | kbd_dev->mods =
|
---|
231 | kbd_dev->mods ^ (mod_mask & ~kbd_dev->lock_keys);
|
---|
232 | kbd_dev->lock_keys = kbd_dev->lock_keys | mod_mask;
|
---|
233 |
|
---|
234 | /* Update keyboard lock indicator lights. */
|
---|
235 | usbhid_kbd_set_led(kbd_dev);
|
---|
236 | } else {
|
---|
237 | kbd_dev->lock_keys = kbd_dev->lock_keys & ~mod_mask;
|
---|
238 | }
|
---|
239 | }
|
---|
240 |
|
---|
241 | if (key == KC_CAPS_LOCK || key == KC_NUM_LOCK || key == KC_SCROLL_LOCK) {
|
---|
242 | // do not send anything to the console, this is our business
|
---|
243 | return;
|
---|
244 | }
|
---|
245 |
|
---|
246 | if (type == KEY_PRESS && (kbd_dev->mods & KM_LCTRL) && key == KC_F1) {
|
---|
247 | active_layout = 0;
|
---|
248 | layout[active_layout]->reset();
|
---|
249 | return;
|
---|
250 | }
|
---|
251 |
|
---|
252 | if (type == KEY_PRESS && (kbd_dev->mods & KM_LCTRL) && key == KC_F2) {
|
---|
253 | active_layout = 1;
|
---|
254 | layout[active_layout]->reset();
|
---|
255 | return;
|
---|
256 | }
|
---|
257 |
|
---|
258 | if (type == KEY_PRESS && (kbd_dev->mods & KM_LCTRL) && key == KC_F3) {
|
---|
259 | active_layout = 2;
|
---|
260 | layout[active_layout]->reset();
|
---|
261 | return;
|
---|
262 | }
|
---|
263 |
|
---|
264 | ev.type = type;
|
---|
265 | ev.key = key;
|
---|
266 | ev.mods = kbd_dev->mods;
|
---|
267 |
|
---|
268 | ev.c = layout[active_layout]->parse_ev(&ev);
|
---|
269 |
|
---|
270 | usb_log_debug2("Sending key %d to the console\n", ev.key);
|
---|
271 | if (kbd_dev->console_phone < 0) {
|
---|
272 | usb_log_warning(
|
---|
273 | "Connection to console not ready, key discarded.\n");
|
---|
274 | return;
|
---|
275 | }
|
---|
276 |
|
---|
277 | async_msg_4(kbd_dev->console_phone, KBD_EVENT, ev.type, ev.key,
|
---|
278 | ev.mods, ev.c);
|
---|
279 | }
|
---|
280 |
|
---|
281 | /*----------------------------------------------------------------------------*/
|
---|
282 |
|
---|
283 | static void usbhid_kbd_check_modifier_changes(usbhid_kbd_t *kbd_dev,
|
---|
284 | uint8_t modifiers)
|
---|
285 | {
|
---|
286 | /*
|
---|
287 | * TODO: why the USB keyboard has NUM_, SCROLL_ and CAPS_LOCK
|
---|
288 | * both as modifiers and as keys with their own scancodes???
|
---|
289 | *
|
---|
290 | * modifiers should be sent as normal keys to usbhid_parse_scancode()!!
|
---|
291 | * so maybe it would be better if I received it from report parser in
|
---|
292 | * that way
|
---|
293 | */
|
---|
294 |
|
---|
295 | int i;
|
---|
296 | for (i = 0; i < USB_HID_MOD_COUNT; ++i) {
|
---|
297 | if ((modifiers & usb_hid_modifiers_consts[i]) &&
|
---|
298 | !(kbd_dev->modifiers & usb_hid_modifiers_consts[i])) {
|
---|
299 | // modifier pressed
|
---|
300 | if (usbhid_modifiers_keycodes[i] != 0) {
|
---|
301 | usbhid_kbd_push_ev(kbd_dev, KEY_PRESS,
|
---|
302 | usbhid_modifiers_keycodes[i]);
|
---|
303 | }
|
---|
304 | } else if (!(modifiers & usb_hid_modifiers_consts[i]) &&
|
---|
305 | (kbd_dev->modifiers & usb_hid_modifiers_consts[i])) {
|
---|
306 | // modifier released
|
---|
307 | if (usbhid_modifiers_keycodes[i] != 0) {
|
---|
308 | usbhid_kbd_push_ev(kbd_dev, KEY_RELEASE,
|
---|
309 | usbhid_modifiers_keycodes[i]);
|
---|
310 | }
|
---|
311 | } // no change
|
---|
312 | }
|
---|
313 |
|
---|
314 | kbd_dev->modifiers = modifiers;
|
---|
315 | }
|
---|
316 |
|
---|
317 | /*----------------------------------------------------------------------------*/
|
---|
318 |
|
---|
319 | static void usbhid_kbd_check_key_changes(usbhid_kbd_t *kbd_dev,
|
---|
320 | const uint8_t *key_codes)
|
---|
321 | {
|
---|
322 | unsigned int key;
|
---|
323 | unsigned int i, j;
|
---|
324 |
|
---|
325 | /*
|
---|
326 | * First of all, check if the kbd have reported phantom state.
|
---|
327 | */
|
---|
328 | i = 0;
|
---|
329 | // all fields should report Error Rollover
|
---|
330 | while (i < kbd_dev->keycode_count &&
|
---|
331 | key_codes[i] == BOOTP_ERROR_ROLLOVER) {
|
---|
332 | ++i;
|
---|
333 | }
|
---|
334 | if (i == kbd_dev->keycode_count) {
|
---|
335 | usb_log_debug("Phantom state occured.\n");
|
---|
336 | // phantom state, do nothing
|
---|
337 | return;
|
---|
338 | }
|
---|
339 |
|
---|
340 | // TODO: quite dummy right now, think of better implementation
|
---|
341 |
|
---|
342 | /*
|
---|
343 | * 1) Key releases
|
---|
344 | */
|
---|
345 | for (j = 0; j < kbd_dev->keycode_count; ++j) {
|
---|
346 | // try to find the old key in the new key list
|
---|
347 | i = 0;
|
---|
348 | while (i < kbd_dev->keycode_count
|
---|
349 | && key_codes[i] != kbd_dev->keycodes[j]) {
|
---|
350 | ++i;
|
---|
351 | }
|
---|
352 |
|
---|
353 | if (i == kbd_dev->keycode_count) {
|
---|
354 | // not found, i.e. the key was released
|
---|
355 | key = usbhid_parse_scancode(kbd_dev->keycodes[j]);
|
---|
356 | usbhid_kbd_push_ev(kbd_dev, KEY_RELEASE, key);
|
---|
357 | usb_log_debug2("Key released: %d\n", key);
|
---|
358 | } else {
|
---|
359 | // found, nothing happens
|
---|
360 | }
|
---|
361 | }
|
---|
362 |
|
---|
363 | /*
|
---|
364 | * 1) Key presses
|
---|
365 | */
|
---|
366 | for (i = 0; i < kbd_dev->keycode_count; ++i) {
|
---|
367 | // try to find the new key in the old key list
|
---|
368 | j = 0;
|
---|
369 | while (j < kbd_dev->keycode_count
|
---|
370 | && kbd_dev->keycodes[j] != key_codes[i]) {
|
---|
371 | ++j;
|
---|
372 | }
|
---|
373 |
|
---|
374 | if (j == kbd_dev->keycode_count) {
|
---|
375 | // not found, i.e. new key pressed
|
---|
376 | key = usbhid_parse_scancode(key_codes[i]);
|
---|
377 | usb_log_debug2("Key pressed: %d (keycode: %d)\n", key,
|
---|
378 | key_codes[i]);
|
---|
379 | usbhid_kbd_push_ev(kbd_dev, KEY_PRESS, key);
|
---|
380 | } else {
|
---|
381 | // found, nothing happens
|
---|
382 | }
|
---|
383 | }
|
---|
384 | // // report all currently pressed keys
|
---|
385 | // for (i = 0; i < kbd_dev->keycode_count; ++i) {
|
---|
386 | // if (key_codes[i] != 0) {
|
---|
387 | // key = usbhid_parse_scancode(key_codes[i]);
|
---|
388 | // usb_log_debug2("Key pressed: %d (keycode: %d)\n", key,
|
---|
389 | // key_codes[i]);
|
---|
390 | // usbhid_kbd_push_ev(kbd_dev, KEY_PRESS, key);
|
---|
391 | // }
|
---|
392 | // }
|
---|
393 |
|
---|
394 | memcpy(kbd_dev->keycodes, key_codes, kbd_dev->keycode_count);
|
---|
395 |
|
---|
396 | usb_log_debug("New stored keycodes: %s\n",
|
---|
397 | usb_debug_str_buffer(kbd_dev->keycodes, kbd_dev->keycode_count, 0));
|
---|
398 | }
|
---|
399 |
|
---|
400 | /*----------------------------------------------------------------------------*/
|
---|
401 | /* Callbacks for parser */
|
---|
402 | /*----------------------------------------------------------------------------*/
|
---|
403 |
|
---|
404 | static void usbhid_kbd_process_keycodes(const uint8_t *key_codes, size_t count,
|
---|
405 | uint8_t modifiers, void *arg)
|
---|
406 | {
|
---|
407 | if (arg == NULL) {
|
---|
408 | usb_log_warning("Missing argument in callback "
|
---|
409 | "usbhid_process_keycodes().\n");
|
---|
410 | return;
|
---|
411 | }
|
---|
412 |
|
---|
413 | usbhid_kbd_t *kbd_dev = (usbhid_kbd_t *)arg;
|
---|
414 | assert(kbd_dev != NULL);
|
---|
415 |
|
---|
416 | usb_log_debug("Got keys from parser: %s\n",
|
---|
417 | usb_debug_str_buffer(key_codes, kbd_dev->keycode_count, 0));
|
---|
418 |
|
---|
419 | if (count != kbd_dev->keycode_count) {
|
---|
420 | usb_log_warning("Number of received keycodes (%d) differs from"
|
---|
421 | " expected number (%d).\n", count, kbd_dev->keycode_count);
|
---|
422 | return;
|
---|
423 | }
|
---|
424 |
|
---|
425 | usbhid_kbd_check_modifier_changes(kbd_dev, modifiers);
|
---|
426 | usbhid_kbd_check_key_changes(kbd_dev, key_codes);
|
---|
427 | }
|
---|
428 |
|
---|
429 | /*----------------------------------------------------------------------------*/
|
---|
430 | /* General kbd functions */
|
---|
431 | /*----------------------------------------------------------------------------*/
|
---|
432 |
|
---|
433 | static void usbhid_kbd_process_data(usbhid_kbd_t *kbd_dev,
|
---|
434 | uint8_t *buffer, size_t actual_size)
|
---|
435 | {
|
---|
436 | usb_hid_report_in_callbacks_t *callbacks =
|
---|
437 | (usb_hid_report_in_callbacks_t *)malloc(
|
---|
438 | sizeof(usb_hid_report_in_callbacks_t));
|
---|
439 |
|
---|
440 | callbacks->keyboard = usbhid_kbd_process_keycodes;
|
---|
441 |
|
---|
442 | usb_log_debug("Calling usb_hid_boot_keyboard_input_report() with "
|
---|
443 | "buffer %s\n", usb_debug_str_buffer(buffer, actual_size, 0));
|
---|
444 |
|
---|
445 | int rc = usb_hid_boot_keyboard_input_report(buffer, actual_size,
|
---|
446 | callbacks, kbd_dev);
|
---|
447 |
|
---|
448 | if (rc != EOK) {
|
---|
449 | usb_log_warning("Error in usb_hid_boot_keyboard_input_report():"
|
---|
450 | "%s\n", str_error(rc));
|
---|
451 | }
|
---|
452 | }
|
---|
453 |
|
---|
454 | /*----------------------------------------------------------------------------*/
|
---|
455 | /* HID/KBD structure manipulation */
|
---|
456 | /*----------------------------------------------------------------------------*/
|
---|
457 |
|
---|
458 | static usbhid_kbd_t *usbhid_kbd_new(void)
|
---|
459 | {
|
---|
460 | usbhid_kbd_t *kbd_dev =
|
---|
461 | (usbhid_kbd_t *)malloc(sizeof(usbhid_kbd_t));
|
---|
462 |
|
---|
463 | if (kbd_dev == NULL) {
|
---|
464 | usb_log_fatal("No memory!\n");
|
---|
465 | return NULL;
|
---|
466 | }
|
---|
467 |
|
---|
468 | memset(kbd_dev, 0, sizeof(usbhid_kbd_t));
|
---|
469 |
|
---|
470 | kbd_dev->hid_dev = usbhid_dev_new();
|
---|
471 | if (kbd_dev->hid_dev == NULL) {
|
---|
472 | usb_log_fatal("Could not create HID device structure.\n");
|
---|
473 | return NULL;
|
---|
474 | }
|
---|
475 |
|
---|
476 | kbd_dev->console_phone = -1;
|
---|
477 | kbd_dev->initialized = 0;
|
---|
478 |
|
---|
479 | return kbd_dev;
|
---|
480 | }
|
---|
481 |
|
---|
482 | /*----------------------------------------------------------------------------*/
|
---|
483 |
|
---|
484 | static void usbhid_kbd_free(usbhid_kbd_t **kbd_dev)
|
---|
485 | {
|
---|
486 | if (kbd_dev == NULL || *kbd_dev == NULL) {
|
---|
487 | return;
|
---|
488 | }
|
---|
489 |
|
---|
490 | // hangup phone to the console
|
---|
491 | async_hangup((*kbd_dev)->console_phone);
|
---|
492 |
|
---|
493 | if ((*kbd_dev)->hid_dev != NULL) {
|
---|
494 | usbhid_dev_free(&(*kbd_dev)->hid_dev);
|
---|
495 | assert((*kbd_dev)->hid_dev == NULL);
|
---|
496 | }
|
---|
497 |
|
---|
498 | free(*kbd_dev);
|
---|
499 | *kbd_dev = NULL;
|
---|
500 | }
|
---|
501 |
|
---|
502 | /*----------------------------------------------------------------------------*/
|
---|
503 |
|
---|
504 | static int usbhid_kbd_init(usbhid_kbd_t *kbd_dev, ddf_dev_t *dev)
|
---|
505 | {
|
---|
506 | int rc;
|
---|
507 |
|
---|
508 | usb_log_info("Initializing HID/KBD structure...\n");
|
---|
509 |
|
---|
510 | if (kbd_dev == NULL) {
|
---|
511 | usb_log_error("Failed to init keyboard structure: no structure"
|
---|
512 | " given.\n");
|
---|
513 | return EINVAL;
|
---|
514 | }
|
---|
515 |
|
---|
516 | if (dev == NULL) {
|
---|
517 | usb_log_error("Failed to init keyboard structure: no device"
|
---|
518 | " given.\n");
|
---|
519 | return EINVAL;
|
---|
520 | }
|
---|
521 |
|
---|
522 | if (kbd_dev->initialized) {
|
---|
523 | usb_log_warning("Keyboard structure already initialized.\n");
|
---|
524 | return EINVAL;
|
---|
525 | }
|
---|
526 |
|
---|
527 | rc = usbhid_dev_init(kbd_dev->hid_dev, dev, &poll_endpoint_description);
|
---|
528 |
|
---|
529 | if (rc != EOK) {
|
---|
530 | usb_log_error("Failed to initialize HID device structure: %s\n",
|
---|
531 | str_error(rc));
|
---|
532 | return rc;
|
---|
533 | }
|
---|
534 |
|
---|
535 | assert(kbd_dev->hid_dev->initialized);
|
---|
536 |
|
---|
537 | // save the size of the report (boot protocol report by default)
|
---|
538 | kbd_dev->keycode_count = BOOTP_REPORT_SIZE;
|
---|
539 | kbd_dev->keycodes = (uint8_t *)calloc(
|
---|
540 | kbd_dev->keycode_count, sizeof(uint8_t));
|
---|
541 |
|
---|
542 | if (kbd_dev->keycodes == NULL) {
|
---|
543 | usb_log_fatal("No memory!\n");
|
---|
544 | return rc;
|
---|
545 | }
|
---|
546 |
|
---|
547 | kbd_dev->modifiers = 0;
|
---|
548 | kbd_dev->mods = DEFAULT_ACTIVE_MODS;
|
---|
549 | kbd_dev->lock_keys = 0;
|
---|
550 |
|
---|
551 | /*
|
---|
552 | * Set boot protocol.
|
---|
553 | * Set LEDs according to initial setup.
|
---|
554 | * Set Idle rate
|
---|
555 | */
|
---|
556 | assert(kbd_dev->hid_dev != NULL);
|
---|
557 | assert(kbd_dev->hid_dev->initialized);
|
---|
558 | usbhid_req_set_protocol(kbd_dev->hid_dev, USB_HID_PROTOCOL_BOOT);
|
---|
559 |
|
---|
560 | usbhid_kbd_set_led(kbd_dev);
|
---|
561 |
|
---|
562 | usbhid_req_set_idle(kbd_dev->hid_dev, IDLE_RATE);
|
---|
563 |
|
---|
564 | kbd_dev->initialized = 1;
|
---|
565 | usb_log_info("HID/KBD device structure initialized.\n");
|
---|
566 |
|
---|
567 | return EOK;
|
---|
568 | }
|
---|
569 |
|
---|
570 | /*----------------------------------------------------------------------------*/
|
---|
571 | /* HID/KBD polling */
|
---|
572 | /*----------------------------------------------------------------------------*/
|
---|
573 |
|
---|
574 | static void usbhid_kbd_poll(usbhid_kbd_t *kbd_dev)
|
---|
575 | {
|
---|
576 | int rc, sess_rc;
|
---|
577 | uint8_t buffer[BOOTP_BUFFER_SIZE];
|
---|
578 | size_t actual_size;
|
---|
579 |
|
---|
580 | usb_log_info("Polling keyboard...\n");
|
---|
581 |
|
---|
582 | if (!kbd_dev->initialized) {
|
---|
583 | usb_log_error("HID/KBD device not initialized!\n");
|
---|
584 | return;
|
---|
585 | }
|
---|
586 |
|
---|
587 | assert(kbd_dev->hid_dev != NULL);
|
---|
588 | assert(kbd_dev->hid_dev->initialized);
|
---|
589 |
|
---|
590 | while (true) {
|
---|
591 | sess_rc = usb_endpoint_pipe_start_session(
|
---|
592 | &kbd_dev->hid_dev->poll_pipe);
|
---|
593 | if (sess_rc != EOK) {
|
---|
594 | usb_log_warning("Failed to start a session: %s.\n",
|
---|
595 | str_error(sess_rc));
|
---|
596 | continue;
|
---|
597 | }
|
---|
598 |
|
---|
599 | rc = usb_endpoint_pipe_read(&kbd_dev->hid_dev->poll_pipe,
|
---|
600 | buffer, BOOTP_BUFFER_SIZE, &actual_size);
|
---|
601 |
|
---|
602 | sess_rc = usb_endpoint_pipe_end_session(
|
---|
603 | &kbd_dev->hid_dev->poll_pipe);
|
---|
604 |
|
---|
605 | if (rc != EOK) {
|
---|
606 | usb_log_warning("Error polling the keyboard: %s.\n",
|
---|
607 | str_error(rc));
|
---|
608 | continue;
|
---|
609 | }
|
---|
610 |
|
---|
611 | if (sess_rc != EOK) {
|
---|
612 | usb_log_warning("Error closing session: %s.\n",
|
---|
613 | str_error(sess_rc));
|
---|
614 | continue;
|
---|
615 | }
|
---|
616 |
|
---|
617 | /*
|
---|
618 | * If the keyboard answered with NAK, it returned no data.
|
---|
619 | * This implies that no change happened since last query.
|
---|
620 | */
|
---|
621 | if (actual_size == 0) {
|
---|
622 | usb_log_debug("Keyboard returned NAK\n");
|
---|
623 | continue;
|
---|
624 | }
|
---|
625 |
|
---|
626 | /*
|
---|
627 | * TODO: Process pressed keys.
|
---|
628 | */
|
---|
629 | usb_log_debug("Calling usbhid_kbd_process_data()\n");
|
---|
630 | usbhid_kbd_process_data(kbd_dev, buffer, actual_size);
|
---|
631 |
|
---|
632 | // disabled for now, no reason to sleep
|
---|
633 | //async_usleep(kbd_dev->hid_dev->poll_interval);
|
---|
634 | }
|
---|
635 |
|
---|
636 | // not reached
|
---|
637 | assert(0);
|
---|
638 | }
|
---|
639 |
|
---|
640 | /*----------------------------------------------------------------------------*/
|
---|
641 |
|
---|
642 | static int usbhid_kbd_fibril(void *arg)
|
---|
643 | {
|
---|
644 | if (arg == NULL) {
|
---|
645 | usb_log_error("No device!\n");
|
---|
646 | return EINVAL;
|
---|
647 | }
|
---|
648 |
|
---|
649 | usbhid_kbd_t *kbd_dev = (usbhid_kbd_t *)arg;
|
---|
650 |
|
---|
651 | usbhid_kbd_poll(kbd_dev);
|
---|
652 |
|
---|
653 | // at the end, properly destroy the KBD structure
|
---|
654 | usbhid_kbd_free(&kbd_dev);
|
---|
655 | assert(kbd_dev == NULL);
|
---|
656 |
|
---|
657 | return EOK;
|
---|
658 | }
|
---|
659 |
|
---|
660 | /*----------------------------------------------------------------------------*/
|
---|
661 | /* API functions */
|
---|
662 | /*----------------------------------------------------------------------------*/
|
---|
663 |
|
---|
664 | int usbhid_kbd_try_add_device(ddf_dev_t *dev)
|
---|
665 | {
|
---|
666 | /*
|
---|
667 | * Create default function.
|
---|
668 | */
|
---|
669 | ddf_fun_t *kbd_fun = ddf_fun_create(dev, fun_exposed, "keyboard");
|
---|
670 | if (kbd_fun == NULL) {
|
---|
671 | usb_log_error("Could not create DDF function node.\n");
|
---|
672 | return ENOMEM;
|
---|
673 | }
|
---|
674 |
|
---|
675 | /*
|
---|
676 | * Initialize device (get and process descriptors, get address, etc.)
|
---|
677 | */
|
---|
678 | usb_log_info("Initializing USB/HID KBD device...\n");
|
---|
679 |
|
---|
680 | usbhid_kbd_t *kbd_dev = usbhid_kbd_new();
|
---|
681 | if (kbd_dev == NULL) {
|
---|
682 | usb_log_error("Error while creating USB/HID KBD device "
|
---|
683 | "structure.\n");
|
---|
684 | ddf_fun_destroy(kbd_fun);
|
---|
685 | return EINVAL; // TODO: some other code??
|
---|
686 | }
|
---|
687 |
|
---|
688 | int rc = usbhid_kbd_init(kbd_dev, dev);
|
---|
689 |
|
---|
690 | if (rc != EOK) {
|
---|
691 | usb_log_error("Failed to initialize USB/HID KBD device.\n");
|
---|
692 | ddf_fun_destroy(kbd_fun);
|
---|
693 | usbhid_kbd_free(&kbd_dev);
|
---|
694 | return rc;
|
---|
695 | }
|
---|
696 |
|
---|
697 | usb_log_info("USB/HID KBD device structure initialized.\n");
|
---|
698 |
|
---|
699 | /*
|
---|
700 | * Store the initialized keyboard device and keyboard ops
|
---|
701 | * to the DDF function.
|
---|
702 | */
|
---|
703 | kbd_fun->driver_data = kbd_dev;
|
---|
704 | kbd_fun->ops = &keyboard_ops;
|
---|
705 |
|
---|
706 | rc = ddf_fun_bind(kbd_fun);
|
---|
707 | if (rc != EOK) {
|
---|
708 | usb_log_error("Could not bind DDF function.\n");
|
---|
709 | // TODO: Can / should I destroy the DDF function?
|
---|
710 | ddf_fun_destroy(kbd_fun);
|
---|
711 | usbhid_kbd_free(&kbd_dev);
|
---|
712 | return rc;
|
---|
713 | }
|
---|
714 |
|
---|
715 | rc = ddf_fun_add_to_class(kbd_fun, "keyboard");
|
---|
716 | if (rc != EOK) {
|
---|
717 | usb_log_error("Could not add DDF function to class 'keyboard'"
|
---|
718 | "\n");
|
---|
719 | // TODO: Can / should I destroy the DDF function?
|
---|
720 | ddf_fun_destroy(kbd_fun);
|
---|
721 | usbhid_kbd_free(&kbd_dev);
|
---|
722 | return rc;
|
---|
723 | }
|
---|
724 |
|
---|
725 | /*
|
---|
726 | * Create new fibril for handling this keyboard
|
---|
727 | */
|
---|
728 | fid_t fid = fibril_create(usbhid_kbd_fibril, kbd_dev);
|
---|
729 | if (fid == 0) {
|
---|
730 | usb_log_error("Failed to start fibril for KBD device\n");
|
---|
731 | return ENOMEM;
|
---|
732 | }
|
---|
733 | fibril_add_ready(fid);
|
---|
734 |
|
---|
735 | (void)keyboard_ops;
|
---|
736 |
|
---|
737 | /*
|
---|
738 | * Hurrah, device is initialized.
|
---|
739 | */
|
---|
740 | return EOK;
|
---|
741 | }
|
---|
742 |
|
---|
743 | /**
|
---|
744 | * @}
|
---|
745 | */
|
---|