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

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

libdrv, usb: Rename session close to disconnect.

  • Property mode set to 100644
File size: 5.0 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_USB_ADDRESS USB11_ADDRESS_MAX
51#define MAX_PATH_LENGTH 1024
52
53static void print_found_hc(service_id_t sid, const char *path)
54{
55 printf("Bus %" PRIun ": %s\n", sid, path);
56}
57static void print_found_dev(usb_address_t addr, const char *path)
58{
59 printf(" Device %02d: %s\n", addr, path);
60}
61
62static void print_usb_devices(devman_handle_t bus_handle,
63 devman_handle_t *fhs, size_t count)
64{
65 for (size_t i = 0; i < count; ++i) {
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 char path[MAX_PATH_LENGTH];
133 rc = devman_fun_get_path(hc_handle, path, MAX_PATH_LENGTH);
134 if (rc != EOK) {
135 printf(NAME ": Error resolving path of HC with SID %"
136 PRIun ", skipping.\n", svcs[i]);
137 continue;
138 }
139 print_found_hc(svcs[i], path);
140
141 // TODO replace this with something sane
142 char name[10];
143 rc = devman_fun_get_name(hc_handle, name, 10);
144 if (rc != EOK) {
145 printf(NAME ": Error resolving name of HC with SID %"
146 PRIun ", skipping.\n", svcs[i]);
147 continue;
148 }
149
150 devman_handle_t fh;
151 path[str_size(path) - str_size(name) - 1] = '\0';
152 rc = devman_fun_get_handle(path, &fh, IPC_FLAG_BLOCKING);
153 if (rc != EOK) {
154 printf(NAME ": Error resolving parent handle of HC with"
155 " SID %" PRIun ", skipping.\n", svcs[i]);
156 continue;
157 }
158 devman_handle_t dh;
159 rc = devman_fun_get_child(fh, &dh);
160 if (rc != EOK) {
161 printf(NAME ": Error resolving parent handle of HC with"
162 " SID %" PRIun ", skipping.\n", svcs[i]);
163 continue;
164 }
165 devman_handle_t *fhs = 0;
166 size_t count;
167 rc = devman_dev_get_functions(dh, &fhs, &count);
168 if (rc != EOK) {
169 printf(NAME ": Error siblings of HC with"
170 " SID %" PRIun ", skipping.\n", svcs[i]);
171 continue;
172 }
173 print_usb_devices(hc_handle, fhs, count);
174 free(fhs);
175 }
176
177 free(svcs);
178}
179
180
181/** @}
182 */
Note: See TracBrowser for help on using the repository browser.