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