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