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