Home | History | Annotate | Line # | Download | only in dev
lunaws.c revision 1.28.2.1
      1 /* $NetBSD: lunaws.c,v 1.28.2.1 2014/08/10 06:54:00 tls 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.28.2.1 2014/08/10 06:54:00 tls 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 
     54 #include "ioconf.h"
     55 
     56 #define OMKBD_RXQ_LEN		64
     57 #define OMKBD_RXQ_LEN_MASK	(OMKBD_RXQ_LEN - 1)
     58 #define OMKBD_NEXTRXQ(x)	(((x) + 1) & OMKBD_RXQ_LEN_MASK)
     59 
     60 static const uint8_t ch1_regs[6] = {
     61 	WR0_RSTINT,				/* Reset E/S Interrupt */
     62 	WR1_RXALLS,				/* Rx per char, No Tx */
     63 	0,					/* */
     64 	WR3_RX8BIT | WR3_RXENBL,		/* Rx */
     65 	WR4_BAUD96 | WR4_STOP1 | WR4_NPARITY,	/* Tx/Rx */
     66 	WR5_TX8BIT | WR5_TXENBL,		/* Tx */
     67 };
     68 
     69 struct ws_softc {
     70 	device_t	sc_dev;
     71 	struct sioreg	*sc_ctl;
     72 	uint8_t		sc_wr[6];
     73 	device_t	sc_wskbddev;
     74 	uint8_t		sc_rxq[OMKBD_RXQ_LEN];
     75 	u_int		sc_rxqhead;
     76 	u_int		sc_rxqtail;
     77 #if NWSMOUSE > 0
     78 	device_t	sc_wsmousedev;
     79 	int		sc_msreport;
     80 	int		sc_msbuttons, sc_msdx, sc_msdy;
     81 #endif
     82 	void		*sc_si;
     83 	int		sc_rawkbd;
     84 };
     85 
     86 static void omkbd_input(void *, int);
     87 static void omkbd_decode(void *, int, u_int *, int *);
     88 static int  omkbd_enable(void *, int);
     89 static void omkbd_set_leds(void *, int);
     90 static int  omkbd_ioctl(void *, u_long, void *, int, struct lwp *);
     91 
     92 static const struct wskbd_mapdata omkbd_keymapdata = {
     93 	omkbd_keydesctab,
     94 	KB_JP,
     95 };
     96 static const struct wskbd_accessops omkbd_accessops = {
     97 	omkbd_enable,
     98 	omkbd_set_leds,
     99 	omkbd_ioctl,
    100 };
    101 
    102 void	ws_cnattach(void);
    103 static void ws_cngetc(void *, u_int *, int *);
    104 static void ws_cnpollc(void *, int);
    105 static const struct wskbd_consops ws_consops = {
    106 	ws_cngetc,
    107 	ws_cnpollc,
    108 };
    109 
    110 #if NWSMOUSE > 0
    111 static int  omms_enable(void *);
    112 static int  omms_ioctl(void *, u_long, void *, int, struct lwp *);
    113 static void omms_disable(void *);
    114 
    115 static const struct wsmouse_accessops omms_accessops = {
    116 	omms_enable,
    117 	omms_ioctl,
    118 	omms_disable,
    119 };
    120 #endif
    121 
    122 static void wsintr(void *);
    123 static void wssoftintr(void *);
    124 
    125 static int  wsmatch(device_t, cfdata_t, void *);
    126 static void wsattach(device_t, device_t, void *);
    127 
    128 CFATTACH_DECL_NEW(ws, sizeof(struct ws_softc),
    129     wsmatch, wsattach, NULL, NULL);
    130 
    131 extern int  syscngetc(dev_t);
    132 extern void syscnputc(dev_t, int);
    133 
    134 static int
    135 wsmatch(device_t parent, cfdata_t cf, void *aux)
    136 {
    137 	struct sio_attach_args *args = aux;
    138 
    139 	if (args->channel != 1)
    140 		return 0;
    141 	return 1;
    142 }
    143 
    144 static void
    145 wsattach(device_t parent, device_t self, void *aux)
    146 {
    147 	struct ws_softc *sc = device_private(self);
    148 	struct sio_softc *siosc = device_private(parent);
    149 	struct sio_attach_args *args = aux;
    150 	int channel = args->channel;
    151 	struct wskbddev_attach_args a;
    152 
    153 	sc->sc_dev = self;
    154 	sc->sc_ctl = &siosc->sc_ctl[channel];
    155 	memcpy(sc->sc_wr, ch1_regs, sizeof(ch1_regs));
    156 	siosc->sc_intrhand[channel].ih_func = wsintr;
    157 	siosc->sc_intrhand[channel].ih_arg = sc;
    158 
    159 	setsioreg(sc->sc_ctl, WR0, sc->sc_wr[WR0]);
    160 	setsioreg(sc->sc_ctl, WR4, sc->sc_wr[WR4]);
    161 	setsioreg(sc->sc_ctl, WR3, sc->sc_wr[WR3]);
    162 	setsioreg(sc->sc_ctl, WR5, sc->sc_wr[WR5]);
    163 	setsioreg(sc->sc_ctl, WR0, sc->sc_wr[WR0]);
    164 	setsioreg(sc->sc_ctl, WR1, sc->sc_wr[WR1]);
    165 
    166 	syscnputc((dev_t)1, 0x20); /* keep quiet mouse */
    167 
    168 	sc->sc_rxqhead = 0;
    169 	sc->sc_rxqtail = 0;
    170 
    171 	sc->sc_si = softint_establish(SOFTINT_SERIAL, wssoftintr, sc);
    172 
    173 	aprint_normal("\n");
    174 
    175 	a.console = (args->hwflags == 1);
    176 	a.keymap = &omkbd_keymapdata;
    177 	a.accessops = &omkbd_accessops;
    178 	a.accesscookie = (void *)sc;
    179 	sc->sc_wskbddev = config_found_ia(self, "wskbddev", &a, wskbddevprint);
    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 =
    187 	    config_found_ia(self, "wsmousedev", &b, wsmousedevprint);
    188 	sc->sc_msreport = 0;
    189 	}
    190 #endif
    191 }
    192 
    193 /*ARGSUSED*/
    194 static void
    195 wsintr(void *arg)
    196 {
    197 	struct ws_softc *sc = arg;
    198 	struct sioreg *sio = sc->sc_ctl;
    199 	uint8_t code;
    200 	int rr;
    201 
    202 	rr = getsiocsr(sio);
    203 	if ((rr & RR_RXRDY) != 0) {
    204 		do {
    205 			code = sio->sio_data;
    206 			if (rr & (RR_FRAMING | RR_OVERRUN | RR_PARITY)) {
    207 				sio->sio_cmd = WR0_ERRRST;
    208 				continue;
    209 			}
    210 			sc->sc_rxq[sc->sc_rxqtail] = code;
    211 			sc->sc_rxqtail = OMKBD_NEXTRXQ(sc->sc_rxqtail);
    212 		} while (((rr = getsiocsr(sio)) & RR_RXRDY) != 0);
    213 		softint_schedule(sc->sc_si);
    214 	}
    215 	if ((rr & RR_TXRDY) != 0)
    216 		sio->sio_cmd = WR0_RSTPEND;
    217 	/* not capable of transmit, yet */
    218 }
    219 
    220 static void
    221 wssoftintr(void *arg)
    222 {
    223 	struct ws_softc *sc = arg;
    224 	uint8_t code;
    225 
    226 	while (sc->sc_rxqhead != sc->sc_rxqtail) {
    227 		code = sc->sc_rxq[sc->sc_rxqhead];
    228 		sc->sc_rxqhead = OMKBD_NEXTRXQ(sc->sc_rxqhead);
    229 #if NWSMOUSE > 0
    230 		/*
    231 		 * if (code >= 0x80 && code <= 0x87), then
    232 		 * it's the first byte of 3 byte long mouse report
    233 		 *	code[0] & 07 -> LMR button condition
    234 		 *	code[1], [2] -> x,y delta
    235 		 * otherwise, key press or release event.
    236 		 */
    237 		if (sc->sc_msreport == 0) {
    238 			if (code < 0x80 || code > 0x87) {
    239 				omkbd_input(sc, code);
    240 				continue;
    241 			}
    242 			code = (code & 07) ^ 07;
    243 			/* LMR->RML: wsevent counts 0 for leftmost */
    244 			sc->sc_msbuttons = (code & 02);
    245 			if ((code & 01) != 0)
    246 				sc->sc_msbuttons |= 04;
    247 			if ((code & 04) != 0)
    248 				sc->sc_msbuttons |= 01;
    249 			sc->sc_msreport = 1;
    250 		} else if (sc->sc_msreport == 1) {
    251 			sc->sc_msdx = (int8_t)code;
    252 			sc->sc_msreport = 2;
    253 		} else if (sc->sc_msreport == 2) {
    254 			sc->sc_msdy = (int8_t)code;
    255 			wsmouse_input(sc->sc_wsmousedev,
    256 			    sc->sc_msbuttons, sc->sc_msdx, sc->sc_msdy, 0, 0,
    257 			    WSMOUSE_INPUT_DELTA);
    258 
    259 			sc->sc_msreport = 0;
    260 		}
    261 #else
    262 		omkbd_input(sc, code);
    263 #endif
    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 	struct ws_softc *sc = v;
    385 
    386 	syscnputc((dev_t)1, 0x60); /* enable 3 byte long mouse reporting */
    387 	sc->sc_msreport = 0;
    388 	return 0;
    389 }
    390 
    391 /*ARGUSED*/
    392 static int
    393 omms_ioctl(void *v, u_long cmd, void *data, int flag, struct lwp *l)
    394 {
    395 
    396 	if (cmd == WSMOUSEIO_GTYPE) {
    397 		*(u_int *)data = 0x19991005; /* XXX */
    398 		return 0;
    399 	}
    400 	return EPASSTHROUGH;
    401 }
    402 
    403 static void
    404 omms_disable(void *v)
    405 {
    406 	struct ws_softc *sc = v;
    407 
    408 	syscnputc((dev_t)1, 0x20); /* quiet mouse */
    409 	sc->sc_msreport = 0;
    410 }
    411 #endif
    412