Project

General

Profile

Submit #3206 ยป psm_2_FBSD12_delay_hopefully_fixed.patch

htse, 10/05/2019 03:48 PM

View differences:

sys/dev/misc/kbd/atkbd.c
31 31
 * NOTE: All locks are handled by the kbd wrappers.
32 32
 */
33 33

  
34

  
34 35
#include "opt_kbd.h"
35 36
#include "opt_atkbd.h"
37
#include "opt_evdev.h"
36 38

  
37 39
#include <sys/param.h>
40
#include <sys/malloc.h>
38 41
#include <sys/systm.h>
39 42
#include <sys/kernel.h>
43

  
40 44
#include <sys/bus.h>
41 45
#include <sys/proc.h>
42 46
#include <sys/malloc.h>
......
49 53

  
50 54
#include <bus/isa/isareg.h>
51 55

  
56
#ifdef EVDEV_SUPPORT
57
#include <dev/misc/evdev/evdev.h>
58
#include <dev/misc/evdev/input.h>
59
#endif
60

  
61

  
52 62
static timeout_t	atkbd_timeout;
53 63

  
54 64
#if 0
......
128 138
	keyboard_t *kbd;
129 139

  
130 140
	/*
131
	 * The original text of the following comments are extracted 
141
	 * The original text of the following comments are extracted
132 142
	 * from syscons.c (1.287)
133
	 * 
143
	 *
134 144
	 * With release 2.1 of the Xaccel server, the keyboard is left
135 145
	 * hanging pretty often. Apparently an interrupt from the
136 146
	 * keyboard is lost, and I don't know why (yet).
......
139 149
	 *
140 150
	 * Try removing anything stuck in the keyboard controller; whether
141 151
	 * it's a keyboard scan code or mouse data. The low-level
142
	 * interrupt routine doesn't read the mouse data directly, 
152
	 * interrupt routine doesn't read the mouse data directly,
143 153
	 * but the keyboard controller driver will, as a side effect.
144 154
	 */
145 155
	/*
......
185 195
	int		ks_accents;	/* accent key index (> 0) */
186 196
	u_int		ks_composed_char; /* composed char code (> 0) */
187 197
	u_char		ks_prefix;	/* AT scan code prefix */
198
#ifdef EVDEV_SUPPORT
199
	struct evdev_dev *ks_evdev;
200
	int		ks_evdev_state;
201
#endif
202

  
188 203
} atkbd_state_t;
204
#define HAS_QUIRK(p, q)     (((atkbdc_softc_t *)(p))->quirks & q)
205
#define ALLOW_DISABLE_KBD(kbdc) !HAS_QUIRK(kbdc, KBDC_QUIRK_KEEP_ACTIVATED)
206

  
207
#define DEFAULT_DELAY	0x1  /* 500ms */
208
#define DEFAULT_RATE	0x10 /* 14Hz */
209

  
210
#ifdef EVDEV_SUPPORT
211
#define PS2_KEYBOARD_VENDOR	1
212
#define PS2_KEYBOARD_PRODUCT	1
213
#endif
189 214

  
190 215
/* keyboard driver declaration */
191 216
static int		atkbd_configure(int flags);
......
243 268
static int		typematic_delay(int delay);
244 269
static int		typematic_rate(int rate);
245 270

  
271
#ifdef EVDEV_SUPPORT
272
static evdev_event_t atkbd_ev_event;
273
static const struct evdev_methods atkbd_evdev_methods = {
274
	.ev_event = atkbd_ev_event,
275
};
276
#endif
277

  
246 278
/* local variables */
247 279

  
248 280
/* the initial key map, accent map and fkey strings */
......
259 291
static accentmap_t	default_accentmap;
260 292
static fkeytab_t	default_fkeytab[NUM_FKEYS];
261 293

  
262
/* 
294
/*
263 295
 * The back door to the keyboard driver!
264 296
 * This function is called by the console driver, via the kbdio module,
265 297
 * to tickle keyboard drivers when the low-level console is being initialized.
......
280 312
	 * is disabled, unregister the keyboard if any.
281 313
	 */
282 314
	if (atkbdc_configure() != 0 ||
283
	    resource_disabled("atkbd", ATKBD_DEFAULT)) {
315
		resource_disabled("atkbd", ATKBD_DEFAULT)) {
284 316
		i = kbd_find_keyboard(ATKBD_DRIVER_NAME, ATKBD_DEFAULT);
285 317
		if (i >= 0) {
286 318
			kbd = kbd_get_keyboard(i);
287 319
			KBD_ALWAYS_LOCK(kbd);
288 320
			kbd_unregister(kbd);
289
			kbd = NULL; /* huh? */
290
		}
321
			kbd->kb_flags &= ~KB_REGISTERED;
322
        }
291 323
		return 0;
292 324
	}
293
	
325

  
294 326
	/* XXX: a kludge to obtain the device configuration flags */
295 327
	if (resource_int_value("atkbd", ATKBD_DEFAULT, "flags", &i) == 0)
296 328
		flags |= i;
......
350 382
	fkeytab_t *fkeymap;
351 383
	int fkeymap_size;
352 384
	int delay[2];
353
	int *data = (int *)arg;	/* data[0]: controller, data[1]: irq */
385
	int *data = (int *)arg;	 /* data[0]: controller, data[1]: irq */
386
	int needfree = 0;
387
	int error = 0;
388
#ifdef EVDEV_SUPPORT
389
	struct evdev_dev *evdev;
390
	char phys_loc[8];
391
#endif
392

  
354 393

  
355 394
	/* XXX */
356 395
	if (unit == ATKBD_DEFAULT) {
......
363 402
		accmap = &default_accentmap;
364 403
		fkeymap = default_fkeytab;
365 404
		fkeymap_size = NELEM(default_fkeytab);
405
		needfree = 0;
366 406
	} else if (*kbdp == NULL) {
367 407
		*kbdp = kbd = kmalloc(sizeof(*kbd), M_DEVBUF, M_WAITOK|M_ZERO);
368 408
		state = kmalloc(sizeof(*state), M_DEVBUF, M_WAITOK|M_ZERO);
......
370 410
		accmap = kmalloc(sizeof(accent_map), M_DEVBUF, M_WAITOK);
371 411
		fkeymap = kmalloc(sizeof(fkey_tab), M_DEVBUF, M_WAITOK);
372 412
		fkeymap_size = NELEM(fkey_tab);
413
		needfree = 1;
414
		if((kbd == NULL) || (state == NULL) || (keymap == NULL) ||
415
			(accmap == NULL) || (fkeymap == NULL)){
416
			error = ENOMEM;
417
			goto bad;
418
		}
373 419
	} else if (KBD_IS_INITIALIZED(*kbdp) && KBD_IS_CONFIGURED(*kbdp)) {
374 420
		return 0;
375 421
	} else {
......
380 426
		accmap = kbd->kb_accentmap;
381 427
		fkeymap = kbd->kb_fkeytab;
382 428
		fkeymap_size = kbd->kb_fkeytab_size;
429
		needfree = 0;
383 430
	}
384 431

  
385 432
	if (!KBD_IS_PROBED(kbd)) {
386 433
		state->kbdc = atkbdc_open(data[0]);
387 434
		if (state->kbdc == NULL) {
388
			return ENXIO;
435
			error = ENXIO;
436
		goto bad;
437

  
389 438
		}
390 439
		kbd_init_struct(kbd, ATKBD_DRIVER_NAME, KB_OTHER, unit, flags,
391 440
				KB_PRI_ATKBD, 0, 0);
......
395 444
		      imin(fkeymap_size*sizeof(fkeymap[0]), sizeof(fkey_tab)));
396 445
		kbd_set_maps(kbd, keymap, accmap, fkeymap, fkeymap_size);
397 446
		kbd->kb_data = (void *)state;
398
	
447

  
399 448
		if (probe_keyboard(state->kbdc, flags)) { /* shouldn't happen */
400 449
			if (flags & KB_CONF_FAIL_IF_NO_KBD) {
401 450
				return ENXIO;
......
405 454
		}
406 455
		atkbd_clear_state(kbd);
407 456
		state->ks_mode = K_XLATE;
408
		/* 
457
		/*
409 458
		 * FIXME: set the initial value for lock keys in ks_state
410 459
		 * according to the BIOS data?
411 460
		 */
......
416 465
		if (!KBD_HAS_DEVICE(kbd)
417 466
		    && init_keyboard(state->kbdc, &kbd->kb_type, kbd->kb_config)
418 467
		    && (kbd->kb_config & KB_CONF_FAIL_IF_NO_KBD)) {
419
			return ENXIO;
468
			kbd_unregister(kbd);
469
			error = ENXIO;
470
			goto bad;
420 471
		}
421 472
		atkbd_ioctl(kbd, KDSETLED, (caddr_t)&state->ks_state);
422 473
		get_typematic(kbd);
423 474
		delay[0] = kbd->kb_delay1;
424 475
		delay[1] = kbd->kb_delay2;
425 476
		atkbd_ioctl(kbd, KDSETREPEAT, (caddr_t)delay);
477
#ifdef EVDEV_SUPPORT
478
	/* register as evdev provider on first init */
479
	if (state->ks_evdev == NULL) {
480
		ksnprintf(phys_loc, sizeof(phys_loc), "atkbd%d", unit);
481
		evdev = evdev_alloc();
482
		evdev_set_name(evdev, "AT keyboard");
483
		evdev_set_phys(evdev, phys_loc);
484
		evdev_set_id(evdev, BUS_I8042, PS2_KEYBOARD_VENDOR,
485
			PS2_KEYBOARD_PRODUCT, 0);
486
		evdev_set_methods(evdev, kbd, &atkbd_evdev_methods);
487
		evdev_support_event(evdev, EV_SYN);
488
		evdev_support_event(evdev, EV_KEY);
489
		evdev_support_event(evdev, EV_LED);
490
		evdev_support_event(evdev, EV_REP);
491
		evdev_support_all_known_keys(evdev);
492
		evdev_support_led(evdev, LED_NUML);
493
		evdev_support_led(evdev, LED_CAPSL);
494
		evdev_support_led(evdev, LED_SCROLLL);
495

  
496
	    /* NOTE: in freebsd, this depends on their Giant mutex lock, 
497
	     * discuss with the others which locking would be best here */
498
		if (evdev_register(evdev))
499
			evdev_free(evdev);
500
		else
501
			state->ks_evdev = evdev;
502
		state->ks_evdev_state = 0;
503
	}
504
#endif
505

  
426 506
		KBD_FOUND_DEVICE(kbd);
427 507
		KBD_INIT_DONE(kbd);
428 508
	}
429 509
	if (!KBD_IS_CONFIGURED(kbd)) {
430 510
		if (kbd_register(kbd) < 0) {
431
			return ENXIO;
511
			error = ENXIO;
512
			goto bad;
432 513
		}
433 514
		KBD_CONFIG_DONE(kbd);
434 515
	}
435 516

  
436 517
	return 0;
518

  
519
bad:
520
	if(needfree) {
521
		if (state != NULL)
522
			kfree(state, M_DEVBUF);
523
		if (keymap != NULL)
524
			kfree(keymap, M_DEVBUF);
525
		if (accmap != NULL)
526
			kfree(accmap, M_DEVBUF);
527
		if (fkeymap != NULL)
528
			kfree(fkeymap, M_DEVBUF);
529
		if (kbd != NULL) {
530
			kfree(kbd, M_DEVBUF);
531
			*kbdp = NULL;   /* insure ref doesn't leak to caller */
532
		}
533

  
534
	}
535

  
536
	return error;
437 537
}
438 538

  
439 539
/* finish using this keyboard */
440 540
static int
441 541
atkbd_term(keyboard_t *kbd)
442 542
{
543
#ifdef EVDEV_SUPPORT
544
	atkbd_state_t *state = (atkbd_state_t *)kbd->kb_data;
545

  
546
	if(state->ks_evdev) {
547
		evdev_free(state->ks_evdev);
548
		state->ks_evdev = NULL;
549
		state->ks_evdev_state = 0;
550
	}
551
#endif
552

  
443 553
	kbd_unregister(kbd);
444 554
	return 0;
445 555
}
......
500 610
	return error;
501 611
}
502 612

  
503
/* 
613
/*
504 614
 * Enable the access to the device; until this function is called,
505 615
 * the client cannot read from the keyboard.
506 616
 */
......
591 701

  
592 702
#if KBDIO_DEBUG >= 10
593 703
	kprintf("atkbd_read_char(): scancode:0x%x\n", scancode);
704
#endif
705
#ifdef EVDEV_SUPPORT
706
	/* push evdev event */
707
	if (evdev_rcpt_mask & EVDEV_RCPT_HW_KBD && state->ks_evdev != NULL) {
708
		keycode = evdev_scancode2key(&state->ks_evdev_state,
709
				scancode);
710

  
711
		if (keycode != KEY_RESERVED) {
712
			evdev_push_event(state->ks_evdev, EV_KEY,
713
				(uint16_t)keycode, scancode & 0x80 ? 0 : 1);
714
			evdev_sync(state->ks_evdev);
715
		}
716
}
594 717
#endif
595 718

  
596 719
	/* return the byte as is for the K_RAW mode */
......
622 745
			goto next_code;
623 746
		}
624 747
		break;
625
	case 0xE0:      /* 0xE0 prefix */
748
	case 0xE0:	/* 0xE0 prefix */
626 749
		state->ks_prefix = 0;
627 750
		switch (keycode) {
628 751
		case 0x1C:	/* right enter key */
......
688 811
		}
689 812
		break;
690 813
	case 0xE1:	/* 0xE1 prefix */
691
		/* 
814
		/*
692 815
		 * The pause/break key on the 101 keyboard produces:
693 816
		 * E1-1D-45 E1-9D-C5
694 817
		 * Ctrl-pause/break produces:
......
803 926
atkbd_check_char(keyboard_t *kbd)
804 927
{
805 928
	atkbd_state_t *state;
806
	int ret;
807 929

  
808 930
	if (!KBD_IS_ACTIVE(kbd)) {
809 931
		return FALSE;
......
812 934
	if (!(state->ks_flags & COMPOSE) && (state->ks_composed_char > 0)) {
813 935
		return TRUE;
814 936
	}
815
	ret = kbdc_data_ready(state->kbdc);
816
	return ret;
937
	return kbdc_data_ready(state->kbdc);
817 938
}
818 939

  
819 940
/* some useful control functions */
......
883 1004
				return error;
884 1005
			}
885 1006
		}
1007
#ifdef EVDEV_SUPPORT
1008
		/* push LED states to evdev */
1009
		if (state->ks_evdev != NULL &&
1010
			evdev_rcpt_mask & EVDEV_RCPT_HW_KBD)
1011
			evdev_push_leds(state->ks_evdev, *(int *)arg);
1012
#endif
1013

  
886 1014
		KBD_LED_VAL(kbd) = *(int *)arg;
887 1015
		break;
888 1016

  
......
910 1038
		if (error == 0) {
911 1039
			kbd->kb_delay1 = typematic_delay(i);
912 1040
			kbd->kb_delay2 = typematic_rate(i);
1041
#ifdef EVDEV_SUPPORT
1042
			if (state->ks_evdev != NULL &&
1043
				evdev_rcpt_mask & EVDEV_RCPT_HW_KBD)
1044
				evdev_push_repeats(state->ks_evdev, kbd);
1045
#endif
1046

  
913 1047
		}
914 1048
		return error;
915 1049

  
......
992 1126
	return 0;
993 1127
}
994 1128

  
1129
#ifdef EVDEV_SUPPORT
1130
static void
1131
atkbd_ev_event(struct evdev_dev *evdev, void *softc, uint16_t type, uint16_t code,
1132
    int32_t value)
1133
{
1134
	keyboard_t *kbd = (keyboard_t *)softc;
1135

  
1136
	if (evdev_rcpt_mask & EVDEV_RCPT_HW_KBD &&
1137
		(type == EV_LED || type == EV_REP)) {
1138
		/*
1139
		 * NOTE: the current data writing part of kbd_ev_event() are its calls to kbd_ioctl()
1140
		 * kbd_ioctl() performs a lockmgr lock per the KBD_LOCK macro, 
1141
		 * so unlike FreeBSD we don't lock here.
1142
		 *
1143
		 * Should the data writes in kbd_ev_event(), which is a completely FreeBSD ported
1144
		 * function, ever change to encompass things outside of kbd_ioctl,
1145
		 * then this needs to be redesigned or rethought, possibly by:
1146
		 *
1147
		 * setting a lockmgr(&kbd->kb_lock, LK_EXCLUSIVE)
1148
		 * saving the KB_POLLING flag of kbd->kb_flags 
1149
		 * setting it
1150
		 * doing our stuff in kbd_ev_event()
1151
		 * and restoring the original KB_POLLING FLAG afterwards
1152
		 * releasing the lockmgr() held lock
1153
		 *
1154
		 * KBD_LOCK defined at sys/dev/misc/kbd/kbdreg.h:137
1155
		 * and works by checking if KB_POLLING is not set.
1156
		 * This would obviously be quite a hack, if it were needed.
1157
		 * Note by htse (harald.brinkhof@gmail.com).
1158
		 */
1159
#if 0
1160
		if(!lockmgr(&kbd->kb_lock, LK_EXCLUSIVE)) {
1161
			/* save original flag */
1162
			int orig_is_polled = KBD_IS_POLLED(kbd);
1163
			/* set state to polling */
1164
			KBD_POLL(kbd);
1165
#endif
1166
			kbd_ev_event(kbd, type, code, value);
1167
#if 0
1168
			/* restore original state if not polling */
1169
			if(orig_is_polled == 0)
1170
				KBD_UNPOLL(kbd);
1171

  
1172
			lockmgr(&kbd->kb_lock, LK_RELEASE);
1173
		}
1174
#endif
1175
	}
1176
}
1177
#endif
1178

  
1179

  
995 1180
/* local functions */
996 1181

  
997 1182
static int
......
1031 1216
	/* enable the keyboard port and intr. */
1032 1217
	if (setup_kbd_port(kbdc, TRUE, TRUE)) {
1033 1218
		/*
1034
		 * CONTROLLER ERROR 
1035
		 * This is serious; the keyboard intr is left disabled! 
1219
		 * CONTROLLER ERROR
1220
		 * This is serious; the keyboard intr is left disabled!
1036 1221
		 */
1037 1222
		return ENXIO;
1038 1223
	}
1039
    
1224

  
1040 1225
	return 0;
1041 1226
}
1042 1227

  
......
1044 1229
probe_keyboard(KBDC kbdc, int flags)
1045 1230
{
1046 1231
	/*
1047
	 * Don't try to print anything in this function.  The low-level 
1232
	 * Don't try to print anything in this function.  The low-level
1048 1233
	 * console may not have been initialized yet...
1049 1234
	 */
1050 1235
	int err;
1051 1236
	int c;
1237
	int m;
1052 1238

  
1053 1239
	if (!kbdc_lock(kbdc, TRUE)) {
1054 1240
		/* driver error? */
......
1066 1252
	empty_both_buffers(kbdc, 100);
1067 1253

  
1068 1254
	/* save the current keyboard controller command byte */
1255
	m = kbdc_get_device_mask(kbdc) & ~KBD_KBD_CONTROL_BITS;
1069 1256
	c = get_controller_command_byte(kbdc);
1070 1257
	if (c == -1) {
1071 1258
		/* CONTROLLER ERROR */
1259
		kbdc_set_device_mask(kbdc, m);
1072 1260
		kbdc_lock(kbdc, FALSE);
1073 1261
		return ENXIO;
1074 1262
	}
1075 1263

  
1076
	/* 
1264
	/*
1077 1265
	 * The keyboard may have been screwed up by the boot block.
1078 1266
	 * We may just be able to recover from error by testing the controller
1079 1267
	 * and the keyboard port. The controller command byte needs to be
1080
	 * saved before this recovery operation, as some controllers seem 
1268
	 * saved before this recovery operation, as some controllers seem
1081 1269
	 * to set the command byte to particular values.
1082 1270
	 */
1083 1271
	test_controller(kbdc);
1084
	test_kbd_port(kbdc);
1272
	if (!(flags & KB_CONF_NO_PROBE_TEST))
1273
		test_kbd_port(kbdc);
1085 1274

  
1086 1275
	err = get_kbd_echo(kbdc);
1087 1276

  
......
1092 1281
	 * to the system later.  It is NOT recommended to hot-plug
1093 1282
	 * the AT keyboard, but many people do so...
1094 1283
	 */
1284
	kbdc_set_device_mask(kbdc, m | KBD_KBD_CONTROL_BITS);
1095 1285
	setup_kbd_port(kbdc, TRUE, TRUE);
1096 1286
#if 0
1097 1287
	if (err) {
......
1101 1291
#endif
1102 1292

  
1103 1293
	kbdc_lock(kbdc, FALSE);
1104
	return err;
1294
	return (HAS_QUIRK(kbdc, KBDC_QUIRK_IGNORE_PROBE_RESULT)) ? 0 : err;
1105 1295
}
1106 1296

  
1107 1297
static int
......
1193 1383
		return EIO;
1194 1384
	}
1195 1385

  
1196
	/* 
1197
	 * Check if we have an XT keyboard before we attempt to reset it. 
1198
	 * The procedure assumes that the keyboard and the controller have 
1199
	 * been set up properly by BIOS and have not been messed up 
1386
	/*
1387
	 * Check if we have an XT keyboard before we attempt to reset it.
1388
	 * The procedure assumes that the keyboard and the controller have
1389
	 * been set up properly by BIOS and have not been messed up
1200 1390
	 * during the boot process.
1201 1391
	 */
1202 1392
	codeset = -1;
......
1207 1397
	else if ((c & KBD_TRANSLATION) == 0) {
1208 1398
		/* SET_SCANCODE_SET is not always supported; ignore error */
1209 1399
		if (send_kbd_command_and_data(kbdc, KBDC_SET_SCANCODE_SET, 0)
1210
			== KBD_ACK) 
1400
			== KBD_ACK)
1211 1401
			codeset = read_kbd_data(kbdc);
1212 1402
	}
1213 1403
#endif /* KBD_DETECT_XT_KEYBOARD */
1214 1404
	if (bootverbose)
1215 1405
		kprintf("atkbd: scancode set %d\n", codeset);
1216
 
1406

  
1217 1407
	/*
1218 1408
	 * Get the keyboard id.
1219 1409
	 */
......
1252 1442
			c &= ~KBD_TRANSLATION;
1253 1443
		} else {
1254 1444
			/*
1255
			 * KEYBOARD ERROR 
1445
			 * KEYBOARD ERROR
1256 1446
			 * The XT kbd isn't usable unless the proper scan
1257
			 * code set is selected. 
1447
			 * code set is selected.
1258 1448
			 */
1259 1449
			set_controller_command_byte(kbdc,
1260 1450
						    KBD_KBD_CONTROL_BITS, c);
......
1324 1514
	}
1325 1515

  
1326 1516
	/* enable the keyboard port and intr. */
1327
	if (!set_controller_command_byte(kbdc, 
1517
	if (!set_controller_command_byte(kbdc,
1328 1518
		KBD_KBD_CONTROL_BITS | KBD_TRANSLATION |
1329 1519
		KBD_OVERRIDE_KBD_LOCK | mux_mask,
1330 1520
		(c & (KBD_TRANSLATION | KBD_OVERRIDE_KBD_LOCK))
1331 1521
		    | KBD_ENABLE_KBD_PORT | KBD_ENABLE_KBD_INT | mux_val)) {
1332 1522
		/*
1333
		 * CONTROLLER ERROR 
1523
		 * CONTROLLER ERROR
1334 1524
		 * This is serious; we are left with the disabled
1335
		 * keyboard intr. 
1525
		 * keyboard intr.
1336 1526
		 */
1337 1527
		set_controller_command_byte(kbdc,
1338 1528
				KBD_KBD_CONTROL_BITS | KBD_TRANSLATION |
......
1398 1588
static int
1399 1589
write_kbd(KBDC kbdc, int command, int data)
1400 1590
{
1401
    /* prevent the timeout routine from polling the keyboard */
1402
    if (!kbdc_lock(kbdc, TRUE)) 
1403
	return EBUSY;
1591
	/* prevent the timeout routine from polling the keyboard */
1592
	if (!kbdc_lock(kbdc, TRUE))
1593
		return EBUSY;
1404 1594

  
1405
    /* disable the keyboard and mouse interrupt */
1406
    crit_enter();
1595
	/* disable the keyboard and mouse interrupt */
1596
	crit_enter();
1407 1597

  
1408 1598
#if 0
1409
    /*
1410
     * XXX NOTE: We can't just disable the KBD port any more, even
1411
     * 	         temporarily, without blowing up some BIOS emulations
1412
     *		 if not followed by a full reset.
1413
     */
1414
    c = get_controller_command_byte(kbdc);
1415
    if ((c == -1) ||
1416
	!set_controller_command_byte(kbdc,
1599
	/*
1600
	 * XXX NOTE: We can't just disable the KBD port any more, even
1601
	 * 	         temporarily, without blowing up some BIOS emulations
1602
	 *		 if not followed by a full reset.
1603
	 */
1604
	c = get_controller_command_byte(kbdc);
1605
	if ((c == -1) ||
1606
		!set_controller_command_byte(kbdc,
1417 1607
		KBD_KBD_CONTROL_BITS,
1418 1608
		KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT)) {
1419
	/* CONTROLLER ERROR */
1420
        kbdc_lock(kbdc, FALSE);
1609
		/* CONTROLLER ERROR */
1610
		kbdc_lock(kbdc, FALSE);
1611
		crit_exit();
1612
		return EIO;
1613
	}
1614
	/*
1615
	 * Now that the keyboard controller is told not to generate
1616
	 * the keyboard and mouse interrupts, call `splx()' to allow
1617
	 * the other tty interrupts. The clock interrupt may also occur,
1618
	 * but the timeout routine (`scrn_timer()') will be blocked
1619
	 * by the lock flag set via `kbdc_lock()'
1620
	 */
1421 1621
	crit_exit();
1422
	return EIO;
1423
    }
1424
    /* 
1425
     * Now that the keyboard controller is told not to generate 
1426
     * the keyboard and mouse interrupts, call `splx()' to allow 
1427
     * the other tty interrupts. The clock interrupt may also occur, 
1428
     * but the timeout routine (`scrn_timer()') will be blocked 
1429
     * by the lock flag set via `kbdc_lock()'
1430
     */
1431
    crit_exit();
1432 1622
#endif
1433 1623

  
1434
    if (send_kbd_command_and_data(kbdc, command, data) != KBD_ACK)
1435
        send_kbd_command(kbdc, KBDC_ENABLE_KBD);
1624
	if (send_kbd_command_and_data(kbdc, command, data) != KBD_ACK)
1625
		send_kbd_command(kbdc, KBDC_ENABLE_KBD);
1436 1626

  
1437 1627
#if 0
1438
    /* restore the interrupts */
1439
    if (!set_controller_command_byte(kbdc, KBD_KBD_CONTROL_BITS, c)) {
1440
	/* CONTROLLER ERROR */
1441
    }
1628
	/* restore the interrupts */
1629
	if (!set_controller_command_byte(kbdc, KBD_KBD_CONTROL_BITS, c)) {
1630
		/* CONTROLLER ERROR */
1631
	}
1442 1632
#else
1443
    crit_exit();
1633
	crit_exit();
1444 1634
#endif
1445
    kbdc_lock(kbdc, FALSE);
1635
	kbdc_lock(kbdc, FALSE);
1446 1636

  
1447
    return 0;
1637
	return 0;
1448 1638
}
1449 1639

  
1450 1640
static int
sys/dev/misc/kbd/atkbdc.c
109 109
static int wait_for_aux_data(atkbdc_softc_t *kbdc);
110 110
static int wait_for_aux_ack(atkbdc_softc_t *kbdc);
111 111

  
112
struct atkbdc_quirks {
113
	const char*	bios_vendor;
114
	const char*	maker;
115
	const char*	product;
116
	int		quirk;
117
};
118

  
119
static struct atkbdc_quirks quirks[] = {
120
	{"coreboot", NULL, NULL,
121
	KBDC_QUIRK_KEEP_ACTIVATED | KBDC_QUIRK_IGNORE_PROBE_RESULT |
122
	KBDC_QUIRK_RESET_AFTER_PROBE | KBDC_QUIRK_SETLEDS_ON_INIT},
123

  
124
	{NULL, NULL, NULL, 0}
125
};
126

  
127
#define QUIRK_STR_MATCH(s1, s2) (s1 == NULL || \
128
	(s2 != NULL && !strcmp(s1, s2)))
129

  
130
static int
131
atkbdc_getquirks(void)
132
{
133
	int i;
134
	char* bios_vendor = kgetenv("smbios.bios.vendor");
135
	char* maker = kgetenv("smbios.system.maker");
136
	char* product = kgetenv("smbios.system.product");
137

  
138
	for (i=0; quirks[i].quirk != 0; ++i)
139
		if (QUIRK_STR_MATCH(quirks[i].bios_vendor, bios_vendor) &&
140
			QUIRK_STR_MATCH(quirks[i].maker, maker) &&
141
			QUIRK_STR_MATCH(quirks[i].product, product))
142
			return (quirks[i].quirk);
143

  
144
	return (0);
145
}
146

  
147

  
112 148
atkbdc_softc_t *
113 149
atkbdc_get_softc(int unit)
114 150
{
......
225 261
atkbdc_setup(atkbdc_softc_t *sc, bus_space_tag_t tag, bus_space_handle_t h0,
226 262
	     bus_space_handle_t h1)
227 263
{
264

  
265
	u_int64_t tscval[3], read_delay;
266
	register_t flags;
267

  
228 268
	if (sc->ioh0 == 0) {	/* XXX */
229
	    sc->command_byte = -1;
230
	    sc->lock = FALSE;
231
	    sc->kbd.head = sc->kbd.tail = 0;
232
	    sc->aux.head = sc->aux.tail = 0;
269
		sc->command_byte = -1;
270
		sc->command_mask = 0;
271
		sc->lock = FALSE;
272
		sc->kbd.head = sc->kbd.tail = 0;
273
		sc->aux.head = sc->aux.tail = 0;
274
		sc->aux_mux_enabled = FALSE;
233 275
#if KBDIO_DEBUG >= 2
234
	    sc->kbd.call_count = 0;
235
	    sc->kbd.qcount = sc->kbd.max_qcount = 0;
236
	    sc->aux.call_count = 0;
237
	    sc->aux.qcount = sc->aux.max_qcount = 0;
276
		sc->kbd.call_count = 0;
277
		sc->kbd.qcount = sc->kbd.max_qcount = 0;
278
		sc->aux.call_count = 0;
279
		sc->aux.qcount = sc->aux.max_qcount = 0;
238 280
#endif
239 281
	}
240 282
	sc->iot = tag;
241 283
	sc->ioh0 = h0;
242 284
	sc->ioh1 = h1;
285
	/*
286
	* On certain chipsets AT keyboard controller isn't present and is
287
	* emulated by BIOS using SMI interrupt. On those chipsets reading
288
	* from the status port may be thousand times slower than usually.
289
	* Sometimes this emilation is not working properly resulting in
290
	* commands timing our and since we assume that inb() operation
291
	* takes very little time to complete we need to adjust number of
292
	* retries to keep waiting time within a designed limits (100ms).
293
	* Measure time it takes to make read_status() call and adjust
294
	* number of retries accordingly.
295
	*/
296
	flags = intr_disable();
297
	tscval[0] = rdtsc();
298
	read_status(sc);
299
	tscval[1] = rdtsc();
300
	DELAY(1000);
301
	tscval[2] = rdtsc();
302
	intr_restore(flags);
303
	read_delay = tscval[1] - tscval[0];
304
	read_delay /= (tscval[2] - tscval[1]) / 1000;
305
	sc->retry = 100000 / ((KBDD_DELAYTIME * 2) + read_delay);
306

  
307
	sc->quirks = atkbdc_getquirks();
243 308
	return 0;
244 309
}
245 310

  
246 311
/* open a keyboard controller */
247
KBDC 
312
KBDC
248 313
atkbdc_open(int unit)
249 314
{
250
    if (unit <= 0)
251
	unit = 0;
252
    if (unit >= MAXKBDC)
315
	if (unit <= 0)
316
		unit = 0;
317
	if (unit >= MAXKBDC)
318
		return NULL;
319
	if ((atkbdc_softc[unit]->port0 != NULL)
320
		|| (atkbdc_softc[unit]->ioh0 != 0))		/* XXX */
321
		return (KBDC)atkbdc_softc[unit];
253 322
	return NULL;
254
    if ((atkbdc_softc[unit]->port0 != NULL)
255
	|| (atkbdc_softc[unit]->ioh0 != 0))		/* XXX */
256
	return (KBDC)atkbdc_softc[unit];
257
    return NULL;
258 323
}
259 324

  
260 325
/*
......
296 361
 */
297 362

  
298 363
/* set/reset polling lock */
299
int 
364
int
300 365
kbdc_lock(KBDC p, int lock)
301 366
{
302
    int prevlock;
367
	int prevlock;
303 368

  
304
    prevlock = kbdcp(p)->lock;
305
    kbdcp(p)->lock = lock;
369
	prevlock = kbdcp(p)->lock;
370
	kbdcp(p)->lock = lock;
306 371

  
307
    return (prevlock != lock);
372
	return (prevlock != lock);
308 373
}
309 374

  
310 375
/* check if any data is waiting to be processed */
311 376
int
312 377
kbdc_data_ready(KBDC p)
313 378
{
314
    return (availq(&kbdcp(p)->kbd) || availq(&kbdcp(p)->aux) 
379
	return (availq(&kbdcp(p)->kbd) || availq(&kbdcp(p)->aux) 
315 380
	|| (read_status(kbdcp(p)) & KBDS_ANY_BUFFER_FULL));
316 381
}
317 382

  
......
320 385
static int
321 386
addq(kbdkqueue *q, int c)
322 387
{
323
    if (nextq(q->tail) != q->head) {
324
	q->q[q->tail] = c;
325
	q->tail = nextq(q->tail);
388
	if (nextq(q->tail) != q->head) {
389
		q->q[q->tail] = c;
390
		q->tail = nextq(q->tail);
326 391
#if KBDIO_DEBUG >= 2
327
        ++q->call_count;
328
        ++q->qcount;
329
	if (q->qcount > q->max_qcount)
330
            q->max_qcount = q->qcount;
392
		++q->call_count;
393
		++q->qcount;
394
		if (q->qcount > q->max_qcount)
395
			q->max_qcount = q->qcount;
331 396
#endif
332
	return TRUE;
333
    }
334
    return FALSE;
397
		return TRUE;
398
	}
399
	return FALSE;
335 400
}
336 401

  
337 402
static int
338 403
removeq(kbdkqueue *q)
339 404
{
340
    int c;
405
	int c;
341 406

  
342
    if (q->tail != q->head) {
343
	c = q->q[q->head];
344
	q->head = nextq(q->head);
407
	if (q->tail != q->head) {
408
		c = q->q[q->head];
409
		q->head = nextq(q->head);
345 410
#if KBDIO_DEBUG >= 2
346
        --q->qcount;
411
		--q->qcount;
347 412
#endif
348
	return c;
349
    }
350
    return -1;
413
		return c;
414
	}
415
	return -1;
351 416
}
352 417

  
353
/* 
418
/*
354 419
 * device I/O routines
355 420
 */
356 421
static int
357 422
wait_while_controller_busy(struct atkbdc_softc *kbdc)
358 423
{
359
    /* CPU will stay inside the loop for 100msec at most */
360
    TOTALDELAY retry = { .us = 70000, .last_clock =0 };	/* 70ms */
361
    int f;
362
    unsigned char c;
363

  
364
    while ((f = read_status(kbdc)) & KBDS_INPUT_BUFFER_FULL) {
365
	if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL) {
366
	    DELAY(KBDD_DELAYTIME);
367
	    c = read_data(kbdc);
368
	    addq(&kbdc->kbd, c);
369
	} else if ((f & KBDS_BUFFER_FULL) == KBDS_AUX_BUFFER_FULL) {
370
	    DELAY(KBDD_DELAYTIME);
371
	    c = read_data(kbdc);
372
	    addq(&kbdc->aux, c);
424
	/* CPU will stay inside the loop for 100msec at most */
425
	TOTALDELAY retry = { .us = 70000, .last_clock =0 };	/* 70ms */
426
	int f;
427
	unsigned char c;
428

  
429
	while ((f = read_status(kbdc)) & KBDS_INPUT_BUFFER_FULL) {
430
		if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL) {
431
			DELAY(KBDD_DELAYTIME);
432
			c = read_data(kbdc);
433
			addq(&kbdc->kbd, c);
434
		} else if ((f & KBDS_BUFFER_FULL) == KBDS_AUX_BUFFER_FULL) {
435
			DELAY(KBDD_DELAYTIME);
436
			c = read_data(kbdc);
437
			addq(&kbdc->aux, c);
438
		}
439
		DELAY(KBDC_DELAYTIME);
440
		if (CHECKTIMEOUT(&retry))
441
			return FALSE;
373 442
	}
374
        DELAY(KBDC_DELAYTIME);
375
	if (CHECKTIMEOUT(&retry))
376
    	    return FALSE;
377
    }
378
    return TRUE;
443
	return TRUE;
379 444
}
380 445

  
381 446
/*
......
385 450
static int
386 451
wait_for_data(struct atkbdc_softc *kbdc)
387 452
{
388
    /* CPU will stay inside the loop for 200msec at most */
389
    TOTALDELAY retry = { 200000, 0 };	/* 200ms */
390
    int f;
391

  
392
    while ((f = read_status(kbdc) & KBDS_ANY_BUFFER_FULL) == 0) {
393
        DELAY(KBDC_DELAYTIME);
394
	if (CHECKTIMEOUT(&retry))
395
    	    return 0;
396
    }
397
    DELAY(KBDD_DELAYTIME);
398
    return f;
453
	/* CPU will stay inside the loop for 200msec at most */
454
	TOTALDELAY retry = { 200000, 0 };	/* 200ms */
455
	int f;
456

  
457
	while ((f = read_status(kbdc) & KBDS_ANY_BUFFER_FULL) == 0) {
458
		DELAY(KBDC_DELAYTIME);
459
		if (CHECKTIMEOUT(&retry))
460
		return 0;
461
	}
462
	DELAY(KBDD_DELAYTIME);
463
	return f;
399 464
}
400 465

  
401 466
/* wait for data from the keyboard */
402 467
static int
403 468
wait_for_kbd_data(struct atkbdc_softc *kbdc)
404 469
{
405
    /* CPU will stay inside the loop for 200msec at most */
406
    TOTALDELAY retry = { 200000, 0 };	/* 200ms */
407
    int f;
408
    unsigned char c;
409

  
410
    while ((f = read_status(kbdc) & KBDS_BUFFER_FULL)
411
	    != KBDS_KBD_BUFFER_FULL) {
412
        if (f == KBDS_AUX_BUFFER_FULL) {
413
	    DELAY(KBDD_DELAYTIME);
414
	    c = read_data(kbdc);
415
	    addq(&kbdc->aux, c);
470
	/* CPU will stay inside the loop for 200msec at most */
471
	TOTALDELAY retry = { 200000, 0 };	/* 200ms */
472
	int f;
473
	unsigned char c;
474

  
475
	while ((f = read_status(kbdc) & KBDS_BUFFER_FULL)
476
		!= KBDS_KBD_BUFFER_FULL) {
477
		if (f == KBDS_AUX_BUFFER_FULL) {
478
			DELAY(KBDD_DELAYTIME);
479
			c = read_data(kbdc);
480
			addq(&kbdc->aux, c);
481
		}
482
		DELAY(KBDC_DELAYTIME);
483
		if (CHECKTIMEOUT(&retry))
484
			return 0;
416 485
	}
417
        DELAY(KBDC_DELAYTIME);
418
        if (CHECKTIMEOUT(&retry))
419
    	    return 0;
420
    }
421
    DELAY(KBDD_DELAYTIME);
422
    return f;
486
	DELAY(KBDD_DELAYTIME);
487
	return f;
423 488
}
424 489

  
425 490
/* 
......
429 494
static int
430 495
wait_for_kbd_ack(struct atkbdc_softc *kbdc)
431 496
{
432
    /* CPU will stay inside the loop for 200msec at most */
433
    TOTALDELAY retry = { 200000, 0 };	/* 200ms */
434
    int f;
435
    int b;
436

  
437
    while (CHECKTIMEOUT(&retry) == 0) {
438
        if ((f = read_status(kbdc)) & KBDS_ANY_BUFFER_FULL) {
439
	    DELAY(KBDD_DELAYTIME);
440
            b = read_data(kbdc);
441
	    if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL) {
442
		if ((b == KBD_ACK) || (b == KBD_RESEND) 
443
		    || (b == KBD_RESET_FAIL))
444
		    return b;
445
		addq(&kbdc->kbd, b);
446
	    } else if ((f & KBDS_BUFFER_FULL) == KBDS_AUX_BUFFER_FULL) {
447
		addq(&kbdc->aux, b);
448
	    }
497
	/* CPU will stay inside the loop for 200msec at most */
498
	TOTALDELAY retry = { 200000, 0 };	/* 200ms */
499
	int f;
500
	int b;
501

  
502
	while (CHECKTIMEOUT(&retry) == 0) {
503
		if ((f = read_status(kbdc)) & KBDS_ANY_BUFFER_FULL) {
504
			DELAY(KBDD_DELAYTIME);
505
			b = read_data(kbdc);
506
			if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL) {
507
				if ((b == KBD_ACK) || (b == KBD_RESEND) 
508
		    			|| (b == KBD_RESET_FAIL))
509
		    			return b;
510
				addq(&kbdc->kbd, b);
511
			} else if ((f & KBDS_BUFFER_FULL) == KBDS_AUX_BUFFER_FULL) {
512
				addq(&kbdc->aux, b);
513
			}
514
		}
515
		DELAY(KBDC_DELAYTIME);
449 516
	}
450
        DELAY(KBDC_DELAYTIME);
451
    }
452
    return -1;
517
	return -1;
453 518
}
454 519

  
455 520
/* wait for data from the aux device */
456 521
static int
457 522
wait_for_aux_data(struct atkbdc_softc *kbdc)
458 523
{
459
    /* CPU will stay inside the loop for 200msec at most */
460
    TOTALDELAY retry = { 200000, 0 };	/* 200ms */
461
    int f;
462
    unsigned char b;
463

  
464
    while ((f = read_status(kbdc) & KBDS_BUFFER_FULL)
465
	    != KBDS_AUX_BUFFER_FULL) {
466
        if (f == KBDS_KBD_BUFFER_FULL) {
467
	    DELAY(KBDD_DELAYTIME);
468
	    b = read_data(kbdc);
469
	    addq(&kbdc->kbd, b);
524
	/* CPU will stay inside the loop for 200msec at most */
525
	TOTALDELAY retry = { 200000, 0 };	/* 200ms */
526
	int f;
527
	unsigned char b;
528

  
529
	while ((f = read_status(kbdc) & KBDS_BUFFER_FULL)
530
		!= KBDS_AUX_BUFFER_FULL) {
531
		if (f == KBDS_KBD_BUFFER_FULL) {
532
			DELAY(KBDD_DELAYTIME);
533
			b = read_data(kbdc);
534
			addq(&kbdc->kbd, b);
535
		}
536
		DELAY(KBDC_DELAYTIME);
537
		if (CHECKTIMEOUT(&retry))
538
			return 0;
470 539
	}
471
        DELAY(KBDC_DELAYTIME);
472
	if (CHECKTIMEOUT(&retry))
473
    	    return 0;
474
    }
475
    DELAY(KBDD_DELAYTIME);
476
    return f;
540
	DELAY(KBDD_DELAYTIME);
541
	return f;
477 542
}
478 543

  
479 544
/* 
......
483 548
static int
484 549
wait_for_aux_ack(struct atkbdc_softc *kbdc)
485 550
{
486
    /* CPU will stay inside the loop for 200msec at most */
487
    TOTALDELAY retry = { 200000, 0 };	/* 200ms */
488
    int f;
489
    int b;
490

  
491
    while (CHECKTIMEOUT(&retry) == 0) {
492
        if ((f = read_status(kbdc)) & KBDS_ANY_BUFFER_FULL) {
493
	    DELAY(KBDD_DELAYTIME);
494
            b = read_data(kbdc);
495
	    if ((f & KBDS_BUFFER_FULL) == KBDS_AUX_BUFFER_FULL) {
496
		if ((b == PSM_ACK) || (b == PSM_RESEND) 
497
		    || (b == PSM_RESET_FAIL))
498
		    return b;
499
		addq(&kbdc->aux, b);
500
	    } else if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL) {
501
		addq(&kbdc->kbd, b);
502
	    }
551
	/* CPU will stay inside the loop for 200msec at most */
552
	TOTALDELAY retry = { 200000, 0 };	/* 200ms */
553
	int f;
554
	int b;
555

  
556
	while (CHECKTIMEOUT(&retry) == 0) {
557
		if ((f = read_status(kbdc)) & KBDS_ANY_BUFFER_FULL) {
558
			DELAY(KBDD_DELAYTIME);
559
			b = read_data(kbdc);
560
			if ((f & KBDS_BUFFER_FULL) == KBDS_AUX_BUFFER_FULL) {
561
				if ((b == PSM_ACK) || (b == PSM_RESEND) 
562
				|| (b == PSM_RESET_FAIL))
563
					return b;
564
				addq(&kbdc->aux, b);
565
		} else if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL) {
566
			addq(&kbdc->kbd, b);
567
		}
568
	}
569
		DELAY(KBDC_DELAYTIME);
503 570
	}
504
        DELAY(KBDC_DELAYTIME);
505
    }
506
    return -1;
571
	return -1;
507 572
}
508 573

  
509 574
/*
......
512 577
int
513 578
write_controller_w1r1(KBDC p, int c, int d)
514 579
{
515
    if (!write_controller_command(p, c))
516
	return(-1);
517
    if (!write_controller_data(p, d))
518
	return(-1);
519
    return (read_controller_data(p));
580
	if (!write_controller_command(p, c))
581
		return(-1);
582
	if (!write_controller_data(p, d))
583
		return(-1);
584
	return (read_controller_data(p));
520 585
}
521 586

  
522 587
/* write a one byte command to the controller */
523 588
int
524 589
write_controller_command(KBDC p, int c)
525 590
{
526
    if (!wait_while_controller_busy(kbdcp(p)))
527
	return FALSE;
528
    write_command(kbdcp(p), c);
529
    return TRUE;
591
	if (!wait_while_controller_busy(kbdcp(p)))
592
		return FALSE;
593
	write_command(kbdcp(p), c);
594
	return TRUE;
530 595
}
531 596

  
532 597
/* write a one byte data to the controller */
533 598
int
534 599
write_controller_data(KBDC p, int c)
535 600
{
536
    if (!wait_while_controller_busy(kbdcp(p)))
537
	return FALSE;
538
    write_data(kbdcp(p), c);
539
    return TRUE;
601
	if (!wait_while_controller_busy(kbdcp(p)))
602
		return FALSE;
603
	write_data(kbdcp(p), c);
604
	return TRUE;
540 605
}
541 606

  
542 607
/* write a one byte keyboard command */
543 608
int
544 609
write_kbd_command(KBDC p, int c)
545 610
{
546
    if (!wait_while_controller_busy(kbdcp(p)))
547
	return FALSE;
548
    write_data(kbdcp(p), c);
549
    return TRUE;
611
	if (!wait_while_controller_busy(kbdcp(p)))
612
		return FALSE;
613
	write_data(kbdcp(p), c);
614
	return TRUE;
550 615
}
551 616

  
552 617
/* write a one byte auxiliary device command */
553 618
int
554 619
write_aux_command(KBDC p, int c)
555 620
{
556
    if (!write_controller_command(p, KBDC_WRITE_TO_AUX))
557
	return FALSE;
558
    return write_controller_data(p, c);
621
	int f;
622

  
623
	f = aux_mux_is_enabled(p) ?
624
		KBDC_WRITE_TO_AUX_MUX + kbdcp(p)->aux_mux_port :
625
		KBDC_WRITE_TO_AUX;
626

  
627

  
628
	if (!write_controller_command(p, f))
629
		return FALSE;
630
	return write_controller_data(p, c);
559 631
}
560 632

  
561 633
/* send a command to the keyboard and wait for ACK */
562 634
int
563 635
send_kbd_command(KBDC p, int c)
564 636
{
565
    int retry = KBD_MAXRETRY;
566
    int res = -1;
567

  
568
    while (retry-- > 0) {
569
	if (!write_kbd_command(p, c))
570
	    continue;
571
        res = wait_for_kbd_ack(kbdcp(p));
572
        if (res == KBD_ACK)
573
    	    break;
574
    }
575
    return res;
637
	int retry = KBD_MAXRETRY;
638
	int res = -1;
639

  
640
	while (retry-- > 0) {
641
		if (!write_kbd_command(p, c))
642
			continue;
643
		res = wait_for_kbd_ack(kbdcp(p));
644
		if (res == KBD_ACK)
645
			break;
646
	}
647
	return res;
576 648
}
577 649

  
578 650
/* send a command to the auxiliary device and wait for ACK */
579 651
int
580 652
send_aux_command(KBDC p, int c)
581 653
{
582
    int retry = KBD_MAXRETRY;
583
    int res = -1;
654
	int retry = KBD_MAXRETRY;
655
	int res = -1;
584 656

  
585
    while (retry-- > 0) {
586
	if (!write_aux_command(p, c))
587
	    continue;
588
	/*
589
	 * FIXME: XXX
590
	 * The aux device may have already sent one or two bytes of 
591
	 * status data, when a command is received. It will immediately 
592
	 * stop data transmission, thus, leaving an incomplete data 
593
	 * packet in our buffer. We have to discard any unprocessed
594
	 * data in order to remove such packets. Well, we may remove 
595
	 * unprocessed, but necessary data byte as well... 
596
	 */
597
	emptyq(&kbdcp(p)->aux);
598
        res = wait_for_aux_ack(kbdcp(p));
599
        if (res == PSM_ACK)
600
    	    break;
601
    }
602
    return res;
657
	while (retry-- > 0) {
658
		if (!write_aux_command(p, c))
659
			continue;
660
		/*
661
		 * FIXME: XXX
662
		 * The aux device may have already sent one or two bytes of 
663
		 * status data, when a command is received. It will immediately 
664
		 * stop data transmission, thus, leaving an incomplete data 
665
		 * packet in our buffer. We have to discard any unprocessed
666
		 * data in order to remove such packets. Well, we may remove 
667
		 * unprocessed, but necessary data byte as well... 
668
		 */
669
		emptyq(&kbdcp(p)->aux);
670
		res = wait_for_aux_ack(kbdcp(p));
671
		if (res == PSM_ACK)
672
			break;
673
	}
674
	return res;
603 675
}
604 676

  
605 677
/* send a command and a data to the keyboard, wait for ACKs */
606 678
int
607 679
send_kbd_command_and_data(KBDC p, int c, int d)
608 680
{
609
    int retry;
610
    int res = -1;
611

  
612
    for (retry = KBD_MAXRETRY; retry > 0; --retry) {
613
	if (!write_kbd_command(p, c))
614
	    continue;
615
        res = wait_for_kbd_ack(kbdcp(p));
616
        if (res == KBD_ACK)
617
    	    break;
618
        else if (res != KBD_RESEND)
619
    	    return res;
620
    }
621
    if (retry <= 0)
681
	int retry;
682
	int res = -1;
683

  
684
	for (retry = KBD_MAXRETRY; retry > 0; --retry) {
685
		if (!write_kbd_command(p, c))
686
			continue;
687
		res = wait_for_kbd_ack(kbdcp(p));
688
		if (res == KBD_ACK)
689
			break;
690
		else if (res != KBD_RESEND)
691
			return res;
692
	}
693
	if (retry <= 0)
694
		return res;
695

  
696
	for (retry = KBD_MAXRETRY, res = -1; retry > 0; --retry) {
697
		if (!write_kbd_command(p, d))
698
			continue;
699
		res = wait_for_kbd_ack(kbdcp(p));
700
		if (res != KBD_RESEND)
701
			break;
702
	}
622 703
	return res;
623

  
624
    for (retry = KBD_MAXRETRY, res = -1; retry > 0; --retry) {
625
	if (!write_kbd_command(p, d))
626
	    continue;
627
        res = wait_for_kbd_ack(kbdcp(p));
628
        if (res != KBD_RESEND)
629
    	    break;
630
    }
631
    return res;
632 704
}
633 705

  
634 706
/* send a command and a data to the auxiliary device, wait for ACKs */
635 707
int
636 708
send_aux_command_and_data(KBDC p, int c, int d)
637 709
{
638
    int retry;
639
    int res = -1;
640

  
641
    for (retry = KBD_MAXRETRY; retry > 0; --retry) {
642
	if (!write_aux_command(p, c))
643
	    continue;
644
	emptyq(&kbdcp(p)->aux);
645
        res = wait_for_aux_ack(kbdcp(p));
646
        if (res == PSM_ACK)
647
    	    break;
648
        else if (res != PSM_RESEND)
649
    	    return res;
650
    }
651
    if (retry <= 0)
710
	int retry;
711
	int res = -1;
712

  
713
	for (retry = KBD_MAXRETRY; retry > 0; --retry) {
714
		if (!write_aux_command(p, c))
715
			continue;
716
		emptyq(&kbdcp(p)->aux);
717
        	res = wait_for_aux_ack(kbdcp(p));
718
		if (res == PSM_ACK)
719
			break;
720
		else if (res != PSM_RESEND)
721
			return res;
722
	}
723
	if (retry <= 0)
724
		return res;
725

  
726
	for (retry = KBD_MAXRETRY, res = -1; retry > 0; --retry) {
727
		if (!write_aux_command(p, d))
728
			continue;
729
		res = wait_for_aux_ack(kbdcp(p));
730
		if (res != PSM_RESEND)
731
			break;
732
	}
652 733
	return res;
653

  
654
    for (retry = KBD_MAXRETRY, res = -1; retry > 0; --retry) {
655
	if (!write_aux_command(p, d))
656
	    continue;
657
        res = wait_for_aux_ack(kbdcp(p));
658
        if (res != PSM_RESEND)
659
    	    break;
660
    }
661
    return res;
662 734
}
663 735

  
664 736
/* 
......
668 740
int
669 741
read_controller_data(KBDC p)
670 742
{
671
    if (availq(&kbdcp(p)->kbd)) 
672
        return removeq(&kbdcp(p)->kbd);
673
    if (availq(&kbdcp(p)->aux)) 
674
        return removeq(&kbdcp(p)->aux);
675
    if (!wait_for_data(kbdcp(p)))
676
        return -1;		/* timeout */
677
    return read_data(kbdcp(p));
743
	if (availq(&kbdcp(p)->kbd)) 
744
		return removeq(&kbdcp(p)->kbd);
745
	if (availq(&kbdcp(p)->aux)) 
746
        	return removeq(&kbdcp(p)->aux);
747
	if (!wait_for_data(kbdcp(p)))
748
        	return -1;		/* timeout */
749
	return read_data(kbdcp(p));
678 750
}
679 751

  
680 752
#if KBDIO_DEBUG >= 2
......
685 757
int
686 758
read_kbd_data(KBDC p)
687 759
{
688
    unsigned char b;
760
	unsigned char b;
689 761

  
690 762
#if KBDIO_DEBUG >= 2
691
    if (++call > 2000) {
692
	call = 0;
693
	log(LOG_DEBUG, "kbdc: kbd q: %d calls, max %d chars, "
694
			     "aux q: %d calls, max %d chars\n",
695
		       kbdcp(p)->kbd.call_count, kbdcp(p)->kbd.max_qcount,
696
		       kbdcp(p)->aux.call_count, kbdcp(p)->aux.max_qcount);
697
    }
763
	if (++call > 2000) {
764
		call = 0;
765
		log(LOG_DEBUG, "kbdc: kbd q: %d calls, max %d chars, "
766
			"aux q: %d calls, max %d chars\n",
767
			kbdcp(p)->kbd.call_count, kbdcp(p)->kbd.max_qcount,
768
			kbdcp(p)->aux.call_count, kbdcp(p)->aux.max_qcount);
769
	}
698 770
#endif
699 771

  
700
    if (availq(&kbdcp(p)->kbd)) 
701
        return removeq(&kbdcp(p)->kbd);
702
    if (!wait_for_kbd_data(kbdcp(p)))
703
        return -1;		/* timeout */
704
    b = read_data(kbdcp(p));
705
    return b;
772
	if (availq(&kbdcp(p)->kbd)) 
773
		return removeq(&kbdcp(p)->kbd);
774
	if (!wait_for_kbd_data(kbdcp(p)))
775
		return -1;		/* timeout */
776
	b = read_data(kbdcp(p));
777
	return b;
706 778
}
707 779

  
708 780
/* read one byte from the keyboard, but return immediately if 
......
711 783
int
712 784
read_kbd_data_no_wait(KBDC p)
713 785
{
714
    int f;
715
    unsigned char b;
786
	int f;
787
	unsigned char b;
716 788

  
717 789
#if KBDIO_DEBUG >= 2
718
    if (++call > 2000) {
719
	call = 0;
720
	log(LOG_DEBUG, "kbdc: kbd q: %d calls, max %d chars, "
721
			     "aux q: %d calls, max %d chars\n",
722
		       kbdcp(p)->kbd.call_count, kbdcp(p)->kbd.max_qcount,
723
		       kbdcp(p)->aux.call_count, kbdcp(p)->aux.max_qcount);
724
    }
790
	if (++call > 2000) {
791
		call = 0;
792
		log(LOG_DEBUG, "kbdc: kbd q: %d calls, max %d chars, "
793
			"aux q: %d calls, max %d chars\n",
794
			kbdcp(p)->kbd.call_count, kbdcp(p)->kbd.max_qcount,
795
			kbdcp(p)->aux.call_count, kbdcp(p)->aux.max_qcount);
796
	}
725 797
#endif
726 798

  
727
    if (availq(&kbdcp(p)->kbd)) 
728
        return removeq(&kbdcp(p)->kbd);
729
    f = read_status(kbdcp(p)) & KBDS_BUFFER_FULL;
730
    while (f == KBDS_AUX_BUFFER_FULL) {
731
        DELAY(KBDD_DELAYTIME);
732
	b = read_data(kbdcp(p));
733
        addq(&kbdcp(p)->aux, b);
734
        f = read_status(kbdcp(p)) & KBDS_BUFFER_FULL;
735
    }
736
    if (f == KBDS_KBD_BUFFER_FULL) {
737
        DELAY(KBDD_DELAYTIME);
799
	if (availq(&kbdcp(p)->kbd)) 
800
		return removeq(&kbdcp(p)->kbd);
801
	f = read_status(kbdcp(p)) & KBDS_BUFFER_FULL;
802
	while (f == KBDS_AUX_BUFFER_FULL) {
803
		DELAY(KBDD_DELAYTIME);
804
		b = read_data(kbdcp(p));
805
        	addq(&kbdcp(p)->aux, b);
806
		f = read_status(kbdcp(p)) & KBDS_BUFFER_FULL;
807
	}
808
	if (f == KBDS_KBD_BUFFER_FULL) {
809
		DELAY(KBDD_DELAYTIME);
738 810
	b = read_data(kbdcp(p));
739
        return (int)b;
740
    }
741
    return -1;		/* no data */
811
	return (int)b;
... This diff was truncated because it exceeds the maximum size that can be displayed.
    (1-1/1)