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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since a6a5b25 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
Line 
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
29/** @addtogroup libusbdev
30 * @{
31 */
32/** @file
33 * Handling alternate interface settings.
34 */
35
36#include <usb/dev/alternate_ifaces.h>
37#include <usb/dev/dp.h>
38#include <malloc.h>
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 */
49size_t usb_interface_count_alternates(const uint8_t *config_descr,
50 size_t config_descr_size, uint8_t interface_no)
51{
52 assert(config_descr != NULL);
53 assert(config_descr_size > 0);
54
55 const usb_dp_parser_t dp_parser = {
56 .nesting = usb_dp_standard_descriptor_nesting
57 };
58 const usb_dp_parser_data_t dp_data = {
59 .data = config_descr,
60 .size = config_descr_size,
61 .arg = NULL
62 };
63
64 size_t alternate_count = 0;
65
66 const void *iface_ptr =
67 usb_dp_get_nested_descriptor(&dp_parser, &dp_data, config_descr);
68 while (iface_ptr != NULL) {
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;
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
81/** Initialize alternate interface representation structure.
82 *
83 * @param[in] alternates Pointer to allocated structure.
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 */
89int usb_alternate_interfaces_init(usb_alternate_interfaces_t *alternates,
90 const uint8_t *config_descr, size_t config_descr_size, int interface_number)
91{
92 assert(alternates != NULL);
93 assert(config_descr != NULL);
94 assert(config_descr_size > 0);
95
96 alternates->alternatives = NULL;
97 alternates->alternative_count = 0;
98 alternates->current = 0;
99
100 /* No interfaces. */
101 if (interface_number < 0) {
102 return EOK;
103 }
104
105 const size_t alt_count =usb_interface_count_alternates(config_descr,
106 config_descr_size, interface_number);
107
108 if (alt_count == 0) {
109 return ENOENT;
110 }
111
112 usb_alternate_interface_descriptors_t *alts = calloc(alt_count,
113 sizeof(usb_alternate_interface_descriptors_t));
114 if (alts == NULL) {
115 return ENOMEM;
116 }
117
118 const usb_dp_parser_t dp_parser = {
119 .nesting = usb_dp_standard_descriptor_nesting
120 };
121 const usb_dp_parser_data_t dp_data = {
122 .data = config_descr,
123 .size = config_descr_size,
124 .arg = NULL
125 };
126
127
128 const void *iface_ptr =
129 usb_dp_get_nested_descriptor(&dp_parser, &dp_data, dp_data.data);
130
131 usb_alternate_interface_descriptors_t *iterator = alts;
132 for (; iface_ptr != NULL && iterator < &alts[alt_count]; ++iterator) {
133 const usb_standard_interface_descriptor_t *iface = iface_ptr;
134
135 if ((iface->descriptor_type != USB_DESCTYPE_INTERFACE)
136 || (iface->interface_number != interface_number)) {
137 /* This is not a valid alternate interface descriptor
138 * for interface with number == interface_number. */
139 iface_ptr = usb_dp_get_sibling_descriptor(&dp_parser,
140 &dp_data, dp_data.data, iface_ptr);
141 continue;
142 }
143
144 iterator->interface = iface;
145 iterator->nested_descriptors = iface_ptr + sizeof(*iface);
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
151 const uint8_t *next = (iface_ptr == NULL) ?
152 dp_data.data + dp_data.size : iface_ptr;
153
154 iterator->nested_descriptors_size =
155 next - iterator->nested_descriptors;
156 }
157
158 alternates->alternatives = alts;
159 alternates->alternative_count = alt_count;
160
161 return EOK;
162}
163
164/** Clean initialized structure.
165 * @param instance structure do deinitialize.
166 */
167void usb_alternate_interfaces_deinit(usb_alternate_interfaces_t *instance)
168{
169 if (!instance)
170 return;
171 free(instance->alternatives);
172 instance->alternatives = NULL;
173}
174/**
175 * @}
176 */
Note: See TracBrowser for help on using the repository browser.