| 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 | */
|
|---|
| 35 |
|
|---|
| 36 | #include <usb/host/bus.h>
|
|---|
| 37 | #include <usb/host/endpoint.h>
|
|---|
| 38 |
|
|---|
| 39 | #include <mem.h>
|
|---|
| 40 | #include <errno.h>
|
|---|
| 41 |
|
|---|
| 42 | /**
|
|---|
| 43 | * Initializes the bus structure.
|
|---|
| 44 | */
|
|---|
| 45 | void bus_init(bus_t *bus, hcd_t *hcd)
|
|---|
| 46 | {
|
|---|
| 47 | memset(bus, 0, sizeof(bus_t));
|
|---|
| 48 |
|
|---|
| 49 | bus->hcd = hcd;
|
|---|
| 50 | fibril_mutex_initialize(&bus->guard);
|
|---|
| 51 | }
|
|---|
| 52 |
|
|---|
| 53 | endpoint_t *bus_create_endpoint(bus_t *bus)
|
|---|
| 54 | {
|
|---|
| 55 | assert(bus);
|
|---|
| 56 |
|
|---|
| 57 | fibril_mutex_lock(&bus->guard);
|
|---|
| 58 | endpoint_t *ep = bus->ops.create_endpoint(bus);
|
|---|
| 59 | if (ep) {
|
|---|
| 60 | /* Exporting reference */
|
|---|
| 61 | endpoint_add_ref(ep);
|
|---|
| 62 | }
|
|---|
| 63 | fibril_mutex_unlock(&bus->guard);
|
|---|
| 64 |
|
|---|
| 65 | return ep;
|
|---|
| 66 | }
|
|---|
| 67 |
|
|---|
| 68 | int bus_register_endpoint(bus_t *bus, endpoint_t *ep)
|
|---|
| 69 | {
|
|---|
| 70 | assert(bus);
|
|---|
| 71 | assert(ep);
|
|---|
| 72 |
|
|---|
| 73 | /* Bus reference */
|
|---|
| 74 | endpoint_add_ref(ep);
|
|---|
| 75 |
|
|---|
| 76 | fibril_mutex_lock(&bus->guard);
|
|---|
| 77 | const int r = bus->ops.register_endpoint(bus, ep);
|
|---|
| 78 | fibril_mutex_unlock(&bus->guard);
|
|---|
| 79 |
|
|---|
| 80 | return r;
|
|---|
| 81 | }
|
|---|
| 82 |
|
|---|
| 83 | int bus_release_endpoint(bus_t *bus, endpoint_t *ep)
|
|---|
| 84 | {
|
|---|
| 85 | int err;
|
|---|
| 86 |
|
|---|
| 87 | assert(bus);
|
|---|
| 88 | assert(ep);
|
|---|
| 89 |
|
|---|
| 90 | fibril_mutex_lock(&bus->guard);
|
|---|
| 91 | if ((err = bus->ops.release_endpoint(bus, ep)))
|
|---|
| 92 | return err;
|
|---|
| 93 | fibril_mutex_unlock(&bus->guard);
|
|---|
| 94 |
|
|---|
| 95 | /* Bus reference */
|
|---|
| 96 | endpoint_del_ref(ep);
|
|---|
| 97 |
|
|---|
| 98 | return EOK;
|
|---|
| 99 | }
|
|---|
| 100 |
|
|---|
| 101 | /** Searches for an endpoint. Returns a reference.
|
|---|
| 102 | */
|
|---|
| 103 | endpoint_t *bus_find_endpoint(bus_t *bus, usb_target_t target, usb_direction_t dir)
|
|---|
| 104 | {
|
|---|
| 105 | assert(bus);
|
|---|
| 106 |
|
|---|
| 107 | fibril_mutex_lock(&bus->guard);
|
|---|
| 108 | endpoint_t *ep = bus->ops.find_endpoint(bus, target, dir);
|
|---|
| 109 | if (ep) {
|
|---|
| 110 | /* Exporting reference */
|
|---|
| 111 | endpoint_add_ref(ep);
|
|---|
| 112 | }
|
|---|
| 113 |
|
|---|
| 114 | fibril_mutex_unlock(&bus->guard);
|
|---|
| 115 | return ep;
|
|---|
| 116 | }
|
|---|
| 117 |
|
|---|
| 118 | int bus_request_address(bus_t *bus, usb_address_t *hint, bool strict, usb_speed_t speed)
|
|---|
| 119 | {
|
|---|
| 120 | assert(bus);
|
|---|
| 121 |
|
|---|
| 122 | if (!bus->ops.request_address)
|
|---|
| 123 | return ENOTSUP;
|
|---|
| 124 |
|
|---|
| 125 | fibril_mutex_lock(&bus->guard);
|
|---|
| 126 | const int r = bus->ops.request_address(bus, hint, strict, speed);
|
|---|
| 127 | fibril_mutex_unlock(&bus->guard);
|
|---|
| 128 | return r;
|
|---|
| 129 | }
|
|---|
| 130 |
|
|---|
| 131 | int bus_release_address(bus_t *bus, usb_address_t address)
|
|---|
| 132 | {
|
|---|
| 133 | assert(bus);
|
|---|
| 134 |
|
|---|
| 135 | if (!bus->ops.release_address)
|
|---|
| 136 | return ENOTSUP;
|
|---|
| 137 |
|
|---|
| 138 | fibril_mutex_lock(&bus->guard);
|
|---|
| 139 | const int r = bus->ops.release_address(bus, address);
|
|---|
| 140 | fibril_mutex_unlock(&bus->guard);
|
|---|
| 141 | return r;
|
|---|
| 142 | }
|
|---|
| 143 |
|
|---|
| 144 | int bus_get_speed(bus_t *bus, usb_address_t address, usb_speed_t *speed)
|
|---|
| 145 | {
|
|---|
| 146 | assert(bus);
|
|---|
| 147 | assert(speed);
|
|---|
| 148 |
|
|---|
| 149 | if (!bus->ops.get_speed)
|
|---|
| 150 | return ENOTSUP;
|
|---|
| 151 |
|
|---|
| 152 | fibril_mutex_lock(&bus->guard);
|
|---|
| 153 | const int r = bus->ops.get_speed(bus, address, speed);
|
|---|
| 154 | fibril_mutex_unlock(&bus->guard);
|
|---|
| 155 | return r;
|
|---|
| 156 | }
|
|---|
| 157 |
|
|---|
| 158 | int bus_reset_toggle(bus_t *bus, usb_target_t target, bool all)
|
|---|
| 159 | {
|
|---|
| 160 | assert(bus);
|
|---|
| 161 |
|
|---|
| 162 | if (!bus->ops.reset_toggle)
|
|---|
| 163 | return ENOTSUP;
|
|---|
| 164 |
|
|---|
| 165 | fibril_mutex_lock(&bus->guard);
|
|---|
| 166 | const int r = bus->ops.reset_toggle(bus, target, all);
|
|---|
| 167 | fibril_mutex_unlock(&bus->guard);
|
|---|
| 168 | return r;
|
|---|
| 169 | }
|
|---|
| 170 |
|
|---|
| 171 | size_t bus_count_bw(endpoint_t *ep, size_t size)
|
|---|
| 172 | {
|
|---|
| 173 | assert(ep);
|
|---|
| 174 |
|
|---|
| 175 | fibril_mutex_lock(&ep->guard);
|
|---|
| 176 | const size_t bw = ep->bus->ops.count_bw(ep, size);
|
|---|
| 177 | fibril_mutex_unlock(&ep->guard);
|
|---|
| 178 | return bw;
|
|---|
| 179 | }
|
|---|
| 180 |
|
|---|
| 181 | /**
|
|---|
| 182 | * @}
|
|---|
| 183 | */
|
|---|