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