dump.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2011 Vojtech Horky
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  *
00009  * - Redistributions of source code must retain the above copyright
00010  *   notice, this list of conditions and the following disclaimer.
00011  * - Redistributions in binary form must reproduce the above copyright
00012  *   notice, this list of conditions and the following disclaimer in the
00013  *   documentation and/or other materials provided with the distribution.
00014  * - The name of the author may not be used to endorse or promote products
00015  *   derived from this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
00018  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00019  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
00020  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
00021  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
00022  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00023  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00024  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00025  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
00026  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00027  */
00028 
00035 #include <adt/list.h>
00036 #include <fibril_synch.h>
00037 #include <errno.h>
00038 #include <stdlib.h>
00039 #include <stdio.h>
00040 #include <usb/debug.h>
00041 #include <usb/descriptor.h>
00042 #include <usb/classes/classes.h>
00043 
00045 typedef struct {
00047         int id;
00049         void (*dump)(FILE *, const char *, const char *,
00050             const uint8_t *, size_t);
00051 } descriptor_dump_t;
00052 
00053 static void usb_dump_descriptor_device(FILE *, const char *, const char *,
00054     const uint8_t *, size_t);
00055 static void usb_dump_descriptor_configuration(FILE *, const char *, const char *,
00056     const uint8_t *, size_t);
00057 static void usb_dump_descriptor_interface(FILE *, const char *, const char *,
00058     const uint8_t *, size_t);
00059 static void usb_dump_descriptor_string(FILE *, const char *, const char *,
00060     const uint8_t *, size_t);
00061 static void usb_dump_descriptor_endpoint(FILE *, const char *, const char *,
00062     const uint8_t *, size_t);
00063 static void usb_dump_descriptor_hid(FILE *, const char *, const char *,
00064     const uint8_t *, size_t);
00065 static void usb_dump_descriptor_hub(FILE *, const char *, const char *,
00066     const uint8_t *, size_t);
00067 static void usb_dump_descriptor_generic(FILE *, const char *, const char *,
00068     const uint8_t *, size_t);
00069 
00071 static descriptor_dump_t descriptor_dumpers[] = {
00072         { USB_DESCTYPE_DEVICE, usb_dump_descriptor_device },
00073         { USB_DESCTYPE_CONFIGURATION, usb_dump_descriptor_configuration },
00074         { USB_DESCTYPE_STRING, usb_dump_descriptor_string },
00075         { USB_DESCTYPE_INTERFACE, usb_dump_descriptor_interface },
00076         { USB_DESCTYPE_ENDPOINT, usb_dump_descriptor_endpoint },
00077         { USB_DESCTYPE_HID, usb_dump_descriptor_hid },
00078         { USB_DESCTYPE_HUB, usb_dump_descriptor_hub },
00079         { -1, usb_dump_descriptor_generic },
00080         { -1, NULL }
00081 };
00082 
00095 void usb_dump_standard_descriptor(FILE *output,
00096     const char *line_prefix, const char *line_suffix,
00097     const uint8_t *descriptor, size_t descriptor_length)
00098 {
00099         if (descriptor_length < 2) {
00100                 return;
00101         }
00102         int type = descriptor[1];
00103 
00104         descriptor_dump_t *dumper = descriptor_dumpers;
00105         while (dumper->dump != NULL) {
00106                 if ((dumper->id == type) || (dumper->id < 0)) {
00107                         dumper->dump(output, line_prefix, line_suffix,
00108                             descriptor, descriptor_length);
00109                         return;
00110                 }
00111                 dumper++;
00112         }
00113 }
00114 
00122 #define PRINTLINE(fmt, ...) \
00123         fprintf(output, "%s" fmt "%s", \
00124             line_prefix ? line_prefix : " - ", \
00125             __VA_ARGS__, \
00126             line_suffix ? line_suffix : "\n")
00127 
00128 #define BCD_INT(a) (((unsigned int)(a)) / 256)
00129 #define BCD_FRAC(a) (((unsigned int)(a)) % 256)
00130 
00131 #define BCD_FMT "%x.%x"
00132 #define BCD_ARGS(a) BCD_INT((a)), BCD_FRAC((a))
00133 
00134 static void usb_dump_descriptor_device(FILE *output,
00135     const char *line_prefix, const char *line_suffix,
00136     const uint8_t *descriptor, size_t descriptor_length)
00137 {
00138         usb_standard_device_descriptor_t *d
00139             = (usb_standard_device_descriptor_t *) descriptor;
00140         if (descriptor_length < sizeof(*d)) {
00141                 return;
00142         }
00143 
00144         PRINTLINE("bLength = %d", d->length);
00145         PRINTLINE("bDescriptorType = 0x%02x", d->descriptor_type);
00146         PRINTLINE("bcdUSB = %d (" BCD_FMT ")", d->usb_spec_version,
00147             BCD_ARGS(d->usb_spec_version));
00148         PRINTLINE("bDeviceClass = 0x%02x", d->device_class);
00149         PRINTLINE("bDeviceSubClass = 0x%02x", d->device_subclass);
00150         PRINTLINE("bDeviceProtocol = 0x%02x", d->device_protocol);
00151         PRINTLINE("bMaxPacketSize0 = %d", d->max_packet_size);
00152         PRINTLINE("idVendor = 0x%04x", d->vendor_id);
00153         PRINTLINE("idProduct = 0x%04x", d->product_id);
00154         PRINTLINE("bcdDevice = %d", d->device_version);
00155         PRINTLINE("iManufacturer = %d", d->str_manufacturer);
00156         PRINTLINE("iProduct = %d", d->str_product);
00157         PRINTLINE("iSerialNumber = %d", d->str_serial_number);
00158         PRINTLINE("bNumConfigurations = %d", d->configuration_count);
00159 }
00160 
00161 static void usb_dump_descriptor_configuration(FILE *output,
00162     const char *line_prefix, const char *line_suffix,
00163     const uint8_t *descriptor, size_t descriptor_length)
00164 {
00165         usb_standard_configuration_descriptor_t *d
00166             = (usb_standard_configuration_descriptor_t *) descriptor;
00167         if (descriptor_length < sizeof(*d)) {
00168                 return;
00169         }
00170 
00171         bool self_powered = d->attributes & 64;
00172         bool remote_wakeup = d->attributes & 32;
00173 
00174         PRINTLINE("bLength = %d", d->length);
00175         PRINTLINE("bDescriptorType = 0x%02x", d->descriptor_type);
00176         PRINTLINE("wTotalLength = %d", d->total_length);
00177         PRINTLINE("bNumInterfaces = %d", d->interface_count);
00178         PRINTLINE("bConfigurationValue = %d", d->configuration_number);
00179         PRINTLINE("iConfiguration = %d", d->str_configuration);
00180         PRINTLINE("bmAttributes = %d [%s%s%s]", d->attributes,
00181             self_powered ? "self-powered" : "",
00182             (self_powered & remote_wakeup) ? ", " : "",
00183             remote_wakeup ? "remote-wakeup" : "");
00184         PRINTLINE("MaxPower = %d (%dmA)", d->max_power,
00185             2 * d->max_power);
00186 }
00187 
00188 static void usb_dump_descriptor_interface(FILE *output,
00189     const char *line_prefix, const char *line_suffix,
00190     const uint8_t *descriptor, size_t descriptor_length)
00191 {
00192         usb_standard_interface_descriptor_t *d
00193             = (usb_standard_interface_descriptor_t *) descriptor;
00194         if (descriptor_length < sizeof(*d)) {
00195                 return;
00196         }
00197 
00198         PRINTLINE("bLength = %d", d->length);
00199         PRINTLINE("bDescriptorType = 0x%02x", d->descriptor_type);
00200         PRINTLINE("bInterfaceNumber = %d", d->interface_number);
00201         PRINTLINE("bAlternateSetting = %d", d->alternate_setting);
00202         PRINTLINE("bNumEndpoints = %d", d->endpoint_count);
00203         PRINTLINE("bInterfaceClass = %s", d->interface_class == 0
00204             ? "reserved (0)" : usb_str_class(d->interface_class));
00205         PRINTLINE("bInterfaceSubClass = %d", d->interface_subclass);
00206         PRINTLINE("bInterfaceProtocol = %d", d->interface_protocol);
00207         PRINTLINE("iInterface = %d", d->str_interface);
00208 }
00209 
00210 static void usb_dump_descriptor_string(FILE *output,
00211     const char *line_prefix, const char *line_suffix,
00212     const uint8_t *descriptor, size_t descriptor_length)
00213 {
00214 }
00215 
00216 static void usb_dump_descriptor_endpoint(FILE *output,
00217     const char *line_prefix, const char *line_suffix,
00218     const uint8_t *descriptor, size_t descriptor_length)
00219 {
00220         usb_standard_endpoint_descriptor_t *d
00221            = (usb_standard_endpoint_descriptor_t *) descriptor;
00222         if (descriptor_length < sizeof(*d)) {
00223                 return;
00224         }
00225 
00226         int endpoint = d->endpoint_address & 15;
00227         usb_direction_t direction = d->endpoint_address & 128
00228             ? USB_DIRECTION_IN : USB_DIRECTION_OUT;
00229         usb_transfer_type_t transfer_type = d->attributes & 3;
00230 
00231         PRINTLINE("bLength = %d", d->length);
00232         PRINTLINE("bDescriptorType = 0x%02X", d->descriptor_type);
00233         PRINTLINE("bEndpointAddress = 0x%02X [%d, %s]",
00234             d->endpoint_address, endpoint,
00235             direction == USB_DIRECTION_IN ? "in" : "out");
00236         PRINTLINE("bmAttributes = %d [%s]", d->attributes,
00237             usb_str_transfer_type(transfer_type));
00238         PRINTLINE("wMaxPacketSize = %d", d->max_packet_size);
00239         PRINTLINE("bInterval = %dms", d->poll_interval);
00240 }
00241 
00242 static void usb_dump_descriptor_hid(FILE *output,
00243     const char *line_prefix, const char *line_suffix,
00244     const uint8_t *descriptor, size_t descriptor_length)
00245 {
00246         usb_standard_hid_descriptor_t *d
00247             = (usb_standard_hid_descriptor_t *) descriptor;
00248         if (descriptor_length < sizeof(*d)) {
00249                 return;
00250         }
00251 
00252         PRINTLINE("bLength = %d", d->length);
00253         PRINTLINE("bDescriptorType = 0x%02x", d->descriptor_type);
00254         PRINTLINE("bcdHID = %d (" BCD_FMT ")", d->spec_release,
00255             BCD_ARGS(d->spec_release));
00256         PRINTLINE("bCountryCode = %d", d->country_code);
00257         PRINTLINE("bNumDescriptors = %d", d->class_desc_count);
00258         PRINTLINE("bDescriptorType = %d", d->report_desc_info.type);
00259         PRINTLINE("wDescriptorLength = %d", d->report_desc_info.length);
00260 
00261         /* Print info about report descriptors. */
00262         size_t i;
00263         size_t count = (descriptor_length - sizeof(*d))
00264             / sizeof(usb_standard_hid_class_descriptor_info_t);
00265         usb_standard_hid_class_descriptor_info_t *d2
00266             = (usb_standard_hid_class_descriptor_info_t *)
00267             (descriptor + sizeof(*d));
00268         for (i = 0; i < count; i++, d2++) {
00269                 PRINTLINE("bDescriptorType = %d", d2->type);
00270                 PRINTLINE("wDescriptorLength = %d", d2->length);
00271         }
00272 }
00273 
00274 static void usb_dump_descriptor_hub(FILE *output,
00275     const char *line_prefix, const char *line_suffix,
00276     const uint8_t *descriptor, size_t descriptor_length)
00277 {
00278         /* TODO */
00279 }
00280 
00281 static void usb_dump_descriptor_generic(FILE *output,
00282     const char *line_prefix, const char *line_suffix,
00283     const uint8_t *descriptor, size_t descriptor_length)
00284 {
00285         /* TODO */
00286 }
00287 
00288 

Generated on Thu Jun 2 07:45:43 2011 for HelenOS/USB by  doxygen 1.4.7