[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>
|
---|
| 43 |
|
---|
[b4c1c95] | 44 | #include "hcd.h"
|
---|
| 45 |
|
---|
| 46 | /** Calls ep_add_hook upon endpoint registration.
|
---|
| 47 | * @param ep Endpoint to be registered.
|
---|
| 48 | * @param arg hcd_t in disguise.
|
---|
| 49 | * @return Error code.
|
---|
| 50 | */
|
---|
| 51 | static int register_helper(endpoint_t *ep, void *arg)
|
---|
| 52 | {
|
---|
| 53 | hcd_t *hcd = arg;
|
---|
| 54 | assert(ep);
|
---|
| 55 | assert(hcd);
|
---|
[9348862] | 56 | if (hcd->driver.ep_add_hook)
|
---|
| 57 | return hcd->driver.ep_add_hook(hcd, ep);
|
---|
[b4c1c95] | 58 | return EOK;
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | /** Calls ep_remove_hook upon endpoint removal.
|
---|
| 62 | * @param ep Endpoint to be unregistered.
|
---|
| 63 | * @param arg hcd_t in disguise.
|
---|
| 64 | */
|
---|
| 65 | static void unregister_helper(endpoint_t *ep, void *arg)
|
---|
| 66 | {
|
---|
| 67 | hcd_t *hcd = arg;
|
---|
| 68 | assert(ep);
|
---|
| 69 | assert(hcd);
|
---|
[9348862] | 70 | if (hcd->driver.ep_remove_hook)
|
---|
| 71 | hcd->driver.ep_remove_hook(hcd, ep);
|
---|
[b4c1c95] | 72 | }
|
---|
| 73 |
|
---|
| 74 | /** Calls ep_remove_hook upon endpoint removal. Prints warning.
|
---|
| 75 | * * @param ep Endpoint to be unregistered.
|
---|
| 76 | * * @param arg hcd_t in disguise.
|
---|
| 77 | * */
|
---|
| 78 | static void unregister_helper_warn(endpoint_t *ep, void *arg)
|
---|
| 79 | {
|
---|
| 80 | assert(ep);
|
---|
| 81 | usb_log_warning("Endpoint %d:%d %s was left behind, removing.\n",
|
---|
| 82 | ep->address, ep->endpoint, usb_str_direction(ep->direction));
|
---|
| 83 | unregister_helper(ep, arg);
|
---|
| 84 | }
|
---|
| 85 |
|
---|
[4daee7a] | 86 |
|
---|
[21be46a] | 87 | /** Initialize hcd_t structure.
|
---|
| 88 | * Initializes device and endpoint managers. Sets data and hook pointer to NULL.
|
---|
| 89 | *
|
---|
| 90 | * @param hcd hcd_t structure to initialize, non-null.
|
---|
| 91 | * @param max_speed Maximum supported USB speed (full, high).
|
---|
| 92 | * @param bandwidth Available bandwidth, passed to endpoint manager.
|
---|
| 93 | * @param bw_count Bandwidth compute function, passed to endpoint manager.
|
---|
| 94 | */
|
---|
| 95 | void hcd_init(hcd_t *hcd, usb_speed_t max_speed, size_t bandwidth,
|
---|
| 96 | bw_count_func_t bw_count)
|
---|
| 97 | {
|
---|
| 98 | assert(hcd);
|
---|
[4cf5b8e0] | 99 | usb_bus_init(&hcd->bus, bandwidth, bw_count, max_speed);
|
---|
[21be46a] | 100 |
|
---|
[9348862] | 101 | hcd->driver.data = NULL;
|
---|
| 102 | hcd->driver.schedule = NULL;
|
---|
| 103 | hcd->driver.ep_add_hook = NULL;
|
---|
| 104 | hcd->driver.ep_remove_hook = NULL;
|
---|
[21be46a] | 105 | }
|
---|
| 106 |
|
---|
[b4c1c95] | 107 | usb_address_t hcd_request_address(hcd_t *hcd, usb_speed_t speed)
|
---|
[0816c2e] | 108 | {
|
---|
| 109 | assert(hcd);
|
---|
[b4c1c95] | 110 | usb_address_t address = 0;
|
---|
[4cf5b8e0] | 111 | const int ret = usb_bus_request_address(
|
---|
| 112 | &hcd->bus, &address, false, speed);
|
---|
[b4c1c95] | 113 | if (ret != EOK)
|
---|
| 114 | return ret;
|
---|
| 115 | return address;
|
---|
[0816c2e] | 116 | }
|
---|
| 117 |
|
---|
[b4c1c95] | 118 | int hcd_release_address(hcd_t *hcd, usb_address_t address)
|
---|
| 119 | {
|
---|
| 120 | assert(hcd);
|
---|
[4cf5b8e0] | 121 | return usb_bus_remove_address(&hcd->bus, address,
|
---|
[b4c1c95] | 122 | unregister_helper_warn, hcd);
|
---|
| 123 | }
|
---|
[0816c2e] | 124 |
|
---|
[b4c1c95] | 125 | int hcd_reserve_default_address(hcd_t *hcd, usb_speed_t speed)
|
---|
[0816c2e] | 126 | {
|
---|
| 127 | assert(hcd);
|
---|
[b4c1c95] | 128 | usb_address_t address = 0;
|
---|
[4cf5b8e0] | 129 | return usb_bus_request_address(
|
---|
| 130 | &hcd->bus, &address, true, speed);
|
---|
[0816c2e] | 131 | }
|
---|
| 132 |
|
---|
| 133 | int hcd_add_ep(hcd_t *hcd, usb_target_t target, usb_direction_t dir,
|
---|
[4e732f1a] | 134 | usb_transfer_type_t type, size_t max_packet_size, unsigned packets,
|
---|
| 135 | size_t size, usb_address_t tt_address, unsigned tt_port)
|
---|
[0816c2e] | 136 | {
|
---|
| 137 | assert(hcd);
|
---|
[4cf5b8e0] | 138 | return usb_bus_add_ep(&hcd->bus, target.address,
|
---|
[4e732f1a] | 139 | target.endpoint, dir, type, max_packet_size, packets, size,
|
---|
| 140 | register_helper, hcd, tt_address, tt_port);
|
---|
[0816c2e] | 141 | }
|
---|
| 142 |
|
---|
| 143 | int hcd_remove_ep(hcd_t *hcd, usb_target_t target, usb_direction_t dir)
|
---|
| 144 | {
|
---|
| 145 | assert(hcd);
|
---|
[4cf5b8e0] | 146 | return usb_bus_remove_ep(&hcd->bus, target.address,
|
---|
[0816c2e] | 147 | target.endpoint, dir, unregister_helper, hcd);
|
---|
| 148 | }
|
---|
| 149 |
|
---|
| 150 |
|
---|
[1affef2f] | 151 | typedef struct {
|
---|
| 152 | void *original_data;
|
---|
| 153 | usbhc_iface_transfer_out_callback_t original_callback;
|
---|
| 154 | usb_target_t target;
|
---|
| 155 | hcd_t *hcd;
|
---|
| 156 | } toggle_t;
|
---|
| 157 |
|
---|
| 158 | static void toggle_reset_callback(int retval, void *arg)
|
---|
| 159 | {
|
---|
| 160 | assert(arg);
|
---|
| 161 | toggle_t *toggle = arg;
|
---|
| 162 | if (retval == EOK) {
|
---|
| 163 | usb_log_debug2("Reseting toggle on %d:%d.\n",
|
---|
| 164 | toggle->target.address, toggle->target.endpoint);
|
---|
[4cf5b8e0] | 165 | usb_bus_reset_toggle(&toggle->hcd->bus,
|
---|
[1affef2f] | 166 | toggle->target, toggle->target.endpoint == 0);
|
---|
| 167 | }
|
---|
| 168 |
|
---|
| 169 | toggle->original_callback(retval, toggle->original_data);
|
---|
| 170 | }
|
---|
| 171 |
|
---|
[d9b2c73] | 172 | /** Prepare generic usb_transfer_batch and schedule it.
|
---|
| 173 | * @param hcd Host controller driver.
|
---|
| 174 | * @param fun DDF fun
|
---|
| 175 | * @param target address and endpoint number.
|
---|
| 176 | * @param setup_data Data to use in setup stage (Control communication type)
|
---|
| 177 | * @param in Callback for device to host communication.
|
---|
| 178 | * @param out Callback for host to device communication.
|
---|
| 179 | * @param arg Callback parameter.
|
---|
| 180 | * @param name Communication identifier (for nicer output).
|
---|
| 181 | * @return Error code.
|
---|
| 182 | */
|
---|
| 183 | int hcd_send_batch(
|
---|
[3c4663e] | 184 | hcd_t *hcd, usb_target_t target, usb_direction_t direction,
|
---|
[d9b2c73] | 185 | void *data, size_t size, uint64_t setup_data,
|
---|
| 186 | usbhc_iface_transfer_in_callback_t in,
|
---|
| 187 | usbhc_iface_transfer_out_callback_t out, void *arg, const char* name)
|
---|
| 188 | {
|
---|
| 189 | assert(hcd);
|
---|
| 190 |
|
---|
[4cf5b8e0] | 191 | endpoint_t *ep = usb_bus_find_ep(&hcd->bus,
|
---|
[d9b2c73] | 192 | target.address, target.endpoint, direction);
|
---|
| 193 | if (ep == NULL) {
|
---|
| 194 | usb_log_error("Endpoint(%d:%d) not registered for %s.\n",
|
---|
| 195 | target.address, target.endpoint, name);
|
---|
| 196 | return ENOENT;
|
---|
| 197 | }
|
---|
| 198 |
|
---|
| 199 | usb_log_debug2("%s %d:%d %zu(%zu).\n",
|
---|
| 200 | name, target.address, target.endpoint, size, ep->max_packet_size);
|
---|
| 201 |
|
---|
| 202 | const size_t bw = bandwidth_count_usb11(
|
---|
| 203 | ep->speed, ep->transfer_type, size, ep->max_packet_size);
|
---|
| 204 | /* Check if we have enough bandwidth reserved */
|
---|
| 205 | if (ep->bandwidth < bw) {
|
---|
| 206 | usb_log_error("Endpoint(%d:%d) %s needs %zu bw "
|
---|
| 207 | "but only %zu is reserved.\n",
|
---|
| 208 | ep->address, ep->endpoint, name, bw, ep->bandwidth);
|
---|
| 209 | return ENOSPC;
|
---|
| 210 | }
|
---|
[9348862] | 211 | if (!hcd->driver.schedule) {
|
---|
[d9b2c73] | 212 | usb_log_error("HCD does not implement scheduler.\n");
|
---|
| 213 | return ENOTSUP;
|
---|
| 214 | }
|
---|
| 215 |
|
---|
[1affef2f] | 216 | /* Check for commands that reset toggle bit */
|
---|
| 217 | if (ep->transfer_type == USB_TRANSFER_CONTROL) {
|
---|
| 218 | const int reset_toggle = usb_request_needs_toggle_reset(
|
---|
| 219 | (usb_device_request_setup_packet_t *) &setup_data);
|
---|
| 220 | if (reset_toggle >= 0) {
|
---|
| 221 | assert(out);
|
---|
| 222 | toggle_t *toggle = malloc(sizeof(toggle_t));
|
---|
| 223 | if (!toggle)
|
---|
| 224 | return ENOMEM;
|
---|
| 225 | toggle->target.address = target.address;
|
---|
| 226 | toggle->target.endpoint = reset_toggle;
|
---|
| 227 | toggle->original_callback = out;
|
---|
| 228 | toggle->original_data = arg;
|
---|
| 229 | toggle->hcd = hcd;
|
---|
| 230 |
|
---|
| 231 | arg = toggle;
|
---|
| 232 | out = toggle_reset_callback;
|
---|
| 233 | }
|
---|
| 234 | }
|
---|
| 235 |
|
---|
| 236 | usb_transfer_batch_t *batch = usb_transfer_batch_create(
|
---|
[3c4663e] | 237 | ep, data, size, setup_data, in, out, arg);
|
---|
[d9b2c73] | 238 | if (!batch) {
|
---|
[bbd68a4] | 239 | usb_log_error("Failed to create transfer batch.\n");
|
---|
[d9b2c73] | 240 | return ENOMEM;
|
---|
| 241 | }
|
---|
| 242 |
|
---|
[9348862] | 243 | const int ret = hcd->driver.schedule(hcd, batch);
|
---|
[d9b2c73] | 244 | if (ret != EOK)
|
---|
| 245 | usb_transfer_batch_destroy(batch);
|
---|
| 246 |
|
---|
| 247 | return ret;
|
---|
| 248 | }
|
---|
| 249 |
|
---|
[237df2f] | 250 | typedef struct {
|
---|
| 251 | volatile unsigned done;
|
---|
[bbd68a4] | 252 | int ret;
|
---|
[237df2f] | 253 | size_t size;
|
---|
| 254 | } sync_data_t;
|
---|
| 255 |
|
---|
| 256 | static void transfer_in_cb(int ret, size_t size, void* data)
|
---|
| 257 | {
|
---|
| 258 | sync_data_t *d = data;
|
---|
| 259 | assert(d);
|
---|
| 260 | d->ret = ret;
|
---|
| 261 | d->done = 1;
|
---|
| 262 | d->size = size;
|
---|
| 263 | }
|
---|
| 264 |
|
---|
| 265 | static void transfer_out_cb(int ret, void* data)
|
---|
| 266 | {
|
---|
| 267 | sync_data_t *d = data;
|
---|
| 268 | assert(data);
|
---|
| 269 | d->ret = ret;
|
---|
| 270 | d->done = 1;
|
---|
| 271 | }
|
---|
| 272 |
|
---|
| 273 | /** this is really ugly version of sync usb communication */
|
---|
| 274 | ssize_t hcd_send_batch_sync(
|
---|
| 275 | hcd_t *hcd, usb_target_t target, usb_direction_t dir,
|
---|
| 276 | void *data, size_t size, uint64_t setup_data, const char* name)
|
---|
| 277 | {
|
---|
| 278 | assert(hcd);
|
---|
[a157846] | 279 | sync_data_t sd = { .done = 0, .ret = EBUSY, .size = size };
|
---|
[237df2f] | 280 |
|
---|
[bbd68a4] | 281 | const int ret = hcd_send_batch(hcd, target, dir, data, size, setup_data,
|
---|
[237df2f] | 282 | dir == USB_DIRECTION_IN ? transfer_in_cb : NULL,
|
---|
| 283 | dir == USB_DIRECTION_OUT ? transfer_out_cb : NULL, &sd, name);
|
---|
| 284 | if (ret != EOK)
|
---|
| 285 | return ret;
|
---|
[bbd68a4] | 286 |
|
---|
| 287 | while (!sd.done) {
|
---|
[237df2f] | 288 | async_usleep(1000);
|
---|
[bbd68a4] | 289 | }
|
---|
| 290 |
|
---|
[237df2f] | 291 | if (sd.ret == EOK)
|
---|
| 292 | return sd.size;
|
---|
| 293 | return sd.ret;
|
---|
| 294 | }
|
---|
| 295 |
|
---|
| 296 |
|
---|
[4daee7a] | 297 | /**
|
---|
| 298 | * @}
|
---|
| 299 | */
|
---|