⬢ DragonFlyBSD Kernel Audit
sys/dev/misc/kbd/atkbdc.c
← back
   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
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
/*
 * (MPSAFE)
 *
 * Copyright (c) 1996-1999
 * Kazutaka YOKOTA (yokota@zodiac.mech.utsunomiya-u.ac.jp)
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. The name of the author may not be used to endorse or promote
 *    products derived from this software without specific prior written
 *    permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * $FreeBSD: src/sys/dev/kbd/atkbdc.c,v 1.5.2.2 2002/03/31 11:02:02 murray Exp $
 * from kbdio.c,v 1.13 1998/09/25 11:55:46 yokota Exp
 */

#include "opt_kbd.h"

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/bus.h>
#include <sys/kbio.h>
#include <sys/kernel.h>
#include <sys/malloc.h>
#include <sys/rman.h>
#include <sys/syslog.h>

#include <machine/clock.h>

#include <bus/isa/isareg.h>

#include "atkbdcreg.h"
#include "kbdreg.h"
#include "use_atkbdc.h"

/* constants */

#define MAXKBDC MAX(NATKBDC, 1) /* XXX */

/* macros */

#ifndef MAX
#define MAX(x, y) ((x) > (y) ? (x) : (y))
#endif

#define nextq(i) (((i) + 1) % KBDQ_BUFSIZE)
#define availq(q) ((q)->head != (q)->tail)
#if KBDIO_DEBUG >= 2
#define emptyq(q) ((q)->tail = (q)->head = (q)->qcount = 0)
#else
#define emptyq(q) ((q)->tail = (q)->head = 0)
#endif

#define read_data(k) (bus_space_read_1((k)->iot, (k)->ioh0, 0))
#define read_status(k) (bus_space_read_1((k)->iot, (k)->ioh1, 0))
#define write_data(k, d) (bus_space_write_1((k)->iot, (k)->ioh0, 0, (d)))
#define write_command(k, d) (bus_space_write_1((k)->iot, (k)->ioh1, 0, (d)))

/* local variables */

/*
 * We always need at least one copy of the kbdc_softc struct for the
 * low-level console.  As the low-level console accesses the keyboard
 * controller before kbdc, and all other devices, is probed, we
 * statically allocate one entry. XXX
 */
static atkbdc_softc_t default_kbdc;
static atkbdc_softc_t *atkbdc_softc[MAXKBDC] = { &default_kbdc };

static int verbose = KBDIO_DEBUG;

/* function prototypes */

static int atkbdc_setup(atkbdc_softc_t *sc, bus_space_tag_t tag,
    bus_space_handle_t h0, bus_space_handle_t h1);
static int addq(kbdkqueue *q, int c);
static int removeq(kbdkqueue *q);
static int wait_while_controller_busy(atkbdc_softc_t *kbdc);
static int wait_for_data(atkbdc_softc_t *kbdc);
static int wait_for_kbd_data(atkbdc_softc_t *kbdc);
static int wait_for_kbd_ack(atkbdc_softc_t *kbdc);
static int wait_for_aux_data(atkbdc_softc_t *kbdc);
static int wait_for_aux_ack(atkbdc_softc_t *kbdc);

struct atkbdc_quirks {
	const char *bios_vendor;
	const char *maker;
	const char *product;
	const char *version;
	int quirk;
};

/* Old chromebooks running coreboot with i8042 emulation quirks */
#define CHROMEBOOK_WORKAROUND                                         \
	(KBDC_QUIRK_KEEP_ACTIVATED | KBDC_QUIRK_IGNORE_PROBE_RESULT | \
	    KBDC_QUIRK_RESET_AFTER_PROBE | KBDC_QUIRK_SETLEDS_ON_INIT)

static struct atkbdc_quirks quirks[] = {
	/*
	 * Older chromebooks running coreboot have an EC that imperfectly
	 * emulates i8042 w/o fixes to its firmware.  Since we can't probe for
	 * the problem, include all chromebooks by matching 'Google_' in the
	 * bios version string or a maker of either 'Google' or 'GOOGLE'. This
	 * is imperfect, but catches all chromebooks while omitting non-Google
	 * systems from System76 and Purism.
	 */
	{ "coreboot", NULL, NULL, "Google_", CHROMEBOOK_WORKAROUND },
	{ "coreboot", "GOOGLE", NULL, NULL, CHROMEBOOK_WORKAROUND },
	{ "coreboot", "Google", NULL, NULL, CHROMEBOOK_WORKAROUND },
	/* KBDC hangs on Lenovo X120e and X121e after disabling AUX MUX */
	{ NULL, "LENOVO", NULL, NULL, KBDC_QUIRK_DISABLE_MUX_PROBE },
};

#define QUIRK_STR_EQUAL(s1, s2) \
	(s1 == NULL || (s2 != NULL && strcmp(s1, s2) == 0))
#define QUIRK_STR_MATCH(s1, s2) \
	(s1 == NULL || (s2 != NULL && strncmp(s1, s2, strlen(s1)) == 0))

static int
atkbdc_getquirks(void)
{
	int i;
	char *bios_vendor = kgetenv("smbios.bios.vendor");
	char *maker = kgetenv("smbios.system.maker");
	char *product = kgetenv("smbios.system.product");
	char *version = kgetenv("smbios.bios.version");
	char *reldate = kgetenv("smbios.bios.reldate");

	for (i = 0; i < nitems(quirks); i++)
		if (QUIRK_STR_EQUAL(quirks[i].bios_vendor, bios_vendor) &&
		    QUIRK_STR_EQUAL(quirks[i].maker, maker) &&
		    QUIRK_STR_EQUAL(quirks[i].product, product) &&
		    QUIRK_STR_MATCH(quirks[i].version, version))
			return (quirks[i].quirk);
	/*
	 * Some Chromebooks don't conform to the google comment above so do the
	 * Chromebook workaround for all <= 2018 coreboot systems that have a
	 * 'blank' version.  At least one Acer "Peppy" chromebook has this
	 * issue, with a reldate of 08/13/2014.
	 */
	if (QUIRK_STR_EQUAL("coreboot", bios_vendor) &&
	    (version != NULL && *version == ' ') &&
	    (reldate != NULL && strlen(reldate) >= 10 &&
		strcmp(reldate + 6, "2018") <= 0))
		return (CHROMEBOOK_WORKAROUND);

	return (0);
}

atkbdc_softc_t *
atkbdc_get_softc(int unit)
{
	atkbdc_softc_t *sc;

	if (unit >= nitems(atkbdc_softc))
		return NULL;
	sc = atkbdc_softc[unit];
	if (sc == NULL) {
		sc = kmalloc(sizeof(*sc), M_DEVBUF, M_WAITOK | M_ZERO);
		atkbdc_softc[unit] = sc;
	}
	return sc;
}

int
atkbdc_probe_unit(int unit, struct resource *port0, struct resource *port1)
{
	if (rman_get_start(port0) <= 0)
		return ENXIO;
	if (rman_get_start(port1) <= 0)
		return ENXIO;
	return 0;
}

int
atkbdc_attach_unit(int unit, atkbdc_softc_t *sc, struct resource *port0,
    struct resource *port1)
{
	return atkbdc_setup(sc, rman_get_bustag(port0),
	    rman_get_bushandle(port0), rman_get_bushandle(port1));
}

extern int acpi_fadt_8042_nolegacy;
int kicked_by_syscons = 0;

static void
atkbdc_fadt_done(void)
{
	if (kicked_by_syscons) {
		/*
		 * Configuring all keyboards is fine, because only atkbd
		 * really does something here, anyway.
		 */
		kbd_configure(KB_CONF_PROBE_ONLY);
	}
}

/* After fadt_probe in platform/pc64/acpica/acpi_fadt.c. */
SYSINIT(atkbdc_kick, SI_BOOT2_PRESMP, SI_ORDER_THIRD, atkbdc_fadt_done, 0);

/* the backdoor to the keyboard controller! XXX */
int
atkbdc_configure(void)
{
	bus_space_tag_t tag;
	bus_space_handle_t h0;
	bus_space_handle_t h1;
	int port0;
	int port1;
#if defined(__x86_64__)
	int i;
#endif

	kicked_by_syscons = 1;
	if (acpi_fadt_8042_nolegacy != 0)
		return ENXIO;

	port0 = IO_KBD;
	resource_int_value("atkbdc", 0, "port", &port0);
	port1 = IO_KBD + KBD_STATUS_PORT;
#if 0
	resource_int_value("atkbdc", 0, "port", &port0);
#endif

	/* XXX: tag should be passed from the caller */
#if defined(__x86_64__)
	tag = X86_64_BUS_SPACE_IO;
#else
	tag = 0; /* XXX */
#endif

#if 0 /* notyet */
	bus_space_map(tag, port0, IO_KBDSIZE, 0, &h0);
	bus_space_map(tag, port1, IO_KBDSIZE, 0, &h1);
#else
	h0 = (bus_space_handle_t)port0;
	h1 = (bus_space_handle_t)port1;
#endif

#if defined(__x86_64__)
	/*
	 * Check if we really have AT keyboard controller. Poll status
	 * register until we get "all clear" indication. If no such
	 * indication comes, it probably means that there is no AT
	 * keyboard controller present. Give up in such case. Check relies
	 * on the fact that reading from non-existing in/out port returns
	 * 0xff on i386. May or may not be true on other platforms.
	 */
	for (i = 65536; i != 0; --i) {
		if ((bus_space_read_1(tag, h1, 0) & 0x2) == 0)
			break;
		DELAY(16);
	}
	if (i == 0)
		return ENXIO;
#endif

	return atkbdc_setup(atkbdc_softc[0], tag, h0, h1);
}

static int
atkbdc_setup(atkbdc_softc_t *sc, bus_space_tag_t tag, bus_space_handle_t h0,
    bus_space_handle_t h1)
{
	if (sc->ioh0 == 0) { /* XXX */
		sc->command_byte = -1;
		sc->command_mask = 0;
		sc->lock = FALSE;
		sc->kbd.head = sc->kbd.tail = 0;
		sc->aux.head = sc->aux.tail = 0;
		sc->aux_mux_enabled = FALSE;
#if KBDIO_DEBUG >= 2
		sc->kbd.call_count = 0;
		sc->kbd.qcount = sc->kbd.max_qcount = 0;
		sc->aux.call_count = 0;
		sc->aux.qcount = sc->aux.max_qcount = 0;
#endif
	}
	sc->iot = tag;
	sc->ioh0 = h0;
	sc->ioh1 = h1;
	sc->quirks = atkbdc_getquirks();

	return 0;
}

/* open a keyboard controller */
KBDC
atkbdc_open(int unit)
{
	if (unit <= 0)
		unit = 0;
	if (unit >= MAXKBDC)
		return NULL;
	if ((atkbdc_softc[unit]->port0 != NULL) ||
	    (atkbdc_softc[unit]->ioh0 != 0)) /* XXX */
		return (KBDC)atkbdc_softc[unit];
	return NULL;
}

/*
 * I/O access arbitration in `kbdio'
 *
 * The `kbdio' module uses a simplistic convention to arbitrate
 * I/O access to the controller/keyboard/mouse. The convention requires
 * close cooperation of the calling device driver.
 *
 * The device drivers which utilize the `kbdio' module are assumed to
 * have the following set of routines.
 *    a. An interrupt handler (the bottom half of the driver).
 *    b. Timeout routines which may briefly poll the keyboard controller.
 *    c. Routines outside interrupt context (the top half of the driver).
 * They should follow the rules below:
 *    1. The interrupt handler may assume that it always has full access
 *       to the controller/keyboard/mouse.
 *    2. The other routines must issue `spltty()' if they wish to
 *       prevent the interrupt handler from accessing
 *       the controller/keyboard/mouse.
 *    3. The timeout routines and the top half routines of the device driver
 *       arbitrate I/O access by observing the lock flag in `kbdio'.
 *       The flag is manipulated via `kbdc_lock()'; when one wants to
 *       perform I/O, call `kbdc_lock(kbdc, TRUE)' and proceed only if
 *       the call returns with TRUE. Otherwise the caller must back off.
 *       Call `kbdc_lock(kbdc, FALSE)' when necessary I/O operaion
 *       is finished. This mechanism does not prevent the interrupt
 *       handler from being invoked at any time and carrying out I/O.
 *       Therefore, `spltty()' must be strategically placed in the device
 *       driver code. Also note that the timeout routine may interrupt
 *       `kbdc_lock()' called by the top half of the driver, but this
 *       interruption is OK so long as the timeout routine observes
 *       rule 4 below.
 *    4. The interrupt and timeout routines should not extend I/O operation
 *       across more than one interrupt or timeout; they must complete any
 *       necessary I/O operation within one invocation of the routine.
 *       This means that if the timeout routine acquires the lock flag,
 *       it must reset the flag to FALSE before it returns.
 */

/* set/reset polling lock */
int
kbdc_lock(KBDC p, int lock)
{
	int prevlock;

	prevlock = p->lock;
	p->lock = lock;

	return (prevlock != lock);
}

/* check if any data is waiting to be processed */
int
kbdc_data_ready(KBDC p)
{
	return (availq(&p->kbd) || availq(&p->aux) ||
	    (read_status(p) & KBDS_ANY_BUFFER_FULL));
}

/* queuing functions */

static int
addq(kbdkqueue *q, int c)
{
	if (nextq(q->tail) != q->head) {
		q->q[q->tail] = c;
		q->tail = nextq(q->tail);
#if KBDIO_DEBUG >= 2
		++q->call_count;
		++q->qcount;
		if (q->qcount > q->max_qcount)
			q->max_qcount = q->qcount;
#endif
		return TRUE;
	}
	return FALSE;
}

static int
removeq(kbdkqueue *q)
{
	int c;

	if (q->tail != q->head) {
		c = q->q[q->head];
		q->head = nextq(q->head);
#if KBDIO_DEBUG >= 2
		--q->qcount;
#endif
		return c;
	}
	return -1;
}

/*
 * device I/O routines
 */
static int
wait_while_controller_busy(struct atkbdc_softc *kbdc)
{
	/* CPU will stay inside the loop for 100msec at most */
	TOTALDELAY retry = { .us = 70000, .last_clock = 0 }; /* 70ms */
	int f;
	unsigned char c;

	while ((f = read_status(kbdc)) & KBDS_INPUT_BUFFER_FULL) {
		if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL) {
			DELAY(KBDD_DELAYTIME);
			c = read_data(kbdc);
			addq(&kbdc->kbd, c);
		} else if ((f & KBDS_BUFFER_FULL) == KBDS_AUX_BUFFER_FULL) {
			DELAY(KBDD_DELAYTIME);
			c = read_data(kbdc);
			addq(&kbdc->aux, c);
		}
		DELAY(KBDC_DELAYTIME);
		if (CHECKTIMEOUT(&retry))
			return FALSE;
	}
	return TRUE;
}

/*
 * wait for any data; whether it's from the controller,
 * the keyboard, or the aux device.
 */
static int
wait_for_data(struct atkbdc_softc *kbdc)
{
	/* CPU will stay inside the loop for 200msec at most */
	TOTALDELAY retry = { .us = 200000, .last_clock = 0 }; /* 200ms */
	int f;

	while ((f = read_status(kbdc) & KBDS_ANY_BUFFER_FULL) == 0) {
		DELAY(KBDC_DELAYTIME);
		if (CHECKTIMEOUT(&retry))
			return 0;
	}
	DELAY(KBDD_DELAYTIME);
	return f;
}

/* wait for data from the keyboard */
static int
wait_for_kbd_data(struct atkbdc_softc *kbdc)
{
	/* CPU will stay inside the loop for 200msec at most */
	TOTALDELAY retry = { .us = 200000, .last_clock = 0 }; /* 200ms */
	int f;
	unsigned char c;

	while ((f = read_status(kbdc) & KBDS_BUFFER_FULL) !=
	    KBDS_KBD_BUFFER_FULL) {
		if (f == KBDS_AUX_BUFFER_FULL) {
			DELAY(KBDD_DELAYTIME);
			c = read_data(kbdc);
			addq(&kbdc->aux, c);
		}
		DELAY(KBDC_DELAYTIME);
		if (CHECKTIMEOUT(&retry))
			return 0;
	}
	DELAY(KBDD_DELAYTIME);
	return f;
}

/*
 * wait for an ACK(FAh), RESEND(FEh), or RESET_FAIL(FCh) from the keyboard.
 * queue anything else.
 */
static int
wait_for_kbd_ack(struct atkbdc_softc *kbdc)
{
	/* CPU will stay inside the loop for 200msec at most */
	TOTALDELAY retry = { .us = 200000, .last_clock = 0 }; /* 200ms */
	int f;
	int b;

	while (CHECKTIMEOUT(&retry) == 0) {
		if ((f = read_status(kbdc)) & KBDS_ANY_BUFFER_FULL) {
			DELAY(KBDD_DELAYTIME);
			b = read_data(kbdc);
			if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL) {
				if ((b == KBD_ACK) || (b == KBD_RESEND) ||
				    (b == KBD_RESET_FAIL))
					return b;
				addq(&kbdc->kbd, b);
			} else if ((f & KBDS_BUFFER_FULL) ==
			    KBDS_AUX_BUFFER_FULL) {
				addq(&kbdc->aux, b);
			}
		}
		DELAY(KBDC_DELAYTIME);
	}
	return -1;
}

/* wait for data from the aux device */
static int
wait_for_aux_data(struct atkbdc_softc *kbdc)
{
	/* CPU will stay inside the loop for 200msec at most */
	TOTALDELAY retry = { .us = 200000, .last_clock = 0 }; /* 200ms */
	int f;
	unsigned char b;

	while ((f = read_status(kbdc) & KBDS_BUFFER_FULL) !=
	    KBDS_AUX_BUFFER_FULL) {
		if (f == KBDS_KBD_BUFFER_FULL) {
			DELAY(KBDD_DELAYTIME);
			b = read_data(kbdc);
			addq(&kbdc->kbd, b);
		}
		DELAY(KBDC_DELAYTIME);
		if (CHECKTIMEOUT(&retry))
			return 0;
	}
	DELAY(KBDD_DELAYTIME);
	return f;
}

/*
 * wait for an ACK(FAh), RESEND(FEh), or RESET_FAIL(FCh) from the aux device.
 * queue anything else.
 */
static int
wait_for_aux_ack(struct atkbdc_softc *kbdc)
{
	/* CPU will stay inside the loop for 200msec at most */
	TOTALDELAY retry = { .us = 200000, .last_clock = 0 }; /* 200ms */
	int f;
	int b;

	while (CHECKTIMEOUT(&retry) == 0) {
		if ((f = read_status(kbdc)) & KBDS_ANY_BUFFER_FULL) {
			DELAY(KBDD_DELAYTIME);
			b = read_data(kbdc);
			if ((f & KBDS_BUFFER_FULL) == KBDS_AUX_BUFFER_FULL) {
				if ((b == PSM_ACK) || (b == PSM_RESEND) ||
				    (b == PSM_RESET_FAIL))
					return b;
				addq(&kbdc->aux, b);
			} else if ((f & KBDS_BUFFER_FULL) ==
			    KBDS_KBD_BUFFER_FULL) {
				addq(&kbdc->kbd, b);
			}
		}
		DELAY(KBDC_DELAYTIME);
	}
	return -1;
}

/*
 * Returns read-back data or -1 on failure
 */
int
write_controller_w1r1(KBDC p, int c, int d)
{
	if (!write_controller_command(p, c))
		return (-1);
	if (!write_controller_data(p, d))
		return (-1);
	return (read_controller_data(p));
}

/* write a one byte command to the controller */
int
write_controller_command(KBDC p, int c)
{
	if (!wait_while_controller_busy(p))
		return FALSE;
	write_command(p, c);
	return TRUE;
}

/* write a one byte data to the controller */
int
write_controller_data(KBDC p, int c)
{
	if (!wait_while_controller_busy(p))
		return FALSE;
	write_data(p, c);
	return TRUE;
}

/* write a one byte keyboard command */
int
write_kbd_command(KBDC p, int c)
{
	if (!wait_while_controller_busy(p))
		return FALSE;
	write_data(p, c);
	return TRUE;
}

/* write a one byte auxiliary device command */
int
write_aux_command(KBDC p, int c)
{
	int f;

	f = aux_mux_is_enabled(p) ? KBDC_WRITE_TO_AUX_MUX + p->aux_mux_port :
				    KBDC_WRITE_TO_AUX;

	if (!write_controller_command(p, f))
		return FALSE;
	return write_controller_data(p, c);
}

/* send a command to the keyboard and wait for ACK */
int
send_kbd_command(KBDC p, int c)
{
	int retry = KBD_MAXRETRY;
	int res = -1;

	while (retry-- > 0) {
		if (!write_kbd_command(p, c))
			continue;
		res = wait_for_kbd_ack(p);
		if (res == KBD_ACK)
			break;
	}
	return res;
}

/* send a command to the auxiliary device and wait for ACK */
int
send_aux_command(KBDC p, int c)
{
	int retry = KBD_MAXRETRY;
	int res = -1;

	while (retry-- > 0) {
		if (!write_aux_command(p, c))
			continue;
		/*
		 * FIXME: XXX
		 * The aux device may have already sent one or two bytes of
		 * status data, when a command is received. It will immediately
		 * stop data transmission, thus, leaving an incomplete data
		 * packet in our buffer. We have to discard any unprocessed
		 * data in order to remove such packets. Well, we may remove
		 * unprocessed, but necessary data byte as well...
		 */
		emptyq(&p->aux);
		res = wait_for_aux_ack(p);
		if (res == PSM_ACK)
			break;
	}
	return res;
}

/* send a command and a data to the keyboard, wait for ACKs */
int
send_kbd_command_and_data(KBDC p, int c, int d)
{
	int retry;
	int res = -1;

	for (retry = KBD_MAXRETRY; retry > 0; --retry) {
		if (!write_kbd_command(p, c))
			continue;
		res = wait_for_kbd_ack(p);
		if (res == KBD_ACK)
			break;
		else if (res != KBD_RESEND)
			return res;
	}
	if (retry <= 0)
		return res;

	for (retry = KBD_MAXRETRY, res = -1; retry > 0; --retry) {
		if (!write_kbd_command(p, d))
			continue;
		res = wait_for_kbd_ack(p);
		if (res != KBD_RESEND)
			break;
	}
	return res;
}

/* send a command and a data to the auxiliary device, wait for ACKs */
int
send_aux_command_and_data(KBDC p, int c, int d)
{
	int retry;
	int res = -1;

	for (retry = KBD_MAXRETRY; retry > 0; --retry) {
		if (!write_aux_command(p, c))
			continue;
		emptyq(&p->aux);
		res = wait_for_aux_ack(p);
		if (res == PSM_ACK)
			break;
		else if (res != PSM_RESEND)
			return res;
	}
	if (retry <= 0)
		return res;

	for (retry = KBD_MAXRETRY, res = -1; retry > 0; --retry) {
		if (!write_aux_command(p, d))
			continue;
		res = wait_for_aux_ack(p);
		if (res != PSM_RESEND)
			break;
	}
	return res;
}

/*
 * read one byte from any source; whether from the controller,
 * the keyboard, or the aux device
 */
int
read_controller_data(KBDC p)
{
	if (availq(&p->kbd))
		return removeq(&p->kbd);
	if (availq(&p->aux))
		return removeq(&p->aux);
	if (!wait_for_data(p))
		return -1; /* timeout */
	return read_data(p);
}

#if KBDIO_DEBUG >= 2
static int call = 0;
#endif

/* read one byte from the keyboard */
int
read_kbd_data(KBDC p)
{
	unsigned char b;

#if KBDIO_DEBUG >= 2
	if (++call > 2000) {
		call = 0;
		log(LOG_DEBUG,
		    "kbdc: kbd q: %d calls, max %d chars, "
		    "aux q: %d calls, max %d chars\n",
		    p->kbd.call_count, p->kbd.max_qcount, p->aux.call_count,
		    p->aux.max_qcount);
	}
#endif

	if (availq(&p->kbd))
		return removeq(&p->kbd);
	if (!wait_for_kbd_data(p))
		return -1; /* timeout */
	b = read_data(p);
	return b;
}

/* read one byte from the keyboard, but return immediately if
 * no data is waiting
 */
int
read_kbd_data_no_wait(KBDC p)
{
	int f;
	unsigned char b;

#if KBDIO_DEBUG >= 2
	if (++call > 2000) {
		call = 0;
		log(LOG_DEBUG,
		    "kbdc: kbd q: %d calls, max %d chars, "
		    "aux q: %d calls, max %d chars\n",
		    p->kbd.call_count, p->kbd.max_qcount, p->aux.call_count,
		    p->aux.max_qcount);
	}
#endif

	if (availq(&p->kbd))
		return removeq(&p->kbd);
	f = read_status(p) & KBDS_BUFFER_FULL;
	while (f == KBDS_AUX_BUFFER_FULL) {
		DELAY(KBDD_DELAYTIME);
		b = read_data(p);
		addq(&p->aux, b);
		f = read_status(p) & KBDS_BUFFER_FULL;
	}
	if (f == KBDS_KBD_BUFFER_FULL) {
		DELAY(KBDD_DELAYTIME);
		b = read_data(p);
		return (int)b;
	}
	return -1; /* no data */
}

/* read one byte from the aux device */
int
read_aux_data(KBDC p)
{
	unsigned char b;
	if (availq(&p->aux))
		return removeq(&p->aux);
	if (!wait_for_aux_data(p))
		return -1; /* timeout */
	b = read_data(p);
	return b;
}

/* read one byte from the aux device, but return immediately if
 * no data is waiting
 */
int
read_aux_data_no_wait(KBDC p)
{
	unsigned char b;
	int f;

	if (availq(&p->aux))
		return removeq(&p->aux);
	f = read_status(p) & KBDS_BUFFER_FULL;
	while (f == KBDS_KBD_BUFFER_FULL) {
		DELAY(KBDD_DELAYTIME);
		b = read_data(p);
		addq(&p->kbd, b);
		f = read_status(p) & KBDS_BUFFER_FULL;
	}
	if (f == KBDS_AUX_BUFFER_FULL) {
		DELAY(KBDD_DELAYTIME);
		b = read_data(p);
		return b;
	}
	return -1; /* no data */
}

/* discard data from the keyboard */
void
empty_kbd_buffer(KBDC p, int wait)
{
	int t;
	int b;
	int f;
#if KBDIO_DEBUG >= 2
	int c1 = 0;
	int c2 = 0;
#endif
	int delta = 2;

	for (t = wait; t > 0;) {
		if ((f = read_status(p)) & KBDS_ANY_BUFFER_FULL) {
			DELAY(KBDD_DELAYTIME);
			b = read_data(p);
			if ((f & KBDS_BUFFER_FULL) == KBDS_AUX_BUFFER_FULL) {
				addq(&p->aux, b);
#if KBDIO_DEBUG >= 2
				++c2;
			} else {
				++c1;
#endif
			}
			t = wait;
		} else {
			t -= delta;
		}
		DELAY(delta * 1000);
	}
#if KBDIO_DEBUG >= 2
	if ((c1 > 0) || (c2 > 0))
		log(LOG_DEBUG, "kbdc: %d:%d char read (empty_kbd_buffer)\n", c1,
		    c2);
#endif

	emptyq(&p->kbd);
}

/* discard data from the aux device */
void
empty_aux_buffer(KBDC p, int wait)
{
	int t;
	int b;
	int f;
#if KBDIO_DEBUG >= 2
	int c1 = 0;
	int c2 = 0;
#endif
	int delta = 2;

	for (t = wait; t > 0;) {
		if ((f = read_status(p)) & KBDS_ANY_BUFFER_FULL) {
			DELAY(KBDD_DELAYTIME);
			b = read_data(p);
			if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL) {
				addq(&p->kbd, b);
#if KBDIO_DEBUG >= 2
				++c1;
			} else {
				++c2;
#endif
			}
			t = wait;
		} else {
			t -= delta;
		}
		DELAY(delta * 1000);
	}
#if KBDIO_DEBUG >= 2
	if ((c1 > 0) || (c2 > 0))
		log(LOG_DEBUG, "kbdc: %d:%d char read (empty_aux_buffer)\n", c1,
		    c2);
#endif

	emptyq(&p->aux);
}

/* discard any data from the keyboard or the aux device */
void
empty_both_buffers(KBDC p, int wait)
{
	int t;
	int f;
	int waited = 0;
#if KBDIO_DEBUG >= 2
	int c1 = 0;
	int c2 = 0;
#endif
	int delta = 2;

	for (t = wait; t > 0;) {
		if ((f = read_status(p)) & KBDS_ANY_BUFFER_FULL) {
			DELAY(KBDD_DELAYTIME);
			(void)read_data(p);
#if KBDIO_DEBUG >= 2
			if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL)
				++c1;
			else
				++c2;
#endif
			t = wait;
		} else {
			t -= delta;
		}
		/*
		* Some systems (Intel/IBM blades) do not have keyboard devices and
		* will thus hang in this procedure. Time out after delta seconds to
		* avoid this hang -- the keyboard attach will fail later on.
		*/
		waited += (delta * 1000);
		if (waited == (delta * 1000000))
			return;

		DELAY(delta * 1000);
	}
#if KBDIO_DEBUG >= 2
	if ((c1 > 0) || (c2 > 0))
		log(LOG_DEBUG, "kbdc: %d:%d char read (empty_both_buffers)\n",
		    c1, c2);
#endif

	emptyq(&p->kbd);
	emptyq(&p->aux);
}

/* keyboard and mouse device control */

/* NOTE: enable the keyboard port but disable the keyboard
 * interrupt before calling "reset_kbd()".
 */
int
reset_kbd(KBDC p)
{
	int retry = KBD_MAXRETRY;
	int again = KBD_MAXWAIT;
	int c = KBD_RESEND; /* keep the compiler happy */

	while (retry-- > 0) {
		empty_both_buffers(p, 10);
		if (!write_kbd_command(p, KBDC_RESET_KBD))
			continue;
		emptyq(&p->kbd);
		c = read_controller_data(p);
		if (verbose || bootverbose)
			log(LOG_DEBUG, "kbdc: RESET_KBD return code:%04x\n", c);
		if (c == KBD_ACK) /* keyboard has agreed to reset itself... */
			break;
	}
	if (retry < 0)
		return FALSE;

	while (again-- > 0) {
		/* wait awhile, well, in fact we must wait quite loooooooooooong
		 */
		DELAY(KBD_RESETDELAY * 1000);
		c = read_controller_data(p); /* RESET_DONE/RESET_FAIL */
		if (c != -1) /* wait again if the controller is not ready */
			break;
	}
	if (verbose || bootverbose)
		log(LOG_DEBUG, "kbdc: RESET_KBD status:%04x\n", c);
	if (c != KBD_RESET_DONE)
		return FALSE;
	return TRUE;
}

/* NOTE: enable the aux port but disable the aux interrupt
 * before calling `reset_aux_dev()'.
 */
int
reset_aux_dev(KBDC p)
{
	int retry = KBD_MAXRETRY;
	int again = KBD_MAXWAIT;
	int c = PSM_RESEND; /* keep the compiler happy */

	while (retry-- > 0) {
		empty_both_buffers(p, 10);
		if (!write_aux_command(p, PSMC_RESET_DEV))
			continue;
		emptyq(&p->aux);
		/* NOTE: Compaq Armada laptops require extra delay here. XXX */
		for (again = KBD_MAXWAIT; again > 0; --again) {
			DELAY(KBD_RESETDELAY * 1000);
			c = read_aux_data_no_wait(p);
			if (c != -1)
				break;
		}
		if (verbose || bootverbose)
			log(LOG_DEBUG, "kbdc: RESET_AUX return code:%04x\n", c);
		if (c == PSM_ACK) /* aux dev is about to reset... */
			break;
	}
	if (retry < 0)
		return FALSE;

	for (again = KBD_MAXWAIT; again > 0; --again) {
		/* wait awhile, well, quite looooooooooooong */
		DELAY(KBD_RESETDELAY * 1000);
		c = read_aux_data_no_wait(p); /* RESET_DONE/RESET_FAIL */
		if (c != -1) /* wait again if the controller is not ready */
			break;
	}
	if (verbose || bootverbose)
		log(LOG_DEBUG, "kbdc: RESET_AUX status:%04x\n", c);
	if (c != PSM_RESET_DONE) /* reset status */
		return FALSE;

	c = read_aux_data(p); /* device ID */
	if (verbose || bootverbose)
		log(LOG_DEBUG, "kbdc: RESET_AUX ID:%04x\n", c);
	/* NOTE: we could check the device ID now, but leave it later... */
	return TRUE;
}

/* controller diagnostics and setup */

int
test_controller(KBDC p)
{
	int retry = KBD_MAXRETRY;
	int again = KBD_MAXWAIT;
	int c = KBD_DIAG_FAIL;

	while (retry-- > 0) {
		empty_both_buffers(p, 10);
		if (write_controller_command(p, KBDC_DIAGNOSE))
			break;
	}
	if (retry < 0)
		return FALSE;

	emptyq(&p->kbd);
	while (again-- > 0) {
		/* wait awhile */
		DELAY(KBD_RESETDELAY * 1000);
		c = read_controller_data(p); /* DIAG_DONE/DIAG_FAIL */
		if (c != -1) /* wait again if the controller is not ready */
			break;
	}
	if (verbose || bootverbose)
		log(LOG_DEBUG, "kbdc: DIAGNOSE status:%04x\n", c);
	return (c == KBD_DIAG_DONE);
}

int
test_kbd_port(KBDC p)
{
	int retry = KBD_MAXRETRY;
	int again = KBD_MAXWAIT;
	int c = -1;

	while (retry-- > 0) {
		empty_both_buffers(p, 10);
		if (write_controller_command(p, KBDC_TEST_KBD_PORT))
			break;
	}
	if (retry < 0)
		return FALSE;

	emptyq(&p->kbd);
	while (again-- > 0) {
		c = read_controller_data(p);
		if (c != -1) /* try again if the controller is not ready */
			break;
	}
	if (verbose || bootverbose)
		log(LOG_DEBUG, "kbdc: TEST_KBD_PORT status:%04x\n", c);
	return c;
}

int
test_aux_port(KBDC p)
{
	int retry = KBD_MAXRETRY;
	int again = KBD_MAXWAIT;
	int c = -1;

	while (retry-- > 0) {
		empty_both_buffers(p, 10);
		if (write_controller_command(p, KBDC_TEST_AUX_PORT))
			break;
	}
	if (retry < 0)
		return FALSE;

	emptyq(&p->kbd);
	while (again-- > 0) {
		c = read_controller_data(p);
		if (c != -1) /* try again if the controller is not ready */
			break;
	}
	if (verbose || bootverbose)
		log(LOG_DEBUG, "kbdc: TEST_AUX_PORT status:%04x\n", c);
	return c;
}

int
kbdc_get_device_mask(KBDC p)
{
	return p->command_mask;
}

void
kbdc_set_device_mask(KBDC p, int mask)
{
	p->command_mask = mask &
	    (((p->quirks & KBDC_QUIRK_KEEP_ACTIVATED) ? 0 :
							KBD_KBD_CONTROL_BITS) |
		KBD_AUX_CONTROL_BITS);
}

int
get_controller_command_byte(KBDC p)
{
	if (p->command_byte != -1)
		return p->command_byte;
	if (!write_controller_command(p, KBDC_GET_COMMAND_BYTE))
		return -1;
	emptyq(&p->kbd);
	p->command_byte = read_controller_data(p);
	return p->command_byte;
}

int
set_controller_command_byte(KBDC p, int mask, int command)
{
	if (get_controller_command_byte(p) == -1)
		return FALSE;

	command = (p->command_byte & ~mask) | (command & mask);
	if (command & KBD_DISABLE_KBD_PORT) {
		if (!write_controller_command(p, KBDC_DISABLE_KBD_PORT))
			return FALSE;
	}
	if (!write_controller_command(p, KBDC_SET_COMMAND_BYTE))
		return FALSE;
	if (!write_controller_data(p, command))
		return FALSE;
	p->command_byte = command;

	if (verbose)
		log(LOG_DEBUG,
		    "kbdc: new command byte:%04x (set_controller...)\n",
		    command);

	return TRUE;
}

/*
 * Rudimentary support for active PS/2 AUX port multiplexing.
 * Only write commands can be routed to a selected AUX port.
 * Source port of data processed by read commands is totally ignored.
 */
static int
set_aux_mux_state(KBDC p, int enabled)
{
	int command, version;

	if (write_controller_command(p, KBDC_FORCE_AUX_OUTPUT) == 0 ||
	    write_controller_data(p, 0xF0) == 0 ||
	    read_controller_data(p) != 0xF0)
		return (-1);

	if (write_controller_command(p, KBDC_FORCE_AUX_OUTPUT) == 0 ||
	    write_controller_data(p, 0x56) == 0 ||
	    read_controller_data(p) != 0x56)
		return (-1);

	command = enabled ? 0xa4 : 0xa5;
	if (write_controller_command(p, KBDC_FORCE_AUX_OUTPUT) == 0 ||
	    write_controller_data(p, command) == 0 ||
	    (version = read_controller_data(p)) == command)
		return (-1);

	return (version);
}

int
set_active_aux_mux_port(KBDC p, int port)
{

	if (!aux_mux_is_enabled(p))
		return (FALSE);

	if (port < 0 || port >= KBDC_AUX_MUX_NUM_PORTS)
		return (FALSE);

	p->aux_mux_port = port;

	return (TRUE);
}

/* Checks for active multiplexing support and enables it */
int
enable_aux_mux(KBDC p)
{
	int version;

	version = set_aux_mux_state(p, TRUE);
	if (version >= 0) {
		p->aux_mux_enabled = TRUE;
		set_active_aux_mux_port(p, 0);
	}

	return (version);
}

int
disable_aux_mux(KBDC p)
{

	p->aux_mux_enabled = FALSE;

	return (set_aux_mux_state(p, FALSE));
}

int
aux_mux_is_enabled(KBDC p)
{

	return (p->aux_mux_enabled);
}