added lsusb and some more config stuff. except of some (simple) setter
[ppcskel.git] / usb / core / core.c
index 56a0b5763a42b8f4301ad01b6f78c820129a4b48..c3574283e0610562cfc13bf7b26d61aad61e8d0a 100644 (file)
@@ -71,10 +71,10 @@ u8 usb_next_address()
 void usb_periodic()
 {
        // call ever registered driver  
-       usb_driver *drv;
-       element *iterator = core.drivers->head;
+       struct usb_driver *drv;
+       struct element *iterator = core.drivers->head;
        while (iterator != NULL) {
-               drv = (usb_driver *) iterator->data;
+               drv = (struct usb_driver *) iterator->data;
                drv->check();
                iterator = iterator->next;
        }
@@ -86,9 +86,10 @@ void usb_periodic()
  * for the core. usb_add_device expected that
  * the device answers to address zero.
  */
-usb_device *usb_add_device()
+struct usb_device *usb_add_device()
 {
-       usb_device *dev = (usb_device *) malloc(sizeof(usb_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;
@@ -101,128 +102,26 @@ 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;
-       u8 dev_desc_size;
-       memset(buf, 0, sizeof(buf));
-       ret = usb_get_dev_desc_simple(dev, buf, sizeof(buf));
-       printf("=============\nbuf: 0x%08X\nafter usb control msg(ret: %d):\n", buf, ret);
-       hexdump(buf, sizeof(buf));
+       ret = usb_get_desc_dev_simple(dev);
+       if(ret < 0) {
+               return (void*) -1;
+       }
 
        /* set MaxPacketSize */
-       dev->bMaxPacketSize0 = (u8) buf[7];
-       if(!(u8)buf[7]) {
-               printf("FU\n");
-               return (void*)1;
-       }
-       /* save device descriptor size */
-       dev_desc_size = buf[0];
 
        u8 address = usb_next_address();
        ret = usb_set_address(dev, address);
        dev->address = address;
        printf("set address to %d\n", dev->address);
 
-       memset(buf, 0, sizeof(buf));
-       ret = usb_get_dev_desc(dev, buf, sizeof(buf), dev_desc_size);
-       printf("=============\nbuf: 0x%08X\nafter usb control msg(ret: %d):\n", buf, ret);
-       hexdump(buf, sizeof(buf));
-
-       dev->bDeviceClass = (u8) buf[4];
-       dev->bDeviceSubClass = (u8) buf[5];
-       dev->bDeviceProtocoll = (u8) 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]);
-
-       printf( "bDeviceClass 0x%02X\n"
-                       "bDeviceSubClass 0x%02X\n"
-                       "bDeviceProtocoll 0x%02X\n"
-                       "idVendor 0x%04X\n"
-                       "idProduct 0x%04X\n"
-                       "bcdDevice 0x%04X\n", dev->bDeviceClass,
-                       dev->bDeviceSubClass, dev->bDeviceProtocoll,
-                       dev->idVendor, dev->idProduct, dev->bcdDevice);
-
-       /*
-       usb_get_descriptor(dev, DEVICE, 0, buf, 8);
-       memset(buf, 0, 8);
-       usb_get_descriptor(dev, DEVICE, 0, buf, size >= buf[0] ? buf[0] : size);
-       */
-#if 0
-       memset(buf, 0, sizeof(buf));
-       usb_control_msg(dev, 0x80, GET_DESCRIPTOR, (DEVICE << 8) | 0, 0, 8, buf, 0);
-       printf("=============\nbuf: 0x%08X\nafter usb control msg:\n", buf);
-       hexdump(buf, sizeof(buf));
-
-       memset(buf, 0, sizeof(buf));
-       usb_control_msg(dev, 0x80, GET_DESCRIPTOR, (DEVICE << 8) | 0, 0, buf[0], buf, 0);
-       printf("=============\nbuf: 0x%08X\nafter usb control msg:\n", buf);
-       hexdump(buf, sizeof(buf));
-
-       memset(buf, 0, sizeof(buf));
-       usb_get_string_simple(dev, 1, buf);
-       printf("=============\nbuf: 0x%08X\nafter usb control msg:\n", buf);
-       hexdump(buf, sizeof(buf));
-#endif
-
-#if 0
-       u8 devdescr_size;
-
-       /* setup real ep0 fifo size */
-       dev->bMaxPacketSize0 = (u8) buf[7];
-       if(!(u8)buf[7]) {
-               printf("FU\n");
-               return (void*)1;
-       }
-
-       /* save real length of device descriptor */
-       devdescr_size = (u8) buf[0];
-
-       /* define new adress */
-       memset(buf, 0, sizeof(buf));
-       usb_control_msg(dev, 0x00, SET_ADDRESS, address, 0, 0, buf, 8, 0);
-       dev->address = address;
-       printf("=============\nbuf: 0x%08X\nafter usb control msg:\n", buf);
-       hexdump(buf, sizeof(buf));
-       printf("address: %d\n", address);
-
-
-       /* get complete device descriptor */
-       memset(buf, 0, sizeof(buf));
-       usb_control_msg(dev, 0x80, GET_DESCRIPTOR, DEVICE<<8, 0, devdescr_size, buf, 8, 0);
-
-       printf("=============\nbuf: 0x%08X\nafter usb control msg:\n", buf);
-       hexdump(buf, sizeof(buf));
-
-       /* save only really neccessary values for this small usbstack */
-#endif
-
-#if 0
-       memset(buf, 0, sizeof(buf));
-       usb_control_msg(dev, 0x80, GET_DESCRIPTOR, (STRING<<8)|2, 0, 0x1a, buf, 8, 0);
-       hexdump(buf, sizeof(buf));
-       printf("String Descriptor [1]: ");
-       u8 i;
-       for (i=2; i<buf[0]; i+=2)
-               printf("%c", buf[i]);
-       printf("\n");
-#endif
+       ret = usb_get_desc_dev(dev);
 
-       /*
-       usb_control_msg(dev, 0x80, GET_DESCRIPTOR, (STRING<<8) | 2, 0, 0x20, buf, 8, 0);
-       printf("String Descriptor [2]: ");
-       for (i=2; i<buf[0]; i+=2)
-               printf("%c", buf[i]);
-       printf("\n");
-       */
+       /* select configuration */
+       ret = usb_set_configuration(dev, dev->conf->bConfigurationValue);
+       printf("=============\nusb_set_configuration(ret: %d) %d\n", ret, dev->conf->bConfigurationValue);
 
-       // string descriptoren werden nicht im arbeitsspeicher gehalten -> on demand mit 
-       // entprechenden funktionen
-       // hier muss man noch mehr abholen, konfigurationene, interfaces und endpunkte
+       lsusb(dev);
 
 #if 0
        /* add device to device list */
@@ -236,11 +135,74 @@ usb_device *usb_add_device()
        return dev;
 }
 
+void lsusb(struct usb_device *dev)
+{
+       printf("=== Device Descriptor === \n");
+       printf("bLength 0x%02X\n", dev->bLength);
+       printf("bDescriptorType 0x%02X\n", dev->bDeviceClass);
+       printf("bcdUSB 0x%02X\n", dev->bcdUSB);
+       printf("bDeviceClass 0x%02X\n", dev->bDeviceClass);
+       printf("bDeviceSubClass 0x%02X\n", dev->bDeviceSubClass);
+       printf("bDeviceProtocoll 0x%02X\n", dev->bDeviceProtocoll);
+       printf("idVendor 0x%04X\n", dev->idVendor);
+       printf("idProduct 0x%04X\n", dev->idProduct);
+       printf("bcdDevice 0x%04X\n", dev->bcdDevice);
+       printf("iManufacturer(0x%02X): \"%s\"\n", dev->iManufacturer, dev->iManufacturer ? usb_get_string_simple(dev, dev->iManufacturer) : "no String");
+       printf("iProduct(0x%02X): \"%s\"\n", dev->iProduct, dev->iProduct ? usb_get_string_simple(dev, dev->iProduct) : "no String");
+       printf("iSerialNumber(0x%02X): \"%s\"\n", dev->iSerialNumber, dev->iSerialNumber ? usb_get_string_simple(dev, dev->iSerialNumber) : "no String");
+       printf("bNumConfigurations 0x%02X\n", dev->bNumConfigurations);
+       
+       u8 c, i, e;
+       struct usb_conf *conf = dev->conf;
+       for(c=0; c <= dev->bNumConfigurations; c++) {
+               printf("  === Configuration Descriptor %d ===\n", c+1);
+               printf("  bLength 0x%02X\n", conf->bLength);
+               printf("  bDescriptorType 0x%02X\n", conf->bDescriptorType);
+               printf("  wTotalLength 0x%04X\n", conf->wTotalLength);
+               printf("  bNumInterfaces 0x%02X\n", conf->bNumInterfaces);
+               printf("  bConfigurationValue 0x%02X\n", conf->bConfigurationValue);
+               printf("  iConfiguration (0x%02X): \"%s\"\n", conf->iConfiguration, conf->iConfiguration ? usb_get_string_simple(dev, conf->iConfiguration) : "no String");
+               printf("  bmAttributes 0x%02X\n", conf->bmAttributes);
+               printf("  bMaxPower 0x%02X\n", conf->bMaxPower);
+
+               struct usb_intf *ifs = conf->intf;
+               for(i=1; i <= conf->bNumInterfaces; i++) {
+                       printf("    === Interface Descriptor %d ===\n", i);
+                       printf("    bLength 0x%02X\n", ifs->bLength);
+                       printf("    bDescriptorType 0x%02X\n", ifs->bDescriptorType);
+                       printf("    bInterfaceNumber 0x%02X\n", ifs->bInterfaceNumber);
+                       printf("    bAlternateSetting 0x%02X\n", ifs->bAlternateSetting);
+                       printf("    bNumEndpoints 0x%02X\n", ifs->bNumEndpoints);
+                       printf("    bInterfaceClass 0x%02X\n", ifs->bInterfaceClass);
+                       printf("    bInterfaceSubClass 0x%02X\n", ifs->bInterfaceSubClass);
+                       printf("    bInterfaceProtocol 0x%02X\n", ifs->bInterfaceProtocol);
+                       printf("    iInterface (0x%02X): \"%s\"\n", ifs->iInterface, ifs->iInterface ? usb_get_string_simple(dev, ifs->iInterface) : "no String");
+
+                       struct usb_endp *ed = ifs->endp;
+                       for(e=1; e <= ifs->bNumEndpoints; e++) {
+                               printf("      === Endpoint Descriptor %d ===\n", e);
+                               printf("      bLength 0x%02X\n", ed->bLength);
+                               printf("      bDescriptorType 0x%02X\n", ed->bDescriptorType);
+                               printf("      bEndpointAddress 0x%02X\n", ed->bEndpointAddress);
+                               printf("      bmAttributes 0x%02X\n", ed->bmAttributes);
+                               printf("      wMaxPacketSize 0x%02X\n", ed->wMaxPacketSize);
+                               printf("      bInterval 0x%02X\n", ed->bInterval);
+
+                               ed = ed->next;
+                       } //endpoint
+                       
+                       ifs = ifs->next;
+               } //interface
+
+               conf = conf->next;
+       } //configuration
+}
+
 /**
  * Find currently detached device and remove
  * data structures
  */
-u8 usb_remove_device(usb_device * dev)
+u8 usb_remove_device(struct usb_device * dev)
 {
        // FIXME!!!! dieser quatsch ist nur temporaer
        free(core.devices->head);
@@ -252,10 +214,10 @@ u8 usb_remove_device(usb_device * dev)
 /**
  * Register new driver at usb stack.
  */
-u8 usb_register_driver(usb_driver * dev)
+u8 usb_register_driver(struct usb_driver * dev)
 {
        /* add driver to driver list */
-       element *tmp = (element *) malloc(sizeof(element));
+       struct element *tmp = (struct element *) malloc(sizeof(struct element));
        tmp->data = (void *) dev;
        tmp->next = NULL;
        list_add_tail(core.drivers, tmp);
@@ -279,10 +241,10 @@ u8 usb_register_driver(usb_driver * dev)
 void usb_probe_driver()
 {
        // call ever registered driver  
-       usb_driver *drv;
-       element *iterator = core.drivers->head;
+       struct usb_driver *drv;
+       struct element *iterator = core.drivers->head;
        while (iterator != NULL) {
-               drv = (usb_driver *) iterator->data;
+               drv = (struct usb_driver *) iterator->data;
                drv->probe();
                iterator = iterator->next;
        }
@@ -291,7 +253,7 @@ void usb_probe_driver()
 /**
  * Not implemented.
  */
-usb_irp *usb_get_irp()
+struct usb_irp *usb_get_irp()
 {
        return 0;
 }
@@ -299,7 +261,7 @@ usb_irp *usb_get_irp()
 /**
  * Not implemented.
  */
-u8 usb_remove_irp(usb_irp * irp)
+u8 usb_remove_irp(struct usb_irp *irp)
 {
 
        return 1;
@@ -311,9 +273,9 @@ u8 usb_remove_irp(usb_irp * irp)
  * In the usbstack they are transported with the
  * usb_transfer_descriptor data structure.
  */
-u16 usb_submit_irp(usb_irp *irp)
+u16 usb_submit_irp(struct usb_irp *irp)
 {
-       usb_transfer_descriptor *td;
+       struct usb_transfer_descriptor *td;
        u8 runloop = 1;
        u16 restlength = irp->len;
        u8 *td_buf_ptr = irp->buffer;
@@ -376,7 +338,6 @@ u16 usb_submit_irp(usb_irp *irp)
                if (bmRequestType & 0x80) { 
                        /* schleife die die tds generiert */
                        while (runloop && (restlength > 0)) {
-                               printf("restlength: %d\t irp->epsize: %d\n", restlength, irp->epsize);
                                td = usb_create_transfer_descriptor(irp);
                                td->actlen = irp->epsize;
                                /* stop loop if all bytes are send */
@@ -493,10 +454,10 @@ u16 usb_submit_irp(usb_irp *irp)
 /** 
  * Create a transfer descriptor with an parent irp.
  */
-usb_transfer_descriptor *usb_create_transfer_descriptor(usb_irp * irp)
+struct usb_transfer_descriptor *usb_create_transfer_descriptor(struct usb_irp * irp)
 {
-       usb_transfer_descriptor *td =
-                       (usb_transfer_descriptor *) malloc(sizeof(usb_transfer_descriptor));
+       struct usb_transfer_descriptor *td =
+                       (struct usb_transfer_descriptor *) malloc(sizeof(struct usb_transfer_descriptor));
 
        td->devaddress = irp->dev->address;
        td->endpoint = irp->endpoint;