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 <devmap.h>
|
---|
45 | #include <usb/usbdrv.h>
|
---|
46 | #include "usbinfo.h"
|
---|
47 |
|
---|
48 | enum {
|
---|
49 | ACTION_HELP = 256,
|
---|
50 | ACTION_DEVICE_ADDRESS,
|
---|
51 | ACTION_HOST_CONTROLLER,
|
---|
52 | ACTION_DEVICE,
|
---|
53 | };
|
---|
54 |
|
---|
55 | static struct option long_options[] = {
|
---|
56 | {"help", no_argument, NULL, ACTION_HELP},
|
---|
57 | {"address", required_argument, NULL, ACTION_DEVICE_ADDRESS},
|
---|
58 | {"host-controller", required_argument, NULL, ACTION_HOST_CONTROLLER},
|
---|
59 | {"device", required_argument, NULL, ACTION_DEVICE},
|
---|
60 | {0, 0, NULL, 0}
|
---|
61 | };
|
---|
62 | static const char *short_options = "ha:t:d:";
|
---|
63 |
|
---|
64 | static void print_usage(char *app_name)
|
---|
65 | {
|
---|
66 | #define INDENT " "
|
---|
67 | printf(NAME ": query USB devices for descriptors\n\n");
|
---|
68 | printf("Usage: %s [options]\n", app_name);
|
---|
69 | printf(" -h --help\n" INDENT \
|
---|
70 | "Display this help.\n");
|
---|
71 | printf(" -tID --host-controller ID\n" INDENT \
|
---|
72 | "Set host controller (ID can be path or class number)\n");
|
---|
73 | printf(" -aADDR --address ADDR\n" INDENT \
|
---|
74 | "Set device address\n");
|
---|
75 | printf("\n");
|
---|
76 | #undef INDENT
|
---|
77 | }
|
---|
78 |
|
---|
79 | static int get_host_controller_handle(const char *path,
|
---|
80 | devman_handle_t *hc_handle)
|
---|
81 | {
|
---|
82 | int rc;
|
---|
83 |
|
---|
84 | devman_handle_t handle;
|
---|
85 | rc = devman_device_get_handle(path, &handle, 0);
|
---|
86 | if (rc != EOK) {
|
---|
87 | fprintf(stderr,
|
---|
88 | NAME ": failed getting handle of `devman::/%s'.\n",
|
---|
89 | path);
|
---|
90 | return rc;
|
---|
91 | }
|
---|
92 | *hc_handle = handle;
|
---|
93 |
|
---|
94 | return EOK;
|
---|
95 | }
|
---|
96 |
|
---|
97 | static int get_device_address(const char *str_address, usb_address_t *address)
|
---|
98 | {
|
---|
99 | usb_address_t addr = (usb_address_t) strtol(str_address, NULL, 0);
|
---|
100 | if ((addr < 0) || (addr >= USB11_ADDRESS_MAX)) {
|
---|
101 | fprintf(stderr, NAME ": USB address out of range.\n");
|
---|
102 | return ERANGE;
|
---|
103 | }
|
---|
104 |
|
---|
105 | *address = addr;
|
---|
106 | return EOK;
|
---|
107 | }
|
---|
108 |
|
---|
109 |
|
---|
110 | int main(int argc, char *argv[])
|
---|
111 | {
|
---|
112 | devman_handle_t hc_handle = (devman_handle_t) -1;
|
---|
113 | usb_address_t device_address = (usb_address_t) -1;
|
---|
114 |
|
---|
115 | if (argc <= 1) {
|
---|
116 | print_usage(argv[0]);
|
---|
117 | return -1;
|
---|
118 | }
|
---|
119 |
|
---|
120 | int i;
|
---|
121 | do {
|
---|
122 | i = getopt_long(argc, argv, short_options, long_options, NULL);
|
---|
123 | switch (i) {
|
---|
124 | case -1:
|
---|
125 | break;
|
---|
126 |
|
---|
127 | case '?':
|
---|
128 | print_usage(argv[0]);
|
---|
129 | return -1;
|
---|
130 |
|
---|
131 | case 'h':
|
---|
132 | case ACTION_HELP:
|
---|
133 | print_usage(argv[0]);
|
---|
134 | return 0;
|
---|
135 |
|
---|
136 | case 'a':
|
---|
137 | case ACTION_DEVICE_ADDRESS: {
|
---|
138 | int rc = get_device_address(optarg,
|
---|
139 | &device_address);
|
---|
140 | if (rc != EOK) {
|
---|
141 | return rc;
|
---|
142 | }
|
---|
143 | break;
|
---|
144 | }
|
---|
145 |
|
---|
146 | case 't':
|
---|
147 | case ACTION_HOST_CONTROLLER: {
|
---|
148 | int rc = get_host_controller_handle(optarg,
|
---|
149 | &hc_handle);
|
---|
150 | if (rc != EOK) {
|
---|
151 | return rc;
|
---|
152 | }
|
---|
153 | break;
|
---|
154 | }
|
---|
155 |
|
---|
156 | case 'd':
|
---|
157 | case ACTION_DEVICE:
|
---|
158 | break;
|
---|
159 |
|
---|
160 | default:
|
---|
161 | break;
|
---|
162 | }
|
---|
163 |
|
---|
164 | } while (i != -1);
|
---|
165 |
|
---|
166 | if ((hc_handle == (devman_handle_t) -1)
|
---|
167 | || (device_address == (usb_address_t) -1)) {
|
---|
168 | fprintf(stderr, NAME ": no target specified.\n");
|
---|
169 | return EINVAL;
|
---|
170 | }
|
---|
171 |
|
---|
172 | dump_device(hc_handle, device_address);
|
---|
173 |
|
---|
174 | return 0;
|
---|
175 | }
|
---|
176 |
|
---|
177 |
|
---|
178 | /** @}
|
---|
179 | */
|
---|