[4daee7a] | 1 | /*
|
---|
| 2 | * Copyright (c) 2011 Jan Vesely
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | /** @addtogroup libusbhost
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file
|
---|
| 33 | *
|
---|
| 34 | */
|
---|
| 35 |
|
---|
| 36 | #include <usb/debug.h>
|
---|
[1affef2f] | 37 | #include <usb/request.h>
|
---|
[4daee7a] | 38 |
|
---|
[8d2e251] | 39 | #include <assert.h>
|
---|
| 40 | #include <async.h>
|
---|
| 41 | #include <errno.h>
|
---|
| 42 | #include <usb_iface.h>
|
---|
[2b61945] | 43 | #include <str_error.h>
|
---|
[8d2e251] | 44 |
|
---|
[b4c1c95] | 45 | #include "hcd.h"
|
---|
| 46 |
|
---|
[41924f30] | 47 |
|
---|
[21be46a] | 48 | /** Initialize hcd_t structure.
|
---|
| 49 | * Initializes device and endpoint managers. Sets data and hook pointer to NULL.
|
---|
| 50 | *
|
---|
| 51 | * @param hcd hcd_t structure to initialize, non-null.
|
---|
| 52 | * @param max_speed Maximum supported USB speed (full, high).
|
---|
| 53 | * @param bandwidth Available bandwidth, passed to endpoint manager.
|
---|
| 54 | * @param bw_count Bandwidth compute function, passed to endpoint manager.
|
---|
| 55 | */
|
---|
[41924f30] | 56 | void hcd_init(hcd_t *hcd) {
|
---|
[21be46a] | 57 | assert(hcd);
|
---|
| 58 |
|
---|
[41924f30] | 59 | hcd_set_implementation(hcd, NULL, NULL, NULL);
|
---|
[21be46a] | 60 | }
|
---|
| 61 |
|
---|
[b4c1c95] | 62 | usb_address_t hcd_request_address(hcd_t *hcd, usb_speed_t speed)
|
---|
[0816c2e] | 63 | {
|
---|
| 64 | assert(hcd);
|
---|
[b4c1c95] | 65 | usb_address_t address = 0;
|
---|
[41924f30] | 66 | const int ret = bus_request_address(hcd->bus, &address, false, speed);
|
---|
[b4c1c95] | 67 | if (ret != EOK)
|
---|
| 68 | return ret;
|
---|
| 69 | return address;
|
---|
[0816c2e] | 70 | }
|
---|
| 71 |
|
---|
[d9b2c73] | 72 | /** Prepare generic usb_transfer_batch and schedule it.
|
---|
| 73 | * @param hcd Host controller driver.
|
---|
| 74 | * @param target address and endpoint number.
|
---|
| 75 | * @param setup_data Data to use in setup stage (Control communication type)
|
---|
| 76 | * @param in Callback for device to host communication.
|
---|
| 77 | * @param out Callback for host to device communication.
|
---|
| 78 | * @param arg Callback parameter.
|
---|
| 79 | * @param name Communication identifier (for nicer output).
|
---|
| 80 | * @return Error code.
|
---|
| 81 | */
|
---|
[327f147] | 82 | int 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,
|
---|
[f9d0a86] | 84 | usbhc_iface_transfer_callback_t on_complete, void *arg, const char *name)
|
---|
[d9b2c73] | 85 | {
|
---|
| 86 | assert(hcd);
|
---|
[327f147] | 87 | assert(device->address == target.address);
|
---|
[d9b2c73] | 88 |
|
---|
[56db65d] | 89 | if (!hcd->ops.schedule) {
|
---|
| 90 | usb_log_error("HCD does not implement scheduler.\n");
|
---|
| 91 | return ENOTSUP;
|
---|
| 92 | }
|
---|
| 93 |
|
---|
[327f147] | 94 | endpoint_t *ep = bus_find_endpoint(hcd->bus, device, target, direction);
|
---|
[d9b2c73] | 95 | if (ep == NULL) {
|
---|
| 96 | usb_log_error("Endpoint(%d:%d) not registered for %s.\n",
|
---|
[327f147] | 97 | device->address, target.endpoint, name);
|
---|
[d9b2c73] | 98 | return ENOENT;
|
---|
| 99 | }
|
---|
| 100 |
|
---|
[56db65d] | 101 | // TODO cut here aka provide helper to call with instance of endpoint_t in hand
|
---|
| 102 |
|
---|
[d9b2c73] | 103 | usb_log_debug2("%s %d:%d %zu(%zu).\n",
|
---|
| 104 | name, target.address, target.endpoint, size, ep->max_packet_size);
|
---|
| 105 |
|
---|
[41924f30] | 106 | const size_t bw = bus_count_bw(ep, size);
|
---|
[d9b2c73] | 107 | /* Check if we have enough bandwidth reserved */
|
---|
| 108 | if (ep->bandwidth < bw) {
|
---|
| 109 | usb_log_error("Endpoint(%d:%d) %s needs %zu bw "
|
---|
| 110 | "but only %zu is reserved.\n",
|
---|
[a5b3de6] | 111 | device->address, ep->endpoint, name, bw, ep->bandwidth);
|
---|
[d9b2c73] | 112 | return ENOSPC;
|
---|
| 113 | }
|
---|
| 114 |
|
---|
[5fd9c30] | 115 | usb_transfer_batch_t *batch = usb_transfer_batch_create(ep);
|
---|
[d9b2c73] | 116 | if (!batch) {
|
---|
[bbd68a4] | 117 | usb_log_error("Failed to create transfer batch.\n");
|
---|
[d9b2c73] | 118 | return ENOMEM;
|
---|
| 119 | }
|
---|
| 120 |
|
---|
[a5b3de6] | 121 | batch->target = target;
|
---|
[5fd9c30] | 122 | batch->buffer = data;
|
---|
| 123 | batch->buffer_size = size;
|
---|
| 124 | batch->setup.packed = setup_data;
|
---|
| 125 | batch->dir = direction;
|
---|
[327f147] | 126 | batch->on_complete = on_complete;
|
---|
| 127 | batch->on_complete_data = arg;
|
---|
[5fd9c30] | 128 |
|
---|
| 129 | /* Check for commands that reset toggle bit */
|
---|
| 130 | if (ep->transfer_type == USB_TRANSFER_CONTROL)
|
---|
| 131 | batch->toggle_reset_mode
|
---|
| 132 | = usb_request_get_toggle_reset_mode(&batch->setup.packet);
|
---|
| 133 |
|
---|
[b5f813c] | 134 | const int ret = hcd->ops.schedule(hcd, batch);
|
---|
[2b61945] | 135 | if (ret != EOK) {
|
---|
| 136 | usb_log_warning("Batch %p failed to schedule: %s", batch, str_error(ret));
|
---|
[d9b2c73] | 137 | usb_transfer_batch_destroy(batch);
|
---|
[2b61945] | 138 | }
|
---|
[d9b2c73] | 139 |
|
---|
[f527f58] | 140 | /* Drop our own reference to ep. */
|
---|
| 141 | endpoint_del_ref(ep);
|
---|
| 142 |
|
---|
[d9b2c73] | 143 | return ret;
|
---|
| 144 | }
|
---|
| 145 |
|
---|
[237df2f] | 146 | typedef struct {
|
---|
[ae03552e] | 147 | fibril_mutex_t done_mtx;
|
---|
| 148 | fibril_condvar_t done_cv;
|
---|
| 149 | unsigned done;
|
---|
[327f147] | 150 |
|
---|
| 151 | size_t transfered_size;
|
---|
| 152 | int error;
|
---|
[237df2f] | 153 | } sync_data_t;
|
---|
| 154 |
|
---|
[f9d0a86] | 155 | static int sync_transfer_complete(void *arg, int error, size_t transfered_size)
|
---|
[237df2f] | 156 | {
|
---|
[f9d0a86] | 157 | sync_data_t *d = arg;
|
---|
[237df2f] | 158 | assert(d);
|
---|
[f9d0a86] | 159 | d->transfered_size = transfered_size;
|
---|
| 160 | d->error = error;
|
---|
[ae03552e] | 161 | fibril_mutex_lock(&d->done_mtx);
|
---|
[237df2f] | 162 | d->done = 1;
|
---|
[ae03552e] | 163 | fibril_condvar_broadcast(&d->done_cv);
|
---|
| 164 | fibril_mutex_unlock(&d->done_mtx);
|
---|
[327f147] | 165 | return EOK;
|
---|
[237df2f] | 166 | }
|
---|
| 167 |
|
---|
[327f147] | 168 | ssize_t hcd_send_batch_sync(hcd_t *hcd, device_t *device, usb_target_t target,
|
---|
| 169 | usb_direction_t direction, char *data, size_t size, uint64_t setup_data,
|
---|
| 170 | const char *name)
|
---|
[237df2f] | 171 | {
|
---|
| 172 | assert(hcd);
|
---|
[327f147] | 173 | sync_data_t sd = { .done = 0 };
|
---|
[ae03552e] | 174 | fibril_mutex_initialize(&sd.done_mtx);
|
---|
| 175 | fibril_condvar_initialize(&sd.done_cv);
|
---|
[237df2f] | 176 |
|
---|
[327f147] | 177 | const int ret = hcd_send_batch(hcd, device, target, direction,
|
---|
| 178 | data, size, setup_data,
|
---|
| 179 | sync_transfer_complete, &sd, name);
|
---|
[237df2f] | 180 | if (ret != EOK)
|
---|
| 181 | return ret;
|
---|
[bbd68a4] | 182 |
|
---|
[ae03552e] | 183 | fibril_mutex_lock(&sd.done_mtx);
|
---|
| 184 | while (!sd.done)
|
---|
| 185 | fibril_condvar_wait(&sd.done_cv, &sd.done_mtx);
|
---|
| 186 | fibril_mutex_unlock(&sd.done_mtx);
|
---|
[bbd68a4] | 187 |
|
---|
[327f147] | 188 | return (sd.error == EOK)
|
---|
| 189 | ? (ssize_t) sd.transfered_size
|
---|
| 190 | : (ssize_t) sd.error;
|
---|
[237df2f] | 191 | }
|
---|
| 192 |
|
---|
| 193 |
|
---|
[4daee7a] | 194 | /**
|
---|
| 195 | * @}
|
---|
| 196 | */
|
---|