Changeset 32fb6bce in mainline for uspace/lib/usbhost/src/bus.c


Ignore:
Timestamp:
2017-12-18T22:50:21Z (6 years ago)
Author:
Ondřej Hlavatý <aearsis@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
7f70d1c
Parents:
1ea0bbf
git-author:
Ondřej Hlavatý <aearsis@…> (2017-12-18 22:04:50)
git-committer:
Ondřej Hlavatý <aearsis@…> (2017-12-18 22:50:21)
Message:

usbhost: refactoring

This commit moves interrupt, status and schedule to bus
operations. Then the purpose of hcd_t is better defined, and split into
hc_driver_t and hc_device_t. hc_driver_t is used to wrap driver
implementation by the library (similar to how usb_driver_t is used to
wrap usb device drivers). hc_device_t is used as a parent for hc_t
inside drivers, and is allocated inside the DDF device node.

To support these changes, some local identifiers were renamed, some
functions were moved and/or renamed and their arguments changed. The
most notable one being hcd_send_batch → bus_device_send_batch.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/usbhost/src/bus.c

    r1ea0bbf r32fb6bce  
    4646 * Initializes the bus structure.
    4747 */
    48 void bus_init(bus_t *bus, hcd_t *hcd, size_t device_size)
    49 {
    50         assert(bus);
    51         assert(hcd);
     48void bus_init(bus_t *bus, size_t device_size)
     49{
     50        assert(bus);
    5251        assert(device_size >= sizeof(device_t));
    5352        memset(bus, 0, sizeof(bus_t));
    5453
    5554        fibril_mutex_initialize(&bus->guard);
    56         bus->hcd = hcd;
    5755        bus->device_size = device_size;
    5856}
     
    6159{
    6260        assert(bus);
    63         assert(bus->hcd);
    6461
    6562        memset(dev, 0, sizeof(*dev));
     
    258255}
    259256
     257/** Prepare generic usb_transfer_batch and schedule it.
     258 * @param device Device for which to send the batch
     259 * @param target address and endpoint number.
     260 * @param setup_data Data to use in setup stage (Control communication type)
     261 * @param in Callback for device to host communication.
     262 * @param out Callback for host to device communication.
     263 * @param arg Callback parameter.
     264 * @param name Communication identifier (for nicer output).
     265 * @return Error code.
     266 */
     267int bus_device_send_batch(device_t *device, usb_target_t target,
     268    usb_direction_t direction, char *data, size_t size, uint64_t setup_data,
     269    usbhc_iface_transfer_callback_t on_complete, void *arg, const char *name)
     270{
     271        assert(device->address == target.address);
     272
     273        /* Temporary reference */
     274        endpoint_t *ep = bus_find_endpoint(device, target, direction);
     275        if (ep == NULL) {
     276                usb_log_error("Endpoint(%d:%d) not registered for %s.\n",
     277                    device->address, target.endpoint, name);
     278                return ENOENT;
     279        }
     280
     281        assert(ep->device == device);
     282
     283        const int err = endpoint_send_batch(ep, target, direction, data, size, setup_data,
     284            on_complete, arg, name);
     285
     286        /* Temporary reference */
     287        endpoint_del_ref(ep);
     288
     289        return err;
     290}
     291
     292typedef struct {
     293        fibril_mutex_t done_mtx;
     294        fibril_condvar_t done_cv;
     295        unsigned done;
     296
     297        size_t transfered_size;
     298        int error;
     299} sync_data_t;
     300
     301static int sync_transfer_complete(void *arg, int error, size_t transfered_size)
     302{
     303        sync_data_t *d = arg;
     304        assert(d);
     305        d->transfered_size = transfered_size;
     306        d->error = error;
     307        fibril_mutex_lock(&d->done_mtx);
     308        d->done = 1;
     309        fibril_condvar_broadcast(&d->done_cv);
     310        fibril_mutex_unlock(&d->done_mtx);
     311        return EOK;
     312}
     313
     314ssize_t bus_device_send_batch_sync(device_t *device, usb_target_t target,
     315    usb_direction_t direction, char *data, size_t size, uint64_t setup_data,
     316    const char *name)
     317{
     318        sync_data_t sd = { .done = 0 };
     319        fibril_mutex_initialize(&sd.done_mtx);
     320        fibril_condvar_initialize(&sd.done_cv);
     321
     322        const int ret = bus_device_send_batch(device, target, direction,
     323            data, size, setup_data,
     324            sync_transfer_complete, &sd, name);
     325        if (ret != EOK)
     326                return ret;
     327
     328        fibril_mutex_lock(&sd.done_mtx);
     329        while (!sd.done) {
     330                fibril_condvar_wait_timeout(&sd.done_cv, &sd.done_mtx, 3000000);
     331                if (!sd.done)
     332                        usb_log_debug2("Still waiting...");
     333        }
     334        fibril_mutex_unlock(&sd.done_mtx);
     335
     336        return (sd.error == EOK)
     337                ? (ssize_t) sd.transfered_size
     338                : (ssize_t) sd.error;
     339}
     340
    260341/**
    261342 * @}
Note: See TracChangeset for help on using the changeset viewer.