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