[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 |
|
---|
[1fb1339] | 29 | /** @addtogroup libusb
|
---|
[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 | fibril_mutex_initialize(&instance->guard);
|
---|
[d93b3e3] | 51 | fibril_condvar_initialize(&instance->change);
|
---|
[5f183dc] | 52 | instance->last_address = 0;
|
---|
| 53 | unsigned i = 0;
|
---|
| 54 | for (; i < USB_ADDRESS_COUNT; ++i) {
|
---|
| 55 | instance->devices[i].occupied = false;
|
---|
| 56 | instance->devices[i].handle = 0;
|
---|
[1998bcd] | 57 | instance->devices[i].speed = USB_SPEED_MAX;
|
---|
[5f183dc] | 58 | }
|
---|
[1998bcd] | 59 | // TODO: is this hack enough?
|
---|
| 60 | // (it is needed to allow smooth registration at default address)
|
---|
| 61 | instance->devices[0].occupied = true;
|
---|
[5f183dc] | 62 | }
|
---|
| 63 | /*----------------------------------------------------------------------------*/
|
---|
[17ceb72] | 64 | /** Attempt to obtain address 0, blocks.
|
---|
[a7e2f0d] | 65 | *
|
---|
| 66 | * @param[in] instance Device keeper structure to use.
|
---|
| 67 | * @param[in] speed Speed of the device requesting default address.
|
---|
| 68 | */
|
---|
[88dd355] | 69 | void usb_device_keeper_reserve_default_address(
|
---|
| 70 | usb_device_keeper_t *instance, usb_speed_t speed)
|
---|
[5f183dc] | 71 | {
|
---|
| 72 | assert(instance);
|
---|
| 73 | fibril_mutex_lock(&instance->guard);
|
---|
| 74 | while (instance->devices[USB_ADDRESS_DEFAULT].occupied) {
|
---|
[d93b3e3] | 75 | fibril_condvar_wait(&instance->change, &instance->guard);
|
---|
[5f183dc] | 76 | }
|
---|
| 77 | instance->devices[USB_ADDRESS_DEFAULT].occupied = true;
|
---|
| 78 | instance->devices[USB_ADDRESS_DEFAULT].speed = speed;
|
---|
| 79 | fibril_mutex_unlock(&instance->guard);
|
---|
| 80 | }
|
---|
| 81 | /*----------------------------------------------------------------------------*/
|
---|
[17ceb72] | 82 | /** Attempt to obtain address 0, blocks.
|
---|
[a7e2f0d] | 83 | *
|
---|
| 84 | * @param[in] instance Device keeper structure to use.
|
---|
| 85 | * @param[in] speed Speed of the device requesting default address.
|
---|
| 86 | */
|
---|
[68b5ed6e] | 87 | void usb_device_keeper_release_default_address(usb_device_keeper_t *instance)
|
---|
[5f183dc] | 88 | {
|
---|
| 89 | assert(instance);
|
---|
| 90 | fibril_mutex_lock(&instance->guard);
|
---|
| 91 | instance->devices[USB_ADDRESS_DEFAULT].occupied = false;
|
---|
| 92 | fibril_mutex_unlock(&instance->guard);
|
---|
[d93b3e3] | 93 | fibril_condvar_signal(&instance->change);
|
---|
[5f183dc] | 94 | }
|
---|
| 95 | /*----------------------------------------------------------------------------*/
|
---|
[98807e16] | 96 | /*----------------------------------------------------------------------------*/
|
---|
[17ceb72] | 97 | /** Get a free USB address
|
---|
[a7e2f0d] | 98 | *
|
---|
| 99 | * @param[in] instance Device keeper structure to use.
|
---|
| 100 | * @param[in] speed Speed of the device requiring address.
|
---|
| 101 | * @return Free address, or error code.
|
---|
| 102 | */
|
---|
[88dd355] | 103 | usb_address_t device_keeper_get_free_address(
|
---|
| 104 | usb_device_keeper_t *instance, usb_speed_t speed)
|
---|
[5f183dc] | 105 | {
|
---|
| 106 | assert(instance);
|
---|
| 107 | fibril_mutex_lock(&instance->guard);
|
---|
| 108 |
|
---|
[171cd88] | 109 | usb_address_t new_address = instance->last_address;
|
---|
| 110 | do {
|
---|
| 111 | ++new_address;
|
---|
| 112 | if (new_address > USB11_ADDRESS_MAX)
|
---|
| 113 | new_address = 1;
|
---|
[5f183dc] | 114 | if (new_address == instance->last_address) {
|
---|
| 115 | fibril_mutex_unlock(&instance->guard);
|
---|
| 116 | return ENOSPC;
|
---|
| 117 | }
|
---|
[171cd88] | 118 | } while (instance->devices[new_address].occupied);
|
---|
[5f183dc] | 119 |
|
---|
| 120 | assert(new_address != USB_ADDRESS_DEFAULT);
|
---|
| 121 | assert(instance->devices[new_address].occupied == false);
|
---|
| 122 | instance->devices[new_address].occupied = true;
|
---|
| 123 | instance->devices[new_address].speed = speed;
|
---|
| 124 | instance->last_address = new_address;
|
---|
| 125 | fibril_mutex_unlock(&instance->guard);
|
---|
| 126 | return new_address;
|
---|
| 127 | }
|
---|
| 128 | /*----------------------------------------------------------------------------*/
|
---|
[17ceb72] | 129 | /** Bind USB address to devman handle.
|
---|
[a7e2f0d] | 130 | *
|
---|
| 131 | * @param[in] instance Device keeper structure to use.
|
---|
| 132 | * @param[in] address Device address
|
---|
| 133 | * @param[in] handle Devman handle of the device.
|
---|
| 134 | */
|
---|
[e34e77a] | 135 | void usb_device_keeper_bind(usb_device_keeper_t *instance,
|
---|
| 136 | usb_address_t address, devman_handle_t handle)
|
---|
[5f183dc] | 137 | {
|
---|
| 138 | assert(instance);
|
---|
| 139 | fibril_mutex_lock(&instance->guard);
|
---|
| 140 | assert(address > 0);
|
---|
| 141 | assert(address <= USB11_ADDRESS_MAX);
|
---|
| 142 | assert(instance->devices[address].occupied);
|
---|
| 143 | instance->devices[address].handle = handle;
|
---|
| 144 | fibril_mutex_unlock(&instance->guard);
|
---|
| 145 | }
|
---|
| 146 | /*----------------------------------------------------------------------------*/
|
---|
[17ceb72] | 147 | /** Release used USB address.
|
---|
[a7e2f0d] | 148 | *
|
---|
| 149 | * @param[in] instance Device keeper structure to use.
|
---|
| 150 | * @param[in] address Device address
|
---|
| 151 | */
|
---|
[88dd355] | 152 | void usb_device_keeper_release(
|
---|
| 153 | usb_device_keeper_t *instance, usb_address_t address)
|
---|
[5f183dc] | 154 | {
|
---|
| 155 | assert(instance);
|
---|
| 156 | assert(address > 0);
|
---|
| 157 | assert(address <= USB11_ADDRESS_MAX);
|
---|
| 158 |
|
---|
| 159 | fibril_mutex_lock(&instance->guard);
|
---|
| 160 | assert(instance->devices[address].occupied);
|
---|
| 161 | instance->devices[address].occupied = false;
|
---|
| 162 | fibril_mutex_unlock(&instance->guard);
|
---|
| 163 | }
|
---|
| 164 | /*----------------------------------------------------------------------------*/
|
---|
[17ceb72] | 165 | /** Find USB address associated with the device
|
---|
[a7e2f0d] | 166 | *
|
---|
| 167 | * @param[in] instance Device keeper structure to use.
|
---|
| 168 | * @param[in] handle Devman handle of the device seeking its address.
|
---|
| 169 | * @return USB Address, or error code.
|
---|
| 170 | */
|
---|
[88dd355] | 171 | usb_address_t usb_device_keeper_find(
|
---|
| 172 | usb_device_keeper_t *instance, devman_handle_t handle)
|
---|
[5f183dc] | 173 | {
|
---|
| 174 | assert(instance);
|
---|
| 175 | fibril_mutex_lock(&instance->guard);
|
---|
| 176 | usb_address_t address = 1;
|
---|
| 177 | while (address <= USB11_ADDRESS_MAX) {
|
---|
| 178 | if (instance->devices[address].handle == handle) {
|
---|
| 179 | fibril_mutex_unlock(&instance->guard);
|
---|
| 180 | return address;
|
---|
| 181 | }
|
---|
[86c2ccd] | 182 | ++address;
|
---|
[5f183dc] | 183 | }
|
---|
| 184 | fibril_mutex_unlock(&instance->guard);
|
---|
| 185 | return ENOENT;
|
---|
| 186 | }
|
---|
| 187 | /*----------------------------------------------------------------------------*/
|
---|
[17ceb72] | 188 | /** Get speed associated with the address
|
---|
[a7e2f0d] | 189 | *
|
---|
| 190 | * @param[in] instance Device keeper structure to use.
|
---|
| 191 | * @param[in] address Address of the device.
|
---|
| 192 | * @return USB speed.
|
---|
| 193 | */
|
---|
[88dd355] | 194 | usb_speed_t usb_device_keeper_get_speed(
|
---|
| 195 | usb_device_keeper_t *instance, usb_address_t address)
|
---|
[5f183dc] | 196 | {
|
---|
| 197 | assert(instance);
|
---|
| 198 | assert(address >= 0);
|
---|
| 199 | assert(address <= USB11_ADDRESS_MAX);
|
---|
| 200 | return instance->devices[address].speed;
|
---|
| 201 | }
|
---|
| 202 | /**
|
---|
| 203 | * @}
|
---|
| 204 | */
|
---|