source: mainline/uspace/app/usbinfo/list.c@ 48fa501

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

ubsinfo: add few explanatory comments

  • Property mode set to 100644
File size: 5.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 lsusb
30 * @{
31 */
32/**
33 * @file
34 * Listing of USB host controllers.
35 */
36
37#include <inttypes.h>
38#include <stdio.h>
39#include <stdlib.h>
40#include <errno.h>
41#include <str_error.h>
42#include <stdbool.h>
43#include <getopt.h>
44#include <devman.h>
45#include <loc.h>
46#include <usb_iface.h>
47
48#include "usbinfo.h"
49
50#define MAX_PATH_LENGTH 1024
51
52static void print_found_hc(service_id_t sid, const char *path)
53{
54 printf("Bus %" PRIun ": %s\n", sid, path);
55}
56static void print_found_dev(usb_address_t addr, const char *path)
57{
58 printf(" Device %02d: %s\n", addr, path);
59}
60
61static void print_usb_devices(devman_handle_t bus_handle,
62 devman_handle_t *fhs, size_t count)
63{
64 for (size_t i = 0; i < count; ++i) {
65 /* Skip hc ctl function */
66 if (fhs[i] == bus_handle)
67 continue;
68 char path[MAX_PATH_LENGTH];
69 int rc = devman_fun_get_path(fhs[i], path, MAX_PATH_LENGTH);
70 if (rc != EOK) {
71 printf(NAME "Failed to get path for device %" PRIun
72 "\n", fhs[i]);
73 continue;
74 }
75 // TODO remove this. Device name contains USB address
76 // and addresses as external id are going away
77 usb_dev_session_t *sess = usb_dev_connect(fhs[i]);
78 if (!sess) {
79 printf(NAME "Failed to connect to device %" PRIun
80 "\n", fhs[i]);
81 continue;
82 }
83 async_exch_t *exch = async_exchange_begin(sess);
84 if (!exch) {
85 printf("Failed to create exchange to dev %" PRIun
86 "\n", fhs[i]);
87 usb_dev_disconnect(sess);
88 continue;
89 }
90 usb_address_t address;
91 rc = usb_get_my_address(exch, &address);
92 async_exchange_end(exch);
93 usb_dev_disconnect(sess);
94 if (rc != EOK) {
95 printf("Failed to get address for device %" PRIun
96 "\n", fhs[i]);
97 continue;
98 }
99 print_found_dev(address, path);
100
101 }
102}
103
104void list(void)
105{
106 category_id_t usbhc_cat;
107 service_id_t *svcs;
108 size_t count;
109 int rc;
110
111 rc = loc_category_get_id(USB_HC_CATEGORY, &usbhc_cat, 0);
112 if (rc != EOK) {
113 printf(NAME ": Error resolving category '%s'",
114 USB_HC_CATEGORY);
115 return;
116 }
117
118 rc = loc_category_get_svcs(usbhc_cat, &svcs, &count);
119 if (rc != EOK) {
120 printf(NAME ": Error getting list of host controllers.\n");
121 return;
122 }
123
124 for (unsigned i = 0; i < count; ++i) {
125 devman_handle_t hc_handle = 0;
126 int rc = devman_fun_sid_to_handle(svcs[i], &hc_handle);
127 if (rc != EOK) {
128 printf(NAME ": Error resolving handle of HC with SID %"
129 PRIun ", skipping.\n", svcs[i]);
130 continue;
131 }
132
133 char path[MAX_PATH_LENGTH];
134 rc = devman_fun_get_path(hc_handle, path, MAX_PATH_LENGTH);
135 if (rc != EOK) {
136 printf(NAME ": Error resolving path of HC with SID %"
137 PRIun ", skipping.\n", svcs[i]);
138 continue;
139 }
140 print_found_hc(svcs[i], path);
141
142 /* Construct device's path.
143 * That's "hc function path" - ( '/' + "hc function name" ) */
144 // TODO replace this with something sane
145 char name[10];
146 rc = devman_fun_get_name(hc_handle, name, 10);
147 if (rc != EOK) {
148 printf(NAME ": Error resolving name of HC with SID %"
149 PRIun ", skipping.\n", svcs[i]);
150 continue;
151 }
152
153 devman_handle_t fh;
154 path[str_size(path) - str_size(name) - 1] = '\0';
155 rc = devman_fun_get_handle(path, &fh, IPC_FLAG_BLOCKING);
156 if (rc != EOK) {
157 printf(NAME ": Error resolving parent handle of HC with"
158 " SID %" PRIun ", skipping.\n", svcs[i]);
159 continue;
160 }
161 devman_handle_t dh;
162 rc = devman_fun_get_child(fh, &dh);
163 if (rc != EOK) {
164 printf(NAME ": Error resolving parent handle of HC with"
165 " SID %" PRIun ", skipping.\n", svcs[i]);
166 continue;
167 }
168 devman_handle_t *fhs = 0;
169 size_t count;
170 rc = devman_dev_get_functions(dh, &fhs, &count);
171 if (rc != EOK) {
172 printf(NAME ": Error siblings of HC with"
173 " SID %" PRIun ", skipping.\n", svcs[i]);
174 continue;
175 }
176 print_usb_devices(hc_handle, fhs, count);
177 free(fhs);
178 }
179
180 free(svcs);
181}
182
183
184/** @}
185 */
Note: See TracBrowser for help on using the repository browser.