Changeset 327f147 in mainline for uspace/lib/usbhost/src


Ignore:
Timestamp:
2017-10-23T19:03:37Z (8 years ago)
Author:
Ondřej Hlavatý <aearsis@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
b724494
Parents:
e160bfe8
Message:

usbhost: change parameters of methods

Pass (device_t, usb_target_t) to read and write, which finally allows to drop hash tables and access device right away. Then, all callbacks to complete transfer now uses usb_transfer_batch. This requires libdrv to include libusbhost, but it is not linked against it - it is there only to share definition of usb_transfer_batch_t.

Location:
uspace/lib/usbhost/src
Files:
5 edited

Legend:

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

    re160bfe8 r327f147  
    9999}
    100100
    101 int bus_remove_ep(bus_t *bus, usb_target_t target, usb_direction_t dir)
    102 {
    103         assert(bus);
    104         endpoint_t *ep = bus_find_endpoint(bus, target, dir);
     101int bus_remove_ep(bus_t *bus, device_t *dev, usb_target_t target, usb_direction_t dir)
     102{
     103        assert(bus);
     104        endpoint_t *ep = bus_find_endpoint(bus, dev, target, dir);
    105105        if (!ep)
    106106                return ENOENT;
     
    200200/** Searches for an endpoint. Returns a reference.
    201201 */
    202 endpoint_t *bus_find_endpoint(bus_t *bus, usb_target_t target, usb_direction_t dir)
    203 {
    204         assert(bus);
    205 
    206         fibril_mutex_lock(&bus->guard);
    207         endpoint_t *ep = bus->ops.find_endpoint(bus, target, dir);
     202endpoint_t *bus_find_endpoint(bus_t *bus, device_t *device, usb_target_t endpoint, usb_direction_t dir)
     203{
     204        assert(bus);
     205
     206        fibril_mutex_lock(&bus->guard);
     207        endpoint_t *ep = bus->ops.find_endpoint(bus, device, endpoint, dir);
    208208        if (ep) {
    209209                /* Exporting reference */
  • uspace/lib/usbhost/src/ddf_helpers.c

    re160bfe8 r327f147  
    135135                dev->address, endpoint_desc->endpoint_no,
    136136                usb_str_direction(endpoint_desc->direction));
    137         return bus_remove_ep(hcd->bus, target, endpoint_desc->direction);
     137        return bus_remove_ep(hcd->bus, dev, target, endpoint_desc->direction);
    138138}
    139139
     
    216216 * @return Error code.
    217217 */
    218 static int dev_read(ddf_fun_t *fun, usb_endpoint_t endpoint,
    219     uint64_t setup_data, uint8_t *data, size_t size,
    220     usbhc_iface_transfer_in_callback_t callback, void *arg)
     218static int dev_read(ddf_fun_t *fun, usb_target_t target,
     219    uint64_t setup_data, char *data, size_t size,
     220    usb_iface_transfer_callback_t callback, void *arg)
    221221{
    222222        assert(fun);
     223        hcd_t *hcd = dev_to_hcd(ddf_fun_get_dev(fun));
    223224        device_t *dev = ddf_fun_data_get(fun);
    224225        assert(dev);
    225         const usb_target_t target = {{
    226             .address  = dev->address,
    227             .endpoint = endpoint,
    228         }};
    229         return hcd_send_batch(dev_to_hcd(ddf_fun_get_dev(fun)), target,
    230             USB_DIRECTION_IN, data, size, setup_data, callback, NULL, arg,
    231             "READ");
     226
     227        target.address = dev->address;
     228
     229        return hcd_send_batch(hcd, dev, target, USB_DIRECTION_IN,
     230            data, size, setup_data,
     231            callback, arg, "READ");
    232232}
    233233
     
    242242 * @return Error code.
    243243 */
    244 static int dev_write(ddf_fun_t *fun, usb_endpoint_t endpoint,
    245     uint64_t setup_data, const uint8_t *data, size_t size,
    246     usbhc_iface_transfer_out_callback_t callback, void *arg)
     244static int dev_write(ddf_fun_t *fun, usb_target_t target,
     245    uint64_t setup_data, const char *data, size_t size,
     246    usb_iface_transfer_callback_t callback, void *arg)
    247247{
    248248        assert(fun);
     249        hcd_t *hcd = dev_to_hcd(ddf_fun_get_dev(fun));
    249250        device_t *dev = ddf_fun_data_get(fun);
    250251        assert(dev);
    251         const usb_target_t target = {{
    252             .address  = dev->address,
    253             .endpoint = endpoint,
    254         }};
    255         return hcd_send_batch(dev_to_hcd(ddf_fun_get_dev(fun)),
    256             target, USB_DIRECTION_OUT, (uint8_t*)data, size, setup_data, NULL,
     252
     253        target.address = dev->address;
     254
     255        return hcd_send_batch(hcd, dev, target, USB_DIRECTION_OUT,
     256            (char *) data, size, setup_data,
    257257            callback, arg, "WRITE");
    258258}
     
    411411        init_match_ids(&mids);
    412412
    413         usb_target_t control_ep = {{
     413        const usb_target_t control_ep = {{
    414414                .address = device->address,
    415                 .endpoint = 0
     415                .endpoint = 0,
    416416        }};
    417417
     
    422422        usb_log_debug("Device(%d): Requesting full device descriptor.",
    423423            device->address);
    424         ssize_t got = hcd_send_batch_sync(hcd, control_ep, USB_DIRECTION_IN,
    425             &desc, sizeof(desc), *(uint64_t *)&get_device_desc,
     424        ssize_t got = hcd_send_batch_sync(hcd, device, control_ep, USB_DIRECTION_IN,
     425            (char *) &desc, sizeof(desc), *(uint64_t *)&get_device_desc,
    426426            "read device descriptor");
    427427        if (got < 0) {
  • uspace/lib/usbhost/src/hcd.c

    re160bfe8 r327f147  
    8080 * @return Error code.
    8181 */
    82 int hcd_send_batch(hcd_t *hcd, usb_target_t target, usb_direction_t direction,
    83         void *data, size_t size, uint64_t setup_data,
    84         usbhc_iface_transfer_in_callback_t in, usbhc_iface_transfer_out_callback_t out,
    85         void *arg, const char *name)
     82int hcd_send_batch(hcd_t *hcd, device_t *device, usb_target_t target,
     83    usb_direction_t direction, char *data, size_t size, uint64_t setup_data,
     84    usb_transfer_batch_callback_t on_complete, void *arg, const char *name)
    8685{
    8786        assert(hcd);
     87        assert(device->address == target.address);
    8888
    89         endpoint_t *ep = bus_find_endpoint(hcd->bus, target, direction);
     89        endpoint_t *ep = bus_find_endpoint(hcd->bus, device, target, direction);
    9090        if (ep == NULL) {
    9191                usb_log_error("Endpoint(%d:%d) not registered for %s.\n",
    92                     target.address, target.endpoint, name);
     92                    device->address, target.endpoint, name);
    9393                return ENOENT;
    9494        }
     
    120120        batch->setup.packed = setup_data;
    121121        batch->dir = direction;
     122        batch->on_complete = on_complete;
     123        batch->on_complete_data = arg;
    122124
    123125        /* Check for commands that reset toggle bit */
     
    125127                batch->toggle_reset_mode
    126128                        = usb_request_get_toggle_reset_mode(&batch->setup.packet);
    127 
    128         usb_transfer_batch_set_old_handlers(batch, in, out, arg);
    129129
    130130        const int ret = hcd->ops.schedule(hcd, batch);
     
    144144        fibril_condvar_t done_cv;
    145145        unsigned done;
    146         int ret;
    147         size_t size;
     146
     147        size_t transfered_size;
     148        int error;
    148149} sync_data_t;
    149150
    150 static void transfer_in_cb(int ret, size_t size, void* data)
     151static int sync_transfer_complete(usb_transfer_batch_t *batch)
    151152{
    152         sync_data_t *d = data;
     153        sync_data_t *d = batch->on_complete_data;
    153154        assert(d);
    154         d->ret = ret;
    155         d->size = size;
     155        d->transfered_size = batch->transfered_size;
     156        d->error = batch->error;
    156157        fibril_mutex_lock(&d->done_mtx);
    157158        d->done = 1;
    158159        fibril_condvar_broadcast(&d->done_cv);
    159160        fibril_mutex_unlock(&d->done_mtx);
     161        return EOK;
    160162}
    161163
    162 static void transfer_out_cb(int ret, void* data)
    163 {
    164         sync_data_t *d = data;
    165         assert(data);
    166         d->ret = ret;
    167         fibril_mutex_lock(&d->done_mtx);
    168         d->done = 1;
    169         fibril_condvar_broadcast(&d->done_cv);
    170         fibril_mutex_unlock(&d->done_mtx);
    171 }
    172 
    173 /** this is really ugly version of sync usb communication */
    174 ssize_t hcd_send_batch_sync(
    175     hcd_t *hcd, usb_target_t target, usb_direction_t dir,
    176     void *data, size_t size, uint64_t setup_data, const char* name)
     164ssize_t hcd_send_batch_sync(hcd_t *hcd, device_t *device, usb_target_t target,
     165    usb_direction_t direction, char *data, size_t size, uint64_t setup_data,
     166    const char *name)
    177167{
    178168        assert(hcd);
    179         sync_data_t sd = { .done = 0, .ret = EBUSY, .size = size };
     169        sync_data_t sd = { .done = 0 };
    180170        fibril_mutex_initialize(&sd.done_mtx);
    181171        fibril_condvar_initialize(&sd.done_cv);
    182172
    183         const int ret = hcd_send_batch(hcd, target, dir, data, size, setup_data,
    184             dir == USB_DIRECTION_IN ? transfer_in_cb : NULL,
    185             dir == USB_DIRECTION_OUT ? transfer_out_cb : NULL, &sd, name);
     173        const int ret = hcd_send_batch(hcd, device, target, direction,
     174            data, size, setup_data,
     175            sync_transfer_complete, &sd, name);
    186176        if (ret != EOK)
    187177                return ret;
     
    192182        fibril_mutex_unlock(&sd.done_mtx);
    193183
    194         if (sd.ret == EOK)
    195                 return sd.size;
    196         return sd.ret;
     184        return (sd.error == EOK)
     185                ? (ssize_t) sd.transfered_size
     186                : (ssize_t) sd.error;
    197187}
    198188
  • uspace/lib/usbhost/src/usb2_bus.c

    re160bfe8 r327f147  
    126126        usb_log_debug("Device(%d): Requesting first 8B of device descriptor.",
    127127            address);
    128         ssize_t got = hcd_send_batch_sync(hcd, default_target, USB_DIRECTION_IN,
    129             &desc, CTRL_PIPE_MIN_PACKET_SIZE, *(uint64_t *)&get_device_desc_8,
     128        ssize_t got = hcd_send_batch_sync(hcd, dev, default_target, USB_DIRECTION_IN,
     129            (char *) &desc, CTRL_PIPE_MIN_PACKET_SIZE, *(uint64_t *)&get_device_desc_8,
    130130            "read first 8 bytes of dev descriptor");
    131131
     
    141141
    142142        usb_log_debug("Device(%d): Setting USB address.", address);
    143         err = hcd_send_batch_sync(hcd, default_target, USB_DIRECTION_OUT,
     143        err = hcd_send_batch_sync(hcd, dev, default_target, USB_DIRECTION_OUT,
    144144            NULL, 0, *(uint64_t *)&set_address, "set address");
    145145        if (err != 0) {
     
    163163        }
    164164
    165         bus_remove_ep(bus, default_target, USB_DIRECTION_BOTH);
     165        bus_remove_ep(bus, dev, default_target, USB_DIRECTION_BOTH);
    166166        return EOK;
    167167
    168168err_default_target:
    169         bus_remove_ep(bus, default_target, USB_DIRECTION_BOTH);
     169        bus_remove_ep(bus, dev, default_target, USB_DIRECTION_BOTH);
    170170err_address:
    171171        bus_release_address(bus, address);
     
    247247 * @note Assumes that the internal mutex is locked.
    248248 */
    249 static endpoint_t *usb2_bus_find_ep(bus_t *bus_base, usb_target_t target, usb_direction_t direction)
     249static endpoint_t *usb2_bus_find_ep(bus_t *bus_base, device_t *device, usb_target_t target, usb_direction_t direction)
    250250{
    251251        usb2_bus_t *bus = bus_to_usb2_bus(bus_base);
     
    293293
    294294        /* Check for existence */
    295         if (usb2_bus_find_ep(bus_base, ep->target, ep->direction))
     295        if (usb2_bus_find_ep(bus_base, ep->device, ep->target, ep->direction))
    296296                return EEXIST;
    297297
  • uspace/lib/usbhost/src/usb_transfer_batch.c

    re160bfe8 r327f147  
    133133}
    134134
    135 
    136 struct old_handler_wrapper_data {
    137         usbhc_iface_transfer_in_callback_t in_callback;
    138         usbhc_iface_transfer_out_callback_t out_callback;
    139         void *arg;
    140 };
    141 
    142 static int old_handler_wrapper(usb_transfer_batch_t *batch)
    143 {
    144         struct old_handler_wrapper_data *data = batch->on_complete_data;
    145 
    146         assert(data);
    147 
    148         if (data->out_callback)
    149                 data->out_callback(batch->error, data->arg);
    150 
    151         if (data->in_callback)
    152                 data->in_callback(batch->error, batch->transfered_size, data->arg);
    153 
    154         free(data);
    155         return EOK;
    156 }
    157 
    158 void usb_transfer_batch_set_old_handlers(usb_transfer_batch_t *batch,
    159         usbhc_iface_transfer_in_callback_t in_callback,
    160         usbhc_iface_transfer_out_callback_t out_callback,
    161         void *arg)
    162 {
    163         struct old_handler_wrapper_data *data = malloc(sizeof(*data));
    164 
    165         assert((!in_callback) != (!out_callback));
    166 
    167         data->in_callback = in_callback;
    168         data->out_callback = out_callback;
    169         data->arg = arg;
    170 
    171         batch->on_complete = old_handler_wrapper;
    172         batch->on_complete_data = data;
    173 }
    174135/**
    175136 * @}
Note: See TracChangeset for help on using the changeset viewer.