source: mainline/uspace/app/usbinfo/main.c@ 2c5cefa

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

Add retrieval of configuration descriptor

The `usbinfo' app displays configuration descriptor as well.

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