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