Home | History | Annotate | Line # | Download | only in hpc
hpckbd.c revision 1.30.30.3
      1 /*	$NetBSD: hpckbd.c,v 1.30.30.3 2018/09/23 17:20:08 martin 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.3 2018/09/23 17:20:08 martin 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 
    264 #if defined(hpcsh) || defined(hpcmips)
    265 /*
    266  * XXX: Use the old wrong code for now as hpcsh and hpcmips attach
    267  * console very early when malloc(9) is not yet available.  It is
    268  * convenient to be able to do early DDB on wscons.
    269  */
    270 void
    271 hpckbd_keymap_setup(struct hpckbd_core *hc,
    272 		    const keysym_t *map, int mapsize)
    273 {
    274 	int i;
    275 	struct wscons_keydesc *desc;
    276 
    277 	/* fix keydesc table */
    278 	/*
    279 	 * XXX The way this is done is really wrong.  The __UNCONST()
    280 	 * is a hint as to what is wrong.  This actually ends up modifying
    281 	 * initialized data which is marked "const".
    282 	 *
    283 	 * The reason we get away with it here is that on sh3 and mips
    284 	 * the kernel is directly mapped.
    285 	 */
    286 	desc = (struct wscons_keydesc *)__UNCONST(hpckbd_keymapdata.keydesc);
    287 	for (i = 0; desc[i].name != 0; i++) {
    288 		if ((desc[i].name & KB_MACHDEP) && desc[i].map == NULL) {
    289 			desc[i].map = map;
    290 			desc[i].map_size = mapsize;
    291 		}
    292 	}
    293 
    294 	return;
    295 }
    296 
    297 #else
    298 
    299 void
    300 hpckbd_keymap_setup(struct hpckbd_core *hc,
    301 		    const keysym_t *map, int mapsize)
    302 {
    303 	int i;
    304 	const struct wscons_keydesc *desc;
    305 	static struct wscons_keydesc *ndesc = NULL;
    306 
    307 	/*
    308 	 * fix keydesc table. Since it is const data, we must
    309 	 * copy it once before changingg it.
    310 	 */
    311 
    312 	if (ndesc == NULL) {
    313 		size_t sz;
    314 
    315 		for (sz = 0; hpckbd_keymapdata.keydesc[sz].name != 0; sz++);
    316 
    317 		ndesc = malloc(sz * sizeof(*ndesc), M_DEVBUF, M_WAITOK);
    318 		memcpy(ndesc, hpckbd_keymapdata.keydesc, sz * sizeof(*ndesc));
    319 
    320 		hpckbd_keymapdata.keydesc = ndesc;
    321 	}
    322 
    323 	desc = hpckbd_keymapdata.keydesc;
    324 	for (i = 0; desc[i].name != 0; i++) {
    325 		if ((desc[i].name & KB_MACHDEP) && desc[i].map == NULL) {
    326 			ndesc[i].map = map;
    327 			ndesc[i].map_size = mapsize;
    328 		}
    329 	}
    330 
    331 	return;
    332 }
    333 #endif
    334 
    335 void
    336 hpckbd_keymap_lookup(struct hpckbd_core *hc)
    337 {
    338 	const struct hpckbd_keymap_table *tab;
    339 	platid_mask_t mask;
    340 
    341 	for (tab = hpckbd_keymap_table; tab->ht_platform != NULL; tab++) {
    342 
    343 		mask = PLATID_DEREF(tab->ht_platform);
    344 
    345 		if (platid_match(&platid, &mask)) {
    346 			hc->hc_keymap = tab->ht_keymap;
    347 			hc->hc_special = tab->ht_special;
    348 #if !defined(PCKBD_LAYOUT)
    349 			hpckbd_keymapdata.layout = tab->ht_layout;
    350 #endif
    351 			if (tab->ht_cmdmap.map) {
    352 				hpckbd_keymap_setup(hc, tab->ht_cmdmap.map,
    353 				    tab->ht_cmdmap.size);
    354 #if !defined(PCKBD_LAYOUT)
    355 				hpckbd_keymapdata.layout |= KB_MACHDEP;
    356 #endif
    357 			} else {
    358 				hpckbd_keymapdata.layout &= ~KB_MACHDEP;
    359 			}
    360 			return;
    361 		}
    362 	}
    363 
    364 	/* no keymap. use default. */
    365 	hc->hc_keymap = default_keymap;
    366 	hc->hc_special = default_special_keymap;
    367 #if !defined(PCKBD_LAYOUT)
    368 	hpckbd_keymapdata.layout = KB_US;
    369 #endif
    370 }
    371 
    372 void
    373 __hpckbd_input_hook(void *arg)
    374 {
    375 #if 0
    376 	struct hpckbd_core *hc = arg;
    377 
    378 	if (hc->hc_polling) {
    379 		hc->hc_type = WSCONS_EVENT_ALL_KEYS_UP;
    380 	}
    381 #endif
    382 }
    383 
    384 int
    385 __hpckbd_input(void *arg, int flag, int scancode)
    386 {
    387 	struct hpckbd_core *hc = arg;
    388 	int type, key;
    389 
    390 	if (flag) {
    391 		type = WSCONS_EVENT_KEY_DOWN;
    392 	} else {
    393 		type = WSCONS_EVENT_KEY_UP;
    394 	}
    395 
    396 	key = hc->hc_keymap[scancode];
    397 	if (key == UNK) {
    398 #ifdef DEBUG
    399 		printf("hpckbd: unknown scan code %#x (%d, %d)\n",
    400 		    scancode, scancode >> 3,
    401 		    scancode - ((scancode >> 3) << 3));
    402 #endif /* DEBUG */
    403 		return (0);
    404 	}
    405 
    406 	if (key == IGN) {
    407 		return (0);
    408 	}
    409 
    410 	if (key == SPL) {
    411 		if (!flag)
    412 			return (0);
    413 
    414 		if (scancode == hc->hc_special[KEY_SPECIAL_OFF]) {
    415 			config_hook_call(CONFIG_HOOK_BUTTONEVENT,
    416 			    CONFIG_HOOK_BUTTONEVENT_POWER, (void *)1 /* on */);
    417 		} else if (scancode == hc->hc_special[KEY_SPECIAL_LIGHT]) {
    418 			static int onoff; /* XXX -uch */
    419 			config_hook_call(CONFIG_HOOK_BUTTONEVENT,
    420 			    CONFIG_HOOK_BUTTONEVENT_LIGHT,
    421 			    (void *)(onoff ^= 1));
    422 		} else {
    423 #ifdef DEBUG
    424 			printf("unknown special key %d\n", scancode);
    425 #endif
    426 		}
    427 
    428 		return (0);
    429 	}
    430 
    431 	if (hc->hc_polling) {
    432 		if (hpckbd_putevent(hc, type, key) == 0)
    433 			printf("hpckbd: queue over flow\n");
    434 	} else {
    435 #ifdef WSDISPLAY_COMPAT_RAWKBD
    436 		if (hc->hc_rawkbd) {
    437 			int n;
    438 			u_char data[16];
    439 			n = pckbd_encode(type, key, data);
    440 			wskbd_rawinput(hc->hc_wskbddev, data, n);
    441 		} else
    442 #endif
    443 			wskbd_input(hc->hc_wskbddev, type, key);
    444 	}
    445 
    446 	return (0);
    447 }
    448 
    449 /*
    450  * console support routines
    451  */
    452 int
    453 hpckbd_cnattach(struct hpckbd_ic_if *ic)
    454 {
    455 	struct hpckbd_core *hc = &hpckbd_consdata;
    456 
    457 	hpckbd_initcore(hc, ic, 1 /* console */);
    458 
    459 	/* attach controller */
    460 	hpckbd_initif(hc);
    461 
    462 	/* attach wskbd */
    463 	wskbd_cnattach(&hpckbd_consops, hc, &hpckbd_keymapdata);
    464 
    465 	return (0);
    466 }
    467 
    468 void
    469 hpckbd_cngetc(void *arg, u_int *type, int *data)
    470 {
    471 	struct hpckbd_core *hc = arg;
    472 
    473 	if (!hc->hc_console || !hc->hc_polling || !hc->hc_ic)
    474 		return;
    475 
    476 	while (hpckbd_getevent(hc, type, data) == 0) /* busy loop */
    477 		hpckbd_ic_poll(hc->hc_ic);
    478 }
    479 
    480 void
    481 hpckbd_cnpollc(void *arg, int on)
    482 {
    483 	struct hpckbd_core *hc = arg;
    484 
    485 	hc->hc_polling = on;
    486 }
    487 
    488 int
    489 hpckbd_enable(void *arg, int on)
    490 {
    491 	struct hpckbd_core *hc = arg;
    492 
    493 	if (on) {
    494 		if (hc->hc_enabled)
    495 			return (EBUSY);
    496 		hc->hc_enabled = 1;
    497 	} else {
    498 		if (hc->hc_console)
    499 			return (EBUSY);
    500 		hc->hc_enabled = 0;
    501 	}
    502 
    503 	return (0);
    504 }
    505 
    506 void
    507 hpckbd_set_leds(void *arg, int leds)
    508 {
    509 	/* Can you find any LED which tells you about keyboard? */
    510 }
    511 
    512 int
    513 hpckbd_ioctl(void *arg, u_long cmd, void *data, int flag,
    514 	     struct lwp *l)
    515 {
    516 #ifdef WSDISPLAY_COMPAT_RAWKBD
    517 	struct hpckbd_core *hc = arg;
    518 #endif
    519 	switch (cmd) {
    520 	case WSKBDIO_GTYPE:
    521 		*(int *)data = WSKBD_TYPE_HPC_KBD;
    522 		return (0);
    523 	case WSKBDIO_SETLEDS:
    524 		return 0;
    525 	case WSKBDIO_GETLEDS:
    526 		*(int *)data = 0;	/* dummy for wsconsctl(8) */
    527 		return (0);
    528 #ifdef WSDISPLAY_COMPAT_RAWKBD
    529 	case WSKBDIO_SETMODE:
    530 		hc->hc_rawkbd = (*(int *)data == WSKBD_RAW);
    531 		return (0);
    532 #endif
    533 	}
    534 	return (EPASSTHROUGH);
    535 }
    536