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 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
|
/*
* Copyright (C) 2003, 2004 Stefan Reinauer
*
* See the file "COPYING" for further information about
* the copyright and warranty status of this work.
*/
#include "config.h"
#include "libopenbios/bindings.h"
#include "kernel/kernel.h"
#include "drivers/drivers.h"
#include "libc/vsprintf.h"
/* ******************************************************************
* simple polling video/keyboard console functions
* ****************************************************************** */
#define SER_SIZE 8
/*
* 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;
}
static void
pc_kbd_reset(void)
{
/* Reset first port */
outb(0xae, 0x64);
while (inb(0x64) & 2);
/* Write mode command, translated mode */
pc_kbd_controller_cmd(0x60, 0x40);
/* Reset keyboard device */
outb(0xff, 0x60);
while (inb(0x64) & 2);
inb(0x60); /* Should be 0xfa */
while (inb(0x64) & 2);
inb(0x60); /* Should be 0xaa */
}
/* ( 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;
fword("my-self");
fword("ihandle>phandle");
ph = (phandle_t)POP();
prop = (unsigned long *)get_property(ph, "address", &len);
*address = *prop;
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 *kdev_name, const char *mdev_name,
uint64_t base, uint64_t offset, int kintr, int mintr)
{
phandle_t chosen, aliases;
char nodebuff[128];
push_str(path);
fword("find-device");
fword("new-device");
push_str("8042");
fword("device-type");
push_str("8042");
fword("device-name");
PUSH((base + offset) >> 32);
fword("encode-int");
PUSH((base + offset) & 0xffffffff);
fword("encode-int");
fword("encode+");
PUSH(SER_SIZE);
fword("encode-int");
fword("encode+");
if (mdev_name != NULL) {
PUSH((base + offset) >> 32);
fword("encode-int");
fword("encode+");
PUSH((base + offset) & 0xffffffff);
fword("encode-int");
fword("encode+");
PUSH(SER_SIZE);
fword("encode-int");
fword("encode+");
}
push_str("reg");
fword("property");
chosen = get_cur_dev();
set_int_property(chosen, "#address-cells", 1);
set_int_property(chosen, "#size-cells", 0);
PUSH(kintr);
fword("encode-int");
if (mdev_name != NULL) {
PUSH(mintr);
fword("encode-int");
fword("encode+");
}
push_str("interrupts");
fword("property");
fword("finish-device");
/* Keyboard */
snprintf(nodebuff, sizeof(nodebuff), "%s/8042/%s", path, kdev_name);
REGISTER_NAMED_NODE(pc_kbd, nodebuff);
push_str(nodebuff);
fword("find-device");
push_str(kdev_name);
fword("device-name");
push_str("serial");
fword("device-type");
PUSH(0);
fword("encode-int");
push_str("reg");
fword("property");
PUSH(-1);
fword("encode-int");
push_str("keyboard");
fword("property");
PUSH(offset);
fword("encode-int");
push_str("address");
fword("property");
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_reset();
/* Mouse (optional) */
if (mdev_name != NULL) {
snprintf(nodebuff, sizeof(nodebuff), "%s/8042", path);
push_str(nodebuff);
fword("find-device");
fword("new-device");
push_str(mdev_name);
fword("device-name");
push_str("mouse");
fword("device-type");
PUSH(1);
fword("encode-int");
push_str("reg");
fword("property");
PUSH(-1);
fword("encode-int");
push_str("mouse");
fword("property");
PUSH(offset);
fword("encode-int");
push_str("address");
fword("property");
fword("finish-device");
}
}
|