some usb stack related clean up; BULK still untested
[ppcskel.git] / usb / core / usb.c
index fd47cdc538b74703314388dc9cf845c440229394..45ceba2ea51a4175010c4c774c858227717dec12 100644 (file)
  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+/*
+       ppcskel - a Free Software replacement for the Nintendo/BroadOn bootloader.
+       libusb like interface
+
+Copyright (C) 2009     Bernhard Urban <lewurm@gmx.net>
+Copyright (C) 2009     Sebastian Falbesoner <sebastian.falbesoner@gmail.com>
+
+# This code is licensed to you under the terms of the GNU GPL, version 2;
+# see file COPYING or http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
+*/
+
 #include "usb.h"
 #include "core.h"
 #include "../host/host.h"
 #include "../../malloc.h"
 #include "../../string.h"
 
-#define cleargbuf() memset(gbuf, 0, 0xff)
+#define cleargbuf() memset(gbuf, 0, 0xffff)
 /* internal global buffer */
-static u8 gbuf[0xff];
-
+static u8 gbuf[0xffff];
 
 /******************* Device Operations **********************/
-
 /**
  * Open a device with verndor- and product-id for a communication.
  */
@@ -103,19 +112,20 @@ s8 usb_reset(struct usb_device *dev)
 
 
 /******************* Control Transfer **********************/
-
 /**
  * Create a control transfer.
  */
 s8 usb_control_msg(struct usb_device *dev, u8 requesttype, u8 request,
                u16 value, u16 index, u16 length, u8 *buf, u16 timeout)
 {
-       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;
+       static struct usb_irp irp;
+       memset(&irp, 0, sizeof(struct usb_irp));
+
+       irp.dev = dev;
+       irp.endpoint = 0;
+
+       irp.epsize = dev->bMaxPacketSize0;
+       irp.type = USB_CTRL;
 
        buf[0]=(u8)requesttype;
        buf[1]=(u8)request;
@@ -126,12 +136,11 @@ s8 usb_control_msg(struct usb_device *dev, u8 requesttype, u8 request,
        buf[6]=(u8)(length);
        buf[7]=(u8)(length >> 8);
 
-       irp->buffer = buf;
-       irp->len = length;
-       irp->timeout = timeout;
+       irp.buffer = buf;
+       irp.len = length;
+       irp.timeout = timeout;
 
-       usb_submit_irp(irp);
-       free(irp);
+       usb_submit_irp(&irp);
 
        return 0;
 }
@@ -160,9 +169,7 @@ char *usb_get_string_simple(struct usb_device *dev, u8 index)
        u16 i;
        for(i=0; i<(gbuf[0]/2)-1; i++) {
                str[i] = gbuf[2+(i*2)];
-               printf("%c", str[i]);
        }
-       printf("\n");
 
        return str;
 }
@@ -205,23 +212,33 @@ s8 usb_get_desc_dev(struct usb_device *dev)
        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_desc_configuration(struct usb_device *dev, u8 index)
+s8 usb_get_desc_configuration(struct usb_device *dev, u8 index, struct usb_conf *conf)
 {
        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];
+       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;
 }
@@ -230,15 +247,56 @@ s8 usb_get_desc_configuration(struct usb_device *dev, u8 index)
  * 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) {
+s8 usb_get_desc_config_ext(struct usb_device *dev, u8 index, struct usb_conf *conf)
+{
        cleargbuf();
 
-       usb_get_desc_configuration(dev, index);
+       usb_get_desc_configuration(dev, index, conf);
        usb_get_descriptor(dev, CONFIGURATION, index, gbuf, dev->conf->wTotalLength);
 
-
-       printf("=============\nafter usb_get_desc_config_ext:\n");
-       hexdump((void*) 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;
+               }
+       }
        return 0;
 }
 
@@ -246,13 +304,27 @@ s8 usb_set_address(struct usb_device *dev, u8 address)
 {
        cleargbuf();
        usb_control_msg(dev, 0x00, SET_ADDRESS, address, 0, 0, gbuf, 0);
+       wait_ms(210);
        return 0;
 }
 
+
+u8 usb_get_configuration(struct usb_device *dev)
+{
+       cleargbuf();
+       usb_control_msg(dev, 0x80, GET_CONFIGURATION, 0, 0, 4, gbuf, 0);
+       printf("=============\nafter usb_get_configuration:\n");
+       hexdump((void*) gbuf, 8);
+       return gbuf[0];
+}
+
 s8 usb_set_configuration(struct usb_device *dev, u8 configuration)
 {
        cleargbuf();
        usb_control_msg(dev, 0x00, SET_CONFIGURATION, configuration, 0, 0, gbuf, 0);
+       printf("=============\nafter usb_set_configuration:\n");
+       hexdump((void*) gbuf, 8);
+       wait_ms(20);
        return 0;
 }
 
@@ -262,31 +334,30 @@ s8 usb_set_altinterface(struct usb_device *dev, u8 alternate)
        return 0;
 }
 
+static s8 usb_gen_rw(struct usb_device *dev, u8 ep, u8 *buf, u8 size, u8 timeout, u8 type) {
+       static struct usb_irp irp;
+       memset(&irp, 0, sizeof(struct usb_irp));
 
+       irp.dev = dev;
+       irp.endpoint = ep;
+       irp.epsize = dev->epSize[ep & 0x7F];
+       irp.type = type;
 
-/******************* Bulk Transfer **********************/
+       irp.buffer = buf;
+       irp.len = size;
+       irp.timeout = timeout;
 
+       usb_submit_irp(&irp);
+       return 0;
+}
+
+/******************* Bulk Transfer **********************/
 /**
  * Write to an a bulk endpoint.
  */
 s8 usb_bulk_write(struct usb_device *dev, u8 ep, u8 *buf, u8 size, u8 timeout)
 {
-       struct usb_irp * irp = (struct usb_irp*)malloc(sizeof(struct usb_irp));
-       irp->dev = dev;
-       //irp->devaddress = dev->address;
-       
-       irp->endpoint = ep;
-       irp->epsize = dev->epSize[ep]; // ermitteln
-       irp->type = USB_BULK;
-
-       irp->buffer = buf;
-       irp->len = size;
-       irp->timeout = timeout;
-
-       usb_submit_irp(irp);
-       free(irp);
-
-       return 0;
+       return usb_gen_rw(dev, ep, buf, size, timeout, USB_BULK);
 }
 
 /**
@@ -294,22 +365,7 @@ s8 usb_bulk_write(struct 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)
 {
-       struct usb_irp * irp = (struct usb_irp*)malloc(sizeof(struct usb_irp));
-       //irp->devaddress = dev->address;
-       irp->dev = dev;
-       
-       irp->endpoint = ep | 0x80;      // from device to host
-       irp->epsize = dev->epSize[ep]; // ermitteln
-       irp->type = USB_BULK;
-
-       irp->buffer = buf;
-       irp->len = size;
-       irp->timeout = timeout;
-
-       usb_submit_irp(irp);
-       free(irp);
-
-       return 0;
+       return usb_gen_rw(dev, ep|0x80, buf, size, timeout, USB_BULK);
 }
 
 
@@ -319,8 +375,7 @@ s8 usb_bulk_read(struct 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;
+       return usb_gen_rw(dev, ep, buf, size, timeout, USB_INTR);
 }
 
 /**
@@ -328,8 +383,7 @@ s8 usb_interrupt_write(struct usb_device *dev, u8 ep, u8 *buf, u8 size, u8 timeo
  */
 s8 usb_interrupt_read(struct usb_device *dev, u8 ep, u8 *buf, u8 size, u8 timeout)
 {
-
-       return 0;
+       return usb_gen_rw(dev, ep|0x80, buf, size, timeout, USB_INTR);
 }
 
 
@@ -354,5 +408,3 @@ s8 usb_isochron_read(struct usb_device *dev, u8 ep, u8 *buf, u8 size, u8 timeout
        return 0;
 }
 
-
-//#endif       //_USB_H_