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

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

libusbhost: Make usb_device_manager more robust.

Return error on incorrect input values.

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