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