Ignore:
File:
1 edited

Legend:

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

    r00b13408 rb05e2fe  
    5151#include <usb/classes/hidparser.h>
    5252#include <usb/classes/classes.h>
    53 #include <usb/classes/hidut.h>
    5453
    5554#include "kbddev.h"
     
    9493        .flags = 0
    9594};
    96 
    97 typedef enum usbhid_kbd_flags {
    98         USBHID_KBD_STATUS_UNINITIALIZED = 0,
    99         USBHID_KBD_STATUS_INITIALIZED = 1,
    100         USBHID_KBD_STATUS_TO_DESTROY = -1
    101 } usbhid_kbd_flags;
    10295
    10396/*----------------------------------------------------------------------------*/
     
    131124};
    132125
    133 typedef enum usbhid_lock_code {
    134         USBHID_LOCK_NUM = 0x53,
    135         USBHID_LOCK_CAPS = 0x39,
    136         USBHID_LOCK_SCROLL = 0x47,
    137         USBHID_LOCK_COUNT = 3
    138 } usbhid_lock_code;
    139 
    140 static const usbhid_lock_code usbhid_lock_codes[USBHID_LOCK_COUNT] = {
    141         USBHID_LOCK_NUM,
    142         USBHID_LOCK_CAPS,
    143         USBHID_LOCK_SCROLL
    144 };
    145 
    146126/*----------------------------------------------------------------------------*/
    147127/* IPC method handler                                                         */
     
    238218       
    239219        assert(kbd_dev->hid_dev != NULL);
    240         assert(kbd_dev->hid_dev->initialized == USBHID_KBD_STATUS_INITIALIZED);
     220        assert(kbd_dev->hid_dev->initialized);
    241221        usbhid_req_set_report(kbd_dev->hid_dev, USB_HID_REPORT_TYPE_OUTPUT,
    242222            buffer, BOOTP_BUFFER_OUT_SIZE);
     
    366346 * @sa usbhid_kbd_push_ev()
    367347 */
    368 //static void usbhid_kbd_check_modifier_changes(usbhid_kbd_t *kbd_dev,
    369 //    const uint8_t *key_codes, size_t count)
    370 //{
    371 //      /*
    372 //       * TODO: why the USB keyboard has NUM_, SCROLL_ and CAPS_LOCK
    373 //       *       both as modifiers and as keyUSB_HID_LOCK_COUNTs with their own scancodes???
    374 //       *
    375 //       * modifiers should be sent as normal keys to usbhid_parse_scancode()!!
    376 //       * so maybe it would be better if I received it from report parser in
    377 //       * that way
    378 //       */
    379        
    380 //      int i;
    381 //      for (i = 0; i < count; ++i) {
    382 //              if ((modifiers & usb_hid_modifiers_consts[i]) &&
    383 //                  !(kbd_dev->modifiers & usb_hid_modifiers_consts[i])) {
    384 //                      // modifier pressed
    385 //                      if (usbhid_modifiers_keycodes[i] != 0) {
    386 //                              usbhid_kbd_push_ev(kbd_dev, KEY_PRESS,
    387 //                                  usbhid_modifiers_keycodes[i]);
    388 //                      }
    389 //              } else if (!(modifiers & usb_hid_modifiers_consts[i]) &&
    390 //                  (kbd_dev->modifiers & usb_hid_modifiers_consts[i])) {
    391 //                      // modifier released
    392 //                      if (usbhid_modifiers_keycodes[i] != 0) {
    393 //                              usbhid_kbd_push_ev(kbd_dev, KEY_RELEASE,
    394 //                                  usbhid_modifiers_keycodes[i]);
    395 //                      }
    396 //              }       // no change
    397 //      }
    398        
    399 //      kbd_dev->modifiers = modifiers;
    400 //}
    401 
    402 /*----------------------------------------------------------------------------*/
    403 
    404 static inline int usbhid_kbd_is_lock(unsigned int key_code)
    405 {
    406         return (key_code == KC_NUM_LOCK
    407             || key_code == KC_SCROLL_LOCK
    408             || key_code == KC_CAPS_LOCK);
     348static void usbhid_kbd_check_modifier_changes(usbhid_kbd_t *kbd_dev,
     349    uint8_t modifiers)
     350{
     351        /*
     352         * TODO: why the USB keyboard has NUM_, SCROLL_ and CAPS_LOCK
     353         *       both as modifiers and as keys with their own scancodes???
     354         *
     355         * modifiers should be sent as normal keys to usbhid_parse_scancode()!!
     356         * so maybe it would be better if I received it from report parser in
     357         * that way
     358         */
     359       
     360        int i;
     361        for (i = 0; i < USB_HID_MOD_COUNT; ++i) {
     362                if ((modifiers & usb_hid_modifiers_consts[i]) &&
     363                    !(kbd_dev->modifiers & usb_hid_modifiers_consts[i])) {
     364                        // modifier pressed
     365                        if (usbhid_modifiers_keycodes[i] != 0) {
     366                                usbhid_kbd_push_ev(kbd_dev, KEY_PRESS,
     367                                    usbhid_modifiers_keycodes[i]);
     368                        }
     369                } else if (!(modifiers & usb_hid_modifiers_consts[i]) &&
     370                    (kbd_dev->modifiers & usb_hid_modifiers_consts[i])) {
     371                        // modifier released
     372                        if (usbhid_modifiers_keycodes[i] != 0) {
     373                                usbhid_kbd_push_ev(kbd_dev, KEY_RELEASE,
     374                                    usbhid_modifiers_keycodes[i]);
     375                        }
     376                }       // no change
     377        }
     378       
     379        kbd_dev->modifiers = modifiers;
    409380}
    410381
     
    433404        /*
    434405         * First of all, check if the kbd have reported phantom state.
    435          *
    436          * TODO: this must be changed as we don't know which keys are modifiers
    437          *       and which are regular keys.
    438406         */
    439407        i = 0;
     
    466434                        // not found, i.e. the key was released
    467435                        key = usbhid_parse_scancode(kbd_dev->keys[j]);
    468                         if (!usbhid_kbd_is_lock(key)) {
    469                                 usbhid_kbd_repeat_stop(kbd_dev, key);
    470                         }
     436                        usbhid_kbd_repeat_stop(kbd_dev, key);
    471437                        usbhid_kbd_push_ev(kbd_dev, KEY_RELEASE, key);
    472438                        usb_log_debug2("Key released: %d\n", key);
     
    492458                            key_codes[i]);
    493459                        usbhid_kbd_push_ev(kbd_dev, KEY_PRESS, key);
    494                         if (!usbhid_kbd_is_lock(key)) {
    495                                 usbhid_kbd_repeat_start(kbd_dev, key);
    496                         }
     460                        usbhid_kbd_repeat_start(kbd_dev, key);
    497461                } else {
    498462                        // found, nothing happens
     
    538502
    539503        usb_log_debug("Got keys from parser: %s\n",
    540             usb_debug_str_buffer(key_codes, count, 0));
     504            usb_debug_str_buffer(key_codes, kbd_dev->key_count, 0));
    541505       
    542506        if (count != kbd_dev->key_count) {
     
    546510        }
    547511       
    548         ///usbhid_kbd_check_modifier_changes(kbd_dev, key_codes, count);
     512        usbhid_kbd_check_modifier_changes(kbd_dev, modifiers);
    549513        usbhid_kbd_check_key_changes(kbd_dev, key_codes, count);
    550514}
     
    571535                                    uint8_t *buffer, size_t actual_size)
    572536{
    573         assert(kbd_dev->initialized == USBHID_KBD_STATUS_INITIALIZED);
    574         assert(kbd_dev->hid_dev->parser != NULL);
    575        
    576537        usb_hid_report_in_callbacks_t *callbacks =
    577538            (usb_hid_report_in_callbacks_t *)malloc(
     
    580541        callbacks->keyboard = usbhid_kbd_process_keycodes;
    581542
    582         usb_log_debug("Calling usb_hid_parse_report() with "
     543        usb_log_debug("Calling usb_hid_boot_keyboard_input_report() with "
    583544            "buffer %s\n", usb_debug_str_buffer(buffer, actual_size, 0));
    584545       
    585 //      int rc = usb_hid_boot_keyboard_input_report(buffer, actual_size,
    586 //          callbacks, kbd_dev);
    587         int rc = usb_hid_parse_report(kbd_dev->hid_dev->parser, buffer,
    588             actual_size, callbacks, kbd_dev);
     546        int rc = usb_hid_boot_keyboard_input_report(buffer, actual_size,
     547            callbacks, kbd_dev);
    589548       
    590549        if (rc != EOK) {
     
    625584       
    626585        kbd_dev->console_phone = -1;
    627         kbd_dev->initialized = USBHID_KBD_STATUS_UNINITIALIZED;
     586        kbd_dev->initialized = 0;
    628587       
    629588        return kbd_dev;
     
    631590
    632591/*----------------------------------------------------------------------------*/
    633 
    634 static void usbhid_kbd_mark_unusable(usbhid_kbd_t *kbd_dev)
    635 {
    636         kbd_dev->initialized = USBHID_KBD_STATUS_TO_DESTROY;
     592/**
     593 * Properly destroys the USB/HID keyboard structure.
     594 *
     595 * @param kbd_dev Pointer to the structure to be destroyed.
     596 */
     597static void usbhid_kbd_free(usbhid_kbd_t **kbd_dev)
     598{
     599        if (kbd_dev == NULL || *kbd_dev == NULL) {
     600                return;
     601        }
     602       
     603        // hangup phone to the console
     604        async_hangup((*kbd_dev)->console_phone);
     605       
     606        if ((*kbd_dev)->hid_dev != NULL) {
     607                usbhid_dev_free(&(*kbd_dev)->hid_dev);
     608                assert((*kbd_dev)->hid_dev == NULL);
     609        }
     610       
     611        if ((*kbd_dev)->repeat_mtx != NULL) {
     612                /* TODO: replace by some check and wait */
     613                assert(!fibril_mutex_is_locked((*kbd_dev)->repeat_mtx));
     614                free((*kbd_dev)->repeat_mtx);
     615        }
     616       
     617        free(*kbd_dev);
     618        *kbd_dev = NULL;
    637619}
    638620
     
    676658        }
    677659       
    678         if (kbd_dev->initialized == USBHID_KBD_STATUS_INITIALIZED) {
     660        if (kbd_dev->initialized) {
    679661                usb_log_warning("Keyboard structure already initialized.\n");
    680662                return EINVAL;
     
    689671        }
    690672       
    691         assert(kbd_dev->hid_dev->initialized == USBHID_KBD_STATUS_INITIALIZED);
     673        assert(kbd_dev->hid_dev->initialized);
    692674       
    693675        // save the size of the report (boot protocol report by default)
    694 //      kbd_dev->key_count = BOOTP_REPORT_SIZE;
    695        
    696         usb_hid_report_path_t path;
    697         path.usage_page = USB_HIDUT_PAGE_KEYBOARD;
    698         kbd_dev->key_count = usb_hid_report_input_length(
    699             kbd_dev->hid_dev->parser, &path);
    700        
    701         usb_log_debug("Size of the input report: %zu\n", kbd_dev->key_count);
    702        
     676        kbd_dev->key_count = BOOTP_REPORT_SIZE;
    703677        kbd_dev->keys = (uint8_t *)calloc(
    704678            kbd_dev->key_count, sizeof(uint8_t));
     
    735709        assert(kbd_dev->hid_dev != NULL);
    736710        assert(kbd_dev->hid_dev->initialized);
    737         //usbhid_req_set_protocol(kbd_dev->hid_dev, USB_HID_PROTOCOL_BOOT);
     711        usbhid_req_set_protocol(kbd_dev->hid_dev, USB_HID_PROTOCOL_BOOT);
    738712       
    739713        usbhid_kbd_set_led(kbd_dev);
     
    741715        usbhid_req_set_idle(kbd_dev->hid_dev, IDLE_RATE);
    742716       
    743         kbd_dev->initialized = USBHID_KBD_STATUS_INITIALIZED;
     717        kbd_dev->initialized = 1;
    744718        usb_log_info("HID/KBD device structure initialized.\n");
    745719       
     
    855829        usbhid_kbd_poll(kbd_dev);
    856830       
    857         // as there is another fibril using this device, so we must leave the
    858         // structure to it, but mark it for destroying.
    859         usbhid_kbd_mark_unusable(kbd_dev);
    860831        // at the end, properly destroy the KBD structure
    861 //      usbhid_kbd_free(&kbd_dev);
    862 //      assert(kbd_dev == NULL);
     832        usbhid_kbd_free(&kbd_dev);
     833        assert(kbd_dev == NULL);
    863834
    864835        return EOK;
     
    982953}
    983954
    984 /*----------------------------------------------------------------------------*/
    985 
    986 int usbhid_kbd_is_usable(const usbhid_kbd_t *kbd_dev)
    987 {
    988         return (kbd_dev->initialized == USBHID_KBD_STATUS_INITIALIZED);
    989 }
    990 
    991 /*----------------------------------------------------------------------------*/
    992 /**
    993  * Properly destroys the USB/HID keyboard structure.
    994  *
    995  * @param kbd_dev Pointer to the structure to be destroyed.
    996  */
    997 void usbhid_kbd_free(usbhid_kbd_t **kbd_dev)
    998 {
    999         if (kbd_dev == NULL || *kbd_dev == NULL) {
    1000                 return;
    1001         }
    1002        
    1003         // hangup phone to the console
    1004         async_hangup((*kbd_dev)->console_phone);
    1005        
    1006         if ((*kbd_dev)->hid_dev != NULL) {
    1007                 usbhid_dev_free(&(*kbd_dev)->hid_dev);
    1008                 assert((*kbd_dev)->hid_dev == NULL);
    1009         }
    1010        
    1011         if ((*kbd_dev)->repeat_mtx != NULL) {
    1012                 /* TODO: replace by some check and wait */
    1013                 assert(!fibril_mutex_is_locked((*kbd_dev)->repeat_mtx));
    1014                 free((*kbd_dev)->repeat_mtx);
    1015         }
    1016 
    1017         free(*kbd_dev);
    1018         *kbd_dev = NULL;
    1019 }
    1020 
    1021955/**
    1022956 * @}
Note: See TracChangeset for help on using the changeset viewer.