[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 |
|
---|
| 29 | /** @addtogroup usb
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /**
|
---|
| 33 | * @file
|
---|
| 34 | * @brief 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>
|
---|
[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 |
|
---|
[c377bc50] | 79 | static int set_new_host_controller(int *phone, const char *path)
|
---|
[07b9203e] | 80 | {
|
---|
| 81 | int rc;
|
---|
[c377bc50] | 82 | int tmp_phone;
|
---|
| 83 |
|
---|
| 84 | if (path[0] != '/') {
|
---|
| 85 | int hc_class_index = (int) strtol(path, NULL, 10);
|
---|
| 86 | char *dev_path;
|
---|
| 87 | rc = asprintf(&dev_path, "class/usbhc\\%d", hc_class_index);
|
---|
| 88 | if (rc < 0) {
|
---|
| 89 | internal_error(rc);
|
---|
| 90 | return rc;
|
---|
| 91 | }
|
---|
| 92 | devmap_handle_t handle;
|
---|
| 93 | rc = devmap_device_get_handle(dev_path, &handle, 0);
|
---|
| 94 | if (rc < 0) {
|
---|
| 95 | fprintf(stderr,
|
---|
| 96 | NAME ": failed getting handle of `devman://%s'.\n",
|
---|
| 97 | dev_path);
|
---|
| 98 | free(dev_path);
|
---|
| 99 | return rc;
|
---|
| 100 | }
|
---|
| 101 | tmp_phone = devmap_device_connect(handle, 0);
|
---|
| 102 | if (tmp_phone < 0) {
|
---|
| 103 | fprintf(stderr,
|
---|
| 104 | NAME ": could not connect to `%s'.\n",
|
---|
| 105 | dev_path);
|
---|
| 106 | free(dev_path);
|
---|
| 107 | return tmp_phone;
|
---|
| 108 | }
|
---|
| 109 | free(dev_path);
|
---|
| 110 | } else {
|
---|
| 111 | devman_handle_t handle;
|
---|
| 112 | rc = devman_device_get_handle(path, &handle, 0);
|
---|
| 113 | if (rc != EOK) {
|
---|
| 114 | fprintf(stderr,
|
---|
| 115 | NAME ": failed getting handle of `devmap::/%s'.\n",
|
---|
| 116 | path);
|
---|
| 117 | return rc;
|
---|
| 118 | }
|
---|
| 119 | tmp_phone = devman_device_connect(handle, 0);
|
---|
| 120 | if (tmp_phone < 0) {
|
---|
| 121 | fprintf(stderr,
|
---|
| 122 | NAME ": could not connect to `%s'.\n",
|
---|
| 123 | path);
|
---|
| 124 | return tmp_phone;
|
---|
| 125 | }
|
---|
[07b9203e] | 126 | }
|
---|
| 127 |
|
---|
[c377bc50] | 128 | *phone = tmp_phone;
|
---|
[07b9203e] | 129 |
|
---|
[c377bc50] | 130 | return EOK;
|
---|
[07b9203e] | 131 | }
|
---|
| 132 |
|
---|
[c377bc50] | 133 | static int connect_with_address(int hc_phone, const char *str_address)
|
---|
[07b9203e] | 134 | {
|
---|
[c377bc50] | 135 | usb_address_t address = (usb_address_t) strtol(str_address, NULL, 0);
|
---|
| 136 | if ((address < 0) || (address >= USB11_ADDRESS_MAX)) {
|
---|
| 137 | fprintf(stderr, NAME ": USB address out of range.\n");
|
---|
| 138 | return ERANGE;
|
---|
[07b9203e] | 139 | }
|
---|
| 140 |
|
---|
| 141 | if (hc_phone < 0) {
|
---|
[c377bc50] | 142 | fprintf(stderr, NAME ": no active host controller.\n");
|
---|
| 143 | return ENOENT;
|
---|
[07b9203e] | 144 | }
|
---|
| 145 |
|
---|
[c377bc50] | 146 | return dump_device(hc_phone, address);
|
---|
| 147 | }
|
---|
[07b9203e] | 148 |
|
---|
| 149 |
|
---|
[c377bc50] | 150 | int main(int argc, char *argv[])
|
---|
| 151 | {
|
---|
| 152 | int hc_phone = -1;
|
---|
| 153 |
|
---|
| 154 | if (argc <= 1) {
|
---|
| 155 | print_usage(argv[0]);
|
---|
| 156 | return -1;
|
---|
[2c5cefa] | 157 | }
|
---|
[07b9203e] | 158 |
|
---|
[c377bc50] | 159 | int i;
|
---|
| 160 | do {
|
---|
| 161 | i = getopt_long(argc, argv, short_options, long_options, NULL);
|
---|
| 162 | switch (i) {
|
---|
| 163 | case -1:
|
---|
| 164 | break;
|
---|
| 165 |
|
---|
| 166 | case '?':
|
---|
| 167 | print_usage(argv[0]);
|
---|
| 168 | return -1;
|
---|
| 169 |
|
---|
| 170 | case 'h':
|
---|
| 171 | case ACTION_HELP:
|
---|
| 172 | print_usage(argv[0]);
|
---|
| 173 | return 0;
|
---|
| 174 |
|
---|
| 175 | case 'a':
|
---|
| 176 | case ACTION_DEVICE_ADDRESS: {
|
---|
| 177 | int rc = connect_with_address(hc_phone, optarg);
|
---|
| 178 | if (rc != EOK) {
|
---|
| 179 | return rc;
|
---|
| 180 | }
|
---|
| 181 | break;
|
---|
| 182 | }
|
---|
| 183 |
|
---|
| 184 | case 't':
|
---|
| 185 | case ACTION_HOST_CONTROLLER: {
|
---|
| 186 | int rc = set_new_host_controller(&hc_phone,
|
---|
| 187 | optarg);
|
---|
| 188 | if (rc != EOK) {
|
---|
| 189 | return rc;
|
---|
| 190 | }
|
---|
| 191 | break;
|
---|
| 192 | }
|
---|
| 193 |
|
---|
| 194 | case 'd':
|
---|
| 195 | case ACTION_DEVICE:
|
---|
| 196 | break;
|
---|
| 197 |
|
---|
| 198 | default:
|
---|
| 199 | break;
|
---|
| 200 | }
|
---|
| 201 |
|
---|
| 202 | } while (i != -1);
|
---|
| 203 |
|
---|
| 204 | return 0;
|
---|
[07b9203e] | 205 | }
|
---|
| 206 |
|
---|
| 207 |
|
---|
| 208 | /** @}
|
---|
| 209 | */
|
---|