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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 7f620e8 was feeac0d, checked in by Jiri Svoboda <jiri@…>, 12 years ago

Simplify use of list_foreach.

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