Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  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
/* $NetBSD: pckbc.c,v 1.65 2022/11/17 23:57:20 riastradh Exp $ */

/*
 * Copyright (c) 2004 Ben Harris.
 * Copyright (c) 1998
 *	Matthias Drochner.  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.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 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.
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: pckbc.c,v 1.65 2022/11/17 23:57:20 riastradh Exp $");

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/callout.h>
#include <sys/kernel.h>
#include <sys/proc.h>
#include <sys/device.h>
#include <sys/malloc.h>
#include <sys/errno.h>
#include <sys/queue.h>

#include <sys/bus.h>

#include <dev/ic/i8042reg.h>
#include <dev/ic/pckbcvar.h>

#include <dev/pckbport/pckbportvar.h>

#include "locators.h"

#include <sys/rndsource.h>

/* data per slave device */
struct pckbc_slotdata {
	int polling;	/* don't process data in interrupt handler */
	int poll_data;	/* data read from inr handler if polling */
	int poll_stat;	/* status read from inr handler if polling */
	krndsource_t	rnd_source;
};

static void pckbc_init_slotdata(struct pckbc_slotdata *);
static int pckbc_attach_slot(struct pckbc_softc *, pckbc_slot_t);

struct pckbc_internal pckbc_consdata;
int pckbc_console_attached;

static int pckbc_console;
static struct pckbc_slotdata pckbc_cons_slotdata;

static int pckbc_xt_translation(void *, pckbport_slot_t, int);
static int pckbc_send_devcmd(void *, pckbport_slot_t, u_char);
static void pckbc_slot_enable(void *, pckbport_slot_t, int);
static void pckbc_intr_establish(void *, pckbport_slot_t);
static void pckbc_set_poll(void *,	pckbc_slot_t, int on);

static int pckbc_wait_output(bus_space_tag_t, bus_space_handle_t);

static int pckbc_get8042cmd(struct pckbc_internal *);
static int pckbc_put8042cmd(struct pckbc_internal *);

void pckbc_cleanqueue(struct pckbc_slotdata *);
void pckbc_cleanup(void *);
int pckbc_cmdresponse(struct pckbc_internal *, pckbc_slot_t, u_char);
void pckbc_start(struct pckbc_internal *, pckbc_slot_t);

const char * const pckbc_slot_names[] = { "kbd", "aux" };

static const struct pckbport_accessops pckbc_ops = {
	.t_xt_translation = pckbc_xt_translation,
	.t_send_devcmd = pckbc_send_devcmd,
	.t_poll_data1 = pckbc_poll_data1,
	.t_slot_enable = pckbc_slot_enable,
	.t_intr_establish = pckbc_intr_establish,
	.t_set_poll = pckbc_set_poll,
};

#define	KBD_DELAY	DELAY(8)

static inline int
pckbc_wait_output(bus_space_tag_t iot, bus_space_handle_t ioh_c)
{
	u_int i;

	for (i = 100000; i; i--)
		if (!(bus_space_read_1(iot, ioh_c, 0) & KBS_IBF)) {
			KBD_DELAY;
			return (1);
		}
	return (0);
}

int
pckbc_send_cmd(bus_space_tag_t iot, bus_space_handle_t ioh_c, u_char val)
{
	if (!pckbc_wait_output(iot, ioh_c))
		return (0);
	bus_space_write_1(iot, ioh_c, 0, val);
	return (1);
}

/*
 * Note: the spl games here are to deal with some strange PC kbd controllers
 * in some system configurations.
 * This is not canonical way to handle polling input.
 */
int
pckbc_poll_data1(void *pt, pckbc_slot_t slot)
{
	struct pckbc_internal *t = pt;
	struct pckbc_slotdata *q = t->t_slotdata[slot];
	int s;
	u_char stat, c;
	int i = 100; /* polls for ~100ms */
	int checkaux = t->t_haveaux;

	s = splhigh();

	if (q && q->polling && q->poll_data != -1 && q->poll_stat != -1) {
		stat	= q->poll_stat;
		c	= q->poll_data;
		q->poll_data = -1;
		q->poll_stat = -1;
		goto process;
	}

	for (; i; i--, delay(1000)) {
		stat = bus_space_read_1(t->t_iot, t->t_ioh_c, 0);
		if (stat & KBS_DIB) {
			c = bus_space_read_1(t->t_iot, t->t_ioh_d, 0);

		    process:
			if (checkaux && (stat & 0x20)) { /* aux data */
				if (slot != PCKBC_AUX_SLOT) {
#ifdef PCKBCDEBUG
					printf("pckbc: lost aux 0x%x\n", c);
#endif
					continue;
				}
			} else {
				if (slot == PCKBC_AUX_SLOT) {
#ifdef PCKBCDEBUG
					printf("pckbc: lost kbd 0x%x\n", c);
#endif
					continue;
				}
			}
			splx(s);
			return (c);
		}
	}

	splx(s);
	return (-1);
}

/*
 * Get the current command byte.
 */
static int
pckbc_get8042cmd(struct pckbc_internal *t)
{
	bus_space_tag_t iot = t->t_iot;
	bus_space_handle_t ioh_c = t->t_ioh_c;
	int data;

	if (!pckbc_send_cmd(iot, ioh_c, K_RDCMDBYTE))
		return (0);
	data = pckbc_poll_data1(t, PCKBC_KBD_SLOT);
	if (data == -1)
		return (0);
	t->t_cmdbyte = data;
	return (1);
}

/*
 * Pass command byte to keyboard controller (8042).
 */
static int
pckbc_put8042cmd(struct pckbc_internal *t)
{
	bus_space_tag_t iot = t->t_iot;
	bus_space_handle_t ioh_d = t->t_ioh_d;
	bus_space_handle_t ioh_c = t->t_ioh_c;

	if (!pckbc_send_cmd(iot, ioh_c, K_LDCMDBYTE))
		return (0);
	if (!pckbc_wait_output(iot, ioh_c))
		return (0);
	bus_space_write_1(iot, ioh_d, 0, t->t_cmdbyte);
	return (1);
}

static int
pckbc_send_devcmd(void *pt, pckbc_slot_t slot, u_char val)
{
	struct pckbc_internal *t = pt;
	bus_space_tag_t iot = t->t_iot;
	bus_space_handle_t ioh_d = t->t_ioh_d;
	bus_space_handle_t ioh_c = t->t_ioh_c;

	if (slot == PCKBC_AUX_SLOT) {
		if (!pckbc_send_cmd(iot, ioh_c, KBC_AUXWRITE))
			return (0);
	}
	if (!pckbc_wait_output(iot, ioh_c))
		return (0);
	bus_space_write_1(iot, ioh_d, 0, val);
	return (1);
}

int
pckbc_is_console(bus_space_tag_t iot, bus_addr_t addr)
{
	if (pckbc_console && !pckbc_console_attached &&
	    bus_space_is_equal(pckbc_consdata.t_iot, iot) &&
	    pckbc_consdata.t_addr == addr)
		return (1);
	return (0);
}

static int
pckbc_attach_slot(struct pckbc_softc *sc, pckbc_slot_t slot)
{
	struct pckbc_internal *t = sc->id;
	void *sdata;
	device_t child;
	int alloced = 0;

	if (t->t_slotdata[slot] == NULL) {
		sdata = malloc(sizeof(struct pckbc_slotdata),
		    M_DEVBUF, M_WAITOK);
		t->t_slotdata[slot] = sdata;
		pckbc_init_slotdata(t->t_slotdata[slot]);
		alloced++;
	}

	child = pckbport_attach_slot(sc->sc_dv, t->t_pt, slot);

	if (child == NULL && alloced) {
		free(t->t_slotdata[slot], M_DEVBUF);
		t->t_slotdata[slot] = NULL;
	}

	if (child != NULL && t->t_slotdata[slot] != NULL) {
		memset(&t->t_slotdata[slot]->rnd_source, 0,
		    sizeof(t->t_slotdata[slot]->rnd_source));
		rnd_attach_source(&t->t_slotdata[slot]->rnd_source,
		    device_xname(child), RND_TYPE_TTY, RND_FLAG_DEFAULT);
	}

	return child != NULL;
}

void
pckbc_attach(struct pckbc_softc *sc)
{
	struct pckbc_internal *t;
	bus_space_tag_t iot;
	bus_space_handle_t ioh_d, ioh_c;
	int res;
	u_char cmdbits = 0;

	t = sc->id;
	iot = t->t_iot;
	ioh_d = t->t_ioh_d;
	ioh_c = t->t_ioh_c;

	t->t_pt = pckbport_attach(t, &pckbc_ops);
	if (t->t_pt == NULL) {
		aprint_error(": attach failed\n");
		return;
	}

	/* flush */
	(void) pckbc_poll_data1(t, PCKBC_KBD_SLOT);

	/* set initial cmd byte */
	if (!pckbc_put8042cmd(t)) {
		aprint_error("pckbc: cmd word write error\n");
		return;
	}

/*
 * XXX Don't check the keyboard port. There are broken keyboard controllers
 * which don't pass the test but work normally otherwise.
 */
#if 0
	/*
	 * check kbd port ok
	 */
	if (!pckbc_send_cmd(iot, ioh_c, KBC_KBDTEST))
		return;
	res = pckbc_poll_data1(t, PCKBC_KBD_SLOT);

	/*
	 * Normally, we should get a "0" here.
	 * But there are keyboard controllers behaving differently.
	 */
	if (!(res == 0 || res == 0xfa || res == 0x01 || res == 0xab)) {
		printf("pckbc: kbd port test: %x\n", res);
		return;
	}
#ifdef PCKBCDEBUG
	if (res != 0)
		printf("pckbc: returned %x on kbd slot test\n", res);
#endif
#endif /* 0 */
	if (pckbc_attach_slot(sc, PCKBC_KBD_SLOT))
		cmdbits |= KC8_KENABLE;

	/*
	 * Check aux port ok.
	 * Avoid KBC_AUXTEST because it hangs some older controllers
	 *  (eg UMC880?).
	 */
	if (!pckbc_send_cmd(iot, ioh_c, KBC_AUXECHO)) {
		aprint_error("pckbc: aux echo error 1\n");
		goto nomouse;
	}
	if (!pckbc_wait_output(iot, ioh_c)) {
		aprint_error("pckbc: aux echo error 2\n");
		goto nomouse;
	}
	t->t_haveaux = 1;
	bus_space_write_1(iot, ioh_d, 0, 0x5a); /* a random value */
	res = pckbc_poll_data1(t, PCKBC_AUX_SLOT);

	/*
	 * The following is needed to find the aux port on the Tadpole
	 * SPARCle.
	 */
	if (res == -1 && ISSET(t->t_flags, PCKBC_NEED_AUXWRITE)) {
		/* Read of aux echo timed out, try again */
		if (!pckbc_send_cmd(iot, ioh_c, KBC_AUXWRITE))
			goto nomouse;
		if (!pckbc_wait_output(iot, ioh_c))
			goto nomouse;
		bus_space_write_1(iot, ioh_d, 0, 0x5a);
		res = pckbc_poll_data1(t, PCKBC_AUX_SLOT);
	}
	if (res != -1) {
		/*
		 * In most cases, the 0x5a gets echoed.
		 * Some older controllers (Gateway 2000 circa 1993)
		 * return 0xfe here.
		 * We are satisfied if there is anything in the
		 * aux output buffer.
		 */
		if (pckbc_attach_slot(sc, PCKBC_AUX_SLOT))
			cmdbits |= KC8_MENABLE;
	} else {

#ifdef PCKBCDEBUG
		printf("pckbc: aux echo test failed\n");
#endif
		t->t_haveaux = 0;
	}

nomouse:
	/* enable needed interrupts */
	t->t_cmdbyte |= cmdbits;
	if (!pckbc_put8042cmd(t))
		aprint_error("pckbc: cmd word write error\n");
}

static void
pckbc_init_slotdata(struct pckbc_slotdata *q)
{

	q->polling = 0;
}

/*
 * switch scancode translation on / off
 * return nonzero on success
 */
static int
pckbc_xt_translation(void *self, pckbc_slot_t slot, int on)
{
	struct pckbc_internal *t = self;
	int ison;

	if (ISSET(t->t_flags, PCKBC_CANT_TRANSLATE))
		return (-1);

	if (slot != PCKBC_KBD_SLOT) {
		/* translation only for kbd slot */
		if (on)
			return (0);
		else
			return (1);
	}

	ison = t->t_cmdbyte & KC8_TRANS;
	if ((on && ison) || (!on && !ison))
		return (1);

	t->t_cmdbyte ^= KC8_TRANS;
	if (!pckbc_put8042cmd(t))
		return (0);

	/* read back to be sure */
	if (!pckbc_get8042cmd(t))
		return (0);

	ison = t->t_cmdbyte & KC8_TRANS;
	if ((on && ison) || (!on && !ison))
		return (1);
	return (0);
}

static const struct pckbc_portcmd {
	u_char cmd_en, cmd_dis;
} pckbc_portcmd[2] = {
	{
		KBC_KBDENABLE, KBC_KBDDISABLE,
	}, {
		KBC_AUXENABLE, KBC_AUXDISABLE,
	}
};

void
pckbc_slot_enable(void *self, pckbc_slot_t slot, int on)
{
	struct pckbc_internal *t = (struct pckbc_internal *)self;
	const struct pckbc_portcmd *cmd;

	cmd = &pckbc_portcmd[slot];

	if (!pckbc_send_cmd(t->t_iot, t->t_ioh_c,
			    on ? cmd->cmd_en : cmd->cmd_dis))
		printf("pckbc: pckbc_slot_enable(%d) failed\n", on);
}

static void
pckbc_set_poll(void *self, pckbc_slot_t slot, int on)
{
	struct pckbc_internal *t = (struct pckbc_internal *)self;

	t->t_slotdata[slot]->polling = on;

	if (on) {
		t->t_slotdata[slot]->poll_data = -1;
		t->t_slotdata[slot]->poll_stat = -1;
	} else {
		int s;

		/*
		 * If disabling polling on a device that's been configured,
		 * make sure there are no bytes left in the FIFO, holding up
		 * the interrupt line.  Otherwise we won't get any further
		 * interrupts.
		 */
		if (t->t_sc) {
			s = spltty();
			pckbcintr(t->t_sc);
			splx(s);
		}
	}
}

static void
pckbc_intr_establish(void *pt, pckbport_slot_t slot)
{
	struct pckbc_internal *t = pt;

	(*t->t_sc->intr_establish)(t->t_sc, slot);
}

int
pckbcintr_hard(void *vsc)
{
	struct pckbc_softc *sc = (struct pckbc_softc *)vsc;
	struct pckbc_internal *t = sc->id;
	u_char stat;
	pckbc_slot_t slot;
	struct pckbc_slotdata *q;
	int served = 0, data, next, s;

	for(;;) {
		stat = bus_space_read_1(t->t_iot, t->t_ioh_c, 0);
		if (!(stat & KBS_DIB))
			break;

		served = 1;

		slot = (t->t_haveaux && (stat & 0x20)) ?
		    PCKBC_AUX_SLOT : PCKBC_KBD_SLOT;
		q = t->t_slotdata[slot];

		if (!q) {
			/* XXX do something for live insertion? */
			printf("pckbc: no dev for slot %d\n", slot);
			(void) bus_space_read_1(t->t_iot, t->t_ioh_d, 0);
			continue;
		}

		data = bus_space_read_1(t->t_iot, t->t_ioh_d, 0);

		rnd_add_uint32(&q->rnd_source, (stat<<8)|data);

		if (q->polling) {
			q->poll_data = data;
			q->poll_stat = stat;
			break; /* pckbc_poll_data() will get it */
		}

#if 0 /* XXXBJH */
		if (CMD_IN_QUEUE(q) && pckbc_cmdresponse(t, slot, data))
			continue;
#endif

		s = splhigh();
		next = (t->rbuf_write+1) % PCKBC_RBUF_SIZE;
		if (next == t->rbuf_read) {
			splx(s);
			break;
		}
		t->rbuf[t->rbuf_write].data = data;
		t->rbuf[t->rbuf_write].slot = slot;
		t->rbuf_write = next;
		splx(s);
	}

	return (served);
}

void
pckbcintr_soft(void *vsc)
{
	struct pckbc_softc *sc = vsc;
	struct pckbc_internal *t = sc->id;
	int data, slot, s;
#ifndef __GENERIC_SOFT_INTERRUPTS_ALL_LEVELS
	int st;

	st = spltty();
#endif

	s = splhigh();
	while (t->rbuf_read != t->rbuf_write) {
		slot = t->rbuf[t->rbuf_read].slot;
		data = t->rbuf[t->rbuf_read].data;
		t->rbuf_read = (t->rbuf_read+1) % PCKBC_RBUF_SIZE;
		splx(s);
		pckbportintr(t->t_pt, slot, data);
		s = splhigh();
	}
	splx(s);


#ifndef __GENERIC_SOFT_INTERRUPTS_ALL_LEVELS
	splx(st);
#endif
}

int
pckbcintr(void *vsc)
{
	struct pckbc_softc *sc = (struct pckbc_softc *)vsc;
	struct pckbc_internal *t = sc->id;
	u_char stat;
	pckbc_slot_t slot;
	struct pckbc_slotdata *q;
	int served = 0, data;

	for(;;) {
		stat = bus_space_read_1(t->t_iot, t->t_ioh_c, 0);
		if (!(stat & KBS_DIB))
			break;

		slot = (t->t_haveaux && (stat & 0x20)) ?
		    PCKBC_AUX_SLOT : PCKBC_KBD_SLOT;
		q = t->t_slotdata[slot];

		if (q != NULL && q->polling)
			return 0;

		served = 1;
		data = bus_space_read_1(t->t_iot, t->t_ioh_d, 0);

		if (q != NULL)
			rnd_add_uint32(&q->rnd_source, (stat<<8)|data);

		pckbportintr(t->t_pt, slot, data);
	}

	return (served);
}

int
pckbc_cnattach(bus_space_tag_t iot, bus_addr_t addr,
	bus_size_t cmd_offset, pckbc_slot_t slot, int flags)
{
	bus_space_handle_t ioh_d, ioh_c;
#ifdef PCKBC_CNATTACH_SELFTEST
	int reply;
#endif
	int res = 0;

	if (bus_space_map(iot, addr + KBDATAP, 1, 0, &ioh_d))
		return (ENXIO);
	if (bus_space_map(iot, addr + cmd_offset, 1, 0, &ioh_c)) {
		bus_space_unmap(iot, ioh_d, 1);
		return (ENXIO);
	}

	memset(&pckbc_consdata, 0, sizeof(pckbc_consdata));
	pckbc_consdata.t_iot = iot;
	pckbc_consdata.t_ioh_d = ioh_d;
	pckbc_consdata.t_ioh_c = ioh_c;
	pckbc_consdata.t_addr = addr;
	pckbc_consdata.t_flags = flags;
	callout_init(&pckbc_consdata.t_cleanup, 0);

	/* flush */
	(void) pckbc_poll_data1(&pckbc_consdata, PCKBC_KBD_SLOT);

#ifdef PCKBC_CNATTACH_SELFTEST
	/*
	 * In some machines (e.g. netwinder) pckbc refuses to talk at
	 * all until we request a self-test.
	 */
	if (!pckbc_send_cmd(iot, ioh_c, KBC_SELFTEST)) {
		printf("pckbc: unable to request selftest\n");
		res = EIO;
		goto out;
	}

	reply = pckbc_poll_data1(&pckbc_consdata, PCKBC_KBD_SLOT);
	if (reply != 0x55) {
		printf("pckbc: selftest returned 0x%02x\n", reply);
		res = EIO;
		goto out;
	}
#endif /* PCKBC_CNATTACH_SELFTEST */

	/* init cmd byte, enable ports */
	pckbc_consdata.t_cmdbyte = KC8_CPU;
	if (!pckbc_put8042cmd(&pckbc_consdata)) {
		printf("pckbc: cmd word write error\n");
		res = EIO;
		goto out;
	}

	res = pckbport_cnattach(&pckbc_consdata, &pckbc_ops, slot);

  out:
	if (res) {
		bus_space_unmap(iot, pckbc_consdata.t_ioh_d, 1);
		bus_space_unmap(iot, pckbc_consdata.t_ioh_c, 1);
	} else {
		pckbc_consdata.t_slotdata[slot] = &pckbc_cons_slotdata;
		pckbc_init_slotdata(&pckbc_cons_slotdata);
		pckbc_console = 1;
	}

	return (res);
}

bool
pckbc_resume(device_t dv, const pmf_qual_t *qual)
{
	struct pckbc_softc *sc = device_private(dv);
	struct pckbc_internal *t;

	t = sc->id;
	(void)pckbc_poll_data1(t, PCKBC_KBD_SLOT);
	if (!pckbc_send_cmd(t->t_iot, t->t_ioh_c, KBC_SELFTEST))
		return false;
	(void)pckbc_poll_data1(t, PCKBC_KBD_SLOT);
	(void)pckbc_put8042cmd(t);
	pckbcintr(t->t_sc);

	return true;
}