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