source: mainline/uspace/lib/usbhost/src/usb_device_manager.c@ 6b6fc232

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

libusbhost, libusbdev: Fix releasing default address.

  • Property mode set to 100644
File size: 7.6 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 libusbhost
30 * @{
31 */
32/** @file
33 * Device manager structure and functions (implementation).
34 */
35#include <assert.h>
36#include <errno.h>
37#include <usb/debug.h>
38#include <usb/host/usb_device_manager.h>
39
40/** Get a free USB address
41 *
42 * @param[in] instance Device manager structure to use.
43 * @param[in] speed Speed of the device requiring address.
44 * @return Free address, or error code.
45 */
46static usb_address_t usb_device_manager_get_free_address(
47 usb_device_manager_t *instance)
48{
49
50 usb_address_t new_address = instance->last_address;
51 do {
52 new_address = (new_address + 1) % USB_ADDRESS_COUNT;
53 if (new_address == USB_ADDRESS_DEFAULT)
54 new_address = 1;
55 if (new_address == instance->last_address) {
56 return ENOSPC;
57 }
58 } while (instance->devices[new_address].occupied);
59
60 assert(new_address != USB_ADDRESS_DEFAULT);
61 instance->last_address = new_address;
62
63 return new_address;
64}
65/*----------------------------------------------------------------------------*/
66/** Initialize device manager structure.
67 *
68 * @param[in] instance Memory place to initialize.
69 * @param[in] max_speed Maximum allowed USB speed of devices (inclusive).
70 *
71 * Set all values to false/0.
72 */
73void usb_device_manager_init(
74 usb_device_manager_t *instance, usb_speed_t max_speed)
75{
76 assert(instance);
77 for (unsigned i = 0; i < USB_ADDRESS_COUNT; ++i) {
78 instance->devices[i].occupied = false;
79 instance->devices[i].handle = 0;
80 instance->devices[i].speed = USB_SPEED_MAX;
81 }
82 instance->last_address = 0;
83 instance->max_speed = max_speed;
84 fibril_mutex_initialize(&instance->guard);
85}
86/*----------------------------------------------------------------------------*/
87/** Request USB address.
88 * @param instance usb_device_manager
89 * @param address Pointer to requested address value, place to store new address
90 * @parma strict Fail if the requested address is not available.
91 * @return Error code.
92 * @note Default address is only available in strict mode.
93 */
94int usb_device_manager_request_address(usb_device_manager_t *instance,
95 usb_address_t *address, bool strict, usb_speed_t speed)
96{
97 assert(instance);
98 assert(address);
99 if (speed > instance->max_speed)
100 return ENOTSUP;
101
102 if ((*address) < 0 || (*address) >= USB_ADDRESS_COUNT)
103 return EINVAL;
104
105 fibril_mutex_lock(&instance->guard);
106 /* Only grant default address to strict requests */
107 if (( (*address) == USB_ADDRESS_DEFAULT) && !strict) {
108 *address = instance->last_address;
109 }
110
111 if (instance->devices[*address].occupied) {
112 if (strict) {
113 fibril_mutex_unlock(&instance->guard);
114 return ENOENT;
115 }
116 *address = usb_device_manager_get_free_address(instance);
117 }
118 assert(instance->devices[*address].occupied == false);
119 assert(instance->devices[*address].handle == 0);
120
121 instance->devices[*address].occupied = true;
122 instance->devices[*address].speed = speed;
123
124 fibril_mutex_unlock(&instance->guard);
125 return EOK;
126}
127/*----------------------------------------------------------------------------*/
128/** Bind USB address to devman handle.
129 *
130 * @param[in] instance Device manager structure to use.
131 * @param[in] address Device address
132 * @param[in] handle Devman handle of the device.
133 * @return Error code.
134 */
135int usb_device_manager_bind_address(usb_device_manager_t *instance,
136 usb_address_t address, devman_handle_t handle)
137{
138 if ((address <= 0) || (address >= USB_ADDRESS_COUNT)) {
139 return EINVAL;
140 }
141 assert(instance);
142
143 fibril_mutex_lock(&instance->guard);
144 /* Not reserved */
145 if (!instance->devices[address].occupied) {
146 fibril_mutex_unlock(&instance->guard);
147 return ENOENT;
148 }
149 /* Already bound */
150 if (instance->devices[address].handle != 0) {
151 fibril_mutex_unlock(&instance->guard);
152 return EEXISTS;
153 }
154 instance->devices[address].handle = handle;
155 fibril_mutex_unlock(&instance->guard);
156 return EOK;
157}
158/*----------------------------------------------------------------------------*/
159/** Release used USB address.
160 *
161 * @param[in] instance Device manager structure to use.
162 * @param[in] address Device address
163 * @return Error code.
164 */
165int usb_device_manager_release_address(
166 usb_device_manager_t *instance, usb_address_t address)
167{
168 if ((address < 0) || (address >= USB_ADDRESS_COUNT)) {
169 return EINVAL;
170 }
171 assert(instance);
172
173 fibril_mutex_lock(&instance->guard);
174 if (!instance->devices[address].occupied) {
175 fibril_mutex_unlock(&instance->guard);
176 return ENOENT;
177 }
178
179 instance->devices[address].occupied = false;
180 instance->devices[address].handle = 0;
181 fibril_mutex_unlock(&instance->guard);
182 return EOK;
183}
184/*----------------------------------------------------------------------------*/
185/** Find USB address associated with the device
186 *
187 * @param[in] instance Device manager structure to use.
188 * @param[in] handle Devman handle of the device seeking its address.
189 * @return USB Address, or error code.
190 */
191usb_address_t usb_device_manager_find_address(
192 usb_device_manager_t *instance, devman_handle_t handle)
193{
194 assert(instance);
195 fibril_mutex_lock(&instance->guard);
196 for (usb_address_t address = 1; address <= USB11_ADDRESS_MAX; ++address)
197 {
198 if (instance->devices[address].handle == handle) {
199 assert(instance->devices[address].occupied);
200 fibril_mutex_unlock(&instance->guard);
201 return address;
202 }
203 }
204 fibril_mutex_unlock(&instance->guard);
205 return ENOENT;
206}
207/*----------------------------------------------------------------------------*/
208/** Find devman handle and speed assigned to USB address.
209 * Intentionally refuse to work on default address.
210 *
211 * @param[in] instance Device manager structure to use.
212 * @param[in] address Address the caller wants to find.
213 * @param[out] handle Where to store found handle.
214 * @param[out] speed Assigned speed.
215 * @return Error code.
216 */
217int usb_device_manager_get_info_by_address(usb_device_manager_t *instance,
218 usb_address_t address, devman_handle_t *handle, usb_speed_t *speed)
219{
220 assert(instance);
221 if ((address < 0) || (address >= USB_ADDRESS_COUNT)) {
222 return EINVAL;
223 }
224
225 fibril_mutex_lock(&instance->guard);
226 if (!instance->devices[address].occupied) {
227 fibril_mutex_unlock(&instance->guard);
228 return ENOENT;
229 }
230
231 if (handle != NULL) {
232 *handle = instance->devices[address].handle;
233 }
234 if (speed != NULL) {
235 *speed = instance->devices[address].speed;
236 }
237
238 fibril_mutex_unlock(&instance->guard);
239 return EOK;
240}
241/**
242 * @}
243 */
Note: See TracBrowser for help on using the repository browser.