| 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>
|
|---|
| 37 | #include <usb/request.h>
|
|---|
| 38 |
|
|---|
| 39 | #include <assert.h>
|
|---|
| 40 | #include <async.h>
|
|---|
| 41 | #include <errno.h>
|
|---|
| 42 | #include <usb_iface.h>
|
|---|
| 43 |
|
|---|
| 44 | #include "hcd.h"
|
|---|
| 45 |
|
|---|
| 46 |
|
|---|
| 47 | /** Initialize hcd_t structure.
|
|---|
| 48 | * Initializes device and endpoint managers. Sets data and hook pointer to NULL.
|
|---|
| 49 | *
|
|---|
| 50 | * @param hcd hcd_t structure to initialize, non-null.
|
|---|
| 51 | * @param max_speed Maximum supported USB speed (full, high).
|
|---|
| 52 | * @param bandwidth Available bandwidth, passed to endpoint manager.
|
|---|
| 53 | * @param bw_count Bandwidth compute function, passed to endpoint manager.
|
|---|
| 54 | */
|
|---|
| 55 | void hcd_init(hcd_t *hcd) {
|
|---|
| 56 | assert(hcd);
|
|---|
| 57 |
|
|---|
| 58 | hcd_set_implementation(hcd, NULL, NULL, NULL);
|
|---|
| 59 | }
|
|---|
| 60 |
|
|---|
| 61 | usb_address_t hcd_request_address(hcd_t *hcd, usb_speed_t speed)
|
|---|
| 62 | {
|
|---|
| 63 | assert(hcd);
|
|---|
| 64 | usb_address_t address = 0;
|
|---|
| 65 | const int ret = bus_request_address(hcd->bus, &address, false, speed);
|
|---|
| 66 | if (ret != EOK)
|
|---|
| 67 | return ret;
|
|---|
| 68 | return address;
|
|---|
| 69 | }
|
|---|
| 70 |
|
|---|
| 71 | typedef struct {
|
|---|
| 72 | void *original_data;
|
|---|
| 73 | usbhc_iface_transfer_out_callback_t original_callback;
|
|---|
| 74 | usb_target_t target;
|
|---|
| 75 | hcd_t *hcd;
|
|---|
| 76 | } toggle_t;
|
|---|
| 77 |
|
|---|
| 78 | static void toggle_reset_callback(int retval, void *arg)
|
|---|
| 79 | {
|
|---|
| 80 | assert(arg);
|
|---|
| 81 | toggle_t *toggle = arg;
|
|---|
| 82 | if (retval == EOK) {
|
|---|
| 83 | usb_log_debug2("Reseting toggle on %d:%d.\n",
|
|---|
| 84 | toggle->target.address, toggle->target.endpoint);
|
|---|
| 85 | bus_reset_toggle(toggle->hcd->bus,
|
|---|
| 86 | toggle->target, toggle->target.endpoint == 0);
|
|---|
| 87 | }
|
|---|
| 88 |
|
|---|
| 89 | toggle->original_callback(retval, toggle->original_data);
|
|---|
| 90 | }
|
|---|
| 91 |
|
|---|
| 92 | /** Prepare generic usb_transfer_batch and schedule it.
|
|---|
| 93 | * @param hcd Host controller driver.
|
|---|
| 94 | * @param fun DDF fun
|
|---|
| 95 | * @param target address and endpoint number.
|
|---|
| 96 | * @param setup_data Data to use in setup stage (Control communication type)
|
|---|
| 97 | * @param in Callback for device to host communication.
|
|---|
| 98 | * @param out Callback for host to device communication.
|
|---|
| 99 | * @param arg Callback parameter.
|
|---|
| 100 | * @param name Communication identifier (for nicer output).
|
|---|
| 101 | * @return Error code.
|
|---|
| 102 | */
|
|---|
| 103 | int hcd_send_batch(
|
|---|
| 104 | hcd_t *hcd, usb_target_t target, usb_direction_t direction,
|
|---|
| 105 | void *data, size_t size, uint64_t setup_data,
|
|---|
| 106 | usbhc_iface_transfer_in_callback_t in,
|
|---|
| 107 | usbhc_iface_transfer_out_callback_t out, void *arg, const char* name)
|
|---|
| 108 | {
|
|---|
| 109 | assert(hcd);
|
|---|
| 110 |
|
|---|
| 111 | endpoint_t *ep = bus_find_endpoint(hcd->bus, target, direction);
|
|---|
| 112 | if (ep == NULL) {
|
|---|
| 113 | usb_log_error("Endpoint(%d:%d) not registered for %s.\n",
|
|---|
| 114 | target.address, target.endpoint, name);
|
|---|
| 115 | return ENOENT;
|
|---|
| 116 | }
|
|---|
| 117 |
|
|---|
| 118 | usb_log_debug2("%s %d:%d %zu(%zu).\n",
|
|---|
| 119 | name, target.address, target.endpoint, size, ep->max_packet_size);
|
|---|
| 120 |
|
|---|
| 121 | const size_t bw = bus_count_bw(ep, size);
|
|---|
| 122 | /* Check if we have enough bandwidth reserved */
|
|---|
| 123 | if (ep->bandwidth < bw) {
|
|---|
| 124 | usb_log_error("Endpoint(%d:%d) %s needs %zu bw "
|
|---|
| 125 | "but only %zu is reserved.\n",
|
|---|
| 126 | ep->target.address, ep->target.endpoint, name, bw, ep->bandwidth);
|
|---|
| 127 | return ENOSPC;
|
|---|
| 128 | }
|
|---|
| 129 | if (!hcd->ops.schedule) {
|
|---|
| 130 | usb_log_error("HCD does not implement scheduler.\n");
|
|---|
| 131 | return ENOTSUP;
|
|---|
| 132 | }
|
|---|
| 133 |
|
|---|
| 134 | /* Check for commands that reset toggle bit */
|
|---|
| 135 | if (ep->transfer_type == USB_TRANSFER_CONTROL) {
|
|---|
| 136 | const int reset_toggle = usb_request_needs_toggle_reset(
|
|---|
| 137 | (usb_device_request_setup_packet_t *) &setup_data);
|
|---|
| 138 | if (reset_toggle >= 0) {
|
|---|
| 139 | assert(out);
|
|---|
| 140 | toggle_t *toggle = malloc(sizeof(toggle_t));
|
|---|
| 141 | if (!toggle)
|
|---|
| 142 | return ENOMEM;
|
|---|
| 143 | toggle->target.address = target.address;
|
|---|
| 144 | toggle->target.endpoint = reset_toggle;
|
|---|
| 145 | toggle->original_callback = out;
|
|---|
| 146 | toggle->original_data = arg;
|
|---|
| 147 | toggle->hcd = hcd;
|
|---|
| 148 |
|
|---|
| 149 | arg = toggle;
|
|---|
| 150 | out = toggle_reset_callback;
|
|---|
| 151 | }
|
|---|
| 152 | }
|
|---|
| 153 |
|
|---|
| 154 | usb_transfer_batch_t *batch = usb_transfer_batch_create(
|
|---|
| 155 | ep, data, size, setup_data, in, out, arg);
|
|---|
| 156 | if (!batch) {
|
|---|
| 157 | usb_log_error("Failed to create transfer batch.\n");
|
|---|
| 158 | return ENOMEM;
|
|---|
| 159 | }
|
|---|
| 160 |
|
|---|
| 161 | const int ret = hcd->ops.schedule(hcd, batch);
|
|---|
| 162 | if (ret != EOK)
|
|---|
| 163 | usb_transfer_batch_destroy(batch);
|
|---|
| 164 |
|
|---|
| 165 | /* Drop our own reference to ep. */
|
|---|
| 166 | endpoint_del_ref(ep);
|
|---|
| 167 |
|
|---|
| 168 | return ret;
|
|---|
| 169 | }
|
|---|
| 170 |
|
|---|
| 171 | typedef struct {
|
|---|
| 172 | fibril_mutex_t done_mtx;
|
|---|
| 173 | fibril_condvar_t done_cv;
|
|---|
| 174 | unsigned done;
|
|---|
| 175 | int ret;
|
|---|
| 176 | size_t size;
|
|---|
| 177 | } sync_data_t;
|
|---|
| 178 |
|
|---|
| 179 | static void transfer_in_cb(int ret, size_t size, void* data)
|
|---|
| 180 | {
|
|---|
| 181 | sync_data_t *d = data;
|
|---|
| 182 | assert(d);
|
|---|
| 183 | d->ret = ret;
|
|---|
| 184 | d->size = size;
|
|---|
| 185 | fibril_mutex_lock(&d->done_mtx);
|
|---|
| 186 | d->done = 1;
|
|---|
| 187 | fibril_condvar_broadcast(&d->done_cv);
|
|---|
| 188 | fibril_mutex_unlock(&d->done_mtx);
|
|---|
| 189 | }
|
|---|
| 190 |
|
|---|
| 191 | static void transfer_out_cb(int ret, void* data)
|
|---|
| 192 | {
|
|---|
| 193 | sync_data_t *d = data;
|
|---|
| 194 | assert(data);
|
|---|
| 195 | d->ret = ret;
|
|---|
| 196 | fibril_mutex_lock(&d->done_mtx);
|
|---|
| 197 | d->done = 1;
|
|---|
| 198 | fibril_condvar_broadcast(&d->done_cv);
|
|---|
| 199 | fibril_mutex_unlock(&d->done_mtx);
|
|---|
| 200 | }
|
|---|
| 201 |
|
|---|
| 202 | /** this is really ugly version of sync usb communication */
|
|---|
| 203 | ssize_t hcd_send_batch_sync(
|
|---|
| 204 | hcd_t *hcd, usb_target_t target, usb_direction_t dir,
|
|---|
| 205 | void *data, size_t size, uint64_t setup_data, const char* name)
|
|---|
| 206 | {
|
|---|
| 207 | assert(hcd);
|
|---|
| 208 | sync_data_t sd = { .done = 0, .ret = EBUSY, .size = size };
|
|---|
| 209 | fibril_mutex_initialize(&sd.done_mtx);
|
|---|
| 210 | fibril_condvar_initialize(&sd.done_cv);
|
|---|
| 211 |
|
|---|
| 212 | const int ret = hcd_send_batch(hcd, target, dir, data, size, setup_data,
|
|---|
| 213 | dir == USB_DIRECTION_IN ? transfer_in_cb : NULL,
|
|---|
| 214 | dir == USB_DIRECTION_OUT ? transfer_out_cb : NULL, &sd, name);
|
|---|
| 215 | if (ret != EOK)
|
|---|
| 216 | return ret;
|
|---|
| 217 |
|
|---|
| 218 | fibril_mutex_lock(&sd.done_mtx);
|
|---|
| 219 | while (!sd.done)
|
|---|
| 220 | fibril_condvar_wait(&sd.done_cv, &sd.done_mtx);
|
|---|
| 221 | fibril_mutex_unlock(&sd.done_mtx);
|
|---|
| 222 |
|
|---|
| 223 | if (sd.ret == EOK)
|
|---|
| 224 | return sd.size;
|
|---|
| 225 | return sd.ret;
|
|---|
| 226 | }
|
|---|
| 227 |
|
|---|
| 228 |
|
|---|
| 229 | /**
|
|---|
| 230 | * @}
|
|---|
| 231 | */
|
|---|