Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/drv/usbhid/kbd/kbddev.c

    re60436b rcfbbe1d3  
    177177/*----------------------------------------------------------------------------*/
    178178
    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 //};
     179static void usb_kbd_process_keycodes(const uint8_t *key_codes, size_t count,
     180    uint8_t report_id, void *arg);
     181
     182static const usb_hid_report_in_callbacks_t usb_kbd_parser_callbacks = {
     183        .keyboard = usb_kbd_process_keycodes
     184};
    185185
    186186/*----------------------------------------------------------------------------*/
     
    299299                return;
    300300        }
    301        
    302         unsigned i = 0;
    303        
     301               
    304302        /* Reset the LED data. */
    305303        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: ");
    323         for (i = 0; i < kbd_dev->led_output_size; ++i) {
    324                 usb_log_debug("%d ", kbd_dev->led_data[i]);
    325         }
    326         usb_log_debug("\n");
    327 
    328         usb_hid_report_output_set_data(hid_dev->report, kbd_dev->led_path,
    329             USB_HID_PATH_COMPARE_END | USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY,
    330             kbd_dev->led_data, kbd_dev->led_output_size);
    331         int rc = usb_hid_report_output_translate(hid_dev->report, 0,
     304        usb_log_debug("Creating output report:\n");
     305
     306        usb_hid_report_field_t *field = usb_hid_report_get_sibling (hid_dev->parser, NULL,
     307                kbd_dev->led_path,
     308                                USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY | USB_HID_PATH_COMPARE_END,
     309                                USB_HID_REPORT_TYPE_OUTPUT);
     310        while(field != NULL) {
     311
     312                if((field->usage == USB_HID_LED_NUM_LOCK) && (kbd_dev->mods & KM_NUM_LOCK)){
     313                        field->value = 1;
     314                }
     315
     316                if((field->usage == USB_HID_LED_CAPS_LOCK) && (kbd_dev->mods & KM_CAPS_LOCK)){
     317                        field->value = 1;
     318                }
     319
     320                if((field->usage == USB_HID_LED_SCROLL_LOCK) && (kbd_dev->mods & KM_SCROLL_LOCK)){
     321                        field->value = 1;
     322                }
     323               
     324                field = usb_hid_report_get_sibling (hid_dev->parser, field,
     325                kbd_dev->led_path,
     326                                USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY | USB_HID_PATH_COMPARE_END,
     327                                USB_HID_REPORT_TYPE_OUTPUT);
     328        }
     329               
     330        int rc = usb_hid_report_output_translate(hid_dev->parser, 0,
    332331            kbd_dev->output_buffer, kbd_dev->output_size);
    333332       
     
    337336                return;
    338337        }
    339        
    340         // TODO: output translating does not work!!
    341338       
    342339        usb_log_debug("Output report buffer: %s\n",
     
    491488 */
    492489static void usb_kbd_check_key_changes(usb_hid_dev_t *hid_dev,
    493     usb_kbd_t *kbd_dev/*, const uint8_t *key_codes, size_t count*/)
     490    usb_kbd_t *kbd_dev, const uint8_t *key_codes, size_t count)
    494491{
    495492        unsigned int key;
     
    505502         */
    506503        i = 0;
    507         while (i < kbd_dev->key_count && kbd_dev->keys[i] != ERROR_ROLLOVER) {
     504        while (i < count && key_codes[i] != ERROR_ROLLOVER) {
    508505                ++i;
    509506        }
    510         if (i != kbd_dev->key_count) {
     507        if (i != count) {
    511508                usb_log_debug("Phantom state occured.\n");
    512509                // phantom state, do nothing
    513510                return;
    514511        }
     512       
     513        /* TODO: quite dummy right now, think of better implementation */
     514        assert(count == kbd_dev->key_count);
    515515       
    516516        /*
    517517         * 1) Key releases
    518518         */
    519         for (j = 0; j < kbd_dev->key_count; ++j) {
     519        for (j = 0; j < count; ++j) {
    520520                // try to find the old key in the new key list
    521521                i = 0;
    522522                while (i < kbd_dev->key_count
    523                     && kbd_dev->keys[i] != kbd_dev->keys_old[j]) {
     523                    && key_codes[i] != kbd_dev->keys[j]) {
    524524                        ++i;
    525525                }
    526526               
    527                 if (i == kbd_dev->key_count) {
     527                if (i == count) {
    528528                        // not found, i.e. the key was released
    529                         key = usbhid_parse_scancode(kbd_dev->keys_old[j]);
     529                        key = usbhid_parse_scancode(kbd_dev->keys[j]);
    530530                        if (!usb_kbd_is_lock(key)) {
    531531                                usb_kbd_repeat_stop(kbd_dev, key);
     
    544544                // try to find the new key in the old key list
    545545                j = 0;
    546                 while (j < kbd_dev->key_count
    547                     && kbd_dev->keys_old[j] != kbd_dev->keys[i]) {
     546                while (j < count && kbd_dev->keys[j] != key_codes[i]) {
    548547                        ++j;
    549548                }
    550549               
    551                 if (j == kbd_dev->key_count) {
     550                if (j == count) {
    552551                        // not found, i.e. new key pressed
    553                         key = usbhid_parse_scancode(kbd_dev->keys[i]);
     552                        key = usbhid_parse_scancode(key_codes[i]);
    554553                        usb_log_debug2("Key pressed: %d (keycode: %d)\n", key,
    555                             kbd_dev->keys[i]);
    556                         usb_kbd_push_ev(hid_dev, kbd_dev, KEY_PRESS, key);
     554                            key_codes[i]);
     555                        usb_kbd_push_ev(hid_dev, kbd_dev, KEY_PRESS,
     556                            key);
    557557                        if (!usb_kbd_is_lock(key)) {
    558558                                usb_kbd_repeat_start(kbd_dev, key);
     
    563563        }
    564564       
    565 //      usb_log_debug("Old keys: ");
    566 //      for (i = 0; i < kbd_dev->key_count; ++i) {
    567 //              usb_log_debug("%d ", kbd_dev->keys_old[i]);
    568 //      }
    569 //      usb_log_debug("\n");
    570        
    571        
    572 //      usb_log_debug("New keys: ");
    573 //      for (i = 0; i < kbd_dev->key_count; ++i) {
    574 //              usb_log_debug("%d ", kbd_dev->keys[i]);
    575 //      }
    576 //      usb_log_debug("\n");
    577        
    578         memcpy(kbd_dev->keys_old, kbd_dev->keys, kbd_dev->key_count * 4);
    579        
    580         usb_log_debug2("New stored keys: ");
    581         for (i = 0; i < kbd_dev->key_count; ++i) {
    582                 usb_log_debug2("%d ", kbd_dev->keys_old[i]);
    583         }
    584         usb_log_debug2("\n");
     565        memcpy(kbd_dev->keys, key_codes, count);
     566
     567        usb_log_debug("New stored keycodes: %s\n",
     568            usb_debug_str_buffer(kbd_dev->keys, kbd_dev->key_count, 0));
    585569}
    586570
     
    604588 * @sa usb_kbd_check_key_changes(), usb_kbd_check_modifier_changes()
    605589 */
    606 //static void usb_kbd_process_keycodes(const uint8_t *key_codes, size_t count,
    607 //    uint8_t report_id, void *arg)
    608 //{
    609 //      if (arg == NULL) {
    610 //              usb_log_warning("Missing argument in callback "
    611 //                  "usbhid_process_keycodes().\n");
    612 //              return;
    613 //      }
    614        
    615 //      usb_hid_dev_t *hid_dev = (usb_hid_dev_t *)arg;
    616        
    617 //      if (hid_dev->data == NULL) {
    618 //              usb_log_warning("Missing KBD device structure in callback.\n");
    619 //              return;
    620 //      }
    621        
    622 //      usb_kbd_t *kbd_dev = (usb_kbd_t *)hid_dev->data;
    623 
    624 //      usb_log_debug("Got keys from parser (report id: %u): %s\n",
    625 //          report_id, usb_debug_str_buffer(key_codes, count, 0));
    626        
    627 //      if (count != kbd_dev->key_count) {
    628 //              usb_log_warning("Number of received keycodes (%zu) differs from"
    629 //                  " expected (%zu).\n", count, kbd_dev->key_count);
    630 //              return;
    631 //      }
    632        
    633 //      ///usb_kbd_check_modifier_changes(kbd_dev, key_codes, count);
    634 //      usb_kbd_check_key_changes(hid_dev, kbd_dev, key_codes, count);
    635 //}
     590static void usb_kbd_process_keycodes(const uint8_t *key_codes, size_t count,
     591    uint8_t report_id, void *arg)
     592{
     593        if (arg == NULL) {
     594                usb_log_warning("Missing argument in callback "
     595                    "usbhid_process_keycodes().\n");
     596                return;
     597        }
     598       
     599        usb_hid_dev_t *hid_dev = (usb_hid_dev_t *)arg;
     600       
     601        if (hid_dev->data == NULL) {
     602                usb_log_warning("Missing KBD device structure in callback.\n");
     603                return;
     604        }
     605       
     606        usb_kbd_t *kbd_dev = (usb_kbd_t *)hid_dev->data;
     607
     608        usb_log_debug("Got keys from parser (report id: %u): %s\n",
     609            report_id, usb_debug_str_buffer(key_codes, count, 0));
     610       
     611        if (count != kbd_dev->key_count) {
     612                usb_log_warning("Number of received keycodes (%d) differs from"
     613                    " expected number (%d).\n", count, kbd_dev->key_count);
     614                return;
     615        }
     616       
     617        ///usb_kbd_check_modifier_changes(kbd_dev, key_codes, count);
     618        usb_kbd_check_key_changes(hid_dev, kbd_dev, key_codes, count);
     619}
    636620
    637621/*----------------------------------------------------------------------------*/
     
    657641                                 uint8_t *buffer, size_t actual_size)
    658642{
    659         assert(hid_dev->report != NULL);
    660         assert(hid_dev != NULL);
    661         assert(hid_dev->data != NULL);
    662        
    663         usb_kbd_t *kbd_dev = (usb_kbd_t *)hid_dev->data;
     643        assert(hid_dev->parser != NULL);
    664644
    665645        usb_log_debug("Calling usb_hid_parse_report() with "
     
    671651        usb_hid_report_path_append_item(path, USB_HIDUT_PAGE_KEYBOARD, 0);
    672652        //usb_hid_report_path_set_report_id(path, 0);
    673        
    674         int rc = usb_hid_parse_report(hid_dev->report, buffer, actual_size);
     653
     654        uint8_t report_id;
     655        int rc = usb_hid_parse_report(hid_dev->parser, buffer, actual_size, &report_id);       
     656        usb_hid_report_path_set_report_id (path, report_id);
     657        usb_hid_report_field_t *field = usb_hid_report_get_sibling(hid_dev->parser,
     658                            NULL, path, USB_HID_PATH_COMPARE_END | USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY,
     659                                                USB_HID_REPORT_TYPE_INPUT);
     660
     661        unsigned i=0;
     662        usb_kbd_t *kbd_dev = (usb_kbd_t *)hid_dev->data;
     663
     664        memset(kbd_dev->keys, 0, kbd_dev->key_count);
     665        while(field != NULL) {
     666                if((i < kbd_dev->key_count) && (field->value != 0)){
     667                        kbd_dev->keys[i++] = (uint8_t)field->usage;
     668                }
     669                field = usb_hid_report_get_sibling(hid_dev->parser, field, path,
     670                                                   USB_HID_PATH_COMPARE_END | USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY,
     671                                                   USB_HID_REPORT_TYPE_INPUT);
     672        }
     673        usb_kbd_process_keycodes(kbd_dev->keys, kbd_dev->key_count, report_id, hid_dev);
     674       
     675        usb_hid_report_path_free(path);
    675676       
    676677        if (rc != EOK) {
    677                 usb_log_warning("Error in usb_hid_parse_report():"
     678                usb_log_warning("Error in usb_hid_boot_keyboard_input_report():"
    678679                    "%s\n", str_error(rc));
    679680        }
    680        
    681         // fill in the currently pressed keys
    682        
    683         usb_hid_report_field_t *field = usb_hid_report_get_sibling(
    684             hid_dev->report, NULL, path, USB_HID_PATH_COMPARE_END
    685             | USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY, USB_HID_REPORT_TYPE_INPUT);
    686         unsigned i = 0;
    687        
    688         // TODO: remove this hack - skipping first field
    689         field = usb_hid_report_get_sibling(hid_dev->report, field, path,
    690             USB_HID_PATH_COMPARE_END
    691             | USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY,
    692             USB_HID_REPORT_TYPE_INPUT);
    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                 kbd_dev->keys[i] = field->usage;
    705                 usb_log_debug2("Saved %u. key usage %d\n", i, kbd_dev->keys[i]);
    706                
    707                 ++i;
    708                 field = usb_hid_report_get_sibling(hid_dev->report, field, path,
    709                     USB_HID_PATH_COMPARE_END
    710                     | USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY,
    711                     USB_HID_REPORT_TYPE_INPUT);
    712         }
    713        
    714         usb_hid_report_path_free(path);
    715        
    716         usb_kbd_check_key_changes(hid_dev, kbd_dev);
    717681}
    718682
     
    802766       
    803767        kbd_dev->key_count = usb_hid_report_input_length(
    804             hid_dev->report, path,
     768            hid_dev->parser, path,
    805769            USB_HID_PATH_COMPARE_END | USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY);
    806770        usb_hid_report_path_free(path);
     
    808772        usb_log_debug("Size of the input report: %zu\n", kbd_dev->key_count);
    809773       
    810         kbd_dev->keys = (int32_t *)calloc(kbd_dev->key_count, sizeof(int32_t));
     774        kbd_dev->keys = (uint8_t *)calloc(kbd_dev->key_count, sizeof(uint8_t));
    811775       
    812776        if (kbd_dev->keys == NULL) {
     
    816780        }
    817781       
    818         kbd_dev->keys_old =
    819                 (int32_t *)calloc(kbd_dev->key_count, sizeof(int32_t));
    820        
    821         if (kbd_dev->keys_old == NULL) {
    822                 usb_log_fatal("No memory!\n");
    823                 free(kbd_dev->keys);
    824                 free(kbd_dev);
    825                 return ENOMEM;
    826         }
    827        
    828782        /*
    829783         * Output report
    830784         */
    831785        kbd_dev->output_size = 0;
    832         kbd_dev->output_buffer = usb_hid_report_output(hid_dev->report,
    833             &kbd_dev->output_size, 0);
     786        kbd_dev->output_buffer = usb_hid_report_output(hid_dev->parser,
     787            &kbd_dev->output_size, 0x00);
    834788        if (kbd_dev->output_buffer == NULL) {
    835789                usb_log_warning("Error creating output report buffer.\n");
     
    844798            kbd_dev->led_path, USB_HIDUT_PAGE_LED, 0);
    845799       
    846         kbd_dev->led_output_size = usb_hid_report_output_size(hid_dev->report,
     800        kbd_dev->led_output_size = usb_hid_report_output_size(hid_dev->parser,
    847801            kbd_dev->led_path,
    848802            USB_HID_PATH_COMPARE_END | USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY);
     
    1001955int usb_kbd_set_boot_protocol(usb_hid_dev_t *hid_dev)
    1002956{
    1003         int rc = usb_hid_parse_report_descriptor(hid_dev->report,
     957        int rc = usb_hid_parse_report_descriptor(hid_dev->parser,
    1004958            USB_KBD_BOOT_REPORT_DESCRIPTOR,
    1005959            USB_KBD_BOOT_REPORT_DESCRIPTOR_SIZE);
Note: See TracChangeset for help on using the changeset viewer.