/*
 * 5799-WZQ (C) COPYRIGHT IBM CORPORATION 1986,1988
 * LICENSED MATERIALS - PROPERTY OF IBM
 * REFER TO COPYRIGHT INSTRUCTIONS FORM NUMBER G120-2083
 */
/* $Header:keyboard.h 12.0$ */
/* $ACIS:keyboard.h 12.0$ */
/* $Source: /ibm/acis/usr/sys/standca/RCS/keyboard.h,v $ */

#if !defined(lint) && !defined(LOCORE) && defined(RCS_HDRS)
static char *rcsidkeyboard = "$Header:keyboard.h 12.0$";
#endif


#if !defined(DEBOUNCE)
#define DEBOUNCE	1
#endif

#define NONE	0377			  /* code for no code defined */
#define END	NONE, NONE		  /* no code for end */
#define E ESC_FLAG +

#define ESC_MARK 0x100
#define ALT_MARK 0x80

int getchar_timeout;			  /* used to provide 'default' input */
char getchar_char;			  /* the character to return */


#define KYB_CNTIW 0x8407		  /* 8255 control port write */
#define KYB_CNTIR 0x8406		  /* 8255 control port read */
#define KYB_READ  0x8404		  /* 8255 data read */
#define KYB_WRITE 0x8400		  /* 8255 data write */
#define KBD_ADAPTOR_RESET 0xFB		  /* adaptor reset */
#define KBD_ADAPTOR_RELEASE 0x04	  /* adaptor release */
#define CRRB	0x8c60			  /* conponent reset reg B */

#define KYB_IIC_MASK	0x0F		  /* Interrupt Ident Code Mask */
#define KYB_INT		0x08		  /* Interrupt Received bit */
#define KYB_INFO	0x08		  /* Information Interrupt */
#define KYB_DATA	0x09		  /* Keyboard data int */
#define UART_DATA	0x0A		  /* Uart data int */
#define KYB_REQD	0x0B		  /* Return requested byte int */
#define UART_BLK	0x0C		  /* Block Transfer int */
#define KYB_UNASS	0x0D		  /* Unassigned */
#define KYB_SR		0x0E		  /* Softr Reset/Completion code */
#define KYB_EC		0x0F		  /* Detected an error condition */
#define KYB_BUSY	0x10		  /* Keyboard busy bit           */

#define IID_MASK	0x07		  /* mask for Ident code */
#define KBD_INFO	0x00		  /* information */
#define KBD_DATA	0x01		  /* value for data present */
#define KBD_UART	0x02		  /* byte from UART */
#define KBD_REQ		0x03		  /* returned requested byte */
#define KBD_BLOCK	0x04		  /* block request */
#define KBD_RESERVED	0x05		  /* reserved */
#define KBD_SELF_TEST	0x06
#define KBD_ERROR	0x07
#define KBD_TIMEOUT	0x100		  /* if we have timed out */

/*    Adapter commands                                               */

#define       EXTCMD      0x00		  /*  Extended command select  */
#define	        READ_STAT 0x12		  /* Read status byte from shared mem */
#define		SP_MEDIUM	0x42	  /*  set speaker medium	   */
#define       UARTCMD     0x04		  /*  Command to uart          */
 /*  These cmds return 2 datablocks                         */
#define         RSMOUSE   0x01		  /*  Reset  mouse             */
#define         QYMOUSE   0x73		  /*  Query  mouse             */
#define       UARTCNT     0x03		  /*  Control to uart          */
#define         MSTRANS   0x08		  /*  Enable mouse for transm. */
#define       KYBDCMD     0x01		  /*  Command to kybd          */
#define         KRESET    0xFF		  /*  Keyboard reset           */
#define         KDEFDS    0xF5		  /*  Kybd default disable     */
#define         KSCAN     0xF4		  /*  Kybd start scanning      */
#define		  NKSCAN	0xF3		  /*  Turn off Kybd scanning    */
#define         READID    0xF2		  /*  Read kybd id             */
#define         SETLED    0xED		  /*  Set kybd LEDs            */
#define       WRRAM       0x10		  /*  Write shr RAM            */
#define       RDRAM       0x00		  /*  Read  shr RAM            */
#define       RD1C        0x1C		  /*  Read shr RAM 0x1C.     */
#define       SETFCHI     0x08		  /*  Set freq counter hi byte */
#define         FCHI      0x01		  /*  Hi frequency byte        */
#define       SETFCLO     0x09		  /*  Set freq counter lo byte */
#define         FCLO      0xAC		  /*  Lo frequency byte        */
#define       SPKRON      0x02		  /*  Turn on speaker          */
#define         SPTIME    0X04		  /*  Speaker duration         */
#define       ENKYBD      0x3B		  /*  Enable kybd mode bit 11  */
#define       ENUART      0x3C		  /*  Enable UART mode bit 12  */
#define       RDVERS      0xE0		  /*  Read version             */
#define       DSLOCK      0x2D		  /*  Disable keylock, bit 13  */
#define       CMDREJ      0x7F		  /*  Command reject received  */
#define       POER        0xFF		  /*  Power-on error report.   */
#define       INFO        0x00		  /*  Int id for information   */


#define KYB_CONFIG	0xc3		  /* 8255 configuration value */

/* Keyboard leds */
#define	      NUM_LED     0x04		  /* Num lock led             */
#define	      CAPS_LED    0x02		  /* Caps lock led            */
#define	      SCROLL_LED  0x01		  /* Scroll lock led          */

/*
 * following bits are in KYB_CNTIR
 */
#define KYB_IBF		0x20		  /* IBF (input buffer full?) */
#define KYB_OBF		0x80		  /* OBF (output buffer full?) */
#define CMD(cmd,data) cmd + (data <<8)	/* pack data and command */

int debug;
#ifdef DEBUG
#define TRACEF(x) if (debug) printf x			/* debugging stuff */
#else
#define TRACEF(x) 					/* debugging stuff */
#endif

static int last_char, key_count;
static last_scan;
static int state, make_break;
static char kbd_leds_state = 0;
