[07b9203e] | 1 | /*
|
---|
[c377bc50] | 2 | * Copyright (c) 2010-2011 Vojtech Horky
|
---|
[07b9203e] | 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 |
|
---|
[632ed68] | 29 | /** @addtogroup usbinfo
|
---|
[07b9203e] | 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /**
|
---|
| 33 | * @file
|
---|
[632ed68] | 34 | * USB querying.
|
---|
[07b9203e] | 35 | */
|
---|
| 36 |
|
---|
| 37 | #include <stdio.h>
|
---|
| 38 | #include <stdlib.h>
|
---|
| 39 | #include <errno.h>
|
---|
| 40 | #include <str_error.h>
|
---|
| 41 | #include <bool.h>
|
---|
[c377bc50] | 42 | #include <getopt.h>
|
---|
[07b9203e] | 43 | #include <devman.h>
|
---|
[c377bc50] | 44 | #include <devmap.h>
|
---|
[07b9203e] | 45 | #include "usbinfo.h"
|
---|
| 46 |
|
---|
[c377bc50] | 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:";
|
---|
[99ea659c] | 62 |
|
---|
[07b9203e] | 63 | static void print_usage(char *app_name)
|
---|
| 64 | {
|
---|
[c377bc50] | 65 | #define INDENT " "
|
---|
[07b9203e] | 66 | printf(NAME ": query USB devices for descriptors\n\n");
|
---|
[c377bc50] | 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");
|
---|
[07b9203e] | 74 | printf("\n");
|
---|
[c377bc50] | 75 | #undef INDENT
|
---|
[07b9203e] | 76 | }
|
---|
| 77 |
|
---|
[a12917e] | 78 | static int get_host_controller_handle(const char *path,
|
---|
| 79 | devman_handle_t *hc_handle)
|
---|
[07b9203e] | 80 | {
|
---|
| 81 | int rc;
|
---|
| 82 |
|
---|
[608afb9] | 83 | if (str_cmp(path, "uhci") == 0) {
|
---|
[6dd35e0] | 84 | path = "/hw/pci0/00:01.2/uhci-hc";
|
---|
[608afb9] | 85 | }
|
---|
| 86 |
|
---|
[a12917e] | 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;
|
---|
[07b9203e] | 96 |
|
---|
[c377bc50] | 97 | return EOK;
|
---|
[07b9203e] | 98 | }
|
---|
| 99 |
|
---|
[a12917e] | 100 | static int get_device_address(const char *str_address, usb_address_t *address)
|
---|
[07b9203e] | 101 | {
|
---|
[a12917e] | 102 | usb_address_t addr = (usb_address_t) strtol(str_address, NULL, 0);
|
---|
| 103 | if ((addr < 0) || (addr >= USB11_ADDRESS_MAX)) {
|
---|
[c377bc50] | 104 | fprintf(stderr, NAME ": USB address out of range.\n");
|
---|
| 105 | return ERANGE;
|
---|
[07b9203e] | 106 | }
|
---|
| 107 |
|
---|
[a12917e] | 108 | *address = addr;
|
---|
| 109 | return EOK;
|
---|
[c377bc50] | 110 | }
|
---|
[07b9203e] | 111 |
|
---|
| 112 |
|
---|
[c377bc50] | 113 | int main(int argc, char *argv[])
|
---|
| 114 | {
|
---|
[a12917e] | 115 | devman_handle_t hc_handle = (devman_handle_t) -1;
|
---|
| 116 | usb_address_t device_address = (usb_address_t) -1;
|
---|
[c377bc50] | 117 |
|
---|
| 118 | if (argc <= 1) {
|
---|
| 119 | print_usage(argv[0]);
|
---|
| 120 | return -1;
|
---|
[2c5cefa] | 121 | }
|
---|
[07b9203e] | 122 |
|
---|
[c377bc50] | 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: {
|
---|
[a12917e] | 141 | int rc = get_device_address(optarg,
|
---|
| 142 | &device_address);
|
---|
[c377bc50] | 143 | if (rc != EOK) {
|
---|
| 144 | return rc;
|
---|
| 145 | }
|
---|
| 146 | break;
|
---|
| 147 | }
|
---|
| 148 |
|
---|
| 149 | case 't':
|
---|
| 150 | case ACTION_HOST_CONTROLLER: {
|
---|
[a12917e] | 151 | int rc = get_host_controller_handle(optarg,
|
---|
| 152 | &hc_handle);
|
---|
[c377bc50] | 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 |
|
---|
[a12917e] | 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 |
|
---|
[c377bc50] | 177 | return 0;
|
---|
[07b9203e] | 178 | }
|
---|
| 179 |
|
---|
| 180 |
|
---|
| 181 | /** @}
|
---|
| 182 | */
|
---|