[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 |
|
---|
[7265558] | 36 | static inline bool ep_match(const endpoint_t *ep,
|
---|
| 37 | usb_address_t address, usb_endpoint_t endpoint, usb_direction_t direction)
|
---|
[f0891ce] | 38 | {
|
---|
[83c3123] | 39 | assert(ep);
|
---|
[7265558] | 40 | return
|
---|
| 41 | ((direction == ep->direction)
|
---|
| 42 | || (ep->direction == USB_DIRECTION_BOTH)
|
---|
| 43 | || (direction == USB_DIRECTION_BOTH))
|
---|
| 44 | && (endpoint == ep->endpoint)
|
---|
| 45 | && (address == ep->address);
|
---|
[f0891ce] | 46 | }
|
---|
| 47 | /*----------------------------------------------------------------------------*/
|
---|
[7265558] | 48 | static list_t * get_list(usb_endpoint_manager_t *instance, usb_address_t addr)
|
---|
[f0891ce] | 49 | {
|
---|
[7265558] | 50 | assert(instance);
|
---|
| 51 | return &instance->endpoint_lists[addr % ENDPOINT_LIST_COUNT];
|
---|
[f0891ce] | 52 | }
|
---|
| 53 | /*----------------------------------------------------------------------------*/
|
---|
[7265558] | 54 | static endpoint_t * find_locked(usb_endpoint_manager_t *instance,
|
---|
| 55 | usb_address_t address, usb_endpoint_t endpoint, usb_direction_t direction)
|
---|
[ba038f4] | 56 | {
|
---|
[7265558] | 57 | assert(instance);
|
---|
| 58 | assert(fibril_mutex_is_locked(&instance->guard));
|
---|
| 59 | list_foreach(*get_list(instance, address), iterator) {
|
---|
| 60 | endpoint_t *ep = endpoint_get_instance(iterator);
|
---|
| 61 | if (ep_match(ep, address, endpoint, direction))
|
---|
| 62 | return ep;
|
---|
| 63 | }
|
---|
| 64 | return NULL;
|
---|
[ba038f4] | 65 | }
|
---|
| 66 | /*----------------------------------------------------------------------------*/
|
---|
[f0891ce] | 67 | size_t bandwidth_count_usb11(usb_speed_t speed, usb_transfer_type_t type,
|
---|
| 68 | size_t size, size_t max_packet_size)
|
---|
| 69 | {
|
---|
[d41f301] | 70 | /* We care about bandwidth only for interrupt and isochronous. */
|
---|
| 71 | if ((type != USB_TRANSFER_INTERRUPT)
|
---|
| 72 | && (type != USB_TRANSFER_ISOCHRONOUS)) {
|
---|
| 73 | return 0;
|
---|
| 74 | }
|
---|
| 75 |
|
---|
[f0891ce] | 76 | const unsigned packet_count =
|
---|
| 77 | (size + max_packet_size - 1) / max_packet_size;
|
---|
[7265558] | 78 | /* TODO: It may be that ISO and INT transfers use only one packet per
|
---|
| 79 | * transaction, but I did not find text in USB spec to confirm this */
|
---|
[f0891ce] | 80 | /* NOTE: All data packets will be considered to be max_packet_size */
|
---|
| 81 | switch (speed)
|
---|
| 82 | {
|
---|
| 83 | case USB_SPEED_LOW:
|
---|
| 84 | assert(type == USB_TRANSFER_INTERRUPT);
|
---|
| 85 | /* Protocol overhead 13B
|
---|
| 86 | * (3 SYNC bytes, 3 PID bytes, 2 Endpoint + CRC bytes, 2
|
---|
| 87 | * CRC bytes, and a 3-byte interpacket delay)
|
---|
| 88 | * see USB spec page 45-46. */
|
---|
| 89 | /* Speed penalty 8: low speed is 8-times slower*/
|
---|
| 90 | return packet_count * (13 + max_packet_size) * 8;
|
---|
| 91 | case USB_SPEED_FULL:
|
---|
| 92 | /* Interrupt transfer overhead see above
|
---|
| 93 | * or page 45 of USB spec */
|
---|
| 94 | if (type == USB_TRANSFER_INTERRUPT)
|
---|
| 95 | return packet_count * (13 + max_packet_size);
|
---|
| 96 |
|
---|
| 97 | assert(type == USB_TRANSFER_ISOCHRONOUS);
|
---|
| 98 | /* Protocol overhead 9B
|
---|
| 99 | * (2 SYNC bytes, 2 PID bytes, 2 Endpoint + CRC bytes, 2 CRC
|
---|
| 100 | * bytes, and a 1-byte interpacket delay)
|
---|
| 101 | * see USB spec page 42 */
|
---|
| 102 | return packet_count * (9 + max_packet_size);
|
---|
| 103 | default:
|
---|
| 104 | return 0;
|
---|
| 105 | }
|
---|
| 106 | }
|
---|
| 107 | /*----------------------------------------------------------------------------*/
|
---|
| 108 | int usb_endpoint_manager_init(usb_endpoint_manager_t *instance,
|
---|
[933b0d7] | 109 | size_t available_bandwidth,
|
---|
| 110 | size_t (*bw_count)(usb_speed_t, usb_transfer_type_t, size_t, size_t))
|
---|
[f0891ce] | 111 | {
|
---|
| 112 | assert(instance);
|
---|
| 113 | fibril_mutex_initialize(&instance->guard);
|
---|
| 114 | instance->free_bw = available_bandwidth;
|
---|
[933b0d7] | 115 | instance->bw_count = bw_count;
|
---|
[7265558] | 116 | for (unsigned i = 0; i < ENDPOINT_LIST_COUNT; ++i) {
|
---|
| 117 | list_initialize(&instance->endpoint_lists[i]);
|
---|
| 118 | }
|
---|
| 119 | return EOK;
|
---|
[f0891ce] | 120 | }
|
---|
| 121 | /*----------------------------------------------------------------------------*/
|
---|
| 122 | int usb_endpoint_manager_register_ep(usb_endpoint_manager_t *instance,
|
---|
[8dc762e0] | 123 | endpoint_t *ep, size_t data_size)
|
---|
[f0891ce] | 124 | {
|
---|
[933b0d7] | 125 | assert(instance);
|
---|
| 126 | assert(instance->bw_count);
|
---|
[8dc762e0] | 127 | assert(ep);
|
---|
[83c3123] | 128 | ep->bandwidth = instance->bw_count(ep->speed, ep->transfer_type,
|
---|
[8dc762e0] | 129 | data_size, ep->max_packet_size);
|
---|
[f0891ce] | 130 |
|
---|
[563d9d0a] | 131 | fibril_mutex_lock(&instance->guard);
|
---|
| 132 |
|
---|
[83c3123] | 133 | if (ep->bandwidth > instance->free_bw) {
|
---|
[563d9d0a] | 134 | fibril_mutex_unlock(&instance->guard);
|
---|
| 135 | return ENOSPC;
|
---|
| 136 | }
|
---|
| 137 |
|
---|
[7265558] | 138 | /* Check for existence */
|
---|
| 139 | const endpoint_t *endpoint =
|
---|
| 140 | find_locked(instance, ep->address, ep->endpoint, ep->direction);
|
---|
| 141 | if (endpoint != NULL) {
|
---|
[f0891ce] | 142 | fibril_mutex_unlock(&instance->guard);
|
---|
| 143 | return EEXISTS;
|
---|
| 144 | }
|
---|
[7265558] | 145 | list_t *list = get_list(instance, ep->address);
|
---|
| 146 | list_append(&ep->link, list);
|
---|
[f0891ce] | 147 |
|
---|
[83c3123] | 148 | instance->free_bw -= ep->bandwidth;
|
---|
[f0891ce] | 149 | fibril_mutex_unlock(&instance->guard);
|
---|
| 150 | return EOK;
|
---|
| 151 | }
|
---|
| 152 | /*----------------------------------------------------------------------------*/
|
---|
| 153 | int usb_endpoint_manager_unregister_ep(usb_endpoint_manager_t *instance,
|
---|
| 154 | usb_address_t address, usb_endpoint_t endpoint, usb_direction_t direction)
|
---|
| 155 | {
|
---|
| 156 | assert(instance);
|
---|
[92d6868] | 157 |
|
---|
[f0891ce] | 158 | fibril_mutex_lock(&instance->guard);
|
---|
[7265558] | 159 | endpoint_t *ep = find_locked(instance, address, endpoint, direction);
|
---|
| 160 | if (ep != NULL) {
|
---|
| 161 | list_remove(&ep->link);
|
---|
| 162 | instance->free_bw += ep->bandwidth;
|
---|
[f0891ce] | 163 | }
|
---|
| 164 |
|
---|
| 165 | fibril_mutex_unlock(&instance->guard);
|
---|
[7265558] | 166 | endpoint_destroy(ep);
|
---|
| 167 | return (ep != NULL) ? EOK : EINVAL;
|
---|
[f0891ce] | 168 | }
|
---|
| 169 | /*----------------------------------------------------------------------------*/
|
---|
[8dc762e0] | 170 | endpoint_t * usb_endpoint_manager_get_ep(usb_endpoint_manager_t *instance,
|
---|
[83c3123] | 171 | usb_address_t address, usb_endpoint_t endpoint, usb_direction_t direction)
|
---|
[f0891ce] | 172 | {
|
---|
| 173 | assert(instance);
|
---|
[92d6868] | 174 |
|
---|
[f0891ce] | 175 | fibril_mutex_lock(&instance->guard);
|
---|
[7265558] | 176 | endpoint_t *ep = find_locked(instance, address, endpoint, direction);
|
---|
[f0891ce] | 177 | fibril_mutex_unlock(&instance->guard);
|
---|
[7265558] | 178 |
|
---|
[83c3123] | 179 | return ep;
|
---|
[f0891ce] | 180 | }
|
---|
[ba038f4] | 181 | /*----------------------------------------------------------------------------*/
|
---|
| 182 | /** Check setup packet data for signs of toggle reset.
|
---|
| 183 | *
|
---|
| 184 | * @param[in] instance Device keeper structure to use.
|
---|
| 185 | * @param[in] target Device to receive setup packet.
|
---|
| 186 | * @param[in] data Setup packet data.
|
---|
| 187 | *
|
---|
| 188 | * Really ugly one.
|
---|
| 189 | */
|
---|
| 190 | void usb_endpoint_manager_reset_if_need(
|
---|
| 191 | usb_endpoint_manager_t *instance, usb_target_t target, const uint8_t *data)
|
---|
| 192 | {
|
---|
| 193 | assert(instance);
|
---|
[563d9d0a] | 194 | if (!usb_target_is_valid(target)) {
|
---|
[ba038f4] | 195 | usb_log_error("Invalid data when checking for toggle reset.\n");
|
---|
| 196 | return;
|
---|
| 197 | }
|
---|
| 198 |
|
---|
[563d9d0a] | 199 | assert(data);
|
---|
[ba038f4] | 200 | switch (data[1])
|
---|
| 201 | {
|
---|
[563d9d0a] | 202 | case 0x01: /* Clear Feature -- resets only cleared ep */
|
---|
| 203 | /* Recipient is endpoint, value is zero (ENDPOINT_STALL) */
|
---|
[ba038f4] | 204 | if (((data[0] & 0xf) == 1) && ((data[2] | data[3]) == 0)) {
|
---|
| 205 | fibril_mutex_lock(&instance->guard);
|
---|
[7265558] | 206 | /* endpoint number is < 16, thus first byte is enough */
|
---|
| 207 | list_foreach(*get_list(instance, target.address), it) {
|
---|
| 208 | endpoint_t *ep = endpoint_get_instance(it);
|
---|
| 209 | if ((ep->address == target.address)
|
---|
| 210 | && (ep->endpoint = data[4])) {
|
---|
| 211 | endpoint_toggle_set(ep,0);
|
---|
| 212 | }
|
---|
| 213 | }
|
---|
[ba038f4] | 214 | fibril_mutex_unlock(&instance->guard);
|
---|
| 215 | }
|
---|
| 216 | break;
|
---|
| 217 |
|
---|
[563d9d0a] | 218 | case 0x9: /* Set Configuration */
|
---|
| 219 | case 0x11: /* Set Interface */
|
---|
[7265558] | 220 | /* Recipient must be device, this resets all endpoints,
|
---|
| 221 | * In fact there should be no endpoints but EP 0 registered
|
---|
| 222 | * as different interfaces use different endpoints. */
|
---|
[ba038f4] | 223 | if ((data[0] & 0xf) == 0) {
|
---|
| 224 | fibril_mutex_lock(&instance->guard);
|
---|
[7265558] | 225 | list_foreach(*get_list(instance, target.address), it) {
|
---|
| 226 | endpoint_t *ep = endpoint_get_instance(it);
|
---|
| 227 | if (ep->address == target.address) {
|
---|
| 228 | endpoint_toggle_set(ep,0);
|
---|
| 229 | }
|
---|
| 230 | }
|
---|
[ba038f4] | 231 | fibril_mutex_unlock(&instance->guard);
|
---|
| 232 | }
|
---|
| 233 | break;
|
---|
| 234 | }
|
---|
| 235 | }
|
---|