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

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

libusbhost: Merge find_by_address and get_speed.

These functions were almost identical.
Clear handle on address release.

  • Property mode set to 100644
File size: 6.3 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/** Initialize device manager structure.
41 *
42 * @param[in] instance Memory place to initialize.
43 *
44 * Set all values to false/0.
45 */
46void usb_device_manager_init(usb_device_manager_t *instance)
47{
48 assert(instance);
49 unsigned i = 0;
50 for (; i < USB_ADDRESS_COUNT; ++i) {
51 instance->devices[i].occupied = false;
52 instance->devices[i].handle = 0;
53 instance->devices[i].speed = USB_SPEED_MAX;
54 }
55 // TODO: is this hack enough?
56 // (it is needed to allow smooth registration at default address)
57 instance->devices[0].occupied = true;
58 instance->last_address = 0;
59 fibril_mutex_initialize(&instance->guard);
60}
61/*----------------------------------------------------------------------------*/
62/** Get a free USB address
63 *
64 * @param[in] instance Device manager structure to use.
65 * @param[in] speed Speed of the device requiring address.
66 * @return Free address, or error code.
67 */
68usb_address_t usb_device_manager_get_free_address(
69 usb_device_manager_t *instance, usb_speed_t speed)
70{
71 assert(instance);
72 fibril_mutex_lock(&instance->guard);
73
74 usb_address_t new_address = instance->last_address;
75 do {
76 ++new_address;
77 if (new_address > USB11_ADDRESS_MAX)
78 new_address = 1; // NOTE it should be safe to put 0 here
79 // TODO Use mod
80 if (new_address == instance->last_address) {
81 fibril_mutex_unlock(&instance->guard);
82 return ENOSPC;
83 }
84 } while (instance->devices[new_address].occupied);
85
86 assert(new_address != USB_ADDRESS_DEFAULT);
87 assert(instance->devices[new_address].occupied == false);
88 assert(instance->devices[new_address].handle == 0);
89
90 instance->devices[new_address].occupied = true;
91 instance->devices[new_address].speed = speed;
92 instance->last_address = new_address;
93
94 fibril_mutex_unlock(&instance->guard);
95 return new_address;
96}
97/*----------------------------------------------------------------------------*/
98/** Bind USB address to devman handle.
99 *
100 * @param[in] instance Device manager structure to use.
101 * @param[in] address Device address
102 * @param[in] handle Devman handle of the device.
103 */
104void usb_device_manager_bind(usb_device_manager_t *instance,
105 usb_address_t address, devman_handle_t handle)
106{
107 assert(instance);
108 fibril_mutex_lock(&instance->guard);
109
110 assert(address > 0);
111 assert(address <= USB11_ADDRESS_MAX);
112 assert(instance->devices[address].occupied);
113
114 instance->devices[address].handle = handle;
115 fibril_mutex_unlock(&instance->guard);
116}
117/*----------------------------------------------------------------------------*/
118/** Release used USB address.
119 *
120 * @param[in] instance Device manager structure to use.
121 * @param[in] address Device address
122 */
123void usb_device_manager_release(
124 usb_device_manager_t *instance, usb_address_t address)
125{
126 assert(instance);
127 assert(address > 0);
128 assert(address <= USB11_ADDRESS_MAX);
129
130 fibril_mutex_lock(&instance->guard);
131 assert(instance->devices[address].occupied);
132
133 instance->devices[address].occupied = false;
134 instance->devices[address].handle = 0;
135 fibril_mutex_unlock(&instance->guard);
136}
137/*----------------------------------------------------------------------------*/
138/** Find USB address associated with the device
139 *
140 * @param[in] instance Device manager structure to use.
141 * @param[in] handle Devman handle of the device seeking its address.
142 * @return USB Address, or error code.
143 */
144usb_address_t usb_device_manager_find_address(
145 usb_device_manager_t *instance, devman_handle_t handle)
146{
147 assert(instance);
148 fibril_mutex_lock(&instance->guard);
149 usb_address_t address = 1;
150 while (address <= USB11_ADDRESS_MAX) {
151 if (instance->devices[address].handle == handle) {
152 assert(instance->devices[address].occupied);
153 fibril_mutex_unlock(&instance->guard);
154 return address;
155 }
156 ++address;
157 }
158 fibril_mutex_unlock(&instance->guard);
159 return ENOENT;
160}
161/*----------------------------------------------------------------------------*/
162/** Find devman handle and speed assigned to USB address.
163 * Intentionally refuse to work on default address.
164 *
165 * @param[in] instance Device manager structure to use.
166 * @param[in] address Address the caller wants to find.
167 * @param[out] handle Where to store found handle.
168 * @param[out] speed Assigned speed.
169 * @return Error code.
170 */
171int usb_device_manager_get_info_by_address(usb_device_manager_t *instance,
172 usb_address_t address, devman_handle_t *handle, usb_speed_t *speed)
173{
174 assert(instance);
175 if ((address <= 0) || (address >= USB_ADDRESS_COUNT)) {
176 return EINVAL;
177 }
178
179 fibril_mutex_lock(&instance->guard);
180 if (!instance->devices[address].occupied) {
181 fibril_mutex_unlock(&instance->guard);
182 return ENOENT;
183 }
184
185 if (handle != NULL) {
186 *handle = instance->devices[address].handle;
187 }
188 if (speed != NULL) {
189 *speed = instance->devices[address].speed;
190 }
191
192 fibril_mutex_unlock(&instance->guard);
193 return EOK;
194}
195/**
196 * @}
197 */
Note: See TracBrowser for help on using the repository browser.