Home | History | Annotate | Line # | Download | only in dev
lunaws.c revision 1.34
      1 /* $NetBSD: lunaws.c,v 1.34 2021/09/04 18:38:03 tsutsui 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.34 2021/09/04 18:38:03 tsutsui 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_msbuttons, sc_msdx, sc_msdy;
     81 #endif
     82 	int		sc_msreport;
     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 	    CFARGS(.iattr = "wskbddev"));
    179 
    180 #if NWSMOUSE > 0
    181 	{
    182 	struct wsmousedev_attach_args b;
    183 	b.accessops = &omms_accessops;
    184 	b.accesscookie = (void *)sc;
    185 	sc->sc_wsmousedev = config_found(self, &b, wsmousedevprint,
    186 	    CFARGS(.iattr = "wsmousedev"));
    187 	}
    188 #endif
    189 	sc->sc_msreport = 0;
    190 }
    191 
    192 /*ARGSUSED*/
    193 static void
    194 wsintr(void *arg)
    195 {
    196 	struct ws_softc *sc = arg;
    197 	struct sioreg *sio = sc->sc_ctl;
    198 	uint8_t code;
    199 	int rr;
    200 
    201 	rr = getsiocsr(sio);
    202 	if ((rr & RR_RXRDY) != 0) {
    203 		do {
    204 			code = sio->sio_data;
    205 			if (rr & (RR_FRAMING | RR_OVERRUN | RR_PARITY)) {
    206 				sio->sio_cmd = WR0_ERRRST;
    207 				continue;
    208 			}
    209 			sc->sc_rxq[sc->sc_rxqtail] = code;
    210 			sc->sc_rxqtail = OMKBD_NEXTRXQ(sc->sc_rxqtail);
    211 		} while (((rr = getsiocsr(sio)) & RR_RXRDY) != 0);
    212 		softint_schedule(sc->sc_si);
    213 	}
    214 	if ((rr & RR_TXRDY) != 0)
    215 		sio->sio_cmd = WR0_RSTPEND;
    216 	/* not capable of transmit, yet */
    217 }
    218 
    219 static void
    220 wssoftintr(void *arg)
    221 {
    222 	struct ws_softc *sc = arg;
    223 	uint8_t code;
    224 
    225 	while (sc->sc_rxqhead != sc->sc_rxqtail) {
    226 		code = sc->sc_rxq[sc->sc_rxqhead];
    227 		sc->sc_rxqhead = OMKBD_NEXTRXQ(sc->sc_rxqhead);
    228 		/*
    229 		 * if (code >= 0x80 && code <= 0x87), i.e.
    230 		 * if ((code & 0xf8) == 0x80), then
    231 		 * it's the first byte of 3 byte long mouse report
    232 		 *	code[0] & 07 -> LMR button condition
    233 		 *	code[1], [2] -> x,y delta
    234 		 * otherwise, key press or release event.
    235 		 */
    236 		if (sc->sc_msreport == 1) {
    237 #if NWSMOUSE > 0
    238 			sc->sc_msdx = (int8_t)code;
    239 #endif
    240 			sc->sc_msreport = 2;
    241 			continue;
    242 		} else if (sc->sc_msreport == 2) {
    243 #if NWSMOUSE > 0
    244 			sc->sc_msdy = (int8_t)code;
    245 			wsmouse_input(sc->sc_wsmousedev,
    246 			    sc->sc_msbuttons, sc->sc_msdx, sc->sc_msdy, 0, 0,
    247 			    WSMOUSE_INPUT_DELTA);
    248 #endif
    249 			sc->sc_msreport = 0;
    250 			continue;
    251 		}
    252 		if ((code & 0xf8) == 0x80) {
    253 #if NWSMOUSE > 0
    254 			/* buttons: Negative logic to positive */
    255 			code = ~code;
    256 			/* LMR->RML: wsevent counts 0 for leftmost */
    257 			sc->sc_msbuttons =
    258 			    ((code & 1) << 2) | (code & 2) | ((code & 4) >> 2);
    259 #endif
    260 			sc->sc_msreport = 1;
    261 			continue;
    262 		}
    263 		omkbd_input(sc, code);
    264 	}
    265 }
    266 
    267 static void
    268 omkbd_input(void *v, int data)
    269 {
    270 	struct ws_softc *sc = v;
    271 	u_int type;
    272 	int key;
    273 
    274 	omkbd_decode(v, data, &type, &key);
    275 
    276 #ifdef WSDISPLAY_COMPAT_RAWKBD
    277 	if (sc->sc_rawkbd) {
    278 		uint8_t cbuf[2];
    279 		int c, j = 0;
    280 
    281 		c = omkbd_raw[key];
    282 		if (c != 0x00) {
    283 			/* fake extended scancode if necessary */
    284 			if (c & 0x80)
    285 				cbuf[j++] = 0xe0;
    286 			cbuf[j] = c & 0x7f;
    287 			if (type == WSCONS_EVENT_KEY_UP)
    288 				cbuf[j] |= 0x80;
    289 			j++;
    290 
    291 			wskbd_rawinput(sc->sc_wskbddev, cbuf, j);
    292 		}
    293 	} else
    294 #endif
    295 	{
    296 		if (sc->sc_wskbddev != NULL)
    297 			wskbd_input(sc->sc_wskbddev, type, key);
    298 	}
    299 }
    300 
    301 static void
    302 omkbd_decode(void *v, int datain, u_int *type, int *dataout)
    303 {
    304 
    305 	*type = (datain & 0x80) ? WSCONS_EVENT_KEY_UP : WSCONS_EVENT_KEY_DOWN;
    306 	*dataout = datain & 0x7f;
    307 }
    308 
    309 static void
    310 ws_cngetc(void *v, u_int *type, int *data)
    311 {
    312 	int code;
    313 
    314 	code = syscngetc((dev_t)1);
    315 	omkbd_decode(v, code, type, data);
    316 }
    317 
    318 static void
    319 ws_cnpollc(void *v, int on)
    320 {
    321 }
    322 
    323 /* EXPORT */ void
    324 ws_cnattach(void)
    325 {
    326 	static int voidfill;
    327 
    328 	/* XXX need CH.B initialization XXX */
    329 
    330 	wskbd_cnattach(&ws_consops, &voidfill, &omkbd_keymapdata);
    331 }
    332 
    333 static int
    334 omkbd_enable(void *v, int on)
    335 {
    336 
    337 	return 0;
    338 }
    339 
    340 static void
    341 omkbd_set_leds(void *v, int leds)
    342 {
    343 
    344 #if 0
    345 	syscnputc((dev_t)1, 0x10); /* kana LED on */
    346 	syscnputc((dev_t)1, 0x00); /* kana LED off */
    347 	syscnputc((dev_t)1, 0x11); /* caps LED on */
    348 	syscnputc((dev_t)1, 0x01); /* caps LED off */
    349 #endif
    350 }
    351 
    352 static int
    353 omkbd_ioctl(void *v, u_long cmd, void *data, int flag, struct lwp *l)
    354 {
    355 #ifdef WSDISPLAY_COMPAT_RAWKBD
    356 	struct ws_softc *sc = v;
    357 #endif
    358 
    359 	switch (cmd) {
    360 	case WSKBDIO_GTYPE:
    361 		*(int *)data = WSKBD_TYPE_LUNA;
    362 		return 0;
    363 	case WSKBDIO_SETLEDS:
    364 	case WSKBDIO_GETLEDS:
    365 	case WSKBDIO_COMPLEXBELL:	/* XXX capable of complex bell */
    366 		return 0;
    367 #ifdef WSDISPLAY_COMPAT_RAWKBD
    368 	case WSKBDIO_SETMODE:
    369 		sc->sc_rawkbd = *(int *)data == WSKBD_RAW;
    370 		return 0;
    371 	case WSKBDIO_GETMODE:
    372 		*(int *)data = sc->sc_rawkbd;
    373 		return 0;
    374 #endif
    375 	}
    376 	return EPASSTHROUGH;
    377 }
    378 
    379 #if NWSMOUSE > 0
    380 
    381 static int
    382 omms_enable(void *v)
    383 {
    384 	syscnputc((dev_t)1, 0x60); /* enable 3 byte long mouse reporting */
    385 	return 0;
    386 }
    387 
    388 /*ARGUSED*/
    389 static int
    390 omms_ioctl(void *v, u_long cmd, void *data, int flag, struct lwp *l)
    391 {
    392 
    393 	if (cmd == WSMOUSEIO_GTYPE) {
    394 		*(u_int *)data = 0x19991005; /* XXX */
    395 		return 0;
    396 	}
    397 	return EPASSTHROUGH;
    398 }
    399 
    400 static void
    401 omms_disable(void *v)
    402 {
    403 	syscnputc((dev_t)1, 0x20); /* quiet mouse */
    404 }
    405 #endif
    406