Changes in uspace/drv/usbhid/kbd/kbddev.c [4125b7d:310c4df] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/drv/usbhid/kbd/kbddev.c
r4125b7d r310c4df 177 177 /*----------------------------------------------------------------------------*/ 178 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 };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_keycodes 184 //}; 185 185 186 186 /*----------------------------------------------------------------------------*/ … … 203 203 /*----------------------------------------------------------------------------*/ 204 204 /** 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 };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 = 3 221 //} 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_SCROLL 227 //}; 228 228 229 229 /*----------------------------------------------------------------------------*/ … … 299 299 return; 300 300 } 301 302 unsigned i = 0; 303 301 304 302 /* Reset the LED data. */ 305 303 memset(kbd_dev->led_data, 0, kbd_dev->led_output_size * sizeof(int32_t)); 306 307 if ((kbd_dev->mods & KM_NUM_LOCK) && (i < kbd_dev->led_output_size)) { 308 kbd_dev->led_data[i++] = USB_HID_LED_NUM_LOCK; 309 } 310 311 if ((kbd_dev->mods & KM_CAPS_LOCK) && (i < kbd_dev->led_output_size)) { 312 kbd_dev->led_data[i++] = USB_HID_LED_CAPS_LOCK; 313 } 314 315 if ((kbd_dev->mods & KM_SCROLL_LOCK) 316 && (i < kbd_dev->led_output_size)) { 317 kbd_dev->led_data[i++] = USB_HID_LED_SCROLL_LOCK; 318 } 319 320 // TODO: COMPOSE and KANA 321 322 usb_log_debug("Creating output report.\n"); 323 324 int rc = usb_hid_report_output_translate(hid_dev->parser, 325 kbd_dev->led_path, 326 USB_HID_PATH_COMPARE_END | USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY, 327 kbd_dev->output_buffer, 328 kbd_dev->output_size, kbd_dev->led_data, kbd_dev->led_output_size); 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); 329 336 330 337 if (rc != EOK) { … … 485 492 */ 486 493 static void usb_kbd_check_key_changes(usb_hid_dev_t *hid_dev, 487 usb_kbd_t *kbd_dev , const uint8_t *key_codes, size_t count)494 usb_kbd_t *kbd_dev/*, const uint8_t *key_codes, size_t count*/) 488 495 { 489 496 unsigned int key; … … 499 506 */ 500 507 i = 0; 501 while (i < count && key_codes[i] != ERROR_ROLLOVER) {508 while (i < kbd_dev->key_count && kbd_dev->keys[i] != ERROR_ROLLOVER) { 502 509 ++i; 503 510 } 504 if (i != count) {511 if (i != kbd_dev->key_count) { 505 512 usb_log_debug("Phantom state occured.\n"); 506 513 // phantom state, do nothing … … 508 515 } 509 516 510 /* TODO: quite dummy right now, think of better implementation */511 assert(count == kbd_dev->key_count);512 513 517 /* 514 518 * 1) Key releases 515 519 */ 516 for (j = 0; j < count; ++j) {520 for (j = 0; j < kbd_dev->key_count; ++j) { 517 521 // try to find the old key in the new key list 518 522 i = 0; 519 523 while (i < kbd_dev->key_count 520 && k ey_codes[i] != kbd_dev->keys[j]) {524 && kbd_dev->keys[i] != kbd_dev->keys_old[j]) { 521 525 ++i; 522 526 } 523 527 524 if (i == count) {528 if (i == kbd_dev->key_count) { 525 529 // not found, i.e. the key was released 526 key = usbhid_parse_scancode(kbd_dev->keys [j]);530 key = usbhid_parse_scancode(kbd_dev->keys_old[j]); 527 531 if (!usb_kbd_is_lock(key)) { 528 532 usb_kbd_repeat_stop(kbd_dev, key); … … 541 545 // try to find the new key in the old key list 542 546 j = 0; 543 while (j < count && kbd_dev->keys[j] != key_codes[i]) { 547 while (j < kbd_dev->key_count 548 && kbd_dev->keys_old[j] != kbd_dev->keys[i]) { 544 549 ++j; 545 550 } 546 551 547 if (j == count) {552 if (j == kbd_dev->key_count) { 548 553 // not found, i.e. new key pressed 549 key = usbhid_parse_scancode(k ey_codes[i]);554 key = usbhid_parse_scancode(kbd_dev->keys[i]); 550 555 usb_log_debug2("Key pressed: %d (keycode: %d)\n", key, 551 key_codes[i]); 552 usb_kbd_push_ev(hid_dev, kbd_dev, KEY_PRESS, 553 key); 556 kbd_dev->keys[i]); 557 usb_kbd_push_ev(hid_dev, kbd_dev, KEY_PRESS, key); 554 558 if (!usb_kbd_is_lock(key)) { 555 559 usb_kbd_repeat_start(kbd_dev, key); … … 560 564 } 561 565 562 memcpy(kbd_dev->keys, key_codes, count); 563 564 usb_log_debug("New stored keycodes: %s\n", 565 usb_debug_str_buffer(kbd_dev->keys, kbd_dev->key_count, 0)); 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"); 566 586 } 567 587 … … 585 605 * @sa usb_kbd_check_key_changes(), usb_kbd_check_modifier_changes() 586 606 */ 587 static void usb_kbd_process_keycodes(const uint8_t *key_codes, size_t count,588 uint8_t report_id, void *arg)589 {590 if (arg == NULL) {591 usb_log_warning("Missing argument in callback "592 "usbhid_process_keycodes().\n");593 return;594 }595 596 usb_hid_dev_t *hid_dev = (usb_hid_dev_t *)arg;597 598 if (hid_dev->data == NULL) {599 usb_log_warning("Missing KBD device structure in callback.\n");600 return;601 }602 603 usb_kbd_t *kbd_dev = (usb_kbd_t *)hid_dev->data;604 605 usb_log_debug("Got keys from parser (report id: %u): %s\n",606 report_id, usb_debug_str_buffer(key_codes, count, 0));607 608 if (count != kbd_dev->key_count) {609 usb_log_warning("Number of received keycodes (%zu) differs from"610 " expected (%zu).\n", count, kbd_dev->key_count);611 return;612 }613 614 ///usb_kbd_check_modifier_changes(kbd_dev, key_codes, count);615 usb_kbd_check_key_changes(hid_dev, kbd_dev, key_codes, count);616 }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 //} 617 637 618 638 /*----------------------------------------------------------------------------*/ … … 638 658 uint8_t *buffer, size_t actual_size) 639 659 { 640 assert(hid_dev->parser != NULL); 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; 641 665 642 666 usb_log_debug("Calling usb_hid_parse_report() with " … … 648 672 usb_hid_report_path_append_item(path, USB_HIDUT_PAGE_KEYBOARD, 0); 649 673 //usb_hid_report_path_set_report_id(path, 0); 650 651 int rc = usb_hid_parse_report(hid_dev->parser, buffer, 652 actual_size, path, 674 675 uint8_t report_id; 676 int rc = usb_hid_parse_report(hid_dev->report, buffer, actual_size, 677 &report_id); 678 679 if (rc != EOK) { 680 usb_log_warning("Error in usb_hid_parse_report():" 681 "%s\n", str_error(rc)); 682 } 683 684 usb_hid_report_path_set_report_id (path, report_id); 685 686 // fill in the currently pressed keys 687 688 usb_hid_report_field_t *field = usb_hid_report_get_sibling( 689 hid_dev->report, NULL, path, 653 690 USB_HID_PATH_COMPARE_END | USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY, 654 &usb_kbd_parser_callbacks, hid_dev); 655 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 usage 704 /* TODO: maybe it's not good to save value, nor usage 705 * as the value may be e.g. 1 for LEDs and usage may be 706 * value of the LED. On the other hand, in case of normal 707 * keys, the usage is more important and we must check 708 * that. One possible solution: distinguish between those 709 * 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_END 722 | USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY, 723 USB_HID_REPORT_TYPE_INPUT); 724 } 725 656 726 usb_hid_report_path_free(path); 657 727 658 if (rc != EOK) { 659 usb_log_warning("Error in usb_hid_boot_keyboard_input_report():" 660 "%s\n", str_error(rc)); 661 } 728 usb_kbd_check_key_changes(hid_dev, kbd_dev); 662 729 } 663 730 … … 696 763 697 764 return kbd_dev; 765 } 766 767 /*----------------------------------------------------------------------------*/ 768 769 static int usb_kbd_create_function(usb_hid_dev_t *hid_dev) 770 { 771 assert(hid_dev != NULL); 772 assert(hid_dev->usb_dev != NULL); 773 774 /* Create the function exposed under /dev/devices. */ 775 usb_log_debug("Creating DDF function %s...\n", HID_KBD_FUN_NAME); 776 ddf_fun_t *fun = ddf_fun_create(hid_dev->usb_dev->ddf_dev, fun_exposed, 777 HID_KBD_FUN_NAME); 778 if (fun == NULL) { 779 usb_log_error("Could not create DDF function node.\n"); 780 return ENOMEM; 781 } 782 783 /* 784 * Store the initialized HID device and HID ops 785 * to the DDF function. 786 */ 787 fun->ops = &hid_dev->ops; 788 fun->driver_data = hid_dev; // TODO: maybe change to hid_dev->data 789 790 int rc = ddf_fun_bind(fun); 791 if (rc != EOK) { 792 usb_log_error("Could not bind DDF function: %s.\n", 793 str_error(rc)); 794 ddf_fun_destroy(fun); 795 return rc; 796 } 797 798 usb_log_debug("Adding DDF function to class %s...\n", 799 HID_KBD_CLASS_NAME); 800 rc = ddf_fun_add_to_class(fun, HID_KBD_CLASS_NAME); 801 if (rc != EOK) { 802 usb_log_error( 803 "Could not add DDF function to class %s: %s.\n", 804 HID_KBD_CLASS_NAME, str_error(rc)); 805 ddf_fun_destroy(fun); 806 return rc; 807 } 808 809 return EOK; 698 810 } 699 811 … … 747 859 748 860 kbd_dev->key_count = usb_hid_report_input_length( 749 hid_dev-> parser, path,861 hid_dev->report, path, 750 862 USB_HID_PATH_COMPARE_END | USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY); 751 863 usb_hid_report_path_free(path); … … 753 865 usb_log_debug("Size of the input report: %zu\n", kbd_dev->key_count); 754 866 755 kbd_dev->keys = ( uint8_t *)calloc(kbd_dev->key_count, sizeof(uint8_t));867 kbd_dev->keys = (int32_t *)calloc(kbd_dev->key_count, sizeof(int32_t)); 756 868 757 869 if (kbd_dev->keys == NULL) { … … 761 873 } 762 874 875 kbd_dev->keys_old = 876 (int32_t *)calloc(kbd_dev->key_count, sizeof(int32_t)); 877 878 if (kbd_dev->keys_old == NULL) { 879 usb_log_fatal("No memory!\n"); 880 free(kbd_dev->keys); 881 free(kbd_dev); 882 return ENOMEM; 883 } 884 763 885 /* 764 886 * Output report 765 887 */ 766 888 kbd_dev->output_size = 0; 767 kbd_dev->output_buffer = usb_hid_report_output(hid_dev-> parser,768 &kbd_dev->output_size );769 if (kbd_dev->output_buffer == NULL && kbd_dev->output_size != 0) {889 kbd_dev->output_buffer = usb_hid_report_output(hid_dev->report, 890 &kbd_dev->output_size, 0); 891 if (kbd_dev->output_buffer == NULL) { 770 892 usb_log_warning("Error creating output report buffer.\n"); 771 893 free(kbd_dev->keys); 772 free(kbd_dev); 773 return ENOMEM; 894 return ENOMEM; /* TODO: other error code */ 774 895 } 775 896 … … 780 901 kbd_dev->led_path, USB_HIDUT_PAGE_LED, 0); 781 902 782 kbd_dev->led_output_size = usb_hid_report_output_size(hid_dev-> parser,903 kbd_dev->led_output_size = usb_hid_report_output_size(hid_dev->report, 783 904 kbd_dev->led_path, 784 905 USB_HID_PATH_COMPARE_END | USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY); … … 854 975 usb_log_debug("HID/KBD device structure initialized.\n"); 855 976 977 usb_log_debug("Creating KBD function...\n"); 978 int rc = usb_kbd_create_function(hid_dev); 979 if (rc != EOK) { 980 usb_kbd_free(&kbd_dev); 981 return rc; 982 } 983 856 984 return EOK; 857 985 } … … 908 1036 } 909 1037 910 // free the output buffer 911 usb_hid_report_output_free((*kbd_dev)->output_buffer); 1038 // free all buffers 1039 if ((*kbd_dev)->keys != NULL) { 1040 free((*kbd_dev)->keys); 1041 } 1042 if ((*kbd_dev)->keys_old != NULL) { 1043 free((*kbd_dev)->keys_old); 1044 } 1045 if ((*kbd_dev)->led_data != NULL) { 1046 free((*kbd_dev)->led_data); 1047 } 1048 if ((*kbd_dev)->led_path != NULL) { 1049 usb_hid_report_path_free((*kbd_dev)->led_path); 1050 } 1051 if ((*kbd_dev)->output_buffer != NULL) { 1052 usb_hid_report_output_free((*kbd_dev)->output_buffer); 1053 } 912 1054 913 1055 free(*kbd_dev); … … 929 1071 } else { 930 1072 usb_kbd_free(&kbd_dev); 1073 hid_dev->data = NULL; 931 1074 } 932 1075 } … … 937 1080 int usb_kbd_set_boot_protocol(usb_hid_dev_t *hid_dev) 938 1081 { 939 int rc = usb_hid_parse_report_descriptor(hid_dev-> parser,1082 int rc = usb_hid_parse_report_descriptor(hid_dev->report, 940 1083 USB_KBD_BOOT_REPORT_DESCRIPTOR, 941 1084 USB_KBD_BOOT_REPORT_DESCRIPTOR_SIZE);
Note:
See TracChangeset
for help on using the changeset viewer.