Home | History | Annotate | Line # | Download | only in maple
mkbd.c revision 1.20
      1 /*	$NetBSD: mkbd.c,v 1.20 2005/02/19 15:37:35 tsutsui Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2001 Marcus Comstedt
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by Marcus Comstedt.
     18  * 4. Neither the name of The NetBSD Foundation nor the names of its
     19  *    contributors may be used to endorse or promote products derived
     20  *    from this software without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     23  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     24  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     25  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     26  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     27  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     28  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     29  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     30  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     31  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     32  * POSSIBILITY OF SUCH DAMAGE.
     33  */
     34 
     35 #include <sys/cdefs.h>
     36 __KERNEL_RCSID(0, "$NetBSD: mkbd.c,v 1.20 2005/02/19 15:37:35 tsutsui Exp $");
     37 
     38 #include <sys/param.h>
     39 #include <sys/device.h>
     40 #include <sys/fcntl.h>
     41 #include <sys/poll.h>
     42 #include <sys/select.h>
     43 #include <sys/proc.h>
     44 #include <sys/signalvar.h>
     45 #include <sys/systm.h>
     46 
     47 #include "wskbd.h"
     48 
     49 #include <dev/wscons/wsconsio.h>
     50 #include <dev/wscons/wskbdvar.h>
     51 #include <dev/wscons/wsksymdef.h>
     52 #include <dev/wscons/wsksymvar.h>
     53 
     54 #include <machine/cpu.h>
     55 #include <machine/bus.h>
     56 
     57 #include <dreamcast/dev/maple/maple.h>
     58 #include <dreamcast/dev/maple/mapleconf.h>
     59 #include <dreamcast/dev/maple/mkbdvar.h>
     60 #include <dreamcast/dev/maple/mkbdmap.h>
     61 
     62 /*
     63  * Function declarations.
     64  */
     65 static	int mkbdmatch(struct device *, struct cfdata *, void *);
     66 static	void mkbdattach(struct device *, struct device *, void *);
     67 static	int mkbddetach(struct device *, int);
     68 
     69 int	mkbd_enable(void *, int);
     70 void	mkbd_set_leds(void *, int);
     71 int	mkbd_ioctl(void *, u_long, caddr_t, int, struct proc *);
     72 
     73 struct wskbd_accessops mkbd_accessops = {
     74 	mkbd_enable,
     75 	mkbd_set_leds,
     76 	mkbd_ioctl,
     77 };
     78 
     79 static void mkbd_intr(void *, struct maple_response *, int, int);
     80 
     81 void	mkbd_cngetc(void *, u_int *, int *);
     82 void	mkbd_cnpollc(void *, int);
     83 int	mkbd_cnattach(void);
     84 
     85 struct wskbd_consops mkbd_consops = {
     86 	mkbd_cngetc,
     87 	mkbd_cnpollc,
     88 };
     89 
     90 struct wskbd_mapdata mkbd_keymapdata = {
     91 	mkbd_keydesctab,
     92 	KB_JP,
     93 };
     94 
     95 static struct mkbd_softc *mkbd_console_softc;
     96 
     97 static int mkbd_is_console;
     98 static int mkbd_console_initted;
     99 
    100 CFATTACH_DECL(mkbd, sizeof(struct mkbd_softc),
    101     mkbdmatch, mkbdattach, mkbddetach, NULL);
    102 
    103 static int
    104 mkbdmatch(struct device *parent, struct cfdata *cf, void *aux)
    105 {
    106 	struct maple_attach_args *ma = aux;
    107 
    108 	return ma->ma_function == MAPLE_FN_KEYBOARD ? MAPLE_MATCH_FUNC : 0;
    109 }
    110 
    111 static void
    112 mkbdattach(struct device *parent, struct device *self, void *aux)
    113 {
    114 	struct mkbd_softc *sc = (struct mkbd_softc *) self;
    115 	struct maple_attach_args *ma = aux;
    116 #if NWSKBD > 0
    117 	struct wskbddev_attach_args a;
    118 #endif
    119 	uint32_t kbdtype;
    120 
    121 	sc->sc_parent = parent;
    122 	sc->sc_unit = ma->ma_unit;
    123 
    124 	kbdtype = maple_get_function_data(ma->ma_devinfo,
    125 	    MAPLE_FN_KEYBOARD) >> 24;
    126 	switch (kbdtype) {
    127 	case 1:
    128 		printf(": Japanese keyboard");
    129 		mkbd_keymapdata.layout = KB_JP;
    130 		break;
    131 	case 2:
    132 		printf(": US keyboard");
    133 		mkbd_keymapdata.layout = KB_US;
    134 		break;
    135 	case 3:
    136 		printf(": European keyboard");
    137 		mkbd_keymapdata.layout = KB_UK;
    138 		break;
    139 	default:
    140 		printf(": Unknown keyboard %d", kbdtype);
    141 	}
    142 	printf("\n");
    143 
    144 #if NWSKBD > 0
    145 	if ((a.console = mkbd_is_console) != 0) {
    146 		mkbd_is_console = 0;
    147 		if (!mkbd_console_initted)
    148 			wskbd_cnattach(&mkbd_consops, NULL, &mkbd_keymapdata);
    149 		mkbd_console_softc = sc;
    150 	}
    151 	a.keymap = &mkbd_keymapdata;
    152 	a.accessops = &mkbd_accessops;
    153 	a.accesscookie = sc;
    154 	sc->sc_wskbddev = config_found(self, &a, wskbddevprint);
    155 #endif
    156 
    157 	maple_set_callback(parent, sc->sc_unit, MAPLE_FN_KEYBOARD,
    158 	    mkbd_intr, sc);
    159 	maple_enable_periodic(parent, sc->sc_unit, MAPLE_FN_KEYBOARD, 1);
    160 }
    161 
    162 static int
    163 mkbddetach(struct device *self, int flags)
    164 {
    165 	struct mkbd_softc *sc = (struct mkbd_softc *) self;
    166 	int rv = 0;
    167 
    168 	if (sc == mkbd_console_softc) {
    169 		/*
    170 		 * Hack to allow another Maple keyboard to be new console.
    171 		 * XXX Should some other type device can be console.
    172 		 */
    173 		printf("%s: was console keyboard\n", sc->sc_dev.dv_xname);
    174 		wskbd_cndetach();
    175 		mkbd_console_softc = NULL;
    176 		mkbd_console_initted = 0;
    177 		mkbd_is_console = 1;
    178 	}
    179 	if (sc->sc_wskbddev)
    180 		rv = config_detach(sc->sc_wskbddev, flags);
    181 
    182 	return rv;
    183 }
    184 
    185 int
    186 mkbd_enable(void *v, int on)
    187 {
    188 
    189 	return 0;
    190 }
    191 
    192 void
    193 mkbd_set_leds(void *v, int on)
    194 {
    195 }
    196 
    197 int
    198 mkbd_ioctl(void *v, u_long cmd, caddr_t data, int flag, struct proc *p)
    199 {
    200 
    201 	switch (cmd) {
    202 	case WSKBDIO_GTYPE:
    203 		*(int *) data = WSKBD_TYPE_MAPLE;
    204 		return 0;
    205 	case WSKBDIO_SETLEDS:
    206 		return 0;
    207 	case WSKBDIO_GETLEDS:
    208 		*(int *) data = 0;
    209 		return 0;
    210 	case WSKBDIO_BELL:
    211 	case WSKBDIO_COMPLEXBELL:
    212 		return 0;
    213 	}
    214 
    215 	return EPASSTHROUGH;
    216 }
    217 
    218 int
    219 mkbd_cnattach(void)
    220 {
    221 
    222 	wskbd_cnattach(&mkbd_consops, NULL, &mkbd_keymapdata);
    223 	mkbd_console_initted = 1;
    224 	mkbd_is_console = 1;
    225 
    226 	return 0;
    227 }
    228 
    229 static int polledkey;
    230 extern int maple_polling;
    231 
    232 #define SHIFT_KEYCODE_BASE 0xe0
    233 #define UP_KEYCODE_FLAG 0x1000
    234 
    235 #define KEY_UP(n) do {							\
    236 	if (maple_polling)						\
    237 		polledkey = (n)|UP_KEYCODE_FLAG;			\
    238 	else								\
    239 		wskbd_input(sc->sc_wskbddev, WSCONS_EVENT_KEY_UP, (n));	\
    240 	} while (/*CONSTCOND*/0)
    241 
    242 #define KEY_DOWN(n) do {						\
    243 	if (maple_polling)						\
    244 		polledkey = (n);					\
    245 	else								\
    246 		wskbd_input(sc->sc_wskbddev, WSCONS_EVENT_KEY_DOWN, (n)); \
    247 	} while (/*CONSTCOND*/0)
    248 
    249 #define SHIFT_UP(n)	KEY_UP((n) | SHIFT_KEYCODE_BASE)
    250 #define SHIFT_DOWN(n)	KEY_DOWN((n) | SHIFT_KEYCODE_BASE)
    251 
    252 static void
    253 mkbd_intr(void *arg, struct maple_response *response, int sz, int flags)
    254 {
    255 	struct mkbd_softc *sc = arg;
    256 	struct mkbd_condition *kbddata = (void *) response->data;
    257 
    258 	if ((flags & MAPLE_FLAG_PERIODIC) &&
    259 	    sz >= sizeof(struct mkbd_condition)) {
    260 		int i, j, v;
    261 
    262 		v = sc->sc_condition.shift & ~kbddata->shift;
    263 		if (v)
    264 			for (i = 0; i < 8; i++)
    265 				if (v & (1 << i))
    266 					SHIFT_UP(i);
    267 
    268 		v = kbddata->shift & ~sc->sc_condition.shift;
    269 		if (v)
    270 			for (i = 0; i < 8; i++)
    271 				if (v & (1 << i))
    272 					SHIFT_DOWN(i);
    273 
    274 		for (i = 0, j = 0; i < 6; i++)
    275 			if (sc->sc_condition.key[i] < 4)
    276 				break;
    277 			else if (sc->sc_condition.key[i] == kbddata->key[j])
    278 				j++;
    279 			else
    280 				KEY_UP(sc->sc_condition.key[i]);
    281 
    282 		for (; j < 6; j++)
    283 			if (kbddata->key[j] < 4)
    284 				break;
    285 			else
    286 				KEY_DOWN(kbddata->key[j]);
    287 
    288 		memcpy(&sc->sc_condition, kbddata,
    289 		    sizeof(struct mkbd_condition));
    290 	}
    291 }
    292 
    293 void
    294 mkbd_cngetc(void *v, u_int *type, int *data)
    295 {
    296 	int key;
    297 
    298 	polledkey = -1;
    299 	maple_polling = 1;
    300 	while (polledkey == -1) {
    301 		if (mkbd_console_softc != NULL ||
    302 		    mkbd_console_softc->sc_parent != NULL) {
    303 			int t;
    304 			for (t = 0; t < 1000000; t++);
    305 			maple_run_polling(mkbd_console_softc->sc_parent);
    306 		}
    307 	}
    308 	maple_polling = 0;
    309 	key = polledkey;
    310 
    311 	*data = key & ~UP_KEYCODE_FLAG;
    312 	*type = (key & UP_KEYCODE_FLAG) ?
    313 	    WSCONS_EVENT_KEY_UP : WSCONS_EVENT_KEY_DOWN;
    314 }
    315 
    316 void
    317 mkbd_cnpollc(void *v, int on)
    318 {
    319 }
    320