| 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 libusbhost
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /** @file
|
|---|
| 33 | * Device manager structure and functions (implementation).
|
|---|
| 34 | */
|
|---|
| 35 | #include <assert.h>
|
|---|
| 36 | #include <errno.h>
|
|---|
| 37 | #include <usb/debug.h>
|
|---|
| 38 | #include <usb/host/usb_device_manager.h>
|
|---|
| 39 |
|
|---|
| 40 | /** Initialize device manager structure.
|
|---|
| 41 | *
|
|---|
| 42 | * @param[in] instance Memory place to initialize.
|
|---|
| 43 | *
|
|---|
| 44 | * Set all values to false/0.
|
|---|
| 45 | */
|
|---|
| 46 | void usb_device_manager_init(usb_device_manager_t *instance)
|
|---|
| 47 | {
|
|---|
| 48 | assert(instance);
|
|---|
| 49 | for (unsigned i = 0; i < USB_ADDRESS_COUNT; ++i) {
|
|---|
| 50 | instance->devices[i].occupied = false;
|
|---|
| 51 | instance->devices[i].handle = 0;
|
|---|
| 52 | instance->devices[i].speed = USB_SPEED_MAX;
|
|---|
| 53 | }
|
|---|
| 54 | // TODO: is this hack enough?
|
|---|
| 55 | // (it is needed to allow smooth registration at default address)
|
|---|
| 56 | instance->devices[0].occupied = true;
|
|---|
| 57 | instance->last_address = 0;
|
|---|
| 58 | fibril_mutex_initialize(&instance->guard);
|
|---|
| 59 | }
|
|---|
| 60 | /*----------------------------------------------------------------------------*/
|
|---|
| 61 | /** Get a free USB address
|
|---|
| 62 | *
|
|---|
| 63 | * @param[in] instance Device manager structure to use.
|
|---|
| 64 | * @param[in] speed Speed of the device requiring address.
|
|---|
| 65 | * @return Free address, or error code.
|
|---|
| 66 | */
|
|---|
| 67 | usb_address_t usb_device_manager_get_free_address(
|
|---|
| 68 | usb_device_manager_t *instance, usb_speed_t speed)
|
|---|
| 69 | {
|
|---|
| 70 | assert(instance);
|
|---|
| 71 | fibril_mutex_lock(&instance->guard);
|
|---|
| 72 |
|
|---|
| 73 | usb_address_t new_address = instance->last_address;
|
|---|
| 74 | do {
|
|---|
| 75 | ++new_address;
|
|---|
| 76 | if (new_address > USB11_ADDRESS_MAX)
|
|---|
| 77 | new_address = 1; // NOTE it should be safe to put 0 here
|
|---|
| 78 | // TODO Use mod
|
|---|
| 79 | if (new_address == instance->last_address) {
|
|---|
| 80 | fibril_mutex_unlock(&instance->guard);
|
|---|
| 81 | return ENOSPC;
|
|---|
| 82 | }
|
|---|
| 83 | } while (instance->devices[new_address].occupied);
|
|---|
| 84 |
|
|---|
| 85 | assert(new_address != USB_ADDRESS_DEFAULT);
|
|---|
| 86 | assert(instance->devices[new_address].occupied == false);
|
|---|
| 87 | assert(instance->devices[new_address].handle == 0);
|
|---|
| 88 |
|
|---|
| 89 | instance->devices[new_address].occupied = true;
|
|---|
| 90 | instance->devices[new_address].speed = speed;
|
|---|
| 91 | instance->last_address = new_address;
|
|---|
| 92 |
|
|---|
| 93 | fibril_mutex_unlock(&instance->guard);
|
|---|
| 94 | return new_address;
|
|---|
| 95 | }
|
|---|
| 96 | /*----------------------------------------------------------------------------*/
|
|---|
| 97 | /** Bind USB address to devman handle.
|
|---|
| 98 | *
|
|---|
| 99 | * @param[in] instance Device manager structure to use.
|
|---|
| 100 | * @param[in] address Device address
|
|---|
| 101 | * @param[in] handle Devman handle of the device.
|
|---|
| 102 | * @return Error code.
|
|---|
| 103 | */
|
|---|
| 104 | int usb_device_manager_bind(usb_device_manager_t *instance,
|
|---|
| 105 | usb_address_t address, devman_handle_t handle)
|
|---|
| 106 | {
|
|---|
| 107 | if ((address <= 0) || (address >= USB_ADDRESS_COUNT)) {
|
|---|
| 108 | return EINVAL;
|
|---|
| 109 | }
|
|---|
| 110 | assert(instance);
|
|---|
| 111 |
|
|---|
| 112 | fibril_mutex_lock(&instance->guard);
|
|---|
| 113 | /* Not reserved */
|
|---|
| 114 | if (!instance->devices[address].occupied) {
|
|---|
| 115 | fibril_mutex_unlock(&instance->guard);
|
|---|
| 116 | return ENOENT;
|
|---|
| 117 | }
|
|---|
| 118 | /* Already bound */
|
|---|
| 119 | if (instance->devices[address].handle != 0) {
|
|---|
| 120 | fibril_mutex_unlock(&instance->guard);
|
|---|
| 121 | return EEXISTS;
|
|---|
| 122 | }
|
|---|
| 123 | instance->devices[address].handle = handle;
|
|---|
| 124 | fibril_mutex_unlock(&instance->guard);
|
|---|
| 125 | return EOK;
|
|---|
| 126 | }
|
|---|
| 127 | /*----------------------------------------------------------------------------*/
|
|---|
| 128 | /** Release used USB address.
|
|---|
| 129 | *
|
|---|
| 130 | * @param[in] instance Device manager structure to use.
|
|---|
| 131 | * @param[in] address Device address
|
|---|
| 132 | * @return Error code.
|
|---|
| 133 | */
|
|---|
| 134 | int usb_device_manager_release(
|
|---|
| 135 | usb_device_manager_t *instance, usb_address_t address)
|
|---|
| 136 | {
|
|---|
| 137 | if ((address <= 0) || (address >= USB_ADDRESS_COUNT)) {
|
|---|
| 138 | return EINVAL;
|
|---|
| 139 | }
|
|---|
| 140 | assert(instance);
|
|---|
| 141 |
|
|---|
| 142 | fibril_mutex_lock(&instance->guard);
|
|---|
| 143 | if (!instance->devices[address].occupied) {
|
|---|
| 144 | fibril_mutex_unlock(&instance->guard);
|
|---|
| 145 | return ENOENT;
|
|---|
| 146 | }
|
|---|
| 147 |
|
|---|
| 148 | instance->devices[address].occupied = false;
|
|---|
| 149 | instance->devices[address].handle = 0;
|
|---|
| 150 | fibril_mutex_unlock(&instance->guard);
|
|---|
| 151 | return EOK;
|
|---|
| 152 | }
|
|---|
| 153 | /*----------------------------------------------------------------------------*/
|
|---|
| 154 | /** Find USB address associated with the device
|
|---|
| 155 | *
|
|---|
| 156 | * @param[in] instance Device manager structure to use.
|
|---|
| 157 | * @param[in] handle Devman handle of the device seeking its address.
|
|---|
| 158 | * @return USB Address, or error code.
|
|---|
| 159 | */
|
|---|
| 160 | usb_address_t usb_device_manager_find_address(
|
|---|
| 161 | usb_device_manager_t *instance, devman_handle_t handle)
|
|---|
| 162 | {
|
|---|
| 163 | assert(instance);
|
|---|
| 164 | fibril_mutex_lock(&instance->guard);
|
|---|
| 165 | for (usb_address_t address = 1; address <= USB11_ADDRESS_MAX; ++address)
|
|---|
| 166 | {
|
|---|
| 167 | if (instance->devices[address].handle == handle) {
|
|---|
| 168 | assert(instance->devices[address].occupied);
|
|---|
| 169 | fibril_mutex_unlock(&instance->guard);
|
|---|
| 170 | return address;
|
|---|
| 171 | }
|
|---|
| 172 | }
|
|---|
| 173 | fibril_mutex_unlock(&instance->guard);
|
|---|
| 174 | return ENOENT;
|
|---|
| 175 | }
|
|---|
| 176 | /*----------------------------------------------------------------------------*/
|
|---|
| 177 | /** Find devman handle and speed assigned to USB address.
|
|---|
| 178 | * Intentionally refuse to work on default address.
|
|---|
| 179 | *
|
|---|
| 180 | * @param[in] instance Device manager structure to use.
|
|---|
| 181 | * @param[in] address Address the caller wants to find.
|
|---|
| 182 | * @param[out] handle Where to store found handle.
|
|---|
| 183 | * @param[out] speed Assigned speed.
|
|---|
| 184 | * @return Error code.
|
|---|
| 185 | */
|
|---|
| 186 | int usb_device_manager_get_info_by_address(usb_device_manager_t *instance,
|
|---|
| 187 | usb_address_t address, devman_handle_t *handle, usb_speed_t *speed)
|
|---|
| 188 | {
|
|---|
| 189 | assert(instance);
|
|---|
| 190 | if ((address <= 0) || (address >= USB_ADDRESS_COUNT)) {
|
|---|
| 191 | return EINVAL;
|
|---|
| 192 | }
|
|---|
| 193 |
|
|---|
| 194 | fibril_mutex_lock(&instance->guard);
|
|---|
| 195 | if (!instance->devices[address].occupied) {
|
|---|
| 196 | fibril_mutex_unlock(&instance->guard);
|
|---|
| 197 | return ENOENT;
|
|---|
| 198 | }
|
|---|
| 199 |
|
|---|
| 200 | if (handle != NULL) {
|
|---|
| 201 | *handle = instance->devices[address].handle;
|
|---|
| 202 | }
|
|---|
| 203 | if (speed != NULL) {
|
|---|
| 204 | *speed = instance->devices[address].speed;
|
|---|
| 205 | }
|
|---|
| 206 |
|
|---|
| 207 | fibril_mutex_unlock(&instance->guard);
|
|---|
| 208 | return EOK;
|
|---|
| 209 | }
|
|---|
| 210 | /**
|
|---|
| 211 | * @}
|
|---|
| 212 | */
|
|---|