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 #ifndef _DEV_EVDEV_EVDEV_PRIVATE_H 31 #define _DEV_EVDEV_EVDEV_PRIVATE_H 32 33 #include <sys/bitstring.h> 34 #include <sys/kbio.h> 35 #include <sys/malloc.h> 36 #include <sys/queue.h> 37 #include <sys/selinfo.h> 38 #include <sys/sysctl.h> 39 40 #include <dev/evdev/evdev.h> 41 #include <dev/evdev/input.h> 42 #include <dev/kbd/kbdreg.h> 43 44 #define NAMELEN 80 45 46 /* 47 * bitstr_t implementation must be identical to one found in EVIOCG* 48 * libevdev ioctls. Our bitstring(3) API is compatible since r299090. 49 */ 50 _Static_assert(sizeof(bitstr_t) == sizeof(unsigned long), 51 "bitstr_t size mismatch"); 52 53 MALLOC_DECLARE(M_EVDEV); 54 55 struct evdev_client; 56 struct evdev_mt; 57 58 #define CURRENT_MT_SLOT(evdev) ((evdev)->ev_absinfo[ABS_MT_SLOT].value) 59 #define MAXIMAL_MT_SLOT(evdev) ((evdev)->ev_absinfo[ABS_MT_SLOT].maximum) 60 61 enum evdev_key_events 62 { 63 KEY_EVENT_UP, 64 KEY_EVENT_DOWN, 65 KEY_EVENT_REPEAT 66 }; 67 68 /* evdev clock IDs in Linux semantic */ 69 enum evdev_clock_id 70 { 71 EV_CLOCK_REALTIME = 0, /* UTC clock */ 72 EV_CLOCK_MONOTONIC, /* monotonic, stops on suspend */ 73 EV_CLOCK_BOOTTIME /* monotonic, suspend-awared */ 74 }; 75 76 enum evdev_lock_type 77 { 78 EV_LOCK_INTERNAL = 0, /* Internal evdev mutex */ 79 EV_LOCK_MTX, /* Driver`s mutex */ 80 }; 81 82 struct evdev_dev 83 { 84 char ev_name[NAMELEN]; 85 char ev_shortname[NAMELEN]; 86 char ev_serial[NAMELEN]; 87 struct cdev * ev_cdev; 88 int ev_unit; 89 enum evdev_lock_type ev_lock_type; 90 struct mtx * ev_lock; 91 struct mtx ev_mtx; 92 struct input_id ev_id; 93 struct evdev_client * ev_grabber; 94 size_t ev_report_size; 95 96 /* Supported features: */ 97 bitstr_t bit_decl(ev_prop_flags, INPUT_PROP_CNT); 98 bitstr_t bit_decl(ev_type_flags, EV_CNT); 99 bitstr_t bit_decl(ev_key_flags, KEY_CNT); 100 bitstr_t bit_decl(ev_rel_flags, REL_CNT); 101 bitstr_t bit_decl(ev_abs_flags, ABS_CNT); 102 bitstr_t bit_decl(ev_msc_flags, MSC_CNT); 103 bitstr_t bit_decl(ev_led_flags, LED_CNT); 104 bitstr_t bit_decl(ev_snd_flags, SND_CNT); 105 bitstr_t bit_decl(ev_sw_flags, SW_CNT); 106 struct input_absinfo * ev_absinfo; 107 bitstr_t bit_decl(ev_flags, EVDEV_FLAG_CNT); 108 109 /* Repeat parameters & callout: */ 110 int ev_rep[REP_CNT]; 111 struct callout ev_rep_callout; 112 uint16_t ev_rep_key; 113 114 /* State: */ 115 bitstr_t bit_decl(ev_key_states, KEY_CNT); 116 bitstr_t bit_decl(ev_led_states, LED_CNT); 117 bitstr_t bit_decl(ev_snd_states, SND_CNT); 118 bitstr_t bit_decl(ev_sw_states, SW_CNT); 119 bool ev_report_opened; 120 121 /* Multitouch protocol type B state: */ 122 struct evdev_mt * ev_mt; 123 124 /* Counters: */ 125 uint64_t ev_event_count; 126 uint64_t ev_report_count; 127 128 /* Parent driver callbacks: */ 129 const struct evdev_methods * ev_methods; 130 void * ev_softc; 131 132 /* Sysctl: */ 133 struct sysctl_ctx_list ev_sysctl_ctx; 134 135 LIST_ENTRY(evdev_dev) ev_link; 136 LIST_HEAD(, evdev_client) ev_clients; 137 }; 138 139 #define EVDEV_LOCK(evdev) mtx_lock((evdev)->ev_lock) 140 #define EVDEV_UNLOCK(evdev) mtx_unlock((evdev)->ev_lock) 141 #define EVDEV_LOCK_ASSERT(evdev) mtx_assert((evdev)->ev_lock, MA_OWNED) 142 #define EVDEV_ENTER(evdev) do { \ 143 if ((evdev)->ev_lock_type == EV_LOCK_INTERNAL) \ 144 EVDEV_LOCK(evdev); \ 145 else \ 146 EVDEV_LOCK_ASSERT(evdev); \ 147 } while (0) 148 #define EVDEV_EXIT(evdev) do { \ 149 if ((evdev)->ev_lock_type == EV_LOCK_INTERNAL) \ 150 EVDEV_UNLOCK(evdev); \ 151 } while (0) 152 153 struct evdev_client 154 { 155 struct evdev_dev * ec_evdev; 156 struct mtx ec_buffer_mtx; 157 size_t ec_buffer_size; 158 size_t ec_buffer_head; 159 size_t ec_buffer_tail; 160 size_t ec_buffer_ready; 161 enum evdev_clock_id ec_clock_id; 162 struct selinfo ec_selp; 163 struct sigio * ec_sigio; 164 bool ec_async; 165 bool ec_revoked; 166 bool ec_blocked; 167 bool ec_selected; 168 169 LIST_ENTRY(evdev_client) ec_link; 170 171 struct input_event ec_buffer[]; 172 }; 173 174 #define EVDEV_CLIENT_LOCKQ(client) mtx_lock(&(client)->ec_buffer_mtx) 175 #define EVDEV_CLIENT_UNLOCKQ(client) mtx_unlock(&(client)->ec_buffer_mtx) 176 #define EVDEV_CLIENT_LOCKQ_ASSERT(client) \ 177 mtx_assert(&(client)->ec_buffer_mtx, MA_OWNED) 178 #define EVDEV_CLIENT_EMPTYQ(client) \ 179 ((client)->ec_buffer_head == (client)->ec_buffer_ready) 180 #define EVDEV_CLIENT_SIZEQ(client) \ 181 (((client)->ec_buffer_ready + (client)->ec_buffer_size - \ 182 (client)->ec_buffer_head) % (client)->ec_buffer_size) 183 184 /* Input device interface: */ 185 void evdev_send_event(struct evdev_dev *, uint16_t, uint16_t, int32_t); 186 int evdev_inject_event(struct evdev_dev *, uint16_t, uint16_t, int32_t); 187 int evdev_cdev_create(struct evdev_dev *); 188 int evdev_cdev_destroy(struct evdev_dev *); 189 bool evdev_event_supported(struct evdev_dev *, uint16_t); 190 void evdev_set_abs_bit(struct evdev_dev *, uint16_t); 191 void evdev_set_absinfo(struct evdev_dev *, uint16_t, struct input_absinfo *); 192 193 /* Client interface: */ 194 int evdev_register_client(struct evdev_dev *, struct evdev_client *); 195 void evdev_dispose_client(struct evdev_dev *, struct evdev_client *); 196 int evdev_grab_client(struct evdev_dev *, struct evdev_client *); 197 int evdev_release_client(struct evdev_dev *, struct evdev_client *); 198 void evdev_client_push(struct evdev_client *, uint16_t, uint16_t, int32_t); 199 void evdev_notify_event(struct evdev_client *); 200 void evdev_revoke_client(struct evdev_client *); 201 202 /* Multitouch related functions: */ 203 void evdev_mt_init(struct evdev_dev *); 204 void evdev_mt_free(struct evdev_dev *); 205 int32_t evdev_get_last_mt_slot(struct evdev_dev *); 206 void evdev_set_last_mt_slot(struct evdev_dev *, int32_t); 207 int32_t evdev_get_mt_value(struct evdev_dev *, int32_t, int16_t); 208 void evdev_set_mt_value(struct evdev_dev *, int32_t, int16_t, int32_t); 209 void evdev_send_mt_compat(struct evdev_dev *); 210 void evdev_send_mt_autorel(struct evdev_dev *); 211 212 /* Utility functions: */ 213 void evdev_client_dumpqueue(struct evdev_client *); 214 215 #endif /* _DEV_EVDEV_EVDEV_PRIVATE_H */ 216