Home | History | Annotate | Line # | Download | only in hpc
hpckbd.c revision 1.30.30.1
      1 /*	$NetBSD: hpckbd.c,v 1.30.30.1 2017/06/30 06:25:43 snj 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  *
     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>
     33 __KERNEL_RCSID(0, "$NetBSD: hpckbd.c,v 1.30.30.1 2017/06/30 06:25:43 snj Exp $");
     34 
     35 #include <sys/param.h>
     36 #include <sys/systm.h>
     37 #include <sys/device.h>
     38 #include <sys/malloc.h>
     39 
     40 #include <sys/tty.h>
     41 
     42 #include <sys/bus.h>
     43 #include <sys/intr.h>
     44 
     45 #include <machine/config_hook.h>
     46 #include <machine/platid.h>
     47 #include <machine/platid_mask.h>
     48 
     49 #include "opt_wsdisplay_compat.h"
     50 #include "opt_pckbd_layout.h"
     51 #include <dev/wscons/wsksymdef.h>
     52 #include <dev/wscons/wsconsio.h>
     53 #include <dev/wscons/wskbdvar.h>
     54 #include <dev/wscons/wsksymvar.h>
     55 #include <dev/pckbport/wskbdmap_mfii.h>
     56 #ifdef WSDISPLAY_COMPAT_RAWKBD
     57 #include <dev/hpc/pckbd_encode.h>
     58 #endif
     59 
     60 #include <dev/hpc/hpckbdvar.h>
     61 #include <dev/hpc/hpckbdkeymap.h>
     62 
     63 struct hpckbd_softc;
     64 
     65 #define NEVENTQ 32
     66 struct hpckbd_eventq {
     67 	u_int	hq_type;
     68 	int	hq_data;
     69 };
     70 
     71 struct hpckbd_core {
     72 	struct hpckbd_if	hc_if;
     73 	struct hpckbd_ic_if	*hc_ic;
     74 	const uint8_t		*hc_keymap;
     75 	const int		*hc_special;
     76 	int			hc_polling;
     77 	int			hc_console;
     78 #define NEVENTQ 32
     79 	struct hpckbd_eventq	hc_eventq[NEVENTQ];
     80 	struct hpckbd_eventq	*hc_head, *hc_tail;
     81 	int			hc_nevents;
     82 	int			hc_enabled;
     83 	device_t		hc_wskbddev;
     84 	struct hpckbd_softc	*hc_sc;	/* back link */
     85 #ifdef WSDISPLAY_COMPAT_RAWKBD
     86 	int			hc_rawkbd;
     87 #endif
     88 };
     89 
     90 struct hpckbd_softc {
     91 	device_t		sc_dev;
     92 	struct hpckbd_core	*sc_core;
     93 	struct hpckbd_core	sc_coredata;
     94 };
     95 
     96 int	hpckbd_match(device_t, cfdata_t, void *);
     97 void	hpckbd_attach(device_t, device_t, void *);
     98 
     99 void	hpckbd_initcore(struct hpckbd_core *, struct hpckbd_ic_if *, int);
    100 void	hpckbd_initif(struct hpckbd_core *);
    101 int	hpckbd_getevent(struct hpckbd_core *, u_int *, int *);
    102 int	hpckbd_putevent(struct hpckbd_core *, u_int, int);
    103 void	hpckbd_keymap_lookup(struct hpckbd_core*);
    104 void	hpckbd_keymap_setup(struct hpckbd_core *, const keysym_t *, int);
    105 int	__hpckbd_input(void *, int, int);
    106 void	__hpckbd_input_hook(void *);
    107 
    108 CFATTACH_DECL_NEW(hpckbd, sizeof(struct hpckbd_softc),
    109     hpckbd_match, hpckbd_attach, NULL, NULL);
    110 
    111 /* wskbd accessopts */
    112 int	hpckbd_enable(void *, int);
    113 void	hpckbd_set_leds(void *, int);
    114 int	hpckbd_ioctl(void *, u_long, void *, int, struct lwp *);
    115 
    116 /* consopts */
    117 struct	hpckbd_core hpckbd_consdata;
    118 void	hpckbd_cngetc(void *, u_int *, int*);
    119 void	hpckbd_cnpollc(void *, int);
    120 
    121 const struct wskbd_accessops hpckbd_accessops = {
    122 	hpckbd_enable,
    123 	hpckbd_set_leds,
    124 	hpckbd_ioctl,
    125 };
    126 
    127 const struct wskbd_consops hpckbd_consops = {
    128 	hpckbd_cngetc,
    129 	hpckbd_cnpollc,
    130 	NULL,
    131 };
    132 
    133 struct wskbd_mapdata hpckbd_keymapdata = {
    134 	pckbd_keydesctab,
    135 #ifdef PCKBD_LAYOUT
    136 	PCKBD_LAYOUT
    137 #else
    138 	KB_US
    139 #endif
    140 };
    141 
    142 int
    143 hpckbd_match(device_t parent, cfdata_t cf, void *aux)
    144 {
    145 	return (1);
    146 }
    147 
    148 void
    149 hpckbd_attach(device_t parent, device_t self, void *aux)
    150 {
    151 	struct hpckbd_attach_args *haa = aux;
    152 	struct hpckbd_softc *sc = device_private(self);
    153 	struct hpckbd_ic_if *ic = haa->haa_ic;
    154 	struct wskbddev_attach_args wa;
    155 
    156 	sc->sc_dev = self;
    157 
    158 	/*
    159 	 * Initialize core if it isn't console
    160 	 */
    161 	if (hpckbd_consdata.hc_ic == ic) {
    162 		sc->sc_core = &hpckbd_consdata;
    163 		/* The core has been initialized in hpckbd_cnattach. */
    164 	} else {
    165 		sc->sc_core = &sc->sc_coredata;
    166 		hpckbd_initcore(sc->sc_core, ic, 0 /* not console */);
    167 	}
    168 
    169 	if (sc->sc_core->hc_keymap == default_keymap)
    170 		printf(": no keymap.");
    171 
    172 	printf("\n");
    173 
    174 	/*
    175 	 * setup hpckbd public interface for parent controller.
    176 	 */
    177 	hpckbd_initif(sc->sc_core);
    178 
    179 	/*
    180 	 * attach wskbd
    181 	 */
    182 	wa.console = sc->sc_core->hc_console;
    183 	wa.keymap = &hpckbd_keymapdata;
    184 	wa.accessops = &hpckbd_accessops;
    185 	wa.accesscookie = sc->sc_core;
    186 	sc->sc_core->hc_wskbddev = config_found(self, &wa, wskbddevprint);
    187 
    188 	if (!pmf_device_register(self, NULL, NULL))
    189 		aprint_error_dev(self, "unable to establish power handler\n");
    190 }
    191 
    192 int
    193 hpckbd_print(void *aux, const char *pnp)
    194 {
    195 	return (pnp ? QUIET : UNCONF);
    196 }
    197 
    198 void
    199 hpckbd_initcore(struct hpckbd_core *hc, struct hpckbd_ic_if *ic, int console)
    200 {
    201 	hc->hc_polling = 0;
    202 	hc->hc_console = console;
    203 	hc->hc_ic = ic;
    204 
    205 	/* setup event queue */
    206 	hc->hc_head = hc->hc_tail = hc->hc_eventq;
    207 	hc->hc_nevents = 0;
    208 
    209 	hpckbd_keymap_lookup(hc);
    210 }
    211 
    212 void
    213 hpckbd_initif(struct hpckbd_core *hc)
    214 {
    215 	struct hpckbd_if *kbdif = &hc->hc_if;
    216 
    217 	kbdif->hi_ctx = hc;
    218 	kbdif->hi_input = __hpckbd_input;
    219 	kbdif->hi_input_hook = __hpckbd_input_hook;
    220 	hpckbd_ic_establish(hc->hc_ic, &hc->hc_if);
    221 }
    222 
    223 int
    224 hpckbd_putevent(struct hpckbd_core* hc, u_int type, int data)
    225 {
    226 	int s = spltty();
    227 
    228 	if (hc->hc_nevents == NEVENTQ) {
    229 		splx(s);
    230 		return (0); /* queue is full */
    231 	}
    232 
    233 	hc->hc_nevents++;
    234 	hc->hc_tail->hq_type = type;
    235 	hc->hc_tail->hq_data = data;
    236 	if (&hc->hc_eventq[NEVENTQ] <= ++hc->hc_tail)
    237 		hc->hc_tail = hc->hc_eventq;
    238 	splx(s);
    239 
    240 	return (1);
    241 }
    242 
    243 int
    244 hpckbd_getevent(struct hpckbd_core* hc, u_int *type, int *data)
    245 {
    246 	int s = spltty();
    247 
    248 	if (hc->hc_nevents == 0) {
    249 		splx(s);
    250 		return (0); /* queue is empty */
    251 	}
    252 
    253 	*type = hc->hc_head->hq_type;
    254 	*data = hc->hc_head->hq_data;
    255 	hc->hc_nevents--;
    256 	if (&hc->hc_eventq[NEVENTQ] <= ++hc->hc_head)
    257 		hc->hc_head = hc->hc_eventq;
    258 	splx(s);
    259 
    260 	return (1);
    261 }
    262 
    263 void
    264 hpckbd_keymap_setup(struct hpckbd_core *hc,
    265 		    const keysym_t *map, int mapsize)
    266 {
    267 	int i;
    268 	const struct wscons_keydesc *desc;
    269 	static struct wscons_keydesc *ndesc = NULL;
    270 
    271 	/*
    272 	 * fix keydesc table. Since it is const data, we must
    273 	 * copy it once before changingg it.
    274 	 */
    275 
    276 	if (ndesc == NULL) {
    277 		size_t sz;
    278 
    279 		for (sz = 0; hpckbd_keymapdata.keydesc[sz].name != 0; sz++);
    280 
    281 		ndesc = malloc(sz * sizeof(*ndesc), M_DEVBUF, M_WAITOK);
    282 		memcpy(ndesc, hpckbd_keymapdata.keydesc, sz * sizeof(*ndesc));
    283 
    284 		hpckbd_keymapdata.keydesc = ndesc;
    285 	}
    286 
    287 	desc = hpckbd_keymapdata.keydesc;
    288 	for (i = 0; desc[i].name != 0; i++) {
    289 		if ((desc[i].name & KB_MACHDEP) && desc[i].map == NULL) {
    290 			ndesc[i].map = map;
    291 			ndesc[i].map_size = mapsize;
    292 		}
    293 	}
    294 
    295 	return;
    296 }
    297 
    298 void
    299 hpckbd_keymap_lookup(struct hpckbd_core *hc)
    300 {
    301 	const struct hpckbd_keymap_table *tab;
    302 	platid_mask_t mask;
    303 
    304 	for (tab = hpckbd_keymap_table; tab->ht_platform != NULL; tab++) {
    305 
    306 		mask = PLATID_DEREF(tab->ht_platform);
    307 
    308 		if (platid_match(&platid, &mask)) {
    309 			hc->hc_keymap = tab->ht_keymap;
    310 			hc->hc_special = tab->ht_special;
    311 #if !defined(PCKBD_LAYOUT)
    312 			hpckbd_keymapdata.layout = tab->ht_layout;
    313 #endif
    314 			if (tab->ht_cmdmap.map) {
    315 				hpckbd_keymap_setup(hc, tab->ht_cmdmap.map,
    316 				    tab->ht_cmdmap.size);
    317 #if !defined(PCKBD_LAYOUT)
    318 				hpckbd_keymapdata.layout |= KB_MACHDEP;
    319 #endif
    320 			} else {
    321 				hpckbd_keymapdata.layout &= ~KB_MACHDEP;
    322 			}
    323 			return;
    324 		}
    325 	}
    326 
    327 	/* no keymap. use default. */
    328 	hc->hc_keymap = default_keymap;
    329 	hc->hc_special = default_special_keymap;
    330 #if !defined(PCKBD_LAYOUT)
    331 	hpckbd_keymapdata.layout = KB_US;
    332 #endif
    333 }
    334 
    335 void
    336 __hpckbd_input_hook(void *arg)
    337 {
    338 #if 0
    339 	struct hpckbd_core *hc = arg;
    340 
    341 	if (hc->hc_polling) {
    342 		hc->hc_type = WSCONS_EVENT_ALL_KEYS_UP;
    343 	}
    344 #endif
    345 }
    346 
    347 int
    348 __hpckbd_input(void *arg, int flag, int scancode)
    349 {
    350 	struct hpckbd_core *hc = arg;
    351 	int type, key;
    352 
    353 	if (flag) {
    354 		type = WSCONS_EVENT_KEY_DOWN;
    355 	} else {
    356 		type = WSCONS_EVENT_KEY_UP;
    357 	}
    358 
    359 	key = hc->hc_keymap[scancode];
    360 	if (key == UNK) {
    361 #ifdef DEBUG
    362 		printf("hpckbd: unknown scan code %#x (%d, %d)\n",
    363 		    scancode, scancode >> 3,
    364 		    scancode - ((scancode >> 3) << 3));
    365 #endif /* DEBUG */
    366 		return (0);
    367 	}
    368 
    369 	if (key == IGN) {
    370 		return (0);
    371 	}
    372 
    373 	if (key == SPL) {
    374 		if (!flag)
    375 			return (0);
    376 
    377 		if (scancode == hc->hc_special[KEY_SPECIAL_OFF]) {
    378 			config_hook_call(CONFIG_HOOK_BUTTONEVENT,
    379 			    CONFIG_HOOK_BUTTONEVENT_POWER, (void *)1 /* on */);
    380 		} else if (scancode == hc->hc_special[KEY_SPECIAL_LIGHT]) {
    381 			static int onoff; /* XXX -uch */
    382 			config_hook_call(CONFIG_HOOK_BUTTONEVENT,
    383 			    CONFIG_HOOK_BUTTONEVENT_LIGHT,
    384 			    (void *)(onoff ^= 1));
    385 		} else {
    386 #ifdef DEBUG
    387 			printf("unknown special key %d\n", scancode);
    388 #endif
    389 		}
    390 
    391 		return (0);
    392 	}
    393 
    394 	if (hc->hc_polling) {
    395 		if (hpckbd_putevent(hc, type, key) == 0)
    396 			printf("hpckbd: queue over flow\n");
    397 	} else {
    398 #ifdef WSDISPLAY_COMPAT_RAWKBD
    399 		if (hc->hc_rawkbd) {
    400 			int n;
    401 			u_char data[16];
    402 			n = pckbd_encode(type, key, data);
    403 			wskbd_rawinput(hc->hc_wskbddev, data, n);
    404 		} else
    405 #endif
    406 			wskbd_input(hc->hc_wskbddev, type, key);
    407 	}
    408 
    409 	return (0);
    410 }
    411 
    412 /*
    413  * console support routines
    414  */
    415 int
    416 hpckbd_cnattach(struct hpckbd_ic_if *ic)
    417 {
    418 	struct hpckbd_core *hc = &hpckbd_consdata;
    419 
    420 	hpckbd_initcore(hc, ic, 1 /* console */);
    421 
    422 	/* attach controller */
    423 	hpckbd_initif(hc);
    424 
    425 	/* attach wskbd */
    426 	wskbd_cnattach(&hpckbd_consops, hc, &hpckbd_keymapdata);
    427 
    428 	return (0);
    429 }
    430 
    431 void
    432 hpckbd_cngetc(void *arg, u_int *type, int *data)
    433 {
    434 	struct hpckbd_core *hc = arg;
    435 
    436 	if (!hc->hc_console || !hc->hc_polling || !hc->hc_ic)
    437 		return;
    438 
    439 	while (hpckbd_getevent(hc, type, data) == 0) /* busy loop */
    440 		hpckbd_ic_poll(hc->hc_ic);
    441 }
    442 
    443 void
    444 hpckbd_cnpollc(void *arg, int on)
    445 {
    446 	struct hpckbd_core *hc = arg;
    447 
    448 	hc->hc_polling = on;
    449 }
    450 
    451 int
    452 hpckbd_enable(void *arg, int on)
    453 {
    454 	struct hpckbd_core *hc = arg;
    455 
    456 	if (on) {
    457 		if (hc->hc_enabled)
    458 			return (EBUSY);
    459 		hc->hc_enabled = 1;
    460 	} else {
    461 		if (hc->hc_console)
    462 			return (EBUSY);
    463 		hc->hc_enabled = 0;
    464 	}
    465 
    466 	return (0);
    467 }
    468 
    469 void
    470 hpckbd_set_leds(void *arg, int leds)
    471 {
    472 	/* Can you find any LED which tells you about keyboard? */
    473 }
    474 
    475 int
    476 hpckbd_ioctl(void *arg, u_long cmd, void *data, int flag,
    477 	     struct lwp *l)
    478 {
    479 #ifdef WSDISPLAY_COMPAT_RAWKBD
    480 	struct hpckbd_core *hc = arg;
    481 #endif
    482 	switch (cmd) {
    483 	case WSKBDIO_GTYPE:
    484 		*(int *)data = WSKBD_TYPE_HPC_KBD;
    485 		return (0);
    486 	case WSKBDIO_SETLEDS:
    487 		return 0;
    488 	case WSKBDIO_GETLEDS:
    489 		*(int *)data = 0;	/* dummy for wsconsctl(8) */
    490 		return (0);
    491 #ifdef WSDISPLAY_COMPAT_RAWKBD
    492 	case WSKBDIO_SETMODE:
    493 		hc->hc_rawkbd = (*(int *)data == WSKBD_RAW);
    494 		return (0);
    495 #endif
    496 	}
    497 	return (EPASSTHROUGH);
    498 }
    499