1 /* $NetBSD: gpiokeys.c,v 1.11 2021/08/07 16:19:10 thorpej Exp $ */ 2 3 /*- 4 * Copyright (c) 2015 Jared D. McNeill <jmcneill@invisible.ca> 5 * 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 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 __KERNEL_RCSID(0, "$NetBSD: gpiokeys.c,v 1.11 2021/08/07 16:19:10 thorpej Exp $"); 31 32 #include <sys/param.h> 33 #include <sys/kernel.h> 34 #include <sys/systm.h> 35 #include <sys/device.h> 36 #include <sys/kmem.h> 37 #include <sys/bus.h> 38 #include <sys/gpio.h> 39 40 #include <dev/sysmon/sysmonvar.h> 41 #include <dev/sysmon/sysmon_taskq.h> 42 43 #include <dev/wscons/wsconsio.h> 44 #include <dev/wscons/wskbdvar.h> 45 #include <dev/wscons/wsksymdef.h> 46 #include <dev/wscons/wsksymvar.h> 47 #include <dev/wscons/linux_keymap.h> 48 49 #include <dev/fdt/fdtvar.h> 50 51 #define GPIOKEYS_POLL_INTERVAL mstohz(200) 52 53 /* Event types */ 54 #define EV_KEY 1 55 #define EV_SW 5 56 57 /* Key and button events */ 58 #define KEY_POWER 116 59 #define KEY_SLEEP 142 60 61 /* Switch events */ 62 #define SW_LID 0 63 64 static int gpiokeys_match(device_t, cfdata_t, void *); 65 static void gpiokeys_attach(device_t, device_t, void *); 66 67 static void gpiokeys_tick(void *); 68 static void gpiokeys_task(void *); 69 70 extern const struct wscons_keydesc hidkbd_keydesctab[]; 71 static const struct wskbd_mapdata gpiokeys_keymapdata = { 72 hidkbd_keydesctab, 73 KB_US, 74 }; 75 76 struct gpiokeys_softc; 77 78 struct gpiokeys_key { 79 struct gpiokeys_softc *key_sc; 80 int key_phandle; 81 char *key_label; 82 struct fdtbus_gpio_pin *key_pin; 83 u_int key_debounce; 84 u_int key_code; 85 struct sysmon_pswitch key_pswitch; 86 uint8_t key_usbcode; 87 int key_state; 88 89 struct gpiokeys_key *key_next; 90 }; 91 92 struct gpiokeys_softc { 93 device_t sc_dev; 94 int sc_phandle; 95 96 struct fdtbus_gpio_pin *sc_pin; 97 bool sc_always_on; 98 bool sc_enable_val; 99 100 struct gpiokeys_key *sc_keys; 101 callout_t sc_tick; 102 103 device_t sc_wskbddev; 104 int sc_enabled; 105 }; 106 107 CFATTACH_DECL_NEW(gpiokeys, sizeof(struct gpiokeys_softc), 108 gpiokeys_match, gpiokeys_attach, NULL, NULL); 109 110 static int 111 gpiokeys_enable(void *v, int on) 112 { 113 struct gpiokeys_softc * const sc = v; 114 115 sc->sc_enabled = on; 116 117 return 0; 118 } 119 120 static void 121 gpiokeys_set_leds(void *v, int leds) 122 { 123 } 124 125 static int 126 gpiokeys_ioctl(void *v, u_long cmd, void *data, int flag, lwp_t *l) 127 { 128 switch (cmd) { 129 case WSKBDIO_GTYPE: 130 *(int *)data = WSKBD_TYPE_USB; 131 return 0; 132 } 133 134 return EPASSTHROUGH; 135 } 136 137 static const struct wskbd_accessops gpiokeys_accessops = { 138 .enable = gpiokeys_enable, 139 .set_leds = gpiokeys_set_leds, 140 .ioctl = gpiokeys_ioctl 141 }; 142 143 static const struct device_compatible_entry compat_data[] = { 144 { .compat = "gpio-keys" }, 145 DEVICE_COMPAT_EOL 146 }; 147 148 static int 149 gpiokeys_match(device_t parent, cfdata_t cf, void *aux) 150 { 151 const struct fdt_attach_args *faa = aux; 152 153 return of_compatible_match(faa->faa_phandle, compat_data); 154 } 155 156 static void 157 gpiokeys_attach(device_t parent, device_t self, void *aux) 158 { 159 struct gpiokeys_softc * const sc = device_private(self); 160 const struct fdt_attach_args *faa = aux; 161 const int phandle = faa->faa_phandle; 162 struct gpiokeys_key *key; 163 u_int debounce, input_type, code; 164 int use_wskbddev = 0; 165 int child, len; 166 167 sc->sc_dev = self; 168 sc->sc_phandle = phandle; 169 170 aprint_naive("\n"); 171 aprint_normal(":"); 172 173 for (child = OF_child(phandle); child; child = OF_peer(child)) { 174 if (of_getprop_uint32(child, "linux,input-type", &input_type)) 175 input_type = EV_KEY; /* default */ 176 if (of_getprop_uint32(child, "linux,code", &code)) 177 continue; 178 if (of_getprop_uint32(child, "debounce-interval", &debounce)) 179 debounce = 5; /* default */ 180 len = OF_getproplen(child, "label"); 181 if (len <= 0) { 182 continue; 183 } 184 key = kmem_zalloc(sizeof(*key), KM_SLEEP); 185 key->key_sc = sc; 186 key->key_phandle = child; 187 key->key_code = code; 188 key->key_label = kmem_zalloc(len, KM_SLEEP); 189 if (OF_getprop(child, "label", key->key_label, len) != len) { 190 kmem_free(key->key_label, len); 191 kmem_free(key, sizeof(*key)); 192 continue; 193 } 194 key->key_debounce = debounce; 195 key->key_pin = fdtbus_gpio_acquire(child, "gpios", 196 GPIO_PIN_INPUT); 197 if (key->key_pin) 198 key->key_state = fdtbus_gpio_read(key->key_pin); 199 200 switch (input_type) { 201 case EV_KEY: 202 switch (code) { 203 case KEY_POWER: 204 key->key_pswitch.smpsw_name = key->key_label; 205 key->key_pswitch.smpsw_type = PSWITCH_TYPE_POWER; 206 break; 207 case KEY_SLEEP: 208 key->key_pswitch.smpsw_name = key->key_label; 209 key->key_pswitch.smpsw_type = PSWITCH_TYPE_SLEEP; 210 break; 211 default: 212 key->key_usbcode = linux_key_to_usb(code); 213 if (key->key_usbcode != 0) { 214 use_wskbddev++; 215 } else { 216 key->key_pswitch.smpsw_name = key->key_label; 217 key->key_pswitch.smpsw_type = PSWITCH_TYPE_HOTKEY; 218 } 219 break; 220 } 221 break; 222 case EV_SW: 223 key->key_pswitch.smpsw_name = key->key_label; 224 switch (code) { 225 case SW_LID: 226 key->key_state = -1; /* Send notification on attach */ 227 key->key_pswitch.smpsw_type = PSWITCH_TYPE_LID; 228 break; 229 default: 230 key->key_pswitch.smpsw_type = PSWITCH_TYPE_HOTKEY; 231 break; 232 } 233 break; 234 } 235 236 if (key->key_pswitch.smpsw_name != NULL && 237 sysmon_pswitch_register(&key->key_pswitch) != 0) { 238 aprint_error(" %s:ERROR", key->key_label); 239 kmem_free(key->key_label, len); 240 kmem_free(key, sizeof(*key)); 241 continue; 242 } 243 244 if (sc->sc_keys) { 245 aprint_normal(", %s", key->key_label); 246 } else { 247 aprint_normal(" %s", key->key_label); 248 } 249 250 key->key_next = sc->sc_keys; 251 sc->sc_keys = key; 252 } 253 254 if (sc->sc_keys == NULL) { 255 aprint_normal(" no keys configured\n"); 256 return; 257 } 258 259 aprint_normal("\n"); 260 261 if (use_wskbddev > 0) { 262 struct wskbddev_attach_args a; 263 memset(&a, 0, sizeof(a)); 264 a.console = false; 265 a.keymap = &gpiokeys_keymapdata; 266 a.accessops = &gpiokeys_accessops; 267 a.accesscookie = sc; 268 sc->sc_wskbddev = 269 config_found(self, &a, wskbddevprint, CFARGS_NONE); 270 } 271 272 callout_init(&sc->sc_tick, CALLOUT_MPSAFE); 273 callout_setfunc(&sc->sc_tick, gpiokeys_tick, sc); 274 275 gpiokeys_tick(sc); 276 } 277 278 static void 279 gpiokeys_tick(void *priv) 280 { 281 struct gpiokeys_softc * const sc = priv; 282 struct gpiokeys_key *key; 283 284 for (key = sc->sc_keys; key; key = key->key_next) { 285 if (key->key_pin == NULL) { 286 continue; 287 } 288 const int new_state = fdtbus_gpio_read(key->key_pin); 289 if (new_state != key->key_state) { 290 key->key_state = new_state; 291 sysmon_task_queue_sched(0, gpiokeys_task, key); 292 } 293 } 294 callout_schedule(&sc->sc_tick, GPIOKEYS_POLL_INTERVAL); 295 } 296 297 static void 298 gpiokeys_task(void *priv) 299 { 300 struct gpiokeys_key *key = priv; 301 struct gpiokeys_softc *sc = key->key_sc; 302 303 if (key->key_pswitch.smpsw_name) { 304 sysmon_pswitch_event(&key->key_pswitch, 305 key->key_state ? PSWITCH_EVENT_PRESSED : PSWITCH_EVENT_RELEASED); 306 } else if (sc->sc_enabled && sc->sc_wskbddev != NULL && key->key_usbcode != 0) { 307 wskbd_input(sc->sc_wskbddev, 308 key->key_state ? WSCONS_EVENT_KEY_DOWN : WSCONS_EVENT_KEY_UP, 309 key->key_usbcode); 310 } 311 } 312