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