source: mainline/uspace/lib/usb/src/host/device_keeper.c@ 1fb1339

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

Move device_keeper to libusb

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