[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 |
|
---|
[160b75e] | 29 | /** @addtogroup libusbhost
|
---|
[5f183dc] | 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file
|
---|
[8b54fe6] | 33 | * Device manager structure and functions (implementation).
|
---|
[5f183dc] | 34 | */
|
---|
| 35 | #include <assert.h>
|
---|
| 36 | #include <errno.h>
|
---|
[c0964800] | 37 | #include <usb/debug.h>
|
---|
[8b54fe6] | 38 | #include <usb/host/usb_device_manager.h>
|
---|
[5f183dc] | 39 |
|
---|
[0cd8089] | 40 | /** Get a free USB address
|
---|
| 41 | *
|
---|
| 42 | * @param[in] instance Device manager structure to use.
|
---|
| 43 | * @param[in] speed Speed of the device requiring address.
|
---|
| 44 | * @return Free address, or error code.
|
---|
| 45 | */
|
---|
| 46 | static usb_address_t usb_device_manager_get_free_address(
|
---|
| 47 | usb_device_manager_t *instance)
|
---|
| 48 | {
|
---|
| 49 |
|
---|
| 50 | usb_address_t new_address = instance->last_address;
|
---|
| 51 | do {
|
---|
| 52 | new_address = (new_address + 1) % USB_ADDRESS_COUNT;
|
---|
| 53 | if (new_address == USB_ADDRESS_DEFAULT)
|
---|
| 54 | new_address = 1;
|
---|
| 55 | if (new_address == instance->last_address) {
|
---|
| 56 | return ENOSPC;
|
---|
| 57 | }
|
---|
| 58 | } while (instance->devices[new_address].occupied);
|
---|
| 59 |
|
---|
| 60 | assert(new_address != USB_ADDRESS_DEFAULT);
|
---|
| 61 | instance->last_address = new_address;
|
---|
| 62 |
|
---|
| 63 | return new_address;
|
---|
| 64 | }
|
---|
| 65 | /*----------------------------------------------------------------------------*/
|
---|
[8b54fe6] | 66 | /** Initialize device manager structure.
|
---|
[a7e2f0d] | 67 | *
|
---|
| 68 | * @param[in] instance Memory place to initialize.
|
---|
[5e07cbc0] | 69 | * @param[in] max_speed Maximum allowed USB speed of devices (inclusive).
|
---|
[17ceb72] | 70 | *
|
---|
| 71 | * Set all values to false/0.
|
---|
[a7e2f0d] | 72 | */
|
---|
[5e07cbc0] | 73 | void usb_device_manager_init(
|
---|
| 74 | usb_device_manager_t *instance, usb_speed_t max_speed)
|
---|
[5f183dc] | 75 | {
|
---|
| 76 | assert(instance);
|
---|
[2fd1f0c6] | 77 | for (unsigned i = 0; i < USB_ADDRESS_COUNT; ++i) {
|
---|
[5f183dc] | 78 | instance->devices[i].occupied = false;
|
---|
| 79 | instance->devices[i].handle = 0;
|
---|
[1998bcd] | 80 | instance->devices[i].speed = USB_SPEED_MAX;
|
---|
[5f183dc] | 81 | }
|
---|
[bbd09694] | 82 | instance->last_address = 1;
|
---|
[5e07cbc0] | 83 | instance->max_speed = max_speed;
|
---|
[c4e3b1f7] | 84 | fibril_mutex_initialize(&instance->guard);
|
---|
[5f183dc] | 85 | }
|
---|
| 86 | /*----------------------------------------------------------------------------*/
|
---|
[0cd8089] | 87 | /** Request USB address.
|
---|
| 88 | * @param instance usb_device_manager
|
---|
| 89 | * @param address Pointer to requested address value, place to store new address
|
---|
| 90 | * @parma strict Fail if the requested address is not available.
|
---|
| 91 | * @return Error code.
|
---|
| 92 | * @note Default address is only available in strict mode.
|
---|
[a7e2f0d] | 93 | */
|
---|
[0cd8089] | 94 | int usb_device_manager_request_address(usb_device_manager_t *instance,
|
---|
| 95 | usb_address_t *address, bool strict, usb_speed_t speed)
|
---|
[5f183dc] | 96 | {
|
---|
| 97 | assert(instance);
|
---|
[0cd8089] | 98 | assert(address);
|
---|
[5e07cbc0] | 99 | if (speed > instance->max_speed)
|
---|
| 100 | return ENOTSUP;
|
---|
[0cd8089] | 101 |
|
---|
| 102 | if ((*address) < 0 || (*address) >= USB_ADDRESS_COUNT)
|
---|
| 103 | return EINVAL;
|
---|
| 104 |
|
---|
[5f183dc] | 105 | fibril_mutex_lock(&instance->guard);
|
---|
[0cd8089] | 106 | /* Only grant default address to strict requests */
|
---|
| 107 | if (( (*address) == USB_ADDRESS_DEFAULT) && !strict) {
|
---|
| 108 | *address = instance->last_address;
|
---|
| 109 | }
|
---|
[5f183dc] | 110 |
|
---|
[0cd8089] | 111 | if (instance->devices[*address].occupied) {
|
---|
| 112 | if (strict) {
|
---|
[5f183dc] | 113 | fibril_mutex_unlock(&instance->guard);
|
---|
[0cd8089] | 114 | return ENOENT;
|
---|
[5f183dc] | 115 | }
|
---|
[0cd8089] | 116 | *address = usb_device_manager_get_free_address(instance);
|
---|
| 117 | }
|
---|
| 118 | assert(instance->devices[*address].occupied == false);
|
---|
| 119 | assert(instance->devices[*address].handle == 0);
|
---|
[bbd09694] | 120 | assert(*address != USB_ADDRESS_DEFAULT || strict);
|
---|
[c4e3b1f7] | 121 |
|
---|
[0cd8089] | 122 | instance->devices[*address].occupied = true;
|
---|
| 123 | instance->devices[*address].speed = speed;
|
---|
[c4e3b1f7] | 124 |
|
---|
[5f183dc] | 125 | fibril_mutex_unlock(&instance->guard);
|
---|
[0cd8089] | 126 | return EOK;
|
---|
[5f183dc] | 127 | }
|
---|
| 128 | /*----------------------------------------------------------------------------*/
|
---|
[17ceb72] | 129 | /** Bind USB address to devman handle.
|
---|
[a7e2f0d] | 130 | *
|
---|
[8b54fe6] | 131 | * @param[in] instance Device manager structure to use.
|
---|
[a7e2f0d] | 132 | * @param[in] address Device address
|
---|
| 133 | * @param[in] handle Devman handle of the device.
|
---|
[2fd1f0c6] | 134 | * @return Error code.
|
---|
[a7e2f0d] | 135 | */
|
---|
[0cd8089] | 136 | int usb_device_manager_bind_address(usb_device_manager_t *instance,
|
---|
[e34e77a] | 137 | usb_address_t address, devman_handle_t handle)
|
---|
[5f183dc] | 138 | {
|
---|
[2fd1f0c6] | 139 | if ((address <= 0) || (address >= USB_ADDRESS_COUNT)) {
|
---|
| 140 | return EINVAL;
|
---|
| 141 | }
|
---|
[5f183dc] | 142 | assert(instance);
|
---|
[c4e3b1f7] | 143 |
|
---|
[2fd1f0c6] | 144 | fibril_mutex_lock(&instance->guard);
|
---|
| 145 | /* Not reserved */
|
---|
| 146 | if (!instance->devices[address].occupied) {
|
---|
| 147 | fibril_mutex_unlock(&instance->guard);
|
---|
| 148 | return ENOENT;
|
---|
| 149 | }
|
---|
| 150 | /* Already bound */
|
---|
| 151 | if (instance->devices[address].handle != 0) {
|
---|
| 152 | fibril_mutex_unlock(&instance->guard);
|
---|
| 153 | return EEXISTS;
|
---|
| 154 | }
|
---|
[5f183dc] | 155 | instance->devices[address].handle = handle;
|
---|
| 156 | fibril_mutex_unlock(&instance->guard);
|
---|
[2fd1f0c6] | 157 | return EOK;
|
---|
[5f183dc] | 158 | }
|
---|
| 159 | /*----------------------------------------------------------------------------*/
|
---|
[17ceb72] | 160 | /** Release used USB address.
|
---|
[a7e2f0d] | 161 | *
|
---|
[8b54fe6] | 162 | * @param[in] instance Device manager structure to use.
|
---|
[a7e2f0d] | 163 | * @param[in] address Device address
|
---|
[2fd1f0c6] | 164 | * @return Error code.
|
---|
[a7e2f0d] | 165 | */
|
---|
[0cd8089] | 166 | int usb_device_manager_release_address(
|
---|
[8b54fe6] | 167 | usb_device_manager_t *instance, usb_address_t address)
|
---|
[5f183dc] | 168 | {
|
---|
[f37eb84] | 169 | if ((address < 0) || (address >= USB_ADDRESS_COUNT)) {
|
---|
[2fd1f0c6] | 170 | return EINVAL;
|
---|
| 171 | }
|
---|
[5f183dc] | 172 | assert(instance);
|
---|
| 173 |
|
---|
| 174 | fibril_mutex_lock(&instance->guard);
|
---|
[2fd1f0c6] | 175 | if (!instance->devices[address].occupied) {
|
---|
| 176 | fibril_mutex_unlock(&instance->guard);
|
---|
| 177 | return ENOENT;
|
---|
| 178 | }
|
---|
[c4e3b1f7] | 179 |
|
---|
[5f183dc] | 180 | instance->devices[address].occupied = false;
|
---|
[4267908] | 181 | instance->devices[address].handle = 0;
|
---|
[5f183dc] | 182 | fibril_mutex_unlock(&instance->guard);
|
---|
[2fd1f0c6] | 183 | return EOK;
|
---|
[5f183dc] | 184 | }
|
---|
| 185 | /*----------------------------------------------------------------------------*/
|
---|
[17ceb72] | 186 | /** Find USB address associated with the device
|
---|
[a7e2f0d] | 187 | *
|
---|
[8b54fe6] | 188 | * @param[in] instance Device manager structure to use.
|
---|
[a7e2f0d] | 189 | * @param[in] handle Devman handle of the device seeking its address.
|
---|
| 190 | * @return USB Address, or error code.
|
---|
| 191 | */
|
---|
[4267908] | 192 | usb_address_t usb_device_manager_find_address(
|
---|
[8b54fe6] | 193 | usb_device_manager_t *instance, devman_handle_t handle)
|
---|
[5f183dc] | 194 | {
|
---|
| 195 | assert(instance);
|
---|
| 196 | fibril_mutex_lock(&instance->guard);
|
---|
[2fd1f0c6] | 197 | for (usb_address_t address = 1; address <= USB11_ADDRESS_MAX; ++address)
|
---|
| 198 | {
|
---|
[5f183dc] | 199 | if (instance->devices[address].handle == handle) {
|
---|
[c4e3b1f7] | 200 | assert(instance->devices[address].occupied);
|
---|
[5f183dc] | 201 | fibril_mutex_unlock(&instance->guard);
|
---|
| 202 | return address;
|
---|
| 203 | }
|
---|
| 204 | }
|
---|
| 205 | fibril_mutex_unlock(&instance->guard);
|
---|
| 206 | return ENOENT;
|
---|
| 207 | }
|
---|
[4267908] | 208 | /*----------------------------------------------------------------------------*/
|
---|
| 209 | /** Find devman handle and speed assigned to USB address.
|
---|
| 210 | * Intentionally refuse to work on default address.
|
---|
[84de606d] | 211 | *
|
---|
[8b54fe6] | 212 | * @param[in] instance Device manager structure to use.
|
---|
[84de606d] | 213 | * @param[in] address Address the caller wants to find.
|
---|
| 214 | * @param[out] handle Where to store found handle.
|
---|
[4267908] | 215 | * @param[out] speed Assigned speed.
|
---|
| 216 | * @return Error code.
|
---|
[84de606d] | 217 | */
|
---|
[4267908] | 218 | int usb_device_manager_get_info_by_address(usb_device_manager_t *instance,
|
---|
| 219 | usb_address_t address, devman_handle_t *handle, usb_speed_t *speed)
|
---|
[84de606d] | 220 | {
|
---|
| 221 | assert(instance);
|
---|
[1b17e37] | 222 | if ((address < 0) || (address >= USB_ADDRESS_COUNT)) {
|
---|
[4267908] | 223 | return EINVAL;
|
---|
[84de606d] | 224 | }
|
---|
[4267908] | 225 |
|
---|
| 226 | fibril_mutex_lock(&instance->guard);
|
---|
[84de606d] | 227 | if (!instance->devices[address].occupied) {
|
---|
| 228 | fibril_mutex_unlock(&instance->guard);
|
---|
[4267908] | 229 | return ENOENT;
|
---|
[84de606d] | 230 | }
|
---|
| 231 |
|
---|
| 232 | if (handle != NULL) {
|
---|
| 233 | *handle = instance->devices[address].handle;
|
---|
| 234 | }
|
---|
[4267908] | 235 | if (speed != NULL) {
|
---|
| 236 | *speed = instance->devices[address].speed;
|
---|
| 237 | }
|
---|
[84de606d] | 238 |
|
---|
| 239 | fibril_mutex_unlock(&instance->guard);
|
---|
[4267908] | 240 | return EOK;
|
---|
[5f183dc] | 241 | }
|
---|
| 242 | /**
|
---|
| 243 | * @}
|
---|
| 244 | */
|
---|