1 /*- 2 * Copyright (c) 2015-2016 Oleksandr Tymoshenko <gonzo@freebsd.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27 #include <sys/cdefs.h> 28 __FBSDID("$FreeBSD$"); 29 30 #include "opt_platform.h" 31 #include "opt_kbd.h" 32 #include "opt_evdev.h" 33 34 #include <sys/param.h> 35 #include <sys/systm.h> 36 #include <sys/bus.h> 37 #include <sys/gpio.h> 38 #include <sys/kernel.h> 39 #include <sys/lock.h> 40 #include <sys/malloc.h> 41 #include <sys/module.h> 42 #include <sys/mutex.h> 43 #include <sys/proc.h> 44 #include <sys/kdb.h> 45 46 #include <sys/ioccom.h> 47 #include <sys/filio.h> 48 #include <sys/kbio.h> 49 50 #include <dev/kbd/kbdreg.h> 51 #include <dev/kbd/kbdtables.h> 52 53 #include <dev/fdt/fdt_common.h> 54 #include <dev/ofw/ofw_bus.h> 55 #include <dev/ofw/ofw_bus_subr.h> 56 57 #include <dev/gpio/gpiobusvar.h> 58 #include <dev/gpio/gpiokeys.h> 59 60 #ifdef EVDEV_SUPPORT 61 #include <dev/evdev/evdev.h> 62 #include <dev/evdev/input.h> 63 #endif 64 65 #define KBD_DRIVER_NAME "gpiokeys" 66 67 #define GPIOKEYS_LOCK(_sc) mtx_lock(&(_sc)->sc_mtx) 68 #define GPIOKEYS_UNLOCK(_sc) mtx_unlock(&(_sc)->sc_mtx) 69 #define GPIOKEYS_LOCK_INIT(_sc) \ 70 mtx_init(&_sc->sc_mtx, device_get_nameunit((_sc)->sc_dev), \ 71 "gpiokeys", MTX_DEF) 72 #define GPIOKEYS_LOCK_DESTROY(_sc) mtx_destroy(&(_sc)->sc_mtx); 73 #define GPIOKEYS_ASSERT_LOCKED(_sc) mtx_assert(&(_sc)->sc_mtx, MA_OWNED) 74 75 #define GPIOKEY_LOCK(_key) mtx_lock(&(_key)->mtx) 76 #define GPIOKEY_UNLOCK(_key) mtx_unlock(&(_key)->mtx) 77 #define GPIOKEY_LOCK_INIT(_key) \ 78 mtx_init(&(_key)->mtx, "gpiokey", "gpiokey", MTX_DEF) 79 #define GPIOKEY_LOCK_DESTROY(_key) mtx_destroy(&(_key)->mtx); 80 81 #define KEY_PRESS 0 82 #define KEY_RELEASE 0x80 83 84 #define SCAN_PRESS 0 85 #define SCAN_RELEASE 0x80 86 #define SCAN_CHAR(c) ((c) & 0x7f) 87 88 #define GPIOKEYS_GLOBAL_NMOD 8 /* units */ 89 #define GPIOKEYS_GLOBAL_NKEYCODE 6 /* units */ 90 #define GPIOKEYS_GLOBAL_IN_BUF_SIZE (2*(GPIOKEYS_GLOBAL_NMOD + (2*GPIOKEYS_GLOBAL_NKEYCODE))) /* bytes */ 91 #define GPIOKEYS_GLOBAL_IN_BUF_FULL (GPIOKEYS_GLOBAL_IN_BUF_SIZE / 2) /* bytes */ 92 #define GPIOKEYS_GLOBAL_NFKEY (sizeof(fkey_tab)/sizeof(fkey_tab[0])) /* units */ 93 #define GPIOKEYS_GLOBAL_BUFFER_SIZE 64 /* bytes */ 94 95 #define AUTOREPEAT_DELAY 250 96 #define AUTOREPEAT_REPEAT 34 97 98 struct gpiokeys_softc; 99 100 struct gpiokey 101 { 102 struct gpiokeys_softc *parent_sc; 103 gpio_pin_t pin; 104 int irq_rid; 105 struct resource *irq_res; 106 void *intr_hl; 107 struct mtx mtx; 108 #ifdef EVDEV_SUPPORT 109 uint32_t evcode; 110 #endif 111 uint32_t keycode; 112 int autorepeat; 113 struct callout debounce_callout; 114 struct callout repeat_callout; 115 int repeat_delay; 116 int repeat; 117 int debounce_interval; 118 }; 119 120 struct gpiokeys_softc 121 { 122 device_t sc_dev; 123 struct mtx sc_mtx; 124 struct gpiokey *sc_keys; 125 int sc_total_keys; 126 127 #ifdef EVDEV_SUPPORT 128 struct evdev_dev *sc_evdev; 129 #endif 130 keyboard_t sc_kbd; 131 keymap_t sc_keymap; 132 accentmap_t sc_accmap; 133 fkeytab_t sc_fkeymap[GPIOKEYS_GLOBAL_NFKEY]; 134 135 uint32_t sc_input[GPIOKEYS_GLOBAL_IN_BUF_SIZE]; /* input buffer */ 136 uint32_t sc_time_ms; 137 #define GPIOKEYS_GLOBAL_FLAG_POLLING 0x00000002 138 139 uint32_t sc_flags; /* flags */ 140 141 int sc_mode; /* input mode (K_XLATE,K_RAW,K_CODE) */ 142 int sc_state; /* shift/lock key state */ 143 int sc_accents; /* accent key index (> 0) */ 144 int sc_kbd_size; 145 146 uint16_t sc_inputs; 147 uint16_t sc_inputhead; 148 uint16_t sc_inputtail; 149 150 uint8_t sc_kbd_id; 151 }; 152 153 /* gpio-keys device */ 154 static int gpiokeys_probe(device_t); 155 static int gpiokeys_attach(device_t); 156 static int gpiokeys_detach(device_t); 157 158 /* kbd methods prototypes */ 159 static int gpiokeys_set_typematic(keyboard_t *, int); 160 static uint32_t gpiokeys_read_char(keyboard_t *, int); 161 static void gpiokeys_clear_state(keyboard_t *); 162 static int gpiokeys_ioctl(keyboard_t *, u_long, caddr_t); 163 static int gpiokeys_enable(keyboard_t *); 164 static int gpiokeys_disable(keyboard_t *); 165 static void gpiokeys_event_keyinput(struct gpiokeys_softc *); 166 167 static void 168 gpiokeys_put_key(struct gpiokeys_softc *sc, uint32_t key) 169 { 170 171 GPIOKEYS_ASSERT_LOCKED(sc); 172 173 if (sc->sc_inputs < GPIOKEYS_GLOBAL_IN_BUF_SIZE) { 174 sc->sc_input[sc->sc_inputtail] = key; 175 ++(sc->sc_inputs); 176 ++(sc->sc_inputtail); 177 if (sc->sc_inputtail >= GPIOKEYS_GLOBAL_IN_BUF_SIZE) { 178 sc->sc_inputtail = 0; 179 } 180 } else { 181 device_printf(sc->sc_dev, "input buffer is full\n"); 182 } 183 } 184 185 static void 186 gpiokeys_key_event(struct gpiokeys_softc *sc, struct gpiokey *key, int pressed) 187 { 188 uint32_t code; 189 190 GPIOKEYS_LOCK(sc); 191 #ifdef EVDEV_SUPPORT 192 if (key->evcode != GPIOKEY_NONE && 193 (evdev_rcpt_mask & EVDEV_RCPT_HW_KBD) != 0) { 194 evdev_push_key(sc->sc_evdev, key->evcode, pressed); 195 evdev_sync(sc->sc_evdev); 196 } 197 #endif 198 if (key->keycode != GPIOKEY_NONE) { 199 code = key->keycode & SCAN_KEYCODE_MASK; 200 if (!pressed) 201 code |= KEY_RELEASE; 202 203 if (key->keycode & SCAN_PREFIX_E0) 204 gpiokeys_put_key(sc, 0xe0); 205 else if (key->keycode & SCAN_PREFIX_E1) 206 gpiokeys_put_key(sc, 0xe1); 207 208 gpiokeys_put_key(sc, code); 209 } 210 GPIOKEYS_UNLOCK(sc); 211 212 if (key->keycode != GPIOKEY_NONE) 213 gpiokeys_event_keyinput(sc); 214 } 215 216 static void 217 gpiokey_autorepeat(void *arg) 218 { 219 struct gpiokey *key; 220 221 key = arg; 222 223 gpiokeys_key_event(key->parent_sc, key, 1); 224 225 callout_reset(&key->repeat_callout, key->repeat, 226 gpiokey_autorepeat, key); 227 } 228 229 static void 230 gpiokey_debounced_intr(void *arg) 231 { 232 struct gpiokey *key; 233 bool active; 234 235 key = arg; 236 237 gpio_pin_is_active(key->pin, &active); 238 if (active) { 239 gpiokeys_key_event(key->parent_sc, key, 1); 240 if (key->autorepeat) { 241 callout_reset(&key->repeat_callout, key->repeat_delay, 242 gpiokey_autorepeat, key); 243 } 244 } 245 else { 246 if (key->autorepeat && 247 callout_pending(&key->repeat_callout)) 248 callout_stop(&key->repeat_callout); 249 gpiokeys_key_event(key->parent_sc, key, 0); 250 } 251 } 252 253 static void 254 gpiokey_intr(void *arg) 255 { 256 struct gpiokey *key; 257 int debounce_ticks; 258 259 key = arg; 260 261 GPIOKEY_LOCK(key); 262 debounce_ticks = (hz * key->debounce_interval) / 1000; 263 if (debounce_ticks == 0) 264 debounce_ticks = 1; 265 if (!callout_pending(&key->debounce_callout)) 266 callout_reset(&key->debounce_callout, debounce_ticks, 267 gpiokey_debounced_intr, key); 268 GPIOKEY_UNLOCK(key); 269 } 270 271 static void 272 gpiokeys_attach_key(struct gpiokeys_softc *sc, phandle_t node, 273 struct gpiokey *key) 274 { 275 pcell_t prop; 276 char *name; 277 uint32_t code; 278 int err; 279 const char *key_name; 280 281 GPIOKEY_LOCK_INIT(key); 282 key->parent_sc = sc; 283 callout_init_mtx(&key->debounce_callout, &key->mtx, 0); 284 callout_init_mtx(&key->repeat_callout, &key->mtx, 0); 285 286 name = NULL; 287 if (OF_getprop_alloc(node, "label", (void **)&name) == -1) 288 OF_getprop_alloc(node, "name", (void **)&name); 289 290 if (name != NULL) 291 key_name = name; 292 else 293 key_name = "unknown"; 294 295 key->autorepeat = OF_hasprop(node, "autorepeat"); 296 297 key->repeat_delay = (hz * AUTOREPEAT_DELAY) / 1000; 298 if (key->repeat_delay == 0) 299 key->repeat_delay = 1; 300 301 key->repeat = (hz * AUTOREPEAT_REPEAT) / 1000; 302 if (key->repeat == 0) 303 key->repeat = 1; 304 305 if ((OF_getprop(node, "debounce-interval", &prop, sizeof(prop))) > 0) 306 key->debounce_interval = fdt32_to_cpu(prop); 307 else 308 key->debounce_interval = 5; 309 310 if ((OF_getprop(node, "freebsd,code", &prop, sizeof(prop))) > 0) 311 key->keycode = fdt32_to_cpu(prop); 312 else if ((OF_getprop(node, "linux,code", &prop, sizeof(prop))) > 0) { 313 code = fdt32_to_cpu(prop); 314 key->keycode = gpiokey_map_linux_code(code); 315 if (key->keycode == GPIOKEY_NONE) 316 device_printf(sc->sc_dev, "<%s> failed to map linux,code value 0x%x\n", 317 key_name, code); 318 #ifdef EVDEV_SUPPORT 319 key->evcode = code; 320 evdev_support_key(sc->sc_evdev, code); 321 #endif 322 } 323 else 324 device_printf(sc->sc_dev, "<%s> no linux,code or freebsd,code property\n", 325 key_name); 326 327 err = gpio_pin_get_by_ofw_idx(sc->sc_dev, node, 0, &key->pin); 328 if (err) { 329 device_printf(sc->sc_dev, "<%s> failed to map pin\n", key_name); 330 if (name) 331 OF_prop_free(name); 332 return; 333 } 334 335 key->irq_res = gpio_alloc_intr_resource(sc->sc_dev, &key->irq_rid, 336 RF_ACTIVE, key->pin, GPIO_INTR_EDGE_BOTH); 337 if (!key->irq_res) { 338 device_printf(sc->sc_dev, "<%s> cannot allocate interrupt\n", key_name); 339 gpio_pin_release(key->pin); 340 key->pin = NULL; 341 if (name) 342 OF_prop_free(name); 343 return; 344 } 345 346 if (bus_setup_intr(sc->sc_dev, key->irq_res, INTR_TYPE_MISC | INTR_MPSAFE, 347 NULL, gpiokey_intr, key, 348 &key->intr_hl) != 0) { 349 device_printf(sc->sc_dev, "<%s> unable to setup the irq handler\n", key_name); 350 bus_release_resource(sc->sc_dev, SYS_RES_IRQ, key->irq_rid, 351 key->irq_res); 352 gpio_pin_release(key->pin); 353 key->pin = NULL; 354 key->irq_res = NULL; 355 if (name) 356 OF_prop_free(name); 357 return; 358 } 359 360 if (bootverbose) 361 device_printf(sc->sc_dev, "<%s> code=%08x, autorepeat=%d, "\ 362 "repeat=%d, repeat_delay=%d\n", key_name, key->keycode, 363 key->autorepeat, key->repeat, key->repeat_delay); 364 365 if (name) 366 OF_prop_free(name); 367 } 368 369 static void 370 gpiokeys_detach_key(struct gpiokeys_softc *sc, struct gpiokey *key) 371 { 372 373 GPIOKEY_LOCK(key); 374 if (key->intr_hl) 375 bus_teardown_intr(sc->sc_dev, key->irq_res, key->intr_hl); 376 if (key->irq_res) 377 bus_release_resource(sc->sc_dev, SYS_RES_IRQ, 378 key->irq_rid, key->irq_res); 379 if (callout_pending(&key->repeat_callout)) 380 callout_drain(&key->repeat_callout); 381 if (callout_pending(&key->debounce_callout)) 382 callout_drain(&key->debounce_callout); 383 if (key->pin) 384 gpio_pin_release(key->pin); 385 GPIOKEY_UNLOCK(key); 386 GPIOKEY_LOCK_DESTROY(key); 387 } 388 389 static int 390 gpiokeys_probe(device_t dev) 391 { 392 if (!ofw_bus_is_compatible(dev, "gpio-keys")) 393 return (ENXIO); 394 395 device_set_desc(dev, "GPIO keyboard"); 396 397 return (0); 398 } 399 400 static int 401 gpiokeys_attach(device_t dev) 402 { 403 struct gpiokeys_softc *sc; 404 keyboard_t *kbd; 405 #ifdef EVDEV_SUPPORT 406 char *name; 407 #endif 408 phandle_t keys, child; 409 int total_keys; 410 int unit; 411 412 if ((keys = ofw_bus_get_node(dev)) == -1) 413 return (ENXIO); 414 415 sc = device_get_softc(dev); 416 sc->sc_dev = dev; 417 kbd = &sc->sc_kbd; 418 419 GPIOKEYS_LOCK_INIT(sc); 420 unit = device_get_unit(dev); 421 kbd_init_struct(kbd, KBD_DRIVER_NAME, KB_OTHER, unit, 0, 0, 0); 422 423 kbd->kb_data = (void *)sc; 424 sc->sc_mode = K_XLATE; 425 426 sc->sc_keymap = key_map; 427 sc->sc_accmap = accent_map; 428 429 kbd_set_maps(kbd, &sc->sc_keymap, &sc->sc_accmap, 430 sc->sc_fkeymap, GPIOKEYS_GLOBAL_NFKEY); 431 432 KBD_FOUND_DEVICE(kbd); 433 434 gpiokeys_clear_state(kbd); 435 436 KBD_PROBE_DONE(kbd); 437 438 KBD_INIT_DONE(kbd); 439 440 if (kbd_register(kbd) < 0) { 441 goto detach; 442 } 443 444 KBD_CONFIG_DONE(kbd); 445 446 gpiokeys_enable(kbd); 447 448 #ifdef KBD_INSTALL_CDEV 449 if (kbd_attach(kbd)) { 450 goto detach; 451 } 452 #endif 453 454 if (bootverbose) { 455 kbdd_diag(kbd, 1); 456 } 457 458 #ifdef EVDEV_SUPPORT 459 sc->sc_evdev = evdev_alloc(); 460 evdev_set_name(sc->sc_evdev, device_get_desc(dev)); 461 462 OF_getprop_alloc(keys, "name", (void **)&name); 463 evdev_set_phys(sc->sc_evdev, name != NULL ? name : "unknown"); 464 OF_prop_free(name); 465 466 evdev_set_id(sc->sc_evdev, BUS_VIRTUAL, 0, 0, 0); 467 evdev_support_event(sc->sc_evdev, EV_SYN); 468 evdev_support_event(sc->sc_evdev, EV_KEY); 469 #endif 470 471 total_keys = 0; 472 473 /* Traverse the 'gpio-keys' node and count keys */ 474 for (child = OF_child(keys); child != 0; child = OF_peer(child)) { 475 if (!OF_hasprop(child, "gpios")) 476 continue; 477 total_keys++; 478 } 479 480 if (total_keys) { 481 sc->sc_keys = malloc(sizeof(struct gpiokey) * total_keys, 482 M_DEVBUF, M_WAITOK | M_ZERO); 483 484 sc->sc_total_keys = 0; 485 /* Traverse the 'gpio-keys' node and count keys */ 486 for (child = OF_child(keys); child != 0; child = OF_peer(child)) { 487 if (!OF_hasprop(child, "gpios")) 488 continue; 489 gpiokeys_attach_key(sc, child ,&sc->sc_keys[sc->sc_total_keys]); 490 sc->sc_total_keys++; 491 } 492 } 493 494 #ifdef EVDEV_SUPPORT 495 if (evdev_register_mtx(sc->sc_evdev, &sc->sc_mtx) != 0) { 496 device_printf(dev, "failed to register evdev device\n"); 497 goto detach; 498 } 499 #endif 500 501 return (0); 502 503 detach: 504 gpiokeys_detach(dev); 505 return (ENXIO); 506 } 507 508 static int 509 gpiokeys_detach(device_t dev) 510 { 511 struct gpiokeys_softc *sc; 512 keyboard_t *kbd; 513 int i; 514 515 sc = device_get_softc(dev); 516 517 for (i = 0; i < sc->sc_total_keys; i++) 518 gpiokeys_detach_key(sc, &sc->sc_keys[i]); 519 520 kbd = kbd_get_keyboard(kbd_find_keyboard(KBD_DRIVER_NAME, 521 device_get_unit(dev))); 522 523 #ifdef KBD_INSTALL_CDEV 524 kbd_detach(kbd); 525 #endif 526 kbd_unregister(kbd); 527 528 #ifdef EVDEV_SUPPORT 529 evdev_free(sc->sc_evdev); 530 #endif 531 532 GPIOKEYS_LOCK_DESTROY(sc); 533 if (sc->sc_keys) 534 free(sc->sc_keys, M_DEVBUF); 535 536 return (0); 537 } 538 539 /* early keyboard probe, not supported */ 540 static int 541 gpiokeys_configure(int flags) 542 { 543 return (0); 544 } 545 546 /* detect a keyboard, not used */ 547 static int 548 gpiokeys__probe(int unit, void *arg, int flags) 549 { 550 return (ENXIO); 551 } 552 553 /* reset and initialize the device, not used */ 554 static int 555 gpiokeys_init(int unit, keyboard_t **kbdp, void *arg, int flags) 556 { 557 return (ENXIO); 558 } 559 560 /* test the interface to the device, not used */ 561 static int 562 gpiokeys_test_if(keyboard_t *kbd) 563 { 564 return (0); 565 } 566 567 /* finish using this keyboard, not used */ 568 static int 569 gpiokeys_term(keyboard_t *kbd) 570 { 571 return (ENXIO); 572 } 573 574 /* keyboard interrupt routine, not used */ 575 static int 576 gpiokeys_intr(keyboard_t *kbd, void *arg) 577 { 578 return (0); 579 } 580 581 /* lock the access to the keyboard, not used */ 582 static int 583 gpiokeys_lock(keyboard_t *kbd, int lock) 584 { 585 return (1); 586 } 587 588 /* 589 * Enable the access to the device; until this function is called, 590 * the client cannot read from the keyboard. 591 */ 592 static int 593 gpiokeys_enable(keyboard_t *kbd) 594 { 595 struct gpiokeys_softc *sc; 596 597 sc = kbd->kb_data; 598 GPIOKEYS_LOCK(sc); 599 KBD_ACTIVATE(kbd); 600 GPIOKEYS_UNLOCK(sc); 601 602 return (0); 603 } 604 605 /* disallow the access to the device */ 606 static int 607 gpiokeys_disable(keyboard_t *kbd) 608 { 609 struct gpiokeys_softc *sc; 610 611 sc = kbd->kb_data; 612 GPIOKEYS_LOCK(sc); 613 KBD_DEACTIVATE(kbd); 614 GPIOKEYS_UNLOCK(sc); 615 616 return (0); 617 } 618 619 static void 620 gpiokeys_do_poll(struct gpiokeys_softc *sc, uint8_t wait) 621 { 622 623 KASSERT((sc->sc_flags & GPIOKEYS_GLOBAL_FLAG_POLLING) != 0, 624 ("gpiokeys_do_poll called when not polling\n")); 625 626 GPIOKEYS_ASSERT_LOCKED(sc); 627 628 if (!kdb_active && !SCHEDULER_STOPPED()) { 629 while (sc->sc_inputs == 0) { 630 kern_yield(PRI_UNCHANGED); 631 if (!wait) 632 break; 633 } 634 return; 635 } 636 637 while ((sc->sc_inputs == 0) && wait) { 638 printf("POLL!\n"); 639 } 640 } 641 642 /* check if data is waiting */ 643 static int 644 gpiokeys_check(keyboard_t *kbd) 645 { 646 struct gpiokeys_softc *sc = kbd->kb_data; 647 648 GPIOKEYS_ASSERT_LOCKED(sc); 649 650 if (!KBD_IS_ACTIVE(kbd)) 651 return (0); 652 653 if (sc->sc_flags & GPIOKEYS_GLOBAL_FLAG_POLLING) 654 gpiokeys_do_poll(sc, 0); 655 656 if (sc->sc_inputs > 0) { 657 return (1); 658 } 659 return (0); 660 } 661 662 /* check if char is waiting */ 663 static int 664 gpiokeys_check_char_locked(keyboard_t *kbd) 665 { 666 if (!KBD_IS_ACTIVE(kbd)) 667 return (0); 668 669 return (gpiokeys_check(kbd)); 670 } 671 672 static int 673 gpiokeys_check_char(keyboard_t *kbd) 674 { 675 int result; 676 struct gpiokeys_softc *sc = kbd->kb_data; 677 678 GPIOKEYS_LOCK(sc); 679 result = gpiokeys_check_char_locked(kbd); 680 GPIOKEYS_UNLOCK(sc); 681 682 return (result); 683 } 684 685 static int32_t 686 gpiokeys_get_key(struct gpiokeys_softc *sc, uint8_t wait) 687 { 688 int32_t c; 689 690 KASSERT((!kdb_active && !SCHEDULER_STOPPED()) 691 || (sc->sc_flags & GPIOKEYS_GLOBAL_FLAG_POLLING) != 0, 692 ("not polling in kdb or panic\n")); 693 694 GPIOKEYS_ASSERT_LOCKED(sc); 695 696 if (sc->sc_flags & GPIOKEYS_GLOBAL_FLAG_POLLING) 697 gpiokeys_do_poll(sc, wait); 698 699 if (sc->sc_inputs == 0) { 700 c = -1; 701 } else { 702 c = sc->sc_input[sc->sc_inputhead]; 703 --(sc->sc_inputs); 704 ++(sc->sc_inputhead); 705 if (sc->sc_inputhead >= GPIOKEYS_GLOBAL_IN_BUF_SIZE) { 706 sc->sc_inputhead = 0; 707 } 708 } 709 710 return (c); 711 } 712 713 /* read one byte from the keyboard if it's allowed */ 714 static int 715 gpiokeys_read(keyboard_t *kbd, int wait) 716 { 717 struct gpiokeys_softc *sc = kbd->kb_data; 718 int32_t keycode; 719 720 if (!KBD_IS_ACTIVE(kbd)) 721 return (-1); 722 723 /* XXX */ 724 keycode = gpiokeys_get_key(sc, (wait == FALSE) ? 0 : 1); 725 if (!KBD_IS_ACTIVE(kbd) || (keycode == -1)) 726 return (-1); 727 728 ++(kbd->kb_count); 729 730 return (keycode); 731 } 732 733 /* read char from the keyboard */ 734 static uint32_t 735 gpiokeys_read_char_locked(keyboard_t *kbd, int wait) 736 { 737 struct gpiokeys_softc *sc = kbd->kb_data; 738 uint32_t action; 739 uint32_t keycode; 740 741 if (!KBD_IS_ACTIVE(kbd)) 742 return (NOKEY); 743 744 next_code: 745 746 /* see if there is something in the keyboard port */ 747 /* XXX */ 748 keycode = gpiokeys_get_key(sc, (wait == FALSE) ? 0 : 1); 749 ++kbd->kb_count; 750 751 /* return the byte as is for the K_RAW mode */ 752 if (sc->sc_mode == K_RAW) { 753 return (keycode); 754 } 755 756 /* return the key code in the K_CODE mode */ 757 /* XXX: keycode |= SCAN_RELEASE; */ 758 759 if (sc->sc_mode == K_CODE) { 760 return (keycode); 761 } 762 763 /* keycode to key action */ 764 action = genkbd_keyaction(kbd, SCAN_CHAR(keycode), 765 (keycode & SCAN_RELEASE), 766 &sc->sc_state, &sc->sc_accents); 767 if (action == NOKEY) { 768 goto next_code; 769 } 770 771 return (action); 772 } 773 774 /* Currently wait is always false. */ 775 static uint32_t 776 gpiokeys_read_char(keyboard_t *kbd, int wait) 777 { 778 uint32_t keycode; 779 struct gpiokeys_softc *sc = kbd->kb_data; 780 781 GPIOKEYS_LOCK(sc); 782 keycode = gpiokeys_read_char_locked(kbd, wait); 783 GPIOKEYS_UNLOCK(sc); 784 785 return (keycode); 786 } 787 788 /* some useful control functions */ 789 static int 790 gpiokeys_ioctl_locked(keyboard_t *kbd, u_long cmd, caddr_t arg) 791 { 792 struct gpiokeys_softc *sc = kbd->kb_data; 793 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \ 794 defined(COMPAT_FREEBSD4) || defined(COMPAT_43) 795 int ival; 796 797 #endif 798 799 switch (cmd) { 800 case KDGKBMODE: /* get keyboard mode */ 801 *(int *)arg = sc->sc_mode; 802 break; 803 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \ 804 defined(COMPAT_FREEBSD4) || defined(COMPAT_43) 805 case _IO('K', 7): 806 ival = IOCPARM_IVAL(arg); 807 arg = (caddr_t)&ival; 808 /* FALLTHROUGH */ 809 #endif 810 case KDSKBMODE: /* set keyboard mode */ 811 switch (*(int *)arg) { 812 case K_XLATE: 813 if (sc->sc_mode != K_XLATE) { 814 /* make lock key state and LED state match */ 815 sc->sc_state &= ~LOCK_MASK; 816 sc->sc_state |= KBD_LED_VAL(kbd); 817 } 818 /* FALLTHROUGH */ 819 case K_RAW: 820 case K_CODE: 821 if (sc->sc_mode != *(int *)arg) { 822 if ((sc->sc_flags & GPIOKEYS_GLOBAL_FLAG_POLLING) == 0) 823 gpiokeys_clear_state(kbd); 824 sc->sc_mode = *(int *)arg; 825 } 826 break; 827 default: 828 return (EINVAL); 829 } 830 break; 831 832 case KDGETLED: /* get keyboard LED */ 833 *(int *)arg = KBD_LED_VAL(kbd); 834 break; 835 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \ 836 defined(COMPAT_FREEBSD4) || defined(COMPAT_43) 837 case _IO('K', 66): 838 ival = IOCPARM_IVAL(arg); 839 arg = (caddr_t)&ival; 840 /* FALLTHROUGH */ 841 #endif 842 case KDSETLED: /* set keyboard LED */ 843 KBD_LED_VAL(kbd) = *(int *)arg; 844 break; 845 case KDGKBSTATE: /* get lock key state */ 846 *(int *)arg = sc->sc_state & LOCK_MASK; 847 break; 848 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \ 849 defined(COMPAT_FREEBSD4) || defined(COMPAT_43) 850 case _IO('K', 20): 851 ival = IOCPARM_IVAL(arg); 852 arg = (caddr_t)&ival; 853 /* FALLTHROUGH */ 854 #endif 855 case KDSKBSTATE: /* set lock key state */ 856 if (*(int *)arg & ~LOCK_MASK) { 857 return (EINVAL); 858 } 859 sc->sc_state &= ~LOCK_MASK; 860 sc->sc_state |= *(int *)arg; 861 return (0); 862 863 case KDSETREPEAT: /* set keyboard repeat rate (new 864 * interface) */ 865 if (!KBD_HAS_DEVICE(kbd)) { 866 return (0); 867 } 868 if (((int *)arg)[1] < 0) { 869 return (EINVAL); 870 } 871 if (((int *)arg)[0] < 0) { 872 return (EINVAL); 873 } 874 if (((int *)arg)[0] < 200) /* fastest possible value */ 875 kbd->kb_delay1 = 200; 876 else 877 kbd->kb_delay1 = ((int *)arg)[0]; 878 kbd->kb_delay2 = ((int *)arg)[1]; 879 return (0); 880 881 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \ 882 defined(COMPAT_FREEBSD4) || defined(COMPAT_43) 883 case _IO('K', 67): 884 ival = IOCPARM_IVAL(arg); 885 arg = (caddr_t)&ival; 886 /* FALLTHROUGH */ 887 #endif 888 case KDSETRAD: /* set keyboard repeat rate (old 889 * interface) */ 890 return (gpiokeys_set_typematic(kbd, *(int *)arg)); 891 892 case PIO_KEYMAP: /* set keyboard translation table */ 893 case OPIO_KEYMAP: /* set keyboard translation table 894 * (compat) */ 895 case PIO_KEYMAPENT: /* set keyboard translation table 896 * entry */ 897 case PIO_DEADKEYMAP: /* set accent key translation table */ 898 sc->sc_accents = 0; 899 /* FALLTHROUGH */ 900 default: 901 return (genkbd_commonioctl(kbd, cmd, arg)); 902 } 903 904 return (0); 905 } 906 907 static int 908 gpiokeys_ioctl(keyboard_t *kbd, u_long cmd, caddr_t arg) 909 { 910 int result; 911 struct gpiokeys_softc *sc; 912 913 sc = kbd->kb_data; 914 /* 915 * XXX Check if someone is calling us from a critical section: 916 */ 917 if (curthread->td_critnest != 0) 918 return (EDEADLK); 919 920 GPIOKEYS_LOCK(sc); 921 result = gpiokeys_ioctl_locked(kbd, cmd, arg); 922 GPIOKEYS_UNLOCK(sc); 923 924 return (result); 925 } 926 927 /* clear the internal state of the keyboard */ 928 static void 929 gpiokeys_clear_state(keyboard_t *kbd) 930 { 931 struct gpiokeys_softc *sc = kbd->kb_data; 932 933 sc->sc_flags &= ~(GPIOKEYS_GLOBAL_FLAG_POLLING); 934 sc->sc_state &= LOCK_MASK; /* preserve locking key state */ 935 sc->sc_accents = 0; 936 } 937 938 /* get the internal state, not used */ 939 static int 940 gpiokeys_get_state(keyboard_t *kbd, void *buf, size_t len) 941 { 942 return (len == 0) ? 1 : -1; 943 } 944 945 /* set the internal state, not used */ 946 static int 947 gpiokeys_set_state(keyboard_t *kbd, void *buf, size_t len) 948 { 949 return (EINVAL); 950 } 951 952 static int 953 gpiokeys_poll(keyboard_t *kbd, int on) 954 { 955 struct gpiokeys_softc *sc = kbd->kb_data; 956 957 GPIOKEYS_LOCK(sc); 958 if (on) 959 sc->sc_flags |= GPIOKEYS_GLOBAL_FLAG_POLLING; 960 else 961 sc->sc_flags &= ~GPIOKEYS_GLOBAL_FLAG_POLLING; 962 GPIOKEYS_UNLOCK(sc); 963 964 return (0); 965 } 966 967 static int 968 gpiokeys_set_typematic(keyboard_t *kbd, int code) 969 { 970 static const int delays[] = {250, 500, 750, 1000}; 971 static const int rates[] = {34, 38, 42, 46, 50, 55, 59, 63, 972 68, 76, 84, 92, 100, 110, 118, 126, 973 136, 152, 168, 184, 200, 220, 236, 252, 974 272, 304, 336, 368, 400, 440, 472, 504}; 975 976 if (code & ~0x7f) { 977 return (EINVAL); 978 } 979 kbd->kb_delay1 = delays[(code >> 5) & 3]; 980 kbd->kb_delay2 = rates[code & 0x1f]; 981 return (0); 982 } 983 984 static void 985 gpiokeys_event_keyinput(struct gpiokeys_softc *sc) 986 { 987 int c; 988 989 if ((sc->sc_flags & GPIOKEYS_GLOBAL_FLAG_POLLING) != 0) 990 return; 991 992 if (KBD_IS_ACTIVE(&sc->sc_kbd) && 993 KBD_IS_BUSY(&sc->sc_kbd)) { 994 /* let the callback function process the input */ 995 (sc->sc_kbd.kb_callback.kc_func) (&sc->sc_kbd, KBDIO_KEYINPUT, 996 sc->sc_kbd.kb_callback.kc_arg); 997 } else { 998 /* read and discard the input, no one is waiting for it */ 999 do { 1000 c = gpiokeys_read_char(&sc->sc_kbd, 0); 1001 } while (c != NOKEY); 1002 } 1003 } 1004 1005 static keyboard_switch_t gpiokeyssw = { 1006 .probe = &gpiokeys__probe, 1007 .init = &gpiokeys_init, 1008 .term = &gpiokeys_term, 1009 .intr = &gpiokeys_intr, 1010 .test_if = &gpiokeys_test_if, 1011 .enable = &gpiokeys_enable, 1012 .disable = &gpiokeys_disable, 1013 .read = &gpiokeys_read, 1014 .check = &gpiokeys_check, 1015 .read_char = &gpiokeys_read_char, 1016 .check_char = &gpiokeys_check_char, 1017 .ioctl = &gpiokeys_ioctl, 1018 .lock = &gpiokeys_lock, 1019 .clear_state = &gpiokeys_clear_state, 1020 .get_state = &gpiokeys_get_state, 1021 .set_state = &gpiokeys_set_state, 1022 .poll = &gpiokeys_poll, 1023 }; 1024 1025 KEYBOARD_DRIVER(gpiokeys, gpiokeyssw, gpiokeys_configure); 1026 1027 static int 1028 gpiokeys_driver_load(module_t mod, int what, void *arg) 1029 { 1030 switch (what) { 1031 case MOD_LOAD: 1032 kbd_add_driver(&gpiokeys_kbd_driver); 1033 break; 1034 case MOD_UNLOAD: 1035 kbd_delete_driver(&gpiokeys_kbd_driver); 1036 break; 1037 } 1038 return (0); 1039 } 1040 1041 static device_method_t gpiokeys_methods[] = { 1042 DEVMETHOD(device_probe, gpiokeys_probe), 1043 DEVMETHOD(device_attach, gpiokeys_attach), 1044 DEVMETHOD(device_detach, gpiokeys_detach), 1045 1046 DEVMETHOD_END 1047 }; 1048 1049 static driver_t gpiokeys_driver = { 1050 "gpiokeys", 1051 gpiokeys_methods, 1052 sizeof(struct gpiokeys_softc), 1053 }; 1054 1055 DRIVER_MODULE(gpiokeys, simplebus, gpiokeys_driver, gpiokeys_driver_load, NULL); 1056 MODULE_VERSION(gpiokeys, 1); 1057