xref: /netbsd-src/sys/dev/hpc/btnmgr.c (revision c7fb772b85b2b5d4cfb282f868f454b4701534fd)
1*c7fb772bSthorpej /*	$NetBSD: btnmgr.c,v 1.31 2021/08/07 16:19:11 thorpej Exp $	*/
2659f65e0Such 
3659f65e0Such /*-
4659f65e0Such  * Copyright (c) 1999
5659f65e0Such  *         Shin Takemura and PocketBSD Project. All rights reserved.
6659f65e0Such  *
7659f65e0Such  * Redistribution and use in source and binary forms, with or without
8659f65e0Such  * modification, are permitted provided that the following conditions
9659f65e0Such  * are met:
10659f65e0Such  * 1. Redistributions of source code must retain the above copyright
11659f65e0Such  *    notice, this list of conditions and the following disclaimer.
12659f65e0Such  * 2. Redistributions in binary form must reproduce the above copyright
13659f65e0Such  *    notice, this list of conditions and the following disclaimer in the
14659f65e0Such  *    documentation and/or other materials provided with the distribution.
15659f65e0Such  * 3. All advertising materials mentioning features or use of this software
16659f65e0Such  *    must display the following acknowledgement:
17659f65e0Such  *	This product includes software developed by the PocketBSD project
18659f65e0Such  *	and its contributors.
19659f65e0Such  * 4. Neither the name of the project nor the names of its contributors
20659f65e0Such  *    may be used to endorse or promote products derived from this software
21659f65e0Such  *    without specific prior written permission.
22659f65e0Such  *
23659f65e0Such  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24659f65e0Such  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25659f65e0Such  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26659f65e0Such  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27659f65e0Such  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28659f65e0Such  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29659f65e0Such  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30659f65e0Such  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31659f65e0Such  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32659f65e0Such  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33659f65e0Such  * SUCH DAMAGE.
34659f65e0Such  *
35659f65e0Such  */
36b84f53efSlukem 
37b84f53efSlukem #include <sys/cdefs.h>
38*c7fb772bSthorpej __KERNEL_RCSID(0, "$NetBSD: btnmgr.c,v 1.31 2021/08/07 16:19:11 thorpej Exp $");
39b84f53efSlukem 
409eddf385Speter #ifdef _KERNEL_OPT
419eddf385Speter #include "opt_btnmgr.h"
429eddf385Speter #include "opt_wsdisplay_compat.h"
439eddf385Speter #endif
44659f65e0Such 
45659f65e0Such #include <sys/param.h>
46659f65e0Such #include <sys/systm.h>
47659f65e0Such #include <sys/ioctl.h>
48659f65e0Such #include <sys/conf.h>
49659f65e0Such #include <sys/device.h>
50659f65e0Such #include <sys/malloc.h>
51659f65e0Such #include <sys/kernel.h>
52659f65e0Such 
53659f65e0Such #include <dev/wscons/wsconsio.h>
54659f65e0Such #include <dev/wscons/wskbdvar.h>
55659f65e0Such #include <dev/wscons/wsksymdef.h>
56659f65e0Such #ifdef WSDISPLAY_COMPAT_RAWKBD
57659f65e0Such #include <dev/hpc/pckbd_encode.h>
58659f65e0Such #endif
59659f65e0Such 
60a2a38285Sad #include <sys/bus.h>
61659f65e0Such #include <machine/autoconf.h>
62659f65e0Such #include <machine/config_hook.h>
63659f65e0Such 
64659f65e0Such #ifdef BTNMGRDEBUG
65659f65e0Such #ifndef BTNMGRDEBUG_CONF
66659f65e0Such #define BTNMGRDEBUG_CONF 0
67659f65e0Such #endif
68659f65e0Such int	btnmgr_debug = BTNMGRDEBUG_CONF;
69659f65e0Such #define	DPRINTF(arg) if (btnmgr_debug) printf arg;
70659f65e0Such #define	DPRINTFN(n, arg) if (btnmgr_debug > (n)) printf arg;
71659f65e0Such #else
72659f65e0Such #define	DPRINTF(arg)
73659f65e0Such #define DPRINTFN(n, arg)
74659f65e0Such #endif
75659f65e0Such 
76659f65e0Such struct btnmgr_softc {
77659f65e0Such 	config_hook_tag	sc_hook_tag;
78659f65e0Such 	int sc_enabled;
79529e91fcScegger 	device_t sc_wskbddev;
80659f65e0Such #ifdef WSDISPLAY_COMPAT_RAWKBD
81659f65e0Such 	int sc_rawkbd;
82659f65e0Such #endif
83659f65e0Such };
84659f65e0Such 
85529e91fcScegger int btnmgrmatch(device_t, cfdata_t, void *);
86529e91fcScegger void btnmgrattach(device_t, device_t, void *);
876a99517aSuwe const char *btnmgr_name(long);
88859a6a49Such static int btnmgr_hook(void *, int, long, void *);
89659f65e0Such 
90659f65e0Such /*
91659f65e0Such  * global/static data
92659f65e0Such  */
93cbab9cadSchs CFATTACH_DECL_NEW(btnmgr, sizeof(struct btnmgr_softc),
94c9b3657cSthorpej     btnmgrmatch, btnmgrattach, NULL, NULL);
95659f65e0Such 
9677a6b82bSgehenna #ifdef notyet
9777a6b82bSgehenna dev_type_open(btnmgropen);
9877a6b82bSgehenna dev_type_close(btnmgrclose);
9977a6b82bSgehenna dev_type_read(btnmgrread);
10077a6b82bSgehenna dev_type_write(btnmgrwrite);
10177a6b82bSgehenna dev_type_ioctl(btnmgrioctl);
10277a6b82bSgehenna 
10377a6b82bSgehenna const struct cdevsw btnmgr_cdevsw = {
104a68f9396Sdholland 	.d_open = btnmgropen,
105a68f9396Sdholland 	.d_close = btnmgrclose,
106a68f9396Sdholland 	.d_read = btnmgrread,
107a68f9396Sdholland 	.d_write = btnmgrwrite,
108a68f9396Sdholland 	.d_ioctl = btnmgrioctl,
109a68f9396Sdholland 	.d_stop = nostop,
110a68f9396Sdholland 	.d_tty = notty,
111a68f9396Sdholland 	.d_poll = nopoll,
112a68f9396Sdholland 	.d_mmap = nommap,
113a68f9396Sdholland 	.d_kqfilter = nokqfilter,
114f9228f42Sdholland 	.d_discard = nodiscard,
115a68f9396Sdholland 	.d_flag = 0
11677a6b82bSgehenna };
11777a6b82bSgehenna #endif /* notyet */
11877a6b82bSgehenna 
119659f65e0Such /* wskbd accessopts */
120859a6a49Such int	btnmgr_wskbd_enable(void *, int);
121859a6a49Such void	btnmgr_wskbd_set_leds(void *, int);
12253524e44Schristos int	btnmgr_wskbd_ioctl(void *, u_long, void *, int, struct lwp *);
123659f65e0Such 
124659f65e0Such const struct wskbd_accessops btnmgr_wskbd_accessops = {
125659f65e0Such 	btnmgr_wskbd_enable,
126659f65e0Such 	btnmgr_wskbd_set_leds,
127659f65e0Such 	btnmgr_wskbd_ioctl,
128659f65e0Such };
129659f65e0Such 
130f4aa0d8fSpeter /* button config: index by button event id */
131659f65e0Such static const struct {
132659f65e0Such 	int  kevent;
133659f65e0Such 	int  keycode;
1346a99517aSuwe 	const char *name;
135659f65e0Such } button_config[] = {
136659f65e0Such 	/* id					kevent keycode name	*/
137659f65e0Such 	[CONFIG_HOOK_BUTTONEVENT_POWER] =	{ 0,   0, "Power"	},
138659f65e0Such 	[CONFIG_HOOK_BUTTONEVENT_OK] =		{ 1,  28, "OK"		},
139659f65e0Such 	[CONFIG_HOOK_BUTTONEVENT_CANCEL] =	{ 1,   1, "Cancel"	},
140659f65e0Such 	[CONFIG_HOOK_BUTTONEVENT_UP] =		{ 1,  72, "Up"		},
141659f65e0Such 	[CONFIG_HOOK_BUTTONEVENT_DOWN] =	{ 1,  80, "Down"	},
142659f65e0Such 	[CONFIG_HOOK_BUTTONEVENT_REC] =		{ 0,   0, "Rec"		},
143659f65e0Such 	[CONFIG_HOOK_BUTTONEVENT_COVER] =	{ 0,   0, "Cover"	},
144659f65e0Such 	[CONFIG_HOOK_BUTTONEVENT_LIGHT] =	{ 1,  57, "Light"	},
145659f65e0Such 	[CONFIG_HOOK_BUTTONEVENT_CONTRAST] =	{ 0,   0, "Contrast"	},
146659f65e0Such 	[CONFIG_HOOK_BUTTONEVENT_APP0] =	{ 1,  67, "Application 0" },
147659f65e0Such 	[CONFIG_HOOK_BUTTONEVENT_APP1] =	{ 1,  68, "Application 1" },
148659f65e0Such 	[CONFIG_HOOK_BUTTONEVENT_APP2] =	{ 1,  87, "Application 2" },
149659f65e0Such 	[CONFIG_HOOK_BUTTONEVENT_APP3] =	{ 1,  88, "Application 3" },
150659f65e0Such };
151659f65e0Such static const int n_button_config =
152659f65e0Such 	sizeof(button_config) / sizeof(*button_config);
153659f65e0Such 
154659f65e0Such #define KC(n) KS_KEYCODE(n)
155659f65e0Such static const keysym_t btnmgr_keydesc_default[] = {
156659f65e0Such /*  pos				normal			shifted		*/
157659f65e0Such 	KC(1), 			KS_Escape,
158659f65e0Such 	KC(28), 			KS_Return,
159659f65e0Such 	KC(57),	KS_Cmd,		KS_Cmd_BacklightToggle,
160659f65e0Such 	KC(67), 			KS_f9,
161659f65e0Such 	KC(68), 			KS_f10,
162659f65e0Such 	KC(72), 			KS_KP_Up,
163659f65e0Such 	KC(80), 			KS_KP_Down,
164659f65e0Such 	KC(87), 			KS_f11,
165659f65e0Such 	KC(88), 			KS_f12,
166659f65e0Such };
167659f65e0Such #undef KC
168659f65e0Such #define KBD_MAP(name, base, map) \
169659f65e0Such 			{ name, base, sizeof(map)/sizeof(keysym_t), map }
170659f65e0Such const struct wscons_keydesc btnmgr_keydesctab[] = {
171659f65e0Such 	KBD_MAP(KB_US,		0,	btnmgr_keydesc_default),
172659f65e0Such 	{0, 0, 0, 0}
173659f65e0Such };
174659f65e0Such #undef KBD_MAP
175659f65e0Such 
176659f65e0Such struct wskbd_mapdata btnmgr_keymapdata = {
177659f65e0Such 	btnmgr_keydesctab,
178659f65e0Such 	KB_US, /* XXX, This is bad idea... */
179659f65e0Such };
180659f65e0Such 
181659f65e0Such /*
182659f65e0Such  *  function bodies
183659f65e0Such  */
184659f65e0Such int
btnmgrmatch(device_t parent,cfdata_t match,void * aux)185529e91fcScegger btnmgrmatch(device_t parent, cfdata_t match, void *aux)
186659f65e0Such {
187659f65e0Such 	struct mainbus_attach_args *ma = aux;
188659f65e0Such 
189d1ad2ac4Sthorpej 	if (strcmp(ma->ma_name, match->cf_name))
190659f65e0Such 		return 0;
191659f65e0Such 
192659f65e0Such 	return (1);
193659f65e0Such }
194659f65e0Such 
195659f65e0Such void
btnmgrattach(device_t parent,device_t self,void * aux)196529e91fcScegger btnmgrattach(device_t parent,
197529e91fcScegger 	     device_t self, void *aux)
198659f65e0Such {
199659f65e0Such 	int id;
20092c7bba3Sthorpej 	struct btnmgr_softc *sc = device_private(self);
201659f65e0Such 	struct wskbddev_attach_args wa;
202659f65e0Such 
203659f65e0Such 	printf("\n");
204659f65e0Such 
205659f65e0Such 	/*
206659f65e0Such 	 * install button event listener
207659f65e0Such 	 */
2081d5354e1Sskrll 	for (id = 0; id < n_button_config; id++)
209659f65e0Such 		if (button_config[id].name != NULL)
210659f65e0Such 			sc->sc_hook_tag = config_hook(CONFIG_HOOK_BUTTONEVENT,
211659f65e0Such 			    id, CONFIG_HOOK_SHARE,
212659f65e0Such 			    btnmgr_hook, sc);
213659f65e0Such 
214659f65e0Such 	/*
215659f65e0Such 	 * attach wskbd
216659f65e0Such 	 */
217659f65e0Such 	wa.console = 0;
218659f65e0Such 	wa.keymap = &btnmgr_keymapdata;
219659f65e0Such 	wa.accessops = &btnmgr_wskbd_accessops;
220659f65e0Such 	wa.accesscookie = sc;
221659f65e0Such 
222*c7fb772bSthorpej 	sc->sc_wskbddev = config_found(self, &wa, wskbddevprint, CFARGS_NONE);
223fe0f5148Suwe 
224fe0f5148Suwe 	if (!pmf_device_register(self, NULL, NULL))
225fe0f5148Suwe 		aprint_error_dev(self, "unable to establish power handler\n");
226659f65e0Such }
227659f65e0Such 
228659f65e0Such static int
btnmgr_hook(void * ctx,int type,long id,void * msg)229168cd830Schristos btnmgr_hook(void *ctx, int type, long id, void *msg)
230659f65e0Such {
231659f65e0Such 	struct btnmgr_softc *sc = ctx;
232659f65e0Such 
233659f65e0Such 	DPRINTF(("%s button: %s\n", btnmgr_name(id), msg ? "ON" : "OFF"));
234659f65e0Such 
235659f65e0Such 	if (button_config[id].kevent) {
236659f65e0Such 		u_int evtype;
237659f65e0Such 		evtype = msg ? WSCONS_EVENT_KEY_DOWN : WSCONS_EVENT_KEY_UP;
238659f65e0Such #ifdef WSDISPLAY_COMPAT_RAWKBD
239659f65e0Such 		if (sc->sc_rawkbd) {
240659f65e0Such 			int n;
241659f65e0Such 			u_char data[16];
242659f65e0Such 			n = pckbd_encode(evtype, button_config[id].keycode,
243659f65e0Such 			    data);
244659f65e0Such 			wskbd_rawinput(sc->sc_wskbddev, data, n);
245659f65e0Such 		} else
246659f65e0Such #endif
247659f65e0Such 			wskbd_input(sc->sc_wskbddev, evtype,
248659f65e0Such 			    button_config[id].keycode);
249659f65e0Such 	}
250659f65e0Such 
251659f65e0Such 	if (id == CONFIG_HOOK_BUTTONEVENT_POWER && msg)
252659f65e0Such 		config_hook_call(CONFIG_HOOK_PMEVENT,
253659f65e0Such 		    CONFIG_HOOK_PMEVENT_SUSPENDREQ, NULL);
254764daa35Shamajima 	else if (id == CONFIG_HOOK_BUTTONEVENT_COVER)
255764daa35Shamajima 		config_hook_call(CONFIG_HOOK_POWERCONTROL,
256764daa35Shamajima 		    CONFIG_HOOK_POWERCONTROL_LCDLIGHT, (void*)(msg ? 0: 1));
257659f65e0Such 
258659f65e0Such 	return (0);
259659f65e0Such }
260659f65e0Such 
2616a99517aSuwe const char *
btnmgr_name(long id)262859a6a49Such btnmgr_name(long id)
263659f65e0Such {
264659f65e0Such 	if (id < n_button_config)
265859a6a49Such 		return (button_config[id].name);
266659f65e0Such 	return ("unknown");
267659f65e0Such }
268659f65e0Such 
269659f65e0Such int
btnmgr_wskbd_enable(void * scx,int on)270859a6a49Such btnmgr_wskbd_enable(void *scx, int on)
271659f65e0Such {
272659f65e0Such 	struct btnmgr_softc *sc = scx;
273659f65e0Such 
274659f65e0Such 	if (on) {
275659f65e0Such 		if (sc->sc_enabled)
276659f65e0Such 			return (EBUSY);
277659f65e0Such 		sc->sc_enabled = 1;
278659f65e0Such 	} else {
279659f65e0Such 		sc->sc_enabled = 0;
280659f65e0Such 	}
281659f65e0Such 
282659f65e0Such 	return (0);
283659f65e0Such }
284659f65e0Such 
285659f65e0Such void
btnmgr_wskbd_set_leds(void * scx,int leds)286168cd830Schristos btnmgr_wskbd_set_leds(void *scx, int leds)
287659f65e0Such {
288659f65e0Such 	/*
289659f65e0Such 	 * We have nothing to do.
290659f65e0Such 	 */
291659f65e0Such }
292659f65e0Such 
293659f65e0Such int
btnmgr_wskbd_ioctl(void * scx,u_long cmd,void * data,int flag,struct lwp * l)29453524e44Schristos btnmgr_wskbd_ioctl(void *scx, u_long cmd, void *data, int flag,
295168cd830Schristos 		   struct lwp *l)
296659f65e0Such {
297659f65e0Such #ifdef WSDISPLAY_COMPAT_RAWKBD
298659f65e0Such 	struct btnmgr_softc *sc = scx;
299659f65e0Such #endif
300659f65e0Such 	switch (cmd) {
301659f65e0Such 	case WSKBDIO_GTYPE:
302659f65e0Such 		*(int *)data = WSKBD_TYPE_HPC_BTN;
303859a6a49Such 		return (0);
304659f65e0Such 	case WSKBDIO_SETLEDS:
305659f65e0Such 		DPRINTF(("%s(%d): no LED\n", __FILE__, __LINE__));
306859a6a49Such 		return (0);
307659f65e0Such 	case WSKBDIO_GETLEDS:
308659f65e0Such 		DPRINTF(("%s(%d): no LED\n", __FILE__, __LINE__));
309659f65e0Such 		*(int *)data = 0;
310659f65e0Such 		return (0);
311659f65e0Such #ifdef WSDISPLAY_COMPAT_RAWKBD
312659f65e0Such 	case WSKBDIO_SETMODE:
313659f65e0Such 		sc->sc_rawkbd = (*(int *)data == WSKBD_RAW);
314659f65e0Such 		DPRINTF(("%s(%d): rawkbd is %s\n", __FILE__, __LINE__,
315659f65e0Such 		    sc->sc_rawkbd ? "on" : "off"));
316659f65e0Such 		return (0);
317659f65e0Such #endif
318659f65e0Such 	}
31931144d99Satatat 	return (EPASSTHROUGH);
320659f65e0Such }
321659f65e0Such 
322659f65e0Such #ifdef notyet
323659f65e0Such int
btnmgropen(dev_t dev,int flag,int mode,struct lwp * l)32495e1ffb1Schristos btnmgropen(dev_t dev, int flag, int mode, struct lwp *l)
325659f65e0Such {
326659f65e0Such 	return (EINVAL);
327659f65e0Such }
328659f65e0Such 
329659f65e0Such int
btnmgrclose(dev_t dev,int flag,int mode,struct lwp * l)33095e1ffb1Schristos btnmgrclose(dev_t dev, int flag, int mode, struct lwp *l)
331659f65e0Such {
332659f65e0Such 	return (EINVAL);
333659f65e0Such }
334659f65e0Such 
335659f65e0Such int
btnmgrread(dev_t dev,struct uio * uio,int flag)336859a6a49Such btnmgrread(dev_t dev, struct uio *uio, int flag)
337659f65e0Such {
338659f65e0Such 	return (EINVAL);
339659f65e0Such }
340659f65e0Such 
341659f65e0Such int
btnmgrwrite(dev_t dev,struct uio * uio,int flag)342859a6a49Such btnmgrwrite(dev_t dev, struct uio *uio, int flag)
343659f65e0Such {
344659f65e0Such 	return (EINVAL);
345659f65e0Such }
346659f65e0Such 
347659f65e0Such int
btnmgrioctl(dev_t dev,u_long cmd,void * data,int flag,struct lwp * l)34853524e44Schristos btnmgrioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
349659f65e0Such {
350659f65e0Such 	return (EINVAL);
351659f65e0Such }
352659f65e0Such #endif /* notyet */
353