12b3f6d66SOleksandr Tymoshenko /*- 22b3f6d66SOleksandr Tymoshenko * Copyright (c) 2014 Jakub Wojciech Klama <jceel@FreeBSD.org> 3e6502802SVladimir Kondratyev * Copyright (c) 2015-2016 Vladimir Kondratyev <wulf@FreeBSD.org> 42b3f6d66SOleksandr Tymoshenko * All rights reserved. 52b3f6d66SOleksandr Tymoshenko * 62b3f6d66SOleksandr Tymoshenko * Redistribution and use in source and binary forms, with or without 72b3f6d66SOleksandr Tymoshenko * modification, are permitted provided that the following conditions 82b3f6d66SOleksandr Tymoshenko * are met: 92b3f6d66SOleksandr Tymoshenko * 1. Redistributions of source code must retain the above copyright 102b3f6d66SOleksandr Tymoshenko * notice, this list of conditions and the following disclaimer. 112b3f6d66SOleksandr Tymoshenko * 2. Redistributions in binary form must reproduce the above copyright 122b3f6d66SOleksandr Tymoshenko * notice, this list of conditions and the following disclaimer in the 132b3f6d66SOleksandr Tymoshenko * documentation and/or other materials provided with the distribution. 142b3f6d66SOleksandr Tymoshenko * 152b3f6d66SOleksandr Tymoshenko * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 162b3f6d66SOleksandr Tymoshenko * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 172b3f6d66SOleksandr Tymoshenko * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 182b3f6d66SOleksandr Tymoshenko * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 192b3f6d66SOleksandr Tymoshenko * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 202b3f6d66SOleksandr Tymoshenko * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 212b3f6d66SOleksandr Tymoshenko * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 222b3f6d66SOleksandr Tymoshenko * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 232b3f6d66SOleksandr Tymoshenko * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 242b3f6d66SOleksandr Tymoshenko * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 252b3f6d66SOleksandr Tymoshenko * SUCH DAMAGE. 262b3f6d66SOleksandr Tymoshenko * 272b3f6d66SOleksandr Tymoshenko * $FreeBSD$ 282b3f6d66SOleksandr Tymoshenko */ 292b3f6d66SOleksandr Tymoshenko 302b3f6d66SOleksandr Tymoshenko #include "opt_evdev.h" 312b3f6d66SOleksandr Tymoshenko 322b3f6d66SOleksandr Tymoshenko #include <sys/param.h> 33ea2e26b1SVladimir Kondratyev #include <sys/bitstring.h> 342b3f6d66SOleksandr Tymoshenko #include <sys/conf.h> 35*d276eae6SVladimir Kondratyev #include <sys/epoch.h> 362b3f6d66SOleksandr Tymoshenko #include <sys/filio.h> 372b3f6d66SOleksandr Tymoshenko #include <sys/fcntl.h> 38ea2e26b1SVladimir Kondratyev #include <sys/kernel.h> 392b3f6d66SOleksandr Tymoshenko #include <sys/malloc.h> 40ea2e26b1SVladimir Kondratyev #include <sys/poll.h> 41ea2e26b1SVladimir Kondratyev #include <sys/proc.h> 42ea2e26b1SVladimir Kondratyev #include <sys/selinfo.h> 43ea2e26b1SVladimir Kondratyev #include <sys/systm.h> 442b3f6d66SOleksandr Tymoshenko #include <sys/time.h> 45ea2e26b1SVladimir Kondratyev #include <sys/uio.h> 462b3f6d66SOleksandr Tymoshenko 472b3f6d66SOleksandr Tymoshenko #include <dev/evdev/evdev.h> 482b3f6d66SOleksandr Tymoshenko #include <dev/evdev/evdev_private.h> 49ea2e26b1SVladimir Kondratyev #include <dev/evdev/input.h> 502b3f6d66SOleksandr Tymoshenko 5180ddbddfSVladimir Kondratyev #ifdef COMPAT_FREEBSD32 5280ddbddfSVladimir Kondratyev #include <sys/mount.h> 5380ddbddfSVladimir Kondratyev #include <sys/sysent.h> 5480ddbddfSVladimir Kondratyev #include <compat/freebsd32/freebsd32.h> 5580ddbddfSVladimir Kondratyev struct input_event32 { 5680ddbddfSVladimir Kondratyev struct timeval32 time; 5780ddbddfSVladimir Kondratyev uint16_t type; 5880ddbddfSVladimir Kondratyev uint16_t code; 5980ddbddfSVladimir Kondratyev int32_t value; 6080ddbddfSVladimir Kondratyev }; 6180ddbddfSVladimir Kondratyev #endif 6280ddbddfSVladimir Kondratyev 632b3f6d66SOleksandr Tymoshenko #ifdef EVDEV_DEBUG 64e0cfa1bcSOleksandr Tymoshenko #define debugf(client, fmt, args...) printf("evdev cdev: "fmt"\n", ##args) 652b3f6d66SOleksandr Tymoshenko #else 662b3f6d66SOleksandr Tymoshenko #define debugf(client, fmt, args...) 672b3f6d66SOleksandr Tymoshenko #endif 682b3f6d66SOleksandr Tymoshenko 692b3f6d66SOleksandr Tymoshenko #define DEF_RING_REPORTS 8 702b3f6d66SOleksandr Tymoshenko 712b3f6d66SOleksandr Tymoshenko static d_open_t evdev_open; 722b3f6d66SOleksandr Tymoshenko static d_read_t evdev_read; 732b3f6d66SOleksandr Tymoshenko static d_write_t evdev_write; 742b3f6d66SOleksandr Tymoshenko static d_ioctl_t evdev_ioctl; 752b3f6d66SOleksandr Tymoshenko static d_poll_t evdev_poll; 762b3f6d66SOleksandr Tymoshenko static d_kqfilter_t evdev_kqfilter; 772b3f6d66SOleksandr Tymoshenko 782b3f6d66SOleksandr Tymoshenko static int evdev_kqread(struct knote *kn, long hint); 792b3f6d66SOleksandr Tymoshenko static void evdev_kqdetach(struct knote *kn); 802b3f6d66SOleksandr Tymoshenko static void evdev_dtor(void *); 812b3f6d66SOleksandr Tymoshenko static int evdev_ioctl_eviocgbit(struct evdev_dev *, int, int, caddr_t); 822b3f6d66SOleksandr Tymoshenko static void evdev_client_filter_queue(struct evdev_client *, uint16_t); 832b3f6d66SOleksandr Tymoshenko 842b3f6d66SOleksandr Tymoshenko static struct cdevsw evdev_cdevsw = { 852b3f6d66SOleksandr Tymoshenko .d_version = D_VERSION, 862b3f6d66SOleksandr Tymoshenko .d_open = evdev_open, 872b3f6d66SOleksandr Tymoshenko .d_read = evdev_read, 882b3f6d66SOleksandr Tymoshenko .d_write = evdev_write, 892b3f6d66SOleksandr Tymoshenko .d_ioctl = evdev_ioctl, 902b3f6d66SOleksandr Tymoshenko .d_poll = evdev_poll, 912b3f6d66SOleksandr Tymoshenko .d_kqfilter = evdev_kqfilter, 922b3f6d66SOleksandr Tymoshenko .d_name = "evdev", 932b3f6d66SOleksandr Tymoshenko }; 942b3f6d66SOleksandr Tymoshenko 952b3f6d66SOleksandr Tymoshenko static struct filterops evdev_cdev_filterops = { 962b3f6d66SOleksandr Tymoshenko .f_isfd = 1, 972b3f6d66SOleksandr Tymoshenko .f_attach = NULL, 982b3f6d66SOleksandr Tymoshenko .f_detach = evdev_kqdetach, 992b3f6d66SOleksandr Tymoshenko .f_event = evdev_kqread, 1002b3f6d66SOleksandr Tymoshenko }; 1012b3f6d66SOleksandr Tymoshenko 1022b3f6d66SOleksandr Tymoshenko static int 1032b3f6d66SOleksandr Tymoshenko evdev_open(struct cdev *dev, int oflags, int devtype, struct thread *td) 1042b3f6d66SOleksandr Tymoshenko { 1052b3f6d66SOleksandr Tymoshenko struct evdev_dev *evdev = dev->si_drv1; 1062b3f6d66SOleksandr Tymoshenko struct evdev_client *client; 1072b3f6d66SOleksandr Tymoshenko size_t buffer_size; 1082b3f6d66SOleksandr Tymoshenko int ret; 1092b3f6d66SOleksandr Tymoshenko 1102b3f6d66SOleksandr Tymoshenko if (evdev == NULL) 1112b3f6d66SOleksandr Tymoshenko return (ENODEV); 1122b3f6d66SOleksandr Tymoshenko 1132b3f6d66SOleksandr Tymoshenko /* Initialize client structure */ 1142b3f6d66SOleksandr Tymoshenko buffer_size = evdev->ev_report_size * DEF_RING_REPORTS; 1152b3f6d66SOleksandr Tymoshenko client = malloc(offsetof(struct evdev_client, ec_buffer) + 1162b3f6d66SOleksandr Tymoshenko sizeof(struct input_event) * buffer_size, 1172b3f6d66SOleksandr Tymoshenko M_EVDEV, M_WAITOK | M_ZERO); 1182b3f6d66SOleksandr Tymoshenko 1192b3f6d66SOleksandr Tymoshenko /* Initialize ring buffer */ 1202b3f6d66SOleksandr Tymoshenko client->ec_buffer_size = buffer_size; 1212b3f6d66SOleksandr Tymoshenko client->ec_buffer_head = 0; 1222b3f6d66SOleksandr Tymoshenko client->ec_buffer_tail = 0; 1232b3f6d66SOleksandr Tymoshenko client->ec_buffer_ready = 0; 1242b3f6d66SOleksandr Tymoshenko 1252b3f6d66SOleksandr Tymoshenko client->ec_evdev = evdev; 1262b3f6d66SOleksandr Tymoshenko mtx_init(&client->ec_buffer_mtx, "evclient", "evdev", MTX_DEF); 1272b3f6d66SOleksandr Tymoshenko knlist_init_mtx(&client->ec_selp.si_note, &client->ec_buffer_mtx); 1282b3f6d66SOleksandr Tymoshenko 1292b3f6d66SOleksandr Tymoshenko /* Avoid race with evdev_unregister */ 130*d276eae6SVladimir Kondratyev EVDEV_LIST_LOCK(evdev); 1312b3f6d66SOleksandr Tymoshenko if (dev->si_drv1 == NULL) 1322b3f6d66SOleksandr Tymoshenko ret = ENODEV; 1332b3f6d66SOleksandr Tymoshenko else 1342b3f6d66SOleksandr Tymoshenko ret = evdev_register_client(evdev, client); 1352b3f6d66SOleksandr Tymoshenko 1362b3f6d66SOleksandr Tymoshenko if (ret != 0) 1372b3f6d66SOleksandr Tymoshenko evdev_revoke_client(client); 138*d276eae6SVladimir Kondratyev EVDEV_LIST_UNLOCK(evdev); 1392b3f6d66SOleksandr Tymoshenko 140*d276eae6SVladimir Kondratyev if (ret == 0) 1412b3f6d66SOleksandr Tymoshenko ret = devfs_set_cdevpriv(client, evdev_dtor); 1422b3f6d66SOleksandr Tymoshenko 1432b3f6d66SOleksandr Tymoshenko if (ret != 0) { 1442b3f6d66SOleksandr Tymoshenko debugf(client, "cannot register evdev client"); 1452b3f6d66SOleksandr Tymoshenko evdev_dtor(client); 1462b3f6d66SOleksandr Tymoshenko } 1472b3f6d66SOleksandr Tymoshenko 1482b3f6d66SOleksandr Tymoshenko return (ret); 1492b3f6d66SOleksandr Tymoshenko } 1502b3f6d66SOleksandr Tymoshenko 1512b3f6d66SOleksandr Tymoshenko static void 1522b3f6d66SOleksandr Tymoshenko evdev_dtor(void *data) 1532b3f6d66SOleksandr Tymoshenko { 1542b3f6d66SOleksandr Tymoshenko struct evdev_client *client = (struct evdev_client *)data; 1552b3f6d66SOleksandr Tymoshenko 156*d276eae6SVladimir Kondratyev EVDEV_LIST_LOCK(client->ec_evdev); 1572b3f6d66SOleksandr Tymoshenko if (!client->ec_revoked) 1582b3f6d66SOleksandr Tymoshenko evdev_dispose_client(client->ec_evdev, client); 159*d276eae6SVladimir Kondratyev EVDEV_LIST_UNLOCK(client->ec_evdev); 1602b3f6d66SOleksandr Tymoshenko 161*d276eae6SVladimir Kondratyev if (client->ec_evdev->ev_lock_type != EV_LOCK_MTX) 162*d276eae6SVladimir Kondratyev epoch_wait_preempt(INPUT_EPOCH); 1632b3f6d66SOleksandr Tymoshenko knlist_clear(&client->ec_selp.si_note, 0); 1642b3f6d66SOleksandr Tymoshenko seldrain(&client->ec_selp); 1652b3f6d66SOleksandr Tymoshenko knlist_destroy(&client->ec_selp.si_note); 1662b3f6d66SOleksandr Tymoshenko funsetown(&client->ec_sigio); 1672b3f6d66SOleksandr Tymoshenko mtx_destroy(&client->ec_buffer_mtx); 1682b3f6d66SOleksandr Tymoshenko free(client, M_EVDEV); 1692b3f6d66SOleksandr Tymoshenko } 1702b3f6d66SOleksandr Tymoshenko 1712b3f6d66SOleksandr Tymoshenko static int 1722b3f6d66SOleksandr Tymoshenko evdev_read(struct cdev *dev, struct uio *uio, int ioflag) 1732b3f6d66SOleksandr Tymoshenko { 1742b3f6d66SOleksandr Tymoshenko struct evdev_client *client; 17580ddbddfSVladimir Kondratyev union { 17680ddbddfSVladimir Kondratyev struct input_event t; 17780ddbddfSVladimir Kondratyev #ifdef COMPAT_FREEBSD32 17880ddbddfSVladimir Kondratyev struct input_event32 t32; 17980ddbddfSVladimir Kondratyev #endif 18080ddbddfSVladimir Kondratyev } event; 18180ddbddfSVladimir Kondratyev struct input_event *head; 18280ddbddfSVladimir Kondratyev size_t evsize; 1832b3f6d66SOleksandr Tymoshenko int ret = 0; 1842b3f6d66SOleksandr Tymoshenko int remaining; 1852b3f6d66SOleksandr Tymoshenko 1862b3f6d66SOleksandr Tymoshenko ret = devfs_get_cdevpriv((void **)&client); 1872b3f6d66SOleksandr Tymoshenko if (ret != 0) 1882b3f6d66SOleksandr Tymoshenko return (ret); 1892b3f6d66SOleksandr Tymoshenko 1902b3f6d66SOleksandr Tymoshenko debugf(client, "read %zd bytes by thread %d", uio->uio_resid, 1912b3f6d66SOleksandr Tymoshenko uio->uio_td->td_tid); 1922b3f6d66SOleksandr Tymoshenko 1932b3f6d66SOleksandr Tymoshenko if (client->ec_revoked) 1942b3f6d66SOleksandr Tymoshenko return (ENODEV); 1952b3f6d66SOleksandr Tymoshenko 19680ddbddfSVladimir Kondratyev #ifdef COMPAT_FREEBSD32 19780ddbddfSVladimir Kondratyev if (SV_CURPROC_FLAG(SV_ILP32)) 19880ddbddfSVladimir Kondratyev evsize = sizeof(struct input_event32); 19980ddbddfSVladimir Kondratyev else 20080ddbddfSVladimir Kondratyev #endif 20180ddbddfSVladimir Kondratyev evsize = sizeof(struct input_event); 20280ddbddfSVladimir Kondratyev 2032b3f6d66SOleksandr Tymoshenko /* Zero-sized reads are allowed for error checking */ 20480ddbddfSVladimir Kondratyev if (uio->uio_resid != 0 && uio->uio_resid < evsize) 2052b3f6d66SOleksandr Tymoshenko return (EINVAL); 2062b3f6d66SOleksandr Tymoshenko 20780ddbddfSVladimir Kondratyev remaining = uio->uio_resid / evsize; 2082b3f6d66SOleksandr Tymoshenko 2092b3f6d66SOleksandr Tymoshenko EVDEV_CLIENT_LOCKQ(client); 2102b3f6d66SOleksandr Tymoshenko 2112b3f6d66SOleksandr Tymoshenko if (EVDEV_CLIENT_EMPTYQ(client)) { 2122b3f6d66SOleksandr Tymoshenko if (ioflag & O_NONBLOCK) 2132b3f6d66SOleksandr Tymoshenko ret = EWOULDBLOCK; 2142b3f6d66SOleksandr Tymoshenko else { 2152b3f6d66SOleksandr Tymoshenko if (remaining != 0) { 2162b3f6d66SOleksandr Tymoshenko client->ec_blocked = true; 2172b3f6d66SOleksandr Tymoshenko ret = mtx_sleep(client, &client->ec_buffer_mtx, 2182b3f6d66SOleksandr Tymoshenko PCATCH, "evread", 0); 219006eb449SVladimir Kondratyev if (ret == 0 && client->ec_revoked) 220006eb449SVladimir Kondratyev ret = ENODEV; 2212b3f6d66SOleksandr Tymoshenko } 2222b3f6d66SOleksandr Tymoshenko } 2232b3f6d66SOleksandr Tymoshenko } 2242b3f6d66SOleksandr Tymoshenko 2252b3f6d66SOleksandr Tymoshenko while (ret == 0 && !EVDEV_CLIENT_EMPTYQ(client) && remaining > 0) { 22680ddbddfSVladimir Kondratyev head = client->ec_buffer + client->ec_buffer_head; 22780ddbddfSVladimir Kondratyev #ifdef COMPAT_FREEBSD32 22880ddbddfSVladimir Kondratyev if (SV_CURPROC_FLAG(SV_ILP32)) { 22980ddbddfSVladimir Kondratyev bzero(&event.t32, sizeof(struct input_event32)); 23080ddbddfSVladimir Kondratyev TV_CP(*head, event.t32, time); 23180ddbddfSVladimir Kondratyev CP(*head, event.t32, type); 23280ddbddfSVladimir Kondratyev CP(*head, event.t32, code); 23380ddbddfSVladimir Kondratyev CP(*head, event.t32, value); 23480ddbddfSVladimir Kondratyev } else 23580ddbddfSVladimir Kondratyev #endif 23680ddbddfSVladimir Kondratyev bcopy(head, &event.t, evsize); 23780ddbddfSVladimir Kondratyev 2382b3f6d66SOleksandr Tymoshenko client->ec_buffer_head = 2392b3f6d66SOleksandr Tymoshenko (client->ec_buffer_head + 1) % client->ec_buffer_size; 2402b3f6d66SOleksandr Tymoshenko remaining--; 2412b3f6d66SOleksandr Tymoshenko 2422b3f6d66SOleksandr Tymoshenko EVDEV_CLIENT_UNLOCKQ(client); 24380ddbddfSVladimir Kondratyev ret = uiomove(&event, evsize, uio); 2442b3f6d66SOleksandr Tymoshenko EVDEV_CLIENT_LOCKQ(client); 2452b3f6d66SOleksandr Tymoshenko } 2462b3f6d66SOleksandr Tymoshenko 2472b3f6d66SOleksandr Tymoshenko EVDEV_CLIENT_UNLOCKQ(client); 2482b3f6d66SOleksandr Tymoshenko 2492b3f6d66SOleksandr Tymoshenko return (ret); 2502b3f6d66SOleksandr Tymoshenko } 2512b3f6d66SOleksandr Tymoshenko 2522b3f6d66SOleksandr Tymoshenko static int 2532b3f6d66SOleksandr Tymoshenko evdev_write(struct cdev *dev, struct uio *uio, int ioflag) 2542b3f6d66SOleksandr Tymoshenko { 2552b3f6d66SOleksandr Tymoshenko struct evdev_dev *evdev = dev->si_drv1; 2562b3f6d66SOleksandr Tymoshenko struct evdev_client *client; 25780ddbddfSVladimir Kondratyev union { 25880ddbddfSVladimir Kondratyev struct input_event t; 25980ddbddfSVladimir Kondratyev #ifdef COMPAT_FREEBSD32 26080ddbddfSVladimir Kondratyev struct input_event32 t32; 26180ddbddfSVladimir Kondratyev #endif 26280ddbddfSVladimir Kondratyev } event; 26380ddbddfSVladimir Kondratyev size_t evsize; 2642b3f6d66SOleksandr Tymoshenko int ret = 0; 2652b3f6d66SOleksandr Tymoshenko 2662b3f6d66SOleksandr Tymoshenko ret = devfs_get_cdevpriv((void **)&client); 2672b3f6d66SOleksandr Tymoshenko if (ret != 0) 2682b3f6d66SOleksandr Tymoshenko return (ret); 2692b3f6d66SOleksandr Tymoshenko 2702b3f6d66SOleksandr Tymoshenko debugf(client, "write %zd bytes by thread %d", uio->uio_resid, 2712b3f6d66SOleksandr Tymoshenko uio->uio_td->td_tid); 2722b3f6d66SOleksandr Tymoshenko 2732b3f6d66SOleksandr Tymoshenko if (client->ec_revoked || evdev == NULL) 2742b3f6d66SOleksandr Tymoshenko return (ENODEV); 2752b3f6d66SOleksandr Tymoshenko 27680ddbddfSVladimir Kondratyev #ifdef COMPAT_FREEBSD32 27780ddbddfSVladimir Kondratyev if (SV_CURPROC_FLAG(SV_ILP32)) 27880ddbddfSVladimir Kondratyev evsize = sizeof(struct input_event32); 27980ddbddfSVladimir Kondratyev else 28080ddbddfSVladimir Kondratyev #endif 28180ddbddfSVladimir Kondratyev evsize = sizeof(struct input_event); 28280ddbddfSVladimir Kondratyev 28380ddbddfSVladimir Kondratyev if (uio->uio_resid % evsize != 0) { 2842b3f6d66SOleksandr Tymoshenko debugf(client, "write size not multiple of input_event size"); 2852b3f6d66SOleksandr Tymoshenko return (EINVAL); 2862b3f6d66SOleksandr Tymoshenko } 2872b3f6d66SOleksandr Tymoshenko 2882b3f6d66SOleksandr Tymoshenko while (uio->uio_resid > 0 && ret == 0) { 28980ddbddfSVladimir Kondratyev ret = uiomove(&event, evsize, uio); 29080ddbddfSVladimir Kondratyev if (ret == 0) { 29180ddbddfSVladimir Kondratyev #ifdef COMPAT_FREEBSD32 29280ddbddfSVladimir Kondratyev if (SV_CURPROC_FLAG(SV_ILP32)) 29380ddbddfSVladimir Kondratyev ret = evdev_inject_event(evdev, event.t32.type, 29480ddbddfSVladimir Kondratyev event.t32.code, event.t32.value); 29580ddbddfSVladimir Kondratyev else 29680ddbddfSVladimir Kondratyev #endif 29780ddbddfSVladimir Kondratyev ret = evdev_inject_event(evdev, event.t.type, 29880ddbddfSVladimir Kondratyev event.t.code, event.t.value); 29980ddbddfSVladimir Kondratyev } 3002b3f6d66SOleksandr Tymoshenko } 3012b3f6d66SOleksandr Tymoshenko 3022b3f6d66SOleksandr Tymoshenko return (ret); 3032b3f6d66SOleksandr Tymoshenko } 3042b3f6d66SOleksandr Tymoshenko 3052b3f6d66SOleksandr Tymoshenko static int 3062b3f6d66SOleksandr Tymoshenko evdev_poll(struct cdev *dev, int events, struct thread *td) 3072b3f6d66SOleksandr Tymoshenko { 3082b3f6d66SOleksandr Tymoshenko struct evdev_client *client; 3092b3f6d66SOleksandr Tymoshenko int ret; 3102b3f6d66SOleksandr Tymoshenko int revents = 0; 3112b3f6d66SOleksandr Tymoshenko 3122b3f6d66SOleksandr Tymoshenko ret = devfs_get_cdevpriv((void **)&client); 3132b3f6d66SOleksandr Tymoshenko if (ret != 0) 3142b3f6d66SOleksandr Tymoshenko return (POLLNVAL); 3152b3f6d66SOleksandr Tymoshenko 3162b3f6d66SOleksandr Tymoshenko debugf(client, "poll by thread %d", td->td_tid); 3172b3f6d66SOleksandr Tymoshenko 3182b3f6d66SOleksandr Tymoshenko if (client->ec_revoked) 3192b3f6d66SOleksandr Tymoshenko return (POLLHUP); 3202b3f6d66SOleksandr Tymoshenko 3212b3f6d66SOleksandr Tymoshenko if (events & (POLLIN | POLLRDNORM)) { 3222b3f6d66SOleksandr Tymoshenko EVDEV_CLIENT_LOCKQ(client); 3232b3f6d66SOleksandr Tymoshenko if (!EVDEV_CLIENT_EMPTYQ(client)) 3242b3f6d66SOleksandr Tymoshenko revents = events & (POLLIN | POLLRDNORM); 3252b3f6d66SOleksandr Tymoshenko else { 3262b3f6d66SOleksandr Tymoshenko client->ec_selected = true; 3272b3f6d66SOleksandr Tymoshenko selrecord(td, &client->ec_selp); 3282b3f6d66SOleksandr Tymoshenko } 3292b3f6d66SOleksandr Tymoshenko EVDEV_CLIENT_UNLOCKQ(client); 3302b3f6d66SOleksandr Tymoshenko } 3312b3f6d66SOleksandr Tymoshenko 3322b3f6d66SOleksandr Tymoshenko return (revents); 3332b3f6d66SOleksandr Tymoshenko } 3342b3f6d66SOleksandr Tymoshenko 3352b3f6d66SOleksandr Tymoshenko static int 3362b3f6d66SOleksandr Tymoshenko evdev_kqfilter(struct cdev *dev, struct knote *kn) 3372b3f6d66SOleksandr Tymoshenko { 3382b3f6d66SOleksandr Tymoshenko struct evdev_client *client; 3392b3f6d66SOleksandr Tymoshenko int ret; 3402b3f6d66SOleksandr Tymoshenko 3412b3f6d66SOleksandr Tymoshenko ret = devfs_get_cdevpriv((void **)&client); 3422b3f6d66SOleksandr Tymoshenko if (ret != 0) 3432b3f6d66SOleksandr Tymoshenko return (ret); 3442b3f6d66SOleksandr Tymoshenko 3452b3f6d66SOleksandr Tymoshenko if (client->ec_revoked) 3462b3f6d66SOleksandr Tymoshenko return (ENODEV); 3472b3f6d66SOleksandr Tymoshenko 3482b3f6d66SOleksandr Tymoshenko switch(kn->kn_filter) { 3492b3f6d66SOleksandr Tymoshenko case EVFILT_READ: 3502b3f6d66SOleksandr Tymoshenko kn->kn_fop = &evdev_cdev_filterops; 3512b3f6d66SOleksandr Tymoshenko break; 3522b3f6d66SOleksandr Tymoshenko default: 3532b3f6d66SOleksandr Tymoshenko return(EINVAL); 3542b3f6d66SOleksandr Tymoshenko } 3552b3f6d66SOleksandr Tymoshenko kn->kn_hook = (caddr_t)client; 3562b3f6d66SOleksandr Tymoshenko 3572b3f6d66SOleksandr Tymoshenko knlist_add(&client->ec_selp.si_note, kn, 0); 3582b3f6d66SOleksandr Tymoshenko return (0); 3592b3f6d66SOleksandr Tymoshenko } 3602b3f6d66SOleksandr Tymoshenko 3612b3f6d66SOleksandr Tymoshenko static int 3622b3f6d66SOleksandr Tymoshenko evdev_kqread(struct knote *kn, long hint) 3632b3f6d66SOleksandr Tymoshenko { 3642b3f6d66SOleksandr Tymoshenko struct evdev_client *client; 3652b3f6d66SOleksandr Tymoshenko int ret; 3662b3f6d66SOleksandr Tymoshenko 3672b3f6d66SOleksandr Tymoshenko client = (struct evdev_client *)kn->kn_hook; 3682b3f6d66SOleksandr Tymoshenko 3692b3f6d66SOleksandr Tymoshenko EVDEV_CLIENT_LOCKQ_ASSERT(client); 3702b3f6d66SOleksandr Tymoshenko 3712b3f6d66SOleksandr Tymoshenko if (client->ec_revoked) { 3722b3f6d66SOleksandr Tymoshenko kn->kn_flags |= EV_EOF; 3732b3f6d66SOleksandr Tymoshenko ret = 1; 3742b3f6d66SOleksandr Tymoshenko } else { 3752b3f6d66SOleksandr Tymoshenko kn->kn_data = EVDEV_CLIENT_SIZEQ(client) * 3762b3f6d66SOleksandr Tymoshenko sizeof(struct input_event); 3772b3f6d66SOleksandr Tymoshenko ret = !EVDEV_CLIENT_EMPTYQ(client); 3782b3f6d66SOleksandr Tymoshenko } 3792b3f6d66SOleksandr Tymoshenko return (ret); 3802b3f6d66SOleksandr Tymoshenko } 3812b3f6d66SOleksandr Tymoshenko 3822b3f6d66SOleksandr Tymoshenko static void 3832b3f6d66SOleksandr Tymoshenko evdev_kqdetach(struct knote *kn) 3842b3f6d66SOleksandr Tymoshenko { 3852b3f6d66SOleksandr Tymoshenko struct evdev_client *client; 3862b3f6d66SOleksandr Tymoshenko 3872b3f6d66SOleksandr Tymoshenko client = (struct evdev_client *)kn->kn_hook; 3882b3f6d66SOleksandr Tymoshenko knlist_remove(&client->ec_selp.si_note, kn, 0); 3892b3f6d66SOleksandr Tymoshenko } 3902b3f6d66SOleksandr Tymoshenko 3912b3f6d66SOleksandr Tymoshenko static int 3922b3f6d66SOleksandr Tymoshenko evdev_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, 3932b3f6d66SOleksandr Tymoshenko struct thread *td) 3942b3f6d66SOleksandr Tymoshenko { 3952b3f6d66SOleksandr Tymoshenko struct evdev_dev *evdev = dev->si_drv1; 3962b3f6d66SOleksandr Tymoshenko struct evdev_client *client; 3972b3f6d66SOleksandr Tymoshenko struct input_keymap_entry *ke; 3982b3f6d66SOleksandr Tymoshenko int ret, len, limit, type_num; 3992b3f6d66SOleksandr Tymoshenko uint32_t code; 4002b3f6d66SOleksandr Tymoshenko size_t nvalues; 4012b3f6d66SOleksandr Tymoshenko 4022b3f6d66SOleksandr Tymoshenko ret = devfs_get_cdevpriv((void **)&client); 4032b3f6d66SOleksandr Tymoshenko if (ret != 0) 4042b3f6d66SOleksandr Tymoshenko return (ret); 4052b3f6d66SOleksandr Tymoshenko 4062b3f6d66SOleksandr Tymoshenko if (client->ec_revoked || evdev == NULL) 4072b3f6d66SOleksandr Tymoshenko return (ENODEV); 4082b3f6d66SOleksandr Tymoshenko 4095ef24889SVladimir Kondratyev /* 4105ef24889SVladimir Kondratyev * Fix evdev state corrupted with discarding of kdb events. 4115ef24889SVladimir Kondratyev * EVIOCGKEY and EVIOCGLED ioctls can suffer from this. 4125ef24889SVladimir Kondratyev */ 4135ef24889SVladimir Kondratyev if (evdev->ev_kdb_active) { 4145ef24889SVladimir Kondratyev EVDEV_LOCK(evdev); 4155ef24889SVladimir Kondratyev if (evdev->ev_kdb_active) { 4165ef24889SVladimir Kondratyev evdev->ev_kdb_active = false; 4175ef24889SVladimir Kondratyev evdev_restore_after_kdb(evdev); 4185ef24889SVladimir Kondratyev } 4195ef24889SVladimir Kondratyev EVDEV_UNLOCK(evdev); 4205ef24889SVladimir Kondratyev } 4215ef24889SVladimir Kondratyev 4222b3f6d66SOleksandr Tymoshenko /* file I/O ioctl handling */ 4232b3f6d66SOleksandr Tymoshenko switch (cmd) { 4242b3f6d66SOleksandr Tymoshenko case FIOSETOWN: 4252b3f6d66SOleksandr Tymoshenko return (fsetown(*(int *)data, &client->ec_sigio)); 4262b3f6d66SOleksandr Tymoshenko 4272b3f6d66SOleksandr Tymoshenko case FIOGETOWN: 4282b3f6d66SOleksandr Tymoshenko *(int *)data = fgetown(&client->ec_sigio); 4292b3f6d66SOleksandr Tymoshenko return (0); 4302b3f6d66SOleksandr Tymoshenko 4312b3f6d66SOleksandr Tymoshenko case FIONBIO: 4322b3f6d66SOleksandr Tymoshenko return (0); 4332b3f6d66SOleksandr Tymoshenko 4342b3f6d66SOleksandr Tymoshenko case FIOASYNC: 4352b3f6d66SOleksandr Tymoshenko if (*(int *)data) 4362b3f6d66SOleksandr Tymoshenko client->ec_async = true; 4372b3f6d66SOleksandr Tymoshenko else 4382b3f6d66SOleksandr Tymoshenko client->ec_async = false; 4392b3f6d66SOleksandr Tymoshenko 4402b3f6d66SOleksandr Tymoshenko return (0); 4412b3f6d66SOleksandr Tymoshenko 4422b3f6d66SOleksandr Tymoshenko case FIONREAD: 4432b3f6d66SOleksandr Tymoshenko EVDEV_CLIENT_LOCKQ(client); 4442b3f6d66SOleksandr Tymoshenko *(int *)data = 4452b3f6d66SOleksandr Tymoshenko EVDEV_CLIENT_SIZEQ(client) * sizeof(struct input_event); 4462b3f6d66SOleksandr Tymoshenko EVDEV_CLIENT_UNLOCKQ(client); 4472b3f6d66SOleksandr Tymoshenko return (0); 4482b3f6d66SOleksandr Tymoshenko } 4492b3f6d66SOleksandr Tymoshenko 4502b3f6d66SOleksandr Tymoshenko len = IOCPARM_LEN(cmd); 4512b3f6d66SOleksandr Tymoshenko debugf(client, "ioctl called: cmd=0x%08lx, data=%p", cmd, data); 4522b3f6d66SOleksandr Tymoshenko 4532b3f6d66SOleksandr Tymoshenko /* evdev fixed-length ioctls handling */ 4542b3f6d66SOleksandr Tymoshenko switch (cmd) { 4552b3f6d66SOleksandr Tymoshenko case EVIOCGVERSION: 4562b3f6d66SOleksandr Tymoshenko *(int *)data = EV_VERSION; 4572b3f6d66SOleksandr Tymoshenko return (0); 4582b3f6d66SOleksandr Tymoshenko 4592b3f6d66SOleksandr Tymoshenko case EVIOCGID: 4602b3f6d66SOleksandr Tymoshenko debugf(client, "EVIOCGID: bus=%d vendor=0x%04x product=0x%04x", 4612b3f6d66SOleksandr Tymoshenko evdev->ev_id.bustype, evdev->ev_id.vendor, 4622b3f6d66SOleksandr Tymoshenko evdev->ev_id.product); 4632b3f6d66SOleksandr Tymoshenko memcpy(data, &evdev->ev_id, sizeof(struct input_id)); 4642b3f6d66SOleksandr Tymoshenko return (0); 4652b3f6d66SOleksandr Tymoshenko 4662b3f6d66SOleksandr Tymoshenko case EVIOCGREP: 4672b3f6d66SOleksandr Tymoshenko if (!evdev_event_supported(evdev, EV_REP)) 4682b3f6d66SOleksandr Tymoshenko return (ENOTSUP); 4692b3f6d66SOleksandr Tymoshenko 4702b3f6d66SOleksandr Tymoshenko memcpy(data, evdev->ev_rep, sizeof(evdev->ev_rep)); 4712b3f6d66SOleksandr Tymoshenko return (0); 4722b3f6d66SOleksandr Tymoshenko 4732b3f6d66SOleksandr Tymoshenko case EVIOCSREP: 4742b3f6d66SOleksandr Tymoshenko if (!evdev_event_supported(evdev, EV_REP)) 4752b3f6d66SOleksandr Tymoshenko return (ENOTSUP); 4762b3f6d66SOleksandr Tymoshenko 4772b3f6d66SOleksandr Tymoshenko evdev_inject_event(evdev, EV_REP, REP_DELAY, ((int *)data)[0]); 4782b3f6d66SOleksandr Tymoshenko evdev_inject_event(evdev, EV_REP, REP_PERIOD, 4792b3f6d66SOleksandr Tymoshenko ((int *)data)[1]); 4802b3f6d66SOleksandr Tymoshenko return (0); 4812b3f6d66SOleksandr Tymoshenko 4822b3f6d66SOleksandr Tymoshenko case EVIOCGKEYCODE: 4832b3f6d66SOleksandr Tymoshenko /* Fake unsupported ioctl */ 4842b3f6d66SOleksandr Tymoshenko return (0); 4852b3f6d66SOleksandr Tymoshenko 4862b3f6d66SOleksandr Tymoshenko case EVIOCGKEYCODE_V2: 4872b3f6d66SOleksandr Tymoshenko if (evdev->ev_methods == NULL || 4882b3f6d66SOleksandr Tymoshenko evdev->ev_methods->ev_get_keycode == NULL) 4892b3f6d66SOleksandr Tymoshenko return (ENOTSUP); 4902b3f6d66SOleksandr Tymoshenko 4912b3f6d66SOleksandr Tymoshenko ke = (struct input_keymap_entry *)data; 492911aed94SVladimir Kondratyev evdev->ev_methods->ev_get_keycode(evdev, ke); 4932b3f6d66SOleksandr Tymoshenko return (0); 4942b3f6d66SOleksandr Tymoshenko 4952b3f6d66SOleksandr Tymoshenko case EVIOCSKEYCODE: 4962b3f6d66SOleksandr Tymoshenko /* Fake unsupported ioctl */ 4972b3f6d66SOleksandr Tymoshenko return (0); 4982b3f6d66SOleksandr Tymoshenko 4992b3f6d66SOleksandr Tymoshenko case EVIOCSKEYCODE_V2: 5002b3f6d66SOleksandr Tymoshenko if (evdev->ev_methods == NULL || 5012b3f6d66SOleksandr Tymoshenko evdev->ev_methods->ev_set_keycode == NULL) 5022b3f6d66SOleksandr Tymoshenko return (ENOTSUP); 5032b3f6d66SOleksandr Tymoshenko 5042b3f6d66SOleksandr Tymoshenko ke = (struct input_keymap_entry *)data; 505911aed94SVladimir Kondratyev evdev->ev_methods->ev_set_keycode(evdev, ke); 5062b3f6d66SOleksandr Tymoshenko return (0); 5072b3f6d66SOleksandr Tymoshenko 5082b3f6d66SOleksandr Tymoshenko case EVIOCGABS(0) ... EVIOCGABS(ABS_MAX): 5092b3f6d66SOleksandr Tymoshenko if (evdev->ev_absinfo == NULL) 5102b3f6d66SOleksandr Tymoshenko return (EINVAL); 5112b3f6d66SOleksandr Tymoshenko 5122b3f6d66SOleksandr Tymoshenko memcpy(data, &evdev->ev_absinfo[cmd - EVIOCGABS(0)], 5132b3f6d66SOleksandr Tymoshenko sizeof(struct input_absinfo)); 5142b3f6d66SOleksandr Tymoshenko return (0); 5152b3f6d66SOleksandr Tymoshenko 5162b3f6d66SOleksandr Tymoshenko case EVIOCSABS(0) ... EVIOCSABS(ABS_MAX): 5172b3f6d66SOleksandr Tymoshenko if (evdev->ev_absinfo == NULL) 5182b3f6d66SOleksandr Tymoshenko return (EINVAL); 5192b3f6d66SOleksandr Tymoshenko 5202b3f6d66SOleksandr Tymoshenko code = cmd - EVIOCSABS(0); 5212b3f6d66SOleksandr Tymoshenko /* mt-slot number can not be changed */ 5222b3f6d66SOleksandr Tymoshenko if (code == ABS_MT_SLOT) 5232b3f6d66SOleksandr Tymoshenko return (EINVAL); 5242b3f6d66SOleksandr Tymoshenko 5252b3f6d66SOleksandr Tymoshenko EVDEV_LOCK(evdev); 5262b3f6d66SOleksandr Tymoshenko evdev_set_absinfo(evdev, code, (struct input_absinfo *)data); 5272b3f6d66SOleksandr Tymoshenko EVDEV_UNLOCK(evdev); 5282b3f6d66SOleksandr Tymoshenko return (0); 5292b3f6d66SOleksandr Tymoshenko 5302b3f6d66SOleksandr Tymoshenko case EVIOCSFF: 5312b3f6d66SOleksandr Tymoshenko case EVIOCRMFF: 5322b3f6d66SOleksandr Tymoshenko case EVIOCGEFFECTS: 5332b3f6d66SOleksandr Tymoshenko /* Fake unsupported ioctls */ 5342b3f6d66SOleksandr Tymoshenko return (0); 5352b3f6d66SOleksandr Tymoshenko 5362b3f6d66SOleksandr Tymoshenko case EVIOCGRAB: 5372b3f6d66SOleksandr Tymoshenko EVDEV_LOCK(evdev); 5382b3f6d66SOleksandr Tymoshenko if (*(int *)data) 5392b3f6d66SOleksandr Tymoshenko ret = evdev_grab_client(evdev, client); 5402b3f6d66SOleksandr Tymoshenko else 5412b3f6d66SOleksandr Tymoshenko ret = evdev_release_client(evdev, client); 5422b3f6d66SOleksandr Tymoshenko EVDEV_UNLOCK(evdev); 5432b3f6d66SOleksandr Tymoshenko return (ret); 5442b3f6d66SOleksandr Tymoshenko 5452b3f6d66SOleksandr Tymoshenko case EVIOCREVOKE: 5462b3f6d66SOleksandr Tymoshenko if (*(int *)data != 0) 5472b3f6d66SOleksandr Tymoshenko return (EINVAL); 5482b3f6d66SOleksandr Tymoshenko 549*d276eae6SVladimir Kondratyev EVDEV_LIST_LOCK(evdev); 5502b3f6d66SOleksandr Tymoshenko if (dev->si_drv1 != NULL && !client->ec_revoked) { 5512b3f6d66SOleksandr Tymoshenko evdev_dispose_client(evdev, client); 5522b3f6d66SOleksandr Tymoshenko evdev_revoke_client(client); 5532b3f6d66SOleksandr Tymoshenko } 554*d276eae6SVladimir Kondratyev EVDEV_LIST_UNLOCK(evdev); 5552b3f6d66SOleksandr Tymoshenko return (0); 5562b3f6d66SOleksandr Tymoshenko 5572b3f6d66SOleksandr Tymoshenko case EVIOCSCLOCKID: 5582b3f6d66SOleksandr Tymoshenko switch (*(int *)data) { 5592b3f6d66SOleksandr Tymoshenko case CLOCK_REALTIME: 5602b3f6d66SOleksandr Tymoshenko client->ec_clock_id = EV_CLOCK_REALTIME; 5612b3f6d66SOleksandr Tymoshenko return (0); 5622b3f6d66SOleksandr Tymoshenko case CLOCK_MONOTONIC: 5632b3f6d66SOleksandr Tymoshenko client->ec_clock_id = EV_CLOCK_MONOTONIC; 5642b3f6d66SOleksandr Tymoshenko return (0); 5652b3f6d66SOleksandr Tymoshenko default: 5662b3f6d66SOleksandr Tymoshenko return (EINVAL); 5672b3f6d66SOleksandr Tymoshenko } 5682b3f6d66SOleksandr Tymoshenko } 5692b3f6d66SOleksandr Tymoshenko 5702b3f6d66SOleksandr Tymoshenko /* evdev variable-length ioctls handling */ 5712b3f6d66SOleksandr Tymoshenko switch (IOCBASECMD(cmd)) { 5722b3f6d66SOleksandr Tymoshenko case EVIOCGNAME(0): 5732b3f6d66SOleksandr Tymoshenko strlcpy(data, evdev->ev_name, len); 5742b3f6d66SOleksandr Tymoshenko return (0); 5752b3f6d66SOleksandr Tymoshenko 5762b3f6d66SOleksandr Tymoshenko case EVIOCGPHYS(0): 5772b3f6d66SOleksandr Tymoshenko if (evdev->ev_shortname[0] == 0) 5782b3f6d66SOleksandr Tymoshenko return (ENOENT); 5792b3f6d66SOleksandr Tymoshenko 5802b3f6d66SOleksandr Tymoshenko strlcpy(data, evdev->ev_shortname, len); 5812b3f6d66SOleksandr Tymoshenko return (0); 5822b3f6d66SOleksandr Tymoshenko 5832b3f6d66SOleksandr Tymoshenko case EVIOCGUNIQ(0): 5842b3f6d66SOleksandr Tymoshenko if (evdev->ev_serial[0] == 0) 5852b3f6d66SOleksandr Tymoshenko return (ENOENT); 5862b3f6d66SOleksandr Tymoshenko 5872b3f6d66SOleksandr Tymoshenko strlcpy(data, evdev->ev_serial, len); 5882b3f6d66SOleksandr Tymoshenko return (0); 5892b3f6d66SOleksandr Tymoshenko 5902b3f6d66SOleksandr Tymoshenko case EVIOCGPROP(0): 5912b3f6d66SOleksandr Tymoshenko limit = MIN(len, bitstr_size(INPUT_PROP_CNT)); 5922b3f6d66SOleksandr Tymoshenko memcpy(data, evdev->ev_prop_flags, limit); 5932b3f6d66SOleksandr Tymoshenko return (0); 5942b3f6d66SOleksandr Tymoshenko 5952b3f6d66SOleksandr Tymoshenko case EVIOCGMTSLOTS(0): 5962b3f6d66SOleksandr Tymoshenko if (evdev->ev_mt == NULL) 5972b3f6d66SOleksandr Tymoshenko return (EINVAL); 5982b3f6d66SOleksandr Tymoshenko if (len < sizeof(uint32_t)) 5992b3f6d66SOleksandr Tymoshenko return (EINVAL); 6002b3f6d66SOleksandr Tymoshenko code = *(uint32_t *)data; 6012b3f6d66SOleksandr Tymoshenko if (!ABS_IS_MT(code)) 6022b3f6d66SOleksandr Tymoshenko return (EINVAL); 6032b3f6d66SOleksandr Tymoshenko 6042b3f6d66SOleksandr Tymoshenko nvalues = 6052b3f6d66SOleksandr Tymoshenko MIN(len / sizeof(int32_t) - 1, MAXIMAL_MT_SLOT(evdev) + 1); 6062b3f6d66SOleksandr Tymoshenko for (int i = 0; i < nvalues; i++) 6072b3f6d66SOleksandr Tymoshenko ((int32_t *)data)[i + 1] = 6082b3f6d66SOleksandr Tymoshenko evdev_get_mt_value(evdev, i, code); 6092b3f6d66SOleksandr Tymoshenko return (0); 6102b3f6d66SOleksandr Tymoshenko 6112b3f6d66SOleksandr Tymoshenko case EVIOCGKEY(0): 6122b3f6d66SOleksandr Tymoshenko limit = MIN(len, bitstr_size(KEY_CNT)); 6132b3f6d66SOleksandr Tymoshenko EVDEV_LOCK(evdev); 6142b3f6d66SOleksandr Tymoshenko evdev_client_filter_queue(client, EV_KEY); 6152b3f6d66SOleksandr Tymoshenko memcpy(data, evdev->ev_key_states, limit); 6162b3f6d66SOleksandr Tymoshenko EVDEV_UNLOCK(evdev); 6172b3f6d66SOleksandr Tymoshenko return (0); 6182b3f6d66SOleksandr Tymoshenko 6192b3f6d66SOleksandr Tymoshenko case EVIOCGLED(0): 6202b3f6d66SOleksandr Tymoshenko limit = MIN(len, bitstr_size(LED_CNT)); 6212b3f6d66SOleksandr Tymoshenko EVDEV_LOCK(evdev); 6222b3f6d66SOleksandr Tymoshenko evdev_client_filter_queue(client, EV_LED); 6232b3f6d66SOleksandr Tymoshenko memcpy(data, evdev->ev_led_states, limit); 6242b3f6d66SOleksandr Tymoshenko EVDEV_UNLOCK(evdev); 6252b3f6d66SOleksandr Tymoshenko return (0); 6262b3f6d66SOleksandr Tymoshenko 6272b3f6d66SOleksandr Tymoshenko case EVIOCGSND(0): 6282b3f6d66SOleksandr Tymoshenko limit = MIN(len, bitstr_size(SND_CNT)); 6292b3f6d66SOleksandr Tymoshenko EVDEV_LOCK(evdev); 6302b3f6d66SOleksandr Tymoshenko evdev_client_filter_queue(client, EV_SND); 6312b3f6d66SOleksandr Tymoshenko memcpy(data, evdev->ev_snd_states, limit); 6322b3f6d66SOleksandr Tymoshenko EVDEV_UNLOCK(evdev); 6332b3f6d66SOleksandr Tymoshenko return (0); 6342b3f6d66SOleksandr Tymoshenko 6352b3f6d66SOleksandr Tymoshenko case EVIOCGSW(0): 6362b3f6d66SOleksandr Tymoshenko limit = MIN(len, bitstr_size(SW_CNT)); 6372b3f6d66SOleksandr Tymoshenko EVDEV_LOCK(evdev); 6382b3f6d66SOleksandr Tymoshenko evdev_client_filter_queue(client, EV_SW); 6392b3f6d66SOleksandr Tymoshenko memcpy(data, evdev->ev_sw_states, limit); 6402b3f6d66SOleksandr Tymoshenko EVDEV_UNLOCK(evdev); 6412b3f6d66SOleksandr Tymoshenko return (0); 6422b3f6d66SOleksandr Tymoshenko 6432b3f6d66SOleksandr Tymoshenko case EVIOCGBIT(0, 0) ... EVIOCGBIT(EV_MAX, 0): 6442b3f6d66SOleksandr Tymoshenko type_num = IOCBASECMD(cmd) - EVIOCGBIT(0, 0); 6452b3f6d66SOleksandr Tymoshenko debugf(client, "EVIOCGBIT(%d): data=%p, len=%d", type_num, 6462b3f6d66SOleksandr Tymoshenko data, len); 6472b3f6d66SOleksandr Tymoshenko return (evdev_ioctl_eviocgbit(evdev, type_num, len, data)); 6482b3f6d66SOleksandr Tymoshenko } 6492b3f6d66SOleksandr Tymoshenko 6502b3f6d66SOleksandr Tymoshenko return (EINVAL); 6512b3f6d66SOleksandr Tymoshenko } 6522b3f6d66SOleksandr Tymoshenko 6532b3f6d66SOleksandr Tymoshenko static int 6542b3f6d66SOleksandr Tymoshenko evdev_ioctl_eviocgbit(struct evdev_dev *evdev, int type, int len, caddr_t data) 6552b3f6d66SOleksandr Tymoshenko { 6562b3f6d66SOleksandr Tymoshenko unsigned long *bitmap; 6572b3f6d66SOleksandr Tymoshenko int limit; 6582b3f6d66SOleksandr Tymoshenko 6592b3f6d66SOleksandr Tymoshenko switch (type) { 6602b3f6d66SOleksandr Tymoshenko case 0: 6612b3f6d66SOleksandr Tymoshenko bitmap = evdev->ev_type_flags; 6622b3f6d66SOleksandr Tymoshenko limit = EV_CNT; 6632b3f6d66SOleksandr Tymoshenko break; 6642b3f6d66SOleksandr Tymoshenko case EV_KEY: 6652b3f6d66SOleksandr Tymoshenko bitmap = evdev->ev_key_flags; 6662b3f6d66SOleksandr Tymoshenko limit = KEY_CNT; 6672b3f6d66SOleksandr Tymoshenko break; 6682b3f6d66SOleksandr Tymoshenko case EV_REL: 6692b3f6d66SOleksandr Tymoshenko bitmap = evdev->ev_rel_flags; 6702b3f6d66SOleksandr Tymoshenko limit = REL_CNT; 6712b3f6d66SOleksandr Tymoshenko break; 6722b3f6d66SOleksandr Tymoshenko case EV_ABS: 6732b3f6d66SOleksandr Tymoshenko bitmap = evdev->ev_abs_flags; 6742b3f6d66SOleksandr Tymoshenko limit = ABS_CNT; 6752b3f6d66SOleksandr Tymoshenko break; 6762b3f6d66SOleksandr Tymoshenko case EV_MSC: 6772b3f6d66SOleksandr Tymoshenko bitmap = evdev->ev_msc_flags; 6782b3f6d66SOleksandr Tymoshenko limit = MSC_CNT; 6792b3f6d66SOleksandr Tymoshenko break; 6802b3f6d66SOleksandr Tymoshenko case EV_LED: 6812b3f6d66SOleksandr Tymoshenko bitmap = evdev->ev_led_flags; 6822b3f6d66SOleksandr Tymoshenko limit = LED_CNT; 6832b3f6d66SOleksandr Tymoshenko break; 6842b3f6d66SOleksandr Tymoshenko case EV_SND: 6852b3f6d66SOleksandr Tymoshenko bitmap = evdev->ev_snd_flags; 6862b3f6d66SOleksandr Tymoshenko limit = SND_CNT; 6872b3f6d66SOleksandr Tymoshenko break; 6882b3f6d66SOleksandr Tymoshenko case EV_SW: 6892b3f6d66SOleksandr Tymoshenko bitmap = evdev->ev_sw_flags; 6902b3f6d66SOleksandr Tymoshenko limit = SW_CNT; 6912b3f6d66SOleksandr Tymoshenko break; 6922b3f6d66SOleksandr Tymoshenko case EV_FF: 6932b3f6d66SOleksandr Tymoshenko /* 6942b3f6d66SOleksandr Tymoshenko * We don't support EV_FF now, so let's 6952b3f6d66SOleksandr Tymoshenko * just fake it returning only zeros. 6962b3f6d66SOleksandr Tymoshenko */ 6972b3f6d66SOleksandr Tymoshenko bzero(data, len); 6982b3f6d66SOleksandr Tymoshenko return (0); 6992b3f6d66SOleksandr Tymoshenko default: 7002b3f6d66SOleksandr Tymoshenko return (ENOTTY); 7012b3f6d66SOleksandr Tymoshenko } 7022b3f6d66SOleksandr Tymoshenko 7032b3f6d66SOleksandr Tymoshenko /* 7042b3f6d66SOleksandr Tymoshenko * Clear ioctl data buffer in case it's bigger than 7052b3f6d66SOleksandr Tymoshenko * bitmap size 7062b3f6d66SOleksandr Tymoshenko */ 7072b3f6d66SOleksandr Tymoshenko bzero(data, len); 7082b3f6d66SOleksandr Tymoshenko 7092b3f6d66SOleksandr Tymoshenko limit = bitstr_size(limit); 7102b3f6d66SOleksandr Tymoshenko len = MIN(limit, len); 7112b3f6d66SOleksandr Tymoshenko memcpy(data, bitmap, len); 7122b3f6d66SOleksandr Tymoshenko return (0); 7132b3f6d66SOleksandr Tymoshenko } 7142b3f6d66SOleksandr Tymoshenko 7152b3f6d66SOleksandr Tymoshenko void 7162b3f6d66SOleksandr Tymoshenko evdev_revoke_client(struct evdev_client *client) 7172b3f6d66SOleksandr Tymoshenko { 7182b3f6d66SOleksandr Tymoshenko 719*d276eae6SVladimir Kondratyev EVDEV_LIST_LOCK_ASSERT(client->ec_evdev); 7202b3f6d66SOleksandr Tymoshenko 7212b3f6d66SOleksandr Tymoshenko client->ec_revoked = true; 7222b3f6d66SOleksandr Tymoshenko } 7232b3f6d66SOleksandr Tymoshenko 7242b3f6d66SOleksandr Tymoshenko void 7252b3f6d66SOleksandr Tymoshenko evdev_notify_event(struct evdev_client *client) 7262b3f6d66SOleksandr Tymoshenko { 7272b3f6d66SOleksandr Tymoshenko 7282b3f6d66SOleksandr Tymoshenko EVDEV_CLIENT_LOCKQ_ASSERT(client); 7292b3f6d66SOleksandr Tymoshenko 7302b3f6d66SOleksandr Tymoshenko if (client->ec_blocked) { 7312b3f6d66SOleksandr Tymoshenko client->ec_blocked = false; 7322b3f6d66SOleksandr Tymoshenko wakeup(client); 7332b3f6d66SOleksandr Tymoshenko } 7342b3f6d66SOleksandr Tymoshenko if (client->ec_selected) { 7352b3f6d66SOleksandr Tymoshenko client->ec_selected = false; 7362b3f6d66SOleksandr Tymoshenko selwakeup(&client->ec_selp); 7372b3f6d66SOleksandr Tymoshenko } 7382b3f6d66SOleksandr Tymoshenko KNOTE_LOCKED(&client->ec_selp.si_note, 0); 7392b3f6d66SOleksandr Tymoshenko 7402b3f6d66SOleksandr Tymoshenko if (client->ec_async && client->ec_sigio != NULL) 7412b3f6d66SOleksandr Tymoshenko pgsigio(&client->ec_sigio, SIGIO, 0); 7422b3f6d66SOleksandr Tymoshenko } 7432b3f6d66SOleksandr Tymoshenko 7442b3f6d66SOleksandr Tymoshenko int 7452b3f6d66SOleksandr Tymoshenko evdev_cdev_create(struct evdev_dev *evdev) 7462b3f6d66SOleksandr Tymoshenko { 7472b3f6d66SOleksandr Tymoshenko struct make_dev_args mda; 7482b3f6d66SOleksandr Tymoshenko int ret, unit = 0; 7492b3f6d66SOleksandr Tymoshenko 7502b3f6d66SOleksandr Tymoshenko make_dev_args_init(&mda); 7512b3f6d66SOleksandr Tymoshenko mda.mda_flags = MAKEDEV_WAITOK | MAKEDEV_CHECKNAME; 7522b3f6d66SOleksandr Tymoshenko mda.mda_devsw = &evdev_cdevsw; 7532b3f6d66SOleksandr Tymoshenko mda.mda_uid = UID_ROOT; 7542b3f6d66SOleksandr Tymoshenko mda.mda_gid = GID_WHEEL; 7552b3f6d66SOleksandr Tymoshenko mda.mda_mode = 0600; 7562b3f6d66SOleksandr Tymoshenko mda.mda_si_drv1 = evdev; 7572b3f6d66SOleksandr Tymoshenko 7582b3f6d66SOleksandr Tymoshenko /* Try to coexist with cuse-backed input/event devices */ 7592b3f6d66SOleksandr Tymoshenko while ((ret = make_dev_s(&mda, &evdev->ev_cdev, "input/event%d", unit)) 7602b3f6d66SOleksandr Tymoshenko == EEXIST) 7612b3f6d66SOleksandr Tymoshenko unit++; 7622b3f6d66SOleksandr Tymoshenko 7632b3f6d66SOleksandr Tymoshenko if (ret == 0) 7642b3f6d66SOleksandr Tymoshenko evdev->ev_unit = unit; 7652b3f6d66SOleksandr Tymoshenko 7662b3f6d66SOleksandr Tymoshenko return (ret); 7672b3f6d66SOleksandr Tymoshenko } 7682b3f6d66SOleksandr Tymoshenko 7692b3f6d66SOleksandr Tymoshenko int 7702b3f6d66SOleksandr Tymoshenko evdev_cdev_destroy(struct evdev_dev *evdev) 7712b3f6d66SOleksandr Tymoshenko { 7722b3f6d66SOleksandr Tymoshenko 7732b3f6d66SOleksandr Tymoshenko destroy_dev(evdev->ev_cdev); 7742b3f6d66SOleksandr Tymoshenko return (0); 7752b3f6d66SOleksandr Tymoshenko } 7762b3f6d66SOleksandr Tymoshenko 7772b3f6d66SOleksandr Tymoshenko static void 7782b3f6d66SOleksandr Tymoshenko evdev_client_gettime(struct evdev_client *client, struct timeval *tv) 7792b3f6d66SOleksandr Tymoshenko { 7802b3f6d66SOleksandr Tymoshenko 7812b3f6d66SOleksandr Tymoshenko switch (client->ec_clock_id) { 7822b3f6d66SOleksandr Tymoshenko case EV_CLOCK_BOOTTIME: 7832b3f6d66SOleksandr Tymoshenko /* 7842b3f6d66SOleksandr Tymoshenko * XXX: FreeBSD does not support true POSIX monotonic clock. 7852b3f6d66SOleksandr Tymoshenko * So aliase EV_CLOCK_BOOTTIME to EV_CLOCK_MONOTONIC. 7862b3f6d66SOleksandr Tymoshenko */ 7872b3f6d66SOleksandr Tymoshenko case EV_CLOCK_MONOTONIC: 7882b3f6d66SOleksandr Tymoshenko microuptime(tv); 7892b3f6d66SOleksandr Tymoshenko break; 7902b3f6d66SOleksandr Tymoshenko 7912b3f6d66SOleksandr Tymoshenko case EV_CLOCK_REALTIME: 7922b3f6d66SOleksandr Tymoshenko default: 7932b3f6d66SOleksandr Tymoshenko microtime(tv); 7942b3f6d66SOleksandr Tymoshenko break; 7952b3f6d66SOleksandr Tymoshenko } 7962b3f6d66SOleksandr Tymoshenko } 7972b3f6d66SOleksandr Tymoshenko 7982b3f6d66SOleksandr Tymoshenko void 7992b3f6d66SOleksandr Tymoshenko evdev_client_push(struct evdev_client *client, uint16_t type, uint16_t code, 8002b3f6d66SOleksandr Tymoshenko int32_t value) 8012b3f6d66SOleksandr Tymoshenko { 8022b3f6d66SOleksandr Tymoshenko struct timeval time; 8032b3f6d66SOleksandr Tymoshenko size_t count, head, tail, ready; 8042b3f6d66SOleksandr Tymoshenko 8052b3f6d66SOleksandr Tymoshenko EVDEV_CLIENT_LOCKQ_ASSERT(client); 8062b3f6d66SOleksandr Tymoshenko head = client->ec_buffer_head; 8072b3f6d66SOleksandr Tymoshenko tail = client->ec_buffer_tail; 8082b3f6d66SOleksandr Tymoshenko ready = client->ec_buffer_ready; 8092b3f6d66SOleksandr Tymoshenko count = client->ec_buffer_size; 8102b3f6d66SOleksandr Tymoshenko 8112b3f6d66SOleksandr Tymoshenko /* If queue is full drop its content and place SYN_DROPPED event */ 8122b3f6d66SOleksandr Tymoshenko if ((tail + 1) % count == head) { 8132b3f6d66SOleksandr Tymoshenko debugf(client, "client %p: buffer overflow", client); 8142b3f6d66SOleksandr Tymoshenko 8152b3f6d66SOleksandr Tymoshenko head = (tail + count - 1) % count; 8162b3f6d66SOleksandr Tymoshenko client->ec_buffer[head] = (struct input_event) { 8172b3f6d66SOleksandr Tymoshenko .type = EV_SYN, 8182b3f6d66SOleksandr Tymoshenko .code = SYN_DROPPED, 8192b3f6d66SOleksandr Tymoshenko .value = 0 8202b3f6d66SOleksandr Tymoshenko }; 8212b3f6d66SOleksandr Tymoshenko /* 8222b3f6d66SOleksandr Tymoshenko * XXX: Here is a small race window from now till the end of 8232b3f6d66SOleksandr Tymoshenko * report. The queue is empty but client has been already 8242b3f6d66SOleksandr Tymoshenko * notified of data readyness. Can be fixed in two ways: 8252b3f6d66SOleksandr Tymoshenko * 1. Implement bulk insert so queue lock would not be dropped 8262b3f6d66SOleksandr Tymoshenko * till the SYN_REPORT event. 8272b3f6d66SOleksandr Tymoshenko * 2. Insert SYN_REPORT just now and skip remaining events 8282b3f6d66SOleksandr Tymoshenko */ 8292b3f6d66SOleksandr Tymoshenko client->ec_buffer_head = head; 8302b3f6d66SOleksandr Tymoshenko client->ec_buffer_ready = head; 8312b3f6d66SOleksandr Tymoshenko } 8322b3f6d66SOleksandr Tymoshenko 8332b3f6d66SOleksandr Tymoshenko client->ec_buffer[tail].type = type; 8342b3f6d66SOleksandr Tymoshenko client->ec_buffer[tail].code = code; 8352b3f6d66SOleksandr Tymoshenko client->ec_buffer[tail].value = value; 8362b3f6d66SOleksandr Tymoshenko client->ec_buffer_tail = (tail + 1) % count; 8372b3f6d66SOleksandr Tymoshenko 8382b3f6d66SOleksandr Tymoshenko /* Allow users to read events only after report has been completed */ 8392b3f6d66SOleksandr Tymoshenko if (type == EV_SYN && code == SYN_REPORT) { 8402b3f6d66SOleksandr Tymoshenko evdev_client_gettime(client, &time); 8412b3f6d66SOleksandr Tymoshenko for (; ready != client->ec_buffer_tail; 8422b3f6d66SOleksandr Tymoshenko ready = (ready + 1) % count) 8432b3f6d66SOleksandr Tymoshenko client->ec_buffer[ready].time = time; 8442b3f6d66SOleksandr Tymoshenko client->ec_buffer_ready = client->ec_buffer_tail; 8452b3f6d66SOleksandr Tymoshenko } 8462b3f6d66SOleksandr Tymoshenko } 8472b3f6d66SOleksandr Tymoshenko 8482b3f6d66SOleksandr Tymoshenko void 8492b3f6d66SOleksandr Tymoshenko evdev_client_dumpqueue(struct evdev_client *client) 8502b3f6d66SOleksandr Tymoshenko { 8512b3f6d66SOleksandr Tymoshenko struct input_event *event; 8522b3f6d66SOleksandr Tymoshenko size_t i, head, tail, ready, size; 8532b3f6d66SOleksandr Tymoshenko 8542b3f6d66SOleksandr Tymoshenko head = client->ec_buffer_head; 8552b3f6d66SOleksandr Tymoshenko tail = client->ec_buffer_tail; 8562b3f6d66SOleksandr Tymoshenko ready = client->ec_buffer_ready; 8572b3f6d66SOleksandr Tymoshenko size = client->ec_buffer_size; 8582b3f6d66SOleksandr Tymoshenko 8592b3f6d66SOleksandr Tymoshenko printf("evdev client: %p\n", client); 8602b3f6d66SOleksandr Tymoshenko printf("event queue: head=%zu ready=%zu tail=%zu size=%zu\n", 8612b3f6d66SOleksandr Tymoshenko head, ready, tail, size); 8622b3f6d66SOleksandr Tymoshenko 8632b3f6d66SOleksandr Tymoshenko printf("queue contents:\n"); 8642b3f6d66SOleksandr Tymoshenko 8652b3f6d66SOleksandr Tymoshenko for (i = 0; i < size; i++) { 8662b3f6d66SOleksandr Tymoshenko event = &client->ec_buffer[i]; 8672b3f6d66SOleksandr Tymoshenko printf("%zu: ", i); 8682b3f6d66SOleksandr Tymoshenko 8692b3f6d66SOleksandr Tymoshenko if (i < head || i > tail) 8702b3f6d66SOleksandr Tymoshenko printf("unused\n"); 8712b3f6d66SOleksandr Tymoshenko else 8722b3f6d66SOleksandr Tymoshenko printf("type=%d code=%d value=%d ", event->type, 8732b3f6d66SOleksandr Tymoshenko event->code, event->value); 8742b3f6d66SOleksandr Tymoshenko 8752b3f6d66SOleksandr Tymoshenko if (i == head) 8762b3f6d66SOleksandr Tymoshenko printf("<- head\n"); 8772b3f6d66SOleksandr Tymoshenko else if (i == tail) 8782b3f6d66SOleksandr Tymoshenko printf("<- tail\n"); 8792b3f6d66SOleksandr Tymoshenko else if (i == ready) 8802b3f6d66SOleksandr Tymoshenko printf("<- ready\n"); 8812b3f6d66SOleksandr Tymoshenko else 8822b3f6d66SOleksandr Tymoshenko printf("\n"); 8832b3f6d66SOleksandr Tymoshenko } 8842b3f6d66SOleksandr Tymoshenko } 8852b3f6d66SOleksandr Tymoshenko 8862b3f6d66SOleksandr Tymoshenko static void 8872b3f6d66SOleksandr Tymoshenko evdev_client_filter_queue(struct evdev_client *client, uint16_t type) 8882b3f6d66SOleksandr Tymoshenko { 8892b3f6d66SOleksandr Tymoshenko struct input_event *event; 8902b3f6d66SOleksandr Tymoshenko size_t head, tail, count, i; 8912b3f6d66SOleksandr Tymoshenko bool last_was_syn = false; 8922b3f6d66SOleksandr Tymoshenko 8932b3f6d66SOleksandr Tymoshenko EVDEV_CLIENT_LOCKQ(client); 8942b3f6d66SOleksandr Tymoshenko 8952b3f6d66SOleksandr Tymoshenko i = head = client->ec_buffer_head; 8962b3f6d66SOleksandr Tymoshenko tail = client->ec_buffer_tail; 8972b3f6d66SOleksandr Tymoshenko count = client->ec_buffer_size; 8982b3f6d66SOleksandr Tymoshenko client->ec_buffer_ready = client->ec_buffer_tail; 8992b3f6d66SOleksandr Tymoshenko 9002b3f6d66SOleksandr Tymoshenko while (i != client->ec_buffer_tail) { 9012b3f6d66SOleksandr Tymoshenko event = &client->ec_buffer[i]; 9022b3f6d66SOleksandr Tymoshenko i = (i + 1) % count; 9032b3f6d66SOleksandr Tymoshenko 9042b3f6d66SOleksandr Tymoshenko /* Skip event of given type */ 9052b3f6d66SOleksandr Tymoshenko if (event->type == type) 9062b3f6d66SOleksandr Tymoshenko continue; 9072b3f6d66SOleksandr Tymoshenko 9082b3f6d66SOleksandr Tymoshenko /* Remove empty SYN_REPORT events */ 9092b3f6d66SOleksandr Tymoshenko if (event->type == EV_SYN && event->code == SYN_REPORT) { 9102b3f6d66SOleksandr Tymoshenko if (last_was_syn) 9112b3f6d66SOleksandr Tymoshenko continue; 9122b3f6d66SOleksandr Tymoshenko else 9132b3f6d66SOleksandr Tymoshenko client->ec_buffer_ready = (tail + 1) % count; 9142b3f6d66SOleksandr Tymoshenko } 9152b3f6d66SOleksandr Tymoshenko 9162b3f6d66SOleksandr Tymoshenko /* Rewrite entry */ 9172b3f6d66SOleksandr Tymoshenko memcpy(&client->ec_buffer[tail], event, 9182b3f6d66SOleksandr Tymoshenko sizeof(struct input_event)); 9192b3f6d66SOleksandr Tymoshenko 9202b3f6d66SOleksandr Tymoshenko last_was_syn = (event->type == EV_SYN && 9212b3f6d66SOleksandr Tymoshenko event->code == SYN_REPORT); 9222b3f6d66SOleksandr Tymoshenko 9232b3f6d66SOleksandr Tymoshenko tail = (tail + 1) % count; 9242b3f6d66SOleksandr Tymoshenko } 9252b3f6d66SOleksandr Tymoshenko 9262b3f6d66SOleksandr Tymoshenko client->ec_buffer_head = i; 9272b3f6d66SOleksandr Tymoshenko client->ec_buffer_tail = tail; 9282b3f6d66SOleksandr Tymoshenko 9292b3f6d66SOleksandr Tymoshenko EVDEV_CLIENT_UNLOCKQ(client); 9302b3f6d66SOleksandr Tymoshenko } 931