source: mainline/uspace/drv/uhci-hcd/utils/device_keeper.c@ 17ceb72

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

Doxygen

  • 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 drvusbuhcihc
30 * @{
31 */
32/** @file
33 * @brief UHCI driver
34 */
35#include <assert.h>
36#include <errno.h>
37#include <usb/debug.h>
38
39#include "device_keeper.h"
40
41/*----------------------------------------------------------------------------*/
42/** Initialize device keeper structure.
43 *
44 * @param[in] instance Memory place to initialize.
45 *
46 * Set all values to false/0.
47 */
48void device_keeper_init(device_keeper_t *instance)
49{
50 assert(instance);
51 fibril_mutex_initialize(&instance->guard);
52 fibril_condvar_initialize(&instance->default_address_occupied);
53 instance->last_address = 0;
54 unsigned i = 0;
55 for (; i < USB_ADDRESS_COUNT; ++i) {
56 instance->devices[i].occupied = false;
57 instance->devices[i].handle = 0;
58 instance->devices[i].toggle_status = 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 */
67void 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 */
85void 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 */
102void 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 &=
122 ~(1 << data[4]);
123 }
124 break;
125
126 case 0x9: /* set configuration */
127 case 0x11: /* set interface */
128 /* target must be device */
129 if ((data[0] & 0xf) == 0) {
130 instance->devices[target.address].toggle_status = 0;
131 }
132 break;
133 }
134 fibril_mutex_unlock(&instance->guard);
135}
136/*----------------------------------------------------------------------------*/
137/** Get current value of endpoint toggle.
138 *
139 * @param[in] instance Device keeper structure to use.
140 * @param[in] target Device and endpoint used.
141 * @return Error code
142 */
143int device_keeper_get_toggle(device_keeper_t *instance, usb_target_t target)
144{
145 assert(instance);
146 int ret;
147 fibril_mutex_lock(&instance->guard);
148 if (target.endpoint > 15 || target.endpoint < 0
149 || target.address >= USB_ADDRESS_COUNT || target.address < 0
150 || !instance->devices[target.address].occupied) {
151 usb_log_error("Invalid data when asking for toggle value.\n");
152 ret = EINVAL;
153 } else {
154 ret = (instance->devices[target.address].toggle_status
155 >> target.endpoint) & 1;
156 }
157 fibril_mutex_unlock(&instance->guard);
158 return ret;
159}
160/*----------------------------------------------------------------------------*/
161/** Set current value of endpoint toggle.
162 *
163 * @param[in] instance Device keeper structure to use.
164 * @param[in] target Device and endpoint used.
165 * @param[in] toggle Toggle value.
166 * @return Error code.
167 */
168int device_keeper_set_toggle(
169 device_keeper_t *instance, usb_target_t target, bool toggle)
170{
171 assert(instance);
172 int ret;
173 fibril_mutex_lock(&instance->guard);
174 if (target.endpoint > 15 || target.endpoint < 0
175 || target.address >= USB_ADDRESS_COUNT || target.address < 0
176 || !instance->devices[target.address].occupied) {
177 usb_log_error("Invalid data when setting toggle value.\n");
178 ret = EINVAL;
179 } else {
180 if (toggle) {
181 instance->devices[target.address].toggle_status |= (1 << target.endpoint);
182 } else {
183 instance->devices[target.address].toggle_status &= ~(1 << target.endpoint);
184 }
185 ret = EOK;
186 }
187 fibril_mutex_unlock(&instance->guard);
188 return ret;
189}
190/*----------------------------------------------------------------------------*/
191/** Get a free USB address
192 *
193 * @param[in] instance Device keeper structure to use.
194 * @param[in] speed Speed of the device requiring address.
195 * @return Free address, or error code.
196 */
197usb_address_t device_keeper_request(
198 device_keeper_t *instance, usb_speed_t speed)
199{
200 assert(instance);
201 fibril_mutex_lock(&instance->guard);
202
203 usb_address_t new_address = instance->last_address + 1;
204 while (instance->devices[new_address].occupied) {
205 if (new_address == instance->last_address) {
206 fibril_mutex_unlock(&instance->guard);
207 return ENOSPC;
208 }
209 if (new_address == USB11_ADDRESS_MAX)
210 new_address = 1;
211 ++new_address;
212 }
213
214 assert(new_address != USB_ADDRESS_DEFAULT);
215 assert(instance->devices[new_address].occupied == false);
216 instance->devices[new_address].occupied = true;
217 instance->devices[new_address].speed = speed;
218 instance->devices[new_address].toggle_status = 0;
219 instance->last_address = new_address;
220 fibril_mutex_unlock(&instance->guard);
221 return new_address;
222}
223/*----------------------------------------------------------------------------*/
224/** Bind USB address to devman handle.
225 *
226 * @param[in] instance Device keeper structure to use.
227 * @param[in] address Device address
228 * @param[in] handle Devman handle of the device.
229 */
230void device_keeper_bind(
231 device_keeper_t *instance, usb_address_t address, devman_handle_t handle)
232{
233 assert(instance);
234 fibril_mutex_lock(&instance->guard);
235 assert(address > 0);
236 assert(address <= USB11_ADDRESS_MAX);
237 assert(instance->devices[address].occupied);
238 instance->devices[address].handle = handle;
239 fibril_mutex_unlock(&instance->guard);
240}
241/*----------------------------------------------------------------------------*/
242/** Release used USB address.
243 *
244 * @param[in] instance Device keeper structure to use.
245 * @param[in] address Device address
246 */
247void device_keeper_release(device_keeper_t *instance, usb_address_t address)
248{
249 assert(instance);
250 assert(address > 0);
251 assert(address <= USB11_ADDRESS_MAX);
252
253 fibril_mutex_lock(&instance->guard);
254 assert(instance->devices[address].occupied);
255 instance->devices[address].occupied = false;
256 fibril_mutex_unlock(&instance->guard);
257}
258/*----------------------------------------------------------------------------*/
259/** Find USB address associated with the device
260 *
261 * @param[in] instance Device keeper structure to use.
262 * @param[in] handle Devman handle of the device seeking its address.
263 * @return USB Address, or error code.
264 */
265usb_address_t device_keeper_find(
266 device_keeper_t *instance, devman_handle_t handle)
267{
268 assert(instance);
269 fibril_mutex_lock(&instance->guard);
270 usb_address_t address = 1;
271 while (address <= USB11_ADDRESS_MAX) {
272 if (instance->devices[address].handle == handle) {
273 fibril_mutex_unlock(&instance->guard);
274 return address;
275 }
276 ++address;
277 }
278 fibril_mutex_unlock(&instance->guard);
279 return ENOENT;
280}
281/*----------------------------------------------------------------------------*/
282/** Get speed associated with the address
283 *
284 * @param[in] instance Device keeper structure to use.
285 * @param[in] address Address of the device.
286 * @return USB speed.
287 */
288usb_speed_t device_keeper_speed(
289 device_keeper_t *instance, usb_address_t address)
290{
291 assert(instance);
292 assert(address >= 0);
293 assert(address <= USB11_ADDRESS_MAX);
294 return instance->devices[address].speed;
295}
296/**
297 * @}
298 */
Note: See TracBrowser for help on using the repository browser.