| 1 | /*
|
|---|
| 2 | * Copyright (c) 2010-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 | * USB querying.
|
|---|
| 35 | */
|
|---|
| 36 |
|
|---|
| 37 | #include <stdio.h>
|
|---|
| 38 | #include <stdlib.h>
|
|---|
| 39 | #include <errno.h>
|
|---|
| 40 | #include <str_error.h>
|
|---|
| 41 | #include <bool.h>
|
|---|
| 42 | #include <getopt.h>
|
|---|
| 43 | #include <devman.h>
|
|---|
| 44 | #include <devmap.h>
|
|---|
| 45 | #include <usb/usbdevice.h>
|
|---|
| 46 | #include <usb/pipes.h>
|
|---|
| 47 | #include <usb/host.h>
|
|---|
| 48 | #include <usb/driver.h>
|
|---|
| 49 | #include "usbinfo.h"
|
|---|
| 50 |
|
|---|
| 51 | static bool try_parse_class_and_address(const char *path,
|
|---|
| 52 | devman_handle_t *out_hc_handle, usb_address_t *out_device_address)
|
|---|
| 53 | {
|
|---|
| 54 | size_t class_index;
|
|---|
| 55 | size_t address;
|
|---|
| 56 | int rc;
|
|---|
| 57 | char *ptr;
|
|---|
| 58 |
|
|---|
| 59 | rc = str_size_t(path, &ptr, 10, false, &class_index);
|
|---|
| 60 | if (rc != EOK) {
|
|---|
| 61 | return false;
|
|---|
| 62 | }
|
|---|
| 63 | if ((*ptr == ':') || (*ptr == '.')) {
|
|---|
| 64 | ptr++;
|
|---|
| 65 | } else {
|
|---|
| 66 | return false;
|
|---|
| 67 | }
|
|---|
| 68 | rc = str_size_t(ptr, NULL, 10, true, &address);
|
|---|
| 69 | if (rc != EOK) {
|
|---|
| 70 | return false;
|
|---|
| 71 | }
|
|---|
| 72 | rc = usb_ddf_get_hc_handle_by_class(class_index, out_hc_handle);
|
|---|
| 73 | if (rc != EOK) {
|
|---|
| 74 | return false;
|
|---|
| 75 | }
|
|---|
| 76 | if (out_device_address != NULL) {
|
|---|
| 77 | *out_device_address = (usb_address_t) address;
|
|---|
| 78 | }
|
|---|
| 79 | return true;
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 | static bool resolve_hc_handle_and_dev_addr(const char *devpath,
|
|---|
| 83 | devman_handle_t *out_hc_handle, usb_address_t *out_device_address)
|
|---|
| 84 | {
|
|---|
| 85 | int rc;
|
|---|
| 86 |
|
|---|
| 87 | /* Hack for QEMU to save-up on typing ;-). */
|
|---|
| 88 | if (str_cmp(devpath, "qemu") == 0) {
|
|---|
| 89 | devpath = "/hw/pci0/00:01.2/uhci-rh/usb00_a1";
|
|---|
| 90 | }
|
|---|
| 91 |
|
|---|
| 92 | /* Hack for virtual keyboard. */
|
|---|
| 93 | if (str_cmp(devpath, "virt") == 0) {
|
|---|
| 94 | devpath = "/virt/usbhc/usb00_a1/usb00_a2";
|
|---|
| 95 | }
|
|---|
| 96 |
|
|---|
| 97 | if (try_parse_class_and_address(devpath,
|
|---|
| 98 | out_hc_handle, out_device_address)) {
|
|---|
| 99 | return true;
|
|---|
| 100 | }
|
|---|
| 101 |
|
|---|
| 102 | char *path = str_dup(devpath);
|
|---|
| 103 | if (path == NULL) {
|
|---|
| 104 | return ENOMEM;
|
|---|
| 105 | }
|
|---|
| 106 |
|
|---|
| 107 | devman_handle_t hc = 0;
|
|---|
| 108 | bool hc_found = false;
|
|---|
| 109 | usb_address_t addr = 0;
|
|---|
| 110 | bool addr_found = false;
|
|---|
| 111 |
|
|---|
| 112 | /* Remove suffixes and hope that we will encounter device node. */
|
|---|
| 113 | while (str_length(path) > 0) {
|
|---|
| 114 | /* Get device handle first. */
|
|---|
| 115 | devman_handle_t dev_handle;
|
|---|
| 116 | rc = devman_device_get_handle(path, &dev_handle, 0);
|
|---|
| 117 | if (rc != EOK) {
|
|---|
| 118 | free(path);
|
|---|
| 119 | return false;
|
|---|
| 120 | }
|
|---|
| 121 |
|
|---|
| 122 | /* Try to find its host controller. */
|
|---|
| 123 | if (!hc_found) {
|
|---|
| 124 | rc = usb_hc_find(dev_handle, &hc);
|
|---|
| 125 | if (rc == EOK) {
|
|---|
| 126 | hc_found = true;
|
|---|
| 127 | }
|
|---|
| 128 | }
|
|---|
| 129 | /* Try to get its address. */
|
|---|
| 130 | if (!addr_found) {
|
|---|
| 131 | addr = usb_device_get_assigned_address(dev_handle);
|
|---|
| 132 | if (addr >= 0) {
|
|---|
| 133 | addr_found = true;
|
|---|
| 134 | }
|
|---|
| 135 | }
|
|---|
| 136 |
|
|---|
| 137 | /* Speed-up. */
|
|---|
| 138 | if (hc_found && addr_found) {
|
|---|
| 139 | break;
|
|---|
| 140 | }
|
|---|
| 141 |
|
|---|
| 142 | /* Remove the last suffix. */
|
|---|
| 143 | char *slash_pos = str_rchr(path, '/');
|
|---|
| 144 | if (slash_pos != NULL) {
|
|---|
| 145 | *slash_pos = 0;
|
|---|
| 146 | }
|
|---|
| 147 | }
|
|---|
| 148 |
|
|---|
| 149 | free(path);
|
|---|
| 150 |
|
|---|
| 151 | if (hc_found && addr_found) {
|
|---|
| 152 | if (out_hc_handle != NULL) {
|
|---|
| 153 | *out_hc_handle = hc;
|
|---|
| 154 | }
|
|---|
| 155 | if (out_device_address != NULL) {
|
|---|
| 156 | *out_device_address = addr;
|
|---|
| 157 | }
|
|---|
| 158 | return true;
|
|---|
| 159 | } else {
|
|---|
| 160 | return false;
|
|---|
| 161 | }
|
|---|
| 162 | }
|
|---|
| 163 |
|
|---|
| 164 | static void print_usage(char *app_name)
|
|---|
| 165 | {
|
|---|
| 166 | #define _INDENT " "
|
|---|
| 167 | #define _OPTION(opt, description) \
|
|---|
| 168 | printf(_INDENT opt "\n" _INDENT _INDENT description "\n")
|
|---|
| 169 |
|
|---|
| 170 | printf(NAME ": query USB devices for descriptors\n\n");
|
|---|
| 171 | printf("Usage: %s [options] device [device [device [ ... ]]]\n",
|
|---|
| 172 | app_name);
|
|---|
| 173 | printf(_INDENT "The device is a devman path to the device.\n");
|
|---|
| 174 |
|
|---|
| 175 | _OPTION("-h --help", "Print this help and exit.");
|
|---|
| 176 | _OPTION("-i --identification", "Brief device identification.");
|
|---|
| 177 | _OPTION("-m --match-ids", "Print match ids generated for the device.");
|
|---|
| 178 | _OPTION("-t --descriptor-tree", "Print descriptor tree.");
|
|---|
| 179 | _OPTION("-T --descriptor-tree-full", "Print detailed descriptor tree");
|
|---|
| 180 | _OPTION("-s --strings", "Try to print all string descriptors.");
|
|---|
| 181 | _OPTION("-S --status", "Get status of the device.");
|
|---|
| 182 |
|
|---|
| 183 | printf("\n");
|
|---|
| 184 | printf("If no option is specified, `-i' is considered default.\n");
|
|---|
| 185 | printf("\n");
|
|---|
| 186 |
|
|---|
| 187 | #undef _OPTION
|
|---|
| 188 | #undef _INDENT
|
|---|
| 189 | }
|
|---|
| 190 |
|
|---|
| 191 | static struct option long_options[] = {
|
|---|
| 192 | {"help", no_argument, NULL, 'h'},
|
|---|
| 193 | {"identification", no_argument, NULL, 'i'},
|
|---|
| 194 | {"match-ids", no_argument, NULL, 'm'},
|
|---|
| 195 | {"descriptor-tree", no_argument, NULL, 't'},
|
|---|
| 196 | {"descriptor-tree-full", no_argument, NULL, 'T'},
|
|---|
| 197 | {"strings", no_argument, NULL, 's'},
|
|---|
| 198 | {"status", no_argument, NULL, 'S'},
|
|---|
| 199 | {0, 0, NULL, 0}
|
|---|
| 200 | };
|
|---|
| 201 | static const char *short_options = "himtTsS";
|
|---|
| 202 |
|
|---|
| 203 | static usbinfo_action_t actions[] = {
|
|---|
| 204 | {
|
|---|
| 205 | .opt = 'i',
|
|---|
| 206 | .action = dump_short_device_identification,
|
|---|
| 207 | .active = false
|
|---|
| 208 | },
|
|---|
| 209 | {
|
|---|
| 210 | .opt = 'm',
|
|---|
| 211 | .action = dump_device_match_ids,
|
|---|
| 212 | .active = false
|
|---|
| 213 | },
|
|---|
| 214 | {
|
|---|
| 215 | .opt = 't',
|
|---|
| 216 | .action = dump_descriptor_tree_brief,
|
|---|
| 217 | .active = false
|
|---|
| 218 | },
|
|---|
| 219 | {
|
|---|
| 220 | .opt = 'T',
|
|---|
| 221 | .action = dump_descriptor_tree_full,
|
|---|
| 222 | .active = false
|
|---|
| 223 | },
|
|---|
| 224 | {
|
|---|
| 225 | .opt = 's',
|
|---|
| 226 | .action = dump_strings,
|
|---|
| 227 | .active = false
|
|---|
| 228 | },
|
|---|
| 229 | {
|
|---|
| 230 | .opt = 'S',
|
|---|
| 231 | .action = dump_status,
|
|---|
| 232 | .active = false
|
|---|
| 233 | },
|
|---|
| 234 | {
|
|---|
| 235 | .opt = 0
|
|---|
| 236 | }
|
|---|
| 237 | };
|
|---|
| 238 |
|
|---|
| 239 | int main(int argc, char *argv[])
|
|---|
| 240 | {
|
|---|
| 241 | if (argc <= 1) {
|
|---|
| 242 | print_usage(argv[0]);
|
|---|
| 243 | return -1;
|
|---|
| 244 | }
|
|---|
| 245 |
|
|---|
| 246 | /*
|
|---|
| 247 | * Process command-line options. They determine what shall be
|
|---|
| 248 | * done with the device.
|
|---|
| 249 | */
|
|---|
| 250 | int opt;
|
|---|
| 251 | do {
|
|---|
| 252 | opt = getopt_long(argc, argv,
|
|---|
| 253 | short_options, long_options, NULL);
|
|---|
| 254 | switch (opt) {
|
|---|
| 255 | case -1:
|
|---|
| 256 | break;
|
|---|
| 257 | case '?':
|
|---|
| 258 | print_usage(argv[0]);
|
|---|
| 259 | return 1;
|
|---|
| 260 | case 'h':
|
|---|
| 261 | print_usage(argv[0]);
|
|---|
| 262 | return 0;
|
|---|
| 263 | default: {
|
|---|
| 264 | int idx = 0;
|
|---|
| 265 | while (actions[idx].opt != 0) {
|
|---|
| 266 | if (actions[idx].opt == opt) {
|
|---|
| 267 | actions[idx].active = true;
|
|---|
| 268 | break;
|
|---|
| 269 | }
|
|---|
| 270 | idx++;
|
|---|
| 271 | }
|
|---|
| 272 | break;
|
|---|
| 273 | }
|
|---|
| 274 | }
|
|---|
| 275 | } while (opt > 0);
|
|---|
| 276 |
|
|---|
| 277 | /* Set the default action. */
|
|---|
| 278 | int idx = 0;
|
|---|
| 279 | bool something_active = false;
|
|---|
| 280 | while (actions[idx].opt != 0) {
|
|---|
| 281 | if (actions[idx].active) {
|
|---|
| 282 | something_active = true;
|
|---|
| 283 | break;
|
|---|
| 284 | }
|
|---|
| 285 | idx++;
|
|---|
| 286 | }
|
|---|
| 287 | if (!something_active) {
|
|---|
| 288 | actions[0].active = true;
|
|---|
| 289 | }
|
|---|
| 290 |
|
|---|
| 291 | /*
|
|---|
| 292 | * Go through all devices given on the command line and run the
|
|---|
| 293 | * specified actions.
|
|---|
| 294 | */
|
|---|
| 295 | int i;
|
|---|
| 296 | for (i = optind; i < argc; i++) {
|
|---|
| 297 | char *devpath = argv[i];
|
|---|
| 298 |
|
|---|
| 299 | /* The initialization is here only to make compiler happy. */
|
|---|
| 300 | devman_handle_t hc_handle = 0;
|
|---|
| 301 | usb_address_t dev_addr = 0;
|
|---|
| 302 | bool found = resolve_hc_handle_and_dev_addr(devpath,
|
|---|
| 303 | &hc_handle, &dev_addr);
|
|---|
| 304 | if (!found) {
|
|---|
| 305 | fprintf(stderr, NAME ": device `%s' not found "
|
|---|
| 306 | "or not of USB kind, skipping.\n",
|
|---|
| 307 | devpath);
|
|---|
| 308 | continue;
|
|---|
| 309 | }
|
|---|
| 310 |
|
|---|
| 311 | usbinfo_device_t *dev = prepare_device(devpath,
|
|---|
| 312 | hc_handle, dev_addr);
|
|---|
| 313 | if (dev == NULL) {
|
|---|
| 314 | continue;
|
|---|
| 315 | }
|
|---|
| 316 |
|
|---|
| 317 | /* Run actions the user specified. */
|
|---|
| 318 | printf("%s\n", devpath);
|
|---|
| 319 |
|
|---|
| 320 | int action = 0;
|
|---|
| 321 | while (actions[action].opt != 0) {
|
|---|
| 322 | if (actions[action].active) {
|
|---|
| 323 | actions[action].action(dev);
|
|---|
| 324 | }
|
|---|
| 325 | action++;
|
|---|
| 326 | }
|
|---|
| 327 |
|
|---|
| 328 | /* Destroy the control pipe (close the session etc.). */
|
|---|
| 329 | destroy_device(dev);
|
|---|
| 330 | }
|
|---|
| 331 |
|
|---|
| 332 | return 0;
|
|---|
| 333 | }
|
|---|
| 334 |
|
|---|
| 335 |
|
|---|
| 336 | /** @}
|
|---|
| 337 | */
|
|---|