From 6dfbb32b3c0be5f9a47fb094f378d76f53b0b4b2 Mon Sep 17 00:00:00 2001 From: Bernhard Urban Date: Tue, 22 Sep 2009 09:30:04 +0200 Subject: [PATCH] further refactoring; usb_get_desc_interfac doesn't work yet --- usb/core/core.c | 21 +++++++++++++++------ usb/core/usb.c | 30 +++++++++++++++++++++++++----- usb/core/usb.h | 22 +++++++++++----------- 3 files changed, 51 insertions(+), 22 deletions(-) diff --git a/usb/core/core.c b/usb/core/core.c index 1571a19..5583c75 100644 --- a/usb/core/core.c +++ b/usb/core/core.c @@ -89,6 +89,7 @@ void usb_periodic() struct usb_device *usb_add_device() { struct usb_device *dev = (struct usb_device *) malloc(sizeof(struct usb_device)); + dev->conf = (struct usb_conf *) malloc(sizeof(struct usb_conf)); dev->address = 0; /* send at first time only 8 bytes */ dev->bMaxPacketSize0 = 8; @@ -107,7 +108,7 @@ struct usb_device *usb_add_device() s8 ret; memset(buf, 0, sizeof(buf)); - ret = usb_get_dev_desc_simple(dev, buf, sizeof(buf)); + ret = usb_get_desc_dev_simple(dev, buf, sizeof(buf)); #ifdef _DU_CORE_ADD printf("=============\nbuf: 0x%08X\nafter usb_get_dev_desc_simple(ret: %d):\n", buf, ret); hexdump(buf, sizeof(buf)); @@ -126,7 +127,7 @@ struct usb_device *usb_add_device() #endif memset(buf, 0, sizeof(buf)); - ret = usb_get_dev_desc(dev, buf, sizeof(buf)); + ret = usb_get_desc_dev(dev, buf, sizeof(buf)); #ifdef _DU_CORE_ADD printf("=============\nbuf: 0x%08X\nafter usb_get_dev_desc(ret: %d):\n", buf, ret); hexdump(buf, sizeof(buf)); @@ -136,8 +137,6 @@ struct usb_device *usb_add_device() if(dev->iManufacturer) { memset(buf, 0, sizeof(buf)); man = usb_get_string_simple(dev, dev->iManufacturer, buf, sizeof(buf)); - printf("iManufacturer:\n"); - hexdump(buf, sizeof(buf)); } else { man = (char*) malloc(11); memset(man, '\0', sizeof(man)); @@ -183,10 +182,20 @@ struct usb_device *usb_add_device() memset(buf, 0, sizeof(buf)); /* in the most cases usb devices have just one configuration descriptor */ - ret = usb_get_configuration(dev, 0, buf, sizeof(buf)); - printf("=============\nbuf: 0x%08X\nafter usb_get_configuration(ret: %d):\n", buf, ret); + ret = usb_get_desc_configuration(dev, 0, buf, sizeof(buf)); + printf("=============\nbuf: 0x%08X\nafter usb_get_desc_configuration(ret: %d):\n", buf, ret); hexdump(buf, sizeof(buf)); + printf("interfaces: %d\n", dev->conf->bNumInterfaces); + u8 i; + for(i = 1; i <= dev->conf->bNumInterfaces; i++) { + memset(buf, 0, sizeof(buf)); + ret = usb_get_desc_interface(dev, i, buf, sizeof(buf)); + printf("=============\nbuf: 0x%08X\nafter usb_get_desc_interface_%d(ret: %d):\n", buf, i, ret); + hexdump(buf, sizeof(buf)); + } + + /* usb_get_descriptor(dev, DEVICE, 0, buf, 8); diff --git a/usb/core/usb.c b/usb/core/usb.c index 938b481..94676d3 100644 --- a/usb/core/usb.c +++ b/usb/core/usb.c @@ -171,7 +171,7 @@ s8 usb_get_descriptor(struct usb_device *dev, u8 type, u8 index, u8 *buf, u8 siz /* ask first 8 bytes of device descriptor with this special * GET Descriptor Request, when device address = 0 */ -s8 usb_get_dev_desc_simple(struct usb_device *dev, u8 *buf, u8 size) +s8 usb_get_desc_dev_simple(struct usb_device *dev, u8 *buf, u8 size) { if(size < 8) { return -1; @@ -186,9 +186,9 @@ s8 usb_get_dev_desc_simple(struct usb_device *dev, u8 *buf, u8 size) return 0; } -s8 usb_get_dev_desc(struct usb_device *dev, u8 *buf, u8 size) +s8 usb_get_desc_dev(struct usb_device *dev, u8 *buf, u8 size) { - if (size < 0x12 || usb_get_dev_desc_simple(dev, buf, size) < 0) { + if (size < 0x12 || usb_get_desc_dev_simple(dev, buf, size) < 0) { return -1; } usb_get_descriptor(dev, DEVICE, 0, buf, size >= buf[0] ? buf[0] : size); @@ -210,13 +210,33 @@ s8 usb_get_dev_desc(struct usb_device *dev, u8 *buf, u8 size) return 0; } -s8 usb_get_configuration(struct usb_device *dev, u8 index, u8 *buf, u8 size) +s8 usb_get_desc_configuration(struct usb_device *dev, u8 index, u8 *buf, u8 size) { - if(size < 8) { + if(size < 9) { return -1; } usb_get_descriptor(dev, CONFIGURATION, index, buf, 8); usb_get_descriptor(dev, CONFIGURATION, index, buf, size >= buf[0] ? buf[0] : size); + + dev->conf->bLength = buf[0]; + dev->conf->bDescriptorType = buf[1]; + dev->conf->wTotalLength = (u16) (buf[3] << 8 | buf[2]); + dev->conf->bNumInterfaces = buf[4]; + dev->conf->bConfigurationValue = buf[5]; + dev->conf->iConfiguration = buf[6]; + dev->conf->bmAttributes = buf[7]; + dev->conf->bMaxPower = buf[8]; + return 0; +} + +s8 usb_get_desc_interface(struct usb_device *dev, u8 index, u8 *buf, u8 size) +{ + if(size < 9) { + return -1; + } + usb_get_descriptor(dev, INTERFACE, index, buf, 8); + usb_get_descriptor(dev, INTERFACE, index, buf, size >= buf[0] ? buf[0] : size); + return 0; } diff --git a/usb/core/usb.h b/usb/core/usb.h index 1a8c3c2..c6b20c8 100644 --- a/usb/core/usb.h +++ b/usb/core/usb.h @@ -50,12 +50,6 @@ struct usb_device *usb_open_class(u8 class); s8 usb_close(struct usb_device *dev); -s8 usb_get_device_descriptor(struct usb_device *dev, u8 *buf, u8 buflen); -s8 usb_set_address(struct usb_device *dev, u8 address); -s8 usb_set_configuration(struct usb_device *dev, u8 configuration); -s8 usb_set_altinterface(struct usb_device *dev, u8 alternate); - - /** * usb_reset resets the specified device by sending a RESET down the port * it is connected to. Returns 0 on success or < 0 on error. @@ -66,12 +60,18 @@ s8 usb_reset(struct usb_device *dev); /******************* Control Transfer **********************/ s8 usb_control_msg(struct usb_device *dev, u8 requesttype, u8 request, u16 value, u16 index, u16 length, u8 *buf, u16 timeout); -s8 usb_get_string(struct usb_device *dev, u8 index, u8 langid, u8 *buf, u8 buflen); -char *usb_get_string_simple(struct usb_device *dev, u8 index, u8 *buf, u8 size); -s8 usb_get_dev_desc_simple(struct usb_device *dev, u8 *buf, u8 size); -s8 usb_get_configuration(struct usb_device *dev, u8 index, u8 *buf, u8 size); -s8 usb_get_dev_desc(struct usb_device *dev, u8 *buf, u8 size); s8 usb_get_descriptor(struct usb_device *dev, u8 type, u8 index, u8 *buf, u8 size); +s8 usb_get_desc_dev_simple(struct usb_device *dev, u8 *buf, u8 size); +s8 usb_get_desc_dev(struct usb_device *dev, u8 *buf, u8 size); +s8 usb_get_desc_configuration(struct usb_device *dev, u8 index, u8 *buf, u8 size); +s8 usb_get_desc_interface(struct usb_device *dev, u8 index, u8 *buf, u8 size); + +char *usb_get_string_simple(struct usb_device *dev, u8 index, u8 *buf, u8 size); +s8 usb_get_string(struct usb_device *dev, u8 index, u8 langid, u8 *buf, u8 buflen); + +s8 usb_set_address(struct usb_device *dev, u8 address); +s8 usb_set_configuration(struct usb_device *dev, u8 configuration); +s8 usb_set_altinterface(struct usb_device *dev, u8 alternate); /******************* Bulk Transfer **********************/ -- 2.25.1