| 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 libusb
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /** @file
|
|---|
| 33 | * Device keeper structure and functions (implementation).
|
|---|
| 34 | */
|
|---|
| 35 | #include <assert.h>
|
|---|
| 36 | #include <errno.h>
|
|---|
| 37 | #include <usb/debug.h>
|
|---|
| 38 | #include <usb/host/device_keeper.h>
|
|---|
| 39 |
|
|---|
| 40 | /*----------------------------------------------------------------------------*/
|
|---|
| 41 | /** Initialize device keeper structure.
|
|---|
| 42 | *
|
|---|
| 43 | * @param[in] instance Memory place to initialize.
|
|---|
| 44 | *
|
|---|
| 45 | * Set all values to false/0.
|
|---|
| 46 | */
|
|---|
| 47 | void usb_device_keeper_init(usb_device_keeper_t *instance)
|
|---|
| 48 | {
|
|---|
| 49 | assert(instance);
|
|---|
| 50 | fibril_mutex_initialize(&instance->guard);
|
|---|
| 51 | fibril_condvar_initialize(&instance->change);
|
|---|
| 52 | instance->last_address = 0;
|
|---|
| 53 | unsigned i = 0;
|
|---|
| 54 | for (; i < USB_ADDRESS_COUNT; ++i) {
|
|---|
| 55 | instance->devices[i].occupied = false;
|
|---|
| 56 | instance->devices[i].control_used = 0;
|
|---|
| 57 | instance->devices[i].handle = 0;
|
|---|
| 58 | list_initialize(&instance->devices[i].endpoints);
|
|---|
| 59 | }
|
|---|
| 60 | }
|
|---|
| 61 | /*----------------------------------------------------------------------------*/
|
|---|
| 62 | void usb_device_keeper_add_ep(
|
|---|
| 63 | usb_device_keeper_t *instance, usb_address_t address, endpoint_t *ep)
|
|---|
| 64 | {
|
|---|
| 65 | assert(instance);
|
|---|
| 66 | fibril_mutex_lock(&instance->guard);
|
|---|
| 67 | assert(instance->devices[address].occupied);
|
|---|
| 68 | list_append(&ep->same_device_eps, &instance->devices[address].endpoints);
|
|---|
| 69 | fibril_mutex_unlock(&instance->guard);
|
|---|
| 70 | }
|
|---|
| 71 | /*----------------------------------------------------------------------------*/
|
|---|
| 72 | /** Attempt to obtain address 0, blocks.
|
|---|
| 73 | *
|
|---|
| 74 | * @param[in] instance Device keeper structure to use.
|
|---|
| 75 | * @param[in] speed Speed of the device requesting default address.
|
|---|
| 76 | */
|
|---|
| 77 | void usb_device_keeper_reserve_default_address(
|
|---|
| 78 | usb_device_keeper_t *instance, usb_speed_t speed)
|
|---|
| 79 | {
|
|---|
| 80 | assert(instance);
|
|---|
| 81 | fibril_mutex_lock(&instance->guard);
|
|---|
| 82 | while (instance->devices[USB_ADDRESS_DEFAULT].occupied) {
|
|---|
| 83 | fibril_condvar_wait(&instance->change, &instance->guard);
|
|---|
| 84 | }
|
|---|
| 85 | instance->devices[USB_ADDRESS_DEFAULT].occupied = true;
|
|---|
| 86 | instance->devices[USB_ADDRESS_DEFAULT].speed = speed;
|
|---|
| 87 | fibril_mutex_unlock(&instance->guard);
|
|---|
| 88 | }
|
|---|
| 89 | /*----------------------------------------------------------------------------*/
|
|---|
| 90 | /** Attempt to obtain address 0, blocks.
|
|---|
| 91 | *
|
|---|
| 92 | * @param[in] instance Device keeper structure to use.
|
|---|
| 93 | * @param[in] speed Speed of the device requesting default address.
|
|---|
| 94 | */
|
|---|
| 95 | void usb_device_keeper_release_default_address(usb_device_keeper_t *instance)
|
|---|
| 96 | {
|
|---|
| 97 | assert(instance);
|
|---|
| 98 | fibril_mutex_lock(&instance->guard);
|
|---|
| 99 | instance->devices[USB_ADDRESS_DEFAULT].occupied = false;
|
|---|
| 100 | fibril_mutex_unlock(&instance->guard);
|
|---|
| 101 | fibril_condvar_signal(&instance->change);
|
|---|
| 102 | }
|
|---|
| 103 | /*----------------------------------------------------------------------------*/
|
|---|
| 104 | /** Check setup packet data for signs of toggle reset.
|
|---|
| 105 | *
|
|---|
| 106 | * @param[in] instance Device keeper structure to use.
|
|---|
| 107 | * @param[in] target Device to receive setup packet.
|
|---|
| 108 | * @param[in] data Setup packet data.
|
|---|
| 109 | *
|
|---|
| 110 | * Really ugly one.
|
|---|
| 111 | */
|
|---|
| 112 | void usb_device_keeper_reset_if_need(
|
|---|
| 113 | usb_device_keeper_t *instance, usb_target_t target, const uint8_t *data)
|
|---|
| 114 | {
|
|---|
| 115 | assert(instance);
|
|---|
| 116 | fibril_mutex_lock(&instance->guard);
|
|---|
| 117 | if (target.endpoint > 15 || target.endpoint < 0
|
|---|
| 118 | || target.address >= USB_ADDRESS_COUNT || target.address < 0
|
|---|
| 119 | || !instance->devices[target.address].occupied) {
|
|---|
| 120 | fibril_mutex_unlock(&instance->guard);
|
|---|
| 121 | usb_log_error("Invalid data when checking for toggle reset.\n");
|
|---|
| 122 | return;
|
|---|
| 123 | }
|
|---|
| 124 |
|
|---|
| 125 | switch (data[1])
|
|---|
| 126 | {
|
|---|
| 127 | case 0x01: /*clear feature*/
|
|---|
| 128 | /* recipient is endpoint, value is zero (ENDPOINT_STALL) */
|
|---|
| 129 | if (((data[0] & 0xf) == 1) && ((data[2] | data[3]) == 0)) {
|
|---|
| 130 | /* endpoint number is < 16, thus first byte is enough */
|
|---|
| 131 | assert(!"NOT IMPLEMENTED!");
|
|---|
| 132 | }
|
|---|
| 133 | break;
|
|---|
| 134 |
|
|---|
| 135 | case 0x9: /* set configuration */
|
|---|
| 136 | case 0x11: /* set interface */
|
|---|
| 137 | /* target must be device */
|
|---|
| 138 | if ((data[0] & 0xf) == 0) {
|
|---|
| 139 | link_t *current =
|
|---|
| 140 | instance->devices[target.address].endpoints.next;
|
|---|
| 141 | while (current !=
|
|---|
| 142 | &instance->devices[target.address].endpoints)
|
|---|
| 143 | {
|
|---|
| 144 | endpoint_toggle_reset(current);
|
|---|
| 145 | current = current->next;
|
|---|
| 146 | }
|
|---|
| 147 | }
|
|---|
| 148 | break;
|
|---|
| 149 | }
|
|---|
| 150 | fibril_mutex_unlock(&instance->guard);
|
|---|
| 151 | }
|
|---|
| 152 | /*----------------------------------------------------------------------------*/
|
|---|
| 153 | /** Get a free USB address
|
|---|
| 154 | *
|
|---|
| 155 | * @param[in] instance Device keeper structure to use.
|
|---|
| 156 | * @param[in] speed Speed of the device requiring address.
|
|---|
| 157 | * @return Free address, or error code.
|
|---|
| 158 | */
|
|---|
| 159 | usb_address_t device_keeper_get_free_address(
|
|---|
| 160 | usb_device_keeper_t *instance, usb_speed_t speed)
|
|---|
| 161 | {
|
|---|
| 162 | assert(instance);
|
|---|
| 163 | fibril_mutex_lock(&instance->guard);
|
|---|
| 164 |
|
|---|
| 165 | usb_address_t new_address = instance->last_address;
|
|---|
| 166 | do {
|
|---|
| 167 | ++new_address;
|
|---|
| 168 | if (new_address > USB11_ADDRESS_MAX)
|
|---|
| 169 | new_address = 1;
|
|---|
| 170 | if (new_address == instance->last_address) {
|
|---|
| 171 | fibril_mutex_unlock(&instance->guard);
|
|---|
| 172 | return ENOSPC;
|
|---|
| 173 | }
|
|---|
| 174 | } while (instance->devices[new_address].occupied);
|
|---|
| 175 |
|
|---|
| 176 | assert(new_address != USB_ADDRESS_DEFAULT);
|
|---|
| 177 | assert(instance->devices[new_address].occupied == false);
|
|---|
| 178 | instance->devices[new_address].occupied = true;
|
|---|
| 179 | instance->devices[new_address].speed = speed;
|
|---|
| 180 | instance->last_address = new_address;
|
|---|
| 181 | fibril_mutex_unlock(&instance->guard);
|
|---|
| 182 | return new_address;
|
|---|
| 183 | }
|
|---|
| 184 | /*----------------------------------------------------------------------------*/
|
|---|
| 185 | /** Bind USB address to devman handle.
|
|---|
| 186 | *
|
|---|
| 187 | * @param[in] instance Device keeper structure to use.
|
|---|
| 188 | * @param[in] address Device address
|
|---|
| 189 | * @param[in] handle Devman handle of the device.
|
|---|
| 190 | */
|
|---|
| 191 | void usb_device_keeper_bind(usb_device_keeper_t *instance,
|
|---|
| 192 | usb_address_t address, devman_handle_t handle)
|
|---|
| 193 | {
|
|---|
| 194 | assert(instance);
|
|---|
| 195 | fibril_mutex_lock(&instance->guard);
|
|---|
| 196 | assert(address > 0);
|
|---|
| 197 | assert(address <= USB11_ADDRESS_MAX);
|
|---|
| 198 | assert(instance->devices[address].occupied);
|
|---|
| 199 | instance->devices[address].handle = handle;
|
|---|
| 200 | fibril_mutex_unlock(&instance->guard);
|
|---|
| 201 | }
|
|---|
| 202 | /*----------------------------------------------------------------------------*/
|
|---|
| 203 | /** Release used USB address.
|
|---|
| 204 | *
|
|---|
| 205 | * @param[in] instance Device keeper structure to use.
|
|---|
| 206 | * @param[in] address Device address
|
|---|
| 207 | */
|
|---|
| 208 | void usb_device_keeper_release(
|
|---|
| 209 | usb_device_keeper_t *instance, usb_address_t address)
|
|---|
| 210 | {
|
|---|
| 211 | assert(instance);
|
|---|
| 212 | assert(address > 0);
|
|---|
| 213 | assert(address <= USB11_ADDRESS_MAX);
|
|---|
| 214 |
|
|---|
| 215 | fibril_mutex_lock(&instance->guard);
|
|---|
| 216 | assert(instance->devices[address].occupied);
|
|---|
| 217 | instance->devices[address].occupied = false;
|
|---|
| 218 | fibril_mutex_unlock(&instance->guard);
|
|---|
| 219 | }
|
|---|
| 220 | /*----------------------------------------------------------------------------*/
|
|---|
| 221 | /** Find USB address associated with the device
|
|---|
| 222 | *
|
|---|
| 223 | * @param[in] instance Device keeper structure to use.
|
|---|
| 224 | * @param[in] handle Devman handle of the device seeking its address.
|
|---|
| 225 | * @return USB Address, or error code.
|
|---|
| 226 | */
|
|---|
| 227 | usb_address_t usb_device_keeper_find(
|
|---|
| 228 | usb_device_keeper_t *instance, devman_handle_t handle)
|
|---|
| 229 | {
|
|---|
| 230 | assert(instance);
|
|---|
| 231 | fibril_mutex_lock(&instance->guard);
|
|---|
| 232 | usb_address_t address = 1;
|
|---|
| 233 | while (address <= USB11_ADDRESS_MAX) {
|
|---|
| 234 | if (instance->devices[address].handle == handle) {
|
|---|
| 235 | fibril_mutex_unlock(&instance->guard);
|
|---|
| 236 | return address;
|
|---|
| 237 | }
|
|---|
| 238 | ++address;
|
|---|
| 239 | }
|
|---|
| 240 | fibril_mutex_unlock(&instance->guard);
|
|---|
| 241 | return ENOENT;
|
|---|
| 242 | }
|
|---|
| 243 | /*----------------------------------------------------------------------------*/
|
|---|
| 244 | /** Get speed associated with the address
|
|---|
| 245 | *
|
|---|
| 246 | * @param[in] instance Device keeper structure to use.
|
|---|
| 247 | * @param[in] address Address of the device.
|
|---|
| 248 | * @return USB speed.
|
|---|
| 249 | */
|
|---|
| 250 | usb_speed_t usb_device_keeper_get_speed(
|
|---|
| 251 | usb_device_keeper_t *instance, usb_address_t address)
|
|---|
| 252 | {
|
|---|
| 253 | assert(instance);
|
|---|
| 254 | assert(address >= 0);
|
|---|
| 255 | assert(address <= USB11_ADDRESS_MAX);
|
|---|
| 256 | return instance->devices[address].speed;
|
|---|
| 257 | }
|
|---|
| 258 | /*----------------------------------------------------------------------------*/
|
|---|
| 259 | void usb_device_keeper_use_control(
|
|---|
| 260 | usb_device_keeper_t *instance, usb_target_t target)
|
|---|
| 261 | {
|
|---|
| 262 | assert(instance);
|
|---|
| 263 | const uint16_t ep = 1 << target.endpoint;
|
|---|
| 264 | fibril_mutex_lock(&instance->guard);
|
|---|
| 265 | while (instance->devices[target.address].control_used & ep) {
|
|---|
| 266 | fibril_condvar_wait(&instance->change, &instance->guard);
|
|---|
| 267 | }
|
|---|
| 268 | instance->devices[target.address].control_used |= ep;
|
|---|
| 269 | fibril_mutex_unlock(&instance->guard);
|
|---|
| 270 | }
|
|---|
| 271 | /*----------------------------------------------------------------------------*/
|
|---|
| 272 | void usb_device_keeper_release_control(
|
|---|
| 273 | usb_device_keeper_t *instance, usb_target_t target)
|
|---|
| 274 | {
|
|---|
| 275 | assert(instance);
|
|---|
| 276 | const uint16_t ep = 1 << target.endpoint;
|
|---|
| 277 | fibril_mutex_lock(&instance->guard);
|
|---|
| 278 | assert((instance->devices[target.address].control_used & ep) != 0);
|
|---|
| 279 | instance->devices[target.address].control_used &= ~ep;
|
|---|
| 280 | fibril_mutex_unlock(&instance->guard);
|
|---|
| 281 | fibril_condvar_signal(&instance->change);
|
|---|
| 282 | }
|
|---|
| 283 | /**
|
|---|
| 284 | * @}
|
|---|
| 285 | */
|
|---|