[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
|
---|
[e34e77a] | 33 | * Device keeper structure and functions (implementation).
|
---|
[5f183dc] | 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 | */
|
---|
[68b5ed6e] | 47 | void usb_device_keeper_init(usb_device_keeper_t *instance)
|
---|
[5f183dc] | 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;
|
---|
[1998bcd] | 54 | instance->devices[i].speed = USB_SPEED_MAX;
|
---|
[5f183dc] | 55 | }
|
---|
[1998bcd] | 56 | // TODO: is this hack enough?
|
---|
| 57 | // (it is needed to allow smooth registration at default address)
|
---|
| 58 | instance->devices[0].occupied = true;
|
---|
[c4e3b1f7] | 59 | instance->last_address = 0;
|
---|
| 60 | fibril_mutex_initialize(&instance->guard);
|
---|
[5f183dc] | 61 | }
|
---|
| 62 | /*----------------------------------------------------------------------------*/
|
---|
[17ceb72] | 63 | /** Get a free USB address
|
---|
[a7e2f0d] | 64 | *
|
---|
| 65 | * @param[in] instance Device keeper structure to use.
|
---|
| 66 | * @param[in] speed Speed of the device requiring address.
|
---|
| 67 | * @return Free address, or error code.
|
---|
| 68 | */
|
---|
[88dd355] | 69 | usb_address_t device_keeper_get_free_address(
|
---|
| 70 | usb_device_keeper_t *instance, usb_speed_t speed)
|
---|
[5f183dc] | 71 | {
|
---|
| 72 | assert(instance);
|
---|
| 73 | fibril_mutex_lock(&instance->guard);
|
---|
| 74 |
|
---|
[171cd88] | 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;
|
---|
[5f183dc] | 80 | if (new_address == instance->last_address) {
|
---|
| 81 | fibril_mutex_unlock(&instance->guard);
|
---|
| 82 | return ENOSPC;
|
---|
| 83 | }
|
---|
[171cd88] | 84 | } while (instance->devices[new_address].occupied);
|
---|
[5f183dc] | 85 |
|
---|
| 86 | assert(new_address != USB_ADDRESS_DEFAULT);
|
---|
| 87 | assert(instance->devices[new_address].occupied == false);
|
---|
[c4e3b1f7] | 88 |
|
---|
[5f183dc] | 89 | instance->devices[new_address].occupied = true;
|
---|
| 90 | instance->devices[new_address].speed = speed;
|
---|
| 91 | instance->last_address = new_address;
|
---|
[c4e3b1f7] | 92 |
|
---|
[5f183dc] | 93 | fibril_mutex_unlock(&instance->guard);
|
---|
| 94 | return new_address;
|
---|
| 95 | }
|
---|
| 96 | /*----------------------------------------------------------------------------*/
|
---|
[17ceb72] | 97 | /** Bind USB address to devman handle.
|
---|
[a7e2f0d] | 98 | *
|
---|
| 99 | * @param[in] instance Device keeper structure to use.
|
---|
| 100 | * @param[in] address Device address
|
---|
| 101 | * @param[in] handle Devman handle of the device.
|
---|
| 102 | */
|
---|
[e34e77a] | 103 | void usb_device_keeper_bind(usb_device_keeper_t *instance,
|
---|
| 104 | usb_address_t address, devman_handle_t handle)
|
---|
[5f183dc] | 105 | {
|
---|
| 106 | assert(instance);
|
---|
| 107 | fibril_mutex_lock(&instance->guard);
|
---|
[c4e3b1f7] | 108 |
|
---|
[5f183dc] | 109 | assert(address > 0);
|
---|
| 110 | assert(address <= USB11_ADDRESS_MAX);
|
---|
| 111 | assert(instance->devices[address].occupied);
|
---|
[c4e3b1f7] | 112 |
|
---|
[5f183dc] | 113 | instance->devices[address].handle = handle;
|
---|
| 114 | fibril_mutex_unlock(&instance->guard);
|
---|
| 115 | }
|
---|
| 116 | /*----------------------------------------------------------------------------*/
|
---|
[17ceb72] | 117 | /** Release used USB address.
|
---|
[a7e2f0d] | 118 | *
|
---|
| 119 | * @param[in] instance Device keeper structure to use.
|
---|
| 120 | * @param[in] address Device address
|
---|
| 121 | */
|
---|
[88dd355] | 122 | void usb_device_keeper_release(
|
---|
| 123 | usb_device_keeper_t *instance, usb_address_t address)
|
---|
[5f183dc] | 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);
|
---|
[c4e3b1f7] | 131 |
|
---|
[5f183dc] | 132 | instance->devices[address].occupied = false;
|
---|
| 133 | fibril_mutex_unlock(&instance->guard);
|
---|
| 134 | }
|
---|
| 135 | /*----------------------------------------------------------------------------*/
|
---|
[17ceb72] | 136 | /** Find USB address associated with the device
|
---|
[a7e2f0d] | 137 | *
|
---|
| 138 | * @param[in] instance Device keeper structure to use.
|
---|
| 139 | * @param[in] handle Devman handle of the device seeking its address.
|
---|
| 140 | * @return USB Address, or error code.
|
---|
| 141 | */
|
---|
[88dd355] | 142 | usb_address_t usb_device_keeper_find(
|
---|
| 143 | usb_device_keeper_t *instance, devman_handle_t handle)
|
---|
[5f183dc] | 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) {
|
---|
[c4e3b1f7] | 150 | assert(instance->devices[address].occupied);
|
---|
[5f183dc] | 151 | fibril_mutex_unlock(&instance->guard);
|
---|
| 152 | return address;
|
---|
| 153 | }
|
---|
[86c2ccd] | 154 | ++address;
|
---|
[5f183dc] | 155 | }
|
---|
| 156 | fibril_mutex_unlock(&instance->guard);
|
---|
| 157 | return ENOENT;
|
---|
| 158 | }
|
---|
[84de606d] | 159 |
|
---|
[17fc40c] | 160 | /** Find devman handle assigned to USB address.
|
---|
| 161 | * Intentionally refuse to find handle of default address.
|
---|
[84de606d] | 162 | *
|
---|
| 163 | * @param[in] instance Device keeper 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_keeper_find_by_address(usb_device_keeper_t *instance,
|
---|
| 169 | usb_address_t address, devman_handle_t *handle)
|
---|
| 170 | {
|
---|
| 171 | assert(instance);
|
---|
| 172 | fibril_mutex_lock(&instance->guard);
|
---|
[17fc40c] | 173 | if ((address <= 0) || (address >= USB_ADDRESS_COUNT)) {
|
---|
[84de606d] | 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 |
|
---|
[5f183dc] | 190 | /*----------------------------------------------------------------------------*/
|
---|
[17ceb72] | 191 | /** Get speed associated with the address
|
---|
[a7e2f0d] | 192 | *
|
---|
| 193 | * @param[in] instance Device keeper structure to use.
|
---|
| 194 | * @param[in] address Address of the device.
|
---|
| 195 | * @return USB speed.
|
---|
| 196 | */
|
---|
[88dd355] | 197 | usb_speed_t usb_device_keeper_get_speed(
|
---|
| 198 | usb_device_keeper_t *instance, usb_address_t address)
|
---|
[5f183dc] | 199 | {
|
---|
| 200 | assert(instance);
|
---|
| 201 | assert(address >= 0);
|
---|
| 202 | assert(address <= USB11_ADDRESS_MAX);
|
---|
[c4e3b1f7] | 203 |
|
---|
[5f183dc] | 204 | return instance->devices[address].speed;
|
---|
| 205 | }
|
---|
| 206 | /**
|
---|
| 207 | * @}
|
---|
| 208 | */
|
---|