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