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