ps2kbd.c

来自「适合KS8695X」· C语言 代码 · 共 691 行 · 第 1/2 页

C
691
字号
/*
 * (C) Copyright 2002
 * John W. Linville, linville@tuxdriver.com
 *
 * Modified from code for support of MIP405 and PIP405 boards.  Previous
 * copyright follows.
 *
 * (C) Copyright 2001
 * Denis Peter, MPL AG Switzerland, d.peter@mpl.ch
 *
 * See file CREDITS for list of people who contributed to this
 * project.
 *
 * This program 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 2 of
 * the License, or (at your option) any later version.
 *
 * This program 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, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 * MA 02111-1307 USA
 *
 *
 * Source partly derived from:
 * linux/drivers/char/pc_keyb.c
 *
 *
 */
#include <common.h>
#include <asm/processor.h>
#include <devices.h>
#include "ps2kbd.h"


unsigned char kbd_read_status(void);
unsigned char kbd_read_input(void);
void kbd_send_data(unsigned char data);
void i8259_mask_irq(unsigned int irq);
void i8259_unmask_irq(unsigned int irq);

/* used only by send_data - set by keyboard_interrupt */


#undef KBG_DEBUG

#ifdef KBG_DEBUG
#define	PRINTF(fmt,args...)	printf (fmt ,##args)
#else
#define PRINTF(fmt,args...)
#endif

#define KBD_STAT_KOBF		0x01
#define KBD_STAT_IBF		0x02
#define KBD_STAT_SYS		0x04
#define KBD_STAT_CD			0x08
#define KBD_STAT_LOCK		0x10
#define KBD_STAT_MOBF		0x20
#define KBD_STAT_TI_OUT	0x40
#define KBD_STAT_PARERR	0x80

#define KBD_INIT_TIMEOUT 2000		/* Timeout in ms for initializing the keyboard */
#define KBC_TIMEOUT 250			/* Timeout in ms for sending to keyboard controller */
#define KBD_TIMEOUT 2000		/* Timeout in ms for keyboard command acknowledge */
/*
 *	Keyboard Controller Commands
 */

#define KBD_CCMD_READ_MODE			0x20	/* Read mode bits */
#define KBD_CCMD_WRITE_MODE			0x60	/* Write mode bits */
#define KBD_CCMD_GET_VERSION		0xA1	/* Get controller version */
#define KBD_CCMD_MOUSE_DISABLE	0xA7	/* Disable mouse interface */
#define KBD_CCMD_MOUSE_ENABLE		0xA8	/* Enable mouse interface */
#define KBD_CCMD_TEST_MOUSE			0xA9	/* Mouse interface test */
#define KBD_CCMD_SELF_TEST			0xAA	/* Controller self test */
#define KBD_CCMD_KBD_TEST				0xAB	/* Keyboard interface test */
#define KBD_CCMD_KBD_DISABLE		0xAD	/* Keyboard interface disable */
#define KBD_CCMD_KBD_ENABLE			0xAE	/* Keyboard interface enable */
#define KBD_CCMD_WRITE_AUX_OBUF	0xD3    /* Write to output buffer as if
					   initiated by the auxiliary device */
#define KBD_CCMD_WRITE_MOUSE		0xD4	/* Write the following byte to the mouse */

/*
 *	Keyboard Commands
 */

#define KBD_CMD_SET_LEDS				0xED	/* Set keyboard leds */
#define KBD_CMD_SET_RATE				0xF3	/* Set typematic rate */
#define KBD_CMD_ENABLE					0xF4	/* Enable scanning */
#define KBD_CMD_DISABLE					0xF5	/* Disable scanning */
#define KBD_CMD_RESET						0xFF	/* Reset */

/*
 *	Keyboard Replies
 */

#define KBD_REPLY_POR						0xAA	/* Power on reset */
#define KBD_REPLY_ACK						0xFA	/* Command ACK */
#define KBD_REPLY_RESEND				0xFE	/* Command NACK, send the cmd again */

/*
 *	Status Register Bits
 */

#define KBD_STAT_OBF 						0x01	/* Keyboard output buffer full */
#define KBD_STAT_IBF 						0x02	/* Keyboard input buffer full */
#define KBD_STAT_SELFTEST				0x04	/* Self test successful */
#define KBD_STAT_CMD						0x08	/* Last write was a command write (0=data) */
#define KBD_STAT_UNLOCKED				0x10	/* Zero if keyboard locked */
#define KBD_STAT_MOUSE_OBF			0x20	/* Mouse output buffer full */
#define KBD_STAT_GTO 						0x40	/* General receive/xmit timeout */
#define KBD_STAT_PERR 					0x80	/* Parity error */

#define AUX_STAT_OBF (KBD_STAT_OBF | KBD_STAT_MOUSE_OBF)

/*
 *	Controller Mode Register Bits
 */

#define KBD_MODE_KBD_INT				0x01	/* Keyboard data generate IRQ1 */
#define KBD_MODE_MOUSE_INT			0x02	/* Mouse data generate IRQ12 */
#define KBD_MODE_SYS 						0x04	/* The system flag (?) */
#define KBD_MODE_NO_KEYLOCK			0x08	/* The keylock doesn't affect the keyboard if set */
#define KBD_MODE_DISABLE_KBD		0x10	/* Disable keyboard interface */
#define KBD_MODE_DISABLE_MOUSE	0x20	/* Disable mouse interface */
#define KBD_MODE_KCC 						0x40	/* Scan code conversion to PC format */
#define KBD_MODE_RFU						0x80


#define KDB_DATA_PORT			0x60
#define KDB_COMMAND_PORT	0x64

#define 	LED_SCR		0x01	/* scroll lock led */
#define 	LED_CAP		0x04	/* caps lock led */
#define 	LED_NUM		0x02	/* num lock led */

#define 	KBD_BUFFER_LEN 0x20  /* size of the keyboardbuffer */


static volatile char kbd_buffer[KBD_BUFFER_LEN];
static volatile int in_pointer = 0;
static volatile int out_pointer = 0;


static unsigned char num_lock = 0;
static unsigned char caps_lock = 0;
static unsigned char scroll_lock = 0;
static unsigned char shift = 0;
static unsigned char ctrl = 0;
static unsigned char alt = 0;
static unsigned char e0 = 0;
static unsigned char leds = 0;

#define DEVNAME "ps2kbd"

/* Simple translation table for the keys */

static unsigned char kbd_plain_xlate[] = {
	0xff,0x1b, '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=','\b','\t',	/* 0x00 - 0x0f */
	 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']','\r',0xff, 'a', 's',	/* 0x10 - 0x1f */
	 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';','\'', '`',0xff,'\\', 'z', 'x', 'c', 'v',	/* 0x20 - 0x2f */
	 'b', 'n', 'm', ',', '.', '/',0xff,0xff,0xff, ' ',0xff,0xff,0xff,0xff,0xff,0xff,	/* 0x30 - 0x3f */
	0xff,0xff,0xff,0xff,0xff,0xff,0xff, '7', '8', '9', '-', '4', '5', '6', '+', '1',	/* 0x40 - 0x4f */
	 '2', '3', '0', '.',0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,  /* 0x50 - 0x5F */
	'\r',0xff,0xff
	};

static unsigned char kbd_shift_xlate[] = {
	0xff,0x1b, '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+','\b','\t',	/* 0x00 - 0x0f */
	 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', '{', '}','\r',0xff, 'A', 'S',	/* 0x10 - 0x1f */
	 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':', '"', '~',0xff, '|', 'Z', 'X', 'C', 'V',	/* 0x20 - 0x2f */
	 'B', 'N', 'M', '<', '>', '?',0xff,0xff,0xff, ' ',0xff,0xff,0xff,0xff,0xff,0xff,	/* 0x30 - 0x3f */
	0xff,0xff,0xff,0xff,0xff,0xff,0xff, '7', '8', '9', '-', '4', '5', '6', '+', '1',	/* 0x40 - 0x4f */
	 '2', '3', '0', '.',0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,  /* 0x50 - 0x5F */
	'\r',0xff,0xff
	};

static unsigned char kbd_ctrl_xlate[] = {
	0xff,0x1b, '1',0x00, '3', '4', '5',0x1E, '7', '8', '9', '0',0x1F, '=','\b','\t',	/* 0x00 - 0x0f */
	0x11,0x17,0x05,0x12,0x14,0x18,0x15,0x09,0x0f,0x10,0x1b,0x1d,'\n',0xff,0x01,0x13,	/* 0x10 - 0x1f */
	0x04,0x06,0x08,0x09,0x0a,0x0b,0x0c, ';','\'', '~',0x00,0x1c,0x1a,0x18,0x03,0x16,	/* 0x20 - 0x2f */
	0x02,0x0e,0x0d, '<', '>', '?',0xff,0xff,0xff,0x00,0xff,0xff,0xff,0xff,0xff,0xff,	/* 0x30 - 0x3f */
	0xff,0xff,0xff,0xff,0xff,0xff,0xff, '7', '8', '9', '-', '4', '5', '6', '+', '1',	/* 0x40 - 0x4f */
	 '2', '3', '0', '.',0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,  /* 0x50 - 0x5F */
	'\r',0xff,0xff
	};

/******************************************************************
 * Init
 ******************************************************************/

int isa_kbd_init(void)
{
	char* result;
	result=kbd_initialize();
	if (result != NULL)
	{
	    result = kbd_initialize();
	}
	if(result==NULL) {
		printf("AT Keyboard initialized\n");
		irq_install_handler(KBD_INTERRUPT, (interrupt_handler_t *)kbd_interrupt, NULL);
		return (1);
	}
	else {
		printf("%s\n",result);
		return (-1);
	}
}

#ifdef CFG_CONSOLE_OVERWRITE_ROUTINE
extern int overwrite_console (void);
#else
int overwrite_console (void)
{
	return (0);
}
#endif

int drv_isa_kbd_init (void)
{
	int error;
  	device_t kbddev ;
	char *stdinname  = getenv ("stdin");

	if(isa_kbd_init()==-1)
		return -1;
  	memset (&kbddev, 0, sizeof(kbddev));
  	strcpy(kbddev.name, DEVNAME);
  	kbddev.flags =  DEV_FLAGS_INPUT | DEV_FLAGS_SYSTEM;
  	kbddev.putc = NULL ;
	kbddev.puts = NULL ;
	kbddev.getc = kbd_getc ;
	kbddev.tstc = kbd_testc ;

 	error = device_register (&kbddev);
	if(error==0) {
		/* check if this is the standard input device */
		if(strcmp(stdinname,DEVNAME)==0) {
			/* reassign the console */
			if(overwrite_console()) {
				return 1;
			}
			error=console_assign(stdin,DEVNAME);
			if(error==0)
				return 1;
			else
				return error;
		}
		return 1;
	}
	return error;
}

/******************************************************************
 * Queue handling
 ******************************************************************/
/* puts character in the queue and sets up the in and out pointer */
void kbd_put_queue(char data)
{
	if((in_pointer+1)==KBD_BUFFER_LEN) {
		if(out_pointer==0) {
			return; /* buffer full */
		} else{
			in_pointer=0;
		}
	} else {
		if((in_pointer+1)==out_pointer)
			return; /* buffer full */
		in_pointer++;
	}
	kbd_buffer[in_pointer]=data;
	return;
}

/* test if a character is in the queue */
int kbd_testc(void)
{
	if(in_pointer==out_pointer)
		return(0); /* no data */
	else
		return(1);
}
/* gets the character from the queue */
int kbd_getc(void)
{
	char c;

	while(in_pointer==out_pointer);
	if((out_pointer+1)==KBD_BUFFER_LEN)
		out_pointer=0;
	else
		out_pointer++;
	c=kbd_buffer[out_pointer];
	return (int)c;

}


/* set LEDs */

void kbd_set_leds(void)
{
	if(caps_lock==0)
		leds&=~LED_CAP; /* switch caps_lock off */
	else
		leds|=LED_CAP; /* switch on LED */
	if(num_lock==0)
		leds&=~LED_NUM; /* switch LED off */
	else
		leds|=LED_NUM;  /* switch on LED */
	if(scroll_lock==0)
		leds&=~LED_SCR; /* switch LED off */
	else
		leds|=LED_SCR; /* switch on LED */
	kbd_send_data(KBD_CMD_SET_LEDS);
	kbd_send_data(leds);
}


void handle_keyboard_event(unsigned char scancode)
{
	unsigned char keycode;

	/*  Convert scancode to keycode */
	PRINTF("scancode %x\n",scancode);
	if(scancode==0xe0) {
		e0=1; /* special charakters */
		return;
	}
	if(e0==1) {
		e0=0; /* delete flag */
		if(!(	((scancode&0x7F)==0x38)|| /* the right ctrl key */
			((scancode&0x7F)==0x1D)|| /* the right alt key */
			((scancode&0x7F)==0x35)||	/* the right '/' key */
			((scancode&0x7F)==0x1C)||  /* the right enter key */
			((scancode)==0x48)|| /* arrow up */
			((scancode)==0x50)|| /* arrow down */
			((scancode)==0x4b)|| /* arrow left */
			((scancode)==0x4d)))  /* arrow right */
			/* we swallow unknown e0 codes */

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?