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

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

Merge mainline changes.

Major conflicts in USB HC drivers.
Compiles and UHCI works (qemu).
OHCI has device remove problems.

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