[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>
|
---|
[15f3c3f] | 44 | #include <loc.h>
|
---|
[0edf7c7] | 45 | #include <usb/hc.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.");
|
---|
| 63 | _OPTION("-i --identification", "Brief device identification.");
|
---|
| 64 | _OPTION("-m --match-ids", "Print match ids generated for the device.");
|
---|
| 65 | _OPTION("-t --descriptor-tree", "Print descriptor tree.");
|
---|
[e387d0f] | 66 | _OPTION("-T --descriptor-tree-full", "Print detailed descriptor tree");
|
---|
[aad3587] | 67 | _OPTION("-s --strings", "Try to print all string descriptors.");
|
---|
[cb61e8f] | 68 | _OPTION("-S --status", "Get status of the device.");
|
---|
[d37d1b77] | 69 | _OPTION("-r --hid-report", "Dump HID report descriptor.");
|
---|
[051f96b] | 70 | _OPTION("-r --hid-report-usages", "Dump usages of HID report.");
|
---|
[e160da4d] | 71 |
|
---|
| 72 | printf("\n");
|
---|
| 73 | printf("If no option is specified, `-i' is considered default.\n");
|
---|
| 74 | printf("\n");
|
---|
| 75 |
|
---|
| 76 | #undef _OPTION
|
---|
| 77 | #undef _INDENT
|
---|
| 78 | }
|
---|
| 79 |
|
---|
[81ca204] | 80 | static struct option long_options[] = {
|
---|
| 81 | {"help", no_argument, NULL, 'h'},
|
---|
| 82 | {"identification", no_argument, NULL, 'i'},
|
---|
| 83 | {"match-ids", no_argument, NULL, 'm'},
|
---|
[e160da4d] | 84 | {"descriptor-tree", no_argument, NULL, 't'},
|
---|
[e387d0f] | 85 | {"descriptor-tree-full", no_argument, NULL, 'T'},
|
---|
[aad3587] | 86 | {"strings", no_argument, NULL, 's'},
|
---|
[cb61e8f] | 87 | {"status", no_argument, NULL, 'S'},
|
---|
[d37d1b77] | 88 | {"hid-report", no_argument, NULL, 'r'},
|
---|
[051f96b] | 89 | {"hid-report-usages", no_argument, NULL, 'R'},
|
---|
[81ca204] | 90 | {0, 0, NULL, 0}
|
---|
| 91 | };
|
---|
[051f96b] | 92 | static const char *short_options = "himtTsSrR";
|
---|
[81ca204] | 93 |
|
---|
[a458bc9] | 94 | static usbinfo_action_t actions[] = {
|
---|
| 95 | {
|
---|
| 96 | .opt = 'i',
|
---|
| 97 | .action = dump_short_device_identification,
|
---|
| 98 | .active = false
|
---|
| 99 | },
|
---|
| 100 | {
|
---|
| 101 | .opt = 'm',
|
---|
| 102 | .action = dump_device_match_ids,
|
---|
| 103 | .active = false
|
---|
| 104 | },
|
---|
| 105 | {
|
---|
| 106 | .opt = 't',
|
---|
| 107 | .action = dump_descriptor_tree_brief,
|
---|
| 108 | .active = false
|
---|
| 109 | },
|
---|
[e387d0f] | 110 | {
|
---|
| 111 | .opt = 'T',
|
---|
| 112 | .action = dump_descriptor_tree_full,
|
---|
| 113 | .active = false
|
---|
| 114 | },
|
---|
[a458bc9] | 115 | {
|
---|
| 116 | .opt = 's',
|
---|
| 117 | .action = dump_strings,
|
---|
| 118 | .active = false
|
---|
| 119 | },
|
---|
[cb61e8f] | 120 | {
|
---|
| 121 | .opt = 'S',
|
---|
| 122 | .action = dump_status,
|
---|
| 123 | .active = false
|
---|
| 124 | },
|
---|
[d37d1b77] | 125 | {
|
---|
| 126 | .opt = 'r',
|
---|
[051f96b] | 127 | .action = dump_hidreport_raw,
|
---|
| 128 | .active = false
|
---|
| 129 | },
|
---|
| 130 | {
|
---|
| 131 | .opt = 'R',
|
---|
| 132 | .action = dump_hidreport_usages,
|
---|
[d37d1b77] | 133 | .active = false
|
---|
| 134 | },
|
---|
[a458bc9] | 135 | {
|
---|
| 136 | .opt = 0
|
---|
| 137 | }
|
---|
| 138 | };
|
---|
| 139 |
|
---|
[c377bc50] | 140 | int main(int argc, char *argv[])
|
---|
| 141 | {
|
---|
| 142 | if (argc <= 1) {
|
---|
| 143 | print_usage(argv[0]);
|
---|
| 144 | return -1;
|
---|
[2c5cefa] | 145 | }
|
---|
[07b9203e] | 146 |
|
---|
[7ffe82f] | 147 | /*
|
---|
| 148 | * Process command-line options. They determine what shall be
|
---|
| 149 | * done with the device.
|
---|
| 150 | */
|
---|
[81ca204] | 151 | int opt;
|
---|
| 152 | do {
|
---|
| 153 | opt = getopt_long(argc, argv,
|
---|
| 154 | short_options, long_options, NULL);
|
---|
| 155 | switch (opt) {
|
---|
| 156 | case -1:
|
---|
| 157 | break;
|
---|
| 158 | case '?':
|
---|
| 159 | print_usage(argv[0]);
|
---|
| 160 | return 1;
|
---|
| 161 | case 'h':
|
---|
| 162 | print_usage(argv[0]);
|
---|
| 163 | return 0;
|
---|
[a458bc9] | 164 | default: {
|
---|
| 165 | int idx = 0;
|
---|
| 166 | while (actions[idx].opt != 0) {
|
---|
| 167 | if (actions[idx].opt == opt) {
|
---|
| 168 | actions[idx].active = true;
|
---|
| 169 | break;
|
---|
| 170 | }
|
---|
| 171 | idx++;
|
---|
| 172 | }
|
---|
[81ca204] | 173 | break;
|
---|
[a458bc9] | 174 | }
|
---|
[81ca204] | 175 | }
|
---|
| 176 | } while (opt > 0);
|
---|
[c377bc50] | 177 |
|
---|
[81ca204] | 178 | /* Set the default action. */
|
---|
[a458bc9] | 179 | int idx = 0;
|
---|
| 180 | bool something_active = false;
|
---|
| 181 | while (actions[idx].opt != 0) {
|
---|
| 182 | if (actions[idx].active) {
|
---|
| 183 | something_active = true;
|
---|
| 184 | break;
|
---|
| 185 | }
|
---|
| 186 | idx++;
|
---|
| 187 | }
|
---|
| 188 | if (!something_active) {
|
---|
| 189 | actions[0].active = true;
|
---|
[81ca204] | 190 | }
|
---|
[c377bc50] | 191 |
|
---|
[7ffe82f] | 192 | /*
|
---|
| 193 | * Go through all devices given on the command line and run the
|
---|
| 194 | * specified actions.
|
---|
| 195 | */
|
---|
| 196 | int i;
|
---|
[81ca204] | 197 | for (i = optind; i < argc; i++) {
|
---|
[7ffe82f] | 198 | char *devpath = argv[i];
|
---|
| 199 |
|
---|
| 200 | /* The initialization is here only to make compiler happy. */
|
---|
| 201 | devman_handle_t hc_handle = 0;
|
---|
| 202 | usb_address_t dev_addr = 0;
|
---|
[96f2aa6] | 203 | int rc = usb_resolve_device_handle(devpath,
|
---|
| 204 | &hc_handle, &dev_addr, NULL);
|
---|
| 205 | if (rc != EOK) {
|
---|
[7ffe82f] | 206 | fprintf(stderr, NAME ": device `%s' not found "
|
---|
| 207 | "or not of USB kind, skipping.\n",
|
---|
| 208 | devpath);
|
---|
| 209 | continue;
|
---|
[c377bc50] | 210 | }
|
---|
| 211 |
|
---|
[faf498d] | 212 | usbinfo_device_t *dev = prepare_device(devpath,
|
---|
| 213 | hc_handle, dev_addr);
|
---|
[3100ebe] | 214 | if (dev == NULL) {
|
---|
| 215 | continue;
|
---|
| 216 | }
|
---|
| 217 |
|
---|
| 218 | /* Run actions the user specified. */
|
---|
| 219 | printf("%s\n", devpath);
|
---|
[81ca204] | 220 |
|
---|
[a458bc9] | 221 | int action = 0;
|
---|
| 222 | while (actions[action].opt != 0) {
|
---|
| 223 | if (actions[action].active) {
|
---|
| 224 | actions[action].action(dev);
|
---|
| 225 | }
|
---|
| 226 | action++;
|
---|
[aad3587] | 227 | }
|
---|
[3100ebe] | 228 |
|
---|
| 229 | /* Destroy the control pipe (close the session etc.). */
|
---|
| 230 | destroy_device(dev);
|
---|
[a12917e] | 231 | }
|
---|
| 232 |
|
---|
[c377bc50] | 233 | return 0;
|
---|
[07b9203e] | 234 | }
|
---|
| 235 |
|
---|
| 236 |
|
---|
| 237 | /** @}
|
---|
| 238 | */
|
---|