[c7137738] | 1 | /*
|
---|
| 2 | * Copyright (c) 2010 Vojtech Horky
|
---|
[1c13dac] | 3 | * Copyright (c) 2011 Lubos Slovak
|
---|
[c7137738] | 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 | */
|
---|
[1c13dac] | 29 |
|
---|
[ba54451] | 30 | /** @addtogroup drvusbhid
|
---|
| 31 | * @{
|
---|
| 32 | */
|
---|
[1c13dac] | 33 | /**
|
---|
| 34 | * @file
|
---|
| 35 | * Main routines of USB HID driver.
|
---|
| 36 | */
|
---|
| 37 |
|
---|
[c7137738] | 38 | #include <usb/usbdrv.h>
|
---|
| 39 | #include <driver.h>
|
---|
[2e15ac40] | 40 | #include <ipc/driver.h>
|
---|
[700af62] | 41 | #include <ipc/kbd.h>
|
---|
| 42 | #include <io/keycode.h>
|
---|
| 43 | #include <io/console.h>
|
---|
[c7137738] | 44 | #include <errno.h>
|
---|
[707ffcf] | 45 | #include <str_error.h>
|
---|
[2e15ac40] | 46 | #include <fibril.h>
|
---|
| 47 | #include <usb/classes/hid.h>
|
---|
[243cb86] | 48 | #include <usb/classes/hidparser.h>
|
---|
[03197ffc] | 49 | #include <usb/request.h>
|
---|
[dafab9e0] | 50 | #include <usb/descriptor.h>
|
---|
[6336b6e] | 51 | #include <io/console.h>
|
---|
[54b5625] | 52 | #include "hid.h"
|
---|
[ca038b4] | 53 | #include "descparser.h"
|
---|
[45019865] | 54 | #include "descdump.h"
|
---|
[6336b6e] | 55 | #include "conv.h"
|
---|
[7c169ce] | 56 | #include "layout.h"
|
---|
[c7137738] | 57 |
|
---|
[2899980] | 58 | #define BUFFER_SIZE 8
|
---|
[66d5062] | 59 | #define NAME "usbhid"
|
---|
[2e15ac40] | 60 |
|
---|
[1b29d6fa] | 61 | #define GUESSED_POLL_ENDPOINT 1
|
---|
[2e15ac40] | 62 |
|
---|
[700af62] | 63 | static void default_connection_handler(device_t *, ipc_callid_t, ipc_call_t *);
|
---|
| 64 | static device_ops_t keyboard_ops = {
|
---|
| 65 | .default_handler = default_connection_handler
|
---|
| 66 | };
|
---|
| 67 |
|
---|
| 68 | static int console_callback_phone = -1;
|
---|
| 69 |
|
---|
| 70 | /** Default handler for IPC methods not handled by DDF.
|
---|
| 71 | *
|
---|
| 72 | * @param dev Device handling the call.
|
---|
| 73 | * @param icallid Call id.
|
---|
| 74 | * @param icall Call data.
|
---|
| 75 | */
|
---|
| 76 | void default_connection_handler(device_t *dev,
|
---|
| 77 | ipc_callid_t icallid, ipc_call_t *icall)
|
---|
| 78 | {
|
---|
| 79 | sysarg_t method = IPC_GET_IMETHOD(*icall);
|
---|
| 80 |
|
---|
| 81 | if (method == IPC_M_CONNECT_TO_ME) {
|
---|
| 82 | int callback = IPC_GET_ARG5(*icall);
|
---|
| 83 |
|
---|
| 84 | if (console_callback_phone != -1) {
|
---|
[17aca1c] | 85 | async_answer_0(icallid, ELIMIT);
|
---|
[700af62] | 86 | return;
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | console_callback_phone = callback;
|
---|
[17aca1c] | 90 | async_answer_0(icallid, EOK);
|
---|
[700af62] | 91 | return;
|
---|
| 92 | }
|
---|
| 93 |
|
---|
[17aca1c] | 94 | async_answer_0(icallid, EINVAL);
|
---|
[700af62] | 95 | }
|
---|
| 96 |
|
---|
[38c5dfa] | 97 | #if 0
|
---|
[700af62] | 98 | static void send_key(int key, int type, wchar_t c) {
|
---|
| 99 | async_msg_4(console_callback_phone, KBD_EVENT, type, key,
|
---|
| 100 | KM_NUM_LOCK, c);
|
---|
| 101 | }
|
---|
[38c5dfa] | 102 | #endif
|
---|
[700af62] | 103 |
|
---|
[6336b6e] | 104 | /*
|
---|
| 105 | * TODO: Move somewhere else
|
---|
| 106 | */
|
---|
| 107 | /*
|
---|
| 108 | #define BYTES_PER_LINE 12
|
---|
| 109 |
|
---|
| 110 | static void dump_buffer(const char *msg, const uint8_t *buffer, size_t length)
|
---|
| 111 | {
|
---|
| 112 | printf("%s\n", msg);
|
---|
| 113 |
|
---|
| 114 | size_t i;
|
---|
| 115 | for (i = 0; i < length; i++) {
|
---|
| 116 | printf(" 0x%02X", buffer[i]);
|
---|
| 117 | if (((i > 0) && (((i+1) % BYTES_PER_LINE) == 0))
|
---|
| 118 | || (i + 1 == length)) {
|
---|
| 119 | printf("\n");
|
---|
| 120 | }
|
---|
| 121 | }
|
---|
| 122 | }
|
---|
| 123 | */
|
---|
| 124 | /*
|
---|
| 125 | * Copy-paste from srv/hid/kbd/generic/kbd.c
|
---|
| 126 | */
|
---|
| 127 |
|
---|
| 128 | /** Currently active modifiers.
|
---|
| 129 | *
|
---|
| 130 | * TODO: put to device?
|
---|
| 131 | */
|
---|
| 132 | static unsigned mods = KM_NUM_LOCK;
|
---|
| 133 |
|
---|
| 134 | /** Currently pressed lock keys. We track these to tackle autorepeat.
|
---|
| 135 | *
|
---|
| 136 | * TODO: put to device?
|
---|
| 137 | */
|
---|
| 138 | static unsigned lock_keys;
|
---|
| 139 |
|
---|
[7c169ce] | 140 | #define NUM_LAYOUTS 3
|
---|
| 141 |
|
---|
| 142 | static layout_op_t *layout[NUM_LAYOUTS] = {
|
---|
| 143 | &us_qwerty_op,
|
---|
| 144 | &us_dvorak_op,
|
---|
| 145 | &cz_op
|
---|
| 146 | };
|
---|
| 147 |
|
---|
| 148 | static int active_layout = 0;
|
---|
[6336b6e] | 149 |
|
---|
| 150 | static void kbd_push_ev(int type, unsigned int key)
|
---|
| 151 | {
|
---|
| 152 | console_event_t ev;
|
---|
| 153 | unsigned mod_mask;
|
---|
| 154 |
|
---|
| 155 | // TODO: replace by our own parsing?? or are the key codes identical??
|
---|
| 156 | switch (key) {
|
---|
| 157 | case KC_LCTRL: mod_mask = KM_LCTRL; break;
|
---|
| 158 | case KC_RCTRL: mod_mask = KM_RCTRL; break;
|
---|
| 159 | case KC_LSHIFT: mod_mask = KM_LSHIFT; break;
|
---|
| 160 | case KC_RSHIFT: mod_mask = KM_RSHIFT; break;
|
---|
| 161 | case KC_LALT: mod_mask = KM_LALT; break;
|
---|
| 162 | case KC_RALT: mod_mask = KM_RALT; break;
|
---|
| 163 | default: mod_mask = 0; break;
|
---|
| 164 | }
|
---|
| 165 |
|
---|
| 166 | if (mod_mask != 0) {
|
---|
| 167 | if (type == KEY_PRESS)
|
---|
| 168 | mods = mods | mod_mask;
|
---|
| 169 | else
|
---|
| 170 | mods = mods & ~mod_mask;
|
---|
| 171 | }
|
---|
| 172 |
|
---|
| 173 | switch (key) {
|
---|
| 174 | case KC_CAPS_LOCK: mod_mask = KM_CAPS_LOCK; break;
|
---|
| 175 | case KC_NUM_LOCK: mod_mask = KM_NUM_LOCK; break;
|
---|
| 176 | case KC_SCROLL_LOCK: mod_mask = KM_SCROLL_LOCK; break;
|
---|
| 177 | default: mod_mask = 0; break;
|
---|
| 178 | }
|
---|
| 179 |
|
---|
| 180 | if (mod_mask != 0) {
|
---|
| 181 | if (type == KEY_PRESS) {
|
---|
| 182 | /*
|
---|
| 183 | * Only change lock state on transition from released
|
---|
| 184 | * to pressed. This prevents autorepeat from messing
|
---|
| 185 | * up the lock state.
|
---|
| 186 | */
|
---|
| 187 | mods = mods ^ (mod_mask & ~lock_keys);
|
---|
| 188 | lock_keys = lock_keys | mod_mask;
|
---|
| 189 |
|
---|
| 190 | /* Update keyboard lock indicator lights. */
|
---|
| 191 | // TODO
|
---|
| 192 | //kbd_ctl_set_ind(mods);
|
---|
| 193 | } else {
|
---|
| 194 | lock_keys = lock_keys & ~mod_mask;
|
---|
| 195 | }
|
---|
| 196 | }
|
---|
| 197 | /*
|
---|
| 198 | printf("type: %d\n", type);
|
---|
| 199 | printf("mods: 0x%x\n", mods);
|
---|
| 200 | printf("keycode: %u\n", key);
|
---|
| 201 | */
|
---|
[11797d5] | 202 |
|
---|
[6336b6e] | 203 | if (type == KEY_PRESS && (mods & KM_LCTRL) &&
|
---|
| 204 | key == KC_F1) {
|
---|
| 205 | active_layout = 0;
|
---|
| 206 | layout[active_layout]->reset();
|
---|
| 207 | return;
|
---|
| 208 | }
|
---|
| 209 |
|
---|
| 210 | if (type == KEY_PRESS && (mods & KM_LCTRL) &&
|
---|
| 211 | key == KC_F2) {
|
---|
| 212 | active_layout = 1;
|
---|
| 213 | layout[active_layout]->reset();
|
---|
| 214 | return;
|
---|
| 215 | }
|
---|
| 216 |
|
---|
| 217 | if (type == KEY_PRESS && (mods & KM_LCTRL) &&
|
---|
| 218 | key == KC_F3) {
|
---|
| 219 | active_layout = 2;
|
---|
| 220 | layout[active_layout]->reset();
|
---|
| 221 | return;
|
---|
| 222 | }
|
---|
[11797d5] | 223 |
|
---|
[6336b6e] | 224 | ev.type = type;
|
---|
| 225 | ev.key = key;
|
---|
| 226 | ev.mods = mods;
|
---|
| 227 |
|
---|
[7c169ce] | 228 | ev.c = layout[active_layout]->parse_ev(&ev);
|
---|
[6336b6e] | 229 |
|
---|
| 230 | printf("Sending key %d to the console\n", ev.key);
|
---|
| 231 | assert(console_callback_phone != -1);
|
---|
[7c169ce] | 232 | async_msg_4(console_callback_phone, KBD_EVENT, ev.type, ev.key, ev.mods, ev.c);
|
---|
[6336b6e] | 233 | }
|
---|
| 234 | /*
|
---|
| 235 | * End of copy-paste
|
---|
| 236 | */
|
---|
| 237 |
|
---|
| 238 | /*
|
---|
| 239 | * TODO:
|
---|
| 240 | * 1) key press / key release - how does the keyboard notify about release?
|
---|
| 241 | * 2) layouts (use the already defined), not important now
|
---|
| 242 | * 3)
|
---|
| 243 | */
|
---|
| 244 |
|
---|
[243cb86] | 245 | /*
|
---|
| 246 | * Callbacks for parser
|
---|
| 247 | */
|
---|
[0a9ea4a] | 248 | static void usbkbd_process_keycodes(const uint8_t *key_codes, size_t count,
|
---|
[11797d5] | 249 | uint8_t modifiers, void *arg)
|
---|
[91db50ac] | 250 | {
|
---|
[692f13e4] | 251 | printf("Got keys: ");
|
---|
| 252 | unsigned i;
|
---|
| 253 | for (i = 0; i < count; ++i) {
|
---|
| 254 | printf("%d ", key_codes[i]);
|
---|
[6336b6e] | 255 | // TODO: Key press / release
|
---|
| 256 |
|
---|
| 257 | // TODO: NOT WORKING
|
---|
| 258 | unsigned int key = usbkbd_parse_scancode(key_codes[i]);
|
---|
| 259 | kbd_push_ev(KEY_PRESS, key);
|
---|
[692f13e4] | 260 | }
|
---|
| 261 | printf("\n");
|
---|
[243cb86] | 262 | }
|
---|
[0e126be7] | 263 |
|
---|
[243cb86] | 264 | /*
|
---|
| 265 | * Kbd functions
|
---|
| 266 | */
|
---|
[6986418] | 267 | static int usbkbd_get_report_descriptor(usb_hid_dev_kbd_t *kbd_dev)
|
---|
| 268 | {
|
---|
| 269 | // iterate over all configurations and interfaces
|
---|
| 270 | // TODO: more configurations!!
|
---|
| 271 | unsigned i;
|
---|
| 272 | for (i = 0; i < kbd_dev->conf->config_descriptor.interface_count; ++i) {
|
---|
| 273 | // TODO: endianness
|
---|
| 274 | uint16_t length =
|
---|
| 275 | kbd_dev->conf->interfaces[i].hid_desc.report_desc_info.length;
|
---|
[45019865] | 276 | size_t actual_size = 0;
|
---|
[6986418] | 277 |
|
---|
| 278 | // allocate space for the report descriptor
|
---|
| 279 | kbd_dev->conf->interfaces[i].report_desc = (uint8_t *)malloc(length);
|
---|
| 280 |
|
---|
[45019865] | 281 | // get the descriptor from the device
|
---|
[03197ffc] | 282 | int rc = usb_request_get_descriptor(&kbd_dev->ctrl_pipe,
|
---|
| 283 | USB_REQUEST_TYPE_CLASS, USB_DESCTYPE_HID_REPORT,
|
---|
| 284 | i, 0,
|
---|
| 285 | kbd_dev->conf->interfaces[i].report_desc, length,
|
---|
[45019865] | 286 | &actual_size);
|
---|
| 287 |
|
---|
| 288 | if (rc != EOK) {
|
---|
| 289 | return rc;
|
---|
| 290 | }
|
---|
| 291 |
|
---|
| 292 | assert(actual_size == length);
|
---|
| 293 |
|
---|
[fbddf94] | 294 | //dump_hid_class_descriptor(0, USB_DESCTYPE_HID_REPORT,
|
---|
| 295 | // kbd_dev->conf->interfaces[i].report_desc, length);
|
---|
[6986418] | 296 | }
|
---|
[45019865] | 297 |
|
---|
| 298 | return EOK;
|
---|
[6986418] | 299 | }
|
---|
[ca038b4] | 300 | static int usbkbd_process_descriptors(usb_hid_dev_kbd_t *kbd_dev)
|
---|
[243cb86] | 301 | {
|
---|
[ca038b4] | 302 | // get the first configuration descriptor (TODO: parse also other!)
|
---|
[dafab9e0] | 303 | usb_standard_configuration_descriptor_t config_desc;
|
---|
| 304 |
|
---|
[03197ffc] | 305 | int rc;
|
---|
| 306 | rc = usb_request_get_bare_configuration_descriptor(&kbd_dev->ctrl_pipe,
|
---|
| 307 | 0, &config_desc);
|
---|
[dafab9e0] | 308 |
|
---|
| 309 | if (rc != EOK) {
|
---|
| 310 | return rc;
|
---|
| 311 | }
|
---|
| 312 |
|
---|
| 313 | // prepare space for all underlying descriptors
|
---|
| 314 | uint8_t *descriptors = (uint8_t *)malloc(config_desc.total_length);
|
---|
| 315 | if (descriptors == NULL) {
|
---|
| 316 | return ENOMEM;
|
---|
| 317 | }
|
---|
| 318 |
|
---|
| 319 | size_t transferred = 0;
|
---|
| 320 | // get full configuration descriptor
|
---|
[03197ffc] | 321 | rc = usb_request_get_full_configuration_descriptor(&kbd_dev->ctrl_pipe,
|
---|
| 322 | 0, descriptors,
|
---|
[dafab9e0] | 323 | config_desc.total_length, &transferred);
|
---|
| 324 |
|
---|
[91db50ac] | 325 | if (rc != EOK) {
|
---|
[243cb86] | 326 | return rc;
|
---|
[91db50ac] | 327 | }
|
---|
[dafab9e0] | 328 | if (transferred != config_desc.total_length) {
|
---|
| 329 | return ELIMIT;
|
---|
| 330 | }
|
---|
| 331 |
|
---|
[ca038b4] | 332 | kbd_dev->conf = (usb_hid_configuration_t *)calloc(1,
|
---|
| 333 | sizeof(usb_hid_configuration_t));
|
---|
| 334 | if (kbd_dev->conf == NULL) {
|
---|
| 335 | free(descriptors);
|
---|
| 336 | return ENOMEM;
|
---|
| 337 | }
|
---|
| 338 |
|
---|
| 339 | rc = usbkbd_parse_descriptors(descriptors, transferred, kbd_dev->conf);
|
---|
[dafab9e0] | 340 | free(descriptors);
|
---|
[45019865] | 341 | if (rc != EOK) {
|
---|
| 342 | printf("Problem with parsing standard descriptors.\n");
|
---|
| 343 | return rc;
|
---|
| 344 | }
|
---|
[6986418] | 345 |
|
---|
| 346 | // get and report descriptors
|
---|
| 347 | rc = usbkbd_get_report_descriptor(kbd_dev);
|
---|
[45019865] | 348 | if (rc != EOK) {
|
---|
| 349 | printf("Problem with parsing HID REPORT descriptor.\n");
|
---|
| 350 | return rc;
|
---|
| 351 | }
|
---|
[243cb86] | 352 |
|
---|
[76cb03c] | 353 | //usbkbd_print_config(kbd_dev->conf);
|
---|
[0a9ea4a] | 354 |
|
---|
| 355 | /*
|
---|
| 356 | * TODO:
|
---|
| 357 | * 1) select one configuration (lets say the first)
|
---|
| 358 | * 2) how many interfaces?? how to select one??
|
---|
| 359 | * ("The default setting for an interface is always alternate setting zero.")
|
---|
| 360 | * 3) find endpoint which is IN and INTERRUPT (parse), save its number
|
---|
| 361 | * as the endpoint for polling
|
---|
| 362 | */
|
---|
[1f383dde] | 363 |
|
---|
[45019865] | 364 | return EOK;
|
---|
[243cb86] | 365 | }
|
---|
[03197ffc] | 366 |
|
---|
[2e15ac40] | 367 | static usb_hid_dev_kbd_t *usbkbd_init_device(device_t *dev)
|
---|
| 368 | {
|
---|
[03197ffc] | 369 | int rc;
|
---|
| 370 |
|
---|
[ca038b4] | 371 | usb_hid_dev_kbd_t *kbd_dev = (usb_hid_dev_kbd_t *)calloc(1,
|
---|
| 372 | sizeof(usb_hid_dev_kbd_t));
|
---|
[2e15ac40] | 373 |
|
---|
| 374 | if (kbd_dev == NULL) {
|
---|
| 375 | fprintf(stderr, NAME ": No memory!\n");
|
---|
| 376 | return NULL;
|
---|
| 377 | }
|
---|
| 378 |
|
---|
| 379 | kbd_dev->device = dev;
|
---|
| 380 |
|
---|
[707ffcf] | 381 | /*
|
---|
| 382 | * Initialize the backing connection to the host controller.
|
---|
| 383 | */
|
---|
[23c7f4d] | 384 | rc = usb_device_connection_initialize_from_device(&kbd_dev->wire, dev);
|
---|
[707ffcf] | 385 | if (rc != EOK) {
|
---|
| 386 | printf("Problem initializing connection to device: %s.\n",
|
---|
| 387 | str_error(rc));
|
---|
| 388 | goto error_leave;
|
---|
| 389 | }
|
---|
| 390 |
|
---|
| 391 | /*
|
---|
| 392 | * Initialize device pipes.
|
---|
| 393 | */
|
---|
[03197ffc] | 394 | rc = usb_endpoint_pipe_initialize_default_control(&kbd_dev->ctrl_pipe,
|
---|
| 395 | &kbd_dev->wire);
|
---|
| 396 | if (rc != EOK) {
|
---|
| 397 | printf("Failed to initialize default control pipe: %s.\n",
|
---|
| 398 | str_error(rc));
|
---|
| 399 | goto error_leave;
|
---|
| 400 | }
|
---|
| 401 |
|
---|
[707ffcf] | 402 | rc = usb_endpoint_pipe_initialize(&kbd_dev->poll_pipe, &kbd_dev->wire,
|
---|
| 403 | GUESSED_POLL_ENDPOINT, USB_TRANSFER_INTERRUPT, USB_DIRECTION_IN);
|
---|
| 404 | if (rc != EOK) {
|
---|
| 405 | printf("Failed to initialize interrupt in pipe: %s.\n",
|
---|
| 406 | str_error(rc));
|
---|
| 407 | goto error_leave;
|
---|
| 408 | }
|
---|
| 409 |
|
---|
[03197ffc] | 410 | /*
|
---|
| 411 | * will need all descriptors:
|
---|
| 412 | * 1) choose one configuration from configuration descriptors
|
---|
| 413 | * (set it to the device)
|
---|
| 414 | * 2) set endpoints from endpoint descriptors
|
---|
| 415 | */
|
---|
| 416 |
|
---|
| 417 | // TODO: get descriptors, parse descriptors and save endpoints
|
---|
| 418 | usb_endpoint_pipe_start_session(&kbd_dev->ctrl_pipe);
|
---|
| 419 | //usb_request_set_configuration(&kbd_dev->ctrl_pipe, 1);
|
---|
| 420 | usbkbd_process_descriptors(kbd_dev);
|
---|
| 421 | usb_endpoint_pipe_end_session(&kbd_dev->ctrl_pipe);
|
---|
[707ffcf] | 422 |
|
---|
[2e15ac40] | 423 | return kbd_dev;
|
---|
[707ffcf] | 424 |
|
---|
| 425 | error_leave:
|
---|
| 426 | free(kbd_dev);
|
---|
| 427 | return NULL;
|
---|
[2e15ac40] | 428 | }
|
---|
[91db50ac] | 429 |
|
---|
[101ef25c] | 430 | static void usbkbd_process_interrupt_in(usb_hid_dev_kbd_t *kbd_dev,
|
---|
[243cb86] | 431 | uint8_t *buffer, size_t actual_size)
|
---|
[101ef25c] | 432 | {
|
---|
[243cb86] | 433 | usb_hid_report_in_callbacks_t *callbacks =
|
---|
| 434 | (usb_hid_report_in_callbacks_t *)malloc(
|
---|
| 435 | sizeof(usb_hid_report_in_callbacks_t));
|
---|
| 436 | callbacks->keyboard = usbkbd_process_keycodes;
|
---|
| 437 |
|
---|
[0a9ea4a] | 438 | //usb_hid_parse_report(kbd_dev->parser, buffer, actual_size, callbacks,
|
---|
| 439 | // NULL);
|
---|
[c113056] | 440 | printf("Calling usb_hid_boot_keyboard_input_report() with size %zu\n",
|
---|
[7c169ce] | 441 | actual_size);
|
---|
| 442 | //dump_buffer("bufffer: ", buffer, actual_size);
|
---|
[6336b6e] | 443 | int rc = usb_hid_boot_keyboard_input_report(buffer, actual_size, callbacks,
|
---|
| 444 | NULL);
|
---|
| 445 | if (rc != EOK) {
|
---|
| 446 | printf("Error in usb_hid_boot_keyboard_input_report(): %d\n", rc);
|
---|
| 447 | }
|
---|
[101ef25c] | 448 | }
|
---|
| 449 |
|
---|
[2e15ac40] | 450 | static void usbkbd_poll_keyboard(usb_hid_dev_kbd_t *kbd_dev)
|
---|
[91db50ac] | 451 | {
|
---|
[707ffcf] | 452 | int rc, sess_rc;
|
---|
[243cb86] | 453 | uint8_t buffer[BUFFER_SIZE];
|
---|
[91db50ac] | 454 | size_t actual_size;
|
---|
[0e126be7] | 455 |
|
---|
[6336b6e] | 456 | printf("Polling keyboard...\n");
|
---|
| 457 |
|
---|
[101ef25c] | 458 | while (true) {
|
---|
[2899980] | 459 | async_usleep(1000 * 10);
|
---|
[101ef25c] | 460 |
|
---|
[707ffcf] | 461 | sess_rc = usb_endpoint_pipe_start_session(&kbd_dev->poll_pipe);
|
---|
| 462 | if (sess_rc != EOK) {
|
---|
| 463 | printf("Failed to start a session: %s.\n",
|
---|
| 464 | str_error(sess_rc));
|
---|
[101ef25c] | 465 | continue;
|
---|
| 466 | }
|
---|
| 467 |
|
---|
[707ffcf] | 468 | rc = usb_endpoint_pipe_read(&kbd_dev->poll_pipe, buffer,
|
---|
| 469 | BUFFER_SIZE, &actual_size);
|
---|
| 470 | sess_rc = usb_endpoint_pipe_end_session(&kbd_dev->poll_pipe);
|
---|
| 471 |
|
---|
[101ef25c] | 472 | if (rc != EOK) {
|
---|
[707ffcf] | 473 | printf("Error polling the keyboard: %s.\n",
|
---|
| 474 | str_error(rc));
|
---|
| 475 | continue;
|
---|
| 476 | }
|
---|
| 477 |
|
---|
| 478 | if (sess_rc != EOK) {
|
---|
| 479 | printf("Error closing session: %s.\n",
|
---|
| 480 | str_error(sess_rc));
|
---|
[101ef25c] | 481 | continue;
|
---|
| 482 | }
|
---|
| 483 |
|
---|
| 484 | /*
|
---|
| 485 | * If the keyboard answered with NAK, it returned no data.
|
---|
| 486 | * This implies that no change happened since last query.
|
---|
| 487 | */
|
---|
| 488 | if (actual_size == 0) {
|
---|
[7c169ce] | 489 | printf("Keyboard returned NAK\n");
|
---|
[101ef25c] | 490 | continue;
|
---|
| 491 | }
|
---|
| 492 |
|
---|
| 493 | /*
|
---|
| 494 | * TODO: Process pressed keys.
|
---|
| 495 | */
|
---|
[6336b6e] | 496 | printf("Calling usbkbd_process_interrupt_in()\n");
|
---|
[101ef25c] | 497 | usbkbd_process_interrupt_in(kbd_dev, buffer, actual_size);
|
---|
[91db50ac] | 498 | }
|
---|
| 499 |
|
---|
[101ef25c] | 500 | // not reached
|
---|
| 501 | assert(0);
|
---|
[91db50ac] | 502 | }
|
---|
| 503 |
|
---|
[2e15ac40] | 504 | static int usbkbd_fibril_device(void *arg)
|
---|
| 505 | {
|
---|
| 506 | printf("!!! USB device fibril\n");
|
---|
| 507 |
|
---|
| 508 | if (arg == NULL) {
|
---|
| 509 | printf("No device!\n");
|
---|
| 510 | return -1;
|
---|
| 511 | }
|
---|
| 512 |
|
---|
| 513 | device_t *dev = (device_t *)arg;
|
---|
| 514 |
|
---|
| 515 | // initialize device (get and process descriptors, get address, etc.)
|
---|
| 516 | usb_hid_dev_kbd_t *kbd_dev = usbkbd_init_device(dev);
|
---|
[6336b6e] | 517 | if (kbd_dev == NULL) {
|
---|
| 518 | printf("Error while initializing device.\n");
|
---|
| 519 | return -1;
|
---|
| 520 | }
|
---|
[2e15ac40] | 521 |
|
---|
| 522 | usbkbd_poll_keyboard(kbd_dev);
|
---|
| 523 |
|
---|
| 524 | return EOK;
|
---|
| 525 | }
|
---|
| 526 |
|
---|
| 527 | static int usbkbd_add_device(device_t *dev)
|
---|
[c7137738] | 528 | {
|
---|
| 529 | /* For now, fail immediately. */
|
---|
[101ef25c] | 530 | //return ENOTSUP;
|
---|
[c7137738] | 531 |
|
---|
| 532 | /*
|
---|
| 533 | * When everything is okay, connect to "our" HC.
|
---|
[2e15ac40] | 534 | *
|
---|
| 535 | * Not supported yet, skip..
|
---|
[c7137738] | 536 | */
|
---|
[71ed4849] | 537 | // int phone = usb_drv_hc_connect_auto(dev, 0);
|
---|
[2e15ac40] | 538 | // if (phone < 0) {
|
---|
| 539 | // /*
|
---|
| 540 | // * Connecting to HC failed, roll-back and announce
|
---|
| 541 | // * failure.
|
---|
| 542 | // */
|
---|
| 543 | // return phone;
|
---|
| 544 | // }
|
---|
[c7137738] | 545 |
|
---|
[2e15ac40] | 546 | // dev->parent_phone = phone;
|
---|
[91db50ac] | 547 |
|
---|
| 548 | /*
|
---|
[2e15ac40] | 549 | * Create new fibril for handling this keyboard
|
---|
[91db50ac] | 550 | */
|
---|
[2e15ac40] | 551 | fid_t fid = fibril_create(usbkbd_fibril_device, dev);
|
---|
| 552 | if (fid == 0) {
|
---|
| 553 | printf("%s: failed to start fibril for HID device\n", NAME);
|
---|
| 554 | return ENOMEM;
|
---|
| 555 | }
|
---|
| 556 | fibril_add_ready(fid);
|
---|
[91db50ac] | 557 |
|
---|
[700af62] | 558 | dev->ops = &keyboard_ops;
|
---|
| 559 |
|
---|
| 560 | add_device_to_class(dev, "keyboard");
|
---|
| 561 |
|
---|
[c7137738] | 562 | /*
|
---|
| 563 | * Hurrah, device is initialized.
|
---|
| 564 | */
|
---|
| 565 | return EOK;
|
---|
| 566 | }
|
---|
| 567 |
|
---|
| 568 | static driver_ops_t kbd_driver_ops = {
|
---|
[2e15ac40] | 569 | .add_device = usbkbd_add_device,
|
---|
[c7137738] | 570 | };
|
---|
| 571 |
|
---|
| 572 | static driver_t kbd_driver = {
|
---|
[2e15ac40] | 573 | .name = NAME,
|
---|
[c7137738] | 574 | .driver_ops = &kbd_driver_ops
|
---|
| 575 | };
|
---|
| 576 |
|
---|
| 577 | int main(int argc, char *argv[])
|
---|
| 578 | {
|
---|
| 579 | return driver_main(&kbd_driver);
|
---|
| 580 | }
|
---|
[ba54451] | 581 |
|
---|
| 582 | /**
|
---|
| 583 | * @}
|
---|
| 584 | */
|
---|