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