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

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

Don't keep endpoints in two separate structures

Fixes crash "[HelenOS-USB-devel] uhci root hub" email
Move toggle resert testing ugliness to endpoint manager

  • Property mode set to 100644
File size: 7.0 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].handle = 0;
57 instance->devices[i].speed = USB_SPEED_MAX;
58 }
59 // TODO: is this hack enough?
60 // (it is needed to allow smooth registration at default address)
61 instance->devices[0].occupied = true;
62}
63/*----------------------------------------------------------------------------*/
64/** Attempt to obtain address 0, blocks.
65 *
66 * @param[in] instance Device keeper structure to use.
67 * @param[in] speed Speed of the device requesting default address.
68 */
69void usb_device_keeper_reserve_default_address(
70 usb_device_keeper_t *instance, usb_speed_t speed)
71{
72 assert(instance);
73 fibril_mutex_lock(&instance->guard);
74 while (instance->devices[USB_ADDRESS_DEFAULT].occupied) {
75 fibril_condvar_wait(&instance->change, &instance->guard);
76 }
77 instance->devices[USB_ADDRESS_DEFAULT].occupied = true;
78 instance->devices[USB_ADDRESS_DEFAULT].speed = speed;
79 fibril_mutex_unlock(&instance->guard);
80}
81/*----------------------------------------------------------------------------*/
82/** Attempt to obtain address 0, blocks.
83 *
84 * @param[in] instance Device keeper structure to use.
85 * @param[in] speed Speed of the device requesting default address.
86 */
87void usb_device_keeper_release_default_address(usb_device_keeper_t *instance)
88{
89 assert(instance);
90 fibril_mutex_lock(&instance->guard);
91 instance->devices[USB_ADDRESS_DEFAULT].occupied = false;
92 fibril_mutex_unlock(&instance->guard);
93 fibril_condvar_signal(&instance->change);
94}
95/*----------------------------------------------------------------------------*/
96/*----------------------------------------------------------------------------*/
97/** Get a free USB address
98 *
99 * @param[in] instance Device keeper structure to use.
100 * @param[in] speed Speed of the device requiring address.
101 * @return Free address, or error code.
102 */
103usb_address_t device_keeper_get_free_address(
104 usb_device_keeper_t *instance, usb_speed_t speed)
105{
106 assert(instance);
107 fibril_mutex_lock(&instance->guard);
108
109 usb_address_t new_address = instance->last_address;
110 do {
111 ++new_address;
112 if (new_address > USB11_ADDRESS_MAX)
113 new_address = 1;
114 if (new_address == instance->last_address) {
115 fibril_mutex_unlock(&instance->guard);
116 return ENOSPC;
117 }
118 } while (instance->devices[new_address].occupied);
119
120 assert(new_address != USB_ADDRESS_DEFAULT);
121 assert(instance->devices[new_address].occupied == false);
122 instance->devices[new_address].occupied = true;
123 instance->devices[new_address].speed = speed;
124 instance->last_address = new_address;
125 fibril_mutex_unlock(&instance->guard);
126 return new_address;
127}
128/*----------------------------------------------------------------------------*/
129/** Bind USB address to devman handle.
130 *
131 * @param[in] instance Device keeper structure to use.
132 * @param[in] address Device address
133 * @param[in] handle Devman handle of the device.
134 */
135void usb_device_keeper_bind(usb_device_keeper_t *instance,
136 usb_address_t address, devman_handle_t handle)
137{
138 assert(instance);
139 fibril_mutex_lock(&instance->guard);
140 assert(address > 0);
141 assert(address <= USB11_ADDRESS_MAX);
142 assert(instance->devices[address].occupied);
143 instance->devices[address].handle = handle;
144 fibril_mutex_unlock(&instance->guard);
145}
146/*----------------------------------------------------------------------------*/
147/** Release used USB address.
148 *
149 * @param[in] instance Device keeper structure to use.
150 * @param[in] address Device address
151 */
152void usb_device_keeper_release(
153 usb_device_keeper_t *instance, usb_address_t address)
154{
155 assert(instance);
156 assert(address > 0);
157 assert(address <= USB11_ADDRESS_MAX);
158
159 fibril_mutex_lock(&instance->guard);
160 assert(instance->devices[address].occupied);
161 instance->devices[address].occupied = false;
162 fibril_mutex_unlock(&instance->guard);
163}
164/*----------------------------------------------------------------------------*/
165/** Find USB address associated with the device
166 *
167 * @param[in] instance Device keeper structure to use.
168 * @param[in] handle Devman handle of the device seeking its address.
169 * @return USB Address, or error code.
170 */
171usb_address_t usb_device_keeper_find(
172 usb_device_keeper_t *instance, devman_handle_t handle)
173{
174 assert(instance);
175 fibril_mutex_lock(&instance->guard);
176 usb_address_t address = 1;
177 while (address <= USB11_ADDRESS_MAX) {
178 if (instance->devices[address].handle == handle) {
179 fibril_mutex_unlock(&instance->guard);
180 return address;
181 }
182 ++address;
183 }
184 fibril_mutex_unlock(&instance->guard);
185 return ENOENT;
186}
187/*----------------------------------------------------------------------------*/
188/** Get speed associated with the address
189 *
190 * @param[in] instance Device keeper structure to use.
191 * @param[in] address Address of the device.
192 * @return USB speed.
193 */
194usb_speed_t usb_device_keeper_get_speed(
195 usb_device_keeper_t *instance, usb_address_t address)
196{
197 assert(instance);
198 assert(address >= 0);
199 assert(address <= USB11_ADDRESS_MAX);
200 return instance->devices[address].speed;
201}
202/**
203 * @}
204 */
Note: See TracBrowser for help on using the repository browser.