source: mainline/uspace/app/usbinfo/list.c@ e242fba

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

usbinfo: remove redundant error checks.

all used functions are NULL safe

  • Property mode set to 100644
File size: 4.6 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_usb_device(devman_handle_t handle)
53{
54 char path[MAX_PATH_LENGTH];
55 int rc = devman_fun_get_path(handle, path, MAX_PATH_LENGTH);
56 if (rc != EOK) {
57 printf(NAME "Failed to get path for device %" PRIun
58 "\n", handle);
59 return;
60 }
61 // TODO remove this. Device name contains USB address
62 // and addresses as external id are going away
63 usb_dev_session_t *sess = usb_dev_connect(handle);
64 async_exch_t *exch = async_exchange_begin(sess);
65
66 usb_address_t address;
67 rc = usb_get_my_address(exch, &address);
68
69 async_exchange_end(exch);
70 usb_dev_disconnect(sess);
71 if (rc != EOK) {
72 printf("Failed to get address for device %" PRIun "\n", handle);
73 return;
74 }
75 printf("\tDevice %02d: %s\n", address, path);
76}
77
78static void print_usb_bus(service_id_t svc)
79{
80 devman_handle_t hc_handle = 0;
81 int rc = devman_fun_sid_to_handle(svc, &hc_handle);
82 if (rc != EOK) {
83 printf(NAME ": Error resolving handle of HC with SID %"
84 PRIun ", skipping.\n", svc);
85 return;
86 }
87
88 char path[MAX_PATH_LENGTH];
89 rc = devman_fun_get_path(hc_handle, path, sizeof(path));
90 if (rc != EOK) {
91 printf(NAME ": Error resolving path of HC with SID %"
92 PRIun ", skipping.\n", svc);
93 return;
94 }
95 printf("Bus %" PRIun ": %s\n", svc, path);
96
97 /* Construct device's path.
98 * That's "hc function path" - ( '/' + "hc function name" ) */
99 // TODO replace this with something sane
100
101 /* Get function name */
102 char name[10];
103 rc = devman_fun_get_name(hc_handle, name, sizeof(name));
104 if (rc != EOK) {
105 printf(NAME ": Error resolving name of HC with SID %"
106 PRIun ", skipping.\n", svc);
107 return;
108 }
109
110 /* Get handle of parent device */
111 devman_handle_t fh;
112 path[str_size(path) - str_size(name) - 1] = '\0';
113 rc = devman_fun_get_handle(path, &fh, IPC_FLAG_BLOCKING);
114 if (rc != EOK) {
115 printf(NAME ": Error resolving parent handle of HC with"
116 " SID %" PRIun ", skipping.\n", svc);
117 return;
118 }
119
120 /* Get child handle */
121 devman_handle_t dh;
122 rc = devman_fun_get_child(fh, &dh);
123 if (rc != EOK) {
124 printf(NAME ": Error resolving parent handle of HC with"
125 " SID %" PRIun ", skipping.\n", svc);
126 return;
127 }
128
129 devman_handle_t *fhs = 0;
130 size_t count;
131 rc = devman_dev_get_functions(dh, &fhs, &count);
132 if (rc != EOK) {
133 printf(NAME ": Error siblings of HC with"
134 " SID %" PRIun ", skipping.\n", svc);
135 return;
136 }
137
138 for (size_t i = 0; i < count; ++i) {
139 if (fhs[i] != hc_handle)
140 print_usb_device(fhs[i]);
141 }
142 free(fhs);
143}
144
145void list(void)
146{
147 category_id_t usbhc_cat;
148 service_id_t *svcs;
149 size_t count;
150 int rc;
151
152 rc = loc_category_get_id(USB_HC_CATEGORY, &usbhc_cat, 0);
153 if (rc != EOK) {
154 printf(NAME ": Error resolving category '%s'",
155 USB_HC_CATEGORY);
156 return;
157 }
158
159 rc = loc_category_get_svcs(usbhc_cat, &svcs, &count);
160 if (rc != EOK) {
161 printf(NAME ": Error getting list of host controllers.\n");
162 return;
163 }
164
165 for (unsigned i = 0; i < count; ++i) {
166 print_usb_bus(svcs[i]);
167 }
168
169 free(svcs);
170}
171
172
173/** @}
174 */
Note: See TracBrowser for help on using the repository browser.