Changeset e3f7418 in mainline


Ignore:
Timestamp:
2011-10-15T13:06:28Z (13 years ago)
Author:
Jan Vesely <jano.vesely@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
a044f71
Parents:
3958e315 (diff), 065064e6 (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:

USB improvements.

USB unplug part2; Unplug support in all drivers (except for unused usbmouse driver).
Make descriptor paring work on const pointers.
Fixes several crashes and memory leaks.

Location:
uspace
Files:
38 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/usbinfo/desctree.c

    r3958e315 re3f7418  
    5050
    5151static void browse_descriptor_tree_internal(usb_dp_parser_t *parser,
    52     usb_dp_parser_data_t *data, uint8_t *root, size_t depth,
     52    usb_dp_parser_data_t *data, const uint8_t *root, size_t depth,
    5353    dump_descriptor_in_tree_t callback, void *arg)
    5454{
     
    5757        }
    5858        callback(root, depth, arg);
    59         uint8_t *child = usb_dp_get_nested_descriptor(parser, data, root);
     59        const uint8_t *child = usb_dp_get_nested_descriptor(parser, data, root);
    6060        do {
    6161                browse_descriptor_tree_internal(parser, data, child, depth + 1,
  • uspace/app/usbinfo/dump.c

    r3958e315 re3f7418  
    110110}
    111111
    112 static void dump_tree_descriptor(uint8_t *descriptor, size_t depth)
     112static void dump_tree_descriptor(const uint8_t *descriptor, size_t depth)
    113113{
    114114        if (descriptor == NULL) {
    115115                return;
    116116        }
    117         int type = (int) *(descriptor + 1);
     117        int type = descriptor[1];
    118118        const char *name = "unknown";
    119119        switch (type) {
     
    136136}
    137137
    138 static void dump_tree_internal(usb_dp_parser_t *parser, usb_dp_parser_data_t *data,
    139     uint8_t *root, size_t depth)
     138static void dump_tree_internal(
     139    usb_dp_parser_t *parser, usb_dp_parser_data_t *data,
     140    const uint8_t *root, size_t depth)
    140141{
    141142        if (root == NULL) {
     
    143144        }
    144145        dump_tree_descriptor(root, depth);
    145         uint8_t *child = usb_dp_get_nested_descriptor(parser, data, root);
     146        const uint8_t *child = usb_dp_get_nested_descriptor(parser, data, root);
    146147        do {
    147148                dump_tree_internal(parser, data, child, depth + 1);
     
    152153static void dump_tree(usb_dp_parser_t *parser, usb_dp_parser_data_t *data)
    153154{
    154         uint8_t *ptr = data->data;
     155        const uint8_t *ptr = data->data;
    155156        printf("Descriptor tree:\n");
    156157        dump_tree_internal(parser, data, ptr, 0);
  • uspace/app/usbinfo/hid.c

    r3958e315 re3f7418  
    5555} descriptor_walk_context_t;
    5656
    57 static bool is_descriptor_kind(uint8_t *d, usb_descriptor_type_t t)
     57static bool is_descriptor_kind(const uint8_t *d, usb_descriptor_type_t t)
    5858{
    5959        if (d == NULL) {
     
    180180 * @param arg Custom argument, passed as descriptor_walk_context_t.
    181181 */
    182 static void descriptor_walk_callback(uint8_t *raw_descriptor,
     182static void descriptor_walk_callback(const uint8_t *raw_descriptor,
    183183    size_t depth, void *arg)
    184184{
  • uspace/app/usbinfo/info.c

    r3958e315 re3f7418  
    5151}
    5252
    53 static void dump_match_ids_from_interface(uint8_t *descriptor, size_t depth,
    54     void *arg)
     53static void dump_match_ids_from_interface(
     54    const uint8_t *descriptor, size_t depth, void *arg)
    5555{
    5656        if (depth != 1) {
     
    165165
    166166
    167 static void dump_descriptor_tree_callback(uint8_t *descriptor,
    168     size_t depth, void *arg)
     167static void dump_descriptor_tree_callback(
     168    const uint8_t *descriptor, size_t depth, void *arg)
    169169{
    170170        const char *indent = get_indent(depth + 1);
     
    246246}
    247247
    248 static void find_string_indexes_callback(uint8_t *descriptor,
    249     size_t depth, void *arg)
     248static void find_string_indexes_callback(
     249    const uint8_t *descriptor, size_t depth, void *arg)
    250250{
    251251        size_t descriptor_length = descriptor[0];
  • uspace/app/usbinfo/usbinfo.h

    r3958e315 re3f7418  
    7474void destroy_device(usbinfo_device_t *);
    7575
    76 typedef void (*dump_descriptor_in_tree_t)(uint8_t *, size_t, void *);
     76typedef void (*dump_descriptor_in_tree_t)(const uint8_t *, size_t, void *);
    7777void browse_descriptor_tree(uint8_t *, size_t, usb_dp_descriptor_nesting_t *,
    7878    dump_descriptor_in_tree_t, size_t, void *);
  • uspace/drv/bus/usb/usbflbk/main.c

    r3958e315 re3f7418  
    6464        }
    6565
     66        dev->driver_data = ctl_fun;
     67
    6668        usb_log_info("Pretending to control %s `%s'" \
    6769            " (node `%s', handle %" PRIun ").\n",
     
    7274}
    7375
     76/** Callback when new device is removed and recognized as gone by DDF.
     77 *
     78 * @param dev Representation of a generic DDF device.
     79 * @return Error code.
     80 */
     81static int usbfallback_device_gone(usb_device_t *dev)
     82{
     83        assert(dev);
     84        ddf_fun_t *ctl_fun = dev->driver_data;
     85        const int ret = ddf_fun_unbind(ctl_fun);
     86        if (ret != EOK) {
     87                usb_log_error("Failed to unbind %s.\n", ctl_fun->name);
     88                return ret;
     89        }
     90        ddf_fun_destroy(ctl_fun);
     91        dev->driver_data = NULL;
     92
     93        return EOK;
     94}
    7495/** USB fallback driver ops. */
    7596static usb_driver_ops_t usbfallback_driver_ops = {
    7697        .device_add = usbfallback_device_add,
     98        .device_gone = usbfallback_device_gone,
    7799};
    78100
  • uspace/drv/bus/usb/usbhid/generic/hiddev.c

    r3958e315 re3f7418  
    5252        .direction = USB_DIRECTION_IN,
    5353        .interface_class = USB_CLASS_HID,
     54        .interface_subclass = -1,
     55        .interface_protocol = -1,
    5456        .flags = 0
    5557};
     
    9294        usb_log_debug2("Generic HID: Get event length (fun: %p, "
    9395            "fun->driver_data: %p.\n", fun, fun->driver_data);
    94        
     96
    9597        if (fun == NULL || fun->driver_data == NULL) {
    9698                return 0;
     
    98100
    99101        usb_hid_dev_t *hid_dev = (usb_hid_dev_t *)fun->driver_data;
    100        
     102
    101103        usb_log_debug2("hid_dev: %p, Max input report size (%zu).\n",
    102104            hid_dev, hid_dev->max_input_report_size);
    103        
     105
    104106        return hid_dev->max_input_report_size;
    105107}
     
    111113{
    112114        usb_log_debug2("Generic HID: Get event.\n");
    113        
     115
    114116        if (fun == NULL || fun->driver_data == NULL || buffer == NULL
    115117            || act_size == NULL || event_nr == NULL) {
     
    119121
    120122        usb_hid_dev_t *hid_dev = (usb_hid_dev_t *)fun->driver_data;
    121        
     123
    122124        if (hid_dev->input_report_size > size) {
    123125                usb_log_debug("input_report_size > size (%zu, %zu)\n",
     
    125127                return EINVAL;  // TODO: other error code
    126128        }
    127        
     129
    128130        /*! @todo This should probably be somehow atomic. */
    129131        memcpy(buffer, hid_dev->input_report,
     
    131133        *act_size = hid_dev->input_report_size;
    132134        *event_nr = usb_hid_report_number(hid_dev);
    133        
     135
    134136        usb_log_debug2("OK\n");
    135        
     137
    136138        return EOK;
    137139}
     
    142144{
    143145        usb_log_debug("Generic HID: Get report descriptor length.\n");
    144        
     146
    145147        if (fun == NULL || fun->driver_data == NULL) {
    146148                usb_log_debug("No function");
    147149                return EINVAL;
    148150        }
    149        
    150         usb_hid_dev_t *hid_dev = (usb_hid_dev_t *)fun->driver_data;
    151        
     151
     152        usb_hid_dev_t *hid_dev = (usb_hid_dev_t *)fun->driver_data;
     153
    152154        usb_log_debug2("hid_dev->report_desc_size = %zu\n",
    153155            hid_dev->report_desc_size);
    154        
     156
    155157        return hid_dev->report_desc_size;
    156158}
     
    162164{
    163165        usb_log_debug2("Generic HID: Get report descriptor.\n");
    164        
     166
    165167        if (fun == NULL || fun->driver_data == NULL) {
    166168                usb_log_debug("No function");
    167169                return EINVAL;
    168170        }
    169        
    170         usb_hid_dev_t *hid_dev = (usb_hid_dev_t *)fun->driver_data;
    171        
     171
     172        usb_hid_dev_t *hid_dev = (usb_hid_dev_t *)fun->driver_data;
     173
    172174        if (hid_dev->report_desc_size > size) {
    173175                return EINVAL;
    174176        }
    175        
     177
    176178        memcpy(desc, hid_dev->report_desc, hid_dev->report_desc_size);
    177179        *actual_size = hid_dev->report_desc_size;
    178        
     180
    179181        return EOK;
    180182}
     
    190192/*----------------------------------------------------------------------------*/
    191193
    192 static int usb_generic_hid_create_function(usb_hid_dev_t *hid_dev)
    193 {       
     194void usb_generic_hid_deinit(usb_hid_dev_t *hid_dev, void *data)
     195{
     196        ddf_fun_t *fun = data;
     197        const int ret = ddf_fun_unbind(fun);
     198        if (ret != EOK) {
     199                usb_log_error("Failed to unbind generic hid fun.\n");
     200                return;
     201        }
     202        usb_log_debug2("%s unbound.\n", fun->name);
     203        /* We did not allocate this, so leave this alone
     204         * the device would take care of it */
     205        fun->driver_data = NULL;
     206        ddf_fun_destroy(fun);
     207}
     208
     209/*----------------------------------------------------------------------------*/
     210
     211int usb_generic_hid_init(usb_hid_dev_t *hid_dev, void **data)
     212{
     213        if (hid_dev == NULL) {
     214                return EINVAL;
     215        }
     216
    194217        /* Create the exposed function. */
    195218        /** @todo Generate numbers for the devices? */
     
    201224                return ENOMEM;
    202225        }
    203        
     226
    204227        fun->ops = &usb_generic_hid_ops;
    205         fun->driver_data = hid_dev;
    206228
    207229        int rc = ddf_fun_bind(fun);
     
    212234                return rc;
    213235        }
    214        
     236        /* This is nasty both device and this function have the same
     237         * driver data, thus destruction would lead to double free */
     238        fun->driver_data = hid_dev;
     239
    215240        usb_log_debug("HID function created. Handle: %" PRIun "\n", fun->handle);
    216        
    217         return EOK;
    218 }
    219 
    220 /*----------------------------------------------------------------------------*/
    221 
    222 int usb_generic_hid_init(usb_hid_dev_t *hid_dev, void **data)
    223 {
    224         if (hid_dev == NULL) {
    225                 return EINVAL;
    226         }
    227        
    228         return usb_generic_hid_create_function(hid_dev);
     241        *data = fun;
     242
     243        return EOK;
    229244}
    230245
  • uspace/drv/bus/usb/usbhid/generic/hiddev.h

    r3958e315 re3f7418  
    5050int usb_generic_hid_init(struct usb_hid_dev *hid_dev, void **data);
    5151
     52void usb_generic_hid_deinit(struct usb_hid_dev *hid_dev, void *data);
     53
    5254bool usb_generic_hid_polling_callback(struct usb_hid_dev *hid_dev, void *data);
    5355
  • uspace/drv/bus/usb/usbhid/kbd/conv.c

    r3958e315 re3f7418  
    8787        [0x26] = KC_9,
    8888        [0x27] = KC_0,
    89        
     89
    9090        [0x28] = KC_ENTER,
    9191        [0x29] = KC_ESCAPE,
     
    109109
    110110        [0x39] = KC_CAPS_LOCK,
    111        
     111
    112112        [0x3a] = KC_F1,
    113113        [0x3b] = KC_F2,
     
    122122        [0x44] = KC_F11,
    123123        [0x45] = KC_F12,
    124        
     124
    125125        [0x46] = KC_PRTSCR,
    126126        [0x47] = KC_SCROLL_LOCK,
     
    136136        [0x51] = KC_DOWN,
    137137        [0x52] = KC_UP,
    138        
     138
    139139        //[0x64] = // some funny key
    140        
     140
    141141        [0xe0] = KC_LCTRL,
    142142        [0xe1] = KC_LSHIFT,
     
    147147        [0xe6] = KC_RALT,
    148148        //[0xe7] = KC_R // TODO: right GUI
    149        
     149
    150150        [0x53] = KC_NUM_LOCK,
    151151        [0x54] = KC_NSLASH,
     
    165165        [0x62] = KC_N0,
    166166        [0x63] = KC_NPERIOD
    167        
     167
    168168};
    169169
     
    186186
    187187        key = map[scancode];
    188        
     188
    189189        return key;
    190190}
  • uspace/drv/bus/usb/usbhid/kbd/kbddev.c

    r3958e315 re3f7418  
    174174{
    175175        sysarg_t method = IPC_GET_IMETHOD(*icall);
    176        
     176
    177177        usb_kbd_t *kbd_dev = (usb_kbd_t *) fun->driver_data;
    178178        if (kbd_dev == NULL) {
     
    182182                return;
    183183        }
    184        
     184
    185185        async_sess_t *sess =
    186186            async_callback_receive_start(EXCHANGE_SERIALIZE, icall);
     
    240240            USB_HID_PATH_COMPARE_END | USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY,
    241241            USB_HID_REPORT_TYPE_OUTPUT);
    242        
     242
    243243        while (field != NULL) {
    244244               
     
    263263                        USB_HID_REPORT_TYPE_OUTPUT);
    264264        }
    265        
     265
    266266        // TODO: what about the Report ID?
    267267        int rc = usb_hid_report_output_translate(hid_dev->report, 0,
    268268            kbd_dev->output_buffer, kbd_dev->output_size);
    269        
     269
    270270        if (rc != EOK) {
    271271                usb_log_warning("Error translating LED output to output report"
     
    273273                return;
    274274        }
    275        
     275
    276276        usb_log_debug("Output report buffer: %s\n",
    277277            usb_debug_str_buffer(kbd_dev->output_buffer, kbd_dev->output_size,
    278278                0));
    279        
     279
    280280        usbhid_req_set_report(&hid_dev->usb_dev->ctrl_pipe,
    281281            hid_dev->usb_dev->interface_no, USB_HID_REPORT_TYPE_OUTPUT,
     
    300300                return;
    301301        }
    302        
     302
    303303        async_exch_t *exch = async_exchange_begin(kbd_dev->console_sess);
    304304        async_msg_2(exch, KBDEV_EVENT, type, key);
     
    347347        unsigned int key;
    348348        size_t i;
    349        
     349
    350350        /*
    351351         * First of all, check if the kbd have reported phantom state.
     
    362362                return;
    363363        }
    364        
     364
    365365        /*
    366366         * Key releases
     
    382382                }
    383383        }
    384        
     384
    385385        /*
    386386         * Key presses
     
    402402                }
    403403        }
    404        
     404
    405405        memcpy(kbd_dev->keys_old, kbd_dev->keys, kbd_dev->key_count * 4);
    406        
     406
    407407        char key_buffer[512];
    408408        ddf_dump_buffer(key_buffer, 512,
     
    435435        assert(hid_dev != NULL);
    436436        assert(kbd_dev != NULL);
    437        
     437
    438438        usb_hid_report_path_t *path = usb_hid_report_path();
    439439        usb_hid_report_path_append_item(path, USB_HIDUT_PAGE_KEYBOARD, 0);
    440440
    441441        usb_hid_report_path_set_report_id (path, hid_dev->report_id);
    442        
     442
    443443        // fill in the currently pressed keys
    444        
     444
    445445        usb_hid_report_field_t *field = usb_hid_report_get_sibling(
    446446            hid_dev->report, NULL, path,
     
    448448            USB_HID_REPORT_TYPE_INPUT);
    449449        unsigned i = 0;
    450        
     450
    451451        while (field != NULL) {
    452452                usb_log_debug2("FIELD (%p) - VALUE(%d) USAGE(%u)\n",
     
    470470                    USB_HID_REPORT_TYPE_INPUT);
    471471        }
    472        
     472
    473473        usb_hid_report_path_free(path);
    474        
     474
    475475        usb_kbd_check_key_changes(hid_dev, kbd_dev);
    476476}
     
    502502
    503503        if (kbd_dev == NULL) {
    504                 usb_log_fatal("No memory!\n");
     504                usb_log_error("No memory!\n");
    505505                return NULL;
    506506        }
    507        
     507
    508508        kbd_dev->console_sess = NULL;
    509509        kbd_dev->initialized = USB_KBD_STATUS_UNINITIALIZED;
    510        
     510
    511511        return kbd_dev;
    512512}
     
    514514/*----------------------------------------------------------------------------*/
    515515
    516 static int usb_kbd_create_function(usb_hid_dev_t *hid_dev, usb_kbd_t *kbd_dev)
    517 {
    518         assert(hid_dev != NULL);
    519         assert(hid_dev->usb_dev != NULL);
     516static int usb_kbd_create_function(usb_kbd_t *kbd_dev)
     517{
    520518        assert(kbd_dev != NULL);
    521        
     519        assert(kbd_dev->hid_dev != NULL);
     520        assert(kbd_dev->hid_dev->usb_dev != NULL);
     521
    522522        /* Create the exposed function. */
    523523        usb_log_debug("Creating DDF function %s...\n", HID_KBD_FUN_NAME);
    524         ddf_fun_t *fun = ddf_fun_create(hid_dev->usb_dev->ddf_dev, fun_exposed,
    525             HID_KBD_FUN_NAME);
     524        ddf_fun_t *fun = ddf_fun_create(kbd_dev->hid_dev->usb_dev->ddf_dev,
     525            fun_exposed, HID_KBD_FUN_NAME);
    526526        if (fun == NULL) {
    527527                usb_log_error("Could not create DDF function node.\n");
    528528                return ENOMEM;
    529529        }
    530        
     530
    531531        /*
    532532         * Store the initialized HID device and HID ops
     
    540540                usb_log_error("Could not bind DDF function: %s.\n",
    541541                    str_error(rc));
     542                fun->driver_data = NULL; /* We need this later */
    542543                ddf_fun_destroy(fun);
    543544                return rc;
    544545        }
    545        
     546
    546547        usb_log_debug("%s function created. Handle: %" PRIun "\n",
    547548            HID_KBD_FUN_NAME, fun->handle);
    548        
     549
    549550        usb_log_debug("Adding DDF function to category %s...\n",
    550551            HID_KBD_CLASS_NAME);
     
    554555                    "Could not add DDF function to category %s: %s.\n",
    555556                    HID_KBD_CLASS_NAME, str_error(rc));
     557                fun->driver_data = NULL; /* We need this later */
    556558                ddf_fun_destroy(fun);
    557559                return rc;
    558560        }
    559        
     561        kbd_dev->fun = fun;
     562
    560563        return EOK;
    561564}
     
    587590{
    588591        usb_log_debug("Initializing HID/KBD structure...\n");
    589        
     592
    590593        if (hid_dev == NULL) {
    591594                usb_log_error("Failed to init keyboard structure: no structure"
     
    593596                return EINVAL;
    594597        }
    595        
     598
    596599        usb_kbd_t *kbd_dev = usb_kbd_new();
    597600        if (kbd_dev == NULL) {
     
    603606        /* Store link to HID device */
    604607        kbd_dev->hid_dev = hid_dev;
    605        
     608
    606609        /*
    607610         * TODO: make more general
     
    609612        usb_hid_report_path_t *path = usb_hid_report_path();
    610613        usb_hid_report_path_append_item(path, USB_HIDUT_PAGE_KEYBOARD, 0);
    611        
     614
    612615        usb_hid_report_path_set_report_id(path, 0);
    613        
     616
    614617        kbd_dev->key_count = usb_hid_report_size(
    615618            hid_dev->report, 0, USB_HID_REPORT_TYPE_INPUT);
    616619        usb_hid_report_path_free(path);
    617        
     620
    618621        usb_log_debug("Size of the input report: %zu\n", kbd_dev->key_count);
    619        
     622
    620623        kbd_dev->keys = (int32_t *)calloc(kbd_dev->key_count, sizeof(int32_t));
    621        
     624
    622625        if (kbd_dev->keys == NULL) {
    623                 usb_log_fatal("No memory!\n");
     626                usb_log_error("No memory!\n");
    624627                free(kbd_dev);
    625628                return ENOMEM;
    626629        }
    627        
     630
    628631        kbd_dev->keys_old =
    629632                (int32_t *)calloc(kbd_dev->key_count, sizeof(int32_t));
    630        
     633
    631634        if (kbd_dev->keys_old == NULL) {
    632                 usb_log_fatal("No memory!\n");
     635                usb_log_error("No memory!\n");
    633636                free(kbd_dev->keys);
    634637                free(kbd_dev);
    635638                return ENOMEM;
    636639        }
    637        
     640
    638641        /*
    639642         * Output report
     
    647650                return ENOMEM;
    648651        }
    649        
     652
    650653        usb_log_debug("Output buffer size: %zu\n", kbd_dev->output_size);
    651        
     654
    652655        kbd_dev->led_path = usb_hid_report_path();
    653656        usb_hid_report_path_append_item(
    654657            kbd_dev->led_path, USB_HIDUT_PAGE_LED, 0);
    655        
     658
    656659        kbd_dev->led_output_size = usb_hid_report_size(hid_dev->report,
    657660            0, USB_HID_REPORT_TYPE_OUTPUT);
    658        
     661
    659662        usb_log_debug("Output report size (in items): %zu\n",
    660663            kbd_dev->led_output_size);
    661        
     664
    662665        kbd_dev->led_data = (int32_t *)calloc(
    663666            kbd_dev->led_output_size, sizeof(int32_t));
    664        
     667
    665668        if (kbd_dev->led_data == NULL) {
    666669                usb_log_warning("Error creating buffer for LED output report."
     
    671674                return ENOMEM;
    672675        }
    673        
     676
    674677        /*
    675678         * Modifiers and locks
     
    678681        kbd_dev->mods = DEFAULT_ACTIVE_MODS;
    679682        kbd_dev->lock_keys = 0;
    680        
     683
    681684        /*
    682685         * Autorepeat
     
    686689        kbd_dev->repeat.delay_before = DEFAULT_DELAY_BEFORE_FIRST_REPEAT;
    687690        kbd_dev->repeat.delay_between = DEFAULT_REPEAT_DELAY;
    688        
    689         kbd_dev->repeat_mtx = (fibril_mutex_t *)(
    690             malloc(sizeof(fibril_mutex_t)));
    691         if (kbd_dev->repeat_mtx == NULL) {
    692                 usb_log_fatal("No memory!\n");
    693                 free(kbd_dev->keys);
    694                 usb_hid_report_output_free(kbd_dev->output_buffer);
    695                 free(kbd_dev);
    696                 return ENOMEM;
    697         }
    698        
    699         fibril_mutex_initialize(kbd_dev->repeat_mtx);
    700        
     691
     692        fibril_mutex_initialize(&kbd_dev->repeat_mtx);
     693
    701694        // save the KBD device structure into the HID device structure
    702695        *data = kbd_dev;
    703        
     696
    704697        // set handler for incoming calls
    705698        kbd_dev->ops.default_handler = default_connection_handler;
    706        
     699
    707700        /*
    708701         * Set LEDs according to initial setup.
     
    710703         */
    711704        usb_kbd_set_led(hid_dev, kbd_dev);
    712        
     705
    713706        usbhid_req_set_idle(&hid_dev->usb_dev->ctrl_pipe,
    714707            hid_dev->usb_dev->interface_no, IDLE_RATE);
    715        
     708
    716709        /*
    717710         * Create new fibril for auto-repeat
     
    723716        }
    724717        fibril_add_ready(fid);
    725        
     718
    726719        kbd_dev->initialized = USB_KBD_STATUS_INITIALIZED;
    727720        usb_log_debug("HID/KBD device structure initialized.\n");
    728        
     721
    729722        usb_log_debug("Creating KBD function...\n");
    730         int rc = usb_kbd_create_function(hid_dev, kbd_dev);
     723        int rc = usb_kbd_create_function(kbd_dev);
    731724        if (rc != EOK) {
    732725                usb_kbd_destroy(kbd_dev);
    733726                return rc;
    734727        }
    735        
     728
    736729        return EOK;
    737730}
     
    745738                return false;
    746739        }
    747        
     740
    748741        usb_kbd_t *kbd_dev = (usb_kbd_t *)data;
    749742        assert(kbd_dev != NULL);
    750        
     743
    751744        // TODO: add return value from this function
    752745        usb_kbd_process_data(hid_dev, kbd_dev);
    753        
     746
    754747        return true;
    755748}
     
    780773                return;
    781774        }
    782        
     775
    783776        // hangup session to the console
    784777        async_hangup(kbd_dev->console_sess);
    785        
    786         if (kbd_dev->repeat_mtx != NULL) {
    787                 //assert(!fibril_mutex_is_locked((*kbd_dev)->repeat_mtx));
    788                 // FIXME - the fibril_mutex_is_locked may not cause
    789                 // fibril scheduling
    790                 while (fibril_mutex_is_locked(kbd_dev->repeat_mtx)) {}
    791                 free(kbd_dev->repeat_mtx);
    792         }
    793        
     778
     779        //assert(!fibril_mutex_is_locked((*kbd_dev)->repeat_mtx));
     780        // FIXME - the fibril_mutex_is_locked may not cause
     781        // fibril scheduling
     782        while (fibril_mutex_is_locked(&kbd_dev->repeat_mtx)) {}
     783
    794784        // free all buffers
    795         if (kbd_dev->keys != NULL) {
    796                 free(kbd_dev->keys);
    797         }
    798         if (kbd_dev->keys_old != NULL) {
    799                 free(kbd_dev->keys_old);
    800         }
    801         if (kbd_dev->led_data != NULL) {
    802                 free(kbd_dev->led_data);
    803         }
     785        free(kbd_dev->keys);
     786        free(kbd_dev->keys_old);
     787        free(kbd_dev->led_data);
     788
    804789        if (kbd_dev->led_path != NULL) {
    805790                usb_hid_report_path_free(kbd_dev->led_path);
     
    808793                usb_hid_report_output_free(kbd_dev->output_buffer);
    809794        }
     795
     796        if (ddf_fun_unbind(kbd_dev->fun) != EOK) {
     797                usb_log_warning("Failed to unbind kbd function.\n");
     798        } else {
     799                usb_log_debug2("%s unbound.\n", kbd_dev->fun->name);
     800                kbd_dev->fun->driver_data = NULL;
     801                ddf_fun_destroy(kbd_dev->fun);
     802        }
    810803}
    811804
     
    817810                return;
    818811        }
    819        
     812
    820813        if (data != NULL) {
    821                 usb_kbd_t *kbd_dev = (usb_kbd_t *)data;
     814                usb_kbd_t *kbd_dev = data;
    822815                if (usb_kbd_is_initialized(kbd_dev)) {
    823816                        usb_kbd_mark_unusable(kbd_dev);
    824                 } else {
     817                        /* wait for autorepeat */
     818                        async_usleep(CHECK_DELAY);
    825819                        usb_kbd_destroy(kbd_dev);
    826820                }
     
    835829            USB_KBD_BOOT_REPORT_DESCRIPTOR,
    836830            USB_KBD_BOOT_REPORT_DESCRIPTOR_SIZE);
    837        
     831
    838832        if (rc != EOK) {
    839833                usb_log_error("Failed to parse boot report descriptor: %s\n",
     
    841835                return rc;
    842836        }
    843        
     837
    844838        rc = usbhid_req_set_protocol(&hid_dev->usb_dev->ctrl_pipe,
    845839            hid_dev->usb_dev->interface_no, USB_HID_PROTOCOL_BOOT);
    846        
     840
    847841        if (rc != EOK) {
    848842                usb_log_warning("Failed to set boot protocol to the device: "
     
    850844                return rc;
    851845        }
    852        
     846
    853847        return EOK;
    854848}
  • uspace/drv/bus/usb/usbhid/kbd/kbddev.h

    r3958e315 re3f7418  
    7575        /** Currently pressed modifiers (bitmap). */
    7676        uint8_t modifiers;
    77        
     77
    7878        /** Currently active modifiers including locks. Sent to the console. */
    7979        unsigned mods;
    80        
     80
    8181        /** Currently active lock keys. */
    8282        unsigned lock_keys;
    83        
     83
    8484        /** IPC session to the console device (for sending key events). */
    8585        async_sess_t *console_sess;
    86        
     86
    8787        /** @todo What is this actually? */
    8888        ddf_dev_ops_t ops;
    89        
     89
    9090        /** Information for auto-repeat of keys. */
    9191        usb_kbd_repeat_t repeat;
    92        
     92
    9393        /** Mutex for accessing the information about auto-repeat. */
    94         fibril_mutex_t *repeat_mtx;
    95        
     94        fibril_mutex_t repeat_mtx;
     95
    9696        uint8_t *output_buffer;
    97        
     97
    9898        size_t output_size;
    99        
     99
    100100        size_t led_output_size;
    101        
     101
    102102        usb_hid_report_path_t *led_path;
    103        
     103
    104104        int32_t *led_data;
    105        
     105
    106106        /** State of the structure (for checking before use).
    107107         *
     
    111111         */
    112112        int initialized;
     113
     114        /** DDF function */
     115        ddf_fun_t *fun;
    113116} usb_kbd_t;
    114117
  • uspace/drv/bus/usb/usbhid/kbd/kbdrepeat.c

    r3958e315 re3f7418  
    4646
    4747
    48 /** Delay between auto-repeat state checks when no key is being repeated. */
    49 static unsigned int CHECK_DELAY = 10000;
    5048
    5149/*----------------------------------------------------------------------------*/
     
    7371{
    7472        unsigned int delay = 0;
    75        
     73
    7674        usb_log_debug("Starting autorepeat loop.\n");
    7775
     
    7977                // check if the kbd structure is usable
    8078                if (!usb_kbd_is_initialized(kbd)) {
    81                         if (usb_kbd_is_ready_to_destroy(kbd)) {
    82                                 usb_kbd_destroy(kbd);
    83                         }
     79                        usb_log_warning("kbd not ready, exiting autorepeat.\n");
    8480                        return;
    8581                }
    8682               
    87                 fibril_mutex_lock(kbd->repeat_mtx);
     83                fibril_mutex_lock(&kbd->repeat_mtx);
    8884
    8985                if (kbd->repeat.key_new > 0) {
     
    109105                        delay = CHECK_DELAY;
    110106                }
    111                 fibril_mutex_unlock(kbd->repeat_mtx);
     107                fibril_mutex_unlock(&kbd->repeat_mtx);
    112108               
    113109                async_usleep(delay);
     
    130126{
    131127        usb_log_debug("Autorepeat fibril spawned.\n");
    132        
     128
    133129        if (arg == NULL) {
    134130                usb_log_error("No device!\n");
    135131                return EINVAL;
    136132        }
    137        
     133
    138134        usb_kbd_t *kbd = (usb_kbd_t *)arg;
    139        
     135
    140136        usb_kbd_repeat_loop(kbd);
    141        
     137
    142138        return EOK;
    143139}
     
    156152void usb_kbd_repeat_start(usb_kbd_t *kbd, unsigned int key)
    157153{
    158         fibril_mutex_lock(kbd->repeat_mtx);
     154        fibril_mutex_lock(&kbd->repeat_mtx);
    159155        kbd->repeat.key_new = key;
    160         fibril_mutex_unlock(kbd->repeat_mtx);
     156        fibril_mutex_unlock(&kbd->repeat_mtx);
    161157}
    162158
     
    174170void usb_kbd_repeat_stop(usb_kbd_t *kbd, unsigned int key)
    175171{
    176         fibril_mutex_lock(kbd->repeat_mtx);
     172        fibril_mutex_lock(&kbd->repeat_mtx);
    177173        if (key == kbd->repeat.key_new) {
    178174                kbd->repeat.key_new = 0;
    179175        }
    180         fibril_mutex_unlock(kbd->repeat_mtx);
     176        fibril_mutex_unlock(&kbd->repeat_mtx);
    181177}
    182178
  • uspace/drv/bus/usb/usbhid/kbd/kbdrepeat.h

    r3958e315 re3f7418  
    3737#define USB_HID_KBDREPEAT_H_
    3838
     39/** Delay between auto-repeat state checks when no key is being repeated. */
     40#define CHECK_DELAY 10000
     41
    3942struct usb_kbd_t;
    4043
  • uspace/drv/bus/usb/usbhid/main.c

    r3958e315 re3f7418  
    7676{
    7777        assert(dev != NULL);
    78        
    79         /*
    80          * Initialize device (get and process descriptors, get address, etc.)
    81          */
     78
     79        /* Initialize device (get and process descriptors, get address, etc.) */
    8280        usb_log_debug("Initializing USB/HID device...\n");
    83        
    84         usb_hid_dev_t *hid_dev = usb_hid_new();
     81
     82        usb_hid_dev_t *hid_dev =
     83            usb_device_data_alloc(dev, sizeof(usb_hid_dev_t));
    8584        if (hid_dev == NULL) {
    8685                usb_log_error("Error while creating USB/HID device "
     
    8887                return ENOMEM;
    8988        }
    90        
     89
    9190        int rc = usb_hid_init(hid_dev, dev);
    92        
     91
    9392        if (rc != EOK) {
    9493                usb_log_error("Failed to initialize USB/HID device.\n");
    9594                usb_hid_destroy(hid_dev);
    9695                return rc;
    97         }       
    98        
     96        }
     97
    9998        usb_log_debug("USB/HID device structure initialized.\n");
    100        
     99
    101100        /*
    102101         * 1) subdriver vytvori vlastnu ddf_fun, vlastne ddf_dev_ops, ktore da
     
    109108         *    pouzit usb/classes/hid/iface.h - prvy int je telefon
    110109         */
    111        
     110
    112111        /* Start automated polling function.
    113112         * This will create a separate fibril that will query the device
     
    125124           /* Custom argument. */
    126125           hid_dev);
    127        
    128        
     126
    129127        if (rc != EOK) {
    130128                usb_log_error("Failed to start polling fibril for `%s'.\n",
    131129                    dev->ddf_dev->name);
     130                usb_hid_destroy(hid_dev);
    132131                return rc;
    133132        }
     133        hid_dev->running = true;
     134        dev->driver_data = hid_dev;
    134135
    135136        /*
     
    153154{
    154155        usb_log_debug("usb_hid_device_add()\n");
    155        
     156
    156157        if (dev == NULL) {
    157158                usb_log_warning("Wrong parameter given for add_device().\n");
    158159                return EINVAL;
    159160        }
    160        
     161
    161162        if (dev->interface_no < 0) {
    162163                usb_log_warning("Device is not a supported HID device.\n");
     
    165166                return ENOTSUP;
    166167        }
    167        
     168
    168169        int rc = usb_hid_try_add_device(dev);
    169        
     170
    170171        if (rc != EOK) {
    171172                usb_log_warning("Device is not a supported HID device.\n");
     
    174175                return rc;
    175176        }
    176        
     177
    177178        usb_log_info("HID device `%s' ready to use.\n", dev->ddf_dev->name);
    178179
     
    182183/*----------------------------------------------------------------------------*/
    183184
    184 /* Currently, the framework supports only device adding. Once the framework
    185  * supports unplug, more callbacks will be added. */
     185/**
     186 * Callback for removing a device from the driver.
     187 *
     188 * @param dev Structure representing the device.
     189 *
     190 * @retval EOK if successful.
     191 * @retval EREFUSED if the device is not supported.
     192 */
     193static int usb_hid_device_gone(usb_device_t *dev)
     194{
     195        usb_hid_dev_t *hid_dev = dev->driver_data;
     196        unsigned tries = 10;
     197        while (hid_dev->running) {
     198                async_usleep(100000);
     199                if (!tries--) {
     200                        usb_log_error("Can't remove hub, still running.\n");
     201                        return EINPROGRESS;
     202                }
     203        }
     204
     205        assert(!hid_dev->running);
     206        usb_hid_destroy(hid_dev);
     207        usb_log_debug2("%s destruction complete.\n", dev->ddf_dev->name);
     208        return EOK;
     209}
     210
     211/** USB generic driver callbacks */
    186212static usb_driver_ops_t usb_hid_driver_ops = {
    187         .device_add = usb_hid_device_add,
     213        .device_add = usb_hid_device_add,
     214        .device_gone = usb_hid_device_gone,
    188215};
    189216
    190217
    191 /* The driver itself. */
     218/** The driver itself. */
    192219static usb_driver_t usb_hid_driver = {
    193220        .name = NAME,
  • uspace/drv/bus/usb/usbhid/mouse/mousedev.c

    r3958e315 re3f7418  
    124124{
    125125        usb_mouse_t *mouse_dev = (usb_mouse_t *) fun->driver_data;
    126        
     126
    127127        if (mouse_dev == NULL) {
    128128                usb_log_debug("default_connection_handler: Missing "
     
    131131                return;
    132132        }
    133        
     133
    134134        usb_log_debug("default_connection_handler: fun->name: %s\n",
    135135                      fun->name);
    136136        usb_log_debug("default_connection_handler: mouse_sess: %p, "
    137137            "wheel_sess: %p\n", mouse_dev->mouse_sess, mouse_dev->wheel_sess);
    138        
     138
    139139        async_sess_t **sess_ptr =
    140140            (str_cmp(fun->name, HID_MOUSE_FUN_NAME) == 0) ?
    141141            &mouse_dev->mouse_sess : &mouse_dev->wheel_sess;
    142        
     142
    143143        async_sess_t *sess =
    144144            async_callback_receive_start(EXCHANGE_SERIALIZE, icall);
     
    170170        mouse->mouse_sess = NULL;
    171171        mouse->wheel_sess = NULL;
    172        
     172
    173173        return mouse;
    174174}
     
    179179{
    180180        assert(mouse_dev != NULL);
    181        
     181
    182182        // hangup session to the console
    183183        if (mouse_dev->mouse_sess != NULL)
    184184                async_hangup(mouse_dev->mouse_sess);
    185        
     185
    186186        if (mouse_dev->wheel_sess != NULL)
    187187                async_hangup(mouse_dev->wheel_sess);
     188        int ret = ddf_fun_unbind(mouse_dev->mouse_fun);
     189        if (ret != EOK) {
     190                usb_log_error("Failed to unbind mouse function.\n");
     191        } else {
     192                ddf_fun_destroy(mouse_dev->mouse_fun);
     193                /* Prevent double free */
     194                mouse_dev->wheel_fun->driver_data = NULL;
     195        }
     196
     197        ret = ddf_fun_unbind(mouse_dev->wheel_fun);
     198        if (ret != EOK) {
     199                usb_log_error("Failed to unbind wheel function.\n");
     200        } else {
     201                ddf_fun_destroy(mouse_dev->wheel_fun);
     202        }
    188203}
    189204
     
    199214                return;
    200215        }
    201        
     216
    202217        int count = ((wheel < 0) ? -wheel : wheel) * ARROWS_PER_SINGLE_WHEEL;
    203218        int i;
    204        
     219
    205220        for (i = 0; i < count; i++) {
    206221                /* Send arrow press and release. */
     
    246261{
    247262        assert(mouse_dev != NULL);
    248        
     263
    249264        if (mouse_dev->mouse_sess == NULL) {
    250265                usb_log_warning(NAME " No console session.\n");
     
    264279                async_exchange_end(exch);
    265280        }
    266        
     281
    267282        if (wheel != 0)
    268283                usb_mouse_send_wheel(mouse_dev, wheel);
    269        
     284
    270285        /*
    271286         * Buttons
     
    274289        usb_hid_report_path_append_item(path, USB_HIDUT_PAGE_BUTTON, 0);
    275290        usb_hid_report_path_set_report_id(path, hid_dev->report_id);
    276        
     291
    277292        usb_hid_report_field_t *field = usb_hid_report_get_sibling(
    278293            hid_dev->report, NULL, path, USB_HID_PATH_COMPARE_END
     
    309324                    USB_HID_REPORT_TYPE_INPUT);
    310325        }
    311        
     326
    312327        usb_hid_report_path_free(path);
    313328
     
    321336        assert(hid_dev != NULL);
    322337        assert(mouse != NULL);
    323        
     338
    324339        /* Create the exposed function. */
    325340        usb_log_debug("Creating DDF function %s...\n", HID_MOUSE_FUN_NAME);
     
    330345                return ENOMEM;
    331346        }
    332        
     347
    333348        fun->ops = &mouse->ops;
    334349        fun->driver_data = mouse;
     
    338353                usb_log_error("Could not bind DDF function: %s.\n",
    339354                    str_error(rc));
    340                 ddf_fun_destroy(fun);
    341                 return rc;
    342         }
    343        
     355                return rc;
     356        }
     357
    344358        usb_log_debug("Adding DDF function to category %s...\n",
    345359            HID_MOUSE_CATEGORY);
     
    349363                    "Could not add DDF function to category %s: %s.\n",
    350364                    HID_MOUSE_CATEGORY, str_error(rc));
    351                 ddf_fun_destroy(fun);
    352                 return rc;
    353         }
    354        
     365                return rc;
     366        }
     367        mouse->mouse_fun = fun;
     368
    355369        /*
    356370         * Special function for acting as keyboard (wheel)
     
    364378                return ENOMEM;
    365379        }
    366        
     380
    367381        /*
    368382         * Store the initialized HID device and HID ops
     
    376390                usb_log_error("Could not bind DDF function: %s.\n",
    377391                    str_error(rc));
    378                 ddf_fun_destroy(fun);
    379                 return rc;
    380         }
    381        
     392                return rc;
     393        }
     394
    382395        usb_log_debug("Adding DDF function to category %s...\n",
    383396            HID_MOUSE_WHEEL_CATEGORY);
     
    387400                    "Could not add DDF function to category %s: %s.\n",
    388401                    HID_MOUSE_WHEEL_CATEGORY, str_error(rc));
    389                 ddf_fun_destroy(fun);
    390                 return rc;
    391         }
    392        
     402                return rc;
     403        }
     404        mouse->wheel_fun = fun;
     405
    393406        return EOK;
    394407}
     
    441454{
    442455        usb_log_debug("Initializing HID/Mouse structure...\n");
    443        
     456
    444457        if (hid_dev == NULL) {
    445458                usb_log_error("Failed to init keyboard structure: no structure"
     
    447460                return EINVAL;
    448461        }
    449        
     462
    450463        usb_mouse_t *mouse_dev = usb_mouse_new();
    451464        if (mouse_dev == NULL) {
     
    454467                return ENOMEM;
    455468        }
    456        
     469
    457470        // FIXME: This may not be optimal since stupid hardware vendor may
    458471        // use buttons 1, 2, 3 and 6000 and we would allocate array of
     
    464477            hid_dev->report_id) + 1;
    465478        mouse_dev->buttons = calloc(mouse_dev->buttons_count, sizeof(int32_t));
    466        
     479
    467480        if (mouse_dev->buttons == NULL) {
    468481                usb_log_error(NAME ": out of memory, giving up on device!\n");
     
    474487        // save the Mouse device structure into the HID device structure
    475488        *data = mouse_dev;
    476        
     489
    477490        // set handler for incoming calls
    478491        mouse_dev->ops.default_handler = default_connection_handler;
    479        
     492
    480493        // TODO: how to know if the device supports the request???
    481494        usbhid_req_set_idle(&hid_dev->usb_dev->ctrl_pipe,
    482495            hid_dev->usb_dev->interface_no, IDLE_RATE);
    483        
     496
    484497        int rc = usb_mouse_create_function(hid_dev, mouse_dev);
    485498        if (rc != EOK) {
     
    487500                return rc;
    488501        }
    489        
     502
    490503        return EOK;
    491504}
     
    500513                return false;
    501514        }
    502        
     515
    503516        usb_mouse_t *mouse_dev = (usb_mouse_t *)data;
    504517               
     
    511524{
    512525        if (data != NULL) {
    513                 usb_mouse_destroy((usb_mouse_t *)data);
     526                usb_mouse_destroy(data);
    514527        }
    515528}
     
    522535            USB_MOUSE_BOOT_REPORT_DESCRIPTOR,
    523536            USB_MOUSE_BOOT_REPORT_DESCRIPTOR_SIZE);
    524        
     537
    525538        if (rc != EOK) {
    526539                usb_log_error("Failed to parse boot report descriptor: %s\n",
     
    528541                return rc;
    529542        }
    530        
     543
    531544        rc = usbhid_req_set_protocol(&hid_dev->usb_dev->ctrl_pipe,
    532545            hid_dev->usb_dev->interface_no, USB_HID_PROTOCOL_BOOT);
    533        
     546
    534547        if (rc != EOK) {
    535548                usb_log_warning("Failed to set boot protocol to the device: "
     
    537550                return rc;
    538551        }
    539        
     552
    540553        return EOK;
    541554}
  • uspace/drv/bus/usb/usbhid/mouse/mousedev.h

    r3958e315 re3f7418  
    4949        async_sess_t *mouse_sess;
    5050        async_sess_t *wheel_sess;
    51        
     51
    5252        /* Mouse buttons statuses. */
    5353        int32_t *buttons;
    5454        size_t buttons_count;
    55        
     55
    5656        ddf_dev_ops_t ops;
     57        /* DDF mouse function */
     58        ddf_fun_t *mouse_fun;
     59        /* DDF mouse function */
     60        ddf_fun_t *wheel_fun;
    5761} usb_mouse_t;
    5862
  • uspace/drv/bus/usb/usbhid/multimedia/multimedia.c

    r3958e315 re3f7418  
    6464        //int32_t *keys;
    6565        /** Count of stored keys (i.e. number of keys in the report). */
    66         //size_t key_count;     
     66        //size_t key_count;
    6767        /** IPC session to the console device (for sending key events). */
    6868        async_sess_t *console_sess;
     69        /** DDF function */
     70        ddf_fun_t *fun;
    6971} usb_multimedia_t;
    7072
     
    8688{
    8789        usb_log_debug(NAME " default_connection_handler()\n");
    88        
     90
    8991        usb_multimedia_t *multim_dev = (usb_multimedia_t *)fun->driver_data;
    90        
     92
    9193        if (multim_dev == NULL) {
    9294                async_answer_0(icallid, EINVAL);
    9395                return;
    9496        }
    95        
     97
    9698        async_sess_t *sess =
    9799            async_callback_receive_start(EXCHANGE_SERIALIZE, icall);
     
    137139        assert(hid_dev != NULL);
    138140        assert(multim_dev != NULL);
    139        
     141
    140142        kbd_event_t ev;
    141        
     143
    142144        ev.type = type;
    143145        ev.key = key;
     
    151153                return;
    152154        }
    153        
     155
    154156        async_exch_t *exch = async_exchange_begin(multim_dev->console_sess);
    155157        async_msg_4(exch, KBDEV_EVENT, ev.type, ev.key, ev.mods, ev.c);
     
    169171                return ENOMEM;
    170172        }
    171        
     173
    172174        fun->ops = &multimedia_ops;
    173175        fun->driver_data = multim_dev;   // TODO: maybe change to hid_dev->data
    174        
     176
    175177        int rc = ddf_fun_bind(fun);
    176178        if (rc != EOK) {
    177179                usb_log_error("Could not bind DDF function: %s.\n",
    178180                    str_error(rc));
    179                 // TODO: Can / should I destroy the DDF function?
    180181                ddf_fun_destroy(fun);
    181182                return rc;
    182183        }
    183        
     184
    184185        usb_log_debug("%s function created (handle: %" PRIun ").\n",
    185186            NAME, fun->handle);
    186        
     187
    187188        rc = ddf_fun_add_to_category(fun, "keyboard");
    188189        if (rc != EOK) {
     
    190191                    "Could not add DDF function to category 'keyboard': %s.\n",
    191192                    str_error(rc));
    192                 // TODO: Can / should I destroy the DDF function?
    193193                ddf_fun_destroy(fun);
    194194                return rc;
    195195        }
    196        
     196        multim_dev->fun = fun;
     197
    197198        return EOK;
    198199}
     
    205206                return EINVAL; /*! @todo Other return code? */
    206207        }
    207        
     208
    208209        usb_log_debug(NAME " Initializing HID/multimedia structure...\n");
    209        
     210
    210211        usb_multimedia_t *multim_dev = (usb_multimedia_t *)malloc(
    211212            sizeof(usb_multimedia_t));
     
    213214                return ENOMEM;
    214215        }
    215        
     216
    216217        multim_dev->console_sess = NULL;
    217        
     218
    218219        /*! @todo Autorepeat */
    219        
     220
    220221        // save the KBD device structure into the HID device structure
    221222        *data = multim_dev;
    222        
     223
    223224        usb_log_debug(NAME " HID/multimedia device structure initialized.\n");
    224        
     225
    225226        int rc = usb_multimedia_create_function(hid_dev, multim_dev);
    226227        if (rc != EOK)
    227228                return rc;
    228        
     229
    229230        usb_log_debug(NAME " HID/multimedia structure initialized.\n");
    230        
     231
    231232        return EOK;
    232233}
     
    239240                return;
    240241        }
    241        
     242
    242243        if (data != NULL) {
    243244                usb_multimedia_t *multim_dev = (usb_multimedia_t *)data;
    244245                // hangup session to the console
    245246                async_hangup(multim_dev->console_sess);
     247                const int ret = ddf_fun_unbind(multim_dev->fun);
     248                if (ret != EOK) {
     249                        usb_log_error("Failed to unbind multim function.\n");
     250                } else {
     251                        usb_log_debug2("%s unbound.\n", multim_dev->fun->name);
     252                        ddf_fun_destroy(multim_dev->fun);
     253                }
    246254        }
    247255}
     
    257265
    258266        usb_multimedia_t *multim_dev = (usb_multimedia_t *)data;
    259        
     267
    260268        usb_hid_report_path_t *path = usb_hid_report_path();
    261269        usb_hid_report_path_append_item(path, USB_HIDUT_PAGE_CONSUMER, 0);
     
    283291                                               key);
    284292                }
    285                
     293
    286294                field = usb_hid_report_get_sibling(
    287295                    hid_dev->report, field, path, USB_HID_PATH_COMPARE_END
    288296                    | USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY,
    289297                    USB_HID_REPORT_TYPE_INPUT);
    290         }       
     298        }
    291299
    292300        usb_hid_report_path_free(path);
    293        
     301
    294302        return true;
    295303}
  • uspace/drv/bus/usb/usbhid/subdrivers.h

    r3958e315 re3f7418  
    6464         */
    6565        const usb_hid_subdriver_usage_t *usage_path;
    66        
     66
    6767        /** Report ID for which the path should apply. */
    6868        int report_id;
    69        
     69
    7070        /** Compare type for the Usage path. */
    7171        int compare;
    72        
     72
    7373        /** Vendor ID (set to -1 if not specified). */
    7474        int vendor_id;
    75        
     75
    7676        /** Product ID (set to -1 if not specified). */
    7777        int product_id;
    78        
     78
    7979        /** Subdriver for controlling this device. */
    8080        usb_hid_subdriver_t subdriver;
  • uspace/drv/bus/usb/usbhid/usbhid.c

    r3958e315 re3f7418  
    5454
    5555/* Array of endpoints expected on the device, NULL terminated. */
    56 usb_endpoint_description_t *usb_hid_endpoints[USB_HID_POLL_EP_COUNT + 1] = {
     56usb_endpoint_description_t *usb_hid_endpoints[] = {
    5757        &usb_hid_kbd_poll_endpoint_description,
    5858        &usb_hid_mouse_poll_endpoint_description,
     
    6868{
    6969        assert(hid_dev != NULL && hid_dev->subdriver_count == 0);
    70        
     70
    7171        hid_dev->subdrivers = (usb_hid_subdriver_t *)malloc(
    7272            sizeof(usb_hid_subdriver_t));
     
    7474                return ENOMEM;
    7575        }
    76        
     76
    7777        assert(hid_dev->subdriver_count >= 0);
    78        
     78
    7979        // set the init callback
    8080        hid_dev->subdrivers[hid_dev->subdriver_count].init = usb_kbd_init;
    81        
     81
    8282        // set the polling callback
    8383        hid_dev->subdrivers[hid_dev->subdriver_count].poll =
    8484            usb_kbd_polling_callback;
    85        
     85
    8686        // set the polling ended callback
    8787        hid_dev->subdrivers[hid_dev->subdriver_count].poll_end = NULL;
    88        
     88
    8989        // set the deinit callback
    9090        hid_dev->subdrivers[hid_dev->subdriver_count].deinit = usb_kbd_deinit;
    91        
     91
    9292        // set subdriver count
    9393        ++hid_dev->subdriver_count;
    94        
     94
    9595        return EOK;
    9696}
     
    101101{
    102102        assert(hid_dev != NULL && hid_dev->subdriver_count == 0);
    103        
     103
    104104        hid_dev->subdrivers = (usb_hid_subdriver_t *)malloc(
    105105            sizeof(usb_hid_subdriver_t));
     
    107107                return ENOMEM;
    108108        }
    109        
     109
    110110        assert(hid_dev->subdriver_count >= 0);
    111        
     111
    112112        // set the init callback
    113113        hid_dev->subdrivers[hid_dev->subdriver_count].init = usb_mouse_init;
    114        
     114
    115115        // set the polling callback
    116116        hid_dev->subdrivers[hid_dev->subdriver_count].poll =
    117117            usb_mouse_polling_callback;
    118        
     118
    119119        // set the polling ended callback
    120120        hid_dev->subdrivers[hid_dev->subdriver_count].poll_end = NULL;
    121        
     121
    122122        // set the deinit callback
    123123        hid_dev->subdrivers[hid_dev->subdriver_count].deinit = usb_mouse_deinit;
    124        
     124
    125125        // set subdriver count
    126126        ++hid_dev->subdriver_count;
    127        
     127
    128128        return EOK;
    129129}
     
    134134{
    135135        assert(hid_dev != NULL && hid_dev->subdriver_count == 0);
    136        
     136
    137137        hid_dev->subdrivers = (usb_hid_subdriver_t *)malloc(
    138138            sizeof(usb_hid_subdriver_t));
     
    140140                return ENOMEM;
    141141        }
    142        
     142
    143143        assert(hid_dev->subdriver_count >= 0);
    144        
     144
    145145        // set the init callback
    146146        hid_dev->subdrivers[hid_dev->subdriver_count].init =
    147147            usb_generic_hid_init;
    148        
     148
    149149        // set the polling callback
    150150        hid_dev->subdrivers[hid_dev->subdriver_count].poll =
    151151            usb_generic_hid_polling_callback;
    152        
     152
    153153        // set the polling ended callback
    154154        hid_dev->subdrivers[hid_dev->subdriver_count].poll_end = NULL;
    155        
     155
    156156        // set the deinit callback
    157         hid_dev->subdrivers[hid_dev->subdriver_count].deinit = NULL;
    158        
     157        hid_dev->subdrivers[hid_dev->subdriver_count].deinit =
     158            usb_generic_hid_deinit;
     159
    159160        // set subdriver count
    160161        ++hid_dev->subdriver_count;
    161        
     162
    162163        return EOK;
    163164}
     
    170171        assert(hid_dev != NULL);
    171172        assert(hid_dev->usb_dev != NULL);
    172        
     173
    173174        return (hid_dev->usb_dev->descriptors.device.vendor_id
    174175            == mapping->vendor_id
     
    184185        assert(hid_dev != NULL);
    185186        assert(mapping != NULL);
    186        
     187
    187188        usb_hid_report_path_t *usage_path = usb_hid_report_path();
    188189        if (usage_path == NULL) {
     
    202203                ++i;
    203204        }
    204        
     205
    205206        assert(hid_dev->report != NULL);
    206        
     207
    207208        usb_log_debug("Compare flags: %d\n", mapping->compare);
    208        
     209
    209210        bool matches = false;
    210211        uint8_t report_id = mapping->report_id;
     
    234235                    USB_HID_REPORT_TYPE_INPUT);
    235236        } while (!matches && report_id != 0);
    236        
     237
    237238        usb_hid_report_path_free(usage_path);
    238        
     239
    239240        return matches;
    240241}
     
    246247{
    247248        int i;
    248        
     249
    249250        if (count <= 0) {
    250251                hid_dev->subdriver_count = 0;
     
    252253                return EOK;
    253254        }
    254        
     255
    255256        // add one generic HID subdriver per device
    256        
     257
    257258        hid_dev->subdrivers = (usb_hid_subdriver_t *)malloc((count + 1) *
    258259            sizeof(usb_hid_subdriver_t));
     
    260261                return ENOMEM;
    261262        }
    262        
     263
    263264        for (i = 0; i < count; ++i) {
    264265                hid_dev->subdrivers[i].init = subdrivers[i]->init;
     
    267268                hid_dev->subdrivers[i].poll_end = subdrivers[i]->poll_end;
    268269        }
    269        
     270
    270271        hid_dev->subdrivers[count].init = usb_generic_hid_init;
    271272        hid_dev->subdrivers[count].poll = usb_generic_hid_polling_callback;
    272         hid_dev->subdrivers[count].deinit = NULL;
     273        hid_dev->subdrivers[count].deinit = usb_generic_hid_deinit;
    273274        hid_dev->subdrivers[count].poll_end = NULL;
    274        
     275
    275276        hid_dev->subdriver_count = count + 1;
    276        
     277
    277278        return EOK;
    278279}
     
    283284{
    284285        assert(hid_dev != NULL);
    285        
     286
    286287        const usb_hid_subdriver_t *subdrivers[USB_HID_MAX_SUBDRIVERS];
    287        
     288
    288289        int i = 0, count = 0;
    289290        const usb_hid_subdriver_mapping_t *mapping = &usb_hid_subdrivers[i];
     
    291292        bool ids_matched;
    292293        bool matched;
    293        
     294
    294295        while (count < USB_HID_MAX_SUBDRIVERS &&
    295296            (mapping->usage_path != NULL
     
    339340                mapping = &usb_hid_subdrivers[++i];
    340341        }
    341        
     342
    342343        // we have all subdrivers determined, save them into the hid device
    343344        return usb_hid_save_subdrivers(hid_dev, subdrivers, count);
     
    349350{
    350351        assert(hid_dev != NULL && dev != NULL);
    351        
     352
    352353        int rc = EOK;
    353        
     354
    354355        if (dev->pipes[USB_HID_KBD_POLL_EP_NO].present) {
    355356                usb_log_debug("Found keyboard endpoint.\n");
     
    369370                rc = ENOTSUP;
    370371        }
    371        
     372
    372373        return rc;
    373374}
     
    378379{
    379380        assert(hid_dev != NULL && hid_dev->report != NULL);
    380        
     381
    381382        uint8_t report_id = 0;
    382383        size_t size;
    383        
     384
    384385        size_t max_size = 0;
    385        
     386
    386387        do {
    387388                usb_log_debug("Getting size of the report.\n");
     
    394395                    report_id, USB_HID_REPORT_TYPE_INPUT);
    395396        } while (report_id != 0);
    396        
     397
    397398        usb_log_debug("Max size of input report: %zu\n", max_size);
    398        
     399
    399400        hid_dev->max_input_report_size = max_size;
    400401        assert(hid_dev->input_report == NULL);
    401        
     402
    402403        hid_dev->input_report = malloc(max_size);
    403404        if (hid_dev->input_report == NULL) {
     
    405406        }
    406407        memset(hid_dev->input_report, 0, max_size);
    407        
     408
    408409        return EOK;
    409410}
     
    411412/*----------------------------------------------------------------------------*/
    412413
    413 usb_hid_dev_t *usb_hid_new(void)
    414 {
    415         usb_hid_dev_t *hid_dev = (usb_hid_dev_t *)calloc(1,
    416             sizeof(usb_hid_dev_t));
    417        
    418         if (hid_dev == NULL) {
    419                 usb_log_fatal("No memory!\n");
    420                 return NULL;
    421         }
    422        
    423         hid_dev->report = (usb_hid_report_t *)(malloc(sizeof(
    424             usb_hid_report_t)));
    425         if (hid_dev->report == NULL) {
    426                 usb_log_fatal("No memory!\n");
    427                 free(hid_dev);
    428                 return NULL;
    429         }
    430        
    431         hid_dev->poll_pipe_index = -1;
    432        
    433         return hid_dev;
    434 }
    435 
    436 /*----------------------------------------------------------------------------*/
    437 
    438414int usb_hid_init(usb_hid_dev_t *hid_dev, usb_device_t *dev)
    439415{
    440416        int rc, i;
    441        
     417
    442418        usb_log_debug("Initializing HID structure...\n");
    443        
     419
    444420        if (hid_dev == NULL) {
    445421                usb_log_error("Failed to init HID structure: no structure given"
     
    447423                return EINVAL;
    448424        }
    449        
     425
    450426        if (dev == NULL) {
    451427                usb_log_error("Failed to init HID structure: no USB device"
     
    453429                return EINVAL;
    454430        }
    455        
     431
     432        hid_dev->report = (usb_hid_report_t *)(malloc(sizeof(
     433            usb_hid_report_t)));
     434        if (hid_dev->report == NULL) {
     435                usb_log_error("No memory!\n");
     436                return ENOMEM;
     437        }
     438        usb_hid_report_init(hid_dev->report);
     439
    456440        /* The USB device should already be initialized, save it in structure */
    457441        hid_dev->usb_dev = dev;
    458        
     442        hid_dev->poll_pipe_index = -1;
     443
    459444        rc = usb_hid_check_pipes(hid_dev, dev);
    460445        if (rc != EOK) {
     
    465450        rc = usb_hid_process_report_descriptor(hid_dev->usb_dev,
    466451            hid_dev->report, &hid_dev->report_desc, &hid_dev->report_desc_size);
    467        
     452
    468453        bool fallback = false;
    469        
     454
    470455        if (rc == EOK) {
    471456                // try to find subdrivers that may want to handle this device
     
    484469                fallback = true;
    485470        }
    486        
     471
    487472        if (fallback) {
    488473                // fall back to boot protocol
     
    510495                }
    511496        }
    512        
     497
    513498        if (rc != EOK) {
    514499                usb_log_error("No subdriver for handling this device could be"
     
    542527                rc = (ok) ? EOK : -1;   // what error to report
    543528        }
    544        
    545        
     529
     530
    546531        if (rc == EOK) {
    547532                // save max input report size and allocate space for the report
     
    552537                }
    553538        }
    554        
    555        
     539
     540
    556541        return rc;
    557542}
     
    563548{
    564549        int i;
    565        
     550
    566551        if (dev == NULL || arg == NULL || buffer == NULL) {
    567552                usb_log_error("Missing arguments to polling callback.\n");
    568553                return false;
    569554        }
    570        
     555
    571556        usb_hid_dev_t *hid_dev = (usb_hid_dev_t *)arg;
    572        
     557
    573558        assert(hid_dev->input_report != NULL);
    574559        usb_log_debug("New data [%zu/%zu]: %s\n", buffer_size,
     
    582567                usb_hid_new_report(hid_dev);
    583568        }
    584        
     569
    585570        // parse the input report
    586        
     571
    587572        int rc = usb_hid_parse_report(hid_dev->report, buffer, buffer_size,
    588573            &hid_dev->report_id);
    589        
     574
    590575        if (rc != EOK) {
    591576                usb_log_warning("Error in usb_hid_parse_report():"
    592577                    "%s\n", str_error(rc));
    593578        }       
    594        
     579
    595580        bool cont = false;
    596        
     581
    597582        // continue if at least one of the subdrivers want to continue
    598583        for (i = 0; i < hid_dev->subdriver_count; ++i) {
     
    603588                }
    604589        }
    605        
     590
    606591        return cont;
    607592}
     
    613598{
    614599        int i;
    615        
     600
    616601        if (dev == NULL || arg == NULL) {
    617602                return;
    618603        }
    619        
     604
    620605        usb_hid_dev_t *hid_dev = (usb_hid_dev_t *)arg;
    621        
     606
    622607        for (i = 0; i < hid_dev->subdriver_count; ++i) {
    623608                if (hid_dev->subdrivers[i].poll_end != NULL) {
     
    626611                }
    627612        }
    628        
    629         usb_hid_destroy(hid_dev);
     613
     614        hid_dev->running = false;
    630615}
    631616
     
    649634{
    650635        int i;
    651        
     636
    652637        if (hid_dev == NULL) {
    653638                return;
    654639        }
    655        
     640
    656641        usb_log_debug("Subdrivers: %p, subdriver count: %d\n",
    657642            hid_dev->subdrivers, hid_dev->subdriver_count);
    658        
     643
    659644        assert(hid_dev->subdrivers != NULL
    660645            || hid_dev->subdriver_count == 0);
    661        
     646
    662647        for (i = 0; i < hid_dev->subdriver_count; ++i) {
    663648                if (hid_dev->subdrivers[i].deinit != NULL) {
     
    666651                }
    667652        }
    668        
    669         // free the subdrivers info
    670         if (hid_dev->subdrivers != NULL) {
    671                 free(hid_dev->subdrivers);
    672         }
    673 
    674         // destroy the parser
     653
     654        /* Free allocated structures */
     655        free(hid_dev->subdrivers);
     656        free(hid_dev->report_desc);
     657
     658        /* Destroy the parser */
    675659        if (hid_dev->report != NULL) {
    676660                usb_hid_free_report(hid_dev->report);
    677661        }
    678662
    679         if (hid_dev->report_desc != NULL) {
    680                 free(hid_dev->report_desc);
    681         }
    682663}
    683664
  • uspace/drv/bus/usb/usbhid/usbhid.h

    r3958e315 re3f7418  
    102102        /** Structure holding generic USB device information. */
    103103        usb_device_t *usb_dev;
    104        
     104
    105105        /** Index of the polling pipe in usb_hid_endpoints array. */
    106106        int poll_pipe_index;
    107        
     107
    108108        /** Subdrivers. */
    109109        usb_hid_subdriver_t *subdrivers;
    110        
     110
    111111        /** Number of subdrivers. */
    112112        int subdriver_count;
    113        
     113
    114114        /** Report descriptor. */
    115115        uint8_t *report_desc;
     
    117117        /** Report descriptor size. */
    118118        size_t report_desc_size;
    119        
     119
    120120        /** HID Report parser. */
    121121        usb_hid_report_t *report;
    122        
     122
    123123        uint8_t report_id;
    124        
     124
    125125        uint8_t *input_report;
    126        
     126
    127127        size_t input_report_size;
    128128        size_t max_input_report_size;
    129        
     129
    130130        int report_nr;
     131        volatile bool running;
    131132};
    132133
     
    140141};
    141142
    142 usb_endpoint_description_t *usb_hid_endpoints[USB_HID_POLL_EP_COUNT + 1];
     143extern usb_endpoint_description_t *usb_hid_endpoints[];
    143144
    144145/*----------------------------------------------------------------------------*/
    145 
    146 usb_hid_dev_t *usb_hid_new(void);
    147146
    148147int usb_hid_init(usb_hid_dev_t *hid_dev, usb_device_t *dev);
  • uspace/drv/bus/usb/usbhub/port.c

    r3958e315 re3f7418  
    288288        port->attached_device.fun = NULL;
    289289
    290         ret = usb_hc_unregister_device(&hub->connection,
    291             port->attached_device.address);
    292         if (ret != EOK) {
    293                 usb_log_warning("Failed to unregister address of the removed "
    294                     "device: %s.\n", str_error(ret));
    295         }
     290        ret = usb_hc_connection_open(&hub->connection);
     291        if (ret == EOK) {
     292                ret = usb_hc_unregister_device(&hub->connection,
     293                    port->attached_device.address);
     294                if (ret != EOK) {
     295                        usb_log_warning("Failed to unregister address of the "
     296                            "removed device: %s.\n", str_error(ret));
     297                }
     298                ret = usb_hc_connection_close(&hub->connection);
     299                if (ret != EOK) {
     300                        usb_log_warning("Failed to close hc connection %s.\n",
     301                            str_error(ret));
     302                }
     303
     304        } else {
     305                usb_log_warning("Failed to open hc connection %s.\n",
     306                    str_error(ret));
     307        }
     308
    296309        port->attached_device.address = -1;
    297310        fibril_mutex_unlock(&port->mutex);
  • uspace/drv/bus/usb/usbhub/usbhub.c

    r3958e315 re3f7418  
    117117        ddf_fun_destroy(hub->hub_fun);
    118118
    119         free(hub);
    120         usb_dev->driver_data = NULL;
    121119        usb_log_info("USB hub driver, stopped and cleaned.\n");
    122120        return EOK;
     
    254252{
    255253        assert(usb_dev);
    256         usb_hub_dev_t *hub_dev = malloc(sizeof(usb_hub_dev_t));
     254        usb_hub_dev_t *hub_dev =
     255            usb_device_data_alloc(usb_dev, sizeof(usb_hub_dev_t));
    257256        if (!hub_dev)
    258257            return NULL;
    259258
    260259        hub_dev->usb_device = usb_dev;
    261 
    262260        hub_dev->ports = NULL;
    263261        hub_dev->port_count = 0;
     
    266264        fibril_mutex_initialize(&hub_dev->pending_ops_mutex);
    267265        fibril_condvar_initialize(&hub_dev->pending_ops_cv);
    268         usb_dev->driver_data = hub_dev;
    269266
    270267        return hub_dev;
  • uspace/drv/bus/usb/usbmast/main.c

    r3958e315 re3f7418  
    8282    void *arg);
    8383
     84/** Callback when a device is removed from the system.
     85 *
     86 * @param dev Representation of USB device.
     87 * @return Error code.
     88 */
     89static int usbmast_device_gone(usb_device_t *dev)
     90{
     91        usbmast_dev_t *mdev = dev->driver_data;
     92        assert(mdev);
     93
     94        for (size_t i = 0; i < mdev->lun_count; ++i) {
     95                const int rc = ddf_fun_unbind(mdev->luns[i]);
     96                if (rc != EOK) {
     97                        usb_log_error("Failed to unbind LUN function %zu: "
     98                            "%s\n", i, str_error(rc));
     99                        return rc;
     100                }
     101                ddf_fun_destroy(mdev->luns[i]);
     102                mdev->luns[i] = NULL;
     103        }
     104        free(mdev->luns);
     105        return EOK;
     106}
     107
    84108/** Callback when new device is attached and recognized as a mass storage.
    85109 *
     
    94118
    95119        /* Allocate softstate */
    96         dev->driver_data = mdev = malloc(sizeof(usbmast_dev_t));
     120        mdev = usb_device_data_alloc(dev, sizeof(usbmast_dev_t));
    97121        if (mdev == NULL) {
    98122                usb_log_error("Failed allocating softstate.\n");
     
    112136
    113137        usb_log_debug("Get LUN count...\n");
    114         mdev->luns = usb_masstor_get_lun_count(mdev);
    115 
    116         for (i = 0; i < mdev->luns; i++) {
     138        mdev->lun_count = usb_masstor_get_lun_count(mdev);
     139        mdev->luns = calloc(mdev->lun_count, sizeof(ddf_fun_t*));
     140        if (mdev->luns == NULL) {
     141                rc = ENOMEM;
     142                usb_log_error("Failed allocating luns table.\n");
     143                goto error;
     144        }
     145
     146        for (i = 0; i < mdev->lun_count; i++) {
    117147                rc = usbmast_fun_create(mdev, i);
    118148                if (rc != EOK)
     
    122152        return EOK;
    123153error:
    124         /* XXX Destroy functions */
     154        /* Destroy functions */
     155        for (size_t i = 0; i < mdev->lun_count; ++i) {
     156                if (mdev->luns[i] == NULL)
     157                        continue;
     158                const int rc = ddf_fun_unbind(mdev->luns[i]);
     159                if (rc != EOK) {
     160                        usb_log_warning("Failed to unbind LUN function %zu: "
     161                            "%s.\n", i, str_error(rc));
     162                }
     163                ddf_fun_destroy(mdev->luns[i]);
     164        }
     165        free(mdev->luns);
     166        free(mdev);
    125167        return rc;
    126168}
     
    162204        }
    163205
     206        mfun->ddf_fun = fun;
    164207        mfun->mdev = mdev;
    165208        mfun->lun = lun;
     
    212255
    213256        free(fun_name);
     257        mdev->luns[lun] = fun;
    214258
    215259        return EOK;
     
    295339static usb_driver_ops_t usbmast_driver_ops = {
    296340        .device_add = usbmast_device_add,
     341        .device_gone = usbmast_device_gone,
    297342};
    298343
  • uspace/drv/bus/usb/usbmast/usbmast.h

    r3958e315 re3f7418  
    4141
    4242/** Mass storage device. */
    43 typedef struct {
     43typedef struct usbmast_dev {
    4444        /** DDF device */
    4545        ddf_dev_t *ddf_dev;
     
    4747        usb_device_t *usb_dev;
    4848        /** Number of LUNs */
    49         unsigned luns;
     49        unsigned lun_count;
     50        /** LUN functions */
     51        ddf_fun_t **luns;
    5052} usbmast_dev_t;
     53
    5154
    5255/** Mass storage function.
  • uspace/drv/bus/usb/usbmid/dump.c

    r3958e315 re3f7418  
    4747 * @param depth Nesting depth.
    4848 */
    49 static void dump_tree_descriptor(uint8_t *data, size_t depth)
     49static void dump_tree_descriptor(const uint8_t *data, size_t depth)
    5050{
    5151        if (data == NULL) {
    5252                return;
    5353        }
    54         int type = (int) *(data + 1);
     54        const int type = data[1];
    5555        if (type == USB_DESCTYPE_INTERFACE) {
    5656                usb_standard_interface_descriptor_t *descriptor
     
    7171 * @param depth Nesting depth.
    7272 */
    73 static void dump_tree_internal(usb_dp_parser_t *parser, usb_dp_parser_data_t *data,
    74     uint8_t *root, size_t depth)
     73static void dump_tree_internal(
     74    usb_dp_parser_t *parser, usb_dp_parser_data_t *data,
     75    const uint8_t *root, size_t depth)
    7576{
    7677        if (root == NULL) {
     
    7879        }
    7980        dump_tree_descriptor(root, depth);
    80         uint8_t *child = usb_dp_get_nested_descriptor(parser, data, root);
     81        const uint8_t *child = usb_dp_get_nested_descriptor(parser, data, root);
    8182        do {
    8283                dump_tree_internal(parser, data, child, depth + 1);
     
    9293static void dump_tree(usb_dp_parser_t *parser, usb_dp_parser_data_t *data)
    9394{
    94         uint8_t *ptr = data->data;
     95        const uint8_t *ptr = data->data;
    9596        dump_tree_internal(parser, data, ptr, 0);
    9697}
  • uspace/drv/bus/usb/usbmid/explore.c

    r3958e315 re3f7418  
    7373 * @param list List where to add the interfaces.
    7474 */
    75 static void create_interfaces(uint8_t *config_descriptor,
     75static void create_interfaces(const uint8_t *config_descriptor,
    7676    size_t config_descriptor_size, list_t *list)
    7777{
    78         usb_dp_parser_data_t data = {
     78        const usb_dp_parser_data_t data = {
    7979                .data = config_descriptor,
    8080                .size = config_descriptor_size,
     
    8686        };
    8787
    88         uint8_t *interface_ptr = usb_dp_get_nested_descriptor(&parser, &data,
    89             data.data);
     88        const uint8_t *interface_ptr =
     89            usb_dp_get_nested_descriptor(&parser, &data, data.data);
    9090        if (interface_ptr == NULL) {
    9191                return;
     
    149149
    150150        /* Short cuts to save on typing ;-). */
    151         uint8_t *config_descriptor_raw = dev->descriptors.configuration;
     151        const void *config_descriptor_raw = dev->descriptors.configuration;
    152152        size_t config_descriptor_size = dev->descriptors.configuration_size;
    153         usb_standard_configuration_descriptor_t *config_descriptor =
    154             (usb_standard_configuration_descriptor_t *) config_descriptor_raw;
     153        const usb_standard_configuration_descriptor_t *config_descriptor =
     154            config_descriptor_raw;
    155155
    156156        /* Select the first configuration */
     
    163163        }
    164164
     165        usb_mid_t *usb_mid = usb_device_data_alloc(dev, sizeof(usb_mid_t));
     166        if (!usb_mid) {
     167                usb_log_error("Failed to create USB MID structure.\n");
     168                return false;
     169        }
     170
    165171        /* Create control function */
    166         ddf_fun_t *ctl_fun = ddf_fun_create(dev->ddf_dev, fun_exposed, "ctl");
    167         if (ctl_fun == NULL) {
     172        usb_mid->ctl_fun = ddf_fun_create(dev->ddf_dev, fun_exposed, "ctl");
     173        if (usb_mid->ctl_fun == NULL) {
    168174                usb_log_error("Failed to create control function.\n");
    169175                return false;
    170176        }
    171177
    172         ctl_fun->ops = &mid_device_ops;
    173 
    174         rc = ddf_fun_bind(ctl_fun);
     178        usb_mid->ctl_fun->ops = &mid_device_ops;
     179
     180        rc = ddf_fun_bind(usb_mid->ctl_fun);
    175181        if (rc != EOK) {
    176182                usb_log_error("Failed to bind control function: %s.\n",
    177183                    str_error(rc));
    178                 return false;
    179         }
     184                ddf_fun_destroy(usb_mid->ctl_fun);
     185                return false;
     186        }
     187
    180188
    181189        /* Create interface children. */
    182         list_t interface_list;
    183         list_initialize(&interface_list);
     190        list_initialize(&usb_mid->interface_list);
    184191        create_interfaces(config_descriptor_raw, config_descriptor_size,
    185             &interface_list);
    186 
    187         list_foreach(interface_list, link) {
     192            &usb_mid->interface_list);
     193
     194        list_foreach(usb_mid->interface_list, link) {
    188195                usbmid_interface_t *iface = list_get_instance(link,
    189196                    usbmid_interface_t, link);
  • uspace/drv/bus/usb/usbmid/main.c

    r3958e315 re3f7418  
    6666}
    6767
     68static int usbmid_device_gone(usb_device_t *dev)
     69{
     70        assert(dev);
     71        usb_log_info("USB MID gone: `%s'.\n", dev->ddf_dev->name);
     72
     73        /* Remove ctl function */
     74        usb_mid_t *usb_mid = dev->driver_data;
     75        int ret = ddf_fun_unbind(usb_mid->ctl_fun);
     76        if (ret != EOK) {
     77                usb_log_error("Failed to unbind USB MID ctl function: %s.\n",
     78                    str_error(ret));
     79                return ret;
     80        }
     81        ddf_fun_destroy(usb_mid->ctl_fun);
     82
     83        /* Now remove all other functions */
     84        while (!list_empty(&usb_mid->interface_list)) {
     85                link_t *item = list_first(&usb_mid->interface_list);
     86                list_remove(item);
     87
     88                usbmid_interface_t *iface = list_get_instance(item,
     89                    usbmid_interface_t, link);
     90
     91                usb_log_info("Removing child for interface %d (%s).\n",
     92                    iface->interface_no,
     93                    usb_str_class(iface->interface->interface_class));
     94
     95                const int pret = usbmid_interface_destroy(iface);
     96                if (pret != EOK) {
     97                        usb_log_error("Failed to remove child for interface "
     98                            "%d (%s): %s\n",
     99                            iface->interface_no,
     100                            usb_str_class(iface->interface->interface_class),
     101                            str_error(pret));
     102                        ret = pret;
     103                }
     104        }
     105        return ret;
     106}
     107
    68108/** USB MID driver ops. */
    69109static usb_driver_ops_t mid_driver_ops = {
    70110        .device_add = usbmid_device_add,
     111        .device_gone = usbmid_device_gone,
    71112};
    72113
  • uspace/drv/bus/usb/usbmid/usbmid.c

    r3958e315 re3f7418  
    4545
    4646/** Callback for DDF USB interface. */
    47 static int usb_iface_get_address_impl(ddf_fun_t *fun, devman_handle_t handle,
    48     usb_address_t *address)
    49 {
    50         return usb_iface_get_address_hub_impl(fun, handle, address);
    51 }
    52 
    53 /** Callback for DDF USB interface. */
    5447static int usb_iface_get_interface_impl(ddf_fun_t *fun, devman_handle_t handle,
    5548    int *iface_no)
     
    7063static usb_iface_t child_usb_iface = {
    7164        .get_hc_handle = usb_iface_get_hc_handle_hub_child_impl,
    72         .get_address = usb_iface_get_address_impl,
     65        .get_address = usb_iface_get_address_hub_impl,
    7366        .get_interface = usb_iface_get_interface_impl
    7467};
     
    7972};
    8073
     74int usbmid_interface_destroy(usbmid_interface_t *mid_iface)
     75{
     76        assert(mid_iface);
     77        assert_link_not_used(&mid_iface->link);
     78        const int ret = ddf_fun_unbind(mid_iface->fun);
     79        if (ret != EOK) {
     80                return ret;
     81        }
     82        /* NOTE: usbmid->interface points somewhere, but we did not
     83         * allocate that space, so don't touch */
     84        ddf_fun_destroy(mid_iface->fun);
     85        /* NOTE: mid_iface is invalid at this point, it was assigned to
     86         * mid_iface->fun->driver_data and freed in ddf_fun_destroy */
     87        return EOK;
     88}
    8189
    8290/** Spawn new child device from one interface.
     
    106114            (int) interface_descriptor->interface_number);
    107115        if (rc < 0) {
    108                 goto error_leave;
     116                return ENOMEM;
    109117        }
    110118
    111119        /* Create the device. */
    112120        child = ddf_fun_create(parent->ddf_dev, fun_inner, child_name);
     121        free(child_name);
    113122        if (child == NULL) {
    114                 rc = ENOMEM;
    115                 goto error_leave;
     123                return ENOMEM;
    116124        }
    117125
     
    122130
    123131        rc = usb_device_create_match_ids_from_interface(device_descriptor,
    124             interface_descriptor,
    125             &child->match_ids);
     132            interface_descriptor, &child->match_ids);
    126133        if (rc != EOK) {
    127                 goto error_leave;
     134                ddf_fun_destroy(child);
     135                return rc;
    128136        }
    129137
    130138        rc = ddf_fun_bind(child);
    131139        if (rc != EOK) {
    132                 goto error_leave;
     140                /* This takes care of match_id deallocation as well. */
     141                ddf_fun_destroy(child);
     142                return rc;
    133143        }
    134144
    135145        return EOK;
    136 
    137 error_leave:
    138         if (child != NULL) {
    139                 child->name = NULL;
    140                 /* This takes care of match_id deallocation as well. */
    141                 ddf_fun_destroy(child);
    142         }
    143         if (child_name != NULL) {
    144                 free(child_name);
    145         }
    146 
    147         return rc;
    148146}
    149147
  • uspace/drv/bus/usb/usbmid/usbmid.h

    r3958e315 re3f7418  
    5858} usbmid_interface_t;
    5959
     60/** Container to hold all the function pointers */
     61typedef struct usb_mid {
     62        ddf_fun_t *ctl_fun;
     63        list_t interface_list;
     64} usb_mid_t;
     65
    6066bool usbmid_explore_device(usb_device_t *);
    6167int usbmid_spawn_interface_child(usb_device_t *, usbmid_interface_t *,
     
    6369    const usb_standard_interface_descriptor_t *);
    6470void usbmid_dump_descriptors(uint8_t *, size_t);
     71int usbmid_interface_destroy(usbmid_interface_t *mid_iface);
    6572
    6673#endif
  • uspace/lib/usbdev/include/usb/dev/dp.h

    r3958e315 re3f7418  
    5959typedef struct {
    6060        /** Used descriptor nesting. */
    61         usb_dp_descriptor_nesting_t *nesting;
     61        const usb_dp_descriptor_nesting_t *nesting;
    6262} usb_dp_parser_t;
    6363
     
    6565typedef struct {
    6666        /** Data to be parsed. */
    67         uint8_t *data;
     67        const uint8_t *data;
    6868        /** Size of input data in bytes. */
    6969        size_t size;
     
    7272} usb_dp_parser_data_t;
    7373
    74 uint8_t *usb_dp_get_nested_descriptor(usb_dp_parser_t *,
    75     usb_dp_parser_data_t *, uint8_t *);
    76 uint8_t *usb_dp_get_sibling_descriptor(usb_dp_parser_t *,
    77     usb_dp_parser_data_t *, uint8_t *, uint8_t *);
     74typedef void (*walk_callback_t)(const uint8_t *, size_t, void *);
    7875
    79 void usb_dp_walk_simple(uint8_t *, size_t, usb_dp_descriptor_nesting_t *,
    80     void (*)(uint8_t *, size_t, void *), void *);
     76const uint8_t *usb_dp_get_nested_descriptor(const usb_dp_parser_t *,
     77    const usb_dp_parser_data_t *, const uint8_t *);
     78const uint8_t *usb_dp_get_sibling_descriptor(const usb_dp_parser_t *,
     79    const usb_dp_parser_data_t *, const uint8_t *, const uint8_t *);
     80
     81void usb_dp_walk_simple(uint8_t *, size_t, const usb_dp_descriptor_nesting_t *,
     82    walk_callback_t, void *);
    8183
    8284#endif
  • uspace/lib/usbdev/include/usb/dev/driver.h

    r3958e315 re3f7418  
    4343        usb_standard_device_descriptor_t device;
    4444        /** Full configuration descriptor of current configuration. */
    45         uint8_t *configuration;
     45        const uint8_t *configuration;
    4646        size_t configuration_size;
    4747} usb_device_descriptors_t;
     
    5353typedef struct {
    5454        /** Interface descriptor. */
    55         usb_standard_interface_descriptor_t *interface;
     55        const usb_standard_interface_descriptor_t *interface;
    5656        /** Pointer to start of descriptor tree bound with this interface. */
    57         uint8_t *nested_descriptors;
     57        const uint8_t *nested_descriptors;
    5858        /** Size of data pointed by nested_descriptors in bytes. */
    5959        size_t nested_descriptors_size;
     
    158158        usb_endpoint_description_t **endpoints;
    159159        /** Driver ops. */
    160         usb_driver_ops_t *ops;
     160        const usb_driver_ops_t *ops;
    161161} usb_driver_t;
    162162
     
    168168int usb_device_retrieve_descriptors(usb_pipe_t *, usb_device_descriptors_t *);
    169169int usb_device_create_pipes(const ddf_dev_t *, usb_device_connection_t *,
    170     usb_endpoint_description_t **, uint8_t *, size_t, int, int,
     170    usb_endpoint_description_t **, const uint8_t *, size_t, int, int,
    171171    usb_endpoint_mapping_t **, size_t *);
    172172int usb_device_destroy_pipes(const ddf_dev_t *, usb_endpoint_mapping_t *, size_t);
    173173int usb_device_create(ddf_dev_t *, usb_endpoint_description_t **, usb_device_t **, const char **);
    174174void usb_device_destroy(usb_device_t *);
     175void * usb_device_data_alloc(usb_device_t *, size_t);
    175176
    176 size_t usb_interface_count_alternates(uint8_t *, size_t, uint8_t);
    177 int usb_alternate_interfaces_create(uint8_t *, size_t, int,
     177size_t usb_interface_count_alternates(const uint8_t *, size_t, uint8_t);
     178int usb_alternate_interfaces_create(const uint8_t *, size_t, int,
    178179    usb_alternate_interfaces_t **);
    179180
  • uspace/lib/usbdev/include/usb/dev/pipes.h

    r3958e315 re3f7418  
    171171int usb_pipe_probe_default_control(usb_pipe_t *);
    172172int usb_pipe_initialize_from_configuration(usb_endpoint_mapping_t *,
    173     size_t, uint8_t *, size_t, usb_device_connection_t *);
     173    size_t, const uint8_t *, size_t, usb_device_connection_t *);
    174174int usb_pipe_register_with_speed(usb_pipe_t *, usb_speed_t,
    175175    unsigned int, usb_hc_connection_t *);
  • uspace/lib/usbdev/src/altiface.c

    r3958e315 re3f7418  
    4848 * @return Number of alternate interfaces for @p interface_no interface.
    4949 */
    50 size_t usb_interface_count_alternates(uint8_t *config_descr,
     50size_t usb_interface_count_alternates(const uint8_t *config_descr,
    5151    size_t config_descr_size, uint8_t interface_no)
    5252{
     
    5454        assert(config_descr_size > 0);
    5555
    56         usb_dp_parser_t dp_parser = {
     56        const usb_dp_parser_t dp_parser = {
    5757                .nesting = usb_dp_standard_descriptor_nesting
    5858        };
    59         usb_dp_parser_data_t dp_data = {
     59        const usb_dp_parser_data_t dp_data = {
    6060                .data = config_descr,
    6161                .size = config_descr_size,
     
    6565        size_t alternate_count = 0;
    6666
    67         uint8_t *iface_ptr = usb_dp_get_nested_descriptor(&dp_parser,
     67        const uint8_t *iface_ptr = usb_dp_get_nested_descriptor(&dp_parser,
    6868            &dp_data, config_descr);
    6969        while (iface_ptr != NULL) {
     
    9090 * @return Error code.
    9191 */
    92 int usb_alternate_interfaces_create(uint8_t *config_descr,
     92int usb_alternate_interfaces_create(const uint8_t *config_descr,
    9393    size_t config_descr_size, int interface_number,
    9494    usb_alternate_interfaces_t **alternates_ptr)
     
    140140            = &alternates->alternatives[0];
    141141
    142         uint8_t *iface_ptr = usb_dp_get_nested_descriptor(&dp_parser,
     142        const uint8_t *iface_ptr = usb_dp_get_nested_descriptor(&dp_parser,
    143143            &dp_data, dp_data.data);
    144144        while (iface_ptr != NULL) {
     
    160160                    dp_data.data, iface_ptr);
    161161                if (iface_ptr == NULL) {
    162                         uint8_t *next = dp_data.data + dp_data.size;
     162                        const uint8_t *next = dp_data.data + dp_data.size;
    163163                        cur_alt_iface->nested_descriptors_size
    164164                            = next - cur_alt_iface->nested_descriptors;
  • uspace/lib/usbdev/src/devdrv.c

    r3958e315 re3f7418  
    5454};
    5555
    56 static usb_driver_t *driver = NULL;
     56static const usb_driver_t *driver = NULL;
    5757
    5858
     
    115115        int rc = usb_device_create_pipes(dev->ddf_dev, &dev->wire, endpoints,
    116116            dev->descriptors.configuration, dev->descriptors.configuration_size,
    117             dev->interface_no, alternate_setting,
    118             &pipes, &pipes_count);
     117            dev->interface_no, alternate_setting, &pipes, &pipes_count);
    119118
    120119        if (rc != EOK) {
     
    153152        gen_dev->driver_data = dev;
    154153
    155         return driver->ops->device_add(dev);
     154        rc = driver->ops->device_add(dev);
     155        if (rc != EOK)
     156                usb_device_destroy(dev);
     157        return rc;
    156158}
    157159/*----------------------------------------------------------------------------*/
     
    186188        if (driver->ops->device_gone == NULL)
    187189                return ENOTSUP;
    188         const int ret = driver->ops->device_gone(gen_dev->driver_data);
     190        usb_device_t *usb_dev = gen_dev->driver_data;
     191        const int ret = driver->ops->device_gone(usb_dev);
    189192        if (ret == EOK)
    190                 usb_device_destroy(gen_dev->driver_data);
     193                usb_device_destroy(usb_dev);
    191194
    192195        return ret;
     
    319322int usb_device_create_pipes(const ddf_dev_t *dev, usb_device_connection_t *wire,
    320323    usb_endpoint_description_t **endpoints,
    321     uint8_t *config_descr, size_t config_descr_size,
     324    const uint8_t *config_descr, size_t config_descr_size,
    322325    int interface_no, int interface_setting,
    323326    usb_endpoint_mapping_t **pipes_ptr, size_t *pipes_count_ptr)
     
    333336        int rc;
    334337
    335         size_t pipe_count = count_other_pipes(endpoints);
     338        const size_t pipe_count = count_other_pipes(endpoints);
    336339        if (pipe_count == 0) {
     340                *pipes_count_ptr = pipe_count;
    337341                *pipes_ptr = NULL;
    338342                return EOK;
     
    445449{
    446450        assert(dev != NULL);
    447         assert(((pipes != NULL) && (pipes_count > 0))
    448             || ((pipes == NULL) && (pipes_count == 0)));
    449451
    450452        if (pipes_count == 0) {
     453                assert(pipes == NULL);
    451454                return EOK;
    452455        }
     456        assert(pipes != NULL);
    453457
    454458        int rc;
     
    468472        size_t i;
    469473        for (i = 0; i < pipes_count; i++) {
    470                 usb_pipe_unregister(pipes[i].pipe, &hc_conn);
     474                usb_log_debug2("Unregistering pipe %zu (%spresent).\n",
     475                    i, pipes[i].present ? "" : "not ");
     476                if (pipes[i].present)
     477                        usb_pipe_unregister(pipes[i].pipe, &hc_conn);
    471478                free(pipes[i].pipe);
    472479        }
     
    592599
    593600        /* Ignore errors and hope for the best. */
    594         usb_device_destroy_pipes(dev->ddf_dev, dev->pipes, dev->pipes_count);
    595         free(dev->descriptors.configuration);
     601        destroy_current_pipes(dev);
    596602
    597603        if (dev->alternate_interfaces != NULL) {
     
    599605        }
    600606        free(dev->alternate_interfaces);
    601 
    602         free(dev);
     607        free(dev->descriptors.configuration);
     608        free(dev->driver_data);
     609}
     610
     611void * usb_device_data_alloc(usb_device_t *usb_dev, size_t size)
     612{
     613        assert(usb_dev);
     614        assert(usb_dev->driver_data == NULL);
     615        return usb_dev->driver_data = calloc(1, size);
     616
    603617}
    604618
  • uspace/lib/usbdev/src/dp.c

    r3958e315 re3f7418  
    7575 * @return Whether @p ptr points inside <code>data->data</code> field.
    7676 */
    77 static bool is_valid_descriptor_pointer(usb_dp_parser_data_t *data,
    78     uint8_t *ptr)
     77static bool is_valid_descriptor_pointer(const usb_dp_parser_data_t *data,
     78    const uint8_t *ptr)
    7979{
    8080        if (ptr == NULL) {
     
    100100 * @retval NULL Invalid input or no next descriptor.
    101101 */
    102 static uint8_t *get_next_descriptor(usb_dp_parser_data_t *data,
    103     uint8_t *current)
     102static const uint8_t *get_next_descriptor(const usb_dp_parser_data_t *data,
     103    const uint8_t *current)
    104104{
    105105        assert(is_valid_descriptor_pointer(data, current));
    106106
    107         uint8_t current_length = *current;
    108         uint8_t *next = current + current_length;
     107        const uint8_t current_length = *current;
     108        const uint8_t *next = current + current_length;
    109109
    110110        if (!is_valid_descriptor_pointer(data, next)) {
     
    124124 * @retval -1 Invalid input.
    125125 */
    126 static int get_descriptor_type(usb_dp_parser_data_t *data, uint8_t *start)
     126static int get_descriptor_type(const usb_dp_parser_data_t *data, const uint8_t *start)
    127127{
    128128        if (start == NULL) {
     
    145145 * @return Whether @p child could be child of @p parent.
    146146 */
    147 static bool is_nested_descriptor_type(usb_dp_parser_t *parser,
     147static bool is_nested_descriptor_type(const usb_dp_parser_t *parser,
    148148    int child, int parent)
    149149{
    150         usb_dp_descriptor_nesting_t *nesting = parser->nesting;
     150        const usb_dp_descriptor_nesting_t *nesting = parser->nesting;
    151151        while ((nesting->child > 0) && (nesting->parent > 0)) {
    152152                if ((nesting->child == child) && (nesting->parent == parent)) {
     
    166166 * @return Whether @p child could be child of @p parent.
    167167 */
    168 static bool is_nested_descriptor(usb_dp_parser_t *parser,
    169     usb_dp_parser_data_t *data, uint8_t *child, uint8_t *parent)
     168static bool is_nested_descriptor(const usb_dp_parser_t *parser,
     169    const usb_dp_parser_data_t *data, const uint8_t *child, const uint8_t *parent)
    170170{
    171171        return is_nested_descriptor_type(parser,
     
    183183 * @retval NULL Invalid input.
    184184 */
    185 uint8_t *usb_dp_get_nested_descriptor(usb_dp_parser_t *parser,
    186     usb_dp_parser_data_t *data, uint8_t *parent)
     185const uint8_t *usb_dp_get_nested_descriptor(const usb_dp_parser_t *parser,
     186    const usb_dp_parser_data_t *data, const uint8_t *parent)
    187187{
    188188        if (!is_valid_descriptor_pointer(data, parent)) {
     
    190190        }
    191191
    192         uint8_t *next = get_next_descriptor(data, parent);
     192        const uint8_t *next = get_next_descriptor(data, parent);
    193193        if (next == NULL) {
    194194                return NULL;
     
    211211 * @retval NULL Invalid input.
    212212 */
    213 static uint8_t *skip_nested_descriptors(usb_dp_parser_t *parser,
    214     usb_dp_parser_data_t *data, uint8_t *parent)
    215 {
    216         uint8_t *child = usb_dp_get_nested_descriptor(parser, data, parent);
     213static const uint8_t *skip_nested_descriptors(const usb_dp_parser_t *parser,
     214    const usb_dp_parser_data_t *data, const uint8_t *parent)
     215{
     216        const uint8_t *child =
     217            usb_dp_get_nested_descriptor(parser, data, parent);
    217218        if (child == NULL) {
    218219                return get_next_descriptor(data, parent);
    219220        }
    220         uint8_t *next_child = skip_nested_descriptors(parser, data, child);
     221        const uint8_t *next_child =
     222            skip_nested_descriptors(parser, data, child);
    221223        while (is_nested_descriptor(parser, data, next_child, parent)) {
    222224                next_child = skip_nested_descriptors(parser, data, next_child);
     
    236238 * @retval NULL Invalid input.
    237239 */
    238 uint8_t *usb_dp_get_sibling_descriptor(usb_dp_parser_t *parser,
    239     usb_dp_parser_data_t *data, uint8_t *parent, uint8_t *sibling)
     240const uint8_t *usb_dp_get_sibling_descriptor(
     241    const usb_dp_parser_t *parser, const usb_dp_parser_data_t *data,
     242    const uint8_t *parent, const uint8_t *sibling)
    240243{
    241244        if (!is_valid_descriptor_pointer(data, parent)
     
    244247        }
    245248
    246         uint8_t *possible_sibling = skip_nested_descriptors(parser, data, sibling);
     249        const uint8_t *possible_sibling =
     250            skip_nested_descriptors(parser, data, sibling);
    247251        if (possible_sibling == NULL) {
    248252                return NULL;
     
    269273 * @param arg Custom (user) argument.
    270274 */
    271 static void usb_dp_browse_simple_internal(usb_dp_parser_t *parser,
    272     usb_dp_parser_data_t *data, uint8_t *root, size_t depth,
    273     void (*callback)(uint8_t *, size_t, void *), void *arg)
     275static void usb_dp_browse_simple_internal(const usb_dp_parser_t *parser,
     276    const usb_dp_parser_data_t *data, const uint8_t *root, size_t depth,
     277    void (*callback)(const uint8_t *, size_t, void *), void *arg)
    274278{
    275279        if (root == NULL) {
     
    277281        }
    278282        callback(root, depth, arg);
    279         uint8_t *child = usb_dp_get_nested_descriptor(parser, data, root);
     283        const uint8_t *child = usb_dp_get_nested_descriptor(parser, data, root);
    280284        do {
    281285                usb_dp_browse_simple_internal(parser, data, child, depth + 1,
     
    301305 */
    302306void usb_dp_walk_simple(uint8_t *descriptors, size_t descriptors_size,
    303     usb_dp_descriptor_nesting_t *descriptor_nesting,
    304     void (*callback)(uint8_t *, size_t, void *), void *arg)
     307    const usb_dp_descriptor_nesting_t *descriptor_nesting,
     308    walk_callback_t callback, void *arg)
    305309{
    306310        if ((descriptors == NULL) || (descriptors_size == 0)
     
    309313        }
    310314
    311         usb_dp_parser_data_t data = {
     315        const usb_dp_parser_data_t data = {
    312316                .data = descriptors,
    313317                .size = descriptors_size,
     
    315319        };
    316320
    317         usb_dp_parser_t parser = {
     321        const usb_dp_parser_t parser = {
    318322                .nesting = descriptor_nesting
    319323        };
  • uspace/lib/usbdev/src/pipesinit.c

    r3958e315 re3f7418  
    6868 * @return Whether the given descriptor is endpoint descriptor.
    6969 */
    70 static inline bool is_endpoint_descriptor(uint8_t *descriptor)
     70static inline bool is_endpoint_descriptor(const uint8_t *descriptor)
    7171{
    7272        return descriptor[1] == USB_DESCTYPE_ENDPOINT;
     
    8080 */
    8181static bool endpoint_fits_description(const usb_endpoint_description_t *wanted,
    82     usb_endpoint_description_t *found)
     82    const usb_endpoint_description_t *found)
    8383{
    8484#define _SAME(fieldname) ((wanted->fieldname) == (found->fieldname))
     
    120120static usb_endpoint_mapping_t *find_endpoint_mapping(
    121121    usb_endpoint_mapping_t *mapping, size_t mapping_count,
    122     usb_endpoint_description_t *found_endpoint,
     122    const usb_endpoint_description_t *found_endpoint,
    123123    int interface_number, int interface_setting)
    124124{
     
    160160    usb_device_connection_t *wire)
    161161{
    162         usb_endpoint_description_t description;
    163162
    164163        /*
     
    167166
    168167        /* Actual endpoint number is in bits 0..3 */
    169         usb_endpoint_t ep_no = endpoint->endpoint_address & 0x0F;
    170 
    171         /* Endpoint direction is set by bit 7 */
    172         description.direction = (endpoint->endpoint_address & 128)
    173             ? USB_DIRECTION_IN : USB_DIRECTION_OUT;
    174         /* Transfer type is in bits 0..2 and the enum values corresponds 1:1 */
    175         description.transfer_type = endpoint->attributes & 3;
    176 
    177         /*
    178          * Get interface characteristics.
    179          */
    180         description.interface_class = interface->interface_class;
    181         description.interface_subclass = interface->interface_subclass;
    182         description.interface_protocol = interface->interface_protocol;
     168        const usb_endpoint_t ep_no = endpoint->endpoint_address & 0x0F;
     169
     170        const usb_endpoint_description_t description = {
     171                /* Endpoint direction is set by bit 7 */
     172                .direction = (endpoint->endpoint_address & 128)
     173                    ? USB_DIRECTION_IN : USB_DIRECTION_OUT,
     174                /* Transfer type is in bits 0..2 and
     175                 * the enum values corresponds 1:1 */
     176                .transfer_type = endpoint->attributes & 3,
     177
     178                /* Get interface characteristics. */
     179                .interface_class = interface->interface_class,
     180                .interface_subclass = interface->interface_subclass,
     181                .interface_protocol = interface->interface_protocol,
     182        };
    183183
    184184        /*
     
    224224static int process_interface(
    225225    usb_endpoint_mapping_t *mapping, size_t mapping_count,
    226     usb_dp_parser_t *parser, usb_dp_parser_data_t *parser_data,
    227     uint8_t *interface_descriptor)
    228 {
    229         uint8_t *descriptor = usb_dp_get_nested_descriptor(parser,
     226    const usb_dp_parser_t *parser, const usb_dp_parser_data_t *parser_data,
     227    const uint8_t *interface_descriptor)
     228{
     229        const uint8_t *descriptor = usb_dp_get_nested_descriptor(parser,
    230230            parser_data, interface_descriptor);
    231231
     
    284284int usb_pipe_initialize_from_configuration(
    285285    usb_endpoint_mapping_t *mapping, size_t mapping_count,
    286     uint8_t *configuration_descriptor, size_t configuration_descriptor_size,
     286    const uint8_t *config_descriptor, size_t config_descriptor_size,
    287287    usb_device_connection_t *connection)
    288288{
    289289        assert(connection);
    290290
    291         if (configuration_descriptor == NULL) {
     291        if (config_descriptor == NULL) {
    292292                return EBADMEM;
    293293        }
    294         if (configuration_descriptor_size
     294        if (config_descriptor_size
    295295            < sizeof(usb_standard_configuration_descriptor_t)) {
    296296                return ERANGE;
     
    310310         * Prepare the descriptor parser.
    311311         */
    312         usb_dp_parser_t dp_parser = {
     312        const usb_dp_parser_t dp_parser = {
    313313                .nesting = descriptor_nesting
    314314        };
    315         usb_dp_parser_data_t dp_data = {
    316                 .data = configuration_descriptor,
    317                 .size = configuration_descriptor_size,
     315        const usb_dp_parser_data_t dp_data = {
     316                .data = config_descriptor,
     317                .size = config_descriptor_size,
    318318                .arg = connection
    319319        };
     
    322322         * Iterate through all interfaces.
    323323         */
    324         uint8_t *interface = usb_dp_get_nested_descriptor(&dp_parser,
    325             &dp_data, configuration_descriptor);
     324        const uint8_t *interface = usb_dp_get_nested_descriptor(&dp_parser,
     325            &dp_data, config_descriptor);
    326326        if (interface == NULL) {
    327327                return ENOENT;
     
    329329        do {
    330330                (void) process_interface(mapping, mapping_count,
    331                     &dp_parser, &dp_data,
    332                     interface);
     331                    &dp_parser, &dp_data, interface);
    333332                interface = usb_dp_get_sibling_descriptor(&dp_parser, &dp_data,
    334                     configuration_descriptor, interface);
     333                    config_descriptor, interface);
    335334        } while (interface != NULL);
    336335
     
    514513{
    515514        assert(pipe);
     515        assert(pipe->wire);
    516516        assert(hc_connection);
    517517       
  • uspace/lib/usbdev/src/request.c

    r3958e315 re3f7418  
    425425
    426426        /* Everything is okay, copy the descriptor. */
    427         memcpy(descriptor, &descriptor_tmp,
    428             sizeof(descriptor_tmp));
     427        memcpy(descriptor, &descriptor_tmp, sizeof(descriptor_tmp));
    429428
    430429        return EOK;
     
    470469
    471470        /* Everything is okay, copy the descriptor. */
    472         memcpy(descriptor, &descriptor_tmp,
    473             sizeof(descriptor_tmp));
     471        memcpy(descriptor, &descriptor_tmp, sizeof(descriptor_tmp));
    474472
    475473        return EOK;
  • uspace/lib/usbhid/src/hidreport.c

    r3958e315 re3f7418  
    6969         * First nested descriptor of the configuration descriptor.
    7070         */
    71         uint8_t *d =
     71        const uint8_t *d =
    7272            usb_dp_get_nested_descriptor(&parser, &parser_data,
    7373            dev->descriptors.configuration);
     
    9292         * First nested descriptor of the interface descriptor.
    9393         */
    94         uint8_t *iface_desc = d;
     94        const uint8_t *iface_desc = d;
    9595        d = usb_dp_get_nested_descriptor(&parser, &parser_data, iface_desc);
    9696       
Note: See TracChangeset for help on using the changeset viewer.