source: mainline/uspace/lib/usbdev/src/altiface.c@ 241ab7e

serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 241ab7e was 09ab0a9a, checked in by Jiri Svoboda <jiri@…>, 7 years ago

Fix vertical spacing with new Ccheck revision.

  • Property mode set to 100644
File size: 5.4 KB
RevLine 
[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
[b208b3b]36#include <usb/dev/alternate_ifaces.h>
[7d521e24]37#include <usb/dev/dp.h>
[231748a]38#include <assert.h>
[c01987c]39#include <errno.h>
40#include <stdlib.h>
[582a0b8]41#include <stddef.h>
[231748a]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]50size_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
[0255d36]67 const void *iface_ptr =
68 usb_dp_get_nested_descriptor(&dp_parser, &dp_data, config_descr);
[231748a]69 while (iface_ptr != NULL) {
[0255d36]70 const usb_standard_interface_descriptor_t *iface = iface_ptr;
[3bacee1]71 if (iface->descriptor_type == USB_DESCTYPE_INTERFACE &&
72 iface->interface_number == interface_no) {
[0255d36]73 ++alternate_count;
[231748a]74 }
75 iface_ptr = usb_dp_get_sibling_descriptor(&dp_parser, &dp_data,
76 config_descr, iface_ptr);
77 }
78
79 return alternate_count;
80}
81
[0255d36]82/** Initialize alternate interface representation structure.
[231748a]83 *
[0255d36]84 * @param[in] alternates Pointer to allocated structure.
[231748a]85 * @param[in] config_descr Configuration descriptor.
86 * @param[in] config_descr_size Size of configuration descriptor.
87 * @param[in] interface_number Interface number.
88 * @return Error code.
89 */
[b7fd2a0]90errno_t usb_alternate_interfaces_init(usb_alternate_interfaces_t *alternates,
[904dcc6]91 const uint8_t *config_descr, size_t config_descr_size, int interface_number)
[231748a]92{
[904dcc6]93 assert(alternates != NULL);
[231748a]94 assert(config_descr != NULL);
95 assert(config_descr_size > 0);
96
[904dcc6]97 alternates->alternatives = NULL;
98 alternates->alternative_count = 0;
99 alternates->current = 0;
100
[0255d36]101 /* No interfaces. */
[231748a]102 if (interface_number < 0) {
103 return EOK;
104 }
105
[c01987c]106 const size_t alt_count = usb_interface_count_alternates(config_descr,
[b6812a1]107 config_descr_size, interface_number);
[231748a]108
[b6812a1]109 if (alt_count == 0) {
[231748a]110 return ENOENT;
111 }
112
[b6812a1]113 usb_alternate_interface_descriptors_t *alts = calloc(alt_count,
[96ec0a9]114 sizeof(usb_alternate_interface_descriptors_t));
[b6812a1]115 if (alts == NULL) {
[231748a]116 return ENOMEM;
117 }
118
[904dcc6]119 const usb_dp_parser_t dp_parser = {
[231748a]120 .nesting = usb_dp_standard_descriptor_nesting
121 };
[904dcc6]122 const usb_dp_parser_data_t dp_data = {
[231748a]123 .data = config_descr,
124 .size = config_descr_size,
125 .arg = NULL
126 };
127
[0255d36]128 const void *iface_ptr =
129 usb_dp_get_nested_descriptor(&dp_parser, &dp_data, dp_data.data);
130
[b6812a1]131 usb_alternate_interface_descriptors_t *iterator = alts;
132 for (; iface_ptr != NULL && iterator < &alts[alt_count]; ++iterator) {
[0255d36]133 const usb_standard_interface_descriptor_t *iface = iface_ptr;
134
[3bacee1]135 if ((iface->descriptor_type != USB_DESCTYPE_INTERFACE) ||
136 (iface->interface_number != interface_number)) {
[7c3fb9b]137 /*
138 * This is not a valid alternate interface descriptor
139 * for interface with number == interface_number.
140 */
[231748a]141 iface_ptr = usb_dp_get_sibling_descriptor(&dp_parser,
[ab27e01]142 &dp_data, dp_data.data, iface_ptr);
[231748a]143 continue;
144 }
145
[69b9740]146 iterator->interface = iface;
147 iterator->nested_descriptors = iface_ptr + sizeof(*iface);
[231748a]148
149 /* Find next interface to count size of nested descriptors. */
150 iface_ptr = usb_dp_get_sibling_descriptor(&dp_parser, &dp_data,
151 dp_data.data, iface_ptr);
152
[0255d36]153 const uint8_t *next = (iface_ptr == NULL) ?
154 dp_data.data + dp_data.size : iface_ptr;
155
[b208b3b]156 iterator->nested_descriptors_size =
157 next - iterator->nested_descriptors;
[231748a]158 }
159
[b6812a1]160 alternates->alternatives = alts;
161 alternates->alternative_count = alt_count;
162
[231748a]163 return EOK;
164}
165
[6e3c005]166/** Clean initialized structure.
167 * @param instance structure do deinitialize.
168 */
169void usb_alternate_interfaces_deinit(usb_alternate_interfaces_t *instance)
[96ec0a9]170{
[6e3c005]171 if (!instance)
[96ec0a9]172 return;
[6e3c005]173 free(instance->alternatives);
174 instance->alternatives = NULL;
[96ec0a9]175}
[231748a]176/**
177 * @}
178 */
Note: See TracBrowser for help on using the repository browser.