From 1bbd827e10d07b61f72b5d1d7174f6d93859bc38 Mon Sep 17 00:00:00 2001 From: Greg Kroah-Hartman Date: Tue, 8 Dec 2009 16:27:23 -0800 Subject: [PATCH] create raw.c and endpoint.c raw.c handles "raw" USB config descriptors. endpoint.c handles struct usb_endpoint stuff. Signed-off-by: Greg Kroah-Hartman --- Makefile | 2 +- endpoint.c | 76 +++++++++++++++++++ lsusb.c | 212 +---------------------------------------------------- raw.c | 205 +++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 283 insertions(+), 212 deletions(-) create mode 100644 endpoint.c create mode 100644 raw.c diff --git a/Makefile b/Makefile index 3442a1f..c917bf4 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ CFLAGS?=-O1 -g ${WARNFLAGS} CC?=gcc -OBJS = device.o interface.o lsusb.o +OBJS = device.o interface.o endpoint.o raw.o lsusb.o lsusb: $(OBJS) Makefile usb.h list.h diff --git a/endpoint.c b/endpoint.c new file mode 100644 index 0000000..13f96c1 --- /dev/null +++ b/endpoint.c @@ -0,0 +1,76 @@ +/* + * endpoint.c + * + * Handle all struct usb_endpoint logic + * + * Copyright (C) 2009 Greg Kroah-Hartman + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License, + * version 2, as published by the Free Software Foundation. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define LIBUDEV_I_KNOW_THE_API_IS_SUBJECT_TO_CHANGE + +#include +#include "list.h" +#include "usb.h" +#include "lsusb.h" + + + +static struct usb_endpoint *new_usb_endpoint(void) +{ + return robust_malloc(sizeof(struct usb_endpoint)); +} + +void free_usb_endpoint(struct usb_endpoint *usb_endpoint) +{ + free(usb_endpoint->bEndpointAddress); + free(usb_endpoint->bInterval); + free(usb_endpoint->bLength); + free(usb_endpoint->bmAttributes); + free(usb_endpoint->direction); + free(usb_endpoint->type); + free(usb_endpoint->wMaxPacketSize); + free(usb_endpoint); +} + +struct usb_endpoint *create_usb_endpoint(struct udev_device *device, const char *endpoint_name) +{ + struct usb_endpoint *ep; + char filename[PATH_MAX]; + + ep = new_usb_endpoint(); + +#define get_endpoint_string(string) \ + sprintf(filename, "%s/"__stringify(string), endpoint_name); \ + ep->string = get_dev_string(device, filename); + + get_endpoint_string(bEndpointAddress); + get_endpoint_string(bInterval); + get_endpoint_string(bLength); + get_endpoint_string(bmAttributes); + get_endpoint_string(direction); + get_endpoint_string(type); + get_endpoint_string(wMaxPacketSize); + + return ep; +} diff --git a/lsusb.c b/lsusb.c index 53f4bb8..2f148bb 100644 --- a/lsusb.c +++ b/lsusb.c @@ -49,23 +49,6 @@ void *robust_malloc(size_t size) return data; } -static struct usb_endpoint *new_usb_endpoint(void) -{ - return robust_malloc(sizeof(struct usb_endpoint)); -} - -void free_usb_endpoint(struct usb_endpoint *usb_endpoint) -{ - free(usb_endpoint->bEndpointAddress); - free(usb_endpoint->bInterval); - free(usb_endpoint->bLength); - free(usb_endpoint->bmAttributes); - free(usb_endpoint->direction); - free(usb_endpoint->type); - free(usb_endpoint->wMaxPacketSize); - free(usb_endpoint); -} - char *get_dev_string(struct udev_device *device, const char *name) { const char *value; @@ -76,199 +59,6 @@ char *get_dev_string(struct udev_device *device, const char *name) return NULL; } -struct usb_endpoint *create_usb_endpoint(struct udev_device *device, const char *endpoint_name) -{ - struct usb_endpoint *ep; - char filename[PATH_MAX]; - - ep = new_usb_endpoint(); - -#define get_endpoint_string(string) \ - sprintf(filename, "%s/"__stringify(string), endpoint_name); \ - ep->string = get_dev_string(device, filename); - - get_endpoint_string(bEndpointAddress); - get_endpoint_string(bInterval); - get_endpoint_string(bLength); - get_endpoint_string(bmAttributes); - get_endpoint_string(direction); - get_endpoint_string(type); - get_endpoint_string(wMaxPacketSize); - - return ep; -} - -static void parse_config_descriptor(const unsigned char *descriptor) -{ - struct usb_config config; - - config.bLength = descriptor[0]; - config.bDescriptorType = descriptor[1]; - config.wTotalLength = (descriptor[3] << 8) | descriptor[2]; - config.bNumInterfaces = descriptor[4]; - config.bConfigurationValue = descriptor[5]; - config.iConfiguration = descriptor[6]; - config.bmAttributes = descriptor[7]; - config.bMaxPower = descriptor[8]; - - printf("Config descriptor\n"); - printf("\tbLength\t\t\t%d\n", config.bLength); - printf("\tbDescriptorType\t\t%d\n", config.bDescriptorType); - printf("\twTotalLength\t\t%d\n", config.wTotalLength); - printf("\tbNumInterfaces\t\t%d\n", config.bNumInterfaces); - printf("\tbConfigurationValue\t%d\n", config.bConfigurationValue); - printf("\tiConfiguration\t\t%d\n", config.iConfiguration); - printf("\tbmAttributes\t\t0x%02x\n", config.bmAttributes); - printf("\tbMaxPower\t\t%d\n", config.bMaxPower); -} - -static void parse_interface_descriptor(const unsigned char *descriptor) -{ - unsigned char bLength = descriptor[0]; - unsigned char bDescriptorType = descriptor[1]; - unsigned char bInterfaceNumber = descriptor[2]; - unsigned char bAlternateSetting = descriptor[3]; - unsigned char bNumEndpoints = descriptor[4]; - unsigned char bInterfaceClass = descriptor[5]; - unsigned char bInterfaceSubClass = descriptor[6]; - unsigned char bInterfaceProtocol = descriptor[7]; - unsigned char iInterface = descriptor[8]; - - printf("Interface descriptor\n"); - printf("\tbLength\t\t\t%d\n", bLength); - printf("\tbDescriptorType\t\t%d\n", bDescriptorType); - printf("\tbInterfaceNumber\t%d\n", bInterfaceNumber); - printf("\tbAlternateSetting\t%d\n", bAlternateSetting); - printf("\tbNumEndpoints\t\t%d\n", bNumEndpoints); - printf("\tbInterfaceClass\t\t%d\n", bInterfaceClass); - printf("\tbInterfaceSubClass\t%d\n", bInterfaceSubClass); - printf("\tbInterfaceProtocol\t%d\n", bInterfaceProtocol); - printf("\tiInterface\t\t%d\n", iInterface); - -} - -static void parse_endpoint_descriptor(const unsigned char *descriptor) -{ - unsigned char bLength = descriptor[0]; - unsigned char bDescriptorType = descriptor[1]; - unsigned char bEndpointAddress = descriptor[2]; - unsigned char bmAttributes = descriptor[3]; - unsigned short wMaxPacketSize = (descriptor[5] << 8) | descriptor[4]; - unsigned char bInterval = descriptor[6]; - - printf("Endpoint descriptor\n"); - printf("\tbLength\t\t\t%d\n", bLength); - printf("\tbDescriptorType\t\t%d\n", bDescriptorType); - printf("\tbEndpointAddress\t%0x\n", bEndpointAddress); - printf("\tbmAtributes\t\t%0x\n", bmAttributes); - printf("\twMaxPacketSize\t\t%d\n", wMaxPacketSize); - printf("\tbInterval\t\t%d\n", bInterval); -} - -static void parse_device_qualifier(struct usb_device *usb_device, const unsigned char *descriptor) -{ - struct usb_device_qualifier *dq; - char string[255]; - unsigned char bLength = descriptor[0]; - unsigned char bDescriptorType = descriptor[1]; - unsigned char bcdUSB0 = descriptor[3]; - unsigned char bcdUSB1 = descriptor[2]; - unsigned char bDeviceClass = descriptor[4]; - unsigned char bDeviceSubClass = descriptor[5]; - unsigned char bDeviceProtocol = descriptor[6]; - unsigned char bMaxPacketSize0 = descriptor[7]; - unsigned char bNumConfigurations = descriptor[8]; - - dq = robust_malloc(sizeof(struct usb_device_qualifier)); - -#define build_string(name) \ - sprintf(string, "%d", name); \ - dq->name = strdup(string); - - build_string(bLength); - build_string(bDescriptorType); - build_string(bDeviceClass); - build_string(bDeviceSubClass); - build_string(bDeviceProtocol); - build_string(bMaxPacketSize0); - build_string(bNumConfigurations); - sprintf(string, "%2x.%2x", bcdUSB0, bcdUSB1); - dq->bcdUSB = strdup(string); - - usb_device->qualifier = dq; - - printf("Device Qualifier\n"); - printf("\tbLength\t\t\t%s\n", dq->bLength); - printf("\tbDescriptorType\t\t%s\n", dq->bDescriptorType); - printf("\tbcdUSB\t\t%s", dq->bcdUSB); -} - -void read_raw_usb_descriptor(struct udev_device *device, struct usb_device *usb_device) -{ - char filename[PATH_MAX]; - int file; - unsigned char *data; - unsigned char size; - ssize_t read_retval; - - sprintf(filename, "%s/descriptors", udev_device_get_syspath(device)); - - file = open(filename, O_RDONLY); - if (file == -1) - exit(1); - while (1) { - read_retval = read(file, &size, 1); - if (read_retval != 1) - break; - data = malloc(size); - data[0] = size; - read_retval = read(file, &data[1], size-1); - switch (data[1]) { - case 0x01: - /* device descriptor */ - /* - * We get all of this information from sysfs, so no - * need to do any parsing here of the raw data. - */ - break; - case 0x02: - /* config descriptor */ - parse_config_descriptor(data); - break; - case 0x03: - /* string descriptor */ -// parse_string_descriptor(data); - break; - case 0x04: - /* interface descriptor */ - parse_interface_descriptor(data); - break; - case 0x05: - /* endpoint descriptor */ - parse_endpoint_descriptor(data); - break; - case 0x06: - /* device qualifier */ - parse_device_qualifier(usb_device, data); - break; - case 0x07: - /* other speed config */ - break; - case 0x08: - /* interface power */ - break; - default: - break; - } - free(data); - } - close(file); -} - -static void create_usb_root_device(struct udev_device *device) -{ - create_usb_device(device); -} //int main(int argc, char *argv[]) int main(void) @@ -295,7 +85,7 @@ int main(void) continue; if (strcmp("usb_device", udev_device_get_devtype(device)) == 0) // if (strstr(udev_device_get_sysname(device), "usb") != NULL) - create_usb_root_device(device); + create_usb_device(device); #if 0 printf("%s: ", udev_list_entry_get_name(list_entry)); printf("\tdevtype: %s\n", udev_device_get_devtype(device)); diff --git a/raw.c b/raw.c new file mode 100644 index 0000000..8854633 --- /dev/null +++ b/raw.c @@ -0,0 +1,205 @@ +/* + * raw.c + * + * Handle "raw" USB config descriptors and the logic to create stuff out + * of them. + * + * Copyright (C) 2009 Greg Kroah-Hartman + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License, + * version 2, as published by the Free Software Foundation. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define LIBUDEV_I_KNOW_THE_API_IS_SUBJECT_TO_CHANGE + +#include +#include "list.h" +#include "usb.h" +#include "lsusb.h" + + +static void parse_config_descriptor(const unsigned char *descriptor) +{ + struct usb_config config; + + config.bLength = descriptor[0]; + config.bDescriptorType = descriptor[1]; + config.wTotalLength = (descriptor[3] << 8) | descriptor[2]; + config.bNumInterfaces = descriptor[4]; + config.bConfigurationValue = descriptor[5]; + config.iConfiguration = descriptor[6]; + config.bmAttributes = descriptor[7]; + config.bMaxPower = descriptor[8]; + + printf("Config descriptor\n"); + printf("\tbLength\t\t\t%d\n", config.bLength); + printf("\tbDescriptorType\t\t%d\n", config.bDescriptorType); + printf("\twTotalLength\t\t%d\n", config.wTotalLength); + printf("\tbNumInterfaces\t\t%d\n", config.bNumInterfaces); + printf("\tbConfigurationValue\t%d\n", config.bConfigurationValue); + printf("\tiConfiguration\t\t%d\n", config.iConfiguration); + printf("\tbmAttributes\t\t0x%02x\n", config.bmAttributes); + printf("\tbMaxPower\t\t%d\n", config.bMaxPower); +} + +static void parse_interface_descriptor(const unsigned char *descriptor) +{ + unsigned char bLength = descriptor[0]; + unsigned char bDescriptorType = descriptor[1]; + unsigned char bInterfaceNumber = descriptor[2]; + unsigned char bAlternateSetting = descriptor[3]; + unsigned char bNumEndpoints = descriptor[4]; + unsigned char bInterfaceClass = descriptor[5]; + unsigned char bInterfaceSubClass = descriptor[6]; + unsigned char bInterfaceProtocol = descriptor[7]; + unsigned char iInterface = descriptor[8]; + + printf("Interface descriptor\n"); + printf("\tbLength\t\t\t%d\n", bLength); + printf("\tbDescriptorType\t\t%d\n", bDescriptorType); + printf("\tbInterfaceNumber\t%d\n", bInterfaceNumber); + printf("\tbAlternateSetting\t%d\n", bAlternateSetting); + printf("\tbNumEndpoints\t\t%d\n", bNumEndpoints); + printf("\tbInterfaceClass\t\t%d\n", bInterfaceClass); + printf("\tbInterfaceSubClass\t%d\n", bInterfaceSubClass); + printf("\tbInterfaceProtocol\t%d\n", bInterfaceProtocol); + printf("\tiInterface\t\t%d\n", iInterface); + +} + +static void parse_endpoint_descriptor(const unsigned char *descriptor) +{ + unsigned char bLength = descriptor[0]; + unsigned char bDescriptorType = descriptor[1]; + unsigned char bEndpointAddress = descriptor[2]; + unsigned char bmAttributes = descriptor[3]; + unsigned short wMaxPacketSize = (descriptor[5] << 8) | descriptor[4]; + unsigned char bInterval = descriptor[6]; + + printf("Endpoint descriptor\n"); + printf("\tbLength\t\t\t%d\n", bLength); + printf("\tbDescriptorType\t\t%d\n", bDescriptorType); + printf("\tbEndpointAddress\t%0x\n", bEndpointAddress); + printf("\tbmAtributes\t\t%0x\n", bmAttributes); + printf("\twMaxPacketSize\t\t%d\n", wMaxPacketSize); + printf("\tbInterval\t\t%d\n", bInterval); +} + +static void parse_device_qualifier(struct usb_device *usb_device, const unsigned char *descriptor) +{ + struct usb_device_qualifier *dq; + char string[255]; + unsigned char bLength = descriptor[0]; + unsigned char bDescriptorType = descriptor[1]; + unsigned char bcdUSB0 = descriptor[3]; + unsigned char bcdUSB1 = descriptor[2]; + unsigned char bDeviceClass = descriptor[4]; + unsigned char bDeviceSubClass = descriptor[5]; + unsigned char bDeviceProtocol = descriptor[6]; + unsigned char bMaxPacketSize0 = descriptor[7]; + unsigned char bNumConfigurations = descriptor[8]; + + dq = robust_malloc(sizeof(struct usb_device_qualifier)); + +#define build_string(name) \ + sprintf(string, "%d", name); \ + dq->name = strdup(string); + + build_string(bLength); + build_string(bDescriptorType); + build_string(bDeviceClass); + build_string(bDeviceSubClass); + build_string(bDeviceProtocol); + build_string(bMaxPacketSize0); + build_string(bNumConfigurations); + sprintf(string, "%2x.%2x", bcdUSB0, bcdUSB1); + dq->bcdUSB = strdup(string); + + usb_device->qualifier = dq; + + printf("Device Qualifier\n"); + printf("\tbLength\t\t\t%s\n", dq->bLength); + printf("\tbDescriptorType\t\t%s\n", dq->bDescriptorType); + printf("\tbcdUSB\t\t%s", dq->bcdUSB); +} + +void read_raw_usb_descriptor(struct udev_device *device, struct usb_device *usb_device) +{ + char filename[PATH_MAX]; + int file; + unsigned char *data; + unsigned char size; + ssize_t read_retval; + + sprintf(filename, "%s/descriptors", udev_device_get_syspath(device)); + + file = open(filename, O_RDONLY); + if (file == -1) + exit(1); + while (1) { + read_retval = read(file, &size, 1); + if (read_retval != 1) + break; + data = malloc(size); + data[0] = size; + read_retval = read(file, &data[1], size-1); + switch (data[1]) { + case 0x01: + /* device descriptor */ + /* + * We get all of this information from sysfs, so no + * need to do any parsing here of the raw data. + */ + break; + case 0x02: + /* config descriptor */ + parse_config_descriptor(data); + break; + case 0x03: + /* string descriptor */ +// parse_string_descriptor(data); + break; + case 0x04: + /* interface descriptor */ + parse_interface_descriptor(data); + break; + case 0x05: + /* endpoint descriptor */ + parse_endpoint_descriptor(data); + break; + case 0x06: + /* device qualifier */ + parse_device_qualifier(usb_device, data); + break; + case 0x07: + /* other speed config */ + break; + case 0x08: + /* interface power */ + break; + default: + break; + } + free(data); + } + close(file); +} +