Home | History | Annotate | Line # | Download | only in hpc
hpckbd.c revision 1.15
      1 /*	$NetBSD: hpckbd.c,v 1.15 2006/02/12 23:19:14 peter 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.15 2006/02/12 23:19:14 peter 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/pckbport/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 uint8_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 CFATTACH_DECL(hpckbd, sizeof(struct hpckbd_softc),
    116     hpckbd_match, hpckbd_attach, NULL, NULL);
    117 
    118 /* wskbd accessopts */
    119 int	hpckbd_enable(void *, int);
    120 void	hpckbd_set_leds(void *, int);
    121 int	hpckbd_ioctl(void *, u_long, caddr_t, int, struct lwp *);
    122 
    123 /* consopts */
    124 struct	hpckbd_core hpckbd_consdata;
    125 void	hpckbd_cngetc(void *, u_int *, int*);
    126 void	hpckbd_cnpollc(void *, int);
    127 
    128 const struct wskbd_accessops hpckbd_accessops = {
    129 	hpckbd_enable,
    130 	hpckbd_set_leds,
    131 	hpckbd_ioctl,
    132 };
    133 
    134 const struct wskbd_consops hpckbd_consops = {
    135 	hpckbd_cngetc,
    136 	hpckbd_cnpollc,
    137 };
    138 
    139 struct wskbd_mapdata hpckbd_keymapdata = {
    140 	pckbd_keydesctab,
    141 #ifdef PCKBD_LAYOUT
    142 	PCKBD_LAYOUT
    143 #else
    144 	KB_US
    145 #endif
    146 };
    147 
    148 int
    149 hpckbd_match(struct device *parent, struct cfdata *cf, void *aux)
    150 {
    151 	return (1);
    152 }
    153 
    154 void
    155 hpckbd_attach(struct device *parent, struct device *self, void *aux)
    156 {
    157 	struct hpckbd_attach_args *haa = aux;
    158 	struct hpckbd_softc *sc = (void*)self;
    159 	struct hpckbd_ic_if *ic = haa->haa_ic;
    160 	struct wskbddev_attach_args wa;
    161 
    162 	/*
    163 	 * Initialize core if it isn't console
    164 	 */
    165 	if (hpckbd_consdata.hc_ic == ic) {
    166 		sc->sc_core = &hpckbd_consdata;
    167 		/* The core has been initialized in hpckbd_cnattach. */
    168 	} else {
    169 		sc->sc_core = &sc->sc_coredata;
    170 		hpckbd_initcore(sc->sc_core, ic, 0 /* not console */);
    171 	}
    172 
    173 	if (sc->sc_core->hc_keymap == default_keymap)
    174 		printf(": no keymap.");
    175 
    176 	printf("\n");
    177 
    178 	/*
    179 	 * setup hpckbd public interface for parent controller.
    180 	 */
    181 	hpckbd_initif(sc->sc_core);
    182 
    183 	/*
    184 	 * attach wskbd
    185 	 */
    186 	wa.console = sc->sc_core->hc_console;
    187 	wa.keymap = &hpckbd_keymapdata;
    188 	wa.accessops = &hpckbd_accessops;
    189 	wa.accesscookie = sc->sc_core;
    190 	sc->sc_core->hc_wskbddev = config_found(self, &wa, wskbddevprint);
    191 }
    192 
    193 int
    194 hpckbd_print(void *aux, const char *pnp)
    195 {
    196 	return (pnp ? QUIET : UNCONF);
    197 }
    198 
    199 void
    200 hpckbd_initcore(struct hpckbd_core *hc, struct hpckbd_ic_if *ic, int console)
    201 {
    202 	hc->hc_polling = 0;
    203 	hc->hc_console = console;
    204 	hc->hc_ic = ic;
    205 
    206 	/* setup event queue */
    207 	hc->hc_head = hc->hc_tail = hc->hc_eventq;
    208 	hc->hc_nevents = 0;
    209 
    210 	hpckbd_keymap_lookup(hc);
    211 }
    212 
    213 void
    214 hpckbd_initif(struct hpckbd_core *hc)
    215 {
    216 	struct hpckbd_if *kbdif = &hc->hc_if;
    217 
    218 	kbdif->hi_ctx = hc;
    219 	kbdif->hi_input = __hpckbd_input;
    220 	kbdif->hi_input_hook = __hpckbd_input_hook;
    221 	hpckbd_ic_establish(hc->hc_ic, &hc->hc_if);
    222 }
    223 
    224 int
    225 hpckbd_putevent(struct hpckbd_core* hc, u_int type, int data)
    226 {
    227 	int s = spltty();
    228 
    229 	if (hc->hc_nevents == NEVENTQ) {
    230 		splx(s);
    231 		return (0); /* queue is full */
    232 	}
    233 
    234 	hc->hc_nevents++;
    235 	hc->hc_tail->hq_type = type;
    236 	hc->hc_tail->hq_data = data;
    237 	if (&hc->hc_eventq[NEVENTQ] <= ++hc->hc_tail)
    238 		hc->hc_tail = hc->hc_eventq;
    239 	splx(s);
    240 
    241 	return (1);
    242 }
    243 
    244 int
    245 hpckbd_getevent(struct hpckbd_core* hc, u_int *type, int *data)
    246 {
    247 	int s = spltty();
    248 
    249 	if (hc->hc_nevents == 0) {
    250 		splx(s);
    251 		return (0); /* queue is empty */
    252 	}
    253 
    254 	*type = hc->hc_head->hq_type;
    255 	*data = hc->hc_head->hq_data;
    256 	hc->hc_nevents--;
    257 	if (&hc->hc_eventq[NEVENTQ] <= ++hc->hc_head)
    258 		hc->hc_head = hc->hc_eventq;
    259 	splx(s);
    260 
    261 	return (1);
    262 }
    263 
    264 void
    265 hpckbd_keymap_setup(struct hpckbd_core *hc, const keysym_t *map, int mapsize)
    266 {
    267 	int i;
    268 	struct wscons_keydesc *desc;
    269 
    270 	/* fix keydesc table */
    271 	/*
    272 	 * XXX The way this is done is really wrong.  The __UNCONST()
    273 	 * is a hint as to what is wrong.  This actually ends up modifying
    274 	 * initialized data which is marked "const".
    275 	 * The reason we get away with it here is apparently that text
    276 	 * and read-only data gets mapped read/write on the platforms
    277 	 * using this code.
    278 	 */
    279 	desc = (struct wscons_keydesc *)__UNCONST(hpckbd_keymapdata.keydesc);
    280 	for (i = 0; desc[i].name != 0; i++) {
    281 		if ((desc[i].name & KB_MACHDEP) && desc[i].map == NULL) {
    282 			desc[i].map = map;
    283 			desc[i].map_size = mapsize;
    284 		}
    285 	}
    286 
    287 	return;
    288 }
    289 
    290 void
    291 hpckbd_keymap_lookup(struct hpckbd_core *hc)
    292 {
    293 	const struct hpckbd_keymap_table *tab;
    294 	platid_mask_t mask;
    295 
    296 	for (tab = hpckbd_keymap_table; tab->ht_platform != NULL; tab++) {
    297 
    298 		mask = PLATID_DEREF(tab->ht_platform);
    299 
    300 		if (platid_match(&platid, &mask)) {
    301 			hc->hc_keymap = tab->ht_keymap;
    302 			hc->hc_special = tab->ht_special;
    303 #if !defined(PCKBD_LAYOUT)
    304 			hpckbd_keymapdata.layout = tab->ht_layout;
    305 #endif
    306 			if (tab->ht_cmdmap.map) {
    307 				hpckbd_keymap_setup(hc, tab->ht_cmdmap.map,
    308 				    tab->ht_cmdmap.size);
    309 #if !defined(PCKBD_LAYOUT)
    310 				hpckbd_keymapdata.layout |= KB_MACHDEP;
    311 #endif
    312 			} else {
    313 				hpckbd_keymapdata.layout &= ~KB_MACHDEP;
    314 			}
    315 			return;
    316 		}
    317 	}
    318 
    319 	/* no keymap. use default. */
    320 	hc->hc_keymap = default_keymap;
    321 	hc->hc_special = default_special_keymap;
    322 #if !defined(PCKBD_LAYOUT)
    323 	hpckbd_keymapdata.layout = KB_US;
    324 #endif
    325 }
    326 
    327 void
    328 __hpckbd_input_hook(void *arg)
    329 {
    330 #if 0
    331 	struct hpckbd_core *hc = arg;
    332 
    333 	if (hc->hc_polling) {
    334 		hc->hc_type = WSCONS_EVENT_ALL_KEYS_UP;
    335 	}
    336 #endif
    337 }
    338 
    339 int
    340 __hpckbd_input(void *arg, int flag, int scancode)
    341 {
    342 	struct hpckbd_core *hc = arg;
    343 	int type, key;
    344 
    345 	if (flag) {
    346 		type = WSCONS_EVENT_KEY_DOWN;
    347 	} else {
    348 		type = WSCONS_EVENT_KEY_UP;
    349 	}
    350 
    351 	if ((key = hc->hc_keymap[scancode]) == 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 #ifdef DEBUG
    370 			printf("off button\n"); // XXX notyet -uch
    371 #endif
    372 		} else if (scancode == hc->hc_special[KEY_SPECIAL_LIGHT]) {
    373 			static int onoff; /* XXX -uch */
    374 			config_hook_call(CONFIG_HOOK_BUTTONEVENT,
    375 			    CONFIG_HOOK_BUTTONEVENT_LIGHT,
    376 			    (void *)(onoff ^= 1));
    377 		} else {
    378 #ifdef DEBUG
    379 			printf("unknown special key %d\n", scancode);
    380 #endif
    381 		}
    382 
    383 		return (0);
    384 	}
    385 
    386 	if (hc->hc_polling) {
    387 		if (hpckbd_putevent(hc, type, key) == 0)
    388 			printf("hpckbd: queue over flow\n");
    389 	} else {
    390 #ifdef WSDISPLAY_COMPAT_RAWKBD
    391 		if (hc->hc_rawkbd) {
    392 			int n;
    393 			u_char data[16];
    394 			n = pckbd_encode(type, key, data);
    395 			wskbd_rawinput(hc->hc_wskbddev, data, n);
    396 		} else
    397 #endif
    398 			wskbd_input(hc->hc_wskbddev, type, key);
    399 	}
    400 
    401 	return (0);
    402 }
    403 
    404 /*
    405  * console support routines
    406  */
    407 int
    408 hpckbd_cnattach(struct hpckbd_ic_if *ic)
    409 {
    410 	struct hpckbd_core *hc = &hpckbd_consdata;
    411 
    412 	hpckbd_initcore(hc, ic, 1 /* console */);
    413 
    414 	/* attach controller */
    415 	hpckbd_initif(hc);
    416 
    417 	/* attach wskbd */
    418 	wskbd_cnattach(&hpckbd_consops, hc, &hpckbd_keymapdata);
    419 
    420 	return (0);
    421 }
    422 
    423 void
    424 hpckbd_cngetc(void *arg, u_int *type, int *data)
    425 {
    426 	struct hpckbd_core *hc = arg;
    427 
    428 	if (!hc->hc_console || !hc->hc_polling || !hc->hc_ic)
    429 		return;
    430 
    431 	while (hpckbd_getevent(hc, type, data) == 0) /* busy loop */
    432 		hpckbd_ic_poll(hc->hc_ic);
    433 }
    434 
    435 void
    436 hpckbd_cnpollc(void *arg, int on)
    437 {
    438 	struct hpckbd_core *hc = arg;
    439 
    440 	hc->hc_polling = on;
    441 }
    442 
    443 int
    444 hpckbd_enable(void *arg, int on)
    445 {
    446 	struct hpckbd_core *hc = arg;
    447 
    448 	if (on) {
    449 		if (hc->hc_enabled)
    450 			return (EBUSY);
    451 		hc->hc_enabled = 1;
    452 	} else {
    453 		if (hc->hc_console)
    454 			return (EBUSY);
    455 		hc->hc_enabled = 0;
    456 	}
    457 
    458 	return (0);
    459 }
    460 
    461 void
    462 hpckbd_set_leds(void *arg, int leds)
    463 {
    464 	/* Can you find any LED which tells you about keyboard? */
    465 }
    466 
    467 int
    468 hpckbd_ioctl(void *arg, u_long cmd, caddr_t data, int flag, 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