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