Changeset 41924f30 in mainline


Ignore:
Timestamp:
2017-10-12T14:07:27Z (7 years ago)
Author:
Ondřej Hlavatý <aearsis@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
a5976973
Parents:
7e74911
Message:

WIP usbhost refactoring

This commit replaces callbacks with more systematic virtual-like inheritance-like solution. Currently breaks build of HelenOS, but both xhci and usbhost are buildable. More refactoring follows…

Location:
uspace
Files:
6 added
2 deleted
17 edited

Legend:

Unmodified
Added
Removed
  • uspace/drv/bus/usb/usbhub/status.h

    r7e74911 r41924f30  
    112112        if ((status & USB_HUB_PORT_STATUS_HIGH_SPEED) != 0)
    113113                return USB_SPEED_HIGH;
     114        /* TODO: add super speed */
    114115        return USB_SPEED_FULL;
    115116}
  • uspace/drv/bus/usb/xhci/Makefile

    r7e74911 r41924f30  
    4545
    4646SOURCES = \
     47        bus.c \
     48        commands.c \
     49        debug.c \
     50        endpoint.c \
    4751        hc.c \
    48         endpoint.c \
    49         debug.c \
    50         trb_ring.c \
     52        main.c \
     53        rh.c \
    5154        scratchpad.c \
    52         commands.c \
    5355        transfers.c \
    54         rh.c \
    55         main.c
     56        trb_ring.c
    5657
    5758TEST_SOURCES = \
  • uspace/drv/bus/usb/xhci/endpoint.c

    r7e74911 r41924f30  
    3434 */
    3535
     36#include <usb/host/endpoint.h>
     37
    3638#include <errno.h>
    3739
     40#include "bus.h"
    3841#include "endpoint.h"
    3942
    40 int endpoint_init(hcd_t *hcd, endpoint_t *ep)
     43int xhci_endpoint_init(xhci_endpoint_t *xhci_ep, xhci_bus_t *xhci_bus)
    4144{
    42         assert(ep);
    43         xhci_endpoint_t *xhci_ep = malloc(sizeof(xhci_endpoint_t));
    44         if (xhci_ep == NULL)
    45                 return ENOMEM;
     45        assert(xhci_ep);
     46        assert(xhci_bus);
     47
     48        bus_t *bus = &xhci_bus->base;
     49        endpoint_t *ep = &xhci_ep->base;
     50
     51        endpoint_init(ep, bus);
    4652
    4753        /* FIXME: Set xhci_ep->slot_id */
    4854
    49         fibril_mutex_lock(&ep->guard);
    50         ep->hc_data.data = xhci_ep;
    51         /* FIXME: The two handlers below should be implemented. */
    52         ep->hc_data.toggle_get = NULL;
    53         ep->hc_data.toggle_set = NULL;
    54         fibril_mutex_unlock(&ep->guard);
    55 
    56         usb_log_debug("Endpoint %d:%d initialized.", ep->address, ep->endpoint);
     55        usb_log_debug("XHCI Endpoint %d:%d initialized.", ep->target.address, ep->target.endpoint);
    5756
    5857        return EOK;
    5958}
    6059
    61 void endpoint_fini(hcd_t *hcd, endpoint_t *ep)
     60void xhci_endpoint_fini(xhci_endpoint_t *xhci_ep)
    6261{
    63         assert(hcd);
    64         assert(ep);
    65         xhci_endpoint_t *xhci_ep = endpoint_get(ep);
     62        assert(xhci_ep);
     63
    6664        /* FIXME: Tear down TR's? */
    67         if (xhci_ep) {
    68                 free(xhci_ep);
    69         }
    7065
    71         fibril_mutex_lock(&ep->guard);
    72         ep->hc_data.data = NULL;
    73         ep->hc_data.toggle_get = NULL;
    74         ep->hc_data.toggle_set = NULL;
    75         fibril_mutex_unlock(&ep->guard);
     66        endpoint_t *ep = &xhci_ep->base;
    7667
    77         usb_log_debug("Endpoint %d:%d destroyed.", ep->address, ep->endpoint);
     68        usb_log_debug("XHCI Endpoint %d:%d destroyed.", ep->target.address, ep->target.endpoint);
    7869}
    7970
  • uspace/drv/bus/usb/xhci/endpoint.h

    r7e74911 r41924f30  
    4343#include <usb/host/hcd.h>
    4444
     45typedef struct xhci_endpoint xhci_endpoint_t;
     46typedef struct xhci_bus xhci_bus_t;
     47
    4548enum {
    4649        EP_TYPE_INVALID = 0,
     
    5659/** Connector structure linking endpoint context to the endpoint. */
    5760typedef struct xhci_endpoint {
     61        endpoint_t base;        /**< Inheritance. Keep this first. */
     62
    5863        uint32_t slot_id;
    5964} xhci_endpoint_t;
    6065
    61 int endpoint_init(hcd_t *hcd, endpoint_t *ep);
    62 void endpoint_fini(hcd_t *hcd, endpoint_t *ep);
     66int xhci_endpoint_init(xhci_endpoint_t *, xhci_bus_t *);
     67void xhci_endpoint_fini(xhci_endpoint_t *);
    6368
    64 static inline xhci_endpoint_t * endpoint_get(const endpoint_t *ep)
     69static inline xhci_endpoint_t * xhci_endpoint_get(endpoint_t *ep)
    6570{
    6671        assert(ep);
    67         return ep->hc_data.data;
     72        return (xhci_endpoint_t *) ep;
    6873}
    6974
  • uspace/drv/bus/usb/xhci/hc.c

    r7e74911 r41924f30  
    447447
    448448        /* Check for root hub communication */
    449         if (batch->ep->address == xhci_rh_get_address(&hc->rh)) {
     449        if (batch->ep->target.address == xhci_rh_get_address(&hc->rh)) {
    450450                usb_log_debug("XHCI root hub request.\n");
    451451                return xhci_rh_schedule(&hc->rh, batch);
     
    453453
    454454        usb_log_debug2("EP(%d:%d) started %s transfer of size %lu.",
    455                 batch->ep->address, batch->ep->endpoint,
     455                batch->ep->target.address, batch->ep->target.endpoint,
    456456                usb_str_transfer_type(batch->ep->transfer_type),
    457457                batch->buffer_size);
    458458
    459         if (!batch->ep->address) {
     459        if (!batch->ep->target.address) {
    460460                usb_log_error("Attempted to schedule transfer to address 0.");
    461461                return EINVAL;
  • uspace/drv/bus/usb/xhci/hc.h

    r7e74911 r41924f30  
    4343#include "trb_ring.h"
    4444#include "rh.h"
     45#include "bus.h"
    4546
    4647typedef struct xhci_virt_device_ctx {
     
    7273        xhci_rh_t rh;
    7374
     75        /* Bus bookkeeping */
     76        xhci_bus_t bus;
     77
    7478        /* Cached capabilities */
    7579        unsigned max_slots;
  • uspace/drv/bus/usb/xhci/main.c

    r7e74911 r41924f30  
    6666                .schedule       = hcd_schedule,
    6767                .irq_hook       = hcd_interrupt,
    68                 .ep_add_hook    = endpoint_init,
    69                 .ep_remove_hook = endpoint_fini,
    7068                .status_hook    = hcd_status,
    7169        }
     
    8381                goto err;
    8482
     83        if ((err = xhci_bus_init(&hc->bus, hcd)))
     84                goto err;
     85
    8586        if ((err = hc_init_memory(hc)))
    8687                goto err;
    8788
    88         hcd_set_implementation(hcd, hc, &xhci_ddf_hc_driver.ops);
     89        hcd_set_implementation(hcd, hc, &xhci_ddf_hc_driver.ops, &hc->bus.base);
    8990
    9091        return EOK;
  • uspace/drv/bus/usb/xhci/rh.c

    r7e74911 r41924f30  
    266266        assert(rh);
    267267        assert(batch);
    268         const usb_target_t target = {{
    269                 .address = batch->ep->address,
    270                 .endpoint = batch->ep->endpoint,
    271         }};
     268        const usb_target_t target = batch->ep->target;
    272269        batch->error = virthub_base_request(&rh->base, target,
    273270            usb_transfer_batch_direction(batch), (void*)batch->setup_buffer,
     
    294291        rh->unfinished_interrupt_transfer = NULL;
    295292        if (batch) {
    296                 const usb_target_t target = {{
    297                         .address = batch->ep->address,
    298                         .endpoint = batch->ep->endpoint,
    299                 }};
     293                const usb_target_t target = batch->ep->target;
    300294                batch->error = virthub_base_request(&rh->base, target,
    301295                    usb_transfer_batch_direction(batch),
  • uspace/drv/bus/usb/xhci/transfers.c

    r7e74911 r41924f30  
    3636#include <usb/host/utils/malloc32.h>
    3737#include <usb/debug.h>
     38#include "endpoint.h"
    3839#include "hc.h"
    3940#include "hw_struct/trb.h"
     
    141142                return EINVAL;
    142143        }
    143         if (batch->ep->endpoint != 0 || batch->ep->transfer_type != USB_TRANSFER_CONTROL) {
     144        if (batch->ep->target.endpoint != 0 || batch->ep->transfer_type != USB_TRANSFER_CONTROL) {
    144145                /* This method only works for control transfers. */
    145146                usb_log_error("Attempted to schedule control transfer to non 0 endpoint.");
     
    147148        }
    148149
    149         uint8_t slot_id = batch->ep->hc_data.slot_id;
     150        xhci_endpoint_t *xhci_ep = xhci_endpoint_get(batch->ep);
     151
     152        uint8_t slot_id = xhci_ep->slot_id;
    150153        xhci_trb_ring_t* ring = hc->dcbaa_virt[slot_id].trs[0];
    151154
     
    230233        }
    231234
    232         uint8_t slot_id = batch->ep->hc_data.slot_id;
    233         xhci_trb_ring_t* ring = hc->dcbaa_virt[slot_id].trs[batch->ep->endpoint];
     235        xhci_endpoint_t *xhci_ep = xhci_endpoint_get(batch->ep);
     236        uint8_t slot_id = xhci_ep->slot_id;
     237        xhci_trb_ring_t* ring = hc->dcbaa_virt[slot_id].trs[batch->ep->target.endpoint];
    234238
    235239        xhci_transfer_t *transfer = xhci_transfer_alloc(batch);
  • uspace/lib/usbhost/Makefile

    r7e74911 r41924f30  
    3939        src/endpoint.c \
    4040        src/hcd.c \
    41         src/usb_bus.c \
     41        src/bus.c \
     42        src/usb2_bus.c \
     43        src/bandwidth.c \
    4244        src/usb_transfer_batch.c
    4345
  • uspace/lib/usbhost/include/usb/host/ddf_helpers.h

    r7e74911 r41924f30  
    3838
    3939#include <usb/host/hcd.h>
    40 #include <usb/host/usb_bus.h>
     40#include <usb/host/bus.h>
    4141#include <usb/usb.h>
    4242
     
    7676int hcd_ddf_add_hc(ddf_dev_t *device, const ddf_hc_driver_t *driver);
    7777
    78 int hcd_ddf_setup_hc(ddf_dev_t *device, usb_speed_t max_speed,
    79     size_t bw, bw_count_func_t bw_count);
     78int hcd_ddf_setup_hc(ddf_dev_t *device);
    8079void hcd_ddf_clean_hc(ddf_dev_t *device);
    8180int hcd_ddf_setup_root_hub(ddf_dev_t *device);
  • uspace/lib/usbhost/include/usb/host/endpoint.h

    r7e74911 r41924f30  
    3232/** @file
    3333 *
     34 * Endpoint structure is tightly coupled to the bus. The bus controls the
     35 * life-cycle of endpoint. In order to keep endpoints lightweight, operations
     36 * on endpoints are part of the bus structure.
     37 *
    3438 */
    3539#ifndef LIBUSBHOST_HOST_ENDPOINT_H
     
    4246#include <atomic.h>
    4347
     48typedef struct bus bus_t;
     49
    4450/** Host controller side endpoint structure. */
    4551typedef struct endpoint {
     52        /** Managing bus */
     53        bus_t *bus;
    4654        /** Reference count. */
    4755        atomic_t refcnt;
     
    4957        link_t link;
    5058        /** USB address. */
    51         usb_address_t address;
    52         /** USB endpoint number. */
    53         usb_endpoint_t endpoint;
     59        usb_target_t target;
    5460        /** Communication direction. */
    5561        usb_direction_t direction;
     
    6268        /** Additional opportunities per uframe */
    6369        unsigned packets;
    64         /** Necessary bandwidth. */
     70        /** Reserved bandwidth. */
    6571        size_t bandwidth;
    6672        /** Value of the toggle bit. */
    6773        unsigned toggle:1;
    6874        /** True if there is a batch using this scheduled for this endpoint. */
    69         volatile bool active;
     75        bool active;
    7076        /** Protects resources and active status changes. */
    7177        fibril_mutex_t guard;
     
    7783                unsigned port;
    7884        } tt;
    79         /** Optional device specific data. */
    80         struct {
    81                 /** Device specific data. */
    82                 void *data;
    83                 /** Callback to get the value of toggle bit. */
    84                 int (*toggle_get)(void *);
    85                 /** Callback to set the value of toggle bit. */
    86                 void (*toggle_set)(void *, int);
    87                 /** Device slot id. */
    88                 uint8_t slot_id;
    89         } hc_data;
     85
     86        /* This structure is meant to be extended by overriding. */
    9087} endpoint_t;
    9188
    92 extern endpoint_t *endpoint_create(usb_address_t, usb_endpoint_t,
    93     usb_direction_t, usb_transfer_type_t, usb_speed_t, size_t, unsigned int,
    94     size_t, usb_address_t, unsigned int);
    95 extern void endpoint_destroy(endpoint_t *);
     89extern void endpoint_init(endpoint_t *, bus_t *);
    9690
    9791extern void endpoint_add_ref(endpoint_t *);
    9892extern void endpoint_del_ref(endpoint_t *);
    99 
    100 extern void endpoint_set_hc_data(endpoint_t *, void *, int (*)(void *),
    101     void (*)(void *, int));
    102 extern void endpoint_clear_hc_data(endpoint_t *);
    10393
    10494extern void endpoint_use(endpoint_t *);
     
    10696
    10797extern int endpoint_toggle_get(endpoint_t *);
    108 extern void endpoint_toggle_set(endpoint_t *, int);
     98extern void endpoint_toggle_set(endpoint_t *, unsigned);
    10999
    110100/** list_get_instance wrapper.
  • uspace/lib/usbhost/include/usb/host/hcd.h

    r7e74911 r41924f30  
    3838
    3939#include <usb/host/endpoint.h>
    40 #include <usb/host/usb_bus.h>
     40#include <usb/host/bus.h>
    4141#include <usb/host/usb_transfer_batch.h>
    4242#include <usb/usb.h>
     
    7171struct hcd {
    7272        /** Endpoint manager. */
    73         usb_bus_t bus;
     73        bus_t *bus;
    7474
    7575        /** Interrupt replacement fibril */
     
    7878        /** Driver implementation */
    7979        hcd_ops_t ops;
     80
    8081        /** Device specific driver data. */
    8182        void * driver_data;
    8283};
    8384
    84 extern void hcd_init(hcd_t *, usb_speed_t, size_t, bw_count_func_t);
     85extern void hcd_init(hcd_t *);
    8586
    8687static inline void hcd_set_implementation(hcd_t *hcd, void *data,
    87     const hcd_ops_t *ops)
     88    const hcd_ops_t *ops, bus_t *bus)
    8889{
    8990        assert(hcd);
     
    9192                hcd->driver_data = data;
    9293                hcd->ops = *ops;
     94                hcd->bus = bus;
    9395        } else {
    9496                memset(&hcd->ops, 0, sizeof(hcd->ops));
  • uspace/lib/usbhost/include/usb/host/usb_transfer_batch.h

    r7e74911 r41924f30  
    9090 */
    9191#define USB_TRANSFER_BATCH_ARGS(batch) \
    92         (batch).ep->address, (batch).ep->endpoint, \
     92        (batch).ep->target.address, (batch).ep->target.endpoint, \
    9393        usb_str_speed((batch).ep->speed), \
    9494        usb_str_transfer_type_short((batch).ep->transfer_type), \
  • uspace/lib/usbhost/src/ddf_helpers.c

    r7e74911 r41924f30  
    469469
    470470        /* This checks whether the default address is reserved and gets speed */
    471         int ret = usb_bus_get_speed(&hcd->bus, USB_ADDRESS_DEFAULT, &speed);
     471        int ret = bus_get_speed(hcd->bus, USB_ADDRESS_DEFAULT, &speed);
    472472        if (ret != EOK) {
    473473                usb_log_error("Failed to verify speed: %s.", str_error(ret));
     
    623623        assert(hcd);
    624624
    625         hcd_reserve_default_address(hcd, hcd->bus.max_speed);
     625        hcd_reserve_default_address(hcd, USB_SPEED_MAX);
    626626        const int ret = hcd_ddf_new_device(device, NULL, 0);
    627627        hcd_release_default_address(hcd);
     
    639639 * This function does all the ddf work for hc driver.
    640640 */
    641 int hcd_ddf_setup_hc(ddf_dev_t *device, usb_speed_t max_speed,
    642     size_t bw, bw_count_func_t bw_count)
     641int hcd_ddf_setup_hc(ddf_dev_t *device)
    643642{
    644643        assert(device);
     
    650649        }
    651650        instance->root_hub = NULL;
    652         hcd_init(&instance->hcd, max_speed, bw, bw_count);
     651        hcd_init(&instance->hcd);
    653652
    654653        int ret = ENOMEM;
     
    851850{
    852851        assert(driver);
    853         static const struct { size_t bw; bw_count_func_t bw_count; }bw[] = {
    854             [USB_SPEED_FULL] = { .bw = BANDWIDTH_AVAILABLE_USB11,
    855                                  .bw_count = bandwidth_count_usb11 },
    856             [USB_SPEED_HIGH] = { .bw = BANDWIDTH_AVAILABLE_USB11,
    857                                  .bw_count = bandwidth_count_usb11 },
    858             [USB_SPEED_SUPER] = { .bw = BANDWIDTH_AVAILABLE_USB11,
    859                                  .bw_count = bandwidth_count_usb11 },
    860         };
    861852
    862853        int ret = EOK;
    863         const usb_speed_t speed = driver->hc_speed;
    864         if (speed >= ARRAY_SIZE(bw) || bw[speed].bw == 0) {
    865                 usb_log_error("Driver `%s' reported unsupported speed: %s",
    866                     driver->name, usb_str_speed(speed));
    867                 return ENOTSUP;
    868         }
    869854
    870855        hw_res_list_parsed_t hw_res;
     
    877862        }
    878863
    879         ret = hcd_ddf_setup_hc(device, speed, bw[speed].bw, bw[speed].bw_count);
     864        ret = hcd_ddf_setup_hc(device);
    880865        if (ret != EOK) {
    881866                usb_log_error("Failed to setup generic HCD.\n");
  • uspace/lib/usbhost/src/endpoint.c

    r7e74911 r41924f30  
    11/*
    22 * Copyright (c) 2011 Jan Vesely
     3 * Copyright (c) 2017 Ondrej Hlavaty <aearsis@eideo.cz>
    34 * All rights reserved.
    45 *
     
    3536
    3637#include <usb/host/endpoint.h>
     38#include <usb/host/bus.h>
    3739
    3840#include <assert.h>
     41#include <atomic.h>
     42#include <mem.h>
    3943#include <stdlib.h>
    40 #include <atomic.h>
    4144
    42 /** Allocate ad initialize endpoint_t structure.
    43  * @param address USB address.
    44  * @param endpoint USB endpoint number.
    45  * @param direction Communication direction.
    46  * @param type USB transfer type.
    47  * @param speed Communication speed.
    48  * @param max_packet_size Maximum size of data packets.
    49  * @param bw Required bandwidth.
    50  * @return Pointer to initialized endpoint_t structure, NULL on failure.
     45/** Initialize provided endpoint structure.
    5146 */
    52 endpoint_t * endpoint_create(usb_address_t address, usb_endpoint_t endpoint,
    53     usb_direction_t direction, usb_transfer_type_t type, usb_speed_t speed,
    54     size_t max_packet_size, unsigned packets, size_t bw,
    55     usb_address_t tt_address, unsigned tt_p)
     47void endpoint_init(endpoint_t *ep, bus_t *bus)
    5648{
    57         endpoint_t *instance = malloc(sizeof(endpoint_t));
    58         if (instance) {
    59                 atomic_set(&instance->refcnt, 0);
    60                 instance->address = address;
    61                 instance->endpoint = endpoint;
    62                 instance->direction = direction;
    63                 instance->transfer_type = type;
    64                 instance->speed = speed;
    65                 instance->max_packet_size = max_packet_size;
    66                 instance->packets = packets;
    67                 instance->bandwidth = bw;
    68                 instance->toggle = 0;
    69                 instance->active = false;
    70                 instance->tt.address = tt_address;
    71                 instance->tt.port = tt_p;
    72                 instance->hc_data.data = NULL;
    73                 instance->hc_data.toggle_get = NULL;
    74                 instance->hc_data.toggle_set = NULL;
    75                 link_initialize(&instance->link);
    76                 fibril_mutex_initialize(&instance->guard);
    77                 fibril_condvar_initialize(&instance->avail);
    78         }
    79         return instance;
     49        memset(ep, 0, sizeof(endpoint_t));
     50
     51        ep->bus = bus;
     52        atomic_set(&ep->refcnt, 0);
     53        link_initialize(&ep->link);
     54        fibril_mutex_initialize(&ep->guard);
     55        fibril_condvar_initialize(&ep->avail);
    8056}
    8157
    82 /** Properly dispose of endpoint_t structure.
    83  * @param instance endpoint_t structure.
    84  */
    85 void endpoint_destroy(endpoint_t *instance)
     58void endpoint_add_ref(endpoint_t *ep)
    8659{
    87         assert(instance);
    88         assert(!instance->active);
    89         assert(instance->hc_data.data == NULL);
    90         free(instance);
     60        atomic_inc(&ep->refcnt);
    9161}
    9262
    93 void endpoint_add_ref(endpoint_t *instance)
     63void endpoint_del_ref(endpoint_t *ep)
    9464{
    95         atomic_inc(&instance->refcnt);
    96 }
     65        if (atomic_predec(&ep->refcnt) == 0) {
     66                if (ep->bus->ops.destroy_endpoint) {
     67                        ep->bus->ops.destroy_endpoint(ep);
     68                }
     69                else {
     70                        assert(!ep->active);
    9771
    98 void endpoint_del_ref(endpoint_t *instance)
    99 {
    100         if (atomic_predec(&instance->refcnt) == 0)
    101                 endpoint_destroy(instance);
    102 }
    103 
    104 /** Set device specific data and hooks.
    105  * @param instance endpoint_t structure.
    106  * @param data device specific data.
    107  * @param toggle_get Hook to call when retrieving value of toggle bit.
    108  * @param toggle_set Hook to call when setting the value of toggle bit.
    109  */
    110 void endpoint_set_hc_data(endpoint_t *instance,
    111     void *data, int (*toggle_get)(void *), void (*toggle_set)(void *, int))
    112 {
    113         assert(instance);
    114         fibril_mutex_lock(&instance->guard);
    115         instance->hc_data.data = data;
    116         instance->hc_data.toggle_get = toggle_get;
    117         instance->hc_data.toggle_set = toggle_set;
    118         fibril_mutex_unlock(&instance->guard);
    119 }
    120 
    121 /** Clear device specific data and hooks.
    122  * @param instance endpoint_t structure.
    123  * @note This function does not free memory pointed to by data pointer.
    124  */
    125 void endpoint_clear_hc_data(endpoint_t *instance)
    126 {
    127         assert(instance);
    128         endpoint_set_hc_data(instance, NULL, NULL, NULL);
     72                        /* Assume mostly the eps will be allocated by malloc. */
     73                        free(ep);
     74                }
     75        }
    12976}
    13077
    13178/** Mark the endpoint as active and block access for further fibrils.
    132  * @param instance endpoint_t structure.
     79 * @param ep endpoint_t structure.
    13380 */
    134 void endpoint_use(endpoint_t *instance)
     81void endpoint_use(endpoint_t *ep)
    13582{
    136         assert(instance);
     83        assert(ep);
    13784        /* Add reference for active endpoint. */
    138         endpoint_add_ref(instance);
    139         fibril_mutex_lock(&instance->guard);
    140         while (instance->active)
    141                 fibril_condvar_wait(&instance->avail, &instance->guard);
    142         instance->active = true;
    143         fibril_mutex_unlock(&instance->guard);
     85        endpoint_add_ref(ep);
     86        fibril_mutex_lock(&ep->guard);
     87        while (ep->active)
     88                fibril_condvar_wait(&ep->avail, &ep->guard);
     89        ep->active = true;
     90        fibril_mutex_unlock(&ep->guard);
    14491}
    14592
    14693/** Mark the endpoint as inactive and allow access for further fibrils.
    147  * @param instance endpoint_t structure.
     94 * @param ep endpoint_t structure.
    14895 */
    149 void endpoint_release(endpoint_t *instance)
     96void endpoint_release(endpoint_t *ep)
    15097{
    151         assert(instance);
    152         fibril_mutex_lock(&instance->guard);
    153         instance->active = false;
    154         fibril_mutex_unlock(&instance->guard);
    155         fibril_condvar_signal(&instance->avail);
     98        assert(ep);
     99        fibril_mutex_lock(&ep->guard);
     100        ep->active = false;
     101        fibril_mutex_unlock(&ep->guard);
     102        fibril_condvar_signal(&ep->avail);
    156103        /* Drop reference for active endpoint. */
    157         endpoint_del_ref(instance);
     104        endpoint_del_ref(ep);
    158105}
    159106
    160 /** Get the value of toggle bit.
    161  * @param instance endpoint_t structure.
    162  * @note Will use provided hook.
     107/** Get the value of toggle bit. Either uses the toggle_get op, or just returns
     108 * the value of the toggle.
     109 * @param ep endpoint_t structure.
    163110 */
    164 int endpoint_toggle_get(endpoint_t *instance)
     111int endpoint_toggle_get(endpoint_t *ep)
    165112{
    166         assert(instance);
    167         fibril_mutex_lock(&instance->guard);
    168         if (instance->hc_data.toggle_get)
    169                 instance->toggle =
    170                     instance->hc_data.toggle_get(instance->hc_data.data);
    171         const int ret = instance->toggle;
    172         fibril_mutex_unlock(&instance->guard);
     113        assert(ep);
     114        fibril_mutex_lock(&ep->guard);
     115        const int ret = ep->bus->ops.endpoint_get_toggle
     116            ? ep->bus->ops.endpoint_get_toggle(ep)
     117            : ep->toggle;
     118        fibril_mutex_unlock(&ep->guard);
    173119        return ret;
    174120}
    175121
    176 /** Set the value of toggle bit.
    177  * @param instance endpoint_t structure.
    178  * @note Will use provided hook.
     122/** Set the value of toggle bit. Either uses the toggle_set op, or just sets
     123 * the toggle inside.
     124 * @param ep endpoint_t structure.
    179125 */
    180 void endpoint_toggle_set(endpoint_t *instance, int toggle)
     126void endpoint_toggle_set(endpoint_t *ep, unsigned toggle)
    181127{
    182         assert(instance);
     128        assert(ep);
    183129        assert(toggle == 0 || toggle == 1);
    184         fibril_mutex_lock(&instance->guard);
    185         instance->toggle = toggle;
    186         if (instance->hc_data.toggle_set)
    187                 instance->hc_data.toggle_set(instance->hc_data.data, toggle);
    188         fibril_mutex_unlock(&instance->guard);
     130        fibril_mutex_lock(&ep->guard);
     131        if (ep->bus->ops.endpoint_set_toggle) {
     132                ep->bus->ops.endpoint_set_toggle(ep, toggle);
     133        }
     134        else {
     135                ep->toggle = toggle;
     136        }
     137        fibril_mutex_unlock(&ep->guard);
    189138}
     139
    190140
    191141/**
  • uspace/lib/usbhost/src/hcd.c

    r7e74911 r41924f30  
    4444#include "hcd.h"
    4545
    46 /** Calls ep_add_hook upon endpoint registration.
     46
     47/*[>* Calls ep_add_hook upon endpoint registration.
    4748 * @param ep Endpoint to be registered.
    4849 * @param arg hcd_t in disguise.
    4950 * @return Error code.
    50  */
     51 * OH TODO: remove
     52 <]
    5153static int register_helper(endpoint_t *ep, void *arg)
    5254{
     
    5961}
    6062
    61 /** Calls ep_remove_hook upon endpoint removal.
     63[>* Calls ep_remove_hook upon endpoint removal.
    6264 * @param ep Endpoint to be unregistered.
    6365 * @param arg hcd_t in disguise.
    64  */
     66 * OH TODO: remove
     67 <]
    6568static void unregister_helper(endpoint_t *ep, void *arg)
    6669{
     
    7275}
    7376
    74 /** Calls ep_remove_hook upon endpoint removal. Prints warning.
     77[>* Calls ep_remove_hook upon endpoint removal. Prints warning.
    7578 *  * @param ep Endpoint to be unregistered.
    7679 *   * @param arg hcd_t in disguise.
    77  *    */
     80 * OH TODO: remove
     81 *    <]
    7882static void unregister_helper_warn(endpoint_t *ep, void *arg)
    7983{
    8084        assert(ep);
    8185        usb_log_warning("Endpoint %d:%d %s was left behind, removing.\n",
    82             ep->address, ep->endpoint, usb_str_direction(ep->direction));
     86            ep->target.address, ep->target.endpoint, usb_str_direction(ep->direction));
    8387        unregister_helper(ep, arg);
    8488}
    85 
     89*/
    8690
    8791/** Initialize hcd_t structure.
     
    9397 * @param bw_count Bandwidth compute function, passed to endpoint manager.
    9498 */
    95 void hcd_init(hcd_t *hcd, usb_speed_t max_speed, size_t bandwidth,
    96     bw_count_func_t bw_count)
    97 {
    98         assert(hcd);
    99         usb_bus_init(&hcd->bus, bandwidth, bw_count, max_speed);
    100 
    101         hcd_set_implementation(hcd, NULL, NULL);
     99void hcd_init(hcd_t *hcd) {
     100        assert(hcd);
     101
     102        hcd_set_implementation(hcd, NULL, NULL, NULL);
    102103}
    103104
     
    106107        assert(hcd);
    107108        usb_address_t address = 0;
    108         const int ret = usb_bus_request_address(
    109             &hcd->bus, &address, false, speed);
     109        const int ret = bus_request_address(hcd->bus, &address, false, speed);
    110110        if (ret != EOK)
    111111                return ret;
     
    116116{
    117117        assert(hcd);
    118         return usb_bus_remove_address(&hcd->bus, address,
    119             unregister_helper_warn, hcd);
     118        return bus_release_address(hcd->bus, address);
     119        // OH TODO removed helper
    120120}
    121121
     
    124124        assert(hcd);
    125125        usb_address_t address = 0;
    126         return usb_bus_request_address(&hcd->bus, &address, true, speed);
     126        return bus_request_address(hcd->bus, &address, true, speed);
    127127}
    128128
     
    132132{
    133133        assert(hcd);
    134         return usb_bus_add_ep(&hcd->bus, target.address,
    135             target.endpoint, dir, type, max_packet_size, packets, size,
    136             register_helper, hcd, tt_address, tt_port);
     134
     135        /* Temporary reference */
     136        endpoint_t *ep = bus_create_endpoint(hcd->bus);
     137        if (!ep)
     138                return ENOMEM;
     139
     140        ep->target = target;
     141        ep->direction = dir;
     142        ep->transfer_type = type;
     143        ep->max_packet_size = max_packet_size;
     144        ep->packets = packets;
     145
     146        ep->tt.address = tt_address;
     147        ep->tt.port = tt_port;
     148
     149        const int err = bus_register_endpoint(hcd->bus, ep);
     150
     151        /* drop Temporary reference */
     152        endpoint_del_ref(ep);
     153
     154        return err;
    137155}
    138156
     
    140158{
    141159        assert(hcd);
    142         return usb_bus_remove_ep(&hcd->bus, target.address,
    143             target.endpoint, dir, unregister_helper, hcd);
     160        endpoint_t *ep = bus_find_endpoint(hcd->bus, target, dir);
     161        if (!ep)
     162                return ENOENT;
     163
     164        return bus_release_endpoint(hcd->bus, ep);
     165        // OH TODO removed helper
    144166}
    145167
     
    159181                usb_log_debug2("Reseting toggle on %d:%d.\n",
    160182                    toggle->target.address, toggle->target.endpoint);
    161                 usb_bus_reset_toggle(&toggle->hcd->bus,
     183                bus_reset_toggle(toggle->hcd->bus,
    162184                    toggle->target, toggle->target.endpoint == 0);
    163185        }
     
    185207        assert(hcd);
    186208
    187         endpoint_t *ep = usb_bus_find_ep(&hcd->bus,
    188             target.address, target.endpoint, direction);
     209        endpoint_t *ep = bus_find_endpoint(hcd->bus, target, direction);
    189210        if (ep == NULL) {
    190211                usb_log_error("Endpoint(%d:%d) not registered for %s.\n",
     
    196217            name, target.address, target.endpoint, size, ep->max_packet_size);
    197218
    198         const size_t bw = bandwidth_count_usb11(
    199             ep->speed, ep->transfer_type, size, ep->max_packet_size);
     219        const size_t bw = bus_count_bw(ep, size);
    200220        /* Check if we have enough bandwidth reserved */
    201221        if (ep->bandwidth < bw) {
    202222                usb_log_error("Endpoint(%d:%d) %s needs %zu bw "
    203223                    "but only %zu is reserved.\n",
    204                     ep->address, ep->endpoint, name, bw, ep->bandwidth);
     224                    ep->target.address, ep->target.endpoint, name, bw, ep->bandwidth);
    205225                return ENOSPC;
    206226        }
Note: See TracChangeset for help on using the changeset viewer.