| 1 | /*
|
|---|
| 2 | * Copyright (c) 2017 Ondrej Hlavaty <aearsis@eideo.cz>
|
|---|
| 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 | * The Bus is a structure that serves as an interface of the HC driver
|
|---|
| 35 | * implementation for the usbhost library. Every HC driver that uses libusbhost
|
|---|
| 36 | * must use a bus_t (or its child), fill it with bus_ops and present it to the
|
|---|
| 37 | * library. The library then handles the DDF interface and translates it to the
|
|---|
| 38 | * bus callbacks.
|
|---|
| 39 | */
|
|---|
| 40 |
|
|---|
| 41 | #include <ddf/driver.h>
|
|---|
| 42 | #include <errno.h>
|
|---|
| 43 | #include <mem.h>
|
|---|
| 44 | #include <stdio.h>
|
|---|
| 45 | #include <usb/debug.h>
|
|---|
| 46 |
|
|---|
| 47 | #include "endpoint.h"
|
|---|
| 48 | #include "bus.h"
|
|---|
| 49 |
|
|---|
| 50 | /**
|
|---|
| 51 | * Initializes the base bus structure.
|
|---|
| 52 | */
|
|---|
| 53 | void bus_init(bus_t *bus, size_t device_size)
|
|---|
| 54 | {
|
|---|
| 55 | assert(bus);
|
|---|
| 56 | assert(device_size >= sizeof(device_t));
|
|---|
| 57 | memset(bus, 0, sizeof(bus_t));
|
|---|
| 58 |
|
|---|
| 59 | fibril_mutex_initialize(&bus->guard);
|
|---|
| 60 | bus->device_size = device_size;
|
|---|
| 61 | bus->default_address_speed = USB_SPEED_MAX;
|
|---|
| 62 | }
|
|---|
| 63 |
|
|---|
| 64 | /**
|
|---|
| 65 | * Initialize the device_t structure belonging to a bus.
|
|---|
| 66 | */
|
|---|
| 67 | int bus_device_init(device_t *dev, bus_t *bus)
|
|---|
| 68 | {
|
|---|
| 69 | assert(bus);
|
|---|
| 70 |
|
|---|
| 71 | memset(dev, 0, sizeof(*dev));
|
|---|
| 72 |
|
|---|
| 73 | dev->bus = bus;
|
|---|
| 74 |
|
|---|
| 75 | link_initialize(&dev->link);
|
|---|
| 76 | list_initialize(&dev->devices);
|
|---|
| 77 | fibril_mutex_initialize(&dev->guard);
|
|---|
| 78 |
|
|---|
| 79 | return EOK;
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 | /**
|
|---|
| 83 | * Create a name of the ddf function node.
|
|---|
| 84 | */
|
|---|
| 85 | int bus_device_set_default_name(device_t *dev)
|
|---|
| 86 | {
|
|---|
| 87 | assert(dev);
|
|---|
| 88 | assert(dev->fun);
|
|---|
| 89 |
|
|---|
| 90 | char buf[10] = { 0 }; /* usbxyz-ss */
|
|---|
| 91 | snprintf(buf, sizeof(buf) - 1, "usb%u-%cs",
|
|---|
| 92 | dev->address, usb_str_speed(dev->speed)[0]);
|
|---|
| 93 |
|
|---|
| 94 | return ddf_fun_set_name(dev->fun, buf);
|
|---|
| 95 | }
|
|---|
| 96 |
|
|---|
| 97 | /**
|
|---|
| 98 | * Invoke the device_enumerate bus operation.
|
|---|
| 99 | */
|
|---|
| 100 | int bus_device_enumerate(device_t *dev)
|
|---|
| 101 | {
|
|---|
| 102 | assert(dev);
|
|---|
| 103 |
|
|---|
| 104 | const bus_ops_t *ops = BUS_OPS_LOOKUP(dev->bus->ops, device_enumerate);
|
|---|
| 105 | if (!ops)
|
|---|
| 106 | return ENOTSUP;
|
|---|
| 107 |
|
|---|
| 108 | return ops->device_enumerate(dev);
|
|---|
| 109 | }
|
|---|
| 110 |
|
|---|
| 111 | /**
|
|---|
| 112 | * Invoke the device_remove bus operation.
|
|---|
| 113 | */
|
|---|
| 114 | int bus_device_remove(device_t *dev)
|
|---|
| 115 | {
|
|---|
| 116 | assert(dev);
|
|---|
| 117 |
|
|---|
| 118 | const bus_ops_t *ops = BUS_OPS_LOOKUP(dev->bus->ops, device_remove);
|
|---|
| 119 | if (!ops)
|
|---|
| 120 | return ENOTSUP;
|
|---|
| 121 |
|
|---|
| 122 | return ops->device_remove(dev);
|
|---|
| 123 | }
|
|---|
| 124 |
|
|---|
| 125 | /**
|
|---|
| 126 | * Invoke the device_online bus operation.
|
|---|
| 127 | */
|
|---|
| 128 | int bus_device_online(device_t *dev)
|
|---|
| 129 | {
|
|---|
| 130 | assert(dev);
|
|---|
| 131 |
|
|---|
| 132 | const bus_ops_t *ops = BUS_OPS_LOOKUP(dev->bus->ops, device_online);
|
|---|
| 133 | if (!ops)
|
|---|
| 134 | return ENOTSUP;
|
|---|
| 135 |
|
|---|
| 136 | return ops->device_online(dev);
|
|---|
| 137 | }
|
|---|
| 138 |
|
|---|
| 139 | /**
|
|---|
| 140 | * Invoke the device_offline bus operation.
|
|---|
| 141 | */
|
|---|
| 142 | int bus_device_offline(device_t *dev)
|
|---|
| 143 | {
|
|---|
| 144 | assert(dev);
|
|---|
| 145 |
|
|---|
| 146 | const bus_ops_t *ops = BUS_OPS_LOOKUP(dev->bus->ops, device_offline);
|
|---|
| 147 | if (!ops)
|
|---|
| 148 | return ENOTSUP;
|
|---|
| 149 |
|
|---|
| 150 | return ops->device_offline(dev);
|
|---|
| 151 | }
|
|---|
| 152 |
|
|---|
| 153 | /**
|
|---|
| 154 | * Create and register new endpoint to the bus.
|
|---|
| 155 | *
|
|---|
| 156 | * @param[in] device The device of which the endpoint shall be created
|
|---|
| 157 | * @param[in] desc Endpoint descriptors as reported by the device
|
|---|
| 158 | * @param[out] out_ep The resulting new endpoint reference, if any. Can be NULL.
|
|---|
| 159 | */
|
|---|
| 160 | int bus_endpoint_add(device_t *device, const usb_endpoint_descriptors_t *desc, endpoint_t **out_ep)
|
|---|
| 161 | {
|
|---|
| 162 | int err;
|
|---|
| 163 | assert(device);
|
|---|
| 164 |
|
|---|
| 165 | bus_t *bus = device->bus;
|
|---|
| 166 |
|
|---|
| 167 | const bus_ops_t *register_ops = BUS_OPS_LOOKUP(bus->ops, endpoint_register);
|
|---|
| 168 | if (!register_ops)
|
|---|
| 169 | return ENOTSUP;
|
|---|
| 170 |
|
|---|
| 171 | const bus_ops_t *create_ops = BUS_OPS_LOOKUP(bus->ops, endpoint_create);
|
|---|
| 172 | endpoint_t *ep;
|
|---|
| 173 | if (create_ops) {
|
|---|
| 174 | ep = create_ops->endpoint_create(device, desc);
|
|---|
| 175 | if (!ep)
|
|---|
| 176 | return ENOMEM;
|
|---|
| 177 | } else {
|
|---|
| 178 | ep = calloc(1, sizeof(endpoint_t));
|
|---|
| 179 | if (!ep)
|
|---|
| 180 | return ENOMEM;
|
|---|
| 181 | endpoint_init(ep, device, desc);
|
|---|
| 182 | }
|
|---|
| 183 |
|
|---|
| 184 | /* Bus reference */
|
|---|
| 185 | endpoint_add_ref(ep);
|
|---|
| 186 |
|
|---|
| 187 | if (ep->max_transfer_size == 0) {
|
|---|
| 188 | usb_log_warning("Invalid endpoint description (mps %zu, "
|
|---|
| 189 | "%u packets)", ep->max_packet_size, ep->packets_per_uframe);
|
|---|
| 190 | /* Bus reference */
|
|---|
| 191 | endpoint_del_ref(ep);
|
|---|
| 192 | return EINVAL;
|
|---|
| 193 | }
|
|---|
| 194 |
|
|---|
| 195 | usb_log_debug("Register endpoint %d:%d %s-%s %zuB.\n",
|
|---|
| 196 | device->address, ep->endpoint,
|
|---|
| 197 | usb_str_transfer_type(ep->transfer_type),
|
|---|
| 198 | usb_str_direction(ep->direction),
|
|---|
| 199 | ep->max_transfer_size);
|
|---|
| 200 |
|
|---|
| 201 | fibril_mutex_lock(&bus->guard);
|
|---|
| 202 | if (!device->online && ep->endpoint != 0) {
|
|---|
| 203 | err = EAGAIN;
|
|---|
| 204 | } else if (device->endpoints[ep->endpoint] != NULL) {
|
|---|
| 205 | err = EEXIST;
|
|---|
| 206 | } else {
|
|---|
| 207 | err = register_ops->endpoint_register(ep);
|
|---|
| 208 | if (!err)
|
|---|
| 209 | device->endpoints[ep->endpoint] = ep;
|
|---|
| 210 | }
|
|---|
| 211 | fibril_mutex_unlock(&bus->guard);
|
|---|
| 212 | if (err) {
|
|---|
| 213 | endpoint_del_ref(ep);
|
|---|
| 214 | return err;
|
|---|
| 215 | }
|
|---|
| 216 |
|
|---|
| 217 | if (out_ep) {
|
|---|
| 218 | /* Exporting reference */
|
|---|
| 219 | endpoint_add_ref(ep);
|
|---|
| 220 | *out_ep = ep;
|
|---|
| 221 | }
|
|---|
| 222 |
|
|---|
| 223 | return EOK;
|
|---|
| 224 | }
|
|---|
| 225 |
|
|---|
| 226 | /**
|
|---|
| 227 | * Search for an endpoint. Returns a reference.
|
|---|
| 228 | */
|
|---|
| 229 | endpoint_t *bus_find_endpoint(device_t *device, usb_endpoint_t endpoint)
|
|---|
| 230 | {
|
|---|
| 231 | assert(device);
|
|---|
| 232 |
|
|---|
| 233 | bus_t *bus = device->bus;
|
|---|
| 234 |
|
|---|
| 235 | fibril_mutex_lock(&bus->guard);
|
|---|
| 236 | endpoint_t *ep = device->endpoints[endpoint];
|
|---|
| 237 | if (ep) {
|
|---|
| 238 | /* Exporting reference */
|
|---|
| 239 | endpoint_add_ref(ep);
|
|---|
| 240 | }
|
|---|
| 241 | fibril_mutex_unlock(&bus->guard);
|
|---|
| 242 | return ep;
|
|---|
| 243 | }
|
|---|
| 244 |
|
|---|
| 245 | /**
|
|---|
| 246 | * Remove an endpoint from the device. Consumes a reference.
|
|---|
| 247 | */
|
|---|
| 248 | int bus_endpoint_remove(endpoint_t *ep)
|
|---|
| 249 | {
|
|---|
| 250 | assert(ep);
|
|---|
| 251 | assert(ep->device);
|
|---|
| 252 |
|
|---|
| 253 | bus_t *bus = endpoint_get_bus(ep);
|
|---|
| 254 |
|
|---|
| 255 | const bus_ops_t *ops = BUS_OPS_LOOKUP(bus->ops, endpoint_unregister);
|
|---|
| 256 | if (!ops)
|
|---|
| 257 | return ENOTSUP;
|
|---|
| 258 |
|
|---|
| 259 | usb_log_debug("Unregister endpoint %d:%d %s-%s %zuB.\n",
|
|---|
| 260 | ep->device->address, ep->endpoint,
|
|---|
| 261 | usb_str_transfer_type(ep->transfer_type),
|
|---|
| 262 | usb_str_direction(ep->direction),
|
|---|
| 263 | ep->max_transfer_size);
|
|---|
| 264 |
|
|---|
| 265 | fibril_mutex_lock(&bus->guard);
|
|---|
| 266 | const int r = ops->endpoint_unregister(ep);
|
|---|
| 267 | if (!r)
|
|---|
| 268 | ep->device->endpoints[ep->endpoint] = NULL;
|
|---|
| 269 | fibril_mutex_unlock(&bus->guard);
|
|---|
| 270 |
|
|---|
| 271 | if (r)
|
|---|
| 272 | return r;
|
|---|
| 273 |
|
|---|
| 274 | /* Bus reference */
|
|---|
| 275 | endpoint_del_ref(ep);
|
|---|
| 276 |
|
|---|
| 277 | /* Given reference */
|
|---|
| 278 | endpoint_del_ref(ep);
|
|---|
| 279 |
|
|---|
| 280 | return EOK;
|
|---|
| 281 | }
|
|---|
| 282 |
|
|---|
| 283 | /**
|
|---|
| 284 | * Reserve the default address on the bus. Also, report the speed of the device
|
|---|
| 285 | * that is listening on the default address.
|
|---|
| 286 | *
|
|---|
| 287 | * The speed is then used for devices enumerated while the address is reserved.
|
|---|
| 288 | */
|
|---|
| 289 | int bus_reserve_default_address(bus_t *bus, usb_speed_t speed)
|
|---|
| 290 | {
|
|---|
| 291 | assert(bus);
|
|---|
| 292 |
|
|---|
| 293 | fibril_mutex_lock(&bus->guard);
|
|---|
| 294 | if (bus->default_address_speed != USB_SPEED_MAX) {
|
|---|
| 295 | fibril_mutex_unlock(&bus->guard);
|
|---|
| 296 | return EAGAIN;
|
|---|
| 297 | } else {
|
|---|
| 298 | bus->default_address_speed = speed;
|
|---|
| 299 | fibril_mutex_unlock(&bus->guard);
|
|---|
| 300 | return EOK;
|
|---|
| 301 | }
|
|---|
| 302 | }
|
|---|
| 303 |
|
|---|
| 304 | /**
|
|---|
| 305 | * Release the default address.
|
|---|
| 306 | */
|
|---|
| 307 | void bus_release_default_address(bus_t *bus)
|
|---|
| 308 | {
|
|---|
| 309 | assert(bus);
|
|---|
| 310 | bus->default_address_speed = USB_SPEED_MAX;
|
|---|
| 311 | }
|
|---|
| 312 |
|
|---|
| 313 | /**
|
|---|
| 314 | * Initiate a transfer on the bus. Finds the target endpoint, creates
|
|---|
| 315 | * a transfer batch and schedules it.
|
|---|
| 316 | *
|
|---|
| 317 | * @param device Device for which to send the batch
|
|---|
| 318 | * @param target The target of the transfer.
|
|---|
| 319 | * @param direction A direction of the transfer.
|
|---|
| 320 | * @param data A pointer to the data buffer.
|
|---|
| 321 | * @param size Size of the data buffer.
|
|---|
| 322 | * @param setup_data Data to use in the setup stage (Control communication type)
|
|---|
| 323 | * @param on_complete Callback which is called after the batch is complete
|
|---|
| 324 | * @param arg Callback parameter.
|
|---|
| 325 | * @param name Communication identifier (for nicer output).
|
|---|
| 326 | * @return Error code.
|
|---|
| 327 | */
|
|---|
| 328 | int bus_device_send_batch(device_t *device, usb_target_t target,
|
|---|
| 329 | usb_direction_t direction, char *data, size_t size, uint64_t setup_data,
|
|---|
| 330 | usbhc_iface_transfer_callback_t on_complete, void *arg, const char *name)
|
|---|
| 331 | {
|
|---|
| 332 | assert(device->address == target.address);
|
|---|
| 333 |
|
|---|
| 334 | /* Temporary reference */
|
|---|
| 335 | endpoint_t *ep = bus_find_endpoint(device, target.endpoint);
|
|---|
| 336 | if (ep == NULL) {
|
|---|
| 337 | usb_log_error("Endpoint(%d:%d) not registered for %s.\n",
|
|---|
| 338 | device->address, target.endpoint, name);
|
|---|
| 339 | return ENOENT;
|
|---|
| 340 | }
|
|---|
| 341 |
|
|---|
| 342 | assert(ep->device == device);
|
|---|
| 343 |
|
|---|
| 344 | const int err = endpoint_send_batch(ep, target, direction, data, size, setup_data,
|
|---|
| 345 | on_complete, arg, name);
|
|---|
| 346 |
|
|---|
| 347 | /* Temporary reference */
|
|---|
| 348 | endpoint_del_ref(ep);
|
|---|
| 349 |
|
|---|
| 350 | return err;
|
|---|
| 351 | }
|
|---|
| 352 |
|
|---|
| 353 | typedef struct {
|
|---|
| 354 | fibril_mutex_t done_mtx;
|
|---|
| 355 | fibril_condvar_t done_cv;
|
|---|
| 356 | unsigned done;
|
|---|
| 357 |
|
|---|
| 358 | size_t transfered_size;
|
|---|
| 359 | int error;
|
|---|
| 360 | } sync_data_t;
|
|---|
| 361 |
|
|---|
| 362 | /**
|
|---|
| 363 | * Callback for finishing the transfer. Wake the issuing thread.
|
|---|
| 364 | */
|
|---|
| 365 | static int sync_transfer_complete(void *arg, int error, size_t transfered_size)
|
|---|
| 366 | {
|
|---|
| 367 | sync_data_t *d = arg;
|
|---|
| 368 | assert(d);
|
|---|
| 369 | d->transfered_size = transfered_size;
|
|---|
| 370 | d->error = error;
|
|---|
| 371 | fibril_mutex_lock(&d->done_mtx);
|
|---|
| 372 | d->done = 1;
|
|---|
| 373 | fibril_condvar_broadcast(&d->done_cv);
|
|---|
| 374 | fibril_mutex_unlock(&d->done_mtx);
|
|---|
| 375 | return EOK;
|
|---|
| 376 | }
|
|---|
| 377 |
|
|---|
| 378 | /**
|
|---|
| 379 | * Issue a transfer on the bus, wait for result.
|
|---|
| 380 | *
|
|---|
| 381 | * @param device Device for which to send the batch
|
|---|
| 382 | * @param target The target of the transfer.
|
|---|
| 383 | * @param direction A direction of the transfer.
|
|---|
| 384 | * @param data A pointer to the data buffer.
|
|---|
| 385 | * @param size Size of the data buffer.
|
|---|
| 386 | * @param setup_data Data to use in the setup stage (Control communication type)
|
|---|
| 387 | * @param name Communication identifier (for nicer output).
|
|---|
| 388 | */
|
|---|
| 389 | ssize_t bus_device_send_batch_sync(device_t *device, usb_target_t target,
|
|---|
| 390 | usb_direction_t direction, char *data, size_t size, uint64_t setup_data,
|
|---|
| 391 | const char *name)
|
|---|
| 392 | {
|
|---|
| 393 | sync_data_t sd = { .done = 0 };
|
|---|
| 394 | fibril_mutex_initialize(&sd.done_mtx);
|
|---|
| 395 | fibril_condvar_initialize(&sd.done_cv);
|
|---|
| 396 |
|
|---|
| 397 | const int ret = bus_device_send_batch(device, target, direction,
|
|---|
| 398 | data, size, setup_data,
|
|---|
| 399 | sync_transfer_complete, &sd, name);
|
|---|
| 400 | if (ret != EOK)
|
|---|
| 401 | return ret;
|
|---|
| 402 |
|
|---|
| 403 | fibril_mutex_lock(&sd.done_mtx);
|
|---|
| 404 | while (!sd.done) {
|
|---|
| 405 | fibril_condvar_wait(&sd.done_cv, &sd.done_mtx);
|
|---|
| 406 | }
|
|---|
| 407 | fibril_mutex_unlock(&sd.done_mtx);
|
|---|
| 408 |
|
|---|
| 409 | return (sd.error == EOK)
|
|---|
| 410 | ? (ssize_t) sd.transfered_size
|
|---|
| 411 | : (ssize_t) sd.error;
|
|---|
| 412 | }
|
|---|
| 413 |
|
|---|
| 414 | /**
|
|---|
| 415 | * @}
|
|---|
| 416 | */
|
|---|