source: mainline/uspace/drv/uhci-hcd/utils/device_keeper.c@ 57c0a7e

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

Add setup data parsing and toggle reset

  • Property mode set to 100644
File size: 7.5 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 drvusbuhci
30 * @{
31 */
32/** @file
33 * @brief UHCI driver
34 */
35#include <assert.h>
36#include <errno.h>
37
38#include "device_keeper.h"
39
40/*----------------------------------------------------------------------------*/
41void device_keeper_init(device_keeper_t *instance)
42{
43 assert(instance);
44 fibril_mutex_initialize(&instance->guard);
45 fibril_condvar_initialize(&instance->default_address_occupied);
46 instance->last_address = 0;
47 unsigned i = 0;
48 for (; i < USB_ADDRESS_COUNT; ++i) {
49 instance->devices[i].occupied = false;
50 instance->devices[i].handle = 0;
51 instance->devices[i].toggle_status = 0;
52 }
53}
54/*----------------------------------------------------------------------------*/
55void device_keeper_reserve_default(
56 device_keeper_t *instance, usb_speed_t speed)
57{
58 assert(instance);
59 fibril_mutex_lock(&instance->guard);
60 while (instance->devices[USB_ADDRESS_DEFAULT].occupied) {
61 fibril_condvar_wait(&instance->default_address_occupied,
62 &instance->guard);
63 }
64 instance->devices[USB_ADDRESS_DEFAULT].occupied = true;
65 instance->devices[USB_ADDRESS_DEFAULT].speed = speed;
66 fibril_mutex_unlock(&instance->guard);
67}
68/*----------------------------------------------------------------------------*/
69void device_keeper_release_default(device_keeper_t *instance)
70{
71 assert(instance);
72 fibril_mutex_lock(&instance->guard);
73 instance->devices[USB_ADDRESS_DEFAULT].occupied = false;
74 fibril_mutex_unlock(&instance->guard);
75 fibril_condvar_signal(&instance->default_address_occupied);
76}
77/*----------------------------------------------------------------------------*/
78void device_keeper_reset_if_need(
79 device_keeper_t *instance, usb_target_t target, const unsigned char *data)
80{
81 assert(instance);
82 fibril_mutex_lock(&instance->guard);
83 if (target.endpoint > 15 || target.endpoint < 0
84 || target.address >= USB_ADDRESS_COUNT || target.address < 0
85 || !instance->devices[target.address].occupied) {
86 goto the_end;
87 }
88
89 switch (data[1])
90 {
91 case 0x01: /*clear feature*/
92 /* recipient is enpoint, value is zero (ENDPOINT_STALL) */
93 if (((data[0] & 0xf) == 1) && ((data[2] | data[3]) == 0)) {
94 /* enpoint number is < 16, thus first byte is enough */
95 instance->devices[target.address].toggle_status &= ~(1 << data[4]);
96 }
97 break;
98
99 case 0x9: /* set configuration */
100 case 0x11: /* set interface */
101 instance->devices[target.address].toggle_status = 0;
102 break;
103 }
104the_end:
105 fibril_mutex_unlock(&instance->guard);
106}
107/*----------------------------------------------------------------------------*/
108int device_keeper_get_toggle(device_keeper_t *instance, usb_target_t target)
109{
110 assert(instance);
111 int ret;
112 fibril_mutex_lock(&instance->guard);
113 if (target.endpoint > 15 || target.endpoint < 0
114 || target.address >= USB_ADDRESS_COUNT || target.address < 0
115 || !instance->devices[target.address].occupied) {
116 ret = EINVAL;
117 } else {
118 ret = (instance->devices[target.address].toggle_status >> target.endpoint) & 1;
119 }
120 fibril_mutex_unlock(&instance->guard);
121 return ret;
122}
123/*----------------------------------------------------------------------------*/
124int device_keeper_set_toggle(
125 device_keeper_t *instance, usb_target_t target, bool toggle)
126{
127 assert(instance);
128 int ret;
129 fibril_mutex_lock(&instance->guard);
130 if (target.endpoint > 15 || target.endpoint < 0
131 || target.address >= USB_ADDRESS_COUNT || target.address < 0
132 || !instance->devices[target.address].occupied) {
133 ret = EINVAL;
134 } else {
135 if (toggle) {
136 instance->devices[target.address].toggle_status |= (1 << target.endpoint);
137 } else {
138 instance->devices[target.address].toggle_status &= ~(1 << target.endpoint);
139 }
140 ret = EOK;
141 }
142 fibril_mutex_unlock(&instance->guard);
143 return ret;
144}
145/*----------------------------------------------------------------------------*/
146usb_address_t device_keeper_request(
147 device_keeper_t *instance, usb_speed_t speed)
148{
149 assert(instance);
150 fibril_mutex_lock(&instance->guard);
151
152 usb_address_t new_address = instance->last_address + 1;
153 while (instance->devices[new_address].occupied) {
154 if (new_address == instance->last_address) {
155 fibril_mutex_unlock(&instance->guard);
156 return ENOSPC;
157 }
158 if (new_address == USB11_ADDRESS_MAX)
159 new_address = 1;
160 ++new_address;
161 }
162
163 assert(new_address != USB_ADDRESS_DEFAULT);
164 assert(instance->devices[new_address].occupied == false);
165 instance->devices[new_address].occupied = true;
166 instance->devices[new_address].speed = speed;
167 instance->devices[new_address].toggle_status = 0;
168 instance->last_address = new_address;
169 fibril_mutex_unlock(&instance->guard);
170 return new_address;
171}
172/*----------------------------------------------------------------------------*/
173void device_keeper_bind(
174 device_keeper_t *instance, usb_address_t address, devman_handle_t handle)
175{
176 assert(instance);
177 fibril_mutex_lock(&instance->guard);
178 assert(address > 0);
179 assert(address <= USB11_ADDRESS_MAX);
180 assert(instance->devices[address].occupied);
181 instance->devices[address].handle = handle;
182 fibril_mutex_unlock(&instance->guard);
183}
184/*----------------------------------------------------------------------------*/
185void device_keeper_release(device_keeper_t *instance, usb_address_t address)
186{
187 assert(instance);
188 assert(address > 0);
189 assert(address <= USB11_ADDRESS_MAX);
190
191 fibril_mutex_lock(&instance->guard);
192 assert(instance->devices[address].occupied);
193 instance->devices[address].occupied = false;
194 fibril_mutex_unlock(&instance->guard);
195}
196/*----------------------------------------------------------------------------*/
197usb_address_t device_keeper_find(
198 device_keeper_t *instance, devman_handle_t handle)
199{
200 assert(instance);
201 fibril_mutex_lock(&instance->guard);
202 usb_address_t address = 1;
203 while (address <= USB11_ADDRESS_MAX) {
204 if (instance->devices[address].handle == handle) {
205 fibril_mutex_unlock(&instance->guard);
206 return address;
207 }
208 ++address;
209 }
210 fibril_mutex_unlock(&instance->guard);
211 return ENOENT;
212}
213/*----------------------------------------------------------------------------*/
214usb_speed_t device_keeper_speed(
215 device_keeper_t *instance, usb_address_t address)
216{
217 assert(instance);
218 assert(address >= 0);
219 assert(address <= USB11_ADDRESS_MAX);
220 return instance->devices[address].speed;
221}
222/**
223 * @}
224 */
Note: See TracBrowser for help on using the repository browser.