source: mainline/uspace/drv/bus/usb/usbmid/explore.c@ f1380b7

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since f1380b7 was a35b458, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 8 years ago

style: Remove trailing whitespace on _all_ lines, including empty ones, for particular file types.

Command used: tools/srepl '\s\+$' '' -- *.c *.h *.py *.sh *.s *.S *.ag

Currently, whitespace on empty lines is very inconsistent.
There are two basic choices: Either remove the whitespace, or keep empty lines
indented to the level of surrounding code. The former is AFAICT more common,
and also much easier to do automatically.

Alternatively, we could write script for automatic indentation, and use that
instead. However, if such a script exists, it's possible to use the indented
style locally, by having the editor apply relevant conversions on load/save,
without affecting remote repository. IMO, it makes more sense to adopt
the simpler rule.

  • Property mode set to 100644
File size: 6.2 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 drvusbmid
30 * @{
31 */
32/**
33 * @file
34 * Exploration of available interfaces in the USB device.
35 */
36
37#include <errno.h>
38#include <str_error.h>
39#include <stdlib.h>
40#include <usb/classes/classes.h>
41#include <usb/dev/request.h>
42#include <usb/dev/dp.h>
43#include "usbmid.h"
44
45/** Tell whether given interface is already in the list.
46 *
47 * @param list List of usbmid_interface_t members to be searched.
48 * @param interface_no Interface number caller is looking for.
49 * @return Interface @p interface_no is already present in the list.
50 */
51static bool interface_in_list(const list_t *list, int interface_no)
52{
53 list_foreach(*list, link, const usbmid_interface_t, iface) {
54 if (iface->interface_no == interface_no) {
55 return true;
56 }
57 }
58
59 return false;
60}
61
62/** Create list of interfaces from configuration descriptor.
63 *
64 * @param config_descriptor Configuration descriptor.
65 * @param config_descriptor_size Size of configuration descriptor in bytes.
66 * @param list List where to add the interfaces.
67 */
68static errno_t create_interfaces(const uint8_t *config_descriptor,
69 size_t config_descriptor_size, list_t *list, usb_device_t *usb_dev)
70{
71 assert(config_descriptor);
72 assert(usb_dev);
73
74 const usb_dp_parser_data_t data = {
75 .data = config_descriptor,
76 .size = config_descriptor_size,
77 .arg = NULL
78 };
79
80 static const usb_dp_parser_t parser = {
81 .nesting = usb_dp_standard_descriptor_nesting
82 };
83
84 const uint8_t *interface_ptr =
85 usb_dp_get_nested_descriptor(&parser, &data, config_descriptor);
86
87 /* Walk all descriptors nested in the current configuration decriptor;
88 * i.e. all interface descriptors. */
89 for (; interface_ptr != NULL;
90 interface_ptr = usb_dp_get_sibling_descriptor(
91 &parser, &data, config_descriptor, interface_ptr))
92 {
93 /* The second byte is DESCTYPE byte in all desriptors. */
94 if (interface_ptr[1] != USB_DESCTYPE_INTERFACE)
95 continue;
96
97 const usb_standard_interface_descriptor_t *interface
98 = (usb_standard_interface_descriptor_t *) interface_ptr;
99
100 /* Skip alternate interfaces. */
101 if (interface_in_list(list, interface->interface_number)) {
102 /* TODO: add the alternatives and create match ids
103 * for them. */
104 continue;
105 }
106
107
108 usb_log_info("Creating child for interface %d (%s).",
109 interface->interface_number,
110 usb_str_class(interface->interface_class));
111
112 usbmid_interface_t *iface = NULL;
113 const errno_t rc = usbmid_spawn_interface_child(usb_dev, &iface,
114 &usb_device_descriptors(usb_dev)->device, interface);
115 if (rc != EOK) {
116 //TODO: Do something about that failure.
117 usb_log_error("Failed to create interface child for "
118 "%d (%s): %s.\n", interface->interface_number,
119 usb_str_class(interface->interface_class),
120 str_error(rc));
121 } else {
122 list_append(&iface->link, list);
123 }
124 }
125 return EOK;
126}
127
128/** Explore MID device.
129 *
130 * We expect that @p dev is initialized and session on control pipe is
131 * started.
132 *
133 * @param dev Device to be explored.
134 * @return Whether to accept this device.
135 */
136errno_t usbmid_explore_device(usb_device_t *dev)
137{
138 assert(dev);
139 const unsigned dev_class =
140 usb_device_descriptors(dev)->device.device_class;
141 if (dev_class != USB_CLASS_USE_INTERFACE) {
142 usb_log_warning(
143 "Device class: %u (%s), but expected class %u.\n",
144 dev_class, usb_str_class(dev_class),
145 USB_CLASS_USE_INTERFACE);
146 usb_log_error("Not a multi-interface device, refusing.");
147 return ENOTSUP;
148 }
149
150 /* Get coonfiguration descriptor. */
151 const size_t config_descriptor_size =
152 usb_device_descriptors(dev)->full_config_size;
153 const void *config_descriptor_raw =
154 usb_device_descriptors(dev)->full_config;
155 const usb_standard_configuration_descriptor_t *config_descriptor =
156 config_descriptor_raw;
157
158 /* Select the first configuration */
159 errno_t rc = usb_request_set_configuration(usb_device_get_default_pipe(dev),
160 config_descriptor->configuration_number);
161 if (rc != EOK) {
162 usb_log_error("Failed to set device configuration: %s.",
163 str_error(rc));
164 return rc;
165 }
166
167 /* Create driver soft-state. */
168 usb_mid_t *usb_mid = usb_device_data_alloc(dev, sizeof(usb_mid_t));
169 if (!usb_mid) {
170 usb_log_error("Failed to create USB MID structure.");
171 return ENOMEM;
172 }
173
174 /* Create control function. */
175 usb_mid->ctl_fun = usb_device_ddf_fun_create(dev, fun_exposed, "ctl");
176 if (usb_mid->ctl_fun == NULL) {
177 usb_log_error("Failed to create control function.");
178 return ENOMEM;
179 }
180
181 /* Bind control function. */
182 rc = ddf_fun_bind(usb_mid->ctl_fun);
183 if (rc != EOK) {
184 usb_log_error("Failed to bind control function: %s.",
185 str_error(rc));
186 ddf_fun_destroy(usb_mid->ctl_fun);
187 return rc;
188 }
189
190 /* Create interface children. */
191 list_initialize(&usb_mid->interface_list);
192 create_interfaces(config_descriptor_raw, config_descriptor_size,
193 &usb_mid->interface_list, dev);
194
195 return EOK;
196}
197
198/**
199 * @}
200 */
Note: See TracBrowser for help on using the repository browser.