[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 | */
|
---|
[7d521e24] | 35 | #include <usb/dev/driver.h>
|
---|
| 36 | #include <usb/dev/request.h>
|
---|
[231748a] | 37 | #include <usb/debug.h>
|
---|
[7d521e24] | 38 | #include <usb/dev/dp.h>
|
---|
[231748a] | 39 | #include <errno.h>
|
---|
| 40 | #include <str_error.h>
|
---|
| 41 | #include <assert.h>
|
---|
| 42 |
|
---|
| 43 | /** Count number of alternate settings of a interface.
|
---|
| 44 | *
|
---|
| 45 | * @param config_descr Full configuration descriptor.
|
---|
| 46 | * @param config_descr_size Size of @p config_descr in bytes.
|
---|
| 47 | * @param interface_no Interface number.
|
---|
| 48 | * @return Number of alternate interfaces for @p interface_no interface.
|
---|
| 49 | */
|
---|
[7c95d6f5] | 50 | size_t usb_interface_count_alternates(const uint8_t *config_descr,
|
---|
[231748a] | 51 | size_t config_descr_size, uint8_t interface_no)
|
---|
| 52 | {
|
---|
| 53 | assert(config_descr != NULL);
|
---|
| 54 | assert(config_descr_size > 0);
|
---|
| 55 |
|
---|
[8a121b1] | 56 | const usb_dp_parser_t dp_parser = {
|
---|
[231748a] | 57 | .nesting = usb_dp_standard_descriptor_nesting
|
---|
| 58 | };
|
---|
[8a121b1] | 59 | const usb_dp_parser_data_t dp_data = {
|
---|
[231748a] | 60 | .data = config_descr,
|
---|
| 61 | .size = config_descr_size,
|
---|
| 62 | .arg = NULL
|
---|
| 63 | };
|
---|
| 64 |
|
---|
| 65 | size_t alternate_count = 0;
|
---|
| 66 |
|
---|
[8a121b1] | 67 | const uint8_t *iface_ptr = usb_dp_get_nested_descriptor(&dp_parser,
|
---|
[231748a] | 68 | &dp_data, config_descr);
|
---|
| 69 | while (iface_ptr != NULL) {
|
---|
| 70 | usb_standard_interface_descriptor_t *iface
|
---|
| 71 | = (usb_standard_interface_descriptor_t *) iface_ptr;
|
---|
| 72 | if (iface->descriptor_type == USB_DESCTYPE_INTERFACE) {
|
---|
| 73 | if (iface->interface_number == interface_no) {
|
---|
| 74 | alternate_count++;
|
---|
| 75 | }
|
---|
| 76 | }
|
---|
| 77 | iface_ptr = usb_dp_get_sibling_descriptor(&dp_parser, &dp_data,
|
---|
| 78 | config_descr, iface_ptr);
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | return alternate_count;
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | /** Create alternate interface representation structure.
|
---|
| 85 | *
|
---|
| 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 | * @param[out] alternates_ptr Where to store pointer to allocated structure.
|
---|
| 90 | * @return Error code.
|
---|
| 91 | */
|
---|
[7c95d6f5] | 92 | int usb_alternate_interfaces_create(const uint8_t *config_descr,
|
---|
[231748a] | 93 | size_t config_descr_size, int interface_number,
|
---|
| 94 | usb_alternate_interfaces_t **alternates_ptr)
|
---|
| 95 | {
|
---|
| 96 | assert(alternates_ptr != NULL);
|
---|
| 97 | assert(config_descr != NULL);
|
---|
| 98 | assert(config_descr_size > 0);
|
---|
| 99 |
|
---|
[96ec0a9] | 100 | alternates_ptr = NULL;
|
---|
[231748a] | 101 | if (interface_number < 0) {
|
---|
| 102 | return EOK;
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 | usb_alternate_interfaces_t *alternates
|
---|
| 106 | = malloc(sizeof(usb_alternate_interfaces_t));
|
---|
| 107 | if (alternates == NULL) {
|
---|
| 108 | return ENOMEM;
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | alternates->alternative_count
|
---|
| 112 | = usb_interface_count_alternates(config_descr, config_descr_size,
|
---|
| 113 | interface_number);
|
---|
| 114 |
|
---|
| 115 | if (alternates->alternative_count == 0) {
|
---|
| 116 | free(alternates);
|
---|
| 117 | return ENOENT;
|
---|
| 118 | }
|
---|
| 119 |
|
---|
[96ec0a9] | 120 | alternates->alternatives = calloc(alternates->alternative_count,
|
---|
| 121 | sizeof(usb_alternate_interface_descriptors_t));
|
---|
[231748a] | 122 | if (alternates->alternatives == NULL) {
|
---|
| 123 | free(alternates);
|
---|
| 124 | return ENOMEM;
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | alternates->current = 0;
|
---|
| 128 |
|
---|
| 129 | usb_dp_parser_t dp_parser = {
|
---|
| 130 | .nesting = usb_dp_standard_descriptor_nesting
|
---|
| 131 | };
|
---|
| 132 | usb_dp_parser_data_t dp_data = {
|
---|
| 133 | .data = config_descr,
|
---|
| 134 | .size = config_descr_size,
|
---|
| 135 | .arg = NULL
|
---|
| 136 | };
|
---|
| 137 |
|
---|
| 138 | usb_alternate_interface_descriptors_t *cur_alt_iface
|
---|
| 139 | = &alternates->alternatives[0];
|
---|
| 140 |
|
---|
[8a121b1] | 141 | const uint8_t *iface_ptr = usb_dp_get_nested_descriptor(&dp_parser,
|
---|
[231748a] | 142 | &dp_data, dp_data.data);
|
---|
| 143 | while (iface_ptr != NULL) {
|
---|
| 144 | usb_standard_interface_descriptor_t *iface
|
---|
| 145 | = (usb_standard_interface_descriptor_t *) iface_ptr;
|
---|
| 146 | if ((iface->descriptor_type != USB_DESCTYPE_INTERFACE)
|
---|
| 147 | || (iface->interface_number != interface_number)) {
|
---|
| 148 | iface_ptr = usb_dp_get_sibling_descriptor(&dp_parser,
|
---|
| 149 | &dp_data,
|
---|
| 150 | dp_data.data, iface_ptr);
|
---|
| 151 | continue;
|
---|
| 152 | }
|
---|
| 153 |
|
---|
| 154 | cur_alt_iface->interface = iface;
|
---|
| 155 | cur_alt_iface->nested_descriptors = iface_ptr + sizeof(*iface);
|
---|
| 156 |
|
---|
| 157 | /* Find next interface to count size of nested descriptors. */
|
---|
| 158 | iface_ptr = usb_dp_get_sibling_descriptor(&dp_parser, &dp_data,
|
---|
| 159 | dp_data.data, iface_ptr);
|
---|
| 160 | if (iface_ptr == NULL) {
|
---|
[7c95d6f5] | 161 | const uint8_t *next = dp_data.data + dp_data.size;
|
---|
[231748a] | 162 | cur_alt_iface->nested_descriptors_size
|
---|
| 163 | = next - cur_alt_iface->nested_descriptors;
|
---|
| 164 | } else {
|
---|
| 165 | cur_alt_iface->nested_descriptors_size
|
---|
| 166 | = iface_ptr - cur_alt_iface->nested_descriptors;
|
---|
| 167 | }
|
---|
| 168 |
|
---|
| 169 | cur_alt_iface++;
|
---|
| 170 | }
|
---|
| 171 |
|
---|
| 172 | *alternates_ptr = alternates;
|
---|
| 173 |
|
---|
| 174 | return EOK;
|
---|
| 175 | }
|
---|
| 176 |
|
---|
[96ec0a9] | 177 | void usb_alternate_interfaces_destroy(usb_alternate_interfaces_t *alternate)
|
---|
| 178 | {
|
---|
| 179 | if (!alternate)
|
---|
| 180 | return;
|
---|
| 181 | free(alternate->alternatives);
|
---|
| 182 | free(alternate);
|
---|
| 183 | }
|
---|
[231748a] | 184 | /**
|
---|
| 185 | * @}
|
---|
| 186 | */
|
---|