Home | History | Annotate | Line # | Download | only in hpc
hpckbd.c revision 1.6
      1 /*	$NetBSD: hpckbd.c,v 1.6 2001/11/13 12:47:56 lukem Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 1999-2001 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by UCHIYAMA Yasushi.
      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  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 #include <sys/cdefs.h>
     40 __KERNEL_RCSID(0, "$NetBSD: hpckbd.c,v 1.6 2001/11/13 12:47:56 lukem Exp $");
     41 
     42 #include <sys/param.h>
     43 #include <sys/systm.h>
     44 #include <sys/device.h>
     45 
     46 #include <sys/tty.h>
     47 
     48 #include <machine/bus.h>
     49 #include <machine/intr.h>
     50 
     51 #include <machine/config_hook.h>
     52 #include <machine/platid.h>
     53 #include <machine/platid_mask.h>
     54 
     55 #include "opt_wsdisplay_compat.h"
     56 #include "opt_pckbd_layout.h"
     57 #include <dev/wscons/wsksymdef.h>
     58 #include <dev/wscons/wsconsio.h>
     59 #include <dev/wscons/wskbdvar.h>
     60 #include <dev/wscons/wsksymdef.h>
     61 #include <dev/wscons/wsksymvar.h>
     62 #include <dev/pckbc/wskbdmap_mfii.h>
     63 #ifdef WSDISPLAY_COMPAT_RAWKBD
     64 #include <dev/hpc/pckbd_encode.h>
     65 #endif
     66 
     67 #include <dev/hpc/hpckbdvar.h>
     68 #include <dev/hpc/hpckbdkeymap.h>
     69 
     70 struct hpckbd_softc;
     71 
     72 #define NEVENTQ 32
     73 struct hpckbd_eventq {
     74 	u_int	hq_type;
     75 	int	hq_data;
     76 };
     77 
     78 struct hpckbd_core {
     79 	struct hpckbd_if	hc_if;
     80 	struct hpckbd_ic_if	*hc_ic;
     81 	const u_int8_t		*hc_keymap;
     82 	const int		*hc_special;
     83 	int			hc_polling;
     84 	int			hc_console;
     85 #define NEVENTQ 32
     86 	struct hpckbd_eventq	hc_eventq[NEVENTQ];
     87 	struct hpckbd_eventq	*hc_head, *hc_tail;
     88 	int			hc_nevents;
     89 	int			hc_enabled;
     90 	struct device		*hc_wskbddev;
     91 	struct hpckbd_softc*	hc_sc;	/* back link */
     92 #ifdef WSDISPLAY_COMPAT_RAWKBD
     93 	int			hc_rawkbd;
     94 #endif
     95 };
     96 
     97 struct hpckbd_softc {
     98 	struct device		sc_dev;
     99 	struct hpckbd_core	*sc_core;
    100 	struct hpckbd_core	sc_coredata;
    101 };
    102 
    103 int	hpckbd_match(struct device *, struct cfdata *, void *);
    104 void	hpckbd_attach(struct device *, struct device *, void *);
    105 
    106 void	hpckbd_initcore(struct hpckbd_core *, struct hpckbd_ic_if *, int);
    107 void	hpckbd_initif(struct hpckbd_core *);
    108 int	hpckbd_getevent(struct hpckbd_core *, u_int *, int *);
    109 int	hpckbd_putevent(struct hpckbd_core *, u_int, int);
    110 void	hpckbd_keymap_lookup(struct hpckbd_core*);
    111 void	hpckbd_keymap_setup(struct hpckbd_core *, const keysym_t *, int);
    112 int	__hpckbd_input(void *, int, int);
    113 void	__hpckbd_input_hook(void*);
    114 
    115 struct cfattach hpckbd_ca = {
    116 	sizeof(struct hpckbd_softc), hpckbd_match, hpckbd_attach
    117 };
    118 
    119 /* wskbd accessopts */
    120 int	hpckbd_enable(void *, int);
    121 void	hpckbd_set_leds(void *, int);
    122 int	hpckbd_ioctl(void *, u_long, caddr_t, int, struct proc *);
    123 
    124 /* consopts */
    125 struct	hpckbd_core hpckbd_consdata;
    126 void	hpckbd_cngetc(void *, u_int *, int*);
    127 void	hpckbd_cnpollc(void *, int);
    128 
    129 const struct wskbd_accessops hpckbd_accessops = {
    130 	hpckbd_enable,
    131 	hpckbd_set_leds,
    132 	hpckbd_ioctl,
    133 };
    134 
    135 const struct wskbd_consops hpckbd_consops = {
    136 	hpckbd_cngetc,
    137 	hpckbd_cnpollc,
    138 };
    139 
    140 struct wskbd_mapdata hpckbd_keymapdata = {
    141 	pckbd_keydesctab,
    142 #ifdef PCKBD_LAYOUT
    143 	PCKBD_LAYOUT
    144 #else
    145 	KB_US
    146 #endif
    147 };
    148 
    149 int
    150 hpckbd_match(struct device *parent, struct cfdata *cf, void *aux)
    151 {
    152 	return (1);
    153 }
    154 
    155 void
    156 hpckbd_attach(struct device *parent, struct device *self, void *aux)
    157 {
    158 	struct hpckbd_attach_args *haa = aux;
    159 	struct hpckbd_softc *sc = (void*)self;
    160 	struct hpckbd_ic_if *ic = haa->haa_ic;
    161 	struct wskbddev_attach_args wa;
    162 
    163 	/*
    164 	 * Initialize core if it isn't console
    165 	 */
    166 	if (hpckbd_consdata.hc_ic == ic) {
    167 		sc->sc_core = &hpckbd_consdata;
    168 		/* The core has been initialized in hpckbd_cnattach. */
    169 	} else {
    170 		sc->sc_core = &sc->sc_coredata;
    171 		hpckbd_initcore(sc->sc_core, ic, 0 /* not console */);
    172 	}
    173 
    174 	if (sc->sc_core->hc_keymap == default_keymap)
    175 		printf(": no keymap.");
    176 
    177 	printf("\n");
    178 
    179 	/*
    180 	 * setup hpckbd public interface for parent controller.
    181 	 */
    182 	hpckbd_initif(sc->sc_core);
    183 
    184 	/*
    185 	 * attach wskbd
    186 	 */
    187 	wa.console = sc->sc_core->hc_console;
    188 	wa.keymap = &hpckbd_keymapdata;
    189 	wa.accessops = &hpckbd_accessops;
    190 	wa.accesscookie = sc->sc_core;
    191 	sc->sc_core->hc_wskbddev = config_found(self, &wa, wskbddevprint);
    192 }
    193 
    194 int
    195 hpckbd_print(void *aux, const char *pnp)
    196 {
    197 	return (pnp ? QUIET : UNCONF);
    198 }
    199 
    200 void
    201 hpckbd_initcore(struct hpckbd_core *hc, struct hpckbd_ic_if *ic, int console)
    202 {
    203 	hc->hc_polling = 0;
    204 	hc->hc_console = console;
    205 	hc->hc_ic = ic;
    206 
    207 	/* setup event queue */
    208 	hc->hc_head = hc->hc_tail = hc->hc_eventq;
    209 	hc->hc_nevents = 0;
    210 
    211 	hpckbd_keymap_lookup(hc);
    212 }
    213 
    214 void
    215 hpckbd_initif(struct hpckbd_core *hc)
    216 {
    217 	struct hpckbd_if *kbdif = &hc->hc_if;
    218 
    219 	kbdif->hi_ctx = hc;
    220 	kbdif->hi_input = __hpckbd_input;
    221 	kbdif->hi_input_hook = __hpckbd_input_hook;
    222 	hpckbd_ic_establish(hc->hc_ic, &hc->hc_if);
    223 }
    224 
    225 int
    226 hpckbd_putevent(struct hpckbd_core* hc, u_int type, int data)
    227 {
    228 	int s = spltty();
    229 
    230 	if (hc->hc_nevents == NEVENTQ) {
    231 		splx(s);
    232 		return (0); /* queue is full */
    233 	}
    234 
    235 	hc->hc_nevents++;
    236 	hc->hc_tail->hq_type = type;
    237 	hc->hc_tail->hq_data = data;
    238 	if (&hc->hc_eventq[NEVENTQ] <= ++hc->hc_tail)
    239 		hc->hc_tail = hc->hc_eventq;
    240 	splx(s);
    241 
    242 	return (1);
    243 }
    244 
    245 int
    246 hpckbd_getevent(struct hpckbd_core* hc, u_int *type, int *data)
    247 {
    248 	int s = spltty();
    249 
    250 	if (hc->hc_nevents == 0) {
    251 		splx(s);
    252 		return (0); /* queue is empty */
    253 	}
    254 
    255 	*type = hc->hc_head->hq_type;
    256 	*data = hc->hc_head->hq_data;
    257 	hc->hc_nevents--;
    258 	if (&hc->hc_eventq[NEVENTQ] <= ++hc->hc_head)
    259 		hc->hc_head = hc->hc_eventq;
    260 	splx(s);
    261 
    262 	return (1);
    263 }
    264 
    265 void
    266 hpckbd_keymap_setup(struct hpckbd_core *hc, const keysym_t *map, int mapsize)
    267 {
    268 	int i;
    269 	struct wscons_keydesc *desc;
    270 
    271 	/* fix keydesc table */
    272 	desc = (struct wscons_keydesc *)hpckbd_keymapdata.keydesc;
    273 	for (i = 0; desc[i].name != 0; i++) {
    274 		if ((desc[i].name & KB_MACHDEP) && desc[i].map == NULL) {
    275 			desc[i].map = map;
    276 			desc[i].map_size = mapsize;
    277 		}
    278 	}
    279 
    280 	return;
    281 }
    282 
    283 void
    284 hpckbd_keymap_lookup(struct hpckbd_core *hc)
    285 {
    286 	const struct hpckbd_keymap_table *tab;
    287 	platid_mask_t mask;
    288 
    289 	for (tab = hpckbd_keymap_table; tab->ht_platform != NULL; tab++) {
    290 
    291 		mask = PLATID_DEREF(tab->ht_platform);
    292 
    293 		if (platid_match(&platid, &mask)) {
    294 			hc->hc_keymap = tab->ht_keymap;
    295 			hc->hc_special = tab->ht_special;
    296 #if !defined(PCKBD_LAYOUT)
    297 			hpckbd_keymapdata.layout = tab->ht_layout;
    298 #endif
    299 			if (tab->ht_cmdmap.map) {
    300 				hpckbd_keymap_setup(hc, tab->ht_cmdmap.map,
    301 				    tab->ht_cmdmap.size);
    302 #if !defined(PCKBD_LAYOUT)
    303 				hpckbd_keymapdata.layout |= KB_MACHDEP;
    304 #endif
    305 			} else {
    306 				hpckbd_keymapdata.layout &= ~KB_MACHDEP;
    307 			}
    308 			return;
    309 		}
    310 	}
    311 
    312 	/* no keymap. use default. */
    313 	hc->hc_keymap = default_keymap;
    314 	hc->hc_special = default_special_keymap;
    315 #if !defined(PCKBD_LAYOUT)
    316 	hpckbd_keymapdata.layout = KB_US;
    317 #endif
    318 }
    319 
    320 void
    321 __hpckbd_input_hook(void *arg)
    322 {
    323 #if 0
    324 	struct hpckbd_core *hc = arg;
    325 
    326 	if (hc->hc_polling) {
    327 		hc->hc_type = WSCONS_EVENT_ALL_KEYS_UP;
    328 	}
    329 #endif
    330 }
    331 
    332 int
    333 __hpckbd_input(void *arg, int flag, int scancode)
    334 {
    335 	struct hpckbd_core *hc = arg;
    336 	int type, key;
    337 
    338 	if (flag) {
    339 		type = WSCONS_EVENT_KEY_DOWN;
    340 	} else {
    341 		type = WSCONS_EVENT_KEY_UP;
    342 	}
    343 
    344 	if ((key = hc->hc_keymap[scancode]) == UNK) {
    345 #ifdef DEBUG
    346 		printf("hpckbd: unknown scan code %#x (%d, %d)\n",
    347 		    scancode, scancode >> 3,
    348 		    scancode - ((scancode >> 3) << 3));
    349 #endif /* DEBUG */
    350 		return (0);
    351 	}
    352 
    353 	if (key == IGN) {
    354 		return (0);
    355 	}
    356 
    357 	if (key == SPL) {
    358 		if (!flag)
    359 			return (0);
    360 
    361 		if (scancode == hc->hc_special[KEY_SPECIAL_OFF]) {
    362 #ifdef DEBUG
    363 			printf("off button\n"); // XXX notyet -uch
    364 #endif
    365 		} else if (scancode == hc->hc_special[KEY_SPECIAL_LIGHT]) {
    366 			static int onoff; /* XXX -uch */
    367 			config_hook_call(CONFIG_HOOK_BUTTONEVENT,
    368 			    CONFIG_HOOK_BUTTONEVENT_LIGHT,
    369 			    (void *)(onoff ^= 1));
    370 		} else {
    371 #ifdef DEBUG
    372 			printf("unknown special key %d\n", scancode);
    373 #endif
    374 		}
    375 
    376 		return (0);
    377 	}
    378 
    379 	if (hc->hc_polling) {
    380 		if (hpckbd_putevent(hc, type, hc->hc_keymap[scancode]) == 0)
    381 			printf("hpckbd: queue over flow");
    382 	} else {
    383 #ifdef WSDISPLAY_COMPAT_RAWKBD
    384 		if (hc->hc_rawkbd) {
    385 			int n;
    386 			u_char data[16];
    387 			n = pckbd_encode(type, hc->hc_keymap[scancode], data);
    388 			wskbd_rawinput(hc->hc_wskbddev, data, n);
    389 		} else
    390 #endif
    391 			wskbd_input(hc->hc_wskbddev, type, hc->hc_keymap[scancode]);
    392 	}
    393 
    394 	return (0);
    395 }
    396 
    397 /*
    398  * console support routines
    399  */
    400 int
    401 hpckbd_cnattach(struct hpckbd_ic_if *ic)
    402 {
    403 	struct hpckbd_core *hc = &hpckbd_consdata;
    404 
    405 	hpckbd_initcore(hc, ic, 1 /* console */);
    406 
    407 	/* attach controller */
    408 	hpckbd_initif(hc);
    409 
    410 	/* attach wskbd */
    411 	wskbd_cnattach(&hpckbd_consops, hc, &hpckbd_keymapdata);
    412 
    413 	return (0);
    414 }
    415 
    416 void
    417 hpckbd_cngetc(void *arg, u_int *type, int *data)
    418 {
    419 	struct hpckbd_core *hc = arg;
    420 
    421 	if (!hc->hc_console || !hc->hc_polling || !hc->hc_ic)
    422 		return;
    423 
    424 	while (hpckbd_getevent(hc, type, data) == 0) /* busy loop */
    425 		hpckbd_ic_poll(hc->hc_ic);
    426 }
    427 
    428 void
    429 hpckbd_cnpollc(void *arg, int on)
    430 {
    431 	struct hpckbd_core *hc = arg;
    432 
    433 	hc->hc_polling = on;
    434 }
    435 
    436 int
    437 hpckbd_enable(void *arg, int on)
    438 {
    439 	struct hpckbd_core *hc = arg;
    440 
    441 	if (on) {
    442 		if (hc->hc_enabled)
    443 			return (EBUSY);
    444 		hc->hc_enabled = 1;
    445 	} else {
    446 		if (hc->hc_console)
    447 			return (EBUSY);
    448 		hc->hc_enabled = 0;
    449 	}
    450 
    451 	return (0);
    452 }
    453 
    454 void
    455 hpckbd_set_leds(void *arg, int leds)
    456 {
    457 	/* Can you find any LED which tells you about keyboard? */
    458 }
    459 
    460 int
    461 hpckbd_ioctl(void *arg, u_long cmd, caddr_t data, int flag, struct proc *p)
    462 {
    463 #ifdef WSDISPLAY_COMPAT_RAWKBD
    464 	struct hpckbd_core *hc = arg;
    465 #endif
    466 	switch (cmd) {
    467 	case WSKBDIO_GTYPE:
    468 		*(int *)data = WSKBD_TYPE_HPC_KBD;
    469 		return (0);
    470 	case WSKBDIO_SETLEDS:
    471 		return 0;
    472 	case WSKBDIO_GETLEDS:
    473 		*(int *)data = 0;	/* dummy for wsconsctl(8) */
    474 		return (0);
    475 #ifdef WSDISPLAY_COMPAT_RAWKBD
    476 	case WSKBDIO_SETMODE:
    477 		hc->hc_rawkbd = (*(int *)data == WSKBD_RAW);
    478 		return (0);
    479 #endif
    480 	}
    481 	return (-1);
    482 }
    483