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