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 | /** Initializes device keeper structure.
|
---|
42 | *
|
---|
43 | * @param[in] instance Memory place to initialize.
|
---|
44 | */
|
---|
45 | void device_keeper_init(device_keeper_t *instance)
|
---|
46 | {
|
---|
47 | assert(instance);
|
---|
48 | fibril_mutex_initialize(&instance->guard);
|
---|
49 | fibril_condvar_initialize(&instance->default_address_occupied);
|
---|
50 | instance->last_address = 0;
|
---|
51 | unsigned i = 0;
|
---|
52 | for (; i < USB_ADDRESS_COUNT; ++i) {
|
---|
53 | instance->devices[i].occupied = false;
|
---|
54 | instance->devices[i].handle = 0;
|
---|
55 | instance->devices[i].toggle_status = 0;
|
---|
56 | }
|
---|
57 | }
|
---|
58 | /*----------------------------------------------------------------------------*/
|
---|
59 | /** Attempts to obtain address 0, blocks.
|
---|
60 | *
|
---|
61 | * @param[in] instance Device keeper structure to use.
|
---|
62 | * @param[in] speed Speed of the device requesting default address.
|
---|
63 | */
|
---|
64 | void device_keeper_reserve_default(device_keeper_t *instance, usb_speed_t speed)
|
---|
65 | {
|
---|
66 | assert(instance);
|
---|
67 | fibril_mutex_lock(&instance->guard);
|
---|
68 | while (instance->devices[USB_ADDRESS_DEFAULT].occupied) {
|
---|
69 | fibril_condvar_wait(&instance->default_address_occupied,
|
---|
70 | &instance->guard);
|
---|
71 | }
|
---|
72 | instance->devices[USB_ADDRESS_DEFAULT].occupied = true;
|
---|
73 | instance->devices[USB_ADDRESS_DEFAULT].speed = speed;
|
---|
74 | fibril_mutex_unlock(&instance->guard);
|
---|
75 | }
|
---|
76 | /*----------------------------------------------------------------------------*/
|
---|
77 | /** Attempts to obtain address 0, blocks.
|
---|
78 | *
|
---|
79 | * @param[in] instance Device keeper structure to use.
|
---|
80 | * @param[in] speed Speed of the device requesting default address.
|
---|
81 | */
|
---|
82 | void device_keeper_release_default(device_keeper_t *instance)
|
---|
83 | {
|
---|
84 | assert(instance);
|
---|
85 | fibril_mutex_lock(&instance->guard);
|
---|
86 | instance->devices[USB_ADDRESS_DEFAULT].occupied = false;
|
---|
87 | fibril_mutex_unlock(&instance->guard);
|
---|
88 | fibril_condvar_signal(&instance->default_address_occupied);
|
---|
89 | }
|
---|
90 | /*----------------------------------------------------------------------------*/
|
---|
91 | /** Checks setup data for signs of toggle reset.
|
---|
92 | *
|
---|
93 | * @param[in] instance Device keeper structure to use.
|
---|
94 | * @param[in] target Device to receive setup packet.
|
---|
95 | * @param[in] data Setup packet data.
|
---|
96 | */
|
---|
97 | void device_keeper_reset_if_need(
|
---|
98 | device_keeper_t *instance, usb_target_t target, const unsigned char *data)
|
---|
99 | {
|
---|
100 | assert(instance);
|
---|
101 | fibril_mutex_lock(&instance->guard);
|
---|
102 | if (target.endpoint > 15 || target.endpoint < 0
|
---|
103 | || target.address >= USB_ADDRESS_COUNT || target.address < 0
|
---|
104 | || !instance->devices[target.address].occupied) {
|
---|
105 | fibril_mutex_unlock(&instance->guard);
|
---|
106 | return;
|
---|
107 | }
|
---|
108 |
|
---|
109 | switch (data[1])
|
---|
110 | {
|
---|
111 | case 0x01: /*clear feature*/
|
---|
112 | /* recipient is endpoint, value is zero (ENDPOINT_STALL) */
|
---|
113 | if (((data[0] & 0xf) == 1) && ((data[2] | data[3]) == 0)) {
|
---|
114 | /* endpoint number is < 16, thus first byte is enough */
|
---|
115 | instance->devices[target.address].toggle_status &=
|
---|
116 | ~(1 << data[4]);
|
---|
117 | }
|
---|
118 | break;
|
---|
119 |
|
---|
120 | case 0x9: /* set configuration */
|
---|
121 | case 0x11: /* set interface */
|
---|
122 | instance->devices[target.address].toggle_status = 0;
|
---|
123 | break;
|
---|
124 | }
|
---|
125 | fibril_mutex_unlock(&instance->guard);
|
---|
126 | }
|
---|
127 | /*----------------------------------------------------------------------------*/
|
---|
128 | /** Gets current value of endpoint toggle.
|
---|
129 | *
|
---|
130 | * @param[in] instance Device keeper structure to use.
|
---|
131 | * @param[in] target Device and endpoint used.
|
---|
132 | * @return Error code
|
---|
133 | */
|
---|
134 | int device_keeper_get_toggle(device_keeper_t *instance, usb_target_t target)
|
---|
135 | {
|
---|
136 | assert(instance);
|
---|
137 | int ret;
|
---|
138 | fibril_mutex_lock(&instance->guard);
|
---|
139 | if (target.endpoint > 15 || target.endpoint < 0
|
---|
140 | || target.address >= USB_ADDRESS_COUNT || target.address < 0
|
---|
141 | || !instance->devices[target.address].occupied) {
|
---|
142 | ret = EINVAL;
|
---|
143 | } else {
|
---|
144 | ret =
|
---|
145 | (instance->devices[target.address].toggle_status
|
---|
146 | >> target.endpoint) & 1;
|
---|
147 | }
|
---|
148 | fibril_mutex_unlock(&instance->guard);
|
---|
149 | return ret;
|
---|
150 | }
|
---|
151 | /*----------------------------------------------------------------------------*/
|
---|
152 | /** Sets current value of endpoint toggle.
|
---|
153 | *
|
---|
154 | * @param[in] instance Device keeper structure to use.
|
---|
155 | * @param[in] target Device and endpoint used.
|
---|
156 | * @param[in] toggle Current toggle value.
|
---|
157 | * @return Error code.
|
---|
158 | */
|
---|
159 | int device_keeper_set_toggle(
|
---|
160 | device_keeper_t *instance, usb_target_t target, bool toggle)
|
---|
161 | {
|
---|
162 | assert(instance);
|
---|
163 | int ret;
|
---|
164 | fibril_mutex_lock(&instance->guard);
|
---|
165 | if (target.endpoint > 15 || target.endpoint < 0
|
---|
166 | || target.address >= USB_ADDRESS_COUNT || target.address < 0
|
---|
167 | || !instance->devices[target.address].occupied) {
|
---|
168 | ret = EINVAL;
|
---|
169 | } else {
|
---|
170 | if (toggle) {
|
---|
171 | instance->devices[target.address].toggle_status |= (1 << target.endpoint);
|
---|
172 | } else {
|
---|
173 | instance->devices[target.address].toggle_status &= ~(1 << target.endpoint);
|
---|
174 | }
|
---|
175 | ret = EOK;
|
---|
176 | }
|
---|
177 | fibril_mutex_unlock(&instance->guard);
|
---|
178 | return ret;
|
---|
179 | }
|
---|
180 | /*----------------------------------------------------------------------------*/
|
---|
181 | /** Gets a free USB address
|
---|
182 | *
|
---|
183 | * @param[in] instance Device keeper structure to use.
|
---|
184 | * @param[in] speed Speed of the device requiring address.
|
---|
185 | * @return Free address, or error code.
|
---|
186 | */
|
---|
187 | usb_address_t device_keeper_request(
|
---|
188 | device_keeper_t *instance, usb_speed_t speed)
|
---|
189 | {
|
---|
190 | assert(instance);
|
---|
191 | fibril_mutex_lock(&instance->guard);
|
---|
192 |
|
---|
193 | usb_address_t new_address = instance->last_address + 1;
|
---|
194 | while (instance->devices[new_address].occupied) {
|
---|
195 | if (new_address == instance->last_address) {
|
---|
196 | fibril_mutex_unlock(&instance->guard);
|
---|
197 | return ENOSPC;
|
---|
198 | }
|
---|
199 | if (new_address == USB11_ADDRESS_MAX)
|
---|
200 | new_address = 1;
|
---|
201 | ++new_address;
|
---|
202 | }
|
---|
203 |
|
---|
204 | assert(new_address != USB_ADDRESS_DEFAULT);
|
---|
205 | assert(instance->devices[new_address].occupied == false);
|
---|
206 | instance->devices[new_address].occupied = true;
|
---|
207 | instance->devices[new_address].speed = speed;
|
---|
208 | instance->devices[new_address].toggle_status = 0;
|
---|
209 | instance->last_address = new_address;
|
---|
210 | fibril_mutex_unlock(&instance->guard);
|
---|
211 | return new_address;
|
---|
212 | }
|
---|
213 | /*----------------------------------------------------------------------------*/
|
---|
214 | /** Binds USB address to devman handle.
|
---|
215 | *
|
---|
216 | * @param[in] instance Device keeper structure to use.
|
---|
217 | * @param[in] address Device address
|
---|
218 | * @param[in] handle Devman handle of the device.
|
---|
219 | */
|
---|
220 | void device_keeper_bind(
|
---|
221 | device_keeper_t *instance, usb_address_t address, devman_handle_t handle)
|
---|
222 | {
|
---|
223 | assert(instance);
|
---|
224 | fibril_mutex_lock(&instance->guard);
|
---|
225 | assert(address > 0);
|
---|
226 | assert(address <= USB11_ADDRESS_MAX);
|
---|
227 | assert(instance->devices[address].occupied);
|
---|
228 | instance->devices[address].handle = handle;
|
---|
229 | fibril_mutex_unlock(&instance->guard);
|
---|
230 | }
|
---|
231 | /*----------------------------------------------------------------------------*/
|
---|
232 | /** Releases used USB address.
|
---|
233 | *
|
---|
234 | * @param[in] instance Device keeper structure to use.
|
---|
235 | * @param[in] address Device address
|
---|
236 | */
|
---|
237 | void device_keeper_release(device_keeper_t *instance, usb_address_t address)
|
---|
238 | {
|
---|
239 | assert(instance);
|
---|
240 | assert(address > 0);
|
---|
241 | assert(address <= USB11_ADDRESS_MAX);
|
---|
242 |
|
---|
243 | fibril_mutex_lock(&instance->guard);
|
---|
244 | assert(instance->devices[address].occupied);
|
---|
245 | instance->devices[address].occupied = false;
|
---|
246 | fibril_mutex_unlock(&instance->guard);
|
---|
247 | }
|
---|
248 | /*----------------------------------------------------------------------------*/
|
---|
249 | /** Finds USB address associated with the device
|
---|
250 | *
|
---|
251 | * @param[in] instance Device keeper structure to use.
|
---|
252 | * @param[in] handle Devman handle of the device seeking its address.
|
---|
253 | * @return USB Address, or error code.
|
---|
254 | */
|
---|
255 | usb_address_t device_keeper_find(
|
---|
256 | device_keeper_t *instance, devman_handle_t handle)
|
---|
257 | {
|
---|
258 | assert(instance);
|
---|
259 | fibril_mutex_lock(&instance->guard);
|
---|
260 | usb_address_t address = 1;
|
---|
261 | while (address <= USB11_ADDRESS_MAX) {
|
---|
262 | if (instance->devices[address].handle == handle) {
|
---|
263 | fibril_mutex_unlock(&instance->guard);
|
---|
264 | return address;
|
---|
265 | }
|
---|
266 | ++address;
|
---|
267 | }
|
---|
268 | fibril_mutex_unlock(&instance->guard);
|
---|
269 | return ENOENT;
|
---|
270 | }
|
---|
271 | /*----------------------------------------------------------------------------*/
|
---|
272 | /** Gets speed associated with the address
|
---|
273 | *
|
---|
274 | * @param[in] instance Device keeper structure to use.
|
---|
275 | * @param[in] address Address of the device.
|
---|
276 | * @return USB speed.
|
---|
277 | */
|
---|
278 | usb_speed_t device_keeper_speed(
|
---|
279 | device_keeper_t *instance, usb_address_t address)
|
---|
280 | {
|
---|
281 | assert(instance);
|
---|
282 | assert(address >= 0);
|
---|
283 | assert(address <= USB11_ADDRESS_MAX);
|
---|
284 | return instance->devices[address].speed;
|
---|
285 | }
|
---|
286 | /**
|
---|
287 | * @}
|
---|
288 | */
|
---|