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

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

libusbdev, usbmid: Minor changes.

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