1b9b347e9SVladimir Kondratyev /*- 2cb022db8SVladimir Kondratyev * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3cb022db8SVladimir Kondratyev * 4cb022db8SVladimir Kondratyev * Copyright (c) 2014-2020 Vladimir Kondratyev <wulf@FreeBSD.org> 5b9b347e9SVladimir Kondratyev * 6b9b347e9SVladimir Kondratyev * Redistribution and use in source and binary forms, with or without 7b9b347e9SVladimir Kondratyev * modification, are permitted provided that the following conditions 8b9b347e9SVladimir Kondratyev * are met: 9b9b347e9SVladimir Kondratyev * 1. Redistributions of source code must retain the above copyright 10b9b347e9SVladimir Kondratyev * notice, this list of conditions and the following disclaimer. 11b9b347e9SVladimir Kondratyev * 2. Redistributions in binary form must reproduce the above copyright 12b9b347e9SVladimir Kondratyev * notice, this list of conditions and the following disclaimer in the 13b9b347e9SVladimir Kondratyev * documentation and/or other materials provided with the distribution. 14b9b347e9SVladimir Kondratyev * 15b9b347e9SVladimir Kondratyev * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16b9b347e9SVladimir Kondratyev * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17b9b347e9SVladimir Kondratyev * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18b9b347e9SVladimir Kondratyev * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19b9b347e9SVladimir Kondratyev * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20b9b347e9SVladimir Kondratyev * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21b9b347e9SVladimir Kondratyev * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22b9b347e9SVladimir Kondratyev * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23b9b347e9SVladimir Kondratyev * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24b9b347e9SVladimir Kondratyev * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25b9b347e9SVladimir Kondratyev * SUCH DAMAGE. 26b9b347e9SVladimir Kondratyev */ 27b9b347e9SVladimir Kondratyev 28b9b347e9SVladimir Kondratyev #include <sys/cdefs.h> 29b9b347e9SVladimir Kondratyev __FBSDID("$FreeBSD$"); 30b9b347e9SVladimir Kondratyev 31b9b347e9SVladimir Kondratyev /* 32cb022db8SVladimir Kondratyev * MS Windows 7/8/10 compatible HID Multi-touch Device driver. 33b9b347e9SVladimir Kondratyev * https://msdn.microsoft.com/en-us/library/windows/hardware/jj151569(v=vs.85).aspx 34cb022db8SVladimir Kondratyev * http://download.microsoft.com/download/7/d/d/7dd44bb7-2a7a-4505-ac1c-7227d3d96d5b/hid-over-i2c-protocol-spec-v1-0.docx 35b9b347e9SVladimir Kondratyev * https://www.kernel.org/doc/Documentation/input/multi-touch-protocol.txt 36b9b347e9SVladimir Kondratyev */ 37b9b347e9SVladimir Kondratyev 385cc21ab9SVladimir Kondratyev #include "opt_hid.h" 395cc21ab9SVladimir Kondratyev 40b9b347e9SVladimir Kondratyev #include <sys/param.h> 41b9b347e9SVladimir Kondratyev #include <sys/bus.h> 42b9b347e9SVladimir Kondratyev #include <sys/kernel.h> 43b9b347e9SVladimir Kondratyev #include <sys/lock.h> 44b9b347e9SVladimir Kondratyev #include <sys/malloc.h> 45b9b347e9SVladimir Kondratyev #include <sys/module.h> 46b9b347e9SVladimir Kondratyev #include <sys/mutex.h> 47b9b347e9SVladimir Kondratyev #include <sys/sysctl.h> 48b9b347e9SVladimir Kondratyev #include <sys/systm.h> 49b9b347e9SVladimir Kondratyev 50b9b347e9SVladimir Kondratyev #include <dev/evdev/evdev.h> 51b9b347e9SVladimir Kondratyev #include <dev/evdev/input.h> 52b9b347e9SVladimir Kondratyev 53cb022db8SVladimir Kondratyev #define HID_DEBUG_VAR hmt_debug 54cb022db8SVladimir Kondratyev #include <dev/hid/hid.h> 55cb022db8SVladimir Kondratyev #include <dev/hid/hidbus.h> 56cb022db8SVladimir Kondratyev #include <dev/hid/hidquirk.h> 57b9b347e9SVladimir Kondratyev 58cb022db8SVladimir Kondratyev #include <dev/hid/hconf.h> 59cb022db8SVladimir Kondratyev 60cb022db8SVladimir Kondratyev static SYSCTL_NODE(_hw_hid, OID_AUTO, hmt, CTLFLAG_RW, 0, 61cb022db8SVladimir Kondratyev "MSWindows 7/8/10 compatible HID Multi-touch Device"); 62cb022db8SVladimir Kondratyev #ifdef HID_DEBUG 63cb022db8SVladimir Kondratyev static int hmt_debug = 0; 64cb022db8SVladimir Kondratyev SYSCTL_INT(_hw_hid_hmt, OID_AUTO, debug, CTLFLAG_RWTUN, 65cb022db8SVladimir Kondratyev &hmt_debug, 1, "Debug level"); 66b9b347e9SVladimir Kondratyev #endif 67cb022db8SVladimir Kondratyev static bool hmt_timestamps = 0; 68cb022db8SVladimir Kondratyev SYSCTL_BOOL(_hw_hid_hmt, OID_AUTO, timestamps, CTLFLAG_RDTUN, 69cb022db8SVladimir Kondratyev &hmt_timestamps, 1, "Enable hardware timestamp reporting"); 70b9b347e9SVladimir Kondratyev 71cb022db8SVladimir Kondratyev #define HMT_BTN_MAX 8 /* Number of buttons supported */ 72b9b347e9SVladimir Kondratyev 73cb022db8SVladimir Kondratyev enum hmt_type { 74cb022db8SVladimir Kondratyev HMT_TYPE_UNKNOWN = 0, /* HID report descriptor is not probed */ 75cb022db8SVladimir Kondratyev HMT_TYPE_UNSUPPORTED, /* Repdescr does not belong to MT device */ 76cb022db8SVladimir Kondratyev HMT_TYPE_TOUCHPAD, 77cb022db8SVladimir Kondratyev HMT_TYPE_TOUCHSCREEN, 78b9b347e9SVladimir Kondratyev }; 79b9b347e9SVladimir Kondratyev 80b9b347e9SVladimir Kondratyev enum { 819d8ebe5eSVladimir Kondratyev HMT_TIP_SWITCH = ABS_MT_INDEX(ABS_MT_TOOL_TYPE), 829d8ebe5eSVladimir Kondratyev HMT_WIDTH = ABS_MT_INDEX(ABS_MT_TOUCH_MAJOR), 839d8ebe5eSVladimir Kondratyev HMT_HEIGHT = ABS_MT_INDEX(ABS_MT_TOUCH_MINOR), 849d8ebe5eSVladimir Kondratyev HMT_ORIENTATION = ABS_MT_INDEX(ABS_MT_ORIENTATION), 859d8ebe5eSVladimir Kondratyev HMT_X = ABS_MT_INDEX(ABS_MT_POSITION_X), 869d8ebe5eSVladimir Kondratyev HMT_Y = ABS_MT_INDEX(ABS_MT_POSITION_Y), 879d8ebe5eSVladimir Kondratyev HMT_CONTACTID = ABS_MT_INDEX(ABS_MT_TRACKING_ID), 889d8ebe5eSVladimir Kondratyev HMT_PRESSURE = ABS_MT_INDEX(ABS_MT_PRESSURE), 899d8ebe5eSVladimir Kondratyev HMT_IN_RANGE = ABS_MT_INDEX(ABS_MT_DISTANCE), 909d8ebe5eSVladimir Kondratyev HMT_CONFIDENCE = ABS_MT_INDEX(ABS_MT_BLOB_ID), 919d8ebe5eSVladimir Kondratyev HMT_TOOL_X = ABS_MT_INDEX(ABS_MT_TOOL_X), 929d8ebe5eSVladimir Kondratyev HMT_TOOL_Y = ABS_MT_INDEX(ABS_MT_TOOL_Y), 93b9b347e9SVladimir Kondratyev }; 94b9b347e9SVladimir Kondratyev 959d8ebe5eSVladimir Kondratyev #define HMT_N_USAGES MT_CNT 96cb022db8SVladimir Kondratyev #define HMT_NO_USAGE -1 97b9b347e9SVladimir Kondratyev 98cb022db8SVladimir Kondratyev struct hmt_hid_map_item { 99b9b347e9SVladimir Kondratyev char name[5]; 100b9b347e9SVladimir Kondratyev int32_t usage; /* HID usage */ 1019d8ebe5eSVladimir Kondratyev bool reported; /* Item value is passed to evdev */ 102b9b347e9SVladimir Kondratyev bool required; /* Required for MT Digitizers */ 103b9b347e9SVladimir Kondratyev }; 104b9b347e9SVladimir Kondratyev 105cb022db8SVladimir Kondratyev static const struct hmt_hid_map_item hmt_hid_map[HMT_N_USAGES] = { 1069d8ebe5eSVladimir Kondratyev [HMT_TIP_SWITCH] = { 107b9b347e9SVladimir Kondratyev .name = "TIP", 108b9b347e9SVladimir Kondratyev .usage = HID_USAGE2(HUP_DIGITIZERS, HUD_TIP_SWITCH), 1099d8ebe5eSVladimir Kondratyev .reported = false, 110b9b347e9SVladimir Kondratyev .required = true, 111b9b347e9SVladimir Kondratyev }, 1129d8ebe5eSVladimir Kondratyev [HMT_WIDTH] = { 113b9b347e9SVladimir Kondratyev .name = "WDTH", 114b9b347e9SVladimir Kondratyev .usage = HID_USAGE2(HUP_DIGITIZERS, HUD_WIDTH), 1159d8ebe5eSVladimir Kondratyev .reported = true, 116b9b347e9SVladimir Kondratyev .required = false, 117b9b347e9SVladimir Kondratyev }, 1189d8ebe5eSVladimir Kondratyev [HMT_HEIGHT] = { 119b9b347e9SVladimir Kondratyev .name = "HGHT", 120b9b347e9SVladimir Kondratyev .usage = HID_USAGE2(HUP_DIGITIZERS, HUD_HEIGHT), 1219d8ebe5eSVladimir Kondratyev .reported = true, 122b9b347e9SVladimir Kondratyev .required = false, 123b9b347e9SVladimir Kondratyev }, 124cb022db8SVladimir Kondratyev [HMT_ORIENTATION] = { 125b9b347e9SVladimir Kondratyev .name = "ORIE", 126cb022db8SVladimir Kondratyev .usage = HMT_NO_USAGE, 1279d8ebe5eSVladimir Kondratyev .reported = true, 128b9b347e9SVladimir Kondratyev .required = false, 129b9b347e9SVladimir Kondratyev }, 130cb022db8SVladimir Kondratyev [HMT_X] = { 131b9b347e9SVladimir Kondratyev .name = "X", 132b9b347e9SVladimir Kondratyev .usage = HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_X), 1339d8ebe5eSVladimir Kondratyev .reported = true, 134b9b347e9SVladimir Kondratyev .required = true, 135b9b347e9SVladimir Kondratyev }, 136cb022db8SVladimir Kondratyev [HMT_Y] = { 137b9b347e9SVladimir Kondratyev .name = "Y", 138b9b347e9SVladimir Kondratyev .usage = HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_Y), 1399d8ebe5eSVladimir Kondratyev .reported = true, 140b9b347e9SVladimir Kondratyev .required = true, 141b9b347e9SVladimir Kondratyev }, 142cb022db8SVladimir Kondratyev [HMT_CONTACTID] = { 143b9b347e9SVladimir Kondratyev .name = "C_ID", 144b9b347e9SVladimir Kondratyev .usage = HID_USAGE2(HUP_DIGITIZERS, HUD_CONTACTID), 1459d8ebe5eSVladimir Kondratyev .reported = true, 146b9b347e9SVladimir Kondratyev .required = true, 147b9b347e9SVladimir Kondratyev }, 148cb022db8SVladimir Kondratyev [HMT_PRESSURE] = { 149b9b347e9SVladimir Kondratyev .name = "PRES", 150b9b347e9SVladimir Kondratyev .usage = HID_USAGE2(HUP_DIGITIZERS, HUD_TIP_PRESSURE), 1519d8ebe5eSVladimir Kondratyev .reported = true, 152b9b347e9SVladimir Kondratyev .required = false, 153b9b347e9SVladimir Kondratyev }, 154cb022db8SVladimir Kondratyev [HMT_IN_RANGE] = { 155b9b347e9SVladimir Kondratyev .name = "RANG", 156b9b347e9SVladimir Kondratyev .usage = HID_USAGE2(HUP_DIGITIZERS, HUD_IN_RANGE), 1579d8ebe5eSVladimir Kondratyev .reported = true, 158b9b347e9SVladimir Kondratyev .required = false, 159b9b347e9SVladimir Kondratyev }, 160cb022db8SVladimir Kondratyev [HMT_CONFIDENCE] = { 161b9b347e9SVladimir Kondratyev .name = "CONF", 162b9b347e9SVladimir Kondratyev .usage = HID_USAGE2(HUP_DIGITIZERS, HUD_CONFIDENCE), 1639d8ebe5eSVladimir Kondratyev .reported = false, 164b9b347e9SVladimir Kondratyev .required = false, 165b9b347e9SVladimir Kondratyev }, 1669d8ebe5eSVladimir Kondratyev [HMT_TOOL_X] = { /* Shares HID usage with POS_X */ 167b9b347e9SVladimir Kondratyev .name = "TL_X", 168b9b347e9SVladimir Kondratyev .usage = HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_X), 1699d8ebe5eSVladimir Kondratyev .reported = true, 170b9b347e9SVladimir Kondratyev .required = false, 171b9b347e9SVladimir Kondratyev }, 1729d8ebe5eSVladimir Kondratyev [HMT_TOOL_Y] = { /* Shares HID usage with POS_Y */ 173b9b347e9SVladimir Kondratyev .name = "TL_Y", 174b9b347e9SVladimir Kondratyev .usage = HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_Y), 1759d8ebe5eSVladimir Kondratyev .reported = true, 176b9b347e9SVladimir Kondratyev .required = false, 177b9b347e9SVladimir Kondratyev }, 178b9b347e9SVladimir Kondratyev }; 179b9b347e9SVladimir Kondratyev 180cb022db8SVladimir Kondratyev struct hmt_softc { 181b9b347e9SVladimir Kondratyev device_t dev; 182cb022db8SVladimir Kondratyev enum hmt_type type; 183b9b347e9SVladimir Kondratyev 18495add157SVladimir Kondratyev int32_t cont_count_max; 185cb022db8SVladimir Kondratyev struct hid_absinfo ai[HMT_N_USAGES]; 186cb022db8SVladimir Kondratyev struct hid_location locs[MAX_MT_SLOTS][HMT_N_USAGES]; 187b9b347e9SVladimir Kondratyev struct hid_location cont_count_loc; 188cb022db8SVladimir Kondratyev struct hid_location btn_loc[HMT_BTN_MAX]; 189b9b347e9SVladimir Kondratyev struct hid_location int_btn_loc; 190b9b347e9SVladimir Kondratyev struct hid_location scan_time_loc; 191b9b347e9SVladimir Kondratyev int32_t scan_time_max; 192b9b347e9SVladimir Kondratyev int32_t scan_time; 193b9b347e9SVladimir Kondratyev int32_t timestamp; 194b9b347e9SVladimir Kondratyev bool touch; 195b9b347e9SVladimir Kondratyev bool prev_touch; 196b9b347e9SVladimir Kondratyev 197b9b347e9SVladimir Kondratyev struct evdev_dev *evdev; 198b9b347e9SVladimir Kondratyev 1999d8ebe5eSVladimir Kondratyev union evdev_mt_slot slot_data; 200cb022db8SVladimir Kondratyev uint8_t caps[howmany(HMT_N_USAGES, 8)]; 201cb022db8SVladimir Kondratyev uint8_t buttons[howmany(HMT_BTN_MAX, 8)]; 202b9b347e9SVladimir Kondratyev uint32_t nconts_per_report; 203b9b347e9SVladimir Kondratyev uint32_t nconts_todo; 204b9b347e9SVladimir Kondratyev uint8_t report_id; 205b9b347e9SVladimir Kondratyev uint32_t max_button; 206b9b347e9SVladimir Kondratyev bool has_int_button; 2070075742dSVladimir Kondratyev bool has_cont_count; 2080075742dSVladimir Kondratyev bool has_scan_time; 209b9b347e9SVladimir Kondratyev bool is_clickpad; 210b9b347e9SVladimir Kondratyev bool do_timestamps; 2115cc21ab9SVladimir Kondratyev #ifdef IICHID_SAMPLING 212cb022db8SVladimir Kondratyev bool iichid_sampling; 2135cc21ab9SVladimir Kondratyev #endif 214b9b347e9SVladimir Kondratyev 215b9b347e9SVladimir Kondratyev struct hid_location cont_max_loc; 216b9b347e9SVladimir Kondratyev uint32_t cont_max_rlen; 217b9b347e9SVladimir Kondratyev uint8_t cont_max_rid; 218b9b347e9SVladimir Kondratyev struct hid_location btn_type_loc; 219b9b347e9SVladimir Kondratyev uint32_t btn_type_rlen; 220b9b347e9SVladimir Kondratyev uint8_t btn_type_rid; 221b9b347e9SVladimir Kondratyev uint32_t thqa_cert_rlen; 222b9b347e9SVladimir Kondratyev uint8_t thqa_cert_rid; 223b9b347e9SVladimir Kondratyev }; 224b9b347e9SVladimir Kondratyev 225cb022db8SVladimir Kondratyev #define HMT_FOREACH_USAGE(caps, usage) \ 226cb022db8SVladimir Kondratyev for ((usage) = 0; (usage) < HMT_N_USAGES; ++(usage)) \ 227b9b347e9SVladimir Kondratyev if (isset((caps), (usage))) 228b9b347e9SVladimir Kondratyev 229cb022db8SVladimir Kondratyev static enum hmt_type hmt_hid_parse(struct hmt_softc *, const void *, 230cb022db8SVladimir Kondratyev hid_size_t, uint32_t, uint8_t); 231cb022db8SVladimir Kondratyev static int hmt_set_input_mode(struct hmt_softc *, enum hconf_input_mode); 232b9b347e9SVladimir Kondratyev 233cb022db8SVladimir Kondratyev static hid_intr_t hmt_intr; 234b9b347e9SVladimir Kondratyev 235cb022db8SVladimir Kondratyev static device_probe_t hmt_probe; 236cb022db8SVladimir Kondratyev static device_attach_t hmt_attach; 237cb022db8SVladimir Kondratyev static device_detach_t hmt_detach; 238b9b347e9SVladimir Kondratyev 239cb022db8SVladimir Kondratyev static evdev_open_t hmt_ev_open; 240cb022db8SVladimir Kondratyev static evdev_close_t hmt_ev_close; 241b9b347e9SVladimir Kondratyev 242cb022db8SVladimir Kondratyev static const struct evdev_methods hmt_evdev_methods = { 243cb022db8SVladimir Kondratyev .ev_open = &hmt_ev_open, 244cb022db8SVladimir Kondratyev .ev_close = &hmt_ev_close, 245b9b347e9SVladimir Kondratyev }; 246b9b347e9SVladimir Kondratyev 247cb022db8SVladimir Kondratyev static const struct hid_device_id hmt_devs[] = { 248cb022db8SVladimir Kondratyev { HID_TLC(HUP_DIGITIZERS, HUD_TOUCHSCREEN) }, 249cb022db8SVladimir Kondratyev { HID_TLC(HUP_DIGITIZERS, HUD_TOUCHPAD) }, 250b9b347e9SVladimir Kondratyev }; 251b9b347e9SVladimir Kondratyev 252b9b347e9SVladimir Kondratyev static int 253cb022db8SVladimir Kondratyev hmt_ev_close(struct evdev_dev *evdev) 254b9b347e9SVladimir Kondratyev { 255cb022db8SVladimir Kondratyev return (hidbus_intr_stop(evdev_get_softc(evdev))); 256cb022db8SVladimir Kondratyev } 257cb022db8SVladimir Kondratyev 258cb022db8SVladimir Kondratyev static int 259cb022db8SVladimir Kondratyev hmt_ev_open(struct evdev_dev *evdev) 260cb022db8SVladimir Kondratyev { 261cb022db8SVladimir Kondratyev return (hidbus_intr_start(evdev_get_softc(evdev))); 262cb022db8SVladimir Kondratyev } 263cb022db8SVladimir Kondratyev 264cb022db8SVladimir Kondratyev static int 265cb022db8SVladimir Kondratyev hmt_probe(device_t dev) 266cb022db8SVladimir Kondratyev { 267cb022db8SVladimir Kondratyev struct hmt_softc *sc = device_get_softc(dev); 268b9b347e9SVladimir Kondratyev void *d_ptr; 269cb022db8SVladimir Kondratyev hid_size_t d_len; 270b9b347e9SVladimir Kondratyev int err; 271b9b347e9SVladimir Kondratyev 272cb022db8SVladimir Kondratyev err = HIDBUS_LOOKUP_DRIVER_INFO(dev, hmt_devs); 273cb022db8SVladimir Kondratyev if (err != 0) 274cb022db8SVladimir Kondratyev return (err); 275cb022db8SVladimir Kondratyev 276cb022db8SVladimir Kondratyev err = hid_get_report_descr(dev, &d_ptr, &d_len); 277cb022db8SVladimir Kondratyev if (err != 0) { 278cb022db8SVladimir Kondratyev device_printf(dev, "could not retrieve report descriptor from " 279cb022db8SVladimir Kondratyev "device: %d\n", err); 280b9b347e9SVladimir Kondratyev return (ENXIO); 281b9b347e9SVladimir Kondratyev } 282b9b347e9SVladimir Kondratyev 283cb022db8SVladimir Kondratyev /* Check if report descriptor belongs to a HID multitouch device */ 284cb022db8SVladimir Kondratyev if (sc->type == HMT_TYPE_UNKNOWN) 285cb022db8SVladimir Kondratyev sc->type = hmt_hid_parse(sc, d_ptr, d_len, 286cb022db8SVladimir Kondratyev hidbus_get_usage(dev), hidbus_get_index(dev)); 287cb022db8SVladimir Kondratyev if (sc->type == HMT_TYPE_UNSUPPORTED) 288cb022db8SVladimir Kondratyev return (ENXIO); 289cb022db8SVladimir Kondratyev 290cb022db8SVladimir Kondratyev hidbus_set_desc(dev, 291cb022db8SVladimir Kondratyev sc->type == HMT_TYPE_TOUCHPAD ? "TouchPad" : "TouchScreen"); 292cb022db8SVladimir Kondratyev 293cb022db8SVladimir Kondratyev return (BUS_PROBE_DEFAULT); 294b9b347e9SVladimir Kondratyev } 295b9b347e9SVladimir Kondratyev 296b9b347e9SVladimir Kondratyev static int 297cb022db8SVladimir Kondratyev hmt_attach(device_t dev) 298b9b347e9SVladimir Kondratyev { 299cb022db8SVladimir Kondratyev struct hmt_softc *sc = device_get_softc(dev); 300cb022db8SVladimir Kondratyev const struct hid_device_info *hw = hid_get_device_info(dev); 301cb022db8SVladimir Kondratyev void *d_ptr; 302cb022db8SVladimir Kondratyev uint8_t *fbuf = NULL; 3039d04336bSVladimir Kondratyev hid_size_t d_len, fsize, rsize; 304b9b347e9SVladimir Kondratyev uint32_t cont_count_max; 305b9b347e9SVladimir Kondratyev int nbuttons, btn; 306b9b347e9SVladimir Kondratyev size_t i; 307b9b347e9SVladimir Kondratyev int err; 308b9b347e9SVladimir Kondratyev 309cb022db8SVladimir Kondratyev err = hid_get_report_descr(dev, &d_ptr, &d_len); 310cb022db8SVladimir Kondratyev if (err != 0) { 311cb022db8SVladimir Kondratyev device_printf(dev, "could not retrieve report descriptor from " 312cb022db8SVladimir Kondratyev "device: %d\n", err); 313cb022db8SVladimir Kondratyev return (ENXIO); 314cb022db8SVladimir Kondratyev } 315cb022db8SVladimir Kondratyev 316b9b347e9SVladimir Kondratyev sc->dev = dev; 317b9b347e9SVladimir Kondratyev 318cb022db8SVladimir Kondratyev fsize = hid_report_size_max(d_ptr, d_len, hid_feature, NULL); 319cb022db8SVladimir Kondratyev if (fsize != 0) 320cb022db8SVladimir Kondratyev fbuf = malloc(fsize, M_TEMP, M_WAITOK | M_ZERO); 321cb022db8SVladimir Kondratyev 322b9b347e9SVladimir Kondratyev /* Fetch and parse "Contact count maximum" feature report */ 323cb022db8SVladimir Kondratyev if (sc->cont_max_rlen > 1) { 3249d04336bSVladimir Kondratyev err = hid_get_report(dev, fbuf, sc->cont_max_rlen, &rsize, 325cb022db8SVladimir Kondratyev HID_FEATURE_REPORT, sc->cont_max_rid); 3269d04336bSVladimir Kondratyev if (err == 0 && (rsize - 1) * 8 >= 3279d04336bSVladimir Kondratyev sc->cont_max_loc.pos + sc->cont_max_loc.size) { 328cb022db8SVladimir Kondratyev cont_count_max = hid_get_udata(fbuf + 1, 329b9b347e9SVladimir Kondratyev sc->cont_max_rlen - 1, &sc->cont_max_loc); 330b9b347e9SVladimir Kondratyev /* 331b9b347e9SVladimir Kondratyev * Feature report is a primary source of 332b9b347e9SVladimir Kondratyev * 'Contact Count Maximum' 333b9b347e9SVladimir Kondratyev */ 334b9b347e9SVladimir Kondratyev if (cont_count_max > 0) 33595add157SVladimir Kondratyev sc->cont_count_max = cont_count_max; 336b9b347e9SVladimir Kondratyev } else 337cb022db8SVladimir Kondratyev DPRINTF("hid_get_report error=%d\n", err); 3389d04336bSVladimir Kondratyev } 3399d04336bSVladimir Kondratyev if (sc->cont_count_max == 0) 3409d04336bSVladimir Kondratyev sc->cont_count_max = sc->type == HMT_TYPE_TOUCHSCREEN ? 10 : 5; 341b9b347e9SVladimir Kondratyev 342b9b347e9SVladimir Kondratyev /* Fetch and parse "Button type" feature report */ 343cb022db8SVladimir Kondratyev if (sc->btn_type_rlen > 1 && sc->btn_type_rid != sc->cont_max_rid) { 344cb022db8SVladimir Kondratyev bzero(fbuf, fsize); 3459d04336bSVladimir Kondratyev err = hid_get_report(dev, fbuf, sc->btn_type_rlen, &rsize, 346cb022db8SVladimir Kondratyev HID_FEATURE_REPORT, sc->btn_type_rid); 3479d04336bSVladimir Kondratyev if (err != 0) 348cb022db8SVladimir Kondratyev DPRINTF("hid_get_report error=%d\n", err); 349b9b347e9SVladimir Kondratyev } 3509d04336bSVladimir Kondratyev if (sc->btn_type_rlen > 1 && err == 0 && (rsize - 1) * 8 >= 3519d04336bSVladimir Kondratyev sc->btn_type_loc.pos + sc->btn_type_loc.size) 3529d04336bSVladimir Kondratyev sc->is_clickpad = hid_get_udata(fbuf + 1, sc->btn_type_rlen - 1, 3539d04336bSVladimir Kondratyev &sc->btn_type_loc) == 0; 3549d04336bSVladimir Kondratyev else 3559d04336bSVladimir Kondratyev sc->is_clickpad = sc->max_button == 0 && sc->has_int_button; 356b9b347e9SVladimir Kondratyev 357b9b347e9SVladimir Kondratyev /* Fetch THQA certificate to enable some devices like WaveShare */ 358cb022db8SVladimir Kondratyev if (sc->thqa_cert_rlen > 1 && sc->thqa_cert_rid != sc->cont_max_rid) 359cb022db8SVladimir Kondratyev (void)hid_get_report(dev, fbuf, sc->thqa_cert_rlen, NULL, 360cb022db8SVladimir Kondratyev HID_FEATURE_REPORT, sc->thqa_cert_rid); 361cb022db8SVladimir Kondratyev 362cb022db8SVladimir Kondratyev free(fbuf, M_TEMP); 363b9b347e9SVladimir Kondratyev 364b9b347e9SVladimir Kondratyev /* Switch touchpad in to absolute multitouch mode */ 365cb022db8SVladimir Kondratyev if (sc->type == HMT_TYPE_TOUCHPAD) { 366cb022db8SVladimir Kondratyev err = hmt_set_input_mode(sc, HCONF_INPUT_MODE_MT_TOUCHPAD); 367b9b347e9SVladimir Kondratyev if (err != 0) 368b9b347e9SVladimir Kondratyev DPRINTF("Failed to set input mode: %d\n", err); 369b9b347e9SVladimir Kondratyev } 370b9b347e9SVladimir Kondratyev 371b9b347e9SVladimir Kondratyev /* Cap contact count maximum to MAX_MT_SLOTS */ 37295add157SVladimir Kondratyev if (sc->cont_count_max > MAX_MT_SLOTS) { 373b9b347e9SVladimir Kondratyev DPRINTF("Hardware reported %d contacts while only %d is " 37495add157SVladimir Kondratyev "supported\n", sc->cont_count_max, MAX_MT_SLOTS); 37595add157SVladimir Kondratyev sc->cont_count_max = MAX_MT_SLOTS; 376b9b347e9SVladimir Kondratyev } 377b9b347e9SVladimir Kondratyev 3780075742dSVladimir Kondratyev if (sc->has_scan_time && 3790075742dSVladimir Kondratyev (hid_test_quirk(hw, HQ_MT_TIMESTAMP) || hmt_timestamps)) 380b9b347e9SVladimir Kondratyev sc->do_timestamps = true; 3815cc21ab9SVladimir Kondratyev #ifdef IICHID_SAMPLING 382cb022db8SVladimir Kondratyev if (hid_test_quirk(hw, HQ_IICHID_SAMPLING)) 383cb022db8SVladimir Kondratyev sc->iichid_sampling = true; 3845cc21ab9SVladimir Kondratyev #endif 385b9b347e9SVladimir Kondratyev 386cb022db8SVladimir Kondratyev hidbus_set_intr(dev, hmt_intr, sc); 387b9b347e9SVladimir Kondratyev 388b9b347e9SVladimir Kondratyev sc->evdev = evdev_alloc(); 389b9b347e9SVladimir Kondratyev evdev_set_name(sc->evdev, device_get_desc(dev)); 390b9b347e9SVladimir Kondratyev evdev_set_phys(sc->evdev, device_get_nameunit(dev)); 391cb022db8SVladimir Kondratyev evdev_set_id(sc->evdev, hw->idBus, hw->idVendor, hw->idProduct, 392cb022db8SVladimir Kondratyev hw->idVersion); 393cb022db8SVladimir Kondratyev evdev_set_serial(sc->evdev, hw->serial); 394cb022db8SVladimir Kondratyev evdev_set_methods(sc->evdev, dev, &hmt_evdev_methods); 395b9b347e9SVladimir Kondratyev evdev_set_flag(sc->evdev, EVDEV_FLAG_MT_STCOMPAT); 396cb022db8SVladimir Kondratyev evdev_set_flag(sc->evdev, EVDEV_FLAG_EXT_EPOCH); /* hidbus child */ 397b9b347e9SVladimir Kondratyev switch (sc->type) { 398cb022db8SVladimir Kondratyev case HMT_TYPE_TOUCHSCREEN: 399b9b347e9SVladimir Kondratyev evdev_support_prop(sc->evdev, INPUT_PROP_DIRECT); 400b9b347e9SVladimir Kondratyev break; 401cb022db8SVladimir Kondratyev case HMT_TYPE_TOUCHPAD: 402b9b347e9SVladimir Kondratyev evdev_support_prop(sc->evdev, INPUT_PROP_POINTER); 403b9b347e9SVladimir Kondratyev if (sc->is_clickpad) 404b9b347e9SVladimir Kondratyev evdev_support_prop(sc->evdev, INPUT_PROP_BUTTONPAD); 405b9b347e9SVladimir Kondratyev break; 406b9b347e9SVladimir Kondratyev default: 407cb022db8SVladimir Kondratyev KASSERT(0, ("hmt_attach: unsupported touch device type")); 408b9b347e9SVladimir Kondratyev } 409b9b347e9SVladimir Kondratyev evdev_support_event(sc->evdev, EV_SYN); 410b9b347e9SVladimir Kondratyev evdev_support_event(sc->evdev, EV_ABS); 411b9b347e9SVladimir Kondratyev if (sc->do_timestamps) { 412b9b347e9SVladimir Kondratyev evdev_support_event(sc->evdev, EV_MSC); 413b9b347e9SVladimir Kondratyev evdev_support_msc(sc->evdev, MSC_TIMESTAMP); 414b9b347e9SVladimir Kondratyev } 4155cc21ab9SVladimir Kondratyev #ifdef IICHID_SAMPLING 416cb022db8SVladimir Kondratyev if (sc->iichid_sampling) 417cb022db8SVladimir Kondratyev evdev_set_flag(sc->evdev, EVDEV_FLAG_MT_AUTOREL); 4185cc21ab9SVladimir Kondratyev #endif 419b9b347e9SVladimir Kondratyev nbuttons = 0; 420b9b347e9SVladimir Kondratyev if (sc->max_button != 0 || sc->has_int_button) { 421b9b347e9SVladimir Kondratyev evdev_support_event(sc->evdev, EV_KEY); 422b9b347e9SVladimir Kondratyev if (sc->has_int_button) 423b9b347e9SVladimir Kondratyev evdev_support_key(sc->evdev, BTN_LEFT); 424b9b347e9SVladimir Kondratyev for (btn = 0; btn < sc->max_button; ++btn) { 425b9b347e9SVladimir Kondratyev if (isset(sc->buttons, btn)) { 426b9b347e9SVladimir Kondratyev evdev_support_key(sc->evdev, BTN_MOUSE + btn); 427b9b347e9SVladimir Kondratyev nbuttons++; 428b9b347e9SVladimir Kondratyev } 429b9b347e9SVladimir Kondratyev } 430b9b347e9SVladimir Kondratyev } 4319d8ebe5eSVladimir Kondratyev evdev_support_abs(sc->evdev, 4329d8ebe5eSVladimir Kondratyev ABS_MT_SLOT, 0, sc->cont_count_max - 1, 0, 0, 0); 433cb022db8SVladimir Kondratyev HMT_FOREACH_USAGE(sc->caps, i) { 4349d8ebe5eSVladimir Kondratyev if (hmt_hid_map[i].reported) 4359d8ebe5eSVladimir Kondratyev evdev_support_abs(sc->evdev, ABS_MT_FIRST + i, 436b9b347e9SVladimir Kondratyev sc->ai[i].min, sc->ai[i].max, 0, 0, sc->ai[i].res); 437b9b347e9SVladimir Kondratyev } 438b9b347e9SVladimir Kondratyev 439cb022db8SVladimir Kondratyev err = evdev_register(sc->evdev); 440cb022db8SVladimir Kondratyev if (err) { 441cb022db8SVladimir Kondratyev hmt_detach(dev); 442b9b347e9SVladimir Kondratyev return (ENXIO); 443b9b347e9SVladimir Kondratyev } 444b9b347e9SVladimir Kondratyev 445cb022db8SVladimir Kondratyev /* Announce information about the touch device */ 4460075742dSVladimir Kondratyev device_printf(sc->dev, "%s %s with %d external button%s%s\n", 4470075742dSVladimir Kondratyev sc->cont_count_max > 1 ? "Multitouch" : "Singletouch", 448cb022db8SVladimir Kondratyev sc->type == HMT_TYPE_TOUCHSCREEN ? "touchscreen" : "touchpad", 449cb022db8SVladimir Kondratyev nbuttons, nbuttons != 1 ? "s" : "", 450cb022db8SVladimir Kondratyev sc->is_clickpad ? ", click-pad" : ""); 451cb022db8SVladimir Kondratyev device_printf(sc->dev, 4520075742dSVladimir Kondratyev "%d contact%s with [%s%s%s%s%s] properties. Report range [%d:%d] - [%d:%d]\n", 4530075742dSVladimir Kondratyev (int)sc->cont_count_max, sc->cont_count_max != 1 ? "s" : "", 454cb022db8SVladimir Kondratyev isset(sc->caps, HMT_IN_RANGE) ? "R" : "", 455cb022db8SVladimir Kondratyev isset(sc->caps, HMT_CONFIDENCE) ? "C" : "", 456cb022db8SVladimir Kondratyev isset(sc->caps, HMT_WIDTH) ? "W" : "", 457cb022db8SVladimir Kondratyev isset(sc->caps, HMT_HEIGHT) ? "H" : "", 458cb022db8SVladimir Kondratyev isset(sc->caps, HMT_PRESSURE) ? "P" : "", 459cb022db8SVladimir Kondratyev (int)sc->ai[HMT_X].min, (int)sc->ai[HMT_Y].min, 460cb022db8SVladimir Kondratyev (int)sc->ai[HMT_X].max, (int)sc->ai[HMT_Y].max); 461cb022db8SVladimir Kondratyev 462cb022db8SVladimir Kondratyev return (0); 463cb022db8SVladimir Kondratyev } 464cb022db8SVladimir Kondratyev 465b9b347e9SVladimir Kondratyev static int 466cb022db8SVladimir Kondratyev hmt_detach(device_t dev) 467b9b347e9SVladimir Kondratyev { 468cb022db8SVladimir Kondratyev struct hmt_softc *sc = device_get_softc(dev); 469b9b347e9SVladimir Kondratyev 470b9b347e9SVladimir Kondratyev evdev_free(sc->evdev); 471cb022db8SVladimir Kondratyev 472b9b347e9SVladimir Kondratyev return (0); 473b9b347e9SVladimir Kondratyev } 474b9b347e9SVladimir Kondratyev 475b9b347e9SVladimir Kondratyev static void 476cb022db8SVladimir Kondratyev hmt_intr(void *context, void *buf, hid_size_t len) 477b9b347e9SVladimir Kondratyev { 478cb022db8SVladimir Kondratyev struct hmt_softc *sc = context; 479b9b347e9SVladimir Kondratyev size_t usage; 4809d8ebe5eSVladimir Kondratyev union evdev_mt_slot *slot_data; 481b9b347e9SVladimir Kondratyev uint32_t cont, btn; 482b9b347e9SVladimir Kondratyev uint32_t cont_count; 483b9b347e9SVladimir Kondratyev uint32_t width; 484b9b347e9SVladimir Kondratyev uint32_t height; 485b9b347e9SVladimir Kondratyev uint32_t int_btn = 0; 486b9b347e9SVladimir Kondratyev uint32_t left_btn = 0; 4879d8ebe5eSVladimir Kondratyev int slot; 488b9b347e9SVladimir Kondratyev uint32_t scan_time; 489b9b347e9SVladimir Kondratyev int32_t delta; 490cb022db8SVladimir Kondratyev uint8_t id; 491cb022db8SVladimir Kondratyev 4925cc21ab9SVladimir Kondratyev #ifdef IICHID_SAMPLING 493cb022db8SVladimir Kondratyev /* 494cb022db8SVladimir Kondratyev * Special packet of zero length is generated by iichid driver running 495cb022db8SVladimir Kondratyev * in polling mode at the start of inactivity period to workaround 496cb022db8SVladimir Kondratyev * "stuck touch" problem caused by miss of finger release events. 497cb022db8SVladimir Kondratyev * This snippet is to be removed after GPIO interrupt support is added. 498cb022db8SVladimir Kondratyev */ 499cb022db8SVladimir Kondratyev if (sc->iichid_sampling && len == 0) { 500cb022db8SVladimir Kondratyev sc->prev_touch = false; 501cb022db8SVladimir Kondratyev sc->timestamp = 0; 5029d8ebe5eSVladimir Kondratyev /* EVDEV_FLAG_MT_AUTOREL releases all touches for us */ 503cb022db8SVladimir Kondratyev evdev_sync(sc->evdev); 504cb022db8SVladimir Kondratyev return; 505cb022db8SVladimir Kondratyev } 5065cc21ab9SVladimir Kondratyev #endif 507cb022db8SVladimir Kondratyev 508cb022db8SVladimir Kondratyev /* Ignore irrelevant reports */ 509cb022db8SVladimir Kondratyev id = sc->report_id != 0 ? *(uint8_t *)buf : 0; 510cb022db8SVladimir Kondratyev if (sc->report_id != id) { 511cb022db8SVladimir Kondratyev DPRINTF("Skip report with unexpected ID: %hhu\n", id); 512cb022db8SVladimir Kondratyev return; 513cb022db8SVladimir Kondratyev } 514cb022db8SVladimir Kondratyev 515cb022db8SVladimir Kondratyev /* Strip leading "report ID" byte */ 516cb022db8SVladimir Kondratyev if (sc->report_id != 0) { 517cb022db8SVladimir Kondratyev len--; 518cb022db8SVladimir Kondratyev buf = (uint8_t *)buf + 1; 519cb022db8SVladimir Kondratyev } 520b9b347e9SVladimir Kondratyev 521b9b347e9SVladimir Kondratyev /* 5220075742dSVladimir Kondratyev * "In Serial mode, each packet contains information that describes a 5230075742dSVladimir Kondratyev * single physical contact point. Multiple contacts are streamed 5240075742dSVladimir Kondratyev * serially. In this mode, devices report all contact information in a 5250075742dSVladimir Kondratyev * series of packets. The device sends a separate packet for each 5260075742dSVladimir Kondratyev * concurrent contact." 5270075742dSVladimir Kondratyev * 528b9b347e9SVladimir Kondratyev * "In Parallel mode, devices report all contact information in a 529b9b347e9SVladimir Kondratyev * single packet. Each physical contact is represented by a logical 530b9b347e9SVladimir Kondratyev * collection that is embedded in the top-level collection." 531b9b347e9SVladimir Kondratyev * 532b9b347e9SVladimir Kondratyev * Since additional contacts that were not present will still be in the 533b9b347e9SVladimir Kondratyev * report with contactid=0 but contactids are zero-based, find 534b9b347e9SVladimir Kondratyev * contactcount first. 535b9b347e9SVladimir Kondratyev */ 5360075742dSVladimir Kondratyev if (sc->has_cont_count) 537b9b347e9SVladimir Kondratyev cont_count = hid_get_udata(buf, len, &sc->cont_count_loc); 5380075742dSVladimir Kondratyev else 5390075742dSVladimir Kondratyev cont_count = 1; 540b9b347e9SVladimir Kondratyev /* 541b9b347e9SVladimir Kondratyev * "In Hybrid mode, the number of contacts that can be reported in one 542b9b347e9SVladimir Kondratyev * report is less than the maximum number of contacts that the device 543b9b347e9SVladimir Kondratyev * supports. For example, a device that supports a maximum of 544b9b347e9SVladimir Kondratyev * 4 concurrent physical contacts, can set up its top-level collection 545b9b347e9SVladimir Kondratyev * to deliver a maximum of two contacts in one report. If four contact 546b9b347e9SVladimir Kondratyev * points are present, the device can break these up into two serial 547b9b347e9SVladimir Kondratyev * reports that deliver two contacts each. 548b9b347e9SVladimir Kondratyev * 549b9b347e9SVladimir Kondratyev * "When a device delivers data in this manner, the Contact Count usage 550b9b347e9SVladimir Kondratyev * value in the first report should reflect the total number of 551b9b347e9SVladimir Kondratyev * contacts that are being delivered in the hybrid reports. The other 552b9b347e9SVladimir Kondratyev * serial reports should have a contact count of zero (0)." 553b9b347e9SVladimir Kondratyev */ 554b9b347e9SVladimir Kondratyev if (cont_count != 0) 555b9b347e9SVladimir Kondratyev sc->nconts_todo = cont_count; 556b9b347e9SVladimir Kondratyev 557cb022db8SVladimir Kondratyev #ifdef HID_DEBUG 558b9b347e9SVladimir Kondratyev DPRINTFN(6, "cont_count:%2u", (unsigned)cont_count); 559cb022db8SVladimir Kondratyev if (hmt_debug >= 6) { 560cb022db8SVladimir Kondratyev HMT_FOREACH_USAGE(sc->caps, usage) { 561cb022db8SVladimir Kondratyev if (hmt_hid_map[usage].usage != HMT_NO_USAGE) 562cb022db8SVladimir Kondratyev printf(" %-4s", hmt_hid_map[usage].name); 563b9b347e9SVladimir Kondratyev } 564b9b347e9SVladimir Kondratyev printf("\n"); 565b9b347e9SVladimir Kondratyev } 566b9b347e9SVladimir Kondratyev #endif 567b9b347e9SVladimir Kondratyev 568b9b347e9SVladimir Kondratyev /* Find the number of contacts reported in current report */ 569b9b347e9SVladimir Kondratyev cont_count = MIN(sc->nconts_todo, sc->nconts_per_report); 570b9b347e9SVladimir Kondratyev 571b9b347e9SVladimir Kondratyev /* Use protocol Type B for reporting events */ 572b9b347e9SVladimir Kondratyev for (cont = 0; cont < cont_count; cont++) { 5739d8ebe5eSVladimir Kondratyev slot_data = &sc->slot_data; 574b9b347e9SVladimir Kondratyev bzero(slot_data, sizeof(sc->slot_data)); 575cb022db8SVladimir Kondratyev HMT_FOREACH_USAGE(sc->caps, usage) { 576b9b347e9SVladimir Kondratyev if (sc->locs[cont][usage].size > 0) 5779d8ebe5eSVladimir Kondratyev slot_data->val[usage] = hid_get_udata( 578b9b347e9SVladimir Kondratyev buf, len, &sc->locs[cont][usage]); 579b9b347e9SVladimir Kondratyev } 580b9b347e9SVladimir Kondratyev 5819d8ebe5eSVladimir Kondratyev slot = evdev_mt_id_to_slot(sc->evdev, slot_data->id); 582b9b347e9SVladimir Kondratyev 583cb022db8SVladimir Kondratyev #ifdef HID_DEBUG 584b9b347e9SVladimir Kondratyev DPRINTFN(6, "cont%01x: data = ", cont); 585cb022db8SVladimir Kondratyev if (hmt_debug >= 6) { 586cb022db8SVladimir Kondratyev HMT_FOREACH_USAGE(sc->caps, usage) { 587cb022db8SVladimir Kondratyev if (hmt_hid_map[usage].usage != HMT_NO_USAGE) 5889d8ebe5eSVladimir Kondratyev printf("%04x ", slot_data->val[usage]); 589b9b347e9SVladimir Kondratyev } 5909d8ebe5eSVladimir Kondratyev printf("slot = %d\n", slot); 591b9b347e9SVladimir Kondratyev } 592b9b347e9SVladimir Kondratyev #endif 593b9b347e9SVladimir Kondratyev 594b9b347e9SVladimir Kondratyev if (slot == -1) { 595b9b347e9SVladimir Kondratyev DPRINTF("Slot overflow for contact_id %u\n", 5969d8ebe5eSVladimir Kondratyev (unsigned)slot_data->id); 597b9b347e9SVladimir Kondratyev continue; 598b9b347e9SVladimir Kondratyev } 599b9b347e9SVladimir Kondratyev 6009d8ebe5eSVladimir Kondratyev if (slot_data->val[HMT_TIP_SWITCH] != 0 && 601cb022db8SVladimir Kondratyev !(isset(sc->caps, HMT_CONFIDENCE) && 6029d8ebe5eSVladimir Kondratyev slot_data->val[HMT_CONFIDENCE] == 0)) { 603b9b347e9SVladimir Kondratyev /* This finger is in proximity of the sensor */ 604b9b347e9SVladimir Kondratyev sc->touch = true; 6059d8ebe5eSVladimir Kondratyev slot_data->dist = !slot_data->val[HMT_IN_RANGE]; 606b9b347e9SVladimir Kondratyev /* Divided by two to match visual scale of touch */ 6079d8ebe5eSVladimir Kondratyev width = slot_data->val[HMT_WIDTH] >> 1; 6089d8ebe5eSVladimir Kondratyev height = slot_data->val[HMT_HEIGHT] >> 1; 6099d8ebe5eSVladimir Kondratyev slot_data->ori = width > height; 6109d8ebe5eSVladimir Kondratyev slot_data->maj = MAX(width, height); 6119d8ebe5eSVladimir Kondratyev slot_data->min = MIN(width, height); 6129d8ebe5eSVladimir Kondratyev } else 6139d8ebe5eSVladimir Kondratyev slot_data = NULL; 614b9b347e9SVladimir Kondratyev 6159d8ebe5eSVladimir Kondratyev evdev_mt_push_slot(sc->evdev, slot, slot_data); 616b9b347e9SVladimir Kondratyev } 617b9b347e9SVladimir Kondratyev 618b9b347e9SVladimir Kondratyev sc->nconts_todo -= cont_count; 619b9b347e9SVladimir Kondratyev if (sc->do_timestamps && sc->nconts_todo == 0) { 620b9b347e9SVladimir Kondratyev /* HUD_SCAN_TIME is measured in 100us, convert to us. */ 621b9b347e9SVladimir Kondratyev scan_time = hid_get_udata(buf, len, &sc->scan_time_loc); 622b9b347e9SVladimir Kondratyev if (sc->prev_touch) { 623b9b347e9SVladimir Kondratyev delta = scan_time - sc->scan_time; 624b9b347e9SVladimir Kondratyev if (delta < 0) 625b9b347e9SVladimir Kondratyev delta += sc->scan_time_max; 626b9b347e9SVladimir Kondratyev } else 627b9b347e9SVladimir Kondratyev delta = 0; 628b9b347e9SVladimir Kondratyev sc->scan_time = scan_time; 629b9b347e9SVladimir Kondratyev sc->timestamp += delta * 100; 630b9b347e9SVladimir Kondratyev evdev_push_msc(sc->evdev, MSC_TIMESTAMP, sc->timestamp); 631b9b347e9SVladimir Kondratyev sc->prev_touch = sc->touch; 632b9b347e9SVladimir Kondratyev sc->touch = false; 633b9b347e9SVladimir Kondratyev if (!sc->prev_touch) 634b9b347e9SVladimir Kondratyev sc->timestamp = 0; 635b9b347e9SVladimir Kondratyev } 636b9b347e9SVladimir Kondratyev if (sc->nconts_todo == 0) { 637b9b347e9SVladimir Kondratyev /* Report both the click and external left btns as BTN_LEFT */ 638b9b347e9SVladimir Kondratyev if (sc->has_int_button) 639b9b347e9SVladimir Kondratyev int_btn = hid_get_data(buf, len, &sc->int_btn_loc); 640b9b347e9SVladimir Kondratyev if (isset(sc->buttons, 0)) 641b9b347e9SVladimir Kondratyev left_btn = hid_get_data(buf, len, &sc->btn_loc[0]); 642b9b347e9SVladimir Kondratyev if (sc->has_int_button || isset(sc->buttons, 0)) 643b9b347e9SVladimir Kondratyev evdev_push_key(sc->evdev, BTN_LEFT, 644b9b347e9SVladimir Kondratyev (int_btn != 0) | (left_btn != 0)); 645b9b347e9SVladimir Kondratyev for (btn = 1; btn < sc->max_button; ++btn) { 646b9b347e9SVladimir Kondratyev if (isset(sc->buttons, btn)) 647b9b347e9SVladimir Kondratyev evdev_push_key(sc->evdev, BTN_MOUSE + btn, 648b9b347e9SVladimir Kondratyev hid_get_data(buf, 649b9b347e9SVladimir Kondratyev len, 650b9b347e9SVladimir Kondratyev &sc->btn_loc[btn]) != 0); 651b9b347e9SVladimir Kondratyev } 652b9b347e9SVladimir Kondratyev evdev_sync(sc->evdev); 653b9b347e9SVladimir Kondratyev } 654b9b347e9SVladimir Kondratyev } 655b9b347e9SVladimir Kondratyev 656cb022db8SVladimir Kondratyev static enum hmt_type 657cb022db8SVladimir Kondratyev hmt_hid_parse(struct hmt_softc *sc, const void *d_ptr, hid_size_t d_len, 658cb022db8SVladimir Kondratyev uint32_t tlc_usage, uint8_t tlc_index) 659b9b347e9SVladimir Kondratyev { 660cb022db8SVladimir Kondratyev struct hid_absinfo ai; 661b9b347e9SVladimir Kondratyev struct hid_item hi; 662b9b347e9SVladimir Kondratyev struct hid_data *hd; 663cb022db8SVladimir Kondratyev uint32_t flags; 664b9b347e9SVladimir Kondratyev size_t i; 665b9b347e9SVladimir Kondratyev size_t cont = 0; 666cb022db8SVladimir Kondratyev enum hmt_type type; 667b9b347e9SVladimir Kondratyev uint32_t left_btn, btn; 668b9b347e9SVladimir Kondratyev int32_t cont_count_max = 0; 669b9b347e9SVladimir Kondratyev uint8_t report_id = 0; 670b9b347e9SVladimir Kondratyev bool finger_coll = false; 671b9b347e9SVladimir Kondratyev bool cont_count_found = false; 672b9b347e9SVladimir Kondratyev bool scan_time_found = false; 673b9b347e9SVladimir Kondratyev bool has_int_button = false; 674b9b347e9SVladimir Kondratyev 675cb910670SJack #define HMT_HI_ABSOLUTE(hi) ((hi).nusages != 0 && \ 676cb910670SJack ((hi).flags & (HIO_VARIABLE | HIO_RELATIVE)) == HIO_VARIABLE) 677b9b347e9SVladimir Kondratyev #define HUMS_THQA_CERT 0xC5 678b9b347e9SVladimir Kondratyev 679cb022db8SVladimir Kondratyev /* 680cb022db8SVladimir Kondratyev * Get left button usage taking in account MS Precision Touchpad specs. 681cb022db8SVladimir Kondratyev * For Windows PTP report descriptor assigns buttons in following way: 682cb022db8SVladimir Kondratyev * Button 1 - Indicates Button State for touchpad button integrated 683cb022db8SVladimir Kondratyev * with digitizer. 684cb022db8SVladimir Kondratyev * Button 2 - Indicates Button State for external button for primary 685cb022db8SVladimir Kondratyev * (default left) clicking. 686cb022db8SVladimir Kondratyev * Button 3 - Indicates Button State for external button for secondary 687cb022db8SVladimir Kondratyev * (default right) clicking. 688cb022db8SVladimir Kondratyev * If a device only supports external buttons, it must still use 689cb022db8SVladimir Kondratyev * Button 2 and Button 3 to reference the external buttons. 690cb022db8SVladimir Kondratyev */ 691cb022db8SVladimir Kondratyev switch (tlc_usage) { 692cb022db8SVladimir Kondratyev case HID_USAGE2(HUP_DIGITIZERS, HUD_TOUCHSCREEN): 693cb022db8SVladimir Kondratyev type = HMT_TYPE_TOUCHSCREEN; 694b9b347e9SVladimir Kondratyev left_btn = 1; 695b9b347e9SVladimir Kondratyev break; 696cb022db8SVladimir Kondratyev case HID_USAGE2(HUP_DIGITIZERS, HUD_TOUCHPAD): 697cb022db8SVladimir Kondratyev type = HMT_TYPE_TOUCHPAD; 698b9b347e9SVladimir Kondratyev left_btn = 2; 699b9b347e9SVladimir Kondratyev break; 700b9b347e9SVladimir Kondratyev default: 701cb022db8SVladimir Kondratyev return (HMT_TYPE_UNSUPPORTED); 702b9b347e9SVladimir Kondratyev } 703b9b347e9SVladimir Kondratyev 704cb022db8SVladimir Kondratyev /* Parse features for mandatory maximum contact count usage */ 705cb022db8SVladimir Kondratyev if (!hidbus_locate(d_ptr, d_len, 706cb022db8SVladimir Kondratyev HID_USAGE2(HUP_DIGITIZERS, HUD_CONTACT_MAX), hid_feature, 707cb022db8SVladimir Kondratyev tlc_index, 0, &sc->cont_max_loc, &flags, &sc->cont_max_rid, &ai) || 708cb022db8SVladimir Kondratyev (flags & (HIO_VARIABLE | HIO_RELATIVE)) != HIO_VARIABLE) 709cb022db8SVladimir Kondratyev return (HMT_TYPE_UNSUPPORTED); 710b9b347e9SVladimir Kondratyev 711cb022db8SVladimir Kondratyev cont_count_max = ai.max; 712cb022db8SVladimir Kondratyev 713cb022db8SVladimir Kondratyev /* Parse features for button type usage */ 714cb022db8SVladimir Kondratyev if (hidbus_locate(d_ptr, d_len, 715cb022db8SVladimir Kondratyev HID_USAGE2(HUP_DIGITIZERS, HUD_BUTTON_TYPE), hid_feature, 716cb022db8SVladimir Kondratyev tlc_index, 0, &sc->btn_type_loc, &flags, &sc->btn_type_rid, NULL) 717cb022db8SVladimir Kondratyev && (flags & (HIO_VARIABLE | HIO_RELATIVE)) != HIO_VARIABLE) 718cb022db8SVladimir Kondratyev sc->btn_type_rid = 0; 719cb022db8SVladimir Kondratyev 720cb022db8SVladimir Kondratyev /* Parse features for THQA certificate report ID */ 721cb022db8SVladimir Kondratyev hidbus_locate(d_ptr, d_len, HID_USAGE2(HUP_MICROSOFT, HUMS_THQA_CERT), 722cb022db8SVladimir Kondratyev hid_feature, tlc_index, 0, NULL, NULL, &sc->thqa_cert_rid, NULL); 723b9b347e9SVladimir Kondratyev 724b9b347e9SVladimir Kondratyev /* Parse input for other parameters */ 725b9b347e9SVladimir Kondratyev hd = hid_start_parse(d_ptr, d_len, 1 << hid_input); 726cb022db8SVladimir Kondratyev HIDBUS_FOREACH_ITEM(hd, &hi, tlc_index) { 727b9b347e9SVladimir Kondratyev switch (hi.kind) { 728b9b347e9SVladimir Kondratyev case hid_collection: 729cb022db8SVladimir Kondratyev if (hi.collevel == 2 && 730b9b347e9SVladimir Kondratyev hi.usage == HID_USAGE2(HUP_DIGITIZERS, HUD_FINGER)) 731b9b347e9SVladimir Kondratyev finger_coll = true; 732b9b347e9SVladimir Kondratyev break; 733b9b347e9SVladimir Kondratyev case hid_endcollection: 734b9b347e9SVladimir Kondratyev if (hi.collevel == 1 && finger_coll) { 735b9b347e9SVladimir Kondratyev finger_coll = false; 736b9b347e9SVladimir Kondratyev cont++; 737cb022db8SVladimir Kondratyev } 738b9b347e9SVladimir Kondratyev break; 739b9b347e9SVladimir Kondratyev case hid_input: 740b9b347e9SVladimir Kondratyev /* 741cb022db8SVladimir Kondratyev * Ensure that all usages belong to the same report 742b9b347e9SVladimir Kondratyev */ 743cb022db8SVladimir Kondratyev if (HMT_HI_ABSOLUTE(hi) && 744b9b347e9SVladimir Kondratyev (report_id == 0 || report_id == hi.report_ID)) 745b9b347e9SVladimir Kondratyev report_id = hi.report_ID; 746b9b347e9SVladimir Kondratyev else 747b9b347e9SVladimir Kondratyev break; 748b9b347e9SVladimir Kondratyev 749b9b347e9SVladimir Kondratyev if (hi.collevel == 1 && left_btn == 2 && 750b9b347e9SVladimir Kondratyev hi.usage == HID_USAGE2(HUP_BUTTON, 1)) { 751b9b347e9SVladimir Kondratyev has_int_button = true; 752b9b347e9SVladimir Kondratyev sc->int_btn_loc = hi.loc; 753b9b347e9SVladimir Kondratyev break; 754b9b347e9SVladimir Kondratyev } 755b9b347e9SVladimir Kondratyev if (hi.collevel == 1 && 756b9b347e9SVladimir Kondratyev hi.usage >= HID_USAGE2(HUP_BUTTON, left_btn) && 757cb022db8SVladimir Kondratyev hi.usage <= HID_USAGE2(HUP_BUTTON, HMT_BTN_MAX)) { 758b9b347e9SVladimir Kondratyev btn = (hi.usage & 0xFFFF) - left_btn; 759b9b347e9SVladimir Kondratyev setbit(sc->buttons, btn); 760b9b347e9SVladimir Kondratyev sc->btn_loc[btn] = hi.loc; 761b9b347e9SVladimir Kondratyev if (btn >= sc->max_button) 762b9b347e9SVladimir Kondratyev sc->max_button = btn + 1; 763b9b347e9SVladimir Kondratyev break; 764b9b347e9SVladimir Kondratyev } 765b9b347e9SVladimir Kondratyev if (hi.collevel == 1 && hi.usage == 766b9b347e9SVladimir Kondratyev HID_USAGE2(HUP_DIGITIZERS, HUD_CONTACTCOUNT)) { 767b9b347e9SVladimir Kondratyev cont_count_found = true; 768b9b347e9SVladimir Kondratyev sc->cont_count_loc = hi.loc; 769b9b347e9SVladimir Kondratyev break; 770b9b347e9SVladimir Kondratyev } 771b9b347e9SVladimir Kondratyev if (hi.collevel == 1 && hi.usage == 772b9b347e9SVladimir Kondratyev HID_USAGE2(HUP_DIGITIZERS, HUD_SCAN_TIME)) { 773b9b347e9SVladimir Kondratyev scan_time_found = true; 774b9b347e9SVladimir Kondratyev sc->scan_time_loc = hi.loc; 775b9b347e9SVladimir Kondratyev sc->scan_time_max = hi.logical_maximum; 776b9b347e9SVladimir Kondratyev break; 777b9b347e9SVladimir Kondratyev } 778b9b347e9SVladimir Kondratyev 779b9b347e9SVladimir Kondratyev if (!finger_coll || hi.collevel != 2) 780b9b347e9SVladimir Kondratyev break; 781b9b347e9SVladimir Kondratyev if (cont >= MAX_MT_SLOTS) { 782b9b347e9SVladimir Kondratyev DPRINTF("Finger %zu ignored\n", cont); 783b9b347e9SVladimir Kondratyev break; 784b9b347e9SVladimir Kondratyev } 785b9b347e9SVladimir Kondratyev 786cb022db8SVladimir Kondratyev for (i = 0; i < HMT_N_USAGES; i++) { 787cb022db8SVladimir Kondratyev if (hi.usage == hmt_hid_map[i].usage) { 788b9b347e9SVladimir Kondratyev /* 789b9b347e9SVladimir Kondratyev * HUG_X usage is an array mapped to 790b9b347e9SVladimir Kondratyev * both ABS_MT_POSITION and ABS_MT_TOOL 791b9b347e9SVladimir Kondratyev * events. So don`t stop search if we 792b9b347e9SVladimir Kondratyev * already have HUG_X mapping done. 793b9b347e9SVladimir Kondratyev */ 794b9b347e9SVladimir Kondratyev if (sc->locs[cont][i].size) 795b9b347e9SVladimir Kondratyev continue; 796b9b347e9SVladimir Kondratyev sc->locs[cont][i] = hi.loc; 797b9b347e9SVladimir Kondratyev /* 798b9b347e9SVladimir Kondratyev * Hid parser returns valid logical and 799b9b347e9SVladimir Kondratyev * physical sizes for first finger only 800b9b347e9SVladimir Kondratyev * at least on ElanTS 0x04f3:0x0012. 801b9b347e9SVladimir Kondratyev */ 802b9b347e9SVladimir Kondratyev if (cont > 0) 803b9b347e9SVladimir Kondratyev break; 804b9b347e9SVladimir Kondratyev setbit(sc->caps, i); 805cb022db8SVladimir Kondratyev sc->ai[i] = (struct hid_absinfo) { 806b9b347e9SVladimir Kondratyev .max = hi.logical_maximum, 807b9b347e9SVladimir Kondratyev .min = hi.logical_minimum, 808b9b347e9SVladimir Kondratyev .res = hid_item_resolution(&hi), 809b9b347e9SVladimir Kondratyev }; 810b9b347e9SVladimir Kondratyev break; 811b9b347e9SVladimir Kondratyev } 812b9b347e9SVladimir Kondratyev } 813b9b347e9SVladimir Kondratyev break; 814b9b347e9SVladimir Kondratyev default: 815b9b347e9SVladimir Kondratyev break; 816b9b347e9SVladimir Kondratyev } 817b9b347e9SVladimir Kondratyev } 818b9b347e9SVladimir Kondratyev hid_end_parse(hd); 819b9b347e9SVladimir Kondratyev 820b9b347e9SVladimir Kondratyev /* Check for required HID Usages */ 8210075742dSVladimir Kondratyev if ((!cont_count_found && cont != 1) || cont == 0) 822cb022db8SVladimir Kondratyev return (HMT_TYPE_UNSUPPORTED); 823cb022db8SVladimir Kondratyev for (i = 0; i < HMT_N_USAGES; i++) { 824cb022db8SVladimir Kondratyev if (hmt_hid_map[i].required && isclr(sc->caps, i)) 825cb022db8SVladimir Kondratyev return (HMT_TYPE_UNSUPPORTED); 826b9b347e9SVladimir Kondratyev } 827b9b347e9SVladimir Kondratyev 828b9b347e9SVladimir Kondratyev /* Touchpads must have at least one button */ 829cb022db8SVladimir Kondratyev if (type == HMT_TYPE_TOUCHPAD && !sc->max_button && !has_int_button) 830cb022db8SVladimir Kondratyev return (HMT_TYPE_UNSUPPORTED); 831b9b347e9SVladimir Kondratyev 832b9b347e9SVladimir Kondratyev /* 833b9b347e9SVladimir Kondratyev * According to specifications 'Contact Count Maximum' should be read 834b9b347e9SVladimir Kondratyev * from Feature Report rather than from HID descriptor. Set sane 835b9b347e9SVladimir Kondratyev * default value now to handle the case of 'Get Report' request failure 836b9b347e9SVladimir Kondratyev */ 837b9b347e9SVladimir Kondratyev if (cont_count_max < 1) 838b9b347e9SVladimir Kondratyev cont_count_max = cont; 839b9b347e9SVladimir Kondratyev 840b9b347e9SVladimir Kondratyev /* Report touch orientation if both width and height are supported */ 841cb022db8SVladimir Kondratyev if (isset(sc->caps, HMT_WIDTH) && isset(sc->caps, HMT_HEIGHT)) { 842cb022db8SVladimir Kondratyev setbit(sc->caps, HMT_ORIENTATION); 843cb022db8SVladimir Kondratyev sc->ai[HMT_ORIENTATION].max = 1; 844b9b347e9SVladimir Kondratyev } 845b9b347e9SVladimir Kondratyev 846b9b347e9SVladimir Kondratyev sc->cont_max_rlen = hid_report_size(d_ptr, d_len, hid_feature, 847b9b347e9SVladimir Kondratyev sc->cont_max_rid); 848b9b347e9SVladimir Kondratyev if (sc->btn_type_rid > 0) 849b9b347e9SVladimir Kondratyev sc->btn_type_rlen = hid_report_size(d_ptr, d_len, 850b9b347e9SVladimir Kondratyev hid_feature, sc->btn_type_rid); 851b9b347e9SVladimir Kondratyev if (sc->thqa_cert_rid > 0) 852b9b347e9SVladimir Kondratyev sc->thqa_cert_rlen = hid_report_size(d_ptr, d_len, 853b9b347e9SVladimir Kondratyev hid_feature, sc->thqa_cert_rid); 854b9b347e9SVladimir Kondratyev 855b9b347e9SVladimir Kondratyev sc->report_id = report_id; 85695add157SVladimir Kondratyev sc->cont_count_max = cont_count_max; 857b9b347e9SVladimir Kondratyev sc->nconts_per_report = cont; 858b9b347e9SVladimir Kondratyev sc->has_int_button = has_int_button; 8590075742dSVladimir Kondratyev sc->has_cont_count = cont_count_found; 8600075742dSVladimir Kondratyev sc->has_scan_time = scan_time_found; 861b9b347e9SVladimir Kondratyev 862b9b347e9SVladimir Kondratyev return (type); 863b9b347e9SVladimir Kondratyev } 864b9b347e9SVladimir Kondratyev 865b9b347e9SVladimir Kondratyev static int 866cb022db8SVladimir Kondratyev hmt_set_input_mode(struct hmt_softc *sc, enum hconf_input_mode mode) 867b9b347e9SVladimir Kondratyev { 868cb022db8SVladimir Kondratyev devclass_t hconf_devclass; 869cb022db8SVladimir Kondratyev device_t hconf; 870b9b347e9SVladimir Kondratyev int err; 871b9b347e9SVladimir Kondratyev 872d14bc723SWarner Losh bus_topo_assert(); 873b9b347e9SVladimir Kondratyev 874cb022db8SVladimir Kondratyev /* Find touchpad's configuration TLC */ 875cb022db8SVladimir Kondratyev hconf = hidbus_find_child(device_get_parent(sc->dev), 876cb022db8SVladimir Kondratyev HID_USAGE2(HUP_DIGITIZERS, HUD_CONFIG)); 877cb022db8SVladimir Kondratyev if (hconf == NULL) 878cb022db8SVladimir Kondratyev return (ENXIO); 879b9b347e9SVladimir Kondratyev 880cb022db8SVladimir Kondratyev /* Ensure that hconf driver is attached to configuration TLC */ 881cb022db8SVladimir Kondratyev if (device_is_alive(hconf) == 0) 882cb022db8SVladimir Kondratyev device_probe_and_attach(hconf); 883cb022db8SVladimir Kondratyev if (device_is_attached(hconf) == 0) 884cb022db8SVladimir Kondratyev return (ENXIO); 885cb022db8SVladimir Kondratyev hconf_devclass = devclass_find("hconf"); 886cb022db8SVladimir Kondratyev if (device_get_devclass(hconf) != hconf_devclass) 887cb022db8SVladimir Kondratyev return (ENXIO); 888cb022db8SVladimir Kondratyev 889*f76a0288SGordon Bergling /* hconf_set_input_mode can drop the topo lock while sleeping */ 890cb022db8SVladimir Kondratyev device_busy(hconf); 891cb022db8SVladimir Kondratyev err = hconf_set_input_mode(hconf, mode); 892cb022db8SVladimir Kondratyev device_unbusy(hconf); 893b9b347e9SVladimir Kondratyev 894b9b347e9SVladimir Kondratyev return (err); 895b9b347e9SVladimir Kondratyev } 896b9b347e9SVladimir Kondratyev 897cb022db8SVladimir Kondratyev static device_method_t hmt_methods[] = { 898cb022db8SVladimir Kondratyev DEVMETHOD(device_probe, hmt_probe), 899cb022db8SVladimir Kondratyev DEVMETHOD(device_attach, hmt_attach), 900cb022db8SVladimir Kondratyev DEVMETHOD(device_detach, hmt_detach), 901b9b347e9SVladimir Kondratyev 902b9b347e9SVladimir Kondratyev DEVMETHOD_END 903b9b347e9SVladimir Kondratyev }; 904b9b347e9SVladimir Kondratyev 905cb022db8SVladimir Kondratyev static driver_t hmt_driver = { 906cb022db8SVladimir Kondratyev .name = "hmt", 907cb022db8SVladimir Kondratyev .methods = hmt_methods, 908cb022db8SVladimir Kondratyev .size = sizeof(struct hmt_softc), 909b9b347e9SVladimir Kondratyev }; 910b9b347e9SVladimir Kondratyev 9117eeede15SJohn Baldwin DRIVER_MODULE(hmt, hidbus, hmt_driver, NULL, NULL); 912cb022db8SVladimir Kondratyev MODULE_DEPEND(hmt, hidbus, 1, 1, 1); 913cb022db8SVladimir Kondratyev MODULE_DEPEND(hmt, hid, 1, 1, 1); 914cb022db8SVladimir Kondratyev MODULE_DEPEND(hmt, hconf, 1, 1, 1); 915cb022db8SVladimir Kondratyev MODULE_DEPEND(hmt, evdev, 1, 1, 1); 916cb022db8SVladimir Kondratyev MODULE_VERSION(hmt, 1); 917cb022db8SVladimir Kondratyev HID_PNP_INFO(hmt_devs); 918