Ignore:
Timestamp:
2011-07-13T22:39:18Z (13 years ago)
Author:
Jiří Zárevúcky <zarevucky.jiri@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
e6910c8
Parents:
5974661 (diff), 8ecef91 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge libposix.

File:
1 moved

Legend:

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

    r5974661 re4f8c77  
    4040
    4141#include <io/keycode.h>
    42 #include <ipc/kbd.h>
     42#include <io/console.h>
     43#include <ipc/kbdev.h>
    4344#include <async.h>
    4445#include <async_obsolete.h>
    4546#include <fibril.h>
    4647#include <fibril_synch.h>
     48
     49#include <ddf/log.h>
    4750
    4851#include <usb/usb.h>
     
    6366#include "kbddev.h"
    6467
    65 #include "layout.h"
    6668#include "conv.h"
    6769#include "kbdrepeat.h"
     
    7375
    7476/*----------------------------------------------------------------------------*/
    75 /** Default modifiers when the keyboard is initialized. */
     77
    7678static const unsigned DEFAULT_ACTIVE_MODS = KM_NUM_LOCK;
    7779
     
    101103const char *HID_KBD_FUN_NAME = "keyboard";
    102104const char *HID_KBD_CLASS_NAME = "keyboard";
     105
     106static void usb_kbd_set_led(usb_hid_dev_t *hid_dev, usb_kbd_t *kbd_dev);
    103107
    104108/*----------------------------------------------------------------------------*/
     
    154158
    155159/*----------------------------------------------------------------------------*/
    156 /* Keyboard layouts                                                           */
    157 /*----------------------------------------------------------------------------*/
    158 
    159 #define NUM_LAYOUTS 3
    160 
    161 /** Keyboard layout map. */
    162 static layout_op_t *layout[NUM_LAYOUTS] = {
    163         &us_qwerty_op,
    164         &us_dvorak_op,
    165         &cz_op
    166 };
    167 
    168 static int active_layout = 0;
    169 
    170 /*----------------------------------------------------------------------------*/
    171160/* IPC method handler                                                         */
    172161/*----------------------------------------------------------------------------*/
     
    174163static void default_connection_handler(ddf_fun_t *, ipc_callid_t, ipc_call_t *);
    175164
    176 /** 
     165/**
    177166 * Default handler for IPC methods not handled by DDF.
    178167 *
     
    189178{
    190179        sysarg_t method = IPC_GET_IMETHOD(*icall);
     180        int callback;
    191181       
    192182        usb_kbd_t *kbd_dev = (usb_kbd_t *)fun->driver_data;
     
    198188        }
    199189
    200         if (method == IPC_M_CONNECT_TO_ME) {
    201                 int callback = IPC_GET_ARG5(*icall);
     190        switch (method) {
     191        case IPC_M_CONNECT_TO_ME:
     192                callback = IPC_GET_ARG5(*icall);
    202193
    203194                if (kbd_dev->console_phone != -1) {
     
    212203                usb_log_debug("default_connection_handler: OK\n");
    213204                async_answer_0(icallid, EOK);
    214                 return;
    215         }
    216        
    217         usb_log_debug("default_connection_handler: Wrong function.\n");
    218         async_answer_0(icallid, EINVAL);
     205                break;
     206        case KBDEV_SET_IND:
     207                kbd_dev->mods = IPC_GET_ARG1(*icall);
     208                usb_kbd_set_led(kbd_dev->hid_dev, kbd_dev);
     209                async_answer_0(icallid, EOK);
     210                break;
     211        default:
     212                usb_log_debug("default_connection_handler: Wrong function.\n");
     213                async_answer_0(icallid, EINVAL);
     214                break;
     215        }
    219216}
    220217
     
    225222 * Handles turning of LED lights on and off.
    226223 *
    227  * In case of USB keyboards, the LEDs are handled in the driver, not in the
    228  * device. When there should be a change (lock key was pressed), the driver
    229  * uses a Set_Report request sent to the device to set the state of the LEDs.
     224 * As with most other keyboards, the LED indicators in USB keyboards are
     225 * driven by software. When state of some modifier changes, the input server
     226 * will call us and tell us to update the LED state and what the new state
     227 * should be.
    230228 *
    231229 * This functions sets the LED lights according to current settings of modifiers
     
    249247            USB_HID_REPORT_TYPE_OUTPUT);
    250248       
    251         while (field != NULL) {         
     249        while (field != NULL) {
    252250               
    253251                if ((field->usage == USB_HID_LED_NUM_LOCK)
     
    292290
    293291/*----------------------------------------------------------------------------*/
    294 /**
    295  * Processes key events.
    296  *
    297  * @note This function was copied from AT keyboard driver and modified to suit
    298  *       USB keyboard.
    299  *
    300  * @note Lock keys are not sent to the console, as they are completely handled
    301  *       in the driver. It may, however, be required later that the driver
    302  *       sends also these keys to application (otherwise it cannot use those
    303  *       keys at all).
    304  *
     292/** Send key event.
     293 *
    305294 * @param kbd_dev Keyboard device structure.
    306  * @param type Type of the event (press / release). Recognized values: 
     295 * @param type Type of the event (press / release). Recognized values:
    307296 *             KEY_PRESS, KEY_RELEASE
    308  * @param key Key code of the key according to HID Usage Tables.
     297 * @param key Key code
    309298 */
    310299void usb_kbd_push_ev(usb_hid_dev_t *hid_dev, usb_kbd_t *kbd_dev, int type,
    311300    unsigned int key)
    312301{
    313         kbd_event_t ev;
    314         unsigned mod_mask;
    315 
    316         /*
    317          * These parts are copy-pasted from the AT keyboard driver.
    318          *
    319          * They definitely require some refactoring, but will keep it for later
    320          * when the console and keyboard system is changed in HelenOS.
    321          */
    322         switch (key) {
    323         case KC_LCTRL: mod_mask = KM_LCTRL; break;
    324         case KC_RCTRL: mod_mask = KM_RCTRL; break;
    325         case KC_LSHIFT: mod_mask = KM_LSHIFT; break;
    326         case KC_RSHIFT: mod_mask = KM_RSHIFT; break;
    327         case KC_LALT: mod_mask = KM_LALT; break;
    328         case KC_RALT: mod_mask = KM_RALT; break;
    329         default: mod_mask = 0; break;
    330         }
    331 
    332         if (mod_mask != 0) {
    333                 if (type == KEY_PRESS)
    334                         kbd_dev->mods = kbd_dev->mods | mod_mask;
    335                 else
    336                         kbd_dev->mods = kbd_dev->mods & ~mod_mask;
    337         }
    338 
    339         switch (key) {
    340         case KC_CAPS_LOCK: mod_mask = KM_CAPS_LOCK; break;
    341         case KC_NUM_LOCK: mod_mask = KM_NUM_LOCK; break;
    342         case KC_SCROLL_LOCK: mod_mask = KM_SCROLL_LOCK; break;
    343         default: mod_mask = 0; break;
    344         }
    345 
    346         if (mod_mask != 0) {
    347                 if (type == KEY_PRESS) {
    348                         /*
    349                          * Only change lock state on transition from released
    350                          * to pressed. This prevents autorepeat from messing
    351                          * up the lock state.
    352                          */
    353                         unsigned int locks_old = kbd_dev->lock_keys;
    354                        
    355                         kbd_dev->mods =
    356                             kbd_dev->mods ^ (mod_mask & ~kbd_dev->lock_keys);
    357                         kbd_dev->lock_keys = kbd_dev->lock_keys | mod_mask;
    358 
    359                         /* Update keyboard lock indicator lights. */
    360                         if (kbd_dev->lock_keys != locks_old
    361                             && hid_dev != NULL) { // ugly hack
    362                                 usb_kbd_set_led(hid_dev, kbd_dev);
    363                         }
    364                 } else {
    365                         kbd_dev->lock_keys = kbd_dev->lock_keys & ~mod_mask;
    366                 }
    367         }
    368 
    369         if (key == KC_CAPS_LOCK || key == KC_NUM_LOCK || key == KC_SCROLL_LOCK) {
    370                 // do not send anything to the console, this is our business
    371                 return;
    372         }
    373        
    374         if (type == KEY_PRESS && (kbd_dev->mods & KM_LCTRL) && key == KC_F1) {
    375                 active_layout = 0;
    376                 layout[active_layout]->reset();
    377                 return;
    378         }
    379 
    380         if (type == KEY_PRESS && (kbd_dev->mods & KM_LCTRL) && key == KC_F2) {
    381                 active_layout = 1;
    382                 layout[active_layout]->reset();
    383                 return;
    384         }
    385 
    386         if (type == KEY_PRESS && (kbd_dev->mods & KM_LCTRL) && key == KC_F3) {
    387                 active_layout = 2;
    388                 layout[active_layout]->reset();
    389                 return;
    390         }
    391        
    392         ev.type = type;
    393         ev.key = key;
    394         ev.mods = kbd_dev->mods;
    395 
    396         ev.c = layout[active_layout]->parse_ev(&ev);
    397 
    398         usb_log_debug2("Sending key %d to the console\n", ev.key);
     302        usb_log_debug2("Sending kbdev event %d/%d to the console\n", type, key);
    399303        if (kbd_dev->console_phone < 0) {
    400304                usb_log_warning(
     
    403307        }
    404308       
    405         async_obsolete_msg_4(kbd_dev->console_phone, KBD_EVENT, ev.type, ev.key,
    406             ev.mods, ev.c);
     309        async_obsolete_msg_2(kbd_dev->console_phone, KBDEV_EVENT, type, key);
    407310}
    408311
     
    414317            || key_code == KC_SCROLL_LOCK
    415318            || key_code == KC_CAPS_LOCK);
     319}
     320
     321static size_t find_in_array_int32(int32_t val, int32_t *arr, size_t arr_size)
     322{
     323        for (size_t i = 0; i < arr_size; i++) {
     324                if (arr[i] == val) {
     325                        return i;
     326                }
     327        }
     328
     329        return (size_t) -1;
    416330}
    417331
     
    436350{
    437351        unsigned int key;
    438         unsigned int i, j;
     352        size_t i;
    439353       
    440354        /*
     
    446360         * whole input report.
    447361         */
    448         i = 0;
    449         while (i < kbd_dev->key_count && kbd_dev->keys[i] != ERROR_ROLLOVER) {
    450                 ++i;
    451         }
    452         if (i != kbd_dev->key_count) {
    453                 usb_log_debug("Phantom state occured.\n");
    454                 // phantom state, do nothing
     362        i = find_in_array_int32(ERROR_ROLLOVER, kbd_dev->keys,
     363            kbd_dev->key_count);
     364        if (i != (size_t) -1) {
     365                usb_log_debug("Detected phantom state.\n");
    455366                return;
    456367        }
    457368       
    458369        /*
    459          * 1) Key releases
    460          */
    461         for (j = 0; j < kbd_dev->key_count; ++j) {
    462                 // try to find the old key in the new key list
    463                 i = 0;
    464                 while (i < kbd_dev->key_count
    465                     && kbd_dev->keys[i] != kbd_dev->keys_old[j]) {
    466                         ++i;
    467                 }
    468                
    469                 if (i == kbd_dev->key_count) {
    470                         // not found, i.e. the key was released
    471                         key = usbhid_parse_scancode(kbd_dev->keys_old[j]);
     370         * Key releases
     371         */
     372        for (i = 0; i < kbd_dev->key_count; i++) {
     373                int32_t old_key = kbd_dev->keys_old[i];
     374                /* Find the old key among currently pressed keys. */
     375                size_t pos = find_in_array_int32(old_key, kbd_dev->keys,
     376                    kbd_dev->key_count);
     377                /* If the key was not found, we need to signal release. */
     378                if (pos == (size_t) -1) {
     379                        key = usbhid_parse_scancode(old_key);
    472380                        if (!usb_kbd_is_lock(key)) {
    473381                                usb_kbd_repeat_stop(kbd_dev, key);
    474382                        }
    475383                        usb_kbd_push_ev(hid_dev, kbd_dev, KEY_RELEASE, key);
    476                         usb_log_debug2("Key released: %d\n", key);
    477                 } else {
    478                         // found, nothing happens
    479                 }
    480         }
    481        
    482         /*
    483          * 1) Key presses
     384                        usb_log_debug2("Key released: %u "
     385                            "(USB code %" PRIu32 ")\n", key, old_key);
     386                }
     387        }
     388       
     389        /*
     390         * Key presses
    484391         */
    485392        for (i = 0; i < kbd_dev->key_count; ++i) {
    486                 // try to find the new key in the old key list
    487                 j = 0;
    488                 while (j < kbd_dev->key_count
    489                     && kbd_dev->keys_old[j] != kbd_dev->keys[i]) {
    490                         ++j;
    491                 }
    492                
    493                 if (j == kbd_dev->key_count) {
    494                         // not found, i.e. new key pressed
     393                int32_t new_key = kbd_dev->keys[i];
     394                /* Find the new key among already pressed keys. */
     395                size_t pos = find_in_array_int32(new_key, kbd_dev->keys_old,
     396                    kbd_dev->key_count);
     397                /* If the key was not found, we need to signal press. */
     398                if (pos == (size_t) -1) {
    495399                        key = usbhid_parse_scancode(kbd_dev->keys[i]);
    496                         usb_log_debug2("Key pressed: %d (keycode: %d)\n", key,
    497                             kbd_dev->keys[i]);
    498400                        if (!usb_kbd_is_lock(key)) {
    499401                                usb_kbd_repeat_start(kbd_dev, key);
    500402                        }
    501403                        usb_kbd_push_ev(hid_dev, kbd_dev, KEY_PRESS, key);
    502                 } else {
    503                         // found, nothing happens
     404                        usb_log_debug2("Key pressed: %u "
     405                            "(USB code %" PRIu32 ")\n", key, new_key);
    504406                }
    505407        }
     
    507409        memcpy(kbd_dev->keys_old, kbd_dev->keys, kbd_dev->key_count * 4);
    508410       
    509         usb_log_debug2("New stored keys: ");
    510         for (i = 0; i < kbd_dev->key_count; ++i) {
    511                 usb_log_debug2("%d ", kbd_dev->keys_old[i]);
    512         }
    513         usb_log_debug2("\n");
     411        char key_buffer[512];
     412        ddf_dump_buffer(key_buffer, 512,
     413            kbd_dev->keys_old, 4, kbd_dev->key_count, 0);
     414        usb_log_debug2("Stored keys %s.\n", key_buffer);
    514415}
    515416
     
    533434 *     usb_hid_parse_report().
    534435 */
    535 static void usb_kbd_process_data(usb_hid_dev_t *hid_dev, usb_kbd_t *kbd_dev,
    536                                  uint8_t *buffer, size_t actual_size)
     436static void usb_kbd_process_data(usb_hid_dev_t *hid_dev, usb_kbd_t *kbd_dev)
    537437{
    538438        assert(hid_dev->report != NULL);
    539439        assert(hid_dev != NULL);
    540440        assert(kbd_dev != NULL);
    541 
    542         usb_log_debug("Calling usb_hid_parse_report() with "
    543             "buffer %s\n", usb_debug_str_buffer(buffer, actual_size, 0));
    544441       
    545442        usb_hid_report_path_t *path = usb_hid_report_path();
    546443        usb_hid_report_path_append_item(path, USB_HIDUT_PAGE_KEYBOARD, 0);
    547444
    548         uint8_t report_id;
    549         int rc = usb_hid_parse_report(hid_dev->report, buffer, actual_size,
    550             &report_id);
    551        
    552         if (rc != EOK) {
    553                 usb_log_warning("Error in usb_hid_parse_report():"
    554                     "%s\n", str_error(rc));
    555         }
    556        
    557         usb_hid_report_path_set_report_id (path, report_id);
     445        usb_hid_report_path_set_report_id (path, hid_dev->report_id);
    558446       
    559447        // fill in the currently pressed keys
     
    716604                return ENOMEM;  // TODO: some other code??
    717605        }
     606
     607        /* Store link to HID device */
     608        kbd_dev->hid_dev = hid_dev;
    718609       
    719610        /*
     
    787678        /*
    788679         * Modifiers and locks
    789          */     
     680         */
    790681        kbd_dev->modifiers = 0;
    791682        kbd_dev->mods = DEFAULT_ACTIVE_MODS;
     
    794685        /*
    795686         * Autorepeat
    796          */     
     687         */
    797688        kbd_dev->repeat.key_new = 0;
    798689        kbd_dev->repeat.key_repeated = 0;
     
    852743/*----------------------------------------------------------------------------*/
    853744
    854 bool usb_kbd_polling_callback(usb_hid_dev_t *hid_dev, void *data,
    855      uint8_t *buffer, size_t buffer_size)
    856 {
    857         if (hid_dev == NULL || buffer == NULL || data == NULL) {
     745bool usb_kbd_polling_callback(usb_hid_dev_t *hid_dev, void *data)
     746{
     747        if (hid_dev == NULL/* || buffer == NULL*/ || data == NULL) {
    858748                // do not continue polling (???)
    859749                return false;
     
    864754       
    865755        // TODO: add return value from this function
    866         usb_kbd_process_data(hid_dev, kbd_dev, buffer, buffer_size);
     756        usb_kbd_process_data(hid_dev, kbd_dev);
    867757       
    868758        return true;
     
    900790        if ((*kbd_dev)->repeat_mtx != NULL) {
    901791                //assert(!fibril_mutex_is_locked((*kbd_dev)->repeat_mtx));
     792                // FIXME - the fibril_mutex_is_locked may not cause
     793                // fibril scheduling
    902794                while (fibril_mutex_is_locked((*kbd_dev)->repeat_mtx)) {}
    903795                free((*kbd_dev)->repeat_mtx);
Note: See TracChangeset for help on using the changeset viewer.