source: mainline/uspace/app/lsusb/main.c@ 94f6df7

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 94f6df7 was 7beb220, checked in by Jiri Svoboda <jiri@…>, 14 years ago

Skeleton of devctl utility. Currently prints device tree.

  • Property mode set to 100644
File size: 3.7 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 <bool.h>
43#include <getopt.h>
44#include <devman.h>
45#include <loc.h>
46#include <usb/dev/hub.h>
47#include <usb/hc.h>
48
49#define NAME "lsusb"
50
51#define MAX_USB_ADDRESS USB11_ADDRESS_MAX
52#define MAX_PATH_LENGTH 1024
53
54static void print_found_hc(service_id_t sid, const char *path)
55{
56 printf("Bus %" PRIun ": %s\n", sid, path);
57}
58static void print_found_dev(usb_address_t addr, const char *path)
59{
60 printf(" Device %02d: %s\n", addr, path);
61}
62
63static void print_hc_devices(devman_handle_t hc_handle)
64{
65 int rc;
66 usb_hc_connection_t conn;
67
68 usb_hc_connection_initialize(&conn, hc_handle);
69 rc = usb_hc_connection_open(&conn);
70 if (rc != EOK) {
71 printf(NAME ": failed to connect to HC: %s.\n",
72 str_error(rc));
73 return;
74 }
75 usb_address_t addr;
76 for (addr = 1; addr < MAX_USB_ADDRESS; addr++) {
77 devman_handle_t dev_handle;
78 rc = usb_hc_get_handle_by_address(&conn, addr, &dev_handle);
79 if (rc != EOK) {
80 continue;
81 }
82 char path[MAX_PATH_LENGTH];
83 rc = devman_fun_get_path(dev_handle, path, MAX_PATH_LENGTH);
84 if (rc != EOK) {
85 continue;
86 }
87 print_found_dev(addr, path);
88 }
89 usb_hc_connection_close(&conn);
90}
91
92int main(int argc, char *argv[])
93{
94 category_id_t usbhc_cat;
95 service_id_t *svcs;
96 size_t count;
97 size_t i;
98 int rc;
99
100 rc = loc_category_get_id(USB_HC_CATEGORY, &usbhc_cat, 0);
101 if (rc != EOK) {
102 printf(NAME ": Error resolving category '%s'",
103 USB_HC_CATEGORY);
104 return 1;
105 }
106
107 rc = loc_category_get_svcs(usbhc_cat, &svcs, &count);
108 if (rc != EOK) {
109 printf(NAME ": Error getting list of host controllers.\n");
110 return 1;
111 }
112
113 for (i = 0; i < count; i++) {
114 devman_handle_t hc_handle = 0;
115 int rc = usb_ddf_get_hc_handle_by_sid(svcs[i], &hc_handle);
116 if (rc != EOK) {
117 printf(NAME ": Error resolving handle of HC with SID %"
118 PRIun ", skipping.\n", svcs[i]);
119 continue;
120 }
121 char path[MAX_PATH_LENGTH];
122 rc = devman_fun_get_path(hc_handle, path, MAX_PATH_LENGTH);
123 if (rc != EOK) {
124 printf(NAME ": Error resolving path of HC with SID %"
125 PRIun ", skipping.\n", svcs[i]);
126 continue;
127 }
128 print_found_hc(svcs[i], path);
129 print_hc_devices(hc_handle);
130 }
131
132 free(svcs);
133
134 return 0;
135}
136
137
138/** @}
139 */
Note: See TracBrowser for help on using the repository browser.