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