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