Home | History | Annotate | Line # | Download | only in hil
hilkbd.c revision 1.1
      1 /*	$NetBSD: hilkbd.c,v 1.1 2011/02/06 18:26:54 tsutsui Exp $	*/
      2 /*	$OpenBSD: hilkbd.c,v 1.14 2009/01/21 21:53:59 grange Exp $	*/
      3 /*
      4  * Copyright (c) 2003, Miodrag Vallat.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     19  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
     20  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     22  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     24  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
     25  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  * POSSIBILITY OF SUCH DAMAGE.
     27  *
     28  */
     29 
     30 #include "opt_wsdisplay_compat.h"
     31 
     32 #include <sys/param.h>
     33 #include <sys/systm.h>
     34 #include <sys/device.h>
     35 #include <sys/ioctl.h>
     36 #include <sys/kernel.h>
     37 #include <sys/callout.h>
     38 #include <sys/bus.h>
     39 #include <sys/cpu.h>
     40 
     41 #include <machine/autoconf.h>
     42 
     43 #include <dev/hil/hilreg.h>
     44 #include <dev/hil/hilvar.h>
     45 #include <dev/hil/hildevs.h>
     46 
     47 #include <dev/wscons/wsconsio.h>
     48 #include <dev/wscons/wskbdvar.h>
     49 #include <dev/wscons/wsksymdef.h>
     50 #include <dev/wscons/wsksymvar.h>
     51 
     52 #include <dev/hil/hilkbdmap.h>
     53 
     54 struct hilkbd_softc {
     55 	struct hildev_softc sc_hildev;
     56 
     57 	int		sc_numleds;
     58 	int		sc_ledstate;
     59 	int		sc_enabled;
     60 	int		sc_console;
     61 	int		sc_lastarrow;
     62 
     63 	device_t	sc_wskbddev;
     64 
     65 #ifdef WSDISPLAY_COMPAT_RAWKBD
     66 	int		sc_rawkbd;
     67 	int		sc_nrep;
     68 	char		sc_rep[HILBUFSIZE * 2];
     69 	struct callout	sc_rawrepeat_ch;
     70 #define	REP_DELAY1	400
     71 #define	REP_DELAYN	100
     72 #endif
     73 };
     74 
     75 int	hilkbdprobe(device_t, cfdata_t, void *);
     76 void	hilkbdattach(device_t, device_t, void *);
     77 int	hilkbddetach(device_t, int);
     78 
     79 CFATTACH_DECL_NEW(hilkbd, sizeof(struct hilkbd_softc),
     80     hilkbdprobe, hilkbdattach, hilkbddetach, NULL);
     81 
     82 int	hilkbd_enable(void *, int);
     83 void	hilkbd_set_leds(void *, int);
     84 int	hilkbd_ioctl(void *, u_long, void *, int, struct lwp *);
     85 
     86 const struct wskbd_accessops hilkbd_accessops = {
     87 	hilkbd_enable,
     88 	hilkbd_set_leds,
     89 	hilkbd_ioctl,
     90 };
     91 
     92 void	hilkbd_cngetc(void *, u_int *, int *);
     93 void	hilkbd_cnpollc(void *, int);
     94 void	hilkbd_cnbell(void *, u_int, u_int, u_int);
     95 
     96 const struct wskbd_consops hilkbd_consops = {
     97 	hilkbd_cngetc,
     98 	hilkbd_cnpollc,
     99 	hilkbd_cnbell,
    100 };
    101 
    102 struct wskbd_mapdata hilkbd_keymapdata = {
    103 	hilkbd_keydesctab,
    104 #ifdef HILKBD_LAYOUT
    105 	HILKBD_LAYOUT,
    106 #else
    107 	KB_US,
    108 #endif
    109 };
    110 
    111 struct wskbd_mapdata hilkbd_keymapdata_ps2 = {
    112 	hilkbd_keydesctab_ps2,
    113 #ifdef HILKBD_LAYOUT
    114 	HILKBD_LAYOUT,
    115 #else
    116 	KB_US,
    117 #endif
    118 };
    119 
    120 void	hilkbd_bell(struct hil_softc *, u_int, u_int, u_int);
    121 void	hilkbd_callback(struct hildev_softc *, u_int, u_int8_t *);
    122 void	hilkbd_decode(struct hilkbd_softc *, u_int8_t, u_int *, int *, int);
    123 int	hilkbd_is_console(int);
    124 void	hilkbd_rawrepeat(void *);
    125 
    126 int	seen_hilkbd_console;
    127 
    128 int
    129 hilkbdprobe(device_t parent, cfdata_t cf, void *aux)
    130 {
    131 	struct hil_attach_args *ha = aux;
    132 
    133 	if (ha->ha_type != HIL_DEVICE_KEYBOARD &&
    134 	    ha->ha_type != HIL_DEVICE_BUTTONBOX)
    135 		return (0);
    136 
    137 	return (1);
    138 }
    139 
    140 void
    141 hilkbdattach(device_t parent, device_t self, void *aux)
    142 {
    143 	struct hilkbd_softc *sc = device_private(self);
    144 	struct hil_attach_args *ha = aux;
    145 	struct wskbddev_attach_args a;
    146 	u_int8_t layoutcode;
    147 	int ps2;
    148 
    149 	sc->sc_hildev.sc_dev = self;
    150 	sc->hd_code = ha->ha_code;
    151 	sc->hd_type = ha->ha_type;
    152 	sc->hd_infolen = ha->ha_infolen;
    153 	memcpy(sc->hd_info, ha->ha_info, ha->ha_infolen);
    154 	sc->hd_fn = hilkbd_callback;
    155 
    156 	if (ha->ha_type == HIL_DEVICE_KEYBOARD) {
    157 		/*
    158 		 * Determine the keyboard language configuration, but don't
    159 		 * override a user-specified setting.
    160 		 */
    161 		layoutcode = ha->ha_id & (MAXHILKBDLAYOUT - 1);
    162 #ifndef HILKBD_LAYOUT
    163 		if (layoutcode < MAXHILKBDLAYOUT &&
    164 		    hilkbd_layouts[layoutcode] != -1)
    165 			hilkbd_keymapdata.layout =
    166 			hilkbd_keymapdata_ps2.layout =
    167 			    hilkbd_layouts[layoutcode];
    168 #endif
    169 
    170 		printf(", layout %x", layoutcode);
    171 	}
    172 
    173 	/*
    174 	 * Interpret the identification bytes, if any
    175 	 */
    176 	if (ha->ha_infolen > 2 && (ha->ha_info[1] & HIL_IOB) != 0) {
    177 		/* HILIOB_PROMPT is not always reported... */
    178 		sc->sc_numleds = (ha->ha_info[2] & HILIOB_PMASK) >> 4;
    179 		if (sc->sc_numleds != 0)
    180 			printf(", %d leds", sc->sc_numleds);
    181 	}
    182 
    183 	printf("\n");
    184 
    185 	/*
    186 	 * Red lettered keyboards come in two flavours, the old one
    187 	 * with only one control key, to the left of the escape key,
    188 	 * and the modern one which has a PS/2 like layout, and leds.
    189 	 *
    190 	 * Unfortunately for us, they use the same device ID range.
    191 	 * We'll differentiate them by looking at the leds property.
    192 	 */
    193 	ps2 = (sc->sc_numleds != 0);
    194 
    195 #ifdef WSDISPLAY_COMPAT_RAWKBD
    196 	callout_init(&sc->sc_rawrepeat_ch, 0);
    197 	callout_setfunc(&sc->sc_rawrepeat_ch, hilkbd_rawrepeat, sc);
    198 #endif
    199 
    200 	/* Do not consider button boxes as console devices. */
    201 	if (ha->ha_type == HIL_DEVICE_BUTTONBOX)
    202 		a.console = 0;
    203 	else
    204 		a.console = hilkbd_is_console(ha->ha_console);
    205 	a.keymap = ps2 ? &hilkbd_keymapdata_ps2 : &hilkbd_keymapdata;
    206 	a.accessops = &hilkbd_accessops;
    207 	a.accesscookie = sc;
    208 
    209 	if (a.console) {
    210 		sc->sc_console = sc->sc_enabled = 1;
    211 		wskbd_cnattach(&hilkbd_consops, sc, a.keymap);
    212 	} else {
    213 		sc->sc_console = sc->sc_enabled = 0;
    214 	}
    215 
    216 	sc->sc_wskbddev = config_found(self, &a, wskbddevprint);
    217 
    218 	/*
    219 	 * If this is an old keyboard with a numeric pad but no ``num lock''
    220 	 * key, simulate it being pressed so that the keyboard runs in
    221 	 * numeric mode.
    222 	 */
    223 	if (!ps2 && sc->sc_wskbddev != NULL) {
    224 		wskbd_input(sc->sc_wskbddev, WSCONS_EVENT_KEY_DOWN, 80);
    225 		wskbd_input(sc->sc_wskbddev, WSCONS_EVENT_KEY_UP, 80);
    226 	}
    227 }
    228 
    229 int
    230 hilkbddetach(struct device *self, int flags)
    231 {
    232 	struct hilkbd_softc *sc = device_private(self);
    233 
    234 	/*
    235 	 * Handle console keyboard for the best. It should have been set
    236 	 * as the first device in the loop anyways.
    237 	 */
    238 	if (sc->sc_console) {
    239 		wskbd_cndetach();
    240 		seen_hilkbd_console = 0;
    241 	}
    242 
    243 	if (sc->sc_wskbddev != NULL)
    244 		return config_detach(sc->sc_wskbddev, flags);
    245 
    246 	return (0);
    247 }
    248 
    249 int
    250 hilkbd_enable(void *v, int on)
    251 {
    252 	struct hilkbd_softc *sc = v;
    253 
    254 	if (on) {
    255 		if (sc->sc_enabled)
    256 			return (EBUSY);
    257 	} else {
    258 		if (sc->sc_console)
    259 			return (EBUSY);
    260 	}
    261 
    262 	sc->sc_enabled = on;
    263 
    264 	return (0);
    265 }
    266 
    267 void
    268 hilkbd_set_leds(void *v, int leds)
    269 {
    270 	struct hilkbd_softc *sc = v;
    271 	struct hildev_softc *hdsc = &sc->sc_hildev;
    272 	int changemask;
    273 
    274 	if (sc->sc_numleds == 0)
    275 		return;
    276 
    277 	changemask = leds ^ sc->sc_ledstate;
    278 	if (changemask == 0)
    279 		return;
    280 
    281 	/* We do not handle more than 3 leds here */
    282 	if (changemask & WSKBD_LED_SCROLL)
    283 		send_hildev_cmd(hdsc,
    284 		    (leds & WSKBD_LED_SCROLL) ? HIL_PROMPT1 : HIL_ACK1,
    285 		    NULL, NULL);
    286 	if (changemask & WSKBD_LED_NUM)
    287 		send_hildev_cmd(hdsc,
    288 		    (leds & WSKBD_LED_NUM) ? HIL_PROMPT2 : HIL_ACK2,
    289 		    NULL, NULL);
    290 	if (changemask & WSKBD_LED_CAPS)
    291 		send_hildev_cmd(hdsc,
    292 		    (leds & WSKBD_LED_CAPS) ? HIL_PROMPT3 : HIL_ACK3,
    293 		    NULL, NULL);
    294 
    295 	sc->sc_ledstate = leds;
    296 }
    297 
    298 int
    299 hilkbd_ioctl(void *v, u_long cmd, void *data, int flag, struct lwp *l)
    300 {
    301 	struct hilkbd_softc *sc = v;
    302 
    303 	switch (cmd) {
    304 	case WSKBDIO_GTYPE:
    305 		*(int *)data = WSKBD_TYPE_HIL;
    306 		return 0;
    307 	case WSKBDIO_SETLEDS:
    308 		hilkbd_set_leds(v, *(int *)data);
    309 		return 0;
    310 	case WSKBDIO_GETLEDS:
    311 		*(int *)data = sc->sc_ledstate;
    312 		return 0;
    313 #ifdef WSDISPLAY_COMPAT_RAWKBD
    314 	case WSKBDIO_SETMODE:
    315 		sc->sc_rawkbd = *(int *)data == WSKBD_RAW;
    316 		callout_stop(&sc->sc_rawrepeat_ch);
    317 		return 0;
    318 #endif
    319 	case WSKBDIO_COMPLEXBELL:
    320 #define	d ((struct wskbd_bell_data *)data)
    321 		hilkbd_bell(device_private(device_parent(sc->sc_hildev.sc_dev)),
    322 		    d->pitch, d->period, d->volume);
    323 #undef d
    324 		return 0;
    325 	}
    326 
    327 	return EPASSTHROUGH;
    328 }
    329 
    330 void
    331 hilkbd_cngetc(void *v, u_int *type, int *data)
    332 {
    333 	struct hilkbd_softc *sc = v;
    334 	struct hildev_softc *hdsc = &sc->sc_hildev;
    335 	u_int8_t c, stat;
    336 
    337 	for (;;) {
    338 		while (hil_poll_data(hdsc, &stat, &c) != 0)
    339 			;
    340 
    341 		/*
    342 		 * Disregard keyboard data packet header.
    343 		 * Note that no key generates it, so we're safe.
    344 		 */
    345 		if (c != HIL_KBDBUTTON)
    346 			break;
    347 	}
    348 
    349 	hilkbd_decode(sc, c, type, data, HIL_KBDBUTTON);
    350 }
    351 
    352 void
    353 hilkbd_cnpollc(void *v, int on)
    354 {
    355 	struct hilkbd_softc *sc = v;
    356 
    357 	hil_set_poll(device_private(device_parent(sc->sc_hildev.sc_dev)), on);
    358 }
    359 
    360 void
    361 hilkbd_cnbell(void *v, u_int pitch, u_int period, u_int volume)
    362 {
    363 	struct hilkbd_softc *sc = v;
    364 
    365 	hilkbd_bell(device_private(device_parent(sc->sc_hildev.sc_dev)),
    366 	    pitch, period, volume);
    367 }
    368 
    369 void
    370 hilkbd_bell(struct hil_softc *sc, u_int pitch, u_int period, u_int volume)
    371 {
    372 	u_int8_t buf[2];
    373 
    374 	/* XXX there could be at least a pitch -> HIL pitch conversion here */
    375 #define	BELLDUR		80	/* tone duration in msec (10-2560) */
    376 #define	BELLFREQ	8	/* tone frequency (0-63) */
    377 	buf[0] = ar_format(period - 10);
    378 	buf[1] = BELLFREQ;
    379 	send_hil_cmd(sc, HIL_SETTONE, buf, 2, NULL);
    380 }
    381 
    382 void
    383 hilkbd_callback(struct hildev_softc *hdsc, u_int buflen, u_int8_t *buf)
    384 {
    385 	struct hilkbd_softc *sc = device_private(hdsc->sc_dev);
    386 	u_int type;
    387 	int kbdtype, key;
    388 	int i, s;
    389 
    390 	/*
    391 	 * Ignore packet if we don't need it
    392 	 */
    393 	if (sc->sc_enabled == 0)
    394 		return;
    395 
    396 	if (buflen == 0)
    397 		return;
    398 	switch ((kbdtype = *buf & HIL_KBDDATA)) {
    399 	case HIL_BUTTONBOX:
    400 	case HIL_KBDBUTTON:
    401 		break;
    402 	default:
    403 		return;
    404 	}
    405 
    406 #ifdef WSDISPLAY_COMPAT_RAWKBD
    407 	if (sc->sc_rawkbd) {
    408 		u_char cbuf[HILBUFSIZE * 2];
    409 		int c, j, npress;
    410 
    411 		npress = j = 0;
    412 		for (i = 1, buf++; i < buflen; i++) {
    413 			hilkbd_decode(sc, *buf++, &type, &key, kbdtype);
    414 			c = hilkbd_raw[key];
    415 			if (c == 0)
    416 				continue;
    417 			/* fake extended scancode if necessary */
    418 			if (c & 0x80)
    419 				cbuf[j++] = 0xe0;
    420 			cbuf[j] = c & 0x7f;
    421 			if (type == WSCONS_EVENT_KEY_UP)
    422 				cbuf[j] |= 0x80;
    423 			else {
    424 				/* remember pressed keys for autorepeat */
    425 				if (c & 0x80)
    426 					sc->sc_rep[npress++] = 0xe0;
    427 				sc->sc_rep[npress++] = c & 0x7f;
    428 			}
    429 			j++;
    430 		}
    431 
    432 		s = spltty();
    433 		wskbd_rawinput(sc->sc_wskbddev, cbuf, j);
    434 		splx(s);
    435 		callout_stop(&sc->sc_rawrepeat_ch);
    436 		sc->sc_nrep = npress;
    437 		if (npress != 0) {
    438 			callout_schedule(&sc->sc_rawrepeat_ch,
    439 			    mstohz(REP_DELAY1));
    440 		}
    441 	} else
    442 #endif
    443 	{
    444 		s = spltty();
    445 		for (i = 1, buf++; i < buflen; i++) {
    446 			hilkbd_decode(sc, *buf++, &type, &key, kbdtype);
    447 			if (sc->sc_wskbddev != NULL)
    448 				wskbd_input(sc->sc_wskbddev, type, key);
    449 		}
    450 		splx(s);
    451 	}
    452 }
    453 
    454 void
    455 hilkbd_decode(struct hilkbd_softc *sc, u_int8_t data, u_int *type, int *key,
    456     int kbdtype)
    457 {
    458 	if (kbdtype == HIL_BUTTONBOX) {
    459 		if (data == 0x02)	/* repeat arrow */
    460 			data = sc->sc_lastarrow;
    461 		else if (data >= 0xf8)
    462 			sc->sc_lastarrow = data;
    463 	}
    464 
    465 	*type = (data & 1) ? WSCONS_EVENT_KEY_UP : WSCONS_EVENT_KEY_DOWN;
    466 	*key = data >> 1;
    467 }
    468 
    469 int
    470 hilkbd_is_console(int hil_is_console)
    471 {
    472 	/* if not first hil keyboard, then not the console */
    473 	if (seen_hilkbd_console)
    474 		return (0);
    475 
    476 	/* if PDC console does not match hil bus path, then not the console */
    477 	if (hil_is_console == 0)
    478 		return (0);
    479 
    480 	seen_hilkbd_console = 1;
    481 	return (1);
    482 }
    483 
    484 #ifdef WSDISPLAY_COMPAT_RAWKBD
    485 void
    486 hilkbd_rawrepeat(void *v)
    487 {
    488 	struct hilkbd_softc *sc = v;
    489 	int s;
    490 
    491 	s = spltty();
    492 	wskbd_rawinput(sc->sc_wskbddev, sc->sc_rep, sc->sc_nrep);
    493 	splx(s);
    494 	callout_schedule(&sc->sc_rawrepeat_ch, mstohz(REP_DELAYN));
    495 }
    496 #endif
    497