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