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

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

Compute used bandwidth during reservation

  • Property mode set to 100644
File size: 8.7 KB
RevLine 
[da9ebca]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;
[8870230]36 usb_direction_t direction;
[da9ebca]37} __attribute__((aligned (sizeof(unsigned long)))) transfer_t;
38/*----------------------------------------------------------------------------*/
39typedef struct {
40 transfer_t transfer;
41 link_t link;
42 bool used;
[8870230]43 size_t required;
[da9ebca]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/*----------------------------------------------------------------------------*/
[4ab89e5]60static int transfer_compare(
61 unsigned long key[], hash_count_t keys, link_t *item)
[da9ebca]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);
69}
70/*----------------------------------------------------------------------------*/
[4ab89e5]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}
[da9ebca]79/*----------------------------------------------------------------------------*/
80hash_table_operations_t op = {
81 .hash = transfer_hash,
[4ab89e5]82 .compare = transfer_compare,
83 .remove_callback = transfer_remove,
[da9ebca]84};
85/*----------------------------------------------------------------------------*/
[4ab89e5]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))
[da9ebca]124{
125 assert(instance);
126 fibril_mutex_initialize(&instance->guard);
[4ab89e5]127 instance->free = bandwidth;
128 instance->usage_fnc = usage_fnc;
[da9ebca]129 return
130 hash_table_create(&instance->reserved, BUCKET_COUNT, MAX_KEYS, &op);
131}
132/*----------------------------------------------------------------------------*/
133void bandwidth_destroy(bandwidth_t *instance)
134{
135 hash_table_destroy(&instance->reserved);
136}
137/*----------------------------------------------------------------------------*/
138int bandwidth_reserve(bandwidth_t *instance, usb_address_t address,
[8870230]139 usb_endpoint_t endpoint, usb_direction_t direction, usb_speed_t speed,
140 usb_transfer_type_t transfer_type, size_t max_packet_size, size_t size,
141 unsigned interval)
[da9ebca]142{
[4ab89e5]143 if (transfer_type != USB_TRANSFER_ISOCHRONOUS &&
144 transfer_type != USB_TRANSFER_INTERRUPT) {
145 return ENOTSUP;
146 }
147
[da9ebca]148 assert(instance);
[4ab89e5]149 assert(instance->usage_fnc);
150
[da9ebca]151 transfer_t trans = {
152 .address = address,
153 .endpoint = endpoint,
[8870230]154 .direction = direction,
[da9ebca]155 };
156 fibril_mutex_lock(&instance->guard);
[4ab89e5]157 const size_t required =
158 instance->usage_fnc(speed, transfer_type, size, max_packet_size);
159
160 if (required > instance->free) {
161 fibril_mutex_unlock(&instance->guard);
162 return ENOSPC;
163 }
164
[da9ebca]165 link_t *item =
166 hash_table_find(&instance->reserved, (unsigned long*)&trans);
167 if (item != NULL) {
168 fibril_mutex_unlock(&instance->guard);
169 return EEXISTS;
170 }
171
172 transfer_status_t *status = malloc(sizeof(transfer_status_t));
173 if (status == NULL) {
174 fibril_mutex_unlock(&instance->guard);
175 return ENOMEM;
176 }
177
178 status->transfer = trans;
[4ab89e5]179 status->required = required;
[da9ebca]180 status->used = false;
181 link_initialize(&status->link);
182
183 hash_table_insert(&instance->reserved,
184 (unsigned long*)&status->transfer, &status->link);
[4ab89e5]185 instance->free -= required;
[da9ebca]186 fibril_mutex_unlock(&instance->guard);
187 return EOK;
188 /* TODO: compute bandwidth used */
189}
190/*----------------------------------------------------------------------------*/
191int bandwidth_release(bandwidth_t *instance, usb_address_t address,
[8870230]192 usb_endpoint_t endpoint, usb_direction_t direction)
[da9ebca]193{
194 assert(instance);
195 transfer_t trans = {
196 .address = address,
197 .endpoint = endpoint,
[8870230]198 .direction = direction,
[da9ebca]199 };
200 fibril_mutex_lock(&instance->guard);
201 link_t *item =
202 hash_table_find(&instance->reserved, (unsigned long*)&trans);
203 if (item == NULL) {
204 fibril_mutex_unlock(&instance->guard);
205 return EINVAL;
206 }
207
[4ab89e5]208 transfer_status_t *status =
209 hash_table_get_instance(item, transfer_status_t, link);
210
211 instance->free += status->required;
212
[da9ebca]213 hash_table_remove(&instance->reserved,
214 (unsigned long*)&trans, MAX_KEYS);
215
216 fibril_mutex_unlock(&instance->guard);
217 return EOK;
218 /* TODO: compute bandwidth freed */
219}
220/*----------------------------------------------------------------------------*/
221int bandwidth_use(bandwidth_t *instance, usb_address_t address,
[8870230]222 usb_endpoint_t endpoint, usb_direction_t direction)
[da9ebca]223{
224 assert(instance);
225 transfer_t trans = {
226 .address = address,
227 .endpoint = endpoint,
[8870230]228 .direction = direction,
[da9ebca]229 };
230 fibril_mutex_lock(&instance->guard);
231 link_t *item =
232 hash_table_find(&instance->reserved, (unsigned long*)&trans);
233 int ret = EOK;
234 if (item != NULL) {
235 transfer_status_t *status =
236 hash_table_get_instance(item, transfer_status_t, link);
237 assert(status);
238 if (status->used) {
239 ret = EINPROGRESS;
240 }
241 status->used = true;
242 } else {
243 ret = EINVAL;
244 }
245 fibril_mutex_unlock(&instance->guard);
246 return ret;
247}
248/*----------------------------------------------------------------------------*/
249int bandwidth_free(bandwidth_t *instance, usb_address_t address,
[8870230]250 usb_endpoint_t endpoint, usb_direction_t direction)
[da9ebca]251{
252 assert(instance);
253 transfer_t trans = {
254 .address = address,
255 .endpoint = endpoint,
[8870230]256 .direction = direction,
[da9ebca]257 };
258 fibril_mutex_lock(&instance->guard);
259 link_t *item =
260 hash_table_find(&instance->reserved, (unsigned long*)&trans);
261 int ret = EOK;
262 if (item != NULL) {
263 transfer_status_t *status =
264 hash_table_get_instance(item, transfer_status_t, link);
265 assert(status);
266 if (!status->used) {
267 ret = ENOENT;
268 }
269 status->used = false;
270 } else {
271 ret = EINVAL;
272 }
273 fibril_mutex_unlock(&instance->guard);
274 return ret;
275}
Note: See TracBrowser for help on using the repository browser.