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

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

Unused code elimination

  • Property mode set to 100644
File size: 5.7 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 unsigned i = 0;
51 for (; i < USB_ADDRESS_COUNT; ++i) {
52 instance->devices[i].occupied = false;
53 instance->devices[i].handle = 0;
54 instance->devices[i].speed = USB_SPEED_MAX;
55 }
56 // TODO: is this hack enough?
57 // (it is needed to allow smooth registration at default address)
58 instance->devices[0].occupied = true;
59 instance->last_address = 0;
60 fibril_mutex_initialize(&instance->guard);
61}
62/*----------------------------------------------------------------------------*/
63/** Get a free USB address
64 *
65 * @param[in] instance Device keeper structure to use.
66 * @param[in] speed Speed of the device requiring address.
67 * @return Free address, or error code.
68 */
69usb_address_t device_keeper_get_free_address(
70 usb_device_keeper_t *instance, usb_speed_t speed)
71{
72 assert(instance);
73 fibril_mutex_lock(&instance->guard);
74
75 usb_address_t new_address = instance->last_address;
76 do {
77 ++new_address;
78 if (new_address > USB11_ADDRESS_MAX)
79 new_address = 1;
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
89 instance->devices[new_address].occupied = true;
90 instance->devices[new_address].speed = speed;
91 instance->last_address = new_address;
92
93 fibril_mutex_unlock(&instance->guard);
94 return new_address;
95}
96/*----------------------------------------------------------------------------*/
97/** Bind USB address to devman handle.
98 *
99 * @param[in] instance Device keeper structure to use.
100 * @param[in] address Device address
101 * @param[in] handle Devman handle of the device.
102 */
103void usb_device_keeper_bind(usb_device_keeper_t *instance,
104 usb_address_t address, devman_handle_t handle)
105{
106 assert(instance);
107 fibril_mutex_lock(&instance->guard);
108
109 assert(address > 0);
110 assert(address <= USB11_ADDRESS_MAX);
111 assert(instance->devices[address].occupied);
112
113 instance->devices[address].handle = handle;
114 fibril_mutex_unlock(&instance->guard);
115}
116/*----------------------------------------------------------------------------*/
117/** Release used USB address.
118 *
119 * @param[in] instance Device keeper structure to use.
120 * @param[in] address Device address
121 */
122void usb_device_keeper_release(
123 usb_device_keeper_t *instance, usb_address_t address)
124{
125 assert(instance);
126 assert(address > 0);
127 assert(address <= USB11_ADDRESS_MAX);
128
129 fibril_mutex_lock(&instance->guard);
130 assert(instance->devices[address].occupied);
131
132 instance->devices[address].occupied = false;
133 fibril_mutex_unlock(&instance->guard);
134}
135/*----------------------------------------------------------------------------*/
136/** Find USB address associated with the device
137 *
138 * @param[in] instance Device keeper structure to use.
139 * @param[in] handle Devman handle of the device seeking its address.
140 * @return USB Address, or error code.
141 */
142usb_address_t usb_device_keeper_find(
143 usb_device_keeper_t *instance, devman_handle_t handle)
144{
145 assert(instance);
146 fibril_mutex_lock(&instance->guard);
147 usb_address_t address = 1;
148 while (address <= USB11_ADDRESS_MAX) {
149 if (instance->devices[address].handle == handle) {
150 assert(instance->devices[address].occupied);
151 fibril_mutex_unlock(&instance->guard);
152 return address;
153 }
154 ++address;
155 }
156 fibril_mutex_unlock(&instance->guard);
157 return ENOENT;
158}
159/*----------------------------------------------------------------------------*/
160/** Get speed associated with the address
161 *
162 * @param[in] instance Device keeper structure to use.
163 * @param[in] address Address of the device.
164 * @return USB speed.
165 */
166usb_speed_t usb_device_keeper_get_speed(
167 usb_device_keeper_t *instance, usb_address_t address)
168{
169 assert(instance);
170 assert(address >= 0);
171 assert(address <= USB11_ADDRESS_MAX);
172
173 return instance->devices[address].speed;
174}
175/**
176 * @}
177 */
Note: See TracBrowser for help on using the repository browser.