Home | History | Annotate | Line # | Download | only in dev
lunaws.c revision 1.9
      1 /* $NetBSD: lunaws.c,v 1.9 2002/10/02 05:31:46 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  * 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 #include <sys/cdefs.h>			/* RCS ID & Copyright macro defns */
     40 
     41 __KERNEL_RCSID(0, "$NetBSD: lunaws.c,v 1.9 2002/10/02 05:31:46 thorpej Exp $");
     42 
     43 #include "wsmouse.h"
     44 
     45 #include <sys/param.h>
     46 #include <sys/systm.h>
     47 #include <sys/conf.h>
     48 #include <sys/device.h>
     49 
     50 #include <dev/wscons/wsconsio.h>
     51 #include <dev/wscons/wskbdvar.h>
     52 #include <dev/wscons/wsksymdef.h>
     53 #include <dev/wscons/wsksymvar.h>
     54 #include <dev/wscons/wsmousevar.h>
     55 
     56 #include <luna68k/dev/sioreg.h>
     57 #include <luna68k/dev/siovar.h>
     58 
     59 static const u_int8_t ch1_regs[6] = {
     60 	WR0_RSTINT,				/* Reset E/S Interrupt */
     61 	WR1_RXALLS,				/* Rx per char, No Tx */
     62 	0,					/* */
     63 	WR3_RX8BIT | WR3_RXENBL,		/* Rx */
     64 	WR4_BAUD96 | WR4_STOP1 | WR4_NPARITY,	/* Tx/Rx */
     65 	WR5_TX8BIT | WR5_TXENBL,		/* Tx */
     66 };
     67 
     68 struct ws_softc {
     69 	struct device	sc_dv;
     70 	struct sioreg	*sc_ctl;
     71 	u_int8_t	sc_wr[6];
     72 	struct device	*sc_wskbddev;
     73 #if NWSMOUSE > 0
     74 	struct device	*sc_wsmousedev;
     75 	int		sc_msreport;
     76 	int		buttons, dx, dy;
     77 #endif
     78 };
     79 
     80 static void omkbd_input __P((void *, int));
     81 static int  omkbd_decode __P((void *, int, u_int *, int *));
     82 static int  omkbd_enable __P((void *, int));
     83 static void omkbd_set_leds __P((void *, int));
     84 static int  omkbd_ioctl __P((void *, u_long, caddr_t, int, struct proc *));
     85 
     86 struct wscons_keydesc omkbd_keydesctab[];
     87 
     88 static const struct wskbd_mapdata omkbd_keymapdata = {
     89 	omkbd_keydesctab,
     90 	KB_JP,
     91 };
     92 static const struct wskbd_accessops omkbd_accessops = {
     93 	omkbd_enable,
     94 	omkbd_set_leds,
     95 	omkbd_ioctl,
     96 };
     97 
     98 void	ws_cnattach __P((void));
     99 static void ws_cngetc __P((void *, u_int *, int *));
    100 static void ws_cnpollc __P((void *, int));
    101 static const struct wskbd_consops ws_consops = {
    102 	ws_cngetc,
    103 	ws_cnpollc,
    104 };
    105 
    106 #if NWSMOUSE > 0
    107 static int  omms_enable __P((void *));
    108 static int  omms_ioctl __P((void *, u_long, caddr_t, int, struct proc *));
    109 static void omms_disable __P((void *));
    110 
    111 static const struct wsmouse_accessops omms_accessops = {
    112 	omms_enable,
    113 	omms_ioctl,
    114 	omms_disable,
    115 };
    116 #endif
    117 
    118 static void wsintr __P((int));
    119 
    120 static int  wsmatch __P((struct device *, struct cfdata *, void *));
    121 static void wsattach __P((struct device *, struct device *, void *));
    122 static int  ws_submatch_kbd __P((struct device *, struct cfdata *, void *));
    123 #if NWSMOUSE > 0
    124 static int  ws_submatch_mouse __P((struct device *, struct cfdata *, void *));
    125 #endif
    126 
    127 CFATTACH_DECL(ws, sizeof(struct ws_softc),
    128     wsmatch, wsattach, NULL, NULL);
    129 extern struct cfdriver ws_cd;
    130 
    131 extern int  syscngetc __P((dev_t));
    132 extern void syscnputc __P((dev_t, int));
    133 
    134 static int
    135 wsmatch(parent, match, aux)
    136 	struct device *parent;
    137 	struct cfdata *match;
    138 	void *aux;
    139 {
    140 	struct sio_attach_args *args = aux;
    141 
    142 	if (args->channel != 1)
    143 		return 0;
    144 	return 1;
    145 }
    146 
    147 static void
    148 wsattach(parent, self, aux)
    149 	struct device *parent;
    150 	struct device *self;
    151 	void *aux;
    152 {
    153 	struct ws_softc *sc = (struct ws_softc *)self;
    154 	struct sio_softc *scp = (struct sio_softc *)parent;
    155 	struct sio_attach_args *args = aux;
    156 	struct wskbddev_attach_args a;
    157 
    158 	sc->sc_ctl = (struct sioreg *)scp->scp_ctl + 1;
    159 	bcopy(ch1_regs, sc->sc_wr, sizeof(ch1_regs));
    160 	scp->scp_intr[1] = wsintr;
    161 
    162 	setsioreg(sc->sc_ctl, WR0, sc->sc_wr[WR0]);
    163 	setsioreg(sc->sc_ctl, WR4, sc->sc_wr[WR4]);
    164 	setsioreg(sc->sc_ctl, WR3, sc->sc_wr[WR3]);
    165 	setsioreg(sc->sc_ctl, WR5, sc->sc_wr[WR5]);
    166 	setsioreg(sc->sc_ctl, WR0, sc->sc_wr[WR0]);
    167 	setsioreg(sc->sc_ctl, WR1, sc->sc_wr[WR1]);
    168 
    169 	syscnputc((dev_t)1, 0x20); /* keep quiet mouse */
    170 
    171 	printf("\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_sm(self, &a, wskbddevprint,
    178 					ws_submatch_kbd);
    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_sm(self, &b, wsmousedevprint,
    186 					ws_submatch_mouse);
    187 	sc->sc_msreport = 0;
    188 	}
    189 #endif
    190 }
    191 
    192 static int
    193 ws_submatch_kbd(parent, cf, aux)
    194         struct device *parent;
    195         struct cfdata *cf;
    196         void *aux;
    197 {
    198 
    199         if (strcmp(cf->cf_name, "wskbd"))
    200                 return (0);
    201         return (config_match(parent, cf, aux));
    202 }
    203 
    204 #if NWSMOUSE > 0
    205 
    206 static int
    207 ws_submatch_mouse(parent, cf, aux)
    208         struct device *parent;
    209         struct cfdata *cf;
    210         void *aux;
    211 {
    212 
    213         if (strcmp(cf->cf_name, "wsmouse"))
    214                 return (0);
    215         return (config_match(parent, cf, aux));
    216 }
    217 
    218 #endif
    219 
    220 /*ARGSUSED*/
    221 static void
    222 wsintr(chan)
    223 	int chan;
    224 {
    225 	struct ws_softc *sc = ws_cd.cd_devs[0];
    226 	struct sioreg *sio = sc->sc_ctl;
    227 	u_int code;
    228 	int rr;
    229 
    230 	rr = getsiocsr(sio);
    231 	if (rr & RR_RXRDY) {
    232 		do {
    233 			code = sio->sio_data;
    234 			if (rr & (RR_FRAMING | RR_OVERRUN | RR_PARITY)) {
    235 				sio->sio_cmd = WR0_ERRRST;
    236 				continue;
    237 			}
    238 #if NWSMOUSE > 0
    239 			/*
    240 			 * if (code >= 0x80 && code <= 0x87), then
    241 			 * it's the first byte of 3 byte long mouse report
    242 			 * 	code[0] & 07 -> LMR button condition
    243 			 *	code[1], [2] -> x,y delta
    244 			 * otherwise, key press or release event.
    245 			 */
    246 			if (sc->sc_msreport == 0) {
    247 				if (code < 0x80 || code > 0x87) {
    248 					omkbd_input(sc, code);
    249 					continue;
    250 				}
    251 				code = (code & 07) ^ 07;
    252 				/* LMR->RML: wsevent counts 0 for leftmost */
    253 				sc->buttons = (code & 02);
    254 				if (code & 01)
    255 					sc->buttons |= 04;
    256 				if (code & 04)
    257 					sc->buttons |= 01;
    258 				sc->sc_msreport = 1;
    259 			}
    260 			else if (sc->sc_msreport == 1) {
    261 				sc->dx = (signed char)code;
    262 				sc->sc_msreport = 2;
    263 			}
    264 			else if (sc->sc_msreport == 2) {
    265 				sc->dy = (signed char)code;
    266 				wsmouse_input(sc->sc_wsmousedev,
    267 					sc->buttons, sc->dx, sc->dy, 0, 0);
    268 				sc->sc_msreport = 0;
    269 			}
    270 #else
    271 			omkbd_input(sc, code);
    272 #endif
    273 		} while ((rr = getsiocsr(sio)) & RR_RXRDY);
    274 	}
    275 	if (rr && RR_TXRDY)
    276 		sio->sio_cmd = WR0_RSTPEND;
    277 	/* not capable of transmit, yet */
    278 }
    279 
    280 static void
    281 omkbd_input(v, data)
    282 	void *v;
    283 	int data;
    284 {
    285 	struct ws_softc *sc = v;
    286 	u_int type;
    287 	int key;
    288 
    289 	if (omkbd_decode(v, data, &type, &key))
    290 		wskbd_input(sc->sc_wskbddev, type, key);
    291 }
    292 
    293 static int
    294 omkbd_decode(v, datain, type, dataout)
    295 	void *v;
    296 	int datain;
    297 	u_int *type;
    298 	int *dataout;
    299 {
    300 	*type = (datain & 0x80) ? WSCONS_EVENT_KEY_UP : WSCONS_EVENT_KEY_DOWN;
    301 	*dataout = datain & 0x7f;
    302 	return 1;
    303 }
    304 
    305 #define KC(n) KS_KEYCODE(n)
    306 
    307 static const keysym_t omkbd_keydesc_1[] = {
    308 /*  pos      command		normal		shifted */
    309     KC(0x9), 			KS_Tab,
    310     KC(0xa),  			KS_Control_L,
    311     KC(0xb),			KS_Mode_switch,	/* Kana */
    312     KC(0xc),			KS_Shift_R,
    313     KC(0xd),			KS_Shift_L,
    314     KC(0xe),			KS_Caps_Lock,
    315     KC(0xf),			KS_Meta_L,	/* Zenmen */
    316     KC(0x10),			KS_Escape,
    317     KC(0x11),			KS_BackSpace,
    318     KC(0x12),			KS_Return,
    319     KC(0x14),			KS_space,
    320     KC(0x15),			KS_Delete,
    321     KC(0x16),			KS_Alt_L,	/* Henkan */
    322     KC(0x17),			KS_Alt_R,	/* Kakutei */
    323     KC(0x18),			KS_f11,		/* Shokyo */
    324     KC(0x19),			KS_f12,		/* Yobidashi */
    325     KC(0x1a),			KS_f13,		/* Bunsetsu L */
    326     KC(0x1b),			KS_f14,		/* Bunsetsu R */
    327     KC(0x1c),			KS_KP_Up,
    328     KC(0x1d),			KS_KP_Left,
    329     KC(0x1e),			KS_KP_Right,
    330     KC(0x1f),			KS_KP_Down,
    331  /* KC(0x20),			KS_f11, */
    332  /* KC(0x21),			KS_f12, */
    333     KC(0x22),  			KS_1,		KS_exclam,
    334     KC(0x23),			KS_2,		KS_quotedbl,
    335     KC(0x24),			KS_3,		KS_numbersign,
    336     KC(0x25),			KS_4,		KS_dollar,
    337     KC(0x26),			KS_5,		KS_percent,
    338     KC(0x27),			KS_6,		KS_ampersand,
    339     KC(0x28),			KS_7,		KS_apostrophe,
    340     KC(0x29),			KS_8,		KS_parenleft,
    341     KC(0x2a),			KS_9,		KS_parenright,
    342     KC(0x2b),			KS_0,
    343     KC(0x2c),			KS_minus,	KS_equal,
    344     KC(0x2d),			KS_asciicircum,	KS_asciitilde,
    345     KC(0x2e),			KS_backslash,	KS_bar,
    346  /* KC(0x30),			KS_f13, */
    347  /* KC(0x31),			KS_f14, */
    348     KC(0x32),			KS_q,
    349     KC(0x33),			KS_w,
    350     KC(0x34),			KS_e,
    351     KC(0x35),			KS_r,
    352     KC(0x36),			KS_t,
    353     KC(0x37),			KS_y,
    354     KC(0x38),			KS_u,
    355     KC(0x39),			KS_i,
    356     KC(0x3a),			KS_o,
    357     KC(0x3b),			KS_p,
    358     KC(0x3c),			KS_at,		KS_grave,
    359     KC(0x3d),			KS_bracketleft,	KS_braceleft,
    360     KC(0x42),			KS_a,
    361     KC(0x43),			KS_s,
    362     KC(0x44),			KS_d,
    363     KC(0x45),			KS_f,
    364     KC(0x46),			KS_g,
    365     KC(0x47),			KS_h,
    366     KC(0x48),			KS_j,
    367     KC(0x49),			KS_k,
    368     KC(0x4a),			KS_l,
    369     KC(0x4b),			KS_semicolon,	KS_plus,
    370     KC(0x4c),			KS_colon,	KS_asterisk,
    371     KC(0x4d),			KS_bracketright, KS_braceright,
    372     KC(0x52),			KS_z,
    373     KC(0x53),			KS_x,
    374     KC(0x54),			KS_c,
    375     KC(0x55),			KS_v,
    376     KC(0x56),			KS_b,
    377     KC(0x57),			KS_n,
    378     KC(0x58),			KS_m,
    379     KC(0x59),			KS_comma,	KS_less,
    380     KC(0x5a),			KS_period,	KS_greater,
    381     KC(0x5b),			KS_slash,	KS_question,
    382     KC(0x5c),			KS_underscore,
    383     KC(0x60),			KS_KP_Delete,
    384     KC(0x61),			KS_KP_Add,
    385     KC(0x62),			KS_KP_Subtract,
    386     KC(0x63),			KS_KP_7,
    387     KC(0x64),			KS_KP_8,
    388     KC(0x65),			KS_KP_9,
    389     KC(0x66),			KS_KP_4,
    390     KC(0x67),			KS_KP_5,
    391     KC(0x68),			KS_KP_6,
    392     KC(0x69),			KS_KP_1,
    393     KC(0x6a),			KS_KP_2,
    394     KC(0x6b),			KS_KP_3,
    395     KC(0x6c),			KS_KP_0,
    396     KC(0x6d),			KS_KP_Decimal,
    397     KC(0x6e),			KS_KP_Enter,
    398     KC(0x72),			KS_f1,
    399     KC(0x73),			KS_f2,
    400     KC(0x74),			KS_f3,
    401     KC(0x75),			KS_f4,
    402     KC(0x76),			KS_f5,
    403     KC(0x77),			KS_f6,
    404     KC(0x78),			KS_f7,
    405     KC(0x79),			KS_f8,
    406     KC(0x7a),			KS_f9,
    407     KC(0x7b),			KS_f10,
    408     KC(0x7c),			KS_KP_Multiply,
    409     KC(0x7d),			KS_KP_Divide,
    410     KC(0x7e),			KS_KP_Equal,
    411     KC(0x7f),			KS_KP_Separator,
    412 };
    413 
    414 #define	SIZE(map) (sizeof(map)/sizeof(keysym_t))
    415 
    416 struct wscons_keydesc omkbd_keydesctab[] = {
    417 	{ KB_JP, 0, SIZE(omkbd_keydesc_1), omkbd_keydesc_1, },
    418 	{ 0, 0, 0, 0 },
    419 };
    420 
    421 static void
    422 ws_cngetc(v, type, data)
    423 	void *v;
    424 	u_int *type;
    425 	int *data;
    426 {
    427 	int code;
    428 
    429 	do {
    430 		code = syscngetc((dev_t)1);
    431 	} while (!omkbd_decode(v, code, type, data));
    432 }
    433 
    434 static void
    435 ws_cnpollc(v, on)
    436 	void *v;
    437         int on;
    438 {
    439 }
    440 
    441 /* EXPORT */ void
    442 ws_cnattach()
    443 {
    444 	static int voidfill;
    445 
    446 	/* XXX need CH.B initialization XXX */
    447 
    448 	wskbd_cnattach(&ws_consops, &voidfill, &omkbd_keymapdata);
    449 }
    450 
    451 static int
    452 omkbd_enable(v, on)
    453 	void *v;
    454 	int on;
    455 {
    456 	return 0;
    457 }
    458 
    459 static void
    460 omkbd_set_leds(v, leds)
    461 	void *v;
    462 	int leds;
    463 {
    464 #if 0
    465 	syscnputc((dev_t)1, 0x10); /* kana LED on */
    466 	syscnputc((dev_t)1, 0x00); /* kana LED off */
    467 	syscnputc((dev_t)1, 0x11); /* caps LED on */
    468 	syscnputc((dev_t)1, 0x01); /* caps LED off */
    469 #endif
    470 }
    471 
    472 static int
    473 omkbd_ioctl(v, cmd, data, flag, p)
    474 	void *v;
    475 	u_long cmd;
    476 	caddr_t data;
    477 	int flag;
    478 	struct proc *p;
    479 {
    480 	switch (cmd) {
    481 	    case WSKBDIO_GTYPE:
    482 		*(int *)data = 0x19991005 /* XXX */;
    483 		return 0;
    484 	    case WSKBDIO_SETLEDS:
    485 	    case WSKBDIO_GETLEDS:
    486 	    case WSKBDIO_COMPLEXBELL:	/* XXX capable of complex bell */
    487 		return 0;
    488 	}
    489 	return EPASSTHROUGH;
    490 }
    491 
    492 #if NWSMOUSE > 0
    493 
    494 static int
    495 omms_enable(v)
    496 	void *v;
    497 {
    498 	struct ws_softc *sc = v;
    499 
    500 	syscnputc((dev_t)1, 0x60); /* enable 3 byte long mouse reporting */
    501 	sc->sc_msreport = 0;
    502 	return 0;
    503 }
    504 
    505 /*ARGUSED*/
    506 static int
    507 omms_ioctl(v, cmd, data, flag, p)
    508 	void *v;
    509 	u_long cmd;
    510 	caddr_t data;
    511 	int flag;
    512 	struct proc *p;
    513 {
    514 	if (cmd == WSMOUSEIO_GTYPE) {
    515 		*(u_int *)data = 0x19991005; /* XXX */
    516 		return 0;
    517 	}
    518 	return EPASSTHROUGH;
    519 }
    520 
    521 static void
    522 omms_disable(v)
    523 	void *v;
    524 {
    525 	struct ws_softc *sc = v;
    526 
    527 	syscnputc((dev_t)1, 0x20); /* quiet mouse */
    528 	sc->sc_msreport = 0;
    529 }
    530 #endif
    531