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 { 81*9d8ebe5eSVladimir Kondratyev HMT_TIP_SWITCH = ABS_MT_INDEX(ABS_MT_TOOL_TYPE), 82*9d8ebe5eSVladimir Kondratyev HMT_WIDTH = ABS_MT_INDEX(ABS_MT_TOUCH_MAJOR), 83*9d8ebe5eSVladimir Kondratyev HMT_HEIGHT = ABS_MT_INDEX(ABS_MT_TOUCH_MINOR), 84*9d8ebe5eSVladimir Kondratyev HMT_ORIENTATION = ABS_MT_INDEX(ABS_MT_ORIENTATION), 85*9d8ebe5eSVladimir Kondratyev HMT_X = ABS_MT_INDEX(ABS_MT_POSITION_X), 86*9d8ebe5eSVladimir Kondratyev HMT_Y = ABS_MT_INDEX(ABS_MT_POSITION_Y), 87*9d8ebe5eSVladimir Kondratyev HMT_CONTACTID = ABS_MT_INDEX(ABS_MT_TRACKING_ID), 88*9d8ebe5eSVladimir Kondratyev HMT_PRESSURE = ABS_MT_INDEX(ABS_MT_PRESSURE), 89*9d8ebe5eSVladimir Kondratyev HMT_IN_RANGE = ABS_MT_INDEX(ABS_MT_DISTANCE), 90*9d8ebe5eSVladimir Kondratyev HMT_CONFIDENCE = ABS_MT_INDEX(ABS_MT_BLOB_ID), 91*9d8ebe5eSVladimir Kondratyev HMT_TOOL_X = ABS_MT_INDEX(ABS_MT_TOOL_X), 92*9d8ebe5eSVladimir Kondratyev HMT_TOOL_Y = ABS_MT_INDEX(ABS_MT_TOOL_Y), 93b9b347e9SVladimir Kondratyev }; 94b9b347e9SVladimir Kondratyev 95*9d8ebe5eSVladimir 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 */ 101*9d8ebe5eSVladimir 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] = { 106*9d8ebe5eSVladimir Kondratyev [HMT_TIP_SWITCH] = { 107b9b347e9SVladimir Kondratyev .name = "TIP", 108b9b347e9SVladimir Kondratyev .usage = HID_USAGE2(HUP_DIGITIZERS, HUD_TIP_SWITCH), 109*9d8ebe5eSVladimir Kondratyev .reported = false, 110b9b347e9SVladimir Kondratyev .required = true, 111b9b347e9SVladimir Kondratyev }, 112*9d8ebe5eSVladimir Kondratyev [HMT_WIDTH] = { 113b9b347e9SVladimir Kondratyev .name = "WDTH", 114b9b347e9SVladimir Kondratyev .usage = HID_USAGE2(HUP_DIGITIZERS, HUD_WIDTH), 115*9d8ebe5eSVladimir Kondratyev .reported = true, 116b9b347e9SVladimir Kondratyev .required = false, 117b9b347e9SVladimir Kondratyev }, 118*9d8ebe5eSVladimir Kondratyev [HMT_HEIGHT] = { 119b9b347e9SVladimir Kondratyev .name = "HGHT", 120b9b347e9SVladimir Kondratyev .usage = HID_USAGE2(HUP_DIGITIZERS, HUD_HEIGHT), 121*9d8ebe5eSVladimir Kondratyev .reported = true, 122b9b347e9SVladimir Kondratyev .required = false, 123b9b347e9SVladimir Kondratyev }, 124cb022db8SVladimir Kondratyev [HMT_ORIENTATION] = { 125b9b347e9SVladimir Kondratyev .name = "ORIE", 126cb022db8SVladimir Kondratyev .usage = HMT_NO_USAGE, 127*9d8ebe5eSVladimir 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), 133*9d8ebe5eSVladimir 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), 139*9d8ebe5eSVladimir 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), 145*9d8ebe5eSVladimir 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), 151*9d8ebe5eSVladimir 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), 157*9d8ebe5eSVladimir 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), 163*9d8ebe5eSVladimir Kondratyev .reported = false, 164b9b347e9SVladimir Kondratyev .required = false, 165b9b347e9SVladimir Kondratyev }, 166*9d8ebe5eSVladimir 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), 169*9d8ebe5eSVladimir Kondratyev .reported = true, 170b9b347e9SVladimir Kondratyev .required = false, 171b9b347e9SVladimir Kondratyev }, 172*9d8ebe5eSVladimir 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), 175*9d8ebe5eSVladimir 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 199*9d8ebe5eSVladimir 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; 207b9b347e9SVladimir Kondratyev bool is_clickpad; 208b9b347e9SVladimir Kondratyev bool do_timestamps; 2095cc21ab9SVladimir Kondratyev #ifdef IICHID_SAMPLING 210cb022db8SVladimir Kondratyev bool iichid_sampling; 2115cc21ab9SVladimir Kondratyev #endif 212b9b347e9SVladimir Kondratyev 213b9b347e9SVladimir Kondratyev struct hid_location cont_max_loc; 214b9b347e9SVladimir Kondratyev uint32_t cont_max_rlen; 215b9b347e9SVladimir Kondratyev uint8_t cont_max_rid; 216b9b347e9SVladimir Kondratyev struct hid_location btn_type_loc; 217b9b347e9SVladimir Kondratyev uint32_t btn_type_rlen; 218b9b347e9SVladimir Kondratyev uint8_t btn_type_rid; 219b9b347e9SVladimir Kondratyev uint32_t thqa_cert_rlen; 220b9b347e9SVladimir Kondratyev uint8_t thqa_cert_rid; 221b9b347e9SVladimir Kondratyev }; 222b9b347e9SVladimir Kondratyev 223cb022db8SVladimir Kondratyev #define HMT_FOREACH_USAGE(caps, usage) \ 224cb022db8SVladimir Kondratyev for ((usage) = 0; (usage) < HMT_N_USAGES; ++(usage)) \ 225b9b347e9SVladimir Kondratyev if (isset((caps), (usage))) 226b9b347e9SVladimir Kondratyev 227cb022db8SVladimir Kondratyev static enum hmt_type hmt_hid_parse(struct hmt_softc *, const void *, 228cb022db8SVladimir Kondratyev hid_size_t, uint32_t, uint8_t); 229cb022db8SVladimir Kondratyev static int hmt_set_input_mode(struct hmt_softc *, enum hconf_input_mode); 230b9b347e9SVladimir Kondratyev 231cb022db8SVladimir Kondratyev static hid_intr_t hmt_intr; 232b9b347e9SVladimir Kondratyev 233cb022db8SVladimir Kondratyev static device_probe_t hmt_probe; 234cb022db8SVladimir Kondratyev static device_attach_t hmt_attach; 235cb022db8SVladimir Kondratyev static device_detach_t hmt_detach; 236b9b347e9SVladimir Kondratyev 237cb022db8SVladimir Kondratyev static evdev_open_t hmt_ev_open; 238cb022db8SVladimir Kondratyev static evdev_close_t hmt_ev_close; 239b9b347e9SVladimir Kondratyev 240cb022db8SVladimir Kondratyev static const struct evdev_methods hmt_evdev_methods = { 241cb022db8SVladimir Kondratyev .ev_open = &hmt_ev_open, 242cb022db8SVladimir Kondratyev .ev_close = &hmt_ev_close, 243b9b347e9SVladimir Kondratyev }; 244b9b347e9SVladimir Kondratyev 245cb022db8SVladimir Kondratyev static const struct hid_device_id hmt_devs[] = { 246cb022db8SVladimir Kondratyev { HID_TLC(HUP_DIGITIZERS, HUD_TOUCHSCREEN) }, 247cb022db8SVladimir Kondratyev { HID_TLC(HUP_DIGITIZERS, HUD_TOUCHPAD) }, 248b9b347e9SVladimir Kondratyev }; 249b9b347e9SVladimir Kondratyev 250b9b347e9SVladimir Kondratyev static int 251cb022db8SVladimir Kondratyev hmt_ev_close(struct evdev_dev *evdev) 252b9b347e9SVladimir Kondratyev { 253cb022db8SVladimir Kondratyev return (hidbus_intr_stop(evdev_get_softc(evdev))); 254cb022db8SVladimir Kondratyev } 255cb022db8SVladimir Kondratyev 256cb022db8SVladimir Kondratyev static int 257cb022db8SVladimir Kondratyev hmt_ev_open(struct evdev_dev *evdev) 258cb022db8SVladimir Kondratyev { 259cb022db8SVladimir Kondratyev return (hidbus_intr_start(evdev_get_softc(evdev))); 260cb022db8SVladimir Kondratyev } 261cb022db8SVladimir Kondratyev 262cb022db8SVladimir Kondratyev static int 263cb022db8SVladimir Kondratyev hmt_probe(device_t dev) 264cb022db8SVladimir Kondratyev { 265cb022db8SVladimir Kondratyev struct hmt_softc *sc = device_get_softc(dev); 266b9b347e9SVladimir Kondratyev void *d_ptr; 267cb022db8SVladimir Kondratyev hid_size_t d_len; 268b9b347e9SVladimir Kondratyev int err; 269b9b347e9SVladimir Kondratyev 270cb022db8SVladimir Kondratyev err = HIDBUS_LOOKUP_DRIVER_INFO(dev, hmt_devs); 271cb022db8SVladimir Kondratyev if (err != 0) 272cb022db8SVladimir Kondratyev return (err); 273cb022db8SVladimir Kondratyev 274cb022db8SVladimir Kondratyev err = hid_get_report_descr(dev, &d_ptr, &d_len); 275cb022db8SVladimir Kondratyev if (err != 0) { 276cb022db8SVladimir Kondratyev device_printf(dev, "could not retrieve report descriptor from " 277cb022db8SVladimir Kondratyev "device: %d\n", err); 278b9b347e9SVladimir Kondratyev return (ENXIO); 279b9b347e9SVladimir Kondratyev } 280b9b347e9SVladimir Kondratyev 281cb022db8SVladimir Kondratyev /* Check if report descriptor belongs to a HID multitouch device */ 282cb022db8SVladimir Kondratyev if (sc->type == HMT_TYPE_UNKNOWN) 283cb022db8SVladimir Kondratyev sc->type = hmt_hid_parse(sc, d_ptr, d_len, 284cb022db8SVladimir Kondratyev hidbus_get_usage(dev), hidbus_get_index(dev)); 285cb022db8SVladimir Kondratyev if (sc->type == HMT_TYPE_UNSUPPORTED) 286cb022db8SVladimir Kondratyev return (ENXIO); 287cb022db8SVladimir Kondratyev 288cb022db8SVladimir Kondratyev hidbus_set_desc(dev, 289cb022db8SVladimir Kondratyev sc->type == HMT_TYPE_TOUCHPAD ? "TouchPad" : "TouchScreen"); 290cb022db8SVladimir Kondratyev 291cb022db8SVladimir Kondratyev return (BUS_PROBE_DEFAULT); 292b9b347e9SVladimir Kondratyev } 293b9b347e9SVladimir Kondratyev 294b9b347e9SVladimir Kondratyev static int 295cb022db8SVladimir Kondratyev hmt_attach(device_t dev) 296b9b347e9SVladimir Kondratyev { 297cb022db8SVladimir Kondratyev struct hmt_softc *sc = device_get_softc(dev); 298cb022db8SVladimir Kondratyev const struct hid_device_info *hw = hid_get_device_info(dev); 299cb022db8SVladimir Kondratyev void *d_ptr; 300cb022db8SVladimir Kondratyev uint8_t *fbuf = NULL; 301cb022db8SVladimir Kondratyev hid_size_t d_len, fsize; 302b9b347e9SVladimir Kondratyev uint32_t cont_count_max; 303b9b347e9SVladimir Kondratyev int nbuttons, btn; 304b9b347e9SVladimir Kondratyev size_t i; 305b9b347e9SVladimir Kondratyev int err; 306b9b347e9SVladimir Kondratyev 307cb022db8SVladimir Kondratyev err = hid_get_report_descr(dev, &d_ptr, &d_len); 308cb022db8SVladimir Kondratyev if (err != 0) { 309cb022db8SVladimir Kondratyev device_printf(dev, "could not retrieve report descriptor from " 310cb022db8SVladimir Kondratyev "device: %d\n", err); 311cb022db8SVladimir Kondratyev return (ENXIO); 312cb022db8SVladimir Kondratyev } 313cb022db8SVladimir Kondratyev 314b9b347e9SVladimir Kondratyev sc->dev = dev; 315b9b347e9SVladimir Kondratyev 316cb022db8SVladimir Kondratyev fsize = hid_report_size_max(d_ptr, d_len, hid_feature, NULL); 317cb022db8SVladimir Kondratyev if (fsize != 0) 318cb022db8SVladimir Kondratyev fbuf = malloc(fsize, M_TEMP, M_WAITOK | M_ZERO); 319cb022db8SVladimir Kondratyev 320b9b347e9SVladimir Kondratyev /* Fetch and parse "Contact count maximum" feature report */ 321cb022db8SVladimir Kondratyev if (sc->cont_max_rlen > 1) { 322cb022db8SVladimir Kondratyev err = hid_get_report(dev, fbuf, sc->cont_max_rlen, NULL, 323cb022db8SVladimir Kondratyev HID_FEATURE_REPORT, sc->cont_max_rid); 324cb022db8SVladimir Kondratyev if (err == 0) { 325cb022db8SVladimir Kondratyev cont_count_max = hid_get_udata(fbuf + 1, 326b9b347e9SVladimir Kondratyev sc->cont_max_rlen - 1, &sc->cont_max_loc); 327b9b347e9SVladimir Kondratyev /* 328b9b347e9SVladimir Kondratyev * Feature report is a primary source of 329b9b347e9SVladimir Kondratyev * 'Contact Count Maximum' 330b9b347e9SVladimir Kondratyev */ 331b9b347e9SVladimir Kondratyev if (cont_count_max > 0) 33295add157SVladimir Kondratyev sc->cont_count_max = cont_count_max; 333b9b347e9SVladimir Kondratyev } else 334cb022db8SVladimir Kondratyev DPRINTF("hid_get_report error=%d\n", err); 335b9b347e9SVladimir Kondratyev } else 336cb022db8SVladimir Kondratyev DPRINTF("Feature report %hhu size invalid: %u\n", 337b9b347e9SVladimir Kondratyev sc->cont_max_rid, sc->cont_max_rlen); 338b9b347e9SVladimir Kondratyev 339b9b347e9SVladimir Kondratyev /* Fetch and parse "Button type" feature report */ 340cb022db8SVladimir Kondratyev if (sc->btn_type_rlen > 1 && sc->btn_type_rid != sc->cont_max_rid) { 341cb022db8SVladimir Kondratyev bzero(fbuf, fsize); 342cb022db8SVladimir Kondratyev err = hid_get_report(dev, fbuf, sc->btn_type_rlen, NULL, 343cb022db8SVladimir Kondratyev HID_FEATURE_REPORT, sc->btn_type_rid); 344b9b347e9SVladimir Kondratyev } 345b9b347e9SVladimir Kondratyev if (sc->btn_type_rlen > 1) { 346b9b347e9SVladimir Kondratyev if (err == 0) 347cb022db8SVladimir Kondratyev sc->is_clickpad = hid_get_udata(fbuf + 1, 348b9b347e9SVladimir Kondratyev sc->btn_type_rlen - 1, &sc->btn_type_loc) == 0; 349b9b347e9SVladimir Kondratyev else 350cb022db8SVladimir Kondratyev DPRINTF("hid_get_report error=%d\n", err); 351b9b347e9SVladimir Kondratyev } 352b9b347e9SVladimir Kondratyev 353b9b347e9SVladimir Kondratyev /* Fetch THQA certificate to enable some devices like WaveShare */ 354cb022db8SVladimir Kondratyev if (sc->thqa_cert_rlen > 1 && sc->thqa_cert_rid != sc->cont_max_rid) 355cb022db8SVladimir Kondratyev (void)hid_get_report(dev, fbuf, sc->thqa_cert_rlen, NULL, 356cb022db8SVladimir Kondratyev HID_FEATURE_REPORT, sc->thqa_cert_rid); 357cb022db8SVladimir Kondratyev 358cb022db8SVladimir Kondratyev free(fbuf, M_TEMP); 359b9b347e9SVladimir Kondratyev 360b9b347e9SVladimir Kondratyev /* Switch touchpad in to absolute multitouch mode */ 361cb022db8SVladimir Kondratyev if (sc->type == HMT_TYPE_TOUCHPAD) { 362cb022db8SVladimir Kondratyev err = hmt_set_input_mode(sc, HCONF_INPUT_MODE_MT_TOUCHPAD); 363b9b347e9SVladimir Kondratyev if (err != 0) 364b9b347e9SVladimir Kondratyev DPRINTF("Failed to set input mode: %d\n", err); 365b9b347e9SVladimir Kondratyev } 366b9b347e9SVladimir Kondratyev 367b9b347e9SVladimir Kondratyev /* Cap contact count maximum to MAX_MT_SLOTS */ 36895add157SVladimir Kondratyev if (sc->cont_count_max > MAX_MT_SLOTS) { 369b9b347e9SVladimir Kondratyev DPRINTF("Hardware reported %d contacts while only %d is " 37095add157SVladimir Kondratyev "supported\n", sc->cont_count_max, MAX_MT_SLOTS); 37195add157SVladimir Kondratyev sc->cont_count_max = MAX_MT_SLOTS; 372b9b347e9SVladimir Kondratyev } 373b9b347e9SVladimir Kondratyev 374cb022db8SVladimir Kondratyev if (hid_test_quirk(hw, HQ_MT_TIMESTAMP) || hmt_timestamps) 375b9b347e9SVladimir Kondratyev sc->do_timestamps = true; 3765cc21ab9SVladimir Kondratyev #ifdef IICHID_SAMPLING 377cb022db8SVladimir Kondratyev if (hid_test_quirk(hw, HQ_IICHID_SAMPLING)) 378cb022db8SVladimir Kondratyev sc->iichid_sampling = true; 3795cc21ab9SVladimir Kondratyev #endif 380b9b347e9SVladimir Kondratyev 381cb022db8SVladimir Kondratyev hidbus_set_intr(dev, hmt_intr, sc); 382b9b347e9SVladimir Kondratyev 383b9b347e9SVladimir Kondratyev sc->evdev = evdev_alloc(); 384b9b347e9SVladimir Kondratyev evdev_set_name(sc->evdev, device_get_desc(dev)); 385b9b347e9SVladimir Kondratyev evdev_set_phys(sc->evdev, device_get_nameunit(dev)); 386cb022db8SVladimir Kondratyev evdev_set_id(sc->evdev, hw->idBus, hw->idVendor, hw->idProduct, 387cb022db8SVladimir Kondratyev hw->idVersion); 388cb022db8SVladimir Kondratyev evdev_set_serial(sc->evdev, hw->serial); 389cb022db8SVladimir Kondratyev evdev_set_methods(sc->evdev, dev, &hmt_evdev_methods); 390b9b347e9SVladimir Kondratyev evdev_set_flag(sc->evdev, EVDEV_FLAG_MT_STCOMPAT); 391cb022db8SVladimir Kondratyev evdev_set_flag(sc->evdev, EVDEV_FLAG_EXT_EPOCH); /* hidbus child */ 392b9b347e9SVladimir Kondratyev switch (sc->type) { 393cb022db8SVladimir Kondratyev case HMT_TYPE_TOUCHSCREEN: 394b9b347e9SVladimir Kondratyev evdev_support_prop(sc->evdev, INPUT_PROP_DIRECT); 395b9b347e9SVladimir Kondratyev break; 396cb022db8SVladimir Kondratyev case HMT_TYPE_TOUCHPAD: 397b9b347e9SVladimir Kondratyev evdev_support_prop(sc->evdev, INPUT_PROP_POINTER); 398b9b347e9SVladimir Kondratyev if (sc->is_clickpad) 399b9b347e9SVladimir Kondratyev evdev_support_prop(sc->evdev, INPUT_PROP_BUTTONPAD); 400b9b347e9SVladimir Kondratyev break; 401b9b347e9SVladimir Kondratyev default: 402cb022db8SVladimir Kondratyev KASSERT(0, ("hmt_attach: unsupported touch device type")); 403b9b347e9SVladimir Kondratyev } 404b9b347e9SVladimir Kondratyev evdev_support_event(sc->evdev, EV_SYN); 405b9b347e9SVladimir Kondratyev evdev_support_event(sc->evdev, EV_ABS); 406b9b347e9SVladimir Kondratyev if (sc->do_timestamps) { 407b9b347e9SVladimir Kondratyev evdev_support_event(sc->evdev, EV_MSC); 408b9b347e9SVladimir Kondratyev evdev_support_msc(sc->evdev, MSC_TIMESTAMP); 409b9b347e9SVladimir Kondratyev } 4105cc21ab9SVladimir Kondratyev #ifdef IICHID_SAMPLING 411cb022db8SVladimir Kondratyev if (sc->iichid_sampling) 412cb022db8SVladimir Kondratyev evdev_set_flag(sc->evdev, EVDEV_FLAG_MT_AUTOREL); 4135cc21ab9SVladimir Kondratyev #endif 414b9b347e9SVladimir Kondratyev nbuttons = 0; 415b9b347e9SVladimir Kondratyev if (sc->max_button != 0 || sc->has_int_button) { 416b9b347e9SVladimir Kondratyev evdev_support_event(sc->evdev, EV_KEY); 417b9b347e9SVladimir Kondratyev if (sc->has_int_button) 418b9b347e9SVladimir Kondratyev evdev_support_key(sc->evdev, BTN_LEFT); 419b9b347e9SVladimir Kondratyev for (btn = 0; btn < sc->max_button; ++btn) { 420b9b347e9SVladimir Kondratyev if (isset(sc->buttons, btn)) { 421b9b347e9SVladimir Kondratyev evdev_support_key(sc->evdev, BTN_MOUSE + btn); 422b9b347e9SVladimir Kondratyev nbuttons++; 423b9b347e9SVladimir Kondratyev } 424b9b347e9SVladimir Kondratyev } 425b9b347e9SVladimir Kondratyev } 426*9d8ebe5eSVladimir Kondratyev evdev_support_abs(sc->evdev, 427*9d8ebe5eSVladimir Kondratyev ABS_MT_SLOT, 0, sc->cont_count_max - 1, 0, 0, 0); 428cb022db8SVladimir Kondratyev HMT_FOREACH_USAGE(sc->caps, i) { 429*9d8ebe5eSVladimir Kondratyev if (hmt_hid_map[i].reported) 430*9d8ebe5eSVladimir Kondratyev evdev_support_abs(sc->evdev, ABS_MT_FIRST + i, 431b9b347e9SVladimir Kondratyev sc->ai[i].min, sc->ai[i].max, 0, 0, sc->ai[i].res); 432b9b347e9SVladimir Kondratyev } 433b9b347e9SVladimir Kondratyev 434cb022db8SVladimir Kondratyev err = evdev_register(sc->evdev); 435cb022db8SVladimir Kondratyev if (err) { 436cb022db8SVladimir Kondratyev hmt_detach(dev); 437b9b347e9SVladimir Kondratyev return (ENXIO); 438b9b347e9SVladimir Kondratyev } 439b9b347e9SVladimir Kondratyev 440cb022db8SVladimir Kondratyev /* Announce information about the touch device */ 441cb022db8SVladimir Kondratyev device_printf(sc->dev, "Multitouch %s with %d external button%s%s\n", 442cb022db8SVladimir Kondratyev sc->type == HMT_TYPE_TOUCHSCREEN ? "touchscreen" : "touchpad", 443cb022db8SVladimir Kondratyev nbuttons, nbuttons != 1 ? "s" : "", 444cb022db8SVladimir Kondratyev sc->is_clickpad ? ", click-pad" : ""); 445cb022db8SVladimir Kondratyev device_printf(sc->dev, 446cb022db8SVladimir Kondratyev "%d contacts with [%s%s%s%s%s] properties. Report range [%d:%d] - [%d:%d]\n", 44795add157SVladimir Kondratyev (int)sc->cont_count_max, 448cb022db8SVladimir Kondratyev isset(sc->caps, HMT_IN_RANGE) ? "R" : "", 449cb022db8SVladimir Kondratyev isset(sc->caps, HMT_CONFIDENCE) ? "C" : "", 450cb022db8SVladimir Kondratyev isset(sc->caps, HMT_WIDTH) ? "W" : "", 451cb022db8SVladimir Kondratyev isset(sc->caps, HMT_HEIGHT) ? "H" : "", 452cb022db8SVladimir Kondratyev isset(sc->caps, HMT_PRESSURE) ? "P" : "", 453cb022db8SVladimir Kondratyev (int)sc->ai[HMT_X].min, (int)sc->ai[HMT_Y].min, 454cb022db8SVladimir Kondratyev (int)sc->ai[HMT_X].max, (int)sc->ai[HMT_Y].max); 455cb022db8SVladimir Kondratyev 456cb022db8SVladimir Kondratyev return (0); 457cb022db8SVladimir Kondratyev } 458cb022db8SVladimir Kondratyev 459b9b347e9SVladimir Kondratyev static int 460cb022db8SVladimir Kondratyev hmt_detach(device_t dev) 461b9b347e9SVladimir Kondratyev { 462cb022db8SVladimir Kondratyev struct hmt_softc *sc = device_get_softc(dev); 463b9b347e9SVladimir Kondratyev 464b9b347e9SVladimir Kondratyev evdev_free(sc->evdev); 465cb022db8SVladimir Kondratyev 466b9b347e9SVladimir Kondratyev return (0); 467b9b347e9SVladimir Kondratyev } 468b9b347e9SVladimir Kondratyev 469b9b347e9SVladimir Kondratyev static void 470cb022db8SVladimir Kondratyev hmt_intr(void *context, void *buf, hid_size_t len) 471b9b347e9SVladimir Kondratyev { 472cb022db8SVladimir Kondratyev struct hmt_softc *sc = context; 473b9b347e9SVladimir Kondratyev size_t usage; 474*9d8ebe5eSVladimir Kondratyev union evdev_mt_slot *slot_data; 475b9b347e9SVladimir Kondratyev uint32_t cont, btn; 476b9b347e9SVladimir Kondratyev uint32_t cont_count; 477b9b347e9SVladimir Kondratyev uint32_t width; 478b9b347e9SVladimir Kondratyev uint32_t height; 479b9b347e9SVladimir Kondratyev uint32_t int_btn = 0; 480b9b347e9SVladimir Kondratyev uint32_t left_btn = 0; 481*9d8ebe5eSVladimir Kondratyev int slot; 482b9b347e9SVladimir Kondratyev uint32_t scan_time; 483b9b347e9SVladimir Kondratyev int32_t delta; 484cb022db8SVladimir Kondratyev uint8_t id; 485cb022db8SVladimir Kondratyev 4865cc21ab9SVladimir Kondratyev #ifdef IICHID_SAMPLING 487cb022db8SVladimir Kondratyev /* 488cb022db8SVladimir Kondratyev * Special packet of zero length is generated by iichid driver running 489cb022db8SVladimir Kondratyev * in polling mode at the start of inactivity period to workaround 490cb022db8SVladimir Kondratyev * "stuck touch" problem caused by miss of finger release events. 491cb022db8SVladimir Kondratyev * This snippet is to be removed after GPIO interrupt support is added. 492cb022db8SVladimir Kondratyev */ 493cb022db8SVladimir Kondratyev if (sc->iichid_sampling && len == 0) { 494cb022db8SVladimir Kondratyev sc->prev_touch = false; 495cb022db8SVladimir Kondratyev sc->timestamp = 0; 496*9d8ebe5eSVladimir Kondratyev /* EVDEV_FLAG_MT_AUTOREL releases all touches for us */ 497cb022db8SVladimir Kondratyev evdev_sync(sc->evdev); 498cb022db8SVladimir Kondratyev return; 499cb022db8SVladimir Kondratyev } 5005cc21ab9SVladimir Kondratyev #endif 501cb022db8SVladimir Kondratyev 502cb022db8SVladimir Kondratyev /* Ignore irrelevant reports */ 503cb022db8SVladimir Kondratyev id = sc->report_id != 0 ? *(uint8_t *)buf : 0; 504cb022db8SVladimir Kondratyev if (sc->report_id != id) { 505cb022db8SVladimir Kondratyev DPRINTF("Skip report with unexpected ID: %hhu\n", id); 506cb022db8SVladimir Kondratyev return; 507cb022db8SVladimir Kondratyev } 508cb022db8SVladimir Kondratyev 509cb022db8SVladimir Kondratyev /* Strip leading "report ID" byte */ 510cb022db8SVladimir Kondratyev if (sc->report_id != 0) { 511cb022db8SVladimir Kondratyev len--; 512cb022db8SVladimir Kondratyev buf = (uint8_t *)buf + 1; 513cb022db8SVladimir Kondratyev } 514b9b347e9SVladimir Kondratyev 515b9b347e9SVladimir Kondratyev /* 516b9b347e9SVladimir Kondratyev * "In Parallel mode, devices report all contact information in a 517b9b347e9SVladimir Kondratyev * single packet. Each physical contact is represented by a logical 518b9b347e9SVladimir Kondratyev * collection that is embedded in the top-level collection." 519b9b347e9SVladimir Kondratyev * 520b9b347e9SVladimir Kondratyev * Since additional contacts that were not present will still be in the 521b9b347e9SVladimir Kondratyev * report with contactid=0 but contactids are zero-based, find 522b9b347e9SVladimir Kondratyev * contactcount first. 523b9b347e9SVladimir Kondratyev */ 524b9b347e9SVladimir Kondratyev cont_count = hid_get_udata(buf, len, &sc->cont_count_loc); 525b9b347e9SVladimir Kondratyev /* 526b9b347e9SVladimir Kondratyev * "In Hybrid mode, the number of contacts that can be reported in one 527b9b347e9SVladimir Kondratyev * report is less than the maximum number of contacts that the device 528b9b347e9SVladimir Kondratyev * supports. For example, a device that supports a maximum of 529b9b347e9SVladimir Kondratyev * 4 concurrent physical contacts, can set up its top-level collection 530b9b347e9SVladimir Kondratyev * to deliver a maximum of two contacts in one report. If four contact 531b9b347e9SVladimir Kondratyev * points are present, the device can break these up into two serial 532b9b347e9SVladimir Kondratyev * reports that deliver two contacts each. 533b9b347e9SVladimir Kondratyev * 534b9b347e9SVladimir Kondratyev * "When a device delivers data in this manner, the Contact Count usage 535b9b347e9SVladimir Kondratyev * value in the first report should reflect the total number of 536b9b347e9SVladimir Kondratyev * contacts that are being delivered in the hybrid reports. The other 537b9b347e9SVladimir Kondratyev * serial reports should have a contact count of zero (0)." 538b9b347e9SVladimir Kondratyev */ 539b9b347e9SVladimir Kondratyev if (cont_count != 0) 540b9b347e9SVladimir Kondratyev sc->nconts_todo = cont_count; 541b9b347e9SVladimir Kondratyev 542cb022db8SVladimir Kondratyev #ifdef HID_DEBUG 543b9b347e9SVladimir Kondratyev DPRINTFN(6, "cont_count:%2u", (unsigned)cont_count); 544cb022db8SVladimir Kondratyev if (hmt_debug >= 6) { 545cb022db8SVladimir Kondratyev HMT_FOREACH_USAGE(sc->caps, usage) { 546cb022db8SVladimir Kondratyev if (hmt_hid_map[usage].usage != HMT_NO_USAGE) 547cb022db8SVladimir Kondratyev printf(" %-4s", hmt_hid_map[usage].name); 548b9b347e9SVladimir Kondratyev } 549b9b347e9SVladimir Kondratyev printf("\n"); 550b9b347e9SVladimir Kondratyev } 551b9b347e9SVladimir Kondratyev #endif 552b9b347e9SVladimir Kondratyev 553b9b347e9SVladimir Kondratyev /* Find the number of contacts reported in current report */ 554b9b347e9SVladimir Kondratyev cont_count = MIN(sc->nconts_todo, sc->nconts_per_report); 555b9b347e9SVladimir Kondratyev 556b9b347e9SVladimir Kondratyev /* Use protocol Type B for reporting events */ 557b9b347e9SVladimir Kondratyev for (cont = 0; cont < cont_count; cont++) { 558*9d8ebe5eSVladimir Kondratyev slot_data = &sc->slot_data; 559b9b347e9SVladimir Kondratyev bzero(slot_data, sizeof(sc->slot_data)); 560cb022db8SVladimir Kondratyev HMT_FOREACH_USAGE(sc->caps, usage) { 561b9b347e9SVladimir Kondratyev if (sc->locs[cont][usage].size > 0) 562*9d8ebe5eSVladimir Kondratyev slot_data->val[usage] = hid_get_udata( 563b9b347e9SVladimir Kondratyev buf, len, &sc->locs[cont][usage]); 564b9b347e9SVladimir Kondratyev } 565b9b347e9SVladimir Kondratyev 566*9d8ebe5eSVladimir Kondratyev slot = evdev_mt_id_to_slot(sc->evdev, slot_data->id); 567b9b347e9SVladimir Kondratyev 568cb022db8SVladimir Kondratyev #ifdef HID_DEBUG 569b9b347e9SVladimir Kondratyev DPRINTFN(6, "cont%01x: data = ", cont); 570cb022db8SVladimir Kondratyev if (hmt_debug >= 6) { 571cb022db8SVladimir Kondratyev HMT_FOREACH_USAGE(sc->caps, usage) { 572cb022db8SVladimir Kondratyev if (hmt_hid_map[usage].usage != HMT_NO_USAGE) 573*9d8ebe5eSVladimir Kondratyev printf("%04x ", slot_data->val[usage]); 574b9b347e9SVladimir Kondratyev } 575*9d8ebe5eSVladimir Kondratyev printf("slot = %d\n", slot); 576b9b347e9SVladimir Kondratyev } 577b9b347e9SVladimir Kondratyev #endif 578b9b347e9SVladimir Kondratyev 579b9b347e9SVladimir Kondratyev if (slot == -1) { 580b9b347e9SVladimir Kondratyev DPRINTF("Slot overflow for contact_id %u\n", 581*9d8ebe5eSVladimir Kondratyev (unsigned)slot_data->id); 582b9b347e9SVladimir Kondratyev continue; 583b9b347e9SVladimir Kondratyev } 584b9b347e9SVladimir Kondratyev 585*9d8ebe5eSVladimir Kondratyev if (slot_data->val[HMT_TIP_SWITCH] != 0 && 586cb022db8SVladimir Kondratyev !(isset(sc->caps, HMT_CONFIDENCE) && 587*9d8ebe5eSVladimir Kondratyev slot_data->val[HMT_CONFIDENCE] == 0)) { 588b9b347e9SVladimir Kondratyev /* This finger is in proximity of the sensor */ 589b9b347e9SVladimir Kondratyev sc->touch = true; 590*9d8ebe5eSVladimir Kondratyev slot_data->dist = !slot_data->val[HMT_IN_RANGE]; 591b9b347e9SVladimir Kondratyev /* Divided by two to match visual scale of touch */ 592*9d8ebe5eSVladimir Kondratyev width = slot_data->val[HMT_WIDTH] >> 1; 593*9d8ebe5eSVladimir Kondratyev height = slot_data->val[HMT_HEIGHT] >> 1; 594*9d8ebe5eSVladimir Kondratyev slot_data->ori = width > height; 595*9d8ebe5eSVladimir Kondratyev slot_data->maj = MAX(width, height); 596*9d8ebe5eSVladimir Kondratyev slot_data->min = MIN(width, height); 597*9d8ebe5eSVladimir Kondratyev } else 598*9d8ebe5eSVladimir Kondratyev slot_data = NULL; 599b9b347e9SVladimir Kondratyev 600*9d8ebe5eSVladimir Kondratyev evdev_mt_push_slot(sc->evdev, slot, slot_data); 601b9b347e9SVladimir Kondratyev } 602b9b347e9SVladimir Kondratyev 603b9b347e9SVladimir Kondratyev sc->nconts_todo -= cont_count; 604b9b347e9SVladimir Kondratyev if (sc->do_timestamps && sc->nconts_todo == 0) { 605b9b347e9SVladimir Kondratyev /* HUD_SCAN_TIME is measured in 100us, convert to us. */ 606b9b347e9SVladimir Kondratyev scan_time = hid_get_udata(buf, len, &sc->scan_time_loc); 607b9b347e9SVladimir Kondratyev if (sc->prev_touch) { 608b9b347e9SVladimir Kondratyev delta = scan_time - sc->scan_time; 609b9b347e9SVladimir Kondratyev if (delta < 0) 610b9b347e9SVladimir Kondratyev delta += sc->scan_time_max; 611b9b347e9SVladimir Kondratyev } else 612b9b347e9SVladimir Kondratyev delta = 0; 613b9b347e9SVladimir Kondratyev sc->scan_time = scan_time; 614b9b347e9SVladimir Kondratyev sc->timestamp += delta * 100; 615b9b347e9SVladimir Kondratyev evdev_push_msc(sc->evdev, MSC_TIMESTAMP, sc->timestamp); 616b9b347e9SVladimir Kondratyev sc->prev_touch = sc->touch; 617b9b347e9SVladimir Kondratyev sc->touch = false; 618b9b347e9SVladimir Kondratyev if (!sc->prev_touch) 619b9b347e9SVladimir Kondratyev sc->timestamp = 0; 620b9b347e9SVladimir Kondratyev } 621b9b347e9SVladimir Kondratyev if (sc->nconts_todo == 0) { 622b9b347e9SVladimir Kondratyev /* Report both the click and external left btns as BTN_LEFT */ 623b9b347e9SVladimir Kondratyev if (sc->has_int_button) 624b9b347e9SVladimir Kondratyev int_btn = hid_get_data(buf, len, &sc->int_btn_loc); 625b9b347e9SVladimir Kondratyev if (isset(sc->buttons, 0)) 626b9b347e9SVladimir Kondratyev left_btn = hid_get_data(buf, len, &sc->btn_loc[0]); 627b9b347e9SVladimir Kondratyev if (sc->has_int_button || isset(sc->buttons, 0)) 628b9b347e9SVladimir Kondratyev evdev_push_key(sc->evdev, BTN_LEFT, 629b9b347e9SVladimir Kondratyev (int_btn != 0) | (left_btn != 0)); 630b9b347e9SVladimir Kondratyev for (btn = 1; btn < sc->max_button; ++btn) { 631b9b347e9SVladimir Kondratyev if (isset(sc->buttons, btn)) 632b9b347e9SVladimir Kondratyev evdev_push_key(sc->evdev, BTN_MOUSE + btn, 633b9b347e9SVladimir Kondratyev hid_get_data(buf, 634b9b347e9SVladimir Kondratyev len, 635b9b347e9SVladimir Kondratyev &sc->btn_loc[btn]) != 0); 636b9b347e9SVladimir Kondratyev } 637b9b347e9SVladimir Kondratyev evdev_sync(sc->evdev); 638b9b347e9SVladimir Kondratyev } 639b9b347e9SVladimir Kondratyev } 640b9b347e9SVladimir Kondratyev 641cb022db8SVladimir Kondratyev static enum hmt_type 642cb022db8SVladimir Kondratyev hmt_hid_parse(struct hmt_softc *sc, const void *d_ptr, hid_size_t d_len, 643cb022db8SVladimir Kondratyev uint32_t tlc_usage, uint8_t tlc_index) 644b9b347e9SVladimir Kondratyev { 645cb022db8SVladimir Kondratyev struct hid_absinfo ai; 646b9b347e9SVladimir Kondratyev struct hid_item hi; 647b9b347e9SVladimir Kondratyev struct hid_data *hd; 648cb022db8SVladimir Kondratyev uint32_t flags; 649b9b347e9SVladimir Kondratyev size_t i; 650b9b347e9SVladimir Kondratyev size_t cont = 0; 651cb022db8SVladimir Kondratyev enum hmt_type type; 652b9b347e9SVladimir Kondratyev uint32_t left_btn, btn; 653b9b347e9SVladimir Kondratyev int32_t cont_count_max = 0; 654b9b347e9SVladimir Kondratyev uint8_t report_id = 0; 655b9b347e9SVladimir Kondratyev bool finger_coll = false; 656b9b347e9SVladimir Kondratyev bool cont_count_found = false; 657b9b347e9SVladimir Kondratyev bool scan_time_found = false; 658b9b347e9SVladimir Kondratyev bool has_int_button = false; 659b9b347e9SVladimir Kondratyev 660cb910670SJack #define HMT_HI_ABSOLUTE(hi) ((hi).nusages != 0 && \ 661cb910670SJack ((hi).flags & (HIO_VARIABLE | HIO_RELATIVE)) == HIO_VARIABLE) 662b9b347e9SVladimir Kondratyev #define HUMS_THQA_CERT 0xC5 663b9b347e9SVladimir Kondratyev 664cb022db8SVladimir Kondratyev /* 665cb022db8SVladimir Kondratyev * Get left button usage taking in account MS Precision Touchpad specs. 666cb022db8SVladimir Kondratyev * For Windows PTP report descriptor assigns buttons in following way: 667cb022db8SVladimir Kondratyev * Button 1 - Indicates Button State for touchpad button integrated 668cb022db8SVladimir Kondratyev * with digitizer. 669cb022db8SVladimir Kondratyev * Button 2 - Indicates Button State for external button for primary 670cb022db8SVladimir Kondratyev * (default left) clicking. 671cb022db8SVladimir Kondratyev * Button 3 - Indicates Button State for external button for secondary 672cb022db8SVladimir Kondratyev * (default right) clicking. 673cb022db8SVladimir Kondratyev * If a device only supports external buttons, it must still use 674cb022db8SVladimir Kondratyev * Button 2 and Button 3 to reference the external buttons. 675cb022db8SVladimir Kondratyev */ 676cb022db8SVladimir Kondratyev switch (tlc_usage) { 677cb022db8SVladimir Kondratyev case HID_USAGE2(HUP_DIGITIZERS, HUD_TOUCHSCREEN): 678cb022db8SVladimir Kondratyev type = HMT_TYPE_TOUCHSCREEN; 679b9b347e9SVladimir Kondratyev left_btn = 1; 680b9b347e9SVladimir Kondratyev break; 681cb022db8SVladimir Kondratyev case HID_USAGE2(HUP_DIGITIZERS, HUD_TOUCHPAD): 682cb022db8SVladimir Kondratyev type = HMT_TYPE_TOUCHPAD; 683b9b347e9SVladimir Kondratyev left_btn = 2; 684b9b347e9SVladimir Kondratyev break; 685b9b347e9SVladimir Kondratyev default: 686cb022db8SVladimir Kondratyev return (HMT_TYPE_UNSUPPORTED); 687b9b347e9SVladimir Kondratyev } 688b9b347e9SVladimir Kondratyev 689cb022db8SVladimir Kondratyev /* Parse features for mandatory maximum contact count usage */ 690cb022db8SVladimir Kondratyev if (!hidbus_locate(d_ptr, d_len, 691cb022db8SVladimir Kondratyev HID_USAGE2(HUP_DIGITIZERS, HUD_CONTACT_MAX), hid_feature, 692cb022db8SVladimir Kondratyev tlc_index, 0, &sc->cont_max_loc, &flags, &sc->cont_max_rid, &ai) || 693cb022db8SVladimir Kondratyev (flags & (HIO_VARIABLE | HIO_RELATIVE)) != HIO_VARIABLE) 694cb022db8SVladimir Kondratyev return (HMT_TYPE_UNSUPPORTED); 695b9b347e9SVladimir Kondratyev 696cb022db8SVladimir Kondratyev cont_count_max = ai.max; 697cb022db8SVladimir Kondratyev 698cb022db8SVladimir Kondratyev /* Parse features for button type usage */ 699cb022db8SVladimir Kondratyev if (hidbus_locate(d_ptr, d_len, 700cb022db8SVladimir Kondratyev HID_USAGE2(HUP_DIGITIZERS, HUD_BUTTON_TYPE), hid_feature, 701cb022db8SVladimir Kondratyev tlc_index, 0, &sc->btn_type_loc, &flags, &sc->btn_type_rid, NULL) 702cb022db8SVladimir Kondratyev && (flags & (HIO_VARIABLE | HIO_RELATIVE)) != HIO_VARIABLE) 703cb022db8SVladimir Kondratyev sc->btn_type_rid = 0; 704cb022db8SVladimir Kondratyev 705cb022db8SVladimir Kondratyev /* Parse features for THQA certificate report ID */ 706cb022db8SVladimir Kondratyev hidbus_locate(d_ptr, d_len, HID_USAGE2(HUP_MICROSOFT, HUMS_THQA_CERT), 707cb022db8SVladimir Kondratyev hid_feature, tlc_index, 0, NULL, NULL, &sc->thqa_cert_rid, NULL); 708b9b347e9SVladimir Kondratyev 709b9b347e9SVladimir Kondratyev /* Parse input for other parameters */ 710b9b347e9SVladimir Kondratyev hd = hid_start_parse(d_ptr, d_len, 1 << hid_input); 711cb022db8SVladimir Kondratyev HIDBUS_FOREACH_ITEM(hd, &hi, tlc_index) { 712b9b347e9SVladimir Kondratyev switch (hi.kind) { 713b9b347e9SVladimir Kondratyev case hid_collection: 714cb022db8SVladimir Kondratyev if (hi.collevel == 2 && 715b9b347e9SVladimir Kondratyev hi.usage == HID_USAGE2(HUP_DIGITIZERS, HUD_FINGER)) 716b9b347e9SVladimir Kondratyev finger_coll = true; 717b9b347e9SVladimir Kondratyev break; 718b9b347e9SVladimir Kondratyev case hid_endcollection: 719b9b347e9SVladimir Kondratyev if (hi.collevel == 1 && finger_coll) { 720b9b347e9SVladimir Kondratyev finger_coll = false; 721b9b347e9SVladimir Kondratyev cont++; 722cb022db8SVladimir Kondratyev } 723b9b347e9SVladimir Kondratyev break; 724b9b347e9SVladimir Kondratyev case hid_input: 725b9b347e9SVladimir Kondratyev /* 726cb022db8SVladimir Kondratyev * Ensure that all usages belong to the same report 727b9b347e9SVladimir Kondratyev */ 728cb022db8SVladimir Kondratyev if (HMT_HI_ABSOLUTE(hi) && 729b9b347e9SVladimir Kondratyev (report_id == 0 || report_id == hi.report_ID)) 730b9b347e9SVladimir Kondratyev report_id = hi.report_ID; 731b9b347e9SVladimir Kondratyev else 732b9b347e9SVladimir Kondratyev break; 733b9b347e9SVladimir Kondratyev 734b9b347e9SVladimir Kondratyev if (hi.collevel == 1 && left_btn == 2 && 735b9b347e9SVladimir Kondratyev hi.usage == HID_USAGE2(HUP_BUTTON, 1)) { 736b9b347e9SVladimir Kondratyev has_int_button = true; 737b9b347e9SVladimir Kondratyev sc->int_btn_loc = hi.loc; 738b9b347e9SVladimir Kondratyev break; 739b9b347e9SVladimir Kondratyev } 740b9b347e9SVladimir Kondratyev if (hi.collevel == 1 && 741b9b347e9SVladimir Kondratyev hi.usage >= HID_USAGE2(HUP_BUTTON, left_btn) && 742cb022db8SVladimir Kondratyev hi.usage <= HID_USAGE2(HUP_BUTTON, HMT_BTN_MAX)) { 743b9b347e9SVladimir Kondratyev btn = (hi.usage & 0xFFFF) - left_btn; 744b9b347e9SVladimir Kondratyev setbit(sc->buttons, btn); 745b9b347e9SVladimir Kondratyev sc->btn_loc[btn] = hi.loc; 746b9b347e9SVladimir Kondratyev if (btn >= sc->max_button) 747b9b347e9SVladimir Kondratyev sc->max_button = btn + 1; 748b9b347e9SVladimir Kondratyev break; 749b9b347e9SVladimir Kondratyev } 750b9b347e9SVladimir Kondratyev if (hi.collevel == 1 && hi.usage == 751b9b347e9SVladimir Kondratyev HID_USAGE2(HUP_DIGITIZERS, HUD_CONTACTCOUNT)) { 752b9b347e9SVladimir Kondratyev cont_count_found = true; 753b9b347e9SVladimir Kondratyev sc->cont_count_loc = hi.loc; 754b9b347e9SVladimir Kondratyev break; 755b9b347e9SVladimir Kondratyev } 756b9b347e9SVladimir Kondratyev /* Scan time is required but clobbered by evdev */ 757b9b347e9SVladimir Kondratyev if (hi.collevel == 1 && hi.usage == 758b9b347e9SVladimir Kondratyev HID_USAGE2(HUP_DIGITIZERS, HUD_SCAN_TIME)) { 759b9b347e9SVladimir Kondratyev scan_time_found = true; 760b9b347e9SVladimir Kondratyev sc->scan_time_loc = hi.loc; 761b9b347e9SVladimir Kondratyev sc->scan_time_max = hi.logical_maximum; 762b9b347e9SVladimir Kondratyev break; 763b9b347e9SVladimir Kondratyev } 764b9b347e9SVladimir Kondratyev 765b9b347e9SVladimir Kondratyev if (!finger_coll || hi.collevel != 2) 766b9b347e9SVladimir Kondratyev break; 767b9b347e9SVladimir Kondratyev if (cont >= MAX_MT_SLOTS) { 768b9b347e9SVladimir Kondratyev DPRINTF("Finger %zu ignored\n", cont); 769b9b347e9SVladimir Kondratyev break; 770b9b347e9SVladimir Kondratyev } 771b9b347e9SVladimir Kondratyev 772cb022db8SVladimir Kondratyev for (i = 0; i < HMT_N_USAGES; i++) { 773cb022db8SVladimir Kondratyev if (hi.usage == hmt_hid_map[i].usage) { 774b9b347e9SVladimir Kondratyev /* 775b9b347e9SVladimir Kondratyev * HUG_X usage is an array mapped to 776b9b347e9SVladimir Kondratyev * both ABS_MT_POSITION and ABS_MT_TOOL 777b9b347e9SVladimir Kondratyev * events. So don`t stop search if we 778b9b347e9SVladimir Kondratyev * already have HUG_X mapping done. 779b9b347e9SVladimir Kondratyev */ 780b9b347e9SVladimir Kondratyev if (sc->locs[cont][i].size) 781b9b347e9SVladimir Kondratyev continue; 782b9b347e9SVladimir Kondratyev sc->locs[cont][i] = hi.loc; 783b9b347e9SVladimir Kondratyev /* 784b9b347e9SVladimir Kondratyev * Hid parser returns valid logical and 785b9b347e9SVladimir Kondratyev * physical sizes for first finger only 786b9b347e9SVladimir Kondratyev * at least on ElanTS 0x04f3:0x0012. 787b9b347e9SVladimir Kondratyev */ 788b9b347e9SVladimir Kondratyev if (cont > 0) 789b9b347e9SVladimir Kondratyev break; 790b9b347e9SVladimir Kondratyev setbit(sc->caps, i); 791cb022db8SVladimir Kondratyev sc->ai[i] = (struct hid_absinfo) { 792b9b347e9SVladimir Kondratyev .max = hi.logical_maximum, 793b9b347e9SVladimir Kondratyev .min = hi.logical_minimum, 794b9b347e9SVladimir Kondratyev .res = hid_item_resolution(&hi), 795b9b347e9SVladimir Kondratyev }; 796b9b347e9SVladimir Kondratyev break; 797b9b347e9SVladimir Kondratyev } 798b9b347e9SVladimir Kondratyev } 799b9b347e9SVladimir Kondratyev break; 800b9b347e9SVladimir Kondratyev default: 801b9b347e9SVladimir Kondratyev break; 802b9b347e9SVladimir Kondratyev } 803b9b347e9SVladimir Kondratyev } 804b9b347e9SVladimir Kondratyev hid_end_parse(hd); 805b9b347e9SVladimir Kondratyev 806b9b347e9SVladimir Kondratyev /* Check for required HID Usages */ 807b9b347e9SVladimir Kondratyev if (!cont_count_found || !scan_time_found || cont == 0) 808cb022db8SVladimir Kondratyev return (HMT_TYPE_UNSUPPORTED); 809cb022db8SVladimir Kondratyev for (i = 0; i < HMT_N_USAGES; i++) { 810cb022db8SVladimir Kondratyev if (hmt_hid_map[i].required && isclr(sc->caps, i)) 811cb022db8SVladimir Kondratyev return (HMT_TYPE_UNSUPPORTED); 812b9b347e9SVladimir Kondratyev } 813b9b347e9SVladimir Kondratyev 814b9b347e9SVladimir Kondratyev /* Touchpads must have at least one button */ 815cb022db8SVladimir Kondratyev if (type == HMT_TYPE_TOUCHPAD && !sc->max_button && !has_int_button) 816cb022db8SVladimir Kondratyev return (HMT_TYPE_UNSUPPORTED); 817b9b347e9SVladimir Kondratyev 818b9b347e9SVladimir Kondratyev /* 819b9b347e9SVladimir Kondratyev * According to specifications 'Contact Count Maximum' should be read 820b9b347e9SVladimir Kondratyev * from Feature Report rather than from HID descriptor. Set sane 821b9b347e9SVladimir Kondratyev * default value now to handle the case of 'Get Report' request failure 822b9b347e9SVladimir Kondratyev */ 823b9b347e9SVladimir Kondratyev if (cont_count_max < 1) 824b9b347e9SVladimir Kondratyev cont_count_max = cont; 825b9b347e9SVladimir Kondratyev 826b9b347e9SVladimir Kondratyev /* Report touch orientation if both width and height are supported */ 827cb022db8SVladimir Kondratyev if (isset(sc->caps, HMT_WIDTH) && isset(sc->caps, HMT_HEIGHT)) { 828cb022db8SVladimir Kondratyev setbit(sc->caps, HMT_ORIENTATION); 829cb022db8SVladimir Kondratyev sc->ai[HMT_ORIENTATION].max = 1; 830b9b347e9SVladimir Kondratyev } 831b9b347e9SVladimir Kondratyev 832b9b347e9SVladimir Kondratyev sc->cont_max_rlen = hid_report_size(d_ptr, d_len, hid_feature, 833b9b347e9SVladimir Kondratyev sc->cont_max_rid); 834b9b347e9SVladimir Kondratyev if (sc->btn_type_rid > 0) 835b9b347e9SVladimir Kondratyev sc->btn_type_rlen = hid_report_size(d_ptr, d_len, 836b9b347e9SVladimir Kondratyev hid_feature, sc->btn_type_rid); 837b9b347e9SVladimir Kondratyev if (sc->thqa_cert_rid > 0) 838b9b347e9SVladimir Kondratyev sc->thqa_cert_rlen = hid_report_size(d_ptr, d_len, 839b9b347e9SVladimir Kondratyev hid_feature, sc->thqa_cert_rid); 840b9b347e9SVladimir Kondratyev 841b9b347e9SVladimir Kondratyev sc->report_id = report_id; 84295add157SVladimir Kondratyev sc->cont_count_max = cont_count_max; 843b9b347e9SVladimir Kondratyev sc->nconts_per_report = cont; 844b9b347e9SVladimir Kondratyev sc->has_int_button = has_int_button; 845b9b347e9SVladimir Kondratyev 846b9b347e9SVladimir Kondratyev return (type); 847b9b347e9SVladimir Kondratyev } 848b9b347e9SVladimir Kondratyev 849b9b347e9SVladimir Kondratyev static int 850cb022db8SVladimir Kondratyev hmt_set_input_mode(struct hmt_softc *sc, enum hconf_input_mode mode) 851b9b347e9SVladimir Kondratyev { 852cb022db8SVladimir Kondratyev devclass_t hconf_devclass; 853cb022db8SVladimir Kondratyev device_t hconf; 854b9b347e9SVladimir Kondratyev int err; 855b9b347e9SVladimir Kondratyev 856cb022db8SVladimir Kondratyev GIANT_REQUIRED; 857b9b347e9SVladimir Kondratyev 858cb022db8SVladimir Kondratyev /* Find touchpad's configuration TLC */ 859cb022db8SVladimir Kondratyev hconf = hidbus_find_child(device_get_parent(sc->dev), 860cb022db8SVladimir Kondratyev HID_USAGE2(HUP_DIGITIZERS, HUD_CONFIG)); 861cb022db8SVladimir Kondratyev if (hconf == NULL) 862cb022db8SVladimir Kondratyev return (ENXIO); 863b9b347e9SVladimir Kondratyev 864cb022db8SVladimir Kondratyev /* Ensure that hconf driver is attached to configuration TLC */ 865cb022db8SVladimir Kondratyev if (device_is_alive(hconf) == 0) 866cb022db8SVladimir Kondratyev device_probe_and_attach(hconf); 867cb022db8SVladimir Kondratyev if (device_is_attached(hconf) == 0) 868cb022db8SVladimir Kondratyev return (ENXIO); 869cb022db8SVladimir Kondratyev hconf_devclass = devclass_find("hconf"); 870cb022db8SVladimir Kondratyev if (device_get_devclass(hconf) != hconf_devclass) 871cb022db8SVladimir Kondratyev return (ENXIO); 872cb022db8SVladimir Kondratyev 873cb022db8SVladimir Kondratyev /* hconf_set_input_mode can drop the Giant while sleeping */ 874cb022db8SVladimir Kondratyev device_busy(hconf); 875cb022db8SVladimir Kondratyev err = hconf_set_input_mode(hconf, mode); 876cb022db8SVladimir Kondratyev device_unbusy(hconf); 877b9b347e9SVladimir Kondratyev 878b9b347e9SVladimir Kondratyev return (err); 879b9b347e9SVladimir Kondratyev } 880b9b347e9SVladimir Kondratyev 881cb022db8SVladimir Kondratyev static devclass_t hmt_devclass; 882b9b347e9SVladimir Kondratyev 883cb022db8SVladimir Kondratyev static device_method_t hmt_methods[] = { 884cb022db8SVladimir Kondratyev DEVMETHOD(device_probe, hmt_probe), 885cb022db8SVladimir Kondratyev DEVMETHOD(device_attach, hmt_attach), 886cb022db8SVladimir Kondratyev DEVMETHOD(device_detach, hmt_detach), 887b9b347e9SVladimir Kondratyev 888b9b347e9SVladimir Kondratyev DEVMETHOD_END 889b9b347e9SVladimir Kondratyev }; 890b9b347e9SVladimir Kondratyev 891cb022db8SVladimir Kondratyev static driver_t hmt_driver = { 892cb022db8SVladimir Kondratyev .name = "hmt", 893cb022db8SVladimir Kondratyev .methods = hmt_methods, 894cb022db8SVladimir Kondratyev .size = sizeof(struct hmt_softc), 895b9b347e9SVladimir Kondratyev }; 896b9b347e9SVladimir Kondratyev 897cb022db8SVladimir Kondratyev DRIVER_MODULE(hmt, hidbus, hmt_driver, hmt_devclass, NULL, 0); 898cb022db8SVladimir Kondratyev MODULE_DEPEND(hmt, hidbus, 1, 1, 1); 899cb022db8SVladimir Kondratyev MODULE_DEPEND(hmt, hid, 1, 1, 1); 900cb022db8SVladimir Kondratyev MODULE_DEPEND(hmt, hconf, 1, 1, 1); 901cb022db8SVladimir Kondratyev MODULE_DEPEND(hmt, evdev, 1, 1, 1); 902cb022db8SVladimir Kondratyev MODULE_VERSION(hmt, 1); 903cb022db8SVladimir Kondratyev HID_PNP_INFO(hmt_devs); 904