source: mainline/uspace/drv/usbhid/kbd/kbddev.c@ 7f2e33a

lfn serial ticket/834-toolchain-update topic/fix-logger-deadlock topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 7f2e33a was 7f2e33a, checked in by Lubos Slovak <lubos.slovak@…>, 15 years ago

Fixed requested Report ID, some debugging stuff.

  • Property mode set to 100644
File size: 28.9 KB
Line 
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 <stdio.h>
40
41#include <io/keycode.h>
42#include <ipc/kbd.h>
43#include <async.h>
44#include <fibril.h>
45#include <fibril_synch.h>
46
47#include <usb/usb.h>
48#include <usb/dp.h>
49#include <usb/request.h>
50#include <usb/classes/hid.h>
51#include <usb/pipes.h>
52#include <usb/debug.h>
53#include <usb/classes/hidparser.h>
54#include <usb/classes/classes.h>
55#include <usb/classes/hidut.h>
56#include <usb/classes/hidreq.h>
57#include <usb/classes/hidreport.h>
58#include <usb/classes/hid/utled.h>
59
60#include <usb/devdrv.h>
61
62#include "kbddev.h"
63
64#include "layout.h"
65#include "conv.h"
66#include "kbdrepeat.h"
67
68#include "../usbhid.h"
69
70/*----------------------------------------------------------------------------*/
71/** Default modifiers when the keyboard is initialized. */
72static const unsigned DEFAULT_ACTIVE_MODS = KM_NUM_LOCK;
73
74///** Boot protocol report size (key part). */
75//static const size_t BOOTP_REPORT_SIZE = 6;
76
77///** Boot protocol total report size. */
78//static const size_t BOOTP_BUFFER_SIZE = 8;
79
80///** Boot protocol output report size. */
81//static const size_t BOOTP_BUFFER_OUT_SIZE = 1;
82
83///** Boot protocol error key code. */
84//static const uint8_t BOOTP_ERROR_ROLLOVER = 1;
85static const uint8_t ERROR_ROLLOVER = 1;
86
87/** Default idle rate for keyboards. */
88static const uint8_t IDLE_RATE = 0;
89
90/** Delay before a pressed key starts auto-repeating. */
91static const unsigned int DEFAULT_DELAY_BEFORE_FIRST_REPEAT = 500 * 1000;
92
93/** Delay between two repeats of a pressed key when auto-repeating. */
94static const unsigned int DEFAULT_REPEAT_DELAY = 50 * 1000;
95
96/*----------------------------------------------------------------------------*/
97
98/** Keyboard polling endpoint description for boot protocol class. */
99usb_endpoint_description_t ush_hid_kbd_poll_endpoint_description = {
100 .transfer_type = USB_TRANSFER_INTERRUPT,
101 .direction = USB_DIRECTION_IN,
102 .interface_class = USB_CLASS_HID,
103 .interface_subclass = USB_HID_SUBCLASS_BOOT,
104 .interface_protocol = USB_HID_PROTOCOL_KEYBOARD,
105 .flags = 0
106};
107
108//static usb_endpoint_description_t hid_poll_endpoint_description = {
109// .transfer_type = USB_TRANSFER_INTERRUPT,
110// .direction = USB_DIRECTION_IN,
111// .interface_class = USB_CLASS_HID,
112// .flags = 0
113//};
114
115///* Array of endpoints expected on the device, NULL terminated. */
116//usb_endpoint_description_t
117// *usb_kbd_endpoints[USB_KBD_POLL_EP_COUNT + 1] = {
118// &boot_poll_endpoint_description,
119// &hid_poll_endpoint_description,
120// NULL
121//};
122
123const char *HID_KBD_FUN_NAME = "keyboard";
124const char *HID_KBD_CLASS_NAME = "keyboard";
125
126/*----------------------------------------------------------------------------*/
127
128enum {
129 USB_KBD_BOOT_REPORT_DESCRIPTOR_SIZE = 63
130};
131
132static const uint8_t USB_KBD_BOOT_REPORT_DESCRIPTOR[
133 USB_KBD_BOOT_REPORT_DESCRIPTOR_SIZE] = {
134 0x05, 0x01, // Usage Page (Generic Desktop),
135 0x09, 0x06, // Usage (Keyboard),
136 0xA1, 0x01, // Collection (Application),
137 0x75, 0x01, // Report Size (1),
138 0x95, 0x08, // Report Count (8),
139 0x05, 0x07, // Usage Page (Key Codes);
140 0x19, 0xE0, // Usage Minimum (224),
141 0x29, 0xE7, // Usage Maximum (231),
142 0x15, 0x00, // Logical Minimum (0),
143 0x25, 0x01, // Logical Maximum (1),
144 0x81, 0x02, // Input (Data, Variable, Absolute), ; Modifier byte
145 0x95, 0x01, // Report Count (1),
146 0x75, 0x08, // Report Size (8),
147 0x81, 0x01, // Input (Constant), ; Reserved byte
148 0x95, 0x05, // Report Count (5),
149 0x75, 0x01, // Report Size (1),
150 0x05, 0x08, // Usage Page (Page# for LEDs),
151 0x19, 0x01, // Usage Minimum (1),
152 0x29, 0x05, // Usage Maxmimum (5),
153 0x91, 0x02, // Output (Data, Variable, Absolute), ; LED report
154 0x95, 0x01, // Report Count (1),
155 0x75, 0x03, // Report Size (3),
156 0x91, 0x01, // Output (Constant), ; LED report padding
157 0x95, 0x06, // Report Count (6),
158 0x75, 0x08, // Report Size (8),
159 0x15, 0x00, // Logical Minimum (0),
160 0x25, 0xff, // Logical Maximum (255),
161 0x05, 0x07, // Usage Page (Key Codes),
162 0x19, 0x00, // Usage Minimum (0),
163 0x29, 0xff, // Usage Maximum (255),
164 0x81, 0x00, // Input (Data, Array), ; Key arrays (6 bytes)
165 0xC0 // End Collection
166
167};
168
169/*----------------------------------------------------------------------------*/
170
171typedef enum usb_kbd_flags {
172 USB_KBD_STATUS_UNINITIALIZED = 0,
173 USB_KBD_STATUS_INITIALIZED = 1,
174 USB_KBD_STATUS_TO_DESTROY = -1
175} usb_kbd_flags;
176
177/*----------------------------------------------------------------------------*/
178/* Keyboard layouts */
179/*----------------------------------------------------------------------------*/
180
181#define NUM_LAYOUTS 3
182
183/** Keyboard layout map. */
184static layout_op_t *layout[NUM_LAYOUTS] = {
185 &us_qwerty_op,
186 &us_dvorak_op,
187 &cz_op
188};
189
190static int active_layout = 0;
191
192/*----------------------------------------------------------------------------*/
193/* Modifier constants */
194/*----------------------------------------------------------------------------*/
195/** Mapping of USB modifier key codes to generic modifier key codes. */
196static const keycode_t usbhid_modifiers_keycodes[USB_HID_MOD_COUNT] = {
197 KC_LCTRL, /* USB_HID_MOD_LCTRL */
198 KC_LSHIFT, /* USB_HID_MOD_LSHIFT */
199 KC_LALT, /* USB_HID_MOD_LALT */
200 0, /* USB_HID_MOD_LGUI */
201 KC_RCTRL, /* USB_HID_MOD_RCTRL */
202 KC_RSHIFT, /* USB_HID_MOD_RSHIFT */
203 KC_RALT, /* USB_HID_MOD_RALT */
204 0, /* USB_HID_MOD_RGUI */
205};
206
207typedef enum usbhid_lock_code {
208 USB_KBD_LOCK_NUM = 0x53,
209 USB_KBD_LOCK_CAPS = 0x39,
210 USB_KBD_LOCK_SCROLL = 0x47,
211 USB_KBD_LOCK_COUNT = 3
212} usbhid_lock_code;
213
214static const usbhid_lock_code usbhid_lock_codes[USB_KBD_LOCK_COUNT] = {
215 USB_KBD_LOCK_NUM,
216 USB_KBD_LOCK_CAPS,
217 USB_KBD_LOCK_SCROLL
218};
219
220/*----------------------------------------------------------------------------*/
221/* IPC method handler */
222/*----------------------------------------------------------------------------*/
223
224static void default_connection_handler(ddf_fun_t *, ipc_callid_t, ipc_call_t *);
225//ddf_dev_ops_t keyboard_ops = {
226// .default_handler = default_connection_handler
227//};
228
229/**
230 * Default handler for IPC methods not handled by DDF.
231 *
232 * Currently recognizes only one method (IPC_M_CONNECT_TO_ME), in which case it
233 * assumes the caller is the console and thus it stores IPC phone to it for
234 * later use by the driver to notify about key events.
235 *
236 * @param fun Device function handling the call.
237 * @param icallid Call id.
238 * @param icall Call data.
239 */
240void default_connection_handler(ddf_fun_t *fun,
241 ipc_callid_t icallid, ipc_call_t *icall)
242{
243 sysarg_t method = IPC_GET_IMETHOD(*icall);
244
245 usb_kbd_t *kbd_dev = (usb_kbd_t *)fun->driver_data;
246 assert(kbd_dev != NULL);
247
248 if (method == IPC_M_CONNECT_TO_ME) {
249 int callback = IPC_GET_ARG5(*icall);
250
251 if (kbd_dev->console_phone != -1) {
252 async_answer_0(icallid, ELIMIT);
253 return;
254 }
255
256 kbd_dev->console_phone = callback;
257 async_answer_0(icallid, EOK);
258 return;
259 }
260
261 async_answer_0(icallid, EINVAL);
262}
263
264/*----------------------------------------------------------------------------*/
265/* Key processing functions */
266/*----------------------------------------------------------------------------*/
267/**
268 * Handles turning of LED lights on and off.
269 *
270 * In case of USB keyboards, the LEDs are handled in the driver, not in the
271 * device. When there should be a change (lock key was pressed), the driver
272 * uses a Set_Report request sent to the device to set the state of the LEDs.
273 *
274 * This functions sets the LED lights according to current settings of modifiers
275 * kept in the keyboard device structure.
276 *
277 * @param kbd_dev Keyboard device structure.
278 */
279static void usb_kbd_set_led(usb_hid_dev_t *hid_dev, usb_kbd_t *kbd_dev)
280{
281 if (kbd_dev->output_size == 0) {
282 return;
283 }
284
285 unsigned i = 0;
286
287 /* Reset the LED data. */
288 memset(kbd_dev->led_data, 0, kbd_dev->led_output_size * sizeof(int32_t));
289
290 if ((kbd_dev->mods & KM_NUM_LOCK) && (i < kbd_dev->led_output_size)) {
291 kbd_dev->led_data[i++] = USB_HID_LED_NUM_LOCK;
292 }
293
294 if ((kbd_dev->mods & KM_CAPS_LOCK) && (i < kbd_dev->led_output_size)) {
295 kbd_dev->led_data[i++] = USB_HID_LED_CAPS_LOCK;
296 }
297
298 if ((kbd_dev->mods & KM_SCROLL_LOCK)
299 && (i < kbd_dev->led_output_size)) {
300 kbd_dev->led_data[i++] = USB_HID_LED_SCROLL_LOCK;
301 }
302
303 // TODO: COMPOSE and KANA
304
305 usb_log_debug("Creating output report.\n");
306
307 int rc = usb_hid_report_output_translate(hid_dev->parser,
308 kbd_dev->led_path,
309 USB_HID_PATH_COMPARE_END | USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY,
310 kbd_dev->output_buffer,
311 kbd_dev->output_size, kbd_dev->led_data, kbd_dev->led_output_size);
312
313 if (rc != EOK) {
314 usb_log_warning("Error translating LED output to output report"
315 ".\n");
316 return;
317 }
318
319 usb_log_debug("Output report buffer: %s\n",
320 usb_debug_str_buffer(kbd_dev->output_buffer, kbd_dev->output_size,
321 0));
322
323 usbhid_req_set_report(&hid_dev->usb_dev->ctrl_pipe,
324 hid_dev->usb_dev->interface_no, USB_HID_REPORT_TYPE_OUTPUT,
325 kbd_dev->output_buffer, kbd_dev->output_size);
326}
327
328/*----------------------------------------------------------------------------*/
329/**
330 * Processes key events.
331 *
332 * @note This function was copied from AT keyboard driver and modified to suit
333 * USB keyboard.
334 *
335 * @note Lock keys are not sent to the console, as they are completely handled
336 * in the driver. It may, however, be required later that the driver
337 * sends also these keys to application (otherwise it cannot use those
338 * keys at all).
339 *
340 * @param kbd_dev Keyboard device structure.
341 * @param type Type of the event (press / release). Recognized values:
342 * KEY_PRESS, KEY_RELEASE
343 * @param key Key code of the key according to HID Usage Tables.
344 */
345void usb_kbd_push_ev(usb_hid_dev_t *hid_dev, usb_kbd_t *kbd_dev, int type,
346 unsigned int key)
347{
348 console_event_t ev;
349 unsigned mod_mask;
350
351 /*
352 * These parts are copy-pasted from the AT keyboard driver.
353 *
354 * They definitely require some refactoring, but will keep it for later
355 * when the console and keyboard system is changed in HelenOS.
356 */
357 switch (key) {
358 case KC_LCTRL: mod_mask = KM_LCTRL; break;
359 case KC_RCTRL: mod_mask = KM_RCTRL; break;
360 case KC_LSHIFT: mod_mask = KM_LSHIFT; break;
361 case KC_RSHIFT: mod_mask = KM_RSHIFT; break;
362 case KC_LALT: mod_mask = KM_LALT; break;
363 case KC_RALT: mod_mask = KM_RALT; break;
364 default: mod_mask = 0; break;
365 }
366
367 if (mod_mask != 0) {
368 if (type == KEY_PRESS)
369 kbd_dev->mods = kbd_dev->mods | mod_mask;
370 else
371 kbd_dev->mods = kbd_dev->mods & ~mod_mask;
372 }
373
374 switch (key) {
375 case KC_CAPS_LOCK: mod_mask = KM_CAPS_LOCK; break;
376 case KC_NUM_LOCK: mod_mask = KM_NUM_LOCK; break;
377 case KC_SCROLL_LOCK: mod_mask = KM_SCROLL_LOCK; break;
378 default: mod_mask = 0; break;
379 }
380
381 if (mod_mask != 0) {
382 if (type == KEY_PRESS) {
383 /*
384 * Only change lock state on transition from released
385 * to pressed. This prevents autorepeat from messing
386 * up the lock state.
387 */
388 unsigned int locks_old = kbd_dev->lock_keys;
389
390 kbd_dev->mods =
391 kbd_dev->mods ^ (mod_mask & ~kbd_dev->lock_keys);
392 kbd_dev->lock_keys = kbd_dev->lock_keys | mod_mask;
393
394 /* Update keyboard lock indicator lights. */
395 if (kbd_dev->lock_keys != locks_old
396 && hid_dev != NULL) { // ugly hack
397 usb_kbd_set_led(hid_dev, kbd_dev);
398 }
399 } else {
400 kbd_dev->lock_keys = kbd_dev->lock_keys & ~mod_mask;
401 }
402 }
403
404 if (key == KC_CAPS_LOCK || key == KC_NUM_LOCK || key == KC_SCROLL_LOCK) {
405 // do not send anything to the console, this is our business
406 return;
407 }
408
409 if (type == KEY_PRESS && (kbd_dev->mods & KM_LCTRL) && key == KC_F1) {
410 active_layout = 0;
411 layout[active_layout]->reset();
412 return;
413 }
414
415 if (type == KEY_PRESS && (kbd_dev->mods & KM_LCTRL) && key == KC_F2) {
416 active_layout = 1;
417 layout[active_layout]->reset();
418 return;
419 }
420
421 if (type == KEY_PRESS && (kbd_dev->mods & KM_LCTRL) && key == KC_F3) {
422 active_layout = 2;
423 layout[active_layout]->reset();
424 return;
425 }
426
427 ev.type = type;
428 ev.key = key;
429 ev.mods = kbd_dev->mods;
430
431 ev.c = layout[active_layout]->parse_ev(&ev);
432
433 usb_log_debug2("Sending key %d to the console\n", ev.key);
434 if (kbd_dev->console_phone < 0) {
435 usb_log_warning(
436 "Connection to console not ready, key discarded.\n");
437 return;
438 }
439
440 async_msg_4(kbd_dev->console_phone, KBD_EVENT, ev.type, ev.key,
441 ev.mods, ev.c);
442}
443
444/*----------------------------------------------------------------------------*/
445
446static inline int usb_kbd_is_lock(unsigned int key_code)
447{
448 return (key_code == KC_NUM_LOCK
449 || key_code == KC_SCROLL_LOCK
450 || key_code == KC_CAPS_LOCK);
451}
452
453/*----------------------------------------------------------------------------*/
454/**
455 * Checks if some keys were pressed or released and generates key events.
456 *
457 * An event is created only when key is pressed or released. Besides handling
458 * the events (usb_kbd_push_ev()), the auto-repeat fibril is notified about
459 * key presses and releases (see usb_kbd_repeat_start() and
460 * usb_kbd_repeat_stop()).
461 *
462 * @param kbd_dev Keyboard device structure.
463 * @param key_codes Parsed keyboard report - codes of currently pressed keys
464 * according to HID Usage Tables.
465 * @param count Number of key codes in report (size of the report).
466 *
467 * @sa usb_kbd_push_ev(), usb_kbd_repeat_start(), usb_kbd_repeat_stop()
468 */
469static void usb_kbd_check_key_changes(usb_hid_dev_t *hid_dev,
470 usb_kbd_t *kbd_dev, const uint8_t *key_codes, size_t count)
471{
472 unsigned int key;
473 unsigned int i, j;
474
475 /*
476 * First of all, check if the kbd have reported phantom state.
477 *
478 * As there is no way to distinguish keys from modifiers, we do not have
479 * a way to check that 'all keys report Error Rollover'. We thus check
480 * if there is at least one such error and in such case we ignore the
481 * whole input report.
482 */
483 i = 0;
484 while (i < count && key_codes[i] != ERROR_ROLLOVER) {
485 ++i;
486 }
487 if (i != count) {
488 usb_log_debug("Phantom state occured.\n");
489 // phantom state, do nothing
490 return;
491 }
492
493 /* TODO: quite dummy right now, think of better implementation */
494 assert(count == kbd_dev->key_count);
495
496 /*
497 * 1) Key releases
498 */
499 for (j = 0; j < count; ++j) {
500 // try to find the old key in the new key list
501 i = 0;
502 while (i < kbd_dev->key_count
503 && key_codes[i] != kbd_dev->keys[j]) {
504 ++i;
505 }
506
507 if (i == count) {
508 // not found, i.e. the key was released
509 key = usbhid_parse_scancode(kbd_dev->keys[j]);
510 if (!usb_kbd_is_lock(key)) {
511 usb_kbd_repeat_stop(kbd_dev, key);
512 }
513 usb_kbd_push_ev(hid_dev, kbd_dev, KEY_RELEASE, key);
514 usb_log_debug2("Key released: %d\n", key);
515 } else {
516 // found, nothing happens
517 }
518 }
519
520 /*
521 * 1) Key presses
522 */
523 for (i = 0; i < kbd_dev->key_count; ++i) {
524 // try to find the new key in the old key list
525 j = 0;
526 while (j < count && kbd_dev->keys[j] != key_codes[i]) {
527 ++j;
528 }
529
530 if (j == count) {
531 // not found, i.e. new key pressed
532 key = usbhid_parse_scancode(key_codes[i]);
533 usb_log_debug2("Key pressed: %d (keycode: %d)\n", key,
534 key_codes[i]);
535 usb_kbd_push_ev(hid_dev, kbd_dev, KEY_PRESS,
536 key);
537 if (!usb_kbd_is_lock(key)) {
538 usb_kbd_repeat_start(kbd_dev, key);
539 }
540 } else {
541 // found, nothing happens
542 }
543 }
544
545 memcpy(kbd_dev->keys, key_codes, count);
546
547 usb_log_debug("New stored keycodes: %s\n",
548 usb_debug_str_buffer(kbd_dev->keys, kbd_dev->key_count, 0));
549}
550
551/*----------------------------------------------------------------------------*/
552/* Callbacks for parser */
553/*----------------------------------------------------------------------------*/
554/**
555 * Callback function for the HID report parser.
556 *
557 * This function is called by the HID report parser with the parsed report.
558 * The parsed report is used to check if any events occured (key was pressed or
559 * released, modifier was pressed or released).
560 *
561 * @param key_codes Parsed keyboard report - codes of currently pressed keys
562 * according to HID Usage Tables.
563 * @param count Number of key codes in report (size of the report).
564 * @param report_id
565 * @param arg User-specified argument. Expects pointer to the keyboard device
566 * structure representing the keyboard.
567 *
568 * @sa usb_kbd_check_key_changes(), usb_kbd_check_modifier_changes()
569 */
570static void usb_kbd_process_keycodes(const uint8_t *key_codes, size_t count,
571 uint8_t report_id, void *arg)
572{
573 if (arg == NULL) {
574 usb_log_warning("Missing argument in callback "
575 "usbhid_process_keycodes().\n");
576 return;
577 }
578
579 usb_hid_dev_t *hid_dev = (usb_hid_dev_t *)arg;
580
581 if (hid_dev->data == NULL) {
582 usb_log_warning("Missing KBD device structure in callback.\n");
583 return;
584 }
585
586 usb_kbd_t *kbd_dev = (usb_kbd_t *)hid_dev->data;
587
588 usb_log_debug("Got keys from parser (report id: %u): %s\n",
589 report_id, usb_debug_str_buffer(key_codes, count, 0));
590
591 if (count != kbd_dev->key_count) {
592 usb_log_warning("Number of received keycodes (%d) differs from"
593 " expected number (%d).\n", count, kbd_dev->key_count);
594 return;
595 }
596
597 ///usb_kbd_check_modifier_changes(kbd_dev, key_codes, count);
598 usb_kbd_check_key_changes(hid_dev, kbd_dev, key_codes, count);
599}
600
601/*----------------------------------------------------------------------------*/
602/* General kbd functions */
603/*----------------------------------------------------------------------------*/
604/**
605 * Processes data received from the device in form of report.
606 *
607 * This function uses the HID report parser to translate the data received from
608 * the device into generic USB HID key codes and into generic modifiers bitmap.
609 * The parser then calls the given callback (usb_kbd_process_keycodes()).
610 *
611 * @note Currently, only the boot protocol is supported.
612 *
613 * @param kbd_dev Keyboard device structure (must be initialized).
614 * @param buffer Data from the keyboard (i.e. the report).
615 * @param actual_size Size of the data from keyboard (report size) in bytes.
616 *
617 * @sa usb_kbd_process_keycodes(), usb_hid_boot_keyboard_input_report(),
618 * usb_hid_parse_report().
619 */
620static void usb_kbd_process_data(usb_hid_dev_t *hid_dev,
621 uint8_t *buffer, size_t actual_size)
622{
623 assert(hid_dev->parser != NULL);
624
625 usb_hid_report_in_callbacks_t *callbacks =
626 (usb_hid_report_in_callbacks_t *)malloc(
627 sizeof(usb_hid_report_in_callbacks_t));
628
629 callbacks->keyboard = usb_kbd_process_keycodes;
630
631 usb_log_debug("Calling usb_hid_parse_report() with "
632 "buffer %s\n", usb_debug_str_buffer(buffer, actual_size, 0));
633
634// int rc = usb_hid_boot_keyboard_input_report(buffer, actual_size,
635// callbacks, kbd_dev);
636 usb_hid_report_path_t *path = usb_hid_report_path();
637 usb_hid_report_path_append_item(path, USB_HIDUT_PAGE_KEYBOARD, 0);
638 usb_hid_report_path_set_report_id(path, 0);
639
640 int rc = usb_hid_parse_report(hid_dev->parser, buffer,
641 actual_size, path,
642 USB_HID_PATH_COMPARE_END | USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY,
643 callbacks, hid_dev);
644
645 usb_hid_report_path_free(path);
646
647 if (rc != EOK) {
648 usb_log_warning("Error in usb_hid_boot_keyboard_input_report():"
649 "%s\n", str_error(rc));
650 }
651}
652
653/*----------------------------------------------------------------------------*/
654/* HID/KBD structure manipulation */
655/*----------------------------------------------------------------------------*/
656
657static void usb_kbd_mark_unusable(usb_kbd_t *kbd_dev)
658{
659 kbd_dev->initialized = USB_KBD_STATUS_TO_DESTROY;
660}
661
662/*----------------------------------------------------------------------------*/
663
664/**
665 * Creates a new USB/HID keyboard structure.
666 *
667 * The structure returned by this function is not initialized. Use
668 * usb_kbd_init() to initialize it prior to polling.
669 *
670 * @return New uninitialized structure for representing a USB/HID keyboard or
671 * NULL if not successful (memory error).
672 */
673static usb_kbd_t *usb_kbd_new(void)
674{
675 usb_kbd_t *kbd_dev =
676 (usb_kbd_t *)calloc(1, sizeof(usb_kbd_t));
677
678 if (kbd_dev == NULL) {
679 usb_log_fatal("No memory!\n");
680 return NULL;
681 }
682
683 kbd_dev->console_phone = -1;
684 kbd_dev->initialized = USB_KBD_STATUS_UNINITIALIZED;
685
686 return kbd_dev;
687}
688
689/*----------------------------------------------------------------------------*/
690/* API functions */
691/*----------------------------------------------------------------------------*/
692/**
693 * Initialization of the USB/HID keyboard structure.
694 *
695 * This functions initializes required structures from the device's descriptors.
696 *
697 * During initialization, the keyboard is switched into boot protocol, the idle
698 * rate is set to 0 (infinity), resulting in the keyboard only reporting event
699 * when a key is pressed or released. Finally, the LED lights are turned on
700 * according to the default setup of lock keys.
701 *
702 * @note By default, the keyboards is initialized with Num Lock turned on and
703 * other locks turned off.
704 *
705 * @param kbd_dev Keyboard device structure to be initialized.
706 * @param dev DDF device structure of the keyboard.
707 *
708 * @retval EOK if successful.
709 * @retval EINVAL if some parameter is not given.
710 * @return Other value inherited from function usbhid_dev_init().
711 */
712int usb_kbd_init(usb_hid_dev_t *hid_dev)
713{
714 usb_log_debug("Initializing HID/KBD structure...\n");
715
716 if (hid_dev == NULL) {
717 usb_log_error("Failed to init keyboard structure: no structure"
718 " given.\n");
719 return EINVAL;
720 }
721
722 usb_kbd_t *kbd_dev = usb_kbd_new();
723 if (kbd_dev == NULL) {
724 usb_log_error("Error while creating USB/HID KBD device "
725 "structure.\n");
726 return ENOMEM; // TODO: some other code??
727 }
728
729 /*
730 * TODO: make more general
731 */
732 usb_hid_report_path_t *path = usb_hid_report_path();
733 usb_hid_report_path_append_item(path, USB_HIDUT_PAGE_KEYBOARD, 0);
734
735 usb_hid_report_path_set_report_id(path, 0);
736
737 kbd_dev->key_count = usb_hid_report_input_length(
738 hid_dev->parser, path,
739 USB_HID_PATH_COMPARE_END | USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY);
740 usb_hid_report_path_free(path);
741
742 usb_log_debug("Size of the input report: %zu\n", kbd_dev->key_count);
743
744 kbd_dev->keys = (uint8_t *)calloc(kbd_dev->key_count, sizeof(uint8_t));
745
746 if (kbd_dev->keys == NULL) {
747 usb_log_fatal("No memory!\n");
748 free(kbd_dev);
749 return ENOMEM;
750 }
751
752 /*
753 * Output report
754 */
755 kbd_dev->output_size = 0;
756 kbd_dev->output_buffer = usb_hid_report_output(hid_dev->parser,
757 &kbd_dev->output_size);
758 if (kbd_dev->output_buffer == NULL && kbd_dev->output_size != 0) {
759 usb_log_warning("Error creating output report buffer.\n");
760 free(kbd_dev->keys);
761 free(kbd_dev);
762 return ENOMEM;
763 }
764
765 usb_log_debug("Output buffer size: %zu\n", kbd_dev->output_size);
766
767 kbd_dev->led_path = usb_hid_report_path();
768 usb_hid_report_path_append_item(
769 kbd_dev->led_path, USB_HIDUT_PAGE_LED, 0);
770
771 kbd_dev->led_output_size = usb_hid_report_output_size(hid_dev->parser,
772 kbd_dev->led_path,
773 USB_HID_PATH_COMPARE_END | USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY);
774
775 usb_log_debug("Output report size (in items): %zu\n",
776 kbd_dev->led_output_size);
777
778 kbd_dev->led_data = (int32_t *)calloc(
779 kbd_dev->led_output_size, sizeof(int32_t));
780
781 if (kbd_dev->led_data == NULL) {
782 usb_log_warning("Error creating buffer for LED output report."
783 "\n");
784 free(kbd_dev->keys);
785 usb_hid_report_output_free(kbd_dev->output_buffer);
786 free(kbd_dev);
787 return ENOMEM;
788 }
789
790 /*
791 * Modifiers and locks
792 */
793 kbd_dev->modifiers = 0;
794 kbd_dev->mods = DEFAULT_ACTIVE_MODS;
795 kbd_dev->lock_keys = 0;
796
797 /*
798 * Autorepeat
799 */
800 kbd_dev->repeat.key_new = 0;
801 kbd_dev->repeat.key_repeated = 0;
802 kbd_dev->repeat.delay_before = DEFAULT_DELAY_BEFORE_FIRST_REPEAT;
803 kbd_dev->repeat.delay_between = DEFAULT_REPEAT_DELAY;
804
805 kbd_dev->repeat_mtx = (fibril_mutex_t *)(
806 malloc(sizeof(fibril_mutex_t)));
807 if (kbd_dev->repeat_mtx == NULL) {
808 usb_log_fatal("No memory!\n");
809 free(kbd_dev->keys);
810 usb_hid_report_output_free(kbd_dev->output_buffer);
811 free(kbd_dev);
812 return ENOMEM;
813 }
814
815 fibril_mutex_initialize(kbd_dev->repeat_mtx);
816
817 // save the KBD device structure into the HID device structure
818 hid_dev->data = kbd_dev;
819
820 // set handler for incoming calls
821 hid_dev->ops.default_handler = default_connection_handler;
822
823 /*
824 * Set LEDs according to initial setup.
825 * Set Idle rate
826 */
827 usb_kbd_set_led(hid_dev, kbd_dev);
828
829 usbhid_req_set_idle(&hid_dev->usb_dev->ctrl_pipe,
830 hid_dev->usb_dev->interface_no, IDLE_RATE);
831
832 /*
833 * Create new fibril for auto-repeat
834 */
835 fid_t fid = fibril_create(usb_kbd_repeat_fibril, kbd_dev);
836 if (fid == 0) {
837 usb_log_error("Failed to start fibril for KBD auto-repeat");
838 return ENOMEM;
839 }
840 fibril_add_ready(fid);
841
842 kbd_dev->initialized = USB_KBD_STATUS_INITIALIZED;
843 usb_log_debug("HID/KBD device structure initialized.\n");
844
845 return EOK;
846}
847
848/*----------------------------------------------------------------------------*/
849
850bool usb_kbd_polling_callback(usb_device_t *dev, uint8_t *buffer,
851 size_t buffer_size, void *arg)
852{
853 if (dev == NULL || buffer == NULL || arg == NULL) {
854 // do not continue polling (???)
855 return false;
856 }
857
858 usb_hid_dev_t *hid_dev = (usb_hid_dev_t *)arg;
859
860 // TODO: add return value from this function
861 usb_kbd_process_data(hid_dev, buffer, buffer_size);
862
863 return true;
864}
865
866/*----------------------------------------------------------------------------*/
867
868int usb_kbd_is_initialized(const usb_kbd_t *kbd_dev)
869{
870 return (kbd_dev->initialized == USB_KBD_STATUS_INITIALIZED);
871}
872
873/*----------------------------------------------------------------------------*/
874
875int usb_kbd_is_ready_to_destroy(const usb_kbd_t *kbd_dev)
876{
877 return (kbd_dev->initialized == USB_KBD_STATUS_TO_DESTROY);
878}
879
880/*----------------------------------------------------------------------------*/
881/**
882 * Properly destroys the USB/HID keyboard structure.
883 *
884 * @param kbd_dev Pointer to the structure to be destroyed.
885 */
886void usb_kbd_free(usb_kbd_t **kbd_dev)
887{
888 if (kbd_dev == NULL || *kbd_dev == NULL) {
889 return;
890 }
891
892 // hangup phone to the console
893 async_hangup((*kbd_dev)->console_phone);
894
895 if ((*kbd_dev)->repeat_mtx != NULL) {
896 /* TODO: replace by some check and wait */
897 assert(!fibril_mutex_is_locked((*kbd_dev)->repeat_mtx));
898 free((*kbd_dev)->repeat_mtx);
899 }
900
901 // free the output buffer
902 usb_hid_report_output_free((*kbd_dev)->output_buffer);
903
904 free(*kbd_dev);
905 *kbd_dev = NULL;
906}
907
908/*----------------------------------------------------------------------------*/
909
910void usb_kbd_deinit(struct usb_hid_dev_t *hid_dev)
911{
912 if (hid_dev == NULL) {
913 return;
914 }
915
916 if (hid_dev->data != NULL) {
917 usb_kbd_t *kbd_dev = (usb_kbd_t *)hid_dev->data;
918 if (usb_kbd_is_initialized(kbd_dev)) {
919 usb_kbd_mark_unusable(kbd_dev);
920 } else {
921 usb_kbd_free(&kbd_dev);
922 }
923 }
924}
925
926/*----------------------------------------------------------------------------*/
927
928int usb_kbd_set_boot_protocol(usb_hid_dev_t *hid_dev)
929{
930 int rc = usb_hid_parse_report_descriptor(hid_dev->parser,
931 USB_KBD_BOOT_REPORT_DESCRIPTOR,
932 USB_KBD_BOOT_REPORT_DESCRIPTOR_SIZE);
933
934 if (rc != EOK) {
935 usb_log_error("Failed to parse boot report descriptor: %s\n",
936 str_error(rc));
937 return rc;
938 }
939
940 rc = usbhid_req_set_protocol(&hid_dev->usb_dev->ctrl_pipe,
941 hid_dev->usb_dev->interface_no, USB_HID_PROTOCOL_BOOT);
942
943 if (rc != EOK) {
944 usb_log_warning("Failed to set boot protocol to the device: "
945 "%s\n", str_error(rc));
946 return rc;
947 }
948
949 return EOK;
950}
951
952/**
953 * @}
954 */
Note: See TracBrowser for help on using the repository browser.