| 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 usbinfo
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /**
|
|---|
| 33 | * @file
|
|---|
| 34 | * USB querying.
|
|---|
| 35 | */
|
|---|
| 36 |
|
|---|
| 37 | #include <stdio.h>
|
|---|
| 38 | #include <stdlib.h>
|
|---|
| 39 | #include <errno.h>
|
|---|
| 40 | #include <str_error.h>
|
|---|
| 41 | #include <bool.h>
|
|---|
| 42 | #include <getopt.h>
|
|---|
| 43 | #include <devman.h>
|
|---|
| 44 | #include <devmap.h>
|
|---|
| 45 | #include "usbinfo.h"
|
|---|
| 46 |
|
|---|
| 47 | enum {
|
|---|
| 48 | ACTION_HELP = 256,
|
|---|
| 49 | ACTION_DEVICE_ADDRESS,
|
|---|
| 50 | ACTION_HOST_CONTROLLER,
|
|---|
| 51 | ACTION_DEVICE,
|
|---|
| 52 | };
|
|---|
| 53 |
|
|---|
| 54 | static struct option long_options[] = {
|
|---|
| 55 | {"help", no_argument, NULL, ACTION_HELP},
|
|---|
| 56 | {"address", required_argument, NULL, ACTION_DEVICE_ADDRESS},
|
|---|
| 57 | {"host-controller", required_argument, NULL, ACTION_HOST_CONTROLLER},
|
|---|
| 58 | {"device", required_argument, NULL, ACTION_DEVICE},
|
|---|
| 59 | {0, 0, NULL, 0}
|
|---|
| 60 | };
|
|---|
| 61 | static const char *short_options = "ha:t:d:";
|
|---|
| 62 |
|
|---|
| 63 | static void print_usage(char *app_name)
|
|---|
| 64 | {
|
|---|
| 65 | #define INDENT " "
|
|---|
| 66 | printf(NAME ": query USB devices for descriptors\n\n");
|
|---|
| 67 | printf("Usage: %s [options]\n", app_name);
|
|---|
| 68 | printf(" -h --help\n" INDENT \
|
|---|
| 69 | "Display this help.\n");
|
|---|
| 70 | printf(" -tID --host-controller ID\n" INDENT \
|
|---|
| 71 | "Set host controller (ID can be path or class number)\n");
|
|---|
| 72 | printf(" -aADDR --address ADDR\n" INDENT \
|
|---|
| 73 | "Set device address\n");
|
|---|
| 74 | printf("\n");
|
|---|
| 75 | #undef INDENT
|
|---|
| 76 | }
|
|---|
| 77 |
|
|---|
| 78 | static int get_host_controller_handle(const char *path,
|
|---|
| 79 | devman_handle_t *hc_handle)
|
|---|
| 80 | {
|
|---|
| 81 | int rc;
|
|---|
| 82 |
|
|---|
| 83 | if (str_cmp(path, "uhci") == 0) {
|
|---|
| 84 | path = "/hw/pci0/00:01.2/uhci";
|
|---|
| 85 | }
|
|---|
| 86 |
|
|---|
| 87 | devman_handle_t handle;
|
|---|
| 88 | rc = devman_device_get_handle(path, &handle, 0);
|
|---|
| 89 | if (rc != EOK) {
|
|---|
| 90 | fprintf(stderr,
|
|---|
| 91 | NAME ": failed getting handle of `devman::/%s'.\n",
|
|---|
| 92 | path);
|
|---|
| 93 | return rc;
|
|---|
| 94 | }
|
|---|
| 95 | *hc_handle = handle;
|
|---|
| 96 |
|
|---|
| 97 | return EOK;
|
|---|
| 98 | }
|
|---|
| 99 |
|
|---|
| 100 | static int get_device_address(const char *str_address, usb_address_t *address)
|
|---|
| 101 | {
|
|---|
| 102 | usb_address_t addr = (usb_address_t) strtol(str_address, NULL, 0);
|
|---|
| 103 | if ((addr < 0) || (addr >= USB11_ADDRESS_MAX)) {
|
|---|
| 104 | fprintf(stderr, NAME ": USB address out of range.\n");
|
|---|
| 105 | return ERANGE;
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|
| 108 | *address = addr;
|
|---|
| 109 | return EOK;
|
|---|
| 110 | }
|
|---|
| 111 |
|
|---|
| 112 |
|
|---|
| 113 | int main(int argc, char *argv[])
|
|---|
| 114 | {
|
|---|
| 115 | devman_handle_t hc_handle = (devman_handle_t) -1;
|
|---|
| 116 | usb_address_t device_address = (usb_address_t) -1;
|
|---|
| 117 |
|
|---|
| 118 | if (argc <= 1) {
|
|---|
| 119 | print_usage(argv[0]);
|
|---|
| 120 | return -1;
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | int i;
|
|---|
| 124 | do {
|
|---|
| 125 | i = getopt_long(argc, argv, short_options, long_options, NULL);
|
|---|
| 126 | switch (i) {
|
|---|
| 127 | case -1:
|
|---|
| 128 | break;
|
|---|
| 129 |
|
|---|
| 130 | case '?':
|
|---|
| 131 | print_usage(argv[0]);
|
|---|
| 132 | return -1;
|
|---|
| 133 |
|
|---|
| 134 | case 'h':
|
|---|
| 135 | case ACTION_HELP:
|
|---|
| 136 | print_usage(argv[0]);
|
|---|
| 137 | return 0;
|
|---|
| 138 |
|
|---|
| 139 | case 'a':
|
|---|
| 140 | case ACTION_DEVICE_ADDRESS: {
|
|---|
| 141 | int rc = get_device_address(optarg,
|
|---|
| 142 | &device_address);
|
|---|
| 143 | if (rc != EOK) {
|
|---|
| 144 | return rc;
|
|---|
| 145 | }
|
|---|
| 146 | break;
|
|---|
| 147 | }
|
|---|
| 148 |
|
|---|
| 149 | case 't':
|
|---|
| 150 | case ACTION_HOST_CONTROLLER: {
|
|---|
| 151 | int rc = get_host_controller_handle(optarg,
|
|---|
| 152 | &hc_handle);
|
|---|
| 153 | if (rc != EOK) {
|
|---|
| 154 | return rc;
|
|---|
| 155 | }
|
|---|
| 156 | break;
|
|---|
| 157 | }
|
|---|
| 158 |
|
|---|
| 159 | case 'd':
|
|---|
| 160 | case ACTION_DEVICE:
|
|---|
| 161 | break;
|
|---|
| 162 |
|
|---|
| 163 | default:
|
|---|
| 164 | break;
|
|---|
| 165 | }
|
|---|
| 166 |
|
|---|
| 167 | } while (i != -1);
|
|---|
| 168 |
|
|---|
| 169 | if ((hc_handle == (devman_handle_t) -1)
|
|---|
| 170 | || (device_address == (usb_address_t) -1)) {
|
|---|
| 171 | fprintf(stderr, NAME ": no target specified.\n");
|
|---|
| 172 | return EINVAL;
|
|---|
| 173 | }
|
|---|
| 174 |
|
|---|
| 175 | dump_device(hc_handle, device_address);
|
|---|
| 176 |
|
|---|
| 177 | return 0;
|
|---|
| 178 | }
|
|---|
| 179 |
|
|---|
| 180 |
|
|---|
| 181 | /** @}
|
|---|
| 182 | */
|
|---|