[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>
|
---|
[306a36d] | 37 | #include <usb/descriptor.h>
|
---|
[1affef2f] | 38 | #include <usb/request.h>
|
---|
[4daee7a] | 39 |
|
---|
[8d2e251] | 40 | #include <assert.h>
|
---|
| 41 | #include <async.h>
|
---|
| 42 | #include <errno.h>
|
---|
[306a36d] | 43 | #include <macros.h>
|
---|
[8d2e251] | 44 | #include <usb_iface.h>
|
---|
[2b61945] | 45 | #include <str_error.h>
|
---|
[8d2e251] | 46 |
|
---|
[b4c1c95] | 47 | #include "hcd.h"
|
---|
| 48 |
|
---|
[41924f30] | 49 |
|
---|
[21be46a] | 50 | /** Initialize hcd_t structure.
|
---|
| 51 | * Initializes device and endpoint managers. Sets data and hook pointer to NULL.
|
---|
| 52 | *
|
---|
| 53 | * @param hcd hcd_t structure to initialize, non-null.
|
---|
| 54 | * @param max_speed Maximum supported USB speed (full, high).
|
---|
| 55 | * @param bandwidth Available bandwidth, passed to endpoint manager.
|
---|
| 56 | * @param bw_count Bandwidth compute function, passed to endpoint manager.
|
---|
| 57 | */
|
---|
[41924f30] | 58 | void hcd_init(hcd_t *hcd) {
|
---|
[21be46a] | 59 | assert(hcd);
|
---|
| 60 |
|
---|
[41924f30] | 61 | hcd_set_implementation(hcd, NULL, NULL, NULL);
|
---|
[21be46a] | 62 | }
|
---|
| 63 |
|
---|
[306a36d] | 64 | /** Get max packet size for the control endpoint 0.
|
---|
| 65 | *
|
---|
| 66 | * For LS, HS, and SS devices this value is fixed. For FS devices we must fetch
|
---|
| 67 | * the first 8B of the device descriptor to determine it.
|
---|
| 68 | *
|
---|
[375211d2] | 69 | * @return Max packet size for EP 0
|
---|
[306a36d] | 70 | */
|
---|
| 71 | int hcd_get_ep0_max_packet_size(uint16_t *mps, hcd_t *hcd, device_t *dev)
|
---|
| 72 | {
|
---|
[375211d2] | 73 | assert(mps);
|
---|
| 74 |
|
---|
[306a36d] | 75 | static const uint16_t mps_fixed [] = {
|
---|
| 76 | [USB_SPEED_LOW] = 8,
|
---|
| 77 | [USB_SPEED_HIGH] = 64,
|
---|
| 78 | [USB_SPEED_SUPER] = 512,
|
---|
| 79 | };
|
---|
| 80 |
|
---|
| 81 | if (dev->speed < ARRAY_SIZE(mps_fixed) && mps_fixed[dev->speed] != 0) {
|
---|
| 82 | *mps = mps_fixed[dev->speed];
|
---|
| 83 | return EOK;
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | const usb_target_t control_ep = {{
|
---|
| 87 | .address = dev->address,
|
---|
| 88 | .endpoint = 0,
|
---|
| 89 | }};
|
---|
| 90 |
|
---|
| 91 | usb_standard_device_descriptor_t desc = { 0 };
|
---|
| 92 | const usb_device_request_setup_packet_t get_device_desc_8 =
|
---|
| 93 | GET_DEVICE_DESC(CTRL_PIPE_MIN_PACKET_SIZE);
|
---|
| 94 |
|
---|
[375211d2] | 95 | usb_log_debug("Requesting first 8B of device descriptor to determine MPS.");
|
---|
[306a36d] | 96 | ssize_t got = hcd_send_batch_sync(hcd, dev, control_ep, USB_DIRECTION_IN,
|
---|
| 97 | (char *) &desc, CTRL_PIPE_MIN_PACKET_SIZE, *(uint64_t *)&get_device_desc_8,
|
---|
| 98 | "read first 8 bytes of dev descriptor");
|
---|
| 99 |
|
---|
| 100 | if (got != CTRL_PIPE_MIN_PACKET_SIZE) {
|
---|
| 101 | const int err = got < 0 ? got : EOVERFLOW;
|
---|
| 102 | usb_log_error("Failed to get 8B of dev descr: %s.", str_error(err));
|
---|
| 103 | return err;
|
---|
| 104 | }
|
---|
| 105 |
|
---|
[375211d2] | 106 | if (desc.descriptor_type != USB_DESCTYPE_DEVICE) {
|
---|
| 107 | usb_log_error("The device responded with wrong device descriptor.");
|
---|
| 108 | return EIO;
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | uint16_t version = uint16_usb2host(desc.usb_spec_version);
|
---|
| 112 | if (version < 0x0300) {
|
---|
| 113 | /* USB 2 and below have MPS raw in the field */
|
---|
| 114 | *mps = desc.max_packet_size;
|
---|
| 115 | } else {
|
---|
| 116 | /* USB 3 have MPS as an 2-based exponent */
|
---|
| 117 | *mps = (1 << desc.max_packet_size);
|
---|
| 118 | }
|
---|
[306a36d] | 119 | return EOK;
|
---|
| 120 | }
|
---|
| 121 |
|
---|
[ff14aede] | 122 | /**
|
---|
| 123 | * Setup devices Transaction Translation.
|
---|
| 124 | *
|
---|
| 125 | * This applies for Low/Full speed devices under High speed hub only. Other
|
---|
| 126 | * devices just inherit TT from the hub.
|
---|
| 127 | *
|
---|
| 128 | * Roothub must be handled specially.
|
---|
| 129 | */
|
---|
| 130 | void hcd_setup_device_tt(device_t *dev)
|
---|
| 131 | {
|
---|
| 132 | if (!dev->hub)
|
---|
| 133 | return;
|
---|
| 134 |
|
---|
| 135 | if (dev->hub->speed == USB_SPEED_HIGH && usb_speed_is_11(dev->speed)) {
|
---|
| 136 | /* For LS devices under HS hub */
|
---|
| 137 | dev->tt.address = dev->hub->address;
|
---|
| 138 | dev->tt.port = dev->port;
|
---|
| 139 | }
|
---|
| 140 | else {
|
---|
| 141 | /* Inherit hub's TT */
|
---|
| 142 | dev->tt = dev->hub->tt;
|
---|
| 143 | }
|
---|
| 144 | }
|
---|
[306a36d] | 145 |
|
---|
[d9b2c73] | 146 | /** Prepare generic usb_transfer_batch and schedule it.
|
---|
| 147 | * @param hcd Host controller driver.
|
---|
| 148 | * @param target address and endpoint number.
|
---|
| 149 | * @param setup_data Data to use in setup stage (Control communication type)
|
---|
| 150 | * @param in Callback for device to host communication.
|
---|
| 151 | * @param out Callback for host to device communication.
|
---|
| 152 | * @param arg Callback parameter.
|
---|
| 153 | * @param name Communication identifier (for nicer output).
|
---|
| 154 | * @return Error code.
|
---|
| 155 | */
|
---|
[327f147] | 156 | int hcd_send_batch(hcd_t *hcd, device_t *device, usb_target_t target,
|
---|
| 157 | usb_direction_t direction, char *data, size_t size, uint64_t setup_data,
|
---|
[f9d0a86] | 158 | usbhc_iface_transfer_callback_t on_complete, void *arg, const char *name)
|
---|
[d9b2c73] | 159 | {
|
---|
| 160 | assert(hcd);
|
---|
[327f147] | 161 | assert(device->address == target.address);
|
---|
[d9b2c73] | 162 |
|
---|
[56db65d] | 163 | if (!hcd->ops.schedule) {
|
---|
| 164 | usb_log_error("HCD does not implement scheduler.\n");
|
---|
| 165 | return ENOTSUP;
|
---|
| 166 | }
|
---|
| 167 |
|
---|
[327f147] | 168 | endpoint_t *ep = bus_find_endpoint(hcd->bus, device, target, direction);
|
---|
[d9b2c73] | 169 | if (ep == NULL) {
|
---|
| 170 | usb_log_error("Endpoint(%d:%d) not registered for %s.\n",
|
---|
[327f147] | 171 | device->address, target.endpoint, name);
|
---|
[d9b2c73] | 172 | return ENOENT;
|
---|
| 173 | }
|
---|
| 174 |
|
---|
[56db65d] | 175 | // TODO cut here aka provide helper to call with instance of endpoint_t in hand
|
---|
| 176 |
|
---|
[d9b2c73] | 177 | usb_log_debug2("%s %d:%d %zu(%zu).\n",
|
---|
| 178 | name, target.address, target.endpoint, size, ep->max_packet_size);
|
---|
| 179 |
|
---|
[41924f30] | 180 | const size_t bw = bus_count_bw(ep, size);
|
---|
[d9b2c73] | 181 | /* Check if we have enough bandwidth reserved */
|
---|
| 182 | if (ep->bandwidth < bw) {
|
---|
| 183 | usb_log_error("Endpoint(%d:%d) %s needs %zu bw "
|
---|
| 184 | "but only %zu is reserved.\n",
|
---|
[a5b3de6] | 185 | device->address, ep->endpoint, name, bw, ep->bandwidth);
|
---|
[d9b2c73] | 186 | return ENOSPC;
|
---|
| 187 | }
|
---|
| 188 |
|
---|
[5fd9c30] | 189 | usb_transfer_batch_t *batch = usb_transfer_batch_create(ep);
|
---|
[d9b2c73] | 190 | if (!batch) {
|
---|
[bbd68a4] | 191 | usb_log_error("Failed to create transfer batch.\n");
|
---|
[d9b2c73] | 192 | return ENOMEM;
|
---|
| 193 | }
|
---|
| 194 |
|
---|
[a5b3de6] | 195 | batch->target = target;
|
---|
[5fd9c30] | 196 | batch->buffer = data;
|
---|
| 197 | batch->buffer_size = size;
|
---|
| 198 | batch->setup.packed = setup_data;
|
---|
| 199 | batch->dir = direction;
|
---|
[327f147] | 200 | batch->on_complete = on_complete;
|
---|
| 201 | batch->on_complete_data = arg;
|
---|
[5fd9c30] | 202 |
|
---|
| 203 | /* Check for commands that reset toggle bit */
|
---|
| 204 | if (ep->transfer_type == USB_TRANSFER_CONTROL)
|
---|
| 205 | batch->toggle_reset_mode
|
---|
| 206 | = usb_request_get_toggle_reset_mode(&batch->setup.packet);
|
---|
| 207 |
|
---|
[b5f813c] | 208 | const int ret = hcd->ops.schedule(hcd, batch);
|
---|
[2b61945] | 209 | if (ret != EOK) {
|
---|
| 210 | usb_log_warning("Batch %p failed to schedule: %s", batch, str_error(ret));
|
---|
[d9b2c73] | 211 | usb_transfer_batch_destroy(batch);
|
---|
[2b61945] | 212 | }
|
---|
[d9b2c73] | 213 |
|
---|
[f527f58] | 214 | /* Drop our own reference to ep. */
|
---|
| 215 | endpoint_del_ref(ep);
|
---|
| 216 |
|
---|
[d9b2c73] | 217 | return ret;
|
---|
| 218 | }
|
---|
| 219 |
|
---|
[237df2f] | 220 | typedef struct {
|
---|
[ae03552e] | 221 | fibril_mutex_t done_mtx;
|
---|
| 222 | fibril_condvar_t done_cv;
|
---|
| 223 | unsigned done;
|
---|
[327f147] | 224 |
|
---|
| 225 | size_t transfered_size;
|
---|
| 226 | int error;
|
---|
[237df2f] | 227 | } sync_data_t;
|
---|
| 228 |
|
---|
[f9d0a86] | 229 | static int sync_transfer_complete(void *arg, int error, size_t transfered_size)
|
---|
[237df2f] | 230 | {
|
---|
[f9d0a86] | 231 | sync_data_t *d = arg;
|
---|
[237df2f] | 232 | assert(d);
|
---|
[f9d0a86] | 233 | d->transfered_size = transfered_size;
|
---|
| 234 | d->error = error;
|
---|
[ae03552e] | 235 | fibril_mutex_lock(&d->done_mtx);
|
---|
[237df2f] | 236 | d->done = 1;
|
---|
[ae03552e] | 237 | fibril_condvar_broadcast(&d->done_cv);
|
---|
| 238 | fibril_mutex_unlock(&d->done_mtx);
|
---|
[327f147] | 239 | return EOK;
|
---|
[237df2f] | 240 | }
|
---|
| 241 |
|
---|
[327f147] | 242 | ssize_t hcd_send_batch_sync(hcd_t *hcd, device_t *device, usb_target_t target,
|
---|
| 243 | usb_direction_t direction, char *data, size_t size, uint64_t setup_data,
|
---|
| 244 | const char *name)
|
---|
[237df2f] | 245 | {
|
---|
| 246 | assert(hcd);
|
---|
[327f147] | 247 | sync_data_t sd = { .done = 0 };
|
---|
[ae03552e] | 248 | fibril_mutex_initialize(&sd.done_mtx);
|
---|
| 249 | fibril_condvar_initialize(&sd.done_cv);
|
---|
[237df2f] | 250 |
|
---|
[327f147] | 251 | const int ret = hcd_send_batch(hcd, device, target, direction,
|
---|
| 252 | data, size, setup_data,
|
---|
| 253 | sync_transfer_complete, &sd, name);
|
---|
[237df2f] | 254 | if (ret != EOK)
|
---|
| 255 | return ret;
|
---|
[bbd68a4] | 256 |
|
---|
[ae03552e] | 257 | fibril_mutex_lock(&sd.done_mtx);
|
---|
| 258 | while (!sd.done)
|
---|
| 259 | fibril_condvar_wait(&sd.done_cv, &sd.done_mtx);
|
---|
| 260 | fibril_mutex_unlock(&sd.done_mtx);
|
---|
[bbd68a4] | 261 |
|
---|
[327f147] | 262 | return (sd.error == EOK)
|
---|
| 263 | ? (ssize_t) sd.transfered_size
|
---|
| 264 | : (ssize_t) sd.error;
|
---|
[237df2f] | 265 | }
|
---|
| 266 |
|
---|
| 267 |
|
---|
[4daee7a] | 268 | /**
|
---|
| 269 | * @}
|
---|
| 270 | */
|
---|