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. */
|
---|
46 | static 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 | */
|
---|
56 | static bool interface_in_list(list_t *list, int interface_no)
|
---|
57 | {
|
---|
58 | list_foreach(*list, l) {
|
---|
59 | usbmid_interface_t *iface
|
---|
60 | = list_get_instance(l, usbmid_interface_t, link);
|
---|
61 | if (iface->interface_no == interface_no) {
|
---|
62 | return true;
|
---|
63 | }
|
---|
64 | }
|
---|
65 |
|
---|
66 | return false;
|
---|
67 | }
|
---|
68 |
|
---|
69 | /** Create list of interfaces from configuration descriptor.
|
---|
70 | *
|
---|
71 | * @param config_descriptor Configuration descriptor.
|
---|
72 | * @param config_descriptor_size Size of configuration descriptor in bytes.
|
---|
73 | * @param list List where to add the interfaces.
|
---|
74 | */
|
---|
75 | static void create_interfaces(uint8_t *config_descriptor,
|
---|
76 | size_t config_descriptor_size, list_t *list)
|
---|
77 | {
|
---|
78 | usb_dp_parser_data_t data = {
|
---|
79 | .data = config_descriptor,
|
---|
80 | .size = config_descriptor_size,
|
---|
81 | .arg = NULL
|
---|
82 | };
|
---|
83 |
|
---|
84 | usb_dp_parser_t parser = {
|
---|
85 | .nesting = usb_dp_standard_descriptor_nesting
|
---|
86 | };
|
---|
87 |
|
---|
88 | uint8_t *interface_ptr = usb_dp_get_nested_descriptor(&parser, &data,
|
---|
89 | data.data);
|
---|
90 | if (interface_ptr == NULL) {
|
---|
91 | return;
|
---|
92 | }
|
---|
93 |
|
---|
94 | do {
|
---|
95 | if (interface_ptr[1] != USB_DESCTYPE_INTERFACE) {
|
---|
96 | goto next_descriptor;
|
---|
97 | }
|
---|
98 |
|
---|
99 | 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 | usbmid_interface_t *iface
|
---|
105 | = malloc(sizeof(usbmid_interface_t));
|
---|
106 | if (iface == NULL) {
|
---|
107 | break;
|
---|
108 | }
|
---|
109 | link_initialize(&iface->link);
|
---|
110 | iface->fun = NULL;
|
---|
111 | iface->interface_no = interface->interface_number;
|
---|
112 | iface->interface = interface;
|
---|
113 |
|
---|
114 | list_append(&iface->link, list);
|
---|
115 | }
|
---|
116 |
|
---|
117 | /* TODO: add the alternatives and create match ids from them
|
---|
118 | * as well.
|
---|
119 | */
|
---|
120 |
|
---|
121 | next_descriptor:
|
---|
122 | interface_ptr = usb_dp_get_sibling_descriptor(&parser, &data,
|
---|
123 | data.data, interface_ptr);
|
---|
124 |
|
---|
125 | } while (interface_ptr != NULL);
|
---|
126 |
|
---|
127 | }
|
---|
128 |
|
---|
129 | /** Explore MID device.
|
---|
130 | *
|
---|
131 | * We expect that @p dev is initialized and session on control pipe is
|
---|
132 | * started.
|
---|
133 | *
|
---|
134 | * @param dev Device to be explored.
|
---|
135 | * @return Whether to accept this device from devman.
|
---|
136 | */
|
---|
137 | bool usbmid_explore_device(usb_device_t *dev)
|
---|
138 | {
|
---|
139 | int rc;
|
---|
140 |
|
---|
141 | int dev_class = dev->descriptors.device.device_class;
|
---|
142 | if (dev_class != USB_CLASS_USE_INTERFACE) {
|
---|
143 | usb_log_warning(
|
---|
144 | "Device class: %d (%s), but expected class 0.\n",
|
---|
145 | dev_class, usb_str_class(dev_class));
|
---|
146 | usb_log_error("Not multi interface device, refusing.\n");
|
---|
147 | return false;
|
---|
148 | }
|
---|
149 |
|
---|
150 | /* Short cuts to save on typing ;-). */
|
---|
151 | uint8_t *config_descriptor_raw = dev->descriptors.configuration;
|
---|
152 | size_t config_descriptor_size = dev->descriptors.configuration_size;
|
---|
153 | usb_standard_configuration_descriptor_t *config_descriptor =
|
---|
154 | (usb_standard_configuration_descriptor_t *) config_descriptor_raw;
|
---|
155 |
|
---|
156 | /* Select the first configuration */
|
---|
157 | rc = usb_request_set_configuration(&dev->ctrl_pipe,
|
---|
158 | config_descriptor->configuration_number);
|
---|
159 | if (rc != EOK) {
|
---|
160 | usb_log_error("Failed to set device configuration: %s.\n",
|
---|
161 | str_error(rc));
|
---|
162 | return false;
|
---|
163 | }
|
---|
164 |
|
---|
165 | usb_mid_t *usb_mid = malloc(sizeof(usb_mid_t));
|
---|
166 | if (!usb_mid) {
|
---|
167 | usb_log_error("Failed to create USB MID structure.\n");
|
---|
168 | return false;
|
---|
169 | }
|
---|
170 |
|
---|
171 | /* Create control function */
|
---|
172 | usb_mid->ctl_fun = ddf_fun_create(dev->ddf_dev, fun_exposed, "ctl");
|
---|
173 | if (usb_mid->ctl_fun == NULL) {
|
---|
174 | usb_log_error("Failed to create control function.\n");
|
---|
175 | free(usb_mid);
|
---|
176 | return false;
|
---|
177 | }
|
---|
178 |
|
---|
179 | usb_mid->ctl_fun->ops = &mid_device_ops;
|
---|
180 |
|
---|
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 | free(usb_mid);
|
---|
187 | return false;
|
---|
188 | }
|
---|
189 |
|
---|
190 |
|
---|
191 | /* Create interface children. */
|
---|
192 | list_initialize(&usb_mid->interface_list);
|
---|
193 | create_interfaces(config_descriptor_raw, config_descriptor_size,
|
---|
194 | &usb_mid->interface_list);
|
---|
195 |
|
---|
196 | list_foreach(usb_mid->interface_list, link) {
|
---|
197 | usbmid_interface_t *iface = list_get_instance(link,
|
---|
198 | usbmid_interface_t, link);
|
---|
199 |
|
---|
200 | usb_log_info("Creating child for interface %d (%s).\n",
|
---|
201 | (int) iface->interface_no,
|
---|
202 | usb_str_class(iface->interface->interface_class));
|
---|
203 |
|
---|
204 | rc = usbmid_spawn_interface_child(dev, iface,
|
---|
205 | &dev->descriptors.device, iface->interface);
|
---|
206 | if (rc != EOK) {
|
---|
207 | usb_log_error("Failed to create interface child: %s.\n",
|
---|
208 | str_error(rc));
|
---|
209 | }
|
---|
210 | }
|
---|
211 | dev->driver_data = usb_mid;
|
---|
212 |
|
---|
213 | return true;
|
---|
214 | }
|
---|
215 |
|
---|
216 | /**
|
---|
217 | * @}
|
---|
218 | */
|
---|