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