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