Home | History | Annotate | Line # | Download | only in dev
lunaws.c revision 1.32
      1 /* $NetBSD: lunaws.c,v 1.32 2021/04/24 23:36:40 thorpej Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2000 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Tohru Nishimura.
      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  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>			/* RCS ID & Copyright macro defns */
     33 
     34 __KERNEL_RCSID(0, "$NetBSD: lunaws.c,v 1.32 2021/04/24 23:36:40 thorpej Exp $");
     35 
     36 #include "opt_wsdisplay_compat.h"
     37 #include "wsmouse.h"
     38 
     39 #include <sys/param.h>
     40 #include <sys/systm.h>
     41 #include <sys/conf.h>
     42 #include <sys/device.h>
     43 
     44 #include <dev/wscons/wsconsio.h>
     45 #include <dev/wscons/wskbdvar.h>
     46 #include <dev/wscons/wsksymdef.h>
     47 #include <dev/wscons/wsksymvar.h>
     48 #include <dev/wscons/wsmousevar.h>
     49 
     50 #include <luna68k/dev/omkbdmap.h>
     51 #include <luna68k/dev/sioreg.h>
     52 #include <luna68k/dev/siovar.h>
     53 #include <luna68k/dev/syscn.h>
     54 
     55 #include "ioconf.h"
     56 
     57 #define OMKBD_RXQ_LEN		64
     58 #define OMKBD_RXQ_LEN_MASK	(OMKBD_RXQ_LEN - 1)
     59 #define OMKBD_NEXTRXQ(x)	(((x) + 1) & OMKBD_RXQ_LEN_MASK)
     60 
     61 static const uint8_t ch1_regs[6] = {
     62 	WR0_RSTINT,				/* Reset E/S Interrupt */
     63 	WR1_RXALLS,				/* Rx per char, No Tx */
     64 	0,					/* */
     65 	WR3_RX8BIT | WR3_RXENBL,		/* Rx */
     66 	WR4_BAUD96 | WR4_STOP1 | WR4_NPARITY,	/* Tx/Rx */
     67 	WR5_TX8BIT | WR5_TXENBL,		/* Tx */
     68 };
     69 
     70 struct ws_softc {
     71 	device_t	sc_dev;
     72 	struct sioreg	*sc_ctl;
     73 	uint8_t		sc_wr[6];
     74 	device_t	sc_wskbddev;
     75 	uint8_t		sc_rxq[OMKBD_RXQ_LEN];
     76 	u_int		sc_rxqhead;
     77 	u_int		sc_rxqtail;
     78 #if NWSMOUSE > 0
     79 	device_t	sc_wsmousedev;
     80 	int		sc_msreport;
     81 	int		sc_msbuttons, sc_msdx, sc_msdy;
     82 #endif
     83 	void		*sc_si;
     84 	int		sc_rawkbd;
     85 };
     86 
     87 static void omkbd_input(void *, int);
     88 static void omkbd_decode(void *, int, u_int *, int *);
     89 static int  omkbd_enable(void *, int);
     90 static void omkbd_set_leds(void *, int);
     91 static int  omkbd_ioctl(void *, u_long, void *, int, struct lwp *);
     92 
     93 static const struct wskbd_mapdata omkbd_keymapdata = {
     94 	omkbd_keydesctab,
     95 	KB_JP,
     96 };
     97 static const struct wskbd_accessops omkbd_accessops = {
     98 	omkbd_enable,
     99 	omkbd_set_leds,
    100 	omkbd_ioctl,
    101 };
    102 
    103 void	ws_cnattach(void);
    104 static void ws_cngetc(void *, u_int *, int *);
    105 static void ws_cnpollc(void *, int);
    106 static const struct wskbd_consops ws_consops = {
    107 	ws_cngetc,
    108 	ws_cnpollc,
    109 };
    110 
    111 #if NWSMOUSE > 0
    112 static int  omms_enable(void *);
    113 static int  omms_ioctl(void *, u_long, void *, int, struct lwp *);
    114 static void omms_disable(void *);
    115 
    116 static const struct wsmouse_accessops omms_accessops = {
    117 	omms_enable,
    118 	omms_ioctl,
    119 	omms_disable,
    120 };
    121 #endif
    122 
    123 static void wsintr(void *);
    124 static void wssoftintr(void *);
    125 
    126 static int  wsmatch(device_t, cfdata_t, void *);
    127 static void wsattach(device_t, device_t, void *);
    128 
    129 CFATTACH_DECL_NEW(ws, sizeof(struct ws_softc),
    130     wsmatch, wsattach, NULL, NULL);
    131 
    132 static int
    133 wsmatch(device_t parent, cfdata_t cf, void *aux)
    134 {
    135 	struct sio_attach_args *args = aux;
    136 
    137 	if (args->channel != 1)
    138 		return 0;
    139 	return 1;
    140 }
    141 
    142 static void
    143 wsattach(device_t parent, device_t self, void *aux)
    144 {
    145 	struct ws_softc *sc = device_private(self);
    146 	struct sio_softc *siosc = device_private(parent);
    147 	struct sio_attach_args *args = aux;
    148 	int channel = args->channel;
    149 	struct wskbddev_attach_args a;
    150 
    151 	sc->sc_dev = self;
    152 	sc->sc_ctl = &siosc->sc_ctl[channel];
    153 	memcpy(sc->sc_wr, ch1_regs, sizeof(ch1_regs));
    154 	siosc->sc_intrhand[channel].ih_func = wsintr;
    155 	siosc->sc_intrhand[channel].ih_arg = sc;
    156 
    157 	setsioreg(sc->sc_ctl, WR0, sc->sc_wr[WR0]);
    158 	setsioreg(sc->sc_ctl, WR4, sc->sc_wr[WR4]);
    159 	setsioreg(sc->sc_ctl, WR3, sc->sc_wr[WR3]);
    160 	setsioreg(sc->sc_ctl, WR5, sc->sc_wr[WR5]);
    161 	setsioreg(sc->sc_ctl, WR0, sc->sc_wr[WR0]);
    162 	setsioreg(sc->sc_ctl, WR1, sc->sc_wr[WR1]);
    163 
    164 	syscnputc((dev_t)1, 0x20); /* keep quiet mouse */
    165 
    166 	sc->sc_rxqhead = 0;
    167 	sc->sc_rxqtail = 0;
    168 
    169 	sc->sc_si = softint_establish(SOFTINT_SERIAL, wssoftintr, sc);
    170 
    171 	aprint_normal("\n");
    172 
    173 	a.console = (args->hwflags == 1);
    174 	a.keymap = &omkbd_keymapdata;
    175 	a.accessops = &omkbd_accessops;
    176 	a.accesscookie = (void *)sc;
    177 	sc->sc_wskbddev = config_found(self, &a, wskbddevprint,
    178 	    CFARG_IATTR, "wskbddev",
    179 	    CFARG_EOL);
    180 
    181 #if NWSMOUSE > 0
    182 	{
    183 	struct wsmousedev_attach_args b;
    184 	b.accessops = &omms_accessops;
    185 	b.accesscookie = (void *)sc;
    186 	sc->sc_wsmousedev = config_found(self, &b, wsmousedevprint,
    187 	    CFARG_IATTR, "wsmousedev",
    188 	    CFARG_EOL);
    189 	sc->sc_msreport = 0;
    190 	}
    191 #endif
    192 }
    193 
    194 /*ARGSUSED*/
    195 static void
    196 wsintr(void *arg)
    197 {
    198 	struct ws_softc *sc = arg;
    199 	struct sioreg *sio = sc->sc_ctl;
    200 	uint8_t code;
    201 	int rr;
    202 
    203 	rr = getsiocsr(sio);
    204 	if ((rr & RR_RXRDY) != 0) {
    205 		do {
    206 			code = sio->sio_data;
    207 			if (rr & (RR_FRAMING | RR_OVERRUN | RR_PARITY)) {
    208 				sio->sio_cmd = WR0_ERRRST;
    209 				continue;
    210 			}
    211 			sc->sc_rxq[sc->sc_rxqtail] = code;
    212 			sc->sc_rxqtail = OMKBD_NEXTRXQ(sc->sc_rxqtail);
    213 		} while (((rr = getsiocsr(sio)) & RR_RXRDY) != 0);
    214 		softint_schedule(sc->sc_si);
    215 	}
    216 	if ((rr & RR_TXRDY) != 0)
    217 		sio->sio_cmd = WR0_RSTPEND;
    218 	/* not capable of transmit, yet */
    219 }
    220 
    221 static void
    222 wssoftintr(void *arg)
    223 {
    224 	struct ws_softc *sc = arg;
    225 	uint8_t code;
    226 
    227 	while (sc->sc_rxqhead != sc->sc_rxqtail) {
    228 		code = sc->sc_rxq[sc->sc_rxqhead];
    229 		sc->sc_rxqhead = OMKBD_NEXTRXQ(sc->sc_rxqhead);
    230 #if NWSMOUSE > 0
    231 		/*
    232 		 * if (code >= 0x80 && code <= 0x87), then
    233 		 * it's the first byte of 3 byte long mouse report
    234 		 *	code[0] & 07 -> LMR button condition
    235 		 *	code[1], [2] -> x,y delta
    236 		 * otherwise, key press or release event.
    237 		 */
    238 		if (sc->sc_msreport == 0) {
    239 			if (code < 0x80 || code > 0x87) {
    240 				omkbd_input(sc, code);
    241 				continue;
    242 			}
    243 			code = (code & 07) ^ 07;
    244 			/* LMR->RML: wsevent counts 0 for leftmost */
    245 			sc->sc_msbuttons = (code & 02);
    246 			if ((code & 01) != 0)
    247 				sc->sc_msbuttons |= 04;
    248 			if ((code & 04) != 0)
    249 				sc->sc_msbuttons |= 01;
    250 			sc->sc_msreport = 1;
    251 		} else if (sc->sc_msreport == 1) {
    252 			sc->sc_msdx = (int8_t)code;
    253 			sc->sc_msreport = 2;
    254 		} else if (sc->sc_msreport == 2) {
    255 			sc->sc_msdy = (int8_t)code;
    256 			wsmouse_input(sc->sc_wsmousedev,
    257 			    sc->sc_msbuttons, sc->sc_msdx, sc->sc_msdy, 0, 0,
    258 			    WSMOUSE_INPUT_DELTA);
    259 
    260 			sc->sc_msreport = 0;
    261 		}
    262 #else
    263 		omkbd_input(sc, code);
    264 #endif
    265 	}
    266 }
    267 
    268 static void
    269 omkbd_input(void *v, int data)
    270 {
    271 	struct ws_softc *sc = v;
    272 	u_int type;
    273 	int key;
    274 
    275 	omkbd_decode(v, data, &type, &key);
    276 
    277 #ifdef WSDISPLAY_COMPAT_RAWKBD
    278 	if (sc->sc_rawkbd) {
    279 		uint8_t cbuf[2];
    280 		int c, j = 0;
    281 
    282 		c = omkbd_raw[key];
    283 		if (c != 0x00) {
    284 			/* fake extended scancode if necessary */
    285 			if (c & 0x80)
    286 				cbuf[j++] = 0xe0;
    287 			cbuf[j] = c & 0x7f;
    288 			if (type == WSCONS_EVENT_KEY_UP)
    289 				cbuf[j] |= 0x80;
    290 			j++;
    291 
    292 			wskbd_rawinput(sc->sc_wskbddev, cbuf, j);
    293 		}
    294 	} else
    295 #endif
    296 	{
    297 		if (sc->sc_wskbddev != NULL)
    298 			wskbd_input(sc->sc_wskbddev, type, key);
    299 	}
    300 }
    301 
    302 static void
    303 omkbd_decode(void *v, int datain, u_int *type, int *dataout)
    304 {
    305 
    306 	*type = (datain & 0x80) ? WSCONS_EVENT_KEY_UP : WSCONS_EVENT_KEY_DOWN;
    307 	*dataout = datain & 0x7f;
    308 }
    309 
    310 static void
    311 ws_cngetc(void *v, u_int *type, int *data)
    312 {
    313 	int code;
    314 
    315 	code = syscngetc((dev_t)1);
    316 	omkbd_decode(v, code, type, data);
    317 }
    318 
    319 static void
    320 ws_cnpollc(void *v, int on)
    321 {
    322 }
    323 
    324 /* EXPORT */ void
    325 ws_cnattach(void)
    326 {
    327 	static int voidfill;
    328 
    329 	/* XXX need CH.B initialization XXX */
    330 
    331 	wskbd_cnattach(&ws_consops, &voidfill, &omkbd_keymapdata);
    332 }
    333 
    334 static int
    335 omkbd_enable(void *v, int on)
    336 {
    337 
    338 	return 0;
    339 }
    340 
    341 static void
    342 omkbd_set_leds(void *v, int leds)
    343 {
    344 
    345 #if 0
    346 	syscnputc((dev_t)1, 0x10); /* kana LED on */
    347 	syscnputc((dev_t)1, 0x00); /* kana LED off */
    348 	syscnputc((dev_t)1, 0x11); /* caps LED on */
    349 	syscnputc((dev_t)1, 0x01); /* caps LED off */
    350 #endif
    351 }
    352 
    353 static int
    354 omkbd_ioctl(void *v, u_long cmd, void *data, int flag, struct lwp *l)
    355 {
    356 #ifdef WSDISPLAY_COMPAT_RAWKBD
    357 	struct ws_softc *sc = v;
    358 #endif
    359 
    360 	switch (cmd) {
    361 	case WSKBDIO_GTYPE:
    362 		*(int *)data = WSKBD_TYPE_LUNA;
    363 		return 0;
    364 	case WSKBDIO_SETLEDS:
    365 	case WSKBDIO_GETLEDS:
    366 	case WSKBDIO_COMPLEXBELL:	/* XXX capable of complex bell */
    367 		return 0;
    368 #ifdef WSDISPLAY_COMPAT_RAWKBD
    369 	case WSKBDIO_SETMODE:
    370 		sc->sc_rawkbd = *(int *)data == WSKBD_RAW;
    371 		return 0;
    372 	case WSKBDIO_GETMODE:
    373 		*(int *)data = sc->sc_rawkbd;
    374 		return 0;
    375 #endif
    376 	}
    377 	return EPASSTHROUGH;
    378 }
    379 
    380 #if NWSMOUSE > 0
    381 
    382 static int
    383 omms_enable(void *v)
    384 {
    385 	struct ws_softc *sc = v;
    386 
    387 	syscnputc((dev_t)1, 0x60); /* enable 3 byte long mouse reporting */
    388 	sc->sc_msreport = 0;
    389 	return 0;
    390 }
    391 
    392 /*ARGUSED*/
    393 static int
    394 omms_ioctl(void *v, u_long cmd, void *data, int flag, struct lwp *l)
    395 {
    396 
    397 	if (cmd == WSMOUSEIO_GTYPE) {
    398 		*(u_int *)data = 0x19991005; /* XXX */
    399 		return 0;
    400 	}
    401 	return EPASSTHROUGH;
    402 }
    403 
    404 static void
    405 omms_disable(void *v)
    406 {
    407 	struct ws_softc *sc = v;
    408 
    409 	syscnputc((dev_t)1, 0x20); /* quiet mouse */
    410 	sc->sc_msreport = 0;
    411 }
    412 #endif
    413