Changeset f1fae414 in mainline for uspace/drv/bus/usb/usbhid/kbd/kbddev.c
- Timestamp:
- 2011-06-22T01:34:53Z (13 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 8d7e82c1, cac458f
- Parents:
- 72ec8cc (diff), bf172825 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)
links above to see all the changes relative to each parent. - File:
-
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
uspace/drv/bus/usb/usbhid/kbd/kbddev.c
r72ec8cc rf1fae414 40 40 41 41 #include <io/keycode.h> 42 #include <ipc/kbd.h> 42 #include <io/console.h> 43 #include <ipc/kbdev.h> 43 44 #include <async.h> 44 45 #include <async_obsolete.h> 45 46 #include <fibril.h> 46 47 #include <fibril_synch.h> 48 49 #include <ddf/log.h> 47 50 48 51 #include <usb/usb.h> … … 63 66 #include "kbddev.h" 64 67 65 #include "layout.h"66 68 #include "conv.h" 67 69 #include "kbdrepeat.h" … … 73 75 74 76 /*----------------------------------------------------------------------------*/ 75 /** Default modifiers when the keyboard is initialized. */ 77 76 78 static const unsigned DEFAULT_ACTIVE_MODS = KM_NUM_LOCK; 77 79 … … 101 103 const char *HID_KBD_FUN_NAME = "keyboard"; 102 104 const char *HID_KBD_CLASS_NAME = "keyboard"; 105 106 static void usb_kbd_set_led(usb_hid_dev_t *hid_dev, usb_kbd_t *kbd_dev); 103 107 104 108 /*----------------------------------------------------------------------------*/ … … 154 158 155 159 /*----------------------------------------------------------------------------*/ 156 /* Keyboard layouts */157 /*----------------------------------------------------------------------------*/158 159 #define NUM_LAYOUTS 3160 161 /** Keyboard layout map. */162 static layout_op_t *layout[NUM_LAYOUTS] = {163 &us_qwerty_op,164 &us_dvorak_op,165 &cz_op166 };167 168 static int active_layout = 0;169 170 /*----------------------------------------------------------------------------*/171 160 /* IPC method handler */ 172 161 /*----------------------------------------------------------------------------*/ … … 174 163 static void default_connection_handler(ddf_fun_t *, ipc_callid_t, ipc_call_t *); 175 164 176 /** 165 /** 177 166 * Default handler for IPC methods not handled by DDF. 178 167 * … … 189 178 { 190 179 sysarg_t method = IPC_GET_IMETHOD(*icall); 180 int callback; 191 181 192 182 usb_kbd_t *kbd_dev = (usb_kbd_t *)fun->driver_data; … … 198 188 } 199 189 200 if (method == IPC_M_CONNECT_TO_ME) { 201 int callback = IPC_GET_ARG5(*icall); 190 switch (method) { 191 case IPC_M_CONNECT_TO_ME: 192 callback = IPC_GET_ARG5(*icall); 202 193 203 194 if (kbd_dev->console_phone != -1) { … … 212 203 usb_log_debug("default_connection_handler: OK\n"); 213 204 async_answer_0(icallid, EOK); 214 return; 215 } 216 217 usb_log_debug("default_connection_handler: Wrong function.\n"); 218 async_answer_0(icallid, EINVAL); 205 break; 206 case KBDEV_SET_IND: 207 kbd_dev->mods = IPC_GET_ARG1(*icall); 208 usb_kbd_set_led(kbd_dev->hid_dev, kbd_dev); 209 async_answer_0(icallid, EOK); 210 break; 211 default: 212 usb_log_debug("default_connection_handler: Wrong function.\n"); 213 async_answer_0(icallid, EINVAL); 214 break; 215 } 219 216 } 220 217 … … 225 222 * Handles turning of LED lights on and off. 226 223 * 227 * In case of USB keyboards, the LEDs are handled in the driver, not in the 228 * device. When there should be a change (lock key was pressed), the driver 229 * uses a Set_Report request sent to the device to set the state of the LEDs. 224 * As with most other keyboards, the LED indicators in USB keyboards are 225 * driven by software. When state of some modifier changes, the input server 226 * will call us and tell us to update the LED state and what the new state 227 * should be. 230 228 * 231 229 * This functions sets the LED lights according to current settings of modifiers … … 249 247 USB_HID_REPORT_TYPE_OUTPUT); 250 248 251 while (field != NULL) { 249 while (field != NULL) { 252 250 253 251 if ((field->usage == USB_HID_LED_NUM_LOCK) … … 292 290 293 291 /*----------------------------------------------------------------------------*/ 294 /** 295 * Processes key events. 296 * 297 * @note This function was copied from AT keyboard driver and modified to suit 298 * USB keyboard. 299 * 300 * @note Lock keys are not sent to the console, as they are completely handled 301 * in the driver. It may, however, be required later that the driver 302 * sends also these keys to application (otherwise it cannot use those 303 * keys at all). 304 * 292 /** Send key event. 293 * 305 294 * @param kbd_dev Keyboard device structure. 306 * @param type Type of the event (press / release). Recognized values: 295 * @param type Type of the event (press / release). Recognized values: 307 296 * KEY_PRESS, KEY_RELEASE 308 * @param key Key code of the key according to HID Usage Tables.297 * @param key Key code 309 298 */ 310 299 void usb_kbd_push_ev(usb_hid_dev_t *hid_dev, usb_kbd_t *kbd_dev, int type, 311 300 unsigned int key) 312 301 { 313 kbd_event_t ev; 314 unsigned mod_mask; 315 316 /* 317 * These parts are copy-pasted from the AT keyboard driver. 318 * 319 * They definitely require some refactoring, but will keep it for later 320 * when the console and keyboard system is changed in HelenOS. 321 */ 322 switch (key) { 323 case KC_LCTRL: mod_mask = KM_LCTRL; break; 324 case KC_RCTRL: mod_mask = KM_RCTRL; break; 325 case KC_LSHIFT: mod_mask = KM_LSHIFT; break; 326 case KC_RSHIFT: mod_mask = KM_RSHIFT; break; 327 case KC_LALT: mod_mask = KM_LALT; break; 328 case KC_RALT: mod_mask = KM_RALT; break; 329 default: mod_mask = 0; break; 330 } 331 332 if (mod_mask != 0) { 333 if (type == KEY_PRESS) 334 kbd_dev->mods = kbd_dev->mods | mod_mask; 335 else 336 kbd_dev->mods = kbd_dev->mods & ~mod_mask; 337 } 338 339 switch (key) { 340 case KC_CAPS_LOCK: mod_mask = KM_CAPS_LOCK; break; 341 case KC_NUM_LOCK: mod_mask = KM_NUM_LOCK; break; 342 case KC_SCROLL_LOCK: mod_mask = KM_SCROLL_LOCK; break; 343 default: mod_mask = 0; break; 344 } 345 346 if (mod_mask != 0) { 347 if (type == KEY_PRESS) { 348 /* 349 * Only change lock state on transition from released 350 * to pressed. This prevents autorepeat from messing 351 * up the lock state. 352 */ 353 unsigned int locks_old = kbd_dev->lock_keys; 354 355 kbd_dev->mods = 356 kbd_dev->mods ^ (mod_mask & ~kbd_dev->lock_keys); 357 kbd_dev->lock_keys = kbd_dev->lock_keys | mod_mask; 358 359 /* Update keyboard lock indicator lights. */ 360 if (kbd_dev->lock_keys != locks_old 361 && hid_dev != NULL) { // ugly hack 362 usb_kbd_set_led(hid_dev, kbd_dev); 363 } 364 } else { 365 kbd_dev->lock_keys = kbd_dev->lock_keys & ~mod_mask; 366 } 367 } 368 369 if (key == KC_CAPS_LOCK || key == KC_NUM_LOCK || key == KC_SCROLL_LOCK) { 370 // do not send anything to the console, this is our business 371 return; 372 } 373 374 if (type == KEY_PRESS && (kbd_dev->mods & KM_LCTRL) && key == KC_F1) { 375 active_layout = 0; 376 layout[active_layout]->reset(); 377 return; 378 } 379 380 if (type == KEY_PRESS && (kbd_dev->mods & KM_LCTRL) && key == KC_F2) { 381 active_layout = 1; 382 layout[active_layout]->reset(); 383 return; 384 } 385 386 if (type == KEY_PRESS && (kbd_dev->mods & KM_LCTRL) && key == KC_F3) { 387 active_layout = 2; 388 layout[active_layout]->reset(); 389 return; 390 } 391 392 ev.type = type; 393 ev.key = key; 394 ev.mods = kbd_dev->mods; 395 396 ev.c = layout[active_layout]->parse_ev(&ev); 397 398 usb_log_debug2("Sending key %d to the console\n", ev.key); 302 usb_log_debug2("Sending kbdev event %d/%d to the console\n", type, key); 399 303 if (kbd_dev->console_phone < 0) { 400 304 usb_log_warning( … … 403 307 } 404 308 405 async_obsolete_msg_4(kbd_dev->console_phone, KBD_EVENT, ev.type, ev.key, 406 ev.mods, ev.c); 309 async_obsolete_msg_2(kbd_dev->console_phone, KBDEV_EVENT, type, key); 407 310 } 408 311 … … 414 317 || key_code == KC_SCROLL_LOCK 415 318 || key_code == KC_CAPS_LOCK); 319 } 320 321 static size_t find_in_array_int32(int32_t val, int32_t *arr, size_t arr_size) 322 { 323 for (size_t i = 0; i < arr_size; i++) { 324 if (arr[i] == val) { 325 return i; 326 } 327 } 328 329 return (size_t) -1; 416 330 } 417 331 … … 436 350 { 437 351 unsigned int key; 438 unsigned int i, j;352 size_t i; 439 353 440 354 /* … … 446 360 * whole input report. 447 361 */ 448 i = 0; 449 while (i < kbd_dev->key_count && kbd_dev->keys[i] != ERROR_ROLLOVER) { 450 ++i; 451 } 452 if (i != kbd_dev->key_count) { 453 usb_log_debug("Phantom state occured.\n"); 454 // phantom state, do nothing 362 i = find_in_array_int32(ERROR_ROLLOVER, kbd_dev->keys, 363 kbd_dev->key_count); 364 if (i != (size_t) -1) { 365 usb_log_debug("Detected phantom state.\n"); 455 366 return; 456 367 } 457 368 458 369 /* 459 * 1) Key releases 460 */ 461 for (j = 0; j < kbd_dev->key_count; ++j) { 462 // try to find the old key in the new key list 463 i = 0; 464 while (i < kbd_dev->key_count 465 && kbd_dev->keys[i] != kbd_dev->keys_old[j]) { 466 ++i; 467 } 468 469 if (i == kbd_dev->key_count) { 470 // not found, i.e. the key was released 471 key = usbhid_parse_scancode(kbd_dev->keys_old[j]); 370 * Key releases 371 */ 372 for (i = 0; i < kbd_dev->key_count; i++) { 373 int32_t old_key = kbd_dev->keys_old[i]; 374 /* Find the old key among currently pressed keys. */ 375 size_t pos = find_in_array_int32(old_key, kbd_dev->keys, 376 kbd_dev->key_count); 377 /* If the key was not found, we need to signal release. */ 378 if (pos == (size_t) -1) { 379 key = usbhid_parse_scancode(old_key); 472 380 if (!usb_kbd_is_lock(key)) { 473 381 usb_kbd_repeat_stop(kbd_dev, key); 474 382 } 475 383 usb_kbd_push_ev(hid_dev, kbd_dev, KEY_RELEASE, key); 476 usb_log_debug2("Key released: %d\n", key); 477 } else { 478 // found, nothing happens 479 } 480 } 481 482 /* 483 * 1) Key presses 384 usb_log_debug2("Key released: %u " 385 "(USB code %" PRIu32 ")\n", key, old_key); 386 } 387 } 388 389 /* 390 * Key presses 484 391 */ 485 392 for (i = 0; i < kbd_dev->key_count; ++i) { 486 // try to find the new key in the old key list 487 j = 0; 488 while (j < kbd_dev->key_count 489 && kbd_dev->keys_old[j] != kbd_dev->keys[i]) { 490 ++j; 491 } 492 493 if (j == kbd_dev->key_count) { 494 // not found, i.e. new key pressed 393 int32_t new_key = kbd_dev->keys[i]; 394 /* Find the new key among already pressed keys. */ 395 size_t pos = find_in_array_int32(new_key, kbd_dev->keys_old, 396 kbd_dev->key_count); 397 /* If the key was not found, we need to signal press. */ 398 if (pos == (size_t) -1) { 495 399 key = usbhid_parse_scancode(kbd_dev->keys[i]); 496 usb_log_debug2("Key pressed: %d (keycode: %d)\n", key,497 kbd_dev->keys[i]);498 400 if (!usb_kbd_is_lock(key)) { 499 401 usb_kbd_repeat_start(kbd_dev, key); 500 402 } 501 403 usb_kbd_push_ev(hid_dev, kbd_dev, KEY_PRESS, key); 502 } else {503 // found, nothing happens404 usb_log_debug2("Key pressed: %u " 405 "(USB code %" PRIu32 ")\n", key, new_key); 504 406 } 505 407 } … … 507 409 memcpy(kbd_dev->keys_old, kbd_dev->keys, kbd_dev->key_count * 4); 508 410 509 usb_log_debug2("New stored keys: "); 510 for (i = 0; i < kbd_dev->key_count; ++i) { 511 usb_log_debug2("%d ", kbd_dev->keys_old[i]); 512 } 513 usb_log_debug2("\n"); 411 char key_buffer[512]; 412 ddf_dump_buffer(key_buffer, 512, 413 kbd_dev->keys_old, 4, kbd_dev->key_count, 0); 414 usb_log_debug2("Stored keys %s.\n", key_buffer); 514 415 } 515 416 … … 533 434 * usb_hid_parse_report(). 534 435 */ 535 static void usb_kbd_process_data(usb_hid_dev_t *hid_dev, usb_kbd_t *kbd_dev, 536 uint8_t *buffer, size_t actual_size) 436 static void usb_kbd_process_data(usb_hid_dev_t *hid_dev, usb_kbd_t *kbd_dev) 537 437 { 538 438 assert(hid_dev->report != NULL); 539 439 assert(hid_dev != NULL); 540 440 assert(kbd_dev != NULL); 541 542 usb_log_debug("Calling usb_hid_parse_report() with "543 "buffer %s\n", usb_debug_str_buffer(buffer, actual_size, 0));544 441 545 442 usb_hid_report_path_t *path = usb_hid_report_path(); 546 443 usb_hid_report_path_append_item(path, USB_HIDUT_PAGE_KEYBOARD, 0); 547 444 548 uint8_t report_id; 549 int rc = usb_hid_parse_report(hid_dev->report, buffer, actual_size, 550 &report_id); 551 552 if (rc != EOK) { 553 usb_log_warning("Error in usb_hid_parse_report():" 554 "%s\n", str_error(rc)); 555 } 556 557 usb_hid_report_path_set_report_id (path, report_id); 445 usb_hid_report_path_set_report_id (path, hid_dev->report_id); 558 446 559 447 // fill in the currently pressed keys … … 716 604 return ENOMEM; // TODO: some other code?? 717 605 } 606 607 /* Store link to HID device */ 608 kbd_dev->hid_dev = hid_dev; 718 609 719 610 /* … … 787 678 /* 788 679 * Modifiers and locks 789 */ 680 */ 790 681 kbd_dev->modifiers = 0; 791 682 kbd_dev->mods = DEFAULT_ACTIVE_MODS; … … 794 685 /* 795 686 * Autorepeat 796 */ 687 */ 797 688 kbd_dev->repeat.key_new = 0; 798 689 kbd_dev->repeat.key_repeated = 0; … … 852 743 /*----------------------------------------------------------------------------*/ 853 744 854 bool usb_kbd_polling_callback(usb_hid_dev_t *hid_dev, void *data, 855 uint8_t *buffer, size_t buffer_size) 856 { 857 if (hid_dev == NULL || buffer == NULL || data == NULL) { 745 bool usb_kbd_polling_callback(usb_hid_dev_t *hid_dev, void *data) 746 { 747 if (hid_dev == NULL/* || buffer == NULL*/ || data == NULL) { 858 748 // do not continue polling (???) 859 749 return false; … … 864 754 865 755 // TODO: add return value from this function 866 usb_kbd_process_data(hid_dev, kbd_dev , buffer, buffer_size);756 usb_kbd_process_data(hid_dev, kbd_dev); 867 757 868 758 return true; … … 900 790 if ((*kbd_dev)->repeat_mtx != NULL) { 901 791 //assert(!fibril_mutex_is_locked((*kbd_dev)->repeat_mtx)); 792 // FIXME - the fibril_mutex_is_locked may not cause 793 // fibril scheduling 902 794 while (fibril_mutex_is_locked((*kbd_dev)->repeat_mtx)) {} 903 795 free((*kbd_dev)->repeat_mtx);
Note:
See TracChangeset
for help on using the changeset viewer.