Home | History | Annotate | Line # | Download | only in ic
pckbc.c revision 1.51.8.2
      1 /* $NetBSD: pckbc.c,v 1.51.8.2 2012/10/30 17:21:07 yamt Exp $ */
      2 
      3 /*
      4  * Copyright (c) 2004 Ben Harris.
      5  * Copyright (c) 1998
      6  *	Matthias Drochner.  All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  *
     17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 #include <sys/cdefs.h>
     30 __KERNEL_RCSID(0, "$NetBSD: pckbc.c,v 1.51.8.2 2012/10/30 17:21:07 yamt Exp $");
     31 
     32 #include <sys/param.h>
     33 #include <sys/systm.h>
     34 #include <sys/callout.h>
     35 #include <sys/kernel.h>
     36 #include <sys/proc.h>
     37 #include <sys/device.h>
     38 #include <sys/malloc.h>
     39 #include <sys/errno.h>
     40 #include <sys/queue.h>
     41 
     42 #include <sys/bus.h>
     43 
     44 #include <dev/ic/i8042reg.h>
     45 #include <dev/ic/pckbcvar.h>
     46 
     47 #include <dev/pckbport/pckbportvar.h>
     48 
     49 #include "locators.h"
     50 
     51 #include <sys/rnd.h>
     52 
     53 /* data per slave device */
     54 struct pckbc_slotdata {
     55 	int polling;	/* don't process data in interrupt handler */
     56 	int poll_data;	/* data read from inr handler if polling */
     57 	int poll_stat;	/* status read from inr handler if polling */
     58 	krndsource_t	rnd_source;
     59 };
     60 
     61 static void pckbc_init_slotdata(struct pckbc_slotdata *);
     62 static int pckbc_attach_slot(struct pckbc_softc *, pckbc_slot_t);
     63 
     64 struct pckbc_internal pckbc_consdata;
     65 int pckbc_console_attached;
     66 
     67 static int pckbc_console;
     68 static struct pckbc_slotdata pckbc_cons_slotdata;
     69 
     70 static int pckbc_xt_translation(void *, pckbport_slot_t, int);
     71 static int pckbc_send_devcmd(void *, pckbport_slot_t, u_char);
     72 static void pckbc_slot_enable(void *, pckbport_slot_t, int);
     73 static void pckbc_intr_establish(void *, pckbport_slot_t);
     74 static void pckbc_set_poll(void *,	pckbc_slot_t, int on);
     75 
     76 static int pckbc_wait_output(bus_space_tag_t, bus_space_handle_t);
     77 
     78 static int pckbc_get8042cmd(struct pckbc_internal *);
     79 static int pckbc_put8042cmd(struct pckbc_internal *);
     80 
     81 void pckbc_cleanqueue(struct pckbc_slotdata *);
     82 void pckbc_cleanup(void *);
     83 int pckbc_cmdresponse(struct pckbc_internal *, pckbc_slot_t, u_char);
     84 void pckbc_start(struct pckbc_internal *, pckbc_slot_t);
     85 
     86 const char * const pckbc_slot_names[] = { "kbd", "aux" };
     87 
     88 static struct pckbport_accessops const pckbc_ops = {
     89 	pckbc_xt_translation,
     90 	pckbc_send_devcmd,
     91 	pckbc_poll_data1,
     92 	pckbc_slot_enable,
     93 	pckbc_intr_establish,
     94 	pckbc_set_poll
     95 };
     96 
     97 #define	KBD_DELAY	DELAY(8)
     98 
     99 static inline int
    100 pckbc_wait_output(bus_space_tag_t iot, bus_space_handle_t ioh_c)
    101 {
    102 	u_int i;
    103 
    104 	for (i = 100000; i; i--)
    105 		if (!(bus_space_read_1(iot, ioh_c, 0) & KBS_IBF)) {
    106 			KBD_DELAY;
    107 			return (1);
    108 		}
    109 	return (0);
    110 }
    111 
    112 int
    113 pckbc_send_cmd(bus_space_tag_t iot, bus_space_handle_t ioh_c, u_char val)
    114 {
    115 	if (!pckbc_wait_output(iot, ioh_c))
    116 		return (0);
    117 	bus_space_write_1(iot, ioh_c, 0, val);
    118 	return (1);
    119 }
    120 
    121 /*
    122  * Note: the spl games here are to deal with some strange PC kbd controllers
    123  * in some system configurations.
    124  * This is not canonical way to handle polling input.
    125  */
    126 int
    127 pckbc_poll_data1(void *pt, pckbc_slot_t slot)
    128 {
    129 	struct pckbc_internal *t = pt;
    130 	struct pckbc_slotdata *q = t->t_slotdata[slot];
    131 	int s;
    132 	u_char stat, c;
    133 	int i = 100; /* polls for ~100ms */
    134 	int checkaux = t->t_haveaux;
    135 
    136 	s = splhigh();
    137 
    138 	if (q && q->polling && q->poll_data != -1 && q->poll_stat != -1) {
    139 		stat	= q->poll_stat;
    140 		c	= q->poll_data;
    141 		q->poll_data = -1;
    142 		q->poll_stat = -1;
    143 		goto process;
    144 	}
    145 
    146 	for (; i; i--, delay(1000)) {
    147 		stat = bus_space_read_1(t->t_iot, t->t_ioh_c, 0);
    148 		if (stat & KBS_DIB) {
    149 			KBD_DELAY;
    150 			c = bus_space_read_1(t->t_iot, t->t_ioh_d, 0);
    151 
    152 		    process:
    153 			if (checkaux && (stat & 0x20)) { /* aux data */
    154 				if (slot != PCKBC_AUX_SLOT) {
    155 #ifdef PCKBCDEBUG
    156 					printf("pckbc: lost aux 0x%x\n", c);
    157 #endif
    158 					continue;
    159 				}
    160 			} else {
    161 				if (slot == PCKBC_AUX_SLOT) {
    162 #ifdef PCKBCDEBUG
    163 					printf("pckbc: lost kbd 0x%x\n", c);
    164 #endif
    165 					continue;
    166 				}
    167 			}
    168 			splx(s);
    169 			return (c);
    170 		}
    171 	}
    172 
    173 	splx(s);
    174 	return (-1);
    175 }
    176 
    177 /*
    178  * Get the current command byte.
    179  */
    180 static int
    181 pckbc_get8042cmd(struct pckbc_internal *t)
    182 {
    183 	bus_space_tag_t iot = t->t_iot;
    184 	bus_space_handle_t ioh_c = t->t_ioh_c;
    185 	int data;
    186 
    187 	if (!pckbc_send_cmd(iot, ioh_c, K_RDCMDBYTE))
    188 		return (0);
    189 	data = pckbc_poll_data1(t, PCKBC_KBD_SLOT);
    190 	if (data == -1)
    191 		return (0);
    192 	t->t_cmdbyte = data;
    193 	return (1);
    194 }
    195 
    196 /*
    197  * Pass command byte to keyboard controller (8042).
    198  */
    199 static int
    200 pckbc_put8042cmd(struct pckbc_internal *t)
    201 {
    202 	bus_space_tag_t iot = t->t_iot;
    203 	bus_space_handle_t ioh_d = t->t_ioh_d;
    204 	bus_space_handle_t ioh_c = t->t_ioh_c;
    205 
    206 	if (!pckbc_send_cmd(iot, ioh_c, K_LDCMDBYTE))
    207 		return (0);
    208 	if (!pckbc_wait_output(iot, ioh_c))
    209 		return (0);
    210 	bus_space_write_1(iot, ioh_d, 0, t->t_cmdbyte);
    211 	return (1);
    212 }
    213 
    214 static int
    215 pckbc_send_devcmd(void *pt, pckbc_slot_t slot, u_char val)
    216 {
    217 	struct pckbc_internal *t = pt;
    218 	bus_space_tag_t iot = t->t_iot;
    219 	bus_space_handle_t ioh_d = t->t_ioh_d;
    220 	bus_space_handle_t ioh_c = t->t_ioh_c;
    221 
    222 	if (slot == PCKBC_AUX_SLOT) {
    223 		if (!pckbc_send_cmd(iot, ioh_c, KBC_AUXWRITE))
    224 			return (0);
    225 	}
    226 	if (!pckbc_wait_output(iot, ioh_c))
    227 		return (0);
    228 	bus_space_write_1(iot, ioh_d, 0, val);
    229 	return (1);
    230 }
    231 
    232 int
    233 pckbc_is_console(bus_space_tag_t iot, bus_addr_t addr)
    234 {
    235 	if (pckbc_console && !pckbc_console_attached &&
    236 	    bus_space_is_equal(pckbc_consdata.t_iot, iot) &&
    237 	    pckbc_consdata.t_addr == addr)
    238 		return (1);
    239 	return (0);
    240 }
    241 
    242 static int
    243 pckbc_attach_slot(struct pckbc_softc *sc, pckbc_slot_t slot)
    244 {
    245 	struct pckbc_internal *t = sc->id;
    246 	struct pckbc_attach_args pa;
    247 	void *sdata;
    248 	device_t child;
    249 	int alloced = 0;
    250 
    251 	pa.pa_tag = t;
    252 	pa.pa_slot = slot;
    253 
    254 	if (t->t_slotdata[slot] == NULL) {
    255 		sdata = malloc(sizeof(struct pckbc_slotdata),
    256 		    M_DEVBUF, M_NOWAIT);
    257 		if (sdata == NULL) {
    258 			aprint_error_dev(sc->sc_dv, "no memory\n");
    259 			return (0);
    260 		}
    261 		t->t_slotdata[slot] = sdata;
    262 		pckbc_init_slotdata(t->t_slotdata[slot]);
    263 		alloced++;
    264 	}
    265 
    266 	child = pckbport_attach_slot(sc->sc_dv, t->t_pt, slot);
    267 
    268 	if (child == NULL && alloced) {
    269 		free(t->t_slotdata[slot], M_DEVBUF);
    270 		t->t_slotdata[slot] = NULL;
    271 	}
    272 
    273 	if (child != NULL && t->t_slotdata[slot] != NULL)
    274 		rnd_attach_source(&t->t_slotdata[slot]->rnd_source,
    275 		    device_xname(child), RND_TYPE_TTY, 0);
    276 
    277 	return child != NULL;
    278 }
    279 
    280 void
    281 pckbc_attach(struct pckbc_softc *sc)
    282 {
    283 	struct pckbc_internal *t;
    284 	bus_space_tag_t iot;
    285 	bus_space_handle_t ioh_d, ioh_c;
    286 	int res;
    287 	u_char cmdbits = 0;
    288 
    289 	t = sc->id;
    290 	iot = t->t_iot;
    291 	ioh_d = t->t_ioh_d;
    292 	ioh_c = t->t_ioh_c;
    293 
    294 	t->t_pt = pckbport_attach(t, &pckbc_ops);
    295 	if (t->t_pt == NULL) {
    296 		aprint_error(": attach failed\n");
    297 		return;
    298 	}
    299 
    300 	/* flush */
    301 	(void) pckbc_poll_data1(t, PCKBC_KBD_SLOT);
    302 
    303 	/* set initial cmd byte */
    304 	if (!pckbc_put8042cmd(t)) {
    305 		printf("pckbc: cmd word write error\n");
    306 		return;
    307 	}
    308 
    309 /*
    310  * XXX Don't check the keyboard port. There are broken keyboard controllers
    311  * which don't pass the test but work normally otherwise.
    312  */
    313 #if 0
    314 	/*
    315 	 * check kbd port ok
    316 	 */
    317 	if (!pckbc_send_cmd(iot, ioh_c, KBC_KBDTEST))
    318 		return;
    319 	res = pckbc_poll_data1(t, PCKBC_KBD_SLOT, 0);
    320 
    321 	/*
    322 	 * Normally, we should get a "0" here.
    323 	 * But there are keyboard controllers behaving differently.
    324 	 */
    325 	if (res == 0 || res == 0xfa || res == 0x01 || res == 0xab) {
    326 #ifdef PCKBCDEBUG
    327 		if (res != 0)
    328 			printf("pckbc: returned %x on kbd slot test\n", res);
    329 #endif
    330 		if (pckbc_attach_slot(sc, PCKBC_KBD_SLOT))
    331 			cmdbits |= KC8_KENABLE;
    332 	} else {
    333 		printf("pckbc: kbd port test: %x\n", res);
    334 		return;
    335 	}
    336 #else
    337 	if (pckbc_attach_slot(sc, PCKBC_KBD_SLOT))
    338 		cmdbits |= KC8_KENABLE;
    339 #endif /* 0 */
    340 
    341 	/*
    342 	 * Check aux port ok.
    343 	 * Avoid KBC_AUXTEST because it hangs some older controllers
    344 	 *  (eg UMC880?).
    345 	 */
    346 	if (!pckbc_send_cmd(iot, ioh_c, KBC_AUXECHO)) {
    347 		printf("pckbc: aux echo error 1\n");
    348 		goto nomouse;
    349 	}
    350 	if (!pckbc_wait_output(iot, ioh_c)) {
    351 		printf("pckbc: aux echo error 2\n");
    352 		goto nomouse;
    353 	}
    354 	t->t_haveaux = 1;
    355 	bus_space_write_1(iot, ioh_d, 0, 0x5a); /* a random value */
    356 	res = pckbc_poll_data1(t, PCKBC_AUX_SLOT);
    357 
    358 	/*
    359 	 * The following is needed to find the aux port on the Tadpole
    360 	 * SPARCle.
    361 	 */
    362 	if (res == -1 && ISSET(t->t_flags, PCKBC_NEED_AUXWRITE)) {
    363 		/* Read of aux echo timed out, try again */
    364 		if (!pckbc_send_cmd(iot, ioh_c, KBC_AUXWRITE))
    365 			goto nomouse;
    366 		if (!pckbc_wait_output(iot, ioh_c))
    367 			goto nomouse;
    368 		bus_space_write_1(iot, ioh_d, 0, 0x5a);
    369 		res = pckbc_poll_data1(t, PCKBC_AUX_SLOT);
    370 	}
    371 	if (res != -1) {
    372 		/*
    373 		 * In most cases, the 0x5a gets echoed.
    374 		 * Some older controllers (Gateway 2000 circa 1993)
    375 		 * return 0xfe here.
    376 		 * We are satisfied if there is anything in the
    377 		 * aux output buffer.
    378 		 */
    379 		if (pckbc_attach_slot(sc, PCKBC_AUX_SLOT))
    380 			cmdbits |= KC8_MENABLE;
    381 	} else {
    382 
    383 #ifdef PCKBCDEBUG
    384 		printf("pckbc: aux echo test failed\n");
    385 #endif
    386 		t->t_haveaux = 0;
    387 	}
    388 
    389 nomouse:
    390 	/* enable needed interrupts */
    391 	t->t_cmdbyte |= cmdbits;
    392 	if (!pckbc_put8042cmd(t))
    393 		printf("pckbc: cmd word write error\n");
    394 }
    395 
    396 static void
    397 pckbc_init_slotdata(struct pckbc_slotdata *q)
    398 {
    399 
    400 	q->polling = 0;
    401 }
    402 
    403 /*
    404  * switch scancode translation on / off
    405  * return nonzero on success
    406  */
    407 static int
    408 pckbc_xt_translation(void *self, pckbc_slot_t slot, int on)
    409 {
    410 	struct pckbc_internal *t = self;
    411 	int ison;
    412 
    413 	if (ISSET(t->t_flags, PCKBC_CANT_TRANSLATE))
    414 		return (-1);
    415 
    416 	if (slot != PCKBC_KBD_SLOT) {
    417 		/* translation only for kbd slot */
    418 		if (on)
    419 			return (0);
    420 		else
    421 			return (1);
    422 	}
    423 
    424 	ison = t->t_cmdbyte & KC8_TRANS;
    425 	if ((on && ison) || (!on && !ison))
    426 		return (1);
    427 
    428 	t->t_cmdbyte ^= KC8_TRANS;
    429 	if (!pckbc_put8042cmd(t))
    430 		return (0);
    431 
    432 	/* read back to be sure */
    433 	if (!pckbc_get8042cmd(t))
    434 		return (0);
    435 
    436 	ison = t->t_cmdbyte & KC8_TRANS;
    437 	if ((on && ison) || (!on && !ison))
    438 		return (1);
    439 	return (0);
    440 }
    441 
    442 static const struct pckbc_portcmd {
    443 	u_char cmd_en, cmd_dis;
    444 } pckbc_portcmd[2] = {
    445 	{
    446 		KBC_KBDENABLE, KBC_KBDDISABLE,
    447 	}, {
    448 		KBC_AUXENABLE, KBC_AUXDISABLE,
    449 	}
    450 };
    451 
    452 void
    453 pckbc_slot_enable(void *self, pckbc_slot_t slot, int on)
    454 {
    455 	struct pckbc_internal *t = (struct pckbc_internal *)self;
    456 	const struct pckbc_portcmd *cmd;
    457 
    458 	cmd = &pckbc_portcmd[slot];
    459 
    460 	if (!pckbc_send_cmd(t->t_iot, t->t_ioh_c,
    461 			    on ? cmd->cmd_en : cmd->cmd_dis))
    462 		printf("pckbc: pckbc_slot_enable(%d) failed\n", on);
    463 }
    464 
    465 static void
    466 pckbc_set_poll(void *self, pckbc_slot_t slot, int on)
    467 {
    468 	struct pckbc_internal *t = (struct pckbc_internal *)self;
    469 
    470 	t->t_slotdata[slot]->polling = on;
    471 
    472 	if (on) {
    473 		t->t_slotdata[slot]->poll_data = -1;
    474 		t->t_slotdata[slot]->poll_stat = -1;
    475 	} else {
    476 		int s;
    477 
    478 		/*
    479 		 * If disabling polling on a device that's been configured,
    480 		 * make sure there are no bytes left in the FIFO, holding up
    481 		 * the interrupt line.  Otherwise we won't get any further
    482 		 * interrupts.
    483 		 */
    484 		if (t->t_sc) {
    485 			s = spltty();
    486 			pckbcintr(t->t_sc);
    487 			splx(s);
    488 		}
    489 	}
    490 }
    491 
    492 static void
    493 pckbc_intr_establish(void *pt, pckbport_slot_t slot)
    494 {
    495 	struct pckbc_internal *t = pt;
    496 
    497 	(*t->t_sc->intr_establish)(t->t_sc, slot);
    498 }
    499 
    500 int
    501 pckbcintr_hard(void *vsc)
    502 {
    503 	struct pckbc_softc *sc = (struct pckbc_softc *)vsc;
    504 	struct pckbc_internal *t = sc->id;
    505 	u_char stat;
    506 	pckbc_slot_t slot;
    507 	struct pckbc_slotdata *q;
    508 	int served = 0, data, next, s;
    509 
    510 	for(;;) {
    511 		stat = bus_space_read_1(t->t_iot, t->t_ioh_c, 0);
    512 		if (!(stat & KBS_DIB))
    513 			break;
    514 
    515 		served = 1;
    516 
    517 		slot = (t->t_haveaux && (stat & 0x20)) ?
    518 		    PCKBC_AUX_SLOT : PCKBC_KBD_SLOT;
    519 		q = t->t_slotdata[slot];
    520 
    521 		if (!q) {
    522 			/* XXX do something for live insertion? */
    523 			printf("pckbc: no dev for slot %d\n", slot);
    524 			KBD_DELAY;
    525 			(void) bus_space_read_1(t->t_iot, t->t_ioh_d, 0);
    526 			continue;
    527 		}
    528 
    529 		KBD_DELAY;
    530 		data = bus_space_read_1(t->t_iot, t->t_ioh_d, 0);
    531 
    532 		rnd_add_uint32(&q->rnd_source, (stat<<8)|data);
    533 
    534 		if (q->polling) {
    535 			q->poll_data = data;
    536 			q->poll_stat = stat;
    537 			break; /* pckbc_poll_data() will get it */
    538 		}
    539 
    540 #if 0 /* XXXBJH */
    541 		if (CMD_IN_QUEUE(q) && pckbc_cmdresponse(t, slot, data))
    542 			continue;
    543 #endif
    544 
    545 		s = splhigh();
    546 		next = (t->rbuf_write+1) % PCKBC_RBUF_SIZE;
    547 		if (next == t->rbuf_read) {
    548 			splx(s);
    549 			break;
    550 		}
    551 		t->rbuf[t->rbuf_write].data = data;
    552 		t->rbuf[t->rbuf_write].slot = slot;
    553 		t->rbuf_write = next;
    554 		splx(s);
    555 	}
    556 
    557 	return (served);
    558 }
    559 
    560 void
    561 pckbcintr_soft(void *vsc)
    562 {
    563 	struct pckbc_softc *sc = vsc;
    564 	struct pckbc_internal *t = sc->id;
    565 	int data, slot, s;
    566 #ifndef __GENERIC_SOFT_INTERRUPTS_ALL_LEVELS
    567 	int st;
    568 
    569 	st = spltty();
    570 #endif
    571 
    572 	s = splhigh();
    573 	while (t->rbuf_read != t->rbuf_write) {
    574 		slot = t->rbuf[t->rbuf_read].slot;
    575 		data = t->rbuf[t->rbuf_read].data;
    576 		t->rbuf_read = (t->rbuf_read+1) % PCKBC_RBUF_SIZE;
    577 		splx(s);
    578 		pckbportintr(t->t_pt, slot, data);
    579 		s = splhigh();
    580 	}
    581 	splx(s);
    582 
    583 
    584 #ifndef __GENERIC_SOFT_INTERRUPTS_ALL_LEVELS
    585 	splx(st);
    586 #endif
    587 }
    588 
    589 int
    590 pckbcintr(void *vsc)
    591 {
    592 	struct pckbc_softc *sc = (struct pckbc_softc *)vsc;
    593 	struct pckbc_internal *t = sc->id;
    594 	u_char stat;
    595 	pckbc_slot_t slot;
    596 	struct pckbc_slotdata *q;
    597 	int served = 0, data;
    598 
    599 	for(;;) {
    600 		stat = bus_space_read_1(t->t_iot, t->t_ioh_c, 0);
    601 		if (!(stat & KBS_DIB))
    602 			break;
    603 
    604 		served = 1;
    605 
    606 		slot = (t->t_haveaux && (stat & 0x20)) ?
    607 		    PCKBC_AUX_SLOT : PCKBC_KBD_SLOT;
    608 		q = t->t_slotdata[slot];
    609 
    610 		KBD_DELAY;
    611 		data = bus_space_read_1(t->t_iot, t->t_ioh_d, 0);
    612 
    613 		rnd_add_uint32(&q->rnd_source, (stat<<8)|data);
    614 
    615 		pckbportintr(t->t_pt, slot, data);
    616 	}
    617 
    618 	return (served);
    619 }
    620 
    621 int
    622 pckbc_cnattach(bus_space_tag_t iot, bus_addr_t addr,
    623 	bus_size_t cmd_offset, pckbc_slot_t slot, int flags)
    624 {
    625 	bus_space_handle_t ioh_d, ioh_c;
    626 #ifdef PCKBC_CNATTACH_SELFTEST
    627 	int reply;
    628 #endif
    629 	int res = 0;
    630 
    631 	if (bus_space_map(iot, addr + KBDATAP, 1, 0, &ioh_d))
    632 		return (ENXIO);
    633 	if (bus_space_map(iot, addr + cmd_offset, 1, 0, &ioh_c)) {
    634 		bus_space_unmap(iot, ioh_d, 1);
    635 		return (ENXIO);
    636 	}
    637 
    638 	memset(&pckbc_consdata, 0, sizeof(pckbc_consdata));
    639 	pckbc_consdata.t_iot = iot;
    640 	pckbc_consdata.t_ioh_d = ioh_d;
    641 	pckbc_consdata.t_ioh_c = ioh_c;
    642 	pckbc_consdata.t_addr = addr;
    643 	pckbc_consdata.t_flags = flags;
    644 	callout_init(&pckbc_consdata.t_cleanup, 0);
    645 
    646 	/* flush */
    647 	(void) pckbc_poll_data1(&pckbc_consdata, PCKBC_KBD_SLOT);
    648 
    649 #ifdef PCKBC_CNATTACH_SELFTEST
    650 	/*
    651 	 * In some machines (e.g. netwinder) pckbc refuses to talk at
    652 	 * all until we request a self-test.
    653 	 */
    654 	if (!pckbc_send_cmd(iot, ioh_c, KBC_SELFTEST)) {
    655 		printf("pckbc: unable to request selftest\n");
    656 		res = EIO;
    657 		goto out;
    658 	}
    659 
    660 	reply = pckbc_poll_data1(&pckbc_consdata, PCKBC_KBD_SLOT);
    661 	if (reply != 0x55) {
    662 		printf("pckbc: selftest returned 0x%02x\n", reply);
    663 		res = EIO;
    664 		goto out;
    665 	}
    666 #endif /* PCKBC_CNATTACH_SELFTEST */
    667 
    668 	/* init cmd byte, enable ports */
    669 	pckbc_consdata.t_cmdbyte = KC8_CPU;
    670 	if (!pckbc_put8042cmd(&pckbc_consdata)) {
    671 		printf("pckbc: cmd word write error\n");
    672 		res = EIO;
    673 		goto out;
    674 	}
    675 
    676 	res = pckbport_cnattach(&pckbc_consdata, &pckbc_ops, slot);
    677 
    678   out:
    679 	if (res) {
    680 		bus_space_unmap(iot, pckbc_consdata.t_ioh_d, 1);
    681 		bus_space_unmap(iot, pckbc_consdata.t_ioh_c, 1);
    682 	} else {
    683 		pckbc_consdata.t_slotdata[slot] = &pckbc_cons_slotdata;
    684 		pckbc_init_slotdata(&pckbc_cons_slotdata);
    685 		pckbc_console = 1;
    686 	}
    687 
    688 	return (res);
    689 }
    690 
    691 bool
    692 pckbc_resume(device_t dv, const pmf_qual_t *qual)
    693 {
    694 	struct pckbc_softc *sc = device_private(dv);
    695 	struct pckbc_internal *t;
    696 
    697 	t = sc->id;
    698 	(void)pckbc_poll_data1(t, PCKBC_KBD_SLOT);
    699 	if (!pckbc_send_cmd(t->t_iot, t->t_ioh_c, KBC_SELFTEST))
    700 		return false;
    701 	(void)pckbc_poll_data1(t, PCKBC_KBD_SLOT);
    702 	(void)pckbc_put8042cmd(t);
    703 	pckbcintr(t->t_sc);
    704 
    705 	return true;
    706 }
    707