File Index
Files and directories
Sign in
FILE

hid-multitouch.c

File preview with syntax highlighting.

linux-6.19.14/drivers/hid/hid-multitouch.c C
   1// SPDX-License-Identifier: GPL-2.0-or-later
   2/*
   3 *  HID driver for multitouch panels
   4 *
   5 *  Copyright (c) 2010-2012 Stephane Chatty <chatty@enac.fr>
   6 *  Copyright (c) 2010-2013 Benjamin Tissoires <benjamin.tissoires@gmail.com>
   7 *  Copyright (c) 2010-2012 Ecole Nationale de l'Aviation Civile, France
   8 *  Copyright (c) 2012-2013 Red Hat, Inc
   9 *
  10 *  This code is partly based on hid-egalax.c:
  11 *
  12 *  Copyright (c) 2010 Stephane Chatty <chatty@enac.fr>
  13 *  Copyright (c) 2010 Henrik Rydberg <rydberg@euromail.se>
  14 *  Copyright (c) 2010 Canonical, Ltd.
  15 *
  16 *  This code is partly based on hid-3m-pct.c:
  17 *
  18 *  Copyright (c) 2009-2010 Stephane Chatty <chatty@enac.fr>
  19 *  Copyright (c) 2010      Henrik Rydberg <rydberg@euromail.se>
  20 *  Copyright (c) 2010      Canonical, Ltd.
  21 */
  22
  23/*
  24 */
  25
  26/*
  27 * This driver is regularly tested thanks to the test suite in hid-tools[1].
  28 * Please run these regression tests before patching this module so that
  29 * your patch won't break existing known devices.
  30 *
  31 * [1] https://gitlab.freedesktop.org/libevdev/hid-tools
  32 */
  33
  34#include <linux/bits.h>
  35#include <linux/device.h>
  36#include <linux/hid.h>
  37#include <linux/module.h>
  38#include <linux/slab.h>
  39#include <linux/input/mt.h>
  40#include <linux/jiffies.h>
  41#include <linux/string.h>
  42#include <linux/timer.h>
  43
  44
  45MODULE_AUTHOR("Stephane Chatty <chatty@enac.fr>");
  46MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
  47MODULE_DESCRIPTION("HID multitouch panels");
  48MODULE_LICENSE("GPL");
  49
  50#include "hid-ids.h"
  51
  52#include "hid-haptic.h"
  53
  54/* quirks to control the device */
  55#define MT_QUIRK_NOT_SEEN_MEANS_UP	BIT(0)
  56#define MT_QUIRK_SLOT_IS_CONTACTID	BIT(1)
  57#define MT_QUIRK_CYPRESS		BIT(2)
  58#define MT_QUIRK_SLOT_IS_CONTACTNUMBER	BIT(3)
  59#define MT_QUIRK_ALWAYS_VALID		BIT(4)
  60#define MT_QUIRK_VALID_IS_INRANGE	BIT(5)
  61#define MT_QUIRK_VALID_IS_CONFIDENCE	BIT(6)
  62#define MT_QUIRK_CONFIDENCE		BIT(7)
  63#define MT_QUIRK_SLOT_IS_CONTACTID_MINUS_ONE	BIT(8)
  64#define MT_QUIRK_NO_AREA		BIT(9)
  65#define MT_QUIRK_IGNORE_DUPLICATES	BIT(10)
  66#define MT_QUIRK_HOVERING		BIT(11)
  67#define MT_QUIRK_CONTACT_CNT_ACCURATE	BIT(12)
  68#define MT_QUIRK_FORCE_GET_FEATURE	BIT(13)
  69#define MT_QUIRK_FIX_CONST_CONTACT_ID	BIT(14)
  70#define MT_QUIRK_TOUCH_SIZE_SCALING	BIT(15)
  71#define MT_QUIRK_STICKY_FINGERS		BIT(16)
  72#define MT_QUIRK_ASUS_CUSTOM_UP		BIT(17)
  73#define MT_QUIRK_WIN8_PTP_BUTTONS	BIT(18)
  74#define MT_QUIRK_SEPARATE_APP_REPORT	BIT(19)
  75#define MT_QUIRK_FORCE_MULTI_INPUT	BIT(20)
  76#define MT_QUIRK_DISABLE_WAKEUP		BIT(21)
  77#define MT_QUIRK_ORIENTATION_INVERT	BIT(22)
  78#define MT_QUIRK_APPLE_TOUCHBAR		BIT(23)
  79#define MT_QUIRK_YOGABOOK9I		BIT(24)
  80#define MT_QUIRK_KEEP_LATENCY_ON_CLOSE	BIT(25)
  81
  82#define MT_INPUTMODE_TOUCHSCREEN	0x02
  83#define MT_INPUTMODE_TOUCHPAD		0x03
  84
  85#define MT_BUTTONTYPE_CLICKPAD		0
  86#define MT_BUTTONTYPE_PRESSUREPAD	1
  87
  88enum latency_mode {
  89	HID_LATENCY_NORMAL = 0,
  90	HID_LATENCY_HIGH = 1,
  91};
  92
  93enum report_mode {
  94	TOUCHPAD_REPORT_NONE = 0,
  95	TOUCHPAD_REPORT_BUTTONS = BIT(0),
  96	TOUCHPAD_REPORT_CONTACTS = BIT(1),
  97	TOUCHPAD_REPORT_ALL = TOUCHPAD_REPORT_BUTTONS | TOUCHPAD_REPORT_CONTACTS,
  98};
  99
 100#define MT_IO_SLOTS_MASK		GENMASK(7, 0) /* reserve first 8 bits for slot tracking */
 101#define MT_IO_FLAGS_RUNNING		32
 102
 103static const bool mtrue = true;		/* default for true */
 104static const bool mfalse;		/* default for false */
 105static const __s32 mzero;		/* default for 0 */
 106
 107#define DEFAULT_TRUE	((void *)&mtrue)
 108#define DEFAULT_FALSE	((void *)&mfalse)
 109#define DEFAULT_ZERO	((void *)&mzero)
 110
 111struct mt_usages {
 112	struct list_head list;
 113	__s32 *x, *y, *cx, *cy, *p, *w, *h, *a;
 114	__s32 *contactid;	/* the device ContactID assigned to this slot */
 115	bool *tip_state;	/* is the touch valid? */
 116	bool *inrange_state;	/* is the finger in proximity of the sensor? */
 117	bool *confidence_state;	/* is the touch made by a finger? */
 118};
 119
 120struct mt_application {
 121	struct list_head list;
 122	unsigned int application;
 123	unsigned int report_id;
 124	struct list_head mt_usages;	/* mt usages list */
 125
 126	__s32 quirks;
 127
 128	__s32 *scantime;		/* scantime reported */
 129	__s32 scantime_logical_max;	/* max value for raw scantime */
 130
 131	__s32 *raw_cc;			/* contact count in the report */
 132	int left_button_state;		/* left button state */
 133	unsigned int mt_flags;		/* flags to pass to input-mt */
 134
 135	unsigned long *pending_palm_slots;	/* slots where we reported palm
 136						 * and need to release */
 137
 138	__u8 num_received;	/* how many contacts we received */
 139	__u8 num_expected;	/* expected last contact index */
 140	__u8 buttons_count;	/* number of physical buttons per touchpad */
 141	__u8 touches_by_report;	/* how many touches are present in one report:
 142				 * 1 means we should use a serial protocol
 143				 * > 1 means hybrid (multitouch) protocol
 144				 */
 145
 146	unsigned long jiffies;	/* the frame's jiffies */
 147	int timestamp;		/* the timestamp to be sent */
 148	int prev_scantime;		/* scantime reported previously */
 149
 150	bool have_contact_count;
 151};
 152
 153struct mt_class {
 154	__s32 name;	/* MT_CLS */
 155	__s32 quirks;
 156	__s32 sn_move;	/* Signal/noise ratio for move events */
 157	__s32 sn_width;	/* Signal/noise ratio for width events */
 158	__s32 sn_height;	/* Signal/noise ratio for height events */
 159	__s32 sn_pressure;	/* Signal/noise ratio for pressure events */
 160	__u8 maxcontacts;
 161	bool is_indirect;	/* true for touchpads */
 162	bool export_all_inputs;	/* do not ignore mouse, keyboards, etc... */
 163};
 164
 165struct mt_report_data {
 166	struct list_head list;
 167	struct hid_report *report;
 168	struct mt_application *application;
 169	bool is_mt_collection;
 170};
 171
 172struct mt_device {
 173	struct mt_class mtclass;	/* our mt device class */
 174	struct timer_list release_timer;	/* to release sticky fingers */
 175	struct hid_haptic_device *haptic;	/* haptic related configuration */
 176	struct hid_device *hdev;	/* hid_device we're attached to */
 177	unsigned long mt_io_flags;	/* mt flags (MT_IO_FLAGS_RUNNING)
 178					 * first 8 bits are reserved for keeping the slot
 179					 * states, this is fine because we only support up
 180					 * to 250 slots (MT_MAX_MAXCONTACT)
 181					 */
 182	__u8 inputmode_value;	/* InputMode HID feature value */
 183	__u8 maxcontacts;
 184	bool is_buttonpad;	/* is this device a button pad? */
 185	bool is_pressurepad;	/* is this device a pressurepad? */
 186	bool is_haptic_touchpad;	/* is this device a haptic touchpad? */
 187	bool serial_maybe;	/* need to check for serial protocol */
 188
 189	struct list_head applications;
 190	struct list_head reports;
 191};
 192
 193static void mt_post_parse_default_settings(struct mt_device *td,
 194					   struct mt_application *app);
 195static void mt_post_parse(struct mt_device *td, struct mt_application *app);
 196
 197/* classes of device behavior */
 198#define MT_CLS_DEFAULT				0x0001
 199
 200#define MT_CLS_SERIAL				0x0002
 201#define MT_CLS_CONFIDENCE			0x0003
 202#define MT_CLS_CONFIDENCE_CONTACT_ID		0x0004
 203#define MT_CLS_CONFIDENCE_MINUS_ONE		0x0005
 204#define MT_CLS_DUAL_INRANGE_CONTACTID		0x0006
 205#define MT_CLS_DUAL_INRANGE_CONTACTNUMBER	0x0007
 206/* reserved					0x0008 */
 207#define MT_CLS_INRANGE_CONTACTNUMBER		0x0009
 208#define MT_CLS_NSMU				0x000a
 209/* reserved					0x0010 */
 210/* reserved					0x0011 */
 211#define MT_CLS_WIN_8				0x0012
 212#define MT_CLS_EXPORT_ALL_INPUTS		0x0013
 213/* reserved					0x0014 */
 214#define MT_CLS_WIN_8_FORCE_MULTI_INPUT		0x0015
 215#define MT_CLS_WIN_8_DISABLE_WAKEUP		0x0016
 216#define MT_CLS_WIN_8_NO_STICKY_FINGERS		0x0017
 217#define MT_CLS_WIN_8_FORCE_MULTI_INPUT_NSMU	0x0018
 218#define MT_CLS_WIN_8_KEEP_LATENCY_ON_CLOSE	0x0019
 219
 220/* vendor specific classes */
 221#define MT_CLS_3M				0x0101
 222/* reserved					0x0102 */
 223#define MT_CLS_EGALAX				0x0103
 224#define MT_CLS_EGALAX_SERIAL			0x0104
 225#define MT_CLS_TOPSEED				0x0105
 226#define MT_CLS_PANASONIC			0x0106
 227#define MT_CLS_FLATFROG				0x0107
 228#define MT_CLS_GENERALTOUCH_TWOFINGERS		0x0108
 229#define MT_CLS_GENERALTOUCH_PWT_TENFINGERS	0x0109
 230#define MT_CLS_LG				0x010a
 231#define MT_CLS_ASUS				0x010b
 232#define MT_CLS_VTL				0x0110
 233#define MT_CLS_GOOGLE				0x0111
 234#define MT_CLS_RAZER_BLADE_STEALTH		0x0112
 235#define MT_CLS_SMART_TECH			0x0113
 236#define MT_CLS_APPLE_TOUCHBAR			0x0114
 237#define MT_CLS_YOGABOOK9I			0x0115
 238#define MT_CLS_EGALAX_P80H84			0x0116
 239#define MT_CLS_SIS				0x0457
 240
 241#define MT_DEFAULT_MAXCONTACT	10
 242#define MT_MAX_MAXCONTACT	250
 243
 244/*
 245 * Resync device and local timestamps after that many microseconds without
 246 * receiving data.
 247 */
 248#define MAX_TIMESTAMP_INTERVAL	1000000
 249
 250#define MT_USB_DEVICE(v, p)	HID_DEVICE(BUS_USB, HID_GROUP_MULTITOUCH, v, p)
 251#define MT_BT_DEVICE(v, p)	HID_DEVICE(BUS_BLUETOOTH, HID_GROUP_MULTITOUCH, v, p)
 252
 253/*
 254 * these device-dependent functions determine what slot corresponds
 255 * to a valid contact that was just read.
 256 */
 257
 258static int cypress_compute_slot(struct mt_application *application,
 259				struct mt_usages *slot)
 260{
 261	if (*slot->contactid != 0 || application->num_received == 0)
 262		return *slot->contactid;
 263	else
 264		return -1;
 265}
 266
 267static const struct mt_class mt_classes[] = {
 268	{ .name = MT_CLS_DEFAULT,
 269		.quirks = MT_QUIRK_ALWAYS_VALID |
 270			MT_QUIRK_CONTACT_CNT_ACCURATE },
 271	{ .name = MT_CLS_NSMU,
 272		.quirks = MT_QUIRK_NOT_SEEN_MEANS_UP },
 273	{ .name = MT_CLS_SERIAL,
 274		.quirks = MT_QUIRK_ALWAYS_VALID},
 275	{ .name = MT_CLS_CONFIDENCE,
 276		.quirks = MT_QUIRK_VALID_IS_CONFIDENCE },
 277	{ .name = MT_CLS_CONFIDENCE_CONTACT_ID,
 278		.quirks = MT_QUIRK_VALID_IS_CONFIDENCE |
 279			MT_QUIRK_SLOT_IS_CONTACTID },
 280	{ .name = MT_CLS_CONFIDENCE_MINUS_ONE,
 281		.quirks = MT_QUIRK_VALID_IS_CONFIDENCE |
 282			MT_QUIRK_SLOT_IS_CONTACTID_MINUS_ONE },
 283	{ .name = MT_CLS_DUAL_INRANGE_CONTACTID,
 284		.quirks = MT_QUIRK_VALID_IS_INRANGE |
 285			MT_QUIRK_SLOT_IS_CONTACTID,
 286		.maxcontacts = 2 },
 287	{ .name = MT_CLS_DUAL_INRANGE_CONTACTNUMBER,
 288		.quirks = MT_QUIRK_VALID_IS_INRANGE |
 289			MT_QUIRK_SLOT_IS_CONTACTNUMBER,
 290		.maxcontacts = 2 },
 291	{ .name = MT_CLS_INRANGE_CONTACTNUMBER,
 292		.quirks = MT_QUIRK_VALID_IS_INRANGE |
 293			MT_QUIRK_SLOT_IS_CONTACTNUMBER },
 294	{ .name = MT_CLS_WIN_8,
 295		.quirks = MT_QUIRK_ALWAYS_VALID |
 296			MT_QUIRK_IGNORE_DUPLICATES |
 297			MT_QUIRK_HOVERING |
 298			MT_QUIRK_CONTACT_CNT_ACCURATE |
 299			MT_QUIRK_STICKY_FINGERS |
 300			MT_QUIRK_WIN8_PTP_BUTTONS,
 301		.export_all_inputs = true },
 302	{ .name = MT_CLS_EXPORT_ALL_INPUTS,
 303		.quirks = MT_QUIRK_ALWAYS_VALID |
 304			MT_QUIRK_CONTACT_CNT_ACCURATE,
 305		.export_all_inputs = true },
 306	{ .name = MT_CLS_WIN_8_FORCE_MULTI_INPUT,
 307		.quirks = MT_QUIRK_ALWAYS_VALID |
 308			MT_QUIRK_IGNORE_DUPLICATES |
 309			MT_QUIRK_HOVERING |
 310			MT_QUIRK_CONTACT_CNT_ACCURATE |
 311			MT_QUIRK_STICKY_FINGERS |
 312			MT_QUIRK_WIN8_PTP_BUTTONS |
 313			MT_QUIRK_FORCE_MULTI_INPUT,
 314		.export_all_inputs = true },
 315	{ .name = MT_CLS_WIN_8_FORCE_MULTI_INPUT_NSMU,
 316		.quirks = MT_QUIRK_IGNORE_DUPLICATES |
 317			MT_QUIRK_HOVERING |
 318			MT_QUIRK_CONTACT_CNT_ACCURATE |
 319			MT_QUIRK_STICKY_FINGERS |
 320			MT_QUIRK_WIN8_PTP_BUTTONS |
 321			MT_QUIRK_FORCE_MULTI_INPUT |
 322			MT_QUIRK_NOT_SEEN_MEANS_UP,
 323		.export_all_inputs = true },
 324	{ .name = MT_CLS_WIN_8_DISABLE_WAKEUP,
 325		.quirks = MT_QUIRK_ALWAYS_VALID |
 326			MT_QUIRK_IGNORE_DUPLICATES |
 327			MT_QUIRK_HOVERING |
 328			MT_QUIRK_CONTACT_CNT_ACCURATE |
 329			MT_QUIRK_STICKY_FINGERS |
 330			MT_QUIRK_WIN8_PTP_BUTTONS |
 331			MT_QUIRK_DISABLE_WAKEUP,
 332		.export_all_inputs = true },
 333	{ .name = MT_CLS_WIN_8_NO_STICKY_FINGERS,
 334		.quirks = MT_QUIRK_ALWAYS_VALID |
 335			MT_QUIRK_IGNORE_DUPLICATES |
 336			MT_QUIRK_HOVERING |
 337			MT_QUIRK_CONTACT_CNT_ACCURATE |
 338			MT_QUIRK_WIN8_PTP_BUTTONS,
 339		.export_all_inputs = true },
 340	{ .name = MT_CLS_WIN_8_KEEP_LATENCY_ON_CLOSE,
 341		.quirks = MT_QUIRK_ALWAYS_VALID |
 342			MT_QUIRK_IGNORE_DUPLICATES |
 343			MT_QUIRK_HOVERING |
 344			MT_QUIRK_CONTACT_CNT_ACCURATE |
 345			MT_QUIRK_STICKY_FINGERS |
 346			MT_QUIRK_WIN8_PTP_BUTTONS |
 347			MT_QUIRK_KEEP_LATENCY_ON_CLOSE,
 348		.export_all_inputs = true },
 349
 350	/*
 351	 * vendor specific classes
 352	 */
 353	{ .name = MT_CLS_3M,
 354		.quirks = MT_QUIRK_VALID_IS_CONFIDENCE |
 355			MT_QUIRK_SLOT_IS_CONTACTID |
 356			MT_QUIRK_TOUCH_SIZE_SCALING,
 357		.sn_move = 2048,
 358		.sn_width = 128,
 359		.sn_height = 128,
 360		.maxcontacts = 60,
 361	},
 362	{ .name = MT_CLS_EGALAX,
 363		.quirks =  MT_QUIRK_SLOT_IS_CONTACTID |
 364			MT_QUIRK_VALID_IS_INRANGE,
 365		.sn_move = 4096,
 366		.sn_pressure = 32,
 367	},
 368	{ .name = MT_CLS_EGALAX_SERIAL,
 369		.quirks =  MT_QUIRK_SLOT_IS_CONTACTID |
 370			MT_QUIRK_ALWAYS_VALID,
 371		.sn_move = 4096,
 372		.sn_pressure = 32,
 373	},
 374	{ .name = MT_CLS_TOPSEED,
 375		.quirks = MT_QUIRK_ALWAYS_VALID,
 376		.is_indirect = true,
 377		.maxcontacts = 2,
 378	},
 379	{ .name = MT_CLS_PANASONIC,
 380		.quirks = MT_QUIRK_NOT_SEEN_MEANS_UP,
 381		.maxcontacts = 4 },
 382	{ .name	= MT_CLS_GENERALTOUCH_TWOFINGERS,
 383		.quirks	= MT_QUIRK_NOT_SEEN_MEANS_UP |
 384			MT_QUIRK_VALID_IS_INRANGE |
 385			MT_QUIRK_SLOT_IS_CONTACTID,
 386		.maxcontacts = 2
 387	},
 388	{ .name	= MT_CLS_GENERALTOUCH_PWT_TENFINGERS,
 389		.quirks	= MT_QUIRK_NOT_SEEN_MEANS_UP |
 390			MT_QUIRK_SLOT_IS_CONTACTID
 391	},
 392
 393	{ .name = MT_CLS_FLATFROG,
 394		.quirks = MT_QUIRK_NOT_SEEN_MEANS_UP |
 395			MT_QUIRK_NO_AREA,
 396		.sn_move = 2048,
 397		.maxcontacts = 40,
 398	},
 399	{ .name = MT_CLS_LG,
 400		.quirks = MT_QUIRK_ALWAYS_VALID |
 401			MT_QUIRK_FIX_CONST_CONTACT_ID |
 402			MT_QUIRK_IGNORE_DUPLICATES |
 403			MT_QUIRK_HOVERING |
 404			MT_QUIRK_CONTACT_CNT_ACCURATE },
 405	{ .name = MT_CLS_ASUS,
 406		.quirks = MT_QUIRK_ALWAYS_VALID |
 407			MT_QUIRK_CONTACT_CNT_ACCURATE |
 408			MT_QUIRK_ASUS_CUSTOM_UP },
 409	{ .name = MT_CLS_VTL,
 410		.quirks = MT_QUIRK_ALWAYS_VALID |
 411			MT_QUIRK_CONTACT_CNT_ACCURATE |
 412			MT_QUIRK_STICKY_FINGERS |
 413			MT_QUIRK_FORCE_GET_FEATURE,
 414	},
 415	{ .name = MT_CLS_GOOGLE,
 416		.quirks = MT_QUIRK_ALWAYS_VALID |
 417			MT_QUIRK_CONTACT_CNT_ACCURATE |
 418			MT_QUIRK_SLOT_IS_CONTACTID |
 419			MT_QUIRK_HOVERING
 420	},
 421	{ .name = MT_CLS_RAZER_BLADE_STEALTH,
 422		.quirks = MT_QUIRK_ALWAYS_VALID |
 423			MT_QUIRK_IGNORE_DUPLICATES |
 424			MT_QUIRK_HOVERING |
 425			MT_QUIRK_CONTACT_CNT_ACCURATE |
 426			MT_QUIRK_WIN8_PTP_BUTTONS,
 427	},
 428	{ .name = MT_CLS_SMART_TECH,
 429		.quirks = MT_QUIRK_ALWAYS_VALID |
 430			MT_QUIRK_IGNORE_DUPLICATES |
 431			MT_QUIRK_CONTACT_CNT_ACCURATE |
 432			MT_QUIRK_SEPARATE_APP_REPORT,
 433	},
 434	{ .name = MT_CLS_APPLE_TOUCHBAR,
 435		.quirks = MT_QUIRK_HOVERING |
 436			MT_QUIRK_SLOT_IS_CONTACTID_MINUS_ONE |
 437			MT_QUIRK_APPLE_TOUCHBAR,
 438		.maxcontacts = 11,
 439	},
 440	{ .name = MT_CLS_SIS,
 441		.quirks = MT_QUIRK_NOT_SEEN_MEANS_UP |
 442			MT_QUIRK_ALWAYS_VALID |
 443			MT_QUIRK_CONTACT_CNT_ACCURATE,
 444	},
 445		{ .name = MT_CLS_YOGABOOK9I,
 446		.quirks = MT_QUIRK_ALWAYS_VALID |
 447			MT_QUIRK_FORCE_MULTI_INPUT |
 448			MT_QUIRK_SEPARATE_APP_REPORT |
 449			MT_QUIRK_HOVERING |
 450			MT_QUIRK_YOGABOOK9I,
 451		.export_all_inputs = true
 452	},
 453	{ .name = MT_CLS_EGALAX_P80H84,
 454		.quirks = MT_QUIRK_ALWAYS_VALID |
 455			MT_QUIRK_IGNORE_DUPLICATES |
 456			MT_QUIRK_CONTACT_CNT_ACCURATE,
 457	},
 458	{ }
 459};
 460
 461static ssize_t mt_show_quirks(struct device *dev,
 462			   struct device_attribute *attr,
 463			   char *buf)
 464{
 465	struct hid_device *hdev = to_hid_device(dev);
 466	struct mt_device *td = hid_get_drvdata(hdev);
 467
 468	return sprintf(buf, "%u\n", td->mtclass.quirks);
 469}
 470
 471static ssize_t mt_set_quirks(struct device *dev,
 472			  struct device_attribute *attr,
 473			  const char *buf, size_t count)
 474{
 475	struct hid_device *hdev = to_hid_device(dev);
 476	struct mt_device *td = hid_get_drvdata(hdev);
 477	struct mt_application *application;
 478
 479	unsigned long val;
 480
 481	if (kstrtoul(buf, 0, &val))
 482		return -EINVAL;
 483
 484	td->mtclass.quirks = val;
 485
 486	list_for_each_entry(application, &td->applications, list) {
 487		application->quirks = val;
 488		if (!application->have_contact_count)
 489			application->quirks &= ~MT_QUIRK_CONTACT_CNT_ACCURATE;
 490	}
 491
 492	return count;
 493}
 494
 495static DEVICE_ATTR(quirks, S_IWUSR | S_IRUGO, mt_show_quirks, mt_set_quirks);
 496
 497static struct attribute *sysfs_attrs[] = {
 498	&dev_attr_quirks.attr,
 499	NULL
 500};
 501
 502static const struct attribute_group mt_attribute_group = {
 503	.attrs = sysfs_attrs
 504};
 505
 506static void mt_get_feature(struct hid_device *hdev, struct hid_report *report)
 507{
 508	int ret;
 509	u32 size = hid_report_len(report);
 510	u8 *buf;
 511
 512	/*
 513	 * Do not fetch the feature report if the device has been explicitly
 514	 * marked as non-capable.
 515	 */
 516	if (hdev->quirks & HID_QUIRK_NO_INIT_REPORTS)
 517		return;
 518
 519	buf = hid_alloc_report_buf(report, GFP_KERNEL);
 520	if (!buf)
 521		return;
 522
 523	ret = hid_hw_raw_request(hdev, report->id, buf, size,
 524				 HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
 525	if (ret < 0) {
 526		dev_warn(&hdev->dev, "failed to fetch feature %d\n",
 527			 report->id);
 528	} else {
 529		/* The report ID in the request and the response should match */
 530		if (report->id != buf[0]) {
 531			hid_err(hdev, "Returned feature report did not match the request\n");
 532			goto free;
 533		}
 534
 535		ret = hid_report_raw_event(hdev, HID_FEATURE_REPORT, buf,
 536					   size, 0);
 537		if (ret)
 538			dev_warn(&hdev->dev, "failed to report feature\n");
 539	}
 540
 541free:
 542	kfree(buf);
 543}
 544
 545static void mt_feature_mapping(struct hid_device *hdev,
 546		struct hid_field *field, struct hid_usage *usage)
 547{
 548	struct mt_device *td = hid_get_drvdata(hdev);
 549
 550	switch (usage->hid) {
 551	case HID_DG_CONTACTMAX:
 552		mt_get_feature(hdev, field->report);
 553
 554		td->maxcontacts = field->value[0];
 555		if (!td->maxcontacts &&
 556		    field->logical_maximum <= MT_MAX_MAXCONTACT)
 557			td->maxcontacts = field->logical_maximum;
 558		if (td->mtclass.maxcontacts)
 559			/* check if the maxcontacts is given by the class */
 560			td->maxcontacts = td->mtclass.maxcontacts;
 561
 562		break;
 563	case HID_DG_BUTTONTYPE:
 564		if (usage->usage_index >= field->report_count) {
 565			dev_err(&hdev->dev, "HID_DG_BUTTONTYPE out of range\n");
 566			break;
 567		}
 568
 569		mt_get_feature(hdev, field->report);
 570		switch (field->value[usage->usage_index]) {
 571		case MT_BUTTONTYPE_CLICKPAD:
 572			td->is_buttonpad = true;
 573			break;
 574		case MT_BUTTONTYPE_PRESSUREPAD:
 575			td->is_pressurepad = true;
 576			break;
 577		}
 578
 579		break;
 580	case 0xff0000c5:
 581		/* Retrieve the Win8 blob once to enable some devices */
 582		if (usage->usage_index == 0)
 583			mt_get_feature(hdev, field->report);
 584		break;
 585	}
 586
 587	hid_haptic_feature_mapping(hdev, td->haptic, field, usage);
 588}
 589
 590static void set_abs(struct input_dev *input, unsigned int code,
 591		struct hid_field *field, int snratio)
 592{
 593	int fmin = field->logical_minimum;
 594	int fmax = field->logical_maximum;
 595	int fuzz = snratio ? (fmax - fmin) / snratio : 0;
 596	input_set_abs_params(input, code, fmin, fmax, fuzz, 0);
 597	input_abs_set_res(input, code, hidinput_calc_abs_res(field, code));
 598}
 599
 600static struct mt_usages *mt_allocate_usage(struct hid_device *hdev,
 601					   struct mt_application *application)
 602{
 603	struct mt_usages *usage;
 604
 605	usage = devm_kzalloc(&hdev->dev, sizeof(*usage), GFP_KERNEL);
 606	if (!usage)
 607		return NULL;
 608
 609	/* set some defaults so we do not need to check for null pointers */
 610	usage->x = DEFAULT_ZERO;
 611	usage->y = DEFAULT_ZERO;
 612	usage->cx = DEFAULT_ZERO;
 613	usage->cy = DEFAULT_ZERO;
 614	usage->p = DEFAULT_ZERO;
 615	usage->w = DEFAULT_ZERO;
 616	usage->h = DEFAULT_ZERO;
 617	usage->a = DEFAULT_ZERO;
 618	usage->contactid = DEFAULT_ZERO;
 619	usage->tip_state = DEFAULT_FALSE;
 620	usage->inrange_state = DEFAULT_FALSE;
 621	usage->confidence_state = DEFAULT_TRUE;
 622
 623	list_add_tail(&usage->list, &application->mt_usages);
 624
 625	return usage;
 626}
 627
 628static struct mt_application *mt_allocate_application(struct mt_device *td,
 629						      struct hid_report *report)
 630{
 631	unsigned int application = report->application;
 632	struct mt_application *mt_application;
 633
 634	mt_application = devm_kzalloc(&td->hdev->dev, sizeof(*mt_application),
 635				      GFP_KERNEL);
 636	if (!mt_application)
 637		return NULL;
 638
 639	mt_application->application = application;
 640	INIT_LIST_HEAD(&mt_application->mt_usages);
 641
 642	if (application == HID_DG_TOUCHSCREEN)
 643		mt_application->mt_flags |= INPUT_MT_DIRECT;
 644
 645	/*
 646	 * Model touchscreens providing buttons as touchpads.
 647	 */
 648	if (application == HID_DG_TOUCHPAD) {
 649		mt_application->mt_flags |= INPUT_MT_POINTER;
 650		td->inputmode_value = MT_INPUTMODE_TOUCHPAD;
 651	}
 652
 653	mt_application->scantime = DEFAULT_ZERO;
 654	mt_application->raw_cc = DEFAULT_ZERO;
 655	mt_application->quirks = td->mtclass.quirks;
 656	mt_application->report_id = report->id;
 657
 658	list_add_tail(&mt_application->list, &td->applications);
 659
 660	return mt_application;
 661}
 662
 663static struct mt_application *mt_find_application(struct mt_device *td,
 664						  struct hid_report *report)
 665{
 666	unsigned int application = report->application;
 667	struct mt_application *tmp, *mt_application = NULL;
 668
 669	list_for_each_entry(tmp, &td->applications, list) {
 670		if (application == tmp->application) {
 671			if (!(td->mtclass.quirks & MT_QUIRK_SEPARATE_APP_REPORT) ||
 672			    tmp->report_id == report->id) {
 673				mt_application = tmp;
 674				break;
 675			}
 676		}
 677	}
 678
 679	if (!mt_application)
 680		mt_application = mt_allocate_application(td, report);
 681
 682	return mt_application;
 683}
 684
 685static struct mt_report_data *mt_allocate_report_data(struct mt_device *td,
 686						      struct hid_report *report)
 687{
 688	struct mt_class *cls = &td->mtclass;
 689	struct mt_report_data *rdata;
 690	struct hid_field *field;
 691	int r, n;
 692
 693	rdata = devm_kzalloc(&td->hdev->dev, sizeof(*rdata), GFP_KERNEL);
 694	if (!rdata)
 695		return NULL;
 696
 697	rdata->report = report;
 698	rdata->application = mt_find_application(td, report);
 699
 700	if (!rdata->application) {
 701		devm_kfree(&td->hdev->dev, rdata);
 702		return NULL;
 703	}
 704
 705	for (r = 0; r < report->maxfield; r++) {
 706		field = report->field[r];
 707
 708		if (!(HID_MAIN_ITEM_VARIABLE & field->flags))
 709			continue;
 710
 711		if (field->logical == HID_DG_FINGER || td->hdev->group != HID_GROUP_MULTITOUCH_WIN_8) {
 712			for (n = 0; n < field->report_count; n++) {
 713				unsigned int hid = field->usage[n].hid;
 714
 715				if (hid == HID_DG_CONTACTID ||
 716				   (cls->quirks & MT_QUIRK_APPLE_TOUCHBAR &&
 717				   hid == HID_DG_TRANSDUCER_INDEX)) {
 718					rdata->is_mt_collection = true;
 719					break;
 720				}
 721			}
 722		}
 723	}
 724
 725	list_add_tail(&rdata->list, &td->reports);
 726
 727	return rdata;
 728}
 729
 730static struct mt_report_data *mt_find_report_data(struct mt_device *td,
 731						  struct hid_report *report)
 732{
 733	struct mt_report_data *tmp, *rdata = NULL;
 734
 735	list_for_each_entry(tmp, &td->reports, list) {
 736		if (report == tmp->report) {
 737			rdata = tmp;
 738			break;
 739		}
 740	}
 741
 742	if (!rdata)
 743		rdata = mt_allocate_report_data(td, report);
 744
 745	return rdata;
 746}
 747
 748static void mt_store_field(struct hid_device *hdev,
 749			   struct mt_application *application,
 750			   __s32 *value,
 751			   size_t offset)
 752{
 753	struct mt_usages *usage;
 754	__s32 **target;
 755
 756	if (list_empty(&application->mt_usages))
 757		usage = mt_allocate_usage(hdev, application);
 758	else
 759		usage = list_last_entry(&application->mt_usages,
 760					struct mt_usages,
 761					list);
 762
 763	if (!usage)
 764		return;
 765
 766	target = (__s32 **)((char *)usage + offset);
 767
 768	/* the value has already been filled, create a new slot */
 769	if (*target != DEFAULT_TRUE &&
 770	    *target != DEFAULT_FALSE &&
 771	    *target != DEFAULT_ZERO) {
 772		if (usage->contactid == DEFAULT_ZERO ||
 773		    usage->x == DEFAULT_ZERO ||
 774		    usage->y == DEFAULT_ZERO) {
 775			hid_dbg(hdev,
 776				"ignoring duplicate usage on incomplete");
 777			return;
 778		}
 779		usage = mt_allocate_usage(hdev, application);
 780		if (!usage)
 781			return;
 782
 783		target = (__s32 **)((char *)usage + offset);
 784	}
 785
 786	*target = value;
 787}
 788
 789#define MT_STORE_FIELD(__name)						\
 790	mt_store_field(hdev, app,					\
 791		       &field->value[usage->usage_index],		\
 792		       offsetof(struct mt_usages, __name))
 793
 794static int mt_touch_input_mapping(struct hid_device *hdev, struct hid_input *hi,
 795		struct hid_field *field, struct hid_usage *usage,
 796		unsigned long **bit, int *max, struct mt_application *app)
 797{
 798	struct mt_device *td = hid_get_drvdata(hdev);
 799	struct mt_class *cls = &td->mtclass;
 800	int code;
 801	struct hid_usage *prev_usage = NULL;
 802
 803	/*
 804	 * Model touchscreens providing buttons as touchpads.
 805	 */
 806	if (field->application == HID_DG_TOUCHSCREEN &&
 807	    (usage->hid & HID_USAGE_PAGE) == HID_UP_BUTTON) {
 808		app->mt_flags |= INPUT_MT_POINTER;
 809		td->inputmode_value = MT_INPUTMODE_TOUCHPAD;
 810	}
 811
 812	/* count the buttons on touchpads */
 813	if ((usage->hid & HID_USAGE_PAGE) == HID_UP_BUTTON)
 814		app->buttons_count++;
 815
 816	if (usage->usage_index)
 817		prev_usage = &field->usage[usage->usage_index - 1];
 818
 819	switch (usage->hid & HID_USAGE_PAGE) {
 820
 821	case HID_UP_GENDESK:
 822		switch (usage->hid) {
 823		case HID_GD_X:
 824			if (prev_usage && (prev_usage->hid == usage->hid)) {
 825				code = ABS_MT_TOOL_X;
 826				MT_STORE_FIELD(cx);
 827			} else {
 828				code = ABS_MT_POSITION_X;
 829				MT_STORE_FIELD(x);
 830			}
 831
 832			set_abs(hi->input, code, field, cls->sn_move);
 833
 834			/*
 835			 * A system multi-axis that exports X and Y has a high
 836			 * chance of being used directly on a surface
 837			 */
 838			if (field->application == HID_GD_SYSTEM_MULTIAXIS) {
 839				__set_bit(INPUT_PROP_DIRECT,
 840					  hi->input->propbit);
 841				input_set_abs_params(hi->input,
 842						     ABS_MT_TOOL_TYPE,
 843						     MT_TOOL_DIAL,
 844						     MT_TOOL_DIAL, 0, 0);
 845			}
 846
 847			return 1;
 848		case HID_GD_Y:
 849			if (prev_usage && (prev_usage->hid == usage->hid)) {
 850				code = ABS_MT_TOOL_Y;
 851				MT_STORE_FIELD(cy);
 852			} else {
 853				code = ABS_MT_POSITION_Y;
 854				MT_STORE_FIELD(y);
 855			}
 856
 857			set_abs(hi->input, code, field, cls->sn_move);
 858
 859			return 1;
 860		}
 861		return 0;
 862
 863	case HID_UP_DIGITIZER:
 864		switch (usage->hid) {
 865		case HID_DG_INRANGE:
 866			if (app->quirks & MT_QUIRK_HOVERING) {
 867				input_set_abs_params(hi->input,
 868					ABS_MT_DISTANCE, 0, 1, 0, 0);
 869			}
 870			MT_STORE_FIELD(inrange_state);
 871			return 1;
 872		case HID_DG_CONFIDENCE:
 873			if ((cls->name == MT_CLS_WIN_8 ||
 874			     cls->name == MT_CLS_WIN_8_FORCE_MULTI_INPUT ||
 875			     cls->name == MT_CLS_WIN_8_FORCE_MULTI_INPUT_NSMU ||
 876			     cls->name == MT_CLS_WIN_8_DISABLE_WAKEUP ||
 877			     cls->name == MT_CLS_WIN_8_KEEP_LATENCY_ON_CLOSE) &&
 878				(field->application == HID_DG_TOUCHPAD ||
 879				 field->application == HID_DG_TOUCHSCREEN))
 880				app->quirks |= MT_QUIRK_CONFIDENCE;
 881
 882			if (app->quirks & MT_QUIRK_CONFIDENCE)
 883				input_set_abs_params(hi->input,
 884						     ABS_MT_TOOL_TYPE,
 885						     MT_TOOL_FINGER,
 886						     MT_TOOL_PALM, 0, 0);
 887
 888			MT_STORE_FIELD(confidence_state);
 889			return 1;
 890		case HID_DG_TOUCH:
 891			/*
 892			 * Legacy devices use TIPSWITCH and not TOUCH.
 893			 * One special case here is of the Apple Touch Bars.
 894			 * In these devices, the tip state is contained in
 895			 * fields with the HID_DG_TOUCH usage.
 896			 * Let's just ignore this field for other devices.
 897			 */
 898			if (!(cls->quirks & MT_QUIRK_APPLE_TOUCHBAR))
 899				return -1;
 900			fallthrough;
 901		case HID_DG_TIPSWITCH:
 902			if (field->application != HID_GD_SYSTEM_MULTIAXIS)
 903				input_set_capability(hi->input,
 904						     EV_KEY, BTN_TOUCH);
 905			MT_STORE_FIELD(tip_state);
 906			return 1;
 907		case HID_DG_TRANSDUCER_INDEX:
 908			/*
 909			 * Contact ID in case of Apple Touch Bars is contained
 910			 * in fields with HID_DG_TRANSDUCER_INDEX usage.
 911			 */
 912			if (!(cls->quirks & MT_QUIRK_APPLE_TOUCHBAR))
 913				return 0;
 914			fallthrough;
 915		case HID_DG_CONTACTID:
 916			MT_STORE_FIELD(contactid);
 917			app->touches_by_report++;
 918			return 1;
 919		case HID_DG_WIDTH:
 920			if (!(app->quirks & MT_QUIRK_NO_AREA))
 921				set_abs(hi->input, ABS_MT_TOUCH_MAJOR, field,
 922					cls->sn_width);
 923			MT_STORE_FIELD(w);
 924			return 1;
 925		case HID_DG_HEIGHT:
 926			if (!(app->quirks & MT_QUIRK_NO_AREA)) {
 927				set_abs(hi->input, ABS_MT_TOUCH_MINOR, field,
 928					cls->sn_height);
 929
 930				/*
 931				 * Only set ABS_MT_ORIENTATION if it is not
 932				 * already set by the HID_DG_AZIMUTH usage.
 933				 */
 934				if (!test_bit(ABS_MT_ORIENTATION,
 935						hi->input->absbit))
 936					input_set_abs_params(hi->input,
 937						ABS_MT_ORIENTATION, 0, 1, 0, 0);
 938			}
 939			MT_STORE_FIELD(h);
 940			return 1;
 941		case HID_DG_TIPPRESSURE:
 942			set_abs(hi->input, ABS_MT_PRESSURE, field,
 943				cls->sn_pressure);
 944			td->is_haptic_touchpad =
 945				hid_haptic_check_pressure_unit(td->haptic,
 946							       hi, field);
 947			MT_STORE_FIELD(p);
 948			return 1;
 949		case HID_DG_SCANTIME:
 950			input_set_capability(hi->input, EV_MSC, MSC_TIMESTAMP);
 951			app->scantime = &field->value[usage->usage_index];
 952			app->scantime_logical_max = field->logical_maximum;
 953			return 1;
 954		case HID_DG_CONTACTCOUNT:
 955			app->have_contact_count = true;
 956			app->raw_cc = &field->value[usage->usage_index];
 957			return 1;
 958		case HID_DG_AZIMUTH:
 959			/*
 960			 * Azimuth has the range of [0, MAX) representing a full
 961			 * revolution. Set ABS_MT_ORIENTATION to a quarter of
 962			 * MAX according the definition of ABS_MT_ORIENTATION
 963			 */
 964			input_set_abs_params(hi->input, ABS_MT_ORIENTATION,
 965				-field->logical_maximum / 4,
 966				field->logical_maximum / 4,
 967				cls->sn_move ?
 968				field->logical_maximum / cls->sn_move : 0, 0);
 969			MT_STORE_FIELD(a);
 970			return 1;
 971		case HID_DG_CONTACTMAX:
 972			/* contact max are global to the report */
 973			return -1;
 974		}
 975		/* let hid-input decide for the others */
 976		return 0;
 977
 978	case HID_UP_BUTTON:
 979		code = BTN_MOUSE + ((usage->hid - 1) & HID_USAGE);
 980		/*
 981		 * MS PTP spec says that external buttons left and right have
 982		 * usages 2 and 3.
 983		 */
 984		if ((app->quirks & MT_QUIRK_WIN8_PTP_BUTTONS) &&
 985		    field->application == HID_DG_TOUCHPAD &&
 986		    (usage->hid & HID_USAGE) > 1)
 987			code--;
 988
 989		if (field->application == HID_GD_SYSTEM_MULTIAXIS)
 990			code = BTN_0  + ((usage->hid - 1) & HID_USAGE);
 991
 992		hid_map_usage(hi, usage, bit, max, EV_KEY, code);
 993		if (!*bit)
 994			return -1;
 995		input_set_capability(hi->input, EV_KEY, code);
 996		return 1;
 997
 998	case 0xff000000:
 999		/* we do not want to map these: no input-oriented meaning */
1000		return -1;
1001	}
1002
1003	return 0;
1004}
1005
1006static int mt_compute_slot(struct mt_device *td, struct mt_application *app,
1007			   struct mt_usages *slot,
1008			   struct input_dev *input)
1009{
1010	__s32 quirks = app->quirks;
1011
1012	if (quirks & MT_QUIRK_SLOT_IS_CONTACTID)
1013		return *slot->contactid;
1014
1015	if (quirks & MT_QUIRK_CYPRESS)
1016		return cypress_compute_slot(app, slot);
1017
1018	if (quirks & MT_QUIRK_SLOT_IS_CONTACTNUMBER)
1019		return app->num_received;
1020
1021	if (quirks & MT_QUIRK_SLOT_IS_CONTACTID_MINUS_ONE)
1022		return *slot->contactid - 1;
1023
1024	return input_mt_get_slot_by_key(input, *slot->contactid);
1025}
1026
1027static void mt_release_pending_palms(struct mt_device *td,
1028				     struct mt_application *app,
1029				     struct input_dev *input)
1030{
1031	int slotnum;
1032	bool need_sync = false;
1033
1034	for_each_set_bit(slotnum, app->pending_palm_slots, td->maxcontacts) {
1035		clear_bit(slotnum, app->pending_palm_slots);
1036		clear_bit(slotnum, &td->mt_io_flags);
1037
1038		input_mt_slot(input, slotnum);
1039		input_mt_report_slot_inactive(input);
1040
1041		need_sync = true;
1042	}
1043
1044	if (need_sync) {
1045		input_mt_sync_frame(input);
1046		input_sync(input);
1047	}
1048}
1049
1050/*
1051 * this function is called when a whole packet has been received and processed,
1052 * so that it can decide what to send to the input layer.
1053 */
1054static void mt_sync_frame(struct mt_device *td, struct mt_application *app,
1055			  struct input_dev *input)
1056{
1057	if (app->quirks & MT_QUIRK_WIN8_PTP_BUTTONS)
1058		input_event(input, EV_KEY, BTN_LEFT, app->left_button_state);
1059
1060	input_mt_sync_frame(input);
1061	input_event(input, EV_MSC, MSC_TIMESTAMP, app->timestamp);
1062	input_sync(input);
1063
1064	mt_release_pending_palms(td, app, input);
1065
1066	app->num_received = 0;
1067	app->left_button_state = 0;
1068	if (td->is_haptic_touchpad)
1069		hid_haptic_pressure_reset(td->haptic);
1070}
1071
1072static int mt_compute_timestamp(struct mt_application *app, __s32 value)
1073{
1074	long delta = value - app->prev_scantime;
1075	unsigned long jdelta = jiffies_to_usecs(jiffies - app->jiffies);
1076
1077	app->jiffies = jiffies;
1078
1079	if (delta < 0)
1080		delta += app->scantime_logical_max;
1081
1082	/* HID_DG_SCANTIME is expressed in 100us, we want it in us. */
1083	delta *= 100;
1084
1085	if (jdelta > MAX_TIMESTAMP_INTERVAL)
1086		/* No data received for a while, resync the timestamp. */
1087		return 0;
1088	else
1089		return app->timestamp + delta;
1090}
1091
1092static int mt_touch_event(struct hid_device *hid, struct hid_field *field,
1093				struct hid_usage *usage, __s32 value)
1094{
1095	/* we will handle the hidinput part later, now remains hiddev */
1096	if (hid->claimed & HID_CLAIMED_HIDDEV && hid->hiddev_hid_event)
1097		hid->hiddev_hid_event(hid, field, usage, value);
1098
1099	return 1;
1100}
1101
1102static int mt_process_slot(struct mt_device *td, struct input_dev *input,
1103			    struct mt_application *app,
1104			    struct mt_usages *slot)
1105{
1106	struct input_mt *mt = input->mt;
1107	struct hid_device *hdev = td->hdev;
1108	__s32 quirks = app->quirks;
1109	bool valid = true;
1110	bool confidence_state = true;
1111	bool inrange_state = false;
1112	int active;
1113	int slotnum;
1114	int tool = MT_TOOL_FINGER;
1115
1116	if (!slot)
1117		return -EINVAL;
1118
1119	if ((quirks & MT_QUIRK_CONTACT_CNT_ACCURATE) &&
1120	    app->num_received >= app->num_expected)
1121		return -EAGAIN;
1122
1123	if (!(quirks & MT_QUIRK_ALWAYS_VALID)) {
1124		if (quirks & MT_QUIRK_VALID_IS_INRANGE)
1125			valid = *slot->inrange_state;
1126		if (quirks & MT_QUIRK_NOT_SEEN_MEANS_UP)
1127			valid = *slot->tip_state;
1128		if (quirks & MT_QUIRK_VALID_IS_CONFIDENCE)
1129			valid = *slot->confidence_state;
1130
1131		if (!valid)
1132			return 0;
1133	}
1134
1135	slotnum = mt_compute_slot(td, app, slot, input);
1136	if (slotnum < 0 || slotnum >= td->maxcontacts)
1137		return 0;
1138
1139	if ((quirks & MT_QUIRK_IGNORE_DUPLICATES) && mt) {
1140		struct input_mt_slot *i_slot = &mt->slots[slotnum];
1141
1142		if (input_mt_is_active(i_slot) &&
1143		    input_mt_is_used(mt, i_slot))
1144			return -EAGAIN;
1145	}
1146
1147	if (quirks & MT_QUIRK_CONFIDENCE)
1148		confidence_state = *slot->confidence_state;
1149
1150	if (quirks & MT_QUIRK_HOVERING)
1151		inrange_state = *slot->inrange_state;
1152
1153	active = *slot->tip_state || inrange_state;
1154
1155	if (app->application == HID_GD_SYSTEM_MULTIAXIS)
1156		tool = MT_TOOL_DIAL;
1157	else if (unlikely(!confidence_state)) {
1158		tool = MT_TOOL_PALM;
1159		if (!active && mt &&
1160		    input_mt_is_active(&mt->slots[slotnum])) {
1161			/*
1162			 * The non-confidence was reported for
1163			 * previously valid contact that is also no
1164			 * longer valid. We can't simply report
1165			 * lift-off as userspace will not be aware
1166			 * of non-confidence, so we need to split
1167			 * it into 2 events: active MT_TOOL_PALM
1168			 * and a separate liftoff.
1169			 */
1170			active = true;
1171			set_bit(slotnum, app->pending_palm_slots);
1172		}
1173	}
1174
1175	input_mt_slot(input, slotnum);
1176	input_mt_report_slot_state(input, tool, active);
1177	if (active) {
1178		/* this finger is in proximity of the sensor */
1179		int wide = (*slot->w > *slot->h);
1180		int major = max(*slot->w, *slot->h);
1181		int minor = min(*slot->w, *slot->h);
1182		int orientation = wide;
1183		int max_azimuth;
1184		int azimuth;
1185		int x;
1186		int y;
1187		int cx;
1188		int cy;
1189
1190		if (slot->a != DEFAULT_ZERO) {
1191			/*
1192			 * Azimuth is counter-clockwise and ranges from [0, MAX)
1193			 * (a full revolution). Convert it to clockwise ranging
1194			 * [-MAX/2, MAX/2].
1195			 *
1196			 * Note that ABS_MT_ORIENTATION require us to report
1197			 * the limit of [-MAX/4, MAX/4], but the value can go
1198			 * out of range to [-MAX/2, MAX/2] to report an upside
1199			 * down ellipsis.
1200			 */
1201			azimuth = *slot->a;
1202			max_azimuth = input_abs_get_max(input,
1203							ABS_MT_ORIENTATION);
1204			if (azimuth > max_azimuth * 2)
1205				azimuth -= max_azimuth * 4;
1206			orientation = -azimuth;
1207			if (quirks & MT_QUIRK_ORIENTATION_INVERT)
1208				orientation = -orientation;
1209
1210		}
1211
1212		if (quirks & MT_QUIRK_TOUCH_SIZE_SCALING) {
1213			/*
1214			 * divided by two to match visual scale of touch
1215			 * for devices with this quirk
1216			 */
1217			major = major >> 1;
1218			minor = minor >> 1;
1219		}
1220
1221		if (td->is_haptic_touchpad)
1222			hid_haptic_pressure_increase(td->haptic, *slot->p);
1223
1224		x = hdev->quirks & HID_QUIRK_X_INVERT ?
1225			input_abs_get_max(input, ABS_MT_POSITION_X) - *slot->x :
1226			*slot->x;
1227		y = hdev->quirks & HID_QUIRK_Y_INVERT ?
1228			input_abs_get_max(input, ABS_MT_POSITION_Y) - *slot->y :
1229			*slot->y;
1230		cx = hdev->quirks & HID_QUIRK_X_INVERT ?
1231			input_abs_get_max(input, ABS_MT_POSITION_X) - *slot->cx :
1232			*slot->cx;
1233		cy = hdev->quirks & HID_QUIRK_Y_INVERT ?
1234			input_abs_get_max(input, ABS_MT_POSITION_Y) - *slot->cy :
1235			*slot->cy;
1236
1237		input_event(input, EV_ABS, ABS_MT_POSITION_X, x);
1238		input_event(input, EV_ABS, ABS_MT_POSITION_Y, y);
1239		input_event(input, EV_ABS, ABS_MT_TOOL_X, cx);
1240		input_event(input, EV_ABS, ABS_MT_TOOL_Y, cy);
1241		input_event(input, EV_ABS, ABS_MT_DISTANCE, !*slot->tip_state);
1242		input_event(input, EV_ABS, ABS_MT_ORIENTATION, orientation);
1243		input_event(input, EV_ABS, ABS_MT_PRESSURE, *slot->p);
1244		input_event(input, EV_ABS, ABS_MT_TOUCH_MAJOR, major);
1245		input_event(input, EV_ABS, ABS_MT_TOUCH_MINOR, minor);
1246
1247		set_bit(slotnum, &td->mt_io_flags);
1248	} else {
1249		clear_bit(slotnum, &td->mt_io_flags);
1250	}
1251
1252	return 0;
1253}
1254
1255static void mt_process_mt_event(struct hid_device *hid,
1256				struct mt_application *app,
1257				struct hid_field *field,
1258				struct hid_usage *usage,
1259				__s32 value,
1260				bool first_packet)
1261{
1262	__s32 quirks = app->quirks;
1263	struct input_dev *input = field->hidinput->input;
1264
1265	if (!usage->type || !(hid->claimed & HID_CLAIMED_INPUT))
1266		return;
1267
1268	if (quirks & MT_QUIRK_WIN8_PTP_BUTTONS) {
1269
1270		/*
1271		 * For Win8 PTP touchpads we should only look at
1272		 * non finger/touch events in the first_packet of a
1273		 * (possible) multi-packet frame.
1274		 */
1275		if (!first_packet)
1276			return;
1277
1278		/*
1279		 * For Win8 PTP touchpads we map both the clickpad click
1280		 * and any "external" left buttons to BTN_LEFT if a
1281		 * device claims to have both we need to report 1 for
1282		 * BTN_LEFT if either is pressed, so we or all values
1283		 * together and report the result in mt_sync_frame().
1284		 */
1285		if (usage->type == EV_KEY && usage->code == BTN_LEFT) {
1286			app->left_button_state |= value;
1287			return;
1288		}
1289	}
1290
1291	input_event(input, usage->type, usage->code, value);
1292}
1293
1294static void mt_touch_report(struct hid_device *hid,
1295			    struct mt_report_data *rdata)
1296{
1297	struct mt_device *td = hid_get_drvdata(hid);
1298	struct hid_report *report = rdata->report;
1299	struct mt_application *app = rdata->application;
1300	struct hid_field *field;
1301	struct input_dev *input;
1302	struct mt_usages *slot;
1303	bool first_packet;
1304	unsigned count;
1305	int r, n;
1306	int scantime = 0;
1307	int contact_count = -1;
1308
1309	/* sticky fingers release in progress, abort */
1310	if (test_and_set_bit_lock(MT_IO_FLAGS_RUNNING, &td->mt_io_flags))
1311		return;
1312
1313	scantime = *app->scantime;
1314	app->timestamp = mt_compute_timestamp(app, scantime);
1315	if (app->raw_cc != DEFAULT_ZERO)
1316		contact_count = *app->raw_cc;
1317
1318	/*
1319	 * Includes multi-packet support where subsequent
1320	 * packets are sent with zero contactcount.
1321	 */
1322	if (contact_count >= 0) {
1323		/*
1324		 * For Win8 PTPs the first packet (td->num_received == 0) may
1325		 * have a contactcount of 0 if there only is a button event.
1326		 * We double check that this is not a continuation packet
1327		 * of a possible multi-packet frame be checking that the
1328		 * timestamp has changed.
1329		 */
1330		if ((app->quirks & MT_QUIRK_WIN8_PTP_BUTTONS) &&
1331		    app->num_received == 0 &&
1332		    app->prev_scantime != scantime)
1333			app->num_expected = contact_count;
1334		/* A non 0 contact count always indicates a first packet */
1335		else if (contact_count)
1336			app->num_expected = contact_count;
1337	}
1338	app->prev_scantime = scantime;
1339
1340	first_packet = app->num_received == 0;
1341
1342	input = report->field[0]->hidinput->input;
1343
1344	list_for_each_entry(slot, &app->mt_usages, list) {
1345		if (!mt_process_slot(td, input, app, slot))
1346			app->num_received++;
1347	}
1348
1349	for (r = 0; r < report->maxfield; r++) {
1350		field = report->field[r];
1351		count = field->report_count;
1352
1353		if (!(HID_MAIN_ITEM_VARIABLE & field->flags))
1354			continue;
1355
1356		for (n = 0; n < count; n++)
1357			mt_process_mt_event(hid, app, field,
1358					    &field->usage[n], field->value[n],
1359					    first_packet);
1360	}
1361
1362	if (app->num_received >= app->num_expected)
1363		mt_sync_frame(td, app, input);
1364
1365	/*
1366	 * Windows 8 specs says 2 things:
1367	 * - once a contact has been reported, it has to be reported in each
1368	 *   subsequent report
1369	 * - the report rate when fingers are present has to be at least
1370	 *   the refresh rate of the screen, 60 or 120 Hz
1371	 *
1372	 * I interprete this that the specification forces a report rate of
1373	 * at least 60 Hz for a touchscreen to be certified.
1374	 * Which means that if we do not get a report whithin 16 ms, either
1375	 * something wrong happens, either the touchscreen forgets to send
1376	 * a release. Taking a reasonable margin allows to remove issues
1377	 * with USB communication or the load of the machine.
1378	 *
1379	 * Given that Win 8 devices are forced to send a release, this will
1380	 * only affect laggish machines and the ones that have a firmware
1381	 * defect.
1382	 */
1383	if (app->quirks & MT_QUIRK_STICKY_FINGERS) {
1384		if (td->mt_io_flags & MT_IO_SLOTS_MASK)
1385			mod_timer(&td->release_timer,
1386				  jiffies + msecs_to_jiffies(100));
1387		else
1388			timer_delete(&td->release_timer);
1389	}
1390
1391	clear_bit_unlock(MT_IO_FLAGS_RUNNING, &td->mt_io_flags);
1392}
1393
1394static int mt_touch_input_configured(struct hid_device *hdev,
1395				     struct hid_input *hi,
1396				     struct mt_application *app)
1397{
1398	struct mt_device *td = hid_get_drvdata(hdev);
1399	struct mt_class *cls = &td->mtclass;
1400	struct input_dev *input = hi->input;
1401	int ret;
1402
1403	/*
1404	 * HID_DG_CONTACTMAX field is not present on Apple Touch Bars,
1405	 * but the maximum contact count is greater than the default.
1406	 */
1407	if (cls->quirks & MT_QUIRK_APPLE_TOUCHBAR && cls->maxcontacts)
1408		td->maxcontacts = cls->maxcontacts;
1409
1410	if (!td->maxcontacts)
1411		td->maxcontacts = MT_DEFAULT_MAXCONTACT;
1412
1413	mt_post_parse(td, app);
1414	if (td->serial_maybe)
1415		mt_post_parse_default_settings(td, app);
1416
1417	/*
1418	 * The application for Apple Touch Bars is HID_DG_TOUCHPAD,
1419	 * but these devices are direct.
1420	 */
1421	if (cls->quirks & MT_QUIRK_APPLE_TOUCHBAR)
1422		app->mt_flags |= INPUT_MT_DIRECT;
1423
1424	if (cls->is_indirect)
1425		app->mt_flags |= INPUT_MT_POINTER;
1426
1427	if (td->is_haptic_touchpad)
1428		app->mt_flags |= INPUT_MT_TOTAL_FORCE;
1429
1430	if (app->quirks & MT_QUIRK_NOT_SEEN_MEANS_UP)
1431		app->mt_flags |= INPUT_MT_DROP_UNUSED;
1432
1433	/* check for clickpads */
1434	if ((app->mt_flags & INPUT_MT_POINTER) &&
1435	    (app->buttons_count == 1))
1436		td->is_buttonpad = true;
1437
1438	if (td->is_buttonpad)
1439		__set_bit(INPUT_PROP_BUTTONPAD, input->propbit);
1440	if (td->is_pressurepad)
1441		__set_bit(INPUT_PROP_PRESSUREPAD, input->propbit);
1442
1443	app->pending_palm_slots = devm_kcalloc(&hi->input->dev,
1444					       BITS_TO_LONGS(td->maxcontacts),
1445					       sizeof(long),
1446					       GFP_KERNEL);
1447	if (!app->pending_palm_slots)
1448		return -ENOMEM;
1449
1450	ret = input_mt_init_slots(input, td->maxcontacts, app->mt_flags);
1451	if (ret)
1452		return ret;
1453
1454	app->mt_flags = 0;
1455	return 0;
1456}
1457
1458#define mt_map_key_clear(c)	hid_map_usage_clear(hi, usage, bit, \
1459						    max, EV_KEY, (c))
1460static int mt_input_mapping(struct hid_device *hdev, struct hid_input *hi,
1461		struct hid_field *field, struct hid_usage *usage,
1462		unsigned long **bit, int *max)
1463{
1464	struct mt_device *td = hid_get_drvdata(hdev);
1465	struct mt_application *application;
1466	struct mt_report_data *rdata;
1467	int ret;
1468
1469	rdata = mt_find_report_data(td, field->report);
1470	if (!rdata) {
1471		hid_err(hdev, "failed to allocate data for report\n");
1472		return 0;
1473	}
1474
1475	application = rdata->application;
1476
1477	/*
1478	 * If mtclass.export_all_inputs is not set, only map fields from
1479	 * TouchScreen or TouchPad collections. We need to ignore fields
1480	 * that belong to other collections such as Mouse that might have
1481	 * the same GenericDesktop usages.
1482	 */
1483	if (!td->mtclass.export_all_inputs &&
1484	    field->application != HID_DG_TOUCHSCREEN &&
1485	    field->application != HID_DG_PEN &&
1486	    field->application != HID_DG_TOUCHPAD &&
1487	    field->application != HID_GD_KEYBOARD &&
1488	    field->application != HID_GD_SYSTEM_CONTROL &&
1489	    field->application != HID_CP_CONSUMER_CONTROL &&
1490	    field->application != HID_GD_WIRELESS_RADIO_CTLS &&
1491	    field->application != HID_GD_SYSTEM_MULTIAXIS &&
1492	    !(field->application == HID_VD_ASUS_CUSTOM_MEDIA_KEYS &&
1493	      application->quirks & MT_QUIRK_ASUS_CUSTOM_UP))
1494		return -1;
1495
1496	/*
1497	 * Some Asus keyboard+touchpad devices have the hotkeys defined in the
1498	 * touchpad report descriptor. We need to treat these as an array to
1499	 * map usages to input keys.
1500	 */
1501	if (field->application == HID_VD_ASUS_CUSTOM_MEDIA_KEYS &&
1502	    application->quirks & MT_QUIRK_ASUS_CUSTOM_UP &&
1503	    (usage->hid & HID_USAGE_PAGE) == HID_UP_CUSTOM) {
1504		set_bit(EV_REP, hi->input->evbit);
1505		if (field->flags & HID_MAIN_ITEM_VARIABLE)
1506			field->flags &= ~HID_MAIN_ITEM_VARIABLE;
1507		switch (usage->hid & HID_USAGE) {
1508		case 0x10: mt_map_key_clear(KEY_BRIGHTNESSDOWN);	break;
1509		case 0x20: mt_map_key_clear(KEY_BRIGHTNESSUP);		break;
1510		case 0x35: mt_map_key_clear(KEY_DISPLAY_OFF);		break;
1511		case 0x6b: mt_map_key_clear(KEY_F21);			break;
1512		case 0x6c: mt_map_key_clear(KEY_SLEEP);			break;
1513		default:
1514			return -1;
1515		}
1516		return 1;
1517	}
1518
1519	if (rdata->is_mt_collection)
1520		return mt_touch_input_mapping(hdev, hi, field, usage, bit, max,
1521					      application);
1522
1523	/*
1524	 * some egalax touchscreens have "application == DG_TOUCHSCREEN"
1525	 * for the stylus. Overwrite the hid_input application
1526	 */
1527	if (field->physical == HID_DG_STYLUS)
1528		hi->application = HID_DG_STYLUS;
1529
1530	ret = hid_haptic_input_mapping(hdev, td->haptic, hi, field, usage, bit,
1531				       max);
1532	if (ret != 0)
1533		return ret;
1534
1535	/* let hid-core decide for the others */
1536	return 0;
1537}
1538
1539static int mt_input_mapped(struct hid_device *hdev, struct hid_input *hi,
1540		struct hid_field *field, struct hid_usage *usage,
1541		unsigned long **bit, int *max)
1542{
1543	struct mt_device *td = hid_get_drvdata(hdev);
1544	struct mt_report_data *rdata;
1545
1546	rdata = mt_find_report_data(td, field->report);
1547	if (rdata && rdata->is_mt_collection) {
1548		/* We own these mappings, tell hid-input to ignore them */
1549		return -1;
1550	}
1551
1552	/* let hid-core decide for the others */
1553	return 0;
1554}
1555
1556static int mt_event(struct hid_device *hid, struct hid_field *field,
1557				struct hid_usage *usage, __s32 value)
1558{
1559	struct mt_device *td = hid_get_drvdata(hid);
1560	struct mt_report_data *rdata;
1561
1562	rdata = mt_find_report_data(td, field->report);
1563	if (rdata && rdata->is_mt_collection)
1564		return mt_touch_event(hid, field, usage, value);
1565
1566	return 0;
1567}
1568
1569static const __u8 *mt_report_fixup(struct hid_device *hdev, __u8 *rdesc,
1570			     unsigned int *size)
1571{
1572	if (hdev->vendor == I2C_VENDOR_ID_GOODIX &&
1573	    (hdev->product == I2C_DEVICE_ID_GOODIX_01E8 ||
1574	     hdev->product == I2C_DEVICE_ID_GOODIX_01E9)) {
1575		if (*size < 608) {
1576			dev_info(
1577				&hdev->dev,
1578				"GT7868Q fixup: report descriptor is only %u bytes, skipping\n",
1579				*size);
1580			return rdesc;
1581		}
1582
1583		if (rdesc[607] == 0x15) {
1584			rdesc[607] = 0x25;
1585			dev_info(
1586				&hdev->dev,
1587				"GT7868Q report descriptor fixup is applied.\n");
1588		} else {
1589			dev_info(
1590				&hdev->dev,
1591				"The byte is not expected for fixing the report descriptor. \
1592It's possible that the touchpad firmware is not suitable for applying the fix. \
1593got: %x\n",
1594				rdesc[607]);
1595		}
1596	}
1597
1598	return rdesc;
1599}
1600
1601static void mt_report(struct hid_device *hid, struct hid_report *report)
1602{
1603	struct mt_device *td = hid_get_drvdata(hid);
1604	struct hid_field *field = report->field[0];
1605	struct mt_report_data *rdata;
1606
1607	if (!(hid->claimed & HID_CLAIMED_INPUT))
1608		return;
1609
1610	rdata = mt_find_report_data(td, report);
1611	if (rdata && rdata->is_mt_collection)
1612		return mt_touch_report(hid, rdata);
1613
1614	/* Lenovo Yoga Book 9i requires consuming and dropping certain bogus reports */
1615	if (rdata && rdata->application &&
1616		(rdata->application->quirks & MT_QUIRK_YOGABOOK9I)) {
1617
1618		bool all_zero_report = true;
1619
1620		for (int f = 0; f < report->maxfield && all_zero_report; f++) {
1621			struct hid_field *fld = report->field[f];
1622
1623			for (int i = 0; i < fld->report_count; i++) {
1624				unsigned int usage = fld->usage[i].hid;
1625
1626				if (usage == HID_DG_INRANGE ||
1627					usage == HID_DG_TIPSWITCH ||
1628					usage == HID_DG_BARRELSWITCH ||
1629					usage == HID_DG_BARRELSWITCH2 ||
1630					usage == HID_DG_CONTACTID ||
1631					usage == HID_DG_TILT_X ||
1632					usage == HID_DG_TILT_Y) {
1633
1634					if (fld->value[i] != 0) {
1635						all_zero_report = false;
1636						break;
1637					}
1638				}
1639			}
1640		}
1641
1642		if (all_zero_report)
1643			return;
1644	}
1645
1646	if (field && field->hidinput && field->hidinput->input)
1647		input_sync(field->hidinput->input);
1648}
1649
1650static bool mt_need_to_apply_feature(struct hid_device *hdev,
1651				     struct hid_field *field,
1652				     struct hid_usage *usage,
1653				     enum latency_mode latency,
1654				     enum report_mode report_mode,
1655				     bool *inputmode_found)
1656{
1657	struct mt_device *td = hid_get_drvdata(hdev);
1658	struct mt_class *cls = &td->mtclass;
1659	struct hid_report *report = field->report;
1660	unsigned int index = usage->usage_index;
1661	char *buf;
1662	u32 report_len;
1663	int max;
1664
1665	switch (usage->hid) {
1666	case HID_DG_INPUTMODE:
1667		/*
1668		 * Some elan panels wrongly declare 2 input mode features,
1669		 * and silently ignore when we set the value in the second
1670		 * field. Skip the second feature and hope for the best.
1671		 */
1672		if (*inputmode_found)
1673			return false;
1674
1675		if (cls->quirks & MT_QUIRK_FORCE_GET_FEATURE) {
1676			report_len = hid_report_len(report);
1677			buf = hid_alloc_report_buf(report, GFP_KERNEL);
1678			if (!buf) {
1679				hid_err(hdev,
1680					"failed to allocate buffer for report\n");
1681				return false;
1682			}
1683			hid_hw_raw_request(hdev, report->id, buf, report_len,
1684					   HID_FEATURE_REPORT,
1685					   HID_REQ_GET_REPORT);
1686			kfree(buf);
1687		}
1688
1689		field->value[index] = td->inputmode_value;
1690		*inputmode_found = true;
1691		return true;
1692
1693	case HID_DG_CONTACTMAX:
1694		if (cls->maxcontacts) {
1695			max = min_t(int, field->logical_maximum,
1696				    cls->maxcontacts);
1697			if (field->value[index] != max) {
1698				field->value[index] = max;
1699				return true;
1700			}
1701		}
1702		break;
1703
1704	case HID_DG_LATENCYMODE:
1705		field->value[index] = latency;
1706		return true;
1707
1708	case HID_DG_SURFACESWITCH:
1709		field->value[index] = !!(report_mode & TOUCHPAD_REPORT_CONTACTS);
1710		return true;
1711
1712	case HID_DG_BUTTONSWITCH:
1713		field->value[index] = !!(report_mode & TOUCHPAD_REPORT_BUTTONS);
1714		return true;
1715	}
1716
1717	return false; /* no need to update the report */
1718}
1719
1720static void mt_set_modes(struct hid_device *hdev, enum latency_mode latency,
1721			 enum report_mode report_mode)
1722{
1723	struct hid_report_enum *rep_enum;
1724	struct hid_report *rep;
1725	struct hid_usage *usage;
1726	int i, j;
1727	bool update_report;
1728	bool inputmode_found = false;
1729
1730	rep_enum = &hdev->report_enum[HID_FEATURE_REPORT];
1731	list_for_each_entry(rep, &rep_enum->report_list, list) {
1732		update_report = false;
1733
1734		for (i = 0; i < rep->maxfield; i++) {
1735			/* Ignore if report count is out of bounds. */
1736			if (rep->field[i]->report_count < 1)
1737				continue;
1738
1739			for (j = 0; j < rep->field[i]->maxusage; j++) {
1740				usage = &rep->field[i]->usage[j];
1741
1742				if (mt_need_to_apply_feature(hdev,
1743							     rep->field[i],
1744							     usage,
1745							     latency,
1746							     report_mode,
1747							     &inputmode_found))
1748					update_report = true;
1749			}
1750		}
1751
1752		if (update_report)
1753			hid_hw_request(hdev, rep, HID_REQ_SET_REPORT);
1754	}
1755}
1756
1757static void mt_post_parse_default_settings(struct mt_device *td,
1758					   struct mt_application *app)
1759{
1760	__s32 quirks = app->quirks;
1761
1762	/* unknown serial device needs special quirks */
1763	if (list_is_singular(&app->mt_usages)) {
1764		quirks |= MT_QUIRK_ALWAYS_VALID;
1765		quirks &= ~MT_QUIRK_NOT_SEEN_MEANS_UP;
1766		quirks &= ~MT_QUIRK_VALID_IS_INRANGE;
1767		quirks &= ~MT_QUIRK_VALID_IS_CONFIDENCE;
1768		quirks &= ~MT_QUIRK_CONTACT_CNT_ACCURATE;
1769	}
1770
1771	app->quirks = quirks;
1772}
1773
1774static void mt_post_parse(struct mt_device *td, struct mt_application *app)
1775{
1776	if (!app->have_contact_count)
1777		app->quirks &= ~MT_QUIRK_CONTACT_CNT_ACCURATE;
1778}
1779
1780static int mt_input_configured(struct hid_device *hdev, struct hid_input *hi)
1781{
1782	struct mt_device *td = hid_get_drvdata(hdev);
1783	const char *suffix = NULL;
1784	struct mt_report_data *rdata;
1785	struct mt_application *mt_application = NULL;
1786	struct hid_report *report;
1787	int ret;
1788
1789	if (td->is_haptic_touchpad && (td->mtclass.name == MT_CLS_WIN_8 ||
1790	    td->mtclass.name == MT_CLS_WIN_8_FORCE_MULTI_INPUT ||
1791	    td->mtclass.name == MT_CLS_WIN_8_KEEP_LATENCY_ON_CLOSE)) {
1792		if (hid_haptic_input_configured(hdev, td->haptic, hi) == 0)
1793			td->is_haptic_touchpad = false;
1794	} else {
1795		td->is_haptic_touchpad = false;
1796	}
1797
1798	list_for_each_entry(report, &hi->reports, hidinput_list) {
1799		rdata = mt_find_report_data(td, report);
1800		if (!rdata) {
1801			hid_err(hdev, "failed to allocate data for report\n");
1802			return -ENOMEM;
1803		}
1804
1805		mt_application = rdata->application;
1806
1807		if (rdata->is_mt_collection) {
1808			ret = mt_touch_input_configured(hdev, hi,
1809							mt_application);
1810			if (ret)
1811				return ret;
1812		}
1813	}
1814
1815	switch (hi->application) {
1816	case HID_GD_KEYBOARD:
1817	case HID_GD_KEYPAD:
1818	case HID_GD_MOUSE:
1819	case HID_DG_TOUCHPAD:
1820	case HID_GD_SYSTEM_CONTROL:
1821	case HID_CP_CONSUMER_CONTROL:
1822	case HID_GD_WIRELESS_RADIO_CTLS:
1823	case HID_GD_SYSTEM_MULTIAXIS:
1824	case HID_DG_PEN:
1825		/* already handled by hid core */
1826		break;
1827	case HID_DG_TOUCHSCREEN:
1828		/* we do not set suffix = "Touchscreen" */
1829		hi->input->name = hdev->name;
1830		break;
1831	case HID_VD_ASUS_CUSTOM_MEDIA_KEYS:
1832		suffix = "Custom Media Keys";
1833		break;
1834	case HID_DG_STYLUS:
1835		/* force BTN_STYLUS to allow tablet matching in udev */
1836		__set_bit(BTN_STYLUS, hi->input->keybit);
1837		break;
1838	default:
1839		suffix = "UNKNOWN";
1840		break;
1841	}
1842
1843	/* Lenovo Yoga Book 9i requires custom naming to allow differentiation in udev */
1844	if (hi->report && td->mtclass.quirks & MT_QUIRK_YOGABOOK9I) {
1845		switch (hi->report->id) {
1846		case 48:
1847			suffix = "Touchscreen Top";
1848			break;
1849		case 56:
1850			suffix = "Touchscreen Bottom";
1851			break;
1852		case 20:
1853			suffix = "Stylus Top";
1854			break;
1855		case 40:
1856			suffix = "Stylus Bottom";
1857			break;
1858		case 80:
1859			suffix = "Emulated Touchpad";
1860			break;
1861		default:
1862			suffix = "";
1863			break;
1864		}
1865	}
1866
1867	if (suffix) {
1868		hi->input->name = devm_kasprintf(&hdev->dev, GFP_KERNEL,
1869						 "%s %s", hdev->name, suffix);
1870		if (!hi->input->name)
1871			return -ENOMEM;
1872	}
1873
1874	return 0;
1875}
1876
1877static void mt_fix_const_field(struct hid_field *field, unsigned int usage)
1878{
1879	if (field->usage[0].hid != usage ||
1880	    !(field->flags & HID_MAIN_ITEM_CONSTANT))
1881		return;
1882
1883	field->flags &= ~HID_MAIN_ITEM_CONSTANT;
1884	field->flags |= HID_MAIN_ITEM_VARIABLE;
1885}
1886
1887static void mt_fix_const_fields(struct hid_device *hdev, unsigned int usage)
1888{
1889	struct hid_report *report;
1890	int i;
1891
1892	list_for_each_entry(report,
1893			    &hdev->report_enum[HID_INPUT_REPORT].report_list,
1894			    list) {
1895
1896		if (!report->maxfield)
1897			continue;
1898
1899		for (i = 0; i < report->maxfield; i++)
1900			if (report->field[i]->maxusage >= 1)
1901				mt_fix_const_field(report->field[i], usage);
1902	}
1903}
1904
1905static void mt_release_contacts(struct hid_device *hid)
1906{
1907	struct hid_input *hidinput;
1908	struct mt_application *application;
1909	struct mt_device *td = hid_get_drvdata(hid);
1910
1911	list_for_each_entry(hidinput, &hid->inputs, list) {
1912		struct input_dev *input_dev = hidinput->input;
1913		struct input_mt *mt = input_dev->mt;
1914		int i;
1915
1916		if (mt) {
1917			for (i = 0; i < mt->num_slots; i++) {
1918				input_mt_slot(input_dev, i);
1919				input_mt_report_slot_inactive(input_dev);
1920				clear_bit(i, &td->mt_io_flags);
1921			}
1922			input_mt_sync_frame(input_dev);
1923			input_sync(input_dev);
1924		}
1925	}
1926
1927	list_for_each_entry(application, &td->applications, list) {
1928		application->num_received = 0;
1929	}
1930}
1931
1932static void mt_expired_timeout(struct timer_list *t)
1933{
1934	struct mt_device *td = timer_container_of(td, t, release_timer);
1935	struct hid_device *hdev = td->hdev;
1936
1937	/*
1938	 * An input report came in just before we release the sticky fingers,
1939	 * it will take care of the sticky fingers.
1940	 */
1941	if (test_and_set_bit_lock(MT_IO_FLAGS_RUNNING, &td->mt_io_flags))
1942		return;
1943	if (td->mt_io_flags & MT_IO_SLOTS_MASK)
1944		mt_release_contacts(hdev);
1945	clear_bit_unlock(MT_IO_FLAGS_RUNNING, &td->mt_io_flags);
1946}
1947
1948static int mt_probe(struct hid_device *hdev, const struct hid_device_id *id)
1949{
1950	int ret, i;
1951	struct mt_device *td;
1952	const struct mt_class *mtclass = mt_classes; /* MT_CLS_DEFAULT */
1953
1954	for (i = 0; mt_classes[i].name ; i++) {
1955		if (id->driver_data == mt_classes[i].name) {
1956			mtclass = &(mt_classes[i]);
1957			break;
1958		}
1959	}
1960
1961	td = devm_kzalloc(&hdev->dev, sizeof(struct mt_device), GFP_KERNEL);
1962	if (!td) {
1963		dev_err(&hdev->dev, "cannot allocate multitouch data\n");
1964		return -ENOMEM;
1965	}
1966	td->haptic = devm_kzalloc(&hdev->dev, sizeof(*(td->haptic)), GFP_KERNEL);
1967	if (!td->haptic)
1968		return -ENOMEM;
1969
1970	td->haptic->hdev = hdev;
1971	td->hdev = hdev;
1972	td->mtclass = *mtclass;
1973	td->inputmode_value = MT_INPUTMODE_TOUCHSCREEN;
1974	hid_set_drvdata(hdev, td);
1975
1976	INIT_LIST_HEAD(&td->applications);
1977	INIT_LIST_HEAD(&td->reports);
1978
1979	if (id->vendor == HID_ANY_ID && id->product == HID_ANY_ID)
1980		td->serial_maybe = true;
1981
1982
1983	/* Orientation is inverted if the X or Y axes are
1984	 * flipped, but normalized if both are inverted.
1985	 */
1986	if (hdev->quirks & (HID_QUIRK_X_INVERT | HID_QUIRK_Y_INVERT) &&
1987	    !((hdev->quirks & HID_QUIRK_X_INVERT)
1988	      && (hdev->quirks & HID_QUIRK_Y_INVERT)))
1989		td->mtclass.quirks = MT_QUIRK_ORIENTATION_INVERT;
1990
1991	/* This allows the driver to correctly support devices
1992	 * that emit events over several HID messages.
1993	 */
1994	hdev->quirks |= HID_QUIRK_NO_INPUT_SYNC;
1995
1996	/*
1997	 * This allows the driver to handle different input sensors
1998	 * that emits events through different applications on the same HID
1999	 * device.
2000	 */
2001	hdev->quirks |= HID_QUIRK_INPUT_PER_APP;
2002
2003	if (id->group != HID_GROUP_MULTITOUCH_WIN_8)
2004		hdev->quirks |= HID_QUIRK_MULTI_INPUT;
2005
2006	if (mtclass->quirks & MT_QUIRK_FORCE_MULTI_INPUT) {
2007		hdev->quirks &= ~HID_QUIRK_INPUT_PER_APP;
2008		hdev->quirks |= HID_QUIRK_MULTI_INPUT;
2009	}
2010
2011	timer_setup(&td->release_timer, mt_expired_timeout, 0);
2012
2013	ret = hid_parse(hdev);
2014	if (ret != 0)
2015		return ret;
2016
2017	if (mtclass->name == MT_CLS_APPLE_TOUCHBAR &&
2018	    !hid_find_field(hdev, HID_INPUT_REPORT,
2019			    HID_DG_TOUCHPAD, HID_DG_TRANSDUCER_INDEX))
2020		return -ENODEV;
2021
2022	if (mtclass->quirks & MT_QUIRK_FIX_CONST_CONTACT_ID)
2023		mt_fix_const_fields(hdev, HID_DG_CONTACTID);
2024
2025	if (hdev->vendor == USB_VENDOR_ID_SIS_TOUCH)
2026		hdev->quirks |= HID_QUIRK_NOGET;
2027
2028	ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
2029	if (ret)
2030		return ret;
2031
2032	ret = sysfs_create_group(&hdev->dev.kobj, &mt_attribute_group);
2033	if (ret)
2034		dev_warn(&hdev->dev, "Cannot allocate sysfs group for %s\n",
2035				hdev->name);
2036
2037	mt_set_modes(hdev, HID_LATENCY_NORMAL, TOUCHPAD_REPORT_ALL);
2038
2039	if (td->is_haptic_touchpad) {
2040		if (hid_haptic_init(hdev, &td->haptic)) {
2041			dev_warn(&hdev->dev, "Cannot allocate haptic for %s\n",
2042				 hdev->name);
2043			td->is_haptic_touchpad = false;
2044			devm_kfree(&hdev->dev, td->haptic);
2045		}
2046	} else {
2047		devm_kfree(&hdev->dev, td->haptic);
2048	}
2049
2050	return 0;
2051}
2052
2053static int mt_suspend(struct hid_device *hdev, pm_message_t state)
2054{
2055	struct mt_device *td = hid_get_drvdata(hdev);
2056
2057	/* High latency is desirable for power savings during S3/S0ix */
2058	if ((td->mtclass.quirks & MT_QUIRK_DISABLE_WAKEUP) ||
2059	    !hid_hw_may_wakeup(hdev))
2060		mt_set_modes(hdev, HID_LATENCY_HIGH, TOUCHPAD_REPORT_NONE);
2061	else
2062		mt_set_modes(hdev, HID_LATENCY_HIGH, TOUCHPAD_REPORT_ALL);
2063
2064	return 0;
2065}
2066
2067static int mt_reset_resume(struct hid_device *hdev)
2068{
2069	mt_release_contacts(hdev);
2070	mt_set_modes(hdev, HID_LATENCY_NORMAL, TOUCHPAD_REPORT_ALL);
2071	return 0;
2072}
2073
2074static int mt_resume(struct hid_device *hdev)
2075{
2076	/* Some Elan legacy devices require SET_IDLE to be set on resume.
2077	 * It should be safe to send it to other devices too.
2078	 * Tested on 3M, Stantum, Cypress, Zytronic, eGalax, and Elan panels. */
2079
2080	hid_hw_idle(hdev, 0, 0, HID_REQ_SET_IDLE);
2081
2082	mt_set_modes(hdev, HID_LATENCY_NORMAL, TOUCHPAD_REPORT_ALL);
2083
2084	return 0;
2085}
2086
2087static void mt_remove(struct hid_device *hdev)
2088{
2089	struct mt_device *td = hid_get_drvdata(hdev);
2090
2091	timer_delete_sync(&td->release_timer);
2092
2093	sysfs_remove_group(&hdev->dev.kobj, &mt_attribute_group);
2094	hid_hw_stop(hdev);
2095}
2096
2097static void mt_on_hid_hw_open(struct hid_device *hdev)
2098{
2099	mt_set_modes(hdev, HID_LATENCY_NORMAL, TOUCHPAD_REPORT_ALL);
2100}
2101
2102static void mt_on_hid_hw_close(struct hid_device *hdev)
2103{
2104	struct mt_device *td = hid_get_drvdata(hdev);
2105
2106	if (td->mtclass.quirks & MT_QUIRK_KEEP_LATENCY_ON_CLOSE)
2107		mt_set_modes(hdev, HID_LATENCY_NORMAL, TOUCHPAD_REPORT_NONE);
2108	else
2109		mt_set_modes(hdev, HID_LATENCY_HIGH, TOUCHPAD_REPORT_NONE);
2110}
2111
2112/*
2113 * This list contains only:
2114 * - VID/PID of products not working with the default multitouch handling
2115 * - 2 generic rules.
2116 * So there is no point in adding here any device with MT_CLS_DEFAULT.
2117 */
2118static const struct hid_device_id mt_devices[] = {
2119
2120	/* 3M panels */
2121	{ .driver_data = MT_CLS_3M,
2122		MT_USB_DEVICE(USB_VENDOR_ID_3M,
2123			USB_DEVICE_ID_3M1968) },
2124	{ .driver_data = MT_CLS_3M,
2125		MT_USB_DEVICE(USB_VENDOR_ID_3M,
2126			USB_DEVICE_ID_3M2256) },
2127	{ .driver_data = MT_CLS_3M,
2128		MT_USB_DEVICE(USB_VENDOR_ID_3M,
2129			USB_DEVICE_ID_3M3266) },
2130
2131	/* Anton devices */
2132	{ .driver_data = MT_CLS_EXPORT_ALL_INPUTS,
2133		MT_USB_DEVICE(USB_VENDOR_ID_ANTON,
2134			USB_DEVICE_ID_ANTON_TOUCH_PAD) },
2135
2136	/* Asus T101HA */
2137	{ .driver_data = MT_CLS_WIN_8_DISABLE_WAKEUP,
2138		HID_DEVICE(BUS_USB, HID_GROUP_MULTITOUCH_WIN_8,
2139			   USB_VENDOR_ID_ASUSTEK,
2140			   USB_DEVICE_ID_ASUSTEK_T101HA_KEYBOARD) },
2141
2142	/* Asus T304UA */
2143	{ .driver_data = MT_CLS_ASUS,
2144		HID_DEVICE(BUS_USB, HID_GROUP_MULTITOUCH_WIN_8,
2145			USB_VENDOR_ID_ASUSTEK,
2146			USB_DEVICE_ID_ASUSTEK_T304_KEYBOARD) },
2147
2148	/* Atmel panels */
2149	{ .driver_data = MT_CLS_SERIAL,
2150		MT_USB_DEVICE(USB_VENDOR_ID_ATMEL,
2151			USB_DEVICE_ID_ATMEL_MXT_DIGITIZER) },
2152
2153	/* Baanto multitouch devices */
2154	{ .driver_data = MT_CLS_NSMU,
2155		MT_USB_DEVICE(USB_VENDOR_ID_BAANTO,
2156			USB_DEVICE_ID_BAANTO_MT_190W2) },
2157
2158	/* Cando panels */
2159	{ .driver_data = MT_CLS_DUAL_INRANGE_CONTACTNUMBER,
2160		MT_USB_DEVICE(USB_VENDOR_ID_CANDO,
2161			USB_DEVICE_ID_CANDO_MULTI_TOUCH) },
2162	{ .driver_data = MT_CLS_DUAL_INRANGE_CONTACTNUMBER,
2163		MT_USB_DEVICE(USB_VENDOR_ID_CANDO,
2164			USB_DEVICE_ID_CANDO_MULTI_TOUCH_15_6) },
2165
2166	/* Chunghwa Telecom touch panels */
2167	{  .driver_data = MT_CLS_NSMU,
2168		MT_USB_DEVICE(USB_VENDOR_ID_CHUNGHWAT,
2169			USB_DEVICE_ID_CHUNGHWAT_MULTITOUCH) },
2170
2171	/* CJTouch panels */
2172	{ .driver_data = MT_CLS_NSMU,
2173		MT_USB_DEVICE(USB_VENDOR_ID_CJTOUCH,
2174			USB_DEVICE_ID_CJTOUCH_MULTI_TOUCH_0020) },
2175	{ .driver_data = MT_CLS_NSMU,
2176		MT_USB_DEVICE(USB_VENDOR_ID_CJTOUCH,
2177			USB_DEVICE_ID_CJTOUCH_MULTI_TOUCH_0040) },
2178
2179	/* CVTouch panels */
2180	{ .driver_data = MT_CLS_NSMU,
2181		MT_USB_DEVICE(USB_VENDOR_ID_CVTOUCH,
2182			USB_DEVICE_ID_CVTOUCH_SCREEN) },
2183
2184	/* eGalax devices (SAW) */
2185	{ .driver_data = MT_CLS_EXPORT_ALL_INPUTS,
2186		MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
2187			USB_DEVICE_ID_EGALAX_TOUCHCONTROLLER) },
2188
2189	/* eGalax devices (resistive) */
2190	{ .driver_data = MT_CLS_EGALAX,
2191		MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
2192			USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_480D) },
2193	{ .driver_data = MT_CLS_EGALAX,
2194		MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
2195			USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_480E) },
2196
2197	/* eGalax devices (capacitive) */
2198	{ .driver_data = MT_CLS_EGALAX_SERIAL,
2199		MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
2200			USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_7207) },
2201	{ .driver_data = MT_CLS_EGALAX,
2202		MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
2203			USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_720C) },
2204	{ .driver_data = MT_CLS_EGALAX_SERIAL,
2205		MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
2206			USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_7224) },
2207	{ .driver_data = MT_CLS_EGALAX_SERIAL,
2208		MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
2209			USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_722A) },
2210	{ .driver_data = MT_CLS_EGALAX_SERIAL,
2211		MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
2212			USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_725E) },
2213	{ .driver_data = MT_CLS_EGALAX_SERIAL,
2214		MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
2215			USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_7262) },
2216	{ .driver_data = MT_CLS_EGALAX,
2217		MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
2218			USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_726B) },
2219	{ .driver_data = MT_CLS_EGALAX,
2220		MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
2221			USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_72A1) },
2222	{ .driver_data = MT_CLS_EGALAX_SERIAL,
2223		MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
2224			USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_72AA) },
2225	{ .driver_data = MT_CLS_EGALAX,
2226		HID_USB_DEVICE(USB_VENDOR_ID_DWAV,
2227			USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_72C4) },
2228	{ .driver_data = MT_CLS_EGALAX,
2229		HID_USB_DEVICE(USB_VENDOR_ID_DWAV,
2230			USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_72D0) },
2231	{ .driver_data = MT_CLS_EGALAX,
2232		MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
2233			USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_72FA) },
2234	{ .driver_data = MT_CLS_EGALAX,
2235		MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
2236			USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_7302) },
2237	{ .driver_data = MT_CLS_EGALAX_SERIAL,
2238		MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
2239			USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_7349) },
2240	{ .driver_data = MT_CLS_EGALAX_SERIAL,
2241		MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
2242			USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_73F7) },
2243	{ .driver_data = MT_CLS_EGALAX_SERIAL,
2244		MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
2245			USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_A001) },
2246	{ .driver_data = MT_CLS_EGALAX_SERIAL,
2247		MT_USB_DEVICE(USB_VENDOR_ID_DWAV,
2248			USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_C000) },
2249	{ .driver_data = MT_CLS_EGALAX_P80H84,
2250		HID_DEVICE(HID_BUS_ANY, HID_GROUP_MULTITOUCH_WIN_8,
2251			USB_VENDOR_ID_DWAV,
2252			USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_C002) },
2253
2254	/* Elan devices */
2255	{ .driver_data = MT_CLS_WIN_8_FORCE_MULTI_INPUT,
2256		HID_DEVICE(BUS_I2C, HID_GROUP_MULTITOUCH_WIN_8,
2257			USB_VENDOR_ID_ELAN, 0x313a) },
2258
2259	{ .driver_data = MT_CLS_WIN_8_FORCE_MULTI_INPUT,
2260		HID_DEVICE(BUS_I2C, HID_GROUP_MULTITOUCH_WIN_8,
2261			USB_VENDOR_ID_ELAN, 0x3148) },
2262
2263	{ .driver_data = MT_CLS_WIN_8_FORCE_MULTI_INPUT_NSMU,
2264		HID_DEVICE(BUS_I2C, HID_GROUP_MULTITOUCH_WIN_8,
2265			USB_VENDOR_ID_ELAN, 0x32ae) },
2266
2267	/* Elitegroup panel */
2268	{ .driver_data = MT_CLS_SERIAL,
2269		MT_USB_DEVICE(USB_VENDOR_ID_ELITEGROUP,
2270			USB_DEVICE_ID_ELITEGROUP_05D8) },
2271
2272	/* Flatfrog Panels */
2273	{ .driver_data = MT_CLS_FLATFROG,
2274		MT_USB_DEVICE(USB_VENDOR_ID_FLATFROG,
2275			USB_DEVICE_ID_MULTITOUCH_3200) },
2276
2277	/* FocalTech Panels */
2278	{ .driver_data = MT_CLS_SERIAL,
2279		MT_USB_DEVICE(USB_VENDOR_ID_CYGNAL,
2280			USB_DEVICE_ID_FOCALTECH_FTXXXX_MULTITOUCH) },
2281
2282	/* GeneralTouch panel */
2283	{ .driver_data = MT_CLS_GENERALTOUCH_TWOFINGERS,
2284		MT_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH,
2285			USB_DEVICE_ID_GENERAL_TOUCH_WIN7_TWOFINGERS) },
2286	{ .driver_data = MT_CLS_GENERALTOUCH_PWT_TENFINGERS,
2287		MT_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH,
2288			USB_DEVICE_ID_GENERAL_TOUCH_WIN8_PWT_TENFINGERS) },
2289	{ .driver_data = MT_CLS_GENERALTOUCH_TWOFINGERS,
2290		MT_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH,
2291			USB_DEVICE_ID_GENERAL_TOUCH_WIN8_PIT_0101) },
2292	{ .driver_data = MT_CLS_GENERALTOUCH_PWT_TENFINGERS,
2293		MT_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH,
2294			USB_DEVICE_ID_GENERAL_TOUCH_WIN8_PIT_0102) },
2295	{ .driver_data = MT_CLS_GENERALTOUCH_PWT_TENFINGERS,
2296		MT_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH,
2297			USB_DEVICE_ID_GENERAL_TOUCH_WIN8_PIT_0106) },
2298	{ .driver_data = MT_CLS_GENERALTOUCH_PWT_TENFINGERS,
2299		MT_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH,
2300			USB_DEVICE_ID_GENERAL_TOUCH_WIN8_PIT_010A) },
2301	{ .driver_data = MT_CLS_GENERALTOUCH_PWT_TENFINGERS,
2302		MT_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH,
2303			USB_DEVICE_ID_GENERAL_TOUCH_WIN8_PIT_E100) },
2304
2305	/* Gametel game controller */
2306	{ .driver_data = MT_CLS_NSMU,
2307		MT_BT_DEVICE(USB_VENDOR_ID_FRUCTEL,
2308			USB_DEVICE_ID_GAMETEL_MT_MODE) },
2309
2310	/* Goodix GT7868Q devices */
2311	{ .driver_data = MT_CLS_WIN_8_FORCE_MULTI_INPUT_NSMU,
2312	  HID_DEVICE(BUS_I2C, HID_GROUP_ANY, I2C_VENDOR_ID_GOODIX,
2313		     I2C_DEVICE_ID_GOODIX_01E8) },
2314	{ .driver_data = MT_CLS_WIN_8_FORCE_MULTI_INPUT_NSMU,
2315	  HID_DEVICE(BUS_I2C, HID_GROUP_ANY, I2C_VENDOR_ID_GOODIX,
2316		     I2C_DEVICE_ID_GOODIX_01E9) },
2317
2318	/* GoodTouch panels */
2319	{ .driver_data = MT_CLS_NSMU,
2320		MT_USB_DEVICE(USB_VENDOR_ID_GOODTOUCH,
2321			USB_DEVICE_ID_GOODTOUCH_000f) },
2322
2323	/* Hanvon panels */
2324	{ .driver_data = MT_CLS_DUAL_INRANGE_CONTACTID,
2325		MT_USB_DEVICE(USB_VENDOR_ID_HANVON_ALT,
2326			USB_DEVICE_ID_HANVON_ALT_MULTITOUCH) },
2327
2328	/* HONOR GLO-GXXX panel */
2329	{ .driver_data = MT_CLS_VTL,
2330		HID_DEVICE(BUS_I2C, HID_GROUP_MULTITOUCH_WIN_8,
2331			0x347d, 0x7853) },
2332
2333	/* HONOR MagicBook Art 14 touchpad */
2334	{ .driver_data = MT_CLS_VTL,
2335		HID_DEVICE(BUS_I2C, HID_GROUP_MULTITOUCH_WIN_8,
2336			0x35cc, 0x0104) },
2337
2338	/* Ilitek dual touch panel */
2339	{  .driver_data = MT_CLS_NSMU,
2340		MT_USB_DEVICE(USB_VENDOR_ID_ILITEK,
2341			USB_DEVICE_ID_ILITEK_MULTITOUCH) },
2342
2343	/* LG Melfas panel */
2344	{ .driver_data = MT_CLS_LG,
2345		HID_USB_DEVICE(USB_VENDOR_ID_LG,
2346			USB_DEVICE_ID_LG_MELFAS_MT) },
2347	{ .driver_data = MT_CLS_LG,
2348		HID_DEVICE(BUS_I2C, HID_GROUP_GENERIC,
2349			USB_VENDOR_ID_LG, I2C_DEVICE_ID_LG_7010) },
2350
2351	/* Lenovo X1 TAB Gen 1 */
2352	{ .driver_data = MT_CLS_WIN_8_FORCE_MULTI_INPUT,
2353		HID_DEVICE(BUS_USB, HID_GROUP_MULTITOUCH_WIN_8,
2354			   USB_VENDOR_ID_LENOVO,
2355			   USB_DEVICE_ID_LENOVO_X1_TAB) },
2356
2357	/* Lenovo X1 TAB Gen 2 */
2358	{ .driver_data = MT_CLS_WIN_8_FORCE_MULTI_INPUT,
2359		HID_DEVICE(BUS_USB, HID_GROUP_MULTITOUCH_WIN_8,
2360			   USB_VENDOR_ID_LENOVO,
2361			   USB_DEVICE_ID_LENOVO_X1_TAB2) },
2362
2363	/* Lenovo X1 TAB Gen 3 */
2364	{ .driver_data = MT_CLS_WIN_8_FORCE_MULTI_INPUT,
2365		HID_DEVICE(BUS_USB, HID_GROUP_MULTITOUCH_WIN_8,
2366			   USB_VENDOR_ID_LENOVO,
2367			   USB_DEVICE_ID_LENOVO_X1_TAB3) },
2368
2369	/* Lenovo X12 TAB Gen 1 */
2370	{ .driver_data = MT_CLS_WIN_8_FORCE_MULTI_INPUT_NSMU,
2371		HID_DEVICE(BUS_USB, HID_GROUP_MULTITOUCH_WIN_8,
2372			   USB_VENDOR_ID_LENOVO,
2373			   USB_DEVICE_ID_LENOVO_X12_TAB) },
2374
2375	/* Lenovo X12 TAB Gen 2 */
2376	{ .driver_data = MT_CLS_WIN_8_FORCE_MULTI_INPUT_NSMU,
2377		HID_DEVICE(BUS_USB, HID_GROUP_MULTITOUCH_WIN_8,
2378			   USB_VENDOR_ID_LENOVO,
2379			   USB_DEVICE_ID_LENOVO_X12_TAB2) },
2380
2381	/* Lenovo Yoga Book 9i */
2382	{ .driver_data = MT_CLS_YOGABOOK9I,
2383		HID_DEVICE(BUS_USB, HID_GROUP_MULTITOUCH_WIN_8,
2384			   USB_VENDOR_ID_LENOVO,
2385			   USB_DEVICE_ID_LENOVO_YOGABOOK9I) },
2386
2387	/* Logitech devices */
2388	{ .driver_data = MT_CLS_NSMU,
2389		HID_DEVICE(BUS_BLUETOOTH, HID_GROUP_MULTITOUCH_WIN_8,
2390			USB_VENDOR_ID_LOGITECH,
2391			USB_DEVICE_ID_LOGITECH_CASA_TOUCHPAD) },
2392	{ .driver_data = MT_CLS_WIN_8_FORCE_MULTI_INPUT_NSMU,
2393		HID_DEVICE(BUS_USB, HID_GROUP_MULTITOUCH_WIN_8,
2394			USB_VENDOR_ID_LOGITECH,
2395			USB_DEVICE_ID_LOGITECH_BOLT_RECEIVER) },
2396
2397	/* MosArt panels */
2398	{ .driver_data = MT_CLS_CONFIDENCE_MINUS_ONE,
2399		MT_USB_DEVICE(USB_VENDOR_ID_ASUS,
2400			USB_DEVICE_ID_ASUS_T91MT)},
2401	{ .driver_data = MT_CLS_CONFIDENCE_MINUS_ONE,
2402		MT_USB_DEVICE(USB_VENDOR_ID_ASUS,
2403			USB_DEVICE_ID_ASUSTEK_MULTITOUCH_YFO) },
2404	{ .driver_data = MT_CLS_CONFIDENCE_MINUS_ONE,
2405		MT_USB_DEVICE(USB_VENDOR_ID_TURBOX,
2406			USB_DEVICE_ID_TURBOX_TOUCHSCREEN_MOSART) },
2407
2408	/* Novatek Panel */
2409	{ .driver_data = MT_CLS_NSMU,
2410		MT_USB_DEVICE(USB_VENDOR_ID_NOVATEK,
2411			USB_DEVICE_ID_NOVATEK_PCT) },
2412
2413	/* Ntrig Panel */
2414	{ .driver_data = MT_CLS_NSMU,
2415		HID_DEVICE(BUS_I2C, HID_GROUP_MULTITOUCH_WIN_8,
2416			USB_VENDOR_ID_NTRIG, 0x1b05) },
2417
2418	/* Panasonic panels */
2419	{ .driver_data = MT_CLS_PANASONIC,
2420		MT_USB_DEVICE(USB_VENDOR_ID_PANASONIC,
2421			USB_DEVICE_ID_PANABOARD_UBT780) },
2422	{ .driver_data = MT_CLS_PANASONIC,
2423		MT_USB_DEVICE(USB_VENDOR_ID_PANASONIC,
2424			USB_DEVICE_ID_PANABOARD_UBT880) },
2425
2426	/* PixArt optical touch screen */
2427	{ .driver_data = MT_CLS_INRANGE_CONTACTNUMBER,
2428		MT_USB_DEVICE(USB_VENDOR_ID_PIXART,
2429			USB_DEVICE_ID_PIXART_OPTICAL_TOUCH_SCREEN) },
2430	{ .driver_data = MT_CLS_INRANGE_CONTACTNUMBER,
2431		MT_USB_DEVICE(USB_VENDOR_ID_PIXART,
2432			USB_DEVICE_ID_PIXART_OPTICAL_TOUCH_SCREEN1) },
2433	{ .driver_data = MT_CLS_INRANGE_CONTACTNUMBER,
2434		MT_USB_DEVICE(USB_VENDOR_ID_PIXART,
2435			USB_DEVICE_ID_PIXART_OPTICAL_TOUCH_SCREEN2) },
2436
2437	/* PixCir-based panels */
2438	{ .driver_data = MT_CLS_DUAL_INRANGE_CONTACTID,
2439		MT_USB_DEVICE(USB_VENDOR_ID_CANDO,
2440			USB_DEVICE_ID_CANDO_PIXCIR_MULTI_TOUCH) },
2441
2442	/* Quanta-based panels */
2443	{ .driver_data = MT_CLS_CONFIDENCE_CONTACT_ID,
2444		MT_USB_DEVICE(USB_VENDOR_ID_QUANTA,
2445			USB_DEVICE_ID_QUANTA_OPTICAL_TOUCH_3001) },
2446
2447	/* Razer touchpads */
2448	{ .driver_data = MT_CLS_RAZER_BLADE_STEALTH,
2449		HID_DEVICE(BUS_I2C, HID_GROUP_MULTITOUCH_WIN_8,
2450			USB_VENDOR_ID_SYNAPTICS, 0x8323) },
2451
2452	/* Smart Tech panels */
2453	{ .driver_data = MT_CLS_SMART_TECH,
2454		MT_USB_DEVICE(0x0b8c, 0x0092)},
2455
2456	/* Stantum panels */
2457	{ .driver_data = MT_CLS_CONFIDENCE,
2458		MT_USB_DEVICE(USB_VENDOR_ID_STANTUM_STM,
2459			USB_DEVICE_ID_MTP_STM)},
2460
2461	/* Synaptics devices */
2462	{ .driver_data = MT_CLS_WIN_8_FORCE_MULTI_INPUT,
2463		HID_DEVICE(BUS_I2C, HID_GROUP_MULTITOUCH_WIN_8,
2464			USB_VENDOR_ID_SYNAPTICS, 0xcd7e) },
2465
2466	{ .driver_data = MT_CLS_WIN_8_FORCE_MULTI_INPUT,
2467		HID_DEVICE(BUS_I2C, HID_GROUP_MULTITOUCH_WIN_8,
2468			USB_VENDOR_ID_SYNAPTICS, 0xcddc) },
2469
2470	{ .driver_data = MT_CLS_WIN_8_FORCE_MULTI_INPUT,
2471		HID_DEVICE(BUS_I2C, HID_GROUP_MULTITOUCH_WIN_8,
2472			USB_VENDOR_ID_SYNAPTICS, 0xce08) },
2473
2474	{ .driver_data = MT_CLS_WIN_8_FORCE_MULTI_INPUT,
2475		HID_DEVICE(BUS_I2C, HID_GROUP_MULTITOUCH_WIN_8,
2476			USB_VENDOR_ID_SYNAPTICS, 0xce09) },
2477
2478	/* TopSeed panels */
2479	{ .driver_data = MT_CLS_TOPSEED,
2480		MT_USB_DEVICE(USB_VENDOR_ID_TOPSEED2,
2481			USB_DEVICE_ID_TOPSEED2_PERIPAD_701) },
2482
2483	/* Touch International panels */
2484	{ .driver_data = MT_CLS_NSMU,
2485		MT_USB_DEVICE(USB_VENDOR_ID_TOUCH_INTL,
2486			USB_DEVICE_ID_TOUCH_INTL_MULTI_TOUCH) },
2487
2488	/* Unitec panels */
2489	{ .driver_data = MT_CLS_NSMU,
2490		MT_USB_DEVICE(USB_VENDOR_ID_UNITEC,
2491			USB_DEVICE_ID_UNITEC_USB_TOUCH_0709) },
2492	{ .driver_data = MT_CLS_NSMU,
2493		MT_USB_DEVICE(USB_VENDOR_ID_UNITEC,
2494			USB_DEVICE_ID_UNITEC_USB_TOUCH_0A19) },
2495
2496	/* Uniwill touchpads */
2497	{ .driver_data = MT_CLS_WIN_8_KEEP_LATENCY_ON_CLOSE,
2498		HID_DEVICE(BUS_I2C, HID_GROUP_MULTITOUCH_WIN_8,
2499			USB_VENDOR_ID_PIXART, 0x0255) },
2500	{ .driver_data = MT_CLS_WIN_8_KEEP_LATENCY_ON_CLOSE,
2501		HID_DEVICE(BUS_I2C, HID_GROUP_MULTITOUCH_WIN_8,
2502			USB_VENDOR_ID_PIXART, 0x0274) },
2503
2504	/* VTL panels */
2505	{ .driver_data = MT_CLS_VTL,
2506		MT_USB_DEVICE(USB_VENDOR_ID_VTL,
2507			USB_DEVICE_ID_VTL_MULTITOUCH_FF3F) },
2508
2509	/* Winbond Electronics Corp. */
2510	{ .driver_data = MT_CLS_WIN_8_NO_STICKY_FINGERS,
2511		HID_DEVICE(HID_BUS_ANY, HID_GROUP_MULTITOUCH_WIN_8,
2512			   USB_VENDOR_ID_WINBOND, USB_DEVICE_ID_TSTP_MTOUCH) },
2513
2514	/* Wistron panels */
2515	{ .driver_data = MT_CLS_NSMU,
2516		MT_USB_DEVICE(USB_VENDOR_ID_WISTRON,
2517			USB_DEVICE_ID_WISTRON_OPTICAL_TOUCH) },
2518
2519	/* XAT */
2520	{ .driver_data = MT_CLS_NSMU,
2521		MT_USB_DEVICE(USB_VENDOR_ID_XAT,
2522			USB_DEVICE_ID_XAT_CSR) },
2523
2524	/* Xiroku */
2525	{ .driver_data = MT_CLS_NSMU,
2526		MT_USB_DEVICE(USB_VENDOR_ID_XIROKU,
2527			USB_DEVICE_ID_XIROKU_SPX) },
2528	{ .driver_data = MT_CLS_NSMU,
2529		MT_USB_DEVICE(USB_VENDOR_ID_XIROKU,
2530			USB_DEVICE_ID_XIROKU_MPX) },
2531	{ .driver_data = MT_CLS_NSMU,
2532		MT_USB_DEVICE(USB_VENDOR_ID_XIROKU,
2533			USB_DEVICE_ID_XIROKU_CSR) },
2534	{ .driver_data = MT_CLS_NSMU,
2535		MT_USB_DEVICE(USB_VENDOR_ID_XIROKU,
2536			USB_DEVICE_ID_XIROKU_SPX1) },
2537	{ .driver_data = MT_CLS_NSMU,
2538		MT_USB_DEVICE(USB_VENDOR_ID_XIROKU,
2539			USB_DEVICE_ID_XIROKU_MPX1) },
2540	{ .driver_data = MT_CLS_NSMU,
2541		MT_USB_DEVICE(USB_VENDOR_ID_XIROKU,
2542			USB_DEVICE_ID_XIROKU_CSR1) },
2543	{ .driver_data = MT_CLS_NSMU,
2544		MT_USB_DEVICE(USB_VENDOR_ID_XIROKU,
2545			USB_DEVICE_ID_XIROKU_SPX2) },
2546	{ .driver_data = MT_CLS_NSMU,
2547		MT_USB_DEVICE(USB_VENDOR_ID_XIROKU,
2548			USB_DEVICE_ID_XIROKU_MPX2) },
2549	{ .driver_data = MT_CLS_NSMU,
2550		MT_USB_DEVICE(USB_VENDOR_ID_XIROKU,
2551			USB_DEVICE_ID_XIROKU_CSR2) },
2552
2553	/* Apple Touch Bar */
2554	{ .driver_data = MT_CLS_APPLE_TOUCHBAR,
2555		HID_USB_DEVICE(USB_VENDOR_ID_APPLE,
2556			USB_DEVICE_ID_APPLE_TOUCHBAR_DISPLAY) },
2557
2558	/* Google MT devices */
2559	{ .driver_data = MT_CLS_GOOGLE,
2560		HID_DEVICE(HID_BUS_ANY, HID_GROUP_ANY, USB_VENDOR_ID_GOOGLE,
2561			USB_DEVICE_ID_GOOGLE_TOUCH_ROSE) },
2562	{ .driver_data = MT_CLS_GOOGLE,
2563		HID_DEVICE(BUS_USB, HID_GROUP_MULTITOUCH_WIN_8, USB_VENDOR_ID_GOOGLE,
2564			USB_DEVICE_ID_GOOGLE_WHISKERS) },
2565
2566	/* sis */
2567	{ .driver_data = MT_CLS_SIS,
2568		HID_DEVICE(HID_BUS_ANY, HID_GROUP_ANY, USB_VENDOR_ID_SIS_TOUCH,
2569			HID_ANY_ID) },
2570
2571	/* Hantick */
2572	{ .driver_data = MT_CLS_NSMU,
2573		HID_DEVICE(BUS_I2C, HID_GROUP_MULTITOUCH_WIN_8,
2574			   I2C_VENDOR_ID_HANTICK, I2C_PRODUCT_ID_HANTICK_5288) },
2575
2576	/* Generic MT device */
2577	{ HID_DEVICE(HID_BUS_ANY, HID_GROUP_MULTITOUCH, HID_ANY_ID, HID_ANY_ID) },
2578
2579	/* Generic Win 8 certified MT device */
2580	{  .driver_data = MT_CLS_WIN_8,
2581		HID_DEVICE(HID_BUS_ANY, HID_GROUP_MULTITOUCH_WIN_8,
2582			HID_ANY_ID, HID_ANY_ID) },
2583	{ }
2584};
2585MODULE_DEVICE_TABLE(hid, mt_devices);
2586
2587static const struct hid_usage_id mt_grabbed_usages[] = {
2588	{ HID_ANY_ID, HID_ANY_ID, HID_ANY_ID },
2589	{ HID_ANY_ID - 1, HID_ANY_ID - 1, HID_ANY_ID - 1}
2590};
2591
2592static struct hid_driver mt_driver = {
2593	.name = "hid-multitouch",
2594	.id_table = mt_devices,
2595	.probe = mt_probe,
2596	.remove = mt_remove,
2597	.input_mapping = mt_input_mapping,
2598	.input_mapped = mt_input_mapped,
2599	.input_configured = mt_input_configured,
2600	.feature_mapping = mt_feature_mapping,
2601	.usage_table = mt_grabbed_usages,
2602	.event = mt_event,
2603	.report_fixup = mt_report_fixup,
2604	.report = mt_report,
2605	.suspend = pm_ptr(mt_suspend),
2606	.reset_resume = pm_ptr(mt_reset_resume),
2607	.resume = pm_ptr(mt_resume),
2608	.on_hid_hw_open = mt_on_hid_hw_open,
2609	.on_hid_hw_close = mt_on_hid_hw_close,
2610};
2611module_hid_driver(mt_driver);
~/ Read-only file preview.