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@…>, 13 years ago

usbmid: Spawn ddf functions sooner.

  • Property mode set to 100644
File size: 6.5 KB
RevLine 
[3ae93a8]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>
[7d521e24]40#include <usb/dev/request.h>
41#include <usb/dev/dp.h>
[fcafa04]42#include <usb/ddfiface.h>
[3ae93a8]43#include "usbmid.h"
44
[fcafa04]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
[ecb107b]50/** Tell whether given interface is already in the list.
[3ae93a8]51 *
[ecb107b]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.
[3ae93a8]55 */
[095bddfc]56static bool interface_in_list(const list_t *list, int interface_no)
[3ae93a8]57{
[b72efe8]58 list_foreach(*list, l) {
[34289ea]59 const usbmid_interface_t *iface = usbmid_interface_from_link(l);
[ecb107b]60 if (iface->interface_no == interface_no) {
61 return true;
62 }
[3ae93a8]63 }
64
[ecb107b]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 */
[6df4adc4]74static int create_interfaces(const uint8_t *config_descriptor,
75 size_t config_descriptor_size, list_t *list, usb_device_t *usb_dev)
[ecb107b]76{
[6df4adc4]77 assert(config_descriptor);
78 assert(usb_dev);
79
[7c95d6f5]80 const usb_dp_parser_data_t data = {
[3ae93a8]81 .data = config_descriptor,
82 .size = config_descriptor_size,
83 .arg = NULL
84 };
85
[9f583151]86 static const usb_dp_parser_t parser = {
[3ae93a8]87 .nesting = usb_dp_standard_descriptor_nesting
88 };
89
[8a121b1]90 const uint8_t *interface_ptr =
[9f583151]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
[ecb107b]104 = (usb_standard_interface_descriptor_t *) interface_ptr;
105
106 /* Skip alternate interfaces. */
[9f583151]107 if (interface_in_list(list, interface->interface_number)) {
108 /* TODO: add the alternatives and create match ids
109 * for them. */
110 continue;
111 }
[6df4adc4]112
[9f583151]113 usbmid_interface_t *iface = malloc(sizeof(usbmid_interface_t));
114 if (iface == NULL) {
115 //TODO: Do something about that failure.
116 break;
[ecb107b]117 }
118
[9f583151]119 link_initialize(&iface->link);
120 iface->fun = NULL;
121 iface->interface_no = interface->interface_number;
122 iface->interface = interface;
[ecb107b]123
[9f583151]124 list_append(&iface->link, list);
[6df4adc4]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 }
[9f583151]138 }
[6df4adc4]139 return EOK;
[3ae93a8]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 */
[34289ea]150int usbmid_explore_device(usb_device_t *dev)
[3ae93a8]151{
[34289ea]152 const unsigned dev_class = dev->descriptors.device.device_class;
[fcafa04]153 if (dev_class != USB_CLASS_USE_INTERFACE) {
[3ae93a8]154 usb_log_warning(
[8be7819]155 "Device class: %u (%s), but expected class %u.\n",
156 dev_class, usb_str_class(dev_class),
157 USB_CLASS_USE_INTERFACE);
[34289ea]158 usb_log_error("Not a multi-interface device, refusing.\n");
159 return ENOTSUP;
[3ae93a8]160 }
161
[8be7819]162 /* Shortcuts to save on typing ;-). */
[7c95d6f5]163 const void *config_descriptor_raw = dev->descriptors.configuration;
[fcafa04]164 size_t config_descriptor_size = dev->descriptors.configuration_size;
[7c95d6f5]165 const usb_standard_configuration_descriptor_t *config_descriptor =
166 config_descriptor_raw;
[3ae93a8]167
[8f74140c]168 /* Select the first configuration */
[34289ea]169 int rc = usb_request_set_configuration(&dev->ctrl_pipe,
[8f74140c]170 config_descriptor->configuration_number);
171 if (rc != EOK) {
172 usb_log_error("Failed to set device configuration: %s.\n",
173 str_error(rc));
[34289ea]174 return rc;
[8f74140c]175 }
176
[34289ea]177
178
[8be7819]179 /* Create driver soft-state. */
[065064e6]180 usb_mid_t *usb_mid = usb_device_data_alloc(dev, sizeof(usb_mid_t));
[229629d]181 if (!usb_mid) {
182 usb_log_error("Failed to create USB MID structure.\n");
[34289ea]183 return ENOMEM;
[229629d]184 }
185
[8be7819]186 /* Create control function. */
[229629d]187 usb_mid->ctl_fun = ddf_fun_create(dev->ddf_dev, fun_exposed, "ctl");
188 if (usb_mid->ctl_fun == NULL) {
[d3880aa]189 usb_log_error("Failed to create control function.\n");
[34289ea]190 return ENOMEM;
[d3880aa]191 }
[56fd7cf]192 ddf_fun_set_ops(usb_mid->ctl_fun, &mid_device_ops);
[fcafa04]193
[8be7819]194 /* Bind control function. */
[229629d]195 rc = ddf_fun_bind(usb_mid->ctl_fun);
[d3880aa]196 if (rc != EOK) {
197 usb_log_error("Failed to bind control function: %s.\n",
198 str_error(rc));
[229629d]199 ddf_fun_destroy(usb_mid->ctl_fun);
[34289ea]200 return rc;
[d3880aa]201 }
202
[229629d]203
[ecb107b]204 /* Create interface children. */
[229629d]205 list_initialize(&usb_mid->interface_list);
[ecb107b]206 create_interfaces(config_descriptor_raw, config_descriptor_size,
[6df4adc4]207 &usb_mid->interface_list, dev);
[3ae93a8]208
[34289ea]209 return EOK;
[3ae93a8]210}
211
212/**
213 * @}
214 */
Note: See TracBrowser for help on using the repository browser.