[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 | */
|
---|
[58563585] | 36 |
|
---|
[3ae93a8] | 37 | #include <errno.h>
|
---|
| 38 | #include <str_error.h>
|
---|
| 39 | #include <stdlib.h>
|
---|
| 40 | #include <usb/classes/classes.h>
|
---|
[7d521e24] | 41 | #include <usb/dev/request.h>
|
---|
| 42 | #include <usb/dev/dp.h>
|
---|
[3ae93a8] | 43 | #include "usbmid.h"
|
---|
| 44 |
|
---|
[ecb107b] | 45 | /** Tell whether given interface is already in the list.
|
---|
[3ae93a8] | 46 | *
|
---|
[ecb107b] | 47 | * @param list List of usbmid_interface_t members to be searched.
|
---|
| 48 | * @param interface_no Interface number caller is looking for.
|
---|
| 49 | * @return Interface @p interface_no is already present in the list.
|
---|
[3ae93a8] | 50 | */
|
---|
[095bddfc] | 51 | static bool interface_in_list(const list_t *list, int interface_no)
|
---|
[3ae93a8] | 52 | {
|
---|
[3f03199] | 53 | list_foreach(*list, link, const usbmid_interface_t, iface) {
|
---|
[ecb107b] | 54 | if (iface->interface_no == interface_no) {
|
---|
| 55 | return true;
|
---|
| 56 | }
|
---|
[3ae93a8] | 57 | }
|
---|
| 58 |
|
---|
[ecb107b] | 59 | return false;
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | /** Create list of interfaces from configuration descriptor.
|
---|
| 63 | *
|
---|
| 64 | * @param config_descriptor Configuration descriptor.
|
---|
| 65 | * @param config_descriptor_size Size of configuration descriptor in bytes.
|
---|
| 66 | * @param list List where to add the interfaces.
|
---|
| 67 | */
|
---|
[5a6cc679] | 68 | static errno_t create_interfaces(const uint8_t *config_descriptor,
|
---|
[6df4adc4] | 69 | size_t config_descriptor_size, list_t *list, usb_device_t *usb_dev)
|
---|
[ecb107b] | 70 | {
|
---|
[6df4adc4] | 71 | assert(config_descriptor);
|
---|
| 72 | assert(usb_dev);
|
---|
| 73 |
|
---|
[7c95d6f5] | 74 | const usb_dp_parser_data_t data = {
|
---|
[3ae93a8] | 75 | .data = config_descriptor,
|
---|
| 76 | .size = config_descriptor_size,
|
---|
| 77 | .arg = NULL
|
---|
| 78 | };
|
---|
| 79 |
|
---|
[9f583151] | 80 | static const usb_dp_parser_t parser = {
|
---|
[3ae93a8] | 81 | .nesting = usb_dp_standard_descriptor_nesting
|
---|
| 82 | };
|
---|
| 83 |
|
---|
[8a121b1] | 84 | const uint8_t *interface_ptr =
|
---|
[9f583151] | 85 | usb_dp_get_nested_descriptor(&parser, &data, config_descriptor);
|
---|
| 86 |
|
---|
[7c3fb9b] | 87 | /*
|
---|
| 88 | * Walk all descriptors nested in the current configuration decriptor;
|
---|
| 89 | * i.e. all interface descriptors.
|
---|
| 90 | */
|
---|
[4ca778b] | 91 | for (; interface_ptr != NULL;
|
---|
[9f583151] | 92 | interface_ptr = usb_dp_get_sibling_descriptor(
|
---|
[3bacee1] | 93 | &parser, &data, config_descriptor, interface_ptr)) {
|
---|
[9f583151] | 94 | /* The second byte is DESCTYPE byte in all desriptors. */
|
---|
| 95 | if (interface_ptr[1] != USB_DESCTYPE_INTERFACE)
|
---|
| 96 | continue;
|
---|
| 97 |
|
---|
[3bacee1] | 98 | const usb_standard_interface_descriptor_t *interface =
|
---|
| 99 | (usb_standard_interface_descriptor_t *) interface_ptr;
|
---|
[ecb107b] | 100 |
|
---|
| 101 | /* Skip alternate interfaces. */
|
---|
[9f583151] | 102 | if (interface_in_list(list, interface->interface_number)) {
|
---|
[7c3fb9b] | 103 | /*
|
---|
| 104 | * TODO: add the alternatives and create match ids
|
---|
| 105 | * for them.
|
---|
| 106 | */
|
---|
[9f583151] | 107 | continue;
|
---|
| 108 | }
|
---|
[6df4adc4] | 109 |
|
---|
[a1732929] | 110 | usb_log_info("Creating child for interface %d (%s).",
|
---|
[6df4adc4] | 111 | interface->interface_number,
|
---|
| 112 | usb_str_class(interface->interface_class));
|
---|
| 113 |
|
---|
[9e2132a] | 114 | usbmid_interface_t *iface = NULL;
|
---|
[5a6cc679] | 115 | const errno_t rc = usbmid_spawn_interface_child(usb_dev, &iface,
|
---|
[3bacee1] | 116 | &usb_device_descriptors(usb_dev)->device, interface);
|
---|
[6df4adc4] | 117 | if (rc != EOK) {
|
---|
[9e2132a] | 118 | //TODO: Do something about that failure.
|
---|
[6df4adc4] | 119 | usb_log_error("Failed to create interface child for "
|
---|
| 120 | "%d (%s): %s.\n", interface->interface_number,
|
---|
| 121 | usb_str_class(interface->interface_class),
|
---|
| 122 | str_error(rc));
|
---|
[9e2132a] | 123 | } else {
|
---|
| 124 | list_append(&iface->link, list);
|
---|
[6df4adc4] | 125 | }
|
---|
[9f583151] | 126 | }
|
---|
[6df4adc4] | 127 | return EOK;
|
---|
[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.
|
---|
[d15797d] | 136 | * @return Whether to accept this device.
|
---|
[3ae93a8] | 137 | */
|
---|
[5a6cc679] | 138 | errno_t usbmid_explore_device(usb_device_t *dev)
|
---|
[3ae93a8] | 139 | {
|
---|
[e2dfa86] | 140 | assert(dev);
|
---|
[945d66c] | 141 | const unsigned dev_class =
|
---|
[e2dfa86] | 142 | usb_device_descriptors(dev)->device.device_class;
|
---|
[fcafa04] | 143 | if (dev_class != USB_CLASS_USE_INTERFACE) {
|
---|
[3ae93a8] | 144 | usb_log_warning(
|
---|
[8be7819] | 145 | "Device class: %u (%s), but expected class %u.\n",
|
---|
| 146 | dev_class, usb_str_class(dev_class),
|
---|
| 147 | USB_CLASS_USE_INTERFACE);
|
---|
[a1732929] | 148 | usb_log_error("Not a multi-interface device, refusing.");
|
---|
[34289ea] | 149 | return ENOTSUP;
|
---|
[3ae93a8] | 150 | }
|
---|
| 151 |
|
---|
[945d66c] | 152 | /* Get coonfiguration descriptor. */
|
---|
[e2dfa86] | 153 | const size_t config_descriptor_size =
|
---|
| 154 | usb_device_descriptors(dev)->full_config_size;
|
---|
[945d66c] | 155 | const void *config_descriptor_raw =
|
---|
[e2dfa86] | 156 | usb_device_descriptors(dev)->full_config;
|
---|
[7c95d6f5] | 157 | const usb_standard_configuration_descriptor_t *config_descriptor =
|
---|
| 158 | config_descriptor_raw;
|
---|
[3ae93a8] | 159 |
|
---|
[8f74140c] | 160 | /* Select the first configuration */
|
---|
[5a6cc679] | 161 | errno_t rc = usb_request_set_configuration(usb_device_get_default_pipe(dev),
|
---|
[8f74140c] | 162 | config_descriptor->configuration_number);
|
---|
| 163 | if (rc != EOK) {
|
---|
[a1732929] | 164 | usb_log_error("Failed to set device configuration: %s.",
|
---|
[8f74140c] | 165 | str_error(rc));
|
---|
[34289ea] | 166 | return rc;
|
---|
[8f74140c] | 167 | }
|
---|
[a35b458] | 168 |
|
---|
[8be7819] | 169 | /* Create driver soft-state. */
|
---|
[065064e6] | 170 | usb_mid_t *usb_mid = usb_device_data_alloc(dev, sizeof(usb_mid_t));
|
---|
[229629d] | 171 | if (!usb_mid) {
|
---|
[a1732929] | 172 | usb_log_error("Failed to create USB MID structure.");
|
---|
[34289ea] | 173 | return ENOMEM;
|
---|
[229629d] | 174 | }
|
---|
| 175 |
|
---|
[8be7819] | 176 | /* Create control function. */
|
---|
[6785b538] | 177 | usb_mid->ctl_fun = usb_device_ddf_fun_create(dev, fun_exposed, "ctl");
|
---|
[229629d] | 178 | if (usb_mid->ctl_fun == NULL) {
|
---|
[a1732929] | 179 | usb_log_error("Failed to create control function.");
|
---|
[34289ea] | 180 | return ENOMEM;
|
---|
[d3880aa] | 181 | }
|
---|
[fcafa04] | 182 |
|
---|
[8be7819] | 183 | /* Bind control function. */
|
---|
[229629d] | 184 | rc = ddf_fun_bind(usb_mid->ctl_fun);
|
---|
[d3880aa] | 185 | if (rc != EOK) {
|
---|
[a1732929] | 186 | usb_log_error("Failed to bind control function: %s.",
|
---|
[d3880aa] | 187 | str_error(rc));
|
---|
[229629d] | 188 | ddf_fun_destroy(usb_mid->ctl_fun);
|
---|
[34289ea] | 189 | return rc;
|
---|
[d3880aa] | 190 | }
|
---|
| 191 |
|
---|
[ecb107b] | 192 | /* Create interface children. */
|
---|
[229629d] | 193 | list_initialize(&usb_mid->interface_list);
|
---|
[ecb107b] | 194 | create_interfaces(config_descriptor_raw, config_descriptor_size,
|
---|
[6df4adc4] | 195 | &usb_mid->interface_list, dev);
|
---|
[3ae93a8] | 196 |
|
---|
[34289ea] | 197 | return EOK;
|
---|
[3ae93a8] | 198 | }
|
---|
| 199 |
|
---|
| 200 | /**
|
---|
| 201 | * @}
|
---|
| 202 | */
|
---|