| 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 |
#include <libpayload-config.h> |
| 30 |
#include <libpayload.h> |
| 31 |
#include <stdbool.h> |
| 32 |
#include <stddef.h> |
| 33 |
|
| 34 |
#include "i8042.h" |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
struct fifo { |
| 39 |
u8 *buf; |
| 40 |
size_t tx; |
| 41 |
size_t rx; |
| 42 |
size_t len; |
| 43 |
}; |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
static struct fifo *fifo_init(size_t len) |
| 51 |
{ |
| 52 |
struct fifo *ret; |
| 53 |
|
| 54 |
ret = malloc(sizeof(*ret)); |
| 55 |
if (!ret) |
| 56 |
return NULL; |
| 57 |
|
| 58 |
memset(ret, 0, sizeof(*ret)); |
| 59 |
|
| 60 |
ret->buf = malloc(len); |
| 61 |
if (!ret->buf) { |
| 62 |
free(ret); |
| 63 |
return NULL; |
| 64 |
} |
| 65 |
|
| 66 |
ret->len = len; |
| 67 |
|
| 68 |
return ret; |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
static void fifo_push(struct fifo *fifo, u8 c) |
| 78 |
{ |
| 79 |
fifo->buf[fifo->tx++] = c; |
| 80 |
fifo->tx = fifo->tx % fifo->len; |
| 81 |
if (fifo->tx == fifo->rx) |
| 82 |
fifo->rx++; |
| 83 |
fifo->rx = fifo->rx % fifo->len; |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
static int fifo_is_empty(struct fifo *fifo) |
| 91 |
{ |
| 92 |
if (!fifo) |
| 93 |
return 1; |
| 94 |
return fifo->tx == fifo->rx; |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
static u8 fifo_pop(struct fifo *fifo) |
| 103 |
{ |
| 104 |
u8 ret; |
| 105 |
|
| 106 |
if (fifo_is_empty(fifo)) |
| 107 |
return 0; |
| 108 |
|
| 109 |
ret = fifo->buf[fifo->rx++]; |
| 110 |
fifo->rx = fifo->rx % fifo->len; |
| 111 |
|
| 112 |
return ret; |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
static u8 fifo_peek(struct fifo *fifo) |
| 121 |
{ |
| 122 |
if (fifo_is_empty(fifo)) |
| 123 |
return 0; |
| 124 |
|
| 125 |
return fifo->buf[fifo->rx]; |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
static void fifo_destroy(struct fifo *fifo) |
| 132 |
{ |
| 133 |
if (fifo && fifo->buf) |
| 134 |
free(fifo->buf); |
| 135 |
if (fifo) |
| 136 |
free(fifo); |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
static inline u8 read_status(void) { return inb(0x64); } |
| 142 |
static inline u8 read_data(void) { return inb(0x60); } |
| 143 |
static inline void write_cmd(u8 cmd) { outb(cmd, 0x64); } |
| 144 |
static inline void write_data(u8 data) { outb(data, 0x60); } |
| 145 |
|
| 146 |
#define OBF 1 |
| 147 |
#define IBF 2 |
| 148 |
|
| 149 |
|
| 150 |
static int initialized = 0; |
| 151 |
static int kbc_init = 0; |
| 152 |
static struct fifo *aux_fifo = NULL; |
| 153 |
static struct fifo *ps2_fifo = NULL; |
| 154 |
|
| 155 |
static int i8042_cmd_with_response(u8 cmd); |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
static u8 i8042_wait_cmd_rdy(void) |
| 162 |
{ |
| 163 |
int retries = 10000; |
| 164 |
while (retries-- && (read_status() & IBF)) |
| 165 |
udelay(50); |
| 166 |
|
| 167 |
return retries > 0; |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
static u8 i8042_wait_data_rdy(void) |
| 175 |
{ |
| 176 |
int retries = 30000; |
| 177 |
while (retries-- && !(read_status() & OBF)) |
| 178 |
udelay(50); |
| 179 |
|
| 180 |
return retries > 0; |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
size_t i8042_has_ps2(void) |
| 187 |
{ |
| 188 |
return !!ps2_fifo; |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
size_t i8042_has_aux(void) |
| 195 |
{ |
| 196 |
return !!aux_fifo; |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
u8 i8042_probe(void) |
| 204 |
{ |
| 205 |
if (initialized) |
| 206 |
return 1; |
| 207 |
|
| 208 |
aux_fifo = NULL; |
| 209 |
ps2_fifo = NULL; |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
if (read_status() == 0xFF) { |
| 214 |
printf("ERROR: No keyboard controller found!\n"); |
| 215 |
return 0; |
| 216 |
} |
| 217 |
|
| 218 |
if (!i8042_wait_cmd_rdy()) { |
| 219 |
printf("ERROR: i8042_wait_cmd_rdy failed!\n"); |
| 220 |
return 0; |
| 221 |
} |
| 222 |
|
| 223 |
kbc_init = 1; |
| 224 |
|
| 225 |
|
| 226 |
if (i8042_cmd(I8042_CMD_DIS_KB) != 0) { |
| 227 |
kbc_init = 0; |
| 228 |
printf("ERROR: i8042_cmd I8042_CMD_DIS_KB failed!\n"); |
| 229 |
return 0; |
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
if (i8042_cmd(I8042_CMD_DIS_AUX) != 0) { |
| 234 |
kbc_init = 0; |
| 235 |
printf("ERROR: i8042_cmd I8042_CMD_DIS_AUX failed!\n"); |
| 236 |
return 0; |
| 237 |
} |
| 238 |
|
| 239 |
|
| 240 |
while (read_status() & OBF) |
| 241 |
read_data(); |
| 242 |
|
| 243 |
|
| 244 |
if (i8042_cmd_with_response(I8042_CMD_SELF_TEST) |
| 245 |
!= I8042_SELF_TEST_RSP) { |
| 246 |
kbc_init = 0; |
| 247 |
printf("ERROR: i8042_cmd I8042_CMD_SELF_TEST failed!\n"); |
| 248 |
return 0; |
| 249 |
} |
| 250 |
|
| 251 |
|
| 252 |
if (CONFIG(LP_PC_MOUSE)) { |
| 253 |
if (i8042_cmd_with_response(I8042_CMD_AUX_TEST) == 0) |
| 254 |
aux_fifo = fifo_init(4 * 32); |
| 255 |
} |
| 256 |
|
| 257 |
|
| 258 |
if (i8042_cmd_with_response(I8042_CMD_KB_TEST) == 0) |
| 259 |
ps2_fifo = fifo_init(2 * 16); |
| 260 |
|
| 261 |
kbc_init = 0; |
| 262 |
|
| 263 |
initialized = aux_fifo || ps2_fifo; |
| 264 |
|
| 265 |
return initialized; |
| 266 |
} |
| 267 |
|
| 268 |
|
| 269 |
void i8042_close(void) |
| 270 |
{ |
| 271 |
if (!initialized) |
| 272 |
return; |
| 273 |
|
| 274 |
fifo_destroy(aux_fifo); |
| 275 |
fifo_destroy(ps2_fifo); |
| 276 |
|
| 277 |
initialized = 0; |
| 278 |
aux_fifo = NULL; |
| 279 |
ps2_fifo = NULL; |
| 280 |
} |
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
int i8042_cmd(u8 cmd) |
| 287 |
{ |
| 288 |
if (!initialized && !kbc_init) |
| 289 |
return -1; |
| 290 |
|
| 291 |
if (!i8042_wait_cmd_rdy()) |
| 292 |
return -1; |
| 293 |
|
| 294 |
write_cmd(cmd); |
| 295 |
|
| 296 |
if (!i8042_wait_cmd_rdy()) |
| 297 |
return -1; |
| 298 |
|
| 299 |
return 0; |
| 300 |
} |
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
static int i8042_cmd_with_response(u8 cmd) |
| 307 |
{ |
| 308 |
const int ret = i8042_cmd(cmd); |
| 309 |
if (ret != 0) |
| 310 |
return ret; |
| 311 |
|
| 312 |
if (!i8042_wait_data_rdy()) |
| 313 |
return -1; |
| 314 |
|
| 315 |
return read_data(); |
| 316 |
} |
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
void i8042_write_data(u8 data) |
| 322 |
{ |
| 323 |
if (!initialized) |
| 324 |
return; |
| 325 |
|
| 326 |
if (!i8042_wait_cmd_rdy()) |
| 327 |
return; |
| 328 |
|
| 329 |
write_data(data); |
| 330 |
|
| 331 |
if (!i8042_wait_cmd_rdy()) |
| 332 |
return; |
| 333 |
} |
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
static int i8042_cmd_with_data(const u8 cmd, const u8 data) |
| 343 |
{ |
| 344 |
const int ret = i8042_cmd(cmd); |
| 345 |
if (ret != 0) |
| 346 |
return ret; |
| 347 |
|
| 348 |
i8042_write_data(data); |
| 349 |
|
| 350 |
return ret; |
| 351 |
} |
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
static void i8042_data_poll(void) |
| 357 |
{ |
| 358 |
u8 c; |
| 359 |
|
| 360 |
if (!initialized) |
| 361 |
return; |
| 362 |
|
| 363 |
c = read_status(); |
| 364 |
while ((c != 0xFF) && (c & OBF)) { |
| 365 |
const u8 in = read_data(); |
| 366 |
|
| 367 |
struct fifo *const fifo = (c & 0x20) ? aux_fifo : ps2_fifo; |
| 368 |
if (fifo) |
| 369 |
fifo_push(fifo, in); |
| 370 |
|
| 371 |
c = read_status(); |
| 372 |
} |
| 373 |
} |
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
u8 i8042_data_ready_ps2(void) |
| 379 |
{ |
| 380 |
if (!initialized) |
| 381 |
return 0; |
| 382 |
i8042_data_poll(); |
| 383 |
return !fifo_is_empty(ps2_fifo); |
| 384 |
} |
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
u8 i8042_data_ready_aux(void) |
| 390 |
{ |
| 391 |
if (!initialized) |
| 392 |
return 0; |
| 393 |
i8042_data_poll(); |
| 394 |
return !fifo_is_empty(aux_fifo); |
| 395 |
} |
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
u8 i8042_read_data_ps2(void) |
| 401 |
{ |
| 402 |
i8042_data_poll(); |
| 403 |
return fifo_pop(ps2_fifo); |
| 404 |
} |
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
u8 i8042_peek_data_ps2(void) |
| 410 |
{ |
| 411 |
return fifo_peek(ps2_fifo); |
| 412 |
} |
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
u8 i8042_read_data_aux(void) |
| 418 |
{ |
| 419 |
i8042_data_poll(); |
| 420 |
return fifo_pop(aux_fifo); |
| 421 |
} |
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
int i8042_wait_read_ps2(void) |
| 429 |
{ |
| 430 |
int retries = 10000; |
| 431 |
|
| 432 |
while (retries-- && !i8042_data_ready_ps2()) |
| 433 |
udelay(50); |
| 434 |
|
| 435 |
return (retries <= 0) ? -1 : i8042_read_data_ps2(); |
| 436 |
} |
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
int i8042_wait_read_aux(void) |
| 443 |
{ |
| 444 |
int retries = 10000; |
| 445 |
|
| 446 |
while (retries-- && !i8042_data_ready_aux()) |
| 447 |
udelay(50); |
| 448 |
|
| 449 |
return (retries <= 0) ? -1 : i8042_read_data_aux(); |
| 450 |
} |
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
int i8042_get_kbd_translation(void) |
| 459 |
{ |
| 460 |
const int cfg = i8042_cmd_with_response(I8042_CMD_RD_CMD_BYTE); |
| 461 |
if (cfg < 0) |
| 462 |
return cfg; |
| 463 |
|
| 464 |
return !!(cfg & I8042_CMD_BYTE_XLATE); |
| 465 |
} |
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
int i8042_set_kbd_translation(const bool xlate) |
| 473 |
{ |
| 474 |
int cfg = i8042_cmd_with_response(I8042_CMD_RD_CMD_BYTE); |
| 475 |
if (cfg < 0) |
| 476 |
return cfg; |
| 477 |
|
| 478 |
if (xlate) |
| 479 |
cfg |= I8042_CMD_BYTE_XLATE; |
| 480 |
else |
| 481 |
cfg &= ~I8042_CMD_BYTE_XLATE; |
| 482 |
return i8042_cmd_with_data(I8042_CMD_WR_CMD_BYTE, cfg); |
| 483 |
} |