18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * HID driver for some ITE "special" devices
48c2ecf20Sopenharmony_ci * Copyright (c) 2017 Hans de Goede <hdegoede@redhat.com>
58c2ecf20Sopenharmony_ci */
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci#include <linux/device.h>
88c2ecf20Sopenharmony_ci#include <linux/input.h>
98c2ecf20Sopenharmony_ci#include <linux/hid.h>
108c2ecf20Sopenharmony_ci#include <linux/module.h>
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci#include "hid-ids.h"
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci#define QUIRK_TOUCHPAD_ON_OFF_REPORT		BIT(0)
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_cistatic __u8 *ite_report_fixup(struct hid_device *hdev, __u8 *rdesc, unsigned int *rsize)
178c2ecf20Sopenharmony_ci{
188c2ecf20Sopenharmony_ci	unsigned long quirks = (unsigned long)hid_get_drvdata(hdev);
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci	if (quirks & QUIRK_TOUCHPAD_ON_OFF_REPORT) {
218c2ecf20Sopenharmony_ci		/* For Acer Aspire Switch 10 SW5-012 keyboard-dock */
228c2ecf20Sopenharmony_ci		if (*rsize == 188 && rdesc[162] == 0x81 && rdesc[163] == 0x02) {
238c2ecf20Sopenharmony_ci			hid_info(hdev, "Fixing up Acer Sw5-012 ITE keyboard report descriptor\n");
248c2ecf20Sopenharmony_ci			rdesc[163] = HID_MAIN_ITEM_RELATIVE;
258c2ecf20Sopenharmony_ci		}
268c2ecf20Sopenharmony_ci		/* For Acer One S1002/S1003 keyboard-dock */
278c2ecf20Sopenharmony_ci		if (*rsize == 188 && rdesc[185] == 0x81 && rdesc[186] == 0x02) {
288c2ecf20Sopenharmony_ci			hid_info(hdev, "Fixing up Acer S1002/S1003 ITE keyboard report descriptor\n");
298c2ecf20Sopenharmony_ci			rdesc[186] = HID_MAIN_ITEM_RELATIVE;
308c2ecf20Sopenharmony_ci		}
318c2ecf20Sopenharmony_ci		/* For Acer Aspire Switch 10E (SW3-016) keyboard-dock */
328c2ecf20Sopenharmony_ci		if (*rsize == 210 && rdesc[184] == 0x81 && rdesc[185] == 0x02) {
338c2ecf20Sopenharmony_ci			hid_info(hdev, "Fixing up Acer Aspire Switch 10E (SW3-016) ITE keyboard report descriptor\n");
348c2ecf20Sopenharmony_ci			rdesc[185] = HID_MAIN_ITEM_RELATIVE;
358c2ecf20Sopenharmony_ci		}
368c2ecf20Sopenharmony_ci	}
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ci	return rdesc;
398c2ecf20Sopenharmony_ci}
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_cistatic int ite_input_mapping(struct hid_device *hdev,
428c2ecf20Sopenharmony_ci		struct hid_input *hi, struct hid_field *field,
438c2ecf20Sopenharmony_ci		struct hid_usage *usage, unsigned long **bit,
448c2ecf20Sopenharmony_ci		int *max)
458c2ecf20Sopenharmony_ci{
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ci	unsigned long quirks = (unsigned long)hid_get_drvdata(hdev);
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci	if ((quirks & QUIRK_TOUCHPAD_ON_OFF_REPORT) &&
508c2ecf20Sopenharmony_ci	    (usage->hid & HID_USAGE_PAGE) == 0x00880000) {
518c2ecf20Sopenharmony_ci		if (usage->hid == 0x00880078) {
528c2ecf20Sopenharmony_ci			/* Touchpad on, userspace expects F22 for this */
538c2ecf20Sopenharmony_ci			hid_map_usage_clear(hi, usage, bit, max, EV_KEY, KEY_F22);
548c2ecf20Sopenharmony_ci			return 1;
558c2ecf20Sopenharmony_ci		}
568c2ecf20Sopenharmony_ci		if (usage->hid == 0x00880079) {
578c2ecf20Sopenharmony_ci			/* Touchpad off, userspace expects F23 for this */
588c2ecf20Sopenharmony_ci			hid_map_usage_clear(hi, usage, bit, max, EV_KEY, KEY_F23);
598c2ecf20Sopenharmony_ci			return 1;
608c2ecf20Sopenharmony_ci		}
618c2ecf20Sopenharmony_ci		return -1;
628c2ecf20Sopenharmony_ci	}
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci	return 0;
658c2ecf20Sopenharmony_ci}
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_cistatic int ite_event(struct hid_device *hdev, struct hid_field *field,
688c2ecf20Sopenharmony_ci		     struct hid_usage *usage, __s32 value)
698c2ecf20Sopenharmony_ci{
708c2ecf20Sopenharmony_ci	struct input_dev *input;
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ci	if (!(hdev->claimed & HID_CLAIMED_INPUT) || !field->hidinput)
738c2ecf20Sopenharmony_ci		return 0;
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci	input = field->hidinput->input;
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci	/*
788c2ecf20Sopenharmony_ci	 * The ITE8595 always reports 0 as value for the rfkill button. Luckily
798c2ecf20Sopenharmony_ci	 * it is the only button in its report, and it sends a report on
808c2ecf20Sopenharmony_ci	 * release only, so receiving a report means the button was pressed.
818c2ecf20Sopenharmony_ci	 */
828c2ecf20Sopenharmony_ci	if (usage->hid == HID_GD_RFKILL_BTN) {
838c2ecf20Sopenharmony_ci		input_event(input, EV_KEY, KEY_RFKILL, 1);
848c2ecf20Sopenharmony_ci		input_sync(input);
858c2ecf20Sopenharmony_ci		input_event(input, EV_KEY, KEY_RFKILL, 0);
868c2ecf20Sopenharmony_ci		input_sync(input);
878c2ecf20Sopenharmony_ci		return 1;
888c2ecf20Sopenharmony_ci	}
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ci	return 0;
918c2ecf20Sopenharmony_ci}
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_cistatic int ite_probe(struct hid_device *hdev, const struct hid_device_id *id)
948c2ecf20Sopenharmony_ci{
958c2ecf20Sopenharmony_ci	int ret;
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ci	hid_set_drvdata(hdev, (void *)id->driver_data);
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_ci	ret = hid_open_report(hdev);
1008c2ecf20Sopenharmony_ci	if (ret)
1018c2ecf20Sopenharmony_ci		return ret;
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci	return hid_hw_start(hdev, HID_CONNECT_DEFAULT);
1048c2ecf20Sopenharmony_ci}
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_cistatic const struct hid_device_id ite_devices[] = {
1078c2ecf20Sopenharmony_ci	{ HID_USB_DEVICE(USB_VENDOR_ID_ITE, USB_DEVICE_ID_ITE8595) },
1088c2ecf20Sopenharmony_ci	{ HID_USB_DEVICE(USB_VENDOR_ID_258A, USB_DEVICE_ID_258A_6A88) },
1098c2ecf20Sopenharmony_ci	/* ITE8595 USB kbd ctlr, with Synaptics touchpad connected to it. */
1108c2ecf20Sopenharmony_ci	{ HID_DEVICE(BUS_USB, HID_GROUP_GENERIC,
1118c2ecf20Sopenharmony_ci		     USB_VENDOR_ID_SYNAPTICS,
1128c2ecf20Sopenharmony_ci		     USB_DEVICE_ID_SYNAPTICS_ACER_SWITCH5_012),
1138c2ecf20Sopenharmony_ci	  .driver_data = QUIRK_TOUCHPAD_ON_OFF_REPORT },
1148c2ecf20Sopenharmony_ci	/* ITE8910 USB kbd ctlr, with Synaptics touchpad connected to it. */
1158c2ecf20Sopenharmony_ci	{ HID_DEVICE(BUS_USB, HID_GROUP_GENERIC,
1168c2ecf20Sopenharmony_ci		     USB_VENDOR_ID_SYNAPTICS,
1178c2ecf20Sopenharmony_ci		     USB_DEVICE_ID_SYNAPTICS_ACER_ONE_S1002),
1188c2ecf20Sopenharmony_ci	  .driver_data = QUIRK_TOUCHPAD_ON_OFF_REPORT },
1198c2ecf20Sopenharmony_ci	/* ITE8910 USB kbd ctlr, with Synaptics touchpad connected to it. */
1208c2ecf20Sopenharmony_ci	{ HID_DEVICE(BUS_USB, HID_GROUP_GENERIC,
1218c2ecf20Sopenharmony_ci		     USB_VENDOR_ID_SYNAPTICS,
1228c2ecf20Sopenharmony_ci		     USB_DEVICE_ID_SYNAPTICS_ACER_ONE_S1003),
1238c2ecf20Sopenharmony_ci	  .driver_data = QUIRK_TOUCHPAD_ON_OFF_REPORT },
1248c2ecf20Sopenharmony_ci	/* ITE8910 USB kbd ctlr, with Synaptics touchpad connected to it. */
1258c2ecf20Sopenharmony_ci	{ HID_DEVICE(BUS_USB, HID_GROUP_GENERIC,
1268c2ecf20Sopenharmony_ci		     USB_VENDOR_ID_SYNAPTICS,
1278c2ecf20Sopenharmony_ci		     USB_DEVICE_ID_SYNAPTICS_ACER_SWITCH5_017),
1288c2ecf20Sopenharmony_ci	  .driver_data = QUIRK_TOUCHPAD_ON_OFF_REPORT },
1298c2ecf20Sopenharmony_ci	{ }
1308c2ecf20Sopenharmony_ci};
1318c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(hid, ite_devices);
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_cistatic struct hid_driver ite_driver = {
1348c2ecf20Sopenharmony_ci	.name = "itetech",
1358c2ecf20Sopenharmony_ci	.id_table = ite_devices,
1368c2ecf20Sopenharmony_ci	.probe = ite_probe,
1378c2ecf20Sopenharmony_ci	.report_fixup = ite_report_fixup,
1388c2ecf20Sopenharmony_ci	.input_mapping = ite_input_mapping,
1398c2ecf20Sopenharmony_ci	.event = ite_event,
1408c2ecf20Sopenharmony_ci};
1418c2ecf20Sopenharmony_cimodule_hid_driver(ite_driver);
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
144