source: mainline/uspace/app/usbinfo/main.c@ 3100ebe

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 3100ebe was 3100ebe, checked in by Vojtech Horky <vojtechhorky@…>, 14 years ago

Preparation for more modular usbinfo

  • Property mode set to 100644
File size: 4.5 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 <devmap.h>
45#include <usb/usbdevice.h>
46#include <usb/pipes.h>
47#include "usbinfo.h"
48
49static void print_usage(char *app_name)
50{
51#define INDENT " "
52 printf(NAME ": query USB devices for descriptors\n\n");
53 printf("Usage: %s [options] device [device [device [ ... ]]]\n",
54 app_name);
55 printf(INDENT "The device is a devman path to the device.\n");
56 printf("\n");
57#undef INDENT
58}
59
60static bool resolve_hc_handle_and_dev_addr(const char *devpath,
61 devman_handle_t *out_hc_handle, usb_address_t *out_device_address)
62{
63 int rc;
64
65 /* Hack for QEMU to save-up on typing ;-). */
66 if (str_cmp(devpath, "qemu") == 0) {
67 devpath = "/hw/pci0/00:01.2/uhci-rh/usb00_a1";
68 }
69
70 char *path = str_dup(devpath);
71 if (path == NULL) {
72 return ENOMEM;
73 }
74
75 devman_handle_t hc = 0;
76 bool hc_found = false;
77 usb_address_t addr = 0;
78 bool addr_found = false;
79
80 /* Remove suffixes and hope that we will encounter device node. */
81 while (str_length(path) > 0) {
82 /* Get device handle first. */
83 devman_handle_t dev_handle;
84 rc = devman_device_get_handle(path, &dev_handle, 0);
85 if (rc != EOK) {
86 free(path);
87 return false;
88 }
89
90 /* Try to find its host controller. */
91 if (!hc_found) {
92 rc = usb_hc_find(dev_handle, &hc);
93 if (rc == EOK) {
94 hc_found = true;
95 }
96 }
97 /* Try to get its address. */
98 if (!addr_found) {
99 addr = usb_device_get_assigned_address(dev_handle);
100 if (addr >= 0) {
101 addr_found = true;
102 }
103 }
104
105 /* Speed-up. */
106 if (hc_found && addr_found) {
107 break;
108 }
109
110 /* Remove the last suffix. */
111 char *slash_pos = str_rchr(path, '/');
112 if (slash_pos != NULL) {
113 *slash_pos = 0;
114 }
115 }
116
117 free(path);
118
119 if (hc_found && addr_found) {
120 if (out_hc_handle != NULL) {
121 *out_hc_handle = hc;
122 }
123 if (out_device_address != NULL) {
124 *out_device_address = addr;
125 }
126 return true;
127 } else {
128 return false;
129 }
130}
131
132int main(int argc, char *argv[])
133{
134 if (argc <= 1) {
135 print_usage(argv[0]);
136 return -1;
137 }
138
139 /*
140 * Process command-line options. They determine what shall be
141 * done with the device.
142 */
143
144 /* TODO */
145
146 /*
147 * Go through all devices given on the command line and run the
148 * specified actions.
149 */
150 int i;
151 for (i = 1; i < argc; i++) {
152 char *devpath = argv[i];
153
154 /* The initialization is here only to make compiler happy. */
155 devman_handle_t hc_handle = 0;
156 usb_address_t dev_addr = 0;
157 bool found = resolve_hc_handle_and_dev_addr(devpath,
158 &hc_handle, &dev_addr);
159 if (!found) {
160 fprintf(stderr, NAME ": device `%s' not found "
161 "or not of USB kind, skipping.\n",
162 devpath);
163 continue;
164 }
165
166 usbinfo_device_t *dev = prepare_device(hc_handle, dev_addr);
167 if (dev == NULL) {
168 continue;
169 }
170
171 /* Run actions the user specified. */
172 /* TODO */
173
174
175 printf("%s\n", devpath);
176 dump_short_device_identification(dev);
177 dump_device_match_ids(dev);
178
179 /* Destroy the control pipe (close the session etc.). */
180 destroy_device(dev);
181 }
182
183 return 0;
184}
185
186
187/** @}
188 */
Note: See TracBrowser for help on using the repository browser.