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

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

Make ddf_dev_t and ddf_fun_t opaque. This further tighthens the DDF interface.

  • 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) {
[9f583151]59 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 */
[7c95d6f5]74static void create_interfaces(const uint8_t *config_descriptor,
[b72efe8]75 size_t config_descriptor_size, list_t *list)
[ecb107b]76{
[7c95d6f5]77 const usb_dp_parser_data_t data = {
[3ae93a8]78 .data = config_descriptor,
79 .size = config_descriptor_size,
80 .arg = NULL
81 };
82
[9f583151]83 static const usb_dp_parser_t parser = {
[3ae93a8]84 .nesting = usb_dp_standard_descriptor_nesting
85 };
86
[8a121b1]87 const uint8_t *interface_ptr =
[9f583151]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
[ecb107b]101 = (usb_standard_interface_descriptor_t *) interface_ptr;
102
103 /* Skip alternate interfaces. */
[9f583151]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;
[ecb107b]113 }
114
[9f583151]115 link_initialize(&iface->link);
116 iface->fun = NULL;
117 iface->interface_no = interface->interface_number;
118 iface->interface = interface;
[ecb107b]119
[9f583151]120 list_append(&iface->link, list);
121 }
[3ae93a8]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 */
[fcafa04]132bool usbmid_explore_device(usb_device_t *dev)
[3ae93a8]133{
[fcafa04]134 int rc;
[3ae93a8]135
[8be7819]136 unsigned dev_class = dev->descriptors.device.device_class;
[fcafa04]137 if (dev_class != USB_CLASS_USE_INTERFACE) {
[3ae93a8]138 usb_log_warning(
[8be7819]139 "Device class: %u (%s), but expected class %u.\n",
140 dev_class, usb_str_class(dev_class),
141 USB_CLASS_USE_INTERFACE);
[3ae93a8]142 usb_log_error("Not multi interface device, refusing.\n");
143 return false;
144 }
145
[8be7819]146 /* Shortcuts to save on typing ;-). */
[7c95d6f5]147 const void *config_descriptor_raw = dev->descriptors.configuration;
[fcafa04]148 size_t config_descriptor_size = dev->descriptors.configuration_size;
[7c95d6f5]149 const usb_standard_configuration_descriptor_t *config_descriptor =
150 config_descriptor_raw;
[3ae93a8]151
[8f74140c]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
[8be7819]161 /* Create driver soft-state. */
[065064e6]162 usb_mid_t *usb_mid = usb_device_data_alloc(dev, sizeof(usb_mid_t));
[229629d]163 if (!usb_mid) {
164 usb_log_error("Failed to create USB MID structure.\n");
165 return false;
166 }
167
[8be7819]168 /* Create control function. */
[229629d]169 usb_mid->ctl_fun = ddf_fun_create(dev->ddf_dev, fun_exposed, "ctl");
170 if (usb_mid->ctl_fun == NULL) {
[d3880aa]171 usb_log_error("Failed to create control function.\n");
172 return false;
173 }
[56fd7cf]174 ddf_fun_set_ops(usb_mid->ctl_fun, &mid_device_ops);
[fcafa04]175
[8be7819]176 /* Bind control function. */
[229629d]177 rc = ddf_fun_bind(usb_mid->ctl_fun);
[d3880aa]178 if (rc != EOK) {
179 usb_log_error("Failed to bind control function: %s.\n",
180 str_error(rc));
[229629d]181 ddf_fun_destroy(usb_mid->ctl_fun);
[d3880aa]182 return false;
183 }
184
[229629d]185
[ecb107b]186 /* Create interface children. */
[229629d]187 list_initialize(&usb_mid->interface_list);
[ecb107b]188 create_interfaces(config_descriptor_raw, config_descriptor_size,
[229629d]189 &usb_mid->interface_list);
[ecb107b]190
[8be7819]191 /* Start child function for every interface. */
[229629d]192 list_foreach(usb_mid->interface_list, link) {
[8be7819]193 usbmid_interface_t *iface = usbmid_interface_from_link(link);
[ecb107b]194
[3ae93a8]195 usb_log_info("Creating child for interface %d (%s).\n",
[8be7819]196 iface->interface_no,
[ecb107b]197 usb_str_class(iface->interface->interface_class));
198
199 rc = usbmid_spawn_interface_child(dev, iface,
200 &dev->descriptors.device, iface->interface);
[3ae93a8]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.