[231748a] | 1 | /*
|
---|
| 2 | * Copyright (c) 2011 Vojtech Horky
|
---|
| 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 |
|
---|
[160b75e] | 29 | /** @addtogroup libusbdev
|
---|
[231748a] | 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file
|
---|
| 33 | * Handling alternate interface settings.
|
---|
| 34 | */
|
---|
[77ad86c] | 35 |
|
---|
[7d521e24] | 36 | #include <usb/dev/driver.h>
|
---|
| 37 | #include <usb/dev/request.h>
|
---|
[231748a] | 38 | #include <usb/debug.h>
|
---|
[7d521e24] | 39 | #include <usb/dev/dp.h>
|
---|
[231748a] | 40 | #include <errno.h>
|
---|
| 41 | #include <str_error.h>
|
---|
| 42 | #include <assert.h>
|
---|
| 43 |
|
---|
| 44 | /** Count number of alternate settings of a interface.
|
---|
| 45 | *
|
---|
| 46 | * @param config_descr Full configuration descriptor.
|
---|
| 47 | * @param config_descr_size Size of @p config_descr in bytes.
|
---|
| 48 | * @param interface_no Interface number.
|
---|
| 49 | * @return Number of alternate interfaces for @p interface_no interface.
|
---|
| 50 | */
|
---|
[7c95d6f5] | 51 | size_t usb_interface_count_alternates(const uint8_t *config_descr,
|
---|
[231748a] | 52 | size_t config_descr_size, uint8_t interface_no)
|
---|
| 53 | {
|
---|
| 54 | assert(config_descr != NULL);
|
---|
| 55 | assert(config_descr_size > 0);
|
---|
| 56 |
|
---|
[8a121b1] | 57 | const usb_dp_parser_t dp_parser = {
|
---|
[231748a] | 58 | .nesting = usb_dp_standard_descriptor_nesting
|
---|
| 59 | };
|
---|
[8a121b1] | 60 | const usb_dp_parser_data_t dp_data = {
|
---|
[231748a] | 61 | .data = config_descr,
|
---|
| 62 | .size = config_descr_size,
|
---|
| 63 | .arg = NULL
|
---|
| 64 | };
|
---|
| 65 |
|
---|
| 66 | size_t alternate_count = 0;
|
---|
| 67 |
|
---|
[0255d36] | 68 | const void *iface_ptr =
|
---|
| 69 | usb_dp_get_nested_descriptor(&dp_parser, &dp_data, config_descr);
|
---|
[231748a] | 70 | while (iface_ptr != NULL) {
|
---|
[0255d36] | 71 | const usb_standard_interface_descriptor_t *iface = iface_ptr;
|
---|
| 72 | if (iface->descriptor_type == USB_DESCTYPE_INTERFACE
|
---|
| 73 | && iface->interface_number == interface_no) {
|
---|
| 74 | ++alternate_count;
|
---|
[231748a] | 75 | }
|
---|
| 76 | iface_ptr = usb_dp_get_sibling_descriptor(&dp_parser, &dp_data,
|
---|
| 77 | config_descr, iface_ptr);
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | return alternate_count;
|
---|
| 81 | }
|
---|
| 82 |
|
---|
[0255d36] | 83 | /** Initialize alternate interface representation structure.
|
---|
[231748a] | 84 | *
|
---|
[0255d36] | 85 | * @param[in] alternates Pointer to allocated structure.
|
---|
[231748a] | 86 | * @param[in] config_descr Configuration descriptor.
|
---|
| 87 | * @param[in] config_descr_size Size of configuration descriptor.
|
---|
| 88 | * @param[in] interface_number Interface number.
|
---|
| 89 | * @return Error code.
|
---|
| 90 | */
|
---|
[904dcc6] | 91 | int usb_alternate_interfaces_init(usb_alternate_interfaces_t *alternates,
|
---|
| 92 | const uint8_t *config_descr, size_t config_descr_size, int interface_number)
|
---|
[231748a] | 93 | {
|
---|
[904dcc6] | 94 | assert(alternates != NULL);
|
---|
[231748a] | 95 | assert(config_descr != NULL);
|
---|
| 96 | assert(config_descr_size > 0);
|
---|
| 97 |
|
---|
[904dcc6] | 98 | alternates->alternatives = NULL;
|
---|
| 99 | alternates->alternative_count = 0;
|
---|
| 100 | alternates->current = 0;
|
---|
| 101 |
|
---|
[0255d36] | 102 | /* No interfaces. */
|
---|
[231748a] | 103 | if (interface_number < 0) {
|
---|
| 104 | return EOK;
|
---|
| 105 | }
|
---|
| 106 |
|
---|
| 107 | alternates->alternative_count
|
---|
| 108 | = usb_interface_count_alternates(config_descr, config_descr_size,
|
---|
[0255d36] | 109 | interface_number);
|
---|
[231748a] | 110 |
|
---|
| 111 | if (alternates->alternative_count == 0) {
|
---|
| 112 | return ENOENT;
|
---|
| 113 | }
|
---|
| 114 |
|
---|
[96ec0a9] | 115 | alternates->alternatives = calloc(alternates->alternative_count,
|
---|
| 116 | sizeof(usb_alternate_interface_descriptors_t));
|
---|
[231748a] | 117 | if (alternates->alternatives == NULL) {
|
---|
| 118 | return ENOMEM;
|
---|
| 119 | }
|
---|
| 120 |
|
---|
[904dcc6] | 121 | const usb_dp_parser_t dp_parser = {
|
---|
[231748a] | 122 | .nesting = usb_dp_standard_descriptor_nesting
|
---|
| 123 | };
|
---|
[904dcc6] | 124 | const usb_dp_parser_data_t dp_data = {
|
---|
[231748a] | 125 | .data = config_descr,
|
---|
| 126 | .size = config_descr_size,
|
---|
| 127 | .arg = NULL
|
---|
| 128 | };
|
---|
| 129 |
|
---|
[69b9740] | 130 | usb_alternate_interface_descriptors_t *iterator
|
---|
[231748a] | 131 | = &alternates->alternatives[0];
|
---|
| 132 |
|
---|
[69b9740] | 133 | const usb_alternate_interface_descriptors_t *end
|
---|
| 134 | = &alternates->alternatives[alternates->alternative_count];
|
---|
| 135 |
|
---|
[0255d36] | 136 | const void *iface_ptr =
|
---|
| 137 | usb_dp_get_nested_descriptor(&dp_parser, &dp_data, dp_data.data);
|
---|
| 138 |
|
---|
[69b9740] | 139 | while (iface_ptr != NULL && iterator < end) {
|
---|
[0255d36] | 140 | const usb_standard_interface_descriptor_t *iface = iface_ptr;
|
---|
| 141 |
|
---|
[231748a] | 142 | if ((iface->descriptor_type != USB_DESCTYPE_INTERFACE)
|
---|
| 143 | || (iface->interface_number != interface_number)) {
|
---|
[0255d36] | 144 | /* This is not a valid alternate interface descriptor
|
---|
| 145 | * for interface with number == interface_number. */
|
---|
[231748a] | 146 | iface_ptr = usb_dp_get_sibling_descriptor(&dp_parser,
|
---|
[ab27e01] | 147 | &dp_data, dp_data.data, iface_ptr);
|
---|
[231748a] | 148 | continue;
|
---|
| 149 | }
|
---|
| 150 |
|
---|
[69b9740] | 151 | iterator->interface = iface;
|
---|
| 152 | iterator->nested_descriptors = iface_ptr + sizeof(*iface);
|
---|
[231748a] | 153 |
|
---|
| 154 | /* Find next interface to count size of nested descriptors. */
|
---|
| 155 | iface_ptr = usb_dp_get_sibling_descriptor(&dp_parser, &dp_data,
|
---|
| 156 | dp_data.data, iface_ptr);
|
---|
| 157 |
|
---|
[0255d36] | 158 | const uint8_t *next = (iface_ptr == NULL) ?
|
---|
| 159 | dp_data.data + dp_data.size : iface_ptr;
|
---|
| 160 |
|
---|
[69b9740] | 161 | iterator->nested_descriptors_size
|
---|
| 162 | = next - iterator->nested_descriptors;
|
---|
[0255d36] | 163 |
|
---|
[69b9740] | 164 | ++iterator;
|
---|
[231748a] | 165 | }
|
---|
| 166 |
|
---|
| 167 | return EOK;
|
---|
| 168 | }
|
---|
| 169 |
|
---|
[6e3c005] | 170 | /** Clean initialized structure.
|
---|
| 171 | * @param instance structure do deinitialize.
|
---|
| 172 | */
|
---|
| 173 | void usb_alternate_interfaces_deinit(usb_alternate_interfaces_t *instance)
|
---|
[96ec0a9] | 174 | {
|
---|
[6e3c005] | 175 | if (!instance)
|
---|
[96ec0a9] | 176 | return;
|
---|
[6e3c005] | 177 | free(instance->alternatives);
|
---|
| 178 | instance->alternatives = NULL;
|
---|
[96ec0a9] | 179 | }
|
---|
[231748a] | 180 | /**
|
---|
| 181 | * @}
|
---|
| 182 | */
|
---|