source: mainline/uspace/lib/usb/src/host/device_keeper.c@ 506d330

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 506d330 was 506d330, checked in by Jan Vesely <jano.vesely@…>, 15 years ago

Implement per endpoint toggle reset

  • Property mode set to 100644
File size: 9.7 KB
Line 
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 * Device keeper structure and functions (implementation).
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 */
47void usb_device_keeper_init(usb_device_keeper_t *instance)
48{
49 assert(instance);
50 fibril_mutex_initialize(&instance->guard);
51 fibril_condvar_initialize(&instance->change);
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].control_used = 0;
57 instance->devices[i].handle = 0;
58 list_initialize(&instance->devices[i].endpoints);
59 }
60}
61/*----------------------------------------------------------------------------*/
62void usb_device_keeper_add_ep(
63 usb_device_keeper_t *instance, usb_address_t address, endpoint_t *ep)
64{
65 assert(instance);
66 fibril_mutex_lock(&instance->guard);
67 assert(instance->devices[address].occupied);
68 list_append(&ep->same_device_eps, &instance->devices[address].endpoints);
69 fibril_mutex_unlock(&instance->guard);
70}
71/*----------------------------------------------------------------------------*/
72/** Attempt to obtain address 0, blocks.
73 *
74 * @param[in] instance Device keeper structure to use.
75 * @param[in] speed Speed of the device requesting default address.
76 */
77void usb_device_keeper_reserve_default_address(
78 usb_device_keeper_t *instance, usb_speed_t speed)
79{
80 assert(instance);
81 fibril_mutex_lock(&instance->guard);
82 while (instance->devices[USB_ADDRESS_DEFAULT].occupied) {
83 fibril_condvar_wait(&instance->change, &instance->guard);
84 }
85 instance->devices[USB_ADDRESS_DEFAULT].occupied = true;
86 instance->devices[USB_ADDRESS_DEFAULT].speed = speed;
87 fibril_mutex_unlock(&instance->guard);
88}
89/*----------------------------------------------------------------------------*/
90/** Attempt to obtain address 0, blocks.
91 *
92 * @param[in] instance Device keeper structure to use.
93 * @param[in] speed Speed of the device requesting default address.
94 */
95void usb_device_keeper_release_default_address(usb_device_keeper_t *instance)
96{
97 assert(instance);
98 fibril_mutex_lock(&instance->guard);
99 instance->devices[USB_ADDRESS_DEFAULT].occupied = false;
100 fibril_mutex_unlock(&instance->guard);
101 fibril_condvar_signal(&instance->change);
102}
103/*----------------------------------------------------------------------------*/
104/** Check setup packet data for signs of toggle reset.
105 *
106 * @param[in] instance Device keeper structure to use.
107 * @param[in] target Device to receive setup packet.
108 * @param[in] data Setup packet data.
109 *
110 * Really ugly one.
111 */
112void usb_device_keeper_reset_if_need(
113 usb_device_keeper_t *instance, usb_target_t target, const uint8_t *data)
114{
115 assert(instance);
116 fibril_mutex_lock(&instance->guard);
117 if (target.endpoint > 15 || target.endpoint < 0
118 || target.address >= USB_ADDRESS_COUNT || target.address < 0
119 || !instance->devices[target.address].occupied) {
120 fibril_mutex_unlock(&instance->guard);
121 usb_log_error("Invalid data when checking for toggle reset.\n");
122 return;
123 }
124
125 switch (data[1])
126 {
127 case 0x01: /*clear feature*/
128 /* recipient is endpoint, value is zero (ENDPOINT_STALL) */
129 if (((data[0] & 0xf) == 1) && ((data[2] | data[3]) == 0)) {
130 while (current !=
131 &instance->devices[target.address].endpoints)
132 {
133 /* endpoint number is < 16, thus first byte is enough */
134 endpoint_toggle_reset_filtered(
135 current, data[4]);
136 current = current->next;
137 }
138 }
139 break;
140
141 case 0x9: /* set configuration */
142 case 0x11: /* set interface */
143 /* target must be device */
144 if ((data[0] & 0xf) == 0) {
145 link_t *current =
146 instance->devices[target.address].endpoints.next;
147 while (current !=
148 &instance->devices[target.address].endpoints)
149 {
150 endpoint_toggle_reset(current);
151 current = current->next;
152 }
153 }
154 break;
155 }
156 fibril_mutex_unlock(&instance->guard);
157}
158/*----------------------------------------------------------------------------*/
159/** Get a free USB address
160 *
161 * @param[in] instance Device keeper structure to use.
162 * @param[in] speed Speed of the device requiring address.
163 * @return Free address, or error code.
164 */
165usb_address_t device_keeper_get_free_address(
166 usb_device_keeper_t *instance, usb_speed_t speed)
167{
168 assert(instance);
169 fibril_mutex_lock(&instance->guard);
170
171 usb_address_t new_address = instance->last_address;
172 do {
173 ++new_address;
174 if (new_address > USB11_ADDRESS_MAX)
175 new_address = 1;
176 if (new_address == instance->last_address) {
177 fibril_mutex_unlock(&instance->guard);
178 return ENOSPC;
179 }
180 } while (instance->devices[new_address].occupied);
181
182 assert(new_address != USB_ADDRESS_DEFAULT);
183 assert(instance->devices[new_address].occupied == false);
184 instance->devices[new_address].occupied = true;
185 instance->devices[new_address].speed = speed;
186 instance->last_address = new_address;
187 fibril_mutex_unlock(&instance->guard);
188 return new_address;
189}
190/*----------------------------------------------------------------------------*/
191/** Bind USB address to devman handle.
192 *
193 * @param[in] instance Device keeper structure to use.
194 * @param[in] address Device address
195 * @param[in] handle Devman handle of the device.
196 */
197void usb_device_keeper_bind(usb_device_keeper_t *instance,
198 usb_address_t address, devman_handle_t handle)
199{
200 assert(instance);
201 fibril_mutex_lock(&instance->guard);
202 assert(address > 0);
203 assert(address <= USB11_ADDRESS_MAX);
204 assert(instance->devices[address].occupied);
205 instance->devices[address].handle = handle;
206 fibril_mutex_unlock(&instance->guard);
207}
208/*----------------------------------------------------------------------------*/
209/** Release used USB address.
210 *
211 * @param[in] instance Device keeper structure to use.
212 * @param[in] address Device address
213 */
214void usb_device_keeper_release(
215 usb_device_keeper_t *instance, usb_address_t address)
216{
217 assert(instance);
218 assert(address > 0);
219 assert(address <= USB11_ADDRESS_MAX);
220
221 fibril_mutex_lock(&instance->guard);
222 assert(instance->devices[address].occupied);
223 instance->devices[address].occupied = false;
224 fibril_mutex_unlock(&instance->guard);
225}
226/*----------------------------------------------------------------------------*/
227/** Find USB address associated with the device
228 *
229 * @param[in] instance Device keeper structure to use.
230 * @param[in] handle Devman handle of the device seeking its address.
231 * @return USB Address, or error code.
232 */
233usb_address_t usb_device_keeper_find(
234 usb_device_keeper_t *instance, devman_handle_t handle)
235{
236 assert(instance);
237 fibril_mutex_lock(&instance->guard);
238 usb_address_t address = 1;
239 while (address <= USB11_ADDRESS_MAX) {
240 if (instance->devices[address].handle == handle) {
241 fibril_mutex_unlock(&instance->guard);
242 return address;
243 }
244 ++address;
245 }
246 fibril_mutex_unlock(&instance->guard);
247 return ENOENT;
248}
249/*----------------------------------------------------------------------------*/
250/** Get speed associated with the address
251 *
252 * @param[in] instance Device keeper structure to use.
253 * @param[in] address Address of the device.
254 * @return USB speed.
255 */
256usb_speed_t usb_device_keeper_get_speed(
257 usb_device_keeper_t *instance, usb_address_t address)
258{
259 assert(instance);
260 assert(address >= 0);
261 assert(address <= USB11_ADDRESS_MAX);
262 return instance->devices[address].speed;
263}
264/*----------------------------------------------------------------------------*/
265void usb_device_keeper_use_control(
266 usb_device_keeper_t *instance, usb_target_t target)
267{
268 assert(instance);
269 const uint16_t ep = 1 << target.endpoint;
270 fibril_mutex_lock(&instance->guard);
271 while (instance->devices[target.address].control_used & ep) {
272 fibril_condvar_wait(&instance->change, &instance->guard);
273 }
274 instance->devices[target.address].control_used |= ep;
275 fibril_mutex_unlock(&instance->guard);
276}
277/*----------------------------------------------------------------------------*/
278void usb_device_keeper_release_control(
279 usb_device_keeper_t *instance, usb_target_t target)
280{
281 assert(instance);
282 const uint16_t ep = 1 << target.endpoint;
283 fibril_mutex_lock(&instance->guard);
284 assert((instance->devices[target.address].control_used & ep) != 0);
285 instance->devices[target.address].control_used &= ~ep;
286 fibril_mutex_unlock(&instance->guard);
287 fibril_condvar_signal(&instance->change);
288}
289/**
290 * @}
291 */
Note: See TracBrowser for help on using the repository browser.