source: mainline/uspace/lib/usb/src/host/usb_endpoint_manager.c@ 0aae4a6a

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

Move endpoint_t to libusb

  • Property mode set to 100644
File size: 7.5 KB
Line 
1/*
2 * Copyright (c) 2011 Jan Vesely
3 * All rights eps.
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 <bool.h>
30#include <assert.h>
31#include <errno.h>
32
33#include <usb/host/usb_endpoint_manager.h>
34
35#define BUCKET_COUNT 7
36
37typedef struct {
38 usb_address_t address;
39 usb_endpoint_t endpoint;
40 usb_direction_t direction;
41} __attribute__((aligned (sizeof(unsigned long)))) id_t;
42#define MAX_KEYS (sizeof(id_t) / sizeof(unsigned long))
43typedef struct {
44 union {
45 id_t id;
46 unsigned long key[MAX_KEYS];
47 };
48 link_t link;
49 size_t bw;
50 endpoint_t *ep;
51} node_t;
52/*----------------------------------------------------------------------------*/
53static hash_index_t node_hash(unsigned long key[])
54{
55 hash_index_t hash = 0;
56 unsigned i = 0;
57 for (;i < MAX_KEYS; ++i) {
58 hash ^= key[i];
59 }
60 hash %= BUCKET_COUNT;
61 return hash;
62}
63/*----------------------------------------------------------------------------*/
64static int node_compare(unsigned long key[], hash_count_t keys, link_t *item)
65{
66 assert(item);
67 node_t *node = hash_table_get_instance(item, node_t, link);
68 hash_count_t i = 0;
69 for (; i < keys; ++i) {
70 if (key[i] != node->key[i])
71 return false;
72 }
73 return true;
74}
75/*----------------------------------------------------------------------------*/
76static void node_remove(link_t *item)
77{
78 assert(item);
79 node_t *node = hash_table_get_instance(item, node_t, link);
80 endpoint_destroy(node->ep);
81 free(node);
82}
83/*----------------------------------------------------------------------------*/
84static hash_table_operations_t op = {
85 .hash = node_hash,
86 .compare = node_compare,
87 .remove_callback = node_remove,
88};
89/*----------------------------------------------------------------------------*/
90size_t bandwidth_count_usb11(usb_speed_t speed, usb_transfer_type_t type,
91 size_t size, size_t max_packet_size)
92{
93 const unsigned packet_count =
94 (size + max_packet_size - 1) / max_packet_size;
95 /* TODO: It may be that ISO and INT transfers use only one data packet
96 * per transaction, but I did not find text in UB spec that confirms
97 * this */
98 /* NOTE: All data packets will be considered to be max_packet_size */
99 switch (speed)
100 {
101 case USB_SPEED_LOW:
102 assert(type == USB_TRANSFER_INTERRUPT);
103 /* Protocol overhead 13B
104 * (3 SYNC bytes, 3 PID bytes, 2 Endpoint + CRC bytes, 2
105 * CRC bytes, and a 3-byte interpacket delay)
106 * see USB spec page 45-46. */
107 /* Speed penalty 8: low speed is 8-times slower*/
108 return packet_count * (13 + max_packet_size) * 8;
109 case USB_SPEED_FULL:
110 /* Interrupt transfer overhead see above
111 * or page 45 of USB spec */
112 if (type == USB_TRANSFER_INTERRUPT)
113 return packet_count * (13 + max_packet_size);
114
115 assert(type == USB_TRANSFER_ISOCHRONOUS);
116 /* Protocol overhead 9B
117 * (2 SYNC bytes, 2 PID bytes, 2 Endpoint + CRC bytes, 2 CRC
118 * bytes, and a 1-byte interpacket delay)
119 * see USB spec page 42 */
120 return packet_count * (9 + max_packet_size);
121 default:
122 return 0;
123 }
124}
125/*----------------------------------------------------------------------------*/
126int usb_endpoint_manager_init(usb_endpoint_manager_t *instance,
127 size_t available_bandwidth)
128{
129 assert(instance);
130 fibril_mutex_initialize(&instance->guard);
131 fibril_condvar_initialize(&instance->change);
132 instance->free_bw = available_bandwidth;
133 bool ht =
134 hash_table_create(&instance->ep_table, BUCKET_COUNT, MAX_KEYS, &op);
135 return ht ? EOK : ENOMEM;
136}
137/*----------------------------------------------------------------------------*/
138void usb_endpoint_manager_destroy(usb_endpoint_manager_t *instance)
139{
140 hash_table_destroy(&instance->ep_table);
141}
142/*----------------------------------------------------------------------------*/
143int usb_endpoint_manager_register_ep(usb_endpoint_manager_t *instance,
144 usb_address_t address, usb_endpoint_t endpoint, usb_direction_t direction,
145 endpoint_t *ep, size_t data_size)
146{
147 assert(ep);
148 size_t bw = bandwidth_count_usb11(ep->speed, ep->transfer_type,
149 data_size, ep->max_packet_size);
150 assert(instance);
151
152 id_t id = {
153 .address = address,
154 .endpoint = endpoint,
155 .direction = direction,
156 };
157 fibril_mutex_lock(&instance->guard);
158
159 link_t *item =
160 hash_table_find(&instance->ep_table, (unsigned long*)&id);
161 if (item != NULL) {
162 fibril_mutex_unlock(&instance->guard);
163 return EEXISTS;
164 }
165
166 if (bw > instance->free_bw) {
167 fibril_mutex_unlock(&instance->guard);
168 return ENOSPC;
169 }
170
171 node_t *node = malloc(sizeof(node_t));
172 if (node == NULL) {
173 fibril_mutex_unlock(&instance->guard);
174 return ENOMEM;
175 }
176
177 node->id = id;
178 node->bw = bw;
179 node->ep = ep;
180 link_initialize(&node->link);
181
182 hash_table_insert(&instance->ep_table,
183 (unsigned long*)&id, &node->link);
184 instance->free_bw -= bw;
185 fibril_mutex_unlock(&instance->guard);
186 fibril_condvar_broadcast(&instance->change);
187 return EOK;
188}
189/*----------------------------------------------------------------------------*/
190int usb_endpoint_manager_unregister_ep(usb_endpoint_manager_t *instance,
191 usb_address_t address, usb_endpoint_t endpoint, usb_direction_t direction)
192{
193 assert(instance);
194 id_t id = {
195 .address = address,
196 .endpoint = endpoint,
197 .direction = direction,
198 };
199 fibril_mutex_lock(&instance->guard);
200 link_t *item =
201 hash_table_find(&instance->ep_table, (unsigned long*)&id);
202 if (item == NULL) {
203 fibril_mutex_unlock(&instance->guard);
204 return EINVAL;
205 }
206
207 node_t *node = hash_table_get_instance(item, node_t, link);
208 instance->free_bw += node->bw;
209 hash_table_remove(&instance->ep_table, (unsigned long*)&id, MAX_KEYS);
210
211 fibril_mutex_unlock(&instance->guard);
212 fibril_condvar_broadcast(&instance->change);
213 return EOK;
214}
215/*----------------------------------------------------------------------------*/
216endpoint_t * usb_endpoint_manager_get_ep(usb_endpoint_manager_t *instance,
217 usb_address_t address, usb_endpoint_t endpoint, usb_direction_t direction,
218 size_t *bw)
219{
220 assert(instance);
221 id_t id = {
222 .address = address,
223 .endpoint = endpoint,
224 .direction = direction,
225 };
226 fibril_mutex_lock(&instance->guard);
227 link_t *item =
228 hash_table_find(&instance->ep_table, (unsigned long*)&id);
229 if (item == NULL) {
230 fibril_mutex_unlock(&instance->guard);
231 return NULL;
232 }
233 node_t *node = hash_table_get_instance(item, node_t, link);
234 if (bw)
235 *bw = node->bw;
236
237 fibril_mutex_unlock(&instance->guard);
238 return node->ep;
239}
Note: See TracBrowser for help on using the repository browser.