The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/dev/evdev/evdev.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * Copyright (c) 2014 Jakub Wojciech Klama <jceel@FreeBSD.org>
    3  * Copyright (c) 2015-2016 Vladimir Kondratyev <wulf@FreeBSD.org>
    4  * All rights reserved.
    5  *
    6  * Redistribution and use in source and binary forms, with or without
    7  * modification, are permitted provided that the following conditions
    8  * are met:
    9  * 1. Redistributions of source code must retain the above copyright
   10  *    notice, this list of conditions and the following disclaimer.
   11  * 2. Redistributions in binary form must reproduce the above copyright
   12  *    notice, this list of conditions and the following disclaimer in the
   13  *    documentation and/or other materials provided with the distribution.
   14  *
   15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
   19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   25  * SUCH DAMAGE.
   26  *
   27  * $FreeBSD$
   28  */
   29 
   30 #include "opt_evdev.h"
   31 
   32 #include <sys/param.h>
   33 #include <sys/bitstring.h>
   34 #include <sys/conf.h>
   35 #include <sys/kdb.h>
   36 #include <sys/kernel.h>
   37 #include <sys/malloc.h>
   38 #include <sys/module.h>
   39 #include <sys/proc.h>
   40 #include <sys/sysctl.h>
   41 #include <sys/systm.h>
   42 
   43 #include <dev/evdev/evdev.h>
   44 #include <dev/evdev/evdev_private.h>
   45 #include <dev/evdev/input.h>
   46 
   47 #ifdef EVDEV_DEBUG
   48 #define debugf(evdev, fmt, args...)     printf("evdev: " fmt "\n", ##args)
   49 #else
   50 #define debugf(evdev, fmt, args...)
   51 #endif
   52 
   53 #ifdef FEATURE
   54 FEATURE(evdev, "Input event devices support");
   55 #ifdef EVDEV_SUPPORT
   56 FEATURE(evdev_support, "Evdev support in hybrid drivers");
   57 #endif
   58 #endif
   59 
   60 enum evdev_sparse_result
   61 {
   62         EV_SKIP_EVENT,          /* Event value not changed */
   63         EV_REPORT_EVENT,        /* Event value changed */
   64         EV_REPORT_MT_SLOT,      /* Event value and MT slot number changed */
   65 };
   66 
   67 MALLOC_DEFINE(M_EVDEV, "evdev", "evdev memory");
   68 
   69 /* adb keyboard driver used on powerpc does not support evdev yet */
   70 #if defined(__powerpc__) && !defined(__powerpc64__)
   71 int evdev_rcpt_mask = EVDEV_RCPT_KBDMUX | EVDEV_RCPT_HW_MOUSE;
   72 #else
   73 int evdev_rcpt_mask = EVDEV_RCPT_HW_MOUSE | EVDEV_RCPT_HW_KBD;
   74 #endif
   75 int evdev_sysmouse_t_axis = 0;
   76 
   77 SYSCTL_NODE(_kern, OID_AUTO, evdev, CTLFLAG_RW, 0, "Evdev args");
   78 #ifdef EVDEV_SUPPORT
   79 SYSCTL_INT(_kern_evdev, OID_AUTO, rcpt_mask, CTLFLAG_RWTUN, &evdev_rcpt_mask, 0,
   80     "Who is receiving events: bit0 - sysmouse, bit1 - kbdmux, "
   81     "bit2 - mouse hardware, bit3 - keyboard hardware");
   82 SYSCTL_INT(_kern_evdev, OID_AUTO, sysmouse_t_axis, CTLFLAG_RWTUN,
   83     &evdev_sysmouse_t_axis, 0, "Extract T-axis from 0-none, 1-ums, 2-psm");
   84 #endif
   85 SYSCTL_NODE(_kern_evdev, OID_AUTO, input, CTLFLAG_RD, 0,
   86     "Evdev input devices");
   87 
   88 static void evdev_start_repeat(struct evdev_dev *, uint16_t);
   89 static void evdev_stop_repeat(struct evdev_dev *);
   90 static int evdev_check_event(struct evdev_dev *, uint16_t, uint16_t, int32_t);
   91 
   92 static inline void
   93 bit_change(bitstr_t *bitstr, int bit, int value)
   94 {
   95         if (value)
   96                 bit_set(bitstr, bit);
   97         else
   98                 bit_clear(bitstr, bit);
   99 }
  100 
  101 struct evdev_dev *
  102 evdev_alloc(void)
  103 {
  104 
  105         return malloc(sizeof(struct evdev_dev), M_EVDEV, M_WAITOK | M_ZERO);
  106 }
  107 
  108 void
  109 evdev_free(struct evdev_dev *evdev)
  110 {
  111 
  112         if (evdev != NULL && evdev->ev_cdev != NULL &&
  113             evdev->ev_cdev->si_drv1 != NULL)
  114                 evdev_unregister(evdev);
  115 
  116         free(evdev, M_EVDEV);
  117 }
  118 
  119 static struct input_absinfo *
  120 evdev_alloc_absinfo(void)
  121 {
  122 
  123         return (malloc(sizeof(struct input_absinfo) * ABS_CNT, M_EVDEV,
  124             M_WAITOK | M_ZERO));
  125 }
  126 
  127 static void
  128 evdev_free_absinfo(struct input_absinfo *absinfo)
  129 {
  130 
  131         free(absinfo, M_EVDEV);
  132 }
  133 
  134 int
  135 evdev_set_report_size(struct evdev_dev *evdev, size_t report_size)
  136 {
  137         if (report_size > KEY_CNT + REL_CNT + ABS_CNT + MAX_MT_SLOTS * MT_CNT +
  138             MSC_CNT + LED_CNT + SND_CNT + SW_CNT + FF_CNT)
  139                 return (EINVAL);
  140 
  141         evdev->ev_report_size = report_size;
  142         return (0);
  143 }
  144 
  145 static size_t
  146 evdev_estimate_report_size(struct evdev_dev *evdev)
  147 {
  148         size_t size = 0;
  149         int res;
  150 
  151         /*
  152          * Keyboards generate one event per report but other devices with
  153          * buttons like mouses can report events simultaneously
  154          */
  155         bit_ffs_at(evdev->ev_key_flags, KEY_OK, KEY_CNT - KEY_OK, &res);
  156         if (res == -1)
  157                 bit_ffs(evdev->ev_key_flags, BTN_MISC, &res);
  158         size += (res != -1);
  159         bit_count(evdev->ev_key_flags, BTN_MISC, KEY_OK - BTN_MISC, &res);
  160         size += res;
  161 
  162         /* All relative axes can be reported simultaneously */
  163         bit_count(evdev->ev_rel_flags, 0, REL_CNT, &res);
  164         size += res;
  165 
  166         /*
  167          * All absolute axes can be reported simultaneously.
  168          * Multitouch axes can be reported ABS_MT_SLOT times
  169          */
  170         if (evdev->ev_absinfo != NULL) {
  171                 bit_count(evdev->ev_abs_flags, 0, ABS_CNT, &res);
  172                 size += res;
  173                 bit_count(evdev->ev_abs_flags, ABS_MT_FIRST, MT_CNT, &res);
  174                 if (res > 0) {
  175                         res++;  /* ABS_MT_SLOT or SYN_MT_REPORT */
  176                         if (bit_test(evdev->ev_abs_flags, ABS_MT_SLOT))
  177                                 /* MT type B */
  178                                 size += res * MAXIMAL_MT_SLOT(evdev);
  179                         else
  180                                 /* MT type A */
  181                                 size += res * (MAX_MT_REPORTS - 1);
  182                 }
  183         }
  184 
  185         /* All misc events can be reported simultaneously */
  186         bit_count(evdev->ev_msc_flags, 0, MSC_CNT, &res);
  187         size += res;
  188 
  189         /* All leds can be reported simultaneously */
  190         bit_count(evdev->ev_led_flags, 0, LED_CNT, &res);
  191         size += res;
  192 
  193         /* Assume other events are generated once per report */
  194         bit_ffs(evdev->ev_snd_flags, SND_CNT, &res);
  195         size += (res != -1);
  196 
  197         bit_ffs(evdev->ev_sw_flags, SW_CNT, &res);
  198         size += (res != -1);
  199 
  200         /* XXX: FF part is not implemented yet */
  201 
  202         size++;         /* SYN_REPORT */
  203         return (size);
  204 }
  205 
  206 static void
  207 evdev_sysctl_create(struct evdev_dev *evdev)
  208 {
  209         struct sysctl_oid *ev_sysctl_tree;
  210         char ev_unit_str[8];
  211 
  212         snprintf(ev_unit_str, sizeof(ev_unit_str), "%d", evdev->ev_unit);
  213         sysctl_ctx_init(&evdev->ev_sysctl_ctx);
  214 
  215         ev_sysctl_tree = SYSCTL_ADD_NODE_WITH_LABEL(&evdev->ev_sysctl_ctx,
  216             SYSCTL_STATIC_CHILDREN(_kern_evdev_input), OID_AUTO,
  217             ev_unit_str, CTLFLAG_RD, NULL, "", "device index");
  218 
  219         SYSCTL_ADD_STRING(&evdev->ev_sysctl_ctx,
  220             SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "name", CTLFLAG_RD,
  221             evdev->ev_name, 0,
  222             "Input device name");
  223 
  224         SYSCTL_ADD_STRUCT(&evdev->ev_sysctl_ctx,
  225             SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "id", CTLFLAG_RD,
  226             &evdev->ev_id, input_id,
  227             "Input device identification");
  228 
  229         /* ioctl returns ENOENT if phys is not set. sysctl returns "" here */
  230         SYSCTL_ADD_STRING(&evdev->ev_sysctl_ctx,
  231             SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "phys", CTLFLAG_RD,
  232             evdev->ev_shortname, 0,
  233             "Input device short name");
  234 
  235         /* ioctl returns ENOENT if uniq is not set. sysctl returns "" here */
  236         SYSCTL_ADD_STRING(&evdev->ev_sysctl_ctx,
  237             SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "uniq", CTLFLAG_RD,
  238             evdev->ev_serial, 0,
  239             "Input device unique number");
  240 
  241         SYSCTL_ADD_OPAQUE(&evdev->ev_sysctl_ctx,
  242             SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "props", CTLFLAG_RD,
  243             evdev->ev_prop_flags, sizeof(evdev->ev_prop_flags), "",
  244             "Input device properties");
  245 
  246         SYSCTL_ADD_OPAQUE(&evdev->ev_sysctl_ctx,
  247             SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "type_bits", CTLFLAG_RD,
  248             evdev->ev_type_flags, sizeof(evdev->ev_type_flags), "",
  249             "Input device supported events types");
  250 
  251         SYSCTL_ADD_OPAQUE(&evdev->ev_sysctl_ctx,
  252             SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "key_bits", CTLFLAG_RD,
  253             evdev->ev_key_flags, sizeof(evdev->ev_key_flags),
  254             "", "Input device supported keys");
  255 
  256         SYSCTL_ADD_OPAQUE(&evdev->ev_sysctl_ctx,
  257             SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "rel_bits", CTLFLAG_RD,
  258             evdev->ev_rel_flags, sizeof(evdev->ev_rel_flags), "",
  259             "Input device supported relative events");
  260 
  261         SYSCTL_ADD_OPAQUE(&evdev->ev_sysctl_ctx,
  262             SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "abs_bits", CTLFLAG_RD,
  263             evdev->ev_abs_flags, sizeof(evdev->ev_abs_flags), "",
  264             "Input device supported absolute events");
  265 
  266         SYSCTL_ADD_OPAQUE(&evdev->ev_sysctl_ctx,
  267             SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "msc_bits", CTLFLAG_RD,
  268             evdev->ev_msc_flags, sizeof(evdev->ev_msc_flags), "",
  269             "Input device supported miscellaneous events");
  270 
  271         SYSCTL_ADD_OPAQUE(&evdev->ev_sysctl_ctx,
  272             SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "led_bits", CTLFLAG_RD,
  273             evdev->ev_led_flags, sizeof(evdev->ev_led_flags), "",
  274             "Input device supported LED events");
  275 
  276         SYSCTL_ADD_OPAQUE(&evdev->ev_sysctl_ctx,
  277             SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "snd_bits", CTLFLAG_RD,
  278             evdev->ev_snd_flags, sizeof(evdev->ev_snd_flags), "",
  279             "Input device supported sound events");
  280 
  281         SYSCTL_ADD_OPAQUE(&evdev->ev_sysctl_ctx,
  282             SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "sw_bits", CTLFLAG_RD,
  283             evdev->ev_sw_flags, sizeof(evdev->ev_sw_flags), "",
  284             "Input device supported switch events");
  285 }
  286 
  287 static int
  288 evdev_register_common(struct evdev_dev *evdev)
  289 {
  290         int ret;
  291 
  292         debugf(evdev, "%s: registered evdev provider: %s <%s>\n",
  293             evdev->ev_shortname, evdev->ev_name, evdev->ev_serial);
  294 
  295         /* Initialize internal structures */
  296         LIST_INIT(&evdev->ev_clients);
  297 
  298         if (evdev_event_supported(evdev, EV_REP) &&
  299             bit_test(evdev->ev_flags, EVDEV_FLAG_SOFTREPEAT)) {
  300                 /* Initialize callout */
  301                 callout_init_mtx(&evdev->ev_rep_callout, evdev->ev_lock, 0);
  302 
  303                 if (evdev->ev_rep[REP_DELAY] == 0 &&
  304                     evdev->ev_rep[REP_PERIOD] == 0) {
  305                         /* Supply default values */
  306                         evdev->ev_rep[REP_DELAY] = 250;
  307                         evdev->ev_rep[REP_PERIOD] = 33;
  308                 }
  309         }
  310 
  311         /* Initialize multitouch protocol type B states */
  312         if (bit_test(evdev->ev_abs_flags, ABS_MT_SLOT) &&
  313             evdev->ev_absinfo != NULL && MAXIMAL_MT_SLOT(evdev) > 0)
  314                 evdev_mt_init(evdev);
  315 
  316         /* Estimate maximum report size */
  317         if (evdev->ev_report_size == 0) {
  318                 ret = evdev_set_report_size(evdev,
  319                     evdev_estimate_report_size(evdev));
  320                 if (ret != 0)
  321                         goto bail_out;
  322         }
  323 
  324         /* Create char device node */
  325         ret = evdev_cdev_create(evdev);
  326         if (ret != 0)
  327                 goto bail_out;
  328 
  329         /* Create sysctls (for device enumeration without /dev/input access rights) */
  330         evdev_sysctl_create(evdev);
  331 
  332 bail_out:
  333         return (ret);
  334 }
  335 
  336 int
  337 evdev_register(struct evdev_dev *evdev)
  338 {
  339         int ret;
  340 
  341         evdev->ev_lock_type = EV_LOCK_INTERNAL;
  342         evdev->ev_lock = &evdev->ev_mtx;
  343         mtx_init(&evdev->ev_mtx, "evmtx", NULL, MTX_DEF);
  344 
  345         ret = evdev_register_common(evdev);
  346         if (ret != 0)
  347                 mtx_destroy(&evdev->ev_mtx);
  348 
  349         return (ret);
  350 }
  351 
  352 int
  353 evdev_register_mtx(struct evdev_dev *evdev, struct mtx *mtx)
  354 {
  355 
  356         evdev->ev_lock_type = EV_LOCK_MTX;
  357         evdev->ev_lock = mtx;
  358         return (evdev_register_common(evdev));
  359 }
  360 
  361 int
  362 evdev_unregister(struct evdev_dev *evdev)
  363 {
  364         struct evdev_client *client, *tmp;
  365         int ret;
  366         debugf(evdev, "%s: unregistered evdev provider: %s\n",
  367             evdev->ev_shortname, evdev->ev_name);
  368 
  369         sysctl_ctx_free(&evdev->ev_sysctl_ctx);
  370 
  371         EVDEV_LOCK(evdev);
  372         evdev->ev_cdev->si_drv1 = NULL;
  373         /* Wake up sleepers */
  374         LIST_FOREACH_SAFE(client, &evdev->ev_clients, ec_link, tmp) {
  375                 evdev_revoke_client(client);
  376                 evdev_dispose_client(evdev, client);
  377                 EVDEV_CLIENT_LOCKQ(client);
  378                 evdev_notify_event(client);
  379                 EVDEV_CLIENT_UNLOCKQ(client);
  380         }
  381         EVDEV_UNLOCK(evdev);
  382 
  383         /* destroy_dev can sleep so release lock */
  384         ret = evdev_cdev_destroy(evdev);
  385         evdev->ev_cdev = NULL;
  386         if (ret == 0 && evdev->ev_lock_type == EV_LOCK_INTERNAL)
  387                 mtx_destroy(&evdev->ev_mtx);
  388 
  389         evdev_free_absinfo(evdev->ev_absinfo);
  390         evdev_mt_free(evdev);
  391 
  392         return (ret);
  393 }
  394 
  395 inline void
  396 evdev_set_name(struct evdev_dev *evdev, const char *name)
  397 {
  398 
  399         snprintf(evdev->ev_name, NAMELEN, "%s", name);
  400 }
  401 
  402 inline void
  403 evdev_set_id(struct evdev_dev *evdev, uint16_t bustype, uint16_t vendor,
  404     uint16_t product, uint16_t version)
  405 {
  406 
  407         evdev->ev_id = (struct input_id) {
  408                 .bustype = bustype,
  409                 .vendor = vendor,
  410                 .product = product,
  411                 .version = version
  412         };
  413 }
  414 
  415 inline void
  416 evdev_set_phys(struct evdev_dev *evdev, const char *name)
  417 {
  418 
  419         snprintf(evdev->ev_shortname, NAMELEN, "%s", name);
  420 }
  421 
  422 inline void
  423 evdev_set_serial(struct evdev_dev *evdev, const char *serial)
  424 {
  425 
  426         snprintf(evdev->ev_serial, NAMELEN, "%s", serial);
  427 }
  428 
  429 inline void
  430 evdev_set_methods(struct evdev_dev *evdev, void *softc,
  431     const struct evdev_methods *methods)
  432 {
  433 
  434         evdev->ev_methods = methods;
  435         evdev->ev_softc = softc;
  436 }
  437 
  438 inline void *
  439 evdev_get_softc(struct evdev_dev *evdev)
  440 {
  441 
  442         return (evdev->ev_softc);
  443 }
  444 
  445 inline void
  446 evdev_support_prop(struct evdev_dev *evdev, uint16_t prop)
  447 {
  448 
  449         KASSERT(prop < INPUT_PROP_CNT, ("invalid evdev input property"));
  450         bit_set(evdev->ev_prop_flags, prop);
  451 }
  452 
  453 inline void
  454 evdev_support_event(struct evdev_dev *evdev, uint16_t type)
  455 {
  456 
  457         KASSERT(type < EV_CNT, ("invalid evdev event property"));
  458         bit_set(evdev->ev_type_flags, type);
  459 }
  460 
  461 inline void
  462 evdev_support_key(struct evdev_dev *evdev, uint16_t code)
  463 {
  464 
  465         KASSERT(code < KEY_CNT, ("invalid evdev key property"));
  466         bit_set(evdev->ev_key_flags, code);
  467 }
  468 
  469 inline void
  470 evdev_support_rel(struct evdev_dev *evdev, uint16_t code)
  471 {
  472 
  473         KASSERT(code < REL_CNT, ("invalid evdev rel property"));
  474         bit_set(evdev->ev_rel_flags, code);
  475 }
  476 
  477 inline void
  478 evdev_support_abs(struct evdev_dev *evdev, uint16_t code, int32_t value,
  479     int32_t minimum, int32_t maximum, int32_t fuzz, int32_t flat,
  480     int32_t resolution)
  481 {
  482         struct input_absinfo absinfo;
  483 
  484         KASSERT(code < ABS_CNT, ("invalid evdev abs property"));
  485 
  486         absinfo = (struct input_absinfo) {
  487                 .value = value,
  488                 .minimum = minimum,
  489                 .maximum = maximum,
  490                 .fuzz = fuzz,
  491                 .flat = flat,
  492                 .resolution = resolution,
  493         };
  494         evdev_set_abs_bit(evdev, code);
  495         evdev_set_absinfo(evdev, code, &absinfo);
  496 }
  497 
  498 inline void
  499 evdev_set_abs_bit(struct evdev_dev *evdev, uint16_t code)
  500 {
  501 
  502         KASSERT(code < ABS_CNT, ("invalid evdev abs property"));
  503         if (evdev->ev_absinfo == NULL)
  504                 evdev->ev_absinfo = evdev_alloc_absinfo();
  505         bit_set(evdev->ev_abs_flags, code);
  506 }
  507 
  508 inline void
  509 evdev_support_msc(struct evdev_dev *evdev, uint16_t code)
  510 {
  511 
  512         KASSERT(code < MSC_CNT, ("invalid evdev msc property"));
  513         bit_set(evdev->ev_msc_flags, code);
  514 }
  515 
  516 
  517 inline void
  518 evdev_support_led(struct evdev_dev *evdev, uint16_t code)
  519 {
  520 
  521         KASSERT(code < LED_CNT, ("invalid evdev led property"));
  522         bit_set(evdev->ev_led_flags, code);
  523 }
  524 
  525 inline void
  526 evdev_support_snd(struct evdev_dev *evdev, uint16_t code)
  527 {
  528 
  529         KASSERT(code < SND_CNT, ("invalid evdev snd property"));
  530         bit_set(evdev->ev_snd_flags, code);
  531 }
  532 
  533 inline void
  534 evdev_support_sw(struct evdev_dev *evdev, uint16_t code)
  535 {
  536 
  537         KASSERT(code < SW_CNT, ("invalid evdev sw property"));
  538         bit_set(evdev->ev_sw_flags, code);
  539 }
  540 
  541 bool
  542 evdev_event_supported(struct evdev_dev *evdev, uint16_t type)
  543 {
  544 
  545         KASSERT(type < EV_CNT, ("invalid evdev event property"));
  546         return (bit_test(evdev->ev_type_flags, type));
  547 }
  548 
  549 inline void
  550 evdev_set_absinfo(struct evdev_dev *evdev, uint16_t axis,
  551     struct input_absinfo *absinfo)
  552 {
  553 
  554         KASSERT(axis < ABS_CNT, ("invalid evdev abs property"));
  555 
  556         if (axis == ABS_MT_SLOT &&
  557             (absinfo->maximum < 1 || absinfo->maximum >= MAX_MT_SLOTS))
  558                 return;
  559 
  560         if (evdev->ev_absinfo == NULL)
  561                 evdev->ev_absinfo = evdev_alloc_absinfo();
  562 
  563         if (axis == ABS_MT_SLOT)
  564                 evdev->ev_absinfo[ABS_MT_SLOT].maximum = absinfo->maximum;
  565         else
  566                 memcpy(&evdev->ev_absinfo[axis], absinfo,
  567                     sizeof(struct input_absinfo));
  568 }
  569 
  570 inline void
  571 evdev_set_repeat_params(struct evdev_dev *evdev, uint16_t property, int value)
  572 {
  573 
  574         KASSERT(property < REP_CNT, ("invalid evdev repeat property"));
  575         evdev->ev_rep[property] = value;
  576 }
  577 
  578 inline void
  579 evdev_set_flag(struct evdev_dev *evdev, uint16_t flag)
  580 {
  581 
  582         KASSERT(flag < EVDEV_FLAG_CNT, ("invalid evdev flag property"));
  583         bit_set(evdev->ev_flags, flag);
  584 }
  585 
  586 static int
  587 evdev_check_event(struct evdev_dev *evdev, uint16_t type, uint16_t code,
  588     int32_t value)
  589 {
  590 
  591         if (type >= EV_CNT)
  592                 return (EINVAL);
  593 
  594         /* Allow SYN events implicitly */
  595         if (type != EV_SYN && !evdev_event_supported(evdev, type))
  596                 return (EINVAL);
  597 
  598         switch (type) {
  599         case EV_SYN:
  600                 if (code >= SYN_CNT)
  601                         return (EINVAL);
  602                 break;
  603 
  604         case EV_KEY:
  605                 if (code >= KEY_CNT)
  606                         return (EINVAL);
  607                 if (!bit_test(evdev->ev_key_flags, code))
  608                         return (EINVAL);
  609                 break;
  610 
  611         case EV_REL:
  612                 if (code >= REL_CNT)
  613                         return (EINVAL);
  614                 if (!bit_test(evdev->ev_rel_flags, code))
  615                         return (EINVAL);
  616                 break;
  617 
  618         case EV_ABS:
  619                 if (code >= ABS_CNT)
  620                         return (EINVAL);
  621                 if (!bit_test(evdev->ev_abs_flags, code))
  622                         return (EINVAL);
  623                 if (code == ABS_MT_SLOT &&
  624                     (value < 0 || value > MAXIMAL_MT_SLOT(evdev)))
  625                         return (EINVAL);
  626                 if (ABS_IS_MT(code) && evdev->ev_mt == NULL &&
  627                     bit_test(evdev->ev_abs_flags, ABS_MT_SLOT))
  628                         return (EINVAL);
  629                 break;
  630 
  631         case EV_MSC:
  632                 if (code >= MSC_CNT)
  633                         return (EINVAL);
  634                 if (!bit_test(evdev->ev_msc_flags, code))
  635                         return (EINVAL);
  636                 break;
  637 
  638         case EV_LED:
  639                 if (code >= LED_CNT)
  640                         return (EINVAL);
  641                 if (!bit_test(evdev->ev_led_flags, code))
  642                         return (EINVAL);
  643                 break;
  644 
  645         case EV_SND:
  646                 if (code >= SND_CNT)
  647                         return (EINVAL);
  648                 if (!bit_test(evdev->ev_snd_flags, code))
  649                         return (EINVAL);
  650                 break;
  651 
  652         case EV_SW:
  653                 if (code >= SW_CNT)
  654                         return (EINVAL);
  655                 if (!bit_test(evdev->ev_sw_flags, code))
  656                         return (EINVAL);
  657                 break;
  658 
  659         case EV_REP:
  660                 if (code >= REP_CNT)
  661                         return (EINVAL);
  662                 break;
  663 
  664         default:
  665                 return (EINVAL);
  666         }
  667 
  668         return (0);
  669 }
  670 
  671 static void
  672 evdev_modify_event(struct evdev_dev *evdev, uint16_t type, uint16_t code,
  673     int32_t *value)
  674 {
  675 
  676         EVDEV_LOCK_ASSERT(evdev);
  677 
  678         switch (type) {
  679         case EV_KEY:
  680                 if (!evdev_event_supported(evdev, EV_REP))
  681                         break;
  682 
  683                 if (!bit_test(evdev->ev_flags, EVDEV_FLAG_SOFTREPEAT)) {
  684                         /* Detect driver key repeats. */
  685                         if (bit_test(evdev->ev_key_states, code) &&
  686                             *value == KEY_EVENT_DOWN)
  687                                 *value = KEY_EVENT_REPEAT;
  688                 } else {
  689                         /* Start/stop callout for evdev repeats */
  690                         if (bit_test(evdev->ev_key_states, code) == !*value &&
  691                             !LIST_EMPTY(&evdev->ev_clients)) {
  692                                 if (*value == KEY_EVENT_DOWN)
  693                                         evdev_start_repeat(evdev, code);
  694                                 else
  695                                         evdev_stop_repeat(evdev);
  696                         }
  697                 }
  698                 break;
  699 
  700         case EV_ABS:
  701                 /* TBD: implement fuzz */
  702                 break;
  703         }
  704 }
  705 
  706 static enum evdev_sparse_result
  707 evdev_sparse_event(struct evdev_dev *evdev, uint16_t type, uint16_t code,
  708     int32_t value)
  709 {
  710         int32_t last_mt_slot;
  711 
  712         EVDEV_LOCK_ASSERT(evdev);
  713 
  714         /*
  715          * For certain event types, update device state bits
  716          * and convert level reporting to edge reporting
  717          */
  718         switch (type) {
  719         case EV_KEY:
  720                 switch (value) {
  721                 case KEY_EVENT_UP:
  722                 case KEY_EVENT_DOWN:
  723                         if (bit_test(evdev->ev_key_states, code) == value)
  724                                 return (EV_SKIP_EVENT);
  725                         bit_change(evdev->ev_key_states, code, value);
  726                         break;
  727 
  728                 case KEY_EVENT_REPEAT:
  729                         if (bit_test(evdev->ev_key_states, code) == 0 ||
  730                             !evdev_event_supported(evdev, EV_REP))
  731                                 return (EV_SKIP_EVENT);
  732                         break;
  733 
  734                 default:
  735                          return (EV_SKIP_EVENT);
  736                 }
  737                 break;
  738 
  739         case EV_LED:
  740                 if (bit_test(evdev->ev_led_states, code) == value)
  741                         return (EV_SKIP_EVENT);
  742                 bit_change(evdev->ev_led_states, code, value);
  743                 break;
  744 
  745         case EV_SND:
  746                 bit_change(evdev->ev_snd_states, code, value);
  747                 break;
  748 
  749         case EV_SW:
  750                 if (bit_test(evdev->ev_sw_states, code) == value)
  751                         return (EV_SKIP_EVENT);
  752                 bit_change(evdev->ev_sw_states, code, value);
  753                 break;
  754 
  755         case EV_REP:
  756                 if (evdev->ev_rep[code] == value)
  757                         return (EV_SKIP_EVENT);
  758                 evdev_set_repeat_params(evdev, code, value);
  759                 break;
  760 
  761         case EV_REL:
  762                 if (value == 0)
  763                         return (EV_SKIP_EVENT);
  764                 break;
  765 
  766         /* For EV_ABS, save last value in absinfo and ev_mt_states */
  767         case EV_ABS:
  768                 switch (code) {
  769                 case ABS_MT_SLOT:
  770                         /* Postpone ABS_MT_SLOT till next event */
  771                         evdev_set_last_mt_slot(evdev, value);
  772                         return (EV_SKIP_EVENT);
  773 
  774                 case ABS_MT_FIRST ... ABS_MT_LAST:
  775                         /* Pass MT protocol type A events as is */
  776                         if (!bit_test(evdev->ev_abs_flags, ABS_MT_SLOT))
  777                                 break;
  778                         /* Don`t repeat MT protocol type B events */
  779                         last_mt_slot = evdev_get_last_mt_slot(evdev);
  780                         if (evdev_get_mt_value(evdev, last_mt_slot, code)
  781                              == value)
  782                                 return (EV_SKIP_EVENT);
  783                         evdev_set_mt_value(evdev, last_mt_slot, code, value);
  784                         if (last_mt_slot != CURRENT_MT_SLOT(evdev)) {
  785                                 CURRENT_MT_SLOT(evdev) = last_mt_slot;
  786                                 evdev->ev_report_opened = true;
  787                                 return (EV_REPORT_MT_SLOT);
  788                         }
  789                         break;
  790 
  791                 default:
  792                         if (evdev->ev_absinfo[code].value == value)
  793                                 return (EV_SKIP_EVENT);
  794                         evdev->ev_absinfo[code].value = value;
  795                 }
  796                 break;
  797 
  798         case EV_SYN:
  799                 if (code == SYN_REPORT) {
  800                         /* Count empty reports as well as non empty */
  801                         evdev->ev_report_count++;
  802                         /* Skip empty reports */
  803                         if (!evdev->ev_report_opened)
  804                                 return (EV_SKIP_EVENT);
  805                         evdev->ev_report_opened = false;
  806                         return (EV_REPORT_EVENT);
  807                 }
  808                 break;
  809         }
  810 
  811         evdev->ev_report_opened = true;
  812         return (EV_REPORT_EVENT);
  813 }
  814 
  815 static void
  816 evdev_propagate_event(struct evdev_dev *evdev, uint16_t type, uint16_t code,
  817     int32_t value)
  818 {
  819         struct evdev_client *client;
  820 
  821         debugf(evdev, "%s pushed event %d/%d/%d",
  822             evdev->ev_shortname, type, code, value);
  823 
  824         EVDEV_LOCK_ASSERT(evdev);
  825 
  826         /* Propagate event through all clients */
  827         LIST_FOREACH(client, &evdev->ev_clients, ec_link) {
  828                 if (evdev->ev_grabber != NULL && evdev->ev_grabber != client)
  829                         continue;
  830 
  831                 EVDEV_CLIENT_LOCKQ(client);
  832                 evdev_client_push(client, type, code, value);
  833                 if (type == EV_SYN && code == SYN_REPORT)
  834                         evdev_notify_event(client);
  835                 EVDEV_CLIENT_UNLOCKQ(client);
  836         }
  837 
  838         evdev->ev_event_count++;
  839 }
  840 
  841 void
  842 evdev_send_event(struct evdev_dev *evdev, uint16_t type, uint16_t code,
  843     int32_t value)
  844 {
  845         enum evdev_sparse_result sparse;
  846 
  847         EVDEV_LOCK_ASSERT(evdev);
  848 
  849         sparse =  evdev_sparse_event(evdev, type, code, value);
  850         switch (sparse) {
  851         case EV_REPORT_MT_SLOT:
  852                 /* report postponed ABS_MT_SLOT */
  853                 evdev_propagate_event(evdev, EV_ABS, ABS_MT_SLOT,
  854                     CURRENT_MT_SLOT(evdev));
  855                 /* FALLTHROUGH */
  856         case EV_REPORT_EVENT:
  857                 evdev_propagate_event(evdev, type, code, value);
  858                 /* FALLTHROUGH */
  859         case EV_SKIP_EVENT:
  860                 break;
  861         }
  862 }
  863 
  864 void
  865 evdev_restore_after_kdb(struct evdev_dev *evdev)
  866 {
  867         int code;
  868 
  869         EVDEV_LOCK_ASSERT(evdev);
  870 
  871         /* Report postponed leds */
  872         for (code = 0; code < LED_CNT; code++)
  873                 if (bit_test(evdev->ev_kdb_led_states, code))
  874                         evdev_send_event(evdev, EV_LED, code,
  875                             !bit_test(evdev->ev_led_states, code));
  876         bit_nclear(evdev->ev_kdb_led_states, 0, LED_MAX);
  877 
  878         /* Release stuck keys (CTRL + ALT + ESC) */
  879         evdev_stop_repeat(evdev);
  880         for (code = 0; code < KEY_CNT; code++) {
  881                 if (bit_test(evdev->ev_key_states, code)) {
  882                         evdev_send_event(evdev, EV_KEY, code, KEY_EVENT_UP);
  883                         evdev_send_event(evdev, EV_SYN, SYN_REPORT, 1);
  884                 }
  885         }
  886 }
  887 
  888 int
  889 evdev_push_event(struct evdev_dev *evdev, uint16_t type, uint16_t code,
  890     int32_t value)
  891 {
  892 
  893         if (evdev_check_event(evdev, type, code, value) != 0)
  894                 return (EINVAL);
  895 
  896         /*
  897          * Discard all but LEDs kdb events as unrelated to userspace.
  898          * Aggregate LED updates and postpone reporting until kdb deactivation.
  899          */
  900         if (kdb_active || SCHEDULER_STOPPED()) {
  901                 evdev->ev_kdb_active = true;
  902                 if (type == EV_LED)
  903                         bit_set(evdev->ev_kdb_led_states,
  904                             bit_test(evdev->ev_led_states, code) != value);
  905                 return (0);
  906         }
  907 
  908         EVDEV_ENTER(evdev);
  909 
  910         /* Fix evdev state corrupted with discarding of kdb events */
  911         if (evdev->ev_kdb_active) {
  912                 evdev->ev_kdb_active = false;
  913                 evdev_restore_after_kdb(evdev);
  914         }
  915 
  916         evdev_modify_event(evdev, type, code, &value);
  917         if (type == EV_SYN && code == SYN_REPORT &&
  918              bit_test(evdev->ev_flags, EVDEV_FLAG_MT_AUTOREL))
  919                 evdev_send_mt_autorel(evdev);
  920         if (type == EV_SYN && code == SYN_REPORT && evdev->ev_report_opened &&
  921             bit_test(evdev->ev_flags, EVDEV_FLAG_MT_STCOMPAT))
  922                 evdev_send_mt_compat(evdev);
  923         evdev_send_event(evdev, type, code, value);
  924 
  925         EVDEV_EXIT(evdev);
  926 
  927         return (0);
  928 }
  929 
  930 int
  931 evdev_inject_event(struct evdev_dev *evdev, uint16_t type, uint16_t code,
  932     int32_t value)
  933 {
  934         int ret = 0;
  935 
  936         switch (type) {
  937         case EV_REP:
  938                 /* evdev repeats should not be processed by hardware driver */
  939                 if (bit_test(evdev->ev_flags, EVDEV_FLAG_SOFTREPEAT))
  940                         goto push;
  941                 /* FALLTHROUGH */
  942         case EV_LED:
  943         case EV_MSC:
  944         case EV_SND:
  945         case EV_FF:
  946                 if (evdev->ev_methods != NULL &&
  947                     evdev->ev_methods->ev_event != NULL)
  948                         evdev->ev_methods->ev_event(evdev, type, code, value);
  949                 /*
  950                  * Leds and driver repeats should be reported in ev_event
  951                  * method body to interoperate with kbdmux states and rates
  952                  * propagation so both ways (ioctl and evdev) of changing it
  953                  * will produce only one evdev event report to client.
  954                  */
  955                 if (type == EV_LED || type == EV_REP)
  956                         break;
  957                 /* FALLTHROUGH */
  958         case EV_SYN:
  959         case EV_KEY:
  960         case EV_REL:
  961         case EV_ABS:
  962         case EV_SW:
  963 push:
  964                 if (evdev->ev_lock_type != EV_LOCK_INTERNAL)
  965                         EVDEV_LOCK(evdev);
  966                 ret = evdev_push_event(evdev, type,  code, value);
  967                 if (evdev->ev_lock_type != EV_LOCK_INTERNAL)
  968                         EVDEV_UNLOCK(evdev);
  969                 break;
  970 
  971         default:
  972                 ret = EINVAL;
  973         }
  974 
  975         return (ret);
  976 }
  977 
  978 int
  979 evdev_register_client(struct evdev_dev *evdev, struct evdev_client *client)
  980 {
  981         int ret = 0;
  982 
  983         debugf(evdev, "adding new client for device %s", evdev->ev_shortname);
  984 
  985         EVDEV_LOCK_ASSERT(evdev);
  986 
  987         if (LIST_EMPTY(&evdev->ev_clients) && evdev->ev_methods != NULL &&
  988             evdev->ev_methods->ev_open != NULL) {
  989                 debugf(evdev, "calling ev_open() on device %s",
  990                     evdev->ev_shortname);
  991                 ret = evdev->ev_methods->ev_open(evdev);
  992         }
  993         if (ret == 0)
  994                 LIST_INSERT_HEAD(&evdev->ev_clients, client, ec_link);
  995         return (ret);
  996 }
  997 
  998 void
  999 evdev_dispose_client(struct evdev_dev *evdev, struct evdev_client *client)
 1000 {
 1001         debugf(evdev, "removing client for device %s", evdev->ev_shortname);
 1002 
 1003         EVDEV_LOCK_ASSERT(evdev);
 1004 
 1005         LIST_REMOVE(client, ec_link);
 1006         if (LIST_EMPTY(&evdev->ev_clients)) {
 1007                 if (evdev->ev_methods != NULL &&
 1008                     evdev->ev_methods->ev_close != NULL)
 1009                         (void)evdev->ev_methods->ev_close(evdev);
 1010                 if (evdev_event_supported(evdev, EV_REP) &&
 1011                     bit_test(evdev->ev_flags, EVDEV_FLAG_SOFTREPEAT))
 1012                         evdev_stop_repeat(evdev);
 1013         }
 1014         evdev_release_client(evdev, client);
 1015 }
 1016 
 1017 int
 1018 evdev_grab_client(struct evdev_dev *evdev, struct evdev_client *client)
 1019 {
 1020 
 1021         EVDEV_LOCK_ASSERT(evdev);
 1022 
 1023         if (evdev->ev_grabber != NULL)
 1024                 return (EBUSY);
 1025 
 1026         evdev->ev_grabber = client;
 1027 
 1028         return (0);
 1029 }
 1030 
 1031 int
 1032 evdev_release_client(struct evdev_dev *evdev, struct evdev_client *client)
 1033 {
 1034 
 1035         EVDEV_LOCK_ASSERT(evdev);
 1036 
 1037         if (evdev->ev_grabber != client)
 1038                 return (EINVAL);
 1039 
 1040         evdev->ev_grabber = NULL;
 1041 
 1042         return (0);
 1043 }
 1044 
 1045 static void
 1046 evdev_repeat_callout(void *arg)
 1047 {
 1048         struct evdev_dev *evdev = (struct evdev_dev *)arg;
 1049 
 1050         evdev_send_event(evdev, EV_KEY, evdev->ev_rep_key, KEY_EVENT_REPEAT);
 1051         evdev_send_event(evdev, EV_SYN, SYN_REPORT, 1);
 1052 
 1053         if (evdev->ev_rep[REP_PERIOD])
 1054                 callout_reset(&evdev->ev_rep_callout,
 1055                     evdev->ev_rep[REP_PERIOD] * hz / 1000,
 1056                     evdev_repeat_callout, evdev);
 1057         else
 1058                 evdev->ev_rep_key = KEY_RESERVED;
 1059 }
 1060 
 1061 static void
 1062 evdev_start_repeat(struct evdev_dev *evdev, uint16_t key)
 1063 {
 1064 
 1065         EVDEV_LOCK_ASSERT(evdev);
 1066 
 1067         if (evdev->ev_rep[REP_DELAY]) {
 1068                 evdev->ev_rep_key = key;
 1069                 callout_reset(&evdev->ev_rep_callout,
 1070                     evdev->ev_rep[REP_DELAY] * hz / 1000,
 1071                     evdev_repeat_callout, evdev);
 1072         }
 1073 }
 1074 
 1075 static void
 1076 evdev_stop_repeat(struct evdev_dev *evdev)
 1077 {
 1078 
 1079         EVDEV_LOCK_ASSERT(evdev);
 1080 
 1081         if (evdev->ev_rep_key != KEY_RESERVED) {
 1082                 callout_stop(&evdev->ev_rep_callout);
 1083                 evdev->ev_rep_key = KEY_RESERVED;
 1084         }
 1085 }
 1086 
 1087 MODULE_VERSION(evdev, 1);

Cache object: 3f7b47790c2f07b0c7343cbc7ccdd7b1


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.