| 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 <usb/hid/hidparser.h> | 
|---|
| 41 | #include <errno.h> | 
|---|
| 42 | #include "usbinfo.h" | 
|---|
| 43 |  | 
|---|
| 44 | #define BYTES_PER_LINE 20 | 
|---|
| 45 |  | 
|---|
| 46 | typedef enum { | 
|---|
| 47 | HID_DUMP_RAW, | 
|---|
| 48 | HID_DUMP_USAGES | 
|---|
| 49 | } hid_dump_type_t; | 
|---|
| 50 |  | 
|---|
| 51 | typedef struct { | 
|---|
| 52 | usbinfo_device_t *dev; | 
|---|
| 53 | hid_dump_type_t dump_type; | 
|---|
| 54 | usb_standard_interface_descriptor_t *last_iface; | 
|---|
| 55 | } descriptor_walk_context_t; | 
|---|
| 56 |  | 
|---|
| 57 | static bool is_descriptor_kind(uint8_t *d, usb_descriptor_type_t t) | 
|---|
| 58 | { | 
|---|
| 59 | if (d == NULL) { | 
|---|
| 60 | return false; | 
|---|
| 61 | } | 
|---|
| 62 | uint8_t size = d[0]; | 
|---|
| 63 | if (size <= 1) { | 
|---|
| 64 | return false; | 
|---|
| 65 | } | 
|---|
| 66 | uint8_t type = d[1]; | 
|---|
| 67 | return type == t; | 
|---|
| 68 | } | 
|---|
| 69 |  | 
|---|
| 70 | /** Dumps HID report in raw format. | 
|---|
| 71 | * | 
|---|
| 72 | * @param iface_no USB interface the report belongs to. | 
|---|
| 73 | * @param report Report descriptor. | 
|---|
| 74 | * @param size Size of the @p report in bytes. | 
|---|
| 75 | */ | 
|---|
| 76 | static void dump_hid_report_raw(int iface_no, uint8_t *report, size_t size) | 
|---|
| 77 | { | 
|---|
| 78 | printf("%sHID report descriptor for interface %d", get_indent(0), | 
|---|
| 79 | iface_no); | 
|---|
| 80 | for (size_t i = 0; i < size; i++) { | 
|---|
| 81 | size_t line_idx = i % BYTES_PER_LINE; | 
|---|
| 82 | if (line_idx == 0) { | 
|---|
| 83 | printf("\n%s", get_indent(1)); | 
|---|
| 84 | } | 
|---|
| 85 | printf("%02X", (int) report[i]); | 
|---|
| 86 | if (line_idx + 1 < BYTES_PER_LINE) { | 
|---|
| 87 | printf(" "); | 
|---|
| 88 | } | 
|---|
| 89 | } | 
|---|
| 90 | printf("\n"); | 
|---|
| 91 | } | 
|---|
| 92 |  | 
|---|
| 93 | /** Dumps usages in HID report. | 
|---|
| 94 | * | 
|---|
| 95 | * @param iface_no USB interface the report belongs to. | 
|---|
| 96 | * @param report Parsed report descriptor. | 
|---|
| 97 | */ | 
|---|
| 98 | static void dump_hid_report_usages(int iface_no, usb_hid_report_t *report) | 
|---|
| 99 | { | 
|---|
| 100 | printf("%sParsed HID report descriptor for interface %d\n", | 
|---|
| 101 | get_indent(0), iface_no); | 
|---|
| 102 | list_foreach(report->reports, report_it) { | 
|---|
| 103 | usb_hid_report_description_t *description = list_get_instance( | 
|---|
| 104 | report_it, usb_hid_report_description_t, reports_link); | 
|---|
| 105 | printf("%sReport %d (type %d)\n", get_indent(1), | 
|---|
| 106 | (int) description->report_id, | 
|---|
| 107 | (int) description->type); | 
|---|
| 108 | list_foreach(description->report_items, item_it) { | 
|---|
| 109 | usb_hid_report_field_t *field = list_get_instance( | 
|---|
| 110 | item_it, usb_hid_report_field_t, ritems_link); | 
|---|
| 111 | printf("%sUsage page = 0x%04x    Usage = 0x%04x\n", | 
|---|
| 112 | get_indent(2), | 
|---|
| 113 | (int) field->usage_page, (int) field->usage); | 
|---|
| 114 | } | 
|---|
| 115 | } | 
|---|
| 116 | } | 
|---|
| 117 |  | 
|---|
| 118 | /** Retrieves HID report from given USB device and dumps it. | 
|---|
| 119 | * | 
|---|
| 120 | * @param dump_type In which format to dump the report. | 
|---|
| 121 | * @param ctrl_pipe Default control pipe to the device. | 
|---|
| 122 | * @param iface_no Interface number. | 
|---|
| 123 | * @param report_size Size of the report descriptor. | 
|---|
| 124 | */ | 
|---|
| 125 | static void retrieve_and_dump_hid_report(hid_dump_type_t dump_type, | 
|---|
| 126 | usb_pipe_t *ctrl_pipe, uint8_t iface_no, size_t report_size) | 
|---|
| 127 | { | 
|---|
| 128 | assert(report_size > 0); | 
|---|
| 129 |  | 
|---|
| 130 | uint8_t *raw_report = malloc(report_size); | 
|---|
| 131 | if (raw_report == NULL) { | 
|---|
| 132 | usb_log_warning( | 
|---|
| 133 | "Failed to allocate %zuB, skipping interface %d.\n", | 
|---|
| 134 | report_size, (int) iface_no); | 
|---|
| 135 | return; | 
|---|
| 136 | } | 
|---|
| 137 |  | 
|---|
| 138 | size_t actual_report_size; | 
|---|
| 139 | int rc = usb_request_get_descriptor(ctrl_pipe, | 
|---|
| 140 | USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_INTERFACE, | 
|---|
| 141 | USB_DESCTYPE_HID_REPORT, 0, iface_no, | 
|---|
| 142 | raw_report, report_size, &actual_report_size); | 
|---|
| 143 | if (rc != EOK) { | 
|---|
| 144 | usb_log_error("Failed to retrieve HID report descriptor: %s.\n", | 
|---|
| 145 | str_error(rc)); | 
|---|
| 146 | free(raw_report); | 
|---|
| 147 | return; | 
|---|
| 148 | } | 
|---|
| 149 |  | 
|---|
| 150 | usb_hid_report_t report; | 
|---|
| 151 | rc = usb_hid_parse_report_descriptor(&report, raw_report, report_size); | 
|---|
| 152 | if (rc != EOK) { | 
|---|
| 153 | usb_log_error("Failed to part report descriptor: %s.\n", | 
|---|
| 154 | str_error(rc)); | 
|---|
| 155 | } | 
|---|
| 156 |  | 
|---|
| 157 | switch (dump_type) { | 
|---|
| 158 | case HID_DUMP_RAW: | 
|---|
| 159 | dump_hid_report_raw(iface_no, raw_report, report_size); | 
|---|
| 160 | break; | 
|---|
| 161 | case HID_DUMP_USAGES: | 
|---|
| 162 | dump_hid_report_usages(iface_no, &report); | 
|---|
| 163 | break; | 
|---|
| 164 | default: | 
|---|
| 165 | assert(false && "unreachable code apparently reached"); | 
|---|
| 166 | } | 
|---|
| 167 |  | 
|---|
| 168 | free(raw_report); | 
|---|
| 169 | usb_hid_free_report(&report); | 
|---|
| 170 | } | 
|---|
| 171 |  | 
|---|
| 172 | /** Callback for walking descriptor tree. | 
|---|
| 173 | * This callback remembers current interface and dumps HID report after | 
|---|
| 174 | * encountering HID descriptor. | 
|---|
| 175 | * It dumps only the first report and it expects it to be a normal | 
|---|
| 176 | * report, not a physical one. | 
|---|
| 177 | * | 
|---|
| 178 | * @param raw_descriptor Descriptor as a byte array. | 
|---|
| 179 | * @param depth Descriptor tree depth (currently ignored). | 
|---|
| 180 | * @param arg Custom argument, passed as descriptor_walk_context_t. | 
|---|
| 181 | */ | 
|---|
| 182 | static void descriptor_walk_callback(uint8_t *raw_descriptor, | 
|---|
| 183 | size_t depth, void *arg) | 
|---|
| 184 | { | 
|---|
| 185 | descriptor_walk_context_t *context = (descriptor_walk_context_t *) arg; | 
|---|
| 186 |  | 
|---|
| 187 | if (is_descriptor_kind(raw_descriptor, USB_DESCTYPE_INTERFACE)) { | 
|---|
| 188 | context->last_iface | 
|---|
| 189 | = (usb_standard_interface_descriptor_t *) raw_descriptor; | 
|---|
| 190 | return; | 
|---|
| 191 | } | 
|---|
| 192 |  | 
|---|
| 193 | if (!is_descriptor_kind(raw_descriptor, USB_DESCTYPE_HID)) { | 
|---|
| 194 | return; | 
|---|
| 195 | } | 
|---|
| 196 |  | 
|---|
| 197 | if (context->last_iface == NULL) { | 
|---|
| 198 | return; | 
|---|
| 199 | } | 
|---|
| 200 |  | 
|---|
| 201 | usb_standard_hid_descriptor_t *hid_descr | 
|---|
| 202 | = (usb_standard_hid_descriptor_t *) raw_descriptor; | 
|---|
| 203 |  | 
|---|
| 204 | if (hid_descr->report_desc_info.type != USB_DESCTYPE_HID_REPORT) { | 
|---|
| 205 | return; | 
|---|
| 206 | } | 
|---|
| 207 |  | 
|---|
| 208 | size_t report_size = hid_descr->report_desc_info.length; | 
|---|
| 209 |  | 
|---|
| 210 | if (report_size == 0) { | 
|---|
| 211 | return; | 
|---|
| 212 | } | 
|---|
| 213 |  | 
|---|
| 214 | retrieve_and_dump_hid_report(context->dump_type, | 
|---|
| 215 | &context->dev->ctrl_pipe, context->last_iface->interface_number, | 
|---|
| 216 | report_size); | 
|---|
| 217 | } | 
|---|
| 218 |  | 
|---|
| 219 |  | 
|---|
| 220 | void dump_hidreport_raw(usbinfo_device_t *dev) | 
|---|
| 221 | { | 
|---|
| 222 | descriptor_walk_context_t context = { | 
|---|
| 223 | .dev = dev, | 
|---|
| 224 | .dump_type = HID_DUMP_RAW, | 
|---|
| 225 | .last_iface = NULL | 
|---|
| 226 | }; | 
|---|
| 227 |  | 
|---|
| 228 | usb_dp_walk_simple(dev->full_configuration_descriptor, | 
|---|
| 229 | dev->full_configuration_descriptor_size, | 
|---|
| 230 | usb_dp_standard_descriptor_nesting, | 
|---|
| 231 | descriptor_walk_callback, &context); | 
|---|
| 232 | } | 
|---|
| 233 |  | 
|---|
| 234 | void dump_hidreport_usages(usbinfo_device_t *dev) | 
|---|
| 235 | { | 
|---|
| 236 | descriptor_walk_context_t context = { | 
|---|
| 237 | .dev = dev, | 
|---|
| 238 | .dump_type = HID_DUMP_USAGES, | 
|---|
| 239 | .last_iface = NULL | 
|---|
| 240 | }; | 
|---|
| 241 |  | 
|---|
| 242 | usb_dp_walk_simple(dev->full_configuration_descriptor, | 
|---|
| 243 | dev->full_configuration_descriptor_size, | 
|---|
| 244 | usb_dp_standard_descriptor_nesting, | 
|---|
| 245 | descriptor_walk_callback, &context); | 
|---|
| 246 | } | 
|---|
| 247 |  | 
|---|
| 248 | /** @} | 
|---|
| 249 | */ | 
|---|