source: mainline/uspace/app/usbinfo/main.c@ 99ea659c

lfn serial ticket/834-toolchain-update topic/fix-logger-deadlock topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 99ea659c was 99ea659c, checked in by Vojtech Horky <vojtechhorky@…>, 15 years ago

App `usbinfo' has default HC set

  • Property mode set to 100644
File size: 5.0 KB
Line 
1/*
2 * Copyright (c) 2010 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 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>
42#include <devman.h>
43#include <usb/usbdrv.h>
44#include "usbinfo.h"
45
46#define DEFAULT_HOST_CONTROLLER_PATH "/virt/usbhc"
47
48static void print_usage(char *app_name)
49{
50 printf(NAME ": query USB devices for descriptors\n\n");
51 printf("Usage: %s /path/to/hc usb-address\n where\n", app_name);
52 printf(" /path/to/hc Devman path to USB host controller " \
53 "(use `-' for\n");
54 printf(" default HC at `%s').\n",
55 DEFAULT_HOST_CONTROLLER_PATH);
56 printf(" usb-address USB address of device to be queried\n");
57 printf("\n");
58}
59
60static int connect_to_hc(const char *path)
61{
62 int rc;
63 devman_handle_t handle;
64
65 rc = devman_device_get_handle(path, &handle, 0);
66 if (rc != EOK) {
67 return rc;
68 }
69
70 int phone = devman_device_connect(handle, 0);
71
72 return phone;
73}
74
75int main(int argc, char *argv[])
76{
77 if (argc != 3) {
78 print_usage(argv[0]);
79 return EINVAL;
80 }
81
82 char *hc_path = argv[1];
83 long int address_long = strtol(argv[2], NULL, 0);
84
85 /*
86 * Connect to given host controller driver.
87 */
88 if (str_cmp(hc_path, "-") == 0) {
89 hc_path = (char *) DEFAULT_HOST_CONTROLLER_PATH;
90 }
91 int hc_phone = connect_to_hc(hc_path);
92 if (hc_phone < 0) {
93 fprintf(stderr,
94 NAME ": unable to connect to HC at `%s': %s.\n",
95 hc_path, str_error(hc_phone));
96 return hc_phone;
97 }
98
99 /*
100 * Verify address is okay.
101 */
102 usb_address_t address = (usb_address_t) address_long;
103 if ((address < 0) || (address >= USB11_ADDRESS_MAX)) {
104 fprintf(stderr, NAME ": USB address out of range.\n");
105 return ERANGE;
106 }
107
108 /*
109 * Now, learn information about the device.
110 */
111 int rc;
112
113 /*
114 * Get device descriptor and dump it.
115 */
116 usb_standard_device_descriptor_t device_descriptor;
117 usb_dprintf(NAME, 1,
118 "usb_drv_req_get_device_descriptor(%d, %d, %p)\n",
119 hc_phone, (int) address, &device_descriptor);
120
121 rc = usb_drv_req_get_device_descriptor(hc_phone, address,
122 &device_descriptor);
123 if (rc != EOK) {
124 fprintf(stderr,
125 NAME ": failed to fetch standard device descriptor: %s.\n",
126 str_error(rc));
127 return rc;
128 }
129 dump_standard_device_descriptor(&device_descriptor);
130
131 /*
132 * Get first configuration descriptor and dump it.
133 */
134 usb_standard_configuration_descriptor_t config_descriptor;
135 int config_index = 0;
136 usb_dprintf(NAME, 1,
137 "usb_drv_req_get_bare_configuration_descriptor(%d, %d, %d, %p)\n",
138 hc_phone, (int) address, config_index, &config_descriptor);
139
140 rc = usb_drv_req_get_bare_configuration_descriptor(hc_phone, address,
141 config_index, &config_descriptor );
142 if (rc != EOK) {
143 fprintf(stderr,
144 NAME ": failed to fetch standard configuration descriptor: %s.\n",
145 str_error(rc));
146 return rc;
147 }
148 dump_standard_configuration_descriptor(config_index,
149 &config_descriptor);
150
151 void *full_config_descriptor = malloc(config_descriptor.total_length);
152 usb_dprintf(NAME, 1,
153 "usb_drv_req_get_full_configuration_descriptor(%d, %d, %d, %p, %zu)\n",
154 hc_phone, (int) address, config_index,
155 full_config_descriptor, config_descriptor.total_length);
156
157 rc = usb_drv_req_get_full_configuration_descriptor(hc_phone, address,
158 config_index,
159 full_config_descriptor, config_descriptor.total_length, NULL);
160 if (rc != EOK) {
161 fprintf(stderr,
162 NAME ": failed to fetch full configuration descriptor: %s.\n",
163 str_error(rc));
164 return rc;
165 }
166 dump_buffer("Full configuration descriptor:",
167 full_config_descriptor, config_descriptor.total_length);
168
169 return EOK;
170}
171
172
173/** @}
174 */
Note: See TracBrowser for help on using the repository browser.