Fork us on GitHub Follow us on Facebook Follow us on Twitter

Changeset e60436b in mainline


Ignore:
Timestamp:
2011-04-21T19:58:20Z (11 years ago)
Author:
Lubos Slovak <lubos.slovak@…>
Branches:
lfn, master
Children:
36f737a
Parents:
3bcac68
Message:

Properly ported HID driver and KBD subdriver to the new parser API.

Location:
uspace/drv/usbhid
Files:
6 edited

Legend:

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

    r3bcac68 re60436b  
    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 };
     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//};
    185185
    186186/*----------------------------------------------------------------------------*/
     
    320320        // TODO: COMPOSE and KANA
    321321       
    322         usb_log_debug("Creating output report: %s\n", usb_debug_str_buffer ((uint8_t *)kbd_dev->led_data, kbd_dev->led_output_size * 4, 0));
    323 
    324         usb_hid_report_output_set_data(hid_dev->parser, kbd_dev->led_path,
    325                                        USB_HID_PATH_COMPARE_END , kbd_dev->led_data,
    326                                        kbd_dev->led_output_size);
    327         int rc = usb_hid_report_output_translate(hid_dev->parser, 0,
     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,
    328332            kbd_dev->output_buffer, kbd_dev->output_size);
    329333       
     
    333337                return;
    334338        }
     339       
     340        // TODO: output translating does not work!!
    335341       
    336342        usb_log_debug("Output report buffer: %s\n",
     
    485491 */
    486492static 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)
     493    usb_kbd_t *kbd_dev/*, const uint8_t *key_codes, size_t count*/)
    488494{
    489495        unsigned int key;
     
    499505         */
    500506        i = 0;
    501         while (i < count && key_codes[i] != ERROR_ROLLOVER) {
     507        while (i < kbd_dev->key_count && kbd_dev->keys[i] != ERROR_ROLLOVER) {
    502508                ++i;
    503509        }
    504         if (i != count) {
     510        if (i != kbd_dev->key_count) {
    505511                usb_log_debug("Phantom state occured.\n");
    506512                // phantom state, do nothing
    507513                return;
    508514        }
    509        
    510         /* TODO: quite dummy right now, think of better implementation */
    511         assert(count == kbd_dev->key_count);
    512515       
    513516        /*
    514517         * 1) Key releases
    515518         */
    516         for (j = 0; j < count; ++j) {
     519        for (j = 0; j < kbd_dev->key_count; ++j) {
    517520                // try to find the old key in the new key list
    518521                i = 0;
    519522                while (i < kbd_dev->key_count
    520                     && key_codes[i] != kbd_dev->keys[j]) {
     523                    && kbd_dev->keys[i] != kbd_dev->keys_old[j]) {
    521524                        ++i;
    522525                }
    523526               
    524                 if (i == count) {
     527                if (i == kbd_dev->key_count) {
    525528                        // not found, i.e. the key was released
    526                         key = usbhid_parse_scancode(kbd_dev->keys[j]);
     529                        key = usbhid_parse_scancode(kbd_dev->keys_old[j]);
    527530                        if (!usb_kbd_is_lock(key)) {
    528531                                usb_kbd_repeat_stop(kbd_dev, key);
     
    541544                // try to find the new key in the old key list
    542545                j = 0;
    543                 while (j < count && kbd_dev->keys[j] != key_codes[i]) {
     546                while (j < kbd_dev->key_count
     547                    && kbd_dev->keys_old[j] != kbd_dev->keys[i]) {
    544548                        ++j;
    545549                }
    546550               
    547                 if (j == count) {
     551                if (j == kbd_dev->key_count) {
    548552                        // not found, i.e. new key pressed
    549                         key = usbhid_parse_scancode(key_codes[i]);
     553                        key = usbhid_parse_scancode(kbd_dev->keys[i]);
    550554                        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);
     555                            kbd_dev->keys[i]);
     556                        usb_kbd_push_ev(hid_dev, kbd_dev, KEY_PRESS, key);
    554557                        if (!usb_kbd_is_lock(key)) {
    555558                                usb_kbd_repeat_start(kbd_dev, key);
     
    560563        }
    561564       
    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));
     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");
    566585}
    567586
     
    585604 * @sa usb_kbd_check_key_changes(), usb_kbd_check_modifier_changes()
    586605 */
    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 }
     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//}
    617636
    618637/*----------------------------------------------------------------------------*/
     
    638657                                 uint8_t *buffer, size_t actual_size)
    639658{
    640         assert(hid_dev->parser != NULL);
     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;
    641664
    642665        usb_log_debug("Calling usb_hid_parse_report() with "
     
    649672        //usb_hid_report_path_set_report_id(path, 0);
    650673       
    651         int rc = usb_hid_parse_report(hid_dev->parser, buffer, actual_size);   
    652         usb_hid_report_field_t *field = usb_hid_report_get_sibling(hid_dev->parser,
    653                             NULL, path, USB_HID_PATH_COMPARE_END | USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY,
    654                                                 USB_HID_REPORT_TYPE_INPUT);
    655        
    656         while(field != NULL) {
    657                 usb_log_debug("FIELD (%p) - VALUE(%d) USAGE(%u)\n", field, field->value, field->usage);
    658                 field = usb_hid_report_get_sibling(hid_dev->parser, field, path,
    659                                                    USB_HID_PATH_COMPARE_END | USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY,
    660                                                    USB_HID_REPORT_TYPE_INPUT);
    661         }
     674        int rc = usb_hid_parse_report(hid_dev->report, buffer, actual_size);
     675       
     676        if (rc != EOK) {
     677                usb_log_warning("Error in usb_hid_parse_report():"
     678                    "%s\n", str_error(rc));
     679        }
     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);
    662697               
     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        }
    663713       
    664714        usb_hid_report_path_free(path);
    665715       
    666         if (rc != EOK) {
    667                 usb_log_warning("Error in usb_hid_boot_keyboard_input_report():"
    668                     "%s\n", str_error(rc));
    669         }
     716        usb_kbd_check_key_changes(hid_dev, kbd_dev);
    670717}
    671718
     
    755802       
    756803        kbd_dev->key_count = usb_hid_report_input_length(
    757             hid_dev->parser, path,
     804            hid_dev->report, path,
    758805            USB_HID_PATH_COMPARE_END | USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY);
    759806        usb_hid_report_path_free(path);
     
    761808        usb_log_debug("Size of the input report: %zu\n", kbd_dev->key_count);
    762809       
    763         kbd_dev->keys = (uint8_t *)calloc(kbd_dev->key_count, sizeof(uint8_t));
     810        kbd_dev->keys = (int32_t *)calloc(kbd_dev->key_count, sizeof(int32_t));
    764811       
    765812        if (kbd_dev->keys == NULL) {
     
    769816        }
    770817       
     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       
    771828        /*
    772829         * Output report
    773830         */
    774831        kbd_dev->output_size = 0;
    775         kbd_dev->output_buffer = usb_hid_report_output(hid_dev->parser,
    776             &kbd_dev->output_size, 0x00);
     832        kbd_dev->output_buffer = usb_hid_report_output(hid_dev->report,
     833            &kbd_dev->output_size, 0);
    777834        if (kbd_dev->output_buffer == NULL) {
    778835                usb_log_warning("Error creating output report buffer.\n");
     
    787844            kbd_dev->led_path, USB_HIDUT_PAGE_LED, 0);
    788845       
    789         kbd_dev->led_output_size = usb_hid_report_output_size(hid_dev->parser,
     846        kbd_dev->led_output_size = usb_hid_report_output_size(hid_dev->report,
    790847            kbd_dev->led_path,
    791848            USB_HID_PATH_COMPARE_END | USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY);
     
    9441001int usb_kbd_set_boot_protocol(usb_hid_dev_t *hid_dev)
    9451002{
    946         int rc = usb_hid_parse_report_descriptor(hid_dev->parser,
     1003        int rc = usb_hid_parse_report_descriptor(hid_dev->report,
    9471004            USB_KBD_BOOT_REPORT_DESCRIPTOR,
    9481005            USB_KBD_BOOT_REPORT_DESCRIPTOR_SIZE);
  • uspace/drv/usbhid/kbd/kbddev.h

    r3bcac68 re60436b  
    6565 */
    6666typedef struct usb_kbd_t {
     67        /** Previously pressed keys (not translated to key codes). */
     68        int32_t *keys_old;
    6769        /** Currently pressed keys (not translated to key codes). */
    68         uint8_t *keys;
     70        int32_t *keys;
    6971        /** Count of stored keys (i.e. number of keys in the report). */
    7072        size_t key_count;
  • uspace/drv/usbhid/lgtch-ultrax/lgtch-ultrax.c

    r3bcac68 re60436b  
    8383        usb_hid_report_path_set_report_id(path, 0);
    8484       
    85         int rc = usb_hid_parse_report(hid_dev->parser, buffer, buffer_size);
     85        int rc = usb_hid_parse_report(hid_dev->report, buffer, buffer_size);
    8686
    87         usb_hid_report_field_t *field = usb_hid_report_get_sibling(hid_dev->parser, NULL, path, USB_HID_PATH_COMPARE_END , USB_HID_REPORT_TYPE_INPUT);
     87        usb_hid_report_field_t *field = usb_hid_report_get_sibling(hid_dev->report, NULL, path, USB_HID_PATH_COMPARE_END , USB_HID_REPORT_TYPE_INPUT);
    8888        while(field != NULL) {
    8989                usb_log_debug("KEY VALUE(%X) USAGE(%X)\n", field->value, field->usage);
  • uspace/drv/usbhid/mouse/mousedev.c

    r3bcac68 re60436b  
    296296int usb_mouse_set_boot_protocol(usb_hid_dev_t *hid_dev)
    297297{
    298         int rc = usb_hid_parse_report_descriptor(hid_dev->parser,
     298        int rc = usb_hid_parse_report_descriptor(hid_dev->report,
    299299            USB_MOUSE_BOOT_REPORT_DESCRIPTOR,
    300300            USB_MOUSE_BOOT_REPORT_DESCRIPTOR_SIZE);
  • uspace/drv/usbhid/usbhid.c

    r3bcac68 re60436b  
    192192        }
    193193       
    194         assert(hid_dev->parser != NULL);
     194        assert(hid_dev->report != NULL);
    195195       
    196196        usb_log_debug("Compare flags: %d\n", mapping->compare);
    197         size_t size = usb_hid_report_input_length(hid_dev->parser, usage_path,
     197        size_t size = usb_hid_report_input_length(hid_dev->report, usage_path,
    198198            mapping->compare);
    199199        usb_log_debug("Size of the input report: %zuB\n", size);
     
    341341        }
    342342       
    343         hid_dev->parser = (usb_hid_report_t *)(malloc(sizeof(
     343        hid_dev->report = (usb_hid_report_t *)(malloc(sizeof(
    344344            usb_hid_report_t)));
    345         if (hid_dev->parser == NULL) {
     345        if (hid_dev->report == NULL) {
    346346                usb_log_fatal("No memory!\n");
    347347                free(hid_dev);
     
    385385        /* Get the report descriptor and parse it. */
    386386        rc = usb_hid_process_report_descriptor(hid_dev->usb_dev,
    387             hid_dev->parser);
     387            hid_dev->report);
    388388       
    389389        bool fallback = false;
     
    583583
    584584        // destroy the parser
    585         if ((*hid_dev)->parser != NULL) {
    586                 usb_hid_free_report((*hid_dev)->parser);
     585        if ((*hid_dev)->report != NULL) {
     586                usb_hid_free_report((*hid_dev)->report);
    587587        }
    588588
  • uspace/drv/usbhid/usbhid.h

    r3bcac68 re60436b  
    9191       
    9292        /** HID Report parser. */
    93         usb_hid_report_t *parser;
     93        usb_hid_report_t *report;
    9494       
    9595        /** Arbitrary data (e.g. a special structure for handling keyboard). */
Note: See TracChangeset for help on using the changeset viewer.