[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[cdi-devel] [PATCH] "Better" indent, wait for ACK instead of sleep



From: Max Reitz <max@ŧyndur.org>

* indent done with the program "indent"
+ While a USB device is not ready, it sends NAK in
  response to IN/OUT - it is a lot better to wait
  for an ACK instead of sleeping randomly.

Signed-off-by: Max Reitz <max@xxxxxxxxxx>
---
 usb/include/msd.h  |   50 +++----
 usb/include/uhci.h |   80 ++++++------
 usb/include/usb.h  |   67 ++++------
 usb/main.c         |  192 ++++++++++++--------------
 usb/msd.c          |  377 +++++++++++++++++++++++-----------------------------
 usb/uhci.c         |  117 +++++++----------
 6 files changed, 392 insertions(+), 491 deletions(-)

diff --git a/usb/include/msd.h b/usb/include/msd.h
index 99c796d..cc3bb4d 100644
--- a/usb/include/msd.h
+++ b/usb/include/msd.h
@@ -32,8 +32,7 @@
 
 //"USBC"
 #define CBW_SIGNATURE 0x43425355
-struct command_block_wrapper
-{
+struct command_block_wrapper {
     uint32_t cbw_signature;
     uint32_t cbw_tag;
     uint32_t cbw_data_transfer_length;
@@ -41,55 +40,50 @@ struct command_block_wrapper
     uint8_t cbw_lun;
     uint8_t cbw_cb_length;
     uint8_t cbw_cb[16];
-} __attribute__((packed));
+} __attribute__ ((packed));
 
 //"USBS"
 #define CSW_SIGNATURE 0x53425355
-struct command_status_wrapper
-{
+struct command_status_wrapper {
     uint32_t csw_signature;
     uint32_t csw_tag;
     uint32_t csw_data_residue;
     uint8_t csw_status;
-} __attribute__((packed));
+} __attribute__ ((packed));
 
-struct msc_sense
-{
-    unsigned error : 7;
-    unsigned valid : 1;
+struct msc_sense {
+    unsigned error:7;
+    unsigned valid:1;
     uint8_t rsvd0;
-    unsigned sense_key : 4;
-    unsigned rsvd1 : 4;
+    unsigned sense_key:4;
+    unsigned rsvd1:4;
     uint32_t information;
     uint8_t additional_length;
     uint32_t rsvd2;
     uint8_t additional_code;
     uint8_t additional_code_qualifier;
     uint32_t rsvd;
-} __attribute__((packed));
+} __attribute__ ((packed));
 
-struct msd_capacity
-{
+struct msd_capacity {
     uint32_t last_lba;
     uint32_t block_length;
-} __attribute__((packed));
+} __attribute__ ((packed));
 
-struct cdi_msd
-{
+struct cdi_msd {
     struct cdi_storage_device cdi_device;
     struct usb_device *usb_device;
-    uint32_t offset; //Für Partitionen
+    uint32_t offset;            //Für Partitionen
 };
 
-struct part_table_entry
-{
-   uint8_t active;
-   uint8_t begin_chs[3];
-   uint8_t type;
-   uint8_t end_chs[3];
-   uint32_t start;
-   uint32_t size;
-} __attribute__((packed));
+struct part_table_entry {
+    uint8_t active;
+    uint8_t begin_chs[3];
+    uint8_t type;
+    uint8_t end_chs[3];
+    uint32_t start;
+    uint32_t size;
+} __attribute__ ((packed));
 
 
 #define MSC_CMD_REZERO   0x01
diff --git a/usb/include/uhci.h b/usb/include/uhci.h
index 0158bd2..bf22c4a 100644
--- a/usb/include/uhci.h
+++ b/usb/include/uhci.h
@@ -31,15 +31,15 @@
 #include "usb.h"
 
 
-#define UHCI_USBCMD    0x00 //w
-#define UHCI_USBSTS    0x02 //w
-#define UHCI_USBINTR   0x04 //w
-#define UHCI_FRNUM     0x06 //w
-#define UHCI_FRBASEADD 0x08 //l
-#define UHCI_SOFMOD    0x0C //b
+#define UHCI_USBCMD    0x00     //w
+#define UHCI_USBSTS    0x02     //w
+#define UHCI_USBINTR   0x04     //w
+#define UHCI_FRNUM     0x06     //w
+#define UHCI_FRBASEADD 0x08     //l
+#define UHCI_SOFMOD    0x0C     //b
 #define UHCI_RPORTS    0x10
-#define UHCI_PORTSC1   0x10 //w
-#define UHCI_PORTSC2   0x12 //w
+#define UHCI_PORTSC1   0x10     //w
+#define UHCI_PORTSC2   0x12     //w
 
 #define MAXP    0x0080
 #define GRESET  0x0004
@@ -47,14 +47,13 @@
 #define USB_RUN 0x0001
 
 #define RPORT_RESET  0x0200
-#define RPORT_LOSPD  0x0100 //Low speed device attached
+#define RPORT_LOSPD  0x0100     //Low speed device attached
 #define RPORT_ENABLE 0x0004
-#define RPORT_CSC    0x0002 //Connect status change
+#define RPORT_CSC    0x0002     //Connect status change
 #define RPORT_DEVICE 0x0001
 
 
-struct uhci
-{
+struct uhci {
     struct hci gen_hci;
     uint16_t pbase;
     uintptr_t phys_frame_list;
@@ -62,46 +61,43 @@ struct uhci
     int root_ports;
 };
 
-struct uhci_qh
-{
+struct uhci_qh {
     volatile uint32_t next;
     volatile uint32_t transfer;
-} __attribute__((packed));
+} __attribute__ ((packed));
 
-struct uhci_td
-{
+struct uhci_td {
     volatile uint32_t next;
 
-    unsigned trans_len : 11;
-    unsigned rsvd0 : 6;
-    unsigned bitstuff_err : 1;
-    unsigned crc_time_err : 1;
-    unsigned nak : 1;
-    unsigned babble : 1;
-    unsigned buf_err : 1;
-    unsigned stalled_err : 1;
-    unsigned active : 1;
-    unsigned ioc : 1;
-    unsigned isochronous : 1;
-    unsigned low_speed : 1;
-    unsigned errors : 2;
-    unsigned spd : 1;
-    unsigned rsvd1 : 2;
-
-    unsigned pid : 8;
-    unsigned device : 7;
-    unsigned endpoint : 4;
-    unsigned data_toggle : 1;
-    unsigned rsvd2 : 1;
-    unsigned maxlen : 11;
+    unsigned trans_len:11;
+    unsigned rsvd0:6;
+    unsigned bitstuff_err:1;
+    unsigned crc_time_err:1;
+    unsigned nak:1;
+    unsigned babble:1;
+    unsigned buf_err:1;
+    unsigned stalled_err:1;
+    unsigned active:1;
+    unsigned ioc:1;
+    unsigned isochronous:1;
+    unsigned low_speed:1;
+    unsigned errors:2;
+    unsigned spd:1;
+    unsigned rsvd1:2;
+
+    unsigned pid:8;
+    unsigned device:7;
+    unsigned endpoint:4;
+    unsigned data_toggle:1;
+    unsigned rsvd2:1;
+    unsigned maxlen:11;
 
     uint32_t buffer;
 
     uint32_t user[4];
-} __attribute__((packed));
+} __attribute__ ((packed));
 
-struct transfer
-{
+struct transfer {
     void *virt;
     uintptr_t phys;
     volatile int error;
diff --git a/usb/include/usb.h b/usb/include/usb.h
index cca698a..9ec8b37 100644
--- a/usb/include/usb.h
+++ b/usb/include/usb.h
@@ -30,8 +30,8 @@
 
 enum usb_packet_type {
     PACKET_SETUP = 0x2D,
-    PACKET_IN    = 0x69,
-    PACKET_OUT   = 0xE1,
+    PACKET_IN = 0x69,
+    PACKET_OUT = 0xE1
 };
 
 #define NO_DATA       0x01
@@ -124,42 +124,39 @@ enum usb_packet_type {
 #define USB_TODS_STATUS         8
 
 
-typedef enum
-{
+typedef enum {
     HCI_UHCI,
     HCI_OHCI,
     HCI_EHCI
 } hci_type_t;
 
-struct cdi_hci
-{
+struct cdi_hci {
     struct cdi_device cdi_device;
     struct hci *hci;
 };
 
 struct usb_device;
 
-struct hci
-{
+struct hci {
     struct cdi_pci_device *pcidev;
     hci_type_t type;
-    cdi_list_t (*find_devices)(struct hci *);
-    void (*activate_device)(struct hci *, struct usb_device *);
-    int (*do_packet)(struct hci *, int frame, int type, int device, int endpoint, int low_speed, uintptr_t phys_data, int length, int datatoggle);
-    int (*get_frame)(struct hci *);
+    cdi_list_t (*find_devices) (struct hci *);
+    void (*activate_device) (struct hci *, struct usb_device *);
+    int (*do_packet) (struct hci *, int frame, int type, int device,
+        int endpoint, int low_speed, uintptr_t phys_data,
+        int length, int datatoggle);
+    int (*get_frame) (struct hci *);
 };
 
-struct setup_packet
-{
+struct setup_packet {
     uint8_t request_type;
     uint8_t request;
     uint16_t value;
     uint16_t index;
     uint16_t length;
-} __attribute__((packed));
+} __attribute__ ((packed));
 
-struct device_desc
-{
+struct device_desc {
     uint8_t length;
     uint8_t descriptor_type;
     uint16_t bcdUSB;
@@ -174,10 +171,9 @@ struct device_desc
     uint8_t iProduct;
     uint8_t iSerialNumber;
     uint8_t num_configurations;
-} __attribute__((packed));
+} __attribute__ ((packed));
 
-struct config_desc
-{
+struct config_desc {
     uint8_t length;
     uint8_t descriptor_type;
     uint16_t total_length;
@@ -186,10 +182,9 @@ struct config_desc
     uint8_t iConfiguration;
     uint8_t attributes;
     uint8_t max_power;
-} __attribute__((packed));
+} __attribute__ ((packed));
 
-struct interface_desc
-{
+struct interface_desc {
     uint8_t length;
     uint8_t descriptor_type;
     uint8_t interface_number;
@@ -199,30 +194,27 @@ struct interface_desc
     uint8_t interface_subclass;
     uint8_t interface_protocol;
     uint8_t iInterface;
-} __attribute__((packed));
+} __attribute__ ((packed));
 
-struct endpoint_desc
-{
+struct endpoint_desc {
     uint8_t length;
     uint8_t descriptor_type;
     uint8_t endpoint_address;
     uint8_t attributes;
     uint16_t max_packet_size;
     uint8_t interval;
-} __attribute__((packed));
+} __attribute__ ((packed));
 
-struct hub_desc
-{
+struct hub_desc {
     uint8_t length;
     uint8_t descriptor_type;
     uint8_t nbr_ports;
     uint16_t hub_characteristics;
     uint8_t pwron2pwrgood;
     uint8_t hub_contr_current;
-} __attribute__((packed));
+} __attribute__ ((packed));
 
-typedef enum
-{
+typedef enum {
     USBDC_COMPOSITE = 0x00,
     USBDC_AUDIO = 0x01,
     USBDC_COMM_CDC = 0x02,
@@ -244,13 +236,11 @@ typedef enum
     USBDC_VENDOR_SPEC = 0xFF
 } usb_class_t;
 
-struct class_data
-{
+struct class_data {
     usb_class_t type;
 };
 
-struct usb_device
-{
+struct usb_device {
     struct hci *hci;
     int id;
     int port;
@@ -264,8 +254,7 @@ struct usb_device
     int expects;
 };
 
-struct msclass_data
-{
+struct msclass_data {
     struct class_data gen_class;
     struct endpoint_desc *bulk_ep_in;
     struct endpoint_desc *bulk_ep_out;
@@ -294,7 +283,7 @@ struct usb_packet {
 #define HCI_STRUCT_SIZE sizeof(struct uhci)
 
 
-int usb_do_packet(struct usb_device *device, struct usb_packet* packet);
+int usb_do_packet(struct usb_device *device, struct usb_packet *packet);
 void enumerate_hci(struct hci *);
 struct cdi_driver *init_uhcd(void);
 void init_msc_driver(void);
diff --git a/usb/main.c b/usb/main.c
index 9200613..51ef95b 100644
--- a/usb/main.c
+++ b/usb/main.c
@@ -41,12 +41,13 @@ static int dprintf(const char *fmt, ...)
 {
     return 0;
 }
+
 #define _dprintf(fmt, args...) dprintf(fmt, ##args)
 #endif
 
 static struct usb_device *devices[128];
 static cdi_list_t ehci, ohci, uhci;
-static void (*ccdriver_take_device[0x100])(struct usb_device *);
+static void (*ccdriver_take_device[0x100]) (struct usb_device *);
 static int usb_dev_ids = 1;
 
 static void usb_init(void);
@@ -94,18 +95,18 @@ static const int next_data_type[9] = {
 };*/
 
 /**
-* Verarbeitet ein USB-Paket.
-*
-* @param device Das USB-Gerät
-*/
+ * Verarbeitet ein USB-Paket.
+ *
+ * @param device Das USB-Gerät
+ */
 
-int usb_do_packet(struct usb_device *device, struct usb_packet* packet)
+int usb_do_packet(struct usb_device *device, struct usb_packet *packet)
 {
-    int error;
+    int error = USB_NAK;
     int tod_short;
 
     if (!(device->expects & packet->type_of_data)) {
-        dprintf("Expected 0x%04X, got 0x%04X...?\n", device->expects,
+        dprintf("0x%04X erwartet, 0x%04X bekommen...?\n", device->expects,
             packet->type_of_data);
         // FIXME Das ist keine elegante Loesung des Problems
         for (;;);
@@ -118,18 +119,18 @@ int usb_do_packet(struct usb_device *device, struct usb_packet* packet)
     device->expects = next_data_type[tod_short];
     packet->endpoint &= 0x07;
     packet->type &= 0xFF;
-    if (device->stalled) //TODO: Irgendwie entstallen, evtl.? oO
-    {
+    if (device->stalled) {
+        //TODO: Irgendwie entstallen, evtl.? oO
         dprintf("Zugriff auf stalled-Gerät verweigert.\n");
         return USB_STALLED;
     }
-    error = device->hci->do_packet(device->hci,
-        (device->hci->get_frame(device->hci) + 3) & 0x3FF,
-        packet->type, device->id, packet->endpoint,
-        device->low_speed, packet->phys_data, packet->length,
-        packet->datatoggle);
-    if (error == USB_STALLED)
-    {
+    while (error == USB_NAK) {
+        error = device->hci->do_packet(device->hci,
+            (device->hci->get_frame(device->hci) + 3) & 0x3FF,
+            packet->type, device->id, packet->endpoint, device->low_speed,
+            packet->phys_data, packet->length, packet->datatoggle);
+    }
+    if (error == USB_STALLED) {
         printf("[usb] ENDPOINT %i DES GERÃ?TS %i STALLED!\n",
             packet->endpoint, device->id);
         device->stalled = 1;
@@ -137,7 +138,8 @@ int usb_do_packet(struct usb_device *device, struct usb_packet* packet)
     return error;
 }
 
-static void *do_control(struct usb_device *device, int direction, void *buffer, int length, int rtype, int recipient, int request, int value, int index)
+static void *do_control(struct usb_device *device, int direction, void *buffer,
+    int length, int rtype, int recipient, int request, int value, int index)
 {
     void *rbuf;
     int rval, no_data;
@@ -149,7 +151,7 @@ static void *do_control(struct usb_device *device, int direction, void *buffer,
         return NULL;
     }
     if (cdi_alloc_phys_mem(sizeof(struct setup_packet),
-                           (void **)&setup, (void **)&psetup) == -1) {
+            (void **)&setup, (void **)&psetup) == -1) {
         return NULL;
     }
     direction &= 0x80;
@@ -163,12 +165,12 @@ static void *do_control(struct usb_device *device, int direction, void *buffer,
     setup->length = length;
 
     struct usb_packet setup_packet = {
-        .type           = PACKET_SETUP,
-        .endpoint       = 0,
-        .phys_data      = psetup,
-        .length         = sizeof(*setup),
-        .datatoggle     = 0,
-        .type_of_data   = USB_TOD_SETUP,
+        .type = PACKET_SETUP,
+        .endpoint = 0,
+        .phys_data = psetup,
+        .length = sizeof(*setup),
+        .datatoggle = 0,
+        .type_of_data = USB_TOD_SETUP,
     };
 
 
@@ -177,43 +179,38 @@ static void *do_control(struct usb_device *device, int direction, void *buffer,
     }
 
     cdi_sleep_ms(20);
-    if (no_data)
-    {
+    if (no_data) {
         rval = USB_NO_ERROR;
-        rbuf = (void *)0xFFFFFFFF; //Kein Fehler, aber auch keine Daten
-    }
-    else
-    {
+        rbuf = (void *)0xFFFFFFFF;      //Kein Fehler, aber auch keine Daten
+    } else {
         struct usb_packet data_packet = {
-            .type           = (direction == DEV_TO_HOST ?
-                              PACKET_IN : PACKET_OUT),
-            .endpoint       = 0,
-            .phys_data      = pbuf,
-            .length         = length,
-            .datatoggle     = 1,
-            .type_of_data   = (direction == DEV_TO_HOST ?
-                              USB_TOD_SETUP_DATA_IN : USB_TOD_SETUP_DATA_OUT),
+            .type = (direction == DEV_TO_HOST ? PACKET_IN : PACKET_OUT),
+            .endpoint = 0,
+            .phys_data = pbuf,
+            .length = length,
+            .datatoggle = 1,
+            .type_of_data = (direction == DEV_TO_HOST ?
+                USB_TOD_SETUP_DATA_IN : USB_TOD_SETUP_DATA_OUT),
         };
 
         rval = usb_do_packet(device, &data_packet);
     }
 
-    if (rval == USB_NO_ERROR)
-    {
+    if (rval == USB_NO_ERROR) {
         struct usb_packet ack_packet = {
-            .endpoint       = 0,
-            .phys_data      = 0,
-            .length         = 0,
-            .datatoggle     = 1,
+            .endpoint = 0,
+            .phys_data = 0,
+            .length = 0,
+            .datatoggle = 1,
         };
 
         cdi_sleep_ms(20);
 
         if (no_data || (direction == HOST_TO_DEV)) {
-            ack_packet.type         = PACKET_IN;
+            ack_packet.type = PACKET_IN;
             ack_packet.type_of_data = USB_TOD_SETUP_ACK_IN;
         } else {
-            ack_packet.type         = PACKET_OUT;
+            ack_packet.type = PACKET_OUT;
             ack_packet.type_of_data = USB_TOD_SETUP_ACK_OUT;
         }
 
@@ -233,7 +230,8 @@ static void usb_init(void)
 
     init_msc_driver();
 
-    memset(ccdriver_take_device, 0, sizeof(void (*)(struct usb_device *)) * 0x100);
+    memset(ccdriver_take_device, 0,
+        sizeof(void (*)(struct usb_device *)) * 0x100);
     ccdriver_take_device[8] = &register_msd;
     ccdriver_take_device[9] = &enumerate_hub;
 
@@ -243,13 +241,11 @@ static void usb_init(void)
 
     cdi_list_t pci_devices = cdi_list_create();
     cdi_pci_get_all_devices(pci_devices);
-    for (i = 0; (dev = cdi_list_get(pci_devices, i)) != NULL; i++)
-    {
+    for (i = 0; (dev = cdi_list_get(pci_devices, i)) != NULL; i++) {
         if ((dev->class_id != 0x0C) || (dev->subclass_id != 0x03)) {
             cdi_pci_device_destroy(dev);
         } else {
-            switch (dev->interface_id)
-            {
+            switch (dev->interface_id) {
                 case 0x00:
                     hci = malloc(HCI_STRUCT_SIZE);
                     hci->pcidev = dev;
@@ -273,14 +269,12 @@ static void usb_init(void)
 
     cdi_list_destroy(pci_devices);
 
-    dprintf("%i EHCIs, %i OHCIs und %i UHCIs gefunden.\n", cdi_list_size(ehci),
-                                                           cdi_list_size(ohci),
-                                                           cdi_list_size(uhci));
+    dprintf("%i EHCIs, %i OHCIs und %i UHCIs gefunden.\n",
+        cdi_list_size(ehci), cdi_list_size(ohci), cdi_list_size(uhci));
 
     uhcd = init_uhcd();
 
-    for (i = 0; (hci = cdi_list_pop(uhci)) != NULL; i++)
-    {
+    for (i = 0; (hci = cdi_list_pop(uhci)) != NULL; i++) {
         cdi_hci = malloc(sizeof(struct cdi_hci));
         cdi_hci->cdi_device.type = CDI_UNKNOWN;
         dev_name = malloc(10);
@@ -313,23 +307,21 @@ static void enum_device(struct usb_device *usbdev)
     usbdev->locked = 0;
     usbdev->expects = USB_TOD_SETUP | USB_TOD_COMMAND;
     dev_desc = do_control(usbdev, DEV_TO_HOST, NULL, sizeof(*dev_desc),
-                          STD_REQUEST, REC_DEVICE, GET_DESCRIPTOR,
-                          DESC_DEVICE << 8, 0);
+        STD_REQUEST, REC_DEVICE, GET_DESCRIPTOR, DESC_DEVICE << 8, 0);
     if (dev_desc == NULL) {
         return;
     }
     usbdev->device = dev_desc;
     dprintf("0x%04X:0x%04X (%i) -> ", dev_desc->vendor_id, dev_desc->device_id,
-                                      dev_desc->class_id);
+        dev_desc->class_id);
     do_control(usbdev, HOST_TO_DEV | NO_DATA, NULL, 0, STD_REQUEST, REC_DEVICE,
-               SET_ADDRESS, (id = usb_dev_ids++), 0);
+        SET_ADDRESS, (id = usb_dev_ids++), 0);
     usbdev->id = id;
     _dprintf("%i\n", id);
-    if (dev_desc->iManufacturer)
-    {
+    if (dev_desc->iManufacturer) {
         name = do_control(usbdev, DEV_TO_HOST, NULL, 64, STD_REQUEST,
-                          REC_DEVICE, GET_DESCRIPTOR,
-                          (DESC_STRING << 8) | dev_desc->iManufacturer, 0);
+            REC_DEVICE, GET_DESCRIPTOR,
+            (DESC_STRING << 8) | dev_desc->iManufacturer, 0);
         if (name == NULL) {
             return;
         }
@@ -339,11 +331,10 @@ static void enum_device(struct usb_device *usbdev)
         }
         _dprintf("\n");
     }
-    if (dev_desc->iProduct)
-    {
+    if (dev_desc->iProduct) {
         name = do_control(usbdev, DEV_TO_HOST, NULL, 64, STD_REQUEST,
-                          REC_DEVICE, GET_DESCRIPTOR,
-                          (DESC_STRING << 8) | dev_desc->iProduct, 0);
+            REC_DEVICE, GET_DESCRIPTOR,
+            (DESC_STRING << 8) | dev_desc->iProduct, 0);
         if (name == NULL) {
             return;
         }
@@ -354,27 +345,25 @@ static void enum_device(struct usb_device *usbdev)
         _dprintf("\n");
     }
     devices[id] = usbdev;
-  //TODO: Man kann doch nicht immer den ersten nehmen...
+    //TODO: Man kann doch nicht immer den ersten nehmen...
     conf_desc = do_control(usbdev, DEV_TO_HOST, NULL,
-                           sizeof(struct config_desc), STD_REQUEST, REC_DEVICE,
-                           GET_DESCRIPTOR, DESC_CONFIGURATION << 8, 0);
+        sizeof(struct config_desc), STD_REQUEST,
+        REC_DEVICE, GET_DESCRIPTOR, DESC_CONFIGURATION << 8, 0);
     if (conf_desc == NULL) {
         return;
     }
     conf_desc = do_control(usbdev, DEV_TO_HOST, NULL, conf_desc->total_length,
-                           STD_REQUEST, REC_DEVICE, GET_DESCRIPTOR,
-                           DESC_CONFIGURATION << 8, 0);
+        STD_REQUEST, REC_DEVICE, GET_DESCRIPTOR, DESC_CONFIGURATION << 8, 0);
     if (conf_desc == NULL) {
         return;
     }
     usbdev->config = conf_desc;
     do_control(usbdev, HOST_TO_DEV, NULL, 0, STD_REQUEST, REC_DEVICE,
-               SET_CONFIGURATION, conf_desc->config_value, 0);
-    if (conf_desc->iConfiguration)
-    {
+        SET_CONFIGURATION, conf_desc->config_value, 0);
+    if (conf_desc->iConfiguration) {
         name = do_control(usbdev, DEV_TO_HOST, NULL, 64, STD_REQUEST,
-                          REC_DEVICE, GET_DESCRIPTOR,
-                          (DESC_STRING << 8) | conf_desc->iConfiguration, 0);
+            REC_DEVICE, GET_DESCRIPTOR,
+            (DESC_STRING << 8) | conf_desc->iConfiguration, 0);
         if (name == NULL) {
             return;
         }
@@ -386,25 +375,25 @@ static void enum_device(struct usb_device *usbdev)
     }
     position = conf_desc;
     position += sizeof(struct config_desc);
-    best_if = position; //Standard-IF
-    for (i = 0; i < conf_desc->num_interfaces; i++)
-    {
+    best_if = position;         //Standard-IF
+    for (i = 0; i < conf_desc->num_interfaces; i++) {
         if_desc = position;
-        if (if_desc->interface_class == 8) //TODO: Mehr Klassencodes natürlich!
-        {
+        //TODO: Mehr Klassencodes natürlich!
+        if (if_desc->interface_class == 8) {
             best_if = if_desc;
             break;
         }
-        position += sizeof(struct interface_desc) + if_desc->num_endpoints * sizeof(struct endpoint_desc);
+        position +=
+            sizeof(struct interface_desc) +
+            if_desc->num_endpoints * sizeof(struct endpoint_desc);
     }
     do_control(usbdev, HOST_TO_DEV, NULL, 0, STD_REQUEST, REC_DEVICE,
-               SET_INTERFACE, best_if->interface_number, 0);
+        SET_INTERFACE, best_if->interface_number, 0);
     usbdev->interface = best_if;
-    if (best_if->iInterface)
-    {
+    if (best_if->iInterface) {
         name = do_control(usbdev, DEV_TO_HOST, NULL, 64, STD_REQUEST,
-                          REC_DEVICE, GET_DESCRIPTOR,
-                          (DESC_STRING << 8) | best_if->iInterface, 0);
+            REC_DEVICE, GET_DESCRIPTOR,
+            (DESC_STRING << 8) | best_if->iInterface, 0);
         if (name == NULL) {
             return;
         }
@@ -415,10 +404,9 @@ static void enum_device(struct usb_device *usbdev)
         _dprintf(".\n");
     }
     dprintf("Konfiguration: %i:%i (%i)\n", conf_desc->config_value,
-                                           best_if->interface_number,
-                                           best_if->interface_class);
+        best_if->interface_number, best_if->interface_class);
     if (ccdriver_take_device[best_if->interface_class] != NULL) {
-        ccdriver_take_device[best_if->interface_class](usbdev);
+        ccdriver_take_device[best_if->interface_class] (usbdev);
     }
 }
 
@@ -432,22 +420,20 @@ static void enumerate_hub(struct usb_device *usbdev)
     //Mehr als 8 Ports sollte es nicht geben, die Grö�e des Deskriptors wird
     //daher wohl nicht grö�er als sizeof()+16 werden
     hub_desc = do_control(usbdev, DEV_TO_HOST, NULL, sizeof(*hub_desc) + 16,
-                          CLS_REQUEST, REC_DEVICE, GET_DESCRIPTOR,
-                          DESC_DEVICE << 8, 0);
-    for (i = 0; i < hub_desc->nbr_ports; i++)
-    {
+        CLS_REQUEST, REC_DEVICE, GET_DESCRIPTOR, DESC_DEVICE << 8, 0);
+    for (i = 0; i < hub_desc->nbr_ports; i++) {
         port_status = do_control(usbdev, DEV_TO_HOST, NULL, 4, CLS_REQUEST,
-                                 REC_OTHER, GET_STATUS, 0, i+1);
+            REC_OTHER, GET_STATUS, 0, i + 1);
         if (!(port_status[0] & PORT_DEVICE)) {
             continue;
         }
         //Strom an
         do_control(usbdev, HOST_TO_DEV, NULL, 0, CLS_REQUEST, REC_OTHER,
-                   SET_FEATURE, PORTF_POWER, i+1);
+            SET_FEATURE, PORTF_POWER, i + 1);
         cdi_sleep_ms(hub_desc->pwron2pwrgood * 2);
         //Resetten
         do_control(usbdev, HOST_TO_DEV, NULL, 0, CLS_REQUEST, REC_OTHER,
-                   SET_FEATURE, PORTF_RESET, i+1);
+            SET_FEATURE, PORTF_RESET, i + 1);
         //Reset sollte jetzt eigtl. schon beendet sein
         //(do_control wartet ja 50 ms)
         down = malloc(sizeof(struct usb_device));
@@ -464,11 +450,9 @@ void enumerate_hci(struct hci *hci)
     struct usb_device *usbdev;
     cdi_list_t usb_devices;
 
-    if (hci->find_devices != NULL)
-    {
+    if (hci->find_devices != NULL) {
         usb_devices = hci->find_devices(hci);
-        while ((usbdev = cdi_list_pop(usb_devices)) != NULL)
-        {
+        while ((usbdev = cdi_list_pop(usb_devices)) != NULL) {
             hci->activate_device(hci, usbdev);
             cdi_sleep_ms(50);
             enum_device(usbdev);
diff --git a/usb/msd.c b/usb/msd.c
index 0f9b79e..9614473 100644
--- a/usb/msd.c
+++ b/usb/msd.c
@@ -48,6 +48,7 @@ static int dprintf(const char *fmt, ...)
 {
     return 0;
 }
+
 #define _dprintf(fmt, args...) dprintf(fmt, ##args)
 #endif
 
@@ -70,9 +71,12 @@ static void deinit_msd(struct cdi_device *cdi_msd);
 static void get_partitions(struct cdi_storage_device *cdistrg);
 static void init_msd(struct cdi_device *cdi_msd);
 static void kill_driver(struct cdi_driver *cdi_msc_driver);
-static int msd_cdi_read(struct cdi_storage_device *strgdev, uint64_t start, uint64_t count, void *buffer);
-static int msd_cdi_write(struct cdi_storage_device *strgdev, uint64_t start, uint64_t count, void *buffer);
-static int msd_get_capacity(struct usb_device *usbdev, uint32_t *block_size, uint32_t *block_count);
+static int msd_cdi_read(struct cdi_storage_device *strgdev, uint64_t start,
+    uint64_t count, void *buffer);
+static int msd_cdi_write(struct cdi_storage_device *strgdev, uint64_t start,
+    uint64_t count, void *buffer);
+static int msd_get_capacity(struct usb_device *usbdev, uint32_t *block_size,
+    uint32_t *block_count);
 
 //TODO: CDI-Funktion wäre hier sicher nützlich...
 #define CPU_IS_LITTLE_ENDIAN
@@ -125,10 +129,9 @@ void register_msd(struct usb_device *usbdev)
     uint64_t size;
     static int msdnum = 0;
 
-    if (usbdev->interface->interface_protocol != 0x50)
-    {
-       dprintf("Es werden nur bulk-only-Interfaces unterstützt.\n");
-       return;
+    if (usbdev->interface->interface_protocol != 0x50) {
+        dprintf("Es werden nur bulk-only-Interfaces unterstützt.\n");
+        return;
     }
     cdimsd = malloc(sizeof(struct cdi_msd));
     if (cdimsd == NULL) {
@@ -139,15 +142,13 @@ void register_msd(struct usb_device *usbdev)
     strgdev = (struct cdi_storage_device *)cdimsd;
     strgdev->dev.type = CDI_STORAGE;
     strgdev->dev.name = malloc(10);
-    if (strgdev->dev.name == NULL)
-    {
+    if (strgdev->dev.name == NULL) {
         free(strgdev);
         return;
     }
     sprintf((char *)strgdev->dev.name, "msd%i", msdnum++);
     msc = malloc(sizeof(struct msclass_data));
-    if (msc == NULL)
-    {
+    if (msc == NULL) {
         free((void *)strgdev->dev.name);
         free(strgdev);
         return;
@@ -156,38 +157,30 @@ void register_msd(struct usb_device *usbdev)
     msc->bulk_ep_in = NULL;
     msc->bulk_ep_out = NULL;
     address = (void *)usbdev->interface + sizeof(struct interface_desc);
-    for (i = 0; i < usbdev->interface->num_endpoints; i++)
-    {
+    for (i = 0; i < usbdev->interface->num_endpoints; i++) {
         ep_desc = address;
-        if ((ep_desc->endpoint_address & 0x80) &&
-            (ep_desc->attributes      == 0x02) &&
-            (msc->bulk_ep_in          == NULL)) { //BULK-IN
+        if ((ep_desc->endpoint_address & 0x80) && (ep_desc->attributes == 0x02) && (msc->bulk_ep_in == NULL)) { //BULK-IN
             msc->bulk_ep_in = ep_desc;
-        } else if (!(ep_desc->endpoint_address & 0x80) &&
-                    (ep_desc->attributes      == 0x02) &&
-                    (msc->bulk_ep_out         == NULL)) { //BULK-OUT
+        } else if (!(ep_desc->endpoint_address & 0x80) && (ep_desc->attributes == 0x02) && (msc->bulk_ep_out == NULL)) {        //BULK-OUT
             msc->bulk_ep_out = ep_desc;
         }
         address += sizeof(struct endpoint_desc);
     }
-    if ((msc->bulk_ep_in == NULL) || (msc->bulk_ep_out == NULL))
-    {
+    if ((msc->bulk_ep_in == NULL) || (msc->bulk_ep_out == NULL)) {
         dprintf("Nicht genügend Endpoints gefunden.\n");
         return;
     }
-    if (!msd_get_capacity(usbdev, &bs, &bc))
-    {
+    if (!msd_get_capacity(usbdev, &bs, &bc)) {
         strgdev->block_size = 0;
         strgdev->block_count = 0;
         dprintf("Konnte Grö�e für %s nicht ermitteln.\n", strgdev->dev.name);
-    }
-    else
-    {
+    } else {
         strgdev->block_size = bs;
         strgdev->block_count = bc;
         size = bs;
         size *= bc;
-        dprintf("%s: %i * %i B (ca. %lld MB).\n", strgdev->dev.name, bc, bs, size >> 20);
+        dprintf("%s: %i * %i B (ca. %lld MB).\n", strgdev->dev.name, bc, bs,
+            size >> 20);
     }
     cdi_storage_device_init(strgdev);
     cdi_list_push(cdi_driver.drv.devices, strgdev);
@@ -210,58 +203,81 @@ static void deinit_msd(struct cdi_device *cdi_msd)
     //TODO: Und gerade hier...
 }
 
-static int write_cmd(struct usb_device* usbdev, uintptr_t src)
+static int write_cmd(struct usb_device *usbdev, uintptr_t src)
 {
-    struct msclass_data *msc = (struct msclass_data*) usbdev->classd;
+    struct msclass_data *msc = (struct msclass_data *)usbdev->classd;
     struct usb_packet cmd_packet = {
-        .type           = PACKET_OUT,
-        .endpoint       = msc->bulk_ep_out->endpoint_address,
-        .phys_data      = src,
-        .length         = 0x1F,
-        .datatoggle     = 0,
-        .type_of_data   = USB_TOD_COMMAND,
+        .type = PACKET_OUT,
+        .endpoint = msc->bulk_ep_out->endpoint_address,
+        .phys_data = src,
+        .length = 0x1F,
+        .datatoggle = 0,
+        .type_of_data = USB_TOD_COMMAND,
     };
 
     return usb_do_packet(usbdev, &cmd_packet);
 }
 
-static int read_status(struct usb_device* usbdev, uintptr_t dest)
+/**
+ * Liest den Status von einem MSD
+ *
+ * @param usbdev Das bewusste Gerät
+ * @param expected_tag Das erwartete Tag
+ *
+ * @return Wenn der Status OK ist USB_NO_ERROR, sonst entsprechender Fehler
+ */
+
+static int read_status(struct usb_device *usbdev, uint32_t expected_tag)
 {
-    struct msclass_data *msc = (struct msclass_data*) usbdev->classd;
+    uintptr_t pcsw;
+    struct msclass_data *msc = (struct msclass_data *)usbdev->classd;
+    struct command_status_wrapper *csw;
+    int error;
+
+    if (cdi_alloc_phys_mem(sizeof(struct command_status_wrapper),
+            (void **)&csw, (void **)&pcsw) == -1) {
+        return USB_TRIVIAL_ERROR;
+    }
+
     struct usb_packet status_packet = {
-        .type           = PACKET_IN,
-        .endpoint       = msc->bulk_ep_in->endpoint_address,
-        .phys_data      = dest,
-        .length         = 0x0D,
-        .datatoggle     = 0,
-        .type_of_data   = USB_TOD_STATUS,
+        .type = PACKET_IN,
+        .endpoint = msc->bulk_ep_in->endpoint_address,
+        .phys_data = pcsw,
+        .length = 0x0D,
+        .datatoggle = 0,
+        .type_of_data = USB_TOD_STATUS,
     };
 
-    return usb_do_packet(usbdev, &status_packet);
+    error = usb_do_packet(usbdev, &status_packet);
+    if (error != USB_NO_ERROR) {
+        return error;
+    }
+    if ((csw->csw_signature != CSW_SIGNATURE) ||
+        (csw->csw_tag != expected_tag) || csw->csw_status) {
+        return USB_CRC; //Watt weiÃ? denn ich
+    }
+
+    return USB_NO_ERROR;
 }
 
-static int msd_get_capacity(struct usb_device *usbdev, uint32_t *block_size, uint32_t *block_count)
+static int msd_get_capacity(struct usb_device *usbdev, uint32_t *block_size,
+    uint32_t *block_count)
 {
     struct msclass_data *msc;
     struct command_block_wrapper *cbw;
-    struct command_status_wrapper *csw;
     struct msd_capacity *cap;
-    uintptr_t pcbw, pcsw, pcap;
+    uintptr_t pcbw, pcap;
     uint32_t expected_tag;
 
     if ((usbdev == NULL) || (block_size == NULL) || (block_count == NULL)) {
         return 0;
     }
     if (cdi_alloc_phys_mem(sizeof(struct command_block_wrapper),
-                           (void **)&cbw, (void **)&pcbw) == -1) {
-        return 0;
-    }
-    if (cdi_alloc_phys_mem(sizeof(struct command_status_wrapper),
-                           (void **)&csw, (void **)&pcsw) == -1) {
+            (void **)&cbw, (void **)&pcbw) == -1) {
         return 0;
     }
     if (cdi_alloc_phys_mem(sizeof(struct msd_capacity),
-                           (void **)&cap, (void **)&pcap) == -1) {
+            (void **)&cap, (void **)&pcap) == -1) {
         return 0;
     }
     msc = (struct msclass_data *)usbdev->classd;
@@ -269,42 +285,30 @@ static int msd_get_capacity(struct usb_device *usbdev, uint32_t *block_size, uin
     cbw->cbw_signature = CBW_SIGNATURE;
     cbw->cbw_tag = (expected_tag = cbw_tag++);
     cbw->cbw_data_transfer_length = sizeof(struct msd_capacity);
-    cbw->cbw_flags = 0x80; //IN
-    cbw->cbw_lun = 0; //Was weiÃ? ich, wie viele LUNs datt Dingens hat?
+    cbw->cbw_flags = 0x80;      //IN
+    cbw->cbw_lun = 0;           //Was weiÃ? ich, wie viele LUNs datt Dingens hat?
     cbw->cbw_cb_length = 12;
     cbw->cbw_cb[0] = MSC_CMD_CAPACITY;
-    cbw->cbw_cb[1] = 0; //LUN: 0
+    cbw->cbw_cb[1] = 0;         //LUN: 0
 
     if (write_cmd(usbdev, pcbw) != USB_NO_ERROR) {
         return 0;
     }
 
-    cdi_sleep_ms(20);
-
     struct usb_packet in_packet = {
-        .type           = PACKET_IN,
-        .endpoint       = msc->bulk_ep_in->endpoint_address,
-        .phys_data      = pcap,
-        .length         = sizeof(*cap),
-        .datatoggle     = 0,
-        .type_of_data   = USB_TOD_DATA_IN,
+        .type = PACKET_IN,
+        .endpoint = msc->bulk_ep_in->endpoint_address,
+        .phys_data = pcap,
+        .length = sizeof(*cap),
+        .datatoggle = 0,
+        .type_of_data = USB_TOD_DATA_IN,
     };
 
     if (usb_do_packet(usbdev, &in_packet) != USB_NO_ERROR) {
         return 0;
     }
 
-    cdi_sleep_ms(5);
-
-    if (read_status(usbdev, pcsw) != USB_NO_ERROR) {
-        return 0;
-    }
-
-    cdi_sleep_ms(5);
-
-    if ((csw->csw_signature != CSW_SIGNATURE) ||
-         (csw->csw_tag      !=  expected_tag) ||
-          csw->csw_status) {
+    if (read_status(usbdev, expected_tag) != USB_NO_ERROR) {
         return 0;
     }
 
@@ -317,46 +321,33 @@ static int msd_get_capacity(struct usb_device *usbdev, uint32_t *block_size, uin
 #ifdef WAIT_FOR_MSD_READY
 static int msd_ready(struct usb_device *usbdev)
 {
-   struct msclass_data *msc;
-   struct command_block_wrapper *cbw;
-   struct command_status_wrapper *csw;
-   uintptr_t pcbw, pcsw;
-   uint32_t expected_tag;
-
-   if (cdi_alloc_phys_mem(sizeof(struct command_block_wrapper),
-                          (void **)&cbw, (void **)&pcbw) == -1) {
-      return 0;
-   }
-   if (cdi_alloc_phys_mem(sizeof(struct command_status_wrapper),
-                          (void **)&csw, (void **)&pcsw) == -1) {
-      return 0;
-   }
-   msc = (struct msclass_data *)usbdev->classd;
-   memset(cbw, 0, 0x1F);
-   cbw->cbw_signature = CBW_SIGNATURE;
-   cbw->cbw_tag = (expected_tag = cbw_tag++);
-   cbw->cbw_data_transfer_length = 0;
-   cbw->cbw_flags = 0x80; //IN
-   cbw->cbw_lun = 0; //Was weiÃ? ich, wie viele LUNs datt Dingens hat?
-   cbw->cbw_cb_length = 12; //Alles null, also "Test unit ready"
-
-   if (write_cmd(usbdev, pcbw) != USB_NO_ERROR) {
-       return 0;
-   }
-
-   cdi_sleep_ms(7);
-   if (read_status(usbdev, pcsw) != USB_NO_ERROR) {
-       return 0;
-   }
-
-   cdi_sleep_ms(4);
-   if ((csw->csw_signature != CSW_SIGNATURE) ||
-       (csw->csw_tag       !=  expected_tag) ||
-        csw->csw_status) {
-      return 0;
-   }
-
-   return 1;
+    struct msclass_data *msc;
+    struct command_block_wrapper *cbw;
+    uintptr_t pcbw;
+    uint32_t expected_tag;
+
+    if (cdi_alloc_phys_mem(sizeof(struct command_block_wrapper),
+            (void **)&cbw, (void **)&pcbw) == -1) {
+        return 0;
+    }
+    msc = (struct msclass_data *)usbdev->classd;
+    memset(cbw, 0, 0x1F);
+    cbw->cbw_signature = CBW_SIGNATURE;
+    cbw->cbw_tag = (expected_tag = cbw_tag++);
+    cbw->cbw_data_transfer_length = 0;
+    cbw->cbw_flags = 0x80;      //IN
+    cbw->cbw_lun = 0;           //Was weiÃ? ich, wie viele LUNs datt Dingens hat?
+    cbw->cbw_cb_length = 12;    //Alles null, also "Test unit ready"
+
+    if (write_cmd(usbdev, pcbw) != USB_NO_ERROR) {
+        return 0;
+    }
+
+    if (read_status(usbdev, expected_tag) != USB_NO_ERROR) {
+        return 0;
+    }
+
+    return 1;
 }
 #endif
 
@@ -368,9 +359,11 @@ static inline int tsl(volatile int *variable)
     return rval;
 }
 
-static uint32_t msd_read(struct usb_device *usbdev, uint32_t lba, uint16_t sectors, uintptr_t phys_buffer, size_t length);
+static uint32_t msd_read(struct usb_device *usbdev, uint32_t lba,
+    uint16_t sectors, uintptr_t phys_buffer, size_t length);
 
-static int msd_cdi_read(struct cdi_storage_device *strgdev, uint64_t start, uint64_t count, void *buffer)
+static int msd_cdi_read(struct cdi_storage_device *strgdev, uint64_t start,
+    uint64_t count, void *buffer)
 {
     void *vb;
     uintptr_t pb;
@@ -383,57 +376,48 @@ static int msd_cdi_read(struct cdi_storage_device *strgdev, uint64_t start, uint
 
     fdprintf("read(%i, %i)\n", (int)start, (int)count);
     start += ((struct cdi_msd *)strgdev)->offset;
-    if (cdi_alloc_phys_mem(bs, &vb, (void **)&pb) == -1)
-    {
+    if (cdi_alloc_phys_mem(bs, &vb, (void **)&pb) == -1) {
         dprintf("Blockspeicher konnte nicht allociert werden.\n");
         return -1;
     }
-    if (!count)
-    {
+    if (!count) {
         dprintf("Leere Leseanfrage.\n");
         return 0;
     }
-    while (tsl(&usbdev->locked))
-    {
+    while (tsl(&usbdev->locked)) {
 #ifndef CDI_STANDALONE
-        __asm__ __volatile__ ("hlt");
+        __asm__ __volatile__("hlt");
 #endif
     }
-    for (i = 0; i < count; i++)
-    {
+    for (i = 0; i < count; i++) {
 #ifdef WAIT_FOR_MSD_READY
         for (j = 0; !msd_ready(usbdev) && (j < 10); j++) {
             cdi_sleep_ms(20);
         }
 #endif
         error = msd_read(usbdev, start + i, 1, pb, bs);
-        if (error != USB_NO_ERROR)
-        {
+        if (error != USB_NO_ERROR) {
             dprintf("Lesefehler 0x%X bei Block %i.\n", error, i);
             usbdev->locked = 0;
             return -1;
         }
-        memcpy(buffer + i*bs, vb, bs);
+        memcpy(buffer + i * bs, vb, bs);
     }
     usbdev->locked = 0;
     return 0;
 }
 
-static uint32_t msd_read(struct usb_device *usbdev, uint32_t lba, uint16_t sectors, uintptr_t phys_buffer, size_t length)
+static uint32_t msd_read(struct usb_device *usbdev, uint32_t lba,
+    uint16_t sectors, uintptr_t phys_buffer, size_t length)
 {
     struct msclass_data *msc;
     struct command_block_wrapper *cbw;
-    struct command_status_wrapper *csw;
-    uintptr_t pcbw, pcsw;
+    uintptr_t pcbw;
     uint32_t expected_tag;
     int error;
 
     if (cdi_alloc_phys_mem(sizeof(struct command_block_wrapper),
-                           (void **)&cbw, (void **)&pcbw) == -1) {
-        return USB_TRIVIAL_ERROR;
-    }
-    if (cdi_alloc_phys_mem(sizeof(struct command_status_wrapper),
-                           (void **)&csw, (void **)&pcsw) == -1) {
+            (void **)&cbw, (void **)&pcbw) == -1) {
         return USB_TRIVIAL_ERROR;
     }
     if (!phys_buffer || !length) {
@@ -444,61 +428,50 @@ static uint32_t msd_read(struct usb_device *usbdev, uint32_t lba, uint16_t secto
     cbw->cbw_signature = CBW_SIGNATURE;
     cbw->cbw_tag = (expected_tag = cbw_tag++);
     cbw->cbw_data_transfer_length = length;
-    cbw->cbw_flags = 0x80; //IN
-    cbw->cbw_lun = 0; //Was weiÃ? ich, wie viele LUNs datt Dingens hat?
+    cbw->cbw_flags = 0x80;      //IN
+    cbw->cbw_lun = 0;           //Was weiÃ? ich, wie viele LUNs datt Dingens hat?
     cbw->cbw_cb_length = 12;
     cbw->cbw_cb[0] = MSC_CMD_READ10;
-    cbw->cbw_cb[1] = 0; //LUN: 0
+    cbw->cbw_cb[1] = 0;         //LUN: 0
     cbw->cbw_cb[2] = (lba & 0xFF000000) >> 24;
     cbw->cbw_cb[3] = (lba & 0x00FF0000) >> 16;
     cbw->cbw_cb[4] = (lba & 0x0000FF00) >> 8;
-    cbw->cbw_cb[5] =  lba & 0x000000FF;
+    cbw->cbw_cb[5] = lba & 0x000000FF;
     cbw->cbw_cb[7] = (sectors & 0x0000FF00) >> 8;
-    cbw->cbw_cb[8] =  sectors & 0x000000FF;
+    cbw->cbw_cb[8] = sectors & 0x000000FF;
 
     error = write_cmd(usbdev, pcbw);
     if (error != USB_NO_ERROR) {
         return error;
     }
-    cdi_sleep_ms(20);
 
     struct usb_packet in_packet = {
-        .type           = PACKET_IN,
-        .endpoint       = msc->bulk_ep_in->endpoint_address,
-        .phys_data      = phys_buffer,
-        .length         = length,
-        .datatoggle     = 0,
-        .type_of_data   = USB_TOD_DATA_IN,
+        .type = PACKET_IN,
+        .endpoint = msc->bulk_ep_in->endpoint_address,
+        .phys_data = phys_buffer,
+        .length = length,
+        .datatoggle = 0,
+        .type_of_data = USB_TOD_DATA_IN,
     };
 
     error = usb_do_packet(usbdev, &in_packet);
     if (error != USB_NO_ERROR) {
         return error;
     }
-    cdi_sleep_ms(5);
 
-    error = read_status(usbdev, pcsw);
+    error = read_status(usbdev, expected_tag);
     if (error != USB_NO_ERROR) {
         return error;
     }
 
-    cdi_sleep_ms(4);
-    if ((csw->csw_signature != CSW_SIGNATURE) ||
-        (csw->csw_tag       !=  expected_tag) ||
-         csw->csw_status)
-    {
-        dprintf("0x%08X 0x%08X==0x%08X 0x%02X\n", csw->csw_signature,
-                                                  csw->csw_tag,
-                                                  expected_tag,
-                                                  csw->csw_status);
-        return USB_CRC; //Watt weiÃ? denn ich...
-    }
     return USB_NO_ERROR;
 }
 
-static uint32_t msd_write(struct usb_device *usbdev, uint32_t lba, uint16_t sectors, uintptr_t phys_buffer, size_t length);
+static uint32_t msd_write(struct usb_device *usbdev, uint32_t lba,
+    uint16_t sectors, uintptr_t phys_buffer, size_t length);
 
-static int msd_cdi_write(struct cdi_storage_device *strgdev, uint64_t start, uint64_t count, void *buffer)
+static int msd_cdi_write(struct cdi_storage_device *strgdev, uint64_t start,
+    uint64_t count, void *buffer)
 {
     void *vb;
     uintptr_t pb;
@@ -511,28 +484,24 @@ static int msd_cdi_write(struct cdi_storage_device *strgdev, uint64_t start, uin
 
     fdprintf("write(%i, %i)\n", (int)start, (int)count);
     start += ((struct cdi_msd *)strgdev)->offset;
-    if (cdi_alloc_phys_mem(bs, &vb, (void **)&pb) == -1)
-    {
+    if (cdi_alloc_phys_mem(bs, &vb, (void **)&pb) == -1) {
         dprintf("Blockspeicher zum Schreiben konnte nicht allociert werden.\n");
         return -1;
     }
-    while (tsl(&usbdev->locked))
-    {
+    while (tsl(&usbdev->locked)) {
 #ifndef CDI_STANDALONE
-        __asm__ __volatile__ ("hlt");
+        __asm__ __volatile__("hlt");
 #endif
     }
-    for (i = 0; i < count; i++)
-    {
-        memcpy(vb, buffer + i*bs, bs);
+    for (i = 0; i < count; i++) {
+        memcpy(vb, buffer + i * bs, bs);
 #ifdef WAIT_FOR_MSD_READY
         for (j = 0; !msd_ready(usbdev) && (j < 10); j++) {
             cdi_sleep_ms(20);
         }
 #endif
         error = msd_write(usbdev, start + i, 1, pb, bs);
-        if (error != USB_NO_ERROR)
-        {
+        if (error != USB_NO_ERROR) {
             dprintf("Schreibfehler 0x%X bei Block %i.\n", error, i);
             usbdev->locked = 0;
             return -1;
@@ -542,7 +511,8 @@ static int msd_cdi_write(struct cdi_storage_device *strgdev, uint64_t start, uin
     return 0;
 }
 
-static uint32_t msd_write(struct usb_device *usbdev, uint32_t lba, uint16_t sectors, uintptr_t phys_buffer, size_t length)
+static uint32_t msd_write(struct usb_device *usbdev, uint32_t lba,
+    uint16_t sectors, uintptr_t phys_buffer, size_t length)
 {
     struct msclass_data *msc;
     struct command_block_wrapper *cbw;
@@ -552,11 +522,11 @@ static uint32_t msd_write(struct usb_device *usbdev, uint32_t lba, uint16_t sect
     int error;
 
     if (cdi_alloc_phys_mem(sizeof(struct command_block_wrapper),
-                           (void **)&cbw, (void **)&pcbw) == -1) {
+            (void **)&cbw, (void **)&pcbw) == -1) {
         return USB_TRIVIAL_ERROR;
     }
     if (cdi_alloc_phys_mem(sizeof(struct command_status_wrapper),
-                           (void **)&csw, (void **)&pcsw) == -1) {
+            (void **)&csw, (void **)&pcsw) == -1) {
         return USB_TRIVIAL_ERROR;
     }
     if (!phys_buffer || !length) {
@@ -567,31 +537,30 @@ static uint32_t msd_write(struct usb_device *usbdev, uint32_t lba, uint16_t sect
     cbw->cbw_signature = CBW_SIGNATURE;
     cbw->cbw_tag = (expected_tag = cbw_tag++);
     cbw->cbw_data_transfer_length = length;
-    cbw->cbw_flags = 0x00; //OUT
-    cbw->cbw_lun = 0; //Was weiÃ? ich, wie viele LUNs datt Dingens hat?
+    cbw->cbw_flags = 0x00;      //OUT
+    cbw->cbw_lun = 0;           //Was weiÃ? ich, wie viele LUNs datt Dingens hat?
     cbw->cbw_cb_length = 12;
     cbw->cbw_cb[0] = MSC_CMD_WRITE10;
-    cbw->cbw_cb[1] = 0; //LUN: 0
+    cbw->cbw_cb[1] = 0;         //LUN: 0
     cbw->cbw_cb[2] = (lba & 0xFF000000) >> 24;
     cbw->cbw_cb[3] = (lba & 0x00FF0000) >> 16;
     cbw->cbw_cb[4] = (lba & 0x0000FF00) >> 8;
-    cbw->cbw_cb[5] =  lba & 0x000000FF;
+    cbw->cbw_cb[5] = lba & 0x000000FF;
     cbw->cbw_cb[7] = (sectors & 0x0000FF00) >> 8;
-    cbw->cbw_cb[8] =  sectors & 0x000000FF;
+    cbw->cbw_cb[8] = sectors & 0x000000FF;
 
     error = write_cmd(usbdev, pcbw);
     if (error != USB_NO_ERROR) {
         return error;
     }
-    cdi_sleep_ms(20);
 
     struct usb_packet out_packet = {
-        .type           = PACKET_OUT,
-        .endpoint       = msc->bulk_ep_out->endpoint_address,
-        .phys_data      = phys_buffer,
-        .length         = length,
-        .datatoggle     = 0,
-        .type_of_data   = USB_TOD_DATA_OUT,
+        .type = PACKET_OUT,
+        .endpoint = msc->bulk_ep_out->endpoint_address,
+        .phys_data = phys_buffer,
+        .length = length,
+        .datatoggle = 0,
+        .type_of_data = USB_TOD_DATA_OUT,
     };
 
     error = usb_do_packet(usbdev, &out_packet);
@@ -599,23 +568,11 @@ static uint32_t msd_write(struct usb_device *usbdev, uint32_t lba, uint16_t sect
         return error;
     }
 
-    cdi_sleep_ms(5);
-    error = read_status(usbdev, pcsw);
+    error = read_status(usbdev, expected_tag);
     if (error != USB_NO_ERROR) {
         return error;
     }
 
-    cdi_sleep_ms(4);
-    if ((csw->csw_signature != CSW_SIGNATURE) ||
-        (csw->csw_tag       !=  expected_tag) ||
-         csw->csw_status)
-    {
-        dprintf("0x%08X 0x%08X==0x%08X 0x%02X\n", csw->csw_signature,
-                                                  csw->csw_tag,
-                                                  expected_tag,
-                                                  csw->csw_status);
-        return USB_CRC;
-    }
     return USB_NO_ERROR;
 }
 
@@ -631,15 +588,15 @@ static void get_partitions(struct cdi_storage_device *cdistrg)
         dprintf("MBR konnte nicht eingelesen werden.\n");
         return;
     }
-    if (((uint16_t *)mbr)[255] != 0xAA55) {
+    if (((uint16_t *) mbr)[255] != 0xAA55) {
         return;
     }
     partition = mbr + 0x1BE;
     dprintf("Partitionen auf %s:", cdistrg->dev.name);
-    for (i = 0; i < 4; i++)
-    {
-        if (partition[i].type && partition[i].size && (partition[i].start + partition[i].size < cdistrg->block_count))
-        {
+    for (i = 0; i < 4; i++) {
+        if (partition[i].type && partition[i].size
+            && (partition[i].start + partition[i].size <
+                cdistrg->block_count)) {
             cdimsd = malloc(sizeof(struct cdi_msd));
             if (cdimsd == NULL) {
                 break;
@@ -648,12 +605,12 @@ static void get_partitions(struct cdi_storage_device *cdistrg)
             cdimsd->usb_device = base_cdimsd->usb_device;
             cdimsd->cdi_device.dev.type = CDI_STORAGE;
             cdimsd->cdi_device.dev.name = malloc(14);
-            if (cdimsd->cdi_device.dev.name == NULL)
-            {
+            if (cdimsd->cdi_device.dev.name == NULL) {
                 free(cdimsd);
                 break;
             }
-            sprintf((char *)cdimsd->cdi_device.dev.name, "%sp%i", cdistrg->dev.name, j++);
+            sprintf((char *)cdimsd->cdi_device.dev.name, "%sp%i",
+                cdistrg->dev.name, j++);
             cdimsd->cdi_device.block_size = base_cdimsd->cdi_device.block_size;
             cdimsd->cdi_device.block_count = partition[i].size;
             _dprintf(" %s", cdimsd->cdi_device.dev.name);
diff --git a/usb/uhci.c b/usb/uhci.c
index 799d526..6cdc44c 100644
--- a/usb/uhci.c
+++ b/usb/uhci.c
@@ -43,6 +43,7 @@ static int dprintf(const char *fmt, ...)
 {
     return 0;
 }
+
 #define _dprintf(fmt, args...) dprintf(fmt, ##args)
 #endif
 
@@ -52,7 +53,9 @@ static struct cdi_driver cdi_driver;
 static cdi_list_t active_transfers;
 
 static void uhci_handler(struct cdi_device *dev);
-static int enqueue_request(struct hci *gen_hci, int frame, int type, int device, int endpoint, int low_speed, uintptr_t phys_data, int length, int datatoggle);
+static int enqueue_request(struct hci *gen_hci, int frame, int type,
+    int device, int endpoint, int low_speed,
+    uintptr_t phys_data, int length, int datatoggle);
 static cdi_list_t get_devices(struct hci *gen_hci);
 static void activate_device(struct hci *gen_hci, struct usb_device *device);
 static int get_current_frame(struct hci *gen_hci);
@@ -75,14 +78,13 @@ struct cdi_driver *init_uhcd()
 
 static void uhci_kill(struct cdi_driver *cdi_hcd)
 {
-  //TODO: Da geht doch noch was
+    //TODO: Da geht doch noch was
 }
 
 static void uhci_deinit(struct cdi_device *cdi_hci)
 {
-    cdi_outw(((struct uhci *)((struct cdi_hci *)cdi_hci)->hci)->pbase + UHCI_USBCMD,
-             MAXP); //HC anhalten
-  //TODO: Hier doch auch
+    cdi_outw(((struct uhci *)((struct cdi_hci *)cdi_hci)->hci)->pbase + UHCI_USBCMD, MAXP);     //HC anhalten
+    //TODO: Hier doch auch
 }
 
 void uhci_init(struct cdi_device *cdi_hci)
@@ -95,13 +97,10 @@ void uhci_init(struct cdi_device *cdi_hci)
     //cdi_pci_alloc_ioports(gen_hci->pcidev);
     uhci->pbase = 0;
     for (i = 0; (res = cdi_list_get(gen_hci->pcidev->resources, i)) != NULL;
-         i++)
-    {
-        if (res->type == CDI_PCI_IOPORTS)
-        {
+        i++) {
+        if (res->type == CDI_PCI_IOPORTS) {
             size = res->length ? res->length : 0x14;
-            if (cdi_ioports_alloc(res->start, size) == -1)
-            {
+            if (cdi_ioports_alloc(res->start, size) == -1) {
                 dprintf("I/O-Ports konnten nicht reserviert werden.\n");
                 return;
             }
@@ -109,14 +108,12 @@ void uhci_init(struct cdi_device *cdi_hci)
             break;
         }
     }
-    if (!uhci->pbase)
-    {
+    if (!uhci->pbase) {
         dprintf("I/O-Basis nicht gefunden!\n");
         return;
     }
     if (cdi_alloc_phys_mem(4096, (void **)&uhci->frame_list,
-                                 (void **)&uhci->phys_frame_list) == -1)
-    {
+            (void **)&uhci->phys_frame_list) == -1) {
         dprintf("Frame List konnte nicht allociert werden!\n");
         return;
     }
@@ -125,9 +122,10 @@ void uhci_init(struct cdi_device *cdi_hci)
     if (uhci->root_ports > 7) { //Laut Linuxkernel ist das so "weird", dass da was nicht stimmen kann...
         uhci->root_ports = 2;
     }
-    dprintf("UHC mit I/O 0x%04X (%i Ports) und IRQ %i\n", uhci->pbase, uhci->root_ports, gen_hci->pcidev->irq);
+    dprintf("UHC mit I/O 0x%04X (%i Ports) und IRQ %i\n", uhci->pbase,
+        uhci->root_ports, gen_hci->pcidev->irq);
     for (i = 0; i < 1024; i++) {
-        uhci->frame_list[i] = 1; //Invalid
+        uhci->frame_list[i] = 1;        //Invalid
     }
     dprintf("Resetten...\n");
     //HC zurücksetzen
@@ -144,10 +142,10 @@ void uhci_init(struct cdi_device *cdi_hci)
     cdi_outw(uhci->pbase + UHCI_USBINTR, 0xF);
     //Framelistadresse eintragen
     cdi_outl(uhci->pbase + UHCI_FRBASEADD, uhci->phys_frame_list);
-    cdi_outw(uhci->pbase + UHCI_FRNUM, 0); //Frame zurücksetzen
+    cdi_outw(uhci->pbase + UHCI_FRNUM, 0);      //Frame zurücksetzen
     //Ports abschalten, damit selektiv eingeschaltet werden kann (Enumeration)
     for (i = 0; i < uhci->root_ports; i++) {
-        cdi_outw(uhci->pbase + UHCI_RPORTS + i*2, RPORT_CSC);
+        cdi_outw(uhci->pbase + UHCI_RPORTS + i * 2, RPORT_CSC);
     }
     dprintf("  Fertig\n");
     gen_hci->find_devices = &get_devices;
@@ -167,10 +165,8 @@ static cdi_list_t get_devices(struct hci *gen_hci)
 
     dprintf("Geräte finden: ");
     cdi_outw(uhci->pbase + UHCI_USBCMD, USB_RUN);
-    for (i = 0; i < uhci->root_ports; i++)
-    {
-        if (!(cdi_inw(uhci->pbase + UHCI_RPORTS + 2*i) & RPORT_DEVICE))
-        {
+    for (i = 0; i < uhci->root_ports; i++) {
+        if (!(cdi_inw(uhci->pbase + UHCI_RPORTS + 2 * i) & RPORT_DEVICE)) {
             _dprintf("-");
             continue;
         }
@@ -180,7 +176,7 @@ static cdi_list_t get_devices(struct hci *gen_hci)
         dev->id = 0;
         dev->port = i;
         dev->low_speed =
-                (cdi_inw(uhci->pbase + UHCI_RPORTS + 2*i) & RPORT_LOSPD) && 1;
+            (cdi_inw(uhci->pbase + UHCI_RPORTS + 2 * i) & RPORT_LOSPD) && 1;
         cdi_list_push(dlist, dev);
     }
     _dprintf("\n");
@@ -193,7 +189,7 @@ static void activate_device(struct hci *gen_hci, struct usb_device *device)
 
     dprintf("Gerät an Port %i wird aktiviert.\n", device->port);
     cdi_outw(uhci->pbase + UHCI_RPORTS + 2 * device->port,
-             RPORT_RESET | RPORT_ENABLE | RPORT_CSC);
+        RPORT_RESET | RPORT_ENABLE | RPORT_CSC);
     cdi_sleep_ms(20);
 }
 
@@ -209,34 +205,37 @@ static inline int tsl(volatile int *variable)
     *variable = 1;
     return rval;
 }
+
 static volatile int locked = 0;
 
-static int enqueue_request(struct hci *gen_hci, int frame, int type, int device, int endpoint, int low_speed, uintptr_t phys_data, int length, int datatoggle)
+static int enqueue_request(struct hci *gen_hci, int frame, int type,
+    int device, int endpoint, int low_speed,
+    uintptr_t phys_data, int length, int datatoggle)
 {
     struct uhci *uhci = (struct uhci *)gen_hci;
     struct uhci_td *td;
     struct uhci_qh *qh;
     uintptr_t ptd, pqh;
     struct transfer *addr;
+    int timeout;
 
     if (cdi_alloc_phys_mem(sizeof(struct uhci_td),
-                           (void **)&td, (void **)&ptd) == -1) {
+            (void **)&td, (void **)&ptd) == -1) {
         return USB_TRIVIAL_ERROR;
     }
     if (cdi_alloc_phys_mem(sizeof(struct uhci_qh),
-                           (void **)&qh, (void **)&pqh) == -1) {
+            (void **)&qh, (void **)&pqh) == -1) {
         return USB_TRIVIAL_ERROR;
     }
-    while (tsl(&locked))
-    {
+    while (tsl(&locked)) {
 #ifndef CDI_STANDALONE
-        __asm__ __volatile__ ("hlt");
+        __asm__ __volatile__("hlt");
 #endif
     }
-    qh->next = 1; //Invalid
+    qh->next = 1;               //Invalid
     qh->transfer = ptd;
     memset(td, 0, sizeof(struct uhci_td));
-    td->next = 1; //Invalid
+    td->next = 1;               //Invalid
     td->active = 1;
     td->ioc = 1;
     td->data_toggle = datatoggle;
@@ -245,7 +244,7 @@ static int enqueue_request(struct hci *gen_hci, int frame, int type, int device,
     td->pid = type;
     td->device = device;
     td->endpoint = endpoint;
-    td->maxlen = length ? length-1 : 0x7FF;
+    td->maxlen = length ? length - 1 : 0x7FF;
     td->buffer = phys_data;
     addr = malloc(sizeof(struct transfer));
     addr->virt = td;
@@ -253,20 +252,12 @@ static int enqueue_request(struct hci *gen_hci, int frame, int type, int device,
     addr->error = 0xFFFF;
     cdi_list_push(active_transfers, addr);
     uhci->frame_list[frame] = pqh | 2;
-#ifdef CDI_STANDALONE
-    /*int cframe = get_current_frame(gen_hci), nframe;
-    nframe = (cframe > frame) ? frame + 1024 : frame; //�berläufe korrigieren
-    //Annähern, wann er wohl fertig ist
-    cdi_sleep_ms(nframe + 30 - get_current_frame(gen_hci));*/
-#else
-    while (!(qh->transfer & 1)) {
-        __asm__ __volatile__ ("hlt");
+    for (timeout = 0; !(qh->transfer & 1) && (timeout < 1000); timeout++) {
+        cdi_sleep_ms(1);
     }
-#endif
-    while (addr->error == 0xFFFF)
-    {
+    while ((timeout < 1000) && (addr->error == 0xFFFF)) {
 #ifndef CDI_STANDALONE
-        __asm__ __volatile__ ("hlt");
+        __asm__ __volatile__("hlt");
 #endif
     }
     uhci->frame_list[frame] = 1;
@@ -281,54 +272,44 @@ static void uhci_handler(struct cdi_device *cdi_hci)
     struct transfer *addr;
     struct uhci_td *td;
 
-    if (!status) { //Also, von hier kommt der IRQ nicht.
+    if (!status) {              //Also, von hier kommt der IRQ nicht.
         return;
     }
     if (status & ~0x0001) {
         dprintf("Unerwarteter IRQ 0x%04X von %s\n", status, cdi_hci->name);
     }
-    if (status & 0x10)
-    {
+    if (status & 0x10) {
         printf("[uhci] SCHWERWIEGENDER FEHLER - HC WIRD ANGEHALTEN\n");
-        cdi_outw(uhci->pbase + UHCI_USBCMD, MAXP | HCRESET); //FU!
-    }
-    else
-    {
-        for (i = 0; (addr = cdi_list_get(active_transfers, i)) != NULL; i++)
-        {
+        cdi_outw(uhci->pbase + UHCI_USBCMD, MAXP | HCRESET);    //FU!
+    } else {
+        for (i = 0; (addr = cdi_list_get(active_transfers, i)) != NULL; i++) {
             td = addr->virt;
             if (td->active) {
                 continue;
             }
             addr->error = USB_NO_ERROR;
             cdi_list_remove(active_transfers, i--);
-            if (td->stalled_err)
-            {
+            if (td->stalled_err) {
                 dprintf("ENDPOINT STALLED\n");
                 addr->error |= USB_STALLED;
             }
-            if (td->buf_err)
-            {
+            if (td->buf_err) {
                 dprintf("Pufferüberlauf oder Underrun\n");
                 addr->error |= USB_BUFFER_ERROR;
             }
-            if (td->babble)
-            {
-                dprintf("Puh, da war ein Gerät wohl sehr gesprächig: Babble.\n");
+            if (td->babble) {
+                dprintf("Da war ein Gerät wohl sehr gesprächig: Babble.\n");
                 addr->error |= USB_BABBLE;
             }
-            if (td->nak)
-            {
+            if (td->nak) {
                 dprintf("NAK empfangen\n");
                 addr->error |= USB_NAK;
             }
-            if (td->crc_time_err)
-            {
+            if (td->crc_time_err) {
                 dprintf("CRC-Fehler oder Timeout\n");
                 addr->error |= USB_CRC | USB_TIMEOUT;
             }
-            if (td->bitstuff_err)
-            {
+            if (td->bitstuff_err) {
                 dprintf("Bitstufffehler\n");
                 addr->error |= USB_BITSTUFF;
             }
-- 
1.6.3.3