Home | History | Annotate | Line # | Download | only in pckbport
pckbd.c revision 1.15.16.3
      1  1.15.16.3      matt /* pckbd.c,v 1.15.16.2 2008/01/09 01:54:13 matt Exp */
      2        1.1     bjh21 
      3        1.1     bjh21 /*-
      4        1.1     bjh21  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      5        1.1     bjh21  * All rights reserved.
      6        1.1     bjh21  *
      7        1.1     bjh21  * This code is derived from software contributed to The NetBSD Foundation
      8        1.1     bjh21  * by Charles M. Hannum.
      9        1.1     bjh21  *
     10        1.1     bjh21  * Redistribution and use in source and binary forms, with or without
     11        1.1     bjh21  * modification, are permitted provided that the following conditions
     12        1.1     bjh21  * are met:
     13        1.1     bjh21  * 1. Redistributions of source code must retain the above copyright
     14        1.1     bjh21  *    notice, this list of conditions and the following disclaimer.
     15        1.1     bjh21  * 2. Redistributions in binary form must reproduce the above copyright
     16        1.1     bjh21  *    notice, this list of conditions and the following disclaimer in the
     17        1.1     bjh21  *    documentation and/or other materials provided with the distribution.
     18        1.1     bjh21  * 3. All advertising materials mentioning features or use of this software
     19        1.1     bjh21  *    must display the following acknowledgement:
     20        1.1     bjh21  *        This product includes software developed by the NetBSD
     21        1.1     bjh21  *        Foundation, Inc. and its contributors.
     22        1.1     bjh21  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23        1.1     bjh21  *    contributors may be used to endorse or promote products derived
     24        1.1     bjh21  *    from this software without specific prior written permission.
     25        1.1     bjh21  *
     26        1.1     bjh21  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27        1.1     bjh21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28        1.1     bjh21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29        1.1     bjh21  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30        1.1     bjh21  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31        1.1     bjh21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32        1.1     bjh21  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33        1.1     bjh21  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34        1.1     bjh21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35        1.1     bjh21  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36        1.1     bjh21  * POSSIBILITY OF SUCH DAMAGE.
     37        1.1     bjh21  */
     38        1.1     bjh21 
     39        1.1     bjh21 /*-
     40        1.1     bjh21  * Copyright (c) 1990 The Regents of the University of California.
     41        1.1     bjh21  * All rights reserved.
     42        1.1     bjh21  *
     43        1.1     bjh21  * This code is derived from software contributed to Berkeley by
     44        1.1     bjh21  * William Jolitz and Don Ahn.
     45        1.1     bjh21  *
     46        1.1     bjh21  * Redistribution and use in source and binary forms, with or without
     47        1.1     bjh21  * modification, are permitted provided that the following conditions
     48        1.1     bjh21  * are met:
     49        1.1     bjh21  * 1. Redistributions of source code must retain the above copyright
     50        1.1     bjh21  *    notice, this list of conditions and the following disclaimer.
     51        1.1     bjh21  * 2. Redistributions in binary form must reproduce the above copyright
     52        1.1     bjh21  *    notice, this list of conditions and the following disclaimer in the
     53        1.1     bjh21  *    documentation and/or other materials provided with the distribution.
     54        1.1     bjh21  * 3. Neither the name of the University nor the names of its contributors
     55        1.1     bjh21  *    may be used to endorse or promote products derived from this software
     56        1.1     bjh21  *    without specific prior written permission.
     57        1.1     bjh21  *
     58        1.1     bjh21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     59        1.1     bjh21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     60        1.1     bjh21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     61        1.1     bjh21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     62        1.1     bjh21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     63        1.1     bjh21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     64        1.1     bjh21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     65        1.1     bjh21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     66        1.1     bjh21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     67        1.1     bjh21  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     68        1.1     bjh21  * SUCH DAMAGE.
     69        1.1     bjh21  *
     70        1.1     bjh21  *	@(#)pccons.c	5.11 (Berkeley) 5/21/91
     71        1.1     bjh21  */
     72        1.1     bjh21 
     73        1.1     bjh21 /*
     74        1.1     bjh21  * code to work keyboard for PC-style console
     75        1.1     bjh21  */
     76        1.1     bjh21 
     77        1.1     bjh21 #include <sys/cdefs.h>
     78  1.15.16.3      matt __KERNEL_RCSID(0, "pckbd.c,v 1.15.16.2 2008/01/09 01:54:13 matt Exp");
     79        1.1     bjh21 
     80        1.1     bjh21 #include <sys/param.h>
     81        1.1     bjh21 #include <sys/systm.h>
     82        1.1     bjh21 #include <sys/device.h>
     83        1.1     bjh21 #include <sys/malloc.h>
     84        1.1     bjh21 #include <sys/ioctl.h>
     85        1.1     bjh21 
     86  1.15.16.1      matt #include <sys/bus.h>
     87        1.1     bjh21 
     88        1.1     bjh21 #include <dev/pckbport/pckbportvar.h>
     89        1.1     bjh21 
     90        1.1     bjh21 #include <dev/wscons/wsconsio.h>
     91        1.1     bjh21 #include <dev/wscons/wskbdvar.h>
     92        1.1     bjh21 #include <dev/wscons/wsksymdef.h>
     93        1.1     bjh21 #include <dev/wscons/wsksymvar.h>
     94        1.1     bjh21 
     95        1.6      yamt #include <dev/pckbport/pckbdreg.h>
     96        1.6      yamt #include <dev/pckbport/pckbdvar.h>
     97        1.6      yamt #include <dev/pckbport/wskbdmap_mfii.h>
     98        1.6      yamt 
     99        1.1     bjh21 #include "locators.h"
    100        1.1     bjh21 
    101        1.1     bjh21 #include "opt_pckbd_layout.h"
    102        1.7  augustss #include "opt_pckbd_cnattach_may_fail.h"
    103        1.1     bjh21 #include "opt_wsdisplay_compat.h"
    104        1.1     bjh21 
    105        1.1     bjh21 struct pckbd_internal {
    106        1.1     bjh21 	int t_isconsole;
    107        1.1     bjh21 	pckbport_tag_t t_kbctag;
    108        1.1     bjh21 	pckbport_slot_t t_kbcslot;
    109        1.1     bjh21 
    110        1.1     bjh21 	int t_lastchar;
    111        1.1     bjh21 	int t_extended0;
    112        1.1     bjh21 	int t_extended1;
    113        1.1     bjh21 
    114        1.1     bjh21 	struct pckbd_softc *t_sc; /* back pointer */
    115        1.1     bjh21 };
    116        1.1     bjh21 
    117        1.1     bjh21 struct pckbd_softc {
    118  1.15.16.3      matt         device_t sc_dev;
    119        1.1     bjh21 
    120        1.1     bjh21 	struct pckbd_internal *id;
    121        1.1     bjh21 	int sc_enabled;
    122        1.1     bjh21 
    123        1.1     bjh21 	int sc_ledstate;
    124        1.1     bjh21 
    125  1.15.16.3      matt 	device_t sc_wskbddev;
    126        1.1     bjh21 #ifdef WSDISPLAY_COMPAT_RAWKBD
    127        1.1     bjh21 	int rawkbd;
    128        1.1     bjh21 #endif
    129        1.1     bjh21 };
    130        1.1     bjh21 
    131        1.2     bjh21 static int pckbd_is_console(pckbport_tag_t, pckbport_slot_t);
    132        1.1     bjh21 
    133  1.15.16.3      matt int pckbdprobe(device_t, cfdata_t, void *);
    134  1.15.16.3      matt void pckbdattach(device_t, device_t, void *);
    135        1.1     bjh21 
    136  1.15.16.3      matt CFATTACH_DECL_NEW(pckbd, sizeof(struct pckbd_softc),
    137        1.1     bjh21     pckbdprobe, pckbdattach, NULL, NULL);
    138        1.1     bjh21 
    139        1.2     bjh21 int	pckbd_enable(void *, int);
    140        1.2     bjh21 void	pckbd_set_leds(void *, int);
    141       1.15  christos int	pckbd_ioctl(void *, u_long, void *, int, struct lwp *);
    142        1.1     bjh21 
    143        1.1     bjh21 const struct wskbd_accessops pckbd_accessops = {
    144        1.1     bjh21 	pckbd_enable,
    145        1.1     bjh21 	pckbd_set_leds,
    146        1.1     bjh21 	pckbd_ioctl,
    147        1.1     bjh21 };
    148        1.1     bjh21 
    149        1.2     bjh21 void	pckbd_cngetc(void *, u_int *, int *);
    150        1.2     bjh21 void	pckbd_cnpollc(void *, int);
    151        1.2     bjh21 void	pckbd_cnbell(void *, u_int, u_int, u_int);
    152        1.1     bjh21 
    153        1.1     bjh21 const struct wskbd_consops pckbd_consops = {
    154        1.1     bjh21 	pckbd_cngetc,
    155        1.1     bjh21 	pckbd_cnpollc,
    156        1.1     bjh21 	pckbd_cnbell,
    157        1.1     bjh21 };
    158        1.1     bjh21 
    159        1.1     bjh21 const struct wskbd_mapdata pckbd_keymapdata = {
    160        1.1     bjh21 	pckbd_keydesctab,
    161        1.1     bjh21 #ifdef PCKBD_LAYOUT
    162        1.1     bjh21 	PCKBD_LAYOUT,
    163        1.1     bjh21 #else
    164        1.5      matt 	KB_US,
    165        1.1     bjh21 #endif
    166        1.1     bjh21 };
    167        1.1     bjh21 
    168        1.1     bjh21 /*
    169        1.1     bjh21  * Hackish support for a bell on the PC Keyboard; when a suitable feeper
    170        1.1     bjh21  * is found, it attaches itself into the pckbd driver here.
    171        1.1     bjh21  */
    172        1.2     bjh21 void	(*pckbd_bell_fn)(void *, u_int, u_int, u_int, int);
    173        1.1     bjh21 void	*pckbd_bell_fn_arg;
    174        1.1     bjh21 
    175        1.2     bjh21 void	pckbd_bell(u_int, u_int, u_int, int);
    176        1.1     bjh21 
    177        1.2     bjh21 int	pckbd_set_xtscancode(pckbport_tag_t, pckbport_slot_t);
    178        1.2     bjh21 int	pckbd_init(struct pckbd_internal *, pckbport_tag_t, pckbport_slot_t,
    179        1.2     bjh21 			int);
    180        1.2     bjh21 void	pckbd_input(void *, int);
    181        1.2     bjh21 
    182        1.2     bjh21 static int	pckbd_decode(struct pckbd_internal *, int, u_int *, int *);
    183        1.2     bjh21 static int	pckbd_led_encode(int);
    184        1.2     bjh21 static int	pckbd_led_decode(int);
    185        1.1     bjh21 
    186        1.1     bjh21 struct pckbd_internal pckbd_consdata;
    187        1.1     bjh21 
    188        1.1     bjh21 int
    189        1.2     bjh21 pckbd_set_xtscancode(pckbport_tag_t kbctag, pckbport_slot_t kbcslot)
    190        1.1     bjh21 {
    191        1.2     bjh21 	int res;
    192        1.1     bjh21 	u_char cmd[2];
    193        1.1     bjh21 
    194        1.1     bjh21 	/*
    195        1.1     bjh21 	 * Some keyboard/8042 combinations do not seem to work if the keyboard
    196        1.1     bjh21 	 * is set to table 1; in fact, it would appear that some keyboards just
    197        1.1     bjh21 	 * ignore the command altogether.  So by default, we use the AT scan
    198        1.1     bjh21 	 * codes and have the 8042 translate them.  Unfortunately, this is
    199        1.1     bjh21 	 * known to not work on some PS/2 machines.  We try desperately to deal
    200        1.1     bjh21 	 * with this by checking the (lack of a) translate bit in the 8042 and
    201        1.1     bjh21 	 * attempting to set the keyboard to XT mode.  If this all fails, well,
    202        1.1     bjh21 	 * tough luck.
    203        1.1     bjh21 	 *
    204        1.1     bjh21 	 * XXX It would perhaps be a better choice to just use AT scan codes
    205        1.1     bjh21 	 * and not bother with this.
    206        1.1     bjh21 	 */
    207        1.1     bjh21 	if (pckbport_xt_translation(kbctag, kbcslot, 1)) {
    208        1.1     bjh21 		/* The 8042 is translating for us; use AT codes. */
    209        1.1     bjh21 		cmd[0] = KBC_SETTABLE;
    210        1.1     bjh21 		cmd[1] = 2;
    211        1.1     bjh21 		res = pckbport_poll_cmd(kbctag, kbcslot, cmd, 2, 0, 0, 0);
    212        1.1     bjh21 		if (res) {
    213        1.8  christos 			u_char cmdb[1];
    214        1.1     bjh21 #ifdef DEBUG
    215        1.1     bjh21 			printf("pckbd: error setting scanset 2\n");
    216        1.1     bjh21 #endif
    217        1.1     bjh21 			/*
    218        1.1     bjh21 			 * XXX at least one keyboard is reported to lock up
    219        1.1     bjh21 			 * if a "set table" is attempted, thus the "reset".
    220        1.1     bjh21 			 * XXX ignore errors, scanset 2 should be
    221        1.1     bjh21 			 * default anyway.
    222        1.1     bjh21 			 */
    223        1.8  christos 			cmdb[0] = KBC_RESET;
    224        1.8  christos 			(void)pckbport_poll_cmd(kbctag, kbcslot, cmdb, 1, 1, 0, 1);
    225        1.1     bjh21 			pckbport_flush(kbctag, kbcslot);
    226        1.1     bjh21 			res = 0;
    227        1.1     bjh21 		}
    228        1.1     bjh21 	} else {
    229        1.1     bjh21 		/* Stupid 8042; set keyboard to XT codes. */
    230        1.1     bjh21 		cmd[0] = KBC_SETTABLE;
    231        1.1     bjh21 		cmd[1] = 1;
    232        1.1     bjh21 		res = pckbport_poll_cmd(kbctag, kbcslot, cmd, 2, 0, 0, 0);
    233        1.1     bjh21 #ifdef DEBUG
    234        1.1     bjh21 		if (res)
    235        1.1     bjh21 			printf("pckbd: error setting scanset 1\n");
    236        1.1     bjh21 #endif
    237        1.1     bjh21 	}
    238        1.2     bjh21 	return res;
    239        1.1     bjh21 }
    240        1.1     bjh21 
    241        1.1     bjh21 static int
    242        1.2     bjh21 pckbd_is_console(pckbport_tag_t tag, pckbport_slot_t slot)
    243        1.2     bjh21 {
    244        1.2     bjh21 
    245        1.2     bjh21 	return pckbd_consdata.t_isconsole &&
    246        1.2     bjh21 	    tag == pckbd_consdata.t_kbctag && slot == pckbd_consdata.t_kbcslot;
    247        1.1     bjh21 }
    248        1.1     bjh21 
    249  1.15.16.2      matt static bool
    250  1.15.16.3      matt pckbd_suspend(device_t dv PMF_FN_ARGS)
    251       1.11  christos {
    252  1.15.16.2      matt 	struct pckbd_softc *sc = device_private(dv);
    253  1.15.16.2      matt 	u_char cmd[1];
    254       1.11  christos 	int res;
    255       1.11  christos 
    256  1.15.16.2      matt 	/* XXX duped from pckbd_enable, but we want to disable
    257  1.15.16.2      matt 	 *     it even if it's the console kbd
    258  1.15.16.2      matt 	 */
    259  1.15.16.2      matt 	cmd[0] = KBC_DISABLE;
    260  1.15.16.2      matt 	res = pckbport_enqueue_cmd(sc->id->t_kbctag,
    261  1.15.16.2      matt 	    sc->id->t_kbcslot, cmd, 1, 0, 1, 0);
    262  1.15.16.2      matt 	if (res)
    263  1.15.16.2      matt 		return false;
    264       1.11  christos 
    265  1.15.16.2      matt 	pckbport_slot_enable(sc->id->t_kbctag,
    266  1.15.16.2      matt 	    sc->id->t_kbcslot, 0);
    267       1.11  christos 
    268  1.15.16.2      matt 	sc->sc_enabled = 0;
    269  1.15.16.2      matt 	return true;
    270  1.15.16.2      matt }
    271       1.11  christos 
    272  1.15.16.2      matt static bool
    273  1.15.16.3      matt pckbd_resume(device_t dv PMF_FN_ARGS)
    274  1.15.16.2      matt {
    275  1.15.16.2      matt 	struct pckbd_softc *sc = device_private(dv);
    276  1.15.16.2      matt 	u_char cmd[1], resp[1];
    277  1.15.16.2      matt 	int res;
    278       1.11  christos 
    279  1.15.16.2      matt 	/* XXX jmcneill reset the keyboard */
    280  1.15.16.2      matt 	pckbport_flush(sc->id->t_kbctag, sc->id->t_kbcslot);
    281  1.15.16.2      matt 
    282  1.15.16.2      matt 	cmd[0] = KBC_RESET;
    283  1.15.16.2      matt 	res = pckbport_poll_cmd(sc->id->t_kbctag,
    284  1.15.16.2      matt 	    sc->id->t_kbcslot, cmd, 1, 1, resp, 1);
    285       1.11  christos #ifdef DEBUG
    286  1.15.16.2      matt 	if (res)
    287  1.15.16.2      matt 		printf("pckbdprobe: reset error %d\n", res);
    288       1.11  christos #endif
    289  1.15.16.2      matt 	if (resp[0] != KBR_RSTDONE)
    290  1.15.16.2      matt 		printf("pckbdprobe: reset response 0x%x\n",
    291  1.15.16.2      matt 		    resp[0]);
    292       1.11  christos 
    293  1.15.16.2      matt 	pckbport_flush(sc->id->t_kbctag, sc->id->t_kbcslot);
    294       1.11  christos 
    295  1.15.16.2      matt 	pckbd_enable(sc, 1);
    296       1.11  christos 
    297  1.15.16.2      matt 	return true;
    298       1.11  christos }
    299       1.11  christos 
    300        1.1     bjh21 /*
    301        1.1     bjh21  * these are both bad jokes
    302        1.1     bjh21  */
    303        1.1     bjh21 int
    304  1.15.16.3      matt pckbdprobe(device_t parent, cfdata_t cf, void *aux)
    305        1.1     bjh21 {
    306        1.1     bjh21 	struct pckbport_attach_args *pa = aux;
    307        1.2     bjh21 	int res;
    308        1.1     bjh21 	u_char cmd[1], resp[1];
    309        1.1     bjh21 
    310        1.1     bjh21 	/*
    311        1.1     bjh21 	 * XXX There are rumours that a keyboard can be connected
    312        1.1     bjh21 	 * to the aux port as well. For me, this didn't work.
    313        1.1     bjh21 	 * For further experiments, allow it if explicitly
    314        1.1     bjh21 	 * wired in the config file.
    315        1.1     bjh21 	 */
    316        1.1     bjh21 	if ((pa->pa_slot != PCKBPORT_KBD_SLOT) &&
    317        1.1     bjh21 	    (cf->cf_loc[PCKBPORTCF_SLOT] == PCKBPORTCF_SLOT_DEFAULT))
    318        1.2     bjh21 		return 0;
    319        1.1     bjh21 
    320        1.1     bjh21 	/* Flush any garbage. */
    321        1.1     bjh21 	pckbport_flush(pa->pa_tag, pa->pa_slot);
    322        1.1     bjh21 
    323        1.1     bjh21 	/* Reset the keyboard. */
    324        1.1     bjh21 	cmd[0] = KBC_RESET;
    325        1.1     bjh21 	res = pckbport_poll_cmd(pa->pa_tag, pa->pa_slot, cmd, 1, 1, resp, 1);
    326        1.1     bjh21 	if (res) {
    327        1.1     bjh21 #ifdef DEBUG
    328        1.1     bjh21 		printf("pckbdprobe: reset error %d\n", res);
    329        1.1     bjh21 #endif
    330        1.1     bjh21 		/*
    331        1.1     bjh21 		 * There is probably no keyboard connected.
    332        1.1     bjh21 		 * Let the probe succeed if the keyboard is used
    333        1.1     bjh21 		 * as console input - it can be connected later.
    334        1.1     bjh21 		 */
    335        1.2     bjh21 		return pckbd_is_console(pa->pa_tag, pa->pa_slot) ? 1 : 0;
    336        1.1     bjh21 	}
    337        1.1     bjh21 	if (resp[0] != KBR_RSTDONE) {
    338        1.1     bjh21 		printf("pckbdprobe: reset response 0x%x\n", resp[0]);
    339        1.2     bjh21 		return 0;
    340        1.1     bjh21 	}
    341        1.1     bjh21 
    342        1.1     bjh21 	/*
    343        1.1     bjh21 	 * Some keyboards seem to leave a second ack byte after the reset.
    344        1.1     bjh21 	 * This is kind of stupid, but we account for them anyway by just
    345        1.1     bjh21 	 * flushing the buffer.
    346        1.1     bjh21 	 */
    347        1.1     bjh21 	pckbport_flush(pa->pa_tag, pa->pa_slot);
    348        1.1     bjh21 
    349        1.1     bjh21 	if (pckbd_set_xtscancode(pa->pa_tag, pa->pa_slot))
    350        1.2     bjh21 		return 0;
    351        1.1     bjh21 
    352        1.2     bjh21 	return 2;
    353        1.1     bjh21 }
    354        1.1     bjh21 
    355        1.1     bjh21 void
    356  1.15.16.3      matt pckbdattach(device_t parent, device_t self, void *aux)
    357        1.1     bjh21 {
    358       1.10   thorpej 	struct pckbd_softc *sc = device_private(self);
    359        1.1     bjh21 	struct pckbport_attach_args *pa = aux;
    360        1.2     bjh21 	struct wskbddev_attach_args a;
    361        1.1     bjh21 	int isconsole;
    362        1.1     bjh21 	u_char cmd[1];
    363        1.1     bjh21 
    364  1.15.16.2      matt 	aprint_naive("\n");
    365  1.15.16.2      matt 	aprint_normal("\n");
    366        1.1     bjh21 
    367  1.15.16.3      matt 	sc->sc_dev = self;
    368        1.1     bjh21 	isconsole = pckbd_is_console(pa->pa_tag, pa->pa_slot);
    369        1.1     bjh21 
    370        1.1     bjh21 	if (isconsole) {
    371        1.1     bjh21 		sc->id = &pckbd_consdata;
    372        1.1     bjh21 
    373        1.3     perry 		/*
    374        1.3     perry 		 * Some keyboards are not enabled after a reset,
    375        1.1     bjh21 		 * so make sure it is enabled now.
    376        1.1     bjh21 		 */
    377        1.1     bjh21 		cmd[0] = KBC_ENABLE;
    378        1.1     bjh21 		(void) pckbport_poll_cmd(sc->id->t_kbctag, sc->id->t_kbcslot,
    379        1.1     bjh21 				      cmd, 1, 0, 0, 0);
    380        1.1     bjh21 		sc->sc_enabled = 1;
    381        1.1     bjh21 	} else {
    382        1.1     bjh21 		sc->id = malloc(sizeof(struct pckbd_internal),
    383        1.1     bjh21 				M_DEVBUF, M_WAITOK);
    384        1.1     bjh21 		(void) pckbd_init(sc->id, pa->pa_tag, pa->pa_slot, 0);
    385        1.1     bjh21 
    386        1.1     bjh21 		/* no interrupts until enabled */
    387        1.1     bjh21 		cmd[0] = KBC_DISABLE;
    388        1.1     bjh21 		(void) pckbport_poll_cmd(sc->id->t_kbctag, sc->id->t_kbcslot,
    389        1.1     bjh21 				      cmd, 1, 0, 0, 0);
    390        1.1     bjh21 		sc->sc_enabled = 0;
    391        1.1     bjh21 	}
    392        1.1     bjh21 
    393        1.1     bjh21 	sc->id->t_sc = sc;
    394        1.1     bjh21 
    395        1.1     bjh21 	pckbport_set_inputhandler(sc->id->t_kbctag, sc->id->t_kbcslot,
    396  1.15.16.3      matt 			       pckbd_input, sc, device_xname(sc->sc_dev));
    397        1.1     bjh21 
    398        1.1     bjh21 	a.console = isconsole;
    399        1.1     bjh21 
    400        1.1     bjh21 	a.keymap = &pckbd_keymapdata;
    401        1.1     bjh21 
    402        1.1     bjh21 	a.accessops = &pckbd_accessops;
    403        1.1     bjh21 	a.accesscookie = sc;
    404        1.1     bjh21 
    405  1.15.16.2      matt 	if (!pmf_device_register(self, pckbd_suspend, pckbd_resume))
    406  1.15.16.2      matt 		aprint_error_dev(self, "couldn't establish power handler\n");
    407       1.11  christos 
    408        1.1     bjh21 	/*
    409        1.1     bjh21 	 * Attach the wskbd, saving a handle to it.
    410        1.1     bjh21 	 * XXX XXX XXX
    411        1.1     bjh21 	 */
    412  1.15.16.3      matt 	sc->sc_wskbddev = config_found_ia(self, "wskbddev", &a, wskbddevprint);
    413        1.1     bjh21 }
    414        1.1     bjh21 
    415        1.1     bjh21 int
    416        1.2     bjh21 pckbd_enable(void *v, int on)
    417        1.1     bjh21 {
    418        1.1     bjh21 	struct pckbd_softc *sc = v;
    419        1.2     bjh21 	int res;
    420        1.1     bjh21 	u_char cmd[1];
    421        1.1     bjh21 
    422        1.1     bjh21 	if (on) {
    423        1.1     bjh21 		if (sc->sc_enabled) {
    424        1.1     bjh21 #ifdef DIAGNOSTIC
    425        1.1     bjh21 			printf("pckbd_enable: bad enable\n");
    426        1.1     bjh21 #endif
    427        1.2     bjh21 			return EBUSY;
    428        1.1     bjh21 		}
    429        1.1     bjh21 
    430        1.1     bjh21 		pckbport_slot_enable(sc->id->t_kbctag, sc->id->t_kbcslot, 1);
    431        1.1     bjh21 
    432        1.1     bjh21 		cmd[0] = KBC_ENABLE;
    433        1.1     bjh21 		res = pckbport_poll_cmd(sc->id->t_kbctag, sc->id->t_kbcslot,
    434        1.1     bjh21 					cmd, 1, 0, NULL, 0);
    435        1.1     bjh21 		if (res) {
    436        1.1     bjh21 			printf("pckbd_enable: command error\n");
    437        1.1     bjh21 			return (res);
    438        1.1     bjh21 		}
    439        1.1     bjh21 
    440        1.1     bjh21 		res = pckbd_set_xtscancode(sc->id->t_kbctag,
    441        1.1     bjh21 					   sc->id->t_kbcslot);
    442        1.1     bjh21 		if (res)
    443        1.2     bjh21 			return res;
    444        1.1     bjh21 
    445        1.1     bjh21 		sc->sc_enabled = 1;
    446        1.1     bjh21 	} else {
    447        1.1     bjh21 		if (sc->id->t_isconsole)
    448        1.2     bjh21 			return EBUSY;
    449        1.1     bjh21 
    450        1.1     bjh21 		cmd[0] = KBC_DISABLE;
    451        1.1     bjh21 		res = pckbport_enqueue_cmd(sc->id->t_kbctag, sc->id->t_kbcslot,
    452        1.1     bjh21 					cmd, 1, 0, 1, 0);
    453        1.1     bjh21 		if (res) {
    454        1.1     bjh21 			printf("pckbd_disable: command error\n");
    455        1.2     bjh21 			return res;
    456        1.1     bjh21 		}
    457        1.1     bjh21 
    458        1.1     bjh21 		pckbport_slot_enable(sc->id->t_kbctag, sc->id->t_kbcslot, 0);
    459        1.1     bjh21 
    460        1.1     bjh21 		sc->sc_enabled = 0;
    461        1.1     bjh21 	}
    462        1.1     bjh21 
    463        1.2     bjh21 	return 0;
    464        1.1     bjh21 }
    465        1.1     bjh21 
    466        1.1     bjh21 static int
    467        1.2     bjh21 pckbd_decode(struct pckbd_internal *id, int datain, u_int *type, int *dataout)
    468        1.1     bjh21 {
    469        1.1     bjh21 	int key;
    470        1.1     bjh21 
    471        1.1     bjh21 	if (datain == KBR_EXTENDED0) {
    472        1.1     bjh21 		id->t_extended0 = 1;
    473        1.2     bjh21 		return 0;
    474        1.1     bjh21 	} else if (datain == KBR_EXTENDED1) {
    475        1.1     bjh21 		id->t_extended1 = 2;
    476        1.2     bjh21 		return 0;
    477        1.1     bjh21 	}
    478        1.1     bjh21 
    479        1.1     bjh21  	/* map extended keys to (unused) codes 128-254 */
    480        1.1     bjh21 	key = (datain & 0x7f) | (id->t_extended0 ? 0x80 : 0);
    481        1.1     bjh21 	id->t_extended0 = 0;
    482        1.1     bjh21 
    483        1.1     bjh21 	/*
    484        1.1     bjh21 	 * process PAUSE (also break) key (EXT1 1D 45  EXT1 9D C5):
    485        1.1     bjh21 	 * map to (unused) code 7F
    486        1.1     bjh21 	 */
    487        1.1     bjh21 	if (id->t_extended1 == 2 && (datain == 0x1d || datain == 0x9d)) {
    488        1.1     bjh21 		id->t_extended1 = 1;
    489        1.2     bjh21 		return 0;
    490        1.1     bjh21 	} else if (id->t_extended1 == 1 &&
    491        1.1     bjh21 		   (datain == 0x45 || datain == 0xc5)) {
    492        1.1     bjh21 		id->t_extended1 = 0;
    493        1.1     bjh21 		key = 0x7f;
    494        1.1     bjh21 	} else if (id->t_extended1 > 0) {
    495        1.1     bjh21 		id->t_extended1 = 0;
    496        1.1     bjh21 	}
    497        1.1     bjh21 
    498        1.1     bjh21 	if (datain & 0x80) {
    499        1.1     bjh21 		id->t_lastchar = 0;
    500        1.1     bjh21 		*type = WSCONS_EVENT_KEY_UP;
    501        1.1     bjh21 	} else {
    502        1.1     bjh21 		/* Always ignore typematic keys */
    503        1.1     bjh21 		if (key == id->t_lastchar)
    504        1.2     bjh21 			return 0;
    505        1.1     bjh21 		id->t_lastchar = key;
    506        1.1     bjh21 		*type = WSCONS_EVENT_KEY_DOWN;
    507        1.1     bjh21 	}
    508        1.1     bjh21 
    509        1.1     bjh21 	*dataout = key;
    510        1.2     bjh21 	return 1;
    511        1.1     bjh21 }
    512        1.1     bjh21 
    513        1.1     bjh21 int
    514        1.2     bjh21 pckbd_init(struct pckbd_internal *t, pckbport_tag_t kbctag,
    515        1.2     bjh21     pckbport_slot_t kbcslot, int console)
    516        1.1     bjh21 {
    517        1.2     bjh21 
    518        1.1     bjh21 	memset(t, 0, sizeof(struct pckbd_internal));
    519        1.1     bjh21 
    520        1.1     bjh21 	t->t_isconsole = console;
    521        1.1     bjh21 	t->t_kbctag = kbctag;
    522        1.1     bjh21 	t->t_kbcslot = kbcslot;
    523        1.1     bjh21 
    524        1.2     bjh21 	return pckbd_set_xtscancode(kbctag, kbcslot);
    525        1.1     bjh21 }
    526        1.1     bjh21 
    527        1.1     bjh21 static int
    528        1.2     bjh21 pckbd_led_encode(int led)
    529        1.1     bjh21 {
    530        1.1     bjh21 	int res;
    531        1.1     bjh21 
    532        1.1     bjh21 	res = 0;
    533        1.1     bjh21 
    534        1.1     bjh21 	if (led & WSKBD_LED_SCROLL)
    535        1.1     bjh21 		res |= 0x01;
    536        1.1     bjh21 	if (led & WSKBD_LED_NUM)
    537        1.1     bjh21 		res |= 0x02;
    538        1.1     bjh21 	if (led & WSKBD_LED_CAPS)
    539        1.1     bjh21 		res |= 0x04;
    540        1.2     bjh21 	return res;
    541        1.1     bjh21 }
    542        1.1     bjh21 
    543        1.1     bjh21 static int
    544        1.2     bjh21 pckbd_led_decode(int led)
    545        1.1     bjh21 {
    546        1.1     bjh21 	int res;
    547        1.1     bjh21 
    548        1.1     bjh21 	res = 0;
    549        1.1     bjh21 	if (led & 0x01)
    550        1.1     bjh21 		res |= WSKBD_LED_SCROLL;
    551        1.1     bjh21 	if (led & 0x02)
    552        1.1     bjh21 		res |= WSKBD_LED_NUM;
    553        1.1     bjh21 	if (led & 0x04)
    554        1.1     bjh21 		res |= WSKBD_LED_CAPS;
    555        1.2     bjh21 	return res;
    556        1.1     bjh21 }
    557        1.1     bjh21 
    558        1.1     bjh21 void
    559        1.2     bjh21 pckbd_set_leds(void *v, int leds)
    560        1.1     bjh21 {
    561        1.1     bjh21 	struct pckbd_softc *sc = v;
    562        1.1     bjh21 	u_char cmd[2];
    563        1.1     bjh21 
    564        1.1     bjh21 	cmd[0] = KBC_MODEIND;
    565        1.1     bjh21 	cmd[1] = pckbd_led_encode(leds);
    566        1.1     bjh21 	sc->sc_ledstate = cmd[1];
    567        1.1     bjh21 
    568        1.2     bjh21 	(void)pckbport_enqueue_cmd(sc->id->t_kbctag, sc->id->t_kbcslot,
    569        1.1     bjh21 				 cmd, 2, 0, 0, 0);
    570        1.1     bjh21 }
    571        1.1     bjh21 
    572        1.1     bjh21 /*
    573        1.1     bjh21  * Got a console receive interrupt -
    574        1.1     bjh21  * the console processor wants to give us a character.
    575        1.1     bjh21  */
    576        1.1     bjh21 void
    577        1.2     bjh21 pckbd_input(void *vsc, int data)
    578        1.1     bjh21 {
    579        1.1     bjh21 	struct pckbd_softc *sc = vsc;
    580        1.1     bjh21 	int key;
    581        1.1     bjh21 	u_int type;
    582        1.1     bjh21 
    583        1.1     bjh21 #ifdef WSDISPLAY_COMPAT_RAWKBD
    584        1.1     bjh21 	if (sc->rawkbd) {
    585        1.1     bjh21 		u_char d = data;
    586        1.1     bjh21 		wskbd_rawinput(sc->sc_wskbddev, &d, 1);
    587        1.1     bjh21 		return;
    588        1.1     bjh21 	}
    589        1.1     bjh21 #endif
    590        1.1     bjh21 	if (pckbd_decode(sc->id, data, &type, &key))
    591        1.1     bjh21 		wskbd_input(sc->sc_wskbddev, type, key);
    592        1.1     bjh21 }
    593        1.1     bjh21 
    594        1.1     bjh21 int
    595       1.15  christos pckbd_ioctl(void *v, u_long cmd, void *data, int flag,
    596       1.14  christos     struct lwp *l)
    597        1.1     bjh21 {
    598        1.1     bjh21 	struct pckbd_softc *sc = v;
    599        1.1     bjh21 
    600        1.1     bjh21 	switch (cmd) {
    601        1.2     bjh21 	case WSKBDIO_GTYPE:
    602        1.1     bjh21 		*(int *)data = WSKBD_TYPE_PC_XT;
    603        1.1     bjh21 		return 0;
    604        1.2     bjh21 	case WSKBDIO_SETLEDS:
    605        1.2     bjh21 	{
    606        1.2     bjh21 		int res;
    607        1.8  christos 		u_char cmdb[2];
    608        1.2     bjh21 
    609        1.8  christos 		cmdb[0] = KBC_MODEIND;
    610        1.8  christos 		cmdb[1] = pckbd_led_encode(*(int *)data);
    611        1.8  christos 		sc->sc_ledstate = cmdb[1];
    612        1.1     bjh21 		res = pckbport_enqueue_cmd(sc->id->t_kbctag, sc->id->t_kbcslot,
    613        1.8  christos 					cmdb, 2, 0, 1, 0);
    614        1.2     bjh21 		return res;
    615        1.2     bjh21 	}
    616        1.2     bjh21 	case WSKBDIO_GETLEDS:
    617        1.1     bjh21 		*(int *)data = pckbd_led_decode(sc->sc_ledstate);
    618        1.2     bjh21 		return 0;
    619        1.2     bjh21 	case WSKBDIO_COMPLEXBELL:
    620        1.1     bjh21 #define d ((struct wskbd_bell_data *)data)
    621        1.1     bjh21 		/*
    622        1.1     bjh21 		 * Keyboard can't beep directly; we have an
    623        1.1     bjh21 		 * externally-provided global hook to do this.
    624        1.1     bjh21 		 */
    625        1.1     bjh21 		pckbd_bell(d->pitch, d->period, d->volume, 0);
    626        1.1     bjh21 #undef d
    627        1.2     bjh21 		return 0;
    628        1.1     bjh21 #ifdef WSDISPLAY_COMPAT_RAWKBD
    629        1.2     bjh21 	case WSKBDIO_SETMODE:
    630        1.1     bjh21 		sc->rawkbd = (*(int *)data == WSKBD_RAW);
    631        1.2     bjh21 		return 0;
    632        1.1     bjh21 #endif
    633        1.1     bjh21 	}
    634        1.1     bjh21 	return EPASSTHROUGH;
    635        1.1     bjh21 }
    636        1.1     bjh21 
    637        1.1     bjh21 void
    638        1.2     bjh21 pckbd_bell(u_int pitch, u_int period, u_int volume, int poll)
    639        1.1     bjh21 {
    640        1.1     bjh21 
    641        1.1     bjh21 	if (pckbd_bell_fn != NULL)
    642        1.1     bjh21 		(*pckbd_bell_fn)(pckbd_bell_fn_arg, pitch, period,
    643        1.1     bjh21 		    volume, poll);
    644        1.1     bjh21 }
    645        1.1     bjh21 
    646        1.1     bjh21 void
    647  1.15.16.3      matt pckbd_unhook_bell(void (*fn)(void *, u_int, u_int, u_int, int), void *arg)
    648  1.15.16.3      matt {
    649  1.15.16.3      matt 	if (pckbd_bell_fn != fn && pckbd_bell_fn_arg != arg)
    650  1.15.16.3      matt 		return;
    651  1.15.16.3      matt 	pckbd_bell_fn = NULL;
    652  1.15.16.3      matt 	pckbd_bell_fn_arg = NULL;
    653  1.15.16.3      matt }
    654  1.15.16.3      matt 
    655  1.15.16.3      matt void
    656        1.2     bjh21 pckbd_hookup_bell(void (*fn)(void *, u_int, u_int, u_int, int), void *arg)
    657        1.1     bjh21 {
    658        1.1     bjh21 
    659        1.1     bjh21 	if (pckbd_bell_fn == NULL) {
    660        1.1     bjh21 		pckbd_bell_fn = fn;
    661        1.1     bjh21 		pckbd_bell_fn_arg = arg;
    662        1.1     bjh21 	}
    663        1.1     bjh21 }
    664        1.1     bjh21 
    665        1.1     bjh21 int
    666        1.2     bjh21 pckbd_cnattach(pckbport_tag_t kbctag, int kbcslot)
    667        1.1     bjh21 {
    668        1.2     bjh21 	int res;
    669        1.1     bjh21 	u_char cmd[1];
    670        1.1     bjh21 
    671        1.1     bjh21 	res = pckbd_init(&pckbd_consdata, kbctag, kbcslot, 1);
    672        1.7  augustss 	/* We may allow the console to be attached if no keyboard is present */
    673        1.7  augustss #if defined(PCKBD_CNATTACH_MAY_FAIL)
    674        1.1     bjh21 	if (res)
    675        1.2     bjh21 		return res;
    676        1.1     bjh21 #endif
    677        1.1     bjh21 
    678        1.1     bjh21 	/* Just to be sure. */
    679        1.1     bjh21 	cmd[0] = KBC_ENABLE;
    680        1.1     bjh21 	res = pckbport_poll_cmd(kbctag, kbcslot, cmd, 1, 0, 0, 0);
    681        1.1     bjh21 
    682        1.7  augustss #if defined(PCKBD_CNATTACH_MAY_FAIL)
    683        1.1     bjh21 	if (res)
    684        1.2     bjh21 		return res;
    685        1.1     bjh21 #endif
    686        1.1     bjh21 
    687        1.1     bjh21 	wskbd_cnattach(&pckbd_consops, &pckbd_consdata, &pckbd_keymapdata);
    688        1.1     bjh21 
    689        1.2     bjh21 	return 0;
    690        1.1     bjh21 }
    691        1.1     bjh21 
    692        1.1     bjh21 /* ARGSUSED */
    693        1.1     bjh21 void
    694        1.2     bjh21 pckbd_cngetc(void *v, u_int *type, int *data)
    695        1.1     bjh21 {
    696        1.1     bjh21         struct pckbd_internal *t = v;
    697        1.1     bjh21 	int val;
    698        1.1     bjh21 
    699        1.1     bjh21 	for (;;) {
    700        1.1     bjh21 		val = pckbport_poll_data(t->t_kbctag, t->t_kbcslot);
    701        1.1     bjh21 		if ((val != -1) && pckbd_decode(t, val, type, data))
    702        1.1     bjh21 			return;
    703        1.1     bjh21 	}
    704        1.1     bjh21 }
    705        1.1     bjh21 
    706        1.1     bjh21 void
    707        1.2     bjh21 pckbd_cnpollc(void *v, int on)
    708        1.1     bjh21 {
    709        1.1     bjh21 	struct pckbd_internal *t = v;
    710        1.1     bjh21 
    711  1.15.16.3      matt 	if (on) {
    712  1.15.16.3      matt 		u_char cmd[1];
    713  1.15.16.3      matt 
    714  1.15.16.3      matt 		cmd[0] = KBC_ENABLE;
    715  1.15.16.3      matt 		(void)pckbport_poll_cmd(t->t_kbctag, t->t_kbcslot, cmd,
    716  1.15.16.3      matt 		    1, 0, 0, 0);
    717  1.15.16.3      matt 	}
    718  1.15.16.3      matt 
    719        1.1     bjh21 	pckbport_set_poll(t->t_kbctag, t->t_kbcslot, on);
    720        1.1     bjh21 }
    721        1.1     bjh21 
    722        1.1     bjh21 void
    723       1.14  christos pckbd_cnbell(void *v, u_int pitch, u_int period, u_int volume)
    724        1.1     bjh21 {
    725        1.1     bjh21 
    726        1.1     bjh21 	pckbd_bell(pitch, period, volume, 1);
    727        1.1     bjh21 }
    728