1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
  
     | 
    
      /*
 * Copyright (C) 2003, 2004 Stefan Reinauer
 *
 * See the file "COPYING" for further information about
 * the copyright and warranty status of this work.
 */
#include "openbios/config.h"
#include "openbios/bindings.h"
#include "openbios/kernel.h"
#include "openbios/drivers.h"
#include "libc/vsprintf.h"
/* ******************************************************************
 *          simple polling video/keyboard console functions
 * ****************************************************************** */
/*
 *  keyboard driver
 */
static const char normal[] = {
	0x0, 0x1b, '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-',
	'=', '\b', '\t', 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o',
	'p', '[', ']', 0xa, 0x0, 'a', 's', 'd', 'f', 'g', 'h', 'j',
	'k', 'l', ';', 0x27, 0x60, 0x0, 0x5c, 'z', 'x', 'c', 'v', 'b',
	'n', 'm', ',', '.', '/', 0x0, '*', 0x0, ' ', 0x0, 0x0, 0x0,
	0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
	0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, '0', 0x7f
};
static const char shifted[] = {
	0x0, 0x1b, '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_',
	'+', '\b', '\t', 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O',
	'P', '{', '}', 0xa, 0x0, 'A', 'S', 'D', 'F', 'G', 'H', 'J',
	'K', 'L', ':', 0x22, '~', 0x0, '|', 'Z', 'X', 'C', 'V', 'B',
	'N', 'M', '<', '>', '?', 0x0, '*', 0x0, ' ', 0x0, 0x0, 0x0,
	0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, '7', '8',
	'9', 0x0, '4', '5', '6', 0x0, '1', '2', '3', '0', 0x7f
};
static int key_ext;
static int key_lshift = 0, key_rshift = 0, key_caps = 0;
static char last_key;
static void pc_kbd_cmd(unsigned char cmd, unsigned char val)
{
	outb(cmd, 0x60);
	/* wait until keyboard controller accepts cmds: */
	while (inb(0x64) & 2);
	outb(val, 0x60);
	while (inb(0x64) & 2);
}
static void pc_kbd_controller_cmd(unsigned char cmd, unsigned char val)
{
	outb(cmd, 0x64);
	/* wait until keyboard controller accepts cmds: */
	while (inb(0x64) & 2);
	outb(val, 0x60);
	while (inb(0x64) & 2);
}
static char pc_kbd_poll(void)
{
	unsigned int c;
	if (inb(0x64) & 1) {
		c = inb(0x60);
		switch (c) {
		case 0xe0:
			key_ext = 1;
			return 0;
		case 0x2a:
			key_lshift = 1;
			return 0;
		case 0x36:
			key_rshift = 1;
			return 0;
		case 0xaa:
			key_lshift = 0;
			return 0;
		case 0xb6:
			key_rshift = 0;
			return 0;
		case 0x3a:
			if (key_caps) {
				key_caps = 0;
				pc_kbd_cmd(0xed, 0);
			} else {
				key_caps = 1;
				pc_kbd_cmd(0xed, 4);	/* set caps led */
			}
			return 0;
		}
		if (key_ext) {
			// void printk(const char *format, ...);
			printk("extended keycode: %x\n", c);
			key_ext = 0;
			return 0;
		}
		if (c & 0x80)	/* unhandled key release */
			return 0;
		if (key_lshift || key_rshift)
			return key_caps ? normal[c] : shifted[c];
		else
			return key_caps ? shifted[c] : normal[c];
	}
	return 0;
}
int pc_kbd_dataready(void)
{
	if (last_key)
		return 1;
	last_key = pc_kbd_poll();
	return (last_key != 0);
}
unsigned char pc_kbd_readdata(void)
{
	char tmp;
	while (!pc_kbd_dataready());
	tmp = last_key;
	last_key = 0;
	return tmp;
}
/* ( addr len -- actual ) */
static void
pc_kbd_read(void)
{
    unsigned char *addr;
    int len;
    len = POP();
    addr = (unsigned char *)POP();
    if (len != 1)
        printk("pc_kbd_read: bad len, addr %lx len %x\n", (unsigned long)addr, len);
    if (pc_kbd_dataready()) {
        *addr = pc_kbd_readdata();
        PUSH(1);
    } else {
        PUSH(0);
    }
}
static void
pc_kbd_close(void)
{
}
static void
pc_kbd_open(unsigned long *address)
{
    int len;
    phandle_t ph;
    unsigned long *prop;
    char *args;
    fword("my-self");
    fword("ihandle>phandle");
    ph = (phandle_t)POP();
    prop = (unsigned long *)get_property(ph, "address", &len);
    *address = *prop;
    fword("my-args");
    args = pop_fstr_copy();
    RET ( -1 );
}
DECLARE_UNNAMED_NODE(pc_kbd, INSTALL_OPEN, sizeof(unsigned long));
NODE_METHODS(pc_kbd) = {
    { "open",               pc_kbd_open              },
    { "close",              pc_kbd_close             },
    { "read",               pc_kbd_read              },
};
void
ob_pc_kbd_init(const char *path, const char *dev_name, uint64_t base,
               uint64_t offset, int intr)
{
    phandle_t chosen, aliases;
    char nodebuff[128];
    snprintf(nodebuff, sizeof(nodebuff), "%s/%s", path, dev_name);
    REGISTER_NAMED_NODE(pc_kbd, nodebuff);
    push_str(nodebuff);
    fword("find-device");
    push_str(dev_name);
    fword("device-name");
    push_str("keyboard");
    fword("device-type");
    chosen = find_dev("/chosen");
    push_str(nodebuff);
    fword("open-dev");
    set_int_property(chosen, "keyboard", POP());
    aliases = find_dev("/aliases");
    set_property(aliases, "keyboard", nodebuff, strlen(nodebuff) + 1);
    pc_kbd_controller_cmd(0x60, 0x40); // Write mode command, translated mode
}
 
     |