Linux Audio

Check our new training course

Loading...
Note: File does not exist in v3.15.
   1// SPDX-License-Identifier: GPL-2.0-or-later
   2/*
   3 *  HID driver for Asus notebook built-in keyboard.
   4 *  Fixes small logical maximum to match usage maximum.
   5 *
   6 *  Currently supported devices are:
   7 *    EeeBook X205TA
   8 *    VivoBook E200HA
   9 *
  10 *  Copyright (c) 2016 Yusuke Fujimaki <usk.fujimaki@gmail.com>
  11 *
  12 *  This module based on hid-ortek by
  13 *  Copyright (c) 2010 Johnathon Harris <jmharris@gmail.com>
  14 *  Copyright (c) 2011 Jiri Kosina
  15 *
  16 *  This module has been updated to add support for Asus i2c touchpad.
  17 *
  18 *  Copyright (c) 2016 Brendan McGrath <redmcg@redmandi.dyndns.org>
  19 *  Copyright (c) 2016 Victor Vlasenko <victor.vlasenko@sysgears.com>
  20 *  Copyright (c) 2016 Frederik Wenigwieser <frederik.wenigwieser@gmail.com>
  21 */
  22
  23/*
  24 */
  25
  26#include <linux/dmi.h>
  27#include <linux/hid.h>
  28#include <linux/module.h>
  29#include <linux/platform_data/x86/asus-wmi.h>
  30#include <linux/input/mt.h>
  31#include <linux/usb.h> /* For to_usb_interface for T100 touchpad intf check */
  32#include <linux/power_supply.h>
  33#include <linux/leds.h>
  34
  35#include "hid-ids.h"
  36
  37MODULE_AUTHOR("Yusuke Fujimaki <usk.fujimaki@gmail.com>");
  38MODULE_AUTHOR("Brendan McGrath <redmcg@redmandi.dyndns.org>");
  39MODULE_AUTHOR("Victor Vlasenko <victor.vlasenko@sysgears.com>");
  40MODULE_AUTHOR("Frederik Wenigwieser <frederik.wenigwieser@gmail.com>");
  41MODULE_DESCRIPTION("Asus HID Keyboard and TouchPad");
  42
  43#define T100_TPAD_INTF 2
  44#define MEDION_E1239T_TPAD_INTF 1
  45
  46#define E1239T_TP_TOGGLE_REPORT_ID 0x05
  47#define T100CHI_MOUSE_REPORT_ID 0x06
  48#define FEATURE_REPORT_ID 0x0d
  49#define INPUT_REPORT_ID 0x5d
  50#define FEATURE_KBD_REPORT_ID 0x5a
  51#define FEATURE_KBD_REPORT_SIZE 16
  52#define FEATURE_KBD_LED_REPORT_ID1 0x5d
  53#define FEATURE_KBD_LED_REPORT_ID2 0x5e
  54
  55#define SUPPORT_KBD_BACKLIGHT BIT(0)
  56
  57#define MAX_TOUCH_MAJOR 8
  58#define MAX_PRESSURE 128
  59
  60#define BTN_LEFT_MASK 0x01
  61#define CONTACT_TOOL_TYPE_MASK 0x80
  62#define CONTACT_X_MSB_MASK 0xf0
  63#define CONTACT_Y_MSB_MASK 0x0f
  64#define CONTACT_TOUCH_MAJOR_MASK 0x07
  65#define CONTACT_PRESSURE_MASK 0x7f
  66
  67#define	BATTERY_REPORT_ID	(0x03)
  68#define	BATTERY_REPORT_SIZE	(1 + 8)
  69#define	BATTERY_LEVEL_MAX	((u8)255)
  70#define	BATTERY_STAT_DISCONNECT	(0)
  71#define	BATTERY_STAT_CHARGING	(1)
  72#define	BATTERY_STAT_FULL	(2)
  73
  74#define QUIRK_FIX_NOTEBOOK_REPORT	BIT(0)
  75#define QUIRK_NO_INIT_REPORTS		BIT(1)
  76#define QUIRK_SKIP_INPUT_MAPPING	BIT(2)
  77#define QUIRK_IS_MULTITOUCH		BIT(3)
  78#define QUIRK_NO_CONSUMER_USAGES	BIT(4)
  79#define QUIRK_USE_KBD_BACKLIGHT		BIT(5)
  80#define QUIRK_T100_KEYBOARD		BIT(6)
  81#define QUIRK_T100CHI			BIT(7)
  82#define QUIRK_G752_KEYBOARD		BIT(8)
  83#define QUIRK_T90CHI			BIT(9)
  84#define QUIRK_MEDION_E1239T		BIT(10)
  85#define QUIRK_ROG_NKEY_KEYBOARD		BIT(11)
  86#define QUIRK_ROG_CLAYMORE_II_KEYBOARD BIT(12)
  87
  88#define I2C_KEYBOARD_QUIRKS			(QUIRK_FIX_NOTEBOOK_REPORT | \
  89						 QUIRK_NO_INIT_REPORTS | \
  90						 QUIRK_NO_CONSUMER_USAGES)
  91#define I2C_TOUCHPAD_QUIRKS			(QUIRK_NO_INIT_REPORTS | \
  92						 QUIRK_SKIP_INPUT_MAPPING | \
  93						 QUIRK_IS_MULTITOUCH)
  94
  95#define TRKID_SGN       ((TRKID_MAX + 1) >> 1)
  96
  97struct asus_kbd_leds {
  98	struct led_classdev cdev;
  99	struct hid_device *hdev;
 100	struct work_struct work;
 101	unsigned int brightness;
 102	spinlock_t lock;
 103	bool removed;
 104};
 105
 106struct asus_touchpad_info {
 107	int max_x;
 108	int max_y;
 109	int res_x;
 110	int res_y;
 111	int contact_size;
 112	int max_contacts;
 113	int report_size;
 114};
 115
 116struct asus_drvdata {
 117	unsigned long quirks;
 118	struct hid_device *hdev;
 119	struct input_dev *input;
 120	struct input_dev *tp_kbd_input;
 121	struct asus_kbd_leds *kbd_backlight;
 122	const struct asus_touchpad_info *tp;
 123	bool enable_backlight;
 124	struct power_supply *battery;
 125	struct power_supply_desc battery_desc;
 126	int battery_capacity;
 127	int battery_stat;
 128	bool battery_in_query;
 129	unsigned long battery_next_query;
 130};
 131
 132static int asus_report_battery(struct asus_drvdata *, u8 *, int);
 133
 134static const struct asus_touchpad_info asus_i2c_tp = {
 135	.max_x = 2794,
 136	.max_y = 1758,
 137	.contact_size = 5,
 138	.max_contacts = 5,
 139	.report_size = 28 /* 2 byte header + 5 * 5 + 1 byte footer */,
 140};
 141
 142static const struct asus_touchpad_info asus_t100ta_tp = {
 143	.max_x = 2240,
 144	.max_y = 1120,
 145	.res_x = 30, /* units/mm */
 146	.res_y = 27, /* units/mm */
 147	.contact_size = 5,
 148	.max_contacts = 5,
 149	.report_size = 28 /* 2 byte header + 5 * 5 + 1 byte footer */,
 150};
 151
 152static const struct asus_touchpad_info asus_t100ha_tp = {
 153	.max_x = 2640,
 154	.max_y = 1320,
 155	.res_x = 30, /* units/mm */
 156	.res_y = 29, /* units/mm */
 157	.contact_size = 5,
 158	.max_contacts = 5,
 159	.report_size = 28 /* 2 byte header + 5 * 5 + 1 byte footer */,
 160};
 161
 162static const struct asus_touchpad_info asus_t200ta_tp = {
 163	.max_x = 3120,
 164	.max_y = 1716,
 165	.res_x = 30, /* units/mm */
 166	.res_y = 28, /* units/mm */
 167	.contact_size = 5,
 168	.max_contacts = 5,
 169	.report_size = 28 /* 2 byte header + 5 * 5 + 1 byte footer */,
 170};
 171
 172static const struct asus_touchpad_info asus_t100chi_tp = {
 173	.max_x = 2640,
 174	.max_y = 1320,
 175	.res_x = 31, /* units/mm */
 176	.res_y = 29, /* units/mm */
 177	.contact_size = 3,
 178	.max_contacts = 4,
 179	.report_size = 15 /* 2 byte header + 3 * 4 + 1 byte footer */,
 180};
 181
 182static const struct asus_touchpad_info medion_e1239t_tp = {
 183	.max_x = 2640,
 184	.max_y = 1380,
 185	.res_x = 29, /* units/mm */
 186	.res_y = 28, /* units/mm */
 187	.contact_size = 5,
 188	.max_contacts = 5,
 189	.report_size = 32 /* 2 byte header + 5 * 5 + 5 byte footer */,
 190};
 191
 192static void asus_report_contact_down(struct asus_drvdata *drvdat,
 193		int toolType, u8 *data)
 194{
 195	struct input_dev *input = drvdat->input;
 196	int touch_major, pressure, x, y;
 197
 198	x = (data[0] & CONTACT_X_MSB_MASK) << 4 | data[1];
 199	y = drvdat->tp->max_y - ((data[0] & CONTACT_Y_MSB_MASK) << 8 | data[2]);
 200
 201	input_report_abs(input, ABS_MT_POSITION_X, x);
 202	input_report_abs(input, ABS_MT_POSITION_Y, y);
 203
 204	if (drvdat->tp->contact_size < 5)
 205		return;
 206
 207	if (toolType == MT_TOOL_PALM) {
 208		touch_major = MAX_TOUCH_MAJOR;
 209		pressure = MAX_PRESSURE;
 210	} else {
 211		touch_major = (data[3] >> 4) & CONTACT_TOUCH_MAJOR_MASK;
 212		pressure = data[4] & CONTACT_PRESSURE_MASK;
 213	}
 214
 215	input_report_abs(input, ABS_MT_TOUCH_MAJOR, touch_major);
 216	input_report_abs(input, ABS_MT_PRESSURE, pressure);
 217}
 218
 219/* Required for Synaptics Palm Detection */
 220static void asus_report_tool_width(struct asus_drvdata *drvdat)
 221{
 222	struct input_mt *mt = drvdat->input->mt;
 223	struct input_mt_slot *oldest;
 224	int oldid, i;
 225
 226	if (drvdat->tp->contact_size < 5)
 227		return;
 228
 229	oldest = NULL;
 230	oldid = mt->trkid;
 231
 232	for (i = 0; i < mt->num_slots; ++i) {
 233		struct input_mt_slot *ps = &mt->slots[i];
 234		int id = input_mt_get_value(ps, ABS_MT_TRACKING_ID);
 235
 236		if (id < 0)
 237			continue;
 238		if ((id - oldid) & TRKID_SGN) {
 239			oldest = ps;
 240			oldid = id;
 241		}
 242	}
 243
 244	if (oldest) {
 245		input_report_abs(drvdat->input, ABS_TOOL_WIDTH,
 246			input_mt_get_value(oldest, ABS_MT_TOUCH_MAJOR));
 247	}
 248}
 249
 250static int asus_report_input(struct asus_drvdata *drvdat, u8 *data, int size)
 251{
 252	int i, toolType = MT_TOOL_FINGER;
 253	u8 *contactData = data + 2;
 254
 255	if (size != drvdat->tp->report_size)
 256		return 0;
 257
 258	for (i = 0; i < drvdat->tp->max_contacts; i++) {
 259		bool down = !!(data[1] & BIT(i+3));
 260
 261		if (drvdat->tp->contact_size >= 5)
 262			toolType = contactData[3] & CONTACT_TOOL_TYPE_MASK ?
 263						MT_TOOL_PALM : MT_TOOL_FINGER;
 264
 265		input_mt_slot(drvdat->input, i);
 266		input_mt_report_slot_state(drvdat->input, toolType, down);
 267
 268		if (down) {
 269			asus_report_contact_down(drvdat, toolType, contactData);
 270			contactData += drvdat->tp->contact_size;
 271		}
 272	}
 273
 274	input_report_key(drvdat->input, BTN_LEFT, data[1] & BTN_LEFT_MASK);
 275	asus_report_tool_width(drvdat);
 276
 277	input_mt_sync_frame(drvdat->input);
 278	input_sync(drvdat->input);
 279
 280	return 1;
 281}
 282
 283static int asus_e1239t_event(struct asus_drvdata *drvdat, u8 *data, int size)
 284{
 285	if (size != 3)
 286		return 0;
 287
 288	/* Handle broken mute key which only sends press events */
 289	if (!drvdat->tp &&
 290	    data[0] == 0x02 && data[1] == 0xe2 && data[2] == 0x00) {
 291		input_report_key(drvdat->input, KEY_MUTE, 1);
 292		input_sync(drvdat->input);
 293		input_report_key(drvdat->input, KEY_MUTE, 0);
 294		input_sync(drvdat->input);
 295		return 1;
 296	}
 297
 298	/* Handle custom touchpad toggle key which only sends press events */
 299	if (drvdat->tp_kbd_input &&
 300	    data[0] == 0x05 && data[1] == 0x02 && data[2] == 0x28) {
 301		input_report_key(drvdat->tp_kbd_input, KEY_F21, 1);
 302		input_sync(drvdat->tp_kbd_input);
 303		input_report_key(drvdat->tp_kbd_input, KEY_F21, 0);
 304		input_sync(drvdat->tp_kbd_input);
 305		return 1;
 306	}
 307
 308	return 0;
 309}
 310
 311static int asus_event(struct hid_device *hdev, struct hid_field *field,
 312		      struct hid_usage *usage, __s32 value)
 313{
 314	if ((usage->hid & HID_USAGE_PAGE) == 0xff310000 &&
 315	    (usage->hid & HID_USAGE) != 0x00 &&
 316	    (usage->hid & HID_USAGE) != 0xff && !usage->type) {
 317		hid_warn(hdev, "Unmapped Asus vendor usagepage code 0x%02x\n",
 318			 usage->hid & HID_USAGE);
 319	}
 320
 321	return 0;
 322}
 323
 324static int asus_raw_event(struct hid_device *hdev,
 325		struct hid_report *report, u8 *data, int size)
 326{
 327	struct asus_drvdata *drvdata = hid_get_drvdata(hdev);
 328
 329	if (drvdata->battery && data[0] == BATTERY_REPORT_ID)
 330		return asus_report_battery(drvdata, data, size);
 331
 332	if (drvdata->tp && data[0] == INPUT_REPORT_ID)
 333		return asus_report_input(drvdata, data, size);
 334
 335	if (drvdata->quirks & QUIRK_MEDION_E1239T)
 336		return asus_e1239t_event(drvdata, data, size);
 337
 338	/*
 339	 * Skip these report ID, the device emits a continuous stream associated
 340	 * with the AURA mode it is in which looks like an 'echo'.
 341	 */
 342	if (report->id == FEATURE_KBD_LED_REPORT_ID1 || report->id == FEATURE_KBD_LED_REPORT_ID2)
 343		return -1;
 344	if (drvdata->quirks & QUIRK_ROG_NKEY_KEYBOARD) {
 345		/*
 346		 * G713 and G733 send these codes on some keypresses, depending on
 347		 * the key pressed it can trigger a shutdown event if not caught.
 348		*/
 349		if (data[0] == 0x02 && data[1] == 0x30) {
 350			return -1;
 351		}
 352	}
 353
 354	if (drvdata->quirks & QUIRK_ROG_CLAYMORE_II_KEYBOARD) {
 355		/*
 356		 * CLAYMORE II keyboard sends this packet when it goes to sleep
 357		 * this causes the whole system to go into suspend.
 358		*/
 359
 360		if(size == 2 && data[0] == 0x02 && data[1] == 0x00) {
 361			return -1;
 362		}
 363	}
 364
 365	return 0;
 366}
 367
 368static int asus_kbd_set_report(struct hid_device *hdev, const u8 *buf, size_t buf_size)
 369{
 370	unsigned char *dmabuf;
 371	int ret;
 372
 373	dmabuf = kmemdup(buf, buf_size, GFP_KERNEL);
 374	if (!dmabuf)
 375		return -ENOMEM;
 376
 377	/*
 378	 * The report ID should be set from the incoming buffer due to LED and key
 379	 * interfaces having different pages
 380	*/
 381	ret = hid_hw_raw_request(hdev, buf[0], dmabuf,
 382				 buf_size, HID_FEATURE_REPORT,
 383				 HID_REQ_SET_REPORT);
 384	kfree(dmabuf);
 385
 386	return ret;
 387}
 388
 389static int asus_kbd_init(struct hid_device *hdev, u8 report_id)
 390{
 391	const u8 buf[] = { report_id, 0x41, 0x53, 0x55, 0x53, 0x20, 0x54,
 392		     0x65, 0x63, 0x68, 0x2e, 0x49, 0x6e, 0x63, 0x2e, 0x00 };
 393	int ret;
 394
 395	ret = asus_kbd_set_report(hdev, buf, sizeof(buf));
 396	if (ret < 0)
 397		hid_err(hdev, "Asus failed to send init command: %d\n", ret);
 398
 399	return ret;
 400}
 401
 402static int asus_kbd_get_functions(struct hid_device *hdev,
 403				  unsigned char *kbd_func,
 404				  u8 report_id)
 405{
 406	const u8 buf[] = { report_id, 0x05, 0x20, 0x31, 0x00, 0x08 };
 407	u8 *readbuf;
 408	int ret;
 409
 410	ret = asus_kbd_set_report(hdev, buf, sizeof(buf));
 411	if (ret < 0) {
 412		hid_err(hdev, "Asus failed to send configuration command: %d\n", ret);
 413		return ret;
 414	}
 415
 416	readbuf = kzalloc(FEATURE_KBD_REPORT_SIZE, GFP_KERNEL);
 417	if (!readbuf)
 418		return -ENOMEM;
 419
 420	ret = hid_hw_raw_request(hdev, FEATURE_KBD_REPORT_ID, readbuf,
 421				 FEATURE_KBD_REPORT_SIZE, HID_FEATURE_REPORT,
 422				 HID_REQ_GET_REPORT);
 423	if (ret < 0) {
 424		hid_err(hdev, "Asus failed to request functions: %d\n", ret);
 425		kfree(readbuf);
 426		return ret;
 427	}
 428
 429	*kbd_func = readbuf[6];
 430
 431	kfree(readbuf);
 432	return ret;
 433}
 434
 435static int asus_kbd_disable_oobe(struct hid_device *hdev)
 436{
 437	const u8 init[][6] = {
 438		{ FEATURE_KBD_REPORT_ID, 0x05, 0x20, 0x31, 0x00, 0x08 },
 439		{ FEATURE_KBD_REPORT_ID, 0xBA, 0xC5, 0xC4 },
 440		{ FEATURE_KBD_REPORT_ID, 0xD0, 0x8F, 0x01 },
 441		{ FEATURE_KBD_REPORT_ID, 0xD0, 0x85, 0xFF }
 442	};
 443	int ret;
 444
 445	for (size_t i = 0; i < ARRAY_SIZE(init); i++) {
 446		ret = asus_kbd_set_report(hdev, init[i], sizeof(init[i]));
 447		if (ret < 0)
 448			return ret;
 449	}
 450
 451	hid_info(hdev, "Disabled OOBE for keyboard\n");
 452	return 0;
 453}
 454
 455static void asus_schedule_work(struct asus_kbd_leds *led)
 456{
 457	unsigned long flags;
 458
 459	spin_lock_irqsave(&led->lock, flags);
 460	if (!led->removed)
 461		schedule_work(&led->work);
 462	spin_unlock_irqrestore(&led->lock, flags);
 463}
 464
 465static void asus_kbd_backlight_set(struct led_classdev *led_cdev,
 466				   enum led_brightness brightness)
 467{
 468	struct asus_kbd_leds *led = container_of(led_cdev, struct asus_kbd_leds,
 469						 cdev);
 470	unsigned long flags;
 471
 472	spin_lock_irqsave(&led->lock, flags);
 473	led->brightness = brightness;
 474	spin_unlock_irqrestore(&led->lock, flags);
 475
 476	asus_schedule_work(led);
 477}
 478
 479static enum led_brightness asus_kbd_backlight_get(struct led_classdev *led_cdev)
 480{
 481	struct asus_kbd_leds *led = container_of(led_cdev, struct asus_kbd_leds,
 482						 cdev);
 483	enum led_brightness brightness;
 484	unsigned long flags;
 485
 486	spin_lock_irqsave(&led->lock, flags);
 487	brightness = led->brightness;
 488	spin_unlock_irqrestore(&led->lock, flags);
 489
 490	return brightness;
 491}
 492
 493static void asus_kbd_backlight_work(struct work_struct *work)
 494{
 495	struct asus_kbd_leds *led = container_of(work, struct asus_kbd_leds, work);
 496	u8 buf[] = { FEATURE_KBD_REPORT_ID, 0xba, 0xc5, 0xc4, 0x00 };
 497	int ret;
 498	unsigned long flags;
 499
 500	spin_lock_irqsave(&led->lock, flags);
 501	buf[4] = led->brightness;
 502	spin_unlock_irqrestore(&led->lock, flags);
 503
 504	ret = asus_kbd_set_report(led->hdev, buf, sizeof(buf));
 505	if (ret < 0)
 506		hid_err(led->hdev, "Asus failed to set keyboard backlight: %d\n", ret);
 507}
 508
 509/* WMI-based keyboard backlight LED control (via asus-wmi driver) takes
 510 * precedence. We only activate HID-based backlight control when the
 511 * WMI control is not available.
 512 */
 513static bool asus_kbd_wmi_led_control_present(struct hid_device *hdev)
 514{
 515	struct asus_drvdata *drvdata = hid_get_drvdata(hdev);
 516	u32 value;
 517	int ret;
 518
 519	if (!IS_ENABLED(CONFIG_ASUS_WMI))
 520		return false;
 521
 522	if (drvdata->quirks & QUIRK_ROG_NKEY_KEYBOARD &&
 523			dmi_check_system(asus_use_hid_led_dmi_ids)) {
 524		hid_info(hdev, "using HID for asus::kbd_backlight\n");
 525		return false;
 526	}
 527
 528	ret = asus_wmi_evaluate_method(ASUS_WMI_METHODID_DSTS,
 529				       ASUS_WMI_DEVID_KBD_BACKLIGHT, 0, &value);
 530	hid_dbg(hdev, "WMI backlight check: rc %d value %x", ret, value);
 531	if (ret)
 532		return false;
 533
 534	return !!(value & ASUS_WMI_DSTS_PRESENCE_BIT);
 535}
 536
 537static int asus_kbd_register_leds(struct hid_device *hdev)
 538{
 539	struct asus_drvdata *drvdata = hid_get_drvdata(hdev);
 540	unsigned char kbd_func;
 541	int ret;
 542
 543	if (drvdata->quirks & QUIRK_ROG_NKEY_KEYBOARD) {
 544		/* Initialize keyboard */
 545		ret = asus_kbd_init(hdev, FEATURE_KBD_REPORT_ID);
 546		if (ret < 0)
 547			return ret;
 548
 549		/* The LED endpoint is initialised in two HID */
 550		ret = asus_kbd_init(hdev, FEATURE_KBD_LED_REPORT_ID1);
 551		if (ret < 0)
 552			return ret;
 553
 554		ret = asus_kbd_init(hdev, FEATURE_KBD_LED_REPORT_ID2);
 555		if (ret < 0)
 556			return ret;
 557
 558		if (dmi_match(DMI_PRODUCT_FAMILY, "ProArt P16")) {
 559			ret = asus_kbd_disable_oobe(hdev);
 560			if (ret < 0)
 561				return ret;
 562		}
 563	} else {
 564		/* Initialize keyboard */
 565		ret = asus_kbd_init(hdev, FEATURE_KBD_REPORT_ID);
 566		if (ret < 0)
 567			return ret;
 568
 569		/* Get keyboard functions */
 570		ret = asus_kbd_get_functions(hdev, &kbd_func, FEATURE_KBD_REPORT_ID);
 571		if (ret < 0)
 572			return ret;
 573
 574		/* Check for backlight support */
 575		if (!(kbd_func & SUPPORT_KBD_BACKLIGHT))
 576			return -ENODEV;
 577	}
 578
 579	drvdata->kbd_backlight = devm_kzalloc(&hdev->dev,
 580					      sizeof(struct asus_kbd_leds),
 581					      GFP_KERNEL);
 582	if (!drvdata->kbd_backlight)
 583		return -ENOMEM;
 584
 585	drvdata->kbd_backlight->removed = false;
 586	drvdata->kbd_backlight->brightness = 0;
 587	drvdata->kbd_backlight->hdev = hdev;
 588	drvdata->kbd_backlight->cdev.name = "asus::kbd_backlight";
 589	drvdata->kbd_backlight->cdev.max_brightness = 3;
 590	drvdata->kbd_backlight->cdev.brightness_set = asus_kbd_backlight_set;
 591	drvdata->kbd_backlight->cdev.brightness_get = asus_kbd_backlight_get;
 592	INIT_WORK(&drvdata->kbd_backlight->work, asus_kbd_backlight_work);
 593	spin_lock_init(&drvdata->kbd_backlight->lock);
 594
 595	ret = devm_led_classdev_register(&hdev->dev, &drvdata->kbd_backlight->cdev);
 596	if (ret < 0) {
 597		/* No need to have this still around */
 598		devm_kfree(&hdev->dev, drvdata->kbd_backlight);
 599	}
 600
 601	return ret;
 602}
 603
 604/*
 605 * [0]       REPORT_ID (same value defined in report descriptor)
 606 * [1]	     rest battery level. range [0..255]
 607 * [2]..[7]  Bluetooth hardware address (MAC address)
 608 * [8]       charging status
 609 *            = 0 : AC offline / discharging
 610 *            = 1 : AC online  / charging
 611 *            = 2 : AC online  / fully charged
 612 */
 613static int asus_parse_battery(struct asus_drvdata *drvdata, u8 *data, int size)
 614{
 615	u8 sts;
 616	u8 lvl;
 617	int val;
 618
 619	lvl = data[1];
 620	sts = data[8];
 621
 622	drvdata->battery_capacity = ((int)lvl * 100) / (int)BATTERY_LEVEL_MAX;
 623
 624	switch (sts) {
 625	case BATTERY_STAT_CHARGING:
 626		val = POWER_SUPPLY_STATUS_CHARGING;
 627		break;
 628	case BATTERY_STAT_FULL:
 629		val = POWER_SUPPLY_STATUS_FULL;
 630		break;
 631	case BATTERY_STAT_DISCONNECT:
 632	default:
 633		val = POWER_SUPPLY_STATUS_DISCHARGING;
 634		break;
 635	}
 636	drvdata->battery_stat = val;
 637
 638	return 0;
 639}
 640
 641static int asus_report_battery(struct asus_drvdata *drvdata, u8 *data, int size)
 642{
 643	/* notify only the autonomous event by device */
 644	if ((drvdata->battery_in_query == false) &&
 645			 (size == BATTERY_REPORT_SIZE))
 646		power_supply_changed(drvdata->battery);
 647
 648	return 0;
 649}
 650
 651static int asus_battery_query(struct asus_drvdata *drvdata)
 652{
 653	u8 *buf;
 654	int ret = 0;
 655
 656	buf = kmalloc(BATTERY_REPORT_SIZE, GFP_KERNEL);
 657	if (!buf)
 658		return -ENOMEM;
 659
 660	drvdata->battery_in_query = true;
 661	ret = hid_hw_raw_request(drvdata->hdev, BATTERY_REPORT_ID,
 662				buf, BATTERY_REPORT_SIZE,
 663				HID_INPUT_REPORT, HID_REQ_GET_REPORT);
 664	drvdata->battery_in_query = false;
 665	if (ret == BATTERY_REPORT_SIZE)
 666		ret = asus_parse_battery(drvdata, buf, BATTERY_REPORT_SIZE);
 667	else
 668		ret = -ENODATA;
 669
 670	kfree(buf);
 671
 672	return ret;
 673}
 674
 675static enum power_supply_property asus_battery_props[] = {
 676	POWER_SUPPLY_PROP_STATUS,
 677	POWER_SUPPLY_PROP_PRESENT,
 678	POWER_SUPPLY_PROP_CAPACITY,
 679	POWER_SUPPLY_PROP_SCOPE,
 680	POWER_SUPPLY_PROP_MODEL_NAME,
 681};
 682
 683#define	QUERY_MIN_INTERVAL	(60 * HZ)	/* 60[sec] */
 684
 685static int asus_battery_get_property(struct power_supply *psy,
 686				enum power_supply_property psp,
 687				union power_supply_propval *val)
 688{
 689	struct asus_drvdata *drvdata = power_supply_get_drvdata(psy);
 690	int ret = 0;
 691
 692	switch (psp) {
 693	case POWER_SUPPLY_PROP_STATUS:
 694	case POWER_SUPPLY_PROP_CAPACITY:
 695		if (time_before(drvdata->battery_next_query, jiffies)) {
 696			drvdata->battery_next_query =
 697					 jiffies + QUERY_MIN_INTERVAL;
 698			ret = asus_battery_query(drvdata);
 699			if (ret)
 700				return ret;
 701		}
 702		if (psp == POWER_SUPPLY_PROP_STATUS)
 703			val->intval = drvdata->battery_stat;
 704		else
 705			val->intval = drvdata->battery_capacity;
 706		break;
 707	case POWER_SUPPLY_PROP_PRESENT:
 708		val->intval = 1;
 709		break;
 710	case POWER_SUPPLY_PROP_SCOPE:
 711		val->intval = POWER_SUPPLY_SCOPE_DEVICE;
 712		break;
 713	case POWER_SUPPLY_PROP_MODEL_NAME:
 714		val->strval = drvdata->hdev->name;
 715		break;
 716	default:
 717		ret = -EINVAL;
 718		break;
 719	}
 720
 721	return ret;
 722}
 723
 724static int asus_battery_probe(struct hid_device *hdev)
 725{
 726	struct asus_drvdata *drvdata = hid_get_drvdata(hdev);
 727	struct power_supply_config pscfg = { .drv_data = drvdata };
 728	int ret = 0;
 729
 730	drvdata->battery_capacity = 0;
 731	drvdata->battery_stat = POWER_SUPPLY_STATUS_UNKNOWN;
 732	drvdata->battery_in_query = false;
 733
 734	drvdata->battery_desc.properties = asus_battery_props;
 735	drvdata->battery_desc.num_properties = ARRAY_SIZE(asus_battery_props);
 736	drvdata->battery_desc.get_property = asus_battery_get_property;
 737	drvdata->battery_desc.type = POWER_SUPPLY_TYPE_BATTERY;
 738	drvdata->battery_desc.use_for_apm = 0;
 739	drvdata->battery_desc.name = devm_kasprintf(&hdev->dev, GFP_KERNEL,
 740					"asus-keyboard-%s-battery",
 741					strlen(hdev->uniq) ?
 742					hdev->uniq : dev_name(&hdev->dev));
 743	if (!drvdata->battery_desc.name)
 744		return -ENOMEM;
 745
 746	drvdata->battery_next_query = jiffies;
 747
 748	drvdata->battery = devm_power_supply_register(&hdev->dev,
 749				&(drvdata->battery_desc), &pscfg);
 750	if (IS_ERR(drvdata->battery)) {
 751		ret = PTR_ERR(drvdata->battery);
 752		drvdata->battery = NULL;
 753		hid_err(hdev, "Unable to register battery device\n");
 754		return ret;
 755	}
 756
 757	power_supply_powers(drvdata->battery, &hdev->dev);
 758
 759	return ret;
 760}
 761
 762static int asus_input_configured(struct hid_device *hdev, struct hid_input *hi)
 763{
 764	struct input_dev *input = hi->input;
 765	struct asus_drvdata *drvdata = hid_get_drvdata(hdev);
 766
 767	/* T100CHI uses MULTI_INPUT, bind the touchpad to the mouse hid_input */
 768	if (drvdata->quirks & QUIRK_T100CHI &&
 769	    hi->report->id != T100CHI_MOUSE_REPORT_ID)
 770		return 0;
 771
 772	/* Handle MULTI_INPUT on E1239T mouse/touchpad USB interface */
 773	if (drvdata->tp && (drvdata->quirks & QUIRK_MEDION_E1239T)) {
 774		switch (hi->report->id) {
 775		case E1239T_TP_TOGGLE_REPORT_ID:
 776			input_set_capability(input, EV_KEY, KEY_F21);
 777			input->name = "Asus Touchpad Keys";
 778			drvdata->tp_kbd_input = input;
 779			return 0;
 780		case INPUT_REPORT_ID:
 781			break; /* Touchpad report, handled below */
 782		default:
 783			return 0; /* Ignore other reports */
 784		}
 785	}
 786
 787	if (drvdata->tp) {
 788		int ret;
 789
 790		input_set_abs_params(input, ABS_MT_POSITION_X, 0,
 791				     drvdata->tp->max_x, 0, 0);
 792		input_set_abs_params(input, ABS_MT_POSITION_Y, 0,
 793				     drvdata->tp->max_y, 0, 0);
 794		input_abs_set_res(input, ABS_MT_POSITION_X, drvdata->tp->res_x);
 795		input_abs_set_res(input, ABS_MT_POSITION_Y, drvdata->tp->res_y);
 796
 797		if (drvdata->tp->contact_size >= 5) {
 798			input_set_abs_params(input, ABS_TOOL_WIDTH, 0,
 799					     MAX_TOUCH_MAJOR, 0, 0);
 800			input_set_abs_params(input, ABS_MT_TOUCH_MAJOR, 0,
 801					     MAX_TOUCH_MAJOR, 0, 0);
 802			input_set_abs_params(input, ABS_MT_PRESSURE, 0,
 803					      MAX_PRESSURE, 0, 0);
 804		}
 805
 806		__set_bit(BTN_LEFT, input->keybit);
 807		__set_bit(INPUT_PROP_BUTTONPAD, input->propbit);
 808
 809		ret = input_mt_init_slots(input, drvdata->tp->max_contacts,
 810					  INPUT_MT_POINTER);
 811
 812		if (ret) {
 813			hid_err(hdev, "Asus input mt init slots failed: %d\n", ret);
 814			return ret;
 815		}
 816	}
 817
 818	drvdata->input = input;
 819
 820	if (drvdata->enable_backlight &&
 821	    !asus_kbd_wmi_led_control_present(hdev) &&
 822	    asus_kbd_register_leds(hdev))
 823		hid_warn(hdev, "Failed to initialize backlight.\n");
 824
 825	return 0;
 826}
 827
 828#define asus_map_key_clear(c)	hid_map_usage_clear(hi, usage, bit, \
 829						    max, EV_KEY, (c))
 830static int asus_input_mapping(struct hid_device *hdev,
 831		struct hid_input *hi, struct hid_field *field,
 832		struct hid_usage *usage, unsigned long **bit,
 833		int *max)
 834{
 835	struct asus_drvdata *drvdata = hid_get_drvdata(hdev);
 836
 837	if (drvdata->quirks & QUIRK_SKIP_INPUT_MAPPING) {
 838		/* Don't map anything from the HID report.
 839		 * We do it all manually in asus_input_configured
 840		 */
 841		return -1;
 842	}
 843
 844	/*
 845	 * Ignore a bunch of bogus collections in the T100CHI descriptor.
 846	 * This avoids a bunch of non-functional hid_input devices getting
 847	 * created because of the T100CHI using HID_QUIRK_MULTI_INPUT.
 848	 */
 849	if ((drvdata->quirks & (QUIRK_T100CHI | QUIRK_T90CHI)) &&
 850	    (field->application == (HID_UP_GENDESK | 0x0080) ||
 851	     field->application == HID_GD_MOUSE ||
 852	     usage->hid == (HID_UP_GENDEVCTRLS | 0x0024) ||
 853	     usage->hid == (HID_UP_GENDEVCTRLS | 0x0025) ||
 854	     usage->hid == (HID_UP_GENDEVCTRLS | 0x0026)))
 855		return -1;
 856
 857	/* ASUS-specific keyboard hotkeys and led backlight */
 858	if ((usage->hid & HID_USAGE_PAGE) == HID_UP_ASUSVENDOR) {
 859		switch (usage->hid & HID_USAGE) {
 860		case 0x10: asus_map_key_clear(KEY_BRIGHTNESSDOWN);	break;
 861		case 0x20: asus_map_key_clear(KEY_BRIGHTNESSUP);		break;
 862		case 0x35: asus_map_key_clear(KEY_DISPLAY_OFF);		break;
 863		case 0x6c: asus_map_key_clear(KEY_SLEEP);		break;
 864		case 0x7c: asus_map_key_clear(KEY_MICMUTE);		break;
 865		case 0x82: asus_map_key_clear(KEY_CAMERA);		break;
 866		case 0x88: asus_map_key_clear(KEY_RFKILL);			break;
 867		case 0xb5: asus_map_key_clear(KEY_CALC);			break;
 868		case 0xc4: asus_map_key_clear(KEY_KBDILLUMUP);		break;
 869		case 0xc5: asus_map_key_clear(KEY_KBDILLUMDOWN);		break;
 870		case 0xc7: asus_map_key_clear(KEY_KBDILLUMTOGGLE);	break;
 871
 872		case 0x6b: asus_map_key_clear(KEY_F21);		break; /* ASUS touchpad toggle */
 873		case 0x38: asus_map_key_clear(KEY_PROG1);	break; /* ROG key */
 874		case 0xba: asus_map_key_clear(KEY_PROG2);	break; /* Fn+C ASUS Splendid */
 875		case 0x5c: asus_map_key_clear(KEY_PROG3);	break; /* Fn+Space Power4Gear */
 876		case 0x99: asus_map_key_clear(KEY_PROG4);	break; /* Fn+F5 "fan" symbol */
 877		case 0xae: asus_map_key_clear(KEY_PROG4);	break; /* Fn+F5 "fan" symbol */
 878		case 0x92: asus_map_key_clear(KEY_CALC);	break; /* Fn+Ret "Calc" symbol */
 879		case 0xb2: asus_map_key_clear(KEY_PROG2);	break; /* Fn+Left previous aura */
 880		case 0xb3: asus_map_key_clear(KEY_PROG3);	break; /* Fn+Left next aura */
 881		case 0x6a: asus_map_key_clear(KEY_F13);		break; /* Screenpad toggle */
 882		case 0x4b: asus_map_key_clear(KEY_F14);		break; /* Arrows/Pg-Up/Dn toggle */
 883		case 0xa5: asus_map_key_clear(KEY_F15);		break; /* ROG Ally left back */
 884		case 0xa6: asus_map_key_clear(KEY_F16);		break; /* ROG Ally QAM button */
 885		case 0xa7: asus_map_key_clear(KEY_F17);		break; /* ROG Ally ROG long-press */
 886		case 0xa8: asus_map_key_clear(KEY_F18);		break; /* ROG Ally ROG long-press-release */
 887
 888		default:
 889			/* ASUS lazily declares 256 usages, ignore the rest,
 890			 * as some make the keyboard appear as a pointer device. */
 891			return -1;
 892		}
 893
 894		/*
 895		 * Check and enable backlight only on devices with UsagePage ==
 896		 * 0xff31 to avoid initializing the keyboard firmware multiple
 897		 * times on devices with multiple HID descriptors but same
 898		 * PID/VID.
 899		 */
 900		if (drvdata->quirks & QUIRK_USE_KBD_BACKLIGHT)
 901			drvdata->enable_backlight = true;
 902
 903		set_bit(EV_REP, hi->input->evbit);
 904		return 1;
 905	}
 906
 907	if ((usage->hid & HID_USAGE_PAGE) == HID_UP_MSVENDOR) {
 908		switch (usage->hid & HID_USAGE) {
 909		case 0xff01: asus_map_key_clear(BTN_1);	break;
 910		case 0xff02: asus_map_key_clear(BTN_2);	break;
 911		case 0xff03: asus_map_key_clear(BTN_3);	break;
 912		case 0xff04: asus_map_key_clear(BTN_4);	break;
 913		case 0xff05: asus_map_key_clear(BTN_5);	break;
 914		case 0xff06: asus_map_key_clear(BTN_6);	break;
 915		case 0xff07: asus_map_key_clear(BTN_7);	break;
 916		case 0xff08: asus_map_key_clear(BTN_8);	break;
 917		case 0xff09: asus_map_key_clear(BTN_9);	break;
 918		case 0xff0a: asus_map_key_clear(BTN_A);	break;
 919		case 0xff0b: asus_map_key_clear(BTN_B);	break;
 920		case 0x00f1: asus_map_key_clear(KEY_WLAN);	break;
 921		case 0x00f2: asus_map_key_clear(KEY_BRIGHTNESSDOWN);	break;
 922		case 0x00f3: asus_map_key_clear(KEY_BRIGHTNESSUP);	break;
 923		case 0x00f4: asus_map_key_clear(KEY_DISPLAY_OFF);	break;
 924		case 0x00f7: asus_map_key_clear(KEY_CAMERA);	break;
 925		case 0x00f8: asus_map_key_clear(KEY_PROG1);	break;
 926		default:
 927			return 0;
 928		}
 929
 930		set_bit(EV_REP, hi->input->evbit);
 931		return 1;
 932	}
 933
 934	if (drvdata->quirks & QUIRK_NO_CONSUMER_USAGES &&
 935		(usage->hid & HID_USAGE_PAGE) == HID_UP_CONSUMER) {
 936		switch (usage->hid & HID_USAGE) {
 937		case 0xe2: /* Mute */
 938		case 0xe9: /* Volume up */
 939		case 0xea: /* Volume down */
 940			return 0;
 941		default:
 942			/* Ignore dummy Consumer usages which make the
 943			 * keyboard incorrectly appear as a pointer device.
 944			 */
 945			return -1;
 946		}
 947	}
 948
 949	/*
 950	 * The mute button is broken and only sends press events, we
 951	 * deal with this in our raw_event handler, so do not map it.
 952	 */
 953	if ((drvdata->quirks & QUIRK_MEDION_E1239T) &&
 954	    usage->hid == (HID_UP_CONSUMER | 0xe2)) {
 955		input_set_capability(hi->input, EV_KEY, KEY_MUTE);
 956		return -1;
 957	}
 958
 959	return 0;
 960}
 961
 962static int asus_start_multitouch(struct hid_device *hdev)
 963{
 964	int ret;
 965	static const unsigned char buf[] = {
 966		FEATURE_REPORT_ID, 0x00, 0x03, 0x01, 0x00
 967	};
 968	unsigned char *dmabuf = kmemdup(buf, sizeof(buf), GFP_KERNEL);
 969
 970	if (!dmabuf) {
 971		ret = -ENOMEM;
 972		hid_err(hdev, "Asus failed to alloc dma buf: %d\n", ret);
 973		return ret;
 974	}
 975
 976	ret = hid_hw_raw_request(hdev, dmabuf[0], dmabuf, sizeof(buf),
 977					HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
 978
 979	kfree(dmabuf);
 980
 981	if (ret != sizeof(buf)) {
 982		hid_err(hdev, "Asus failed to start multitouch: %d\n", ret);
 983		return ret;
 984	}
 985
 986	return 0;
 987}
 988
 989static int __maybe_unused asus_resume(struct hid_device *hdev) {
 990	struct asus_drvdata *drvdata = hid_get_drvdata(hdev);
 991	int ret = 0;
 992
 993	if (drvdata->kbd_backlight) {
 994		const u8 buf[] = { FEATURE_KBD_REPORT_ID, 0xba, 0xc5, 0xc4,
 995				drvdata->kbd_backlight->cdev.brightness };
 996		ret = asus_kbd_set_report(hdev, buf, sizeof(buf));
 997		if (ret < 0) {
 998			hid_err(hdev, "Asus failed to set keyboard backlight: %d\n", ret);
 999			goto asus_resume_err;
1000		}
1001	}
1002
1003asus_resume_err:
1004	return ret;
1005}
1006
1007static int __maybe_unused asus_reset_resume(struct hid_device *hdev)
1008{
1009	struct asus_drvdata *drvdata = hid_get_drvdata(hdev);
1010
1011	if (drvdata->tp)
1012		return asus_start_multitouch(hdev);
1013
1014	return 0;
1015}
1016
1017static int asus_probe(struct hid_device *hdev, const struct hid_device_id *id)
1018{
1019	int ret;
1020	struct asus_drvdata *drvdata;
1021
1022	drvdata = devm_kzalloc(&hdev->dev, sizeof(*drvdata), GFP_KERNEL);
1023	if (drvdata == NULL) {
1024		hid_err(hdev, "Can't alloc Asus descriptor\n");
1025		return -ENOMEM;
1026	}
1027
1028	hid_set_drvdata(hdev, drvdata);
1029
1030	drvdata->quirks = id->driver_data;
1031
1032	/*
1033	 * T90CHI's keyboard dock returns same ID values as T100CHI's dock.
1034	 * Thus, identify T90CHI dock with product name string.
1035	 */
1036	if (strstr(hdev->name, "T90CHI")) {
1037		drvdata->quirks &= ~QUIRK_T100CHI;
1038		drvdata->quirks |= QUIRK_T90CHI;
1039	}
1040
1041	if (drvdata->quirks & QUIRK_IS_MULTITOUCH)
1042		drvdata->tp = &asus_i2c_tp;
1043
1044	if ((drvdata->quirks & QUIRK_T100_KEYBOARD) && hid_is_usb(hdev)) {
1045		struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
1046
1047		if (intf->altsetting->desc.bInterfaceNumber == T100_TPAD_INTF) {
1048			drvdata->quirks = QUIRK_SKIP_INPUT_MAPPING;
1049			/*
1050			 * The T100HA uses the same USB-ids as the T100TAF and
1051			 * the T200TA uses the same USB-ids as the T100TA, while
1052			 * both have different max x/y values as the T100TA[F].
1053			 */
1054			if (dmi_match(DMI_PRODUCT_NAME, "T100HAN"))
1055				drvdata->tp = &asus_t100ha_tp;
1056			else if (dmi_match(DMI_PRODUCT_NAME, "T200TA"))
1057				drvdata->tp = &asus_t200ta_tp;
1058			else
1059				drvdata->tp = &asus_t100ta_tp;
1060		}
1061	}
1062
1063	if (drvdata->quirks & QUIRK_T100CHI) {
1064		/*
1065		 * All functionality is on a single HID interface and for
1066		 * userspace the touchpad must be a separate input_dev.
1067		 */
1068		hdev->quirks |= HID_QUIRK_MULTI_INPUT;
1069		drvdata->tp = &asus_t100chi_tp;
1070	}
1071
1072	if ((drvdata->quirks & QUIRK_MEDION_E1239T) && hid_is_usb(hdev)) {
1073		struct usb_host_interface *alt =
1074			to_usb_interface(hdev->dev.parent)->altsetting;
1075
1076		if (alt->desc.bInterfaceNumber == MEDION_E1239T_TPAD_INTF) {
1077			/* For separate input-devs for tp and tp toggle key */
1078			hdev->quirks |= HID_QUIRK_MULTI_INPUT;
1079			drvdata->quirks |= QUIRK_SKIP_INPUT_MAPPING;
1080			drvdata->tp = &medion_e1239t_tp;
1081		}
1082	}
1083
1084	if (drvdata->quirks & QUIRK_NO_INIT_REPORTS)
1085		hdev->quirks |= HID_QUIRK_NO_INIT_REPORTS;
1086
1087	drvdata->hdev = hdev;
1088
1089	if (drvdata->quirks & (QUIRK_T100CHI | QUIRK_T90CHI)) {
1090		ret = asus_battery_probe(hdev);
1091		if (ret) {
1092			hid_err(hdev,
1093			    "Asus hid battery_probe failed: %d\n", ret);
1094			return ret;
1095		}
1096	}
1097
1098	ret = hid_parse(hdev);
1099	if (ret) {
1100		hid_err(hdev, "Asus hid parse failed: %d\n", ret);
1101		return ret;
1102	}
1103
1104	ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
1105	if (ret) {
1106		hid_err(hdev, "Asus hw start failed: %d\n", ret);
1107		return ret;
1108	}
1109
1110	if (!drvdata->input) {
1111		hid_err(hdev, "Asus input not registered\n");
1112		ret = -ENOMEM;
1113		goto err_stop_hw;
1114	}
1115
1116	if (drvdata->tp) {
1117		drvdata->input->name = "Asus TouchPad";
1118	} else {
1119		drvdata->input->name = "Asus Keyboard";
1120	}
1121
1122	if (drvdata->tp) {
1123		ret = asus_start_multitouch(hdev);
1124		if (ret)
1125			goto err_stop_hw;
1126	}
1127
1128	return 0;
1129err_stop_hw:
1130	hid_hw_stop(hdev);
1131	return ret;
1132}
1133
1134static void asus_remove(struct hid_device *hdev)
1135{
1136	struct asus_drvdata *drvdata = hid_get_drvdata(hdev);
1137	unsigned long flags;
1138
1139	if (drvdata->kbd_backlight) {
1140		spin_lock_irqsave(&drvdata->kbd_backlight->lock, flags);
1141		drvdata->kbd_backlight->removed = true;
1142		spin_unlock_irqrestore(&drvdata->kbd_backlight->lock, flags);
1143
1144		cancel_work_sync(&drvdata->kbd_backlight->work);
1145	}
1146
1147	hid_hw_stop(hdev);
1148}
1149
1150static const __u8 asus_g752_fixed_rdesc[] = {
1151        0x19, 0x00,			/*   Usage Minimum (0x00)       */
1152        0x2A, 0xFF, 0x00,		/*   Usage Maximum (0xFF)       */
1153};
1154
1155static const __u8 *asus_report_fixup(struct hid_device *hdev, __u8 *rdesc,
1156		unsigned int *rsize)
1157{
1158	struct asus_drvdata *drvdata = hid_get_drvdata(hdev);
1159
1160	if (drvdata->quirks & QUIRK_FIX_NOTEBOOK_REPORT &&
1161			*rsize >= 56 && rdesc[54] == 0x25 && rdesc[55] == 0x65) {
1162		hid_info(hdev, "Fixing up Asus notebook report descriptor\n");
1163		rdesc[55] = 0xdd;
1164	}
1165	/* For the T100TA/T200TA keyboard dock */
1166	if (drvdata->quirks & QUIRK_T100_KEYBOARD &&
1167		 (*rsize == 76 || *rsize == 101) &&
1168		 rdesc[73] == 0x81 && rdesc[74] == 0x01) {
1169		hid_info(hdev, "Fixing up Asus T100 keyb report descriptor\n");
1170		rdesc[74] &= ~HID_MAIN_ITEM_CONSTANT;
1171	}
1172	/* For the T100CHI/T90CHI keyboard dock */
1173	if (drvdata->quirks & (QUIRK_T100CHI | QUIRK_T90CHI)) {
1174		int rsize_orig;
1175		int offs;
1176
1177		if (drvdata->quirks & QUIRK_T100CHI) {
1178			rsize_orig = 403;
1179			offs = 388;
1180		} else {
1181			rsize_orig = 306;
1182			offs = 291;
1183		}
1184
1185		/*
1186		 * Change Usage (76h) to Usage Minimum (00h), Usage Maximum
1187		 * (FFh) and clear the flags in the Input() byte.
1188		 * Note the descriptor has a bogus 0 byte at the end so we
1189		 * only need 1 extra byte.
1190		 */
1191		if (*rsize == rsize_orig &&
1192			rdesc[offs] == 0x09 && rdesc[offs + 1] == 0x76) {
1193			*rsize = rsize_orig + 1;
1194			rdesc = kmemdup(rdesc, *rsize, GFP_KERNEL);
1195			if (!rdesc)
1196				return NULL;
1197
1198			hid_info(hdev, "Fixing up %s keyb report descriptor\n",
1199				drvdata->quirks & QUIRK_T100CHI ?
1200				"T100CHI" : "T90CHI");
1201			memmove(rdesc + offs + 4, rdesc + offs + 2, 12);
1202			rdesc[offs] = 0x19;
1203			rdesc[offs + 1] = 0x00;
1204			rdesc[offs + 2] = 0x29;
1205			rdesc[offs + 3] = 0xff;
1206			rdesc[offs + 14] = 0x00;
1207		}
1208	}
1209
1210	if (drvdata->quirks & QUIRK_G752_KEYBOARD &&
1211		 *rsize == 75 && rdesc[61] == 0x15 && rdesc[62] == 0x00) {
1212		/* report is missing usage minimum and maximum */
1213		__u8 *new_rdesc;
1214		size_t new_size = *rsize + sizeof(asus_g752_fixed_rdesc);
1215
1216		new_rdesc = devm_kzalloc(&hdev->dev, new_size, GFP_KERNEL);
1217		if (new_rdesc == NULL)
1218			return rdesc;
1219
1220		hid_info(hdev, "Fixing up Asus G752 keyb report descriptor\n");
1221		/* copy the valid part */
1222		memcpy(new_rdesc, rdesc, 61);
1223		/* insert missing part */
1224		memcpy(new_rdesc + 61, asus_g752_fixed_rdesc, sizeof(asus_g752_fixed_rdesc));
1225		/* copy remaining data */
1226		memcpy(new_rdesc + 61 + sizeof(asus_g752_fixed_rdesc), rdesc + 61, *rsize - 61);
1227
1228		*rsize = new_size;
1229		rdesc = new_rdesc;
1230	}
1231
1232	if (drvdata->quirks & QUIRK_ROG_NKEY_KEYBOARD &&
1233			*rsize == 331 && rdesc[190] == 0x85 && rdesc[191] == 0x5a &&
1234			rdesc[204] == 0x95 && rdesc[205] == 0x05) {
1235		hid_info(hdev, "Fixing up Asus N-KEY keyb report descriptor\n");
1236		rdesc[205] = 0x01;
1237	}
1238
1239	/* match many more n-key devices */
1240	if (drvdata->quirks & QUIRK_ROG_NKEY_KEYBOARD && *rsize > 15) {
1241		for (int i = 0; i < *rsize - 15; i++) {
1242			/* offset to the count from 0x5a report part always 14 */
1243			if (rdesc[i] == 0x85 && rdesc[i + 1] == 0x5a &&
1244			    rdesc[i + 14] == 0x95 && rdesc[i + 15] == 0x05) {
1245				hid_info(hdev, "Fixing up Asus N-Key report descriptor\n");
1246				rdesc[i + 15] = 0x01;
1247				break;
1248			}
1249		}
1250	}
1251
1252	return rdesc;
1253}
1254
1255static const struct hid_device_id asus_devices[] = {
1256	{ HID_I2C_DEVICE(USB_VENDOR_ID_ASUSTEK,
1257		USB_DEVICE_ID_ASUSTEK_I2C_KEYBOARD), I2C_KEYBOARD_QUIRKS},
1258	{ HID_I2C_DEVICE(USB_VENDOR_ID_ASUSTEK,
1259		USB_DEVICE_ID_ASUSTEK_I2C_TOUCHPAD), I2C_TOUCHPAD_QUIRKS },
1260	{ HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK,
1261		USB_DEVICE_ID_ASUSTEK_ROG_KEYBOARD1), QUIRK_USE_KBD_BACKLIGHT },
1262	{ HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK,
1263		USB_DEVICE_ID_ASUSTEK_ROG_KEYBOARD2), QUIRK_USE_KBD_BACKLIGHT },
1264	{ HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK,
1265		USB_DEVICE_ID_ASUSTEK_ROG_KEYBOARD3), QUIRK_G752_KEYBOARD },
1266	{ HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK,
1267		USB_DEVICE_ID_ASUSTEK_FX503VD_KEYBOARD),
1268	  QUIRK_USE_KBD_BACKLIGHT },
1269	{ HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK,
1270	    USB_DEVICE_ID_ASUSTEK_ROG_NKEY_KEYBOARD),
1271	  QUIRK_USE_KBD_BACKLIGHT | QUIRK_ROG_NKEY_KEYBOARD },
1272	{ HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK,
1273	    USB_DEVICE_ID_ASUSTEK_ROG_NKEY_KEYBOARD2),
1274	  QUIRK_USE_KBD_BACKLIGHT | QUIRK_ROG_NKEY_KEYBOARD },
1275	{ HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK,
1276	    USB_DEVICE_ID_ASUSTEK_ROG_NKEY_KEYBOARD3),
1277	  QUIRK_USE_KBD_BACKLIGHT | QUIRK_ROG_NKEY_KEYBOARD },
1278	{ HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK,
1279	    USB_DEVICE_ID_ASUSTEK_ROG_Z13_LIGHTBAR),
1280	  QUIRK_USE_KBD_BACKLIGHT | QUIRK_ROG_NKEY_KEYBOARD },
1281	{ HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK,
1282	    USB_DEVICE_ID_ASUSTEK_ROG_NKEY_ALLY),
1283	  QUIRK_USE_KBD_BACKLIGHT | QUIRK_ROG_NKEY_KEYBOARD },
1284	{ HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK,
1285	    USB_DEVICE_ID_ASUSTEK_ROG_NKEY_ALLY_X),
1286	  QUIRK_USE_KBD_BACKLIGHT | QUIRK_ROG_NKEY_KEYBOARD },
1287	{ HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK,
1288	    USB_DEVICE_ID_ASUSTEK_ROG_CLAYMORE_II_KEYBOARD),
1289	  QUIRK_ROG_CLAYMORE_II_KEYBOARD },
1290	{ HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK,
1291		USB_DEVICE_ID_ASUSTEK_T100TA_KEYBOARD),
1292	  QUIRK_T100_KEYBOARD | QUIRK_NO_CONSUMER_USAGES },
1293	{ HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK,
1294		USB_DEVICE_ID_ASUSTEK_T100TAF_KEYBOARD),
1295	  QUIRK_T100_KEYBOARD | QUIRK_NO_CONSUMER_USAGES },
1296	{ HID_USB_DEVICE(USB_VENDOR_ID_CHICONY, USB_DEVICE_ID_ASUS_AK1D) },
1297	{ HID_USB_DEVICE(USB_VENDOR_ID_TURBOX, USB_DEVICE_ID_ASUS_MD_5110) },
1298	{ HID_USB_DEVICE(USB_VENDOR_ID_JESS, USB_DEVICE_ID_ASUS_MD_5112) },
1299	{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_ASUSTEK,
1300		USB_DEVICE_ID_ASUSTEK_T100CHI_KEYBOARD), QUIRK_T100CHI },
1301	{ HID_USB_DEVICE(USB_VENDOR_ID_ITE, USB_DEVICE_ID_ITE_MEDION_E1239T),
1302		QUIRK_MEDION_E1239T },
1303	/*
1304	 * Note bind to the HID_GROUP_GENERIC group, so that we only bind to the keyboard
1305	 * part, while letting hid-multitouch.c handle the touchpad.
1306	 */
1307	{ HID_DEVICE(BUS_USB, HID_GROUP_GENERIC,
1308		USB_VENDOR_ID_ASUSTEK, USB_DEVICE_ID_ASUSTEK_T101HA_KEYBOARD) },
1309	{ }
1310};
1311MODULE_DEVICE_TABLE(hid, asus_devices);
1312
1313static struct hid_driver asus_driver = {
1314	.name			= "asus",
1315	.id_table		= asus_devices,
1316	.report_fixup		= asus_report_fixup,
1317	.probe                  = asus_probe,
1318	.remove			= asus_remove,
1319	.input_mapping          = asus_input_mapping,
1320	.input_configured       = asus_input_configured,
1321#ifdef CONFIG_PM
1322	.reset_resume           = asus_reset_resume,
1323	.resume					= asus_resume,
1324#endif
1325	.event			= asus_event,
1326	.raw_event		= asus_raw_event
1327};
1328module_hid_driver(asus_driver);
1329
1330MODULE_LICENSE("GPL");