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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 8582076 was b208b3b, checked in by Jan Vesely <jano.vesely@…>, 12 years ago

libusbdev: Move alternate interface handling to its own header file.

Fix initialization loop.

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