source: mainline/uspace/lib/usbhost/src/usb_endpoint_manager.c@ 612af1a0

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

libusbhost: Don't include direction in endpoint hash, make BIDIRECTIONAL match both directions.

This servers dual purpose:
1) Prevents registering IN and OUT endpoints if there already is a BIDIRECTIONAL endpoint with the same address and number
2) BIDIRECTIONAL endpoints are found when searching for any direction, thus control transfers don't need special handling in batch preparation.

  • Property mode set to 100644
File size: 9.4 KB
RevLine 
[f0891ce]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>
[90b9ab5]32
[ba038f4]33#include <usb/debug.h>
[f0891ce]34#include <usb/host/usb_endpoint_manager.h>
35
36#define BUCKET_COUNT 7
37
[92d6868]38#define MAX_KEYS (3)
[f0891ce]39typedef struct {
40 link_t link;
41 size_t bw;
[8dc762e0]42 endpoint_t *ep;
43} node_t;
[f0891ce]44/*----------------------------------------------------------------------------*/
[8dc762e0]45static hash_index_t node_hash(unsigned long key[])
[f0891ce]46{
[4cbb6e4]47 /* USB endpoints use 4 bits, thus ((key[0] << 4) | key[1])
48 * produces unique value for every address.endpoint pair */
49 return ((key[0] << 4) | key[1]) % BUCKET_COUNT;
[f0891ce]50}
51/*----------------------------------------------------------------------------*/
[8dc762e0]52static int node_compare(unsigned long key[], hash_count_t keys, link_t *item)
[f0891ce]53{
54 assert(item);
[8dc762e0]55 node_t *node = hash_table_get_instance(item, node_t, link);
[92d6868]56 assert(node);
57 assert(node->ep);
58 bool match = true;
59 switch (keys) {
60 case 3:
[4cbb6e4]61 match = match &&
62 ((key[2] == node->ep->direction)
63 || (node->ep->direction == USB_DIRECTION_BOTH));
[92d6868]64 case 2:
65 match = match && (key[1] == (unsigned long)node->ep->endpoint);
66 case 1:
67 match = match && (key[0] == (unsigned long)node->ep->address);
68 break;
69 default:
70 match = false;
[f0891ce]71 }
[92d6868]72 return match;
[f0891ce]73}
74/*----------------------------------------------------------------------------*/
[8dc762e0]75static void node_remove(link_t *item)
[f0891ce]76{
77 assert(item);
[8dc762e0]78 node_t *node = hash_table_get_instance(item, node_t, link);
79 endpoint_destroy(node->ep);
80 free(node);
[f0891ce]81}
82/*----------------------------------------------------------------------------*/
[ba038f4]83static void node_toggle_reset_filtered(link_t *item, void *arg)
84{
85 assert(item);
86 node_t *node = hash_table_get_instance(item, node_t, link);
87 usb_target_t *target = arg;
88 endpoint_toggle_reset_filtered(node->ep, *target);
89}
90/*----------------------------------------------------------------------------*/
[9262571]91static hash_table_operations_t op = {
[8dc762e0]92 .hash = node_hash,
93 .compare = node_compare,
94 .remove_callback = node_remove,
[f0891ce]95};
96/*----------------------------------------------------------------------------*/
97size_t bandwidth_count_usb11(usb_speed_t speed, usb_transfer_type_t type,
98 size_t size, size_t max_packet_size)
99{
[d41f301]100 /* We care about bandwidth only for interrupt and isochronous. */
101 if ((type != USB_TRANSFER_INTERRUPT)
102 && (type != USB_TRANSFER_ISOCHRONOUS)) {
103 return 0;
104 }
105
[f0891ce]106 const unsigned packet_count =
107 (size + max_packet_size - 1) / max_packet_size;
108 /* TODO: It may be that ISO and INT transfers use only one data packet
109 * per transaction, but I did not find text in UB spec that confirms
110 * this */
111 /* NOTE: All data packets will be considered to be max_packet_size */
112 switch (speed)
113 {
114 case USB_SPEED_LOW:
115 assert(type == USB_TRANSFER_INTERRUPT);
116 /* Protocol overhead 13B
117 * (3 SYNC bytes, 3 PID bytes, 2 Endpoint + CRC bytes, 2
118 * CRC bytes, and a 3-byte interpacket delay)
119 * see USB spec page 45-46. */
120 /* Speed penalty 8: low speed is 8-times slower*/
121 return packet_count * (13 + max_packet_size) * 8;
122 case USB_SPEED_FULL:
123 /* Interrupt transfer overhead see above
124 * or page 45 of USB spec */
125 if (type == USB_TRANSFER_INTERRUPT)
126 return packet_count * (13 + max_packet_size);
127
128 assert(type == USB_TRANSFER_ISOCHRONOUS);
129 /* Protocol overhead 9B
130 * (2 SYNC bytes, 2 PID bytes, 2 Endpoint + CRC bytes, 2 CRC
131 * bytes, and a 1-byte interpacket delay)
132 * see USB spec page 42 */
133 return packet_count * (9 + max_packet_size);
134 default:
135 return 0;
136 }
137}
138/*----------------------------------------------------------------------------*/
139int usb_endpoint_manager_init(usb_endpoint_manager_t *instance,
[933b0d7]140 size_t available_bandwidth,
141 size_t (*bw_count)(usb_speed_t, usb_transfer_type_t, size_t, size_t))
[f0891ce]142{
143 assert(instance);
144 fibril_mutex_initialize(&instance->guard);
145 instance->free_bw = available_bandwidth;
[933b0d7]146 instance->bw_count = bw_count;
[563d9d0a]147 const bool ht =
[f0891ce]148 hash_table_create(&instance->ep_table, BUCKET_COUNT, MAX_KEYS, &op);
149 return ht ? EOK : ENOMEM;
150}
151/*----------------------------------------------------------------------------*/
152void usb_endpoint_manager_destroy(usb_endpoint_manager_t *instance)
153{
154 hash_table_destroy(&instance->ep_table);
155}
156/*----------------------------------------------------------------------------*/
157int usb_endpoint_manager_register_ep(usb_endpoint_manager_t *instance,
[8dc762e0]158 endpoint_t *ep, size_t data_size)
[f0891ce]159{
[933b0d7]160 assert(instance);
161 assert(instance->bw_count);
[8dc762e0]162 assert(ep);
[933b0d7]163 const size_t bw = instance->bw_count(ep->speed, ep->transfer_type,
[8dc762e0]164 data_size, ep->max_packet_size);
[f0891ce]165
[563d9d0a]166 fibril_mutex_lock(&instance->guard);
167
168 if (bw > instance->free_bw) {
169 fibril_mutex_unlock(&instance->guard);
170 return ENOSPC;
171 }
172
[92d6868]173 unsigned long key[MAX_KEYS] =
174 {ep->address, ep->endpoint, ep->direction};
[f0891ce]175
[563d9d0a]176 const link_t *item =
[92d6868]177 hash_table_find(&instance->ep_table, key);
[f0891ce]178 if (item != NULL) {
179 fibril_mutex_unlock(&instance->guard);
180 return EEXISTS;
181 }
182
[8dc762e0]183 node_t *node = malloc(sizeof(node_t));
184 if (node == NULL) {
[f0891ce]185 fibril_mutex_unlock(&instance->guard);
186 return ENOMEM;
187 }
188
[8dc762e0]189 node->bw = bw;
190 node->ep = ep;
191 link_initialize(&node->link);
[f0891ce]192
[92d6868]193 hash_table_insert(&instance->ep_table, key, &node->link);
[f0891ce]194 instance->free_bw -= bw;
195 fibril_mutex_unlock(&instance->guard);
196 return EOK;
197}
198/*----------------------------------------------------------------------------*/
199int usb_endpoint_manager_unregister_ep(usb_endpoint_manager_t *instance,
200 usb_address_t address, usb_endpoint_t endpoint, usb_direction_t direction)
201{
202 assert(instance);
[92d6868]203 unsigned long key[MAX_KEYS] = {address, endpoint, direction};
204
[f0891ce]205 fibril_mutex_lock(&instance->guard);
[92d6868]206 link_t *item = hash_table_find(&instance->ep_table, key);
[f0891ce]207 if (item == NULL) {
208 fibril_mutex_unlock(&instance->guard);
209 return EINVAL;
210 }
211
[8dc762e0]212 node_t *node = hash_table_get_instance(item, node_t, link);
[563d9d0a]213 if (node->ep->active) {
214 fibril_mutex_unlock(&instance->guard);
[3eeb270f]215 return EBUSY;
[563d9d0a]216 }
[3eeb270f]217
[8dc762e0]218 instance->free_bw += node->bw;
[92d6868]219 hash_table_remove(&instance->ep_table, key, MAX_KEYS);
[f0891ce]220
221 fibril_mutex_unlock(&instance->guard);
222 return EOK;
223}
224/*----------------------------------------------------------------------------*/
[8dc762e0]225endpoint_t * usb_endpoint_manager_get_ep(usb_endpoint_manager_t *instance,
[f0891ce]226 usb_address_t address, usb_endpoint_t endpoint, usb_direction_t direction,
227 size_t *bw)
228{
229 assert(instance);
[92d6868]230 unsigned long key[MAX_KEYS] = {address, endpoint, direction};
231
[f0891ce]232 fibril_mutex_lock(&instance->guard);
[563d9d0a]233 const link_t *item = hash_table_find(&instance->ep_table, key);
[f0891ce]234 if (item == NULL) {
235 fibril_mutex_unlock(&instance->guard);
236 return NULL;
237 }
[563d9d0a]238 const node_t *node = hash_table_get_instance(item, node_t, link);
[f0891ce]239 if (bw)
[8dc762e0]240 *bw = node->bw;
[f0891ce]241
242 fibril_mutex_unlock(&instance->guard);
[8dc762e0]243 return node->ep;
[f0891ce]244}
[ba038f4]245/*----------------------------------------------------------------------------*/
246/** Check setup packet data for signs of toggle reset.
247 *
248 * @param[in] instance Device keeper structure to use.
249 * @param[in] target Device to receive setup packet.
250 * @param[in] data Setup packet data.
251 *
252 * Really ugly one.
253 */
254void usb_endpoint_manager_reset_if_need(
255 usb_endpoint_manager_t *instance, usb_target_t target, const uint8_t *data)
256{
257 assert(instance);
[563d9d0a]258 if (!usb_target_is_valid(target)) {
[ba038f4]259 usb_log_error("Invalid data when checking for toggle reset.\n");
260 return;
261 }
262
[563d9d0a]263 assert(data);
[ba038f4]264 switch (data[1])
265 {
[563d9d0a]266 case 0x01: /* Clear Feature -- resets only cleared ep */
267 /* Recipient is endpoint, value is zero (ENDPOINT_STALL) */
[ba038f4]268 if (((data[0] & 0xf) == 1) && ((data[2] | data[3]) == 0)) {
269 /* endpoint number is < 16, thus first byte is enough */
270 usb_target_t reset_target =
271 { .address = target.address, data[4] };
272 fibril_mutex_lock(&instance->guard);
273 hash_table_apply(&instance->ep_table,
274 node_toggle_reset_filtered, &reset_target);
275 fibril_mutex_unlock(&instance->guard);
276 }
277 break;
278
[563d9d0a]279 case 0x9: /* Set Configuration */
280 case 0x11: /* Set Interface */
281 /* Recipient must be device */
[ba038f4]282 if ((data[0] & 0xf) == 0) {
283 usb_target_t reset_target =
284 { .address = target.address, 0 };
285 fibril_mutex_lock(&instance->guard);
286 hash_table_apply(&instance->ep_table,
287 node_toggle_reset_filtered, &reset_target);
288 fibril_mutex_unlock(&instance->guard);
289 }
290 break;
291 }
292}
Note: See TracBrowser for help on using the repository browser.