Changeset 4687fcd4 in mainline for uspace/drv/usbhid/main.c


Ignore:
Timestamp:
2011-01-29T10:19:04Z (13 years ago)
Author:
Jan Vesely <jano.vesely@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
72e19f50
Parents:
67a1b78 (diff), a09128c (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:

Changes from devel, break stuff.

File:
1 moved

Legend:

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

    r67a1b78 r4687fcd4  
    11/*
    22 * Copyright (c) 2010 Vojtech Horky
     3 * Copyright (c) 2011 Lubos Slovak
    34 * All rights reserved.
    45 *
     
    2627 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    2728 */
     29
    2830/** @addtogroup drvusbhid
    2931 * @{
    3032 */
     33/**
     34 * @file
     35 * Main routines of USB HID driver.
     36 */
     37
    3138#include <usb/usbdrv.h>
    3239#include <driver.h>
     
    4148#include <usb/devreq.h>
    4249#include <usb/descriptor.h>
     50#include <io/console.h>
    4351#include "descparser.h"
    4452#include "descdump.h"
     53#include "conv.h"
     54#include "layout.h"
    4555
    4656#define BUFFER_SIZE 32
    47 #define NAME "usbkbd"
     57#define NAME "usbhid"
    4858
    4959#define GUESSED_POLL_ENDPOINT 1
     
    91101
    92102/*
     103 * TODO: Move somewhere else
     104 */
     105/*
     106#define BYTES_PER_LINE 12
     107
     108static void dump_buffer(const char *msg, const uint8_t *buffer, size_t length)
     109{
     110        printf("%s\n", msg);
     111       
     112        size_t i;
     113        for (i = 0; i < length; i++) {
     114                printf("  0x%02X", buffer[i]);
     115                if (((i > 0) && (((i+1) % BYTES_PER_LINE) == 0))
     116                    || (i + 1 == length)) {
     117                        printf("\n");
     118                }
     119        }
     120}
     121*/
     122/*
     123 * Copy-paste from srv/hid/kbd/generic/kbd.c
     124 */
     125
     126/** Currently active modifiers.
     127 *
     128 * TODO: put to device?
     129 */
     130static unsigned mods = KM_NUM_LOCK;
     131
     132/** Currently pressed lock keys. We track these to tackle autorepeat. 
     133 *
     134 * TODO: put to device?
     135 */
     136static unsigned lock_keys;
     137
     138#define NUM_LAYOUTS 3
     139
     140static layout_op_t *layout[NUM_LAYOUTS] = {
     141        &us_qwerty_op,
     142        &us_dvorak_op,
     143        &cz_op
     144};
     145
     146static int active_layout = 0;
     147
     148static void kbd_push_ev(int type, unsigned int key)
     149{
     150        console_event_t ev;
     151        unsigned mod_mask;
     152
     153        // TODO: replace by our own parsing?? or are the key codes identical??
     154        switch (key) {
     155        case KC_LCTRL: mod_mask = KM_LCTRL; break;
     156        case KC_RCTRL: mod_mask = KM_RCTRL; break;
     157        case KC_LSHIFT: mod_mask = KM_LSHIFT; break;
     158        case KC_RSHIFT: mod_mask = KM_RSHIFT; break;
     159        case KC_LALT: mod_mask = KM_LALT; break;
     160        case KC_RALT: mod_mask = KM_RALT; break;
     161        default: mod_mask = 0; break;
     162        }
     163
     164        if (mod_mask != 0) {
     165                if (type == KEY_PRESS)
     166                        mods = mods | mod_mask;
     167                else
     168                        mods = mods & ~mod_mask;
     169        }
     170
     171        switch (key) {
     172        case KC_CAPS_LOCK: mod_mask = KM_CAPS_LOCK; break;
     173        case KC_NUM_LOCK: mod_mask = KM_NUM_LOCK; break;
     174        case KC_SCROLL_LOCK: mod_mask = KM_SCROLL_LOCK; break;
     175        default: mod_mask = 0; break;
     176        }
     177
     178        if (mod_mask != 0) {
     179                if (type == KEY_PRESS) {
     180                        /*
     181                         * Only change lock state on transition from released
     182                         * to pressed. This prevents autorepeat from messing
     183                         * up the lock state.
     184                         */
     185                        mods = mods ^ (mod_mask & ~lock_keys);
     186                        lock_keys = lock_keys | mod_mask;
     187
     188                        /* Update keyboard lock indicator lights. */
     189                        // TODO
     190                        //kbd_ctl_set_ind(mods);
     191                } else {
     192                        lock_keys = lock_keys & ~mod_mask;
     193                }
     194        }
     195/*
     196        printf("type: %d\n", type);
     197        printf("mods: 0x%x\n", mods);
     198        printf("keycode: %u\n", key);
     199*/
     200       
     201        if (type == KEY_PRESS && (mods & KM_LCTRL) &&
     202                key == KC_F1) {
     203                active_layout = 0;
     204                layout[active_layout]->reset();
     205                return;
     206        }
     207
     208        if (type == KEY_PRESS && (mods & KM_LCTRL) &&
     209                key == KC_F2) {
     210                active_layout = 1;
     211                layout[active_layout]->reset();
     212                return;
     213        }
     214
     215        if (type == KEY_PRESS && (mods & KM_LCTRL) &&
     216                key == KC_F3) {
     217                active_layout = 2;
     218                layout[active_layout]->reset();
     219                return;
     220        }
     221       
     222        ev.type = type;
     223        ev.key = key;
     224        ev.mods = mods;
     225
     226        ev.c = layout[active_layout]->parse_ev(&ev);
     227
     228        printf("Sending key %d to the console\n", ev.key);
     229        assert(console_callback_phone != -1);
     230        async_msg_4(console_callback_phone, KBD_EVENT, ev.type, ev.key, ev.mods, ev.c);
     231}
     232/*
     233 * End of copy-paste
     234 */
     235
     236        /*
     237         * TODO:
     238         * 1) key press / key release - how does the keyboard notify about release?
     239         * 2) layouts (use the already defined), not important now
     240         * 3)
     241         */
     242
     243/*
    93244 * Callbacks for parser
    94245 */
    95246static void usbkbd_process_keycodes(const uint8_t *key_codes, size_t count,
    96                                     uint8_t modifiers, void *arg)
     247    uint8_t modifiers, void *arg)
    97248{
    98249        printf("Got keys: ");
     
    100251        for (i = 0; i < count; ++i) {
    101252                printf("%d ", key_codes[i]);
     253                // TODO: Key press / release
     254
     255                // TODO: NOT WORKING
     256                unsigned int key = usbkbd_parse_scancode(key_codes[i]);
     257                kbd_push_ev(KEY_PRESS, key);
    102258        }
    103259        printf("\n");
     
    267423        //usb_hid_parse_report(kbd_dev->parser, buffer, actual_size, callbacks,
    268424        //    NULL);
    269         printf("Calling usb_hid_boot_keyboard_input_report()...\n)");
    270         usb_hid_boot_keyboard_input_report(buffer, actual_size, callbacks, NULL);
     425        printf("Calling usb_hid_boot_keyboard_input_report() with size %zu\n",
     426            actual_size);
     427        //dump_buffer("bufffer: ", buffer, actual_size);
     428        int rc = usb_hid_boot_keyboard_input_report(buffer, actual_size, callbacks,
     429            NULL);
     430        if (rc != EOK) {
     431                printf("Error in usb_hid_boot_keyboard_input_report(): %d\n", rc);
     432        }
    271433}
    272434
    273435static void usbkbd_poll_keyboard(usb_hid_dev_kbd_t *kbd_dev)
    274436{
    275         return;
    276        
    277437        int rc;
    278438        usb_handle_t handle;
     
    292452        };
    293453
     454        printf("Polling keyboard...\n");
     455
    294456        while (true) {
    295                 async_usleep(1000 * 1000);
     457                async_usleep(1000 * 1000 * 2);
    296458                rc = usb_drv_async_interrupt_in(kbd_dev->device->parent_phone,
    297459                    poll_target, buffer, BUFFER_SIZE, &actual_size, &handle);
    298460
    299461                if (rc != EOK) {
     462                        printf("Error in usb_drv_async_interrupt_in(): %d\n", rc);
    300463                        continue;
    301464                }
     
    303466                rc = usb_drv_async_wait_for(handle);
    304467                if (rc != EOK) {
     468                        printf("Error in usb_drv_async_wait_for(): %d\n", rc);
    305469                        continue;
    306470                }
     
    311475                 */
    312476                if (actual_size == 0) {
     477                        printf("Keyboard returned NAK\n");
    313478                        continue;
    314479                }
     
    317482                 * TODO: Process pressed keys.
    318483                 */
     484                printf("Calling usbkbd_process_interrupt_in()\n");
    319485                usbkbd_process_interrupt_in(kbd_dev, buffer, actual_size);
    320486        }
     
    337503        // initialize device (get and process descriptors, get address, etc.)
    338504        usb_hid_dev_kbd_t *kbd_dev = usbkbd_init_device(dev);
     505        if (kbd_dev == NULL) {
     506                printf("Error while initializing device.\n");
     507                return -1;
     508        }
    339509
    340510        usbkbd_poll_keyboard(kbd_dev);
Note: See TracChangeset for help on using the changeset viewer.