source: mainline/uspace/lib/usb/src/host/device_keeper.c@ 049eb87

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

Fix device_keeper compile error, disable legacy for OHCI

  • Property mode set to 100644
File size: 9.8 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 link_t *current =
131 instance->devices[target.address].endpoints.next;
132 while (current !=
133 &instance->devices[target.address].endpoints)
134 {
135 /* endpoint number is < 16, thus first byte is enough */
136 endpoint_toggle_reset_filtered(
137 current, data[4]);
138 current = current->next;
139 }
140 }
141 break;
142
143 case 0x9: /* set configuration */
144 case 0x11: /* set interface */
145 /* target must be device */
146 if ((data[0] & 0xf) == 0) {
147 link_t *current =
148 instance->devices[target.address].endpoints.next;
149 while (current !=
150 &instance->devices[target.address].endpoints)
151 {
152 endpoint_toggle_reset(current);
153 current = current->next;
154 }
155 }
156 break;
157 }
158 fibril_mutex_unlock(&instance->guard);
159}
160/*----------------------------------------------------------------------------*/
161/** Get a free USB address
162 *
163 * @param[in] instance Device keeper structure to use.
164 * @param[in] speed Speed of the device requiring address.
165 * @return Free address, or error code.
166 */
167usb_address_t device_keeper_get_free_address(
168 usb_device_keeper_t *instance, usb_speed_t speed)
169{
170 assert(instance);
171 fibril_mutex_lock(&instance->guard);
172
173 usb_address_t new_address = instance->last_address;
174 do {
175 ++new_address;
176 if (new_address > USB11_ADDRESS_MAX)
177 new_address = 1;
178 if (new_address == instance->last_address) {
179 fibril_mutex_unlock(&instance->guard);
180 return ENOSPC;
181 }
182 } while (instance->devices[new_address].occupied);
183
184 assert(new_address != USB_ADDRESS_DEFAULT);
185 assert(instance->devices[new_address].occupied == false);
186 instance->devices[new_address].occupied = true;
187 instance->devices[new_address].speed = speed;
188 instance->last_address = new_address;
189 fibril_mutex_unlock(&instance->guard);
190 return new_address;
191}
192/*----------------------------------------------------------------------------*/
193/** Bind USB address to devman handle.
194 *
195 * @param[in] instance Device keeper structure to use.
196 * @param[in] address Device address
197 * @param[in] handle Devman handle of the device.
198 */
199void usb_device_keeper_bind(usb_device_keeper_t *instance,
200 usb_address_t address, devman_handle_t handle)
201{
202 assert(instance);
203 fibril_mutex_lock(&instance->guard);
204 assert(address > 0);
205 assert(address <= USB11_ADDRESS_MAX);
206 assert(instance->devices[address].occupied);
207 instance->devices[address].handle = handle;
208 fibril_mutex_unlock(&instance->guard);
209}
210/*----------------------------------------------------------------------------*/
211/** Release used USB address.
212 *
213 * @param[in] instance Device keeper structure to use.
214 * @param[in] address Device address
215 */
216void usb_device_keeper_release(
217 usb_device_keeper_t *instance, usb_address_t address)
218{
219 assert(instance);
220 assert(address > 0);
221 assert(address <= USB11_ADDRESS_MAX);
222
223 fibril_mutex_lock(&instance->guard);
224 assert(instance->devices[address].occupied);
225 instance->devices[address].occupied = false;
226 fibril_mutex_unlock(&instance->guard);
227}
228/*----------------------------------------------------------------------------*/
229/** Find USB address associated with the device
230 *
231 * @param[in] instance Device keeper structure to use.
232 * @param[in] handle Devman handle of the device seeking its address.
233 * @return USB Address, or error code.
234 */
235usb_address_t usb_device_keeper_find(
236 usb_device_keeper_t *instance, devman_handle_t handle)
237{
238 assert(instance);
239 fibril_mutex_lock(&instance->guard);
240 usb_address_t address = 1;
241 while (address <= USB11_ADDRESS_MAX) {
242 if (instance->devices[address].handle == handle) {
243 fibril_mutex_unlock(&instance->guard);
244 return address;
245 }
246 ++address;
247 }
248 fibril_mutex_unlock(&instance->guard);
249 return ENOENT;
250}
251/*----------------------------------------------------------------------------*/
252/** Get speed associated with the address
253 *
254 * @param[in] instance Device keeper structure to use.
255 * @param[in] address Address of the device.
256 * @return USB speed.
257 */
258usb_speed_t usb_device_keeper_get_speed(
259 usb_device_keeper_t *instance, usb_address_t address)
260{
261 assert(instance);
262 assert(address >= 0);
263 assert(address <= USB11_ADDRESS_MAX);
264 return instance->devices[address].speed;
265}
266/*----------------------------------------------------------------------------*/
267void usb_device_keeper_use_control(
268 usb_device_keeper_t *instance, usb_target_t target)
269{
270 assert(instance);
271 const uint16_t ep = 1 << target.endpoint;
272 fibril_mutex_lock(&instance->guard);
273 while (instance->devices[target.address].control_used & ep) {
274 fibril_condvar_wait(&instance->change, &instance->guard);
275 }
276 instance->devices[target.address].control_used |= ep;
277 fibril_mutex_unlock(&instance->guard);
278}
279/*----------------------------------------------------------------------------*/
280void usb_device_keeper_release_control(
281 usb_device_keeper_t *instance, usb_target_t target)
282{
283 assert(instance);
284 const uint16_t ep = 1 << target.endpoint;
285 fibril_mutex_lock(&instance->guard);
286 assert((instance->devices[target.address].control_used & ep) != 0);
287 instance->devices[target.address].control_used &= ~ep;
288 fibril_mutex_unlock(&instance->guard);
289 fibril_condvar_signal(&instance->change);
290}
291/**
292 * @}
293 */
Note: See TracBrowser for help on using the repository browser.