[5f183dc] | 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 |
|
---|
[1fb1339] | 29 | /** @addtogroup libusb
|
---|
[5f183dc] | 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file
|
---|
| 33 | * @brief UHCI driver
|
---|
| 34 | */
|
---|
| 35 | #include <assert.h>
|
---|
| 36 | #include <errno.h>
|
---|
[c0964800] | 37 | #include <usb/debug.h>
|
---|
[1fb1339] | 38 | #include <usb/host/device_keeper.h>
|
---|
[5f183dc] | 39 |
|
---|
| 40 | /*----------------------------------------------------------------------------*/
|
---|
[17ceb72] | 41 | /** Initialize device keeper structure.
|
---|
[a7e2f0d] | 42 | *
|
---|
| 43 | * @param[in] instance Memory place to initialize.
|
---|
[17ceb72] | 44 | *
|
---|
| 45 | * Set all values to false/0.
|
---|
[a7e2f0d] | 46 | */
|
---|
[5f183dc] | 47 | void device_keeper_init(device_keeper_t *instance)
|
---|
| 48 | {
|
---|
| 49 | assert(instance);
|
---|
| 50 | fibril_mutex_initialize(&instance->guard);
|
---|
| 51 | fibril_condvar_initialize(&instance->default_address_occupied);
|
---|
| 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].handle = 0;
|
---|
[c15070c] | 57 | instance->devices[i].toggle_status[0] = 0;
|
---|
| 58 | instance->devices[i].toggle_status[1] = 0;
|
---|
[5f183dc] | 59 | }
|
---|
| 60 | }
|
---|
| 61 | /*----------------------------------------------------------------------------*/
|
---|
[17ceb72] | 62 | /** Attempt to obtain address 0, blocks.
|
---|
[a7e2f0d] | 63 | *
|
---|
| 64 | * @param[in] instance Device keeper structure to use.
|
---|
| 65 | * @param[in] speed Speed of the device requesting default address.
|
---|
| 66 | */
|
---|
| 67 | void device_keeper_reserve_default(device_keeper_t *instance, usb_speed_t speed)
|
---|
[5f183dc] | 68 | {
|
---|
| 69 | assert(instance);
|
---|
| 70 | fibril_mutex_lock(&instance->guard);
|
---|
| 71 | while (instance->devices[USB_ADDRESS_DEFAULT].occupied) {
|
---|
| 72 | fibril_condvar_wait(&instance->default_address_occupied,
|
---|
| 73 | &instance->guard);
|
---|
| 74 | }
|
---|
| 75 | instance->devices[USB_ADDRESS_DEFAULT].occupied = true;
|
---|
| 76 | instance->devices[USB_ADDRESS_DEFAULT].speed = speed;
|
---|
| 77 | fibril_mutex_unlock(&instance->guard);
|
---|
| 78 | }
|
---|
| 79 | /*----------------------------------------------------------------------------*/
|
---|
[17ceb72] | 80 | /** Attempt to obtain address 0, blocks.
|
---|
[a7e2f0d] | 81 | *
|
---|
| 82 | * @param[in] instance Device keeper structure to use.
|
---|
| 83 | * @param[in] speed Speed of the device requesting default address.
|
---|
| 84 | */
|
---|
[5f183dc] | 85 | void device_keeper_release_default(device_keeper_t *instance)
|
---|
| 86 | {
|
---|
| 87 | assert(instance);
|
---|
| 88 | fibril_mutex_lock(&instance->guard);
|
---|
| 89 | instance->devices[USB_ADDRESS_DEFAULT].occupied = false;
|
---|
| 90 | fibril_mutex_unlock(&instance->guard);
|
---|
| 91 | fibril_condvar_signal(&instance->default_address_occupied);
|
---|
| 92 | }
|
---|
| 93 | /*----------------------------------------------------------------------------*/
|
---|
[17ceb72] | 94 | /** Check setup packet data for signs of toggle reset.
|
---|
[a7e2f0d] | 95 | *
|
---|
| 96 | * @param[in] instance Device keeper structure to use.
|
---|
| 97 | * @param[in] target Device to receive setup packet.
|
---|
| 98 | * @param[in] data Setup packet data.
|
---|
[17ceb72] | 99 | *
|
---|
| 100 | * Really ugly one.
|
---|
[a7e2f0d] | 101 | */
|
---|
[98807e16] | 102 | void device_keeper_reset_if_need(
|
---|
| 103 | device_keeper_t *instance, usb_target_t target, const unsigned char *data)
|
---|
| 104 | {
|
---|
| 105 | assert(instance);
|
---|
| 106 | fibril_mutex_lock(&instance->guard);
|
---|
| 107 | if (target.endpoint > 15 || target.endpoint < 0
|
---|
| 108 | || target.address >= USB_ADDRESS_COUNT || target.address < 0
|
---|
| 109 | || !instance->devices[target.address].occupied) {
|
---|
[a7e2f0d] | 110 | fibril_mutex_unlock(&instance->guard);
|
---|
[17ceb72] | 111 | usb_log_error("Invalid data when checking for toggle reset.\n");
|
---|
[a7e2f0d] | 112 | return;
|
---|
[98807e16] | 113 | }
|
---|
| 114 |
|
---|
| 115 | switch (data[1])
|
---|
| 116 | {
|
---|
| 117 | case 0x01: /*clear feature*/
|
---|
[a7e2f0d] | 118 | /* recipient is endpoint, value is zero (ENDPOINT_STALL) */
|
---|
[98807e16] | 119 | if (((data[0] & 0xf) == 1) && ((data[2] | data[3]) == 0)) {
|
---|
[a7e2f0d] | 120 | /* endpoint number is < 16, thus first byte is enough */
|
---|
[c15070c] | 121 | instance->devices[target.address].toggle_status[0] &=
|
---|
| 122 | ~(1 << data[4]);
|
---|
| 123 | instance->devices[target.address].toggle_status[1] &=
|
---|
[a7e2f0d] | 124 | ~(1 << data[4]);
|
---|
[98807e16] | 125 | }
|
---|
| 126 | break;
|
---|
| 127 |
|
---|
| 128 | case 0x9: /* set configuration */
|
---|
| 129 | case 0x11: /* set interface */
|
---|
[c0964800] | 130 | /* target must be device */
|
---|
| 131 | if ((data[0] & 0xf) == 0) {
|
---|
[c15070c] | 132 | instance->devices[target.address].toggle_status[0] = 0;
|
---|
| 133 | instance->devices[target.address].toggle_status[1] = 0;
|
---|
[c0964800] | 134 | }
|
---|
[98807e16] | 135 | break;
|
---|
| 136 | }
|
---|
| 137 | fibril_mutex_unlock(&instance->guard);
|
---|
| 138 | }
|
---|
| 139 | /*----------------------------------------------------------------------------*/
|
---|
[17ceb72] | 140 | /** Get current value of endpoint toggle.
|
---|
[a7e2f0d] | 141 | *
|
---|
| 142 | * @param[in] instance Device keeper structure to use.
|
---|
| 143 | * @param[in] target Device and endpoint used.
|
---|
| 144 | * @return Error code
|
---|
| 145 | */
|
---|
[c15070c] | 146 | int device_keeper_get_toggle(
|
---|
| 147 | device_keeper_t *instance, usb_target_t target, usb_direction_t direction)
|
---|
[edb5f837] | 148 | {
|
---|
| 149 | assert(instance);
|
---|
[c15070c] | 150 | /* only control pipes are bi-directional and those do not need toggle */
|
---|
| 151 | if (direction == USB_DIRECTION_BOTH)
|
---|
| 152 | return ENOENT;
|
---|
[edb5f837] | 153 | int ret;
|
---|
| 154 | fibril_mutex_lock(&instance->guard);
|
---|
| 155 | if (target.endpoint > 15 || target.endpoint < 0
|
---|
| 156 | || target.address >= USB_ADDRESS_COUNT || target.address < 0
|
---|
| 157 | || !instance->devices[target.address].occupied) {
|
---|
[17ceb72] | 158 | usb_log_error("Invalid data when asking for toggle value.\n");
|
---|
[edb5f837] | 159 | ret = EINVAL;
|
---|
| 160 | } else {
|
---|
[c15070c] | 161 | ret = (instance->devices[target.address].toggle_status[direction]
|
---|
[a7e2f0d] | 162 | >> target.endpoint) & 1;
|
---|
[edb5f837] | 163 | }
|
---|
| 164 | fibril_mutex_unlock(&instance->guard);
|
---|
| 165 | return ret;
|
---|
| 166 | }
|
---|
| 167 | /*----------------------------------------------------------------------------*/
|
---|
[17ceb72] | 168 | /** Set current value of endpoint toggle.
|
---|
[a7e2f0d] | 169 | *
|
---|
| 170 | * @param[in] instance Device keeper structure to use.
|
---|
| 171 | * @param[in] target Device and endpoint used.
|
---|
[17ceb72] | 172 | * @param[in] toggle Toggle value.
|
---|
[a7e2f0d] | 173 | * @return Error code.
|
---|
| 174 | */
|
---|
[c15070c] | 175 | int device_keeper_set_toggle(device_keeper_t *instance,
|
---|
| 176 | usb_target_t target, usb_direction_t direction, bool toggle)
|
---|
[edb5f837] | 177 | {
|
---|
| 178 | assert(instance);
|
---|
[c15070c] | 179 | /* only control pipes are bi-directional and those do not need toggle */
|
---|
| 180 | if (direction == USB_DIRECTION_BOTH)
|
---|
| 181 | return ENOENT;
|
---|
[edb5f837] | 182 | int ret;
|
---|
| 183 | fibril_mutex_lock(&instance->guard);
|
---|
| 184 | if (target.endpoint > 15 || target.endpoint < 0
|
---|
| 185 | || target.address >= USB_ADDRESS_COUNT || target.address < 0
|
---|
| 186 | || !instance->devices[target.address].occupied) {
|
---|
[17ceb72] | 187 | usb_log_error("Invalid data when setting toggle value.\n");
|
---|
[edb5f837] | 188 | ret = EINVAL;
|
---|
| 189 | } else {
|
---|
| 190 | if (toggle) {
|
---|
[c15070c] | 191 | instance->devices[target.address].toggle_status[direction]
|
---|
| 192 | |= (1 << target.endpoint);
|
---|
[edb5f837] | 193 | } else {
|
---|
[c15070c] | 194 | instance->devices[target.address].toggle_status[direction]
|
---|
| 195 | &= ~(1 << target.endpoint);
|
---|
[edb5f837] | 196 | }
|
---|
| 197 | ret = EOK;
|
---|
| 198 | }
|
---|
| 199 | fibril_mutex_unlock(&instance->guard);
|
---|
| 200 | return ret;
|
---|
| 201 | }
|
---|
| 202 | /*----------------------------------------------------------------------------*/
|
---|
[17ceb72] | 203 | /** Get a free USB address
|
---|
[a7e2f0d] | 204 | *
|
---|
| 205 | * @param[in] instance Device keeper structure to use.
|
---|
| 206 | * @param[in] speed Speed of the device requiring address.
|
---|
| 207 | * @return Free address, or error code.
|
---|
| 208 | */
|
---|
[5f183dc] | 209 | usb_address_t device_keeper_request(
|
---|
| 210 | device_keeper_t *instance, usb_speed_t speed)
|
---|
| 211 | {
|
---|
| 212 | assert(instance);
|
---|
| 213 | fibril_mutex_lock(&instance->guard);
|
---|
| 214 |
|
---|
| 215 | usb_address_t new_address = instance->last_address + 1;
|
---|
| 216 | while (instance->devices[new_address].occupied) {
|
---|
| 217 | if (new_address == instance->last_address) {
|
---|
| 218 | fibril_mutex_unlock(&instance->guard);
|
---|
| 219 | return ENOSPC;
|
---|
| 220 | }
|
---|
| 221 | if (new_address == USB11_ADDRESS_MAX)
|
---|
| 222 | new_address = 1;
|
---|
| 223 | ++new_address;
|
---|
| 224 | }
|
---|
| 225 |
|
---|
| 226 | assert(new_address != USB_ADDRESS_DEFAULT);
|
---|
| 227 | assert(instance->devices[new_address].occupied == false);
|
---|
| 228 | instance->devices[new_address].occupied = true;
|
---|
| 229 | instance->devices[new_address].speed = speed;
|
---|
[c15070c] | 230 | instance->devices[new_address].toggle_status[0] = 0;
|
---|
| 231 | instance->devices[new_address].toggle_status[1] = 0;
|
---|
[5f183dc] | 232 | instance->last_address = new_address;
|
---|
| 233 | fibril_mutex_unlock(&instance->guard);
|
---|
| 234 | return new_address;
|
---|
| 235 | }
|
---|
| 236 | /*----------------------------------------------------------------------------*/
|
---|
[17ceb72] | 237 | /** Bind USB address to devman handle.
|
---|
[a7e2f0d] | 238 | *
|
---|
| 239 | * @param[in] instance Device keeper structure to use.
|
---|
| 240 | * @param[in] address Device address
|
---|
| 241 | * @param[in] handle Devman handle of the device.
|
---|
| 242 | */
|
---|
[5f183dc] | 243 | void device_keeper_bind(
|
---|
| 244 | device_keeper_t *instance, usb_address_t address, devman_handle_t handle)
|
---|
| 245 | {
|
---|
| 246 | assert(instance);
|
---|
| 247 | fibril_mutex_lock(&instance->guard);
|
---|
| 248 | assert(address > 0);
|
---|
| 249 | assert(address <= USB11_ADDRESS_MAX);
|
---|
| 250 | assert(instance->devices[address].occupied);
|
---|
| 251 | instance->devices[address].handle = handle;
|
---|
| 252 | fibril_mutex_unlock(&instance->guard);
|
---|
| 253 | }
|
---|
| 254 | /*----------------------------------------------------------------------------*/
|
---|
[17ceb72] | 255 | /** Release used USB address.
|
---|
[a7e2f0d] | 256 | *
|
---|
| 257 | * @param[in] instance Device keeper structure to use.
|
---|
| 258 | * @param[in] address Device address
|
---|
| 259 | */
|
---|
[5f183dc] | 260 | void device_keeper_release(device_keeper_t *instance, usb_address_t address)
|
---|
| 261 | {
|
---|
| 262 | assert(instance);
|
---|
| 263 | assert(address > 0);
|
---|
| 264 | assert(address <= USB11_ADDRESS_MAX);
|
---|
| 265 |
|
---|
| 266 | fibril_mutex_lock(&instance->guard);
|
---|
| 267 | assert(instance->devices[address].occupied);
|
---|
| 268 | instance->devices[address].occupied = false;
|
---|
| 269 | fibril_mutex_unlock(&instance->guard);
|
---|
| 270 | }
|
---|
| 271 | /*----------------------------------------------------------------------------*/
|
---|
[17ceb72] | 272 | /** Find USB address associated with the device
|
---|
[a7e2f0d] | 273 | *
|
---|
| 274 | * @param[in] instance Device keeper structure to use.
|
---|
| 275 | * @param[in] handle Devman handle of the device seeking its address.
|
---|
| 276 | * @return USB Address, or error code.
|
---|
| 277 | */
|
---|
[5f183dc] | 278 | usb_address_t device_keeper_find(
|
---|
| 279 | device_keeper_t *instance, devman_handle_t handle)
|
---|
| 280 | {
|
---|
| 281 | assert(instance);
|
---|
| 282 | fibril_mutex_lock(&instance->guard);
|
---|
| 283 | usb_address_t address = 1;
|
---|
| 284 | while (address <= USB11_ADDRESS_MAX) {
|
---|
| 285 | if (instance->devices[address].handle == handle) {
|
---|
| 286 | fibril_mutex_unlock(&instance->guard);
|
---|
| 287 | return address;
|
---|
| 288 | }
|
---|
[86c2ccd] | 289 | ++address;
|
---|
[5f183dc] | 290 | }
|
---|
| 291 | fibril_mutex_unlock(&instance->guard);
|
---|
| 292 | return ENOENT;
|
---|
| 293 | }
|
---|
| 294 | /*----------------------------------------------------------------------------*/
|
---|
[17ceb72] | 295 | /** Get speed associated with the address
|
---|
[a7e2f0d] | 296 | *
|
---|
| 297 | * @param[in] instance Device keeper structure to use.
|
---|
| 298 | * @param[in] address Address of the device.
|
---|
| 299 | * @return USB speed.
|
---|
| 300 | */
|
---|
[5f183dc] | 301 | usb_speed_t device_keeper_speed(
|
---|
| 302 | device_keeper_t *instance, usb_address_t address)
|
---|
| 303 | {
|
---|
| 304 | assert(instance);
|
---|
| 305 | assert(address >= 0);
|
---|
| 306 | assert(address <= USB11_ADDRESS_MAX);
|
---|
| 307 | return instance->devices[address].speed;
|
---|
| 308 | }
|
---|
| 309 | /**
|
---|
| 310 | * @}
|
---|
| 311 | */
|
---|