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