]> git.friedersdorff.com Git - max/tmk_keyboard.git/commitdiff
add system controls(power down/wake up) from generic desktop page(HID)
authortmk <nobody@nowhere>
Thu, 18 Nov 2010 13:35:49 +0000 (22:35 +0900)
committertmk <nobody@nowhere>
Thu, 18 Nov 2010 13:35:49 +0000 (22:35 +0900)
hhkb/keymap.c
key_process.c
tmk.c
usb.c
usb.h
usb_extra.c
usb_extra.h

index ea8e39a934720768e5d38b7642430210d6c3b892..42a830c527d72c8bddb598ea1ea18c07c5679726 100644 (file)
@@ -71,7 +71,7 @@ static const uint8_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
 
     /* Layer 1: HHKB mode (HHKB Fn)
      * ,-----------------------------------------------------------.
-     * |Pow| F1| F2| F3| F4| F5| F6| F7| F8| F9|F10|F11|F12|Ins|Del|
+     * |Esc| F1| F2| F3| F4| F5| F6| F7| F8| F9|F10|F11|F12|Ins|Del|
      * |-----------------------------------------------------------|
      * |Caps |   |   |   |   |   |   |   |Psc|Slk|Pus|Up |   |Backs|
      * |-----------------------------------------------------------|
@@ -82,7 +82,7 @@ static const uint8_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
      *      |Gui |Alt  |Space                  |Alt  |xxx|
      *      `--------------------------------------------'
      */ 
-    KEYMAP(KB_PWR, KB_F1,  KB_F2,  KB_F3,  KB_F4,  KB_F5,  KB_F6,  KB_F7,  KB_F8,  KB_F9,  KB_F10, KB_F11, KB_F12, KB_INS, KB_DEL, \
+    KEYMAP(KB_ESC, KB_F1,  KB_F2,  KB_F3,  KB_F4,  KB_F5,  KB_F6,  KB_F7,  KB_F8,  KB_F9,  KB_F10, KB_F11, KB_F12, KB_INS, KB_DEL, \
            KB_CAPS,KB_NO,  KB_NO,  KB_NO,  KB_NO,  KB_NO,  KB_NO,  KB_NO,  KB_PSCR,KB_SLCK,KB_BRK, KB_UP,  KB_NO,  KB_BSPC, \
            KB_LCTL,KB_VOLD,KB_VOLU,KB_MUTE,KB_NO,  KB_NO,  KP_ASTR,KP_SLSH,KB_HOME,KB_PGUP,KB_LEFT,KB_RGHT,KB_ENT, \
            KB_LSFT,KB_NO,  KB_NO,  KB_NO,  KB_NO,  KB_NO,  KP_PLUS,KP_MINS,KB_END, KB_PGDN,KB_DOWN,KB_RSFT,FN_1, \
index 243f4aad8a19d35849fc959f151f8cd51d255f97..0bd20280610fc1c91671db654a810431263c6d6e 100644 (file)
@@ -10,6 +10,7 @@
 #include "usb_mouse.h"
 #include "usb_extra.h"
 #include "usb_keycodes.h"
+#include "usb.h"
 #include "layer.h"
 #include "matrix_skel.h"
 #include "keymap_skel.h"
@@ -31,7 +32,6 @@ void proc_matrix(void) {
     static int mouse_repeat = 0;
 
     bool modified = false;
-    //bool has_ghost = false;
     int key_index = 0;
     uint8_t mouse_btn = 0;
     int8_t mouse_x = 0;
@@ -42,7 +42,7 @@ void proc_matrix(void) {
 
     matrix_scan();
     modified = matrix_is_modified();
-
+    
     if (modified) {
         if (debug_matrix) matrix_print();
 #ifdef DEBUG_LED
@@ -63,46 +63,54 @@ void proc_matrix(void) {
         for (int col = 0; col < matrix_cols(); col++) {
             if (!matrix_is_on(row, col)) continue;
 
+            // TODO: clean code
             uint8_t code = layer_get_keycode(row, col);
             if (code == KB_NO) {
                 // do nothing
             } else if (IS_MOD(code)) {
                 usb_keyboard_mods |= MOD_BIT(code);
+            } else if (IS_FN(code)) {
+                fn_bits |= FN_BIT(code);
             } else if (IS_MOUSE(code)) {
-                // mouse
-                if (code == MS_UP)
-                    mouse_y -= MOUSE_MOVE_UNIT + MOUSE_MOVE_ACCEL;
-                if (code == MS_DOWN)
-                    mouse_y += MOUSE_MOVE_UNIT + MOUSE_MOVE_ACCEL;
-                if (code == MS_LEFT)
-                    mouse_x -= MOUSE_MOVE_UNIT + MOUSE_MOVE_ACCEL;
-                if (code == MS_RIGHT)
-                    mouse_x += MOUSE_MOVE_UNIT + MOUSE_MOVE_ACCEL;
+                if (code == MS_UP)   mouse_y -= MOUSE_MOVE_UNIT + MOUSE_MOVE_ACCEL;
+                if (code == MS_DOWN) mouse_y += MOUSE_MOVE_UNIT + MOUSE_MOVE_ACCEL;
+                if (code == MS_LEFT) mouse_x -= MOUSE_MOVE_UNIT + MOUSE_MOVE_ACCEL;
+                if (code == MS_RGHT) mouse_x += MOUSE_MOVE_UNIT + MOUSE_MOVE_ACCEL;
                 if (code == MS_BTN1) mouse_btn |= BIT_BTN1;
                 if (code == MS_BTN2) mouse_btn |= BIT_BTN2;
                 if (code == MS_BTN3) mouse_btn |= BIT_BTN3;
                 if (code == MS_BTN4) mouse_btn |= BIT_BTN4;
                 if (code == MS_BTN5) mouse_btn |= BIT_BTN5;
-                if (code == MS_WH_UP)    mouse_vwheel  += 1;
-                if (code == MS_WH_DOWN)  mouse_vwheel  -= 1;
-                if (code == MS_WH_LEFT)  mouse_hwheel -= 1;
-                if (code == MS_WH_RIGHT) mouse_hwheel += 1;
-            } else if (IS_FN(code)) {
-                fn_bits |= FN_BIT(code);
-            } else if (code == KB_MUTE) {
-                usb_extra_send(AUDIO_MUTE);
-                usb_extra_send(0);
+                if (code == MS_WH_U) mouse_vwheel += 1;
+                if (code == MS_WH_D) mouse_vwheel -= 1;
+                if (code == MS_WH_L) mouse_hwheel -= 1;
+                if (code == MS_WH_R) mouse_hwheel += 1;
+            }
+
+            // audio control & system control
+            else if (code == KB_MUTE) {
+                usb_extra_audio_send(AUDIO_MUTE);
+                usb_extra_audio_send(0);
                 _delay_ms(500);
             } else if (code == KB_VOLU) {
-                usb_extra_send(AUDIO_VOL_UP);
-                usb_extra_send(0);
+                usb_extra_audio_send(AUDIO_VOL_UP);
+                usb_extra_audio_send(0);
                 _delay_ms(100);
             } else if (code == KB_VOLD) {
-                usb_extra_send(AUDIO_VOL_DOWN);
-                usb_extra_send(0);
+                usb_extra_audio_send(AUDIO_VOL_DOWN);
+                usb_extra_audio_send(0);
                 _delay_ms(100);
-            } else {
-                // normal keys
+            } else if (code == KB_PWR) {
+                if (suspend && remote_wakeup) {
+                    usb_remote_wakeup();
+                } else {
+                    usb_extra_system_send(SYSTEM_POWER_DOWN);
+                }
+                _delay_ms(1000);
+            }
+
+            // normal keys
+            else {
                 if (key_index < 6)
                     usb_keyboard_keys[key_index] = code;
                 key_index++;
@@ -120,6 +128,7 @@ void proc_matrix(void) {
 
     layer_switching(fn_bits);
 
+    // TODO: clean code
     // when 4 left modifier keys down
     if (keymap_is_special_mode(fn_bits)) {
         switch (usb_keyboard_keys[0]) {
@@ -127,12 +136,14 @@ void proc_matrix(void) {
                 print_enable = true;
                 print("b: jump to bootloader\n");
                 print("d: debug print toggle\n");
+                print("x: matrix debug toggle\n");
                 print("k: keyboard debug toggle\n");
                 print("m: mouse debug toggle\n");
-                print("x: matrix debug toggle\n");
+                print("p: print enable toggle\n");
                 print("v: print version\n");
                 print("t: print timer count\n");
-                print("p: print enable toggle\n");
+                print("r: print registers\n");
+                print("ESC: power down/wake up\n");
                 _delay_ms(500);
                 print_enable = false;
                 break;
@@ -219,6 +230,23 @@ void proc_matrix(void) {
                 }
                 _delay_ms(1000);
                 break;
+            case KB_R:
+                usb_keyboard_clear_report();
+                usb_keyboard_send();
+                print("UDIEN: "); phex(UDIEN); print("\n");
+                print("UDINT: "); phex(UDINT); print("\n");
+                _delay_ms(1000);
+                break;
+            case KB_ESC:
+                usb_keyboard_clear_report();
+                usb_keyboard_send();
+                if (suspend && remote_wakeup) {
+                    usb_remote_wakeup();
+                } else {
+                    usb_extra_system_send(SYSTEM_POWER_DOWN);
+                }
+                _delay_ms(1000);
+                break;
         }
     }
 
diff --git a/tmk.c b/tmk.c
index 2ad31e97d490ed82016a22780a39225f9a2878b7..f7042bd6fbe2754267d8d48e0c41c72f4c7be1b1 100644 (file)
--- a/tmk.c
+++ b/tmk.c
@@ -36,6 +36,7 @@
 #include "util.h"
 #include "controller.h"
 #include "timer.h"
+#include "jump_bootloader.h"
 
 
 #define CPU_PRESCALE(n)    (CLKPR = 0x80, CLKPR = (n))
@@ -65,14 +66,8 @@ int main(void)
 
     matrix_init();
     matrix_scan();
-    // debug on by pressing down any 4 or more keys during boot time.
+    // bootloader comes up when any 4 or more keys are pressed at startup
     if (matrix_key_count() >= 4) {
-        print_enable = true;
-        debug_enable = true;
-    }
-
-    /* wait for debug pipe ready */
-    if (print_enable) {
 #ifdef DEBUG_LED
         for (int i = 0; i < 6; i++) {
             DEBUG_LED_CONFIG;
@@ -82,11 +77,13 @@ int main(void)
             _delay_ms(500);
         }
 #else
-            _delay_ms(6000);
+        _delay_ms(5000);
 #endif
+        print_enable = true;
+        print("jump to bootloader...\n");
+        _delay_ms(1000);
+        jump_bootloader(); // not return
     }
-    // print description
-    print(STR(DESCRIPTION) "\n");
 
     while (1) {
        proc_matrix(); 
diff --git a/usb.c b/usb.c
index 7add57daa32f04fffc91629c3c92b2ebb1461ac7..d3aa46e2de2dccd8345c4b405823a72334409528 100644 (file)
--- a/usb.c
+++ b/usb.c
@@ -21,6 +21,8 @@
  * THE SOFTWARE.
  */
 
+#include <stdint.h>
+#include <stdbool.h>
 #include <avr/io.h>
 #include <avr/pgmspace.h>
 #include <avr/interrupt.h>
@@ -82,6 +84,9 @@
 
 #define ENDPOINT0_SIZE         32
 
+bool remote_wakeup = false;
+bool suspend = false;
+
 // 0:control endpoint is enabled automatically by controller.
 static const uint8_t PROGMEM endpoint_config_table[] = {
        // enable, UECFG0X(type, direction), UECFG1X(size, bank, allocation)
@@ -250,7 +255,7 @@ static uint8_t PROGMEM debug_hid_report_desc[] = {
        0xC0                                    // end collection
 };
 
-// audio controls(consumer page)
+// audio controls & system controls
 // http://www.microsoft.com/whdc/archive/w2kbd.mspx
 static uint8_t PROGMEM extra_hid_report_desc[] = {
     0x05, 0x0c,                    // USAGE_PAGE (Consumer Devices)
@@ -272,6 +277,18 @@ static uint8_t PROGMEM extra_hid_report_desc[] = {
     0x81, 0x06,                    //   INPUT (Data,Var,Rel)
     0x95, 0x05,                    //   REPORT_COUNT (5)
     0x81, 0x07,                    //   INPUT (Cnst,Var,Abs)
+    0xc0,                          // END_COLLECTION
+
+    0x05, 0x01,                    // USAGE_PAGE (Generic Desktop)
+    0x09, 0x80,                    // USAGE (System Control)
+    0xa1, 0x01,                    // COLLECTION (Application)
+    0x85, 0x02,                    //   REPORT_ID (2)
+    0x19, 0x81,                    //   USAGE_MINIMUM (System Power Down)
+    0x29, 0x83,                    //   USAGE_MAXIMUM (System Wake Up)
+    0x95, 0x03,                    //   REPORT_COUNT (3)
+    0x81, 0x06,                    //   INPUT (Data,Var,Rel)
+    0x95, 0x05,                    //   REPORT_COUNT (5)
+    0x81, 0x07,                    //   INPUT (Cnst,Var,Rel)
     0xc0                           // END_COLLECTION
 };
 
@@ -289,7 +306,7 @@ static uint8_t PROGMEM config1_descriptor[CONFIG1_DESC_SIZE] = {
        4,                                      // bNumInterfaces
        1,                                      // bConfigurationValue
        0,                                      // iConfiguration
-       0xC0,                                   // bmAttributes
+       0xA0,                                   // bmAttributes
        50,                                     // bMaxPower
 
        // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
@@ -484,7 +501,7 @@ void usb_init(void)
         USB_CONFIG();                          // start USB clock
         UDCON = 0;                             // enable attach resistor
        usb_configuration = 0;
-        UDIEN = (1<<EORSTE)|(1<<SOFE);
+        UDIEN = (1<<EORSTE)|(1<<SOFE)|(1<<SUSPE);
        sei();
 }
 
@@ -492,7 +509,12 @@ void usb_init(void)
 // number selected by the HOST
 uint8_t usb_configured(void)
 {
-       return usb_configuration;
+       return usb_configuration && !suspend;
+}
+
+void usb_remote_wakeup(void)
+{
+    UDCON |= (1<<RMWKUP);
 }
 
 
@@ -515,6 +537,11 @@ ISR(USB_GEN_vect)
 
         intbits = UDINT;
         UDINT = 0;
+        if (intbits & (1<<SUSPI)) {
+            suspend = true;
+        } else {
+            suspend = false;
+        }
         if (intbits & (1<<EORSTI)) {
                UENUM = 0;
                UECONX = 1;
@@ -693,9 +720,9 @@ ISR(USB_COM_vect)
                        usb_send_in();
                        return;
                }
-               #ifdef SUPPORT_ENDPOINT_HALT
-               if ((bRequest == CLEAR_FEATURE || bRequest == SET_FEATURE)
-                 && bmRequestType == 0x02 && wValue == 0) {
+               if (bRequest == CLEAR_FEATURE || bRequest == SET_FEATURE) {
+#ifdef SUPPORT_ENDPOINT_HALT
+                   if (bmRequestType == 0x02 && wValue == ENDPOINT_HALT) {
                        i = wIndex & 0x7F;
                        if (i >= 1 && i <= MAX_ENDPOINT) {
                                usb_send_in();
@@ -709,8 +736,18 @@ ISR(USB_COM_vect)
                                }
                                return;
                        }
+                    }
+#endif
+                    if (bmRequestType == 0x00 && wValue == DEVICE_REMOTE_WAKEUP) {
+                        if (bRequest == SET_FEATURE) {
+                            remote_wakeup = true;   
+                        } else {
+                            remote_wakeup = false;
+                        }
+                        usb_send_in();
+                        return;
+                    }
                }
-               #endif
                if (wIndex == KEYBOARD_INTERFACE) {
                        if (bmRequestType == 0xA1) {
                                if (bRequest == HID_GET_REPORT) {
diff --git a/usb.h b/usb.h
index cb2f6a223289147faa89bd329e57532cc0ae4433..594f307db81f3f871e509565663b07475b0ec7b6 100644 (file)
--- a/usb.h
+++ b/usb.h
@@ -2,11 +2,13 @@
 #define  USB_H 1
 
 #include <stdint.h>
+#include <stdbool.h>
 #include <avr/io.h>
 
 
 void usb_init(void);                   // initialize everything
 uint8_t usb_configured(void);          // is the USB port configured
+void usb_remote_wakeup(void);
 
 
 
@@ -78,5 +80,13 @@ uint8_t usb_configured(void);                // is the USB port configured
 #define CDC_SET_LINE_CODING            0x20
 #define CDC_GET_LINE_CODING            0x21
 #define CDC_SET_CONTROL_LINE_STATE     0x22
+// HID feature selectors
+#define DEVICE_REMOTE_WAKEUP           1
+#define ENDPOINT_HALT                  0
+#define TEST_MODE                      2
+
+
+extern bool remote_wakeup;
+extern bool suspend;
 
 #endif
index 94c317d981e11057a0ee5c4082735a47b7f74d62..9bc0c3f5f3cde0dece69d3ef08138d74ff56e3f9 100644 (file)
@@ -1,7 +1,8 @@
 #include <avr/interrupt.h>
 #include "usb_extra.h"
 
-int8_t usb_extra_send(uint8_t bits)
+
+int8_t usb_extra_send(uint8_t report_id, uint8_t bits)
 {
        uint8_t intr_state, timeout;
 
@@ -24,10 +25,20 @@ int8_t usb_extra_send(uint8_t bits)
                UENUM = EXTRA_ENDPOINT;
        }
 
-       UEDATX = 1; // report id
+       UEDATX = report_id;
        UEDATX = bits;
 
        UEINTX = 0x3A;
        SREG = intr_state;
        return 0;
 }
+
+int8_t usb_extra_audio_send(uint8_t bits)
+{
+       return usb_extra_send(1, bits);
+}
+
+int8_t usb_extra_system_send(uint8_t bits)
+{
+       return usb_extra_send(2, bits);
+}
index 499e6b2fb340bd6f15dac9d1588373a281eb7e16..202f3223b366fca445d44f0cc958d1b319fde057 100644 (file)
 #define EXTRA_SIZE             2
 #define EXTRA_BUFFER           EP_DOUBLE_BUFFER
 
+// http://www.microsoft.com/whdc/archive/w2kbd.mspx
 #define AUDIO_VOL_UP           (1<<0)
 #define AUDIO_VOL_DOWN         (1<<1)
 #define AUDIO_MUTE             (1<<2)
 
+#define SYSTEM_POWER_DOWN      (1<<0)
+#define SYSTEM_SLEEP           (1<<1)
+#define SYSTEM_WAKE_UP         (1<<2)
 
-int8_t usb_extra_send(uint8_t bits);
+
+int8_t usb_extra_audio_send(uint8_t bits);
+int8_t usb_extra_system_send(uint8_t bits);
 
 #endif