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