/******************************************************************//**
 * Zerocat PS/2 Keyboard --- Get rid of keyloggers,
 *                           avoid USB firmware attacks!
 *
 * Copyright (C) 2017, 2018 Kai Mertens <kmx@posteo.net>
 *
 * The Zerocat PS/2 Keyboard is free software: you can redistribute it
 * and/or modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation, either version 3 of
 * the License, or (at your option) any later version.
 *
 * The Zerocat PS/2 Keyboard is distributed in the hope that it will be
 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * This file is part of the Zerocat PS/2 Keyboard.
 *
 *
 * @file
 *
 */


#ifndef __MAIN_H__
#define __MAIN_H__


/**
 * @defgroup CommandBytes Command Bytes
 * @brief Command Bytes
 *
 * Compare to: <http://wiki.osdev.org/PS/2_Keyboard>, Section "Commands"
 *
 * Command  | Data Byte/s | Description | Response
 * :------  | :---------- | :---------- | :-------
 * 0xed     | Bit0 = ScrollLock, Bit1 = NumLock, Bit2 = CapsLock | Set LEDs | 0xFA (ACK) or 0xFE (Resend)
 * 0xee     | None | Echo (for diagnostic purposes, and useful for device removal detection) | 0xEE (Echo) or 0xFE (Resend)
 * 0xf0     | 0 = Get current scan code set, 1..3 = Set scan code set 1..3 | Get/set code set | 0xFA (ACK) or 0xFE (Resend) if scan code is being set; 0xFA (ACK) then the scan code set number, or 0xFE (Resend) if you're getting the scancode
 * 0xf2     | None | Identify keyboard | 0xFA (ACK) followed by none or more ID bytes (see "Detecting Device Types")
 * 0xf3     | Bits0..4 = Repeat Rate (00000b = 30Hz, ..., 11111b = 2Hz); Bits5..6: Delay before keys repeat (00b = 250ms, ..., 11b =1000ms); Bit7: must be zero | Set typematic rate and delay | 0xFA (ACK) or 0xFE (Resend)
 * 0xf4     | None | Enable scanning (keyboard will send scan codes) | 0xFA (ACK) or 0xFE (Resend)
 * 0xf5     | None | Disable scanning (keyboard won't send scan codes) Note: May also restore default parameters | 0xFA (ACK) or 0xFE (Resend)
 * 0xf6     | None | Set default parameters | 0xFA (ACK) or 0xFE (Resend)
 * 0xf7     | None | Set all keys to typematic/autorepeat only (scancode set 3 only) | 0xFA (ACK) or 0xFE (Resend)
 * 0xf8     | None | Set all keys to make/release (scancode set 3 only) | 0xFA (ACK) or 0xFE (Resend)
 * 0xf9     | None | Set all keys to make only (scancode set 3 only) | 0xFA (ACK) or 0xFE (Resend)
 * 0xfa     | None | Set all keys to typematic/autorepeat/make/release (scancode set 3 only) |  0xFA (ACK) or 0xFE (Resend)
 * 0xfb     | Scancode for key | Set specific key to typematic/autorepeat only (scancode set 3 only) | 0xFA (ACK) or 0xFE (Resend)
 * 0xfc     | Scancode for key | Set specific key to make/release (scancode set 3 only) | 0xFA (ACK) or 0xFE (Resend)
 * 0xfd     | Scancode for key | Set specific key to make only (scancode set 3 only) | 0xFA (ACK) or 0xFE (Resend)
 * 0xfe     | None | Resend last byte | Previously sent byte or 0xFE (Resend)
 * 0xff     | None | Reset and start self-test | 0xAA (self-test passed), 0xFC or 0xFD (self test failed), or 0xFE (Resend)
 *
 */


/**
 * @defgroup SpecialBytes Special Response Bytes
 * @brief Special Response Bytes
 *
 * Compare to: <http://wiki.osdev.org/PS/2_Keyboard>, Section "Special Bytes"
 */


/**
 * @ingroup CommandBytes
 * @{
 */
#define SET_LEDS                                            0xed
#define ECHO                                                0xee
#define GET_SET_CURRENT_CODE_SET                            0xf0
#define IDENTIFY_KEYBOARD                                   0xf2
#define SET_TYPEMATIC_RATE_AND_DELAY                        0xf3
#define ENABLE_SCANNING                                     0xf4
#define DISABLE_SCANNING                                    0xf5
#define SET_DEFAULT_PARAMETERS                              0xf6
#define SET_ALL_KEYS_TO_TYPEMATIC_AUTOREPEAT_ONLY           0xf7
#define SET_ALL_KEYS_TO_MAKE_RELEASE                        0xf8
#define SET_ALL_KEYS_TO_MAKE_ONLY                           0xf9
#define SET_ALL_KEYS_TO_TYPEMATIC_AUTOREPEAT_MAKE_RELEASE   0xfa
#define SET_SPECIFIC_KEY_TO_TYPEMATIC_AUTOREPEAT_ONLY       0xfb
#define SET_SPECIFIC_KEY_TO_MAKE_RELEASE                    0xfc
#define SET_SPECIFIC_KEY_TO_MAKE_ONLY                       0xfd
#define RESEND_LAST_BYTE                                    0xfe
#define RESET_AND_START_SELF_TEST                           0xff
/** @} */


/**
 * @ingroup SpecialBytes
 * @{
 */
#define KEY_DETECTION_ERROR         0x00    //!< Key detection error or internal buffer overrun
#define SELF_TEST_PASSED            0xaa    //!< sent after "0xFF (reset)" command or keyboard power up
#define RESPONSE_TO_ECHO            0xee    //!< Response to "0xEE (echo)" command
#define COMMAND_ACKNOWLEDGED        0xfa    //!< Command acknowledged (ACK)
#define SELF_TEST_FAILED            0xfc    //!< or 0xfd; Self test failed (sent after "0xFF (reset)" command or keyboard power up)
#define RESEND_LAST_COMMAND         0xfe    //!< Resend (keyboard wants controller to repeat last command it sent)
#define INTERNAL_BUFFER_OVERRUN     0xff    //!< Key detection error or internal buffer overrun
/** @} */


/**
 * @brief Struct that characterizes keyboard scan codes of set 1.
 */
struct scancode_set1_t {
  char * key_cap;               //!< What is written on the key.
  unsigned long long key_make;  //!< Key Make Code Sequence, up to 6 bytes.
  unsigned long key_break;      //!< Key Break Code Sequence, up to 4 bytes.
};

/**
 * @brief Struct that characterizes keyboard scan codes of set 2.
 */
struct scancode_set2_t {
  char * key_cap;               //!< What is written on the key.
  unsigned long long key_make;  //!< Key Make Code Sequence, up to 8 bytes.
  unsigned long long key_break; //!< Key Break Code Sequence, up to 6 bytes.
};

/**
 * @brief Struct that characterizes keyboard scan codes of set 3.
 */
struct scancode_set3_t {
  char * key_cap;               //!< What is written on the key.
  unsigned char key_make;       //!< Key Make Code Sequence, one byte.
  unsigned int key_break;       //!< Key Break Code Sequence, two bytes.
};


#endif
/* __MAIN_H__ */
