| 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 drvusbuhci
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /** @file
|
|---|
| 33 | * @brief UHCI driver
|
|---|
| 34 | */
|
|---|
| 35 | #include <assert.h>
|
|---|
| 36 | #include <errno.h>
|
|---|
| 37 |
|
|---|
| 38 | #include "device_keeper.h"
|
|---|
| 39 |
|
|---|
| 40 | /*----------------------------------------------------------------------------*/
|
|---|
| 41 | void device_keeper_init(device_keeper_t *instance)
|
|---|
| 42 | {
|
|---|
| 43 | assert(instance);
|
|---|
| 44 | fibril_mutex_initialize(&instance->guard);
|
|---|
| 45 | fibril_condvar_initialize(&instance->default_address_occupied);
|
|---|
| 46 | instance->last_address = 0;
|
|---|
| 47 | unsigned i = 0;
|
|---|
| 48 | for (; i < USB_ADDRESS_COUNT; ++i) {
|
|---|
| 49 | instance->devices[i].occupied = false;
|
|---|
| 50 | instance->devices[i].handle = 0;
|
|---|
| 51 | }
|
|---|
| 52 | }
|
|---|
| 53 | /*----------------------------------------------------------------------------*/
|
|---|
| 54 | void device_keeper_reserve_default(
|
|---|
| 55 | device_keeper_t *instance, usb_speed_t speed)
|
|---|
| 56 | {
|
|---|
| 57 | assert(instance);
|
|---|
| 58 | fibril_mutex_lock(&instance->guard);
|
|---|
| 59 | while (instance->devices[USB_ADDRESS_DEFAULT].occupied) {
|
|---|
| 60 | fibril_condvar_wait(&instance->default_address_occupied,
|
|---|
| 61 | &instance->guard);
|
|---|
| 62 | }
|
|---|
| 63 | instance->devices[USB_ADDRESS_DEFAULT].occupied = true;
|
|---|
| 64 | instance->devices[USB_ADDRESS_DEFAULT].speed = speed;
|
|---|
| 65 | fibril_mutex_unlock(&instance->guard);
|
|---|
| 66 | }
|
|---|
| 67 | /*----------------------------------------------------------------------------*/
|
|---|
| 68 | void device_keeper_release_default(device_keeper_t *instance)
|
|---|
| 69 | {
|
|---|
| 70 | assert(instance);
|
|---|
| 71 | fibril_mutex_lock(&instance->guard);
|
|---|
| 72 | instance->devices[USB_ADDRESS_DEFAULT].occupied = false;
|
|---|
| 73 | fibril_mutex_unlock(&instance->guard);
|
|---|
| 74 | fibril_condvar_signal(&instance->default_address_occupied);
|
|---|
| 75 | }
|
|---|
| 76 | /*----------------------------------------------------------------------------*/
|
|---|
| 77 | usb_address_t device_keeper_request(
|
|---|
| 78 | device_keeper_t *instance, usb_speed_t speed)
|
|---|
| 79 | {
|
|---|
| 80 | assert(instance);
|
|---|
| 81 | fibril_mutex_lock(&instance->guard);
|
|---|
| 82 |
|
|---|
| 83 | usb_address_t new_address = instance->last_address + 1;
|
|---|
| 84 | while (instance->devices[new_address].occupied) {
|
|---|
| 85 | if (new_address == instance->last_address) {
|
|---|
| 86 | fibril_mutex_unlock(&instance->guard);
|
|---|
| 87 | return ENOSPC;
|
|---|
| 88 | }
|
|---|
| 89 | if (new_address == USB11_ADDRESS_MAX)
|
|---|
| 90 | new_address = 1;
|
|---|
| 91 | ++new_address;
|
|---|
| 92 | }
|
|---|
| 93 |
|
|---|
| 94 | assert(new_address != USB_ADDRESS_DEFAULT);
|
|---|
| 95 | assert(instance->devices[new_address].occupied == false);
|
|---|
| 96 | instance->devices[new_address].occupied = true;
|
|---|
| 97 | instance->devices[new_address].speed = speed;
|
|---|
| 98 | instance->last_address = new_address;
|
|---|
| 99 | fibril_mutex_unlock(&instance->guard);
|
|---|
| 100 | return new_address;
|
|---|
| 101 | }
|
|---|
| 102 | /*----------------------------------------------------------------------------*/
|
|---|
| 103 | void device_keeper_bind(
|
|---|
| 104 | device_keeper_t *instance, usb_address_t address, devman_handle_t handle)
|
|---|
| 105 | {
|
|---|
| 106 | assert(instance);
|
|---|
| 107 | fibril_mutex_lock(&instance->guard);
|
|---|
| 108 | assert(address > 0);
|
|---|
| 109 | assert(address <= USB11_ADDRESS_MAX);
|
|---|
| 110 | assert(instance->devices[address].occupied);
|
|---|
| 111 | instance->devices[address].handle = handle;
|
|---|
| 112 | fibril_mutex_unlock(&instance->guard);
|
|---|
| 113 | }
|
|---|
| 114 | /*----------------------------------------------------------------------------*/
|
|---|
| 115 | void device_keeper_release(device_keeper_t *instance, usb_address_t address)
|
|---|
| 116 | {
|
|---|
| 117 | assert(instance);
|
|---|
| 118 | assert(address > 0);
|
|---|
| 119 | assert(address <= USB11_ADDRESS_MAX);
|
|---|
| 120 |
|
|---|
| 121 | fibril_mutex_lock(&instance->guard);
|
|---|
| 122 | assert(instance->devices[address].occupied);
|
|---|
| 123 | instance->devices[address].occupied = false;
|
|---|
| 124 | fibril_mutex_unlock(&instance->guard);
|
|---|
| 125 | }
|
|---|
| 126 | /*----------------------------------------------------------------------------*/
|
|---|
| 127 | usb_address_t device_keeper_find(
|
|---|
| 128 | device_keeper_t *instance, devman_handle_t handle)
|
|---|
| 129 | {
|
|---|
| 130 | assert(instance);
|
|---|
| 131 | fibril_mutex_lock(&instance->guard);
|
|---|
| 132 | usb_address_t address = 1;
|
|---|
| 133 | while (address <= USB11_ADDRESS_MAX) {
|
|---|
| 134 | if (instance->devices[address].handle == handle) {
|
|---|
| 135 | fibril_mutex_unlock(&instance->guard);
|
|---|
| 136 | return address;
|
|---|
| 137 | }
|
|---|
| 138 | ++address;
|
|---|
| 139 | }
|
|---|
| 140 | fibril_mutex_unlock(&instance->guard);
|
|---|
| 141 | return ENOENT;
|
|---|
| 142 | }
|
|---|
| 143 | /*----------------------------------------------------------------------------*/
|
|---|
| 144 | usb_speed_t device_keeper_speed(
|
|---|
| 145 | device_keeper_t *instance, usb_address_t address)
|
|---|
| 146 | {
|
|---|
| 147 | assert(instance);
|
|---|
| 148 | assert(address >= 0);
|
|---|
| 149 | assert(address <= USB11_ADDRESS_MAX);
|
|---|
| 150 | return instance->devices[address].speed;
|
|---|
| 151 | }
|
|---|
| 152 | /**
|
|---|
| 153 | * @}
|
|---|
| 154 | */
|
|---|