Changes in / [299d53e:8e1eb4d0] in mainline


Ignore:
Location:
uspace
Files:
2 added
13 edited

Legend:

Unmodified
Added
Removed
  • uspace/drv/usbhid/Makefile

    r299d53e r8e1eb4d0  
    3939SOURCES = \
    4040        main.c \
     41        descparser.c \
     42        descdump.c \
    4143        conv.c \
    4244        $(STOLEN_LAYOUT_SOURCES)
  • uspace/drv/usbhid/conv.c

    r299d53e r8e1eb4d0  
    3636#include <io/keycode.h>
    3737#include <stdint.h>
    38 #include <stdio.h>
    39 #include <usb/debug.h>
    4038#include "conv.h"
    4139
     
    143141        //[0xe7] = KC_R // TODO: right GUI
    144142       
    145         [0x53] = KC_NUM_LOCK,
    146         [0x54] = KC_NSLASH,
    147         [0x55] = KC_NTIMES,
    148         [0x56] = KC_NMINUS,
    149         [0x57] = KC_NPLUS,
    150         [0x58] = KC_NENTER,
    151         [0x59] = KC_N1,
    152         [0x5a] = KC_N2,
    153         [0x5b] = KC_N3,
    154         [0x5c] = KC_N4,
    155         [0x5d] = KC_N5,
    156         [0x5e] = KC_N6,
    157         [0x5f] = KC_N7,
    158         [0x60] = KC_N8,
    159         [0x61] = KC_N9,
    160         [0x62] = KC_N0,
    161         [0x63] = KC_NPERIOD
    162        
    163143};
    164144
     
    209189
    210190        key = map[scancode];
    211        
    212         if (scancode == 0x53) {
    213                 usb_log_debug("\n\nWe have a NUM LOCK!, sending key %u\n\n", key);
    214         }
    215        
    216         if (scancode == 0x47) {
    217                 usb_log_debug("\n\nWe have a SCROLL LOCK!, sending key %u\n\n", key);
    218         }
    219        
    220         if (scancode == 0x39) {
    221                 usb_log_debug("\n\nWe have a CAPS LOCK!, sending key %u\n\n", key);
    222         }
    223        
    224191//      if (key != 0)
    225192//              kbd_push_ev(type, key);
  • uspace/drv/usbhid/hid.h

    r299d53e r8e1eb4d0  
    3737#define USBHID_HID_H_
    3838
    39 #include <stdint.h>
    40 
    4139#include <usb/classes/hid.h>
    4240#include <ddf/driver.h>
    4341#include <usb/pipes.h>
     42
     43/**
     44 *
     45 */
     46typedef struct {
     47        usb_standard_interface_descriptor_t iface_desc;
     48        usb_standard_endpoint_descriptor_t *endpoints;
     49        usb_standard_hid_descriptor_t hid_desc;
     50        uint8_t *report_desc;
     51        //usb_standard_hid_class_descriptor_info_t *class_desc_info;
     52        //uint8_t **class_descs;
     53} usb_hid_iface_t;
     54
     55/**
     56 *
     57 */
     58typedef struct {
     59        usb_standard_configuration_descriptor_t config_descriptor;
     60        usb_hid_iface_t *interfaces;
     61} usb_hid_configuration_t;
    4462
    4563/**
     
    5068typedef struct {
    5169        ddf_dev_t *device;
     70        usb_hid_configuration_t *conf;
     71        usb_hid_report_parser_t *parser;
    5272
    5373        usb_device_connection_t wire;
    5474        usb_endpoint_pipe_t ctrl_pipe;
    5575        usb_endpoint_pipe_t poll_pipe;
    56        
    57         uint16_t iface;
    58        
    59         uint8_t *report_desc;
    60         usb_hid_report_parser_t *parser;
    61        
    62         uint8_t *keycodes;
    63         size_t keycode_count;
    64         uint8_t modifiers;
    65        
    66         unsigned mods;
    67         unsigned lock_keys;
    6876} usb_hid_dev_kbd_t;
    6977
     78// TODO: more configurations!
     79
    7080#endif
  • uspace/drv/usbhid/main.c

    r299d53e r8e1eb4d0  
    5151#include <usb/descriptor.h>
    5252#include <io/console.h>
    53 #include <stdint.h>
    54 #include <usb/dp.h>
    5553#include "hid.h"
     54#include "descparser.h"
     55#include "descdump.h"
    5656#include "conv.h"
    5757#include "layout.h"
    5858
    5959#define BUFFER_SIZE 8
    60 #define BUFFER_OUT_SIZE 1
    6160#define NAME "usbhid"
    6261
    63 //#define GUESSED_POLL_ENDPOINT 1
    64 #define BOOTP_REPORT_SIZE 6
    65 
    66 static unsigned DEFAULT_ACTIVE_MODS = KM_NUM_LOCK;
     62#define GUESSED_POLL_ENDPOINT 1
    6763
    6864/** Keyboard polling endpoint description for boot protocol class. */
     
    124120
    125121static void dump_buffer(const char *msg, const uint8_t *buffer, size_t length)
    126 {uint8_t buffer[BUFFER_SIZE];
     122{
    127123        printf("%s\n", msg);
    128124       
     
    141137 */
    142138
    143 /** Currently active modifiers (locks is probably better word).
     139/** Currently active modifiers.
    144140 *
    145141 * TODO: put to device?
    146142 */
    147 //static unsigned mods = KM_NUM_LOCK;
     143static unsigned mods = KM_NUM_LOCK;
    148144
    149145/** Currently pressed lock keys. We track these to tackle autorepeat. 
     
    151147 * TODO: put to device?
    152148 */
    153 //static unsigned lock_keys;
     149static unsigned lock_keys;
    154150
    155151#define NUM_LAYOUTS 3
     
    163159static int active_layout = 0;
    164160
    165 static void usbkbd_req_set_report(usb_hid_dev_kbd_t *kbd_dev, uint16_t iface,
    166     usb_hid_report_type_t type, uint8_t *buffer, size_t buf_size)
    167 {
    168         int rc, sess_rc;
    169        
    170         sess_rc = usb_endpoint_pipe_start_session(&kbd_dev->ctrl_pipe);
    171         if (sess_rc != EOK) {
    172                 usb_log_warning("Failed to start a session: %s.\n",
    173                     str_error(sess_rc));
    174                 return;
    175         }
    176 
    177         usb_log_debug("Sending Set_Report request to the device.\n");
    178        
    179         rc = usb_control_request_set(&kbd_dev->ctrl_pipe,
    180             USB_REQUEST_TYPE_CLASS, USB_REQUEST_RECIPIENT_INTERFACE,
    181             USB_HIDREQ_SET_REPORT, type, iface, buffer, buf_size);
    182 
    183         sess_rc = usb_endpoint_pipe_end_session(&kbd_dev->ctrl_pipe);
    184 
    185         if (rc != EOK) {
    186                 usb_log_warning("Error sending output report to the keyboard: "
    187                     "%s.\n", str_error(rc));
    188                 return;
    189         }
    190 
    191         if (sess_rc != EOK) {
    192                 usb_log_warning("Error closing session: %s.\n",
    193                     str_error(sess_rc));
    194                 return;
    195         }
    196 }
    197 
    198 static void usbkbd_req_set_protocol(usb_hid_dev_kbd_t *kbd_dev,
    199     usb_hid_protocol_t protocol)
    200 {
    201         int rc, sess_rc;
    202        
    203         sess_rc = usb_endpoint_pipe_start_session(&kbd_dev->ctrl_pipe);
    204         if (sess_rc != EOK) {
    205                 usb_log_warning("Failed to start a session: %s.\n",
    206                     str_error(sess_rc));
    207                 return;
    208         }
    209 
    210         usb_log_debug("Sending Set_Protocol request to the device ("
    211             "protocol: %d, iface: %d).\n", protocol, kbd_dev->iface);
    212        
    213         rc = usb_control_request_set(&kbd_dev->ctrl_pipe,
    214             USB_REQUEST_TYPE_CLASS, USB_REQUEST_RECIPIENT_INTERFACE,
    215             USB_HIDREQ_SET_PROTOCOL, protocol, kbd_dev->iface, NULL, 0);
    216 
    217         sess_rc = usb_endpoint_pipe_end_session(&kbd_dev->ctrl_pipe);
    218 
    219         if (rc != EOK) {
    220                 usb_log_warning("Error sending output report to the keyboard: "
    221                     "%s.\n", str_error(rc));
    222                 return;
    223         }
    224 
    225         if (sess_rc != EOK) {
    226                 usb_log_warning("Error closing session: %s.\n",
    227                     str_error(sess_rc));
    228                 return;
    229         }
    230 }
    231 
    232 static void usbkbd_set_led(usb_hid_dev_kbd_t *kbd_dev)
    233 {
    234         uint8_t buffer[BUFFER_OUT_SIZE];
    235         int rc= 0, i;
    236        
    237         memset(buffer, 0, BUFFER_OUT_SIZE);
    238         uint8_t leds = 0;
    239 
    240         if (kbd_dev->mods & KM_NUM_LOCK) {
    241                 leds |= USB_HID_LED_NUM_LOCK;
    242         }
    243        
    244         if (kbd_dev->mods & KM_CAPS_LOCK) {
    245                 leds |= USB_HID_LED_CAPS_LOCK;
    246         }
    247        
    248         if (kbd_dev->mods & KM_SCROLL_LOCK) {
    249                 leds |= USB_HID_LED_SCROLL_LOCK;
    250         }
    251 
    252         // TODO: COMPOSE and KANA
    253        
    254         usb_log_debug("Creating output report.\n");
    255         usb_log_debug("Leds: 0x%x\n", leds);
    256         if ((rc = usb_hid_boot_keyboard_output_report(
    257             leds, buffer, BUFFER_OUT_SIZE)) != EOK) {
    258                 usb_log_warning("Error composing output report to the keyboard:"
    259                     "%s.\n", str_error(rc));
    260                 return;
    261         }
    262        
    263         usb_log_debug("Output report buffer: ");
    264         for (i = 0; i < BUFFER_OUT_SIZE; ++i) {
    265                 usb_log_debug("0x%x ", buffer[i]);
    266         }
    267         usb_log_debug("\n");
    268        
    269         uint16_t value = 0;
    270         value |= (USB_HID_REPORT_TYPE_OUTPUT << 8);
    271 
    272         usbkbd_req_set_report(kbd_dev, kbd_dev->iface, value, buffer,
    273             BUFFER_OUT_SIZE);
    274 }
    275 
    276 static void kbd_push_ev(int type, unsigned int key, usb_hid_dev_kbd_t *kbd_dev)
     161static void kbd_push_ev(int type, unsigned int key)
    277162{
    278163        console_event_t ev;
     
    292177        if (mod_mask != 0) {
    293178                if (type == KEY_PRESS)
    294                         kbd_dev->mods = kbd_dev->mods | mod_mask;
     179                        mods = mods | mod_mask;
    295180                else
    296                         kbd_dev->mods = kbd_dev->mods & ~mod_mask;
     181                        mods = mods & ~mod_mask;
    297182        }
    298183
    299184        switch (key) {
    300         case KC_CAPS_LOCK: mod_mask = KM_CAPS_LOCK; usb_log_debug2("\n\nPushing CAPS LOCK! (mask: %u)\n\n", mod_mask); break;
    301         case KC_NUM_LOCK: mod_mask = KM_NUM_LOCK; usb_log_debug2("\n\nPushing NUM LOCK! (mask: %u)\n\n", mod_mask); break;
    302         case KC_SCROLL_LOCK: mod_mask = KM_SCROLL_LOCK; usb_log_debug2("\n\nPushing SCROLL LOCK! (mask: %u)\n\n", mod_mask); break;
     185        case KC_CAPS_LOCK: mod_mask = KM_CAPS_LOCK; break;
     186        case KC_NUM_LOCK: mod_mask = KM_NUM_LOCK; break;
     187        case KC_SCROLL_LOCK: mod_mask = KM_SCROLL_LOCK; break;
    303188        default: mod_mask = 0; break;
    304189        }
    305190
    306191        if (mod_mask != 0) {
    307                 usb_log_debug2("\n\nChanging mods and lock keys\n");
    308                 usb_log_debug2("\nmods before: 0x%x\n", kbd_dev->mods);
    309                 usb_log_debug2("\nLock keys before:0x%x\n\n", kbd_dev->lock_keys);
    310                
    311192                if (type == KEY_PRESS) {
    312                         usb_log_debug2("\nKey pressed.\n");
    313193                        /*
    314194                         * Only change lock state on transition from released
     
    316196                         * up the lock state.
    317197                         */
    318                         kbd_dev->mods =
    319                             kbd_dev->mods ^ (mod_mask & ~kbd_dev->lock_keys);
    320                         kbd_dev->lock_keys = kbd_dev->lock_keys | mod_mask;
     198                        mods = mods ^ (mod_mask & ~lock_keys);
     199                        lock_keys = lock_keys | mod_mask;
    321200
    322201                        /* Update keyboard lock indicator lights. */
    323                         usbkbd_set_led(kbd_dev);
     202                        // TODO
     203                        //kbd_ctl_set_ind(mods);
    324204                } else {
    325                         usb_log_debug2("\nKey released.\n");
    326                         kbd_dev->lock_keys = kbd_dev->lock_keys & ~mod_mask;
    327                 }
    328         }
    329 
    330         usb_log_debug2("\n\nmods after: 0x%x\n", kbd_dev->mods);
    331         usb_log_debug2("\nLock keys after: 0x%x\n\n", kbd_dev->lock_keys);
    332        
    333         if (type == KEY_PRESS && (kbd_dev->mods & KM_LCTRL) && key == KC_F1) {
     205                        lock_keys = lock_keys & ~mod_mask;
     206                }
     207        }
     208/*
     209        printf("type: %d\n", type);
     210        printf("mods: 0x%x\n", mods);
     211        printf("keycode: %u\n", key);
     212*/
     213       
     214        if (type == KEY_PRESS && (mods & KM_LCTRL) &&
     215                key == KC_F1) {
    334216                active_layout = 0;
    335217                layout[active_layout]->reset();
     
    337219        }
    338220
    339         if (type == KEY_PRESS && (kbd_dev->mods & KM_LCTRL) && key == KC_F2) {
     221        if (type == KEY_PRESS && (mods & KM_LCTRL) &&
     222                key == KC_F2) {
    340223                active_layout = 1;
    341224                layout[active_layout]->reset();
     
    343226        }
    344227
    345         if (type == KEY_PRESS && (kbd_dev->mods & KM_LCTRL) && key == KC_F3) {
     228        if (type == KEY_PRESS && (mods & KM_LCTRL) &&
     229                key == KC_F3) {
    346230                active_layout = 2;
    347231                layout[active_layout]->reset();
     
    351235        ev.type = type;
    352236        ev.key = key;
    353         ev.mods = kbd_dev->mods;
    354        
    355         if (ev.mods & KM_NUM_LOCK) {
    356                 usb_log_debug("\n\nNum Lock turned on.\n\n");
    357         }
     237        ev.mods = mods;
    358238
    359239        ev.c = layout[active_layout]->parse_ev(&ev);
    360240
    361         usb_log_debug2("Sending key %d to the console\n", ev.key);
     241        printf("Sending key %d to the console\n", ev.key);
    362242        assert(console_callback_phone != -1);
    363         async_msg_4(console_callback_phone, KBD_EVENT, ev.type, ev.key,
    364             ev.mods, ev.c);
     243        async_msg_4(console_callback_phone, KBD_EVENT, ev.type, ev.key, ev.mods, ev.c);
    365244}
    366245/*
     
    370249        /*
    371250         * TODO:
    372          * 1) key press / key release - how does the keyboard notify about
    373          *    release?
     251         * 1) key press / key release - how does the keyboard notify about release?
    374252         * 2) layouts (use the already defined), not important now
    375253         * 3)
    376254         */
    377255
    378 static const keycode_t usb_hid_modifiers_keycodes[USB_HID_MOD_COUNT] = {
    379         KC_LCTRL,         /* USB_HID_MOD_LCTRL */
    380         KC_LSHIFT,        /* USB_HID_MOD_LSHIFT */
    381         KC_LALT,          /* USB_HID_MOD_LALT */
    382         0,                /* USB_HID_MOD_LGUI */
    383         KC_RCTRL,         /* USB_HID_MOD_RCTRL */
    384         KC_RSHIFT,        /* USB_HID_MOD_RSHIFT */
    385         KC_RALT,          /* USB_HID_MOD_RALT */
    386         0,                /* USB_HID_MOD_RGUI */
    387 };
    388 
    389 static void usbkbd_check_modifier_changes(usb_hid_dev_kbd_t *kbd_dev,
    390     uint8_t modifiers)
    391 {
    392         /*
    393          * TODO: why the USB keyboard has NUM_, SCROLL_ and CAPS_LOCK
    394          *       both as modifiers and as keys with their own scancodes???
    395          *
    396          * modifiers should be sent as normal keys to usbkbd_parse_scancode()!!
    397          * so maybe it would be better if I received it from report parser in
    398          * that way
    399          */
    400        
    401         int i;
    402         for (i = 0; i < USB_HID_MOD_COUNT; ++i) {
    403                 if ((modifiers & usb_hid_modifiers_consts[i]) &&
    404                     !(kbd_dev->modifiers & usb_hid_modifiers_consts[i])) {
    405                         // modifier pressed
    406                         if (usb_hid_modifiers_keycodes[i] != 0) {
    407                                 kbd_push_ev(KEY_PRESS,
    408                                     usb_hid_modifiers_keycodes[i], kbd_dev);
    409                         }
    410                 } else if (!(modifiers & usb_hid_modifiers_consts[i]) &&
    411                     (kbd_dev->modifiers & usb_hid_modifiers_consts[i])) {
    412                         // modifier released
    413                         if (usb_hid_modifiers_keycodes[i] != 0) {
    414                                 kbd_push_ev(KEY_RELEASE,
    415                                     usb_hid_modifiers_keycodes[i], kbd_dev);
    416                         }
    417                 }       // no change
    418         }
    419        
    420         kbd_dev->modifiers = modifiers;
    421 }
    422 
    423 static void usbkbd_check_key_changes(usb_hid_dev_kbd_t *kbd_dev,
    424     const uint8_t *key_codes)
    425 {
    426         // TODO: phantom state!!
    427        
    428         unsigned int key;
    429         unsigned int i, j;
    430        
    431         // TODO: quite dummy right now, think of better implementation
    432        
    433         // key releases
    434         for (j = 0; j < kbd_dev->keycode_count; ++j) {
    435                 // try to find the old key in the new key list
    436                 i = 0;
    437                 while (i < kbd_dev->keycode_count
    438                     && key_codes[i] != kbd_dev->keycodes[j]) {
    439                         ++i;
    440                 }
    441                
    442                 if (i == kbd_dev->keycode_count) {
    443                         // not found, i.e. the key was released
    444                         key = usbkbd_parse_scancode(kbd_dev->keycodes[j]);
    445                         kbd_push_ev(KEY_RELEASE, key, kbd_dev);
    446                         usb_log_debug2("\nKey released: %d\n", key);
    447                 } else {
    448                         // found, nothing happens
    449                 }
    450         }
    451        
    452         // key presses
    453         for (i = 0; i < kbd_dev->keycode_count; ++i) {
    454                 // try to find the new key in the old key list
    455                 j = 0;
    456                 while (j < kbd_dev->keycode_count
    457                     && kbd_dev->keycodes[j] != key_codes[i]) {
    458                         ++j;
    459                 }
    460                
    461                 if (j == kbd_dev->keycode_count) {
    462                         // not found, i.e. new key pressed
    463                         key = usbkbd_parse_scancode(key_codes[i]);
    464                         usb_log_debug2("\nKey pressed: %d (keycode: %d)\n", key,
    465                             key_codes[i]);
    466                         kbd_push_ev(KEY_PRESS, key, kbd_dev);
    467                 } else {
    468                         // found, nothing happens
    469                 }
    470         }
    471        
    472         memcpy(kbd_dev->keycodes, key_codes, kbd_dev->keycode_count);
    473        
    474         usb_log_debug2("\nNew stored keycodes: ");
    475         for (i = 0; i < kbd_dev->keycode_count; ++i) {
    476                 usb_log_debug2("%d ", kbd_dev->keycodes[i]);
    477         }
    478 }
    479 
    480256/*
    481257 * Callbacks for parser
     
    484260    uint8_t modifiers, void *arg)
    485261{
    486         if (arg == NULL) {
    487                 usb_log_warning("Missing argument in callback "
    488                     "usbkbd_process_keycodes().\n");
    489                 return;
    490         }
    491 
    492         usb_log_debug2("Got keys from parser: ");
     262        printf("Got keys: ");
    493263        unsigned i;
    494264        for (i = 0; i < count; ++i) {
    495                 usb_log_debug2("%d ", key_codes[i]);
    496         }
    497         usb_log_debug2("\n");
    498        
    499         usb_hid_dev_kbd_t *kbd_dev = (usb_hid_dev_kbd_t *)arg;
    500        
    501         if (count != kbd_dev->keycode_count) {
    502                 usb_log_warning("Number of received keycodes (%d) differs from"
    503                     " expected number (%d).\n", count, kbd_dev->keycode_count);
    504                 return;
    505         }
    506        
    507         usbkbd_check_modifier_changes(kbd_dev, modifiers);
    508         usbkbd_check_key_changes(kbd_dev, key_codes);
     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");
    509281}
    510282
     
    512284 * Kbd functions
    513285 */
    514 //static int usbkbd_get_report_descriptor(usb_hid_dev_kbd_t *kbd_dev)
    515 //{
    516 //      // iterate over all configurations and interfaces
    517 //      // TODO: more configurations!!
    518 //      unsigned i;
    519 //      for (i = 0; i < kbd_dev->conf->config_descriptor.interface_count; ++i) {
    520 //              // TODO: endianness
    521 //              uint16_t length =  kbd_dev->conf->interfaces[i].hid_desc.
    522 //                  report_desc_info.length;
    523 //              size_t actual_size = 0;
    524 
    525 //              // allocate space for the report descriptor
    526 //              kbd_dev->conf->interfaces[i].report_desc =
    527 //                  (uint8_t *)malloc(length);
     286static int usbkbd_get_report_descriptor(usb_hid_dev_kbd_t *kbd_dev)
     287{
     288        // iterate over all configurations and interfaces
     289        // TODO: more configurations!!
     290        unsigned i;
     291        for (i = 0; i < kbd_dev->conf->config_descriptor.interface_count; ++i) {
     292                // TODO: endianness
     293                uint16_t length =
     294                    kbd_dev->conf->interfaces[i].hid_desc.report_desc_info.length;
     295                size_t actual_size = 0;
     296
     297                // allocate space for the report descriptor
     298                kbd_dev->conf->interfaces[i].report_desc = (uint8_t *)malloc(length);
    528299               
    529 //              // get the descriptor from the device
    530 //              int rc = usb_request_get_descriptor(&kbd_dev->ctrl_pipe,
    531 //                  USB_REQUEST_TYPE_CLASS, USB_DESCTYPE_HID_REPORT,
    532 //                  i, 0,
    533 //                  kbd_dev->conf->interfaces[i].report_desc, length,
    534 //                  &actual_size);
    535 
    536 //              if (rc != EOK) {
    537 //                      return rc;
    538 //              }
    539 
    540 //              assert(actual_size == length);
    541 
    542 //              //dump_hid_class_descriptor(0, USB_DESCTYPE_HID_REPORT,
    543 //              //    kbd_dev->conf->interfaces[i].report_desc, length);
    544 //      }
    545 
    546 //      return EOK;
    547 //}
    548 
    549 static int usbkbd_get_report_descriptor(usb_hid_dev_kbd_t *kbd_dev,
    550     uint8_t *config_desc, size_t config_desc_size, uint8_t *iface_desc)
    551 {
    552         assert(kbd_dev != NULL);
    553         assert(config_desc != NULL);
    554         assert(config_desc_size != 0);
    555         assert(iface_desc != NULL);
    556        
    557         usb_dp_parser_t parser =  {
    558                 .nesting = usb_dp_standard_descriptor_nesting
    559         };
    560        
    561         usb_dp_parser_data_t parser_data = {
    562                 .data = config_desc,
    563                 .size = config_desc_size,
    564                 .arg = NULL
    565         };
    566        
    567         /*
    568          * First nested descriptor of interface descriptor.
    569          */
    570         uint8_t *d =
    571             usb_dp_get_nested_descriptor(&parser, &parser_data, iface_desc);
    572        
    573         /*
    574          * Search through siblings until the HID descriptor is found.
    575          */
    576         while (d != NULL && *(d + 1) != USB_DESCTYPE_HID) {
    577                 d = usb_dp_get_sibling_descriptor(&parser, &parser_data,
    578                     iface_desc, d);
    579         }
    580        
    581         if (d == NULL) {
    582                 usb_log_fatal("No HID descriptor found!\n");
    583                 return ENOENT;
    584         }
    585        
    586         if (*d != sizeof(usb_standard_hid_descriptor_t)) {
    587                 usb_log_fatal("HID descriptor hass wrong size (%u, expected %u"
    588                     ")\n", *d, sizeof(usb_standard_hid_descriptor_t));
    589                 return EINVAL;
    590         }
    591        
    592         usb_standard_hid_descriptor_t *hid_desc =
    593             (usb_standard_hid_descriptor_t *)d;
    594        
    595         uint16_t length =  hid_desc->report_desc_info.length;
    596         size_t actual_size = 0;
    597 
    598         /*
    599          * Allocate space for the report descriptor.
    600          */
    601         kbd_dev->report_desc = (uint8_t *)malloc(length);
    602         if (kbd_dev->report_desc == NULL) {
    603                 usb_log_fatal("Failed to allocate space for Report descriptor."
    604                     "\n");
    605                 return ENOMEM;
    606         }
    607        
    608         usb_log_debug("Getting Report descriptor, expected size: %u\n", length);
    609        
    610         /*
    611          * Get the descriptor from the device.
    612          */
    613         int rc = usb_request_get_descriptor(&kbd_dev->ctrl_pipe,
    614             USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_INTERFACE,
    615             USB_DESCTYPE_HID_REPORT, 0,
    616             kbd_dev->iface, kbd_dev->report_desc, length, &actual_size);
    617 
    618         if (rc != EOK) {
    619                 return rc;
    620         }
    621 
    622         if (actual_size != length) {
    623                 free(kbd_dev->report_desc);
    624                 kbd_dev->report_desc = NULL;
    625                 usb_log_fatal("Report descriptor has wrong size (%u, expected "
    626                     "%u)\n", actual_size, length);
    627                 return EINVAL;
    628         }
    629        
    630         usb_log_debug("Done.\n");
    631        
     300                // get the descriptor from the device
     301                int rc = usb_request_get_descriptor(&kbd_dev->ctrl_pipe,
     302                    USB_REQUEST_TYPE_CLASS, USB_DESCTYPE_HID_REPORT,
     303                    i, 0,
     304                    kbd_dev->conf->interfaces[i].report_desc, length,
     305                    &actual_size);
     306
     307                if (rc != EOK) {
     308                        return rc;
     309                }
     310
     311                assert(actual_size == length);
     312
     313                //dump_hid_class_descriptor(0, USB_DESCTYPE_HID_REPORT,
     314                //    kbd_dev->conf->interfaces[i].report_desc, length);
     315        }
     316
    632317        return EOK;
    633318}
    634 
    635319static int usbkbd_process_descriptors(usb_hid_dev_kbd_t *kbd_dev)
    636320{
     
    680364            descriptors, config_desc.total_length,
    681365            &kbd_dev->wire);
    682        
    683366        if (rc != EOK) {
    684367                usb_log_error("Failed to initialize poll pipe: %s.\n",
    685368                    str_error(rc));
    686                 free(descriptors);
    687369                return rc;
    688370        }
    689        
    690371        if (!endpoint_mapping[0].present) {
    691372                usb_log_warning("Not accepting device, " \
    692373                    "not boot-protocol keyboard.\n");
     374                return EREFUSED;
     375        }
     376
     377
     378
     379
     380        kbd_dev->conf = (usb_hid_configuration_t *)calloc(1,
     381            sizeof(usb_hid_configuration_t));
     382        if (kbd_dev->conf == NULL) {
    693383                free(descriptors);
    694                 return EREFUSED;
    695         }
    696        
    697         usb_log_debug("Accepted device. Saving interface, and getting Report"
    698             " descriptor.\n");
    699        
    700         /*
    701          * Save assigned interface number.
    702          */
    703         if (endpoint_mapping[0].interface_no < 0) {
    704                 usb_log_error("Bad interface number.\n");
    705                 free(descriptors);
    706                 return EINVAL;
    707         }
    708        
    709         kbd_dev->iface = endpoint_mapping[0].interface_no;
    710        
    711         assert(endpoint_mapping[0].interface != NULL);
    712        
    713         rc = usbkbd_get_report_descriptor(kbd_dev, descriptors, transferred,
    714             (uint8_t *)endpoint_mapping[0].interface);
    715        
     384                return ENOMEM;
     385        }
     386       
     387        /*rc = usbkbd_parse_descriptors(descriptors, transferred, kbd_dev->conf);
    716388        free(descriptors);
    717        
    718         if (rc != EOK) {
    719                 usb_log_warning("Problem with parsing REPORT descriptor.\n");
     389        if (rc != EOK) {
     390                printf("Problem with parsing standard descriptors.\n");
    720391                return rc;
    721392        }
    722        
    723         usb_log_debug("Done parsing descriptors.\n");
    724        
     393
     394        // get and report descriptors*/
     395        rc = usbkbd_get_report_descriptor(kbd_dev);
     396        if (rc != EOK) {
     397                printf("Problem with parsing HID REPORT descriptor.\n");
     398                return rc;
     399        }
     400       
     401        //usbkbd_print_config(kbd_dev->conf);
     402
     403        /*
     404         * TODO:
     405         * 1) select one configuration (lets say the first)
     406         * 2) how many interfaces?? how to select one??
     407     *    ("The default setting for an interface is always alternate setting zero.")
     408         * 3) find endpoint which is IN and INTERRUPT (parse), save its number
     409     *    as the endpoint for polling
     410         */
     411
    725412        return EOK;
    726413}
     
    734421
    735422        if (kbd_dev == NULL) {
    736                 usb_log_fatal("No memory!\n");
     423                fprintf(stderr, NAME ": No memory!\n");
    737424                return NULL;
    738425        }
     
    762449
    763450        /*
    764          * Get descriptors, parse descriptors and save endpoints.
    765          */
     451         * will need all descriptors:
     452         * 1) choose one configuration from configuration descriptors
     453         *    (set it to the device)
     454         * 2) set endpoints from endpoint descriptors
     455         */
     456
     457        // TODO: get descriptors, parse descriptors and save endpoints
    766458        usb_endpoint_pipe_start_session(&kbd_dev->ctrl_pipe);
    767        
     459        //usb_request_set_configuration(&kbd_dev->ctrl_pipe, 1);
    768460        rc = usbkbd_process_descriptors(kbd_dev);
    769        
    770461        usb_endpoint_pipe_end_session(&kbd_dev->ctrl_pipe);
    771462        if (rc != EOK) {
    772463                goto error_leave;
    773464        }
    774        
    775         // save the size of the report (boot protocol report by default)
    776         kbd_dev->keycode_count = BOOTP_REPORT_SIZE;
    777         kbd_dev->keycodes = (uint8_t *)calloc(
    778             kbd_dev->keycode_count, sizeof(uint8_t));
    779        
    780         if (kbd_dev->keycodes == NULL) {
    781                 usb_log_fatal("No memory!\n");
    782                 goto error_leave;
    783         }
    784        
    785         kbd_dev->modifiers = 0;
    786         kbd_dev->mods = DEFAULT_ACTIVE_MODS;
    787         kbd_dev->lock_keys = 0;
    788        
    789         // set boot protocol
    790         usbkbd_req_set_protocol(kbd_dev, USB_HID_PROTOCOL_BOOT);
    791        
    792         // set LEDs according to internal setup (NUM LOCK enabled)
    793         usbkbd_set_led(kbd_dev);
    794        
     465
    795466        return kbd_dev;
    796467
     
    805476        usb_hid_report_in_callbacks_t *callbacks =
    806477            (usb_hid_report_in_callbacks_t *)malloc(
    807                 sizeof(usb_hid_report_in_callbacks_t));
     478                sizeof(usb_hid_report_in_callbacks_t));
    808479        callbacks->keyboard = usbkbd_process_keycodes;
    809480
    810481        //usb_hid_parse_report(kbd_dev->parser, buffer, actual_size, callbacks,
    811482        //    NULL);
    812         /*usb_log_debug2("Calling usb_hid_boot_keyboard_input_report() with size"
    813             " %zu\n", actual_size);*/
     483        printf("Calling usb_hid_boot_keyboard_input_report() with size %zu\n",
     484            actual_size);
    814485        //dump_buffer("bufffer: ", buffer, actual_size);
    815         int rc = usb_hid_boot_keyboard_input_report(buffer, actual_size,
    816             callbacks, kbd_dev);
    817        
    818         if (rc != EOK) {
    819                 usb_log_warning("Error in usb_hid_boot_keyboard_input_report():"
    820                     "%s\n", str_error(rc));
     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);
    821490        }
    822491}
     
    828497        size_t actual_size;
    829498
    830         usb_log_info("Polling keyboard...\n");
     499        printf("Polling keyboard...\n");
    831500
    832501        while (true) {
     
    835504                sess_rc = usb_endpoint_pipe_start_session(&kbd_dev->poll_pipe);
    836505                if (sess_rc != EOK) {
    837                         usb_log_warning("Failed to start a session: %s.\n",
     506                        printf("Failed to start a session: %s.\n",
    838507                            str_error(sess_rc));
    839508                        continue;
     
    845514
    846515                if (rc != EOK) {
    847                         usb_log_warning("Error polling the keyboard: %s.\n",
     516                        printf("Error polling the keyboard: %s.\n",
    848517                            str_error(rc));
    849518                        continue;
     
    851520
    852521                if (sess_rc != EOK) {
    853                         usb_log_warning("Error closing session: %s.\n",
     522                        printf("Error closing session: %s.\n",
    854523                            str_error(sess_rc));
    855524                        continue;
     
    861530                 */
    862531                if (actual_size == 0) {
    863                         usb_log_debug("Keyboard returned NAK\n");
     532                        printf("Keyboard returned NAK\n");
    864533                        continue;
    865534                }
     
    868537                 * TODO: Process pressed keys.
    869538                 */
    870                 usb_log_debug("Calling usbkbd_process_interrupt_in()\n");
     539                printf("Calling usbkbd_process_interrupt_in()\n");
    871540                usbkbd_process_interrupt_in(kbd_dev, buffer, actual_size);
    872541        }
     
    878547static int usbkbd_fibril_device(void *arg)
    879548{
     549        printf("!!! USB device fibril\n");
     550
    880551        if (arg == NULL) {
    881                 usb_log_error("No device!\n");
     552                printf("No device!\n");
    882553                return -1;
    883554        }
    884        
    885         usb_hid_dev_kbd_t *kbd_dev = (usb_hid_dev_kbd_t *)arg;
     555
     556        ddf_dev_t *dev = (ddf_dev_t *)arg;
     557
     558        // initialize device (get and process descriptors, get address, etc.)
     559        usb_hid_dev_kbd_t *kbd_dev = usbkbd_init_device(dev);
     560        if (kbd_dev == NULL) {
     561                printf("Error while initializing device.\n");
     562                return -1;
     563        }
    886564
    887565        usbkbd_poll_keyboard(kbd_dev);
     
    892570static int usbkbd_add_device(ddf_dev_t *dev)
    893571{
     572        /* For now, fail immediately. */
     573        //return ENOTSUP;
     574
     575        /*
     576         * When everything is okay, connect to "our" HC.
     577         *
     578         * Not supported yet, skip..
     579         */
     580//      int phone = usb_drv_hc_connect_auto(dev, 0);
     581//      if (phone < 0) {
     582//              /*
     583//               * Connecting to HC failed, roll-back and announce
     584//               * failure.
     585//               */
     586//              return phone;
     587//      }
     588
     589//      dev->parent_phone = phone;
     590
    894591        /*
    895592         * Create default function.
     
    904601        rc = ddf_fun_add_to_class(kbd_fun, "keyboard");
    905602        assert(rc == EOK);
    906        
    907         /*
    908          * Initialize device (get and process descriptors, get address, etc.)
    909          */
    910         usb_hid_dev_kbd_t *kbd_dev = usbkbd_init_device(dev);
    911         if (kbd_dev == NULL) {
    912                 usb_log_error("Error while initializing device.\n");
    913                 return -1;
    914         }
    915 
    916         usb_log_info("Device initialized.\n");
    917        
     603
    918604        /*
    919605         * Create new fibril for handling this keyboard
    920606         */
    921         fid_t fid = fibril_create(usbkbd_fibril_device, kbd_dev);
     607        fid_t fid = fibril_create(usbkbd_fibril_device, dev);
    922608        if (fid == 0) {
    923                 usb_log_error("Failed to start fibril for HID device\n");
     609                printf("%s: failed to start fibril for HID device\n", NAME);
    924610                return ENOMEM;
    925611        }
     
    948634int main(int argc, char *argv[])
    949635{
    950         usb_log_enable(USB_LOG_LEVEL_INFO, NAME);
     636        usb_log_enable(USB_LOG_LEVEL_INFO, "usbhid");
    951637        return ddf_driver_main(&kbd_driver);
    952638}
  • uspace/drv/usbhub/main.c

    r299d53e r8e1eb4d0  
    3434#include <errno.h>
    3535#include <async.h>
    36 #include <stdio.h>
    3736
    3837#include "usbhub.h"
     
    5251};
    5352
     53int usb_hub_control_loop(void * noparam){
     54        while(true){
     55                usb_hub_check_hub_changes();
     56                async_usleep(1000 * 1000 );/// \TODO proper number once
     57        }
     58        return 0;
     59}
     60
     61
    5462int main(int argc, char *argv[])
    5563{
    5664        usb_log_enable(USB_LOG_LEVEL_DEBUG, NAME);
    57         dprintf(USB_LOG_LEVEL_INFO, "starting hub driver");
    58 
    59         //this is probably not needed anymore
     65       
    6066        fibril_mutex_initialize(&usb_hub_list_lock);
    6167        fibril_mutex_lock(&usb_hub_list_lock);
    6268        usb_lst_init(&usb_hub_list);
    6369        fibril_mutex_unlock(&usb_hub_list_lock);
    64        
     70
     71        fid_t fid = fibril_create(usb_hub_control_loop, NULL);
     72        if (fid == 0) {
     73                fprintf(stderr, NAME ": failed to start monitoring fibril," \
     74                    " driver aborting.\n");
     75                return ENOMEM;
     76        }
     77        fibril_add_ready(fid);
     78
    6579        return ddf_driver_main(&hub_driver);
    6680}
  • uspace/drv/usbhub/port_status.h

    r299d53e r8e1eb4d0  
    177177}
    178178
    179 /**
    180  * set the device request to be a port disable request
    181  * @param request
    182  * @param port
    183  */
    184 static inline void usb_hub_unset_power_port_request(
    185 usb_device_request_setup_packet_t * request, uint16_t port
    186 ){
    187         request->index = port;
    188         request->request_type = USB_HUB_REQ_TYPE_SET_PORT_FEATURE;
    189         request->request = USB_HUB_REQUEST_CLEAR_FEATURE;
    190         request->value = USB_HUB_FEATURE_PORT_POWER;
    191         request->length = 0;
    192 }
    193 
    194179/** get i`th bit of port status */
    195180static inline bool usb_port_get_bit(usb_port_status_t * status, int idx)
  • uspace/drv/usbhub/usbhub.c

    r299d53e r8e1eb4d0  
    4444#include <usb/request.h>
    4545#include <usb/classes/hub.h>
    46 #include <stdio.h>
    4746
    4847#include "usbhub.h"
     
    5756};
    5857
    59 /** Hub status-change endpoint description
    60  *
    61  * For more see usb hub specification in 11.15.1 of
    62  */
     58/** Hub status-change endpoint description */
    6359static usb_endpoint_description_t status_change_endpoint_description = {
    6460        .transfer_type = USB_TRANSFER_INTERRUPT,
    6561        .direction = USB_DIRECTION_IN,
    6662        .interface_class = USB_CLASS_HUB,
    67         .interface_subclass = 0,
    68         .interface_protocol = 0,
    6963        .flags = 0
    7064};
    71 
    72 int usb_hub_control_loop(void * hub_info_param){
    73         usb_hub_info_t * hub_info = (usb_hub_info_t*)hub_info_param;
    74         while(true){
    75                 usb_hub_check_hub_changes(hub_info);
    76                 async_usleep(1000 * 1000 );/// \TODO proper number once
    77         }
    78         return 0;
    79 }
    8065
    8166
     
    150135
    151136        //configuration descriptor
    152         /// \TODO check other configurations?
     137        /// \TODO check other configurations
    153138        usb_standard_configuration_descriptor_t config_descriptor;
    154139        opResult = usb_request_get_bare_configuration_descriptor(
     
    194179        }
    195180
     181        /**
     182         * Initialize the interrupt in endpoint.
     183         * \TODO this code should be checked...
     184         */
    196185        usb_endpoint_mapping_t endpoint_mapping[1] = {
    197186                {
     
    221210        return EOK;
    222211       
     212
     213        // Initialize the interrupt(=status change) endpoint.
     214        /*usb_endpoint_pipe_initialize(
     215                &result->endpoints->status_change,
     216                &result->device_connection, );USB_TRANSFER_INTERRUPT
     217        USB_DIRECTION_IN*/
     218
    223219}
    224220
     
    253249        usb_endpoint_pipe_start_session(&result->endpoints.control);
    254250        opResult = usb_request_get_descriptor(&result->endpoints.control,
    255                         USB_REQUEST_TYPE_CLASS, USB_REQUEST_RECIPIENT_DEVICE,
    256                         USB_DESCTYPE_HUB,
    257                         0, 0, serialized_descriptor,
     251                        USB_REQUEST_TYPE_CLASS,
     252                        USB_DESCTYPE_HUB, 0, 0, serialized_descriptor,
    258253                        USB_HUB_MAX_DESCRIPTOR_SIZE, &received_size);
    259254        usb_endpoint_pipe_end_session(&result->endpoints.control);
     
    272267        }
    273268
     269       
    274270        dprintf(USB_LOG_LEVEL_INFO, "setting port count to %d",descriptor->ports_count);
    275271        result->port_count = descriptor->ports_count;
     
    333329
    334330        //add the hub to list
    335         //is this needed now?
    336331        fibril_mutex_lock(&usb_hub_list_lock);
    337332        usb_lst_append(&usb_hub_list, hub_info);
    338333        fibril_mutex_unlock(&usb_hub_list_lock);
     334
    339335        dprintf(USB_LOG_LEVEL_DEBUG, "hub info added to list");
    340 
    341         dprintf(USB_LOG_LEVEL_DEBUG, "adding to ddf");
    342         ddf_fun_t *hub_fun = ddf_fun_create(dev, fun_exposed, "hub");
    343         assert(hub_fun != NULL);
    344         hub_fun->ops = NULL;
    345 
    346         int rc = ddf_fun_bind(hub_fun);
    347         assert(rc == EOK);
    348         rc = ddf_fun_add_to_class(hub_fun, "hub");
    349         assert(rc == EOK);
    350 
    351         fid_t fid = fibril_create(usb_hub_control_loop, hub_info);
    352         if (fid == 0) {
    353                 dprintf(USB_LOG_LEVEL_ERROR,
    354                                 ": failed to start monitoring fibril for new hub");
    355                 return ENOMEM;
    356         }
    357         fibril_add_ready(fid);
    358 
    359         dprintf(USB_LOG_LEVEL_DEBUG, "hub fibril created");
    360336        //(void)hub_info;
    361         //usb_hub_check_hub_changes();
     337        usb_hub_check_hub_changes();
    362338       
    363339        dprintf(USB_LOG_LEVEL_INFO, "hub dev added");
     
    392368        //opResult = usb_drv_reserve_default_address(hc);
    393369        opResult = usb_hc_reserve_default_address(&hub->connection, USB_SPEED_LOW);
    394        
    395         if (opResult != EOK) {
    396                 dprintf(USB_LOG_LEVEL_WARNING,
    397                                 "cannot assign default address, it is probably used %d",opResult);
     370
     371        if (opResult != EOK) {
     372                dprintf(USB_LOG_LEVEL_WARNING, "cannot assign default address, it is probably used");
    398373                return;
    399374        }
     
    406381                        );
    407382        if (opResult != EOK) {
    408                 dprintf(USB_LOG_LEVEL_ERROR,
    409                                 "something went wrong when reseting a port %d",opResult);
     383                dprintf(USB_LOG_LEVEL_ERROR, "something went wrong when reseting a port");
    410384                //usb_hub_release_default_address(hc);
    411385                usb_hc_release_default_address(&hub->connection);
     
    420394 */
    421395static void usb_hub_finalize_add_device( usb_hub_info_t * hub,
    422                 uint16_t port, bool isLowSpeed) {
     396                uint16_t port) {
    423397
    424398        int opResult;
     
    443417                        &new_device_connection);
    444418        /// \TODO get highspeed info
    445         usb_speed_t speed = isLowSpeed?USB_SPEED_LOW:USB_SPEED_FULL;
     419
     420
     421
    446422
    447423
     
    449425        usb_address_t new_device_address = usb_hc_request_address(
    450426                        &hub->connection,
    451                         speed/// \TODO fullspeed??
     427                        USB_SPEED_LOW/// \TODO fullspeed??
    452428                        );
    453429        if (new_device_address < 0) {
     
    460436        //opResult = usb_drv_req_set_address(hc, USB_ADDRESS_DEFAULT,
    461437        //    new_device_address);
    462         usb_endpoint_pipe_start_session(&new_device_pipe);
    463438        opResult = usb_request_set_address(&new_device_pipe,new_device_address);
    464         usb_endpoint_pipe_end_session(&new_device_pipe);
    465         if (opResult != EOK) {
    466                 dprintf(USB_LOG_LEVEL_ERROR,
    467                                 "could not set address for new device %d",opResult);
     439
     440        if (opResult != EOK) {
     441                dprintf(USB_LOG_LEVEL_ERROR, "could not set address for new device");
    468442                usb_hc_release_default_address(&hub->connection);
    469443                return;
     
    484458
    485459        if (opResult != EOK) {
    486                 dprintf(USB_LOG_LEVEL_ERROR,
    487                                 "could not start driver for new device %d",opResult);
     460                dprintf(USB_LOG_LEVEL_ERROR, "could not start driver for new device");
    488461                return;
    489462        }
     
    496469                        &hub->attached_devs[port]);
    497470        if (opResult != EOK) {
    498                 dprintf(USB_LOG_LEVEL_ERROR,
    499                                 "could not assign address of device in hcd %d",opResult);
     471                dprintf(USB_LOG_LEVEL_ERROR, "could not assign address of device in hcd");
    500472                return;
    501473        }
     
    539511}
    540512
    541 
    542 /**
    543  *Process over current condition on port.
    544  *
    545  * Turn off the power on the port.
    546  *
    547  * @param hub
    548  * @param port
    549  */
    550 static void usb_hub_over_current( usb_hub_info_t * hub,
    551                 uint16_t port){
    552         int opResult;
    553         opResult = usb_hub_clear_port_feature(&hub->endpoints.control,
    554             port, USB_HUB_FEATURE_PORT_POWER);
    555         if(opResult!=EOK){
    556                 dprintf(USB_LOG_LEVEL_ERROR, "cannot power off port %d;  %d",
    557                                 port, opResult);
    558         }
    559 }
    560 
    561513/**
    562514 * Process interrupts on given hub port
     
    570522        //determine type of change
    571523        usb_endpoint_pipe_t *pipe = &hub->endpoints.control;
     524        int opResult = usb_endpoint_pipe_start_session(pipe);
    572525       
    573         int opResult;
     526        if(opResult != EOK){
     527                dprintf(USB_LOG_LEVEL_ERROR, "cannot open pipe %d", opResult);
     528        }
     529
     530        /*
     531        usb_target_t target;
     532        target.address=address;
     533        target.endpoint=0;
     534        */
    574535
    575536        usb_port_status_t status;
     
    586547                        );
    587548        if (opResult != EOK) {
    588                 dprintf(USB_LOG_LEVEL_ERROR, "could not get port status");
     549                dprintf(USB_LOG_LEVEL_ERROR, "ERROR: could not get port status");
    589550                return;
    590551        }
    591552        if (rcvd_size != sizeof (usb_port_status_t)) {
    592                 dprintf(USB_LOG_LEVEL_ERROR, "received status has incorrect size");
     553                dprintf(USB_LOG_LEVEL_ERROR, "ERROR: received status has incorrect size");
    593554                return;
    594555        }
     
    605566                }
    606567        }
    607         //over current
    608         if (usb_port_overcurrent_change(&status)) {
    609                 //check if it was not auto-resolved
    610                 if(usb_port_over_current(&status)){
    611                         usb_hub_over_current(hub,port);
    612                 }else{
    613                         dprintf(USB_LOG_LEVEL_INFO,
    614                                 "over current condition was auto-resolved on port %d",port);
    615                 }
    616         }
    617568        //port reset
    618569        if (usb_port_reset_completed(&status)) {
    619570                dprintf(USB_LOG_LEVEL_INFO, "port reset complete");
    620571                if (usb_port_enabled(&status)) {
    621                         usb_hub_finalize_add_device(hub, port, usb_port_low_speed(&status));
     572                        usb_hub_finalize_add_device(hub, port);
    622573                } else {
    623                         dprintf(USB_LOG_LEVEL_WARNING, "port reset, but port still not enabled");
     574                        dprintf(USB_LOG_LEVEL_WARNING, "ERROR: port reset, but port still not enabled");
    624575                }
    625576        }
     
    634585        }
    635586        /// \TODO handle other changes
    636 }
    637 
    638 /**
    639  * Check changes on particular hub
    640  * @param hub_info_param
    641  */
    642 void usb_hub_check_hub_changes(usb_hub_info_t * hub_info){
    643         int opResult;
    644         opResult = usb_endpoint_pipe_start_session(&hub_info->endpoints.status_change);
    645         if(opResult != EOK){
    646                 dprintf(USB_LOG_LEVEL_ERROR,
    647                                 "could not initialize communication for hub; %d", opResult);
    648                 return;
    649         }
    650 
    651         size_t port_count = hub_info->port_count;
    652 
    653         /// FIXME: count properly
    654         size_t byte_length = ((port_count+1) / 8) + 1;
     587        /// \TODO debug log for various situations
     588        usb_endpoint_pipe_end_session(pipe);
     589
     590
     591}
     592
     593/**
     594 * Check changes on all known hubs.
     595 */
     596void usb_hub_check_hub_changes(void) {
     597        /*
     598         * Iterate through all hubs.
     599         */
     600        usb_general_list_t * lst_item;
     601        fibril_mutex_lock(&usb_hub_list_lock);
     602        for (lst_item = usb_hub_list.next;
     603                        lst_item != &usb_hub_list;
     604                        lst_item = lst_item->next) {
     605                fibril_mutex_unlock(&usb_hub_list_lock);
     606                usb_hub_info_t * hub_info = ((usb_hub_info_t*)lst_item->data);
     607                int opResult;
     608
     609                opResult = usb_endpoint_pipe_start_session(&hub_info->endpoints.status_change);
     610                if(opResult != EOK){
     611                        continue;
     612                }
     613                /*
     614                 * Check status change pipe of this hub.
     615                 */
     616                /*
     617                usb_target_t target;
     618                target.address = hub_info->address;
     619                target.endpoint = 1;/// \TODO get from endpoint descriptor
     620                dprintf(USB_LOG_LEVEL_INFO, "checking changes for hub at addr %d",
     621                    target.address);
     622                */
     623                size_t port_count = hub_info->port_count;
     624
     625                /*
     626                 * Connect to respective HC.
     627                 *
     628                int hc = usb_drv_hc_connect_auto(hub_info->device, 0);
     629                if (hc < 0) {
     630                        continue;
     631                }*/
     632
     633                /// FIXME: count properly
     634                size_t byte_length = ((port_count+1) / 8) + 1;
     635
    655636                void *change_bitmap = malloc(byte_length);
    656         size_t actual_size;
    657 
    658         /*
    659          * Send the request.
    660          */
    661         opResult = usb_endpoint_pipe_read(
    662                         &hub_info->endpoints.status_change,
    663                         change_bitmap, byte_length, &actual_size
    664                         );
    665 
    666         if (opResult != EOK) {
     637                size_t actual_size;
     638                //usb_handle_t handle;
     639
     640                /*
     641                 * Send the request.
     642                 */
     643                opResult = usb_endpoint_pipe_read(
     644                                &hub_info->endpoints.status_change,
     645                                change_bitmap, byte_length, &actual_size
     646                                );
     647
     648                //usb_drv_async_wait_for(handle);
     649
     650                if (opResult != EOK) {
     651                        free(change_bitmap);
     652                        dprintf(USB_LOG_LEVEL_WARNING, "something went wrong while getting status of hub");
     653                        continue;
     654                }
     655                unsigned int port;
     656                for (port = 1; port < port_count+1; ++port) {
     657                        bool interrupt =
     658                                        (((uint8_t*) change_bitmap)[port / 8] >> (port % 8)) % 2;
     659                        if (interrupt) {
     660                                usb_hub_process_interrupt(
     661                                        hub_info, port);
     662                        }
     663                }
     664                usb_endpoint_pipe_end_session(&hub_info->endpoints.status_change);
    667665                free(change_bitmap);
    668                 dprintf(USB_LOG_LEVEL_WARNING, "something went wrong while getting status of hub");
    669                 usb_endpoint_pipe_end_session(&hub_info->endpoints.status_change);
    670                 return;
    671         }
    672         unsigned int port;
    673         opResult = usb_endpoint_pipe_start_session(&hub_info->endpoints.control);
    674         if(opResult!=EOK){
    675                 dprintf(USB_LOG_LEVEL_ERROR, "could not start control pipe session %d",
    676                                 opResult);
    677                 usb_endpoint_pipe_end_session(&hub_info->endpoints.status_change);
    678                 return;
    679         }
    680         opResult = usb_hc_connection_open(&hub_info->connection);
    681         if(opResult!=EOK){
    682                 dprintf(USB_LOG_LEVEL_ERROR, "could not start host controller session %d",
    683                                 opResult);
    684                 usb_endpoint_pipe_end_session(&hub_info->endpoints.control);
    685                 usb_endpoint_pipe_end_session(&hub_info->endpoints.status_change);
    686                 return;
    687         }
    688 
    689         ///todo, opresult check, pre obe konekce
    690         for (port = 1; port < port_count+1; ++port) {
    691                 bool interrupt =
    692                                 (((uint8_t*) change_bitmap)[port / 8] >> (port % 8)) % 2;
    693                 if (interrupt) {
    694                         usb_hub_process_interrupt(
    695                                 hub_info, port);
    696                 }
    697         }
    698         usb_hc_connection_close(&hub_info->connection);
    699         usb_endpoint_pipe_end_session(&hub_info->endpoints.control);
    700         usb_endpoint_pipe_end_session(&hub_info->endpoints.status_change);
    701         free(change_bitmap);
    702 }
     666               
     667
     668                //async_hangup(hc);
     669                fibril_mutex_lock(&usb_hub_list_lock);
     670        }
     671        fibril_mutex_unlock(&usb_hub_list_lock);
     672}
     673
     674
    703675
    704676
  • uspace/drv/usbhub/usbhub.h

    r299d53e r8e1eb4d0  
    7575/**
    7676 * function running the hub-controlling loop.
    77  * @param hub_info_param hub info pointer
     77 * @param noparam fundtion does not need any parameters
    7878 */
    79 int usb_hub_control_loop(void * hub_info_param);
     79int usb_hub_control_loop(void * noparam);
    8080
    8181/** Callback when new hub device is detected.
     
    8787
    8888/**
    89  * check changes on specified hub
    90  * @param hub_info_param pointer to usb_hub_info_t structure
     89 * check changes on all registered hubs
    9190 */
    92 void usb_hub_check_hub_changes(usb_hub_info_t * hub_info_param);
     91void usb_hub_check_hub_changes(void);
    9392
    9493
     94//int usb_add_hub_device(device_t *);
    9595
    9696
  • uspace/lib/usb/include/usb/classes/hid.h

    r299d53e r8e1eb4d0  
    5151} usb_hid_request_t;
    5252
    53 typedef enum {
    54         USB_HID_REPORT_TYPE_INPUT = 1,
    55         USB_HID_REPORT_TYPE_OUTPUT = 2,
    56         USB_HID_REPORT_TYPE_FEATURE = 3
    57 } usb_hid_report_type_t;
    58 
    59 typedef enum {
    60         USB_HID_PROTOCOL_BOOT = 0,
    61         USB_HID_PROTOCOL_REPORT = 1
    62 } usb_hid_protocol_t;
    63 
    6453/** USB/HID subclass constants. */
    6554typedef enum {
     
    7362        USB_HID_PROTOCOL_KEYBOARD = 1,
    7463        USB_HID_PROTOCOL_MOUSE = 2
    75 } usb_hid_iface_protocol_t;
     64} usb_hid_protocol_t;
    7665
    7766/** Part of standard USB HID descriptor specifying one class descriptor.
  • uspace/lib/usb/include/usb/classes/hidparser.h

    r299d53e r8e1eb4d0  
    7070} usb_hid_report_in_callbacks_t;
    7171
    72 
    73 typedef enum {
    74         USB_HID_MOD_LCTRL = 0x01,
    75         USB_HID_MOD_LSHIFT = 0x02,
    76         USB_HID_MOD_LALT = 0x04,
    77         USB_HID_MOD_LGUI = 0x08,
    78         USB_HID_MOD_RCTRL = 0x10,
    79         USB_HID_MOD_RSHIFT = 0x20,
    80         USB_HID_MOD_RALT = 0x40,
    81         USB_HID_MOD_RGUI = 0x80,
    82         USB_HID_MOD_COUNT = 8
    83 } usb_hid_modifiers_t;
    84 
    85 typedef enum {
    86         USB_HID_LED_NUM_LOCK = 0x1,
    87         USB_HID_LED_CAPS_LOCK = 0x2,
    88         USB_HID_LED_SCROLL_LOCK = 0x4,
    89         USB_HID_LED_COMPOSE = 0x8,
    90         USB_HID_LED_KANA = 0x10,
    91         USB_HID_LED_COUNT = 5
    92 } usb_hid_led_t;
    93 
    94 static const usb_hid_modifiers_t
    95     usb_hid_modifiers_consts[USB_HID_MOD_COUNT] = {
    96         USB_HID_MOD_LCTRL,
    97         USB_HID_MOD_LSHIFT,
    98         USB_HID_MOD_LALT,
    99         USB_HID_MOD_LGUI,
    100         USB_HID_MOD_RCTRL,
    101         USB_HID_MOD_RSHIFT,
    102         USB_HID_MOD_RALT,
    103         USB_HID_MOD_RGUI
    104 };
    105 
    106 //static const usb_hid_led_t usb_hid_led_consts[USB_HID_LED_COUNT] = {
    107 //      USB_HID_LED_NUM_LOCK,
    108 //      USB_HID_LED_CAPS_LOCK,
    109 //      USB_HID_LED_SCROLL_LOCK,
    110 //      USB_HID_LED_COMPOSE,
    111 //      USB_HID_LED_KANA
    112 //};
    113 
    114 //#define USB_HID_BOOT_KEYBOARD_NUM_LOCK                0x01
    115 //#define USB_HID_BOOT_KEYBOARD_CAPS_LOCK               0x02
    116 //#define USB_HID_BOOT_KEYBOARD_SCROLL_LOCK     0x04
    117 //#define USB_HID_BOOT_KEYBOARD_COMPOSE         0x08
    118 //#define USB_HID_BOOT_KEYBOARD_KANA                    0x10
     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
    11977
    12078/*
  • uspace/lib/usb/include/usb/request.h

    r299d53e r8e1eb4d0  
    9696int usb_request_set_address(usb_endpoint_pipe_t *, usb_address_t);
    9797int usb_request_get_descriptor(usb_endpoint_pipe_t *, usb_request_type_t,
    98     usb_request_recipient_t, uint8_t, uint8_t, uint16_t, void *, size_t,
    99     size_t *);
     98    uint8_t, uint8_t, uint16_t, void *, size_t, size_t *);
    10099int usb_request_get_descriptor_alloc(usb_endpoint_pipe_t *, usb_request_type_t,
    101     usb_request_recipient_t, uint8_t, uint8_t, uint16_t, void **, size_t *);
     100    uint8_t, uint8_t, uint16_t, void **, size_t *);
    102101int usb_request_get_device_descriptor(usb_endpoint_pipe_t *,
    103102    usb_standard_device_descriptor_t *);
  • uspace/lib/usb/src/hidparser.c

    r299d53e r8e1eb4d0  
    144144int usb_hid_boot_keyboard_output_report(uint8_t leds, uint8_t *data, size_t size)
    145145{
    146         if (size < 1){
     146        if(size != 1){
    147147                return -1;
    148148        }
    149149
    150         data[0] = leds;
     150        /* used only first five bits, others are only padding*/
     151        *data = leds;
    151152        return EOK;
    152153}
  • uspace/lib/usb/src/request.c

    r299d53e r8e1eb4d0  
    3636#include <errno.h>
    3737#include <assert.h>
    38 #include <usb/debug.h>
    3938
    4039#define MAX_DATA_LENGTH ((size_t)(0xFFFF))
     
    210209 */
    211210int usb_request_get_descriptor(usb_endpoint_pipe_t *pipe,
    212     usb_request_type_t request_type, usb_request_recipient_t recipient,
     211    usb_request_type_t request_type,
    213212    uint8_t descriptor_type, uint8_t descriptor_index,
    214213    uint16_t language,
     
    225224
    226225        return usb_control_request_get(pipe,
    227             request_type, recipient,
     226            request_type, USB_REQUEST_RECIPIENT_DEVICE,
    228227            USB_DEVREQ_GET_DESCRIPTOR,
    229228            wValue, language,
     
    243242 */
    244243int usb_request_get_descriptor_alloc(usb_endpoint_pipe_t * pipe,
    245     usb_request_type_t request_type, usb_request_recipient_t recipient,
     244    usb_request_type_t request_type,
    246245    uint8_t descriptor_type, uint8_t descriptor_index,
    247246    uint16_t language,
     
    259258        uint8_t tmp_buffer[1];
    260259        size_t bytes_transfered;
    261         rc = usb_request_get_descriptor(pipe, request_type, recipient,
     260        rc = usb_request_get_descriptor(pipe, request_type,
    262261            descriptor_type, descriptor_index, language,
    263262            &tmp_buffer, 1, &bytes_transfered);
     
    284283        }
    285284
    286         rc = usb_request_get_descriptor(pipe, request_type, recipient,
     285        rc = usb_request_get_descriptor(pipe, request_type,
    287286            descriptor_type, descriptor_index, language,
    288287            buffer, size, &bytes_transfered);
     
    321320        usb_standard_device_descriptor_t descriptor_tmp;
    322321        int rc = usb_request_get_descriptor(pipe,
    323             USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_DEVICE,
    324             USB_DESCTYPE_DEVICE, 0, 0,
     322            USB_REQUEST_TYPE_STANDARD, USB_DESCTYPE_DEVICE,
     323            0, 0,
    325324            &descriptor_tmp, sizeof(descriptor_tmp),
    326325            &actually_transferred);
     
    367366        usb_standard_configuration_descriptor_t descriptor_tmp;
    368367        int rc = usb_request_get_descriptor(pipe,
    369             USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_DEVICE,
    370             USB_DESCTYPE_CONFIGURATION, index, 0,
     368            USB_REQUEST_TYPE_STANDARD, USB_DESCTYPE_CONFIGURATION,
     369            index, 0,
    371370            &descriptor_tmp, sizeof(descriptor_tmp),
    372371            &actually_transferred);
     
    407406
    408407        return usb_request_get_descriptor(pipe,
    409             USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_DEVICE,
    410             USB_DESCTYPE_CONFIGURATION, index, 0,
     408            USB_REQUEST_TYPE_STANDARD, USB_DESCTYPE_CONFIGURATION,
     409            index, 0,
    411410            descriptor, descriptor_size, actual_size);
    412411}
     
    453452        size_t string_descriptor_size = 0;
    454453        rc = usb_request_get_descriptor_alloc(pipe,
    455             USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_DEVICE,
    456             USB_DESCTYPE_STRING, 0, 0,
     454            USB_REQUEST_TYPE_STANDARD, USB_DESCTYPE_STRING, 0, 0,
    457455            (void **) &string_descriptor, &string_descriptor_size);
    458456        if (rc != EOK) {
     
    533531        size_t string_size;
    534532        rc = usb_request_get_descriptor_alloc(pipe,
    535             USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_DEVICE,
    536             USB_DESCTYPE_STRING, index, uint16_host2usb(lang),
     533            USB_REQUEST_TYPE_STANDARD, USB_DESCTYPE_STRING,
     534            index, uint16_host2usb(lang),
    537535            (void **) &string, &string_size);
    538536        if (rc != EOK) {
Note: See TracChangeset for help on using the changeset viewer.