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