Home | History | Annotate | Line # | Download | only in ic
pckbc.c revision 1.8
      1 /* $NetBSD: pckbc.c,v 1.8 2001/05/17 10:48:39 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 != -1) {
    375 		/*
    376 		 * In most cases, the 0x5a gets echoed.
    377 		 * Some older controllers (Gateway 2000 circa 1993)
    378 		 * return 0xfe here.
    379 		 * We are satisfied if there is anything in the
    380 		 * aux output buffer.
    381 		 */
    382 		t->t_haveaux = 1;
    383 		if (pckbc_attach_slot(sc, PCKBC_AUX_SLOT))
    384 			cmdbits |= KC8_MENABLE;
    385 	}
    386 #ifdef PCKBCDEBUG
    387 	  else
    388 		printf("kbc: aux echo test failed\n");
    389 #endif
    390 
    391 nomouse:
    392 	/* enable needed interrupts */
    393 	t->t_cmdbyte |= cmdbits;
    394 	if (!pckbc_put8042cmd(t))
    395 		printf("kbc: cmd word write error\n");
    396 }
    397 
    398 int
    399 pckbcprint(aux, pnp)
    400 	void *aux;
    401 	const char *pnp;
    402 {
    403 	struct pckbc_attach_args *pa = aux;
    404 
    405 	if (!pnp)
    406 		printf(" (%s slot)", pckbc_slot_names[pa->pa_slot]);
    407 	return (QUIET);
    408 }
    409 
    410 void
    411 pckbc_init_slotdata(q)
    412 	struct pckbc_slotdata *q;
    413 {
    414 	int i;
    415 	TAILQ_INIT(&q->cmdqueue);
    416 	TAILQ_INIT(&q->freequeue);
    417 
    418 	for (i = 0; i < NCMD; i++) {
    419 		TAILQ_INSERT_TAIL(&q->freequeue, &(q->cmds[i]), next);
    420 	}
    421 	q->polling = 0;
    422 }
    423 
    424 void
    425 pckbc_flush(self, slot)
    426 	pckbc_tag_t self;
    427 	pckbc_slot_t slot;
    428 {
    429 	struct pckbc_internal *t = self;
    430 
    431 	(void) pckbc_poll_data1(t->t_iot, t->t_ioh_d, t->t_ioh_c,
    432 				slot, t->t_haveaux);
    433 }
    434 
    435 int
    436 pckbc_poll_data(self, slot)
    437 	pckbc_tag_t self;
    438 	pckbc_slot_t slot;
    439 {
    440 	struct pckbc_internal *t = self;
    441 	struct pckbc_slotdata *q = t->t_slotdata[slot];
    442 	int c;
    443 
    444 	c = pckbc_poll_data1(t->t_iot, t->t_ioh_d, t->t_ioh_c,
    445 			     slot, t->t_haveaux);
    446 	if (c != -1 && q && CMD_IN_QUEUE(q)) {
    447 		/* we jumped into a running command - try to
    448 		 deliver the response */
    449 		if (pckbc_cmdresponse(t, slot, c))
    450 			return (-1);
    451 	}
    452 	return (c);
    453 }
    454 
    455 /*
    456  * switch scancode translation on / off
    457  * return nonzero on success
    458  */
    459 int
    460 pckbc_xt_translation(self, slot, on)
    461 	pckbc_tag_t self;
    462 	pckbc_slot_t slot;
    463 	int on;
    464 {
    465 	struct pckbc_internal *t = self;
    466 	int ison;
    467 
    468 	if (slot != PCKBC_KBD_SLOT) {
    469 		/* translation only for kbd slot */
    470 		if (on)
    471 			return (0);
    472 		else
    473 			return (1);
    474 	}
    475 
    476 	ison = t->t_cmdbyte & KC8_TRANS;
    477 	if ((on && ison) || (!on && !ison))
    478 		return (1);
    479 
    480 	t->t_cmdbyte ^= KC8_TRANS;
    481 	if (!pckbc_put8042cmd(t))
    482 		return (0);
    483 
    484 	/* read back to be sure */
    485 	if (!pckbc_get8042cmd(t))
    486 		return (0);
    487 
    488 	ison = t->t_cmdbyte & KC8_TRANS;
    489 	if ((on && ison) || (!on && !ison))
    490 		return (1);
    491 	return (0);
    492 }
    493 
    494 static struct pckbc_portcmd {
    495 	u_char cmd_en, cmd_dis;
    496 } pckbc_portcmd[2] = {
    497 	{
    498 		KBC_KBDENABLE, KBC_KBDDISABLE,
    499 	}, {
    500 		KBC_AUXENABLE, KBC_AUXDISABLE,
    501 	}
    502 };
    503 
    504 void
    505 pckbc_slot_enable(self, slot, on)
    506 	pckbc_tag_t self;
    507 	pckbc_slot_t slot;
    508 	int on;
    509 {
    510 	struct pckbc_internal *t = (struct pckbc_internal *)self;
    511 	struct pckbc_portcmd *cmd;
    512 
    513 	cmd = &pckbc_portcmd[slot];
    514 
    515 	if (!pckbc_send_cmd(t->t_iot, t->t_ioh_c,
    516 			    on ? cmd->cmd_en : cmd->cmd_dis))
    517 		printf("pckbc_slot_enable(%d) failed\n", on);
    518 }
    519 
    520 void
    521 pckbc_set_poll(self, slot, on)
    522 	pckbc_tag_t self;
    523 	pckbc_slot_t slot;
    524 	int on;
    525 {
    526 	struct pckbc_internal *t = (struct pckbc_internal *)self;
    527 
    528 	t->t_slotdata[slot]->polling = on;
    529 
    530 	if (!on) {
    531                 int s;
    532 
    533                 /*
    534                  * If disabling polling on a device that's been configured,
    535                  * make sure there are no bytes left in the FIFO, holding up
    536                  * the interrupt line.  Otherwise we won't get any further
    537                  * interrupts.
    538                  */
    539 		if (t->t_sc) {
    540 			s = spltty();
    541 			pckbcintr(t->t_sc);
    542 			splx(s);
    543 		}
    544 	}
    545 }
    546 
    547 /*
    548  * Pass command to device, poll for ACK and data.
    549  * to be called at spltty()
    550  */
    551 static void
    552 pckbc_poll_cmd1(t, slot, cmd)
    553 	struct pckbc_internal *t;
    554 	pckbc_slot_t slot;
    555 	struct pckbc_devcmd *cmd;
    556 {
    557 	bus_space_tag_t iot = t->t_iot;
    558 	bus_space_handle_t ioh_d = t->t_ioh_d;
    559 	bus_space_handle_t ioh_c = t->t_ioh_c;
    560 	int i, c = 0;
    561 
    562 	while (cmd->cmdidx < cmd->cmdlen) {
    563 		if (!pckbc_send_devcmd(t, slot, cmd->cmd[cmd->cmdidx])) {
    564 			printf("pckbc_cmd: send error\n");
    565 			cmd->status = EIO;
    566 			return;
    567 		}
    568 		for (i = 10; i; i--) { /* 1s ??? */
    569 			c = pckbc_poll_data1(iot, ioh_d, ioh_c, slot,
    570 					     t->t_haveaux);
    571 			if (c != -1)
    572 				break;
    573 		}
    574 
    575 		if (c == KBC_DEVCMD_ACK) {
    576 			cmd->cmdidx++;
    577 			continue;
    578 		}
    579 		if (c == KBC_DEVCMD_RESEND) {
    580 #ifdef PCKBCDEBUG
    581 			printf("pckbc_cmd: RESEND\n");
    582 #endif
    583 			if (cmd->retries++ < 5)
    584 				continue;
    585 			else {
    586 #ifdef PCKBCDEBUG
    587 				printf("pckbc: cmd failed\n");
    588 #endif
    589 				cmd->status = EIO;
    590 				return;
    591 			}
    592 		}
    593 		if (c == -1) {
    594 #ifdef PCKBCDEBUG
    595 			printf("pckbc_cmd: timeout\n");
    596 #endif
    597 			cmd->status = EIO;
    598 			return;
    599 		}
    600 #ifdef PCKBCDEBUG
    601 		printf("pckbc_cmd: lost 0x%x\n", c);
    602 #endif
    603 	}
    604 
    605 	while (cmd->responseidx < cmd->responselen) {
    606 		if (cmd->flags & KBC_CMDFLAG_SLOW)
    607 			i = 100; /* 10s ??? */
    608 		else
    609 			i = 10; /* 1s ??? */
    610 		while (i--) {
    611 			c = pckbc_poll_data1(iot, ioh_d, ioh_c, slot,
    612 					     t->t_haveaux);
    613 			if (c != -1)
    614 				break;
    615 		}
    616 		if (c == -1) {
    617 #ifdef PCKBCDEBUG
    618 			printf("pckbc_cmd: no data\n");
    619 #endif
    620 			cmd->status = ETIMEDOUT;
    621 			return;
    622 		} else
    623 			cmd->response[cmd->responseidx++] = c;
    624 	}
    625 }
    626 
    627 /* for use in autoconfiguration */
    628 int
    629 pckbc_poll_cmd(self, slot, cmd, len, responselen, respbuf, slow)
    630 	pckbc_tag_t self;
    631 	pckbc_slot_t slot;
    632 	u_char *cmd;
    633 	int len, responselen;
    634 	u_char *respbuf;
    635 	int slow;
    636 {
    637 	struct pckbc_internal *t = self;
    638 	struct pckbc_devcmd nc;
    639 
    640 	if ((len > 4) || (responselen > 4))
    641 		return (EINVAL);
    642 
    643 	bzero(&nc, sizeof(nc));
    644 	bcopy(cmd, nc.cmd, len);
    645 	nc.cmdlen = len;
    646 	nc.responselen = responselen;
    647 	nc.flags = (slow ? KBC_CMDFLAG_SLOW : 0);
    648 
    649 	pckbc_poll_cmd1(t, slot, &nc);
    650 
    651 	if (nc.status == 0 && respbuf)
    652 		bcopy(nc.response, respbuf, responselen);
    653 
    654 	return (nc.status);
    655 }
    656 
    657 /*
    658  * Clean up a command queue, throw away everything.
    659  */
    660 void
    661 pckbc_cleanqueue(q)
    662 	struct pckbc_slotdata *q;
    663 {
    664 	struct pckbc_devcmd *cmd;
    665 #ifdef PCKBCDEBUG
    666 	int i;
    667 #endif
    668 
    669 	while ((cmd = TAILQ_FIRST(&q->cmdqueue))) {
    670 		TAILQ_REMOVE(&q->cmdqueue, cmd, next);
    671 #ifdef PCKBCDEBUG
    672 		printf("pckbc_cleanqueue: removing");
    673 		for (i = 0; i < cmd->cmdlen; i++)
    674 			printf(" %02x", cmd->cmd[i]);
    675 		printf("\n");
    676 #endif
    677 		TAILQ_INSERT_TAIL(&q->freequeue, cmd, next);
    678 	}
    679 }
    680 
    681 /*
    682  * Timeout error handler: clean queues and data port.
    683  * XXX could be less invasive.
    684  */
    685 void
    686 pckbc_cleanup(self)
    687 	void *self;
    688 {
    689 	struct pckbc_internal *t = self;
    690 	int s;
    691 
    692 	printf("pckbc: command timeout\n");
    693 
    694 	s = spltty();
    695 
    696 	if (t->t_slotdata[PCKBC_KBD_SLOT])
    697 		pckbc_cleanqueue(t->t_slotdata[PCKBC_KBD_SLOT]);
    698 	if (t->t_slotdata[PCKBC_AUX_SLOT])
    699 		pckbc_cleanqueue(t->t_slotdata[PCKBC_AUX_SLOT]);
    700 
    701 	while (bus_space_read_1(t->t_iot, t->t_ioh_c, 0) & KBS_DIB) {
    702 		KBD_DELAY;
    703 		(void) bus_space_read_1(t->t_iot, t->t_ioh_d, 0);
    704 	}
    705 
    706 	/* reset KBC? */
    707 
    708 	splx(s);
    709 }
    710 
    711 /*
    712  * Pass command to device during normal operation.
    713  * to be called at spltty()
    714  */
    715 void
    716 pckbc_start(t, slot)
    717 	struct pckbc_internal *t;
    718 	pckbc_slot_t slot;
    719 {
    720 	struct pckbc_slotdata *q = t->t_slotdata[slot];
    721 	struct pckbc_devcmd *cmd = TAILQ_FIRST(&q->cmdqueue);
    722 
    723 	if (q->polling) {
    724 		do {
    725 			pckbc_poll_cmd1(t, slot, cmd);
    726 			if (cmd->status)
    727 				printf("pckbc_start: command error\n");
    728 
    729 			TAILQ_REMOVE(&q->cmdqueue, cmd, next);
    730 			if (cmd->flags & KBC_CMDFLAG_SYNC)
    731 				wakeup(cmd);
    732 			else {
    733 				callout_stop(&t->t_cleanup);
    734 				TAILQ_INSERT_TAIL(&q->freequeue, cmd, next);
    735 			}
    736 			cmd = TAILQ_FIRST(&q->cmdqueue);
    737 		} while (cmd);
    738 		return;
    739 	}
    740 
    741 	if (!pckbc_send_devcmd(t, slot, cmd->cmd[cmd->cmdidx])) {
    742 		printf("pckbc_start: send error\n");
    743 		/* XXX what now? */
    744 		return;
    745 	}
    746 }
    747 
    748 /*
    749  * Handle command responses coming in asynchonously,
    750  * return nonzero if valid response.
    751  * to be called at spltty()
    752  */
    753 int
    754 pckbc_cmdresponse(t, slot, data)
    755 	struct pckbc_internal *t;
    756 	pckbc_slot_t slot;
    757 	u_char data;
    758 {
    759 	struct pckbc_slotdata *q = t->t_slotdata[slot];
    760 	struct pckbc_devcmd *cmd = TAILQ_FIRST(&q->cmdqueue);
    761 #ifdef DIAGNOSTIC
    762 	if (!cmd)
    763 		panic("pckbc_cmdresponse: no active command");
    764 #endif
    765 	if (cmd->cmdidx < cmd->cmdlen) {
    766 		if (data != KBC_DEVCMD_ACK && data != KBC_DEVCMD_RESEND)
    767 			return (0);
    768 
    769 		if (data == KBC_DEVCMD_RESEND) {
    770 			if (cmd->retries++ < 5) {
    771 				/* try again last command */
    772 				goto restart;
    773 			} else {
    774 				printf("pckbc: cmd failed\n");
    775 				cmd->status = EIO;
    776 				/* dequeue */
    777 			}
    778 		} else {
    779 			if (++cmd->cmdidx < cmd->cmdlen)
    780 				goto restart;
    781 			if (cmd->responselen)
    782 				return (1);
    783 			/* else dequeue */
    784 		}
    785 	} else if (cmd->responseidx < cmd->responselen) {
    786 		cmd->response[cmd->responseidx++] = data;
    787 		if (cmd->responseidx < cmd->responselen)
    788 			return (1);
    789 		/* else dequeue */
    790 	} else
    791 		return (0);
    792 
    793 	/* dequeue: */
    794 	TAILQ_REMOVE(&q->cmdqueue, cmd, next);
    795 	if (cmd->flags & KBC_CMDFLAG_SYNC)
    796 		wakeup(cmd);
    797 	else {
    798 		callout_stop(&t->t_cleanup);
    799 		TAILQ_INSERT_TAIL(&q->freequeue, cmd, next);
    800 	}
    801 	if (!CMD_IN_QUEUE(q))
    802 		return (1);
    803 restart:
    804 	pckbc_start(t, slot);
    805 	return (1);
    806 }
    807 
    808 /*
    809  * Put command into the device's command queue, return zero or errno.
    810  */
    811 int
    812 pckbc_enqueue_cmd(self, slot, cmd, len, responselen, sync, respbuf)
    813 	pckbc_tag_t self;
    814 	pckbc_slot_t slot;
    815 	u_char *cmd;
    816 	int len, responselen, sync;
    817 	u_char *respbuf;
    818 {
    819 	struct pckbc_internal *t = self;
    820 	struct pckbc_slotdata *q = t->t_slotdata[slot];
    821 	struct pckbc_devcmd *nc;
    822 	int s, isactive, res = 0;
    823 
    824 	if ((len > 4) || (responselen > 4))
    825 		return (EINVAL);
    826 	s = spltty();
    827 	nc = TAILQ_FIRST(&q->freequeue);
    828 	if (nc) {
    829 		TAILQ_REMOVE(&q->freequeue, nc, next);
    830 	}
    831 	splx(s);
    832 	if (!nc)
    833 		return (ENOMEM);
    834 
    835 	bzero(nc, sizeof(*nc));
    836 	bcopy(cmd, nc->cmd, len);
    837 	nc->cmdlen = len;
    838 	nc->responselen = responselen;
    839 	nc->flags = (sync ? KBC_CMDFLAG_SYNC : 0);
    840 
    841 	s = spltty();
    842 
    843 	if (q->polling && sync) {
    844 		/*
    845 		 * XXX We should poll until the queue is empty.
    846 		 * But we don't come here normally, so make
    847 		 * it simple and throw away everything.
    848 		 */
    849 		pckbc_cleanqueue(q);
    850 	}
    851 
    852 	isactive = CMD_IN_QUEUE(q);
    853 	TAILQ_INSERT_TAIL(&q->cmdqueue, nc, next);
    854 	if (!isactive)
    855 		pckbc_start(t, slot);
    856 
    857 	if (q->polling)
    858 		res = (sync ? nc->status : 0);
    859 	else if (sync) {
    860 		if ((res = tsleep(nc, 0, "kbccmd", 1*hz))) {
    861 			TAILQ_REMOVE(&q->cmdqueue, nc, next);
    862 			pckbc_cleanup(t);
    863 		} else
    864 			res = nc->status;
    865 	} else
    866 		callout_reset(&t->t_cleanup, hz, pckbc_cleanup, t);
    867 
    868 	if (sync) {
    869 		if (respbuf)
    870 			bcopy(nc->response, respbuf, responselen);
    871 		TAILQ_INSERT_TAIL(&q->freequeue, nc, next);
    872 	}
    873 
    874 	splx(s);
    875 
    876 	return (res);
    877 }
    878 
    879 void
    880 pckbc_set_inputhandler(self, slot, func, arg, name)
    881 	pckbc_tag_t self;
    882 	pckbc_slot_t slot;
    883 	pckbc_inputfcn func;
    884 	void *arg;
    885 	char *name;
    886 {
    887 	struct pckbc_internal *t = (struct pckbc_internal *)self;
    888 	struct pckbc_softc *sc = t->t_sc;
    889 
    890 	if (slot >= PCKBC_NSLOTS)
    891 		panic("pckbc_set_inputhandler: bad slot %d", slot);
    892 
    893 	(*sc->intr_establish)(sc, slot);
    894 
    895 	sc->inputhandler[slot] = func;
    896 	sc->inputarg[slot] = arg;
    897 	sc->subname[slot] = name;
    898 }
    899 
    900 int
    901 pckbcintr(vsc)
    902 	void *vsc;
    903 {
    904 	struct pckbc_softc *sc = (struct pckbc_softc *)vsc;
    905 	struct pckbc_internal *t = sc->id;
    906 	u_char stat;
    907 	pckbc_slot_t slot;
    908 	struct pckbc_slotdata *q;
    909 	int served = 0, data;
    910 
    911 	for(;;) {
    912 		stat = bus_space_read_1(t->t_iot, t->t_ioh_c, 0);
    913 		if (!(stat & KBS_DIB))
    914 			break;
    915 
    916 		served = 1;
    917 
    918 		slot = (t->t_haveaux && (stat & 0x20)) ?
    919 		    PCKBC_AUX_SLOT : PCKBC_KBD_SLOT;
    920 		q = t->t_slotdata[slot];
    921 
    922 		if (!q) {
    923 			/* XXX do something for live insertion? */
    924 			printf("pckbcintr: no dev for slot %d\n", slot);
    925 			KBD_DELAY;
    926 			(void) bus_space_read_1(t->t_iot, t->t_ioh_d, 0);
    927 			continue;
    928 		}
    929 
    930 		if (q->polling)
    931 			break; /* pckbc_poll_data() will get it */
    932 
    933 		KBD_DELAY;
    934 		data = bus_space_read_1(t->t_iot, t->t_ioh_d, 0);
    935 
    936 #if NRND > 0
    937 		rnd_add_uint32(&q->rnd_source, (stat<<8)|data);
    938 #endif
    939 		if (CMD_IN_QUEUE(q) && pckbc_cmdresponse(t, slot, data))
    940 			continue;
    941 
    942 		if (sc->inputhandler[slot])
    943 			(*sc->inputhandler[slot])(sc->inputarg[slot], data);
    944 #ifdef PCKBCDEBUG
    945 		else
    946 			printf("pckbcintr: slot %d lost %d\n", slot, data);
    947 #endif
    948 	}
    949 
    950 	return (served);
    951 }
    952 
    953 int
    954 pckbc_cnattach(iot, addr, cmd_offset, slot)
    955 	bus_space_tag_t iot;
    956 	bus_addr_t addr;
    957 	bus_size_t cmd_offset;
    958 	pckbc_slot_t slot;
    959 {
    960 	bus_space_handle_t ioh_d, ioh_c;
    961 	int res = 0;
    962 
    963 	if (bus_space_map(iot, addr + KBDATAP, 1, 0, &ioh_d))
    964                 return (ENXIO);
    965 	if (bus_space_map(iot, addr + cmd_offset, 1, 0, &ioh_c)) {
    966 		bus_space_unmap(iot, ioh_d, 1);
    967                 return (ENXIO);
    968 	}
    969 
    970 	pckbc_consdata.t_iot = iot;
    971 	pckbc_consdata.t_ioh_d = ioh_d;
    972 	pckbc_consdata.t_ioh_c = ioh_c;
    973 	pckbc_consdata.t_addr = addr;
    974 	callout_init(&pckbc_consdata.t_cleanup);
    975 
    976 	/* flush */
    977 	(void) pckbc_poll_data1(iot, ioh_d, ioh_c, PCKBC_KBD_SLOT, 0);
    978 
    979 	/* selftest? */
    980 
    981 	/* init cmd byte, enable ports */
    982 	pckbc_consdata.t_cmdbyte = KC8_CPU;
    983 	if (!pckbc_put8042cmd(&pckbc_consdata)) {
    984 		printf("kbc: cmd word write error\n");
    985 		res = EIO;
    986 	}
    987 
    988 	if (!res) {
    989 #if (NPCKBD > 0)
    990 		res = pckbd_cnattach(&pckbc_consdata, slot);
    991 #else
    992 		/*
    993 		 * XXX This should be replaced with the `notyet' case
    994 		 * XXX when all of the old PC-style console drivers
    995 		 * XXX have gone away.  When that happens, all of
    996 		 * XXX the pckbc_machdep_cnattach() should be purged,
    997 		 * XXX as well.
    998 		 */
    999 #ifdef notyet
   1000 		res = ENXIO;
   1001 #else
   1002 		res = pckbc_machdep_cnattach(&pckbc_consdata, slot);
   1003 #endif
   1004 #endif /* NPCKBD > 0 */
   1005 	}
   1006 
   1007 	if (res) {
   1008 		bus_space_unmap(iot, pckbc_consdata.t_ioh_d, 1);
   1009 		bus_space_unmap(iot, pckbc_consdata.t_ioh_c, 1);
   1010 	} else {
   1011 		pckbc_consdata.t_slotdata[slot] = &pckbc_cons_slotdata;
   1012 		pckbc_init_slotdata(&pckbc_cons_slotdata);
   1013 		pckbc_console = 1;
   1014 	}
   1015 
   1016 	return (res);
   1017 }
   1018