| 1 | /*
|
|---|
| 2 | * Copyright (c) 2010 Lubos Slovak
|
|---|
| 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 drvusbhid
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /** @file
|
|---|
| 33 | * Descriptor dumping.
|
|---|
| 34 | */
|
|---|
| 35 |
|
|---|
| 36 | #include <usb/classes/hid.h>
|
|---|
| 37 | #include <stdio.h>
|
|---|
| 38 | #include <assert.h>
|
|---|
| 39 |
|
|---|
| 40 | #include "descdump.h"
|
|---|
| 41 |
|
|---|
| 42 | /*----------------------------------------------------------------------------*/
|
|---|
| 43 |
|
|---|
| 44 | #define BYTES_PER_LINE 12
|
|---|
| 45 |
|
|---|
| 46 | /**
|
|---|
| 47 | * Dumps the given buffer in hexadecimal format to standard output.
|
|---|
| 48 | *
|
|---|
| 49 | * @param msg Message to print before the buffer.
|
|---|
| 50 | * @param buffer Buffer to print.
|
|---|
| 51 | * @param length Size of the buffer in bytes.
|
|---|
| 52 | */
|
|---|
| 53 | static void dump_buffer(const char *msg, const uint8_t *buffer, size_t length)
|
|---|
| 54 | {
|
|---|
| 55 | printf("%s\n", msg);
|
|---|
| 56 |
|
|---|
| 57 | size_t i;
|
|---|
| 58 | for (i = 0; i < length; i++) {
|
|---|
| 59 | printf(" 0x%02X", buffer[i]);
|
|---|
| 60 | if (((i > 0) && (((i+1) % BYTES_PER_LINE) == 0))
|
|---|
| 61 | || (i + 1 == length)) {
|
|---|
| 62 | printf("\n");
|
|---|
| 63 | }
|
|---|
| 64 | }
|
|---|
| 65 | }
|
|---|
| 66 |
|
|---|
| 67 | /*----------------------------------------------------------------------------*/
|
|---|
| 68 |
|
|---|
| 69 | #define INDENT " "
|
|---|
| 70 |
|
|---|
| 71 | /**
|
|---|
| 72 | * Print standard configuration descriptor to standard output.
|
|---|
| 73 | *
|
|---|
| 74 | * @param index Index of the descriptor.
|
|---|
| 75 | * @param d Standard configuration descriptor to print.
|
|---|
| 76 | */
|
|---|
| 77 | void dump_standard_configuration_descriptor(
|
|---|
| 78 | int index, const usb_standard_configuration_descriptor_t *d)
|
|---|
| 79 | {
|
|---|
| 80 | bool self_powered = d->attributes & 64;
|
|---|
| 81 | bool remote_wakeup = d->attributes & 32;
|
|---|
| 82 |
|
|---|
| 83 | printf("Standard configuration descriptor #%d\n", index);
|
|---|
| 84 | printf(INDENT "bLength = %d\n", d->length);
|
|---|
| 85 | printf(INDENT "bDescriptorType = 0x%02x\n", d->descriptor_type);
|
|---|
| 86 | printf(INDENT "wTotalLength = %d\n", d->total_length);
|
|---|
| 87 | printf(INDENT "bNumInterfaces = %d\n", d->interface_count);
|
|---|
| 88 | printf(INDENT "bConfigurationValue = %d\n", d->configuration_number);
|
|---|
| 89 | printf(INDENT "iConfiguration = %d\n", d->str_configuration);
|
|---|
| 90 | printf(INDENT "bmAttributes = %d [%s%s%s]\n", d->attributes,
|
|---|
| 91 | self_powered ? "self-powered" : "",
|
|---|
| 92 | (self_powered & remote_wakeup) ? ", " : "",
|
|---|
| 93 | remote_wakeup ? "remote-wakeup" : "");
|
|---|
| 94 | printf(INDENT "MaxPower = %d (%dmA)\n", d->max_power,
|
|---|
| 95 | 2 * d->max_power);
|
|---|
| 96 | // printf(INDENT " = %d\n", d->);
|
|---|
| 97 | }
|
|---|
| 98 |
|
|---|
| 99 | /**
|
|---|
| 100 | * Print standard interface descriptor to standard output.
|
|---|
| 101 | *
|
|---|
| 102 | * @param d Standard interface descriptor to print.
|
|---|
| 103 | */
|
|---|
| 104 | void dump_standard_interface_descriptor(
|
|---|
| 105 | const usb_standard_interface_descriptor_t *d)
|
|---|
| 106 | {
|
|---|
| 107 | printf("Standard interface descriptor\n");
|
|---|
| 108 | printf(INDENT "bLength = %d\n", d->length);
|
|---|
| 109 | printf(INDENT "bDescriptorType = 0x%02x\n", d->descriptor_type);
|
|---|
| 110 | printf(INDENT "bInterfaceNumber = %d\n", d->interface_number);
|
|---|
| 111 | printf(INDENT "bAlternateSetting = %d\n", d->alternate_setting);
|
|---|
| 112 | printf(INDENT "bNumEndpoints = %d\n", d->endpoint_count);
|
|---|
| 113 | printf(INDENT "bInterfaceClass = %d\n", d->interface_class);
|
|---|
| 114 | printf(INDENT "bInterfaceSubClass = %d\n", d->interface_subclass);
|
|---|
| 115 | printf(INDENT "bInterfaceProtocol = %d\n", d->interface_protocol);
|
|---|
| 116 | printf(INDENT "iInterface = %d\n", d->str_interface);
|
|---|
| 117 | }
|
|---|
| 118 |
|
|---|
| 119 | /**
|
|---|
| 120 | * Print standard endpoint descriptor to standard output.
|
|---|
| 121 | *
|
|---|
| 122 | * @param d Standard endpoint descriptor to print.
|
|---|
| 123 | */
|
|---|
| 124 | void dump_standard_endpoint_descriptor(
|
|---|
| 125 | const usb_standard_endpoint_descriptor_t *d)
|
|---|
| 126 | {
|
|---|
| 127 | const char *transfer_type;
|
|---|
| 128 | switch (d->attributes & 3) {
|
|---|
| 129 | case USB_TRANSFER_CONTROL:
|
|---|
| 130 | transfer_type = "control";
|
|---|
| 131 | break;
|
|---|
| 132 | case USB_TRANSFER_ISOCHRONOUS:
|
|---|
| 133 | transfer_type = "isochronous";
|
|---|
| 134 | break;
|
|---|
| 135 | case USB_TRANSFER_BULK:
|
|---|
| 136 | transfer_type = "bulk";
|
|---|
| 137 | break;
|
|---|
| 138 | case USB_TRANSFER_INTERRUPT:
|
|---|
| 139 | transfer_type = "interrupt";
|
|---|
| 140 | break;
|
|---|
| 141 | }
|
|---|
| 142 |
|
|---|
| 143 | printf("Standard endpoint descriptor\n");
|
|---|
| 144 | printf(INDENT "bLength = %d\n", d->length);
|
|---|
| 145 | printf(INDENT "bDescriptorType = 0x%02x\n", d->descriptor_type);
|
|---|
| 146 | printf(INDENT "bmAttributes = %d [%s]\n", d->attributes, transfer_type);
|
|---|
| 147 | printf(INDENT "wMaxPacketSize = %d\n", d->max_packet_size);
|
|---|
| 148 | printf(INDENT "bInterval = %d\n", d->poll_interval);
|
|---|
| 149 | }
|
|---|
| 150 |
|
|---|
| 151 | /**
|
|---|
| 152 | * Print standard HID descriptor to standard output.
|
|---|
| 153 | *
|
|---|
| 154 | * @param d Standard HID descriptor to print.
|
|---|
| 155 | */
|
|---|
| 156 | void dump_standard_hid_descriptor_header(
|
|---|
| 157 | const usb_standard_hid_descriptor_t *d)
|
|---|
| 158 | {
|
|---|
| 159 | printf("Standard HID descriptor\n");
|
|---|
| 160 | printf(INDENT "bLength = %d\n", d->length);
|
|---|
| 161 | printf(INDENT "bDescriptorType = 0x%02x\n", d->descriptor_type);
|
|---|
| 162 | printf(INDENT "bcdHID = %d\n", d->spec_release);
|
|---|
| 163 | printf(INDENT "bCountryCode = %d\n", d->country_code);
|
|---|
| 164 | printf(INDENT "bNumDescriptors = %d\n", d->class_desc_count);
|
|---|
| 165 | printf(INDENT "bDescriptorType = %d\n", d->report_desc_info.type);
|
|---|
| 166 | printf(INDENT "wDescriptorLength = %d\n", d->report_desc_info.length);
|
|---|
| 167 | }
|
|---|
| 168 |
|
|---|
| 169 | /**
|
|---|
| 170 | * Print HID class-specific descriptor header (type and length) to standard
|
|---|
| 171 | * output.
|
|---|
| 172 | *
|
|---|
| 173 | * @param d HID class-specific descriptor header to print.
|
|---|
| 174 | */
|
|---|
| 175 | void dump_standard_hid_class_descriptor_info(
|
|---|
| 176 | const usb_standard_hid_class_descriptor_info_t *d)
|
|---|
| 177 | {
|
|---|
| 178 | printf(INDENT "bDescriptorType = %d\n", d->type);
|
|---|
| 179 | printf(INDENT "wDescriptorLength = %d\n", d->length);
|
|---|
| 180 | }
|
|---|
| 181 |
|
|---|
| 182 | /**
|
|---|
| 183 | * Print HID class-specific descriptor (without the header) to standard output.
|
|---|
| 184 | *
|
|---|
| 185 | * @param index Index of the descriptor.
|
|---|
| 186 | * @param type Type of the HID class-specific descriptor (Report or Physical).
|
|---|
| 187 | * @param d HID class descriptor to print.
|
|---|
| 188 | * @param size Size of the descriptor in bytes.
|
|---|
| 189 | */
|
|---|
| 190 | void dump_hid_class_descriptor(int index, uint8_t type,
|
|---|
| 191 | const uint8_t *d, size_t size )
|
|---|
| 192 | {
|
|---|
| 193 | printf("Class-specific descriptor #%d (type: %u)\n", index, type);
|
|---|
| 194 | assert(d != NULL);
|
|---|
| 195 | dump_buffer("", d, size);
|
|---|
| 196 | }
|
|---|
| 197 |
|
|---|
| 198 | /**
|
|---|
| 199 | * @}
|
|---|
| 200 | */
|
|---|
| 201 |
|
|---|