| 1 | /*
 | 
|---|
| 2 |  * Copyright (c) 2011 Jan Vesely
 | 
|---|
| 3 |  * Copyright (c) 2018 Ondrej Hlavaty
 | 
|---|
| 4 |  * All rights reserved.
 | 
|---|
| 5 |  *
 | 
|---|
| 6 |  * Redistribution and use in source and binary forms, with or without
 | 
|---|
| 7 |  * modification, are permitted provided that the following conditions
 | 
|---|
| 8 |  * are met:
 | 
|---|
| 9 |  *
 | 
|---|
| 10 |  * - Redistributions of source code must retain the above copyright
 | 
|---|
| 11 |  *   notice, this list of conditions and the following disclaimer.
 | 
|---|
| 12 |  * - Redistributions in binary form must reproduce the above copyright
 | 
|---|
| 13 |  *   notice, this list of conditions and the following disclaimer in the
 | 
|---|
| 14 |  *   documentation and/or other materials provided with the distribution.
 | 
|---|
| 15 |  * - The name of the author may not be used to endorse or promote products
 | 
|---|
| 16 |  *   derived from this software without specific prior written permission.
 | 
|---|
| 17 |  *
 | 
|---|
| 18 |  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 | 
|---|
| 19 |  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 | 
|---|
| 20 |  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 | 
|---|
| 21 |  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 | 
|---|
| 22 |  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 | 
|---|
| 23 |  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 | 
|---|
| 24 |  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 | 
|---|
| 25 |  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 | 
|---|
| 26 |  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 | 
|---|
| 27 |  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 | 
|---|
| 28 |  */
 | 
|---|
| 29 | /**  @addtogroup libusbhost
 | 
|---|
| 30 |  * @{
 | 
|---|
| 31 |  */
 | 
|---|
| 32 | /** @file
 | 
|---|
| 33 |  *
 | 
|---|
| 34 |  * Bandwidth calculation functions. Shared among uhci, ohci and ehci drivers.
 | 
|---|
| 35 |  */
 | 
|---|
| 36 | 
 | 
|---|
| 37 | #include <assert.h>
 | 
|---|
| 38 | #include <stdlib.h>
 | 
|---|
| 39 | 
 | 
|---|
| 40 | #include "endpoint.h"
 | 
|---|
| 41 | #include "bus.h"
 | 
|---|
| 42 | 
 | 
|---|
| 43 | #include "bandwidth.h"
 | 
|---|
| 44 | 
 | 
|---|
| 45 | /** Bytes per second in FULL SPEED */
 | 
|---|
| 46 | #define BANDWIDTH_TOTAL_USB11 (12000000 / 8)
 | 
|---|
| 47 | /** 90% of total bandwidth is available for periodic transfers */
 | 
|---|
| 48 | #define BANDWIDTH_AVAILABLE_USB11 ((BANDWIDTH_TOTAL_USB11 * 9) / 10)
 | 
|---|
| 49 | 
 | 
|---|
| 50 | /**
 | 
|---|
| 51 |  * Calculate bandwidth that needs to be reserved for communication with EP.
 | 
|---|
| 52 |  * Calculation follows USB 1.1 specification.
 | 
|---|
| 53 |  *
 | 
|---|
| 54 |  * @param ep An endpoint for which the bandwidth is to be counted
 | 
|---|
| 55 |  */
 | 
|---|
| 56 | static size_t bandwidth_count_usb11(endpoint_t *ep)
 | 
|---|
| 57 | {
 | 
|---|
| 58 |         assert(ep);
 | 
|---|
| 59 |         assert(ep->device);
 | 
|---|
| 60 | 
 | 
|---|
| 61 |         const usb_transfer_type_t type = ep->transfer_type;
 | 
|---|
| 62 | 
 | 
|---|
| 63 |         /* We care about bandwidth only for interrupt and isochronous. */
 | 
|---|
| 64 |         if ((type != USB_TRANSFER_INTERRUPT) &&
 | 
|---|
| 65 |             (type != USB_TRANSFER_ISOCHRONOUS)) {
 | 
|---|
| 66 |                 return 0;
 | 
|---|
| 67 |         }
 | 
|---|
| 68 | 
 | 
|---|
| 69 |         const size_t max_packet_size = ep->max_packet_size;
 | 
|---|
| 70 |         const size_t packet_count = ep->packets_per_uframe;
 | 
|---|
| 71 | 
 | 
|---|
| 72 |         /*
 | 
|---|
| 73 |          * TODO: It may be that ISO and INT transfers use only one packet per
 | 
|---|
| 74 |          * transaction, but I did not find text in USB spec to confirm this
 | 
|---|
| 75 |          */
 | 
|---|
| 76 |         /* NOTE: All data packets will be considered to be max_packet_size */
 | 
|---|
| 77 |         switch (ep->device->speed) {
 | 
|---|
| 78 |         case USB_SPEED_LOW:
 | 
|---|
| 79 |                 assert(type == USB_TRANSFER_INTERRUPT);
 | 
|---|
| 80 |                 /*
 | 
|---|
| 81 |                  * Protocol overhead 13B
 | 
|---|
| 82 |                  * (3 SYNC bytes, 3 PID bytes, 2 Endpoint + CRC bytes, 2
 | 
|---|
| 83 |                  * CRC bytes, and a 3-byte interpacket delay)
 | 
|---|
| 84 |                  * see USB spec page 45-46.
 | 
|---|
| 85 |                  */
 | 
|---|
| 86 |                 /* Speed penalty 8: low speed is 8-times slower*/
 | 
|---|
| 87 |                 return packet_count * (13 + max_packet_size) * 8;
 | 
|---|
| 88 |         case USB_SPEED_FULL:
 | 
|---|
| 89 |                 /*
 | 
|---|
| 90 |                  * Interrupt transfer overhead see above
 | 
|---|
| 91 |                  * or page 45 of USB spec
 | 
|---|
| 92 |                  */
 | 
|---|
| 93 |                 if (type == USB_TRANSFER_INTERRUPT)
 | 
|---|
| 94 |                         return packet_count * (13 + max_packet_size);
 | 
|---|
| 95 | 
 | 
|---|
| 96 |                 assert(type == USB_TRANSFER_ISOCHRONOUS);
 | 
|---|
| 97 |                 /*
 | 
|---|
| 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 |                  */
 | 
|---|
| 103 |                 return packet_count * (9 + max_packet_size);
 | 
|---|
| 104 |         default:
 | 
|---|
| 105 |                 return 0;
 | 
|---|
| 106 |         }
 | 
|---|
| 107 | }
 | 
|---|
| 108 | 
 | 
|---|
| 109 | const bandwidth_accounting_t bandwidth_accounting_usb11 = {
 | 
|---|
| 110 |         .available_bandwidth = BANDWIDTH_AVAILABLE_USB11,
 | 
|---|
| 111 |         .count_bw = &bandwidth_count_usb11,
 | 
|---|
| 112 | };
 | 
|---|
| 113 | 
 | 
|---|
| 114 | /** Number of nanoseconds in one microframe */
 | 
|---|
| 115 | #define BANDWIDTH_TOTAL_USB2 (125000)
 | 
|---|
| 116 | /** 90% of total bandwidth is available for periodic transfers */
 | 
|---|
| 117 | #define BANDWIDTH_AVAILABLE_USB2  ((BANDWIDTH_TOTAL_USB2 * 9) / 10)
 | 
|---|
| 118 | 
 | 
|---|
| 119 | /**
 | 
|---|
| 120 |  * Calculate bandwidth that needs to be reserved for communication with EP.
 | 
|---|
| 121 |  * Calculation follows USB 2.0 specification, chapter 5.11.3.
 | 
|---|
| 122 |  *
 | 
|---|
| 123 |  * FIXME: Interrupt transfers shall be probably divided by their polling interval.
 | 
|---|
| 124 |  *
 | 
|---|
| 125 |  * @param ep An endpoint for which the bandwidth is to be counted
 | 
|---|
| 126 |  * @return Number of nanoseconds transaction with @c size bytes payload will
 | 
|---|
| 127 |  *         take.
 | 
|---|
| 128 |  */
 | 
|---|
| 129 | static size_t bandwidth_count_usb2(endpoint_t *ep)
 | 
|---|
| 130 | {
 | 
|---|
| 131 |         assert(ep);
 | 
|---|
| 132 |         assert(ep->device);
 | 
|---|
| 133 | 
 | 
|---|
| 134 |         const usb_transfer_type_t type = ep->transfer_type;
 | 
|---|
| 135 | 
 | 
|---|
| 136 |         /* We care about bandwidth only for interrupt and isochronous. */
 | 
|---|
| 137 |         if ((type != USB_TRANSFER_INTERRUPT) &&
 | 
|---|
| 138 |             (type != USB_TRANSFER_ISOCHRONOUS)) {
 | 
|---|
| 139 |                 return 0;
 | 
|---|
| 140 |         }
 | 
|---|
| 141 | 
 | 
|---|
| 142 |         // FIXME: Come up with some upper bound for these (in ns).
 | 
|---|
| 143 |         const size_t host_delay = 0;
 | 
|---|
| 144 |         const size_t hub_ls_setup = 0;
 | 
|---|
| 145 | 
 | 
|---|
| 146 |         // Approx. Floor(3.167 + BitStuffTime(Data_bc))
 | 
|---|
| 147 |         const size_t base_time = (ep->max_transfer_size * 8 + 19) / 6;
 | 
|---|
| 148 | 
 | 
|---|
| 149 |         switch (ep->device->speed) {
 | 
|---|
| 150 |         case USB_SPEED_LOW:
 | 
|---|
| 151 |                 if (ep->direction == USB_DIRECTION_IN)
 | 
|---|
| 152 |                         return 64060 + (2 * hub_ls_setup) +
 | 
|---|
| 153 |                             (677 * base_time) + host_delay;
 | 
|---|
| 154 |                 else
 | 
|---|
| 155 |                         return 64107 + (2 * hub_ls_setup) +
 | 
|---|
| 156 |                             (667 * base_time) + host_delay;
 | 
|---|
| 157 | 
 | 
|---|
| 158 |         case USB_SPEED_FULL:
 | 
|---|
| 159 |                 if (ep->transfer_type == USB_TRANSFER_INTERRUPT)
 | 
|---|
| 160 |                         return 9107 + 84 * base_time + host_delay;
 | 
|---|
| 161 | 
 | 
|---|
| 162 |                 if (ep->direction == USB_DIRECTION_IN)
 | 
|---|
| 163 |                         return 7268 + 84 * base_time + host_delay;
 | 
|---|
| 164 |                 else
 | 
|---|
| 165 |                         return 6265 + 84 * base_time + host_delay;
 | 
|---|
| 166 | 
 | 
|---|
| 167 |         case USB_SPEED_HIGH:
 | 
|---|
| 168 |                 if (ep->transfer_type == USB_TRANSFER_INTERRUPT)
 | 
|---|
| 169 |                         return (3648 + 25 * base_time + 11) / 12 + host_delay;
 | 
|---|
| 170 |                 else
 | 
|---|
| 171 |                         return (5280 + 25 * base_time + 11) / 12 + host_delay;
 | 
|---|
| 172 | 
 | 
|---|
| 173 |         default:
 | 
|---|
| 174 |                 return 0;
 | 
|---|
| 175 |         }
 | 
|---|
| 176 | }
 | 
|---|
| 177 | 
 | 
|---|
| 178 | const bandwidth_accounting_t bandwidth_accounting_usb2 = {
 | 
|---|
| 179 |         .available_bandwidth = BANDWIDTH_AVAILABLE_USB2,
 | 
|---|
| 180 |         .count_bw = &bandwidth_count_usb2,
 | 
|---|
| 181 | };
 | 
|---|