m00h @ previous commit: using wTotalLenght of CONFIGURATION you get
[ppcskel.git] / usb / core / usb.c
index 1665abf131ff4b1ba75ea1f28f4be57fa9cecc07..95110672b1142f434950153ec4f9cec8c7b7dd97 100644 (file)
  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-//wtf??!
-//#ifndef _USB_H_
-//#define _USB_H_
-
-//#include <stdlib.h>
-
 #include "usb.h"
 #include "core.h"
 #include "../host/host.h"
 #include "../usbspec/usb11spec.h"
 #include "../../malloc.h"
+#include "../../string.h"
 
 
 /******************* 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;
@@ -72,12 +67,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;
@@ -87,40 +82,15 @@ usb_device * usb_open_class(u8 class)
        return NULL;
 }
 
-/**
- * Close device after a communication.
+/* Close device after a communication.
  */
-u8 usb_close(usb_device *dev)
-{
-
-       return 0;
-}
-
-u8 usb_get_device_descriptor(usb_device *dev, char *buf,u8 size)
-{
-
-       return 0;
-}
-
-u8 usb_set_address(usb_device *dev, u8 address)
-{
-
-       return 0;
-}
-
-u8 usb_set_configuration(usb_device *dev, u8 configuration)
-{
-
-       return 0;
-}
-
-u8 usb_set_altinterface(usb_device *dev, u8 alternate)
+s8 usb_close(struct usb_device *dev)
 {
 
        return 0;
 }
 
-u8 usb_reset(usb_device *dev)
+s8 usb_reset(struct usb_device *dev)
 {
 
 
@@ -133,39 +103,24 @@ u8 usb_reset(usb_device *dev)
 /**
  * Create a control transfer.
  */
-u8 usb_control_msg(usb_device *dev, u8 requesttype, u8 request, u16 value, u16 index, u16 length,char *buf, u16 size, u16 timeout)
+s8 usb_control_msg(struct usb_device *dev, u8 requesttype, u8 request,
+               u16 value, u16 index, u16 length, u8 *buf, u16 timeout)
 {
-       //usb_control_msg(dev, 0x80, GET_DESCRIPTOR, 1, 0, 8, buf, 8, 0);
-       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;
        
        irp->epsize = dev->bMaxPacketSize0;
        irp->type = USB_CTRL;
 
-#if 1
-       buf[0]=(char)requesttype;
-       buf[1]=(char)request;            
-       buf[2]=(char)(value >> 8);
-       buf[3]=(char)(value);           
-       buf[4]=(char)(index >> 8);
-       buf[5]=(char)(index);   
-       // lenght buf are the only where the order is inverted
-       buf[6]=(char)(length);
-       buf[7]=(char)(length >> 8);
-#endif
-#if 0
-       //should be the right way around? :O
-       buf[0]=(char)requesttype;
-       buf[1]=(char)request;
-       buf[2]=(char)(value);
-       buf[3]=(char)(value >> 8);
-       buf[4]=(char)(index);
-       buf[5]=(char)(index >> 8);
-       // lenght buf are the only where the order is inverted
-       buf[6]=(char)(length);
-       buf[7]=(char)(length >> 8);
-#endif
+       buf[0]=(u8)requesttype;
+       buf[1]=(u8)request;
+       buf[2]=(u8)(value);
+       buf[3]=(u8)(value >> 8);
+       buf[4]=(u8)(index);
+       buf[5]=(u8)(index >> 8);
+       buf[6]=(u8)(length);
+       buf[7]=(u8)(length >> 8);
 
        irp->buffer = buf;
        irp->len = length;
@@ -178,34 +133,142 @@ u8 usb_control_msg(usb_device *dev, u8 requesttype, u8 request, u16 value, u16 i
 }
 
 
-u8 usb_get_string(usb_device *dev, u8 index, u8 langid, char *buf, u8 buflen)
+s8 usb_get_string(struct usb_device *dev, u8 index, u8 langid, u8 *buf, u8 buflen)
+{
+
+       return 0;
+}
+
+
+char *usb_get_string_simple(struct usb_device *dev, u8 index, u8 *buf, u8 size)
+{
+       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);
+
+       char *str = (char*)malloc((size/2));
+       memset(str, '\0', (size/2));
+
+       u16 i;
+       for(i=0; i<(size/2)-1; i++) {
+               str[i] = buf[2+(i*2)];
+               printf("%c", str[i]);
+       }
+       printf("\n");
+
+       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)
+{
+       if(size < 8) {
+               return -1;
+       }
+       usb_get_descriptor(dev, DEVICE, 0, buf, 8);
+
+       if(!buf[7]) {
+               printf("FU: %d\n", buf[7]);
+               return -2;
+       }
+       dev->bMaxPacketSize0 = buf[7];
+       return 0;
+}
+
+s8 usb_get_desc_dev(struct usb_device *dev, u8 *buf, u8 size)
 {
+       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);
+
+       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];
+
+       return 0;
+}
 
+s8 usb_get_desc_configuration(struct usb_device *dev, u8 index, u8 *buf, u8 size)
+{
+       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, 9);
+
+       return 0;
+}
 
-u8 usb_get_string_simple(usb_device *dev, u8 index, char *buf, u8 buflen)
+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);
+       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);
        return 0;
 }
 
-u8 usb_get_descriptor(usb_device *dev, unsigned char type, unsigned char index, void *buf, u8 size)
+s8 usb_set_altinterface(struct usb_device *dev, u8 alternate)
 {
 
        return 0;
 }
 
 
+
 /******************* Bulk Transfer **********************/
 
 /**
  * Write to an a bulk endpoint.
  */
-u8 usb_bulk_write(usb_device *dev, u8 ep, char *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;
        
@@ -226,9 +289,9 @@ u8 usb_bulk_write(usb_device *dev, u8 ep, char *buf, u8 size, u8 timeout)
 /**
  * Read from an bulk endpoint.
  */
-u8 usb_bulk_read(usb_device *dev, u8 ep, char *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;
        
@@ -251,7 +314,7 @@ u8 usb_bulk_read(usb_device *dev, u8 ep, char *buf, u8 size, u8 timeout)
 /**
  * Write to an interrupt endpoint.
  */
-u8 usb_interrupt_write(usb_device *dev, u8 ep, char *buf, u8 size, u8 timeout)
+s8 usb_interrupt_write(struct usb_device *dev, u8 ep, u8 *buf, u8 size, u8 timeout)
 {
 
        return 0;
@@ -260,7 +323,7 @@ u8 usb_interrupt_write(usb_device *dev, u8 ep, char *buf, u8 size, u8 timeout)
 /**
  * Read from an interrupt endpoint.
  */
-u8 usb_interrupt_read(usb_device *dev, u8 ep, char *buf, u8 size, u8 timeout)
+s8 usb_interrupt_read(struct usb_device *dev, u8 ep, u8 *buf, u8 size, u8 timeout)
 {
 
        return 0;
@@ -272,7 +335,7 @@ u8 usb_interrupt_read(usb_device *dev, u8 ep, char *buf, u8 size, u8 timeout)
 /**
  * Write to an isochron endpoint.
  */
-u8 usb_isochron_write(usb_device *dev, u8 ep, char *buf, u8 size, u8 timeout)
+s8 usb_isochron_write(struct usb_device *dev, u8 ep, u8 *buf, u8 size, u8 timeout)
 {
 
        return 0;
@@ -281,7 +344,7 @@ u8 usb_isochron_write(usb_device *dev, u8 ep, char *buf, u8 size, u8 timeout)
 /**
  * Read from an isochron endpoint.
  */
-u8 usb_isochron_read(usb_device *dev, u8 ep, char *buf, u8 size, u8 timeout)
+s8 usb_isochron_read(struct usb_device *dev, u8 ep, u8 *buf, u8 size, u8 timeout)
 {