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