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