Changeset 51b46f2 in mainline


Ignore:
Timestamp:
2011-03-01T15:39:52Z (13 years ago)
Author:
Vojtech Horky <vojtechhorky@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
e135751
Parents:
0e3505a (diff), cc44f7e (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge development/ changes

Location:
uspace
Files:
1 added
1 deleted
35 edited
1 moved

Legend:

Unmodified
Added
Removed
  • uspace/drv/pciintel/pci.c

    r0e3505a r51b46f2  
    320320        /* Get the value of the BAR. */
    321321        val = pci_conf_read_32(fun, addr);
     322
     323#define IO_MASK  (~0x3)
     324#define MEM_MASK (~0xf)
    322325       
    323326        io = (bool) (val & 1);
    324327        if (io) {
    325328                addrw64 = false;
     329                mask = IO_MASK;
    326330        } else {
     331                mask = MEM_MASK;
    327332                switch ((val >> 1) & 3) {
    328333                case 0:
     
    340345        /* Get the address mask. */
    341346        pci_conf_write_32(fun, addr, 0xffffffff);
    342         mask = pci_conf_read_32(fun, addr);
     347        mask &= pci_conf_read_32(fun, addr);
    343348       
    344349        /* Restore the original value. */
     
    659664size_t pci_bar_mask_to_size(uint32_t mask)
    660665{
    661         return ((mask & 0xfffffff0) ^ 0xffffffff) + 1;
     666        size_t size = mask & ~(mask - 1);
     667        return size;
    662668}
    663669
  • uspace/drv/uhci-hcd/Makefile

    r0e3505a r51b46f2  
    3939        uhci.c \
    4040        uhci_struct/transfer_descriptor.c \
     41        utils/device_keeper.c \
    4142        pci.c \
    4243        batch.c
  • uspace/drv/uhci-hcd/batch.c

    r0e3505a r51b46f2  
    3333 */
    3434#include <errno.h>
     35#include <str_error.h>
    3536
    3637#include <usb/debug.h>
     
    5354batch_t * batch_get(ddf_fun_t *fun, usb_target_t target,
    5455    usb_transfer_type_t transfer_type, size_t max_packet_size,
    55     dev_speed_t speed, char *buffer, size_t size,
     56    usb_speed_t speed, char *buffer, size_t size,
    5657    char* setup_buffer, size_t setup_size,
    5758    usbhc_iface_transfer_in_callback_t func_in,
     
    133134
    134135        queue_head_element_td(instance->qh, addr_to_phys(instance->tds));
     136        usb_log_debug("Batch(%p) %d:%d memory structures ready.\n",
     137            instance, target.address, target.endpoint);
    135138        return instance;
    136139}
     
    139142{
    140143        assert(instance);
    141         usb_log_debug("Checking(%p) %d packet for completion.\n",
     144        usb_log_debug2("Batch(%p) checking %d packet(s) for completion.\n",
    142145            instance, instance->packets);
    143146        instance->transfered_size = 0;
     
    151154                        if (i > 0)
    152155                                instance->transfered_size -= instance->setup_size;
     156                        usb_log_debug("Batch(%p) found error TD(%d):%x.\n",
     157                          instance, i, instance->tds[i].status);
    153158                        return true;
    154159                }
     
    156161                    transfer_descriptor_actual_size(&instance->tds[i]);
    157162        }
    158         /* This is just an ugly trick to support the old API */
    159163        instance->transfered_size -= instance->setup_size;
    160164        return true;
     
    168172        memcpy(instance->transport_buffer, instance->buffer, instance->buffer_size);
    169173
     174        const bool low_speed = instance->speed == USB_SPEED_LOW;
    170175        int toggle = 0;
    171176        /* setup stage */
    172177        transfer_descriptor_init(instance->tds, DEFAULT_ERROR_COUNT,
    173             instance->setup_size, toggle, false, instance->target,
    174             USB_PID_SETUP, instance->setup_buffer, &instance->tds[1]);
     178            instance->setup_size, toggle, false, low_speed,
     179            instance->target, USB_PID_SETUP, instance->setup_buffer,
     180            &instance->tds[1]);
    175181
    176182        /* data stage */
     
    182188
    183189                transfer_descriptor_init(&instance->tds[i], DEFAULT_ERROR_COUNT,
    184                     instance->max_packet_size, toggle++, false, instance->target,
    185                     USB_PID_OUT, data, &instance->tds[i + 1]);
     190                    instance->max_packet_size, toggle++, false, low_speed,
     191                    instance->target, USB_PID_OUT, data, &instance->tds[i + 1]);
    186192        }
    187193
     
    189195        i = instance->packets - 1;
    190196        transfer_descriptor_init(&instance->tds[i], DEFAULT_ERROR_COUNT,
    191             0, 1, false, instance->target, USB_PID_IN, NULL, NULL);
     197            0, 1, false, low_speed, instance->target, USB_PID_IN, NULL, NULL);
    192198
    193199        instance->tds[i].status |= TD_STATUS_COMPLETE_INTERRUPT_FLAG;
     200        usb_log_debug2("Control write last TD status: %x.\n",
     201                instance->tds[i].status);
    194202
    195203        instance->next_step = batch_call_out_and_dispose;
     204        usb_log_debug("Batch(%p) CONTROL WRITE initialized.\n", instance);
    196205        batch_schedule(instance);
    197206}
     
    201210        assert(instance);
    202211
     212        const bool low_speed = instance->speed == USB_SPEED_LOW;
    203213        int toggle = 0;
    204214        /* setup stage */
    205215        transfer_descriptor_init(instance->tds, DEFAULT_ERROR_COUNT,
    206             instance->setup_size, toggle, false, instance->target,
     216            instance->setup_size, toggle, false, low_speed, instance->target,
    207217            USB_PID_SETUP, instance->setup_buffer, &instance->tds[1]);
    208218
     
    215225
    216226                transfer_descriptor_init(&instance->tds[i], DEFAULT_ERROR_COUNT,
    217                     instance->max_packet_size, toggle, false, instance->target,
    218                     USB_PID_IN, data, &instance->tds[i + 1]);
     227                    instance->max_packet_size, toggle, false, low_speed,
     228                                instance->target, USB_PID_IN, data, &instance->tds[i + 1]);
    219229        }
    220230
     
    222232        i = instance->packets - 1;
    223233        transfer_descriptor_init(&instance->tds[i], DEFAULT_ERROR_COUNT,
    224             0, 1, false, instance->target, USB_PID_OUT, NULL, NULL);
     234            0, 1, false, low_speed, instance->target, USB_PID_OUT, NULL, NULL);
    225235
    226236        instance->tds[i].status |= TD_STATUS_COMPLETE_INTERRUPT_FLAG;
     237        usb_log_debug2("Control read last TD status: %x.\n",
     238                instance->tds[i].status);
    227239
    228240        instance->next_step = batch_call_in_and_dispose;
     241        usb_log_debug("Batch(%p) CONTROL READ initialized.\n", instance);
    229242        batch_schedule(instance);
    230243}
     
    234247        assert(instance);
    235248
     249        const bool low_speed = instance->speed == USB_SPEED_LOW;
    236250        int toggle = 1;
    237251        size_t i = 0;
     
    244258
    245259                transfer_descriptor_init(&instance->tds[i], DEFAULT_ERROR_COUNT,
    246                     instance->max_packet_size, toggle, false, instance->target,
    247                     USB_PID_IN, data, next);
     260                    instance->max_packet_size, toggle, false, low_speed,
     261                    instance->target, USB_PID_IN, data, next);
    248262        }
    249263
     
    251265
    252266        instance->next_step = batch_call_in_and_dispose;
     267        usb_log_debug("Batch(%p) INTERRUPT IN initialized.\n", instance);
    253268        batch_schedule(instance);
    254269}
     
    260275        memcpy(instance->transport_buffer, instance->buffer, instance->buffer_size);
    261276
     277        const bool low_speed = instance->speed == USB_SPEED_LOW;
    262278        int toggle = 1;
    263279        size_t i = 0;
     
    270286
    271287                transfer_descriptor_init(&instance->tds[i], DEFAULT_ERROR_COUNT,
    272                     instance->max_packet_size, toggle++, false, instance->target,
    273                     USB_PID_OUT, data, next);
     288                    instance->max_packet_size, toggle++, false, low_speed,
     289                    instance->target, USB_PID_OUT, data, next);
    274290        }
    275291
     
    277293
    278294        instance->next_step = batch_call_out_and_dispose;
     295        usb_log_debug("Batch(%p) INTERRUPT OUT initialized.\n", instance);
    279296        batch_schedule(instance);
    280297}
     
    288305
    289306        int err = instance->error;
    290         usb_log_info("Callback IN(%d): %d, %zu.\n", instance->transfer_type,
    291             err, instance->transfered_size);
     307        usb_log_debug("Batch(%p) callback IN(type:%d): %s(%d), %zu.\n",
     308            instance, instance->transfer_type, str_error(err), err,
     309            instance->transfered_size);
    292310
    293311        instance->callback_in(instance->fun,
     
    302320
    303321        int err = instance->error;
    304         usb_log_info("Callback OUT(%d): %d.\n", instance->transfer_type, err);
     322        usb_log_debug("Batch(%p) callback OUT(type:%d): %s(%d).\n",
     323            instance, instance->transfer_type, str_error(err), err);
    305324        instance->callback_out(instance->fun,
    306325            err, instance->arg);
     
    311330        assert(instance);
    312331        batch_call_in(instance);
    313         usb_log_debug("Disposing batch: %p.\n", instance);
     332        usb_log_debug("Batch(%p) disposing.\n", instance);
    314333        free32(instance->tds);
    315334        free32(instance->qh);
     
    323342        assert(instance);
    324343        batch_call_out(instance);
    325         usb_log_debug("Disposing batch: %p.\n", instance);
     344        usb_log_debug("Batch(%p) disposing.\n", instance);
    326345        free32(instance->tds);
    327346        free32(instance->qh);
     
    338357        return uhci_schedule(hc, instance);
    339358}
    340 /*----------------------------------------------------------------------------*/
    341 /* DEPRECATED FUNCTIONS NEEDED BY THE OLD API */
    342 void batch_control_setup_old(batch_t *instance)
    343 {
    344         assert(instance);
    345         instance->packets = 1;
    346 
    347         /* setup stage */
    348         transfer_descriptor_init(instance->tds, DEFAULT_ERROR_COUNT,
    349             instance->setup_size, 0, false, instance->target,
    350             USB_PID_SETUP, instance->setup_buffer, NULL);
    351 
    352         instance->next_step = batch_call_out_and_dispose;
    353         batch_schedule(instance);
    354 }
    355 /*----------------------------------------------------------------------------*/
    356 void batch_control_write_data_old(batch_t *instance)
    357 {
    358         assert(instance);
    359         instance->packets -= 2;
    360         batch_interrupt_out(instance);
    361 }
    362 /*----------------------------------------------------------------------------*/
    363 void batch_control_read_data_old(batch_t *instance)
    364 {
    365         assert(instance);
    366         instance->packets -= 2;
    367         batch_interrupt_in(instance);
    368 }
    369 /*----------------------------------------------------------------------------*/
    370 void batch_control_write_status_old(batch_t *instance)
    371 {
    372         assert(instance);
    373         instance->packets = 1;
    374         transfer_descriptor_init(instance->tds, DEFAULT_ERROR_COUNT,
    375             0, 1, false, instance->target, USB_PID_IN, NULL, NULL);
    376         instance->next_step = batch_call_in_and_dispose;
    377         batch_schedule(instance);
    378 }
    379 /*----------------------------------------------------------------------------*/
    380 void batch_control_read_status_old(batch_t *instance)
    381 {
    382         assert(instance);
    383         instance->packets = 1;
    384         transfer_descriptor_init(instance->tds, DEFAULT_ERROR_COUNT,
    385             0, 1, false, instance->target, USB_PID_OUT, NULL, NULL);
    386         instance->next_step = batch_call_out_and_dispose;
    387         batch_schedule(instance);
    388 }
    389359/**
    390360 * @}
  • uspace/drv/uhci-hcd/batch.h

    r0e3505a r51b46f2  
    4343#include "uhci_struct/queue_head.h"
    4444
    45 typedef enum {
    46         LOW_SPEED,
    47         FULL_SPEED,
    48 } dev_speed_t;
    49 
    5045typedef struct batch
    5146{
    5247        link_t link;
    53         dev_speed_t speed;
     48        usb_speed_t speed;
    5449        usb_target_t target;
    5550        usb_transfer_type_t transfer_type;
     
    7671batch_t * batch_get(ddf_fun_t *fun, usb_target_t target,
    7772    usb_transfer_type_t transfer_type, size_t max_packet_size,
    78     dev_speed_t speed, char *buffer, size_t size,
     73    usb_speed_t speed, char *buffer, size_t size,
    7974                char *setup_buffer, size_t setup_size,
    8075    usbhc_iface_transfer_in_callback_t func_in,
  • uspace/drv/uhci-hcd/iface.c

    r0e3505a r51b46f2  
    4141#include "iface.h"
    4242#include "uhci.h"
     43#include "utils/device_keeper.h"
    4344
    4445/*----------------------------------------------------------------------------*/
     
    4849        uhci_t *hc = fun_to_uhci(fun);
    4950        assert(hc);
    50         usb_address_keeping_reserve_default(&hc->address_manager);
     51        usb_log_debug("Default address request with speed %d.\n", speed);
     52        device_keeper_reserve_default(&hc->device_manager, speed);
    5153        return EOK;
    5254}
     
    5759        uhci_t *hc = fun_to_uhci(fun);
    5860        assert(hc);
    59         usb_address_keeping_release_default(&hc->address_manager);
     61        usb_log_debug("Default address release.\n");
     62        device_keeper_release_default(&hc->device_manager);
    6063        return EOK;
    6164}
     
    6770        uhci_t *hc = fun_to_uhci(fun);
    6871        assert(hc);
    69         *address = usb_address_keeping_request(&hc->address_manager);
     72        assert(address);
     73
     74        usb_log_debug("Address request with speed %d.\n", speed);
     75        *address = device_keeper_request(&hc->device_manager, speed);
     76        usb_log_debug("Address request with result: %d.\n", *address);
    7077        if (*address <= 0)
    7178          return *address;
     
    7986        uhci_t *hc = fun_to_uhci(fun);
    8087        assert(hc);
    81         usb_address_keeping_devman_bind(&hc->address_manager, address, handle);
     88        usb_log_debug("Address bind %d-%d.\n", address, handle);
     89        device_keeper_bind(&hc->device_manager, address, handle);
    8290        return EOK;
    8391}
     
    8896        uhci_t *hc = fun_to_uhci(fun);
    8997        assert(hc);
    90         usb_address_keeping_release_default(&hc->address_manager);
     98        usb_log_debug("Address release %d.\n", address);
     99        device_keeper_release(&hc->device_manager, address);
    91100        return EOK;
    92101}
    93102/*----------------------------------------------------------------------------*/
    94103static int interrupt_out(ddf_fun_t *fun, usb_target_t target,
    95     size_t max_packet_size,
    96     void *data, size_t size,
     104    size_t max_packet_size, void *data, size_t size,
    97105    usbhc_iface_transfer_out_callback_t callback, void *arg)
    98106{
    99         dev_speed_t speed = FULL_SPEED;
     107        assert(fun);
     108        uhci_t *hc = fun_to_uhci(fun);
     109        assert(hc);
     110        usb_speed_t speed = device_keeper_speed(&hc->device_manager, target.address);
     111
     112        usb_log_debug("Interrupt OUT %d:%d %zu(%zu).\n",
     113            target.address, target.endpoint, size, max_packet_size);
    100114
    101115        batch_t *batch = batch_get(fun, target, USB_TRANSFER_INTERRUPT,
     
    108122/*----------------------------------------------------------------------------*/
    109123static int interrupt_in(ddf_fun_t *fun, usb_target_t target,
    110     size_t max_packet_size,
    111     void *data, size_t size,
     124    size_t max_packet_size, void *data, size_t size,
    112125    usbhc_iface_transfer_in_callback_t callback, void *arg)
    113126{
    114         dev_speed_t speed = FULL_SPEED;
     127        assert(fun);
     128        uhci_t *hc = fun_to_uhci(fun);
     129        assert(hc);
     130        usb_speed_t speed = device_keeper_speed(&hc->device_manager, target.address);
     131        usb_log_debug("Interrupt IN %d:%d %zu(%zu).\n",
     132            target.address, target.endpoint, size, max_packet_size);
    115133
    116134        batch_t *batch = batch_get(fun, target, USB_TRANSFER_INTERRUPT,
     
    127145    usbhc_iface_transfer_out_callback_t callback, void *arg)
    128146{
    129         dev_speed_t speed = FULL_SPEED;
     147        assert(fun);
     148        uhci_t *hc = fun_to_uhci(fun);
     149        assert(hc);
     150        usb_speed_t speed = device_keeper_speed(&hc->device_manager, target.address);
     151        usb_log_debug("Control WRITE %d:%d %zu(%zu).\n",
     152            target.address, target.endpoint, size, max_packet_size);
    130153
    131154        batch_t *batch = batch_get(fun, target, USB_TRANSFER_CONTROL,
     
    143166    usbhc_iface_transfer_in_callback_t callback, void *arg)
    144167{
    145         dev_speed_t speed = FULL_SPEED;
    146 
     168        assert(fun);
     169        uhci_t *hc = fun_to_uhci(fun);
     170        assert(hc);
     171        usb_speed_t speed = device_keeper_speed(&hc->device_manager, target.address);
     172
     173        usb_log_debug("Control READ %d:%d %zu(%zu).\n",
     174            target.address, target.endpoint, size, max_packet_size);
    147175        batch_t *batch = batch_get(fun, target, USB_TRANSFER_CONTROL,
    148176            max_packet_size, speed, data, size, setup_data, setup_size, callback,
  • uspace/drv/uhci-hcd/main.c

    r0e3505a r51b46f2  
    8282        usb_log_info("uhci_add_device() called\n");
    8383
    84 
    85         uintptr_t io_reg_base;
    86         size_t io_reg_size;
    87         int irq;
     84        uintptr_t io_reg_base = 0;
     85        size_t io_reg_size = 0;
     86        int irq = 0;
    8887
    8988        int ret =
     
    9594            io_reg_base, io_reg_size, irq);
    9695
    97         ret = pci_enable_interrupts(device);
    98         CHECK_RET_RETURN(ret, "Failed(%d) to get enable interrupts:\n", ret);
     96//      ret = pci_enable_interrupts(device);
     97//      CHECK_RET_RETURN(ret, "Failed(%d) to get enable interrupts:\n", ret);
    9998
    10099        uhci_t *uhci_hc = malloc(sizeof(uhci_t));
     
    114113         */
    115114        device->driver_data = uhci_hc;
    116 
    117115        ret = register_interrupt_handler(device, irq, irq_handler,
    118116            &uhci_hc->interrupt_code);
     
    149147{
    150148        sleep(3);
    151         usb_log_enable(USB_LOG_LEVEL_INFO, NAME);
     149        usb_log_enable(USB_LOG_LEVEL_DEBUG, NAME);
    152150
    153151        return ddf_driver_main(&uhci_driver);
  • uspace/drv/uhci-hcd/pci.c

    r0e3505a r51b46f2  
    3838#include <devman.h>
    3939#include <device/hw_res.h>
     40
     41#include <usb/debug.h>
    4042
    4143#include "pci.h"
     
    8385                                irq = res->res.interrupt.irq;
    8486                                irq_found = true;
     87                                usb_log_debug2("Found interrupt: %d.\n", irq);
    8588                                break;
    8689                        case IO_RANGE:
    87                                 io_address = (uintptr_t)
    88                                     res->res.io_range.address;
     90                                io_address = res->res.io_range.address;
    8991                                io_size = res->res.io_range.size;
     92                                usb_log_debug2("Found io: %llx %zu.\n",
     93                                    res->res.io_range.address, res->res.io_range.size);
    9094                                io_found = true;
    9195                                break;
     
    105109        }
    106110
    107         if (io_reg_address != NULL) {
    108                 *io_reg_address = io_address;
    109         }
    110         if (io_reg_size != NULL) {
    111                 *io_reg_size = io_size;
    112         }
    113         if (irq_no != NULL) {
    114                 *irq_no = irq;
    115         }
     111        *io_reg_address = io_address;
     112        *io_reg_size = io_size;
     113        *irq_no = irq;
    116114
    117115        rc = EOK;
     
    127125            IPC_FLAG_BLOCKING);
    128126        bool enabled = hw_res_enable_interrupt(parent_phone);
     127        async_hangup(parent_phone);
    129128        return enabled ? EOK : EIO;
    130129}
  • uspace/drv/uhci-hcd/root_hub.c

    r0e3505a r51b46f2  
    3434#include <assert.h>
    3535#include <errno.h>
     36#include <str_error.h>
    3637#include <stdio.h>
     38#include <ops/hw_res.h>
     39
    3740#include <usb_iface.h>
    3841#include <usb/debug.h>
     
    4144#include "uhci.h"
    4245
     46/*----------------------------------------------------------------------------*/
    4347static int usb_iface_get_hc_handle_rh_impl(ddf_fun_t *root_hub_fun,
    4448    devman_handle_t *handle)
     
    5155        return EOK;
    5256}
    53 
     57/*----------------------------------------------------------------------------*/
    5458static int usb_iface_get_address_rh_impl(ddf_fun_t *fun, devman_handle_t handle,
    5559    usb_address_t *address)
     
    6165        assert(hc);
    6266
    63         usb_address_t addr = usb_address_keeping_find(&hc->address_manager,
     67        usb_address_t addr = device_keeper_find(&hc->device_manager,
    6468            handle);
    6569        if (addr < 0) {
     
    7377        return EOK;
    7478}
    75 
     79/*----------------------------------------------------------------------------*/
    7680usb_iface_t usb_iface_root_hub_fun_impl = {
    7781        .get_hc_handle = usb_iface_get_hc_handle_rh_impl,
    7882        .get_address = usb_iface_get_address_rh_impl
    7983};
     84/*----------------------------------------------------------------------------*/
     85static hw_resource_list_t *get_resource_list(ddf_fun_t *dev)
     86{
     87        assert(dev);
     88        ddf_fun_t *hc_ddf_instance = dev->driver_data;
     89        assert(hc_ddf_instance);
     90        uhci_t *hc = hc_ddf_instance->driver_data;
     91        assert(hc);
    8092
     93        //TODO: fix memory leak
     94        hw_resource_list_t *resource_list = malloc(sizeof(hw_resource_list_t));
     95        assert(resource_list);
     96        resource_list->count = 1;
     97        resource_list->resources = malloc(sizeof(hw_resource_t));
     98        assert(resource_list->resources);
     99        resource_list->resources[0].type = IO_RANGE;
     100        resource_list->resources[0].res.io_range.address =
     101            ((uintptr_t)hc->registers) + 0x10; // see UHCI design guide
     102        resource_list->resources[0].res.io_range.size = 4;
     103        resource_list->resources[0].res.io_range.endianness = LITTLE_ENDIAN;
     104
     105        return resource_list;
     106}
     107/*----------------------------------------------------------------------------*/
     108static hw_res_ops_t hw_res_iface = {
     109        .get_resource_list = get_resource_list,
     110        .enable_interrupt = NULL
     111};
     112/*----------------------------------------------------------------------------*/
    81113static ddf_dev_ops_t root_hub_ops = {
    82         .interfaces[USB_DEV_IFACE] = &usb_iface_root_hub_fun_impl
     114        .interfaces[USB_DEV_IFACE] = &usb_iface_root_hub_fun_impl,
     115        .interfaces[HW_RES_DEV_IFACE] = &hw_res_iface
    83116};
    84 
    85117/*----------------------------------------------------------------------------*/
    86118int setup_root_hub(ddf_fun_t **fun, ddf_dev_t *hc)
    87119{
    88120        assert(fun);
     121        assert(hc);
    89122        int ret;
    90123
     
    105138        ret = ddf_fun_add_match_id(hub, match_str, 100);
    106139        if (ret != EOK) {
    107                 usb_log_error("Failed to add root hub match id.\n");
     140                usb_log_error("Failed(%d) to add root hub match id: %s\n",
     141                    ret, str_error(ret));
    108142                ddf_fun_destroy(hub);
    109                 return ENOMEM;
     143                return ret;
    110144        }
    111145
  • uspace/drv/uhci-hcd/transfer_list.c

    r0e3505a r51b46f2  
    7070        assert(instance);
    7171        assert(batch);
     72        usb_log_debug2("Adding batch(%p) to queue %s.\n", batch, instance->name);
    7273
    7374        uint32_t pa = (uintptr_t)addr_to_phys(batch->qh);
     
    8384                list_append(&batch->link, &instance->batch_list);
    8485                instance->queue_head->element = pa;
    85                 usb_log_debug2("Added batch(%p) to queue %s first.\n",
     86                usb_log_debug("Batch(%p) added to queue %s first.\n",
    8687                        batch, instance->name);
    8788                fibril_mutex_unlock(&instance->guard);
     
    9697        queue_head_append_qh(last->qh, pa);
    9798        list_append(&batch->link, &instance->batch_list);
    98         usb_log_debug2("Added batch(%p) to queue %s last, first is %p.\n",
     99        usb_log_debug("Batch(%p) added to queue %s last, first is %p.\n",
    99100                batch, instance->name, first );
    100101        fibril_mutex_unlock(&instance->guard);
     
    108109        assert(instance->queue_head);
    109110        assert(batch->qh);
     111        usb_log_debug2("Removing batch(%p) from queue %s.\n", batch, instance->name);
    110112
    111113        /* I'm the first one here */
    112114        if (batch->link.prev == &instance->batch_list) {
    113                 usb_log_debug("Removing batch %p was first, next element %x.\n",
    114                         batch, batch->qh->next_queue);
     115                usb_log_debug("Batch(%p) removed (FIRST) from queue %s, next element %x.\n",
     116                        batch, instance->name, batch->qh->next_queue);
    115117                instance->queue_head->element = batch->qh->next_queue;
    116118        } else {
    117                 usb_log_debug("Removing batch %p was NOT first, next element %x.\n",
    118                         batch, batch->qh->next_queue);
     119                usb_log_debug("Batch(%p) removed (NOT FIRST) from queue, next element %x.\n",
     120                        batch, instance->name, batch->qh->next_queue);
    119121                batch_t *prev = list_get_instance(batch->link.prev, batch_t, link);
    120122                prev->qh->next_queue = batch->qh->next_queue;
     
    123125}
    124126/*----------------------------------------------------------------------------*/
    125 void transfer_list_check(transfer_list_t *instance)
     127void transfer_list_remove_finished(transfer_list_t *instance)
    126128{
    127129        assert(instance);
     130
     131        LIST_INITIALIZE(done);
     132
    128133        fibril_mutex_lock(&instance->guard);
    129134        link_t *current = instance->batch_list.next;
     
    134139                if (batch_is_complete(batch)) {
    135140                        transfer_list_remove_batch(instance, batch);
    136                         batch->next_step(batch);
     141                        list_append(current, &done);
    137142                }
    138143                current = next;
    139144        }
    140145        fibril_mutex_unlock(&instance->guard);
     146
     147        while (!list_empty(&done)) {
     148                link_t *item = done.next;
     149                list_remove(item);
     150                batch_t *batch = list_get_instance(item, batch_t, link);
     151                batch->next_step(batch);
     152        }
    141153}
    142154/**
  • uspace/drv/uhci-hcd/transfer_list.h

    r0e3505a r51b46f2  
    6060        queue_head_dispose(instance->queue_head);
    6161}
    62 void transfer_list_check(transfer_list_t *instance);
     62void transfer_list_remove_finished(transfer_list_t *instance);
    6363
    6464void transfer_list_add_batch(transfer_list_t *instance, batch_t *batch);
  • uspace/drv/uhci-hcd/uhci-hcd.ma

    r0e3505a r51b46f2  
    1110 pci/ven=8086&dev=7020
    2210 pci/ven=8086&dev=7112
     3
     410 pci/ven=8086&dev=27c8
     510 pci/ven=8086&dev=27c9
     610 pci/ven=8086&dev=27ca
     710 pci/ven=8086&dev=27cb
     8
     9
     1010 pci/ven=8086&dev=2830
     1110 pci/ven=8086&dev=2831
     1210 pci/ven=8086&dev=2832
     1310 pci/ven=8086&dev=2834
     1410 pci/ven=8086&dev=2835
     15
     1610 pci/ven=8086&dev=2934
     1710 pci/ven=8086&dev=2935
     1810 pci/ven=8086&dev=2936
     1910 pci/ven=8086&dev=2937
     2010 pci/ven=8086&dev=2938
     2110 pci/ven=8086&dev=2939
  • uspace/drv/uhci-hcd/uhci.c

    r0e3505a r51b46f2  
    4848        {
    4949                .cmd = CMD_PIO_READ_16,
    50                 .addr = (void*)0xc022,
     50                .addr = NULL, /* patched for every instance */
    5151                .dstarg = 1
    5252        },
    5353        {
    5454                .cmd = CMD_PIO_WRITE_16,
    55                 .addr = (void*)0xc022,
     55                .addr = NULL, /* pathed for every instance */
    5656                .value = 0x1f
    5757        },
     
    6868        assert(hc);
    6969
    70         usb_address_t addr = usb_address_keeping_find(&hc->address_manager,
     70        usb_address_t addr = device_keeper_find(&hc->device_manager,
    7171            handle);
    7272        if (addr < 0) {
     
    8080        return EOK;
    8181}
    82 
    83 
     82/*----------------------------------------------------------------------------*/
    8483static usb_iface_t hc_usb_iface = {
    8584        .get_hc_handle = usb_iface_get_hc_handle_hc_impl,
     
    8988static ddf_dev_ops_t uhci_ops = {
    9089        .interfaces[USB_DEV_IFACE] = &hc_usb_iface,
    91         .interfaces[USBHC_DEV_IFACE] = &uhci_iface
     90        .interfaces[USBHC_DEV_IFACE] = &uhci_iface,
    9291};
    9392
     
    102101        bool low_speed, usb_transfer_type_t, size_t size);
    103102
    104 #define CHECK_RET_RETURN(ret, message...) \
     103
     104int uhci_init(uhci_t *instance, ddf_dev_t *dev, void *regs, size_t reg_size)
     105{
     106        assert(reg_size >= sizeof(regs_t));
     107        int ret;
     108
     109#define CHECK_RET_DEST_FUN_RETURN(ret, message...) \
    105110        if (ret != EOK) { \
    106111                usb_log_error(message); \
     112                if (instance->ddf_instance) \
     113                        ddf_fun_destroy(instance->ddf_instance); \
    107114                return ret; \
    108115        } else (void) 0
    109 
    110 int uhci_init(uhci_t *instance, ddf_dev_t *dev, void *regs, size_t reg_size)
    111 {
    112         assert(reg_size >= sizeof(regs_t));
    113         int ret;
    114116
    115117        /*
     
    117119         */
    118120        instance->ddf_instance = ddf_fun_create(dev, fun_exposed, "uhci");
    119         if (instance->ddf_instance == NULL) {
    120                 usb_log_error("Failed to create UHCI device function.\n");
    121                 return ENOMEM;
    122         }
     121        ret = (instance->ddf_instance == NULL) ? ENOMEM : EOK;
     122        CHECK_RET_DEST_FUN_RETURN(ret, "Failed to create UHCI device function.\n");
     123
    123124        instance->ddf_instance->ops = &uhci_ops;
    124125        instance->ddf_instance->driver_data = instance;
    125126
    126127        ret = ddf_fun_bind(instance->ddf_instance);
    127         CHECK_RET_RETURN(ret, "Failed to bind UHCI device function: %s.\n",
    128             str_error(ret));
     128        CHECK_RET_DEST_FUN_RETURN(ret, "Failed(%d) to bind UHCI device function: %s.\n",
     129            ret, str_error(ret));
    129130
    130131        /* allow access to hc control registers */
    131132        regs_t *io;
    132133        ret = pio_enable(regs, reg_size, (void**)&io);
    133         CHECK_RET_RETURN(ret, "Failed to gain access to registers at %p.\n", io);
     134        CHECK_RET_DEST_FUN_RETURN(ret, "Failed(%d) to gain access to registers at %p: %s.\n",
     135            ret, str_error(ret), io);
    134136        instance->registers = io;
    135         usb_log_debug("Device registers accessible.\n");
     137        usb_log_debug("Device registers at %p(%u) accessible.\n", io, reg_size);
    136138
    137139        ret = uhci_init_mem_structures(instance);
    138         CHECK_RET_RETURN(ret, "Failed to initialize memory structures.\n");
     140        CHECK_RET_DEST_FUN_RETURN(ret, "Failed to initialize UHCI memory structures.\n");
    139141
    140142        uhci_init_hw(instance);
    141143
    142144        instance->cleaner = fibril_create(uhci_interrupt_emulator, instance);
    143 //      fibril_add_ready(instance->cleaner);
     145        fibril_add_ready(instance->cleaner);
    144146
    145147        instance->debug_checker = fibril_create(uhci_debug_checker, instance);
    146148        fibril_add_ready(instance->debug_checker);
    147149
    148         return EOK;
     150        usb_log_info("Started UHCI driver.\n");
     151        return EOK;
     152#undef CHECK_RET_DEST_FUN_RETURN
    149153}
    150154/*----------------------------------------------------------------------------*/
    151155void uhci_init_hw(uhci_t *instance)
    152156{
     157        /* reset everything, who knows what touched it before us */
     158        pio_write_16(&instance->registers->usbcmd, UHCI_CMD_GLOBAL_RESET);
     159        async_usleep(10000); /* 10ms according to USB spec */
     160        pio_write_16(&instance->registers->usbcmd, 0);
     161
     162        /* reset hc, all states and counters */
     163        pio_write_16(&instance->registers->usbcmd, UHCI_CMD_HCRESET);
     164        while ((pio_read_16(&instance->registers->usbcmd) & UHCI_CMD_HCRESET) != 0)
     165                { async_usleep(10); }
    153166
    154167        /* set framelist pointer */
     
    163176        pio_write_16(&instance->registers->usbcmd,
    164177            UHCI_CMD_RUN_STOP | UHCI_CMD_MAX_PACKET | UHCI_CMD_CONFIGURE);
    165         usb_log_debug("Started UHCI HC.\n");
    166178}
    167179/*----------------------------------------------------------------------------*/
     
    169181{
    170182        assert(instance);
     183#define CHECK_RET_DEST_CMDS_RETURN(ret, message...) \
     184        if (ret != EOK) { \
     185                usb_log_error(message); \
     186                if (instance->interrupt_code.cmds != NULL) \
     187                        free(instance->interrupt_code.cmds); \
     188                return ret; \
     189        } else (void) 0
    171190
    172191        /* init interrupt code */
    173         irq_cmd_t *interrupt_commands = malloc(sizeof(uhci_cmds));
    174         if (interrupt_commands == NULL) {
    175                 return ENOMEM;
    176         }
    177         memcpy(interrupt_commands, uhci_cmds, sizeof(uhci_cmds));
    178         interrupt_commands[0].addr = (void*)&instance->registers->usbsts;
    179         interrupt_commands[1].addr = (void*)&instance->registers->usbsts;
    180         instance->interrupt_code.cmds = interrupt_commands;
    181         instance->interrupt_code.cmdcount =
    182             sizeof(uhci_cmds) / sizeof(irq_cmd_t);
     192        instance->interrupt_code.cmds = malloc(sizeof(uhci_cmds));
     193        int ret = (instance->interrupt_code.cmds == NULL) ? ENOMEM : EOK;
     194        CHECK_RET_DEST_CMDS_RETURN(ret, "Failed to allocate interrupt cmds space.\n");
     195
     196        {
     197                irq_cmd_t *interrupt_commands = instance->interrupt_code.cmds;
     198                memcpy(interrupt_commands, uhci_cmds, sizeof(uhci_cmds));
     199                interrupt_commands[0].addr = (void*)&instance->registers->usbsts;
     200                interrupt_commands[1].addr = (void*)&instance->registers->usbsts;
     201                instance->interrupt_code.cmdcount =
     202                                sizeof(uhci_cmds) / sizeof(irq_cmd_t);
     203        }
    183204
    184205        /* init transfer lists */
    185         int ret = uhci_init_transfer_lists(instance);
    186         CHECK_RET_RETURN(ret, "Failed to initialize transfer lists.\n");
     206        ret = uhci_init_transfer_lists(instance);
     207        CHECK_RET_DEST_CMDS_RETURN(ret, "Failed to initialize transfer lists.\n");
    187208        usb_log_debug("Initialized transfer lists.\n");
    188209
     
    190211        instance->frame_list = get_page();
    191212        ret = instance ? EOK : ENOMEM;
    192         CHECK_RET_RETURN(ret, "Failed to get frame list page.\n");
     213        CHECK_RET_DEST_CMDS_RETURN(ret, "Failed to get frame list page.\n");
    193214        usb_log_debug("Initialized frame list.\n");
    194215
     
    197218          instance->transfers_interrupt.queue_head_pa
    198219          | LINK_POINTER_QUEUE_HEAD_FLAG;
     220
    199221        unsigned i = 0;
    200222        for(; i < UHCI_FRAME_LIST_COUNT; ++i) {
     
    203225
    204226        /* init address keeper(libusb) */
    205         usb_address_keeping_init(&instance->address_manager, USB11_ADDRESS_MAX);
    206         usb_log_debug("Initialized address manager.\n");
    207 
    208         return EOK;
     227        device_keeper_init(&instance->device_manager);
     228        usb_log_debug("Initialized device manager.\n");
     229
     230        return EOK;
     231#undef CHECK_RET_DEST_CMDS_RETURN
    209232}
    210233/*----------------------------------------------------------------------------*/
     
    212235{
    213236        assert(instance);
     237#define CHECK_RET_CLEAR_RETURN(ret, message...) \
     238        if (ret != EOK) { \
     239                usb_log_error(message); \
     240                transfer_list_fini(&instance->transfers_bulk_full); \
     241                transfer_list_fini(&instance->transfers_control_full); \
     242                transfer_list_fini(&instance->transfers_control_slow); \
     243                transfer_list_fini(&instance->transfers_interrupt); \
     244                return ret; \
     245        } else (void) 0
    214246
    215247        /* initialize TODO: check errors */
    216248        int ret;
    217249        ret = transfer_list_init(&instance->transfers_bulk_full, "BULK_FULL");
    218         assert(ret == EOK);
     250        CHECK_RET_CLEAR_RETURN(ret, "Failed to init BULK list.");
    219251        ret = transfer_list_init(&instance->transfers_control_full, "CONTROL_FULL");
    220         assert(ret == EOK);
     252        CHECK_RET_CLEAR_RETURN(ret, "Failed to init CONTROL FULL list.");
    221253        ret = transfer_list_init(&instance->transfers_control_slow, "CONTROL_SLOW");
    222         assert(ret == EOK);
     254        CHECK_RET_CLEAR_RETURN(ret, "Failed to init CONTROL SLOW list.");
    223255        ret = transfer_list_init(&instance->transfers_interrupt, "INTERRUPT");
    224         assert(ret == EOK);
     256        CHECK_RET_CLEAR_RETURN(ret, "Failed to init INTERRUPT list.");
    225257
    226258        transfer_list_set_next(&instance->transfers_control_full,
     
    249281
    250282        return EOK;
     283#undef CHECK_RET_CLEAR_RETURN
    251284}
    252285/*----------------------------------------------------------------------------*/
     
    255288        assert(instance);
    256289        assert(batch);
    257         const int low_speed = (batch->speed == LOW_SPEED);
     290        const int low_speed = (batch->speed == USB_SPEED_LOW);
    258291        if (!allowed_usb_packet(
    259292            low_speed, batch->transfer_type, batch->max_packet_size)) {
     
    276309{
    277310        assert(instance);
    278         if ((status & (UHCI_STATUS_INTERRUPT | UHCI_STATUS_ERROR_INTERRUPT)) == 0)
    279                 return;
    280         usb_log_debug("UHCI interrupt: %X.\n", status);
    281         transfer_list_check(&instance->transfers_interrupt);
    282         transfer_list_check(&instance->transfers_control_slow);
    283         transfer_list_check(&instance->transfers_control_full);
    284         transfer_list_check(&instance->transfers_bulk_full);
     311//      if ((status & (UHCI_STATUS_INTERRUPT | UHCI_STATUS_ERROR_INTERRUPT)) == 0)
     312//              return;
     313//      usb_log_debug2("UHCI interrupt: %X.\n", status);
     314        transfer_list_remove_finished(&instance->transfers_interrupt);
     315        transfer_list_remove_finished(&instance->transfers_control_slow);
     316        transfer_list_remove_finished(&instance->transfers_control_full);
     317        transfer_list_remove_finished(&instance->transfers_bulk_full);
    285318}
    286319/*----------------------------------------------------------------------------*/
     
    291324        assert(instance);
    292325
    293         while(1) {
     326        while (1) {
    294327                uint16_t status = pio_read_16(&instance->registers->usbsts);
     328                if (status != 0)
     329                        usb_log_debug2("UHCI status: %x.\n", status);
     330                status |= 1;
    295331                uhci_interrupt(instance, status);
    296                 async_usleep(UHCI_CLEANER_TIMEOUT);
     332                pio_write_16(&instance->registers->usbsts, 0x1f);
     333                async_usleep(UHCI_CLEANER_TIMEOUT * 5);
    297334        }
    298335        return EOK;
     
    307344                const uint16_t sts = pio_read_16(&instance->registers->usbsts);
    308345                const uint16_t intr = pio_read_16(&instance->registers->usbintr);
    309                 usb_log_debug("Command: %X Status: %X Interrupts: %x\n",
    310                     cmd, sts, intr);
    311 
    312                 uintptr_t frame_list = pio_read_32(&instance->registers->flbaseadd);
     346                if (((cmd & UHCI_CMD_RUN_STOP) != 1) || (sts != 0)) {
     347                        usb_log_debug2("Command: %X Status: %X Intr: %x\n",
     348                            cmd, sts, intr);
     349                }
     350
     351                uintptr_t frame_list =
     352                    pio_read_32(&instance->registers->flbaseadd) & ~0xfff;
    313353                if (frame_list != addr_to_phys(instance->frame_list)) {
    314354                        usb_log_debug("Framelist address: %p vs. %p.\n",
  • uspace/drv/uhci-hcd/uhci.h

    r0e3505a r51b46f2  
    4141#include <ddi.h>
    4242
    43 #include <usb/addrkeep.h>
    4443#include <usbhc_iface.h>
    4544
     45#include "batch.h"
    4646#include "transfer_list.h"
    47 #include "batch.h"
     47#include "utils/device_keeper.h"
    4848
    4949typedef struct uhci_regs {
     
    8282
    8383typedef struct uhci {
    84         usb_address_keeping_t address_manager;
     84        device_keeper_t device_manager;
     85
    8586        volatile regs_t *registers;
    8687
  • uspace/drv/uhci-hcd/uhci_struct/transfer_descriptor.c

    r0e3505a r51b46f2  
    3939
    4040void transfer_descriptor_init(transfer_descriptor_t *instance,
    41     int error_count, size_t size, bool toggle, bool isochronous,
     41    int error_count, size_t size, bool toggle, bool isochronous, bool low_speed,
    4242    usb_target_t target, int pid, void *buffer, transfer_descriptor_t *next)
    4343{
     
    5050        instance->status = 0
    5151          | ((error_count & TD_STATUS_ERROR_COUNT_MASK) << TD_STATUS_ERROR_COUNT_POS)
     52                | (low_speed ? TD_STATUS_LOW_SPEED_FLAG : 0)
    5253          | TD_STATUS_ERROR_ACTIVE;
    5354
     
    6667        }
    6768
    68         usb_log_info("Created TD: %X:%X:%X:%X(%p).\n",
     69        usb_log_debug2("Created TD: %X:%X:%X:%X(%p).\n",
    6970                instance->next, instance->status, instance->device,
    7071          instance->buffer_ptr, buffer);
    71 #if 0
    72         if (size) {
    73                 unsigned char * buff = buffer;
    74                 uhci_print_verbose("TD Buffer dump(%p-%dB): ", buffer, size);
    75                 unsigned i = 0;
    76                 /* TODO: Verbose? */
    77                 for (; i < size; ++i) {
    78                         printf((i & 1) ? "%x " : "%x", buff[i]);
    79                 }
    80                 printf("\n");
    81         }
    82 #endif
    8372}
    8473/*----------------------------------------------------------------------------*/
     
    8877
    8978        if ((instance->status & TD_STATUS_ERROR_STALLED) != 0)
    90                 return EIO;
     79                return ESTALL;
    9180
    9281        if ((instance->status & TD_STATUS_ERROR_CRC) != 0)
    93                 return EAGAIN;
     82                return EBADCHECKSUM;
    9483
    9584        if ((instance->status & TD_STATUS_ERROR_BUFFER) != 0)
  • uspace/drv/uhci-hcd/uhci_struct/transfer_descriptor.h

    r0e3505a r51b46f2  
    9292
    9393void transfer_descriptor_init(transfer_descriptor_t *instance,
    94     int error_count, size_t size, bool toggle, bool isochronous,
     94    int error_count, size_t size, bool toggle, bool isochronous, bool low_speed,
    9595    usb_target_t target, int pid, void *buffer, transfer_descriptor_t * next);
    9696
  • uspace/drv/uhci-hcd/utils/device_keeper.h

    r0e3505a r51b46f2  
    11/*
    2  * Copyright (c) 2010 Lubos Slovak
     2 * Copyright (c) 2011 Jan Vesely
    33 * All rights reserved.
    44 *
     
    2727 */
    2828
    29 /** @addtogroup drvusbhid
     29/** @addtogroup drvusbuhci
    3030 * @{
    3131 */
    3232/** @file
    33  * Descriptor parser.
     33 * @brief UHCI driver
    3434 */
     35#ifndef UTILS_DEVICE_KEEPER_H
     36#define UTILS_DEVICE_KEEPER_H
     37#include <devman.h>
     38#include <fibril_synch.h>
     39#include <usb/usb.h>
    3540
    36 #ifndef USBHID_DESCPARSER_H_
    37 #define USBHID_DESCPARSER_H_
     41#define USB_ADDRESS_COUNT (USB11_ADDRESS_MAX + 1)
    3842
    39 #include "hid.h"
     43struct usb_device_info {
     44        usb_speed_t speed;
     45        bool occupied;
     46        devman_handle_t handle;
     47};
    4048
    41 int usbkbd_parse_descriptors(const uint8_t *data, size_t size,
    42                              usb_hid_configuration_t *config);
     49typedef struct device_keeper {
     50        struct usb_device_info devices[USB_ADDRESS_COUNT];
     51        fibril_mutex_t guard;
     52        fibril_condvar_t default_address_occupied;
     53        usb_address_t last_address;
     54} device_keeper_t;
    4355
    44 void usbkbd_print_config(const usb_hid_configuration_t *config);
     56void device_keeper_init(device_keeper_t *instance);
     57void device_keeper_reserve_default(
     58    device_keeper_t *instance, usb_speed_t speed);
     59void device_keeper_release_default(device_keeper_t *instance);
    4560
     61usb_address_t device_keeper_request(
     62    device_keeper_t *instance, usb_speed_t speed);
     63void device_keeper_bind(
     64    device_keeper_t *instance, usb_address_t address, devman_handle_t handle);
     65void device_keeper_release(device_keeper_t *instance, usb_address_t address);
     66usb_address_t device_keeper_find(
     67    device_keeper_t *instance, devman_handle_t handle);
     68
     69usb_speed_t device_keeper_speed(
     70    device_keeper_t *instance, usb_address_t address);
    4671#endif
    47 
    4872/**
    4973 * @}
  • uspace/drv/uhci-rhd/main.c

    r0e3505a r51b46f2  
    3333 */
    3434#include <ddf/driver.h>
     35#include <devman.h>
     36#include <device/hw_res.h>
    3537#include <usb_iface.h>
    3638#include <usb/ddfiface.h>
     
    4345
    4446#define NAME "uhci-rhd"
     47static int hc_get_my_registers(ddf_dev_t *dev,
     48    uintptr_t *io_reg_address, size_t *io_reg_size);
    4549
    4650static int usb_iface_get_hc_handle(ddf_fun_t *fun, devman_handle_t *handle)
     
    8084        }
    8185
    82         /* TODO: get register values from hc */
    83         int ret = uhci_root_hub_init(rh, (void*)0xc030, 4, device);
     86        uintptr_t io_regs = 0;
     87        size_t io_size = 0;
     88
     89        int ret = hc_get_my_registers(device, &io_regs, &io_size);
     90        assert(ret == EOK);
     91
     92        /* TODO: verify values from hc */
     93        usb_log_info("I/O regs at 0x%X (size %zu).\n", io_regs, io_size);
     94        ret = uhci_root_hub_init(rh, (void*)io_regs, io_size, device);
    8495        if (ret != EOK) {
    8596                usb_log_error("Failed(%d) to initialize driver instance.\n", ret);
     
    102113        .driver_ops = &uhci_rh_driver_ops
    103114};
    104 
     115/*----------------------------------------------------------------------------*/
    105116int main(int argc, char *argv[])
    106117{
    107         usb_log_enable(USB_LOG_LEVEL_INFO, NAME);
     118        usb_log_enable(USB_LOG_LEVEL_DEBUG, NAME);
    108119        return ddf_driver_main(&uhci_rh_driver);
     120}
     121/*----------------------------------------------------------------------------*/
     122int hc_get_my_registers(ddf_dev_t *dev,
     123    uintptr_t *io_reg_address, size_t *io_reg_size)
     124{
     125        assert(dev != NULL);
     126
     127        int parent_phone = devman_parent_device_connect(dev->handle,
     128            IPC_FLAG_BLOCKING);
     129        if (parent_phone < 0) {
     130                return parent_phone;
     131        }
     132
     133        int rc;
     134
     135        hw_resource_list_t hw_resources;
     136        rc = hw_res_get_resource_list(parent_phone, &hw_resources);
     137        if (rc != EOK) {
     138                goto leave;
     139        }
     140
     141        uintptr_t io_address = 0;
     142        size_t io_size = 0;
     143        bool io_found = false;
     144
     145        size_t i;
     146        for (i = 0; i < hw_resources.count; i++) {
     147                hw_resource_t *res = &hw_resources.resources[i];
     148                switch (res->type) {
     149                        case IO_RANGE:
     150                                io_address = (uintptr_t)
     151                                    res->res.io_range.address;
     152                                io_size = res->res.io_range.size;
     153                                io_found = true;
     154                                break;
     155                        default:
     156                                break;
     157                }
     158        }
     159
     160        if (!io_found) {
     161                rc = ENOENT;
     162                goto leave;
     163        }
     164
     165        if (io_reg_address != NULL) {
     166                *io_reg_address = io_address;
     167        }
     168        if (io_reg_size != NULL) {
     169                *io_reg_size = io_size;
     170        }
     171        rc = EOK;
     172leave:
     173        async_hangup(parent_phone);
     174
     175        return rc;
    109176}
    110177/**
  • uspace/drv/uhci-rhd/port.c

    r0e3505a r51b46f2  
    3434#include <errno.h>
    3535#include <str_error.h>
     36#include <fibril_synch.h>
    3637
    3738#include <usb/usb.h>    /* usb_address_t */
     
    4546#include "port_status.h"
    4647
    47 static int uhci_port_new_device(uhci_port_t *port);
     48static int uhci_port_new_device(uhci_port_t *port, uint16_t status);
    4849static int uhci_port_remove_device(uhci_port_t *port);
    4950static int uhci_port_set_enabled(uhci_port_t *port, bool enabled);
    5051static int uhci_port_check(void *port);
     52static int new_device_enable_port(int portno, void *arg);
    5153
    5254int uhci_port_init(
    5355  uhci_port_t *port, port_status_t *address, unsigned number,
    54   unsigned usec, ddf_dev_t *rh, int parent_phone)
     56  unsigned usec, ddf_dev_t *rh)
    5557{
    5658        assert(port);
     
    6971        port->checker = fibril_create(uhci_port_check, port);
    7072        if (port->checker == 0) {
    71                 usb_log_error(": failed to launch root hub fibril.");
     73                usb_log_error("Port(%p - %d): failed to launch root hub fibril.",
     74                    port->address, port->number);
    7275                return ENOMEM;
    7376        }
    7477        fibril_add_ready(port->checker);
    75         usb_log_debug(
    76           "Added fibril for port %d: %p.\n", number, port->checker);
     78        usb_log_debug("Port(%p - %d): Added fibril. %x\n",
     79            port->address, port->number, port->checker);
    7780        return EOK;
    7881}
     
    9093        uhci_port_t *port_instance = port;
    9194        assert(port_instance);
     95//      port_status_write(port_instance->address, 0);
     96
     97        unsigned count = 0;
    9298
    9399        while (1) {
     100                async_usleep(port_instance->wait_period_usec);
     101
    94102                /* read register value */
    95103                port_status_t port_status =
     
    97105
    98106                /* debug print */
    99                 usb_log_debug("Port %d status at %p: 0x%04x.\n",
    100                   port_instance->number, port_instance->address, port_status);
    101                 print_port_status(port_status);
    102 
    103                 if (port_status & STATUS_CONNECTED_CHANGED) {
     107                static fibril_mutex_t dbg_mtx = FIBRIL_MUTEX_INITIALIZER(dbg_mtx);
     108                fibril_mutex_lock(&dbg_mtx);
     109                usb_log_debug2("Port(%p - %d): Status: %#04x. === %u\n",
     110                  port_instance->address, port_instance->number, port_status, count++);
     111//              print_port_status(port_status);
     112                fibril_mutex_unlock(&dbg_mtx);
     113
     114                if ((port_status & STATUS_CONNECTED_CHANGED) != 0) {
     115                        usb_log_debug("Port(%p - %d): Connected change detected: %x.\n",
     116                            port_instance->address, port_instance->number, port_status);
     117
     118
    104119                        int rc = usb_hc_connection_open(
    105120                            &port_instance->hc_connection);
    106121                        if (rc != EOK) {
    107                                 usb_log_error("Failed to connect to HC.");
    108                                 goto next;
    109                         }
    110 
    111                         if (port_status & STATUS_CONNECTED) {
     122                                usb_log_error("Port(%p - %d): Failed to connect to HC.",
     123                                    port_instance->address, port_instance->number);
     124                                continue;
     125                        }
     126
     127                        /* remove any old device */
     128                        if (port_instance->attached_device) {
     129                                usb_log_debug("Port(%p - %d): Removing device.\n",
     130                                    port_instance->address, port_instance->number);
     131                                uhci_port_remove_device(port_instance);
     132                        }
     133
     134                        if ((port_status & STATUS_CONNECTED) != 0) {
    112135                                /* new device */
    113                                 uhci_port_new_device(port_instance);
     136                                uhci_port_new_device(port_instance, port_status);
    114137                        } else {
    115                                 uhci_port_remove_device(port_instance);
     138                                /* ack changes by writing one to WC bits */
     139                                port_status_write(port_instance->address, port_status);
     140                                usb_log_debug("Port(%p - %d): Change status ACK.\n",
     141                                                port_instance->address, port_instance->number);
    116142                        }
    117143
     
    119145                            &port_instance->hc_connection);
    120146                        if (rc != EOK) {
    121                                 usb_log_error("Failed to disconnect from HC.");
    122                                 goto next;
     147                                usb_log_error("Port(%p - %d): Failed to disconnect from HC.",
     148                                    port_instance->address, port_instance->number);
    123149                        }
    124150                }
    125         next:
    126                 async_usleep(port_instance->wait_period_usec);
    127151        }
    128152        return EOK;
     
    139163        uhci_port_t *port = (uhci_port_t *) arg;
    140164
    141         usb_log_debug("new_device_enable_port(%d)\n", port->number);
     165        usb_log_debug2("Port(%p - %d): new_device_enable_port.\n",
     166            port->address, port->number);
    142167
    143168        /*
     
    147172        async_usleep(100000);
    148173
    149         /* Enable the port. */
    150         uhci_port_set_enabled(port, true);
    151174
    152175        /* The hub maintains the reset signal to that port for 10 ms
     
    154177         */
    155178        {
    156                 usb_log_debug("Reset Signal start on port %d.\n",
    157                     port->number);
     179                usb_log_debug("Port(%p - %d): Reset Signal start.\n",
     180                    port->address, port->number);
    158181                port_status_t port_status =
    159182                        port_status_read(port->address);
     
    165188                port_status &= ~STATUS_IN_RESET;
    166189                port_status_write(port->address, port_status);
    167                 usb_log_debug("Reset Signal stop on port %d.\n",
    168                     port->number);
    169         }
    170 
    171         return EOK;
    172 }
    173 
    174 /*----------------------------------------------------------------------------*/
    175 static int uhci_port_new_device(uhci_port_t *port)
     190                usb_log_debug("Port(%p - %d): Reset Signal stop.\n",
     191                    port->address, port->number);
     192        }
     193
     194        /* Enable the port. */
     195        uhci_port_set_enabled(port, true);
     196
     197        return EOK;
     198}
     199
     200/*----------------------------------------------------------------------------*/
     201static int uhci_port_new_device(uhci_port_t *port, uint16_t status)
    176202{
    177203        assert(port);
    178204        assert(usb_hc_connection_is_opened(&port->hc_connection));
    179205
    180         usb_log_info("Detected new device on port %u.\n", port->number);
     206        usb_log_info("Port(%p-%d): Detected new device.\n",
     207            port->address, port->number);
    181208
    182209        usb_address_t dev_addr;
    183210        int rc = usb_hc_new_device_wrapper(port->rh, &port->hc_connection,
    184             USB_SPEED_FULL,
     211            ((status & STATUS_LOW_SPEED) != 0) ? USB_SPEED_LOW : USB_SPEED_FULL,
    185212            new_device_enable_port, port->number, port,
    186213            &dev_addr, &port->attached_device, NULL, NULL, NULL);
     214
    187215        if (rc != EOK) {
    188                 usb_log_error("Failed adding new device on port %u: %s.\n",
    189                     port->number, str_error(rc));
     216                usb_log_error("Port(%p-%d): Failed(%d) adding new device: %s.\n",
     217                    port->address, port->number, rc, str_error(rc));
    190218                uhci_port_set_enabled(port, false);
    191219                return rc;
    192220        }
    193221
    194         usb_log_info("New device on port %u has address %d (handle %zu).\n",
    195             port->number, dev_addr, port->attached_device);
     222        usb_log_info("Port(%p-%d): New device has address %d (handle %zu).\n",
     223            port->address, port->number, dev_addr, port->attached_device);
    196224
    197225        return EOK;
     
    201229static int uhci_port_remove_device(uhci_port_t *port)
    202230{
    203         usb_log_error("Don't know how to remove device %#x.\n",
    204                 (unsigned int)port->attached_device);
     231        usb_log_error("Port(%p-%d): Don't know how to remove device %#x.\n",
     232                port->address, port->number, (unsigned int)port->attached_device);
    205233//      uhci_port_set_enabled(port, false);
    206234        return EOK;
     
    223251        port_status_write(port->address, port_status);
    224252
    225         usb_log_info("%s port %d.\n",
    226           enabled ? "Enabled" : "Disabled", port->number);
     253        usb_log_info("Port(%p-%d): %sabled port.\n",
     254                port->address, port->number, enabled ? "En" : "Dis");
    227255        return EOK;
    228256}
  • uspace/drv/uhci-rhd/port.h

    r0e3505a r51b46f2  
    5555int uhci_port_init(
    5656  uhci_port_t *port, port_status_t *address, unsigned number,
    57   unsigned usec, ddf_dev_t *rh, int parent_phone);
     57  unsigned usec, ddf_dev_t *rh);
    5858
    5959void uhci_port_fini(uhci_port_t *port);
  • uspace/drv/uhci-rhd/port_status.c

    r0e3505a r51b46f2  
    4141struct flag_name
    4242{
    43         unsigned flag;
     43        uint16_t flag;
    4444        const char *name;
    4545};
     
    6565        for (;i < sizeof(flags)/sizeof(struct flag_name); ++i) {
    6666                usb_log_debug2("\t%s status: %s.\n", flags[i].name,
    67                   value & flags[i].flag ? "YES" : "NO");
     67                  ((value & flags[i].flag) != 0) ? "YES" : "NO");
    6868        }
    6969}
  • uspace/drv/uhci-rhd/port_status.h

    r0e3505a r51b46f2  
    4141typedef uint16_t port_status_t;
    4242
    43 enum {
    44         STATUS_CONNECTED         = 1 << 0,
    45         STATUS_CONNECTED_CHANGED = 1 << 1,
    46         STATUS_ENABLED           = 1 << 2,
    47         STATUS_ENABLED_CHANGED   = 1 << 3,
    48         STATUS_LINE_D_PLUS       = 1 << 4,
    49         STATUS_LINE_D_MINUS      = 1 << 5,
    50         STATUS_RESUME            = 1 << 6,
    51         STATUS_ALWAYS_ONE        = 1 << 7,
     43#define STATUS_CONNECTED         (1 << 0)
     44#define STATUS_CONNECTED_CHANGED (1 << 1)
     45#define STATUS_ENABLED           (1 << 2)
     46#define STATUS_ENABLED_CHANGED   (1 << 3)
     47#define STATUS_LINE_D_PLUS       (1 << 4)
     48#define STATUS_LINE_D_MINUS      (1 << 5)
     49#define STATUS_RESUME            (1 << 6)
     50#define STATUS_ALWAYS_ONE        (1 << 7)
    5251
    53         STATUS_LOW_SPEED = 1 <<  8,
    54         STATUS_IN_RESET  = 1 <<  9,
    55         STATUS_SUSPEND   = 1 << 12,
    56 };
     52#define STATUS_LOW_SPEED (1 <<  8)
     53#define STATUS_IN_RESET  (1 <<  9)
     54#define STATUS_SUSPEND   (1 << 12)
    5755
    5856static inline port_status_t port_status_read(port_status_t * address)
  • uspace/drv/uhci-rhd/root_hub.c

    r0e3505a r51b46f2  
    4040#include "root_hub.h"
    4141
    42 
    4342int uhci_root_hub_init(
    4443  uhci_root_hub_t *instance, void *addr, size_t size, ddf_dev_t *rh)
     
    4746        assert(rh);
    4847        int ret;
    49         ret = usb_hc_find(rh->handle, &instance->hc_handle);
    50         usb_log_info("rh found(%d) hc handle: %d.\n", ret, instance->hc_handle);
    51         if (ret != EOK) {
    52                 return ret;
    53         }
    5448
    5549        /* allow access to root hub registers */
    5650        assert(sizeof(port_status_t) * UHCI_ROOT_HUB_PORT_COUNT == size);
    5751        port_status_t *regs;
    58         ret = pio_enable(
    59           addr, sizeof(port_status_t) * UHCI_ROOT_HUB_PORT_COUNT, (void**)&regs);
     52        ret = pio_enable(addr, size, (void**)&regs);
    6053
    6154        if (ret < 0) {
     
    6760        unsigned i = 0;
    6861        for (; i < UHCI_ROOT_HUB_PORT_COUNT; ++i) {
    69                 /* connect to the parent device (HC) */
    70                 int parent_phone = devman_device_connect(instance->hc_handle, 0);
    71                 //usb_drv_hc_connect(rh, instance->hc_handle, 0);
    72                 if (parent_phone < 0) {
    73                         usb_log_error("Failed to connect to the HC device port %d.\n", i);
    74                         return parent_phone;
    75                 }
    7662                /* mind pointer arithmetics */
    77                 int ret = uhci_port_init(
    78                   &instance->ports[i], regs + i, i, ROOT_HUB_WAIT_USEC, rh, parent_phone);
     63                ret = uhci_port_init(
     64                  &instance->ports[i], regs + i, i, ROOT_HUB_WAIT_USEC, rh);
    7965                if (ret != EOK) {
    8066                        unsigned j = 0;
  • uspace/drv/uhci-rhd/root_hub.h

    r0e3505a r51b46f2  
    4141
    4242#define UHCI_ROOT_HUB_PORT_COUNT 2
    43 #define UHCI_ROOT_HUB_PORT_REGISTERS_OFFSET 0x10
    44 #define ROOT_HUB_WAIT_USEC 10000000 /* 10 seconds */
     43#define ROOT_HUB_WAIT_USEC 5000000 /* 5 seconds */
    4544
    4645typedef struct root_hub {
  • uspace/drv/usbhid/Makefile

    r0e3505a r51b46f2  
    3939SOURCES = \
    4040        main.c \
    41         descparser.c \
    42         descdump.c \
    4341        conv.c \
    4442        $(STOLEN_LAYOUT_SOURCES)
  • uspace/drv/usbhid/conv.c

    r0e3505a r51b46f2  
    3636#include <io/keycode.h>
    3737#include <stdint.h>
     38#include <stdio.h>
     39#include <usb/debug.h>
    3840#include "conv.h"
    3941
     
    141143        //[0xe7] = KC_R // TODO: right GUI
    142144       
     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       
    143163};
    144164
     
    189209
    190210        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       
    191224//      if (key != 0)
    192225//              kbd_push_ev(type, key);
  • uspace/drv/usbhid/hid.h

    r0e3505a r51b46f2  
    3737#define USBHID_HID_H_
    3838
     39#include <stdint.h>
     40
    3941#include <usb/classes/hid.h>
    4042#include <ddf/driver.h>
    4143#include <usb/pipes.h>
    42 
    43 /**
    44  *
    45  */
    46 typedef 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  */
    58 typedef struct {
    59         usb_standard_configuration_descriptor_t config_descriptor;
    60         usb_hid_iface_t *interfaces;
    61 } usb_hid_configuration_t;
    6244
    6345/**
     
    6850typedef struct {
    6951        ddf_dev_t *device;
    70         usb_hid_configuration_t *conf;
    71         usb_hid_report_parser_t *parser;
    7252
    7353        usb_device_connection_t wire;
    7454        usb_endpoint_pipe_t ctrl_pipe;
    7555        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;
    7668} usb_hid_dev_kbd_t;
    7769
    78 // TODO: more configurations!
    79 
    8070#endif
  • uspace/drv/usbhid/main.c

    r0e3505a r51b46f2  
    5151#include <usb/descriptor.h>
    5252#include <io/console.h>
     53#include <stdint.h>
     54#include <usb/dp.h>
    5355#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
    6061#define NAME "usbhid"
    6162
    62 #define GUESSED_POLL_ENDPOINT 1
     63//#define GUESSED_POLL_ENDPOINT 1
     64#define BOOTP_REPORT_SIZE 6
     65
     66static unsigned DEFAULT_ACTIVE_MODS = KM_NUM_LOCK;
    6367
    6468/** Keyboard polling endpoint description for boot protocol class. */
     
    120124
    121125static void dump_buffer(const char *msg, const uint8_t *buffer, size_t length)
    122 {
     126{uint8_t buffer[BUFFER_SIZE];
    123127        printf("%s\n", msg);
    124128       
     
    137141 */
    138142
    139 /** Currently active modifiers.
     143/** Currently active modifiers (locks is probably better word).
    140144 *
    141145 * TODO: put to device?
    142146 */
    143 static unsigned mods = KM_NUM_LOCK;
     147//static unsigned mods = KM_NUM_LOCK;
    144148
    145149/** Currently pressed lock keys. We track these to tackle autorepeat. 
     
    147151 * TODO: put to device?
    148152 */
    149 static unsigned lock_keys;
     153//static unsigned lock_keys;
    150154
    151155#define NUM_LAYOUTS 3
     
    159163static int active_layout = 0;
    160164
    161 static void kbd_push_ev(int type, unsigned int key)
     165static 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
     198static 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
     232static 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
     276static void kbd_push_ev(int type, unsigned int key, usb_hid_dev_kbd_t *kbd_dev)
    162277{
    163278        console_event_t ev;
     
    177292        if (mod_mask != 0) {
    178293                if (type == KEY_PRESS)
    179                         mods = mods | mod_mask;
     294                        kbd_dev->mods = kbd_dev->mods | mod_mask;
    180295                else
    181                         mods = mods & ~mod_mask;
     296                        kbd_dev->mods = kbd_dev->mods & ~mod_mask;
    182297        }
    183298
    184299        switch (key) {
    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;
     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;
    188303        default: mod_mask = 0; break;
    189304        }
    190305
    191306        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               
    192311                if (type == KEY_PRESS) {
     312                        usb_log_debug2("\nKey pressed.\n");
    193313                        /*
    194314                         * Only change lock state on transition from released
     
    196316                         * up the lock state.
    197317                         */
    198                         mods = mods ^ (mod_mask & ~lock_keys);
    199                         lock_keys = lock_keys | mod_mask;
     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;
    200321
    201322                        /* Update keyboard lock indicator lights. */
    202                         // TODO
    203                         //kbd_ctl_set_ind(mods);
     323                        usbkbd_set_led(kbd_dev);
    204324                } else {
    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) {
     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) {
    216334                active_layout = 0;
    217335                layout[active_layout]->reset();
     
    219337        }
    220338
    221         if (type == KEY_PRESS && (mods & KM_LCTRL) &&
    222                 key == KC_F2) {
     339        if (type == KEY_PRESS && (kbd_dev->mods & KM_LCTRL) && key == KC_F2) {
    223340                active_layout = 1;
    224341                layout[active_layout]->reset();
     
    226343        }
    227344
    228         if (type == KEY_PRESS && (mods & KM_LCTRL) &&
    229                 key == KC_F3) {
     345        if (type == KEY_PRESS && (kbd_dev->mods & KM_LCTRL) && key == KC_F3) {
    230346                active_layout = 2;
    231347                layout[active_layout]->reset();
     
    235351        ev.type = type;
    236352        ev.key = key;
    237         ev.mods = mods;
     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        }
    238358
    239359        ev.c = layout[active_layout]->parse_ev(&ev);
    240360
    241         printf("Sending key %d to the console\n", ev.key);
     361        usb_log_debug2("Sending key %d to the console\n", ev.key);
    242362        assert(console_callback_phone != -1);
    243         async_msg_4(console_callback_phone, KBD_EVENT, ev.type, ev.key, ev.mods, ev.c);
     363        async_msg_4(console_callback_phone, KBD_EVENT, ev.type, ev.key,
     364            ev.mods, ev.c);
    244365}
    245366/*
     
    249370        /*
    250371         * TODO:
    251          * 1) key press / key release - how does the keyboard notify about release?
     372         * 1) key press / key release - how does the keyboard notify about
     373         *    release?
    252374         * 2) layouts (use the already defined), not important now
    253375         * 3)
    254376         */
    255377
     378static 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
     389static 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
     423static 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
    256480/*
    257481 * Callbacks for parser
     
    260484    uint8_t modifiers, void *arg)
    261485{
    262         printf("Got keys: ");
     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: ");
    263493        unsigned i;
    264494        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");
     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);
    281509}
    282510
     
    284512 * Kbd functions
    285513 */
    286 static 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);
     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);
    299528               
    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 
     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
     549static 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       
    317632        return EOK;
    318633}
     634
    319635static int usbkbd_process_descriptors(usb_hid_dev_kbd_t *kbd_dev)
    320636{
     
    364680            descriptors, config_desc.total_length,
    365681            &kbd_dev->wire);
     682       
    366683        if (rc != EOK) {
    367684                usb_log_error("Failed to initialize poll pipe: %s.\n",
    368685                    str_error(rc));
     686                free(descriptors);
    369687                return rc;
    370688        }
     689       
    371690        if (!endpoint_mapping[0].present) {
    372691                usb_log_warning("Not accepting device, " \
    373692                    "not boot-protocol keyboard.\n");
     693                free(descriptors);
    374694                return EREFUSED;
    375695        }
    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) {
     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");
    383705                free(descriptors);
    384                 return ENOMEM;
    385         }
    386        
    387         /*rc = usbkbd_parse_descriptors(descriptors, transferred, kbd_dev->conf);
     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       
    388716        free(descriptors);
    389         if (rc != EOK) {
    390                 printf("Problem with parsing standard descriptors.\n");
     717       
     718        if (rc != EOK) {
     719                usb_log_warning("Problem with parsing REPORT descriptor.\n");
    391720                return rc;
    392721        }
    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 
     722       
     723        usb_log_debug("Done parsing descriptors.\n");
     724       
    412725        return EOK;
    413726}
     
    421734
    422735        if (kbd_dev == NULL) {
    423                 fprintf(stderr, NAME ": No memory!\n");
     736                usb_log_fatal("No memory!\n");
    424737                return NULL;
    425738        }
     
    449762
    450763        /*
    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
     764         * Get descriptors, parse descriptors and save endpoints.
     765         */
    458766        usb_endpoint_pipe_start_session(&kbd_dev->ctrl_pipe);
    459         //usb_request_set_configuration(&kbd_dev->ctrl_pipe, 1);
     767       
    460768        rc = usbkbd_process_descriptors(kbd_dev);
     769       
    461770        usb_endpoint_pipe_end_session(&kbd_dev->ctrl_pipe);
    462771        if (rc != EOK) {
    463772                goto error_leave;
    464773        }
    465 
     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       
    466795        return kbd_dev;
    467796
     
    476805        usb_hid_report_in_callbacks_t *callbacks =
    477806            (usb_hid_report_in_callbacks_t *)malloc(
    478                 sizeof(usb_hid_report_in_callbacks_t));
     807                sizeof(usb_hid_report_in_callbacks_t));
    479808        callbacks->keyboard = usbkbd_process_keycodes;
    480809
    481810        //usb_hid_parse_report(kbd_dev->parser, buffer, actual_size, callbacks,
    482811        //    NULL);
    483         printf("Calling usb_hid_boot_keyboard_input_report() with size %zu\n",
    484             actual_size);
     812        /*usb_log_debug2("Calling usb_hid_boot_keyboard_input_report() with size"
     813            " %zu\n", actual_size);*/
    485814        //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);
     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));
    490821        }
    491822}
     
    497828        size_t actual_size;
    498829
    499         printf("Polling keyboard...\n");
     830        usb_log_info("Polling keyboard...\n");
    500831
    501832        while (true) {
     
    504835                sess_rc = usb_endpoint_pipe_start_session(&kbd_dev->poll_pipe);
    505836                if (sess_rc != EOK) {
    506                         printf("Failed to start a session: %s.\n",
     837                        usb_log_warning("Failed to start a session: %s.\n",
    507838                            str_error(sess_rc));
    508839                        continue;
     
    514845
    515846                if (rc != EOK) {
    516                         printf("Error polling the keyboard: %s.\n",
     847                        usb_log_warning("Error polling the keyboard: %s.\n",
    517848                            str_error(rc));
    518849                        continue;
     
    520851
    521852                if (sess_rc != EOK) {
    522                         printf("Error closing session: %s.\n",
     853                        usb_log_warning("Error closing session: %s.\n",
    523854                            str_error(sess_rc));
    524855                        continue;
     
    530861                 */
    531862                if (actual_size == 0) {
    532                         printf("Keyboard returned NAK\n");
     863                        usb_log_debug("Keyboard returned NAK\n");
    533864                        continue;
    534865                }
     
    537868                 * TODO: Process pressed keys.
    538869                 */
    539                 printf("Calling usbkbd_process_interrupt_in()\n");
     870                usb_log_debug("Calling usbkbd_process_interrupt_in()\n");
    540871                usbkbd_process_interrupt_in(kbd_dev, buffer, actual_size);
    541872        }
     
    547878static int usbkbd_fibril_device(void *arg)
    548879{
    549         printf("!!! USB device fibril\n");
    550 
    551880        if (arg == NULL) {
    552                 printf("No device!\n");
     881                usb_log_error("No device!\n");
    553882                return -1;
    554883        }
    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         }
     884       
     885        usb_hid_dev_kbd_t *kbd_dev = (usb_hid_dev_kbd_t *)arg;
    564886
    565887        usbkbd_poll_keyboard(kbd_dev);
     
    570892static int usbkbd_add_device(ddf_dev_t *dev)
    571893{
    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 
    591894        /*
    592895         * Create default function.
     
    601904        rc = ddf_fun_add_to_class(kbd_fun, "keyboard");
    602905        assert(rc == EOK);
    603 
     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       
    604918        /*
    605919         * Create new fibril for handling this keyboard
    606920         */
    607         fid_t fid = fibril_create(usbkbd_fibril_device, dev);
     921        fid_t fid = fibril_create(usbkbd_fibril_device, kbd_dev);
    608922        if (fid == 0) {
    609                 printf("%s: failed to start fibril for HID device\n", NAME);
     923                usb_log_error("Failed to start fibril for HID device\n");
    610924                return ENOMEM;
    611925        }
     
    634948int main(int argc, char *argv[])
    635949{
    636         usb_log_enable(USB_LOG_LEVEL_INFO, "usbhid");
     950        usb_log_enable(USB_LOG_LEVEL_INFO, NAME);
    637951        return ddf_driver_main(&kbd_driver);
    638952}
  • uspace/drv/usbhub/main.c

    r0e3505a r51b46f2  
    3434#include <errno.h>
    3535#include <async.h>
     36#include <stdio.h>
    3637
    3738#include "usbhub.h"
     
    5152};
    5253
    53 int 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 
    6254int main(int argc, char *argv[])
    6355{
    6456        usb_log_enable(USB_LOG_LEVEL_DEBUG, NAME);
    65        
     57        dprintf(USB_LOG_LEVEL_INFO, "starting hub driver");
     58
     59        //this is probably not needed anymore
    6660        fibril_mutex_initialize(&usb_hub_list_lock);
    6761        fibril_mutex_lock(&usb_hub_list_lock);
    6862        usb_lst_init(&usb_hub_list);
    6963        fibril_mutex_unlock(&usb_hub_list_lock);
    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 
     64       
    7965        return ddf_driver_main(&hub_driver);
    8066}
  • uspace/drv/usbhub/port_status.h

    r0e3505a r51b46f2  
    177177}
    178178
     179/**
     180 * set the device request to be a port disable request
     181 * @param request
     182 * @param port
     183 */
     184static inline void usb_hub_unset_power_port_request(
     185usb_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
    179194/** get i`th bit of port status */
    180195static inline bool usb_port_get_bit(usb_port_status_t * status, int idx)
  • uspace/drv/usbhub/usbhub.c

    r0e3505a r51b46f2  
    4444#include <usb/request.h>
    4545#include <usb/classes/hub.h>
     46#include <stdio.h>
    4647
    4748#include "usbhub.h"
     
    5657};
    5758
    58 /** Hub status-change endpoint description */
     59/** Hub status-change endpoint description
     60 *
     61 * For more see usb hub specification in 11.15.1 of
     62 */
    5963static usb_endpoint_description_t status_change_endpoint_description = {
    6064        .transfer_type = USB_TRANSFER_INTERRUPT,
    6165        .direction = USB_DIRECTION_IN,
    6266        .interface_class = USB_CLASS_HUB,
     67        .interface_subclass = 0,
     68        .interface_protocol = 0,
    6369        .flags = 0
    6470};
     71
     72int 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}
    6580
    6681
     
    135150
    136151        //configuration descriptor
    137         /// \TODO check other configurations
     152        /// \TODO check other configurations?
    138153        usb_standard_configuration_descriptor_t config_descriptor;
    139154        opResult = usb_request_get_bare_configuration_descriptor(
     
    179194        }
    180195
    181         /**
    182          * Initialize the interrupt in endpoint.
    183          * \TODO this code should be checked...
    184          */
    185196        usb_endpoint_mapping_t endpoint_mapping[1] = {
    186197                {
     
    210221        return EOK;
    211222       
    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 
    219223}
    220224
     
    249253        usb_endpoint_pipe_start_session(&result->endpoints.control);
    250254        opResult = usb_request_get_descriptor(&result->endpoints.control,
    251                         USB_REQUEST_TYPE_CLASS,
    252                         USB_DESCTYPE_HUB, 0, 0, serialized_descriptor,
     255                        USB_REQUEST_TYPE_CLASS, USB_REQUEST_RECIPIENT_DEVICE,
     256                        USB_DESCTYPE_HUB,
     257                        0, 0, serialized_descriptor,
    253258                        USB_HUB_MAX_DESCRIPTOR_SIZE, &received_size);
    254259        usb_endpoint_pipe_end_session(&result->endpoints.control);
     
    267272        }
    268273
    269        
    270274        dprintf(USB_LOG_LEVEL_INFO, "setting port count to %d",descriptor->ports_count);
    271275        result->port_count = descriptor->ports_count;
     
    329333
    330334        //add the hub to list
     335        //is this needed now?
    331336        fibril_mutex_lock(&usb_hub_list_lock);
    332337        usb_lst_append(&usb_hub_list, hub_info);
    333338        fibril_mutex_unlock(&usb_hub_list_lock);
    334 
    335339        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");
    336360        //(void)hub_info;
    337         usb_hub_check_hub_changes();
     361        //usb_hub_check_hub_changes();
    338362       
    339363        dprintf(USB_LOG_LEVEL_INFO, "hub dev added");
     
    368392        //opResult = usb_drv_reserve_default_address(hc);
    369393        opResult = usb_hc_reserve_default_address(&hub->connection, USB_SPEED_LOW);
    370 
    371         if (opResult != EOK) {
    372                 dprintf(USB_LOG_LEVEL_WARNING, "cannot assign default address, it is probably used");
     394       
     395        if (opResult != EOK) {
     396                dprintf(USB_LOG_LEVEL_WARNING,
     397                                "cannot assign default address, it is probably used %d",opResult);
    373398                return;
    374399        }
     
    381406                        );
    382407        if (opResult != EOK) {
    383                 dprintf(USB_LOG_LEVEL_ERROR, "something went wrong when reseting a port");
     408                dprintf(USB_LOG_LEVEL_ERROR,
     409                                "something went wrong when reseting a port %d",opResult);
    384410                //usb_hub_release_default_address(hc);
    385411                usb_hc_release_default_address(&hub->connection);
     
    394420 */
    395421static void usb_hub_finalize_add_device( usb_hub_info_t * hub,
    396                 uint16_t port) {
     422                uint16_t port, bool isLowSpeed) {
    397423
    398424        int opResult;
     
    417443                        &new_device_connection);
    418444        /// \TODO get highspeed info
    419 
    420 
    421 
     445        usb_speed_t speed = isLowSpeed?USB_SPEED_LOW:USB_SPEED_FULL;
    422446
    423447
     
    425449        usb_address_t new_device_address = usb_hc_request_address(
    426450                        &hub->connection,
    427                         USB_SPEED_LOW/// \TODO fullspeed??
     451                        speed/// \TODO fullspeed??
    428452                        );
    429453        if (new_device_address < 0) {
     
    436460        //opResult = usb_drv_req_set_address(hc, USB_ADDRESS_DEFAULT,
    437461        //    new_device_address);
     462        usb_endpoint_pipe_start_session(&new_device_pipe);
    438463        opResult = usb_request_set_address(&new_device_pipe,new_device_address);
    439 
    440         if (opResult != EOK) {
    441                 dprintf(USB_LOG_LEVEL_ERROR, "could not set address for new device");
     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);
    442468                usb_hc_release_default_address(&hub->connection);
    443469                return;
     
    458484
    459485        if (opResult != EOK) {
    460                 dprintf(USB_LOG_LEVEL_ERROR, "could not start driver for new device");
     486                dprintf(USB_LOG_LEVEL_ERROR,
     487                                "could not start driver for new device %d",opResult);
    461488                return;
    462489        }
     
    469496                        &hub->attached_devs[port]);
    470497        if (opResult != EOK) {
    471                 dprintf(USB_LOG_LEVEL_ERROR, "could not assign address of device in hcd");
     498                dprintf(USB_LOG_LEVEL_ERROR,
     499                                "could not assign address of device in hcd %d",opResult);
    472500                return;
    473501        }
     
    511539}
    512540
     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 */
     550static 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
    513561/**
    514562 * Process interrupts on given hub port
     
    522570        //determine type of change
    523571        usb_endpoint_pipe_t *pipe = &hub->endpoints.control;
    524         int opResult = usb_endpoint_pipe_start_session(pipe);
    525572       
    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         */
     573        int opResult;
    535574
    536575        usb_port_status_t status;
     
    547586                        );
    548587        if (opResult != EOK) {
    549                 dprintf(USB_LOG_LEVEL_ERROR, "ERROR: could not get port status");
     588                dprintf(USB_LOG_LEVEL_ERROR, "could not get port status");
    550589                return;
    551590        }
    552591        if (rcvd_size != sizeof (usb_port_status_t)) {
    553                 dprintf(USB_LOG_LEVEL_ERROR, "ERROR: received status has incorrect size");
     592                dprintf(USB_LOG_LEVEL_ERROR, "received status has incorrect size");
    554593                return;
    555594        }
     
    566605                }
    567606        }
     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        }
    568617        //port reset
    569618        if (usb_port_reset_completed(&status)) {
    570619                dprintf(USB_LOG_LEVEL_INFO, "port reset complete");
    571620                if (usb_port_enabled(&status)) {
    572                         usb_hub_finalize_add_device(hub, port);
     621                        usb_hub_finalize_add_device(hub, port, usb_port_low_speed(&status));
    573622                } else {
    574                         dprintf(USB_LOG_LEVEL_WARNING, "ERROR: port reset, but port still not enabled");
     623                        dprintf(USB_LOG_LEVEL_WARNING, "port reset, but port still not enabled");
    575624                }
    576625        }
     
    585634        }
    586635        /// \TODO handle other changes
    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  */
    596 void usb_hub_check_hub_changes(void) {
     636}
     637
     638/**
     639 * Check changes on particular hub
     640 * @param hub_info_param
     641 */
     642void 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;
     655                void *change_bitmap = malloc(byte_length);
     656        size_t actual_size;
     657
    597658        /*
    598          * Iterate through all hubs.
     659         * Send the request.
    599660         */
    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;
     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) {
     667                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);
    612696                }
    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 
    636                 void *change_bitmap = malloc(byte_length);
    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);
    665                 free(change_bitmap);
    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 
     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}
    675703
    676704
  • uspace/drv/usbhub/usbhub.h

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

    r0e3505a r51b46f2  
    5151} usb_hid_request_t;
    5252
     53typedef 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
     59typedef enum {
     60        USB_HID_PROTOCOL_BOOT = 0,
     61        USB_HID_PROTOCOL_REPORT = 1
     62} usb_hid_protocol_t;
     63
    5364/** USB/HID subclass constants. */
    5465typedef enum {
     
    6273        USB_HID_PROTOCOL_KEYBOARD = 1,
    6374        USB_HID_PROTOCOL_MOUSE = 2
    64 } usb_hid_protocol_t;
     75} usb_hid_iface_protocol_t;
    6576
    6677/** Part of standard USB HID descriptor specifying one class descriptor.
  • uspace/lib/usb/include/usb/classes/hidparser.h

    r0e3505a r51b46f2  
    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_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
     85typedef 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
     94static 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
    77119
    78120/*
  • uspace/lib/usb/include/usb/request.h

    r0e3505a r51b46f2  
    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     uint8_t, uint8_t, uint16_t, void *, size_t, size_t *);
     98    usb_request_recipient_t, uint8_t, uint8_t, uint16_t, void *, size_t,
     99    size_t *);
    99100int usb_request_get_descriptor_alloc(usb_endpoint_pipe_t *, usb_request_type_t,
    100     uint8_t, uint8_t, uint16_t, void **, size_t *);
     101    usb_request_recipient_t, uint8_t, uint8_t, uint16_t, void **, size_t *);
    101102int usb_request_get_device_descriptor(usb_endpoint_pipe_t *,
    102103    usb_standard_device_descriptor_t *);
  • uspace/lib/usb/src/hidparser.c

    r0e3505a r51b46f2  
    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         /* used only first five bits, others are only padding*/
    151         *data = leds;
     150        data[0] = leds;
    152151        return EOK;
    153152}
  • uspace/lib/usb/src/request.c

    r0e3505a r51b46f2  
    3636#include <errno.h>
    3737#include <assert.h>
     38#include <usb/debug.h>
    3839
    3940#define MAX_DATA_LENGTH ((size_t)(0xFFFF))
     
    209210 */
    210211int usb_request_get_descriptor(usb_endpoint_pipe_t *pipe,
    211     usb_request_type_t request_type,
     212    usb_request_type_t request_type, usb_request_recipient_t recipient,
    212213    uint8_t descriptor_type, uint8_t descriptor_index,
    213214    uint16_t language,
     
    224225
    225226        return usb_control_request_get(pipe,
    226             request_type, USB_REQUEST_RECIPIENT_DEVICE,
     227            request_type, recipient,
    227228            USB_DEVREQ_GET_DESCRIPTOR,
    228229            wValue, language,
     
    242243 */
    243244int usb_request_get_descriptor_alloc(usb_endpoint_pipe_t * pipe,
    244     usb_request_type_t request_type,
     245    usb_request_type_t request_type, usb_request_recipient_t recipient,
    245246    uint8_t descriptor_type, uint8_t descriptor_index,
    246247    uint16_t language,
     
    258259        uint8_t tmp_buffer[1];
    259260        size_t bytes_transfered;
    260         rc = usb_request_get_descriptor(pipe, request_type,
     261        rc = usb_request_get_descriptor(pipe, request_type, recipient,
    261262            descriptor_type, descriptor_index, language,
    262263            &tmp_buffer, 1, &bytes_transfered);
     
    283284        }
    284285
    285         rc = usb_request_get_descriptor(pipe, request_type,
     286        rc = usb_request_get_descriptor(pipe, request_type, recipient,
    286287            descriptor_type, descriptor_index, language,
    287288            buffer, size, &bytes_transfered);
     
    320321        usb_standard_device_descriptor_t descriptor_tmp;
    321322        int rc = usb_request_get_descriptor(pipe,
    322             USB_REQUEST_TYPE_STANDARD, USB_DESCTYPE_DEVICE,
    323             0, 0,
     323            USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_DEVICE,
     324            USB_DESCTYPE_DEVICE, 0, 0,
    324325            &descriptor_tmp, sizeof(descriptor_tmp),
    325326            &actually_transferred);
     
    366367        usb_standard_configuration_descriptor_t descriptor_tmp;
    367368        int rc = usb_request_get_descriptor(pipe,
    368             USB_REQUEST_TYPE_STANDARD, USB_DESCTYPE_CONFIGURATION,
    369             index, 0,
     369            USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_DEVICE,
     370            USB_DESCTYPE_CONFIGURATION, index, 0,
    370371            &descriptor_tmp, sizeof(descriptor_tmp),
    371372            &actually_transferred);
     
    406407
    407408        return usb_request_get_descriptor(pipe,
    408             USB_REQUEST_TYPE_STANDARD, USB_DESCTYPE_CONFIGURATION,
    409             index, 0,
     409            USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_DEVICE,
     410            USB_DESCTYPE_CONFIGURATION, index, 0,
    410411            descriptor, descriptor_size, actual_size);
    411412}
     
    452453        size_t string_descriptor_size = 0;
    453454        rc = usb_request_get_descriptor_alloc(pipe,
    454             USB_REQUEST_TYPE_STANDARD, USB_DESCTYPE_STRING, 0, 0,
     455            USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_DEVICE,
     456            USB_DESCTYPE_STRING, 0, 0,
    455457            (void **) &string_descriptor, &string_descriptor_size);
    456458        if (rc != EOK) {
     
    535537        size_t string_size;
    536538        rc = usb_request_get_descriptor_alloc(pipe,
    537             USB_REQUEST_TYPE_STANDARD, USB_DESCTYPE_STRING,
    538             index, uint16_host2usb(lang),
     539            USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_DEVICE,
     540            USB_DESCTYPE_STRING, index, uint16_host2usb(lang),
    539541            (void **) &string, &string_size);
    540542        if (rc != EOK) {
Note: See TracChangeset for help on using the changeset viewer.