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