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 | instance->devices[i].toggle_status = 0;
|
---|
52 | }
|
---|
53 | }
|
---|
54 | /*----------------------------------------------------------------------------*/
|
---|
55 | void device_keeper_reserve_default(
|
---|
56 | device_keeper_t *instance, usb_speed_t speed)
|
---|
57 | {
|
---|
58 | assert(instance);
|
---|
59 | fibril_mutex_lock(&instance->guard);
|
---|
60 | while (instance->devices[USB_ADDRESS_DEFAULT].occupied) {
|
---|
61 | fibril_condvar_wait(&instance->default_address_occupied,
|
---|
62 | &instance->guard);
|
---|
63 | }
|
---|
64 | instance->devices[USB_ADDRESS_DEFAULT].occupied = true;
|
---|
65 | instance->devices[USB_ADDRESS_DEFAULT].speed = speed;
|
---|
66 | fibril_mutex_unlock(&instance->guard);
|
---|
67 | }
|
---|
68 | /*----------------------------------------------------------------------------*/
|
---|
69 | void device_keeper_release_default(device_keeper_t *instance)
|
---|
70 | {
|
---|
71 | assert(instance);
|
---|
72 | fibril_mutex_lock(&instance->guard);
|
---|
73 | instance->devices[USB_ADDRESS_DEFAULT].occupied = false;
|
---|
74 | fibril_mutex_unlock(&instance->guard);
|
---|
75 | fibril_condvar_signal(&instance->default_address_occupied);
|
---|
76 | }
|
---|
77 | /*----------------------------------------------------------------------------*/
|
---|
78 | int device_keeper_get_toggle(device_keeper_t *instance, usb_target_t target)
|
---|
79 | {
|
---|
80 | assert(instance);
|
---|
81 | int ret;
|
---|
82 | fibril_mutex_lock(&instance->guard);
|
---|
83 | if (target.endpoint > 15 || target.endpoint < 0
|
---|
84 | || target.address >= USB_ADDRESS_COUNT || target.address < 0
|
---|
85 | || !instance->devices[target.address].occupied) {
|
---|
86 | ret = EINVAL;
|
---|
87 | } else {
|
---|
88 | ret = (instance->devices[target.address].toggle_status >> target.endpoint) & 1;
|
---|
89 | }
|
---|
90 | fibril_mutex_unlock(&instance->guard);
|
---|
91 | return ret;
|
---|
92 | }
|
---|
93 | /*----------------------------------------------------------------------------*/
|
---|
94 | int device_keeper_set_toggle(
|
---|
95 | device_keeper_t *instance, usb_target_t target, bool toggle)
|
---|
96 | {
|
---|
97 | assert(instance);
|
---|
98 | int ret;
|
---|
99 | fibril_mutex_lock(&instance->guard);
|
---|
100 | if (target.endpoint > 15 || target.endpoint < 0
|
---|
101 | || target.address >= USB_ADDRESS_COUNT || target.address < 0
|
---|
102 | || !instance->devices[target.address].occupied) {
|
---|
103 | ret = EINVAL;
|
---|
104 | } else {
|
---|
105 | if (toggle) {
|
---|
106 | instance->devices[target.address].toggle_status |= (1 << target.endpoint);
|
---|
107 | } else {
|
---|
108 | instance->devices[target.address].toggle_status &= ~(1 << target.endpoint);
|
---|
109 | }
|
---|
110 | ret = EOK;
|
---|
111 | }
|
---|
112 | fibril_mutex_unlock(&instance->guard);
|
---|
113 | return ret;
|
---|
114 | }
|
---|
115 | /*----------------------------------------------------------------------------*/
|
---|
116 | usb_address_t device_keeper_request(
|
---|
117 | device_keeper_t *instance, usb_speed_t speed)
|
---|
118 | {
|
---|
119 | assert(instance);
|
---|
120 | fibril_mutex_lock(&instance->guard);
|
---|
121 |
|
---|
122 | usb_address_t new_address = instance->last_address + 1;
|
---|
123 | while (instance->devices[new_address].occupied) {
|
---|
124 | if (new_address == instance->last_address) {
|
---|
125 | fibril_mutex_unlock(&instance->guard);
|
---|
126 | return ENOSPC;
|
---|
127 | }
|
---|
128 | if (new_address == USB11_ADDRESS_MAX)
|
---|
129 | new_address = 1;
|
---|
130 | ++new_address;
|
---|
131 | }
|
---|
132 |
|
---|
133 | assert(new_address != USB_ADDRESS_DEFAULT);
|
---|
134 | assert(instance->devices[new_address].occupied == false);
|
---|
135 | instance->devices[new_address].occupied = true;
|
---|
136 | instance->devices[new_address].speed = speed;
|
---|
137 | instance->devices[new_address].toggle_status = 0;
|
---|
138 | instance->last_address = new_address;
|
---|
139 | fibril_mutex_unlock(&instance->guard);
|
---|
140 | return new_address;
|
---|
141 | }
|
---|
142 | /*----------------------------------------------------------------------------*/
|
---|
143 | void device_keeper_bind(
|
---|
144 | device_keeper_t *instance, usb_address_t address, devman_handle_t handle)
|
---|
145 | {
|
---|
146 | assert(instance);
|
---|
147 | fibril_mutex_lock(&instance->guard);
|
---|
148 | assert(address > 0);
|
---|
149 | assert(address <= USB11_ADDRESS_MAX);
|
---|
150 | assert(instance->devices[address].occupied);
|
---|
151 | instance->devices[address].handle = handle;
|
---|
152 | fibril_mutex_unlock(&instance->guard);
|
---|
153 | }
|
---|
154 | /*----------------------------------------------------------------------------*/
|
---|
155 | void device_keeper_release(device_keeper_t *instance, usb_address_t address)
|
---|
156 | {
|
---|
157 | assert(instance);
|
---|
158 | assert(address > 0);
|
---|
159 | assert(address <= USB11_ADDRESS_MAX);
|
---|
160 |
|
---|
161 | fibril_mutex_lock(&instance->guard);
|
---|
162 | assert(instance->devices[address].occupied);
|
---|
163 | instance->devices[address].occupied = false;
|
---|
164 | fibril_mutex_unlock(&instance->guard);
|
---|
165 | }
|
---|
166 | /*----------------------------------------------------------------------------*/
|
---|
167 | usb_address_t device_keeper_find(
|
---|
168 | device_keeper_t *instance, devman_handle_t handle)
|
---|
169 | {
|
---|
170 | assert(instance);
|
---|
171 | fibril_mutex_lock(&instance->guard);
|
---|
172 | usb_address_t address = 1;
|
---|
173 | while (address <= USB11_ADDRESS_MAX) {
|
---|
174 | if (instance->devices[address].handle == handle) {
|
---|
175 | fibril_mutex_unlock(&instance->guard);
|
---|
176 | return address;
|
---|
177 | }
|
---|
178 | ++address;
|
---|
179 | }
|
---|
180 | fibril_mutex_unlock(&instance->guard);
|
---|
181 | return ENOENT;
|
---|
182 | }
|
---|
183 | /*----------------------------------------------------------------------------*/
|
---|
184 | usb_speed_t device_keeper_speed(
|
---|
185 | device_keeper_t *instance, usb_address_t address)
|
---|
186 | {
|
---|
187 | assert(instance);
|
---|
188 | assert(address >= 0);
|
---|
189 | assert(address <= USB11_ADDRESS_MAX);
|
---|
190 | return instance->devices[address].speed;
|
---|
191 | }
|
---|
192 | /**
|
---|
193 | * @}
|
---|
194 | */
|
---|