| 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 | /** @addtogroup libusbhost
|
|---|
| 29 | * @{
|
|---|
| 30 | */
|
|---|
| 31 | /** @file
|
|---|
| 32 | *
|
|---|
| 33 | * Bandwidth calculation functions. Shared among uhci, ohci and ehci drivers.
|
|---|
| 34 | */
|
|---|
| 35 |
|
|---|
| 36 | #include <assert.h>
|
|---|
| 37 | #include <stdlib.h>
|
|---|
| 38 |
|
|---|
| 39 | #include "endpoint.h"
|
|---|
| 40 | #include "bus.h"
|
|---|
| 41 |
|
|---|
| 42 | #include "bandwidth.h"
|
|---|
| 43 |
|
|---|
| 44 | /**
|
|---|
| 45 | * Calculate bandwidth that needs to be reserved for communication with EP.
|
|---|
| 46 | * Calculation follows USB 1.1 specification.
|
|---|
| 47 | * @param ep Registered endpoint
|
|---|
| 48 | * @param size Number of bytes to transfer.
|
|---|
| 49 | * @param max_packet_size Maximum bytes in one packet.
|
|---|
| 50 | */
|
|---|
| 51 | ssize_t bandwidth_count_usb11(endpoint_t *ep, size_t size)
|
|---|
| 52 | {
|
|---|
| 53 | assert(ep);
|
|---|
| 54 | assert(ep->device);
|
|---|
| 55 |
|
|---|
| 56 | const usb_transfer_type_t type = ep->transfer_type;
|
|---|
| 57 |
|
|---|
| 58 | /* We care about bandwidth only for interrupt and isochronous. */
|
|---|
| 59 | if ((type != USB_TRANSFER_INTERRUPT)
|
|---|
| 60 | && (type != USB_TRANSFER_ISOCHRONOUS)) {
|
|---|
| 61 | return 0;
|
|---|
| 62 | }
|
|---|
| 63 |
|
|---|
| 64 | const size_t max_packet_size = ep->max_packet_size;
|
|---|
| 65 |
|
|---|
| 66 | const unsigned packet_count =
|
|---|
| 67 | (size + max_packet_size - 1) / max_packet_size;
|
|---|
| 68 | /* TODO: It may be that ISO and INT transfers use only one packet per
|
|---|
| 69 | * transaction, but I did not find text in USB spec to confirm this */
|
|---|
| 70 | /* NOTE: All data packets will be considered to be max_packet_size */
|
|---|
| 71 | switch (ep->device->speed)
|
|---|
| 72 | {
|
|---|
| 73 | case USB_SPEED_LOW:
|
|---|
| 74 | assert(type == USB_TRANSFER_INTERRUPT);
|
|---|
| 75 | /* Protocol overhead 13B
|
|---|
| 76 | * (3 SYNC bytes, 3 PID bytes, 2 Endpoint + CRC bytes, 2
|
|---|
| 77 | * CRC bytes, and a 3-byte interpacket delay)
|
|---|
| 78 | * see USB spec page 45-46. */
|
|---|
| 79 | /* Speed penalty 8: low speed is 8-times slower*/
|
|---|
| 80 | return packet_count * (13 + max_packet_size) * 8;
|
|---|
| 81 | case USB_SPEED_FULL:
|
|---|
| 82 | /* Interrupt transfer overhead see above
|
|---|
| 83 | * or page 45 of USB spec */
|
|---|
| 84 | if (type == USB_TRANSFER_INTERRUPT)
|
|---|
| 85 | return packet_count * (13 + max_packet_size);
|
|---|
| 86 |
|
|---|
| 87 | assert(type == USB_TRANSFER_ISOCHRONOUS);
|
|---|
| 88 | /* Protocol overhead 9B
|
|---|
| 89 | * (2 SYNC bytes, 2 PID bytes, 2 Endpoint + CRC bytes, 2 CRC
|
|---|
| 90 | * bytes, and a 1-byte interpacket delay)
|
|---|
| 91 | * see USB spec page 42 */
|
|---|
| 92 | return packet_count * (9 + max_packet_size);
|
|---|
| 93 | default:
|
|---|
| 94 | return 0;
|
|---|
| 95 | }
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 | /**
|
|---|
| 99 | * Calculate bandwidth that needs to be reserved for communication with EP.
|
|---|
| 100 | * Calculation follows USB 2.0 specification, chapter 5.11.3.
|
|---|
| 101 | *
|
|---|
| 102 | * @param speed Device's speed.
|
|---|
| 103 | * @param type Type of the transfer.
|
|---|
| 104 | * @param size Number of byte to transfer.
|
|---|
| 105 | * @param max_packet_size Maximum bytes in one packet.
|
|---|
| 106 | * @return Number of nanoseconds transaction with @c size bytes payload will
|
|---|
| 107 | * take.
|
|---|
| 108 | */
|
|---|
| 109 | ssize_t bandwidth_count_usb20(endpoint_t *ep, size_t size)
|
|---|
| 110 | {
|
|---|
| 111 | assert(ep);
|
|---|
| 112 |
|
|---|
| 113 | const usb_transfer_type_t type = ep->transfer_type;
|
|---|
| 114 |
|
|---|
| 115 | /* We care about bandwidth only for interrupt and isochronous. */
|
|---|
| 116 | if ((type != USB_TRANSFER_INTERRUPT)
|
|---|
| 117 | && (type != USB_TRANSFER_ISOCHRONOUS)) {
|
|---|
| 118 | return 0;
|
|---|
| 119 | }
|
|---|
| 120 |
|
|---|
| 121 | // FIXME: Come up with some upper bound for these (in ns).
|
|---|
| 122 | const size_t host_delay = 0;
|
|---|
| 123 | const size_t hub_ls_setup = 0;
|
|---|
| 124 |
|
|---|
| 125 | // Approx. Floor(3.167 + BitStuffTime(Data_bc))
|
|---|
| 126 | const size_t base_time = (size * 8 + 19) / 6;
|
|---|
| 127 |
|
|---|
| 128 | switch (ep->device->speed) {
|
|---|
| 129 | case USB_SPEED_LOW:
|
|---|
| 130 | if (ep->direction == USB_DIRECTION_IN)
|
|---|
| 131 | return 64060 + (2 * hub_ls_setup) + (677 * base_time) + host_delay;
|
|---|
| 132 | else
|
|---|
| 133 | return 64107 + (2 * hub_ls_setup) + (667 * base_time) + host_delay;
|
|---|
| 134 |
|
|---|
| 135 | case USB_SPEED_FULL:
|
|---|
| 136 | if (ep->transfer_type == USB_TRANSFER_INTERRUPT)
|
|---|
| 137 | return 9107 + 84 * base_time + host_delay;
|
|---|
| 138 |
|
|---|
| 139 | if (ep->direction == USB_DIRECTION_IN)
|
|---|
| 140 | return 7268 + 84 * base_time + host_delay;
|
|---|
| 141 | else
|
|---|
| 142 | return 6265 + 84 * base_time + host_delay;
|
|---|
| 143 |
|
|---|
| 144 | case USB_SPEED_HIGH:
|
|---|
| 145 | if (ep->transfer_type == USB_TRANSFER_INTERRUPT)
|
|---|
| 146 | return (3648 + 25 * base_time + 11) / 12 + host_delay;
|
|---|
| 147 | else
|
|---|
| 148 | return (5280 + 25 * base_time + 11) / 12 + host_delay;
|
|---|
| 149 |
|
|---|
| 150 | default:
|
|---|
| 151 | return 0;
|
|---|
| 152 | }
|
|---|
| 153 | }
|
|---|