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