added lsusb and some more config stuff. except of some (simple) setter
[ppcskel.git] / usb / core / usb.c
index 7a495c03caf3baf6e88b332d0c6833b954396b9a..6a7562e60a90afdbe84b46ffc3331e59dfed2586 100644 (file)
 #include "../../malloc.h"
 #include "../../string.h"
 
+#define cleargbuf() memset(gbuf, 0, 0xffff)
+/* internal global buffer */
+static u8 gbuf[0xffff];
 
 /******************* Device Operations **********************/
-
 /**
  * Open a device with verndor- and product-id for a communication.
  */
-usb_device *usb_open(u32 vendor_id, u32 product_id)
+struct usb_device *usb_open(u32 vendor_id, u32 product_id)
 {
-       usb_device* dev;
-       element * iterator = core.devices->head;
+       struct usb_device* dev;
+       struct element * iterator = core.devices->head;
        while(iterator != NULL) {
-               dev = (usb_device*)iterator->data;
+               dev = (struct usb_device*)iterator->data;
                
                if(dev->idVendor==vendor_id&&dev->idProduct==product_id)
                        return dev;
@@ -67,12 +69,12 @@ usb_device *usb_open(u32 vendor_id, u32 product_id)
 /**
  * Open a device with an class code for a communication.
  */
-usb_device *usb_open_class(u8 class)
+struct usb_device *usb_open_class(u8 class)
 {
-       usb_device* dev;
-       element * iterator = core.devices->head;
+       struct usb_device* dev;
+       struct element * iterator = core.devices->head;
        while(iterator != NULL) {
-               dev = (usb_device*)iterator->data;
+               dev = (struct usb_device*)iterator->data;
                
                if(dev->bDeviceClass==class)
                        return dev;
@@ -84,13 +86,13 @@ usb_device *usb_open_class(u8 class)
 
 /* Close device after a communication.
  */
-s8 usb_close(usb_device *dev)
+s8 usb_close(struct usb_device *dev)
 {
 
        return 0;
 }
 
-s8 usb_reset(usb_device *dev)
+s8 usb_reset(struct usb_device *dev)
 {
 
 
@@ -103,10 +105,10 @@ s8 usb_reset(usb_device *dev)
 /**
  * Create a control transfer.
  */
-s8 usb_control_msg(usb_device *dev, u8 requesttype, u8 request,
+s8 usb_control_msg(struct usb_device *dev, u8 requesttype, u8 request,
                u16 value, u16 index, u16 length, u8 *buf, u16 timeout)
 {
-       usb_irp *irp = (usb_irp*)malloc(sizeof(usb_irp));
+       struct usb_irp *irp = (struct usb_irp*)malloc(sizeof(struct usb_irp));
        irp->dev = dev;
        irp->endpoint = 0;
        
@@ -132,108 +134,179 @@ s8 usb_control_msg(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(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(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)];
-               printf("%c", str[i]);
+       for(i=0; i<(gbuf[0]/2)-1; i++) {
+               str[i] = gbuf[2+(i*2)];
        }
-       printf("\n");
 
        return str;
 }
 
-s8 usb_get_descriptor(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_dev_desc_simple(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_dev_desc(usb_device *dev, u8 *buf, u8 size)
+s8 usb_get_desc_dev(struct usb_device *dev)
 {
-       if (size < 0x12 || usb_get_dev_desc_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];
+
+       u8 i;
+       struct usb_conf *conf = dev->conf = (struct usb_conf*) malloc(sizeof(struct usb_conf));
+       for(i=0; i <= dev->bNumConfigurations; i++) {
+               if(i!=0) {
+                       conf = (struct usb_conf*) malloc(sizeof(struct usb_conf));
+               }
+               usb_get_desc_config_ext(dev, i, conf);
+       }
 
        return 0;
 }
 
-s8 usb_get_configuration(usb_device *dev, u8 index, u8 *buf, u8 size)
+s8 usb_get_desc_configuration(struct usb_device *dev, u8 index, struct usb_conf *conf)
 {
-       if(size < 8) {
-               return -1;
-       }
-       usb_get_descriptor(dev, CONFIGURATION, index, buf, 8);
-       usb_get_descriptor(dev, CONFIGURATION, index, buf, size >= buf[0] ? buf[0] : size);
+       cleargbuf();
+       usb_get_descriptor(dev, CONFIGURATION, index, gbuf, 8);
+       usb_get_descriptor(dev, CONFIGURATION, index, gbuf, gbuf[0]);
+
+       conf->bLength = gbuf[0];
+       conf->bDescriptorType = gbuf[1];
+       conf->wTotalLength = (u16) (gbuf[3] << 8 | gbuf[2]);
+       conf->bNumInterfaces = gbuf[4];
+       conf->bConfigurationValue = gbuf[5];
+       conf->iConfiguration = gbuf[6];
+       conf->bmAttributes = gbuf[7];
+       conf->bMaxPower = gbuf[8];
+       conf->intf = NULL;
+
        return 0;
 }
 
-s8 usb_set_address(usb_device *dev, u8 address)
+/* 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, struct usb_conf *conf)
 {
-       u8 buf[64];
-       usb_control_msg(dev, 0x00, SET_ADDRESS, address, 0, 0, buf, 0);
+       cleargbuf();
+
+       usb_get_desc_configuration(dev, index, conf);
+       usb_get_descriptor(dev, CONFIGURATION, index, gbuf, dev->conf->wTotalLength);
+
+       u8 i,j,off=9;
+       struct usb_intf *ifs = dev->conf->intf = (struct usb_intf*) malloc(sizeof(struct usb_intf));
+       for(i=1; i <= dev->conf->bNumInterfaces; i++) {
+               if(i!=1) {
+                       ifs->next = (struct usb_intf*) malloc(sizeof(struct usb_intf));
+                       ifs = ifs->next;
+               }
+               ifs->bLength = gbuf[off+0];
+               ifs->bDescriptorType = gbuf[off+1];
+               ifs->bInterfaceNumber = gbuf[off+2];
+               ifs->bAlternateSetting = gbuf[off+3];
+               ifs->bNumEndpoints = gbuf[off+4];
+               ifs->bInterfaceClass = gbuf[off+5];
+               ifs->bInterfaceSubClass = gbuf[off+6];
+               ifs->bInterfaceProtocol = gbuf[off+7];
+               ifs->iInterface = gbuf[off+8];
+
+               off += 9;
+
+               struct usb_endp *ep = ifs->endp = (struct usb_endp*) malloc(sizeof(struct usb_endp));
+               for(j=1; j <= ifs->bNumEndpoints; j++) {
+                       /* skip HID Device Descriptor (see lsusb) */
+                       if(gbuf[off+1] == 33) {
+                               j--;
+                               off += 9;
+                               continue;
+                       }
+
+                       if(j!=1) {
+                               ep->next = (struct usb_endp*) malloc(sizeof(struct usb_endp));
+                               ep = ep->next;
+                       }
+
+                       ep->bLength = gbuf[off+0];
+                       ep->bDescriptorType = gbuf[off+1];
+                       ep->bEndpointAddress = gbuf[off+2];
+                       ep->bmAttributes = gbuf[off+3];
+                       ep->wMaxPacketSize = (u16) ((gbuf[off+5] << 8) | (gbuf[off+4]));
+                       ep->bInterval = gbuf[off+6];
+
+                       off += 7;
+               }
+       }
+
+       printf("=============\nafter usb_get_desc_config_ext:\n");
+       hexdump((void*) gbuf, dev->conf->wTotalLength);
        return 0;
 }
 
-s8 usb_set_configuration(usb_device *dev, u8 configuration)
+s8 usb_set_address(struct usb_device *dev, u8 address)
 {
+       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)
+{
+       cleargbuf();
+       usb_control_msg(dev, 0x00, SET_CONFIGURATION, configuration, 0, 0, gbuf, 0);
        return 0;
 }
 
-s8 usb_set_altinterface(usb_device *dev, u8 alternate)
+s8 usb_set_altinterface(struct usb_device *dev, u8 alternate)
 {
 
        return 0;
@@ -246,9 +319,9 @@ s8 usb_set_altinterface(usb_device *dev, u8 alternate)
 /**
  * Write to an a bulk endpoint.
  */
-s8 usb_bulk_write(usb_device *dev, u8 ep, u8 *buf, u8 size, u8 timeout)
+s8 usb_bulk_write(struct usb_device *dev, u8 ep, u8 *buf, u8 size, u8 timeout)
 {
-       usb_irp * irp = (usb_irp*)malloc(sizeof(usb_irp));
+       struct usb_irp * irp = (struct usb_irp*)malloc(sizeof(struct usb_irp));
        irp->dev = dev;
        //irp->devaddress = dev->address;
        
@@ -269,9 +342,9 @@ s8 usb_bulk_write(usb_device *dev, u8 ep, u8 *buf, u8 size, u8 timeout)
 /**
  * Read from an bulk endpoint.
  */
-s8 usb_bulk_read(usb_device *dev, u8 ep, u8 *buf, u8 size, u8 timeout)
+s8 usb_bulk_read(struct usb_device *dev, u8 ep, u8 *buf, u8 size, u8 timeout)
 {
-       usb_irp * irp = (usb_irp*)malloc(sizeof(usb_irp));
+       struct usb_irp * irp = (struct usb_irp*)malloc(sizeof(struct usb_irp));
        //irp->devaddress = dev->address;
        irp->dev = dev;
        
@@ -294,7 +367,7 @@ s8 usb_bulk_read(usb_device *dev, u8 ep, u8 *buf, u8 size, u8 timeout)
 /**
  * Write to an interrupt endpoint.
  */
-s8 usb_interrupt_write(usb_device *dev, u8 ep, u8 *buf, u8 size, u8 timeout)
+s8 usb_interrupt_write(struct usb_device *dev, u8 ep, u8 *buf, u8 size, u8 timeout)
 {
 
        return 0;
@@ -303,7 +376,7 @@ s8 usb_interrupt_write(usb_device *dev, u8 ep, u8 *buf, u8 size, u8 timeout)
 /**
  * Read from an interrupt endpoint.
  */
-s8 usb_interrupt_read(usb_device *dev, u8 ep, u8 *buf, u8 size, u8 timeout)
+s8 usb_interrupt_read(struct usb_device *dev, u8 ep, u8 *buf, u8 size, u8 timeout)
 {
 
        return 0;
@@ -315,7 +388,7 @@ s8 usb_interrupt_read(usb_device *dev, u8 ep, u8 *buf, u8 size, u8 timeout)
 /**
  * Write to an isochron endpoint.
  */
-s8 usb_isochron_write(usb_device *dev, u8 ep, u8 *buf, u8 size, u8 timeout)
+s8 usb_isochron_write(struct usb_device *dev, u8 ep, u8 *buf, u8 size, u8 timeout)
 {
 
        return 0;
@@ -324,7 +397,7 @@ s8 usb_isochron_write(usb_device *dev, u8 ep, u8 *buf, u8 size, u8 timeout)
 /**
  * Read from an isochron endpoint.
  */
-s8 usb_isochron_read(usb_device *dev, u8 ep, u8 *buf, u8 size, u8 timeout)
+s8 usb_isochron_read(struct usb_device *dev, u8 ep, u8 *buf, u8 size, u8 timeout)
 {