Home | History | Annotate | Line # | Download | only in hpc
btnmgr.c revision 1.2.4.1
      1 /*	$NetBSD: btnmgr.c,v 1.2.4.1 2001/10/10 11:56:52 fvdl Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1999
      5  *         Shin Takemura and PocketBSD Project. All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the PocketBSD project
     18  *	and its contributors.
     19  * 4. Neither the name of the project nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  *
     35  */
     36 #define BTNMGRDEBUG
     37 
     38 #include <sys/param.h>
     39 #include <sys/systm.h>
     40 #include <sys/ioctl.h>
     41 #include <sys/conf.h>
     42 #include <sys/device.h>
     43 #include <sys/malloc.h>
     44 #include <sys/kernel.h>
     45 #ifdef notyet
     46 #include <sys/vnode.h>
     47 #endif
     48 
     49 #include "opt_wsdisplay_compat.h"
     50 #include <dev/wscons/wsconsio.h>
     51 #include <dev/wscons/wskbdvar.h>
     52 #include <dev/wscons/wsksymdef.h>
     53 #ifdef WSDISPLAY_COMPAT_RAWKBD
     54 #include <dev/hpc/pckbd_encode.h>
     55 #endif
     56 
     57 #include <machine/bus.h>
     58 #include <machine/autoconf.h>
     59 #include <machine/config_hook.h>
     60 
     61 #ifdef BTNMGRDEBUG
     62 #ifndef BTNMGRDEBUG_CONF
     63 #define BTNMGRDEBUG_CONF 0
     64 #endif
     65 int	btnmgr_debug = BTNMGRDEBUG_CONF;
     66 #define	DPRINTF(arg) if (btnmgr_debug) printf arg;
     67 #define	DPRINTFN(n, arg) if (btnmgr_debug > (n)) printf arg;
     68 #else
     69 #define	DPRINTF(arg)
     70 #define DPRINTFN(n, arg)
     71 #endif
     72 
     73 cdev_decl(btnmgr);
     74 
     75 struct btnmgr_softc {
     76 	struct device sc_dev;
     77 	config_hook_tag	sc_hook_tag;
     78 	int sc_enabled;
     79 	struct device *sc_wskbddev;
     80 #ifdef WSDISPLAY_COMPAT_RAWKBD
     81 	int sc_rawkbd;
     82 #endif
     83 };
     84 
     85 int btnmgrmatch(struct device *, struct cfdata *, void *);
     86 void btnmgrattach(struct device *, struct device *, void *);
     87 char *btnmgr_name(long);
     88 static int btnmgr_hook(void *, int, long, void *);
     89 
     90 /*
     91  * global/static data
     92  */
     93 struct cfattach btnmgr_ca = {
     94 	sizeof(struct btnmgr_softc), btnmgrmatch, btnmgrattach
     95 };
     96 
     97 /* wskbd accessopts */
     98 int	btnmgr_wskbd_enable(void *, int);
     99 void	btnmgr_wskbd_set_leds(void *, int);
    100 int	btnmgr_wskbd_ioctl(void *, u_long, caddr_t, int, struct proc *);
    101 
    102 const struct wskbd_accessops btnmgr_wskbd_accessops = {
    103 	btnmgr_wskbd_enable,
    104 	btnmgr_wskbd_set_leds,
    105 	btnmgr_wskbd_ioctl,
    106 };
    107 
    108 /* button config: index by buttun event id */
    109 static const struct {
    110 	int  kevent;
    111 	int  keycode;
    112 	char *name;
    113 } button_config[] = {
    114 	/* id					kevent keycode name	*/
    115 	[CONFIG_HOOK_BUTTONEVENT_POWER] =	{ 0,   0, "Power"	},
    116 	[CONFIG_HOOK_BUTTONEVENT_OK] =		{ 1,  28, "OK"		},
    117 	[CONFIG_HOOK_BUTTONEVENT_CANCEL] =	{ 1,   1, "Cancel"	},
    118 	[CONFIG_HOOK_BUTTONEVENT_UP] =		{ 1,  72, "Up"		},
    119 	[CONFIG_HOOK_BUTTONEVENT_DOWN] =	{ 1,  80, "Down"	},
    120 	[CONFIG_HOOK_BUTTONEVENT_REC] =		{ 0,   0, "Rec"		},
    121 	[CONFIG_HOOK_BUTTONEVENT_COVER] =	{ 0,   0, "Cover"	},
    122 	[CONFIG_HOOK_BUTTONEVENT_LIGHT] =	{ 1,  57, "Light"	},
    123 	[CONFIG_HOOK_BUTTONEVENT_CONTRAST] =	{ 0,   0, "Contrast"	},
    124 	[CONFIG_HOOK_BUTTONEVENT_APP0] =	{ 1,  67, "Application 0" },
    125 	[CONFIG_HOOK_BUTTONEVENT_APP1] =	{ 1,  68, "Application 1" },
    126 	[CONFIG_HOOK_BUTTONEVENT_APP2] =	{ 1,  87, "Application 2" },
    127 	[CONFIG_HOOK_BUTTONEVENT_APP3] =	{ 1,  88, "Application 3" },
    128 };
    129 static const int n_button_config =
    130 	sizeof(button_config) / sizeof(*button_config);
    131 
    132 #define KC(n) KS_KEYCODE(n)
    133 static const keysym_t btnmgr_keydesc_default[] = {
    134 /*  pos				normal			shifted		*/
    135 	KC(1), 			KS_Escape,
    136 	KC(28), 			KS_Return,
    137 	KC(57),	KS_Cmd,		KS_Cmd_BacklightToggle,
    138 	KC(67), 			KS_f9,
    139 	KC(68), 			KS_f10,
    140 	KC(72), 			KS_KP_Up,
    141 	KC(80), 			KS_KP_Down,
    142 	KC(87), 			KS_f11,
    143 	KC(88), 			KS_f12,
    144 };
    145 #undef KC
    146 #define KBD_MAP(name, base, map) \
    147 			{ name, base, sizeof(map)/sizeof(keysym_t), map }
    148 const struct wscons_keydesc btnmgr_keydesctab[] = {
    149 	KBD_MAP(KB_US,		0,	btnmgr_keydesc_default),
    150 	{0, 0, 0, 0}
    151 };
    152 #undef KBD_MAP
    153 
    154 struct wskbd_mapdata btnmgr_keymapdata = {
    155 	btnmgr_keydesctab,
    156 	KB_US, /* XXX, This is bad idea... */
    157 };
    158 
    159 /*
    160  *  function bodies
    161  */
    162 int
    163 btnmgrmatch(struct device *parent, struct cfdata *match, void *aux)
    164 {
    165 	struct mainbus_attach_args *ma = aux;
    166 
    167 	if (strcmp(ma->ma_name, match->cf_driver->cd_name))
    168 		return 0;
    169 
    170 	return (1);
    171 }
    172 
    173 void
    174 btnmgrattach(struct device *parent, struct device *self, void *aux)
    175 {
    176 	int id;
    177 	struct btnmgr_softc *sc = (struct btnmgr_softc *)self;
    178 	struct wskbddev_attach_args wa;
    179 
    180 	printf("\n");
    181 
    182 	/*
    183 	 * install button event listener
    184 	 */
    185 	for (id = 0; id <= CONFIG_HOOK_MAX_ID; id++)
    186 		if (button_config[id].name != NULL)
    187 			sc->sc_hook_tag = config_hook(CONFIG_HOOK_BUTTONEVENT,
    188 			    id, CONFIG_HOOK_SHARE,
    189 			    btnmgr_hook, sc);
    190 
    191 	/*
    192 	 * attach wskbd
    193 	 */
    194 	wa.console = 0;
    195 	wa.keymap = &btnmgr_keymapdata;
    196 	wa.accessops = &btnmgr_wskbd_accessops;
    197 	wa.accesscookie = sc;
    198 
    199 	sc->sc_wskbddev = config_found(self, &wa, wskbddevprint);
    200 }
    201 
    202 static int
    203 btnmgr_hook(void *ctx, int type, long id, void *msg)
    204 {
    205 	struct btnmgr_softc *sc = ctx;
    206 
    207 	DPRINTF(("%s button: %s\n", btnmgr_name(id), msg ? "ON" : "OFF"));
    208 
    209 	if (button_config[id].kevent) {
    210 		u_int evtype;
    211 		evtype = msg ? WSCONS_EVENT_KEY_DOWN : WSCONS_EVENT_KEY_UP;
    212 #ifdef WSDISPLAY_COMPAT_RAWKBD
    213 		if (sc->sc_rawkbd) {
    214 			int n;
    215 			u_char data[16];
    216 			n = pckbd_encode(evtype, button_config[id].keycode,
    217 			    data);
    218 			wskbd_rawinput(sc->sc_wskbddev, data, n);
    219 		} else
    220 #endif
    221 			wskbd_input(sc->sc_wskbddev, evtype,
    222 			    button_config[id].keycode);
    223 	}
    224 
    225 	if (id == CONFIG_HOOK_BUTTONEVENT_POWER && msg)
    226 		config_hook_call(CONFIG_HOOK_PMEVENT,
    227 		    CONFIG_HOOK_PMEVENT_SUSPENDREQ, NULL);
    228 
    229 
    230 	return (0);
    231 }
    232 
    233 char*
    234 btnmgr_name(long id)
    235 {
    236 	if (id < n_button_config)
    237 		return (button_config[id].name);
    238 	return ("unknown");
    239 }
    240 
    241 int
    242 btnmgr_wskbd_enable(void *scx, int on)
    243 {
    244 	struct btnmgr_softc *sc = scx;
    245 
    246 	if (on) {
    247 		if (sc->sc_enabled)
    248 			return (EBUSY);
    249 		sc->sc_enabled = 1;
    250 	} else {
    251 		sc->sc_enabled = 0;
    252 	}
    253 
    254 	return (0);
    255 }
    256 
    257 void
    258 btnmgr_wskbd_set_leds(void *scx, int leds)
    259 {
    260 	/*
    261 	 * We have nothing to do.
    262 	 */
    263 }
    264 
    265 int
    266 btnmgr_wskbd_ioctl(void *scx, u_long cmd, caddr_t data, int flag,
    267     struct proc *p)
    268 {
    269 #ifdef WSDISPLAY_COMPAT_RAWKBD
    270 	struct btnmgr_softc *sc = scx;
    271 #endif
    272 	switch (cmd) {
    273 	case WSKBDIO_GTYPE:
    274 		*(int *)data = WSKBD_TYPE_HPC_BTN;
    275 		return (0);
    276 	case WSKBDIO_SETLEDS:
    277 		DPRINTF(("%s(%d): no LED\n", __FILE__, __LINE__));
    278 		return (0);
    279 	case WSKBDIO_GETLEDS:
    280 		DPRINTF(("%s(%d): no LED\n", __FILE__, __LINE__));
    281 		*(int *)data = 0;
    282 		return (0);
    283 #ifdef WSDISPLAY_COMPAT_RAWKBD
    284 	case WSKBDIO_SETMODE:
    285 		sc->sc_rawkbd = (*(int *)data == WSKBD_RAW);
    286 		DPRINTF(("%s(%d): rawkbd is %s\n", __FILE__, __LINE__,
    287 		    sc->sc_rawkbd ? "on" : "off"));
    288 		return (0);
    289 #endif
    290 	}
    291 	return (-1);
    292 }
    293 
    294 #ifdef notyet
    295 int
    296 btnmgropen(struct vnode *devvp, int flag, int mode, struct proc *p)
    297 {
    298 	return (EINVAL);
    299 }
    300 
    301 int
    302 btnmgrclose(struct vnode *devvp, int flag, int mode, struct proc *p)
    303 {
    304 	return (EINVAL);
    305 }
    306 
    307 int
    308 btnmgrread(struct vnode *devvp, struct uio *uio, int flag)
    309 {
    310 	return (EINVAL);
    311 }
    312 
    313 int
    314 btnmgrwrite(struct vnode *devvp, struct uio *uio, int flag)
    315 {
    316 	return (EINVAL);
    317 }
    318 
    319 int
    320 btnmgrioctl(struct vnode *devvp, u_long cmd, caddr_t data, int flag,
    321 	    struct proc *p)
    322 {
    323 	return (EINVAL);
    324 }
    325 #endif /* notyet */
    326