| 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 usbinfo
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /**
|
|---|
| 33 | * @file
|
|---|
| 34 | * Dumping of generic device properties.
|
|---|
| 35 | */
|
|---|
| 36 | #include <stdio.h>
|
|---|
| 37 | #include <str_error.h>
|
|---|
| 38 | #include <errno.h>
|
|---|
| 39 | #include <usb/pipes.h>
|
|---|
| 40 | #include <usb/recognise.h>
|
|---|
| 41 | #include <usb/request.h>
|
|---|
| 42 | #include "usbinfo.h"
|
|---|
| 43 |
|
|---|
| 44 | int dump_device(devman_handle_t hc_handle, usb_address_t address)
|
|---|
| 45 | {
|
|---|
| 46 | int rc;
|
|---|
| 47 | usb_device_connection_t wire;
|
|---|
| 48 | usb_endpoint_pipe_t ctrl_pipe;
|
|---|
| 49 |
|
|---|
| 50 | /*
|
|---|
| 51 | * Initialize pipes.
|
|---|
| 52 | */
|
|---|
| 53 | rc = usb_device_connection_initialize(&wire, hc_handle, address);
|
|---|
| 54 | if (rc != EOK) {
|
|---|
| 55 | fprintf(stderr,
|
|---|
| 56 | NAME ": failed to create connection to the device: %s.\n",
|
|---|
| 57 | str_error(rc));
|
|---|
| 58 | goto leave;
|
|---|
| 59 | }
|
|---|
| 60 | rc = usb_endpoint_pipe_initialize_default_control(&ctrl_pipe, &wire);
|
|---|
| 61 | if (rc != EOK) {
|
|---|
| 62 | fprintf(stderr,
|
|---|
| 63 | NAME ": failed to create default control pipe: %s.\n",
|
|---|
| 64 | str_error(rc));
|
|---|
| 65 | goto leave;
|
|---|
| 66 | }
|
|---|
| 67 | rc = usb_endpoint_pipe_start_session(&ctrl_pipe);
|
|---|
| 68 | if (rc != EOK) {
|
|---|
| 69 | fprintf(stderr,
|
|---|
| 70 | NAME ": failed to start session on control pipe: %s.\n",
|
|---|
| 71 | str_error(rc));
|
|---|
| 72 | goto leave;
|
|---|
| 73 | }
|
|---|
| 74 |
|
|---|
| 75 | /*
|
|---|
| 76 | * Dump information about possible match ids.
|
|---|
| 77 | */
|
|---|
| 78 | match_id_list_t match_id_list;
|
|---|
| 79 | init_match_ids(&match_id_list);
|
|---|
| 80 | rc = usb_device_create_match_ids(&ctrl_pipe, &match_id_list);
|
|---|
| 81 | if (rc != EOK) {
|
|---|
| 82 | fprintf(stderr,
|
|---|
| 83 | NAME ": failed to fetch match ids of the device: %s.\n",
|
|---|
| 84 | str_error(rc));
|
|---|
| 85 | goto leave;
|
|---|
| 86 | }
|
|---|
| 87 | dump_match_ids(&match_id_list);
|
|---|
| 88 |
|
|---|
| 89 | /*
|
|---|
| 90 | * Get device descriptor and dump it.
|
|---|
| 91 | */
|
|---|
| 92 | usb_standard_device_descriptor_t device_descriptor;
|
|---|
| 93 | rc = usb_request_get_device_descriptor(&ctrl_pipe, &device_descriptor);
|
|---|
| 94 | if (rc != EOK) {
|
|---|
| 95 | fprintf(stderr,
|
|---|
| 96 | NAME ": failed to fetch standard device descriptor: %s.\n",
|
|---|
| 97 | str_error(rc));
|
|---|
| 98 | goto leave;
|
|---|
| 99 | }
|
|---|
| 100 | dump_usb_descriptor((uint8_t *)&device_descriptor, sizeof(device_descriptor));
|
|---|
| 101 |
|
|---|
| 102 | /*
|
|---|
| 103 | * Get first configuration descriptor and dump it.
|
|---|
| 104 | */
|
|---|
| 105 | usb_standard_configuration_descriptor_t config_descriptor;
|
|---|
| 106 | int config_index = 0;
|
|---|
| 107 | rc = usb_request_get_bare_configuration_descriptor(&ctrl_pipe,
|
|---|
| 108 | config_index, &config_descriptor);
|
|---|
| 109 | if (rc != EOK) {
|
|---|
| 110 | fprintf(stderr,
|
|---|
| 111 | NAME ": failed to fetch standard configuration descriptor: %s.\n",
|
|---|
| 112 | str_error(rc));
|
|---|
| 113 | goto leave;
|
|---|
| 114 | }
|
|---|
| 115 | //dump_standard_configuration_descriptor(config_index, &config_descriptor);
|
|---|
| 116 |
|
|---|
| 117 | void *full_config_descriptor = malloc(config_descriptor.total_length);
|
|---|
| 118 | rc = usb_request_get_full_configuration_descriptor(&ctrl_pipe,
|
|---|
| 119 | config_index,
|
|---|
| 120 | full_config_descriptor, config_descriptor.total_length, NULL);
|
|---|
| 121 | if (rc != EOK) {
|
|---|
| 122 | fprintf(stderr,
|
|---|
| 123 | NAME ": failed to fetch full configuration descriptor: %s.\n",
|
|---|
| 124 | str_error(rc));
|
|---|
| 125 | goto leave;
|
|---|
| 126 | }
|
|---|
| 127 |
|
|---|
| 128 | dump_descriptor_tree(full_config_descriptor,
|
|---|
| 129 | config_descriptor.total_length);
|
|---|
| 130 |
|
|---|
| 131 | /*
|
|---|
| 132 | * Get supported languages of STRING descriptors.
|
|---|
| 133 | */
|
|---|
| 134 | l18_win_locales_t *langs;
|
|---|
| 135 | size_t langs_count;
|
|---|
| 136 | rc = usb_request_get_supported_languages(&ctrl_pipe,
|
|---|
| 137 | &langs, &langs_count);
|
|---|
| 138 | if (rc != EOK) {
|
|---|
| 139 | fprintf(stderr,
|
|---|
| 140 | NAME ": failed to get list of supported languages: %s.\n",
|
|---|
| 141 | str_error(rc));
|
|---|
| 142 | goto skip_strings;
|
|---|
| 143 | }
|
|---|
| 144 |
|
|---|
| 145 | printf("String languages (%zu):", langs_count);
|
|---|
| 146 | size_t i;
|
|---|
| 147 | for (i = 0; i < langs_count; i++) {
|
|---|
| 148 | printf(" 0x%04x", (int) langs[i]);
|
|---|
| 149 | }
|
|---|
| 150 | printf(".\n");
|
|---|
| 151 |
|
|---|
| 152 | /*
|
|---|
| 153 | * Dump all strings in English (0x0409).
|
|---|
| 154 | */
|
|---|
| 155 | uint16_t lang = 0x0409;
|
|---|
| 156 | /*
|
|---|
| 157 | * Up to 3 string in device descriptor, 1 for configuration and
|
|---|
| 158 | * one for each interface.
|
|---|
| 159 | */
|
|---|
| 160 | size_t max_idx = 3 + 1 + config_descriptor.interface_count;
|
|---|
| 161 | size_t idx;
|
|---|
| 162 | for (idx = 1; idx <= max_idx; idx++) {
|
|---|
| 163 | uint8_t *string;
|
|---|
| 164 | size_t string_size;
|
|---|
| 165 | rc = usb_request_get_descriptor_alloc(&ctrl_pipe,
|
|---|
| 166 | USB_REQUEST_TYPE_STANDARD, USB_DESCTYPE_STRING,
|
|---|
| 167 | idx, uint16_host2usb(lang),
|
|---|
| 168 | (void **) &string, &string_size);
|
|---|
| 169 | if (rc == EINTR) {
|
|---|
| 170 | /* Invalid index, skip silently. */
|
|---|
| 171 | continue;
|
|---|
| 172 | }
|
|---|
| 173 | if (rc != EOK) {
|
|---|
| 174 | fprintf(stderr,
|
|---|
| 175 | NAME ": failed to fetch string #%zu " \
|
|---|
| 176 | "(lang 0x%04x): %s.\n",
|
|---|
| 177 | idx, (int) lang, str_error(rc));
|
|---|
| 178 | continue;
|
|---|
| 179 | }
|
|---|
| 180 | /*
|
|---|
| 181 | printf("String #%zu (language 0x%04x):\n", idx, (int) lang);
|
|---|
| 182 | dump_buffer(NULL, 0,
|
|---|
| 183 | string, string_size);
|
|---|
| 184 | */
|
|---|
| 185 | if (string_size <= 2) {
|
|---|
| 186 | fprintf(stderr,
|
|---|
| 187 | NAME ": no string for index #%zu.\n", idx);
|
|---|
| 188 | free(string);
|
|---|
| 189 | continue;
|
|---|
| 190 | }
|
|---|
| 191 | string_size -= 2;
|
|---|
| 192 |
|
|---|
| 193 | size_t string_char_count = string_size / 2;
|
|---|
| 194 | wchar_t *string_chars = malloc(sizeof(wchar_t) * (string_char_count + 1));
|
|---|
| 195 | assert(string_chars != NULL);
|
|---|
| 196 | for (i = 0; i < string_char_count; i++) {
|
|---|
| 197 | uint16_t uni_char = (string[2 + 2 * i + 1] << 8)
|
|---|
| 198 | + string[2 + 2 * i];
|
|---|
| 199 | string_chars[i] = uni_char;
|
|---|
| 200 | }
|
|---|
| 201 | string_chars[string_char_count] = 0;
|
|---|
| 202 | free(string);
|
|---|
| 203 |
|
|---|
| 204 | char *str = wstr_to_astr(string_chars);
|
|---|
| 205 | assert(str != NULL);
|
|---|
| 206 | free(string_chars);
|
|---|
| 207 |
|
|---|
| 208 | printf("String #%zu (language 0x%04x): \"%s\"\n",
|
|---|
| 209 | idx, (int) lang, str);
|
|---|
| 210 | free(str);
|
|---|
| 211 | }
|
|---|
| 212 |
|
|---|
| 213 | skip_strings:
|
|---|
| 214 |
|
|---|
| 215 | rc = EOK;
|
|---|
| 216 |
|
|---|
| 217 | leave:
|
|---|
| 218 | /* Ignoring errors here. */
|
|---|
| 219 | usb_endpoint_pipe_end_session(&ctrl_pipe);
|
|---|
| 220 |
|
|---|
| 221 | return rc;
|
|---|
| 222 | }
|
|---|
| 223 |
|
|---|
| 224 | /** @}
|
|---|
| 225 | */
|
|---|