Home | History | Annotate | Line # | Download | only in maple
mkbd.c revision 1.5
      1 /*	$NetBSD: mkbd.c,v 1.5 2001/02/02 03:09:16 thorpej 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/param.h>
     36 #include <sys/device.h>
     37 #include <sys/fcntl.h>
     38 #include <sys/poll.h>
     39 #include <sys/select.h>
     40 #include <sys/proc.h>
     41 #include <sys/signalvar.h>
     42 #include <sys/systm.h>
     43 
     44 #include "wskbd.h"
     45 
     46 #include <dev/wscons/wsconsio.h>
     47 #include <dev/wscons/wskbdvar.h>
     48 #include <dev/wscons/wsksymdef.h>
     49 #include <dev/wscons/wsksymvar.h>
     50 
     51 #include <machine/cpu.h>
     52 #include <machine/bus.h>
     53 
     54 #include <dreamcast/dev/maple/maple.h>
     55 #include <dreamcast/dev/maple/mapleconf.h>
     56 #include <dreamcast/dev/maple/mkbdvar.h>
     57 #include <dreamcast/dev/maple/mkbdmap.h>
     58 
     59 
     60 /*
     61  * Function declarations.
     62  */
     63 static int	mkbdmatch __P((struct device *, struct cfdata *, void *));
     64 static void	mkbdattach __P((struct device *, struct device *, void *));
     65 
     66 int mkbd_enable __P((void *, int));
     67 void mkbd_set_leds __P((void *, int));
     68 int mkbd_ioctl __P((void *, u_long, caddr_t, int, struct proc *));
     69 
     70 struct wskbd_accessops mkbd_accessops = {
     71 	mkbd_enable,
     72 	mkbd_set_leds,
     73 	mkbd_ioctl,
     74 };
     75 
     76 static void	mkbd_intr __P((struct mkbd_softc *, struct mkbd_condition *, int));
     77 
     78 void mkbd_cngetc __P((void *, u_int *, int *));
     79 void mkbd_cnpollc __P((void *, int));
     80 int mkbd_cnattach __P((void));
     81 
     82 struct wskbd_consops mkbd_consops = {
     83 	mkbd_cngetc,
     84 	mkbd_cnpollc,
     85 };
     86 
     87 struct wskbd_mapdata mkbd_keymapdata = {
     88 	mkbd_keydesctab,
     89 	KB_JP,
     90 };
     91 
     92 static struct mkbd_softc *mkbd_console_softc = NULL;
     93 
     94 /* Driver definition. */
     95 struct cfattach mkbd_ca = {
     96 	sizeof(struct mkbd_softc), mkbdmatch, mkbdattach
     97 };
     98 
     99 static int
    100 mkbdmatch(parent, cf, aux)
    101 	struct device *parent;
    102 	struct cfdata *cf;
    103 	void   *aux;
    104 {
    105 	struct maple_attach_args *ma = aux;
    106 
    107 	return (ma->ma_devinfo->di_func & MAPLE_FUNC_KEYBOARD) != 0;
    108 }
    109 
    110 static void
    111 mkbdattach(parent, self, aux)
    112 	struct device *parent, *self;
    113 	void   *aux;
    114 {
    115 	struct mkbd_softc *sc = (struct mkbd_softc *)self;
    116 	struct maple_attach_args *ma = aux;
    117 #if NWSKBD > 0
    118 	struct wskbddev_attach_args a;
    119 	static int mkbd_console_initted = 0;
    120 #endif
    121 	u_int32_t kbdtype;
    122 
    123 	sc->sc_parent = parent;
    124 	sc->sc_port = ma->ma_port;
    125 	sc->sc_subunit = ma->ma_subunit;
    126 
    127 	kbdtype = maple_get_function_data(ma->ma_devinfo,
    128 	    MAPLE_FUNC_KEYBOARD) >> 24;
    129 	switch(kbdtype) {
    130  	case 1:
    131  		printf(": Japanese keyboard");
    132 		mkbd_keymapdata.layout = KB_JP;
    133  		break;
    134 	case 2:
    135 		printf(": US keyboard");
    136 		mkbd_keymapdata.layout = KB_US;
    137 		break;
    138 	case 3:
    139 		printf(": European keyboard");
    140 		mkbd_keymapdata.layout = KB_UK;
    141 		break;
    142 	default:
    143 		printf(": Unknown keyboard %d", kbdtype);
    144 	}
    145 	printf("\n");
    146 
    147 #if NWSKBD > 0
    148 	a.console = (++mkbd_console_initted == 1);
    149 	a.keymap = &mkbd_keymapdata;
    150 	a.accessops = &mkbd_accessops;
    151 	a.accesscookie = sc;
    152 	if (a.console)
    153 	  mkbd_console_softc = sc;
    154 	sc->sc_wskbddev = config_found(self, &a, wskbddevprint);
    155 #endif
    156 
    157 	maple_set_condition_callback(parent, sc->sc_port, sc->sc_subunit,
    158 				     MAPLE_FUNC_KEYBOARD,
    159 				     (void (*)(void *, void *, int))mkbd_intr,
    160 				     sc);
    161 }
    162 
    163 
    164 
    165 int
    166 mkbd_enable(v, on)
    167 	void *v;
    168 	int on;
    169 {
    170 	return 0;
    171 }
    172 
    173 void
    174 mkbd_set_leds(v, on)
    175 	void *v;
    176 	int on;
    177 {
    178 }
    179 
    180 int
    181 mkbd_ioctl(v, cmd, data, flag, p)
    182 	void *v;
    183 	u_long cmd;
    184 	caddr_t data;
    185 	int flag;
    186 	struct proc *p;
    187 {
    188 	switch (cmd) {
    189 
    190 	case WSKBDIO_GTYPE:
    191 		*(int *)data = 0;		/* XXX */
    192 		return 0;
    193 	case WSKBDIO_SETLEDS:
    194 		return 0;
    195 	case WSKBDIO_GETLEDS:
    196 		*(int *)data = 0;
    197 		return 0;
    198 	case WSKBDIO_BELL:
    199 	case WSKBDIO_COMPLEXBELL:
    200 		return 0;
    201 	}
    202 
    203 	return -1;
    204 }
    205 
    206 
    207 int
    208 mkbd_cnattach()
    209 {
    210 	wskbd_cnattach(&mkbd_consops, NULL, &mkbd_keymapdata);
    211 
    212 	return 0;
    213 }
    214 
    215 static int polledkey;
    216 extern int maple_polling;
    217 
    218 #define SHIFT_KEYCODE_BASE 0x100
    219 #define UP_KEYCODE_FLAG 0x1000
    220 
    221 #define KEY_UP(n) \
    222 	if (maple_polling) \
    223 	  polledkey = (n)|UP_KEYCODE_FLAG; \
    224 	else \
    225 	  wskbd_input(sc->sc_wskbddev, WSCONS_EVENT_KEY_UP, (n))
    226 
    227 #define KEY_DOWN(n) \
    228 	if (maple_polling) \
    229 	  polledkey = (n); \
    230 	else \
    231 	  wskbd_input(sc->sc_wskbddev, WSCONS_EVENT_KEY_DOWN, (n))
    232 
    233 #define SHIFT_UP(n) KEY_UP((n)|SHIFT_KEYCODE_BASE)
    234 #define SHIFT_DOWN(n) KEY_DOWN((n)|SHIFT_KEYCODE_BASE)
    235 
    236 static void
    237 mkbd_intr(sc, kbddata, sz)
    238 	struct mkbd_softc *sc;
    239 	struct mkbd_condition *kbddata;
    240 	int sz;
    241 {
    242 	if (sz >= sizeof(struct mkbd_condition)) {
    243 	  int i, j, v;
    244 
    245 	  v = sc->sc_condition.shift & ~kbddata->shift;
    246 	  if (v)
    247 	    for (i=0; i<8; i++)
    248 	      if (v & (1<<i))
    249 		SHIFT_UP(i);
    250 
    251 	  v = kbddata->shift & ~sc->sc_condition.shift;
    252 	  if (v)
    253 	    for (i=0; i<8; i++)
    254 	      if (v & (1<<i))
    255 		SHIFT_DOWN(i);
    256 
    257 	  for (i=0, j=0; i<6; i++)
    258 	    if (sc->sc_condition.key[i] < 4)
    259 	      break;
    260 	    else if(sc->sc_condition.key[i] == kbddata->key[j])
    261 	      j++;
    262 	    else
    263 	      KEY_UP(sc->sc_condition.key[i]);
    264 
    265 	  for (; j<6; j++)
    266 	    if (kbddata->key[j] < 4)
    267 	      break;
    268 	    else
    269 	      KEY_DOWN(kbddata->key[j]);
    270 
    271 	  bcopy(kbddata, &sc->sc_condition, sizeof(struct mkbd_condition));
    272 	}
    273 }
    274 
    275 void
    276 mkbd_cngetc(v, type, data)
    277 	void *v;
    278 	u_int *type;
    279 	int *data;
    280 {
    281 	int key;
    282 
    283 	polledkey = -1;
    284 	maple_polling = 1;
    285 	while (polledkey == -1)
    286 	  if (mkbd_console_softc != NULL ||
    287 	      mkbd_console_softc->sc_parent != NULL) {
    288 	    int t;
    289 	    for(t=0; t<1000000; t++);
    290 	    maple_run_polling(mkbd_console_softc->sc_parent);
    291 	  }
    292 	maple_polling = 0;
    293 	key = polledkey;
    294 
    295 	*data = key & ~UP_KEYCODE_FLAG;
    296 	*type = (key & UP_KEYCODE_FLAG)?
    297 	  WSCONS_EVENT_KEY_UP : WSCONS_EVENT_KEY_DOWN;
    298 }
    299 
    300 void
    301 mkbd_cnpollc(v, on)
    302 	void *v;
    303 	int on;
    304 {
    305 }
    306