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