source: mainline/uspace/lib/usbhost/src/bandwidth.c@ ecbad17

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since ecbad17 was ecbad17, checked in by Ondřej Hlavatý <aearsis@…>, 7 years ago

usbhost: Implemented bandwidth accounting for USB 2.0

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