source: mainline/uspace/app/usbinfo/main.c@ 8d355aa8

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 8d355aa8 was 8d355aa8, checked in by Jan Vesely <jano.vesely@…>, 13 years ago

usbinfo: allow combining list with other commands.

  • Property mode set to 100644
File size: 6.0 KB
Line 
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
50static 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("-l --list", "Print a list of host controllers and devices.");
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.");
68 _OPTION("-T --descriptor-tree-full", "Print detailed descriptor tree");
69 _OPTION("-s --strings", "Try to print all string descriptors.");
70 _OPTION("-S --status", "Get status of the device.");
71 _OPTION("-r --hid-report", "Dump HID report descriptor.");
72 _OPTION("-r --hid-report-usages", "Dump usages of HID report.");
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
82static struct option long_options[] = {
83 {"help", no_argument, NULL, 'h'},
84 {"identification", no_argument, NULL, 'i'},
85 {"list", no_argument, NULL, 'l'},
86 {"match-ids", no_argument, NULL, 'm'},
87 {"descriptor-tree", no_argument, NULL, 't'},
88 {"descriptor-tree-full", no_argument, NULL, 'T'},
89 {"strings", no_argument, NULL, 's'},
90 {"status", no_argument, NULL, 'S'},
91 {"hid-report", no_argument, NULL, 'r'},
92 {"hid-report-usages", no_argument, NULL, 'R'},
93 {0, 0, NULL, 0}
94};
95static const char *short_options = "hilmtTsSrR";
96
97static 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 },
113 {
114 .opt = 'T',
115 .action = dump_descriptor_tree_full,
116 .active = false
117 },
118 {
119 .opt = 's',
120 .action = dump_strings,
121 .active = false
122 },
123 {
124 .opt = 'S',
125 .action = dump_status,
126 .active = false
127 },
128 {
129 .opt = 'r',
130 .action = dump_hidreport_raw,
131 .active = false
132 },
133 {
134 .opt = 'R',
135 .action = dump_hidreport_usages,
136 .active = false
137 },
138 {
139 .opt = 0
140 }
141};
142
143int main(int argc, char *argv[])
144{
145 if (argc <= 1) {
146 print_usage(argv[0]);
147 return -1;
148 }
149
150 /*
151 * Process command-line options. They determine what shall be
152 * done with the device.
153 */
154 int opt;
155 do {
156 opt = getopt_long(argc, argv,
157 short_options, long_options, NULL);
158 switch (opt) {
159 case -1:
160 break;
161 case 'l':
162 list();
163 break;
164 case '?':
165 print_usage(argv[0]);
166 return 1;
167 case 'h':
168 print_usage(argv[0]);
169 return 0;
170 default: {
171 int idx = 0;
172 while (actions[idx].opt != 0) {
173 if (actions[idx].opt == opt) {
174 actions[idx].active = true;
175 break;
176 }
177 idx++;
178 }
179 break;
180 }
181 }
182 } while (opt > 0);
183
184 /* Set the default action. */
185 int idx = 0;
186 bool something_active = false;
187 while (actions[idx].opt != 0) {
188 if (actions[idx].active) {
189 something_active = true;
190 break;
191 }
192 idx++;
193 }
194 if (!something_active) {
195 actions[0].active = true;
196 }
197
198 /*
199 * Go through all devices given on the command line and run the
200 * specified actions.
201 */
202 int i;
203 for (i = optind; i < argc; i++) {
204 char *devpath = argv[i];
205
206 /* The initialization is here only to make compiler happy. */
207 devman_handle_t hc_handle = 0;
208 usb_address_t dev_addr = 0;
209 int rc = usb_resolve_device_handle(devpath,
210 &hc_handle, &dev_addr, NULL);
211 if (rc != EOK) {
212 fprintf(stderr, NAME ": device `%s' not found "
213 "or not of USB kind, skipping.\n",
214 devpath);
215 continue;
216 }
217
218 usbinfo_device_t *dev = prepare_device(devpath,
219 hc_handle, dev_addr);
220 if (dev == NULL) {
221 continue;
222 }
223
224 /* Run actions the user specified. */
225 printf("%s\n", devpath);
226
227 int action = 0;
228 while (actions[action].opt != 0) {
229 if (actions[action].active) {
230 actions[action].action(dev);
231 }
232 action++;
233 }
234
235 /* Destroy the control pipe (close the session etc.). */
236 destroy_device(dev);
237 }
238
239 return 0;
240}
241
242
243/** @}
244 */
Note: See TracBrowser for help on using the repository browser.