Home | History | Annotate | Line # | Download | only in ic
pckbc.c revision 1.6
      1 /* $NetBSD: pckbc.c,v 1.6 2001/04/09 15:45:50 drochner Exp $ */
      2 
      3 /*
      4  * Copyright (c) 1998
      5  *	Matthias Drochner.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed for the NetBSD Project
     18  *	by Matthias Drochner.
     19  * 4. The name of the author may not be used to endorse or promote products
     20  *    derived from this software without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32  */
     33 
     34 #include <sys/param.h>
     35 #include <sys/systm.h>
     36 #include <sys/callout.h>
     37 #include <sys/kernel.h>
     38 #include <sys/proc.h>
     39 #include <sys/device.h>
     40 #include <sys/malloc.h>
     41 #include <sys/errno.h>
     42 #include <sys/queue.h>
     43 #include <sys/lock.h>
     44 
     45 #include <machine/bus.h>
     46 
     47 #include <dev/ic/i8042reg.h>
     48 #include <dev/ic/pckbcvar.h>
     49 
     50 #include "rnd.h"
     51 #include "locators.h"
     52 
     53 #ifdef __HAVE_NWSCONS /* XXX: this port uses sys/dev/pckbc */
     54 #include "pckbd.h"
     55 #else /* ie: only md drivers attach to pckbc */
     56 #define NPCKBD 0
     57 #endif
     58 #if (NPCKBD > 0)
     59 #include <dev/pckbc/pckbdvar.h>
     60 #endif
     61 #if NRND > 0
     62 #include <sys/rnd.h>
     63 #endif
     64 
     65 /* descriptor for one device command */
     66 struct pckbc_devcmd {
     67 	TAILQ_ENTRY(pckbc_devcmd) next;
     68 	int flags;
     69 #define KBC_CMDFLAG_SYNC 1 /* give descriptor back to caller */
     70 #define KBC_CMDFLAG_SLOW 2
     71 	u_char cmd[4];
     72 	int cmdlen, cmdidx, retries;
     73 	u_char response[4];
     74 	int status, responselen, responseidx;
     75 };
     76 
     77 /* data per slave device */
     78 struct pckbc_slotdata {
     79 	int polling; /* don't read data port in interrupt handler */
     80 	TAILQ_HEAD(, pckbc_devcmd) cmdqueue; /* active commands */
     81 	TAILQ_HEAD(, pckbc_devcmd) freequeue; /* free commands */
     82 #define NCMD 5
     83 	struct pckbc_devcmd cmds[NCMD];
     84 #if NRND > 0
     85 	rndsource_element_t	rnd_source;
     86 #endif
     87 };
     88 
     89 #define CMD_IN_QUEUE(q) (TAILQ_FIRST(&(q)->cmdqueue) != NULL)
     90 
     91 void pckbc_init_slotdata __P((struct pckbc_slotdata *));
     92 int pckbc_attach_slot __P((struct pckbc_softc *, pckbc_slot_t));
     93 int pckbc_submatch __P((struct device *, struct cfdata *, void *));
     94 int pckbcprint __P((void *, const char *));
     95 
     96 struct pckbc_internal pckbc_consdata;
     97 int pckbc_console_attached;
     98 
     99 static int pckbc_console;
    100 static struct pckbc_slotdata pckbc_cons_slotdata;
    101 
    102 static int pckbc_wait_output __P((bus_space_tag_t, bus_space_handle_t));
    103 
    104 static int pckbc_get8042cmd __P((struct pckbc_internal *));
    105 static int pckbc_put8042cmd __P((struct pckbc_internal *));
    106 static int pckbc_send_devcmd __P((struct pckbc_internal *, pckbc_slot_t,
    107 				  u_char));
    108 static void pckbc_poll_cmd1 __P((struct pckbc_internal *, pckbc_slot_t,
    109 				 struct pckbc_devcmd *));
    110 
    111 void pckbc_cleanqueue __P((struct pckbc_slotdata *));
    112 void pckbc_cleanup __P((void *));
    113 int pckbc_cmdresponse __P((struct pckbc_internal *, pckbc_slot_t, u_char));
    114 void pckbc_start __P((struct pckbc_internal *, pckbc_slot_t));
    115 
    116 const char *pckbc_slot_names[] = { "kbd", "aux" };
    117 
    118 #define KBC_DEVCMD_ACK 0xfa
    119 #define KBC_DEVCMD_RESEND 0xfe
    120 
    121 #define	KBD_DELAY	DELAY(8)
    122 
    123 static inline int
    124 pckbc_wait_output(iot, ioh_c)
    125 	bus_space_tag_t iot;
    126 	bus_space_handle_t ioh_c;
    127 {
    128 	u_int i;
    129 
    130 	for (i = 100000; i; i--)
    131 		if (!(bus_space_read_1(iot, ioh_c, 0) & KBS_IBF)) {
    132 			KBD_DELAY;
    133 			return (1);
    134 		}
    135 	return (0);
    136 }
    137 
    138 int
    139 pckbc_send_cmd(iot, ioh_c, val)
    140 	bus_space_tag_t iot;
    141 	bus_space_handle_t ioh_c;
    142 	u_char val;
    143 {
    144 	if (!pckbc_wait_output(iot, ioh_c))
    145 		return (0);
    146 	bus_space_write_1(iot, ioh_c, 0, val);
    147 	return (1);
    148 }
    149 
    150 int
    151 pckbc_poll_data1(iot, ioh_d, ioh_c, slot, checkaux)
    152 	bus_space_tag_t iot;
    153 	bus_space_handle_t ioh_d, ioh_c;
    154 	pckbc_slot_t slot;
    155 	int checkaux;
    156 {
    157 	int i;
    158 	u_char stat;
    159 
    160 	/* if 1 port read takes 1us (?), this polls for 100ms */
    161 	for (i = 100000; i; i--) {
    162 		stat = bus_space_read_1(iot, ioh_c, 0);
    163 		if (stat & KBS_DIB) {
    164 			register u_char c;
    165 
    166 			KBD_DELAY;
    167 			c = bus_space_read_1(iot, ioh_d, 0);
    168 			if (checkaux && (stat & 0x20)) { /* aux data */
    169 				if (slot != PCKBC_AUX_SLOT) {
    170 #ifdef PCKBCDEBUG
    171 					printf("lost aux 0x%x\n", c);
    172 #endif
    173 					continue;
    174 				}
    175 			} else {
    176 				if (slot == PCKBC_AUX_SLOT) {
    177 #ifdef PCKBCDEBUG
    178 					printf("lost kbd 0x%x\n", c);
    179 #endif
    180 					continue;
    181 				}
    182 			}
    183 			return (c);
    184 		}
    185 	}
    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_d = t->t_ioh_d;
    198 	bus_space_handle_t ioh_c = t->t_ioh_c;
    199 	int data;
    200 
    201 	if (!pckbc_send_cmd(iot, ioh_c, K_RDCMDBYTE))
    202 		return (0);
    203 	data = pckbc_poll_data1(iot, ioh_d, ioh_c, PCKBC_KBD_SLOT,
    204 				t->t_haveaux);
    205 	if (data == -1)
    206 		return (0);
    207 	t->t_cmdbyte = data;
    208 	return (1);
    209 }
    210 
    211 /*
    212  * Pass command byte to keyboard controller (8042).
    213  */
    214 static int
    215 pckbc_put8042cmd(t)
    216 	struct pckbc_internal *t;
    217 {
    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 (!pckbc_send_cmd(iot, ioh_c, K_LDCMDBYTE))
    223 		return (0);
    224 	if (!pckbc_wait_output(iot, ioh_c))
    225 		return (0);
    226 	bus_space_write_1(iot, ioh_d, 0, t->t_cmdbyte);
    227 	return (1);
    228 }
    229 
    230 static int
    231 pckbc_send_devcmd(t, slot, val)
    232 	struct pckbc_internal *t;
    233 	pckbc_slot_t slot;
    234 	u_char val;
    235 {
    236 	bus_space_tag_t iot = t->t_iot;
    237 	bus_space_handle_t ioh_d = t->t_ioh_d;
    238 	bus_space_handle_t ioh_c = t->t_ioh_c;
    239 
    240 	if (slot == PCKBC_AUX_SLOT) {
    241 		if (!pckbc_send_cmd(iot, ioh_c, KBC_AUXWRITE))
    242 			return (0);
    243 	}
    244 	if (!pckbc_wait_output(iot, ioh_c))
    245 		return (0);
    246 	bus_space_write_1(iot, ioh_d, 0, val);
    247 	return (1);
    248 }
    249 
    250 int
    251 pckbc_is_console(iot, addr)
    252 	bus_space_tag_t iot;
    253 	bus_addr_t addr;
    254 {
    255 	if (pckbc_console && !pckbc_console_attached &&
    256 	    pckbc_consdata.t_iot == iot &&
    257 	    pckbc_consdata.t_addr == addr)
    258 		return (1);
    259 	return (0);
    260 }
    261 
    262 int
    263 pckbc_submatch(parent, cf, aux)
    264 	struct device *parent;
    265 	struct cfdata *cf;
    266 	void *aux;
    267 {
    268 	struct pckbc_attach_args *pa = aux;
    269 
    270 	if (cf->cf_loc[PCKBCCF_SLOT] != PCKBCCF_SLOT_DEFAULT &&
    271 	    cf->cf_loc[PCKBCCF_SLOT] != pa->pa_slot)
    272 		return (0);
    273 	return ((*cf->cf_attach->ca_match)(parent, cf, aux));
    274 }
    275 
    276 int
    277 pckbc_attach_slot(sc, slot)
    278 	struct pckbc_softc *sc;
    279 	pckbc_slot_t slot;
    280 {
    281 	struct pckbc_internal *t = sc->id;
    282 	struct pckbc_attach_args pa;
    283 	int found;
    284 
    285 	pa.pa_tag = t;
    286 	pa.pa_slot = slot;
    287 	found = (config_found_sm((struct device *)sc, &pa,
    288 				 pckbcprint, pckbc_submatch) != NULL);
    289 
    290 	if (found && !t->t_slotdata[slot]) {
    291 		t->t_slotdata[slot] = malloc(sizeof(struct pckbc_slotdata),
    292 					     M_DEVBUF, M_NOWAIT);
    293 		pckbc_init_slotdata(t->t_slotdata[slot]);
    294 	}
    295 #if NRND > 0
    296 	if (found && (t->t_slotdata[slot] != NULL))
    297 		rnd_attach_source(&t->t_slotdata[slot]->rnd_source, sc->subname[slot],
    298 		    RND_TYPE_TTY, 0);
    299 #endif
    300 	return (found);
    301 }
    302 
    303 void
    304 pckbc_attach(sc)
    305 	struct pckbc_softc *sc;
    306 {
    307 	struct pckbc_internal *t;
    308 	bus_space_tag_t iot;
    309 	bus_space_handle_t ioh_d, ioh_c;
    310 	int res;
    311 	u_char cmdbits = 0;
    312 
    313 	t = sc->id;
    314 	iot = t->t_iot;
    315 	ioh_d = t->t_ioh_d;
    316 	ioh_c = t->t_ioh_c;
    317 
    318 	/* flush */
    319 	(void) pckbc_poll_data1(iot, ioh_d, ioh_c, PCKBC_KBD_SLOT, 0);
    320 
    321 	/* set initial cmd byte */
    322 	if (!pckbc_put8042cmd(t)) {
    323 		printf("kbc: cmd word write error\n");
    324 		return;
    325 	}
    326 
    327 /*
    328  * XXX Don't check the keyboard port. There are broken keyboard controllers
    329  * which don't pass the test but work normally otherwise.
    330  */
    331 #if 0
    332 	/*
    333 	 * check kbd port ok
    334 	 */
    335 	if (!pckbc_send_cmd(iot, ioh_c, KBC_KBDTEST))
    336 		return;
    337 	res = pckbc_poll_data1(iot, ioh_d, ioh_c, PCKBC_KBD_SLOT, 0);
    338 
    339 	/*
    340 	 * Normally, we should get a "0" here.
    341 	 * But there are keyboard controllers behaving differently.
    342 	 */
    343 	if (res == 0 || res == 0xfa || res == 0x01 || res == 0xab) {
    344 #ifdef PCKBCDEBUG
    345 		if (res != 0)
    346 			printf("kbc: returned %x on kbd slot test\n", res);
    347 #endif
    348 		if (pckbc_attach_slot(sc, PCKBC_KBD_SLOT))
    349 			cmdbits |= KC8_KENABLE;
    350 	} else {
    351 		printf("kbc: kbd port test: %x\n", res);
    352 		return;
    353 	}
    354 #else
    355 	if (pckbc_attach_slot(sc, PCKBC_KBD_SLOT))
    356 		cmdbits |= KC8_KENABLE;
    357 #endif /* 0 */
    358 
    359 	/*
    360 	 * Check aux port ok.
    361 	 * Avoid KBC_AUXTEST because it hangs some older controllers
    362 	 *  (eg UMC880?).
    363 	 */
    364 	if (!pckbc_send_cmd(iot, ioh_c, KBC_AUXECHO)) {
    365 		printf("kbc: aux echo error 1\n");
    366 		goto nomouse;
    367 	}
    368 	if (!pckbc_wait_output(iot, ioh_c)) {
    369 		printf("kbc: aux echo error 2\n");
    370 		goto nomouse;
    371 	}
    372 	bus_space_write_1(iot, ioh_d, 0, 0x5a); /* a random value */
    373 	res = pckbc_poll_data1(iot, ioh_d, ioh_c, PCKBC_AUX_SLOT, 1);
    374 	if (res == 0x5a) {
    375 		t->t_haveaux = 1;
    376 		if (pckbc_attach_slot(sc, PCKBC_AUX_SLOT))
    377 			cmdbits |= KC8_MENABLE;
    378 	}
    379 #ifdef PCKBCDEBUG
    380 	  else
    381 		printf("kbc: aux echo: %x\n", res);
    382 #endif
    383 
    384 nomouse:
    385 	/* enable needed interrupts */
    386 	t->t_cmdbyte |= cmdbits;
    387 	if (!pckbc_put8042cmd(t))
    388 		printf("kbc: cmd word write error\n");
    389 }
    390 
    391 int
    392 pckbcprint(aux, pnp)
    393 	void *aux;
    394 	const char *pnp;
    395 {
    396 	struct pckbc_attach_args *pa = aux;
    397 
    398 	if (!pnp)
    399 		printf(" (%s slot)", pckbc_slot_names[pa->pa_slot]);
    400 	return (QUIET);
    401 }
    402 
    403 void
    404 pckbc_init_slotdata(q)
    405 	struct pckbc_slotdata *q;
    406 {
    407 	int i;
    408 	TAILQ_INIT(&q->cmdqueue);
    409 	TAILQ_INIT(&q->freequeue);
    410 
    411 	for (i = 0; i < NCMD; i++) {
    412 		TAILQ_INSERT_TAIL(&q->freequeue, &(q->cmds[i]), next);
    413 	}
    414 	q->polling = 0;
    415 }
    416 
    417 void
    418 pckbc_flush(self, slot)
    419 	pckbc_tag_t self;
    420 	pckbc_slot_t slot;
    421 {
    422 	struct pckbc_internal *t = self;
    423 
    424 	(void) pckbc_poll_data1(t->t_iot, t->t_ioh_d, t->t_ioh_c,
    425 				slot, t->t_haveaux);
    426 }
    427 
    428 int
    429 pckbc_poll_data(self, slot)
    430 	pckbc_tag_t self;
    431 	pckbc_slot_t slot;
    432 {
    433 	struct pckbc_internal *t = self;
    434 	struct pckbc_slotdata *q = t->t_slotdata[slot];
    435 	int c;
    436 
    437 	c = pckbc_poll_data1(t->t_iot, t->t_ioh_d, t->t_ioh_c,
    438 			     slot, t->t_haveaux);
    439 	if (c != -1 && q && CMD_IN_QUEUE(q)) {
    440 		/* we jumped into a running command - try to
    441 		 deliver the response */
    442 		if (pckbc_cmdresponse(t, slot, c))
    443 			return (-1);
    444 	}
    445 	return (c);
    446 }
    447 
    448 /*
    449  * switch scancode translation on / off
    450  * return nonzero on success
    451  */
    452 int
    453 pckbc_xt_translation(self, slot, on)
    454 	pckbc_tag_t self;
    455 	pckbc_slot_t slot;
    456 	int on;
    457 {
    458 	struct pckbc_internal *t = self;
    459 	int ison;
    460 
    461 	if (slot != PCKBC_KBD_SLOT) {
    462 		/* translation only for kbd slot */
    463 		if (on)
    464 			return (0);
    465 		else
    466 			return (1);
    467 	}
    468 
    469 	ison = t->t_cmdbyte & KC8_TRANS;
    470 	if ((on && ison) || (!on && !ison))
    471 		return (1);
    472 
    473 	t->t_cmdbyte ^= KC8_TRANS;
    474 	if (!pckbc_put8042cmd(t))
    475 		return (0);
    476 
    477 	/* read back to be sure */
    478 	if (!pckbc_get8042cmd(t))
    479 		return (0);
    480 
    481 	ison = t->t_cmdbyte & KC8_TRANS;
    482 	if ((on && ison) || (!on && !ison))
    483 		return (1);
    484 	return (0);
    485 }
    486 
    487 static struct pckbc_portcmd {
    488 	u_char cmd_en, cmd_dis;
    489 } pckbc_portcmd[2] = {
    490 	{
    491 		KBC_KBDENABLE, KBC_KBDDISABLE,
    492 	}, {
    493 		KBC_AUXENABLE, KBC_AUXDISABLE,
    494 	}
    495 };
    496 
    497 void
    498 pckbc_slot_enable(self, slot, on)
    499 	pckbc_tag_t self;
    500 	pckbc_slot_t slot;
    501 	int on;
    502 {
    503 	struct pckbc_internal *t = (struct pckbc_internal *)self;
    504 	struct pckbc_portcmd *cmd;
    505 
    506 	cmd = &pckbc_portcmd[slot];
    507 
    508 	if (!pckbc_send_cmd(t->t_iot, t->t_ioh_c,
    509 			    on ? cmd->cmd_en : cmd->cmd_dis))
    510 		printf("pckbc_slot_enable(%d) failed\n", on);
    511 }
    512 
    513 void
    514 pckbc_set_poll(self, slot, on)
    515 	pckbc_tag_t self;
    516 	pckbc_slot_t slot;
    517 	int on;
    518 {
    519 	struct pckbc_internal *t = (struct pckbc_internal *)self;
    520 
    521 	t->t_slotdata[slot]->polling = on;
    522 
    523 	if (!on) {
    524                 int s;
    525 
    526                 /*
    527                  * If disabling polling on a device that's been configured,
    528                  * make sure there are no bytes left in the FIFO, holding up
    529                  * the interrupt line.  Otherwise we won't get any further
    530                  * interrupts.
    531                  */
    532 		if (t->t_sc) {
    533 			s = spltty();
    534 			pckbcintr(t->t_sc);
    535 			splx(s);
    536 		}
    537 	}
    538 }
    539 
    540 /*
    541  * Pass command to device, poll for ACK and data.
    542  * to be called at spltty()
    543  */
    544 static void
    545 pckbc_poll_cmd1(t, slot, cmd)
    546 	struct pckbc_internal *t;
    547 	pckbc_slot_t slot;
    548 	struct pckbc_devcmd *cmd;
    549 {
    550 	bus_space_tag_t iot = t->t_iot;
    551 	bus_space_handle_t ioh_d = t->t_ioh_d;
    552 	bus_space_handle_t ioh_c = t->t_ioh_c;
    553 	int i, c = 0;
    554 
    555 	while (cmd->cmdidx < cmd->cmdlen) {
    556 		if (!pckbc_send_devcmd(t, slot, cmd->cmd[cmd->cmdidx])) {
    557 			printf("pckbc_cmd: send error\n");
    558 			cmd->status = EIO;
    559 			return;
    560 		}
    561 		for (i = 10; i; i--) { /* 1s ??? */
    562 			c = pckbc_poll_data1(iot, ioh_d, ioh_c, slot,
    563 					     t->t_haveaux);
    564 			if (c != -1)
    565 				break;
    566 		}
    567 
    568 		if (c == KBC_DEVCMD_ACK) {
    569 			cmd->cmdidx++;
    570 			continue;
    571 		}
    572 		if (c == KBC_DEVCMD_RESEND) {
    573 #ifdef PCKBCDEBUG
    574 			printf("pckbc_cmd: RESEND\n");
    575 #endif
    576 			if (cmd->retries++ < 5)
    577 				continue;
    578 			else {
    579 #ifdef PCKBCDEBUG
    580 				printf("pckbc: cmd failed\n");
    581 #endif
    582 				cmd->status = EIO;
    583 				return;
    584 			}
    585 		}
    586 		if (c == -1) {
    587 #ifdef PCKBCDEBUG
    588 			printf("pckbc_cmd: timeout\n");
    589 #endif
    590 			cmd->status = EIO;
    591 			return;
    592 		}
    593 #ifdef PCKBCDEBUG
    594 		printf("pckbc_cmd: lost 0x%x\n", c);
    595 #endif
    596 	}
    597 
    598 	while (cmd->responseidx < cmd->responselen) {
    599 		if (cmd->flags & KBC_CMDFLAG_SLOW)
    600 			i = 100; /* 10s ??? */
    601 		else
    602 			i = 10; /* 1s ??? */
    603 		while (i--) {
    604 			c = pckbc_poll_data1(iot, ioh_d, ioh_c, slot,
    605 					     t->t_haveaux);
    606 			if (c != -1)
    607 				break;
    608 		}
    609 		if (c == -1) {
    610 #ifdef PCKBCDEBUG
    611 			printf("pckbc_cmd: no data\n");
    612 #endif
    613 			cmd->status = ETIMEDOUT;
    614 			return;
    615 		} else
    616 			cmd->response[cmd->responseidx++] = c;
    617 	}
    618 }
    619 
    620 /* for use in autoconfiguration */
    621 int
    622 pckbc_poll_cmd(self, slot, cmd, len, responselen, respbuf, slow)
    623 	pckbc_tag_t self;
    624 	pckbc_slot_t slot;
    625 	u_char *cmd;
    626 	int len, responselen;
    627 	u_char *respbuf;
    628 	int slow;
    629 {
    630 	struct pckbc_internal *t = self;
    631 	struct pckbc_devcmd nc;
    632 
    633 	if ((len > 4) || (responselen > 4))
    634 		return (EINVAL);
    635 
    636 	bzero(&nc, sizeof(nc));
    637 	bcopy(cmd, nc.cmd, len);
    638 	nc.cmdlen = len;
    639 	nc.responselen = responselen;
    640 	nc.flags = (slow ? KBC_CMDFLAG_SLOW : 0);
    641 
    642 	pckbc_poll_cmd1(t, slot, &nc);
    643 
    644 	if (nc.status == 0 && respbuf)
    645 		bcopy(nc.response, respbuf, responselen);
    646 
    647 	return (nc.status);
    648 }
    649 
    650 /*
    651  * Clean up a command queue, throw away everything.
    652  */
    653 void
    654 pckbc_cleanqueue(q)
    655 	struct pckbc_slotdata *q;
    656 {
    657 	struct pckbc_devcmd *cmd;
    658 #ifdef PCKBCDEBUG
    659 	int i;
    660 #endif
    661 
    662 	while ((cmd = TAILQ_FIRST(&q->cmdqueue))) {
    663 		TAILQ_REMOVE(&q->cmdqueue, cmd, next);
    664 #ifdef PCKBCDEBUG
    665 		printf("pckbc_cleanqueue: removing");
    666 		for (i = 0; i < cmd->cmdlen; i++)
    667 			printf(" %02x", cmd->cmd[i]);
    668 		printf("\n");
    669 #endif
    670 		TAILQ_INSERT_TAIL(&q->freequeue, cmd, next);
    671 	}
    672 }
    673 
    674 /*
    675  * Timeout error handler: clean queues and data port.
    676  * XXX could be less invasive.
    677  */
    678 void
    679 pckbc_cleanup(self)
    680 	void *self;
    681 {
    682 	struct pckbc_internal *t = self;
    683 	int s;
    684 
    685 	printf("pckbc: command timeout\n");
    686 
    687 	s = spltty();
    688 
    689 	if (t->t_slotdata[PCKBC_KBD_SLOT])
    690 		pckbc_cleanqueue(t->t_slotdata[PCKBC_KBD_SLOT]);
    691 	if (t->t_slotdata[PCKBC_AUX_SLOT])
    692 		pckbc_cleanqueue(t->t_slotdata[PCKBC_AUX_SLOT]);
    693 
    694 	while (bus_space_read_1(t->t_iot, t->t_ioh_c, 0) & KBS_DIB) {
    695 		KBD_DELAY;
    696 		(void) bus_space_read_1(t->t_iot, t->t_ioh_d, 0);
    697 	}
    698 
    699 	/* reset KBC? */
    700 
    701 	splx(s);
    702 }
    703 
    704 /*
    705  * Pass command to device during normal operation.
    706  * to be called at spltty()
    707  */
    708 void
    709 pckbc_start(t, slot)
    710 	struct pckbc_internal *t;
    711 	pckbc_slot_t slot;
    712 {
    713 	struct pckbc_slotdata *q = t->t_slotdata[slot];
    714 	struct pckbc_devcmd *cmd = TAILQ_FIRST(&q->cmdqueue);
    715 
    716 	if (q->polling) {
    717 		do {
    718 			pckbc_poll_cmd1(t, slot, cmd);
    719 			if (cmd->status)
    720 				printf("pckbc_start: command error\n");
    721 
    722 			TAILQ_REMOVE(&q->cmdqueue, cmd, next);
    723 			if (cmd->flags & KBC_CMDFLAG_SYNC)
    724 				wakeup(cmd);
    725 			else {
    726 				callout_stop(&t->t_cleanup);
    727 				TAILQ_INSERT_TAIL(&q->freequeue, cmd, next);
    728 			}
    729 			cmd = TAILQ_FIRST(&q->cmdqueue);
    730 		} while (cmd);
    731 		return;
    732 	}
    733 
    734 	if (!pckbc_send_devcmd(t, slot, cmd->cmd[cmd->cmdidx])) {
    735 		printf("pckbc_start: send error\n");
    736 		/* XXX what now? */
    737 		return;
    738 	}
    739 }
    740 
    741 /*
    742  * Handle command responses coming in asynchonously,
    743  * return nonzero if valid response.
    744  * to be called at spltty()
    745  */
    746 int
    747 pckbc_cmdresponse(t, slot, data)
    748 	struct pckbc_internal *t;
    749 	pckbc_slot_t slot;
    750 	u_char data;
    751 {
    752 	struct pckbc_slotdata *q = t->t_slotdata[slot];
    753 	struct pckbc_devcmd *cmd = TAILQ_FIRST(&q->cmdqueue);
    754 #ifdef DIAGNOSTIC
    755 	if (!cmd)
    756 		panic("pckbc_cmdresponse: no active command");
    757 #endif
    758 	if (cmd->cmdidx < cmd->cmdlen) {
    759 		if (data != KBC_DEVCMD_ACK && data != KBC_DEVCMD_RESEND)
    760 			return (0);
    761 
    762 		if (data == KBC_DEVCMD_RESEND) {
    763 			if (cmd->retries++ < 5) {
    764 				/* try again last command */
    765 				goto restart;
    766 			} else {
    767 				printf("pckbc: cmd failed\n");
    768 				cmd->status = EIO;
    769 				/* dequeue */
    770 			}
    771 		} else {
    772 			if (++cmd->cmdidx < cmd->cmdlen)
    773 				goto restart;
    774 			if (cmd->responselen)
    775 				return (1);
    776 			/* else dequeue */
    777 		}
    778 	} else if (cmd->responseidx < cmd->responselen) {
    779 		cmd->response[cmd->responseidx++] = data;
    780 		if (cmd->responseidx < cmd->responselen)
    781 			return (1);
    782 		/* else dequeue */
    783 	} else
    784 		return (0);
    785 
    786 	/* dequeue: */
    787 	TAILQ_REMOVE(&q->cmdqueue, cmd, next);
    788 	if (cmd->flags & KBC_CMDFLAG_SYNC)
    789 		wakeup(cmd);
    790 	else {
    791 		callout_stop(&t->t_cleanup);
    792 		TAILQ_INSERT_TAIL(&q->freequeue, cmd, next);
    793 	}
    794 	if (!CMD_IN_QUEUE(q))
    795 		return (1);
    796 restart:
    797 	pckbc_start(t, slot);
    798 	return (1);
    799 }
    800 
    801 /*
    802  * Put command into the device's command queue, return zero or errno.
    803  */
    804 int
    805 pckbc_enqueue_cmd(self, slot, cmd, len, responselen, sync, respbuf)
    806 	pckbc_tag_t self;
    807 	pckbc_slot_t slot;
    808 	u_char *cmd;
    809 	int len, responselen, sync;
    810 	u_char *respbuf;
    811 {
    812 	struct pckbc_internal *t = self;
    813 	struct pckbc_slotdata *q = t->t_slotdata[slot];
    814 	struct pckbc_devcmd *nc;
    815 	int s, isactive, res = 0;
    816 
    817 	if ((len > 4) || (responselen > 4))
    818 		return (EINVAL);
    819 	s = spltty();
    820 	nc = TAILQ_FIRST(&q->freequeue);
    821 	if (nc) {
    822 		TAILQ_REMOVE(&q->freequeue, nc, next);
    823 	}
    824 	splx(s);
    825 	if (!nc)
    826 		return (ENOMEM);
    827 
    828 	bzero(nc, sizeof(*nc));
    829 	bcopy(cmd, nc->cmd, len);
    830 	nc->cmdlen = len;
    831 	nc->responselen = responselen;
    832 	nc->flags = (sync ? KBC_CMDFLAG_SYNC : 0);
    833 
    834 	s = spltty();
    835 
    836 	if (q->polling && sync) {
    837 		/*
    838 		 * XXX We should poll until the queue is empty.
    839 		 * But we don't come here normally, so make
    840 		 * it simple and throw away everything.
    841 		 */
    842 		pckbc_cleanqueue(q);
    843 	}
    844 
    845 	isactive = CMD_IN_QUEUE(q);
    846 	TAILQ_INSERT_TAIL(&q->cmdqueue, nc, next);
    847 	if (!isactive)
    848 		pckbc_start(t, slot);
    849 
    850 	if (q->polling)
    851 		res = (sync ? nc->status : 0);
    852 	else if (sync) {
    853 		if ((res = tsleep(nc, 0, "kbccmd", 1*hz))) {
    854 			TAILQ_REMOVE(&q->cmdqueue, nc, next);
    855 			pckbc_cleanup(t);
    856 		} else
    857 			res = nc->status;
    858 	} else
    859 		callout_reset(&t->t_cleanup, hz, pckbc_cleanup, t);
    860 
    861 	if (sync) {
    862 		if (respbuf)
    863 			bcopy(nc->response, respbuf, responselen);
    864 		TAILQ_INSERT_TAIL(&q->freequeue, nc, next);
    865 	}
    866 
    867 	splx(s);
    868 
    869 	return (res);
    870 }
    871 
    872 void
    873 pckbc_set_inputhandler(self, slot, func, arg, name)
    874 	pckbc_tag_t self;
    875 	pckbc_slot_t slot;
    876 	pckbc_inputfcn func;
    877 	void *arg;
    878 	char *name;
    879 {
    880 	struct pckbc_internal *t = (struct pckbc_internal *)self;
    881 	struct pckbc_softc *sc = t->t_sc;
    882 
    883 	if (slot >= PCKBC_NSLOTS)
    884 		panic("pckbc_set_inputhandler: bad slot %d", slot);
    885 
    886 	(*sc->intr_establish)(sc, slot);
    887 
    888 	sc->inputhandler[slot] = func;
    889 	sc->inputarg[slot] = arg;
    890 	sc->subname[slot] = name;
    891 }
    892 
    893 int
    894 pckbcintr(vsc)
    895 	void *vsc;
    896 {
    897 	struct pckbc_softc *sc = (struct pckbc_softc *)vsc;
    898 	struct pckbc_internal *t = sc->id;
    899 	u_char stat;
    900 	pckbc_slot_t slot;
    901 	struct pckbc_slotdata *q;
    902 	int served = 0, data;
    903 
    904 	for(;;) {
    905 		stat = bus_space_read_1(t->t_iot, t->t_ioh_c, 0);
    906 		if (!(stat & KBS_DIB))
    907 			break;
    908 
    909 		served = 1;
    910 
    911 		slot = (t->t_haveaux && (stat & 0x20)) ?
    912 		    PCKBC_AUX_SLOT : PCKBC_KBD_SLOT;
    913 		q = t->t_slotdata[slot];
    914 
    915 		if (!q) {
    916 			/* XXX do something for live insertion? */
    917 			printf("pckbcintr: no dev for slot %d\n", slot);
    918 			KBD_DELAY;
    919 			(void) bus_space_read_1(t->t_iot, t->t_ioh_d, 0);
    920 			continue;
    921 		}
    922 
    923 		if (q->polling)
    924 			break; /* pckbc_poll_data() will get it */
    925 
    926 		KBD_DELAY;
    927 		data = bus_space_read_1(t->t_iot, t->t_ioh_d, 0);
    928 
    929 #if NRND > 0
    930 		rnd_add_uint32(&q->rnd_source, (stat<<8)|data);
    931 #endif
    932 		if (CMD_IN_QUEUE(q) && pckbc_cmdresponse(t, slot, data))
    933 			continue;
    934 
    935 		if (sc->inputhandler[slot])
    936 			(*sc->inputhandler[slot])(sc->inputarg[slot], data);
    937 #ifdef PCKBCDEBUG
    938 		else
    939 			printf("pckbcintr: slot %d lost %d\n", slot, data);
    940 #endif
    941 	}
    942 
    943 	return (served);
    944 }
    945 
    946 int
    947 pckbc_cnattach(iot, addr, cmd_offset, slot)
    948 	bus_space_tag_t iot;
    949 	bus_addr_t addr;
    950 	bus_size_t cmd_offset;
    951 	pckbc_slot_t slot;
    952 {
    953 	bus_space_handle_t ioh_d, ioh_c;
    954 	int res = 0;
    955 
    956 	if (bus_space_map(iot, addr + KBDATAP, 1, 0, &ioh_d))
    957                 return (ENXIO);
    958 	if (bus_space_map(iot, addr + cmd_offset, 1, 0, &ioh_c)) {
    959 		bus_space_unmap(iot, ioh_d, 1);
    960                 return (ENXIO);
    961 	}
    962 
    963 	pckbc_consdata.t_iot = iot;
    964 	pckbc_consdata.t_ioh_d = ioh_d;
    965 	pckbc_consdata.t_ioh_c = ioh_c;
    966 	pckbc_consdata.t_addr = addr;
    967 	callout_init(&pckbc_consdata.t_cleanup);
    968 
    969 	/* flush */
    970 	(void) pckbc_poll_data1(iot, ioh_d, ioh_c, PCKBC_KBD_SLOT, 0);
    971 
    972 	/* selftest? */
    973 
    974 	/* init cmd byte, enable ports */
    975 	pckbc_consdata.t_cmdbyte = KC8_CPU;
    976 	if (!pckbc_put8042cmd(&pckbc_consdata)) {
    977 		printf("kbc: cmd word write error\n");
    978 		res = EIO;
    979 	}
    980 
    981 	if (!res) {
    982 #if (NPCKBD > 0)
    983 		res = pckbd_cnattach(&pckbc_consdata, slot);
    984 #else
    985 		/*
    986 		 * XXX This should be replaced with the `notyet' case
    987 		 * XXX when all of the old PC-style console drivers
    988 		 * XXX have gone away.  When that happens, all of
    989 		 * XXX the pckbc_machdep_cnattach() should be purged,
    990 		 * XXX as well.
    991 		 */
    992 #ifdef notyet
    993 		res = ENXIO;
    994 #else
    995 		res = pckbc_machdep_cnattach(&pckbc_consdata, slot);
    996 #endif
    997 #endif /* NPCKBD > 0 */
    998 	}
    999 
   1000 	if (res) {
   1001 		bus_space_unmap(iot, pckbc_consdata.t_ioh_d, 1);
   1002 		bus_space_unmap(iot, pckbc_consdata.t_ioh_c, 1);
   1003 	} else {
   1004 		pckbc_consdata.t_slotdata[slot] = &pckbc_cons_slotdata;
   1005 		pckbc_init_slotdata(&pckbc_cons_slotdata);
   1006 		pckbc_console = 1;
   1007 	}
   1008 
   1009 	return (res);
   1010 }
   1011