2
* Copyright (c) 2011 Vojtech Horky
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions
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.
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.
29
/** @addtogroup drvusbmid
34
* Exploration of available interfaces in the USB device.
37
#include <str_error.h>
39
#include <usb/classes/classes.h>
40
#include <usb/dev/request.h>
41
#include <usb/dev/dp.h>
42
#include <usb/ddfiface.h>
45
/** Operations of the device itself. */
46
static ddf_dev_ops_t mid_device_ops = {
47
.interfaces[USB_DEV_IFACE] = &usb_iface_hub_impl
50
/** Tell whether given interface is already in the list.
52
* @param list List of usbmid_interface_t members to be searched.
53
* @param interface_no Interface number caller is looking for.
54
* @return Interface @p interface_no is already present in the list.
56
static bool interface_in_list(link_t *list, int interface_no)
59
for (l = list->next; l != list; l = l->next) {
60
usbmid_interface_t *iface
61
= list_get_instance(l, usbmid_interface_t, link);
62
if (iface->interface_no == interface_no) {
70
/** Create list of interfaces from configuration descriptor.
72
* @param config_descriptor Configuration descriptor.
73
* @param config_descriptor_size Size of configuration descriptor in bytes.
74
* @param list List where to add the interfaces.
76
static void create_interfaces(uint8_t *config_descriptor,
77
size_t config_descriptor_size, link_t *list)
79
usb_dp_parser_data_t data = {
80
.data = config_descriptor,
81
.size = config_descriptor_size,
85
usb_dp_parser_t parser = {
86
.nesting = usb_dp_standard_descriptor_nesting
89
uint8_t *interface_ptr = usb_dp_get_nested_descriptor(&parser, &data,
91
if (interface_ptr == NULL) {
96
if (interface_ptr[1] != USB_DESCTYPE_INTERFACE) {
100
usb_standard_interface_descriptor_t *interface
101
= (usb_standard_interface_descriptor_t *) interface_ptr;
103
/* Skip alternate interfaces. */
104
if (!interface_in_list(list, interface->interface_number)) {
105
usbmid_interface_t *iface
106
= malloc(sizeof(usbmid_interface_t));
110
link_initialize(&iface->link);
112
iface->interface_no = interface->interface_number;
113
iface->interface = interface;
115
list_append(&iface->link, list);
118
/* TODO: add the alternatives and create match ids from them
123
interface_ptr = usb_dp_get_sibling_descriptor(&parser, &data,
124
data.data, interface_ptr);
126
} while (interface_ptr != NULL);
130
/** Explore MID device.
132
* We expect that @p dev is initialized and session on control pipe is
135
* @param dev Device to be explored.
136
* @return Whether to accept this device from devman.
138
bool usbmid_explore_device(usb_device_t *dev)
142
int dev_class = dev->descriptors.device.device_class;
143
if (dev_class != USB_CLASS_USE_INTERFACE) {
145
"Device class: %d (%s), but expected class 0.\n",
146
dev_class, usb_str_class(dev_class));
147
usb_log_error("Not multi interface device, refusing.\n");
151
/* Short cuts to save on typing ;-). */
152
uint8_t *config_descriptor_raw = dev->descriptors.configuration;
153
size_t config_descriptor_size = dev->descriptors.configuration_size;
154
usb_standard_configuration_descriptor_t *config_descriptor =
155
(usb_standard_configuration_descriptor_t *) config_descriptor_raw;
157
/* Select the first configuration */
158
rc = usb_request_set_configuration(&dev->ctrl_pipe,
159
config_descriptor->configuration_number);
161
usb_log_error("Failed to set device configuration: %s.\n",
166
/* Create control function */
167
ddf_fun_t *ctl_fun = ddf_fun_create(dev->ddf_dev, fun_exposed, "ctl");
168
if (ctl_fun == NULL) {
169
usb_log_error("Failed to create control function.\n");
173
ctl_fun->ops = &mid_device_ops;
175
rc = ddf_fun_bind(ctl_fun);
177
usb_log_error("Failed to bind control function: %s.\n",
182
/* Create interface children. */
183
link_t interface_list;
184
list_initialize(&interface_list);
185
create_interfaces(config_descriptor_raw, config_descriptor_size,
189
for (link = interface_list.next; link != &interface_list;
191
usbmid_interface_t *iface = list_get_instance(link,
192
usbmid_interface_t, link);
194
usb_log_info("Creating child for interface %d (%s).\n",
195
(int) iface->interface_no,
196
usb_str_class(iface->interface->interface_class));
198
rc = usbmid_spawn_interface_child(dev, iface,
199
&dev->descriptors.device, iface->interface);
201
usb_log_error("Failed to create interface child: %s.\n",