| 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 | /*----------------------------------------------------------------------------*/
|
|---|
| 41 | /** Initialize device manager structure.
|
|---|
| 42 | *
|
|---|
| 43 | * @param[in] instance Memory place to initialize.
|
|---|
| 44 | *
|
|---|
| 45 | * Set all values to false/0.
|
|---|
| 46 | */
|
|---|
| 47 | void usb_device_manager_init(usb_device_manager_t *instance)
|
|---|
| 48 | {
|
|---|
| 49 | assert(instance);
|
|---|
| 50 | unsigned i = 0;
|
|---|
| 51 | for (; i < USB_ADDRESS_COUNT; ++i) {
|
|---|
| 52 | instance->devices[i].occupied = false;
|
|---|
| 53 | instance->devices[i].handle = 0;
|
|---|
| 54 | instance->devices[i].speed = USB_SPEED_MAX;
|
|---|
| 55 | }
|
|---|
| 56 | // TODO: is this hack enough?
|
|---|
| 57 | // (it is needed to allow smooth registration at default address)
|
|---|
| 58 | instance->devices[0].occupied = true;
|
|---|
| 59 | instance->last_address = 0;
|
|---|
| 60 | fibril_mutex_initialize(&instance->guard);
|
|---|
| 61 | }
|
|---|
| 62 | /*----------------------------------------------------------------------------*/
|
|---|
| 63 | /** Get a free USB address
|
|---|
| 64 | *
|
|---|
| 65 | * @param[in] instance Device manager structure to use.
|
|---|
| 66 | * @param[in] speed Speed of the device requiring address.
|
|---|
| 67 | * @return Free address, or error code.
|
|---|
| 68 | */
|
|---|
| 69 | usb_address_t usb_device_manager_get_free_address(
|
|---|
| 70 | usb_device_manager_t *instance, usb_speed_t speed)
|
|---|
| 71 | {
|
|---|
| 72 | assert(instance);
|
|---|
| 73 | fibril_mutex_lock(&instance->guard);
|
|---|
| 74 |
|
|---|
| 75 | usb_address_t new_address = instance->last_address;
|
|---|
| 76 | do {
|
|---|
| 77 | ++new_address;
|
|---|
| 78 | if (new_address > USB11_ADDRESS_MAX)
|
|---|
| 79 | new_address = 1;
|
|---|
| 80 | if (new_address == instance->last_address) {
|
|---|
| 81 | fibril_mutex_unlock(&instance->guard);
|
|---|
| 82 | return ENOSPC;
|
|---|
| 83 | }
|
|---|
| 84 | } while (instance->devices[new_address].occupied);
|
|---|
| 85 |
|
|---|
| 86 | assert(new_address != USB_ADDRESS_DEFAULT);
|
|---|
| 87 | assert(instance->devices[new_address].occupied == false);
|
|---|
| 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 | */
|
|---|
| 103 | void usb_device_manager_bind(usb_device_manager_t *instance,
|
|---|
| 104 | usb_address_t address, devman_handle_t handle)
|
|---|
| 105 | {
|
|---|
| 106 | assert(instance);
|
|---|
| 107 | fibril_mutex_lock(&instance->guard);
|
|---|
| 108 |
|
|---|
| 109 | assert(address > 0);
|
|---|
| 110 | assert(address <= USB11_ADDRESS_MAX);
|
|---|
| 111 | assert(instance->devices[address].occupied);
|
|---|
| 112 |
|
|---|
| 113 | instance->devices[address].handle = handle;
|
|---|
| 114 | fibril_mutex_unlock(&instance->guard);
|
|---|
| 115 | }
|
|---|
| 116 | /*----------------------------------------------------------------------------*/
|
|---|
| 117 | /** Release used USB address.
|
|---|
| 118 | *
|
|---|
| 119 | * @param[in] instance Device manager structure to use.
|
|---|
| 120 | * @param[in] address Device address
|
|---|
| 121 | */
|
|---|
| 122 | void usb_device_manager_release(
|
|---|
| 123 | usb_device_manager_t *instance, usb_address_t address)
|
|---|
| 124 | {
|
|---|
| 125 | assert(instance);
|
|---|
| 126 | assert(address > 0);
|
|---|
| 127 | assert(address <= USB11_ADDRESS_MAX);
|
|---|
| 128 |
|
|---|
| 129 | fibril_mutex_lock(&instance->guard);
|
|---|
| 130 | assert(instance->devices[address].occupied);
|
|---|
| 131 |
|
|---|
| 132 | instance->devices[address].occupied = false;
|
|---|
| 133 | fibril_mutex_unlock(&instance->guard);
|
|---|
| 134 | }
|
|---|
| 135 | /*----------------------------------------------------------------------------*/
|
|---|
| 136 | /** Find USB address associated with the device
|
|---|
| 137 | *
|
|---|
| 138 | * @param[in] instance Device manager structure to use.
|
|---|
| 139 | * @param[in] handle Devman handle of the device seeking its address.
|
|---|
| 140 | * @return USB Address, or error code.
|
|---|
| 141 | */
|
|---|
| 142 | usb_address_t usb_device_manager_find(
|
|---|
| 143 | usb_device_manager_t *instance, devman_handle_t handle)
|
|---|
| 144 | {
|
|---|
| 145 | assert(instance);
|
|---|
| 146 | fibril_mutex_lock(&instance->guard);
|
|---|
| 147 | usb_address_t address = 1;
|
|---|
| 148 | while (address <= USB11_ADDRESS_MAX) {
|
|---|
| 149 | if (instance->devices[address].handle == handle) {
|
|---|
| 150 | assert(instance->devices[address].occupied);
|
|---|
| 151 | fibril_mutex_unlock(&instance->guard);
|
|---|
| 152 | return address;
|
|---|
| 153 | }
|
|---|
| 154 | ++address;
|
|---|
| 155 | }
|
|---|
| 156 | fibril_mutex_unlock(&instance->guard);
|
|---|
| 157 | return ENOENT;
|
|---|
| 158 | }
|
|---|
| 159 |
|
|---|
| 160 | /** Find devman handle assigned to USB address.
|
|---|
| 161 | * Intentionally refuse to find handle of default address.
|
|---|
| 162 | *
|
|---|
| 163 | * @param[in] instance Device manager structure to use.
|
|---|
| 164 | * @param[in] address Address the caller wants to find.
|
|---|
| 165 | * @param[out] handle Where to store found handle.
|
|---|
| 166 | * @return Whether such address is currently occupied.
|
|---|
| 167 | */
|
|---|
| 168 | bool usb_device_manager_find_by_address(usb_device_manager_t *instance,
|
|---|
| 169 | usb_address_t address, devman_handle_t *handle)
|
|---|
| 170 | {
|
|---|
| 171 | assert(instance);
|
|---|
| 172 | fibril_mutex_lock(&instance->guard);
|
|---|
| 173 | if ((address <= 0) || (address >= USB_ADDRESS_COUNT)) {
|
|---|
| 174 | fibril_mutex_unlock(&instance->guard);
|
|---|
| 175 | return false;
|
|---|
| 176 | }
|
|---|
| 177 | if (!instance->devices[address].occupied) {
|
|---|
| 178 | fibril_mutex_unlock(&instance->guard);
|
|---|
| 179 | return false;
|
|---|
| 180 | }
|
|---|
| 181 |
|
|---|
| 182 | if (handle != NULL) {
|
|---|
| 183 | *handle = instance->devices[address].handle;
|
|---|
| 184 | }
|
|---|
| 185 |
|
|---|
| 186 | fibril_mutex_unlock(&instance->guard);
|
|---|
| 187 | return true;
|
|---|
| 188 | }
|
|---|
| 189 |
|
|---|
| 190 | /*----------------------------------------------------------------------------*/
|
|---|
| 191 | /** Get speed associated with the address
|
|---|
| 192 | *
|
|---|
| 193 | * @param[in] instance Device manager structure to use.
|
|---|
| 194 | * @param[in] address Address of the device.
|
|---|
| 195 | * @return USB speed.
|
|---|
| 196 | */
|
|---|
| 197 | usb_speed_t usb_device_manager_get_speed(
|
|---|
| 198 | usb_device_manager_t *instance, usb_address_t address)
|
|---|
| 199 | {
|
|---|
| 200 | assert(instance);
|
|---|
| 201 | assert(address >= 0);
|
|---|
| 202 | assert(address <= USB11_ADDRESS_MAX);
|
|---|
| 203 |
|
|---|
| 204 | return instance->devices[address].speed;
|
|---|
| 205 | }
|
|---|
| 206 | /**
|
|---|
| 207 | * @}
|
|---|
| 208 | */
|
|---|