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