[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>
|
---|
[3e6a98c5] | 41 | #include <stdbool.h>
|
---|
[c377bc50] | 42 | #include <getopt.h>
|
---|
[07b9203e] | 43 | #include <devman.h>
|
---|
[15f3c3f] | 44 | #include <loc.h>
|
---|
[1a38701] | 45 | #include <usb/dev.h>
|
---|
[7d521e24] | 46 | #include <usb/dev/pipes.h>
|
---|
[07b9203e] | 47 | #include "usbinfo.h"
|
---|
| 48 |
|
---|
[e160da4d] | 49 | static void print_usage(char *app_name)
|
---|
| 50 | {
|
---|
| 51 | #define _INDENT " "
|
---|
| 52 | #define _OPTION(opt, description) \
|
---|
| 53 | printf(_INDENT opt "\n" _INDENT _INDENT description "\n")
|
---|
| 54 |
|
---|
| 55 | printf(NAME ": query USB devices for descriptors\n\n");
|
---|
| 56 | printf("Usage: %s [options] device [device [device [ ... ]]]\n",
|
---|
| 57 | app_name);
|
---|
[8d12065] | 58 | printf(_INDENT "The device can be specified in two ways.\n");
|
---|
| 59 | printf(_INDENT " o Using its devman path, e.g. /hw/pci0/.../usb00_a1.\n");
|
---|
| 60 | printf(_INDENT " o Or using BUS.ADDR numbers as printed by lsusb.\n");
|
---|
[e160da4d] | 61 |
|
---|
| 62 | _OPTION("-h --help", "Print this help and exit.");
|
---|
[9e279c4] | 63 | _OPTION("-l --list", "Print a list of host controllers and devices.");
|
---|
[e160da4d] | 64 | _OPTION("-i --identification", "Brief device identification.");
|
---|
| 65 | _OPTION("-m --match-ids", "Print match ids generated for the device.");
|
---|
| 66 | _OPTION("-t --descriptor-tree", "Print descriptor tree.");
|
---|
[e387d0f] | 67 | _OPTION("-T --descriptor-tree-full", "Print detailed descriptor tree");
|
---|
[aad3587] | 68 | _OPTION("-s --strings", "Try to print all string descriptors.");
|
---|
[cb61e8f] | 69 | _OPTION("-S --status", "Get status of the device.");
|
---|
[d37d1b77] | 70 | _OPTION("-r --hid-report", "Dump HID report descriptor.");
|
---|
[689a48e] | 71 | _OPTION("-R --hid-report-usages", "Dump usages of HID report.");
|
---|
[e160da4d] | 72 |
|
---|
| 73 | printf("\n");
|
---|
| 74 | printf("If no option is specified, `-i' is considered default.\n");
|
---|
| 75 | printf("\n");
|
---|
| 76 |
|
---|
| 77 | #undef _OPTION
|
---|
| 78 | #undef _INDENT
|
---|
| 79 | }
|
---|
| 80 |
|
---|
[81ca204] | 81 | static struct option long_options[] = {
|
---|
[1433ecda] | 82 | { "help", no_argument, NULL, 'h' },
|
---|
| 83 | { "identification", no_argument, NULL, 'i' },
|
---|
| 84 | { "list", no_argument, NULL, 'l' },
|
---|
| 85 | { "match-ids", no_argument, NULL, 'm' },
|
---|
| 86 | { "descriptor-tree", no_argument, NULL, 't' },
|
---|
| 87 | { "descriptor-tree-full", no_argument, NULL, 'T' },
|
---|
| 88 | { "strings", no_argument, NULL, 's' },
|
---|
| 89 | { "status", no_argument, NULL, 'S' },
|
---|
| 90 | { "hid-report", no_argument, NULL, 'r' },
|
---|
| 91 | { "hid-report-usages", no_argument, NULL, 'R' },
|
---|
| 92 | { 0, 0, NULL, 0 }
|
---|
[81ca204] | 93 | };
|
---|
[9e279c4] | 94 | static const char *short_options = "hilmtTsSrR";
|
---|
[81ca204] | 95 |
|
---|
[a458bc9] | 96 | static usbinfo_action_t actions[] = {
|
---|
| 97 | {
|
---|
| 98 | .opt = 'i',
|
---|
| 99 | .action = dump_short_device_identification,
|
---|
| 100 | .active = false
|
---|
| 101 | },
|
---|
| 102 | {
|
---|
| 103 | .opt = 'm',
|
---|
| 104 | .action = dump_device_match_ids,
|
---|
| 105 | .active = false
|
---|
| 106 | },
|
---|
| 107 | {
|
---|
| 108 | .opt = 't',
|
---|
| 109 | .action = dump_descriptor_tree_brief,
|
---|
| 110 | .active = false
|
---|
| 111 | },
|
---|
[e387d0f] | 112 | {
|
---|
| 113 | .opt = 'T',
|
---|
| 114 | .action = dump_descriptor_tree_full,
|
---|
| 115 | .active = false
|
---|
| 116 | },
|
---|
[a458bc9] | 117 | {
|
---|
| 118 | .opt = 's',
|
---|
| 119 | .action = dump_strings,
|
---|
| 120 | .active = false
|
---|
| 121 | },
|
---|
[cb61e8f] | 122 | {
|
---|
| 123 | .opt = 'S',
|
---|
| 124 | .action = dump_status,
|
---|
| 125 | .active = false
|
---|
| 126 | },
|
---|
[d37d1b77] | 127 | {
|
---|
| 128 | .opt = 'r',
|
---|
[051f96b] | 129 | .action = dump_hidreport_raw,
|
---|
| 130 | .active = false
|
---|
| 131 | },
|
---|
| 132 | {
|
---|
| 133 | .opt = 'R',
|
---|
| 134 | .action = dump_hidreport_usages,
|
---|
[d37d1b77] | 135 | .active = false
|
---|
| 136 | },
|
---|
[a458bc9] | 137 | {
|
---|
| 138 | .opt = 0
|
---|
| 139 | }
|
---|
| 140 | };
|
---|
| 141 |
|
---|
[c377bc50] | 142 | int main(int argc, char *argv[])
|
---|
| 143 | {
|
---|
| 144 | if (argc <= 1) {
|
---|
| 145 | print_usage(argv[0]);
|
---|
| 146 | return -1;
|
---|
[2c5cefa] | 147 | }
|
---|
[07b9203e] | 148 |
|
---|
[32260a0] | 149 | bool something_active = false;
|
---|
[7ffe82f] | 150 | /*
|
---|
| 151 | * Process command-line options. They determine what shall be
|
---|
| 152 | * done with the device.
|
---|
| 153 | */
|
---|
[81ca204] | 154 | int opt;
|
---|
[850fd32] | 155 | int idx;
|
---|
[81ca204] | 156 | do {
|
---|
| 157 | opt = getopt_long(argc, argv,
|
---|
| 158 | short_options, long_options, NULL);
|
---|
| 159 | switch (opt) {
|
---|
[1433ecda] | 160 | case -1:
|
---|
| 161 | break;
|
---|
| 162 | case 'l':
|
---|
| 163 | list();
|
---|
| 164 | break;
|
---|
| 165 | case '?':
|
---|
| 166 | print_usage(argv[0]);
|
---|
| 167 | return 1;
|
---|
| 168 | case 'h':
|
---|
| 169 | print_usage(argv[0]);
|
---|
| 170 | return 0;
|
---|
| 171 | default:
|
---|
| 172 | idx = 0;
|
---|
| 173 | while (actions[idx].opt != 0) {
|
---|
| 174 | if (actions[idx].opt == opt) {
|
---|
| 175 | actions[idx].active = true;
|
---|
| 176 | something_active = true;
|
---|
| 177 | break;
|
---|
[a458bc9] | 178 | }
|
---|
[1433ecda] | 179 | idx++;
|
---|
| 180 | }
|
---|
| 181 | break;
|
---|
[81ca204] | 182 | }
|
---|
| 183 | } while (opt > 0);
|
---|
[c377bc50] | 184 |
|
---|
[81ca204] | 185 | /* Set the default action. */
|
---|
[a458bc9] | 186 | if (!something_active) {
|
---|
| 187 | actions[0].active = true;
|
---|
[81ca204] | 188 | }
|
---|
[c377bc50] | 189 |
|
---|
[7ffe82f] | 190 | /*
|
---|
| 191 | * Go through all devices given on the command line and run the
|
---|
| 192 | * specified actions.
|
---|
| 193 | */
|
---|
| 194 | int i;
|
---|
[81ca204] | 195 | for (i = optind; i < argc; i++) {
|
---|
[7ffe82f] | 196 | char *devpath = argv[i];
|
---|
| 197 |
|
---|
| 198 | /* The initialization is here only to make compiler happy. */
|
---|
[06f9a9c9] | 199 | devman_handle_t handle = 0;
|
---|
[b7fd2a0] | 200 | errno_t rc = usb_resolve_device_handle(devpath, &handle);
|
---|
[96f2aa6] | 201 | if (rc != EOK) {
|
---|
[7ffe82f] | 202 | fprintf(stderr, NAME ": device `%s' not found "
|
---|
| 203 | "or not of USB kind, skipping.\n",
|
---|
| 204 | devpath);
|
---|
| 205 | continue;
|
---|
[c377bc50] | 206 | }
|
---|
| 207 |
|
---|
[06f9a9c9] | 208 | usb_device_t *usb_dev = usb_device_create(handle);
|
---|
| 209 |
|
---|
| 210 | if (usb_dev == NULL) {
|
---|
[bdddc9d] | 211 | fprintf(stderr, NAME ": device `%s' not found "
|
---|
| 212 | "or not of USB kind, skipping.\n",
|
---|
| 213 | devpath);
|
---|
[3100ebe] | 214 | continue;
|
---|
| 215 | }
|
---|
| 216 |
|
---|
| 217 | /* Run actions the user specified. */
|
---|
| 218 | printf("%s\n", devpath);
|
---|
[81ca204] | 219 |
|
---|
[a458bc9] | 220 | int action = 0;
|
---|
| 221 | while (actions[action].opt != 0) {
|
---|
| 222 | if (actions[action].active) {
|
---|
[06f9a9c9] | 223 | actions[action].action(usb_dev);
|
---|
[a458bc9] | 224 | }
|
---|
| 225 | action++;
|
---|
[aad3587] | 226 | }
|
---|
[3100ebe] | 227 |
|
---|
[06f9a9c9] | 228 | usb_device_destroy(usb_dev);
|
---|
[a12917e] | 229 | }
|
---|
| 230 |
|
---|
[c377bc50] | 231 | return 0;
|
---|
[07b9203e] | 232 | }
|
---|
| 233 |
|
---|
| 234 | /** @}
|
---|
| 235 | */
|
---|