| 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 | /* Create control function */
|
|---|
| 166 | ddf_fun_t *ctl_fun = ddf_fun_create(dev->ddf_dev, fun_exposed, "ctl");
|
|---|
| 167 | if (ctl_fun == NULL) {
|
|---|
| 168 | usb_log_error("Failed to create control function.\n");
|
|---|
| 169 | return false;
|
|---|
| 170 | }
|
|---|
| 171 |
|
|---|
| 172 | ctl_fun->ops = &mid_device_ops;
|
|---|
| 173 |
|
|---|
| 174 | rc = ddf_fun_bind(ctl_fun);
|
|---|
| 175 | if (rc != EOK) {
|
|---|
| 176 | usb_log_error("Failed to bind control function: %s.\n",
|
|---|
| 177 | str_error(rc));
|
|---|
| 178 | return false;
|
|---|
| 179 | }
|
|---|
| 180 |
|
|---|
| 181 | /* Create interface children. */
|
|---|
| 182 | list_t interface_list;
|
|---|
| 183 | list_initialize(&interface_list);
|
|---|
| 184 | create_interfaces(config_descriptor_raw, config_descriptor_size,
|
|---|
| 185 | &interface_list);
|
|---|
| 186 |
|
|---|
| 187 | list_foreach(interface_list, link) {
|
|---|
| 188 | usbmid_interface_t *iface = list_get_instance(link,
|
|---|
| 189 | usbmid_interface_t, link);
|
|---|
| 190 |
|
|---|
| 191 | usb_log_info("Creating child for interface %d (%s).\n",
|
|---|
| 192 | (int) iface->interface_no,
|
|---|
| 193 | usb_str_class(iface->interface->interface_class));
|
|---|
| 194 |
|
|---|
| 195 | rc = usbmid_spawn_interface_child(dev, iface,
|
|---|
| 196 | &dev->descriptors.device, iface->interface);
|
|---|
| 197 | if (rc != EOK) {
|
|---|
| 198 | usb_log_error("Failed to create interface child: %s.\n",
|
|---|
| 199 | str_error(rc));
|
|---|
| 200 | }
|
|---|
| 201 | }
|
|---|
| 202 |
|
|---|
| 203 | return true;
|
|---|
| 204 | }
|
|---|
| 205 |
|
|---|
| 206 | /**
|
|---|
| 207 | * @}
|
|---|
| 208 | */
|
|---|