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