| 1 | /*
|
|---|
| 2 | * Copyright (c) 2011 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 usbinfo
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /**
|
|---|
| 33 | * @file
|
|---|
| 34 | * Dumping of HID-related properties.
|
|---|
| 35 | */
|
|---|
| 36 | #include <stdio.h>
|
|---|
| 37 | #include <str_error.h>
|
|---|
| 38 | #include <usb/classes/classes.h>
|
|---|
| 39 | #include <usb/dev/request.h>
|
|---|
| 40 | #include <errno.h>
|
|---|
| 41 | #include "usbinfo.h"
|
|---|
| 42 |
|
|---|
| 43 | #define BYTES_PER_LINE 20
|
|---|
| 44 |
|
|---|
| 45 | typedef struct {
|
|---|
| 46 | usbinfo_device_t *dev;
|
|---|
| 47 | usb_standard_interface_descriptor_t *last_iface;
|
|---|
| 48 | } descriptor_walk_context_t;
|
|---|
| 49 |
|
|---|
| 50 | static bool is_descriptor_kind(uint8_t *d, usb_descriptor_type_t t)
|
|---|
| 51 | {
|
|---|
| 52 | if (d == NULL) {
|
|---|
| 53 | return false;
|
|---|
| 54 | }
|
|---|
| 55 | uint8_t size = d[0];
|
|---|
| 56 | if (size <= 1) {
|
|---|
| 57 | return false;
|
|---|
| 58 | }
|
|---|
| 59 | uint8_t type = d[1];
|
|---|
| 60 | return type == t;
|
|---|
| 61 | }
|
|---|
| 62 |
|
|---|
| 63 | /** Dumps HID report in raw format.
|
|---|
| 64 | *
|
|---|
| 65 | * @param iface_no USB interface the report belongs to.
|
|---|
| 66 | * @param report Report descriptor.
|
|---|
| 67 | * @param size Size of the @p report in bytes.
|
|---|
| 68 | */
|
|---|
| 69 | static void dump_hid_report_raw(int iface_no, uint8_t *report, size_t size)
|
|---|
| 70 | {
|
|---|
| 71 | printf("%sHID report descriptor for interface %d", get_indent(0),
|
|---|
| 72 | iface_no);
|
|---|
| 73 | for (size_t i = 0; i < size; i++) {
|
|---|
| 74 | size_t line_idx = i % BYTES_PER_LINE;
|
|---|
| 75 | if (line_idx == 0) {
|
|---|
| 76 | printf("\n%s", get_indent(1));
|
|---|
| 77 | }
|
|---|
| 78 | printf("%02X", (int) report[i]);
|
|---|
| 79 | if (line_idx + 1 < BYTES_PER_LINE) {
|
|---|
| 80 | printf(" ");
|
|---|
| 81 | }
|
|---|
| 82 | }
|
|---|
| 83 | printf("\n");
|
|---|
| 84 | }
|
|---|
| 85 |
|
|---|
| 86 | /** Retrieves HID report from given USB device and dumps it.
|
|---|
| 87 | *
|
|---|
| 88 | * @param ctrl_pipe Default control pipe to the device.
|
|---|
| 89 | * @param iface_no Interface number.
|
|---|
| 90 | * @param report_size Size of the report descriptor.
|
|---|
| 91 | */
|
|---|
| 92 | static void retrieve_and_dump_hid_report(usb_pipe_t *ctrl_pipe,
|
|---|
| 93 | uint8_t iface_no, size_t report_size)
|
|---|
| 94 | {
|
|---|
| 95 | assert(report_size > 0);
|
|---|
| 96 |
|
|---|
| 97 | uint8_t *raw_report = malloc(report_size);
|
|---|
| 98 | if (raw_report == NULL) {
|
|---|
| 99 | usb_log_warning(
|
|---|
| 100 | "Failed to allocate %zuB, skipping interface %d.\n",
|
|---|
| 101 | report_size, (int) iface_no);
|
|---|
| 102 | return;
|
|---|
| 103 | }
|
|---|
| 104 |
|
|---|
| 105 | size_t actual_report_size;
|
|---|
| 106 | int rc = usb_request_get_descriptor(ctrl_pipe,
|
|---|
| 107 | USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_INTERFACE,
|
|---|
| 108 | USB_DESCTYPE_HID_REPORT, 0, iface_no,
|
|---|
| 109 | raw_report, report_size, &actual_report_size);
|
|---|
| 110 | if (rc != EOK) {
|
|---|
| 111 | usb_log_error("Failed to retrieve HID report descriptor: %s.\n",
|
|---|
| 112 | str_error(rc));
|
|---|
| 113 | free(raw_report);
|
|---|
| 114 | return;
|
|---|
| 115 | }
|
|---|
| 116 |
|
|---|
| 117 | dump_hid_report_raw(iface_no, raw_report, report_size);
|
|---|
| 118 |
|
|---|
| 119 | free(raw_report);
|
|---|
| 120 | }
|
|---|
| 121 |
|
|---|
| 122 | /** Callback for walking descriptor tree.
|
|---|
| 123 | * This callback remembers current interface and dumps HID report after
|
|---|
| 124 | * encountering HID descriptor.
|
|---|
| 125 | * It dumps only the first report and it expects it to be a normal
|
|---|
| 126 | * report, not a physical one.
|
|---|
| 127 | *
|
|---|
| 128 | * @param raw_descriptor Descriptor as a byte array.
|
|---|
| 129 | * @param depth Descriptor tree depth (currently ignored).
|
|---|
| 130 | * @param arg Custom argument, passed as descriptor_walk_context_t.
|
|---|
| 131 | */
|
|---|
| 132 | static void descriptor_walk_callback(uint8_t *raw_descriptor,
|
|---|
| 133 | size_t depth, void *arg)
|
|---|
| 134 | {
|
|---|
| 135 | descriptor_walk_context_t *context = (descriptor_walk_context_t *) arg;
|
|---|
| 136 |
|
|---|
| 137 | if (is_descriptor_kind(raw_descriptor, USB_DESCTYPE_INTERFACE)) {
|
|---|
| 138 | context->last_iface
|
|---|
| 139 | = (usb_standard_interface_descriptor_t *) raw_descriptor;
|
|---|
| 140 | return;
|
|---|
| 141 | }
|
|---|
| 142 |
|
|---|
| 143 | if (!is_descriptor_kind(raw_descriptor, USB_DESCTYPE_HID)) {
|
|---|
| 144 | return;
|
|---|
| 145 | }
|
|---|
| 146 |
|
|---|
| 147 | if (context->last_iface == NULL) {
|
|---|
| 148 | return;
|
|---|
| 149 | }
|
|---|
| 150 |
|
|---|
| 151 | usb_standard_hid_descriptor_t *hid_descr
|
|---|
| 152 | = (usb_standard_hid_descriptor_t *) raw_descriptor;
|
|---|
| 153 |
|
|---|
| 154 | if (hid_descr->report_desc_info.type != USB_DESCTYPE_HID_REPORT) {
|
|---|
| 155 | return;
|
|---|
| 156 | }
|
|---|
| 157 |
|
|---|
| 158 | size_t report_size = hid_descr->report_desc_info.length;
|
|---|
| 159 |
|
|---|
| 160 | if (report_size == 0) {
|
|---|
| 161 | return;
|
|---|
| 162 | }
|
|---|
| 163 |
|
|---|
| 164 | retrieve_and_dump_hid_report(&context->dev->ctrl_pipe,
|
|---|
| 165 | context->last_iface->interface_number, report_size);
|
|---|
| 166 | }
|
|---|
| 167 |
|
|---|
| 168 |
|
|---|
| 169 | void dump_hidreport(usbinfo_device_t *dev)
|
|---|
| 170 | {
|
|---|
| 171 | descriptor_walk_context_t context = {
|
|---|
| 172 | .dev = dev,
|
|---|
| 173 | .last_iface = NULL
|
|---|
| 174 | };
|
|---|
| 175 |
|
|---|
| 176 | usb_dp_walk_simple(dev->full_configuration_descriptor,
|
|---|
| 177 | dev->full_configuration_descriptor_size,
|
|---|
| 178 | usb_dp_standard_descriptor_nesting,
|
|---|
| 179 | descriptor_walk_callback, &context);
|
|---|
| 180 | }
|
|---|
| 181 |
|
|---|
| 182 | /** @}
|
|---|
| 183 | */
|
|---|