hid-multitouch.c 58.5 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-or-later
2 3 4
/*
 *  HID driver for multitouch panels
 *
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 11 12 13 14 15
 *  This code is partly based on hid-egalax.c:
 *
 *  Copyright (c) 2010 Stephane Chatty <chatty@enac.fr>
 *  Copyright (c) 2010 Henrik Rydberg <rydberg@euromail.se>
 *  Copyright (c) 2010 Canonical, Ltd.
 *
16 17 18 19 20
 *  This code is partly based on hid-3m-pct.c:
 *
 *  Copyright (c) 2009-2010 Stephane Chatty <chatty@enac.fr>
 *  Copyright (c) 2010      Henrik Rydberg <rydberg@euromail.se>
 *  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 29 30
 * Please run these regression tests before patching this module so that
 * your patch won't break existing known devices.
 *
31
 * [1] https://gitlab.freedesktop.org/libevdev/hid-tools
32 33
 */

34 35 36 37 38
#include <linux/device.h>
#include <linux/hid.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/input/mt.h>
39
#include <linux/jiffies.h>
40
#include <linux/string.h>
41
#include <linux/timer.h>
42 43 44


MODULE_AUTHOR("Stephane Chatty <chatty@enac.fr>");
45
MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
46 47 48 49 50 51
MODULE_DESCRIPTION("HID multitouch panels");
MODULE_LICENSE("GPL");

#include "hid-ids.h"

/* quirks to control the device */
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
#define MT_QUIRK_NOT_SEEN_MEANS_UP	BIT(0)
#define MT_QUIRK_SLOT_IS_CONTACTID	BIT(1)
#define MT_QUIRK_CYPRESS		BIT(2)
#define MT_QUIRK_SLOT_IS_CONTACTNUMBER	BIT(3)
#define MT_QUIRK_ALWAYS_VALID		BIT(4)
#define MT_QUIRK_VALID_IS_INRANGE	BIT(5)
#define MT_QUIRK_VALID_IS_CONFIDENCE	BIT(6)
#define MT_QUIRK_CONFIDENCE		BIT(7)
#define MT_QUIRK_SLOT_IS_CONTACTID_MINUS_ONE	BIT(8)
#define MT_QUIRK_NO_AREA		BIT(9)
#define MT_QUIRK_IGNORE_DUPLICATES	BIT(10)
#define MT_QUIRK_HOVERING		BIT(11)
#define MT_QUIRK_CONTACT_CNT_ACCURATE	BIT(12)
#define MT_QUIRK_FORCE_GET_FEATURE	BIT(13)
#define MT_QUIRK_FIX_CONST_CONTACT_ID	BIT(14)
#define MT_QUIRK_TOUCH_SIZE_SCALING	BIT(15)
68
#define MT_QUIRK_STICKY_FINGERS		BIT(16)
69
#define MT_QUIRK_ASUS_CUSTOM_UP		BIT(17)
70
#define MT_QUIRK_WIN8_PTP_BUTTONS	BIT(18)
71

72 73 74
#define MT_INPUTMODE_TOUCHSCREEN	0x02
#define MT_INPUTMODE_TOUCHPAD		0x03

75 76
#define MT_BUTTONTYPE_CLICKPAD		0

77 78 79 80 81
enum latency_mode {
	HID_LATENCY_NORMAL = 0,
	HID_LATENCY_HIGH = 1,
};

82
#define MT_IO_FLAGS_RUNNING		0
83 84
#define MT_IO_FLAGS_ACTIVE_SLOTS	1
#define MT_IO_FLAGS_PENDING_SLOTS	2
85

86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
static const bool mtrue = true;		/* default for true */
static const bool mfalse;		/* default for false */
static const __s32 mzero;		/* default for 0 */

#define DEFAULT_TRUE	((void *)&mtrue)
#define DEFAULT_FALSE	((void *)&mfalse)
#define DEFAULT_ZERO	((void *)&mzero)

struct mt_usages {
	struct list_head list;
	__s32 *x, *y, *cx, *cy, *p, *w, *h, *a;
	__s32 *contactid;	/* the device ContactID assigned to this slot */
	bool *tip_state;	/* is the touch valid? */
	bool *inrange_state;	/* is the finger in proximity of the sensor? */
	bool *confidence_state;	/* is the touch made by a finger? */
101 102
};

103 104 105
struct mt_application {
	struct list_head list;
	unsigned int application;
106
	struct list_head mt_usages;	/* mt usages list */
107 108 109

	__s32 quirks;

110 111
	__s32 *scantime;		/* scantime reported */
	__s32 scantime_logical_max;	/* max value for raw scantime */
112

113
	__s32 *raw_cc;			/* contact count in the report */
114 115 116
	int left_button_state;		/* left button state */
	unsigned int mt_flags;		/* flags to pass to input-mt */

117 118 119
	unsigned long *pending_palm_slots;	/* slots where we reported palm
						 * and need to release */

120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
	__u8 num_received;	/* how many contacts we received */
	__u8 num_expected;	/* expected last contact index */
	__u8 buttons_count;	/* number of physical buttons per touchpad */
	__u8 touches_by_report;	/* how many touches are present in one report:
				 * 1 means we should use a serial protocol
				 * > 1 means hybrid (multitouch) protocol
				 */

	__s32 dev_time;		/* the scan time provided by the device */
	unsigned long jiffies;	/* the frame's jiffies */
	int timestamp;		/* the timestamp to be sent */
	int prev_scantime;		/* scantime reported previously */

	bool have_contact_count;
};

136 137 138 139 140 141 142 143
struct mt_class {
	__s32 name;	/* MT_CLS */
	__s32 quirks;
	__s32 sn_move;	/* Signal/noise ratio for move events */
	__s32 sn_width;	/* Signal/noise ratio for width events */
	__s32 sn_height;	/* Signal/noise ratio for height events */
	__s32 sn_pressure;	/* Signal/noise ratio for pressure events */
	__u8 maxcontacts;
144
	bool is_indirect;	/* true for touchpads */
145
	bool export_all_inputs;	/* do not ignore mouse, keyboards, etc... */
146 147
};

148 149 150 151 152 153 154
struct mt_report_data {
	struct list_head list;
	struct hid_report *report;
	struct mt_application *application;
	bool is_mt_collection;
};

155
struct mt_device {
156
	struct mt_class mtclass;	/* our mt device class */
157
	struct timer_list release_timer;	/* to release sticky fingers */
158
	struct hid_device *hdev;	/* hid_device we're attached to */
159
	unsigned long mt_io_flags;	/* mt flags (MT_IO_FLAGS_*) */
160
	__u8 inputmode_value;	/* InputMode HID feature value */
161
	__u8 maxcontacts;
162
	bool is_buttonpad;	/* is this device a button pad? */
163
	bool serial_maybe;	/* need to check for serial protocol */
164 165

	struct list_head applications;
166
	struct list_head reports;
167 168
};

169 170 171
static void mt_post_parse_default_settings(struct mt_device *td,
					   struct mt_application *app);
static void mt_post_parse(struct mt_device *td, struct mt_application *app);
172

173
/* classes of device behavior */
174 175
#define MT_CLS_DEFAULT				0x0001

176 177
#define MT_CLS_SERIAL				0x0002
#define MT_CLS_CONFIDENCE			0x0003
178 179 180 181
#define MT_CLS_CONFIDENCE_CONTACT_ID		0x0004
#define MT_CLS_CONFIDENCE_MINUS_ONE		0x0005
#define MT_CLS_DUAL_INRANGE_CONTACTID		0x0006
#define MT_CLS_DUAL_INRANGE_CONTACTNUMBER	0x0007
182
/* reserved					0x0008 */
183
#define MT_CLS_INRANGE_CONTACTNUMBER		0x0009
184
#define MT_CLS_NSMU				0x000a
185 186
/* reserved					0x0010 */
/* reserved					0x0011 */
187
#define MT_CLS_WIN_8				0x0012
188
#define MT_CLS_EXPORT_ALL_INPUTS		0x0013
189
#define MT_CLS_WIN_8_DUAL			0x0014
190 191 192

/* vendor specific classes */
#define MT_CLS_3M				0x0101
193
/* reserved					0x0102 */
194
#define MT_CLS_EGALAX				0x0103
195
#define MT_CLS_EGALAX_SERIAL			0x0104
196
#define MT_CLS_TOPSEED				0x0105
197
#define MT_CLS_PANASONIC			0x0106
198
#define MT_CLS_FLATFROG				0x0107
199 200
#define MT_CLS_GENERALTOUCH_TWOFINGERS		0x0108
#define MT_CLS_GENERALTOUCH_PWT_TENFINGERS	0x0109
201
#define MT_CLS_LG				0x010a
202
#define MT_CLS_ASUS				0x010b
203
#define MT_CLS_VTL				0x0110
204
#define MT_CLS_GOOGLE				0x0111
205
#define MT_CLS_RAZER_BLADE_STEALTH		0x0112
206

207
#define MT_DEFAULT_MAXCONTACT	10
208
#define MT_MAX_MAXCONTACT	250
209

210 211 212 213 214 215
/*
 * Resync device and local timestamps after that many microseconds without
 * receiving data.
 */
#define MAX_TIMESTAMP_INTERVAL	1000000

216 217 218
#define MT_USB_DEVICE(v, p)	HID_DEVICE(BUS_USB, HID_GROUP_MULTITOUCH, v, p)
#define MT_BT_DEVICE(v, p)	HID_DEVICE(BUS_BLUETOOTH, HID_GROUP_MULTITOUCH, v, p)

219 220 221 222 223
/*
 * these device-dependent functions determine what slot corresponds
 * to a valid contact that was just read.
 */

224 225
static int cypress_compute_slot(struct mt_application *application,
				struct mt_usages *slot)
226
{
227 228
	if (*slot->contactid != 0 || application->num_received == 0)
		return *slot->contactid;
229 230 231 232
	else
		return -1;
}

233
static const struct mt_class mt_classes[] = {
234
	{ .name = MT_CLS_DEFAULT,
235 236 237
		.quirks = MT_QUIRK_ALWAYS_VALID |
			MT_QUIRK_CONTACT_CNT_ACCURATE },
	{ .name = MT_CLS_NSMU,
238
		.quirks = MT_QUIRK_NOT_SEEN_MEANS_UP },
239 240
	{ .name = MT_CLS_SERIAL,
		.quirks = MT_QUIRK_ALWAYS_VALID},
241 242
	{ .name = MT_CLS_CONFIDENCE,
		.quirks = MT_QUIRK_VALID_IS_CONFIDENCE },
243 244 245
	{ .name = MT_CLS_CONFIDENCE_CONTACT_ID,
		.quirks = MT_QUIRK_VALID_IS_CONFIDENCE |
			MT_QUIRK_SLOT_IS_CONTACTID },
246 247 248
	{ .name = MT_CLS_CONFIDENCE_MINUS_ONE,
		.quirks = MT_QUIRK_VALID_IS_CONFIDENCE |
			MT_QUIRK_SLOT_IS_CONTACTID_MINUS_ONE },
249
	{ .name = MT_CLS_DUAL_INRANGE_CONTACTID,
250 251 252
		.quirks = MT_QUIRK_VALID_IS_INRANGE |
			MT_QUIRK_SLOT_IS_CONTACTID,
		.maxcontacts = 2 },
253
	{ .name = MT_CLS_DUAL_INRANGE_CONTACTNUMBER,
254 255 256
		.quirks = MT_QUIRK_VALID_IS_INRANGE |
			MT_QUIRK_SLOT_IS_CONTACTNUMBER,
		.maxcontacts = 2 },
257 258 259
	{ .name = MT_CLS_INRANGE_CONTACTNUMBER,
		.quirks = MT_QUIRK_VALID_IS_INRANGE |
			MT_QUIRK_SLOT_IS_CONTACTNUMBER },
260 261 262 263
	{ .name = MT_CLS_WIN_8,
		.quirks = MT_QUIRK_ALWAYS_VALID |
			MT_QUIRK_IGNORE_DUPLICATES |
			MT_QUIRK_HOVERING |
264
			MT_QUIRK_CONTACT_CNT_ACCURATE |
265 266
			MT_QUIRK_STICKY_FINGERS |
			MT_QUIRK_WIN8_PTP_BUTTONS },
267 268 269 270
	{ .name = MT_CLS_EXPORT_ALL_INPUTS,
		.quirks = MT_QUIRK_ALWAYS_VALID |
			MT_QUIRK_CONTACT_CNT_ACCURATE,
		.export_all_inputs = true },
271 272 273 274
	{ .name = MT_CLS_WIN_8_DUAL,
		.quirks = MT_QUIRK_ALWAYS_VALID |
			MT_QUIRK_IGNORE_DUPLICATES |
			MT_QUIRK_HOVERING |
275 276
			MT_QUIRK_CONTACT_CNT_ACCURATE |
			MT_QUIRK_WIN8_PTP_BUTTONS,
277
		.export_all_inputs = true },
278 279 280 281 282 283

	/*
	 * vendor specific classes
	 */
	{ .name = MT_CLS_3M,
		.quirks = MT_QUIRK_VALID_IS_CONFIDENCE |
284 285
			MT_QUIRK_SLOT_IS_CONTACTID |
			MT_QUIRK_TOUCH_SIZE_SCALING,
286 287
		.sn_move = 2048,
		.sn_width = 128,
288 289 290
		.sn_height = 128,
		.maxcontacts = 60,
	},
291 292
	{ .name = MT_CLS_EGALAX,
		.quirks =  MT_QUIRK_SLOT_IS_CONTACTID |
293
			MT_QUIRK_VALID_IS_INRANGE,
294 295 296
		.sn_move = 4096,
		.sn_pressure = 32,
	},
297 298 299
	{ .name = MT_CLS_EGALAX_SERIAL,
		.quirks =  MT_QUIRK_SLOT_IS_CONTACTID |
			MT_QUIRK_ALWAYS_VALID,
300 301 302
		.sn_move = 4096,
		.sn_pressure = 32,
	},
303 304 305 306 307
	{ .name = MT_CLS_TOPSEED,
		.quirks = MT_QUIRK_ALWAYS_VALID,
		.is_indirect = true,
		.maxcontacts = 2,
	},
308 309 310
	{ .name = MT_CLS_PANASONIC,
		.quirks = MT_QUIRK_NOT_SEEN_MEANS_UP,
		.maxcontacts = 4 },
311 312 313
	{ .name	= MT_CLS_GENERALTOUCH_TWOFINGERS,
		.quirks	= MT_QUIRK_NOT_SEEN_MEANS_UP |
			MT_QUIRK_VALID_IS_INRANGE |
314
			MT_QUIRK_SLOT_IS_CONTACTID,
315 316 317 318
		.maxcontacts = 2
	},
	{ .name	= MT_CLS_GENERALTOUCH_PWT_TENFINGERS,
		.quirks	= MT_QUIRK_NOT_SEEN_MEANS_UP |
319
			MT_QUIRK_SLOT_IS_CONTACTID
320
	},
321

322 323 324 325 326 327
	{ .name = MT_CLS_FLATFROG,
		.quirks = MT_QUIRK_NOT_SEEN_MEANS_UP |
			MT_QUIRK_NO_AREA,
		.sn_move = 2048,
		.maxcontacts = 40,
	},
328 329 330 331 332 333
	{ .name = MT_CLS_LG,
		.quirks = MT_QUIRK_ALWAYS_VALID |
			MT_QUIRK_FIX_CONST_CONTACT_ID |
			MT_QUIRK_IGNORE_DUPLICATES |
			MT_QUIRK_HOVERING |
			MT_QUIRK_CONTACT_CNT_ACCURATE },
334 335 336 337
	{ .name = MT_CLS_ASUS,
		.quirks = MT_QUIRK_ALWAYS_VALID |
			MT_QUIRK_CONTACT_CNT_ACCURATE |
			MT_QUIRK_ASUS_CUSTOM_UP },
338 339 340 341 342
	{ .name = MT_CLS_VTL,
		.quirks = MT_QUIRK_ALWAYS_VALID |
			MT_QUIRK_CONTACT_CNT_ACCURATE |
			MT_QUIRK_FORCE_GET_FEATURE,
	},
343 344 345 346 347 348
	{ .name = MT_CLS_GOOGLE,
		.quirks = MT_QUIRK_ALWAYS_VALID |
			MT_QUIRK_CONTACT_CNT_ACCURATE |
			MT_QUIRK_SLOT_IS_CONTACTID |
			MT_QUIRK_HOVERING
	},
349 350 351 352 353 354 355
	{ .name = MT_CLS_RAZER_BLADE_STEALTH,
		.quirks = MT_QUIRK_ALWAYS_VALID |
			MT_QUIRK_IGNORE_DUPLICATES |
			MT_QUIRK_HOVERING |
			MT_QUIRK_CONTACT_CNT_ACCURATE |
			MT_QUIRK_WIN8_PTP_BUTTONS,
	},
356
	{ }
357 358
};

359 360 361 362
static ssize_t mt_show_quirks(struct device *dev,
			   struct device_attribute *attr,
			   char *buf)
{
Geliang Tang's avatar
Geliang Tang committed
363
	struct hid_device *hdev = to_hid_device(dev);
364 365 366 367 368 369 370 371 372
	struct mt_device *td = hid_get_drvdata(hdev);

	return sprintf(buf, "%u\n", td->mtclass.quirks);
}

static ssize_t mt_set_quirks(struct device *dev,
			  struct device_attribute *attr,
			  const char *buf, size_t count)
{
Geliang Tang's avatar
Geliang Tang committed
373
	struct hid_device *hdev = to_hid_device(dev);
374
	struct mt_device *td = hid_get_drvdata(hdev);
375
	struct mt_application *application;
376 377 378 379 380 381 382 383

	unsigned long val;

	if (kstrtoul(buf, 0, &val))
		return -EINVAL;

	td->mtclass.quirks = val;

384
	list_for_each_entry(application, &td->applications, list) {
385 386 387
		application->quirks = val;
		if (!application->have_contact_count)
			application->quirks &= ~MT_QUIRK_CONTACT_CNT_ACCURATE;
388 389
	}

390 391 392 393 394 395 396 397 398 399
	return count;
}

static DEVICE_ATTR(quirks, S_IWUSR | S_IRUGO, mt_show_quirks, mt_set_quirks);

static struct attribute *sysfs_attrs[] = {
	&dev_attr_quirks.attr,
	NULL
};

400
static const struct attribute_group mt_attribute_group = {
401 402 403
	.attrs = sysfs_attrs
};

404 405
static void mt_get_feature(struct hid_device *hdev, struct hid_report *report)
{
Aaron Ma's avatar
Aaron Ma committed
406 407
	int ret;
	u32 size = hid_report_len(report);
408 409 410
	u8 *buf;

	/*
411 412
	 * Do not fetch the feature report if the device has been explicitly
	 * marked as non-capable.
413
	 */
414
	if (hdev->quirks & HID_QUIRK_NO_INIT_REPORTS)
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435
		return;

	buf = hid_alloc_report_buf(report, GFP_KERNEL);
	if (!buf)
		return;

	ret = hid_hw_raw_request(hdev, report->id, buf, size,
				 HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
	if (ret < 0) {
		dev_warn(&hdev->dev, "failed to fetch feature %d\n",
			 report->id);
	} else {
		ret = hid_report_raw_event(hdev, HID_FEATURE_REPORT, buf,
					   size, 0);
		if (ret)
			dev_warn(&hdev->dev, "failed to report feature\n");
	}

	kfree(buf);
}

436
static void mt_feature_mapping(struct hid_device *hdev,
437 438
		struct hid_field *field, struct hid_usage *usage)
{
439 440 441 442
	struct mt_device *td = hid_get_drvdata(hdev);

	switch (usage->hid) {
	case HID_DG_CONTACTMAX:
443 444
		mt_get_feature(hdev, field->report);

445
		td->maxcontacts = field->value[0];
446 447 448
		if (!td->maxcontacts &&
		    field->logical_maximum <= MT_MAX_MAXCONTACT)
			td->maxcontacts = field->logical_maximum;
449
		if (td->mtclass.maxcontacts)
450
			/* check if the maxcontacts is given by the class */
451
			td->maxcontacts = td->mtclass.maxcontacts;
452

453 454 455 456 457 458 459
		break;
	case HID_DG_BUTTONTYPE:
		if (usage->usage_index >= field->report_count) {
			dev_err(&hdev->dev, "HID_DG_BUTTONTYPE out of range\n");
			break;
		}

460
		mt_get_feature(hdev, field->report);
461 462 463
		if (field->value[usage->usage_index] == MT_BUTTONTYPE_CLICKPAD)
			td->is_buttonpad = true;

464
		break;
465 466 467 468 469
	case 0xff0000c5:
		/* Retrieve the Win8 blob once to enable some devices */
		if (usage->usage_index == 0)
			mt_get_feature(hdev, field->report);
		break;
470 471 472 473 474 475 476 477 478 479
	}
}

static void set_abs(struct input_dev *input, unsigned int code,
		struct hid_field *field, int snratio)
{
	int fmin = field->logical_minimum;
	int fmax = field->logical_maximum;
	int fuzz = snratio ? (fmax - fmin) / snratio : 0;
	input_set_abs_params(input, code, fmin, fmax, fuzz, 0);
480
	input_abs_set_res(input, code, hidinput_calc_abs_res(field, code));
481 482
}

483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510
static struct mt_usages *mt_allocate_usage(struct hid_device *hdev,
					   struct mt_application *application)
{
	struct mt_usages *usage;

	usage = devm_kzalloc(&hdev->dev, sizeof(*usage), GFP_KERNEL);
	if (!usage)
		return NULL;

	/* set some defaults so we do not need to check for null pointers */
	usage->x = DEFAULT_ZERO;
	usage->y = DEFAULT_ZERO;
	usage->cx = DEFAULT_ZERO;
	usage->cy = DEFAULT_ZERO;
	usage->p = DEFAULT_ZERO;
	usage->w = DEFAULT_ZERO;
	usage->h = DEFAULT_ZERO;
	usage->a = DEFAULT_ZERO;
	usage->contactid = DEFAULT_ZERO;
	usage->tip_state = DEFAULT_FALSE;
	usage->inrange_state = DEFAULT_FALSE;
	usage->confidence_state = DEFAULT_TRUE;

	list_add_tail(&usage->list, &application->mt_usages);

	return usage;
}

511 512 513 514 515 516 517 518 519 520 521
static struct mt_application *mt_allocate_application(struct mt_device *td,
						      unsigned int application)
{
	struct mt_application *mt_application;

	mt_application = devm_kzalloc(&td->hdev->dev, sizeof(*mt_application),
				      GFP_KERNEL);
	if (!mt_application)
		return NULL;

	mt_application->application = application;
522
	INIT_LIST_HEAD(&mt_application->mt_usages);
523 524 525 526 527 528 529 530 531 532 533 534

	if (application == HID_DG_TOUCHSCREEN)
		mt_application->mt_flags |= INPUT_MT_DIRECT;

	/*
	 * Model touchscreens providing buttons as touchpads.
	 */
	if (application == HID_DG_TOUCHPAD) {
		mt_application->mt_flags |= INPUT_MT_POINTER;
		td->inputmode_value = MT_INPUTMODE_TOUCHPAD;
	}

535 536
	mt_application->scantime = DEFAULT_ZERO;
	mt_application->raw_cc = DEFAULT_ZERO;
537
	mt_application->quirks = td->mtclass.quirks;
538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561

	list_add_tail(&mt_application->list, &td->applications);

	return mt_application;
}

static struct mt_application *mt_find_application(struct mt_device *td,
						  unsigned int application)
{
	struct mt_application *tmp, *mt_application = NULL;

	list_for_each_entry(tmp, &td->applications, list) {
		if (application == tmp->application) {
			mt_application = tmp;
			break;
		}
	}

	if (!mt_application)
		mt_application = mt_allocate_application(td, application);

	return mt_application;
}

562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615
static struct mt_report_data *mt_allocate_report_data(struct mt_device *td,
						      struct hid_report *report)
{
	struct mt_report_data *rdata;
	struct hid_field *field;
	int r, n;

	rdata = devm_kzalloc(&td->hdev->dev, sizeof(*rdata), GFP_KERNEL);
	if (!rdata)
		return NULL;

	rdata->report = report;
	rdata->application = mt_find_application(td, report->application);

	if (!rdata->application) {
		devm_kfree(&td->hdev->dev, rdata);
		return NULL;
	}

	for (r = 0; r < report->maxfield; r++) {
		field = report->field[r];

		if (!(HID_MAIN_ITEM_VARIABLE & field->flags))
			continue;

		for (n = 0; n < field->report_count; n++) {
			if (field->usage[n].hid == HID_DG_CONTACTID)
				rdata->is_mt_collection = true;
		}
	}

	list_add_tail(&rdata->list, &td->reports);

	return rdata;
}

static struct mt_report_data *mt_find_report_data(struct mt_device *td,
						  struct hid_report *report)
{
	struct mt_report_data *tmp, *rdata = NULL;

	list_for_each_entry(tmp, &td->reports, list) {
		if (report == tmp->report) {
			rdata = tmp;
			break;
		}
	}

	if (!rdata)
		rdata = mt_allocate_report_data(td, report);

	return rdata;
}

616 617 618 619
static void mt_store_field(struct hid_device *hdev,
			   struct mt_application *application,
			   __s32 *value,
			   size_t offset)
620
{
621 622 623 624 625 626 627 628 629
	struct mt_usages *usage;
	__s32 **target;

	if (list_empty(&application->mt_usages))
		usage = mt_allocate_usage(hdev, application);
	else
		usage = list_last_entry(&application->mt_usages,
					struct mt_usages,
					list);
630

631
	if (!usage)
632 633
		return;

634 635 636 637 638 639
	target = (__s32 **)((char *)usage + offset);

	/* the value has already been filled, create a new slot */
	if (*target != DEFAULT_TRUE &&
	    *target != DEFAULT_FALSE &&
	    *target != DEFAULT_ZERO) {
640 641 642 643 644 645 646
		if (usage->contactid == DEFAULT_ZERO ||
		    usage->x == DEFAULT_ZERO ||
		    usage->y == DEFAULT_ZERO) {
			hid_dbg(hdev,
				"ignoring duplicate usage on incomplete");
			return;
		}
647 648 649 650 651 652 653 654
		usage = mt_allocate_usage(hdev, application);
		if (!usage)
			return;

		target = (__s32 **)((char *)usage + offset);
	}

	*target = value;
655 656
}

657 658 659 660 661
#define MT_STORE_FIELD(__name)						\
	mt_store_field(hdev, app,					\
		       &field->value[usage->usage_index],		\
		       offsetof(struct mt_usages, __name))

662
static int mt_touch_input_mapping(struct hid_device *hdev, struct hid_input *hi,
663
		struct hid_field *field, struct hid_usage *usage,
664
		unsigned long **bit, int *max, struct mt_application *app)
665 666
{
	struct mt_device *td = hid_get_drvdata(hdev);
667
	struct mt_class *cls = &td->mtclass;
668
	int code;
669
	struct hid_usage *prev_usage = NULL;
670

671 672
	/*
	 * Model touchscreens providing buttons as touchpads.
673
	 */
674 675
	if (field->application == HID_DG_TOUCHSCREEN &&
	    (usage->hid & HID_USAGE_PAGE) == HID_UP_BUTTON) {
676
		app->mt_flags |= INPUT_MT_POINTER;
677 678
		td->inputmode_value = MT_INPUTMODE_TOUCHPAD;
	}
679

680 681
	/* count the buttons on touchpads */
	if ((usage->hid & HID_USAGE_PAGE) == HID_UP_BUTTON)
682
		app->buttons_count++;
683

684 685 686
	if (usage->usage_index)
		prev_usage = &field->usage[usage->usage_index - 1];

687 688 689 690 691
	switch (usage->hid & HID_USAGE_PAGE) {

	case HID_UP_GENDESK:
		switch (usage->hid) {
		case HID_GD_X:
692
			if (prev_usage && (prev_usage->hid == usage->hid)) {
693
				code = ABS_MT_TOOL_X;
694 695
				MT_STORE_FIELD(cx);
			} else {
696
				code = ABS_MT_POSITION_X;
697 698
				MT_STORE_FIELD(x);
			}
699

700
			set_abs(hi->input, code, field, cls->sn_move);
701

702 703 704 705 706 707 708 709 710 711 712 713 714
			/*
			 * A system multi-axis that exports X and Y has a high
			 * chance of being used directly on a surface
			 */
			if (field->application == HID_GD_SYSTEM_MULTIAXIS) {
				__set_bit(INPUT_PROP_DIRECT,
					  hi->input->propbit);
				input_set_abs_params(hi->input,
						     ABS_MT_TOOL_TYPE,
						     MT_TOOL_DIAL,
						     MT_TOOL_DIAL, 0, 0);
			}

715 716
			return 1;
		case HID_GD_Y:
717
			if (prev_usage && (prev_usage->hid == usage->hid)) {
718
				code = ABS_MT_TOOL_Y;
719 720
				MT_STORE_FIELD(cy);
			} else {
721
				code = ABS_MT_POSITION_Y;
722 723
				MT_STORE_FIELD(y);
			}
724

725
			set_abs(hi->input, code, field, cls->sn_move);
726

727 728 729 730 731 732 733
			return 1;
		}
		return 0;

	case HID_UP_DIGITIZER:
		switch (usage->hid) {
		case HID_DG_INRANGE:
734
			if (app->quirks & MT_QUIRK_HOVERING) {
735 736 737
				input_set_abs_params(hi->input,
					ABS_MT_DISTANCE, 0, 1, 0, 0);
			}
738
			MT_STORE_FIELD(inrange_state);
739 740
			return 1;
		case HID_DG_CONFIDENCE:
741 742
			if ((cls->name == MT_CLS_WIN_8 ||
				cls->name == MT_CLS_WIN_8_DUAL) &&
743 744
				(field->application == HID_DG_TOUCHPAD ||
				 field->application == HID_DG_TOUCHSCREEN))
745
				app->quirks |= MT_QUIRK_CONFIDENCE;
746 747 748 749 750 751 752

			if (app->quirks & MT_QUIRK_CONFIDENCE)
				input_set_abs_params(hi->input,
						     ABS_MT_TOOL_TYPE,
						     MT_TOOL_FINGER,
						     MT_TOOL_PALM, 0, 0);

753
			MT_STORE_FIELD(confidence_state);
754 755
			return 1;
		case HID_DG_TIPSWITCH:
756 757 758
			if (field->application != HID_GD_SYSTEM_MULTIAXIS)
				input_set_capability(hi->input,
						     EV_KEY, BTN_TOUCH);
759
			MT_STORE_FIELD(tip_state);
760 761
			return 1;
		case HID_DG_CONTACTID:
762
			MT_STORE_FIELD(contactid);
763
			app->touches_by_report++;
764 765
			return 1;
		case HID_DG_WIDTH:
766
			if (!(app->quirks & MT_QUIRK_NO_AREA))
767 768
				set_abs(hi->input, ABS_MT_TOUCH_MAJOR, field,
					cls->sn_width);
769
			MT_STORE_FIELD(w);
770 771
			return 1;
		case HID_DG_HEIGHT:
772
			if (!(app->quirks & MT_QUIRK_NO_AREA)) {
773 774
				set_abs(hi->input, ABS_MT_TOUCH_MINOR, field,
					cls->sn_height);
775 776 777 778 779 780 781 782 783

				/*
				 * Only set ABS_MT_ORIENTATION if it is not
				 * already set by the HID_DG_AZIMUTH usage.
				 */
				if (!test_bit(ABS_MT_ORIENTATION,
						hi->input->absbit))
					input_set_abs_params(hi->input,
						ABS_MT_ORIENTATION, 0, 1, 0, 0);
784
			}
785
			MT_STORE_FIELD(h);
786 787 788 789
			return 1;
		case HID_DG_TIPPRESSURE:
			set_abs(hi->input, ABS_MT_PRESSURE, field,
				cls->sn_pressure);
790
			MT_STORE_FIELD(p);
791
			return 1;
792 793
		case HID_DG_SCANTIME:
			input_set_capability(hi->input, EV_MSC, MSC_TIMESTAMP);
794 795
			app->scantime = &field->value[usage->usage_index];
			app->scantime_logical_max = field->logical_maximum;
796
			return 1;
797
		case HID_DG_CONTACTCOUNT:
798 799
			app->have_contact_count = true;
			app->raw_cc = &field->value[usage->usage_index];
800
			return 1;
801 802 803 804 805 806 807 808 809 810 811
		case HID_DG_AZIMUTH:
			/*
			 * Azimuth has the range of [0, MAX) representing a full
			 * revolution. Set ABS_MT_ORIENTATION to a quarter of
			 * MAX according the definition of ABS_MT_ORIENTATION
			 */
			input_set_abs_params(hi->input, ABS_MT_ORIENTATION,
				-field->logical_maximum / 4,
				field->logical_maximum / 4,
				cls->sn_move ?
				field->logical_maximum / cls->sn_move : 0, 0);
812
			MT_STORE_FIELD(a);
813
			return 1;
814
		case HID_DG_CONTACTMAX:
815
			/* contact max are global to the report */
816
			return -1;
817 818 819 820
		case HID_DG_TOUCH:
			/* Legacy devices use TIPSWITCH and not TOUCH.
			 * Let's just ignore this field. */
			return -1;
821
		}
822 823 824
		/* let hid-input decide for the others */
		return 0;

825 826
	case HID_UP_BUTTON:
		code = BTN_MOUSE + ((usage->hid - 1) & HID_USAGE);
827 828 829 830
		/*
		 * MS PTP spec says that external buttons left and right have
		 * usages 2 and 3.
		 */
831
		if ((app->quirks & MT_QUIRK_WIN8_PTP_BUTTONS) &&
832 833 834
		    field->application == HID_DG_TOUCHPAD &&
		    (usage->hid & HID_USAGE) > 1)
			code--;
835 836 837 838

		if (field->application == HID_GD_SYSTEM_MULTIAXIS)
			code = BTN_0  + ((usage->hid - 1) & HID_USAGE);

839 840 841 842
		hid_map_usage(hi, usage, bit, max, EV_KEY, code);
		input_set_capability(hi->input, EV_KEY, code);
		return 1;

843 844 845 846 847 848 849 850
	case 0xff000000:
		/* we do not want to map these: no input-oriented meaning */
		return -1;
	}

	return 0;
}

851
static int mt_compute_slot(struct mt_device *td, struct mt_application *app,
852
			   struct mt_usages *slot,
853
			   struct input_dev *input)
854
{
855
	__s32 quirks = app->quirks;
856

857
	if (quirks & MT_QUIRK_SLOT_IS_CONTACTID)
858
		return *slot->contactid;
859

860
	if (quirks & MT_QUIRK_CYPRESS)
861
		return cypress_compute_slot(app, slot);
862

863
	if (quirks & MT_QUIRK_SLOT_IS_CONTACTNUMBER)
864
		return app->num_received;
865

866
	if (quirks & MT_QUIRK_SLOT_IS_CONTACTID_MINUS_ONE)
867
		return *slot->contactid - 1;
868

869
	return input_mt_get_slot_by_key(input, *slot->contactid);
870 871
}

872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893
static void mt_release_pending_palms(struct mt_device *td,
				     struct mt_application *app,
				     struct input_dev *input)
{
	int slotnum;
	bool need_sync = false;

	for_each_set_bit(slotnum, app->pending_palm_slots, td->maxcontacts) {
		clear_bit(slotnum, app->pending_palm_slots);

		input_mt_slot(input, slotnum);
		input_mt_report_slot_state(input, MT_TOOL_PALM, false);

		need_sync = true;
	}

	if (need_sync) {
		input_mt_sync_frame(input);
		input_sync(input);
	}
}

894 895 896 897
/*
 * this function is called when a whole packet has been received and processed,
 * so that it can decide what to send to the input layer.
 */
898 899
static void mt_sync_frame(struct mt_device *td, struct mt_application *app,
			  struct input_dev *input)
900
{
901
	if (app->quirks & MT_QUIRK_WIN8_PTP_BUTTONS)
902
		input_event(input, EV_KEY, BTN_LEFT, app->left_button_state);
903

904
	input_mt_sync_frame(input);
905
	input_event(input, EV_MSC, MSC_TIMESTAMP, app->timestamp);
906
	input_sync(input);
907 908 909

	mt_release_pending_palms(td, app, input);

910 911 912
	app->num_received = 0;
	app->left_button_state = 0;

913 914 915 916 917
	if (test_bit(MT_IO_FLAGS_ACTIVE_SLOTS, &td->mt_io_flags))
		set_bit(MT_IO_FLAGS_PENDING_SLOTS, &td->mt_io_flags);
	else
		clear_bit(MT_IO_FLAGS_PENDING_SLOTS, &td->mt_io_flags);
	clear_bit(MT_IO_FLAGS_ACTIVE_SLOTS, &td->mt_io_flags);
918 919
}

920
static int mt_compute_timestamp(struct mt_application *app, __s32 value)
921
{
922 923
	long delta = value - app->prev_scantime;
	unsigned long jdelta = jiffies_to_usecs(jiffies - app->jiffies);
924

925
	app->jiffies = jiffies;
926 927

	if (delta < 0)
928
		delta += app->scantime_logical_max;
929 930 931 932 933 934 935 936

	/* HID_DG_SCANTIME is expressed in 100us, we want it in us. */
	delta *= 100;

	if (jdelta > MAX_TIMESTAMP_INTERVAL)
		/* No data received for a while, resync the timestamp. */
		return 0;
	else
937
		return app->timestamp + delta;
938 939
}

940
static int mt_touch_event(struct hid_device *hid, struct hid_field *field,
941
				struct hid_usage *usage, __s32 value)
942 943 944 945 946 947 948 949
{
	/* we will handle the hidinput part later, now remains hiddev */
	if (hid->claimed & HID_CLAIMED_HIDDEV && hid->hiddev_hid_event)
		hid->hiddev_hid_event(hid, field, usage, value);

	return 1;
}

950 951 952
static int mt_process_slot(struct mt_device *td, struct input_dev *input,
			    struct mt_application *app,
			    struct mt_usages *slot)
953
{
954
	struct input_mt *mt = input->mt;
955
	__s32 quirks = app->quirks;
956 957 958 959 960
	bool valid = true;
	bool confidence_state = true;
	bool inrange_state = false;
	int active;
	int slotnum;
961
	int tool = MT_TOOL_FINGER;
962

963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999
	if (!slot)
		return -EINVAL;

	if ((quirks & MT_QUIRK_CONTACT_CNT_ACCURATE) &&
	    app->num_received >= app->num_expected)
		return -EAGAIN;

	if (!(quirks & MT_QUIRK_ALWAYS_VALID)) {
		if (quirks & MT_QUIRK_VALID_IS_INRANGE)
			valid = *slot->inrange_state;
		if (quirks & MT_QUIRK_NOT_SEEN_MEANS_UP)
			valid = *slot->tip_state;
		if (quirks & MT_QUIRK_VALID_IS_CONFIDENCE)
			valid = *slot->confidence_state;

		if (!valid)
			return 0;
	}

	slotnum = mt_compute_slot(td, app, slot, input);
	if (slotnum < 0 || slotnum >= td->maxcontacts)
		return 0;

	if ((quirks & MT_QUIRK_IGNORE_DUPLICATES) && mt) {
		struct input_mt_slot *i_slot = &mt->slots[slotnum];

		if (input_mt_is_active(i_slot) &&
		    input_mt_is_used(mt, i_slot))
			return -EAGAIN;
	}

	if (quirks & MT_QUIRK_CONFIDENCE)
		confidence_state = *slot->confidence_state;

	if (quirks & MT_QUIRK_HOVERING)
		inrange_state = *slot->inrange_state;

1000
	active = *slot->tip_state || inrange_state;