more refactor
authorBernhard Urban <lewurm@gmx.net>
Tue, 22 Sep 2009 09:13:31 +0000 (11:13 +0200)
committerBernhard Urban <lewurm@gmx.net>
Tue, 22 Sep 2009 09:13:31 +0000 (11:13 +0200)
usb/core/core.c
usb/core/core.h
usb/core/usb.c
usb/core/usb.h

index b57531564eeef768bc7be37643bf5bdf399a356c..88e6f18e6063b228c01a5ac97fc013ef9cf09cb6 100644 (file)
@@ -102,17 +102,8 @@ struct usb_device *usb_add_device()
        dev->epTogl[1] = 0;
        dev->epTogl[2] = 0;
 
-#define LEN 128
-       u8 buf[LEN];
-       memset(buf, 0, sizeof(buf));
-
        s8 ret;
-       memset(buf, 0, sizeof(buf));
-       ret = usb_get_desc_dev_simple(dev, buf, sizeof(buf));
-#ifdef _DU_CORE_ADD
-       printf("=============\nafter usb_get_dev_desc_simple(ret: %d):\n", ret);
-       hexdump(buf, sizeof(buf));
-#endif
+       ret = usb_get_desc_dev_simple(dev);
        if(ret < 0) {
                return (void*) -1;
        }
@@ -126,40 +117,31 @@ struct usb_device *usb_add_device()
        printf("set address to %d\n", dev->address);
 #endif
 
-       memset(buf, 0, sizeof(buf));
-       ret = usb_get_desc_dev(dev, buf, sizeof(buf));
-#ifdef _DU_CORE_ADD
-       printf("=============\nafter usb_get_dev_desc(ret: %d):\n", ret);
-       hexdump(buf, sizeof(buf));
-#endif
+       ret = usb_get_desc_dev(dev);
 
        char *man, *prod, *serial;
        if(dev->iManufacturer) {
-               memset(buf, 0, sizeof(buf));
-               man = usb_get_string_simple(dev, dev->iManufacturer, buf, sizeof(buf));
+               man = usb_get_string_simple(dev, dev->iManufacturer);
        } else {
                man = (char*) malloc(11);
                memset(man, '\0', sizeof(man));
                strlcpy(man, "no String", 10);
        }
        if(dev->iProduct) {
-               memset(buf, 0, sizeof(buf));
-               prod = usb_get_string_simple(dev, dev->iProduct, buf, sizeof(buf));
+               prod = usb_get_string_simple(dev, dev->iProduct);
        } else {
                prod = (char*) malloc(11);
                memset(prod, '\0', sizeof(prod));
                strlcpy(prod, "no String", 10);
        }
        if(dev->iSerialNumber) {
-               memset(buf, 0, sizeof(buf));
-               serial = usb_get_string_simple(dev, dev->iSerialNumber, buf, sizeof(buf));
+               serial = usb_get_string_simple(dev, dev->iSerialNumber);
        } else {
                serial = (char*) malloc(11);
                memset(serial, '\0', sizeof(serial));
                strlcpy(serial, "no String", 10);
        }
 
-
        printf( "bLength 0x%02X\n"
                        "bDescriptorType 0x%02X\n"
                        "bcdUSB 0x%02X\n"
@@ -180,16 +162,8 @@ struct usb_device *usb_add_device()
                        dev->iSerialNumber, serial,
                        dev->bNumConfigurations);
 
-       memset(buf, 0, sizeof(buf));
        /* in the most cases usb devices have just one configuration descriptor */
-       ret = usb_get_desc_configuration(dev, 0, buf, sizeof(buf));
-       printf("=============\nafter usb_get_desc_configuration(ret: %d):\n", ret);
-       hexdump(buf, sizeof(buf));
-
-       memset(buf, 0, sizeof(buf));
-       ret = usb_get_descriptor(dev, CONFIGURATION, 0, buf, dev->conf->wTotalLength);
-       printf("=============\nafter usb_get_HACK(ret: %d):\n", ret);
-       hexdump(buf, sizeof(buf));
+       ret = usb_get_desc_config_ext(dev, 0);
 
        /* select configuration */
        ret = usb_set_configuration(dev, dev->conf->bConfigurationValue);
index cb521fc394cd8c3861b85595644910f4b81d1252..f541306ad5204dee855b429d25b9c4be0f5c9397 100644 (file)
@@ -84,6 +84,31 @@ struct usb_conf {
        u8 bMaxPower;
 };
 
+struct usb_intf {
+       u8 bLength;
+       u8 bDescriptorType;
+       u8 bInterfaceNumber;
+       u8 bAlternateSetting;
+       u8 bNumEndpoints;
+       u8 bInterfaceClass;
+       u8 bInterfaceSubClass;
+       u8 bInterfaceProtocol;
+       u8 iInterface;
+
+       struct usb_intf *next;
+};
+
+struct usb_endp {
+       u8 bLength;
+       u8 bDescriptorType;
+       u8 bEndpointAddress;
+       u8 bmAttributes;
+       u16 wMaxPacketSize;
+       u8 bInterval;
+
+       struct usb_endp *next;
+};
+
 struct usb_endpoint {
        u8 type;
        u8 size;
index 95110672b1142f434950153ec4f9cec8c7b7dd97..fd47cdc538b74703314388dc9cf845c440229394 100644 (file)
 #include "../../malloc.h"
 #include "../../string.h"
 
+#define cleargbuf() memset(gbuf, 0, 0xff)
+/* internal global buffer */
+static u8 gbuf[0xff];
+
 
 /******************* Device Operations **********************/
 
@@ -132,29 +136,30 @@ s8 usb_control_msg(struct usb_device *dev, u8 requesttype, u8 request,
        return 0;
 }
 
+s8 usb_get_descriptor(struct usb_device *dev, u8 type, u8 index, u8 *buf, u8 size)
+{
+       usb_control_msg(dev, 0x80, GET_DESCRIPTOR, (type << 8) | index, 0, size, buf, 0);
+       return 0;
+}
 
-s8 usb_get_string(struct usb_device *dev, u8 index, u8 langid, u8 *buf, u8 buflen)
+s8 usb_get_string(struct usb_device *dev, u8 index, u8 langid)
 {
 
        return 0;
 }
 
-
-char *usb_get_string_simple(struct usb_device *dev, u8 index, u8 *buf, u8 size)
+char *usb_get_string_simple(struct usb_device *dev, u8 index)
 {
-       if(size < 8) {
-               return (char*) -1;
-       }
-       usb_get_descriptor(dev, STRING, index, buf, (u8) 8);
-       size = size >= (buf[0]) ? (buf[0]) : size;
-       usb_get_descriptor(dev, STRING, index, buf, size);
+       cleargbuf();
+       usb_get_descriptor(dev, STRING, index, gbuf, (u8) 8);
+       usb_get_descriptor(dev, STRING, index, gbuf, gbuf[0]);
 
-       char *str = (char*)malloc((size/2));
-       memset(str, '\0', (size/2));
+       char *str = (char*)malloc((gbuf[0]/2));
+       memset(str, '\0', (gbuf[0]/2));
 
        u16 i;
-       for(i=0; i<(size/2)-1; i++) {
-               str[i] = buf[2+(i*2)];
+       for(i=0; i<(gbuf[0]/2)-1; i++) {
+               str[i] = gbuf[2+(i*2)];
                printf("%c", str[i]);
        }
        printf("\n");
@@ -162,94 +167,92 @@ char *usb_get_string_simple(struct usb_device *dev, u8 index, u8 *buf, u8 size)
        return str;
 }
 
-s8 usb_get_descriptor(struct usb_device *dev, u8 type, u8 index, u8 *buf, u8 size)
-{
-       usb_control_msg(dev, 0x80, GET_DESCRIPTOR, (type << 8) | index, 0, size, buf, 0);
-       return 0;
-}
-
 /* ask first 8 bytes of device descriptor with this special 
  * GET Descriptor Request, when device address = 0
  */
-s8 usb_get_desc_dev_simple(struct usb_device *dev, u8 *buf, u8 size)
+s8 usb_get_desc_dev_simple(struct usb_device *dev)
 {
-       if(size < 8) {
-               return -1;
-       }
-       usb_get_descriptor(dev, DEVICE, 0, buf, 8);
+       cleargbuf();
+       usb_get_descriptor(dev, DEVICE, 0, gbuf, 8);
 
-       if(!buf[7]) {
-               printf("FU: %d\n", buf[7]);
+       if(!gbuf[7]) {
+               printf("FU: %d\n", gbuf[7]);
                return -2;
        }
-       dev->bMaxPacketSize0 = buf[7];
+       dev->bMaxPacketSize0 = gbuf[7];
        return 0;
 }
 
-s8 usb_get_desc_dev(struct usb_device *dev, u8 *buf, u8 size)
+s8 usb_get_desc_dev(struct usb_device *dev)
 {
-       if (size < 0x12 || usb_get_desc_dev_simple(dev, buf, size) < 0) {
+       cleargbuf();
+       if (usb_get_desc_dev_simple(dev) < 0) {
                return -1;
        }
-       usb_get_descriptor(dev, DEVICE, 0, buf, size >= buf[0] ? buf[0] : size);
-
-       dev->bLength = buf[0];
-       dev->bDescriptorType = buf[1];
-       dev->bcdUSB = (u16) (buf[3] << 8 | buf[2]);
-       dev->bDeviceClass = buf[4];
-       dev->bDeviceSubClass = buf[5];
-       dev->bDeviceProtocoll = buf[6];
-       dev->idVendor = (u16) (buf[9] << 8) | (buf[8]);
-       dev->idProduct = (u16) (buf[11] << 8) | (buf[10]);
-       dev->bcdDevice = (u16) (buf[13] << 8) | (buf[12]);
-       dev->iManufacturer = buf[14];
-       dev->iProduct = buf[15];
-       dev->iSerialNumber = buf[16];
-       dev->bNumConfigurations = buf[17];
+       usb_get_descriptor(dev, DEVICE, 0, gbuf, gbuf[0]);
+
+       dev->bLength = gbuf[0];
+       dev->bDescriptorType = gbuf[1];
+       dev->bcdUSB = (u16) (gbuf[3] << 8 | gbuf[2]);
+       dev->bDeviceClass = gbuf[4];
+       dev->bDeviceSubClass = gbuf[5];
+       dev->bDeviceProtocoll = gbuf[6];
+       dev->idVendor = (u16) (gbuf[9] << 8) | (gbuf[8]);
+       dev->idProduct = (u16) (gbuf[11] << 8) | (gbuf[10]);
+       dev->bcdDevice = (u16) (gbuf[13] << 8) | (gbuf[12]);
+       dev->iManufacturer = gbuf[14];
+       dev->iProduct = gbuf[15];
+       dev->iSerialNumber = gbuf[16];
+       dev->bNumConfigurations = gbuf[17];
 
        return 0;
 }
 
-s8 usb_get_desc_configuration(struct usb_device *dev, u8 index, u8 *buf, u8 size)
+s8 usb_get_desc_configuration(struct usb_device *dev, u8 index)
 {
-       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];
+       cleargbuf();
+       usb_get_descriptor(dev, CONFIGURATION, index, gbuf, 8);
+       usb_get_descriptor(dev, CONFIGURATION, index, gbuf, gbuf[0]);
+
+       dev->conf->bLength = gbuf[0];
+       dev->conf->bDescriptorType = gbuf[1];
+       dev->conf->wTotalLength = (u16) (gbuf[3] << 8 | gbuf[2]);
+       dev->conf->bNumInterfaces = gbuf[4];
+       dev->conf->bConfigurationValue = gbuf[5];
+       dev->conf->iConfiguration = gbuf[6];
+       dev->conf->bmAttributes = gbuf[7];
+       dev->conf->bMaxPower = gbuf[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, 9);
+/* returns more information about CONFIGURATION, including
+ * INTERFACE(s) and ENDPOINT(s)
+ * usb_get_desc_configuration() must be called for this device before
+ */
+s8 usb_get_desc_config_ext(struct usb_device *dev, u8 index) {
+       cleargbuf();
+
+       usb_get_desc_configuration(dev, index);
+       usb_get_descriptor(dev, CONFIGURATION, index, gbuf, dev->conf->wTotalLength);
+
 
+       printf("=============\nafter usb_get_desc_config_ext:\n");
+       hexdump((void*) gbuf, dev->conf->wTotalLength);
        return 0;
 }
 
 s8 usb_set_address(struct usb_device *dev, u8 address)
 {
-       u8 buf[64];
-       usb_control_msg(dev, 0x00, SET_ADDRESS, address, 0, 0, buf, 0);
+       cleargbuf();
+       usb_control_msg(dev, 0x00, SET_ADDRESS, address, 0, 0, gbuf, 0);
        return 0;
 }
 
 s8 usb_set_configuration(struct usb_device *dev, u8 configuration)
 {
-       u8 buf[64];
-       usb_control_msg(dev, 0x00, SET_CONFIGURATION, configuration, 0, 0, buf, 0);
+       cleargbuf();
+       usb_control_msg(dev, 0x00, SET_CONFIGURATION, configuration, 0, 0, gbuf, 0);
        return 0;
 }
 
index c6b20c867a7f8cba96603a26909c50c7c31d9ef8..b4c2db10334e0fb915a6ec0ce099ead96ff3e6fb 100644 (file)
@@ -61,13 +61,13 @@ 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_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);
+s8 usb_get_desc_dev_simple(struct usb_device *dev);
+s8 usb_get_desc_dev(struct usb_device *dev);
+s8 usb_get_desc_configuration(struct usb_device *dev, u8 index);
+s8 usb_get_desc_config_ext(struct usb_device *dev, u8 index);
 
-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);
+char *usb_get_string_simple(struct usb_device *dev, u8 index);
+s8 usb_get_string(struct usb_device *dev, u8 index, u8 langid);
 
 s8 usb_set_address(struct usb_device *dev, u8 address);
 s8 usb_set_configuration(struct usb_device *dev, u8 configuration);