]> git.donarmstrong.com Git - kiibohd-controller.git/blobdiff - Output/pjrcUSB/arm/usb_dev.c
Fixing long-standing SET_REPORT bug
[kiibohd-controller.git] / Output / pjrcUSB / arm / usb_dev.c
index e41a4e3fa1424b6601682dd99e81e5f9641dd964..e913006e615d06970ed8e5408f0ced007a2b7af9 100644 (file)
@@ -1,7 +1,7 @@
 /* Teensyduino Core Library
  * http://www.pjrc.com/teensy/
  * Copyright (c) 2013 PJRC.COM, LLC.
- * Modifications by Jacob Alexander (2013-2014)
+ * Modifications by Jacob Alexander (2013-2015)
  *
  * Permission is hereby granted, free of charge, to any person obtaining
  * a copy of this software and associated documentation files (the
@@ -34,6 +34,7 @@
 // Project Includes
 #include <Lib/OutputLib.h>
 #include <print.h>
+#include <kll_defs.h>
 
 // Local Includes
 #include "usb_dev.h"
 //#define UART_DEBUG_UNKNOWN 1
 
 
-#define TX_STATE_BOTH_FREE_EVEN_FIRST  0
-#define TX_STATE_BOTH_FREE_ODD_FIRST   1
-#define TX_STATE_EVEN_FREE             2
-#define TX_STATE_ODD_FREE              3
-#define TX_STATE_NONE_FREE_EVEN_FIRST  4
-#define TX_STATE_NONE_FREE_ODD_FIRST   5
+#define TX_STATE_BOTH_FREE_EVEN_FIRST   0
+#define TX_STATE_BOTH_FREE_ODD_FIRST    1
+#define TX_STATE_EVEN_FREE              2
+#define TX_STATE_ODD_FREE               3
+#define TX_STATE_NONE_FREE_EVEN_FIRST   4
+#define TX_STATE_NONE_FREE_ODD_FIRST    5
 
-#define BDT_OWN                0x80
-#define BDT_DATA1      0x40
-#define BDT_DATA0      0x00
-#define BDT_DTS                0x08
-#define BDT_STALL      0x04
+#define BDT_OWN         0x80
+#define BDT_DATA1       0x40
+#define BDT_DATA0       0x00
+#define BDT_DTS         0x08
+#define BDT_STALL       0x04
 
 #define TX    1
 #define RX    0
 #define DATA1 1
 
 
-#define GET_STATUS             0
-#define CLEAR_FEATURE          1
-#define SET_FEATURE            3
-#define SET_ADDRESS            5
-#define GET_DESCRIPTOR         6
-#define SET_DESCRIPTOR         7
-#define GET_CONFIGURATION      8
-#define SET_CONFIGURATION      9
-#define GET_INTERFACE          10
-#define SET_INTERFACE          11
-#define SYNCH_FRAME            12
+#define GET_STATUS              0
+#define CLEAR_FEATURE           1
+#define SET_FEATURE             3
+#define SET_ADDRESS             5
+#define GET_DESCRIPTOR          6
+#define SET_DESCRIPTOR          7
+#define GET_CONFIGURATION       8
+#define SET_CONFIGURATION       9
+#define GET_INTERFACE           10
+#define SET_INTERFACE           11
+#define SYNCH_FRAME             12
 
-#define TX_STATE_BOTH_FREE_EVEN_FIRST  0
-#define TX_STATE_BOTH_FREE_ODD_FIRST   1
-#define TX_STATE_EVEN_FREE             2
-#define TX_STATE_ODD_FREE              3
-#define TX_STATE_NONE_FREE             4
+#define TX_STATE_BOTH_FREE_EVEN_FIRST   0
+#define TX_STATE_BOTH_FREE_ODD_FIRST    1
+#define TX_STATE_EVEN_FREE              2
+#define TX_STATE_ODD_FREE               3
+#define TX_STATE_NONE_FREE              4
 
 
 
@@ -96,9 +97,9 @@
 
 // ----- Macros -----
 
-#define BDT_PID(n)     (((n) >> 2) & 15)
+#define BDT_PID(n)      (((n) >> 2) & 15)
 
-#define BDT_DESC(count, data)  (BDT_OWN | BDT_DTS \
+#define BDT_DESC(count, data)   (BDT_OWN | BDT_DTS \
                                | ((data) ? BDT_DATA1 : BDT_DATA0) \
                                | ((count) << 16))
 
@@ -173,6 +174,9 @@ static uint8_t reply_buffer[8];
 
 static void endpoint0_stall()
 {
+       #ifdef UART_DEBUG_UNKNOWN
+       print("STALL" NL );
+       #endif
        USB0_ENDPT0 = USB_ENDPT_EPSTALL | USB_ENDPT_EPRXEN | USB_ENDPT_EPTXEN | USB_ENDPT_EPHSHK;
 }
 
@@ -198,12 +202,14 @@ static void usb_setup()
        switch ( setup.wRequestAndType )
        {
        case 0x0500: // SET_ADDRESS
-               break;
+               goto send;
+
        case 0x0900: // SET_CONFIGURATION
                #ifdef UART_DEBUG
                print("CONFIGURE - ");
                #endif
                usb_configuration = setup.wValue;
+               Output_Available = usb_configuration;
                reg = &USB0_ENDPT1;
                cfg = usb_endpoint_config_table;
                // clear all BDT entries, free any allocated memory...
@@ -225,8 +231,8 @@ static void usb_setup()
                                usb_free(p);
                                p = n;
                        }
-                       rx_first[i] = NULL;
-                       rx_last[i] = NULL;
+                       rx_first[ i ] = NULL;
+                       rx_last[ i ] = NULL;
                        p = tx_first[i];
                        while (p)
                        {
@@ -234,18 +240,19 @@ static void usb_setup()
                                usb_free(p);
                                p = n;
                        }
-                       tx_first[i] = NULL;
-                       tx_last[i] = NULL;
+                       tx_first[ i ] = NULL;
+                       tx_last[ i ] = NULL;
                        usb_rx_byte_count_data[i] = 0;
 
-                       switch (tx_state[i]) {
+                       switch ( tx_state[ i ] )
+                       {
                        case TX_STATE_EVEN_FREE:
                        case TX_STATE_NONE_FREE_EVEN_FIRST:
-                               tx_state[i] = TX_STATE_BOTH_FREE_EVEN_FIRST;
+                               tx_state[ i ] = TX_STATE_BOTH_FREE_EVEN_FIRST;
                                break;
                        case TX_STATE_ODD_FREE:
                        case TX_STATE_NONE_FREE_ODD_FIRST:
-                               tx_state[i] = TX_STATE_BOTH_FREE_ODD_FIRST;
+                               tx_state[ i ] = TX_STATE_BOTH_FREE_ODD_FIRST;
                                break;
                        default:
                                break;
@@ -263,43 +270,46 @@ static void usb_setup()
                                p = usb_malloc();
                                if ( p )
                                {
-                                       table[index(i, RX, EVEN)].addr = p->buf;
-                                       table[index(i, RX, EVEN)].desc = BDT_DESC(64, 0);
+                                       table[ index( i, RX, EVEN ) ].addr = p->buf;
+                                       table[ index( i, RX, EVEN ) ].desc = BDT_DESC( 64, 0 );
                                }
                                else
                                {
-                                       table[index(i, RX, EVEN)].desc = 0;
+                                       table[ index( i, RX, EVEN ) ].desc = 0;
                                        usb_rx_memory_needed++;
                                }
                                p = usb_malloc();
                                if ( p )
                                {
-                                       table[index(i, RX, ODD)].addr = p->buf;
-                                       table[index(i, RX, ODD)].desc = BDT_DESC(64, 1);
+                                       table[ index( i, RX, ODD ) ].addr = p->buf;
+                                       table[ index( i, RX, ODD ) ].desc = BDT_DESC( 64, 1 );
                                }
                                else
                                {
-                                       table[index(i, RX, ODD)].desc = 0;
+                                       table[ index( i, RX, ODD ) ].desc = 0;
                                        usb_rx_memory_needed++;
                                }
                        }
-                       table[index(i, TX, EVEN)].desc = 0;
-                       table[index(i, TX, ODD)].desc = 0;
+                       table[ index( i, TX, EVEN ) ].desc = 0;
+                       table[ index( i, TX, ODD ) ].desc = 0;
                }
-               break;
+               goto send;
+
        case 0x0880: // GET_CONFIGURATION
                reply_buffer[0] = usb_configuration;
                datalen = 1;
                data = reply_buffer;
-               break;
+               goto send;
+
        case 0x0080: // GET_STATUS (device)
                reply_buffer[0] = 0;
                reply_buffer[1] = 0;
                datalen = 2;
                data = reply_buffer;
-               break;
+               goto send;
+
        case 0x0082: // GET_STATUS (endpoint)
-               if (setup.wIndex > NUM_ENDPOINTS)
+               if ( setup.wIndex > NUM_ENDPOINTS )
                {
                        // TODO: do we need to handle IN vs OUT here?
                        endpoint0_stall();
@@ -311,18 +321,33 @@ static void usb_setup()
                        reply_buffer[0] = 1;
                data = reply_buffer;
                datalen = 2;
-               break;
+               goto send;
+
+       case 0x0100: // CLEAR_FEATURE (device)
+       case 0x0101: // CLEAR_FEATURE (interface)
+               // TODO: Currently ignoring, perhaps useful? -HaaTa
+               warn_print("CLEAR_FEATURE - Device/Interface");
+               endpoint0_stall();
+               return;
+
        case 0x0102: // CLEAR_FEATURE (endpoint)
                i = setup.wIndex & 0x7F;
                if ( i > NUM_ENDPOINTS || setup.wValue != 0 )
                {
-                       // TODO: do we need to handle IN vs OUT here?
                        endpoint0_stall();
                        return;
                }
                (*(uint8_t *)(&USB0_ENDPT0 + setup.wIndex * 4)) &= ~0x02;
                // TODO: do we need to clear the data toggle here?
-               break;
+               goto send;
+
+       case 0x0300: // SET_FEATURE (device)
+       case 0x0301: // SET_FEATURE (interface)
+               // TODO: Currently ignoring, perhaps useful? -HaaTa
+               warn_print("SET_FEATURE - Device/Interface");
+               endpoint0_stall();
+               return;
+
        case 0x0302: // SET_FEATURE (endpoint)
                i = setup.wIndex & 0x7F;
                if ( i > NUM_ENDPOINTS || setup.wValue != 0 )
@@ -333,20 +358,19 @@ static void usb_setup()
                }
                (*(uint8_t *)(&USB0_ENDPT0 + setup.wIndex * 4)) |= 0x02;
                // TODO: do we need to clear the data toggle here?
-               break;
+               goto send;
+
        case 0x0680: // GET_DESCRIPTOR
        case 0x0681:
                #ifdef UART_DEBUG
                print("desc:");
-               printHex(setup.wValue);
-               print(NL);
+               printHex( setup.wValue );
+               print( NL );
                #endif
                for ( list = usb_descriptor_list; 1; list++ )
                {
                        if ( list->addr == NULL )
                                break;
-                       //if (setup.wValue == list->wValue &&
-                       //(setup.wIndex == list->wIndex) || ((setup.wValue >> 8) == 3)) {
                        if ( setup.wValue == list->wValue && setup.wIndex == list->wIndex )
                        {
                                data = list->addr;
@@ -363,23 +387,23 @@ static void usb_setup()
                                }
                                #if UART_DEBUG
                                print("Desc found, ");
-                               printHex32((uint32_t)data);
+                               printHex32( (uint32_t)data );
                                print(",");
-                               printHex(datalen);
+                               printHex( datalen );
                                print(",");
-                               printHex_op(data[0], 2);
-                               printHex_op(data[1], 2);
-                               printHex_op(data[2], 2);
-                               printHex_op(data[3], 2);
-                               printHex_op(data[4], 2);
-                               printHex_op(data[5], 2);
-                               print(NL);
+                               printHex_op( data[0], 2 );
+                               printHex_op( data[1], 2 );
+                               printHex_op( data[2], 2 );
+                               printHex_op( data[3], 2 );
+                               printHex_op( data[4], 2 );
+                               printHex_op( data[5], 2 );
+                               print( NL );
                                #endif
                                goto send;
                        }
                }
                #ifdef UART_DEBUG
-               print("desc: not found"NL);
+               print( "desc: not found" NL );
                #endif
                endpoint0_stall();
                return;
@@ -387,8 +411,7 @@ static void usb_setup()
        case 0x2221: // CDC_SET_CONTROL_LINE_STATE
                usb_cdc_line_rtsdtr = setup.wValue;
                //serial_print("set control line state\n");
-               endpoint0_stall();
-               return;
+               goto send;
 
        case 0x21A1: // CDC_GET_LINE_CODING
                data = (uint8_t*)usb_cdc_line_coding;
@@ -398,29 +421,50 @@ static void usb_setup()
        case 0x2021: // CDC_SET_LINE_CODING
                // XXX Needed?
                //serial_print("set coding, waiting...\n");
-               return; // Cannot stall here (causes issues)
+               return;
 
        case 0x0921: // HID SET_REPORT
-               #ifdef UART_DEBUG
-               print("SET_REPORT - ");
-               printHex( setup.wValue );
-               print(" - ");
-               printHex( setup.wValue & 0xFF );
-               print(NL);
-               #endif
-               USBKeys_LEDs = setup.wValue & 0xFF;
-               endpoint0_stall();
+               // Interface
+               switch ( setup.wIndex & 0xFF )
+               {
+               // Keyboard Interface
+               case KEYBOARD_INTERFACE:
+                       break;
+               // NKRO Keyboard Interface
+               case NKRO_KEYBOARD_INTERFACE:
+                       break;
+               default:
+                       warn_msg("Unknown interface - ");
+                       printHex( setup.wIndex );
+                       print( NL );
+                       endpoint0_stall();
+                       break;
+               }
+
                return;
 
        case 0x01A1: // HID GET_REPORT
                #ifdef UART_DEBUG
                print("GET_REPORT - ");
-               printHex( USBKeys_LEDs );
+               printHex( setup.wIndex );
                print(NL);
                #endif
-               data = (uint8_t*)&USBKeys_LEDs;
-               datalen = 1;
-               goto send;
+               // Search through descriptors returning necessary info
+               for ( list = usb_descriptor_list; 1; list++ )
+               {
+                       if ( list->addr == NULL )
+                               break;
+                       if ( list->wValue != 0x2200 )
+                               continue;
+                       if ( setup.wIndex == list->wIndex )
+                       {
+                               data = list->addr;
+                               datalen = list->length;
+                               goto send;
+                       }
+                }
+                endpoint0_stall();
+                return;
 
        case 0x0A21: // HID SET_IDLE
                #ifdef UART_DEBUG
@@ -430,8 +474,7 @@ static void usb_setup()
                #endif
                USBKeys_Idle_Config = (setup.wValue >> 8);
                USBKeys_Idle_Count = 0;
-               endpoint0_stall();
-               return;
+               goto send;
 
        case 0x0B21: // HID SET_PROTOCOL
                #ifdef UART_DEBUG
@@ -442,8 +485,7 @@ static void usb_setup()
                print(NL);
                #endif
                USBKeys_Protocol = setup.wValue & 0xFF; // 0 - Boot Mode, 1 - NKRO Mode
-               endpoint0_stall();
-               return;
+               goto send;
 
        // case 0xC940:
        default:
@@ -457,10 +499,16 @@ static void usb_setup()
 send:
        #ifdef UART_DEBUG
        print("setup send ");
-       printHex32((uint32_t)data);
+       printHex32( (uint32_t)data );
        print(",");
-       printHex(datalen);
-       print(NL);
+       for ( uint8_t c = 0; c < datalen; c++ )
+       {
+               printHex( data[c] );
+               print(" ");
+       }
+       print(",");
+       printHex( datalen );
+       print( NL );
        #endif
 
        if ( datalen > setup.wLength )
@@ -570,6 +618,10 @@ static void usb_control( uint32_t stat )
                printHex(setup.wIndex);
                print(", len:");
                printHex(setup.wLength);
+               print(" -- ");
+               printHex32(setup.word1);
+               print(" ");
+               printHex32(setup.word2);
                print(NL);
                #endif
                // actually "do" the setup request
@@ -577,11 +629,25 @@ static void usb_control( uint32_t stat )
                // unfreeze the USB, now that we're ready
                USB0_CTL = USB_CTL_USBENSOFEN; // clear TXSUSPENDTOKENBUSY bit
                break;
+
        case 0x01:  // OUT transaction received from host
        case 0x02:
-               #ifdef UART_DEBUG
-               print("PID=OUT"NL);
+               #ifdef UART_DEBUG_UNKNOWN
+               print("PID=OUT wRequestAndType:");
+               printHex(setup.wRequestAndType);
+               print(", wValue:");
+               printHex(setup.wValue);
+               print(", wIndex:");
+               printHex(setup.wIndex);
+               print(", len:");
+               printHex(setup.wLength);
+               print(" -- ");
+               printHex32(setup.word1);
+               print(" ");
+               printHex32(setup.word2);
+               print(NL);
                #endif
+
                // CDC Interface
                if ( setup.wRequestAndType == 0x2021 /*CDC_SET_LINE_CODING*/ )
                {
@@ -600,17 +666,38 @@ static void usb_control( uint32_t stat )
                        endpoint0_transmit( NULL, 0 );
                }
 
-               // Keyboard Interface
-               if ( setup.word1 == 0x02000921 && setup.word2 == ( (1<<16) | KEYBOARD_INTERFACE ) )
-               {
-                       USBKeys_LEDs = buf[0];
-                       endpoint0_transmit( NULL, 0 );
-               }
-               // NKRO Keyboard Interface
-               if ( setup.word1 == 0x02000921 && setup.word2 == ( (1<<16) | NKRO_KEYBOARD_INTERFACE ) )
+               // Keyboard SET_REPORT
+               if ( setup.wRequestAndType == 0x921 && setup.wValue & 0x200 )
                {
-                       USBKeys_LEDs = buf[0];
-                       endpoint0_transmit( NULL, 0 );
+                       // Interface
+                       switch ( setup.wIndex & 0xFF )
+                       {
+                       // Keyboard Interface
+                       case KEYBOARD_INTERFACE:
+                               USBKeys_LEDs = buf[0];
+                               endpoint0_transmit( NULL, 0 );
+                               break;
+                       // NKRO Keyboard Interface
+                       case NKRO_KEYBOARD_INTERFACE:
+                               // Only use 2nd byte, first byte is the report id
+                               USBKeys_LEDs = buf[1];
+                               endpoint0_transmit( NULL, 0 );
+                               break;
+                       default:
+                               warn_msg("Unknown interface - ");
+                               printHex( setup.wIndex );
+                               print( NL );
+                               break;
+                       }
+
+                       #ifdef UART_DEBUG
+                       for ( size_t len = 0; len < setup.wLength; len++ )
+                       {
+                               printHex( buf[ len ] );
+                               print(" ");
+                       }
+                       print( NL );
+                       #endif
                }
 
                // give the buffer back
@@ -648,6 +735,7 @@ static void usb_control( uint32_t stat )
                }
 
                break;
+
        default:
                #ifdef UART_DEBUG
                print("PID=unknown:");
@@ -734,12 +822,14 @@ void usb_rx_memory( usb_packet_t *packet )
        cfg = usb_endpoint_config_table;
        //serial_print("rx_mem:");
        __disable_irq();
-       for (i=1; i <= NUM_ENDPOINTS; i++) {
-               if (*cfg++ & USB_ENDPT_EPRXEN) {
+       for ( i = 1; i <= NUM_ENDPOINTS; i++ )
+       {
+               if ( *cfg++ & USB_ENDPT_EPRXEN )
+               {
                        if ( table[ index( i, RX, EVEN ) ].desc == 0 )
                        {
-                               table[index(i, RX, EVEN)].addr = packet->buf;
-                               table[index(i, RX, EVEN)].desc = BDT_DESC( 64, 0 );
+                               table[ index( i, RX, EVEN ) ].addr = packet->buf;
+                               table[ index( i, RX, EVEN ) ].desc = BDT_DESC( 64, 0 );
                                usb_rx_memory_needed--;
                                __enable_irq();
                                //serial_phex(i);
@@ -821,6 +911,14 @@ void usb_tx( uint32_t endpoint, usb_packet_t *packet )
 
 void usb_device_reload()
 {
+       if ( flashModeEnabled_define == 0 )
+       {
+               print( NL );
+               warn_print("flashModeEnabled not set, cancelling firmware reload...");
+               info_msg("Set flashModeEnabled to 1 in your kll configuration.");
+               return;
+       }
+
 // MCHCK
 #if defined(_mk20dx128vlf5_)
 
@@ -831,7 +929,7 @@ void usb_device_reload()
        PORTA_PCR3 = PORT_PCR_PFE | PORT_PCR_MUX(1); // Internal pull-up
 
        // Check for jumper
-       if ( GPIOA_PDIR & (1<<3) )
+       if ( GPIOA_PDIR & (1<<3) && flashModeEnabled_define != 0 )
        {
                print( NL );
                warn_print("Security jumper not present, cancelling firmware reload...");
@@ -845,6 +943,13 @@ void usb_device_reload()
                SOFTWARE_RESET();
        }
 
+// Kiibohd mk20dx256vlh7
+#elif defined(_mk20dx256vlh7_)
+       // Copies variable into the VBAT register, must be identical to the variable in the bootloader to jump to the bootloader flash mode
+       for ( int pos = 0; pos < sizeof(sys_reset_to_loader_magic); pos++ )
+               (&VBAT)[ pos ] = sys_reset_to_loader_magic[ pos ];
+       SOFTWARE_RESET();
+
 // Teensy 3.0 and 3.1
 #else
        asm volatile("bkpt");
@@ -860,7 +965,7 @@ void usb_isr()
        //status = USB0_ISTAT;
        //serial_phex(status);
        //serial_print("\n");
-       restart:
+restart:
        status = USB0_ISTAT;
        /*
        print("USB ISR STATUS: ");
@@ -920,12 +1025,12 @@ void usb_isr()
                        serial_phex(b->desc >> 16);
                        serial_print("\n");
 #endif
-                       endpoint--;     // endpoint is index to zero-based arrays
+                       endpoint--;     // endpoint is index to zero-based arrays
 
                        if ( stat & 0x08 )
                        { // transmit
                                usb_free( packet );
-                               packet = tx_first[endpoint];
+                               packet = tx_first[ endpoint ];
                                if ( packet )
                                {
                                        //serial_print("tx packet\n");
@@ -934,21 +1039,21 @@ void usb_isr()
                                        switch ( tx_state[ endpoint ] )
                                        {
                                        case TX_STATE_BOTH_FREE_EVEN_FIRST:
-                                               tx_state[endpoint] = TX_STATE_ODD_FREE;
+                                               tx_state[ endpoint ] = TX_STATE_ODD_FREE;
                                                break;
                                        case TX_STATE_BOTH_FREE_ODD_FIRST:
-                                               tx_state[endpoint] = TX_STATE_EVEN_FREE;
+                                               tx_state[ endpoint ] = TX_STATE_EVEN_FREE;
                                                break;
                                        case TX_STATE_EVEN_FREE:
-                                               tx_state[endpoint] = TX_STATE_NONE_FREE_ODD_FIRST;
+                                               tx_state[ endpoint ] = TX_STATE_NONE_FREE_ODD_FIRST;
                                                break;
                                        case TX_STATE_ODD_FREE:
-                                               tx_state[endpoint] = TX_STATE_NONE_FREE_EVEN_FIRST;
+                                               tx_state[ endpoint ] = TX_STATE_NONE_FREE_EVEN_FIRST;
                                                break;
                                        default:
                                                break;
                                        }
-                                       b->desc = BDT_DESC(packet->len, ((uint32_t)b & 8) ? DATA1 : DATA0);
+                                       b->desc = BDT_DESC( packet->len, ((uint32_t)b & 8) ? DATA1 : DATA0 );
                                } else {
                                        //serial_print("tx no packet\n");
                                        switch ( tx_state[ endpoint ] )
@@ -957,15 +1062,15 @@ void usb_isr()
                                        case TX_STATE_BOTH_FREE_ODD_FIRST:
                                                break;
                                        case TX_STATE_EVEN_FREE:
-                                               tx_state[endpoint] = TX_STATE_BOTH_FREE_EVEN_FIRST;
+                                               tx_state[ endpoint ] = TX_STATE_BOTH_FREE_EVEN_FIRST;
                                                break;
                                        case TX_STATE_ODD_FREE:
-                                               tx_state[endpoint] = TX_STATE_BOTH_FREE_ODD_FIRST;
+                                               tx_state[ endpoint ] = TX_STATE_BOTH_FREE_ODD_FIRST;
                                                break;
                                        default:
-                                               tx_state[endpoint] = ((uint32_t)b & 8)
-                                                 ? TX_STATE_ODD_FREE
-                                                 : TX_STATE_EVEN_FREE;
+                                               tx_state[ endpoint ] = ((uint32_t)b & 8)
+                                                       ? TX_STATE_ODD_FREE
+                                                       : TX_STATE_EVEN_FREE;
                                                break;
                                        }
                                }
@@ -973,17 +1078,18 @@ void usb_isr()
                        else
                        { // receive
                                packet->len = b->desc >> 16;
-                               if (packet->len > 0) {
+                               if ( packet->len > 0 )
+                               {
                                        packet->index = 0;
                                        packet->next = NULL;
-                                       if (rx_first[endpoint] == NULL)
+                                       if ( rx_first[ endpoint ] == NULL )
                                        {
                                                //serial_print("rx 1st, epidx=");
                                                //serial_phex(endpoint);
                                                //serial_print(", packet=");
                                                //serial_phex32((uint32_t)packet);
                                                //serial_print("\n");
-                                               rx_first[endpoint] = packet;
+                                               rx_first[ endpoint ] = packet;
                                        }
                                        else
                                        {
@@ -992,10 +1098,10 @@ void usb_isr()
                                                //serial_print(", packet=");
                                                //serial_phex32((uint32_t)packet);
                                                //serial_print("\n");
-                                               rx_last[endpoint]->next = packet;
+                                               rx_last[ endpoint ]->next = packet;
                                        }
-                                       rx_last[endpoint] = packet;
-                                       usb_rx_byte_count_data[endpoint] += packet->len;
+                                       rx_last[ endpoint ] = packet;
+                                       usb_rx_byte_count_data[ endpoint ] += packet->len;
                                        // TODO: implement a per-endpoint maximum # of allocated packets
                                        // so a flood of incoming data on 1 endpoint doesn't starve
                                        // the others if the user isn't reading it regularly
@@ -1003,7 +1109,7 @@ void usb_isr()
                                        if ( packet )
                                        {
                                                b->addr = packet->buf;
-                                               b->desc = BDT_DESC(64, ((uint32_t)b & 8) ? DATA1 : DATA0);
+                                               b->desc = BDT_DESC( 64, ((uint32_t)b & 8) ? DATA1 : DATA0 );
                                        }
                                        else
                                        {
@@ -1016,7 +1122,7 @@ void usb_isr()
                                }
                                else
                                {
-                                       b->desc = BDT_DESC(64, ((uint32_t)b & 8) ? DATA1 : DATA0);
+                                       b->desc = BDT_DESC( 64, ((uint32_t)b & 8) ? DATA1 : DATA0 );
                                }
                        }
 
@@ -1038,12 +1144,12 @@ void usb_isr()
                ep0_tx_bdt_bank = 0;
 
                // set up buffers to receive Setup and OUT packets
-               table[index(0, RX, EVEN)].desc = BDT_DESC(EP0_SIZE, 0);
-               table[index(0, RX, EVEN)].addr = ep0_rx0_buf;
-               table[index(0, RX, ODD)].desc = BDT_DESC(EP0_SIZE, 0);
-               table[index(0, RX, ODD)].addr = ep0_rx1_buf;
-               table[index(0, TX, EVEN)].desc = 0;
-               table[index(0, TX, ODD)].desc = 0;
+               table[index( 0, RX, EVEN ) ].desc = BDT_DESC( EP0_SIZE, 0 );
+               table[index( 0, RX, EVEN ) ].addr = ep0_rx0_buf;
+               table[index( 0, RX, ODD ) ].desc = BDT_DESC( EP0_SIZE, 0 );
+               table[index( 0, RX, ODD ) ].addr = ep0_rx1_buf;
+               table[index( 0, TX, EVEN ) ].desc = 0;
+               table[index( 0, TX, ODD ) ].desc = 0;
 
                // activate endpoint 0
                USB0_ENDPT0 = USB_ENDPT_EPRXEN | USB_ENDPT_EPTXEN | USB_ENDPT_EPHSHK;
@@ -1091,12 +1197,11 @@ void usb_isr()
                //serial_print("sleep\n");
                USB0_ISTAT = USB_ISTAT_SLEEP;
        }
-
 }
 
 
 
-void usb_init()
+uint8_t usb_init()
 {
        #ifdef UART_DEBUG
        print("USB INIT"NL);
@@ -1145,6 +1250,8 @@ void usb_init()
 
        // enable d+ pullup
        USB0_CONTROL = USB_CONTROL_DPPULLUPNONOTG;
+
+       return 1;
 }
 
 // return 0 if the USB is not configured, or the configuration