[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);
|
---|
[b5f813c] | 56 | if (hcd->ops.ep_add_hook)
|
---|
| 57 | return hcd->ops.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);
|
---|
[b5f813c] | 70 | if (hcd->ops.ep_remove_hook)
|
---|
| 71 | hcd->ops.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 |
|
---|
[b5f813c] | 101 | hcd_set_implementation(hcd, NULL, NULL);
|
---|
[21be46a] | 102 | }
|
---|
| 103 |
|
---|
[b4c1c95] | 104 | usb_address_t hcd_request_address(hcd_t *hcd, usb_speed_t speed)
|
---|
[0816c2e] | 105 | {
|
---|
| 106 | assert(hcd);
|
---|
[b4c1c95] | 107 | usb_address_t address = 0;
|
---|
[4cf5b8e0] | 108 | const int ret = usb_bus_request_address(
|
---|
| 109 | &hcd->bus, &address, false, speed);
|
---|
[b4c1c95] | 110 | if (ret != EOK)
|
---|
| 111 | return ret;
|
---|
| 112 | return address;
|
---|
[0816c2e] | 113 | }
|
---|
| 114 |
|
---|
[b4c1c95] | 115 | int hcd_release_address(hcd_t *hcd, usb_address_t address)
|
---|
| 116 | {
|
---|
| 117 | assert(hcd);
|
---|
[4cf5b8e0] | 118 | return usb_bus_remove_address(&hcd->bus, address,
|
---|
[b4c1c95] | 119 | unregister_helper_warn, hcd);
|
---|
| 120 | }
|
---|
[0816c2e] | 121 |
|
---|
[b4c1c95] | 122 | int hcd_reserve_default_address(hcd_t *hcd, usb_speed_t speed)
|
---|
[0816c2e] | 123 | {
|
---|
| 124 | assert(hcd);
|
---|
[b4c1c95] | 125 | usb_address_t address = 0;
|
---|
[b5f813c] | 126 | return usb_bus_request_address(&hcd->bus, &address, true, speed);
|
---|
[0816c2e] | 127 | }
|
---|
| 128 |
|
---|
| 129 | int hcd_add_ep(hcd_t *hcd, usb_target_t target, usb_direction_t dir,
|
---|
[4e732f1a] | 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)
|
---|
[0816c2e] | 132 | {
|
---|
| 133 | assert(hcd);
|
---|
[4cf5b8e0] | 134 | return usb_bus_add_ep(&hcd->bus, target.address,
|
---|
[4e732f1a] | 135 | target.endpoint, dir, type, max_packet_size, packets, size,
|
---|
| 136 | register_helper, hcd, tt_address, tt_port);
|
---|
[0816c2e] | 137 | }
|
---|
| 138 |
|
---|
| 139 | int hcd_remove_ep(hcd_t *hcd, usb_target_t target, usb_direction_t dir)
|
---|
| 140 | {
|
---|
| 141 | assert(hcd);
|
---|
[4cf5b8e0] | 142 | return usb_bus_remove_ep(&hcd->bus, target.address,
|
---|
[0816c2e] | 143 | target.endpoint, dir, unregister_helper, hcd);
|
---|
| 144 | }
|
---|
| 145 |
|
---|
| 146 |
|
---|
[1affef2f] | 147 | typedef struct {
|
---|
| 148 | void *original_data;
|
---|
| 149 | usbhc_iface_transfer_out_callback_t original_callback;
|
---|
| 150 | usb_target_t target;
|
---|
| 151 | hcd_t *hcd;
|
---|
| 152 | } toggle_t;
|
---|
| 153 |
|
---|
| 154 | static void toggle_reset_callback(int retval, void *arg)
|
---|
| 155 | {
|
---|
| 156 | assert(arg);
|
---|
| 157 | toggle_t *toggle = arg;
|
---|
| 158 | if (retval == EOK) {
|
---|
| 159 | usb_log_debug2("Reseting toggle on %d:%d.\n",
|
---|
| 160 | toggle->target.address, toggle->target.endpoint);
|
---|
[4cf5b8e0] | 161 | usb_bus_reset_toggle(&toggle->hcd->bus,
|
---|
[1affef2f] | 162 | toggle->target, toggle->target.endpoint == 0);
|
---|
| 163 | }
|
---|
| 164 |
|
---|
| 165 | toggle->original_callback(retval, toggle->original_data);
|
---|
| 166 | }
|
---|
| 167 |
|
---|
[d9b2c73] | 168 | /** Prepare generic usb_transfer_batch and schedule it.
|
---|
| 169 | * @param hcd Host controller driver.
|
---|
| 170 | * @param fun DDF fun
|
---|
| 171 | * @param target address and endpoint number.
|
---|
| 172 | * @param setup_data Data to use in setup stage (Control communication type)
|
---|
| 173 | * @param in Callback for device to host communication.
|
---|
| 174 | * @param out Callback for host to device communication.
|
---|
| 175 | * @param arg Callback parameter.
|
---|
| 176 | * @param name Communication identifier (for nicer output).
|
---|
| 177 | * @return Error code.
|
---|
| 178 | */
|
---|
| 179 | int hcd_send_batch(
|
---|
[3c4663e] | 180 | hcd_t *hcd, usb_target_t target, usb_direction_t direction,
|
---|
[d9b2c73] | 181 | void *data, size_t size, uint64_t setup_data,
|
---|
| 182 | usbhc_iface_transfer_in_callback_t in,
|
---|
| 183 | usbhc_iface_transfer_out_callback_t out, void *arg, const char* name)
|
---|
| 184 | {
|
---|
| 185 | assert(hcd);
|
---|
| 186 |
|
---|
[4cf5b8e0] | 187 | endpoint_t *ep = usb_bus_find_ep(&hcd->bus,
|
---|
[d9b2c73] | 188 | target.address, target.endpoint, direction);
|
---|
| 189 | if (ep == NULL) {
|
---|
| 190 | usb_log_error("Endpoint(%d:%d) not registered for %s.\n",
|
---|
| 191 | target.address, target.endpoint, name);
|
---|
| 192 | return ENOENT;
|
---|
| 193 | }
|
---|
| 194 |
|
---|
| 195 | usb_log_debug2("%s %d:%d %zu(%zu).\n",
|
---|
| 196 | name, target.address, target.endpoint, size, ep->max_packet_size);
|
---|
| 197 |
|
---|
| 198 | const size_t bw = bandwidth_count_usb11(
|
---|
| 199 | ep->speed, ep->transfer_type, size, ep->max_packet_size);
|
---|
| 200 | /* Check if we have enough bandwidth reserved */
|
---|
| 201 | if (ep->bandwidth < bw) {
|
---|
| 202 | usb_log_error("Endpoint(%d:%d) %s needs %zu bw "
|
---|
| 203 | "but only %zu is reserved.\n",
|
---|
| 204 | ep->address, ep->endpoint, name, bw, ep->bandwidth);
|
---|
| 205 | return ENOSPC;
|
---|
| 206 | }
|
---|
[b5f813c] | 207 | if (!hcd->ops.schedule) {
|
---|
[d9b2c73] | 208 | usb_log_error("HCD does not implement scheduler.\n");
|
---|
| 209 | return ENOTSUP;
|
---|
| 210 | }
|
---|
| 211 |
|
---|
[1affef2f] | 212 | /* Check for commands that reset toggle bit */
|
---|
| 213 | if (ep->transfer_type == USB_TRANSFER_CONTROL) {
|
---|
| 214 | const int reset_toggle = usb_request_needs_toggle_reset(
|
---|
| 215 | (usb_device_request_setup_packet_t *) &setup_data);
|
---|
| 216 | if (reset_toggle >= 0) {
|
---|
| 217 | assert(out);
|
---|
| 218 | toggle_t *toggle = malloc(sizeof(toggle_t));
|
---|
| 219 | if (!toggle)
|
---|
| 220 | return ENOMEM;
|
---|
| 221 | toggle->target.address = target.address;
|
---|
| 222 | toggle->target.endpoint = reset_toggle;
|
---|
| 223 | toggle->original_callback = out;
|
---|
| 224 | toggle->original_data = arg;
|
---|
| 225 | toggle->hcd = hcd;
|
---|
| 226 |
|
---|
| 227 | arg = toggle;
|
---|
| 228 | out = toggle_reset_callback;
|
---|
| 229 | }
|
---|
| 230 | }
|
---|
| 231 |
|
---|
| 232 | usb_transfer_batch_t *batch = usb_transfer_batch_create(
|
---|
[3c4663e] | 233 | ep, data, size, setup_data, in, out, arg);
|
---|
[d9b2c73] | 234 | if (!batch) {
|
---|
[bbd68a4] | 235 | usb_log_error("Failed to create transfer batch.\n");
|
---|
[d9b2c73] | 236 | return ENOMEM;
|
---|
| 237 | }
|
---|
| 238 |
|
---|
[b5f813c] | 239 | const int ret = hcd->ops.schedule(hcd, batch);
|
---|
[d9b2c73] | 240 | if (ret != EOK)
|
---|
| 241 | usb_transfer_batch_destroy(batch);
|
---|
| 242 |
|
---|
[f527f58] | 243 | /* Drop our own reference to ep. */
|
---|
| 244 | endpoint_del_ref(ep);
|
---|
| 245 |
|
---|
[d9b2c73] | 246 | return ret;
|
---|
| 247 | }
|
---|
| 248 |
|
---|
[237df2f] | 249 | typedef struct {
|
---|
| 250 | volatile unsigned done;
|
---|
[bbd68a4] | 251 | int ret;
|
---|
[237df2f] | 252 | size_t size;
|
---|
| 253 | } sync_data_t;
|
---|
| 254 |
|
---|
| 255 | static void transfer_in_cb(int ret, size_t size, void* data)
|
---|
| 256 | {
|
---|
| 257 | sync_data_t *d = data;
|
---|
| 258 | assert(d);
|
---|
| 259 | d->ret = ret;
|
---|
| 260 | d->done = 1;
|
---|
| 261 | d->size = size;
|
---|
| 262 | }
|
---|
| 263 |
|
---|
| 264 | static void transfer_out_cb(int ret, void* data)
|
---|
| 265 | {
|
---|
| 266 | sync_data_t *d = data;
|
---|
| 267 | assert(data);
|
---|
| 268 | d->ret = ret;
|
---|
| 269 | d->done = 1;
|
---|
| 270 | }
|
---|
| 271 |
|
---|
| 272 | /** this is really ugly version of sync usb communication */
|
---|
| 273 | ssize_t hcd_send_batch_sync(
|
---|
| 274 | hcd_t *hcd, usb_target_t target, usb_direction_t dir,
|
---|
| 275 | void *data, size_t size, uint64_t setup_data, const char* name)
|
---|
| 276 | {
|
---|
| 277 | assert(hcd);
|
---|
[a157846] | 278 | sync_data_t sd = { .done = 0, .ret = EBUSY, .size = size };
|
---|
[237df2f] | 279 |
|
---|
[bbd68a4] | 280 | const int ret = hcd_send_batch(hcd, target, dir, data, size, setup_data,
|
---|
[237df2f] | 281 | dir == USB_DIRECTION_IN ? transfer_in_cb : NULL,
|
---|
| 282 | dir == USB_DIRECTION_OUT ? transfer_out_cb : NULL, &sd, name);
|
---|
| 283 | if (ret != EOK)
|
---|
| 284 | return ret;
|
---|
[bbd68a4] | 285 |
|
---|
| 286 | while (!sd.done) {
|
---|
[237df2f] | 287 | async_usleep(1000);
|
---|
[bbd68a4] | 288 | }
|
---|
| 289 |
|
---|
[237df2f] | 290 | if (sd.ret == EOK)
|
---|
| 291 | return sd.size;
|
---|
| 292 | return sd.ret;
|
---|
| 293 | }
|
---|
| 294 |
|
---|
| 295 |
|
---|
[4daee7a] | 296 | /**
|
---|
| 297 | * @}
|
---|
| 298 | */
|
---|