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