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

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

WIP usbhost refactoring

This commit replaces callbacks with more systematic virtual-like inheritance-like solution. Currently breaks build of HelenOS, but both xhci and usbhost are buildable. More refactoring follows…

  • Property mode set to 100644
File size: 3.9 KB
Line 
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 <usb/host/bandwidth.h>
37
38#include <assert.h>
39#include <stdlib.h>
40
41/** Calculate bandwidth that needs to be reserved for communication with EP.
42 * Calculation follows USB 1.1 specification.
43 * @param speed Device's speed.
44 * @param type Type of the transfer.
45 * @param size Number of byte to transfer.
46 * @param max_packet_size Maximum bytes in one packet.
47 */
48size_t bandwidth_count_usb11(usb_speed_t speed, usb_transfer_type_t type,
49 size_t size, size_t max_packet_size)
50{
51 /* We care about bandwidth only for interrupt and isochronous. */
52 if ((type != USB_TRANSFER_INTERRUPT)
53 && (type != USB_TRANSFER_ISOCHRONOUS)) {
54 return 0;
55 }
56
57 const unsigned packet_count =
58 (size + max_packet_size - 1) / max_packet_size;
59 /* TODO: It may be that ISO and INT transfers use only one packet per
60 * transaction, but I did not find text in USB spec to confirm this */
61 /* NOTE: All data packets will be considered to be max_packet_size */
62 switch (speed)
63 {
64 case USB_SPEED_LOW:
65 assert(type == USB_TRANSFER_INTERRUPT);
66 /* Protocol overhead 13B
67 * (3 SYNC bytes, 3 PID bytes, 2 Endpoint + CRC bytes, 2
68 * CRC bytes, and a 3-byte interpacket delay)
69 * see USB spec page 45-46. */
70 /* Speed penalty 8: low speed is 8-times slower*/
71 return packet_count * (13 + max_packet_size) * 8;
72 case USB_SPEED_FULL:
73 /* Interrupt transfer overhead see above
74 * or page 45 of USB spec */
75 if (type == USB_TRANSFER_INTERRUPT)
76 return packet_count * (13 + max_packet_size);
77
78 assert(type == USB_TRANSFER_ISOCHRONOUS);
79 /* Protocol overhead 9B
80 * (2 SYNC bytes, 2 PID bytes, 2 Endpoint + CRC bytes, 2 CRC
81 * bytes, and a 1-byte interpacket delay)
82 * see USB spec page 42 */
83 return packet_count * (9 + max_packet_size);
84 default:
85 return 0;
86 }
87}
88
89/** Calculate bandwidth that needs to be reserved for communication with EP.
90 * Calculation follows USB 2.0 specification.
91 * @param speed Device's speed.
92 * @param type Type of the transfer.
93 * @param size Number of byte to transfer.
94 * @param max_packet_size Maximum bytes in one packet.
95 */
96size_t bandwidth_count_usb20(usb_speed_t speed, usb_transfer_type_t type,
97 size_t size, size_t max_packet_size)
98{
99 /* We care about bandwidth only for interrupt and isochronous. */
100 if ((type != USB_TRANSFER_INTERRUPT)
101 && (type != USB_TRANSFER_ISOCHRONOUS)) {
102 return 0;
103 }
104 //TODO Implement
105 return 0;
106}
Note: See TracBrowser for help on using the repository browser.