| 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 | /*[>* Calls ep_add_hook upon endpoint registration.
|
|---|
| 48 | * @param ep Endpoint to be registered.
|
|---|
| 49 | * @param arg hcd_t in disguise.
|
|---|
| 50 | * @return Error code.
|
|---|
| 51 | * OH TODO: remove
|
|---|
| 52 | <]
|
|---|
| 53 | static int register_helper(endpoint_t *ep, void *arg)
|
|---|
| 54 | {
|
|---|
| 55 | hcd_t *hcd = arg;
|
|---|
| 56 | assert(ep);
|
|---|
| 57 | assert(hcd);
|
|---|
| 58 | if (hcd->ops.ep_add_hook)
|
|---|
| 59 | return hcd->ops.ep_add_hook(hcd, ep);
|
|---|
| 60 | return EOK;
|
|---|
| 61 | }
|
|---|
| 62 |
|
|---|
| 63 | [>* Calls ep_remove_hook upon endpoint removal.
|
|---|
| 64 | * @param ep Endpoint to be unregistered.
|
|---|
| 65 | * @param arg hcd_t in disguise.
|
|---|
| 66 | * OH TODO: remove
|
|---|
| 67 | <]
|
|---|
| 68 | static void unregister_helper(endpoint_t *ep, void *arg)
|
|---|
| 69 | {
|
|---|
| 70 | hcd_t *hcd = arg;
|
|---|
| 71 | assert(ep);
|
|---|
| 72 | assert(hcd);
|
|---|
| 73 | if (hcd->ops.ep_remove_hook)
|
|---|
| 74 | hcd->ops.ep_remove_hook(hcd, ep);
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | [>* Calls ep_remove_hook upon endpoint removal. Prints warning.
|
|---|
| 78 | * * @param ep Endpoint to be unregistered.
|
|---|
| 79 | * * @param arg hcd_t in disguise.
|
|---|
| 80 | * OH TODO: remove
|
|---|
| 81 | * <]
|
|---|
| 82 | static void unregister_helper_warn(endpoint_t *ep, void *arg)
|
|---|
| 83 | {
|
|---|
| 84 | assert(ep);
|
|---|
| 85 | usb_log_warning("Endpoint %d:%d %s was left behind, removing.\n",
|
|---|
| 86 | ep->target.address, ep->target.endpoint, usb_str_direction(ep->direction));
|
|---|
| 87 | unregister_helper(ep, arg);
|
|---|
| 88 | }
|
|---|
| 89 | */
|
|---|
| 90 |
|
|---|
| 91 | /** Initialize hcd_t structure.
|
|---|
| 92 | * Initializes device and endpoint managers. Sets data and hook pointer to NULL.
|
|---|
| 93 | *
|
|---|
| 94 | * @param hcd hcd_t structure to initialize, non-null.
|
|---|
| 95 | * @param max_speed Maximum supported USB speed (full, high).
|
|---|
| 96 | * @param bandwidth Available bandwidth, passed to endpoint manager.
|
|---|
| 97 | * @param bw_count Bandwidth compute function, passed to endpoint manager.
|
|---|
| 98 | */
|
|---|
| 99 | void hcd_init(hcd_t *hcd) {
|
|---|
| 100 | assert(hcd);
|
|---|
| 101 |
|
|---|
| 102 | hcd_set_implementation(hcd, NULL, NULL, NULL);
|
|---|
| 103 | }
|
|---|
| 104 |
|
|---|
| 105 | usb_address_t hcd_request_address(hcd_t *hcd, usb_speed_t speed)
|
|---|
| 106 | {
|
|---|
| 107 | assert(hcd);
|
|---|
| 108 | usb_address_t address = 0;
|
|---|
| 109 | const int ret = bus_request_address(hcd->bus, &address, false, speed);
|
|---|
| 110 | if (ret != EOK)
|
|---|
| 111 | return ret;
|
|---|
| 112 | return address;
|
|---|
| 113 | }
|
|---|
| 114 |
|
|---|
| 115 | int hcd_release_address(hcd_t *hcd, usb_address_t address)
|
|---|
| 116 | {
|
|---|
| 117 | assert(hcd);
|
|---|
| 118 | return bus_release_address(hcd->bus, address);
|
|---|
| 119 | // OH TODO removed helper
|
|---|
| 120 | }
|
|---|
| 121 |
|
|---|
| 122 | int hcd_reserve_default_address(hcd_t *hcd, usb_speed_t speed)
|
|---|
| 123 | {
|
|---|
| 124 | assert(hcd);
|
|---|
| 125 | usb_address_t address = 0;
|
|---|
| 126 | return bus_request_address(hcd->bus, &address, true, speed);
|
|---|
| 127 | }
|
|---|
| 128 |
|
|---|
| 129 | int hcd_add_ep(hcd_t *hcd, usb_target_t target, usb_direction_t dir,
|
|---|
| 130 | usb_transfer_type_t type, size_t max_packet_size, unsigned packets,
|
|---|
| 131 | size_t size, usb_address_t tt_address, unsigned tt_port)
|
|---|
| 132 | {
|
|---|
| 133 | assert(hcd);
|
|---|
| 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;
|
|---|
| 155 | }
|
|---|
| 156 |
|
|---|
| 157 | int hcd_remove_ep(hcd_t *hcd, usb_target_t target, usb_direction_t dir)
|
|---|
| 158 | {
|
|---|
| 159 | assert(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
|
|---|
| 166 | }
|
|---|
| 167 |
|
|---|
| 168 |
|
|---|
| 169 | typedef struct {
|
|---|
| 170 | void *original_data;
|
|---|
| 171 | usbhc_iface_transfer_out_callback_t original_callback;
|
|---|
| 172 | usb_target_t target;
|
|---|
| 173 | hcd_t *hcd;
|
|---|
| 174 | } toggle_t;
|
|---|
| 175 |
|
|---|
| 176 | static void toggle_reset_callback(int retval, void *arg)
|
|---|
| 177 | {
|
|---|
| 178 | assert(arg);
|
|---|
| 179 | toggle_t *toggle = arg;
|
|---|
| 180 | if (retval == EOK) {
|
|---|
| 181 | usb_log_debug2("Reseting toggle on %d:%d.\n",
|
|---|
| 182 | toggle->target.address, toggle->target.endpoint);
|
|---|
| 183 | bus_reset_toggle(toggle->hcd->bus,
|
|---|
| 184 | toggle->target, toggle->target.endpoint == 0);
|
|---|
| 185 | }
|
|---|
| 186 |
|
|---|
| 187 | toggle->original_callback(retval, toggle->original_data);
|
|---|
| 188 | }
|
|---|
| 189 |
|
|---|
| 190 | /** Prepare generic usb_transfer_batch and schedule it.
|
|---|
| 191 | * @param hcd Host controller driver.
|
|---|
| 192 | * @param fun DDF fun
|
|---|
| 193 | * @param target address and endpoint number.
|
|---|
| 194 | * @param setup_data Data to use in setup stage (Control communication type)
|
|---|
| 195 | * @param in Callback for device to host communication.
|
|---|
| 196 | * @param out Callback for host to device communication.
|
|---|
| 197 | * @param arg Callback parameter.
|
|---|
| 198 | * @param name Communication identifier (for nicer output).
|
|---|
| 199 | * @return Error code.
|
|---|
| 200 | */
|
|---|
| 201 | int hcd_send_batch(
|
|---|
| 202 | hcd_t *hcd, usb_target_t target, usb_direction_t direction,
|
|---|
| 203 | void *data, size_t size, uint64_t setup_data,
|
|---|
| 204 | usbhc_iface_transfer_in_callback_t in,
|
|---|
| 205 | usbhc_iface_transfer_out_callback_t out, void *arg, const char* name)
|
|---|
| 206 | {
|
|---|
| 207 | assert(hcd);
|
|---|
| 208 |
|
|---|
| 209 | endpoint_t *ep = bus_find_endpoint(hcd->bus, target, direction);
|
|---|
| 210 | if (ep == NULL) {
|
|---|
| 211 | usb_log_error("Endpoint(%d:%d) not registered for %s.\n",
|
|---|
| 212 | target.address, target.endpoint, name);
|
|---|
| 213 | return ENOENT;
|
|---|
| 214 | }
|
|---|
| 215 |
|
|---|
| 216 | usb_log_debug2("%s %d:%d %zu(%zu).\n",
|
|---|
| 217 | name, target.address, target.endpoint, size, ep->max_packet_size);
|
|---|
| 218 |
|
|---|
| 219 | const size_t bw = bus_count_bw(ep, size);
|
|---|
| 220 | /* Check if we have enough bandwidth reserved */
|
|---|
| 221 | if (ep->bandwidth < bw) {
|
|---|
| 222 | usb_log_error("Endpoint(%d:%d) %s needs %zu bw "
|
|---|
| 223 | "but only %zu is reserved.\n",
|
|---|
| 224 | ep->target.address, ep->target.endpoint, name, bw, ep->bandwidth);
|
|---|
| 225 | return ENOSPC;
|
|---|
| 226 | }
|
|---|
| 227 | if (!hcd->ops.schedule) {
|
|---|
| 228 | usb_log_error("HCD does not implement scheduler.\n");
|
|---|
| 229 | return ENOTSUP;
|
|---|
| 230 | }
|
|---|
| 231 |
|
|---|
| 232 | /* Check for commands that reset toggle bit */
|
|---|
| 233 | if (ep->transfer_type == USB_TRANSFER_CONTROL) {
|
|---|
| 234 | const int reset_toggle = usb_request_needs_toggle_reset(
|
|---|
| 235 | (usb_device_request_setup_packet_t *) &setup_data);
|
|---|
| 236 | if (reset_toggle >= 0) {
|
|---|
| 237 | assert(out);
|
|---|
| 238 | toggle_t *toggle = malloc(sizeof(toggle_t));
|
|---|
| 239 | if (!toggle)
|
|---|
| 240 | return ENOMEM;
|
|---|
| 241 | toggle->target.address = target.address;
|
|---|
| 242 | toggle->target.endpoint = reset_toggle;
|
|---|
| 243 | toggle->original_callback = out;
|
|---|
| 244 | toggle->original_data = arg;
|
|---|
| 245 | toggle->hcd = hcd;
|
|---|
| 246 |
|
|---|
| 247 | arg = toggle;
|
|---|
| 248 | out = toggle_reset_callback;
|
|---|
| 249 | }
|
|---|
| 250 | }
|
|---|
| 251 |
|
|---|
| 252 | usb_transfer_batch_t *batch = usb_transfer_batch_create(
|
|---|
| 253 | ep, data, size, setup_data, in, out, arg);
|
|---|
| 254 | if (!batch) {
|
|---|
| 255 | usb_log_error("Failed to create transfer batch.\n");
|
|---|
| 256 | return ENOMEM;
|
|---|
| 257 | }
|
|---|
| 258 |
|
|---|
| 259 | const int ret = hcd->ops.schedule(hcd, batch);
|
|---|
| 260 | if (ret != EOK)
|
|---|
| 261 | usb_transfer_batch_destroy(batch);
|
|---|
| 262 |
|
|---|
| 263 | /* Drop our own reference to ep. */
|
|---|
| 264 | endpoint_del_ref(ep);
|
|---|
| 265 |
|
|---|
| 266 | return ret;
|
|---|
| 267 | }
|
|---|
| 268 |
|
|---|
| 269 | typedef struct {
|
|---|
| 270 | fibril_mutex_t done_mtx;
|
|---|
| 271 | fibril_condvar_t done_cv;
|
|---|
| 272 | unsigned done;
|
|---|
| 273 | int ret;
|
|---|
| 274 | size_t size;
|
|---|
| 275 | } sync_data_t;
|
|---|
| 276 |
|
|---|
| 277 | static void transfer_in_cb(int ret, size_t size, void* data)
|
|---|
| 278 | {
|
|---|
| 279 | sync_data_t *d = data;
|
|---|
| 280 | assert(d);
|
|---|
| 281 | d->ret = ret;
|
|---|
| 282 | d->size = size;
|
|---|
| 283 | fibril_mutex_lock(&d->done_mtx);
|
|---|
| 284 | d->done = 1;
|
|---|
| 285 | fibril_condvar_broadcast(&d->done_cv);
|
|---|
| 286 | fibril_mutex_unlock(&d->done_mtx);
|
|---|
| 287 | }
|
|---|
| 288 |
|
|---|
| 289 | static void transfer_out_cb(int ret, void* data)
|
|---|
| 290 | {
|
|---|
| 291 | sync_data_t *d = data;
|
|---|
| 292 | assert(data);
|
|---|
| 293 | d->ret = ret;
|
|---|
| 294 | fibril_mutex_lock(&d->done_mtx);
|
|---|
| 295 | d->done = 1;
|
|---|
| 296 | fibril_condvar_broadcast(&d->done_cv);
|
|---|
| 297 | fibril_mutex_unlock(&d->done_mtx);
|
|---|
| 298 | }
|
|---|
| 299 |
|
|---|
| 300 | /** this is really ugly version of sync usb communication */
|
|---|
| 301 | ssize_t hcd_send_batch_sync(
|
|---|
| 302 | hcd_t *hcd, usb_target_t target, usb_direction_t dir,
|
|---|
| 303 | void *data, size_t size, uint64_t setup_data, const char* name)
|
|---|
| 304 | {
|
|---|
| 305 | assert(hcd);
|
|---|
| 306 | sync_data_t sd = { .done = 0, .ret = EBUSY, .size = size };
|
|---|
| 307 | fibril_mutex_initialize(&sd.done_mtx);
|
|---|
| 308 | fibril_condvar_initialize(&sd.done_cv);
|
|---|
| 309 |
|
|---|
| 310 | const int ret = hcd_send_batch(hcd, target, dir, data, size, setup_data,
|
|---|
| 311 | dir == USB_DIRECTION_IN ? transfer_in_cb : NULL,
|
|---|
| 312 | dir == USB_DIRECTION_OUT ? transfer_out_cb : NULL, &sd, name);
|
|---|
| 313 | if (ret != EOK)
|
|---|
| 314 | return ret;
|
|---|
| 315 |
|
|---|
| 316 | fibril_mutex_lock(&sd.done_mtx);
|
|---|
| 317 | while (!sd.done)
|
|---|
| 318 | fibril_condvar_wait(&sd.done_cv, &sd.done_mtx);
|
|---|
| 319 | fibril_mutex_unlock(&sd.done_mtx);
|
|---|
| 320 |
|
|---|
| 321 | if (sd.ret == EOK)
|
|---|
| 322 | return sd.size;
|
|---|
| 323 | return sd.ret;
|
|---|
| 324 | }
|
|---|
| 325 |
|
|---|
| 326 |
|
|---|
| 327 | /**
|
|---|
| 328 | * @}
|
|---|
| 329 | */
|
|---|