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

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

usbmid: Spawn ddf functions sooner.

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