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