| 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 | #include <str.h>
|
|---|
| 48 |
|
|---|
| 49 | #include "usbinfo.h"
|
|---|
| 50 |
|
|---|
| 51 | #define MAX_PATH_LENGTH 1024
|
|---|
| 52 |
|
|---|
| 53 | static void print_usb_device(devman_handle_t handle)
|
|---|
| 54 | {
|
|---|
| 55 | char path[MAX_PATH_LENGTH];
|
|---|
| 56 | errno_t rc = devman_fun_get_path(handle, path, MAX_PATH_LENGTH);
|
|---|
| 57 | if (rc != EOK) {
|
|---|
| 58 | printf(NAME "Failed to get path for device %" PRIun "\n", handle);
|
|---|
| 59 | return;
|
|---|
| 60 | }
|
|---|
| 61 | printf("\tDevice %" PRIun ": %s\n", handle, path);
|
|---|
| 62 | }
|
|---|
| 63 |
|
|---|
| 64 | static void print_usb_bus(service_id_t svc)
|
|---|
| 65 | {
|
|---|
| 66 | devman_handle_t hc_handle = 0;
|
|---|
| 67 | errno_t rc = devman_fun_sid_to_handle(svc, &hc_handle);
|
|---|
| 68 | if (rc != EOK) {
|
|---|
| 69 | printf(NAME ": Error resolving handle of HC with SID %"
|
|---|
| 70 | PRIun ", skipping.\n", svc);
|
|---|
| 71 | return;
|
|---|
| 72 | }
|
|---|
| 73 |
|
|---|
| 74 | char path[MAX_PATH_LENGTH];
|
|---|
| 75 | rc = devman_fun_get_path(hc_handle, path, sizeof(path));
|
|---|
| 76 | if (rc != EOK) {
|
|---|
| 77 | printf(NAME ": Error resolving path of HC with SID %"
|
|---|
| 78 | PRIun ", skipping.\n", svc);
|
|---|
| 79 | return;
|
|---|
| 80 | }
|
|---|
| 81 | printf("Bus %" PRIun ": %s\n", svc, path);
|
|---|
| 82 |
|
|---|
| 83 | /*
|
|---|
| 84 | * Construct device's path.
|
|---|
| 85 | * That's "hc function path" - ( '/' + "hc function name" )
|
|---|
| 86 | */
|
|---|
| 87 | // TODO replace this with something sane
|
|---|
| 88 |
|
|---|
| 89 | /* Get function name */
|
|---|
| 90 | char name[10];
|
|---|
| 91 | rc = devman_fun_get_name(hc_handle, name, sizeof(name));
|
|---|
| 92 | if (rc != EOK) {
|
|---|
| 93 | printf(NAME ": Error resolving name of HC with SID %"
|
|---|
| 94 | PRIun ", skipping.\n", svc);
|
|---|
| 95 | return;
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 | /* Get handle of parent device */
|
|---|
| 99 | devman_handle_t fh;
|
|---|
| 100 | path[str_size(path) - str_size(name) - 1] = '\0';
|
|---|
| 101 | rc = devman_fun_get_handle(path, &fh, IPC_FLAG_BLOCKING);
|
|---|
| 102 | if (rc != EOK) {
|
|---|
| 103 | printf(NAME ": Error resolving parent handle of HC with"
|
|---|
| 104 | " SID %" PRIun ", skipping.\n", svc);
|
|---|
| 105 | return;
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|
| 108 | /* Get child handle */
|
|---|
| 109 | devman_handle_t dh;
|
|---|
| 110 | rc = devman_fun_get_child(fh, &dh);
|
|---|
| 111 | if (rc != EOK) {
|
|---|
| 112 | printf(NAME ": Error resolving parent handle of HC with"
|
|---|
| 113 | " SID %" PRIun ", skipping.\n", svc);
|
|---|
| 114 | return;
|
|---|
| 115 | }
|
|---|
| 116 |
|
|---|
| 117 | devman_handle_t *fhs = 0;
|
|---|
| 118 | size_t count;
|
|---|
| 119 | rc = devman_dev_get_functions(dh, &fhs, &count);
|
|---|
| 120 | if (rc != EOK) {
|
|---|
| 121 | printf(NAME ": Error siblings of HC with"
|
|---|
| 122 | " SID %" PRIun ", skipping.\n", svc);
|
|---|
| 123 | return;
|
|---|
| 124 | }
|
|---|
| 125 |
|
|---|
| 126 | for (size_t i = 0; i < count; ++i) {
|
|---|
| 127 | if (fhs[i] != hc_handle)
|
|---|
| 128 | print_usb_device(fhs[i]);
|
|---|
| 129 | }
|
|---|
| 130 | free(fhs);
|
|---|
| 131 | }
|
|---|
| 132 |
|
|---|
| 133 | void list(void)
|
|---|
| 134 | {
|
|---|
| 135 | category_id_t usbhc_cat;
|
|---|
| 136 | service_id_t *svcs;
|
|---|
| 137 | size_t count;
|
|---|
| 138 | errno_t rc;
|
|---|
| 139 |
|
|---|
| 140 | rc = loc_category_get_id(USB_HC_CATEGORY, &usbhc_cat, 0);
|
|---|
| 141 | if (rc != EOK) {
|
|---|
| 142 | printf(NAME ": Error resolving category '%s'",
|
|---|
| 143 | USB_HC_CATEGORY);
|
|---|
| 144 | return;
|
|---|
| 145 | }
|
|---|
| 146 |
|
|---|
| 147 | rc = loc_category_get_svcs(usbhc_cat, &svcs, &count);
|
|---|
| 148 | if (rc != EOK) {
|
|---|
| 149 | printf(NAME ": Error getting list of host controllers.\n");
|
|---|
| 150 | return;
|
|---|
| 151 | }
|
|---|
| 152 |
|
|---|
| 153 | for (unsigned int i = 0; i < count; ++i) {
|
|---|
| 154 | print_usb_bus(svcs[i]);
|
|---|
| 155 | }
|
|---|
| 156 |
|
|---|
| 157 | free(svcs);
|
|---|
| 158 | }
|
|---|
| 159 |
|
|---|
| 160 |
|
|---|
| 161 | /** @}
|
|---|
| 162 | */
|
|---|