Home | History | Annotate | Line # | Download | only in hpc
btnmgr.c revision 1.25
      1 /*	$NetBSD: btnmgr.c,v 1.25 2009/05/12 14:22:39 cegger 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 
     37 #include <sys/cdefs.h>
     38 __KERNEL_RCSID(0, "$NetBSD: btnmgr.c,v 1.25 2009/05/12 14:22:39 cegger Exp $");
     39 
     40 #ifdef _KERNEL_OPT
     41 #include "opt_btnmgr.h"
     42 #include "opt_wsdisplay_compat.h"
     43 #endif
     44 
     45 #include <sys/param.h>
     46 #include <sys/systm.h>
     47 #include <sys/ioctl.h>
     48 #include <sys/conf.h>
     49 #include <sys/device.h>
     50 #include <sys/malloc.h>
     51 #include <sys/kernel.h>
     52 
     53 #include <dev/wscons/wsconsio.h>
     54 #include <dev/wscons/wskbdvar.h>
     55 #include <dev/wscons/wsksymdef.h>
     56 #ifdef WSDISPLAY_COMPAT_RAWKBD
     57 #include <dev/hpc/pckbd_encode.h>
     58 #endif
     59 
     60 #include <sys/bus.h>
     61 #include <machine/autoconf.h>
     62 #include <machine/config_hook.h>
     63 
     64 #ifdef BTNMGRDEBUG
     65 #ifndef BTNMGRDEBUG_CONF
     66 #define BTNMGRDEBUG_CONF 0
     67 #endif
     68 int	btnmgr_debug = BTNMGRDEBUG_CONF;
     69 #define	DPRINTF(arg) if (btnmgr_debug) printf arg;
     70 #define	DPRINTFN(n, arg) if (btnmgr_debug > (n)) printf arg;
     71 #else
     72 #define	DPRINTF(arg)
     73 #define DPRINTFN(n, arg)
     74 #endif
     75 
     76 struct btnmgr_softc {
     77 	struct device sc_dev;
     78 	config_hook_tag	sc_hook_tag;
     79 	int sc_enabled;
     80 	device_t sc_wskbddev;
     81 #ifdef WSDISPLAY_COMPAT_RAWKBD
     82 	int sc_rawkbd;
     83 #endif
     84 };
     85 
     86 int btnmgrmatch(device_t, cfdata_t, void *);
     87 void btnmgrattach(device_t, device_t, void *);
     88 const char *btnmgr_name(long);
     89 static int btnmgr_hook(void *, int, long, void *);
     90 
     91 /*
     92  * global/static data
     93  */
     94 CFATTACH_DECL(btnmgr, sizeof(struct btnmgr_softc),
     95     btnmgrmatch, btnmgrattach, NULL, NULL);
     96 
     97 #ifdef notyet
     98 dev_type_open(btnmgropen);
     99 dev_type_close(btnmgrclose);
    100 dev_type_read(btnmgrread);
    101 dev_type_write(btnmgrwrite);
    102 dev_type_ioctl(btnmgrioctl);
    103 
    104 const struct cdevsw btnmgr_cdevsw = {
    105 	btnmgropen, btnmgrclose, btnmgrread, btnmgrwrite, btnmgrioctl,
    106 	nostop, notty, nopoll, nommap, nokqfilter,
    107 };
    108 #endif /* notyet */
    109 
    110 /* wskbd accessopts */
    111 int	btnmgr_wskbd_enable(void *, int);
    112 void	btnmgr_wskbd_set_leds(void *, int);
    113 int	btnmgr_wskbd_ioctl(void *, u_long, void *, int, struct lwp *);
    114 
    115 const struct wskbd_accessops btnmgr_wskbd_accessops = {
    116 	btnmgr_wskbd_enable,
    117 	btnmgr_wskbd_set_leds,
    118 	btnmgr_wskbd_ioctl,
    119 };
    120 
    121 /* button config: index by button event id */
    122 static const struct {
    123 	int  kevent;
    124 	int  keycode;
    125 	const char *name;
    126 } button_config[] = {
    127 	/* id					kevent keycode name	*/
    128 	[CONFIG_HOOK_BUTTONEVENT_POWER] =	{ 0,   0, "Power"	},
    129 	[CONFIG_HOOK_BUTTONEVENT_OK] =		{ 1,  28, "OK"		},
    130 	[CONFIG_HOOK_BUTTONEVENT_CANCEL] =	{ 1,   1, "Cancel"	},
    131 	[CONFIG_HOOK_BUTTONEVENT_UP] =		{ 1,  72, "Up"		},
    132 	[CONFIG_HOOK_BUTTONEVENT_DOWN] =	{ 1,  80, "Down"	},
    133 	[CONFIG_HOOK_BUTTONEVENT_REC] =		{ 0,   0, "Rec"		},
    134 	[CONFIG_HOOK_BUTTONEVENT_COVER] =	{ 0,   0, "Cover"	},
    135 	[CONFIG_HOOK_BUTTONEVENT_LIGHT] =	{ 1,  57, "Light"	},
    136 	[CONFIG_HOOK_BUTTONEVENT_CONTRAST] =	{ 0,   0, "Contrast"	},
    137 	[CONFIG_HOOK_BUTTONEVENT_APP0] =	{ 1,  67, "Application 0" },
    138 	[CONFIG_HOOK_BUTTONEVENT_APP1] =	{ 1,  68, "Application 1" },
    139 	[CONFIG_HOOK_BUTTONEVENT_APP2] =	{ 1,  87, "Application 2" },
    140 	[CONFIG_HOOK_BUTTONEVENT_APP3] =	{ 1,  88, "Application 3" },
    141 };
    142 static const int n_button_config =
    143 	sizeof(button_config) / sizeof(*button_config);
    144 
    145 #define KC(n) KS_KEYCODE(n)
    146 static const keysym_t btnmgr_keydesc_default[] = {
    147 /*  pos				normal			shifted		*/
    148 	KC(1), 			KS_Escape,
    149 	KC(28), 			KS_Return,
    150 	KC(57),	KS_Cmd,		KS_Cmd_BacklightToggle,
    151 	KC(67), 			KS_f9,
    152 	KC(68), 			KS_f10,
    153 	KC(72), 			KS_KP_Up,
    154 	KC(80), 			KS_KP_Down,
    155 	KC(87), 			KS_f11,
    156 	KC(88), 			KS_f12,
    157 };
    158 #undef KC
    159 #define KBD_MAP(name, base, map) \
    160 			{ name, base, sizeof(map)/sizeof(keysym_t), map }
    161 const struct wscons_keydesc btnmgr_keydesctab[] = {
    162 	KBD_MAP(KB_US,		0,	btnmgr_keydesc_default),
    163 	{0, 0, 0, 0}
    164 };
    165 #undef KBD_MAP
    166 
    167 struct wskbd_mapdata btnmgr_keymapdata = {
    168 	btnmgr_keydesctab,
    169 	KB_US, /* XXX, This is bad idea... */
    170 };
    171 
    172 /*
    173  *  function bodies
    174  */
    175 int
    176 btnmgrmatch(device_t parent, cfdata_t match, void *aux)
    177 {
    178 	struct mainbus_attach_args *ma = aux;
    179 
    180 	if (strcmp(ma->ma_name, match->cf_name))
    181 		return 0;
    182 
    183 	return (1);
    184 }
    185 
    186 void
    187 btnmgrattach(device_t parent,
    188 	     device_t self, void *aux)
    189 {
    190 	int id;
    191 	struct btnmgr_softc *sc = device_private(self);
    192 	struct wskbddev_attach_args wa;
    193 
    194 	printf("\n");
    195 
    196 	/*
    197 	 * install button event listener
    198 	 */
    199 	for (id = 0; id <= n_button_config; id++)
    200 		if (button_config[id].name != NULL)
    201 			sc->sc_hook_tag = config_hook(CONFIG_HOOK_BUTTONEVENT,
    202 			    id, CONFIG_HOOK_SHARE,
    203 			    btnmgr_hook, sc);
    204 
    205 	/*
    206 	 * attach wskbd
    207 	 */
    208 	wa.console = 0;
    209 	wa.keymap = &btnmgr_keymapdata;
    210 	wa.accessops = &btnmgr_wskbd_accessops;
    211 	wa.accesscookie = sc;
    212 
    213 	sc->sc_wskbddev = config_found(self, &wa, wskbddevprint);
    214 
    215 	if (!pmf_device_register(self, NULL, NULL))
    216 		aprint_error_dev(self, "unable to establish power handler\n");
    217 }
    218 
    219 static int
    220 btnmgr_hook(void *ctx, int type, long id, void *msg)
    221 {
    222 	struct btnmgr_softc *sc = ctx;
    223 
    224 	DPRINTF(("%s button: %s\n", btnmgr_name(id), msg ? "ON" : "OFF"));
    225 
    226 	if (button_config[id].kevent) {
    227 		u_int evtype;
    228 		evtype = msg ? WSCONS_EVENT_KEY_DOWN : WSCONS_EVENT_KEY_UP;
    229 #ifdef WSDISPLAY_COMPAT_RAWKBD
    230 		if (sc->sc_rawkbd) {
    231 			int n;
    232 			u_char data[16];
    233 			n = pckbd_encode(evtype, button_config[id].keycode,
    234 			    data);
    235 			wskbd_rawinput(sc->sc_wskbddev, data, n);
    236 		} else
    237 #endif
    238 			wskbd_input(sc->sc_wskbddev, evtype,
    239 			    button_config[id].keycode);
    240 	}
    241 
    242 	if (id == CONFIG_HOOK_BUTTONEVENT_POWER && msg)
    243 		config_hook_call(CONFIG_HOOK_PMEVENT,
    244 		    CONFIG_HOOK_PMEVENT_SUSPENDREQ, NULL);
    245 	else if (id == CONFIG_HOOK_BUTTONEVENT_COVER)
    246 		config_hook_call(CONFIG_HOOK_POWERCONTROL,
    247 		    CONFIG_HOOK_POWERCONTROL_LCDLIGHT, (void*)(msg ? 0: 1));
    248 
    249 	return (0);
    250 }
    251 
    252 const char *
    253 btnmgr_name(long id)
    254 {
    255 	if (id < n_button_config)
    256 		return (button_config[id].name);
    257 	return ("unknown");
    258 }
    259 
    260 int
    261 btnmgr_wskbd_enable(void *scx, int on)
    262 {
    263 	struct btnmgr_softc *sc = scx;
    264 
    265 	if (on) {
    266 		if (sc->sc_enabled)
    267 			return (EBUSY);
    268 		sc->sc_enabled = 1;
    269 	} else {
    270 		sc->sc_enabled = 0;
    271 	}
    272 
    273 	return (0);
    274 }
    275 
    276 void
    277 btnmgr_wskbd_set_leds(void *scx, int leds)
    278 {
    279 	/*
    280 	 * We have nothing to do.
    281 	 */
    282 }
    283 
    284 int
    285 btnmgr_wskbd_ioctl(void *scx, u_long cmd, void *data, int flag,
    286 		   struct lwp *l)
    287 {
    288 #ifdef WSDISPLAY_COMPAT_RAWKBD
    289 	struct btnmgr_softc *sc = scx;
    290 #endif
    291 	switch (cmd) {
    292 	case WSKBDIO_GTYPE:
    293 		*(int *)data = WSKBD_TYPE_HPC_BTN;
    294 		return (0);
    295 	case WSKBDIO_SETLEDS:
    296 		DPRINTF(("%s(%d): no LED\n", __FILE__, __LINE__));
    297 		return (0);
    298 	case WSKBDIO_GETLEDS:
    299 		DPRINTF(("%s(%d): no LED\n", __FILE__, __LINE__));
    300 		*(int *)data = 0;
    301 		return (0);
    302 #ifdef WSDISPLAY_COMPAT_RAWKBD
    303 	case WSKBDIO_SETMODE:
    304 		sc->sc_rawkbd = (*(int *)data == WSKBD_RAW);
    305 		DPRINTF(("%s(%d): rawkbd is %s\n", __FILE__, __LINE__,
    306 		    sc->sc_rawkbd ? "on" : "off"));
    307 		return (0);
    308 #endif
    309 	}
    310 	return (EPASSTHROUGH);
    311 }
    312 
    313 #ifdef notyet
    314 int
    315 btnmgropen(dev_t dev, int flag, int mode, struct lwp *l)
    316 {
    317 	return (EINVAL);
    318 }
    319 
    320 int
    321 btnmgrclose(dev_t dev, int flag, int mode, struct lwp *l)
    322 {
    323 	return (EINVAL);
    324 }
    325 
    326 int
    327 btnmgrread(dev_t dev, struct uio *uio, int flag)
    328 {
    329 	return (EINVAL);
    330 }
    331 
    332 int
    333 btnmgrwrite(dev_t dev, struct uio *uio, int flag)
    334 {
    335 	return (EINVAL);
    336 }
    337 
    338 int
    339 btnmgrioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
    340 {
    341 	return (EINVAL);
    342 }
    343 #endif /* notyet */
    344