Ignore:
Timestamp:
2011-11-06T13:58:50Z (12 years ago)
Author:
Jan Vesely <jano.vesely@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
5723ae49
Parents:
cf99c4c
Message:

usbhid: Sanitize kbd routines.

File:
1 edited

Legend:

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

    rcf99c4c r50f2095  
    226226 * @param kbd_dev Keyboard device structure.
    227227 */
    228 static void usb_kbd_set_led(usb_hid_dev_t *hid_dev, usb_kbd_t *kbd_dev) 
     228static void usb_kbd_set_led(usb_hid_dev_t *hid_dev, usb_kbd_t *kbd_dev)
    229229{
    230230        if (kbd_dev->output_size == 0) {
     
    274274        }
    275275
    276         usb_log_debug("Output report buffer: %s\n", 
    277             usb_debug_str_buffer(kbd_dev->output_buffer, kbd_dev->output_size, 
     276        usb_log_debug("Output report buffer: %s\n",
     277            usb_debug_str_buffer(kbd_dev->output_buffer, kbd_dev->output_size,
    278278                0));
    279279
    280         usbhid_req_set_report(&hid_dev->usb_dev->ctrl_pipe, 
    281             hid_dev->usb_dev->interface_no, USB_HID_REPORT_TYPE_OUTPUT, 
     280        usbhid_req_set_report(&hid_dev->usb_dev->ctrl_pipe,
     281            hid_dev->usb_dev->interface_no, USB_HID_REPORT_TYPE_OUTPUT,
    282282            kbd_dev->output_buffer, kbd_dev->output_size);
    283283}
     
    291291 * @param key Key code
    292292 */
    293 void usb_kbd_push_ev(usb_hid_dev_t *hid_dev, usb_kbd_t *kbd_dev, int type, 
     293void usb_kbd_push_ev(usb_hid_dev_t *hid_dev, usb_kbd_t *kbd_dev, int type,
    294294    unsigned int key)
    295295{
     
    308308/*----------------------------------------------------------------------------*/
    309309
    310 static inline int usb_kbd_is_lock(unsigned int key_code) 
     310static inline int usb_kbd_is_lock(unsigned int key_code)
    311311{
    312312        return (key_code == KC_NUM_LOCK
     
    342342 * @sa usb_kbd_push_ev(), usb_kbd_repeat_start(), usb_kbd_repeat_stop()
    343343 */
    344 static void usb_kbd_check_key_changes(usb_hid_dev_t *hid_dev, 
     344static void usb_kbd_check_key_changes(usb_hid_dev_t *hid_dev,
    345345    usb_kbd_t *kbd_dev)
    346346{
     
    405405        memcpy(kbd_dev->keys_old, kbd_dev->keys, kbd_dev->key_count * 4);
    406406
     407        // TODO Get rid of this
    407408        char key_buffer[512];
    408409        ddf_dump_buffer(key_buffer, 512,
     
    438439        usb_hid_report_path_append_item(path, USB_HIDUT_PAGE_KEYBOARD, 0);
    439440
    440         usb_hid_report_path_set_report_id (path, hid_dev->report_id);
     441        usb_hid_report_path_set_report_id(path, hid_dev->report_id);
    441442
    442443        // fill in the currently pressed keys
     
    449450
    450451        while (field != NULL) {
    451                 usb_log_debug2("FIELD (%p) - VALUE(%d) USAGE(%u)\n", 
     452                usb_log_debug2("FIELD (%p) - VALUE(%d) USAGE(%u)\n",
    452453                    field, field->value, field->usage);
    453                
     454
    454455                assert(i < kbd_dev->key_count);
    455456
     
    482483{
    483484        kbd_dev->initialized = USB_KBD_STATUS_TO_DESTROY;
    484 }
    485 
    486 /*----------------------------------------------------------------------------*/
    487 
    488 /**
    489  * Creates a new USB/HID keyboard structure.
    490  *
    491  * The structure returned by this function is not initialized. Use
    492  * usb_kbd_init() to initialize it prior to polling.
    493  *
    494  * @return New uninitialized structure for representing a USB/HID keyboard or
    495  *         NULL if not successful (memory error).
    496  */
    497 static usb_kbd_t *usb_kbd_new(void)
    498 {
    499         usb_kbd_t *kbd_dev =
    500             (usb_kbd_t *)calloc(1, sizeof(usb_kbd_t));
    501 
    502         if (kbd_dev == NULL) {
    503                 usb_log_error("No memory!\n");
    504                 return NULL;
    505         }
    506 
    507         kbd_dev->console_sess = NULL;
    508         kbd_dev->initialized = USB_KBD_STATUS_UNINITIALIZED;
    509 
    510         return kbd_dev;
    511485}
    512486
     
    591565
    592566        if (hid_dev == NULL) {
    593                 usb_log_error("Failed to init keyboard structure: no structure"
    594                     " given.\n");
     567                usb_log_error(
     568                    "Failed to init keyboard structure: no structure given.\n");
    595569                return EINVAL;
    596570        }
    597571
    598         usb_kbd_t *kbd_dev = usb_kbd_new();
     572        usb_kbd_t *kbd_dev = malloc(sizeof(usb_kbd_t));
    599573        if (kbd_dev == NULL) {
    600                 usb_log_error("Error while creating USB/HID KBD device "
    601                     "structure.\n");
    602                 return ENOMEM;  // TODO: some other code??
    603         }
     574                usb_log_error("Failed to allocate KBD device structure.\n");
     575                return ENOMEM;
     576        }
     577        /* Default values */
     578        fibril_mutex_initialize(&kbd_dev->repeat_mtx);
     579        kbd_dev->console_sess = NULL;
     580        kbd_dev->initialized = USB_KBD_STATUS_UNINITIALIZED;
     581        kbd_dev->ops.default_handler = default_connection_handler;
    604582
    605583        /* Store link to HID device */
    606584        kbd_dev->hid_dev = hid_dev;
    607585
    608         /*
    609          * TODO: make more general
    610          */
     586        /* Modifiers and locks */
     587        kbd_dev->modifiers = 0;
     588        kbd_dev->mods = DEFAULT_ACTIVE_MODS;
     589        kbd_dev->lock_keys = 0;
     590
     591        /* Autorepeat */
     592        kbd_dev->repeat.key_new = 0;
     593        kbd_dev->repeat.key_repeated = 0;
     594        kbd_dev->repeat.delay_before = DEFAULT_DELAY_BEFORE_FIRST_REPEAT;
     595        kbd_dev->repeat.delay_between = DEFAULT_REPEAT_DELAY;
     596
     597
     598        // TODO: make more general
    611599        usb_hid_report_path_t *path = usb_hid_report_path();
    612600        usb_hid_report_path_append_item(path, USB_HIDUT_PAGE_KEYBOARD, 0);
     
    621609
    622610        kbd_dev->keys = calloc(kbd_dev->key_count, sizeof(int32_t));
    623 
    624611        if (kbd_dev->keys == NULL) {
    625                 usb_log_error("No memory!\n");
    626612                free(kbd_dev);
    627613                return ENOMEM;
    628614        }
    629615
    630         kbd_dev->keys_old =
    631                 (int32_t *)calloc(kbd_dev->key_count, sizeof(int32_t));
    632 
     616        kbd_dev->keys_old = calloc(kbd_dev->key_count, sizeof(int32_t));
    633617        if (kbd_dev->keys_old == NULL) {
    634                 usb_log_error("No memory!\n");
    635618                free(kbd_dev->keys);
    636619                free(kbd_dev);
     
    638621        }
    639622
    640         /*
    641          * Output report
    642          */
     623        /* Output report */
    643624        kbd_dev->output_size = 0;
    644625        kbd_dev->output_buffer = usb_hid_report_output(&hid_dev->report,
    645626            &kbd_dev->output_size, 0);
    646627        if (kbd_dev->output_buffer == NULL) {
    647                 usb_log_warning("Error creating output report buffer.\n");
     628                usb_log_error("Error creating output report buffer.\n");
     629                free(kbd_dev->keys_old);
    648630                free(kbd_dev->keys);
     631                free(kbd_dev);
    649632                return ENOMEM;
    650633        }
     
    659642            &hid_dev->report, 0, USB_HID_REPORT_TYPE_OUTPUT);
    660643
    661         usb_log_debug("Output report size (in items): %zu\n", 
     644        usb_log_debug("Output report size (in items): %zu\n",
    662645            kbd_dev->led_output_size);
    663646
    664         kbd_dev->led_data = (int32_t *)calloc(
    665             kbd_dev->led_output_size, sizeof(int32_t));
    666 
     647        kbd_dev->led_data = calloc(kbd_dev->led_output_size, sizeof(int32_t));
    667648        if (kbd_dev->led_data == NULL) {
    668                 usb_log_warning("Error creating buffer for LED output report."
    669                     "\n");
     649                usb_log_error("Error creating buffer for LED output report.\n");
     650                usb_hid_report_output_free(kbd_dev->output_buffer);
     651                free(kbd_dev->keys_old);
    670652                free(kbd_dev->keys);
    671                 usb_hid_report_output_free(kbd_dev->output_buffer);
    672653                free(kbd_dev);
    673654                return ENOMEM;
    674655        }
    675656
    676         /*
    677          * Modifiers and locks
    678          */
    679         kbd_dev->modifiers = 0;
    680         kbd_dev->mods = DEFAULT_ACTIVE_MODS;
    681         kbd_dev->lock_keys = 0;
    682 
    683         /*
    684          * Autorepeat
    685          */
    686         kbd_dev->repeat.key_new = 0;
    687         kbd_dev->repeat.key_repeated = 0;
    688         kbd_dev->repeat.delay_before = DEFAULT_DELAY_BEFORE_FIRST_REPEAT;
    689         kbd_dev->repeat.delay_between = DEFAULT_REPEAT_DELAY;
    690 
    691         fibril_mutex_initialize(&kbd_dev->repeat_mtx);
    692 
    693         // save the KBD device structure into the HID device structure
    694         *data = kbd_dev;
    695 
    696         // set handler for incoming calls
    697         kbd_dev->ops.default_handler = default_connection_handler;
    698657
    699658        /*
     
    703662        usb_kbd_set_led(hid_dev, kbd_dev);
    704663
    705         usbhid_req_set_idle(&hid_dev->usb_dev->ctrl_pipe, 
     664        usbhid_req_set_idle(&hid_dev->usb_dev->ctrl_pipe,
    706665            hid_dev->usb_dev->interface_no, IDLE_RATE);
    707666
     
    712671        if (fid == 0) {
    713672                usb_log_error("Failed to start fibril for KBD auto-repeat");
     673                usb_kbd_destroy(kbd_dev);
    714674                return ENOMEM;
    715675        }
     
    726686        }
    727687
     688        /* Save the KBD device structure into the HID device structure. */
     689        *data = kbd_dev;
     690
     691
    728692        return EOK;
    729693}
     
    733697bool usb_kbd_polling_callback(usb_hid_dev_t *hid_dev, void *data)
    734698{
    735         if (hid_dev == NULL/* || buffer == NULL*/ || data == NULL) {
    736                 // do not continue polling (???)
     699        if (hid_dev == NULL || data == NULL) {
     700                /* This means something serious */
    737701                return false;
    738702        }
    739703
    740         usb_kbd_t *kbd_dev = (usb_kbd_t *)data;
    741         assert(kbd_dev != NULL);
    742 
     704        usb_kbd_t *kbd_dev = data;
    743705        // TODO: add return value from this function
    744706        usb_kbd_process_data(hid_dev, kbd_dev);
     
    773735        }
    774736
    775         // hangup session to the console
    776         async_hangup(kbd_dev->console_sess);
     737        /* hangup session to the console. */
     738        if (kbd_dev->console_sess)
     739                async_hangup(kbd_dev->console_sess);
    777740
    778741        //assert(!fibril_mutex_is_locked((*kbd_dev)->repeat_mtx));
     
    789752                usb_hid_report_path_free(kbd_dev->led_path);
    790753        }
    791         if (kbd_dev->output_buffer != NULL) {
    792                 usb_hid_report_output_free(kbd_dev->output_buffer);
    793         }
    794 
    795         if (ddf_fun_unbind(kbd_dev->fun) != EOK) {
    796                 usb_log_warning("Failed to unbind kbd function.\n");
    797         } else {
    798                 usb_log_debug2("%s unbound.\n", kbd_dev->fun->name);
    799                 kbd_dev->fun->driver_data = NULL;
    800                 ddf_fun_destroy(kbd_dev->fun);
    801         }
     754        usb_hid_report_output_free(kbd_dev->output_buffer);
     755
     756        if (kbd_dev->fun) {
     757                if (ddf_fun_unbind(kbd_dev->fun) != EOK) {
     758                        usb_log_warning("Failed to unbind %s.\n",
     759                            kbd_dev->fun->name);
     760                } else {
     761                        usb_log_debug2("%s unbound.\n", kbd_dev->fun->name);
     762                        kbd_dev->fun->driver_data = NULL;
     763                        ddf_fun_destroy(kbd_dev->fun);
     764                }
     765        }
     766        free(kbd_dev);
    802767}
    803768
     
    806771void usb_kbd_deinit(usb_hid_dev_t *hid_dev, void *data)
    807772{
    808         if (hid_dev == NULL) {
    809                 return;
    810         }
    811 
    812773        if (data != NULL) {
    813774                usb_kbd_t *kbd_dev = data;
     
    825786int usb_kbd_set_boot_protocol(usb_hid_dev_t *hid_dev)
    826787{
     788        assert(hid_dev);
    827789        int rc = usb_hid_parse_report_descriptor(
    828790            &hid_dev->report, USB_KBD_BOOT_REPORT_DESCRIPTOR,
Note: See TracChangeset for help on using the changeset viewer.