| 1 | /*
|
|---|
| 2 | * Copyright (c) 2010 Vojtech Horky
|
|---|
| 3 | * Copyright (c) 2011 Lubos Slovak
|
|---|
| 4 | * All rights reserved.
|
|---|
| 5 | *
|
|---|
| 6 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 7 | * modification, are permitted provided that the following conditions
|
|---|
| 8 | * are met:
|
|---|
| 9 | *
|
|---|
| 10 | * - Redistributions of source code must retain the above copyright
|
|---|
| 11 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 12 | * - Redistributions in binary form must reproduce the above copyright
|
|---|
| 13 | * notice, this list of conditions and the following disclaimer in the
|
|---|
| 14 | * documentation and/or other materials provided with the distribution.
|
|---|
| 15 | * - The name of the author may not be used to endorse or promote products
|
|---|
| 16 | * derived from this software without specific prior written permission.
|
|---|
| 17 | *
|
|---|
| 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|---|
| 19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|---|
| 20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|---|
| 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|---|
| 23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|---|
| 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 28 | */
|
|---|
| 29 |
|
|---|
| 30 | /** @addtogroup drvusbhid
|
|---|
| 31 | * @{
|
|---|
| 32 | */
|
|---|
| 33 | /**
|
|---|
| 34 | * @file
|
|---|
| 35 | * Main routines of USB HID driver.
|
|---|
| 36 | */
|
|---|
| 37 |
|
|---|
| 38 | #include <ddf/driver.h>
|
|---|
| 39 | #include <ipc/driver.h>
|
|---|
| 40 | #include <ipc/kbd.h>
|
|---|
| 41 | #include <io/keycode.h>
|
|---|
| 42 | #include <io/console.h>
|
|---|
| 43 | #include <errno.h>
|
|---|
| 44 | #include <str_error.h>
|
|---|
| 45 | #include <fibril.h>
|
|---|
| 46 | #include <usb/debug.h>
|
|---|
| 47 | #include <usb/classes/classes.h>
|
|---|
| 48 | #include <usb/classes/hid.h>
|
|---|
| 49 | #include <usb/classes/hidparser.h>
|
|---|
| 50 | #include <usb/request.h>
|
|---|
| 51 | #include <usb/descriptor.h>
|
|---|
| 52 | #include <io/console.h>
|
|---|
| 53 | #include <stdint.h>
|
|---|
| 54 | #include "hid.h"
|
|---|
| 55 | #include "descparser.h"
|
|---|
| 56 | #include "descdump.h"
|
|---|
| 57 | #include "conv.h"
|
|---|
| 58 | #include "layout.h"
|
|---|
| 59 |
|
|---|
| 60 | #define BUFFER_SIZE 8
|
|---|
| 61 | #define NAME "usbhid"
|
|---|
| 62 |
|
|---|
| 63 | #define GUESSED_POLL_ENDPOINT 1
|
|---|
| 64 | #define BOOTP_REPORT_SIZE 6
|
|---|
| 65 |
|
|---|
| 66 | /** Keyboard polling endpoint description for boot protocol class. */
|
|---|
| 67 | static usb_endpoint_description_t poll_endpoint_description = {
|
|---|
| 68 | .transfer_type = USB_TRANSFER_INTERRUPT,
|
|---|
| 69 | .direction = USB_DIRECTION_IN,
|
|---|
| 70 | .interface_class = USB_CLASS_HID,
|
|---|
| 71 | .interface_subclass = USB_HID_SUBCLASS_BOOT,
|
|---|
| 72 | .interface_protocol = USB_HID_PROTOCOL_KEYBOARD,
|
|---|
| 73 | .flags = 0
|
|---|
| 74 | };
|
|---|
| 75 |
|
|---|
| 76 | static void default_connection_handler(ddf_fun_t *, ipc_callid_t, ipc_call_t *);
|
|---|
| 77 | static ddf_dev_ops_t keyboard_ops = {
|
|---|
| 78 | .default_handler = default_connection_handler
|
|---|
| 79 | };
|
|---|
| 80 |
|
|---|
| 81 | static int console_callback_phone = -1;
|
|---|
| 82 |
|
|---|
| 83 | /** Default handler for IPC methods not handled by DDF.
|
|---|
| 84 | *
|
|---|
| 85 | * @param dev Device handling the call.
|
|---|
| 86 | * @param icallid Call id.
|
|---|
| 87 | * @param icall Call data.
|
|---|
| 88 | */
|
|---|
| 89 | void default_connection_handler(ddf_fun_t *fun,
|
|---|
| 90 | ipc_callid_t icallid, ipc_call_t *icall)
|
|---|
| 91 | {
|
|---|
| 92 | sysarg_t method = IPC_GET_IMETHOD(*icall);
|
|---|
| 93 |
|
|---|
| 94 | if (method == IPC_M_CONNECT_TO_ME) {
|
|---|
| 95 | int callback = IPC_GET_ARG5(*icall);
|
|---|
| 96 |
|
|---|
| 97 | if (console_callback_phone != -1) {
|
|---|
| 98 | async_answer_0(icallid, ELIMIT);
|
|---|
| 99 | return;
|
|---|
| 100 | }
|
|---|
| 101 |
|
|---|
| 102 | console_callback_phone = callback;
|
|---|
| 103 | async_answer_0(icallid, EOK);
|
|---|
| 104 | return;
|
|---|
| 105 | }
|
|---|
| 106 |
|
|---|
| 107 | async_answer_0(icallid, EINVAL);
|
|---|
| 108 | }
|
|---|
| 109 |
|
|---|
| 110 | #if 0
|
|---|
| 111 | static void send_key(int key, int type, wchar_t c) {
|
|---|
| 112 | async_msg_4(console_callback_phone, KBD_EVENT, type, key,
|
|---|
| 113 | KM_NUM_LOCK, c);
|
|---|
| 114 | }
|
|---|
| 115 | #endif
|
|---|
| 116 |
|
|---|
| 117 | /*
|
|---|
| 118 | * TODO: Move somewhere else
|
|---|
| 119 | */
|
|---|
| 120 | /*
|
|---|
| 121 | #define BYTES_PER_LINE 12
|
|---|
| 122 |
|
|---|
| 123 | static void dump_buffer(const char *msg, const uint8_t *buffer, size_t length)
|
|---|
| 124 | {
|
|---|
| 125 | printf("%s\n", msg);
|
|---|
| 126 |
|
|---|
| 127 | size_t i;
|
|---|
| 128 | for (i = 0; i < length; i++) {
|
|---|
| 129 | printf(" 0x%02X", buffer[i]);
|
|---|
| 130 | if (((i > 0) && (((i+1) % BYTES_PER_LINE) == 0))
|
|---|
| 131 | || (i + 1 == length)) {
|
|---|
| 132 | printf("\n");
|
|---|
| 133 | }
|
|---|
| 134 | }
|
|---|
| 135 | }
|
|---|
| 136 | */
|
|---|
| 137 | /*
|
|---|
| 138 | * Copy-paste from srv/hid/kbd/generic/kbd.c
|
|---|
| 139 | */
|
|---|
| 140 |
|
|---|
| 141 | /** Currently active modifiers (locks is probably better word).
|
|---|
| 142 | *
|
|---|
| 143 | * TODO: put to device?
|
|---|
| 144 | */
|
|---|
| 145 | static unsigned mods = KM_NUM_LOCK;
|
|---|
| 146 |
|
|---|
| 147 | /** Currently pressed lock keys. We track these to tackle autorepeat.
|
|---|
| 148 | *
|
|---|
| 149 | * TODO: put to device?
|
|---|
| 150 | */
|
|---|
| 151 | static unsigned lock_keys;
|
|---|
| 152 |
|
|---|
| 153 | #define NUM_LAYOUTS 3
|
|---|
| 154 |
|
|---|
| 155 | static layout_op_t *layout[NUM_LAYOUTS] = {
|
|---|
| 156 | &us_qwerty_op,
|
|---|
| 157 | &us_dvorak_op,
|
|---|
| 158 | &cz_op
|
|---|
| 159 | };
|
|---|
| 160 |
|
|---|
| 161 | static int active_layout = 0;
|
|---|
| 162 |
|
|---|
| 163 | static void kbd_push_ev(int type, unsigned int key)
|
|---|
| 164 | {
|
|---|
| 165 | console_event_t ev;
|
|---|
| 166 | unsigned mod_mask;
|
|---|
| 167 |
|
|---|
| 168 | // TODO: replace by our own parsing?? or are the key codes identical??
|
|---|
| 169 | switch (key) {
|
|---|
| 170 | case KC_LCTRL: mod_mask = KM_LCTRL; break;
|
|---|
| 171 | case KC_RCTRL: mod_mask = KM_RCTRL; break;
|
|---|
| 172 | case KC_LSHIFT: mod_mask = KM_LSHIFT; break;
|
|---|
| 173 | case KC_RSHIFT: mod_mask = KM_RSHIFT; break;
|
|---|
| 174 | case KC_LALT: mod_mask = KM_LALT; break;
|
|---|
| 175 | case KC_RALT: mod_mask = KM_RALT; break;
|
|---|
| 176 | default: mod_mask = 0; break;
|
|---|
| 177 | }
|
|---|
| 178 |
|
|---|
| 179 | if (mod_mask != 0) {
|
|---|
| 180 | if (type == KEY_PRESS)
|
|---|
| 181 | mods = mods | mod_mask;
|
|---|
| 182 | else
|
|---|
| 183 | mods = mods & ~mod_mask;
|
|---|
| 184 | }
|
|---|
| 185 |
|
|---|
| 186 | switch (key) {
|
|---|
| 187 | case KC_CAPS_LOCK: mod_mask = KM_CAPS_LOCK; break;
|
|---|
| 188 | case KC_NUM_LOCK: mod_mask = KM_NUM_LOCK; break;
|
|---|
| 189 | case KC_SCROLL_LOCK: mod_mask = KM_SCROLL_LOCK; break;
|
|---|
| 190 | default: mod_mask = 0; break;
|
|---|
| 191 | }
|
|---|
| 192 |
|
|---|
| 193 | if (mod_mask != 0) {
|
|---|
| 194 | if (type == KEY_PRESS) {
|
|---|
| 195 | /*
|
|---|
| 196 | * Only change lock state on transition from released
|
|---|
| 197 | * to pressed. This prevents autorepeat from messing
|
|---|
| 198 | * up the lock state.
|
|---|
| 199 | */
|
|---|
| 200 | mods = mods ^ (mod_mask & ~lock_keys);
|
|---|
| 201 | lock_keys = lock_keys | mod_mask;
|
|---|
| 202 |
|
|---|
| 203 | /* Update keyboard lock indicator lights. */
|
|---|
| 204 | // TODO
|
|---|
| 205 | //kbd_ctl_set_ind(mods);
|
|---|
| 206 | } else {
|
|---|
| 207 | lock_keys = lock_keys & ~mod_mask;
|
|---|
| 208 | }
|
|---|
| 209 | }
|
|---|
| 210 | /*
|
|---|
| 211 | usb_log_debug2("type: %d\n", type);
|
|---|
| 212 | usb_log_debug2("mods: 0x%x\n", mods);
|
|---|
| 213 | usb_log_debug2("keycode: %u\n", key);
|
|---|
| 214 | */
|
|---|
| 215 |
|
|---|
| 216 | if (type == KEY_PRESS && (mods & KM_LCTRL) &&
|
|---|
| 217 | key == KC_F1) {
|
|---|
| 218 | active_layout = 0;
|
|---|
| 219 | layout[active_layout]->reset();
|
|---|
| 220 | return;
|
|---|
| 221 | }
|
|---|
| 222 |
|
|---|
| 223 | if (type == KEY_PRESS && (mods & KM_LCTRL) &&
|
|---|
| 224 | key == KC_F2) {
|
|---|
| 225 | active_layout = 1;
|
|---|
| 226 | layout[active_layout]->reset();
|
|---|
| 227 | return;
|
|---|
| 228 | }
|
|---|
| 229 |
|
|---|
| 230 | if (type == KEY_PRESS && (mods & KM_LCTRL) &&
|
|---|
| 231 | key == KC_F3) {
|
|---|
| 232 | active_layout = 2;
|
|---|
| 233 | layout[active_layout]->reset();
|
|---|
| 234 | return;
|
|---|
| 235 | }
|
|---|
| 236 |
|
|---|
| 237 | ev.type = type;
|
|---|
| 238 | ev.key = key;
|
|---|
| 239 | ev.mods = mods;
|
|---|
| 240 |
|
|---|
| 241 | ev.c = layout[active_layout]->parse_ev(&ev);
|
|---|
| 242 |
|
|---|
| 243 | usb_log_debug("Sending key %d to the console\n", ev.key);
|
|---|
| 244 | assert(console_callback_phone != -1);
|
|---|
| 245 | async_msg_4(console_callback_phone, KBD_EVENT, ev.type, ev.key,
|
|---|
| 246 | ev.mods, ev.c);
|
|---|
| 247 | }
|
|---|
| 248 | /*
|
|---|
| 249 | * End of copy-paste
|
|---|
| 250 | */
|
|---|
| 251 |
|
|---|
| 252 | /*
|
|---|
| 253 | * TODO:
|
|---|
| 254 | * 1) key press / key release - how does the keyboard notify about
|
|---|
| 255 | * release?
|
|---|
| 256 | * 2) layouts (use the already defined), not important now
|
|---|
| 257 | * 3)
|
|---|
| 258 | */
|
|---|
| 259 |
|
|---|
| 260 | static const keycode_t usb_hid_modifiers_keycodes[USB_HID_MOD_COUNT] = {
|
|---|
| 261 | KC_LCTRL, /* USB_HID_MOD_LCTRL */
|
|---|
| 262 | KC_LSHIFT, /* USB_HID_MOD_LSHIFT */
|
|---|
| 263 | KC_LALT, /* USB_HID_MOD_LALT */
|
|---|
| 264 | 0, /* USB_HID_MOD_LGUI */
|
|---|
| 265 | KC_RCTRL, /* USB_HID_MOD_RCTRL */
|
|---|
| 266 | KC_RSHIFT, /* USB_HID_MOD_RSHIFT */
|
|---|
| 267 | KC_RALT, /* USB_HID_MOD_RALT */
|
|---|
| 268 | 0, /* USB_HID_MOD_RGUI */
|
|---|
| 269 | };
|
|---|
| 270 |
|
|---|
| 271 | static void usbkbd_check_modifier_changes(usb_hid_dev_kbd_t *kbd_dev,
|
|---|
| 272 | uint8_t modifiers)
|
|---|
| 273 | {
|
|---|
| 274 | /*
|
|---|
| 275 | * TODO: why the USB keyboard has NUM_, SCROLL_ and CAPS_LOCK
|
|---|
| 276 | * both as modifiers and as keys with their own scancodes???
|
|---|
| 277 | *
|
|---|
| 278 | * modifiers should be sent as normal keys to usbkbd_parse_scancode()!!
|
|---|
| 279 | * so maybe it would be better if I received it from report parser in
|
|---|
| 280 | * that way
|
|---|
| 281 | */
|
|---|
| 282 |
|
|---|
| 283 | int i;
|
|---|
| 284 | for (i = 0; i < USB_HID_MOD_COUNT; ++i) {
|
|---|
| 285 | if ((modifiers & usb_hid_modifiers_consts[i]) &&
|
|---|
| 286 | !(kbd_dev->modifiers & usb_hid_modifiers_consts[i])) {
|
|---|
| 287 | // modifier pressed
|
|---|
| 288 | if (usb_hid_modifiers_keycodes[i] != 0) {
|
|---|
| 289 | kbd_push_ev(KEY_PRESS,
|
|---|
| 290 | usb_hid_modifiers_keycodes[i]);
|
|---|
| 291 | }
|
|---|
| 292 | } else if (!(modifiers & usb_hid_modifiers_consts[i]) &&
|
|---|
| 293 | (kbd_dev->modifiers & usb_hid_modifiers_consts[i])) {
|
|---|
| 294 | // modifier released
|
|---|
| 295 | if (usb_hid_modifiers_keycodes[i] != 0) {
|
|---|
| 296 | kbd_push_ev(KEY_RELEASE,
|
|---|
| 297 | usb_hid_modifiers_keycodes[i]);
|
|---|
| 298 | }
|
|---|
| 299 | } // no change
|
|---|
| 300 | }
|
|---|
| 301 | }
|
|---|
| 302 |
|
|---|
| 303 | static void usbkbd_check_key_changes(usb_hid_dev_kbd_t *kbd_dev,
|
|---|
| 304 | const uint8_t *key_codes)
|
|---|
| 305 | {
|
|---|
| 306 | // TODO: phantom state!!
|
|---|
| 307 |
|
|---|
| 308 | unsigned int key;
|
|---|
| 309 | unsigned int i, j;
|
|---|
| 310 |
|
|---|
| 311 | // TODO: quite dummy right now, think of better implementation
|
|---|
| 312 |
|
|---|
| 313 | // key releases
|
|---|
| 314 | for (j = 0; j < kbd_dev->keycode_count; ++j) {
|
|---|
| 315 | // try to find the old key in the new key list
|
|---|
| 316 | i = 0;
|
|---|
| 317 | while (i < kbd_dev->keycode_count
|
|---|
| 318 | && key_codes[i] != kbd_dev->keycodes[j]) {
|
|---|
| 319 | ++j;
|
|---|
| 320 | }
|
|---|
| 321 |
|
|---|
| 322 | if (j == kbd_dev->keycode_count) {
|
|---|
| 323 | // not found, i.e. the key was released
|
|---|
| 324 | key = usbkbd_parse_scancode(kbd_dev->keycodes[j]);
|
|---|
| 325 | kbd_push_ev(KEY_RELEASE, key);
|
|---|
| 326 | } else {
|
|---|
| 327 | // found, nothing happens
|
|---|
| 328 | }
|
|---|
| 329 | }
|
|---|
| 330 |
|
|---|
| 331 | // key presses
|
|---|
| 332 | for (i = 0; i < kbd_dev->keycode_count; ++i) {
|
|---|
| 333 | // try to find the new key in the old key list
|
|---|
| 334 | j = 0;
|
|---|
| 335 | while (j < kbd_dev->keycode_count
|
|---|
| 336 | && kbd_dev->keycodes[j] != key_codes[i]) {
|
|---|
| 337 | ++j;
|
|---|
| 338 | }
|
|---|
| 339 |
|
|---|
| 340 | assert(kbd_dev->keycode_count <= kbd_dev->keycode_count);
|
|---|
| 341 |
|
|---|
| 342 | if (j == kbd_dev->keycode_count) {
|
|---|
| 343 | // not found, i.e. new key pressed
|
|---|
| 344 | key = usbkbd_parse_scancode(key_codes[i]);
|
|---|
| 345 | kbd_push_ev(KEY_PRESS, key);
|
|---|
| 346 | } else {
|
|---|
| 347 | // found, nothing happens
|
|---|
| 348 | }
|
|---|
| 349 | }
|
|---|
| 350 | }
|
|---|
| 351 |
|
|---|
| 352 | /*
|
|---|
| 353 | * Callbacks for parser
|
|---|
| 354 | */
|
|---|
| 355 | static void usbkbd_process_keycodes(const uint8_t *key_codes, size_t count,
|
|---|
| 356 | uint8_t modifiers, void *arg)
|
|---|
| 357 | {
|
|---|
| 358 | if (arg == NULL) {
|
|---|
| 359 | usb_log_warning("Missing argument in callback "
|
|---|
| 360 | "usbkbd_process_keycodes().\n");
|
|---|
| 361 | return;
|
|---|
| 362 | }
|
|---|
| 363 |
|
|---|
| 364 | usb_log_debug2("Got keys from parser: ");
|
|---|
| 365 | unsigned i;
|
|---|
| 366 | for (i = 0; i < count; ++i) {
|
|---|
| 367 | usb_log_debug2("%d ", key_codes[i]);
|
|---|
| 368 | }
|
|---|
| 369 | usb_log_debug2("\n");
|
|---|
| 370 |
|
|---|
| 371 | usb_hid_dev_kbd_t *kbd_dev = (usb_hid_dev_kbd_t *)arg;
|
|---|
| 372 |
|
|---|
| 373 | if (count != kbd_dev->keycode_count) {
|
|---|
| 374 | usb_log_warning("Number of received keycodes (%d) differs from"
|
|---|
| 375 | " expected number (%d).\n", count, kbd_dev->keycode_count);
|
|---|
| 376 | return;
|
|---|
| 377 | }
|
|---|
| 378 |
|
|---|
| 379 | usbkbd_check_modifier_changes(kbd_dev, modifiers);
|
|---|
| 380 | usbkbd_check_key_changes(kbd_dev, key_codes);
|
|---|
| 381 | }
|
|---|
| 382 |
|
|---|
| 383 | /*
|
|---|
| 384 | * Kbd functions
|
|---|
| 385 | */
|
|---|
| 386 | static int usbkbd_get_report_descriptor(usb_hid_dev_kbd_t *kbd_dev)
|
|---|
| 387 | {
|
|---|
| 388 | // iterate over all configurations and interfaces
|
|---|
| 389 | // TODO: more configurations!!
|
|---|
| 390 | unsigned i;
|
|---|
| 391 | for (i = 0; i < kbd_dev->conf->config_descriptor.interface_count; ++i) {
|
|---|
| 392 | // TODO: endianness
|
|---|
| 393 | uint16_t length = kbd_dev->conf->interfaces[i].hid_desc.
|
|---|
| 394 | report_desc_info.length;
|
|---|
| 395 | size_t actual_size = 0;
|
|---|
| 396 |
|
|---|
| 397 | // allocate space for the report descriptor
|
|---|
| 398 | kbd_dev->conf->interfaces[i].report_desc =
|
|---|
| 399 | (uint8_t *)malloc(length);
|
|---|
| 400 |
|
|---|
| 401 | // get the descriptor from the device
|
|---|
| 402 | int rc = usb_request_get_descriptor(&kbd_dev->ctrl_pipe,
|
|---|
| 403 | USB_REQUEST_TYPE_CLASS, USB_DESCTYPE_HID_REPORT,
|
|---|
| 404 | i, 0,
|
|---|
| 405 | kbd_dev->conf->interfaces[i].report_desc, length,
|
|---|
| 406 | &actual_size);
|
|---|
| 407 |
|
|---|
| 408 | if (rc != EOK) {
|
|---|
| 409 | return rc;
|
|---|
| 410 | }
|
|---|
| 411 |
|
|---|
| 412 | assert(actual_size == length);
|
|---|
| 413 |
|
|---|
| 414 | //dump_hid_class_descriptor(0, USB_DESCTYPE_HID_REPORT,
|
|---|
| 415 | // kbd_dev->conf->interfaces[i].report_desc, length);
|
|---|
| 416 | }
|
|---|
| 417 |
|
|---|
| 418 | return EOK;
|
|---|
| 419 | }
|
|---|
| 420 |
|
|---|
| 421 | static int usbkbd_process_descriptors(usb_hid_dev_kbd_t *kbd_dev)
|
|---|
| 422 | {
|
|---|
| 423 | // get the first configuration descriptor (TODO: parse also other!)
|
|---|
| 424 | usb_standard_configuration_descriptor_t config_desc;
|
|---|
| 425 |
|
|---|
| 426 | int rc;
|
|---|
| 427 | rc = usb_request_get_bare_configuration_descriptor(&kbd_dev->ctrl_pipe,
|
|---|
| 428 | 0, &config_desc);
|
|---|
| 429 |
|
|---|
| 430 | if (rc != EOK) {
|
|---|
| 431 | return rc;
|
|---|
| 432 | }
|
|---|
| 433 |
|
|---|
| 434 | // prepare space for all underlying descriptors
|
|---|
| 435 | uint8_t *descriptors = (uint8_t *)malloc(config_desc.total_length);
|
|---|
| 436 | if (descriptors == NULL) {
|
|---|
| 437 | return ENOMEM;
|
|---|
| 438 | }
|
|---|
| 439 |
|
|---|
| 440 | size_t transferred = 0;
|
|---|
| 441 | // get full configuration descriptor
|
|---|
| 442 | rc = usb_request_get_full_configuration_descriptor(&kbd_dev->ctrl_pipe,
|
|---|
| 443 | 0, descriptors,
|
|---|
| 444 | config_desc.total_length, &transferred);
|
|---|
| 445 |
|
|---|
| 446 | if (rc != EOK) {
|
|---|
| 447 | return rc;
|
|---|
| 448 | }
|
|---|
| 449 | if (transferred != config_desc.total_length) {
|
|---|
| 450 | return ELIMIT;
|
|---|
| 451 | }
|
|---|
| 452 |
|
|---|
| 453 | /*
|
|---|
| 454 | * Initialize the interrupt in endpoint.
|
|---|
| 455 | */
|
|---|
| 456 | usb_endpoint_mapping_t endpoint_mapping[1] = {
|
|---|
| 457 | {
|
|---|
| 458 | .pipe = &kbd_dev->poll_pipe,
|
|---|
| 459 | .description = &poll_endpoint_description,
|
|---|
| 460 | .interface_no =
|
|---|
| 461 | usb_device_get_assigned_interface(kbd_dev->device)
|
|---|
| 462 | }
|
|---|
| 463 | };
|
|---|
| 464 | rc = usb_endpoint_pipe_initialize_from_configuration(
|
|---|
| 465 | endpoint_mapping, 1,
|
|---|
| 466 | descriptors, config_desc.total_length,
|
|---|
| 467 | &kbd_dev->wire);
|
|---|
| 468 | if (rc != EOK) {
|
|---|
| 469 | usb_log_error("Failed to initialize poll pipe: %s.\n",
|
|---|
| 470 | str_error(rc));
|
|---|
| 471 | return rc;
|
|---|
| 472 | }
|
|---|
| 473 | if (!endpoint_mapping[0].present) {
|
|---|
| 474 | usb_log_warning("Not accepting device, " \
|
|---|
| 475 | "not boot-protocol keyboard.\n");
|
|---|
| 476 | return EREFUSED;
|
|---|
| 477 | }
|
|---|
| 478 |
|
|---|
| 479 |
|
|---|
| 480 |
|
|---|
| 481 |
|
|---|
| 482 | kbd_dev->conf = (usb_hid_configuration_t *)calloc(1,
|
|---|
| 483 | sizeof(usb_hid_configuration_t));
|
|---|
| 484 | if (kbd_dev->conf == NULL) {
|
|---|
| 485 | free(descriptors);
|
|---|
| 486 | return ENOMEM;
|
|---|
| 487 | }
|
|---|
| 488 |
|
|---|
| 489 | /*rc = usbkbd_parse_descriptors(descriptors, transferred, kbd_dev->conf);
|
|---|
| 490 | free(descriptors);
|
|---|
| 491 | if (rc != EOK) {
|
|---|
| 492 | usb_log_warning("Problem with parsing standard descriptors.\n");
|
|---|
| 493 | return rc;
|
|---|
| 494 | }
|
|---|
| 495 |
|
|---|
| 496 | // get and report descriptors*/
|
|---|
| 497 | rc = usbkbd_get_report_descriptor(kbd_dev);
|
|---|
| 498 | if (rc != EOK) {
|
|---|
| 499 | usb_log_warning("Problem with parsing REPORT descriptor.\n");
|
|---|
| 500 | return rc;
|
|---|
| 501 | }
|
|---|
| 502 |
|
|---|
| 503 | //usbkbd_print_config(kbd_dev->conf);
|
|---|
| 504 |
|
|---|
| 505 | /*
|
|---|
| 506 | * TODO:
|
|---|
| 507 | * 1) select one configuration (lets say the first)
|
|---|
| 508 | * 2) how many interfaces?? how to select one??
|
|---|
| 509 | * ("The default setting for an interface is always alternate
|
|---|
| 510 | * setting zero.")
|
|---|
| 511 | * 3) find endpoint which is IN and INTERRUPT (parse), save its number
|
|---|
| 512 | * as the endpoint for polling
|
|---|
| 513 | */
|
|---|
| 514 |
|
|---|
| 515 | return EOK;
|
|---|
| 516 | }
|
|---|
| 517 |
|
|---|
| 518 | static usb_hid_dev_kbd_t *usbkbd_init_device(ddf_dev_t *dev)
|
|---|
| 519 | {
|
|---|
| 520 | int rc;
|
|---|
| 521 |
|
|---|
| 522 | usb_hid_dev_kbd_t *kbd_dev = (usb_hid_dev_kbd_t *)calloc(1,
|
|---|
| 523 | sizeof(usb_hid_dev_kbd_t));
|
|---|
| 524 |
|
|---|
| 525 | if (kbd_dev == NULL) {
|
|---|
| 526 | usb_log_fatal("No memory!\n");
|
|---|
| 527 | return NULL;
|
|---|
| 528 | }
|
|---|
| 529 |
|
|---|
| 530 | kbd_dev->device = dev;
|
|---|
| 531 |
|
|---|
| 532 | /*
|
|---|
| 533 | * Initialize the backing connection to the host controller.
|
|---|
| 534 | */
|
|---|
| 535 | rc = usb_device_connection_initialize_from_device(&kbd_dev->wire, dev);
|
|---|
| 536 | if (rc != EOK) {
|
|---|
| 537 | printf("Problem initializing connection to device: %s.\n",
|
|---|
| 538 | str_error(rc));
|
|---|
| 539 | goto error_leave;
|
|---|
| 540 | }
|
|---|
| 541 |
|
|---|
| 542 | /*
|
|---|
| 543 | * Initialize device pipes.
|
|---|
| 544 | */
|
|---|
| 545 | rc = usb_endpoint_pipe_initialize_default_control(&kbd_dev->ctrl_pipe,
|
|---|
| 546 | &kbd_dev->wire);
|
|---|
| 547 | if (rc != EOK) {
|
|---|
| 548 | printf("Failed to initialize default control pipe: %s.\n",
|
|---|
| 549 | str_error(rc));
|
|---|
| 550 | goto error_leave;
|
|---|
| 551 | }
|
|---|
| 552 |
|
|---|
| 553 | /*
|
|---|
| 554 | * will need all descriptors:
|
|---|
| 555 | * 1) choose one configuration from configuration descriptors
|
|---|
| 556 | * (set it to the device)
|
|---|
| 557 | * 2) set endpoints from endpoint descriptors
|
|---|
| 558 | */
|
|---|
| 559 |
|
|---|
| 560 | // TODO: get descriptors, parse descriptors and save endpoints
|
|---|
| 561 | usb_endpoint_pipe_start_session(&kbd_dev->ctrl_pipe);
|
|---|
| 562 | rc = usbkbd_process_descriptors(kbd_dev);
|
|---|
| 563 | usb_endpoint_pipe_end_session(&kbd_dev->ctrl_pipe);
|
|---|
| 564 | if (rc != EOK) {
|
|---|
| 565 | goto error_leave;
|
|---|
| 566 | }
|
|---|
| 567 |
|
|---|
| 568 | // save the size of the report
|
|---|
| 569 | kbd_dev->keycode_count = BOOTP_REPORT_SIZE;
|
|---|
| 570 | kbd_dev->keycodes = (uint8_t *)calloc(
|
|---|
| 571 | kbd_dev->keycode_count, sizeof(uint8_t));
|
|---|
| 572 |
|
|---|
| 573 | if (kbd_dev->keycodes == NULL) {
|
|---|
| 574 | usb_log_fatal("No memory!\n");
|
|---|
| 575 | goto error_leave;
|
|---|
| 576 | }
|
|---|
| 577 |
|
|---|
| 578 | // set configuration to the first one
|
|---|
| 579 | // TODO: handle case with no configurations
|
|---|
| 580 | usb_endpoint_pipe_start_session(&kbd_dev->ctrl_pipe);
|
|---|
| 581 | usb_request_set_configuration(&kbd_dev->ctrl_pipe,
|
|---|
| 582 | kbd_dev->conf->config_descriptor.configuration_number);
|
|---|
| 583 | usb_endpoint_pipe_end_session(&kbd_dev->ctrl_pipe);
|
|---|
| 584 |
|
|---|
| 585 | return kbd_dev;
|
|---|
| 586 |
|
|---|
| 587 | error_leave:
|
|---|
| 588 | free(kbd_dev);
|
|---|
| 589 | return NULL;
|
|---|
| 590 | }
|
|---|
| 591 |
|
|---|
| 592 | static void usbkbd_process_interrupt_in(usb_hid_dev_kbd_t *kbd_dev,
|
|---|
| 593 | uint8_t *buffer, size_t actual_size)
|
|---|
| 594 | {
|
|---|
| 595 | usb_hid_report_in_callbacks_t *callbacks =
|
|---|
| 596 | (usb_hid_report_in_callbacks_t *)malloc(
|
|---|
| 597 | sizeof(usb_hid_report_in_callbacks_t));
|
|---|
| 598 | callbacks->keyboard = usbkbd_process_keycodes;
|
|---|
| 599 |
|
|---|
| 600 | //usb_hid_parse_report(kbd_dev->parser, buffer, actual_size, callbacks,
|
|---|
| 601 | // NULL);
|
|---|
| 602 | /*usb_log_debug2("Calling usb_hid_boot_keyboard_input_report() with size"
|
|---|
| 603 | " %zu\n", actual_size);*/
|
|---|
| 604 | //dump_buffer("bufffer: ", buffer, actual_size);
|
|---|
| 605 | int rc = usb_hid_boot_keyboard_input_report(buffer, actual_size,
|
|---|
| 606 | callbacks, kbd_dev);
|
|---|
| 607 |
|
|---|
| 608 | if (rc != EOK) {
|
|---|
| 609 | usb_log_warning("Error in usb_hid_boot_keyboard_input_report():"
|
|---|
| 610 | "%s\n", str_error(rc));
|
|---|
| 611 | }
|
|---|
| 612 | }
|
|---|
| 613 |
|
|---|
| 614 | static void usbkbd_poll_keyboard(usb_hid_dev_kbd_t *kbd_dev)
|
|---|
| 615 | {
|
|---|
| 616 | int rc, sess_rc;
|
|---|
| 617 | uint8_t buffer[BUFFER_SIZE];
|
|---|
| 618 | size_t actual_size;
|
|---|
| 619 |
|
|---|
| 620 | usb_log_info("Polling keyboard...\n");
|
|---|
| 621 |
|
|---|
| 622 | while (true) {
|
|---|
| 623 | async_usleep(1000 * 10);
|
|---|
| 624 |
|
|---|
| 625 | sess_rc = usb_endpoint_pipe_start_session(&kbd_dev->poll_pipe);
|
|---|
| 626 | if (sess_rc != EOK) {
|
|---|
| 627 | usb_log_warning("Failed to start a session: %s.\n",
|
|---|
| 628 | str_error(sess_rc));
|
|---|
| 629 | continue;
|
|---|
| 630 | }
|
|---|
| 631 |
|
|---|
| 632 | rc = usb_endpoint_pipe_read(&kbd_dev->poll_pipe, buffer,
|
|---|
| 633 | BUFFER_SIZE, &actual_size);
|
|---|
| 634 | sess_rc = usb_endpoint_pipe_end_session(&kbd_dev->poll_pipe);
|
|---|
| 635 |
|
|---|
| 636 | if (rc != EOK) {
|
|---|
| 637 | usb_log_warning("Error polling the keyboard: %s.\n",
|
|---|
| 638 | str_error(rc));
|
|---|
| 639 | continue;
|
|---|
| 640 | }
|
|---|
| 641 |
|
|---|
| 642 | if (sess_rc != EOK) {
|
|---|
| 643 | usb_log_warning("Error closing session: %s.\n",
|
|---|
| 644 | str_error(sess_rc));
|
|---|
| 645 | continue;
|
|---|
| 646 | }
|
|---|
| 647 |
|
|---|
| 648 | /*
|
|---|
| 649 | * If the keyboard answered with NAK, it returned no data.
|
|---|
| 650 | * This implies that no change happened since last query.
|
|---|
| 651 | */
|
|---|
| 652 | if (actual_size == 0) {
|
|---|
| 653 | usb_log_debug("Keyboard returned NAK\n");
|
|---|
| 654 | continue;
|
|---|
| 655 | }
|
|---|
| 656 |
|
|---|
| 657 | /*
|
|---|
| 658 | * TODO: Process pressed keys.
|
|---|
| 659 | */
|
|---|
| 660 | usb_log_debug("Calling usbkbd_process_interrupt_in()\n");
|
|---|
| 661 | usbkbd_process_interrupt_in(kbd_dev, buffer, actual_size);
|
|---|
| 662 | }
|
|---|
| 663 |
|
|---|
| 664 | // not reached
|
|---|
| 665 | assert(0);
|
|---|
| 666 | }
|
|---|
| 667 |
|
|---|
| 668 | static int usbkbd_fibril_device(void *arg)
|
|---|
| 669 | {
|
|---|
| 670 | if (arg == NULL) {
|
|---|
| 671 | usb_log_error("No device!\n");
|
|---|
| 672 | return -1;
|
|---|
| 673 | }
|
|---|
| 674 |
|
|---|
| 675 | ddf_dev_t *dev = (ddf_dev_t *)arg;
|
|---|
| 676 |
|
|---|
| 677 | // initialize device (get and process descriptors, get address, etc.)
|
|---|
| 678 | usb_hid_dev_kbd_t *kbd_dev = usbkbd_init_device(dev);
|
|---|
| 679 | if (kbd_dev == NULL) {
|
|---|
| 680 | usb_log_error("Error while initializing device.\n");
|
|---|
| 681 | return -1;
|
|---|
| 682 | }
|
|---|
| 683 |
|
|---|
| 684 | usbkbd_poll_keyboard(kbd_dev);
|
|---|
| 685 |
|
|---|
| 686 | return EOK;
|
|---|
| 687 | }
|
|---|
| 688 |
|
|---|
| 689 | static int usbkbd_add_device(ddf_dev_t *dev)
|
|---|
| 690 | {
|
|---|
| 691 | /* For now, fail immediately. */
|
|---|
| 692 | //return ENOTSUP;
|
|---|
| 693 |
|
|---|
| 694 | /*
|
|---|
| 695 | * When everything is okay, connect to "our" HC.
|
|---|
| 696 | *
|
|---|
| 697 | * Not supported yet, skip..
|
|---|
| 698 | */
|
|---|
| 699 | // int phone = usb_drv_hc_connect_auto(dev, 0);
|
|---|
| 700 | // if (phone < 0) {
|
|---|
| 701 | // /*
|
|---|
| 702 | // * Connecting to HC failed, roll-back and announce
|
|---|
| 703 | // * failure.
|
|---|
| 704 | // */
|
|---|
| 705 | // return phone;
|
|---|
| 706 | // }
|
|---|
| 707 |
|
|---|
| 708 | // dev->parent_phone = phone;
|
|---|
| 709 |
|
|---|
| 710 | /*
|
|---|
| 711 | * Create default function.
|
|---|
| 712 | */
|
|---|
| 713 | // FIXME - check for errors
|
|---|
| 714 | ddf_fun_t *kbd_fun = ddf_fun_create(dev, fun_exposed, "keyboard");
|
|---|
| 715 | assert(kbd_fun != NULL);
|
|---|
| 716 | kbd_fun->ops = &keyboard_ops;
|
|---|
| 717 |
|
|---|
| 718 | int rc = ddf_fun_bind(kbd_fun);
|
|---|
| 719 | assert(rc == EOK);
|
|---|
| 720 | rc = ddf_fun_add_to_class(kbd_fun, "keyboard");
|
|---|
| 721 | assert(rc == EOK);
|
|---|
| 722 |
|
|---|
| 723 | /*
|
|---|
| 724 | * Create new fibril for handling this keyboard
|
|---|
| 725 | */
|
|---|
| 726 | fid_t fid = fibril_create(usbkbd_fibril_device, dev);
|
|---|
| 727 | if (fid == 0) {
|
|---|
| 728 | usb_log_error("Failed to start fibril for HID device\n");
|
|---|
| 729 | return ENOMEM;
|
|---|
| 730 | }
|
|---|
| 731 | fibril_add_ready(fid);
|
|---|
| 732 |
|
|---|
| 733 | //dev->ops = &keyboard_ops;
|
|---|
| 734 | (void)keyboard_ops;
|
|---|
| 735 |
|
|---|
| 736 | //add_device_to_class(dev, "keyboard");
|
|---|
| 737 |
|
|---|
| 738 | /*
|
|---|
| 739 | * Hurrah, device is initialized.
|
|---|
| 740 | */
|
|---|
| 741 | return EOK;
|
|---|
| 742 | }
|
|---|
| 743 |
|
|---|
| 744 | static driver_ops_t kbd_driver_ops = {
|
|---|
| 745 | .add_device = usbkbd_add_device,
|
|---|
| 746 | };
|
|---|
| 747 |
|
|---|
| 748 | static driver_t kbd_driver = {
|
|---|
| 749 | .name = NAME,
|
|---|
| 750 | .driver_ops = &kbd_driver_ops
|
|---|
| 751 | };
|
|---|
| 752 |
|
|---|
| 753 | int main(int argc, char *argv[])
|
|---|
| 754 | {
|
|---|
| 755 | usb_log_enable(USB_LOG_LEVEL_MAX, NAME);
|
|---|
| 756 | return ddf_driver_main(&kbd_driver);
|
|---|
| 757 | }
|
|---|
| 758 |
|
|---|
| 759 | /**
|
|---|
| 760 | * @}
|
|---|
| 761 | */
|
|---|