Changes in uspace/drv/usbhid/kbd/kbddev.c [323b0ec:73ae3373] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/drv/usbhid/kbd/kbddev.c
r323b0ec r73ae3373 176 176 177 177 /*----------------------------------------------------------------------------*/ 178 179 //static void usb_kbd_process_keycodes(const uint8_t *key_codes, size_t count,180 // uint8_t report_id, void *arg);181 182 //static const usb_hid_report_in_callbacks_t usb_kbd_parser_callbacks = {183 // .keyboard = usb_kbd_process_keycodes184 //};185 186 /*----------------------------------------------------------------------------*/187 178 /* Keyboard layouts */ 188 179 /*----------------------------------------------------------------------------*/ … … 203 194 /*----------------------------------------------------------------------------*/ 204 195 /** Mapping of USB modifier key codes to generic modifier key codes. */ 205 //static const keycode_t usbhid_modifiers_keycodes[USB_HID_MOD_COUNT] = {206 //KC_LCTRL, /* USB_HID_MOD_LCTRL */207 //KC_LSHIFT, /* USB_HID_MOD_LSHIFT */208 //KC_LALT, /* USB_HID_MOD_LALT */209 //0, /* USB_HID_MOD_LGUI */210 //KC_RCTRL, /* USB_HID_MOD_RCTRL */211 //KC_RSHIFT, /* USB_HID_MOD_RSHIFT */212 //KC_RALT, /* USB_HID_MOD_RALT */213 //0, /* USB_HID_MOD_RGUI */214 //};215 216 //typedef enum usbhid_lock_code {217 //USB_KBD_LOCK_NUM = 0x53,218 //USB_KBD_LOCK_CAPS = 0x39,219 //USB_KBD_LOCK_SCROLL = 0x47,220 //USB_KBD_LOCK_COUNT = 3221 //} usbhid_lock_code;222 223 //static const usbhid_lock_code usbhid_lock_codes[USB_KBD_LOCK_COUNT] = {224 //USB_KBD_LOCK_NUM,225 //USB_KBD_LOCK_CAPS,226 //USB_KBD_LOCK_SCROLL227 //};196 static const keycode_t usbhid_modifiers_keycodes[USB_HID_MOD_COUNT] = { 197 KC_LCTRL, /* USB_HID_MOD_LCTRL */ 198 KC_LSHIFT, /* USB_HID_MOD_LSHIFT */ 199 KC_LALT, /* USB_HID_MOD_LALT */ 200 0, /* USB_HID_MOD_LGUI */ 201 KC_RCTRL, /* USB_HID_MOD_RCTRL */ 202 KC_RSHIFT, /* USB_HID_MOD_RSHIFT */ 203 KC_RALT, /* USB_HID_MOD_RALT */ 204 0, /* USB_HID_MOD_RGUI */ 205 }; 206 207 typedef enum usbhid_lock_code { 208 USB_KBD_LOCK_NUM = 0x53, 209 USB_KBD_LOCK_CAPS = 0x39, 210 USB_KBD_LOCK_SCROLL = 0x47, 211 USB_KBD_LOCK_COUNT = 3 212 } usbhid_lock_code; 213 214 static const usbhid_lock_code usbhid_lock_codes[USB_KBD_LOCK_COUNT] = { 215 USB_KBD_LOCK_NUM, 216 USB_KBD_LOCK_CAPS, 217 USB_KBD_LOCK_SCROLL 218 }; 228 219 229 220 /*----------------------------------------------------------------------------*/ … … 247 238 * @param icall Call data. 248 239 */ 249 staticvoid default_connection_handler(ddf_fun_t *fun,240 void default_connection_handler(ddf_fun_t *fun, 250 241 ipc_callid_t icallid, ipc_call_t *icall) 251 242 { … … 299 290 return; 300 291 } 301 292 293 unsigned i = 0; 294 302 295 /* Reset the LED data. */ 303 296 memset(kbd_dev->led_data, 0, kbd_dev->led_output_size * sizeof(int32_t)); 304 usb_log_debug("Creating output report:\n"); 305 306 usb_hid_report_field_t *field = usb_hid_report_get_sibling( 307 hid_dev->report, NULL, kbd_dev->led_path, 308 USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY | USB_HID_PATH_COMPARE_END, 309 USB_HID_REPORT_TYPE_OUTPUT); 310 311 while (field != NULL) { 312 313 if ((field->usage == USB_HID_LED_NUM_LOCK) 314 && (kbd_dev->mods & KM_NUM_LOCK)){ 315 field->value = 1; 316 } 317 318 if ((field->usage == USB_HID_LED_CAPS_LOCK) 319 && (kbd_dev->mods & KM_CAPS_LOCK)){ 320 field->value = 1; 321 } 322 323 if ((field->usage == USB_HID_LED_SCROLL_LOCK) 324 && (kbd_dev->mods & KM_SCROLL_LOCK)){ 325 field->value = 1; 326 } 327 328 field = usb_hid_report_get_sibling(hid_dev->report, field, 329 kbd_dev->led_path, USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY 330 | USB_HID_PATH_COMPARE_END, USB_HID_REPORT_TYPE_OUTPUT); 331 } 332 333 // TODO: what about the Report ID? 334 int rc = usb_hid_report_output_translate(hid_dev->report, 0, 335 kbd_dev->output_buffer, kbd_dev->output_size); 297 298 if ((kbd_dev->mods & KM_NUM_LOCK) && (i < kbd_dev->led_output_size)) { 299 kbd_dev->led_data[i++] = USB_HID_LED_NUM_LOCK; 300 } 301 302 if ((kbd_dev->mods & KM_CAPS_LOCK) && (i < kbd_dev->led_output_size)) { 303 kbd_dev->led_data[i++] = USB_HID_LED_CAPS_LOCK; 304 } 305 306 if ((kbd_dev->mods & KM_SCROLL_LOCK) 307 && (i < kbd_dev->led_output_size)) { 308 kbd_dev->led_data[i++] = USB_HID_LED_SCROLL_LOCK; 309 } 310 311 // TODO: COMPOSE and KANA 312 313 usb_log_debug("Creating output report.\n"); 314 315 int rc = usb_hid_report_output_translate(hid_dev->parser, 316 kbd_dev->led_path, 317 USB_HID_PATH_COMPARE_END | USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY, 318 kbd_dev->output_buffer, 319 kbd_dev->output_size, kbd_dev->led_data, kbd_dev->led_output_size); 336 320 337 321 if (rc != EOK) { … … 492 476 */ 493 477 static void usb_kbd_check_key_changes(usb_hid_dev_t *hid_dev, 494 usb_kbd_t *kbd_dev /*, const uint8_t *key_codes, size_t count*/)478 usb_kbd_t *kbd_dev, const uint8_t *key_codes, size_t count) 495 479 { 496 480 unsigned int key; … … 506 490 */ 507 491 i = 0; 508 while (i < kbd_dev->key_count && kbd_dev->keys[i] != ERROR_ROLLOVER) {492 while (i < count && key_codes[i] != ERROR_ROLLOVER) { 509 493 ++i; 510 494 } 511 if (i != kbd_dev->key_count) {495 if (i != count) { 512 496 usb_log_debug("Phantom state occured.\n"); 513 497 // phantom state, do nothing 514 498 return; 515 499 } 500 501 /* TODO: quite dummy right now, think of better implementation */ 502 assert(count == kbd_dev->key_count); 516 503 517 504 /* 518 505 * 1) Key releases 519 506 */ 520 for (j = 0; j < kbd_dev->key_count; ++j) {507 for (j = 0; j < count; ++j) { 521 508 // try to find the old key in the new key list 522 509 i = 0; 523 510 while (i < kbd_dev->key_count 524 && k bd_dev->keys[i] != kbd_dev->keys_old[j]) {511 && key_codes[i] != kbd_dev->keys[j]) { 525 512 ++i; 526 513 } 527 514 528 if (i == kbd_dev->key_count) {515 if (i == count) { 529 516 // not found, i.e. the key was released 530 key = usbhid_parse_scancode(kbd_dev->keys _old[j]);517 key = usbhid_parse_scancode(kbd_dev->keys[j]); 531 518 if (!usb_kbd_is_lock(key)) { 532 519 usb_kbd_repeat_stop(kbd_dev, key); … … 545 532 // try to find the new key in the old key list 546 533 j = 0; 547 while (j < kbd_dev->key_count 548 && kbd_dev->keys_old[j] != kbd_dev->keys[i]) { 534 while (j < count && kbd_dev->keys[j] != key_codes[i]) { 549 535 ++j; 550 536 } 551 537 552 if (j == kbd_dev->key_count) {538 if (j == count) { 553 539 // not found, i.e. new key pressed 554 key = usbhid_parse_scancode(k bd_dev->keys[i]);540 key = usbhid_parse_scancode(key_codes[i]); 555 541 usb_log_debug2("Key pressed: %d (keycode: %d)\n", key, 556 kbd_dev->keys[i]); 557 usb_kbd_push_ev(hid_dev, kbd_dev, KEY_PRESS, key); 542 key_codes[i]); 543 usb_kbd_push_ev(hid_dev, kbd_dev, KEY_PRESS, 544 key); 558 545 if (!usb_kbd_is_lock(key)) { 559 546 usb_kbd_repeat_start(kbd_dev, key); … … 564 551 } 565 552 566 // usb_log_debug("Old keys: "); 567 // for (i = 0; i < kbd_dev->key_count; ++i) { 568 // usb_log_debug("%d ", kbd_dev->keys_old[i]); 569 // } 570 // usb_log_debug("\n"); 571 572 573 // usb_log_debug("New keys: "); 574 // for (i = 0; i < kbd_dev->key_count; ++i) { 575 // usb_log_debug("%d ", kbd_dev->keys[i]); 576 // } 577 // usb_log_debug("\n"); 578 579 memcpy(kbd_dev->keys_old, kbd_dev->keys, kbd_dev->key_count * 4); 580 581 usb_log_debug2("New stored keys: "); 582 for (i = 0; i < kbd_dev->key_count; ++i) { 583 usb_log_debug2("%d ", kbd_dev->keys_old[i]); 584 } 585 usb_log_debug2("\n"); 553 memcpy(kbd_dev->keys, key_codes, count); 554 555 usb_log_debug("New stored keycodes: %s\n", 556 usb_debug_str_buffer(kbd_dev->keys, kbd_dev->key_count, 0)); 586 557 } 587 558 … … 605 576 * @sa usb_kbd_check_key_changes(), usb_kbd_check_modifier_changes() 606 577 */ 607 //static void usb_kbd_process_keycodes(const uint8_t *key_codes, size_t count,608 //uint8_t report_id, void *arg)609 //{610 //if (arg == NULL) {611 //usb_log_warning("Missing argument in callback "612 //"usbhid_process_keycodes().\n");613 //return;614 //}615 616 //usb_hid_dev_t *hid_dev = (usb_hid_dev_t *)arg;617 618 //if (hid_dev->data == NULL) {619 //usb_log_warning("Missing KBD device structure in callback.\n");620 //return;621 //}622 623 //usb_kbd_t *kbd_dev = (usb_kbd_t *)hid_dev->data;624 625 //usb_log_debug("Got keys from parser (report id: %u): %s\n",626 //report_id, usb_debug_str_buffer(key_codes, count, 0));627 628 //if (count != kbd_dev->key_count) {629 // usb_log_warning("Number of received keycodes (%zu) differs from"630 // " expected (%zu).\n", count, kbd_dev->key_count);631 //return;632 //}633 634 /////usb_kbd_check_modifier_changes(kbd_dev, key_codes, count);635 //usb_kbd_check_key_changes(hid_dev, kbd_dev, key_codes, count);636 //}578 static void usb_kbd_process_keycodes(const uint8_t *key_codes, size_t count, 579 uint8_t report_id, void *arg) 580 { 581 if (arg == NULL) { 582 usb_log_warning("Missing argument in callback " 583 "usbhid_process_keycodes().\n"); 584 return; 585 } 586 587 usb_hid_dev_t *hid_dev = (usb_hid_dev_t *)arg; 588 589 if (hid_dev->data == NULL) { 590 usb_log_warning("Missing KBD device structure in callback.\n"); 591 return; 592 } 593 594 usb_kbd_t *kbd_dev = (usb_kbd_t *)hid_dev->data; 595 596 usb_log_debug("Got keys from parser (report id: %u): %s\n", 597 report_id, usb_debug_str_buffer(key_codes, count, 0)); 598 599 if (count != kbd_dev->key_count) { 600 usb_log_warning("Number of received keycodes (%d) differs from" 601 " expected number (%d).\n", count, kbd_dev->key_count); 602 return; 603 } 604 605 ///usb_kbd_check_modifier_changes(kbd_dev, key_codes, count); 606 usb_kbd_check_key_changes(hid_dev, kbd_dev, key_codes, count); 607 } 637 608 638 609 /*----------------------------------------------------------------------------*/ … … 658 629 uint8_t *buffer, size_t actual_size) 659 630 { 660 assert(hid_dev->report != NULL); 661 assert(hid_dev != NULL); 662 assert(hid_dev->data != NULL); 663 664 usb_kbd_t *kbd_dev = (usb_kbd_t *)hid_dev->data; 631 assert(hid_dev->parser != NULL); 632 633 usb_hid_report_in_callbacks_t *callbacks = 634 (usb_hid_report_in_callbacks_t *)malloc( 635 sizeof(usb_hid_report_in_callbacks_t)); 636 637 callbacks->keyboard = usb_kbd_process_keycodes; 665 638 666 639 usb_log_debug("Calling usb_hid_parse_report() with " … … 671 644 usb_hid_report_path_t *path = usb_hid_report_path(); 672 645 usb_hid_report_path_append_item(path, USB_HIDUT_PAGE_KEYBOARD, 0); 673 //usb_hid_report_path_set_report_id(path, 0); 674 675 uint8_t report_id; 676 int rc = usb_hid_parse_report(hid_dev->report, buffer, actual_size, 677 &report_id); 646 usb_hid_report_path_set_report_id(path, 0); 647 648 int rc = usb_hid_parse_report(hid_dev->parser, buffer, 649 actual_size, path, 650 USB_HID_PATH_COMPARE_END | USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY, 651 callbacks, hid_dev); 652 653 usb_hid_report_path_free(path); 678 654 679 655 if (rc != EOK) { 680 usb_log_warning("Error in usb_hid_ parse_report():"656 usb_log_warning("Error in usb_hid_boot_keyboard_input_report():" 681 657 "%s\n", str_error(rc)); 682 658 } 683 684 usb_hid_report_path_set_report_id (path, report_id);685 686 // fill in the currently pressed keys687 688 usb_hid_report_field_t *field = usb_hid_report_get_sibling(689 hid_dev->report, NULL, path,690 USB_HID_PATH_COMPARE_END | USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY,691 USB_HID_REPORT_TYPE_INPUT);692 unsigned i = 0;693 694 while (field != NULL) {695 usb_log_debug2("FIELD (%p) - VALUE(%d) USAGE(%u)\n",696 field, field->value, field->usage);697 698 assert(i < kbd_dev->key_count);699 // if (i == kbd_dev->key_count) {700 // break;701 // }702 703 // save the key usage704 /* TODO: maybe it's not good to save value, nor usage705 * as the value may be e.g. 1 for LEDs and usage may be706 * value of the LED. On the other hand, in case of normal707 * keys, the usage is more important and we must check708 * that. One possible solution: distinguish between those709 * two parts of the Report somehow.710 */711 if (field->value != 0) {712 kbd_dev->keys[i] = field->usage;713 }714 else {715 kbd_dev->keys[i] = 0;716 }717 usb_log_debug2("Saved %u. key usage %d\n", i, kbd_dev->keys[i]);718 719 ++i;720 field = usb_hid_report_get_sibling(hid_dev->report, field, path,721 USB_HID_PATH_COMPARE_END722 | USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY,723 USB_HID_REPORT_TYPE_INPUT);724 }725 726 usb_hid_report_path_free(path);727 728 usb_kbd_check_key_changes(hid_dev, kbd_dev);729 659 } 730 660 … … 814 744 815 745 kbd_dev->key_count = usb_hid_report_input_length( 816 hid_dev-> report, path,746 hid_dev->parser, path, 817 747 USB_HID_PATH_COMPARE_END | USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY); 818 748 usb_hid_report_path_free(path); … … 820 750 usb_log_debug("Size of the input report: %zu\n", kbd_dev->key_count); 821 751 822 kbd_dev->keys = ( int32_t *)calloc(kbd_dev->key_count, sizeof(int32_t));752 kbd_dev->keys = (uint8_t *)calloc(kbd_dev->key_count, sizeof(uint8_t)); 823 753 824 754 if (kbd_dev->keys == NULL) { … … 828 758 } 829 759 830 kbd_dev->keys_old =831 (int32_t *)calloc(kbd_dev->key_count, sizeof(int32_t));832 833 if (kbd_dev->keys_old == NULL) {834 usb_log_fatal("No memory!\n");835 free(kbd_dev->keys);836 free(kbd_dev);837 return ENOMEM;838 }839 840 760 /* 841 761 * Output report 842 762 */ 843 763 kbd_dev->output_size = 0; 844 kbd_dev->output_buffer = usb_hid_report_output(hid_dev-> report,845 &kbd_dev->output_size , 0);846 if (kbd_dev->output_buffer == NULL ) {764 kbd_dev->output_buffer = usb_hid_report_output(hid_dev->parser, 765 &kbd_dev->output_size); 766 if (kbd_dev->output_buffer == NULL && kbd_dev->output_size != 0) { 847 767 usb_log_warning("Error creating output report buffer.\n"); 848 768 free(kbd_dev->keys); 849 return ENOMEM; /* TODO: other error code */ 769 free(kbd_dev); 770 return ENOMEM; 850 771 } 851 772 … … 856 777 kbd_dev->led_path, USB_HIDUT_PAGE_LED, 0); 857 778 858 kbd_dev->led_output_size = usb_hid_report_output_size(hid_dev-> report,779 kbd_dev->led_output_size = usb_hid_report_output_size(hid_dev->parser, 859 780 kbd_dev->led_path, 860 781 USB_HID_PATH_COMPARE_END | USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY); … … 935 856 /*----------------------------------------------------------------------------*/ 936 857 937 bool usb_kbd_polling_callback(usb_ hid_dev_t *hid_dev, uint8_t *buffer,938 size_t buffer_size )939 { 940 if ( hid_dev == NULL || buffer== NULL) {858 bool usb_kbd_polling_callback(usb_device_t *dev, uint8_t *buffer, 859 size_t buffer_size, void *arg) 860 { 861 if (dev == NULL || buffer == NULL || arg == NULL) { 941 862 // do not continue polling (???) 942 863 return false; 943 864 } 865 866 usb_hid_dev_t *hid_dev = (usb_hid_dev_t *)arg; 944 867 945 868 // TODO: add return value from this function … … 984 907 } 985 908 986 // free all buffers 987 if ((*kbd_dev)->keys != NULL) { 988 free((*kbd_dev)->keys); 989 } 990 if ((*kbd_dev)->keys_old != NULL) { 991 free((*kbd_dev)->keys_old); 992 } 993 if ((*kbd_dev)->led_data != NULL) { 994 free((*kbd_dev)->led_data); 995 } 996 if ((*kbd_dev)->output_buffer != NULL) { 997 free((*kbd_dev)->output_buffer); 998 } 999 if ((*kbd_dev)->led_path != NULL) { 1000 usb_hid_report_path_free((*kbd_dev)->led_path); 1001 } 1002 if ((*kbd_dev)->output_buffer != NULL) { 1003 usb_hid_report_output_free((*kbd_dev)->output_buffer); 1004 } 909 // free the output buffer 910 usb_hid_report_output_free((*kbd_dev)->output_buffer); 1005 911 1006 912 free(*kbd_dev); … … 1010 916 /*----------------------------------------------------------------------------*/ 1011 917 1012 void usb_kbd_deinit( usb_hid_dev_t *hid_dev)918 void usb_kbd_deinit(struct usb_hid_dev_t *hid_dev) 1013 919 { 1014 920 if (hid_dev == NULL) { … … 1022 928 } else { 1023 929 usb_kbd_free(&kbd_dev); 1024 hid_dev->data = NULL;1025 930 } 1026 931 } … … 1031 936 int usb_kbd_set_boot_protocol(usb_hid_dev_t *hid_dev) 1032 937 { 1033 int rc = usb_hid_parse_report_descriptor(hid_dev-> report,938 int rc = usb_hid_parse_report_descriptor(hid_dev->parser, 1034 939 USB_KBD_BOOT_REPORT_DESCRIPTOR, 1035 940 USB_KBD_BOOT_REPORT_DESCRIPTOR_SIZE);
Note:
See TracChangeset
for help on using the changeset viewer.