source: mainline/uspace/app/usbinfo/main.c@ 7ffe82f

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

usbinfo expects list of devices

The functionality was cut down but that will be improved in next
revisions.

  • Property mode set to 100644
File size: 4.1 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 char *path = str_dup(devpath);
66 if (path == NULL) {
67 return ENOMEM;
68 }
69
70 devman_handle_t hc = 0;
71 bool hc_found = false;
72 usb_address_t addr = 0;
73 bool addr_found = false;
74
75 /* Remove suffixes and hope that we will encounter device node. */
76 while (str_length(path) > 0) {
77 /* Get device handle first. */
78 devman_handle_t dev_handle;
79 rc = devman_device_get_handle(path, &dev_handle, 0);
80 if (rc != EOK) {
81 free(path);
82 return false;
83 }
84
85 /* Try to find its host controller. */
86 if (!hc_found) {
87 rc = usb_hc_find(dev_handle, &hc);
88 if (rc == EOK) {
89 hc_found = true;
90 }
91 }
92 /* Try to get its address. */
93 if (!addr_found) {
94 addr = usb_device_get_assigned_address(dev_handle);
95 if (addr >= 0) {
96 addr_found = true;
97 }
98 }
99
100 /* Speed-up. */
101 if (hc_found && addr_found) {
102 break;
103 }
104
105 /* Remove the last suffix. */
106 char *slash_pos = str_rchr(path, '/');
107 if (slash_pos != NULL) {
108 *slash_pos = 0;
109 }
110 }
111
112 free(path);
113
114 if (hc_found && addr_found) {
115 if (out_hc_handle != NULL) {
116 *out_hc_handle = hc;
117 }
118 if (out_device_address != NULL) {
119 *out_device_address = addr;
120 }
121 return true;
122 } else {
123 return false;
124 }
125}
126
127int main(int argc, char *argv[])
128{
129 if (argc <= 1) {
130 print_usage(argv[0]);
131 return -1;
132 }
133
134 /*
135 * Process command-line options. They determine what shall be
136 * done with the device.
137 */
138
139 /* TODO */
140
141 /*
142 * Go through all devices given on the command line and run the
143 * specified actions.
144 */
145 int i;
146 for (i = 1; i < argc; i++) {
147 char *devpath = argv[i];
148
149 /* The initialization is here only to make compiler happy. */
150 devman_handle_t hc_handle = 0;
151 usb_address_t dev_addr = 0;
152 bool found = resolve_hc_handle_and_dev_addr(devpath,
153 &hc_handle, &dev_addr);
154 if (!found) {
155 fprintf(stderr, NAME ": device `%s' not found "
156 "or not of USB kind, skipping.\n",
157 devpath);
158 continue;
159 }
160
161 printf("Device `%s':\n------------\n", devpath);
162 dump_device(hc_handle, dev_addr);
163 }
164
165 return 0;
166}
167
168
169/** @}
170 */
Note: See TracBrowser for help on using the repository browser.