source: mainline/uspace/lib/usb/src/host/bandwidth.c@ df40775

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

Check reserved bw

  • Property mode set to 100644
File size: 8.8 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#include <assert.h>
30#include <errno.h>
31#include <usb/host/bandwidth.h>
32
33typedef struct {
34 usb_address_t address;
35 usb_endpoint_t endpoint;
36 usb_direction_t direction;
37} __attribute__((aligned (sizeof(unsigned long)))) transfer_t;
38/*----------------------------------------------------------------------------*/
39typedef struct {
40 transfer_t transfer;
41 link_t link;
42 bool used;
43 size_t required;
44} transfer_status_t;
45/*----------------------------------------------------------------------------*/
46#define BUCKET_COUNT 7
47#define MAX_KEYS (sizeof(transfer_t) / sizeof(unsigned long))
48/*----------------------------------------------------------------------------*/
49static hash_index_t transfer_hash(unsigned long key[])
50{
51 hash_index_t hash = 0;
52 unsigned i = 0;
53 for (;i < MAX_KEYS; ++i) {
54 hash ^= key[i];
55 }
56 hash %= BUCKET_COUNT;
57 return hash;
58}
59/*----------------------------------------------------------------------------*/
60static int transfer_compare(
61 unsigned long key[], hash_count_t keys, link_t *item)
62{
63 assert(item);
64 transfer_status_t *status =
65 hash_table_get_instance(item, transfer_status_t, link);
66 const size_t bytes =
67 keys < MAX_KEYS ? keys * sizeof(unsigned long) : sizeof(transfer_t);
68 return bcmp(key, &status->transfer, bytes) == 0;
69}
70/*----------------------------------------------------------------------------*/
71static void transfer_remove(link_t *item)
72{
73 assert(item);
74 transfer_status_t *status =
75 hash_table_get_instance(item, transfer_status_t, link);
76 assert(status);
77 free(status);
78}
79/*----------------------------------------------------------------------------*/
80hash_table_operations_t op = {
81 .hash = transfer_hash,
82 .compare = transfer_compare,
83 .remove_callback = transfer_remove,
84};
85/*----------------------------------------------------------------------------*/
86size_t bandwidth_count_usb11(usb_speed_t speed, usb_transfer_type_t type,
87 size_t size, size_t max_packet_size)
88{
89 const unsigned packet_count =
90 (size + max_packet_size - 1) / max_packet_size;
91 /* TODO: It may be that ISO and INT transfers use only one data packet
92 * per transaction, but I did not find text in UB spec that confirms
93 * this */
94 /* NOTE: All data packets will be considered to be max_packet_size */
95 switch (speed)
96 {
97 case USB_SPEED_LOW:
98 assert(type == USB_TRANSFER_INTERRUPT);
99 /* Protocol overhead 13B
100 * (3 SYNC bytes, 3 PID bytes, 2 Endpoint + CRC bytes, 2
101 * CRC bytes, and a 3-byte interpacket delay)
102 * see USB spec page 45-46. */
103 /* Speed penalty 8: low speed is 8-times slower*/
104 return packet_count * (13 + max_packet_size) * 8;
105 case USB_SPEED_FULL:
106 /* Interrupt transfer overhead see above
107 * or page 45 of USB spec */
108 if (type == USB_TRANSFER_INTERRUPT)
109 return packet_count * (13 + max_packet_size);
110
111 assert(type == USB_TRANSFER_ISOCHRONOUS);
112 /* Protocol overhead 9B
113 * (2 SYNC bytes, 2 PID bytes, 2 Endpoint + CRC bytes, 2 CRC
114 * bytes, and a 1-byte interpacket delay)
115 * see USB spec page 42 */
116 return packet_count * (9 + max_packet_size);
117 default:
118 return 0;
119 }
120}
121/*----------------------------------------------------------------------------*/
122int bandwidth_init(bandwidth_t *instance, size_t bandwidth,
123 size_t (*usage_fnc)(usb_speed_t, usb_transfer_type_t, size_t, size_t))
124{
125 assert(instance);
126 fibril_mutex_initialize(&instance->guard);
127 instance->free = bandwidth;
128 instance->usage_fnc = usage_fnc;
129 bool ht =
130 hash_table_create(&instance->reserved, BUCKET_COUNT, MAX_KEYS, &op);
131 return ht ? EOK : ENOMEM;
132}
133/*----------------------------------------------------------------------------*/
134void bandwidth_destroy(bandwidth_t *instance)
135{
136 hash_table_destroy(&instance->reserved);
137}
138/*----------------------------------------------------------------------------*/
139int bandwidth_reserve(bandwidth_t *instance, usb_address_t address,
140 usb_endpoint_t endpoint, usb_direction_t direction, usb_speed_t speed,
141 usb_transfer_type_t transfer_type, size_t max_packet_size, size_t size,
142 unsigned interval)
143{
144 if (transfer_type != USB_TRANSFER_ISOCHRONOUS &&
145 transfer_type != USB_TRANSFER_INTERRUPT) {
146 return ENOTSUP;
147 }
148
149 assert(instance);
150 assert(instance->usage_fnc);
151
152 transfer_t trans = {
153 .address = address,
154 .endpoint = endpoint,
155 .direction = direction,
156 };
157 fibril_mutex_lock(&instance->guard);
158 const size_t required =
159 instance->usage_fnc(speed, transfer_type, size, max_packet_size);
160
161 if (required > instance->free) {
162 fibril_mutex_unlock(&instance->guard);
163 return ENOSPC;
164 }
165
166 link_t *item =
167 hash_table_find(&instance->reserved, (unsigned long*)&trans);
168 if (item != NULL) {
169 fibril_mutex_unlock(&instance->guard);
170 return EEXISTS;
171 }
172
173 transfer_status_t *status = malloc(sizeof(transfer_status_t));
174 if (status == NULL) {
175 fibril_mutex_unlock(&instance->guard);
176 return ENOMEM;
177 }
178
179 status->transfer = trans;
180 status->required = required;
181 status->used = false;
182 link_initialize(&status->link);
183
184 hash_table_insert(&instance->reserved,
185 (unsigned long*)&status->transfer, &status->link);
186 instance->free -= required;
187 fibril_mutex_unlock(&instance->guard);
188 return EOK;
189 /* TODO: compute bandwidth used */
190}
191/*----------------------------------------------------------------------------*/
192int bandwidth_release(bandwidth_t *instance, usb_address_t address,
193 usb_endpoint_t endpoint, usb_direction_t direction)
194{
195 assert(instance);
196 transfer_t trans = {
197 .address = address,
198 .endpoint = endpoint,
199 .direction = direction,
200 };
201 fibril_mutex_lock(&instance->guard);
202 link_t *item =
203 hash_table_find(&instance->reserved, (unsigned long*)&trans);
204 if (item == NULL) {
205 fibril_mutex_unlock(&instance->guard);
206 return EINVAL;
207 }
208
209 transfer_status_t *status =
210 hash_table_get_instance(item, transfer_status_t, link);
211
212 instance->free += status->required;
213
214 hash_table_remove(&instance->reserved,
215 (unsigned long*)&trans, MAX_KEYS);
216
217 fibril_mutex_unlock(&instance->guard);
218 return EOK;
219 /* TODO: compute bandwidth freed */
220}
221/*----------------------------------------------------------------------------*/
222int bandwidth_use(bandwidth_t *instance, usb_address_t address,
223 usb_endpoint_t endpoint, usb_direction_t direction, size_t bw)
224{
225 assert(instance);
226 transfer_t trans = {
227 .address = address,
228 .endpoint = endpoint,
229 .direction = direction,
230 };
231 fibril_mutex_lock(&instance->guard);
232 link_t *item =
233 hash_table_find(&instance->reserved, (unsigned long*)&trans);
234 int ret = EOK;
235 if (item != NULL) {
236 transfer_status_t *status =
237 hash_table_get_instance(item, transfer_status_t, link);
238 assert(status);
239 if (status->required >= bw) {
240 if (status->used) {
241 ret = EINPROGRESS;
242 }
243 status->used = true;
244 } else {
245 ret = ENOSPC;
246 }
247 } else {
248 ret = EINVAL;
249 }
250 fibril_mutex_unlock(&instance->guard);
251 return ret;
252}
253/*----------------------------------------------------------------------------*/
254int bandwidth_free(bandwidth_t *instance, usb_address_t address,
255 usb_endpoint_t endpoint, usb_direction_t direction)
256{
257 assert(instance);
258 transfer_t trans = {
259 .address = address,
260 .endpoint = endpoint,
261 .direction = direction,
262 };
263 fibril_mutex_lock(&instance->guard);
264 link_t *item =
265 hash_table_find(&instance->reserved, (unsigned long*)&trans);
266 int ret = EOK;
267 if (item != NULL) {
268 transfer_status_t *status =
269 hash_table_get_instance(item, transfer_status_t, link);
270 assert(status);
271 if (!status->used) {
272 ret = ENOENT;
273 }
274 status->used = false;
275 } else {
276 ret = EINVAL;
277 }
278 fibril_mutex_unlock(&instance->guard);
279 return ret;
280}
Note: See TracBrowser for help on using the repository browser.