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