Changes in / [a80849c:92574f4] in mainline


Ignore:
Location:
uspace
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • uspace/drv/usbhid/hid.h

    ra80849c r92574f4  
    3737#define USBHID_HID_H_
    3838
     39#include <stdint.h>
     40
    3941#include <usb/classes/hid.h>
    4042#include <ddf/driver.h>
     
    7476        usb_endpoint_pipe_t ctrl_pipe;
    7577        usb_endpoint_pipe_t poll_pipe;
     78       
     79        uint8_t *keycodes;
     80        size_t keycode_count;
     81        uint8_t modifiers;
    7682} usb_hid_dev_kbd_t;
    7783
  • uspace/drv/usbhid/main.c

    ra80849c r92574f4  
    5151#include <usb/descriptor.h>
    5252#include <io/console.h>
     53#include <stdint.h>
    5354#include "hid.h"
    5455#include "descparser.h"
     
    6162
    6263#define GUESSED_POLL_ENDPOINT 1
     64#define BOOTP_REPORT_SIZE 6
    6365
    6466/** Keyboard polling endpoint description for boot protocol class. */
     
    207209        }
    208210/*
    209         printf("type: %d\n", type);
    210         printf("mods: 0x%x\n", mods);
    211         printf("keycode: %u\n", key);
     211        usb_log_debug2("type: %d\n", type);
     212        usb_log_debug2("mods: 0x%x\n", mods);
     213        usb_log_debug2("keycode: %u\n", key);
    212214*/
    213215       
     
    239241        ev.c = layout[active_layout]->parse_ev(&ev);
    240242
    241         printf("Sending key %d to the console\n", ev.key);
     243        usb_log_debug("Sending key %d to the console\n", ev.key);
    242244        assert(console_callback_phone != -1);
    243         async_msg_4(console_callback_phone, KBD_EVENT, ev.type, ev.key, ev.mods, ev.c);
     245        async_msg_4(console_callback_phone, KBD_EVENT, ev.type, ev.key,
     246            ev.mods, ev.c);
    244247}
    245248/*
     
    249252        /*
    250253         * TODO:
    251          * 1) key press / key release - how does the keyboard notify about release?
     254         * 1) key press / key release - how does the keyboard notify about
     255         *    release?
    252256         * 2) layouts (use the already defined), not important now
    253257         * 3)
    254258         */
    255259
     260static const keycode_t usb_hid_modifiers_boot_keycodes[5] = {
     261        KC_NUM_LOCK,      /* USB_HID_MOD_BOOT_NUM_LOCK */
     262        KC_CAPS_LOCK,     /* USB_HID_MOD_BOOT_CAPS_LOCK */
     263        KC_SCROLL_LOCK,   /* USB_HID_MOD_BOOT_SCROLL_LOCK */
     264        0,                /* USB_HID_MOD_BOOT_COMPOSE */
     265        0                 /* USB_HID_MOD_BOOT_KANA */
     266};
     267
     268static void usbkbd_check_modifier_changes(usb_hid_dev_kbd_t *kbd_dev,
     269    uint8_t modifiers)
     270{
     271        /*
     272         * TODO: why the USB keyboard has NUM_, SCROLL_ and CAPS_LOCK
     273         *       both as modifiers and as keys with their own scancodes???
     274         *
     275         * modifiers should be sent as normal keys to usbkbd_parse_scancode()!!
     276         * so maybe it would be better if I received it from report parser in
     277         * that way
     278         */
     279       
     280        int i;
     281        for (i = 0; i < USB_HID_MOD_BOOT_COUNT; ++i) {
     282                if ((modifiers & usb_hid_modifiers_boot_consts[i]) &&
     283                    !(kbd_dev->modifiers & usb_hid_modifiers_boot_consts[i])) {
     284                        // modifier pressed
     285                        if (usb_hid_modifiers_boot_keycodes[i] != 0) {
     286                                kbd_push_ev(KEY_PRESS,
     287                                    usb_hid_modifiers_boot_keycodes[i]);
     288                        }
     289                } else if (!(modifiers & usb_hid_modifiers_boot_consts[i]) &&
     290                    (kbd_dev->modifiers & usb_hid_modifiers_boot_consts[i])) {
     291                        // modifier released
     292                        if (usb_hid_modifiers_boot_keycodes[i] != 0) {
     293                                kbd_push_ev(KEY_RELEASE,
     294                                    usb_hid_modifiers_boot_keycodes[i]);
     295                        }
     296                }       // no change
     297        }
     298}
     299
     300static void usbkbd_check_key_changes(usb_hid_dev_kbd_t *kbd_dev,
     301    const uint8_t *key_codes)
     302{
     303        // TODO: phantom state!!
     304       
     305        unsigned int key;
     306        unsigned int i, j;
     307       
     308        // TODO: quite dummy right now, think of better implementation
     309       
     310        // key releases
     311        for (j = 0; j < kbd_dev->keycode_count; ++j) {
     312                // try to find the old key in the new key list
     313                i = 0;
     314                while (i < kbd_dev->keycode_count
     315                    && key_codes[i] != kbd_dev->keycodes[j]) {
     316                        ++j;
     317                }
     318               
     319                if (j == kbd_dev->keycode_count) {
     320                        // not found, i.e. the key was released
     321                        key = usbkbd_parse_scancode(kbd_dev->keycodes[j]);
     322                        kbd_push_ev(KEY_RELEASE, key);
     323                } else {
     324                        // found, nothing happens
     325                }
     326        }
     327       
     328        // key presses
     329        for (i = 0; i < kbd_dev->keycode_count; ++i) {
     330                // try to find the new key in the old key list
     331                j = 0;
     332                while (j < kbd_dev->keycode_count
     333                    && kbd_dev->keycodes[j] != key_codes[i]) {
     334                        ++j;
     335                }
     336               
     337                assert(kbd_dev->keycode_count <= kbd_dev->keycode_count);
     338               
     339                if (j == kbd_dev->keycode_count) {
     340                        // not found, i.e. new key pressed
     341                        key = usbkbd_parse_scancode(key_codes[i]);
     342                        kbd_push_ev(KEY_PRESS, key);
     343                } else {
     344                        // found, nothing happens
     345                }
     346        }
     347}
     348
    256349/*
    257350 * Callbacks for parser
     
    260353    uint8_t modifiers, void *arg)
    261354{
    262         printf("Got keys: ");
     355        if (arg == NULL) {
     356                usb_log_warning("Missing argument in callback "
     357                    "usbkbd_process_keycodes().\n");
     358                return;
     359        }
     360
     361        usb_log_debug2("Got keys from parser: ");
    263362        unsigned i;
    264363        for (i = 0; i < count; ++i) {
    265                 printf("%d ", key_codes[i]);
    266         }
    267         printf("\n");
    268 
    269         for (i = 0; i < count; ++i) {
    270                 // TODO: Key press / release
    271 
    272                 // TODO: NOT WORKING
    273                 unsigned int key = usbkbd_parse_scancode(key_codes[i]);
    274 
    275                 if (key == 0) {
    276                         continue;
    277                 }
    278                 kbd_push_ev(KEY_PRESS, key);
    279         }
    280         printf("\n");
     364                usb_log_debug2("%d ", key_codes[i]);
     365        }
     366        usb_log_debug2("\n");
     367       
     368        usb_hid_dev_kbd_t *kbd_dev = (usb_hid_dev_kbd_t *)arg;
     369       
     370        if (count != kbd_dev->keycode_count) {
     371                usb_log_warning("Number of received keycodes (%d) differs from"
     372                    " expected number (%d).\n", count, kbd_dev->keycode_count);
     373                return;
     374        }
     375       
     376        usbkbd_check_modifier_changes(kbd_dev, modifiers);
     377        usbkbd_check_key_changes(kbd_dev, key_codes);
    281378}
    282379
     
    291388        for (i = 0; i < kbd_dev->conf->config_descriptor.interface_count; ++i) {
    292389                // TODO: endianness
    293                 uint16_t length =
    294                     kbd_dev->conf->interfaces[i].hid_desc.report_desc_info.length;
     390                uint16_t length =  kbd_dev->conf->interfaces[i].hid_desc.
     391                    report_desc_info.length;
    295392                size_t actual_size = 0;
    296393
    297394                // allocate space for the report descriptor
    298                 kbd_dev->conf->interfaces[i].report_desc = (uint8_t *)malloc(length);
     395                kbd_dev->conf->interfaces[i].report_desc =
     396                    (uint8_t *)malloc(length);
    299397               
    300398                // get the descriptor from the device
     
    317415        return EOK;
    318416}
     417
    319418static int usbkbd_process_descriptors(usb_hid_dev_kbd_t *kbd_dev)
    320419{
     
    388487        free(descriptors);
    389488        if (rc != EOK) {
    390                 printf("Problem with parsing standard descriptors.\n");
     489                usb_log_warning("Problem with parsing standard descriptors.\n");
    391490                return rc;
    392491        }
     
    395494        rc = usbkbd_get_report_descriptor(kbd_dev);
    396495        if (rc != EOK) {
    397                 printf("Problem with parsing HID REPORT descriptor.\n");
     496                usb_log_warning("Problem with parsing REPORT descriptor.\n");
    398497                return rc;
    399498        }
     
    405504         * 1) select one configuration (lets say the first)
    406505         * 2) how many interfaces?? how to select one??
    407      *    ("The default setting for an interface is always alternate setting zero.")
     506         *    ("The default setting for an interface is always alternate
     507         *      setting zero.")
    408508         * 3) find endpoint which is IN and INTERRUPT (parse), save its number
    409     *    as the endpoint for polling
     509        *    as the endpoint for polling
    410510         */
    411511
     
    421521
    422522        if (kbd_dev == NULL) {
    423                 fprintf(stderr, NAME ": No memory!\n");
     523                usb_log_fatal("No memory!\n");
    424524                return NULL;
    425525        }
     
    476576        usb_hid_report_in_callbacks_t *callbacks =
    477577            (usb_hid_report_in_callbacks_t *)malloc(
    478                 sizeof(usb_hid_report_in_callbacks_t));
     578                sizeof(usb_hid_report_in_callbacks_t));
    479579        callbacks->keyboard = usbkbd_process_keycodes;
    480580
    481581        //usb_hid_parse_report(kbd_dev->parser, buffer, actual_size, callbacks,
    482582        //    NULL);
    483         printf("Calling usb_hid_boot_keyboard_input_report() with size %zu\n",
    484             actual_size);
     583        /*usb_log_debug2("Calling usb_hid_boot_keyboard_input_report() with size"
     584            " %zu\n", actual_size);*/
    485585        //dump_buffer("bufffer: ", buffer, actual_size);
    486         int rc = usb_hid_boot_keyboard_input_report(buffer, actual_size, callbacks,
    487             NULL);
    488         if (rc != EOK) {
    489                 printf("Error in usb_hid_boot_keyboard_input_report(): %d\n", rc);
     586        int rc = usb_hid_boot_keyboard_input_report(buffer, actual_size,
     587            callbacks, kbd_dev);
     588       
     589        if (rc != EOK) {
     590                usb_log_warning("Error in usb_hid_boot_keyboard_input_report():"
     591                    "%s\n", str_error(rc));
    490592        }
    491593}
     
    497599        size_t actual_size;
    498600
    499         printf("Polling keyboard...\n");
     601        usb_log_info("Polling keyboard...\n");
    500602
    501603        while (true) {
     
    504606                sess_rc = usb_endpoint_pipe_start_session(&kbd_dev->poll_pipe);
    505607                if (sess_rc != EOK) {
    506                         printf("Failed to start a session: %s.\n",
     608                        usb_log_warning("Failed to start a session: %s.\n",
    507609                            str_error(sess_rc));
    508610                        continue;
     
    514616
    515617                if (rc != EOK) {
    516                         printf("Error polling the keyboard: %s.\n",
     618                        usb_log_warning("Error polling the keyboard: %s.\n",
    517619                            str_error(rc));
    518620                        continue;
     
    520622
    521623                if (sess_rc != EOK) {
    522                         printf("Error closing session: %s.\n",
     624                        usb_log_warning("Error closing session: %s.\n",
    523625                            str_error(sess_rc));
    524626                        continue;
     
    530632                 */
    531633                if (actual_size == 0) {
    532                         printf("Keyboard returned NAK\n");
     634                        usb_log_debug("Keyboard returned NAK\n");
    533635                        continue;
    534636                }
     
    537639                 * TODO: Process pressed keys.
    538640                 */
    539                 printf("Calling usbkbd_process_interrupt_in()\n");
     641                usb_log_debug("Calling usbkbd_process_interrupt_in()\n");
    540642                usbkbd_process_interrupt_in(kbd_dev, buffer, actual_size);
    541643        }
     
    547649static int usbkbd_fibril_device(void *arg)
    548650{
    549         printf("!!! USB device fibril\n");
    550 
    551651        if (arg == NULL) {
    552                 printf("No device!\n");
     652                usb_log_error("No device!\n");
    553653                return -1;
    554654        }
     
    559659        usb_hid_dev_kbd_t *kbd_dev = usbkbd_init_device(dev);
    560660        if (kbd_dev == NULL) {
    561                 printf("Error while initializing device.\n");
     661                usb_log_error("Error while initializing device.\n");
    562662                return -1;
    563663        }
     
    607707        fid_t fid = fibril_create(usbkbd_fibril_device, dev);
    608708        if (fid == 0) {
    609                 printf("%s: failed to start fibril for HID device\n", NAME);
     709                usb_log_error("Failed to start fibril for HID device\n");
    610710                return ENOMEM;
    611711        }
     
    634734int main(int argc, char *argv[])
    635735{
    636         usb_log_enable(USB_LOG_LEVEL_INFO, "usbhid");
     736        usb_log_enable(USB_LOG_LEVEL_MAX, NAME);
    637737        return ddf_driver_main(&kbd_driver);
    638738}
  • uspace/lib/usb/include/usb/classes/hidparser.h

    ra80849c r92574f4  
    7070} usb_hid_report_in_callbacks_t;
    7171
    72 #define USB_HID_BOOT_KEYBOARD_NUM_LOCK          0x01
    73 #define USB_HID_BOOT_KEYBOARD_CAPS_LOCK         0x02
    74 #define USB_HID_BOOT_KEYBOARD_SCROLL_LOCK       0x04
    75 #define USB_HID_BOOT_KEYBOARD_COMPOSE           0x08
    76 #define USB_HID_BOOT_KEYBOARD_KANA                      0x10
     72
     73typedef enum {
     74        USB_HID_MOD_BOOT_NUM_LOCK = 0x01,
     75        USB_HID_MOD_BOOT_CAPS_LOCK = 0x02,
     76        USB_HID_MOD_BOOT_SCROLL_LOCK = 0x04,
     77        USB_HID_MOD_BOOT_COMPOSE = 0x08,
     78        USB_HID_MOD_BOOT_KANA = 0x10,
     79        USB_HID_MOD_BOOT_COUNT = 5
     80} usb_hid_modifiers_boot_t;
     81
     82static const usb_hid_modifiers_boot_t usb_hid_modifiers_boot_consts[5] = {
     83        USB_HID_MOD_BOOT_NUM_LOCK,
     84        USB_HID_MOD_BOOT_CAPS_LOCK,
     85        USB_HID_MOD_BOOT_SCROLL_LOCK,
     86        USB_HID_MOD_BOOT_COMPOSE,
     87        USB_HID_MOD_BOOT_KANA
     88};
     89
     90//#define USB_HID_BOOT_KEYBOARD_NUM_LOCK                0x01
     91//#define USB_HID_BOOT_KEYBOARD_CAPS_LOCK               0x02
     92//#define USB_HID_BOOT_KEYBOARD_SCROLL_LOCK     0x04
     93//#define USB_HID_BOOT_KEYBOARD_COMPOSE         0x08
     94//#define USB_HID_BOOT_KEYBOARD_KANA                    0x10
    7795
    7896/*
Note: See TracChangeset for help on using the changeset viewer.