xref: /linux/drivers/hid/hid-elan.c (revision 79d11f2a2070613b63a620e4a8b87feb3e5543c3)
19a6a4193SAlexandrov Stansilav /*
29a6a4193SAlexandrov Stansilav  * HID Driver for ELAN Touchpad
39a6a4193SAlexandrov Stansilav  *
49a6a4193SAlexandrov Stansilav  * Currently only supports touchpad found on HP Pavilion X2 10
59a6a4193SAlexandrov Stansilav  *
69a6a4193SAlexandrov Stansilav  * Copyright (c) 2016 Alexandrov Stanislav <neko@nya.ai>
79a6a4193SAlexandrov Stansilav  *
89a6a4193SAlexandrov Stansilav  * This program is free software; you can redistribute it and/or modify it
99a6a4193SAlexandrov Stansilav  * under the terms of the GNU General Public License as published by the Free
109a6a4193SAlexandrov Stansilav  * Software Foundation; either version 2 of the License, or (at your option)
119a6a4193SAlexandrov Stansilav  * any later version.
129a6a4193SAlexandrov Stansilav  */
139a6a4193SAlexandrov Stansilav 
149a6a4193SAlexandrov Stansilav #include <linux/hid.h>
159a6a4193SAlexandrov Stansilav #include <linux/input/mt.h>
169a6a4193SAlexandrov Stansilav #include <linux/leds.h>
179a6a4193SAlexandrov Stansilav #include <linux/module.h>
189a6a4193SAlexandrov Stansilav #include <linux/usb.h>
199a6a4193SAlexandrov Stansilav 
209a6a4193SAlexandrov Stansilav #include "hid-ids.h"
219a6a4193SAlexandrov Stansilav 
229a6a4193SAlexandrov Stansilav #define ELAN_SINGLE_FINGER	0x81
239a6a4193SAlexandrov Stansilav #define ELAN_MT_FIRST_FINGER	0x82
249a6a4193SAlexandrov Stansilav #define ELAN_MT_SECOND_FINGER	0x83
259a6a4193SAlexandrov Stansilav #define ELAN_INPUT_REPORT_SIZE	8
26314f04e8SHans de Goede #define ELAN_MAX_FINGERS	5
272f612de2SHans de Goede #define ELAN_MAX_PRESSURE	255
28314f04e8SHans de Goede #define ELAN_TP_USB_INTF	1
299a6a4193SAlexandrov Stansilav 
30*79d11f2aSHans de Goede #define ELAN_FEATURE_REPORT	0x0d
31*79d11f2aSHans de Goede #define ELAN_FEATURE_SIZE	5
32*79d11f2aSHans de Goede #define ELAN_PARAM_MAX_X	6
33*79d11f2aSHans de Goede #define ELAN_PARAM_MAX_Y	7
34*79d11f2aSHans de Goede #define ELAN_PARAM_RES		8
35*79d11f2aSHans de Goede 
369a6a4193SAlexandrov Stansilav #define ELAN_MUTE_LED_REPORT	0xBC
379a6a4193SAlexandrov Stansilav #define ELAN_LED_REPORT_SIZE	8
389a6a4193SAlexandrov Stansilav 
399a6a4193SAlexandrov Stansilav struct elan_drvdata {
409a6a4193SAlexandrov Stansilav 	struct input_dev *input;
419a6a4193SAlexandrov Stansilav 	u8 prev_report[ELAN_INPUT_REPORT_SIZE];
429a6a4193SAlexandrov Stansilav 	struct led_classdev mute_led;
439a6a4193SAlexandrov Stansilav 	u8 mute_led_state;
44*79d11f2aSHans de Goede 	u16 max_x;
45*79d11f2aSHans de Goede 	u16 max_y;
469a6a4193SAlexandrov Stansilav };
479a6a4193SAlexandrov Stansilav 
489a6a4193SAlexandrov Stansilav static int is_not_elan_touchpad(struct hid_device *hdev)
499a6a4193SAlexandrov Stansilav {
509a6a4193SAlexandrov Stansilav 	struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
519a6a4193SAlexandrov Stansilav 
52314f04e8SHans de Goede 	return (intf->altsetting->desc.bInterfaceNumber != ELAN_TP_USB_INTF);
539a6a4193SAlexandrov Stansilav }
549a6a4193SAlexandrov Stansilav 
559a6a4193SAlexandrov Stansilav static int elan_input_mapping(struct hid_device *hdev, struct hid_input *hi,
569a6a4193SAlexandrov Stansilav 			      struct hid_field *field, struct hid_usage *usage,
579a6a4193SAlexandrov Stansilav 			      unsigned long **bit, int *max)
589a6a4193SAlexandrov Stansilav {
599a6a4193SAlexandrov Stansilav 	if (is_not_elan_touchpad(hdev))
609a6a4193SAlexandrov Stansilav 		return 0;
619a6a4193SAlexandrov Stansilav 
629a6a4193SAlexandrov Stansilav 	if (field->report->id == ELAN_SINGLE_FINGER ||
639a6a4193SAlexandrov Stansilav 	    field->report->id == ELAN_MT_FIRST_FINGER ||
649a6a4193SAlexandrov Stansilav 	    field->report->id == ELAN_MT_SECOND_FINGER)
659a6a4193SAlexandrov Stansilav 		return -1;
669a6a4193SAlexandrov Stansilav 
679a6a4193SAlexandrov Stansilav 	return 0;
689a6a4193SAlexandrov Stansilav }
699a6a4193SAlexandrov Stansilav 
70*79d11f2aSHans de Goede static int elan_get_device_param(struct hid_device *hdev,
71*79d11f2aSHans de Goede 				 unsigned char *dmabuf, unsigned char param)
72*79d11f2aSHans de Goede {
73*79d11f2aSHans de Goede 	int ret;
74*79d11f2aSHans de Goede 
75*79d11f2aSHans de Goede 	dmabuf[0] = ELAN_FEATURE_REPORT;
76*79d11f2aSHans de Goede 	dmabuf[1] = 0x05;
77*79d11f2aSHans de Goede 	dmabuf[2] = 0x03;
78*79d11f2aSHans de Goede 	dmabuf[3] = param;
79*79d11f2aSHans de Goede 	dmabuf[4] = 0x01;
80*79d11f2aSHans de Goede 
81*79d11f2aSHans de Goede 	ret = hid_hw_raw_request(hdev, ELAN_FEATURE_REPORT, dmabuf,
82*79d11f2aSHans de Goede 				 ELAN_FEATURE_SIZE, HID_FEATURE_REPORT,
83*79d11f2aSHans de Goede 				 HID_REQ_SET_REPORT);
84*79d11f2aSHans de Goede 	if (ret != ELAN_FEATURE_SIZE) {
85*79d11f2aSHans de Goede 		hid_err(hdev, "Set report error for parm %d: %d\n", param, ret);
86*79d11f2aSHans de Goede 		return ret;
87*79d11f2aSHans de Goede 	}
88*79d11f2aSHans de Goede 
89*79d11f2aSHans de Goede 	ret = hid_hw_raw_request(hdev, ELAN_FEATURE_REPORT, dmabuf,
90*79d11f2aSHans de Goede 				 ELAN_FEATURE_SIZE, HID_FEATURE_REPORT,
91*79d11f2aSHans de Goede 				 HID_REQ_GET_REPORT);
92*79d11f2aSHans de Goede 	if (ret != ELAN_FEATURE_SIZE) {
93*79d11f2aSHans de Goede 		hid_err(hdev, "Get report error for parm %d: %d\n", param, ret);
94*79d11f2aSHans de Goede 		return ret;
95*79d11f2aSHans de Goede 	}
96*79d11f2aSHans de Goede 
97*79d11f2aSHans de Goede 	return 0;
98*79d11f2aSHans de Goede }
99*79d11f2aSHans de Goede 
100*79d11f2aSHans de Goede static int elan_get_device_params(struct hid_device *hdev)
101*79d11f2aSHans de Goede {
102*79d11f2aSHans de Goede 	struct elan_drvdata *drvdata = hid_get_drvdata(hdev);
103*79d11f2aSHans de Goede 	unsigned char *dmabuf;
104*79d11f2aSHans de Goede 	int ret;
105*79d11f2aSHans de Goede 
106*79d11f2aSHans de Goede 	dmabuf = kmalloc(ELAN_FEATURE_SIZE, GFP_KERNEL);
107*79d11f2aSHans de Goede 	if (!dmabuf)
108*79d11f2aSHans de Goede 		return -ENOMEM;
109*79d11f2aSHans de Goede 
110*79d11f2aSHans de Goede 	ret = elan_get_device_param(hdev, dmabuf, ELAN_PARAM_MAX_X);
111*79d11f2aSHans de Goede 	if (ret)
112*79d11f2aSHans de Goede 		goto err;
113*79d11f2aSHans de Goede 
114*79d11f2aSHans de Goede 	drvdata->max_x = (dmabuf[4] << 8) | dmabuf[3];
115*79d11f2aSHans de Goede 
116*79d11f2aSHans de Goede 	ret = elan_get_device_param(hdev, dmabuf, ELAN_PARAM_MAX_Y);
117*79d11f2aSHans de Goede 	if (ret)
118*79d11f2aSHans de Goede 		goto err;
119*79d11f2aSHans de Goede 
120*79d11f2aSHans de Goede 	drvdata->max_y = (dmabuf[4] << 8) | dmabuf[3];
121*79d11f2aSHans de Goede 
122*79d11f2aSHans de Goede err:
123*79d11f2aSHans de Goede 	kfree(dmabuf);
124*79d11f2aSHans de Goede 	return ret;
125*79d11f2aSHans de Goede }
126*79d11f2aSHans de Goede 
1279a6a4193SAlexandrov Stansilav static int elan_input_configured(struct hid_device *hdev, struct hid_input *hi)
1289a6a4193SAlexandrov Stansilav {
1299a6a4193SAlexandrov Stansilav 	int ret;
1309a6a4193SAlexandrov Stansilav 	struct input_dev *input;
1319a6a4193SAlexandrov Stansilav 	struct elan_drvdata *drvdata = hid_get_drvdata(hdev);
1329a6a4193SAlexandrov Stansilav 
1339a6a4193SAlexandrov Stansilav 	if (is_not_elan_touchpad(hdev))
1349a6a4193SAlexandrov Stansilav 		return 0;
1359a6a4193SAlexandrov Stansilav 
136*79d11f2aSHans de Goede 	ret = elan_get_device_params(hdev);
137*79d11f2aSHans de Goede 	if (ret)
138*79d11f2aSHans de Goede 		return ret;
139*79d11f2aSHans de Goede 
1409a6a4193SAlexandrov Stansilav 	input = devm_input_allocate_device(&hdev->dev);
1419a6a4193SAlexandrov Stansilav 	if (!input)
1429a6a4193SAlexandrov Stansilav 		return -ENOMEM;
1439a6a4193SAlexandrov Stansilav 
1449a6a4193SAlexandrov Stansilav 	input->name = "Elan Touchpad";
1459a6a4193SAlexandrov Stansilav 	input->phys = hdev->phys;
1469a6a4193SAlexandrov Stansilav 	input->uniq = hdev->uniq;
1479a6a4193SAlexandrov Stansilav 	input->id.bustype = hdev->bus;
1489a6a4193SAlexandrov Stansilav 	input->id.vendor  = hdev->vendor;
1499a6a4193SAlexandrov Stansilav 	input->id.product = hdev->product;
1509a6a4193SAlexandrov Stansilav 	input->id.version = hdev->version;
1519a6a4193SAlexandrov Stansilav 	input->dev.parent = &hdev->dev;
1529a6a4193SAlexandrov Stansilav 
153*79d11f2aSHans de Goede 	input_set_abs_params(input, ABS_MT_POSITION_X, 0, drvdata->max_x,
154*79d11f2aSHans de Goede 			     0, 0);
155*79d11f2aSHans de Goede 	input_set_abs_params(input, ABS_MT_POSITION_Y, 0, drvdata->max_y,
156*79d11f2aSHans de Goede 			     0, 0);
1572f612de2SHans de Goede 	input_set_abs_params(input, ABS_MT_PRESSURE, 0, ELAN_MAX_PRESSURE,
1582f612de2SHans de Goede 			     0, 0);
1599a6a4193SAlexandrov Stansilav 
1609a6a4193SAlexandrov Stansilav 	__set_bit(BTN_LEFT, input->keybit);
1619a6a4193SAlexandrov Stansilav 	__set_bit(INPUT_PROP_BUTTONPAD, input->propbit);
1629a6a4193SAlexandrov Stansilav 
163314f04e8SHans de Goede 	ret = input_mt_init_slots(input, ELAN_MAX_FINGERS, INPUT_MT_POINTER);
1649a6a4193SAlexandrov Stansilav 	if (ret) {
1659a6a4193SAlexandrov Stansilav 		hid_err(hdev, "Failed to init elan MT slots: %d\n", ret);
1669a6a4193SAlexandrov Stansilav 		return ret;
1679a6a4193SAlexandrov Stansilav 	}
1689a6a4193SAlexandrov Stansilav 
1699a6a4193SAlexandrov Stansilav 	ret = input_register_device(input);
1709a6a4193SAlexandrov Stansilav 	if (ret) {
1719a6a4193SAlexandrov Stansilav 		hid_err(hdev, "Failed to register elan input device: %d\n",
1729a6a4193SAlexandrov Stansilav 			ret);
1739a6a4193SAlexandrov Stansilav 		input_free_device(input);
1749a6a4193SAlexandrov Stansilav 		return ret;
1759a6a4193SAlexandrov Stansilav 	}
1769a6a4193SAlexandrov Stansilav 
1779a6a4193SAlexandrov Stansilav 	drvdata->input = input;
1789a6a4193SAlexandrov Stansilav 
1799a6a4193SAlexandrov Stansilav 	return 0;
1809a6a4193SAlexandrov Stansilav }
1819a6a4193SAlexandrov Stansilav 
1829a6a4193SAlexandrov Stansilav static void elan_report_mt_slot(struct elan_drvdata *drvdata, u8 *data,
1839a6a4193SAlexandrov Stansilav 				unsigned int slot_num)
1849a6a4193SAlexandrov Stansilav {
1859a6a4193SAlexandrov Stansilav 	struct input_dev *input = drvdata->input;
1862f612de2SHans de Goede 	int x, y, p;
1879a6a4193SAlexandrov Stansilav 
1889a6a4193SAlexandrov Stansilav 	bool active = !!data;
1899a6a4193SAlexandrov Stansilav 
1909a6a4193SAlexandrov Stansilav 	input_mt_slot(input, slot_num);
1919a6a4193SAlexandrov Stansilav 	input_mt_report_slot_state(input, MT_TOOL_FINGER, active);
1929a6a4193SAlexandrov Stansilav 	if (active) {
1939a6a4193SAlexandrov Stansilav 		x = ((data[0] & 0xF0) << 4) | data[1];
194*79d11f2aSHans de Goede 		y = drvdata->max_y -
1959a6a4193SAlexandrov Stansilav 		    (((data[0] & 0x07) << 8) | data[2]);
1962f612de2SHans de Goede 		p = data[4];
1979a6a4193SAlexandrov Stansilav 
1989a6a4193SAlexandrov Stansilav 		input_report_abs(input, ABS_MT_POSITION_X, x);
1999a6a4193SAlexandrov Stansilav 		input_report_abs(input, ABS_MT_POSITION_Y, y);
2002f612de2SHans de Goede 		input_report_abs(input, ABS_MT_PRESSURE, p);
2019a6a4193SAlexandrov Stansilav 	}
2029a6a4193SAlexandrov Stansilav }
2039a6a4193SAlexandrov Stansilav 
2049a6a4193SAlexandrov Stansilav static void elan_report_input(struct elan_drvdata *drvdata, u8 *data)
2059a6a4193SAlexandrov Stansilav {
2069a6a4193SAlexandrov Stansilav 	int i;
2079a6a4193SAlexandrov Stansilav 	struct input_dev *input = drvdata->input;
2089a6a4193SAlexandrov Stansilav 
2099a6a4193SAlexandrov Stansilav 	/*
2109a6a4193SAlexandrov Stansilav 	 * There is 3 types of reports: for single touch,
2119a6a4193SAlexandrov Stansilav 	 * for multitouch - first finger and for multitouch - second finger
2129a6a4193SAlexandrov Stansilav 	 *
2139a6a4193SAlexandrov Stansilav 	 * packet structure for ELAN_SINGLE_FINGER and ELAN_MT_FIRST_FINGER:
2149a6a4193SAlexandrov Stansilav 	 *
2159a6a4193SAlexandrov Stansilav 	 * byte 1: 1   0   0   0   0   0   0   1  // 0x81 or 0x82
2169a6a4193SAlexandrov Stansilav 	 * byte 2: 0   0   0   0   0   0   0   0  // looks like unused
2179a6a4193SAlexandrov Stansilav 	 * byte 3: f5  f4  f3  f2  f1  0   0   L
2189a6a4193SAlexandrov Stansilav 	 * byte 4: x12 x11 x10 x9  0?  y11 y10 y9
2199a6a4193SAlexandrov Stansilav 	 * byte 5: x8  x7  x6  x5  x4  x3  x2  x1
2209a6a4193SAlexandrov Stansilav 	 * byte 6: y8  y7  y6  y5  y4  y3  y2  y1
2219a6a4193SAlexandrov Stansilav 	 * byte 7: sy4 sy3 sy2 sy1 sx4 sx3 sx2 sx1
2222f612de2SHans de Goede 	 * byte 8: p8  p7  p6  p5  p4  p3  p2  p1
2239a6a4193SAlexandrov Stansilav 	 *
2249a6a4193SAlexandrov Stansilav 	 * packet structure for ELAN_MT_SECOND_FINGER:
2259a6a4193SAlexandrov Stansilav 	 *
2269a6a4193SAlexandrov Stansilav 	 * byte 1: 1   0   0   0   0   0   1   1  // 0x83
2279a6a4193SAlexandrov Stansilav 	 * byte 2: x12 x11 x10 x9  0   y11 y10 y9
2289a6a4193SAlexandrov Stansilav 	 * byte 3: x8  x7  x6  x5  x4  x3  x2  x1
2299a6a4193SAlexandrov Stansilav 	 * byte 4: y8  y7  y6  y5  y4  y3  y2  y1
2309a6a4193SAlexandrov Stansilav 	 * byte 5: sy4 sy3 sy2 sy1 sx4 sx3 sx2 sx1
2312f612de2SHans de Goede 	 * byte 6: p8  p7  p6  p5  p4  p3  p2  p1
2329a6a4193SAlexandrov Stansilav 	 * byte 7: 0   0   0   0   0   0   0   0
2339a6a4193SAlexandrov Stansilav 	 * byte 8: 0   0   0   0   0   0   0   0
2349a6a4193SAlexandrov Stansilav 	 *
2359a6a4193SAlexandrov Stansilav 	 * f5-f1: finger touch bits
2369a6a4193SAlexandrov Stansilav 	 * L: clickpad button
2372f612de2SHans de Goede 	 * sy / sx: finger width / height expressed in traces, the total number
2382f612de2SHans de Goede 	 *          of traces can be queried by doing a HID_REQ_SET_REPORT
2392f612de2SHans de Goede 	 *          { 0x0d, 0x05, 0x03, 0x05, 0x01 } followed by a GET, in the
2402f612de2SHans de Goede 	 *          returned buf, buf[3]=no-x-traces, buf[4]=no-y-traces.
2412f612de2SHans de Goede 	 * p: pressure
2429a6a4193SAlexandrov Stansilav 	 */
2439a6a4193SAlexandrov Stansilav 
2449a6a4193SAlexandrov Stansilav 	if (data[0] == ELAN_SINGLE_FINGER) {
245314f04e8SHans de Goede 		for (i = 0; i < ELAN_MAX_FINGERS; i++) {
2469a6a4193SAlexandrov Stansilav 			if (data[2] & BIT(i + 3))
2479a6a4193SAlexandrov Stansilav 				elan_report_mt_slot(drvdata, data + 3, i);
2489a6a4193SAlexandrov Stansilav 			else
2499a6a4193SAlexandrov Stansilav 				elan_report_mt_slot(drvdata, NULL, i);
2509a6a4193SAlexandrov Stansilav 		}
2519a6a4193SAlexandrov Stansilav 		input_report_key(input, BTN_LEFT, data[2] & 0x01);
2529a6a4193SAlexandrov Stansilav 	}
2539a6a4193SAlexandrov Stansilav 	/*
2549a6a4193SAlexandrov Stansilav 	 * When touched with two fingers Elan touchpad will emit two HID reports
2559a6a4193SAlexandrov Stansilav 	 * first is ELAN_MT_FIRST_FINGER and second is ELAN_MT_SECOND_FINGER
2569a6a4193SAlexandrov Stansilav 	 * we will save ELAN_MT_FIRST_FINGER report and wait for
2579a6a4193SAlexandrov Stansilav 	 * ELAN_MT_SECOND_FINGER to finish multitouch
2589a6a4193SAlexandrov Stansilav 	 */
2599a6a4193SAlexandrov Stansilav 	if (data[0] == ELAN_MT_FIRST_FINGER) {
2609a6a4193SAlexandrov Stansilav 		memcpy(drvdata->prev_report, data,
2619a6a4193SAlexandrov Stansilav 		       sizeof(drvdata->prev_report));
2629a6a4193SAlexandrov Stansilav 		return;
2639a6a4193SAlexandrov Stansilav 	}
2649a6a4193SAlexandrov Stansilav 
2659a6a4193SAlexandrov Stansilav 	if (data[0] == ELAN_MT_SECOND_FINGER) {
2669a6a4193SAlexandrov Stansilav 		int first = 0;
2679a6a4193SAlexandrov Stansilav 		u8 *prev_report = drvdata->prev_report;
2689a6a4193SAlexandrov Stansilav 
2699a6a4193SAlexandrov Stansilav 		if (prev_report[0] != ELAN_MT_FIRST_FINGER)
2709a6a4193SAlexandrov Stansilav 			return;
2719a6a4193SAlexandrov Stansilav 
272314f04e8SHans de Goede 		for (i = 0; i < ELAN_MAX_FINGERS; i++) {
2739a6a4193SAlexandrov Stansilav 			if (prev_report[2] & BIT(i + 3)) {
2749a6a4193SAlexandrov Stansilav 				if (!first) {
2759a6a4193SAlexandrov Stansilav 					first = 1;
2769a6a4193SAlexandrov Stansilav 					elan_report_mt_slot(drvdata, prev_report + 3, i);
2779a6a4193SAlexandrov Stansilav 				} else {
2789a6a4193SAlexandrov Stansilav 					elan_report_mt_slot(drvdata, data + 1, i);
2799a6a4193SAlexandrov Stansilav 				}
2809a6a4193SAlexandrov Stansilav 			} else {
2819a6a4193SAlexandrov Stansilav 				elan_report_mt_slot(drvdata, NULL, i);
2829a6a4193SAlexandrov Stansilav 			}
2839a6a4193SAlexandrov Stansilav 		}
2849a6a4193SAlexandrov Stansilav 		input_report_key(input, BTN_LEFT, prev_report[2] & 0x01);
2859a6a4193SAlexandrov Stansilav 	}
2869a6a4193SAlexandrov Stansilav 
2879a6a4193SAlexandrov Stansilav 	input_mt_sync_frame(input);
2889a6a4193SAlexandrov Stansilav 	input_sync(input);
2899a6a4193SAlexandrov Stansilav }
2909a6a4193SAlexandrov Stansilav 
2919a6a4193SAlexandrov Stansilav static int elan_raw_event(struct hid_device *hdev,
2929a6a4193SAlexandrov Stansilav 			  struct hid_report *report, u8 *data, int size)
2939a6a4193SAlexandrov Stansilav {
2949a6a4193SAlexandrov Stansilav 	struct elan_drvdata *drvdata = hid_get_drvdata(hdev);
2959a6a4193SAlexandrov Stansilav 
2969a6a4193SAlexandrov Stansilav 	if (is_not_elan_touchpad(hdev))
2979a6a4193SAlexandrov Stansilav 		return 0;
2989a6a4193SAlexandrov Stansilav 
2999a6a4193SAlexandrov Stansilav 	if (data[0] == ELAN_SINGLE_FINGER ||
3009a6a4193SAlexandrov Stansilav 	    data[0] == ELAN_MT_FIRST_FINGER ||
3019a6a4193SAlexandrov Stansilav 	    data[0] == ELAN_MT_SECOND_FINGER) {
3029a6a4193SAlexandrov Stansilav 		if (size == ELAN_INPUT_REPORT_SIZE) {
3039a6a4193SAlexandrov Stansilav 			elan_report_input(drvdata, data);
3049a6a4193SAlexandrov Stansilav 			return 1;
3059a6a4193SAlexandrov Stansilav 		}
3069a6a4193SAlexandrov Stansilav 	}
3079a6a4193SAlexandrov Stansilav 
3089a6a4193SAlexandrov Stansilav 	return 0;
3099a6a4193SAlexandrov Stansilav }
3109a6a4193SAlexandrov Stansilav 
3119a6a4193SAlexandrov Stansilav static int elan_start_multitouch(struct hid_device *hdev)
3129a6a4193SAlexandrov Stansilav {
3139a6a4193SAlexandrov Stansilav 	int ret;
3149a6a4193SAlexandrov Stansilav 
3159a6a4193SAlexandrov Stansilav 	/*
3169a6a4193SAlexandrov Stansilav 	 * This byte sequence will enable multitouch mode and disable
3179a6a4193SAlexandrov Stansilav 	 * mouse emulation
3189a6a4193SAlexandrov Stansilav 	 */
3199a6a4193SAlexandrov Stansilav 	const unsigned char buf[] = { 0x0D, 0x00, 0x03, 0x21, 0x00 };
3209a6a4193SAlexandrov Stansilav 	unsigned char *dmabuf = kmemdup(buf, sizeof(buf), GFP_KERNEL);
3219a6a4193SAlexandrov Stansilav 
3229a6a4193SAlexandrov Stansilav 	if (!dmabuf)
3239a6a4193SAlexandrov Stansilav 		return -ENOMEM;
3249a6a4193SAlexandrov Stansilav 
3259a6a4193SAlexandrov Stansilav 	ret = hid_hw_raw_request(hdev, dmabuf[0], dmabuf, sizeof(buf),
3269a6a4193SAlexandrov Stansilav 				 HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
3279a6a4193SAlexandrov Stansilav 
3289a6a4193SAlexandrov Stansilav 	kfree(dmabuf);
3299a6a4193SAlexandrov Stansilav 
3309a6a4193SAlexandrov Stansilav 	if (ret != sizeof(buf)) {
3319a6a4193SAlexandrov Stansilav 		hid_err(hdev, "Failed to start multitouch: %d\n", ret);
3329a6a4193SAlexandrov Stansilav 		return ret;
3339a6a4193SAlexandrov Stansilav 	}
3349a6a4193SAlexandrov Stansilav 
3359a6a4193SAlexandrov Stansilav 	return 0;
3369a6a4193SAlexandrov Stansilav }
3379a6a4193SAlexandrov Stansilav 
3389a6a4193SAlexandrov Stansilav static enum led_brightness elan_mute_led_get_brigtness(struct led_classdev *led_cdev)
3399a6a4193SAlexandrov Stansilav {
3409a6a4193SAlexandrov Stansilav 	struct device *dev = led_cdev->dev->parent;
3419a6a4193SAlexandrov Stansilav 	struct hid_device *hdev = to_hid_device(dev);
3429a6a4193SAlexandrov Stansilav 	struct elan_drvdata *drvdata = hid_get_drvdata(hdev);
3439a6a4193SAlexandrov Stansilav 
3449a6a4193SAlexandrov Stansilav 	return drvdata->mute_led_state;
3459a6a4193SAlexandrov Stansilav }
3469a6a4193SAlexandrov Stansilav 
3479a6a4193SAlexandrov Stansilav static int elan_mute_led_set_brigtness(struct led_classdev *led_cdev,
3489a6a4193SAlexandrov Stansilav 				       enum led_brightness value)
3499a6a4193SAlexandrov Stansilav {
3509a6a4193SAlexandrov Stansilav 	int ret;
3519a6a4193SAlexandrov Stansilav 	u8 led_state;
3529a6a4193SAlexandrov Stansilav 	struct device *dev = led_cdev->dev->parent;
3539a6a4193SAlexandrov Stansilav 	struct hid_device *hdev = to_hid_device(dev);
3549a6a4193SAlexandrov Stansilav 	struct elan_drvdata *drvdata = hid_get_drvdata(hdev);
3559a6a4193SAlexandrov Stansilav 
3569a6a4193SAlexandrov Stansilav 	unsigned char *dmabuf = kzalloc(ELAN_LED_REPORT_SIZE, GFP_KERNEL);
3579a6a4193SAlexandrov Stansilav 
3589a6a4193SAlexandrov Stansilav 	if (!dmabuf)
3599a6a4193SAlexandrov Stansilav 		return -ENOMEM;
3609a6a4193SAlexandrov Stansilav 
3619a6a4193SAlexandrov Stansilav 	led_state = !!value;
3629a6a4193SAlexandrov Stansilav 
3639a6a4193SAlexandrov Stansilav 	dmabuf[0] = ELAN_MUTE_LED_REPORT;
3649a6a4193SAlexandrov Stansilav 	dmabuf[1] = 0x02;
3659a6a4193SAlexandrov Stansilav 	dmabuf[2] = led_state;
3669a6a4193SAlexandrov Stansilav 
3679a6a4193SAlexandrov Stansilav 	ret = hid_hw_raw_request(hdev, dmabuf[0], dmabuf, ELAN_LED_REPORT_SIZE,
3689a6a4193SAlexandrov Stansilav 				 HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
3699a6a4193SAlexandrov Stansilav 
3709a6a4193SAlexandrov Stansilav 	kfree(dmabuf);
3719a6a4193SAlexandrov Stansilav 
3729a6a4193SAlexandrov Stansilav 	if (ret != ELAN_LED_REPORT_SIZE) {
3739a6a4193SAlexandrov Stansilav 		hid_err(hdev, "Failed to set mute led brightness: %d\n", ret);
3749a6a4193SAlexandrov Stansilav 		return ret;
3759a6a4193SAlexandrov Stansilav 	}
3769a6a4193SAlexandrov Stansilav 
3779a6a4193SAlexandrov Stansilav 	drvdata->mute_led_state = led_state;
3789a6a4193SAlexandrov Stansilav 	return 0;
3799a6a4193SAlexandrov Stansilav }
3809a6a4193SAlexandrov Stansilav 
3819a6a4193SAlexandrov Stansilav static int elan_init_mute_led(struct hid_device *hdev)
3829a6a4193SAlexandrov Stansilav {
3839a6a4193SAlexandrov Stansilav 	struct elan_drvdata *drvdata = hid_get_drvdata(hdev);
3849a6a4193SAlexandrov Stansilav 	struct led_classdev *mute_led = &drvdata->mute_led;
3859a6a4193SAlexandrov Stansilav 
3869a6a4193SAlexandrov Stansilav 	mute_led->name = "elan:red:mute";
3879a6a4193SAlexandrov Stansilav 	mute_led->brightness_get = elan_mute_led_get_brigtness;
3889a6a4193SAlexandrov Stansilav 	mute_led->brightness_set_blocking = elan_mute_led_set_brigtness;
3899a6a4193SAlexandrov Stansilav 	mute_led->max_brightness = LED_ON;
3909a6a4193SAlexandrov Stansilav 	mute_led->dev = &hdev->dev;
3919a6a4193SAlexandrov Stansilav 
3929a6a4193SAlexandrov Stansilav 	return devm_led_classdev_register(&hdev->dev, mute_led);
3939a6a4193SAlexandrov Stansilav }
3949a6a4193SAlexandrov Stansilav 
3959a6a4193SAlexandrov Stansilav static int elan_probe(struct hid_device *hdev, const struct hid_device_id *id)
3969a6a4193SAlexandrov Stansilav {
3979a6a4193SAlexandrov Stansilav 	int ret;
3989a6a4193SAlexandrov Stansilav 	struct elan_drvdata *drvdata;
3999a6a4193SAlexandrov Stansilav 
4009a6a4193SAlexandrov Stansilav 	drvdata = devm_kzalloc(&hdev->dev, sizeof(*drvdata), GFP_KERNEL);
4019a6a4193SAlexandrov Stansilav 
4029a6a4193SAlexandrov Stansilav 	if (!drvdata)
4039a6a4193SAlexandrov Stansilav 		return -ENOMEM;
4049a6a4193SAlexandrov Stansilav 
4059a6a4193SAlexandrov Stansilav 	hid_set_drvdata(hdev, drvdata);
4069a6a4193SAlexandrov Stansilav 
4079a6a4193SAlexandrov Stansilav 	ret = hid_parse(hdev);
4089a6a4193SAlexandrov Stansilav 	if (ret) {
4099a6a4193SAlexandrov Stansilav 		hid_err(hdev, "Hid Parse failed\n");
4109a6a4193SAlexandrov Stansilav 		return ret;
4119a6a4193SAlexandrov Stansilav 	}
4129a6a4193SAlexandrov Stansilav 
4139a6a4193SAlexandrov Stansilav 	ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
4149a6a4193SAlexandrov Stansilav 	if (ret) {
4159a6a4193SAlexandrov Stansilav 		hid_err(hdev, "Hid hw start failed\n");
4169a6a4193SAlexandrov Stansilav 		return ret;
4179a6a4193SAlexandrov Stansilav 	}
4189a6a4193SAlexandrov Stansilav 
4199a6a4193SAlexandrov Stansilav 	if (is_not_elan_touchpad(hdev))
4209a6a4193SAlexandrov Stansilav 		return 0;
4219a6a4193SAlexandrov Stansilav 
4229a6a4193SAlexandrov Stansilav 	if (!drvdata->input) {
4239a6a4193SAlexandrov Stansilav 		hid_err(hdev, "Input device is not registred\n");
4249a6a4193SAlexandrov Stansilav 		ret = -ENAVAIL;
4259a6a4193SAlexandrov Stansilav 		goto err;
4269a6a4193SAlexandrov Stansilav 	}
4279a6a4193SAlexandrov Stansilav 
4289a6a4193SAlexandrov Stansilav 	ret = elan_start_multitouch(hdev);
4299a6a4193SAlexandrov Stansilav 	if (ret)
4309a6a4193SAlexandrov Stansilav 		goto err;
4319a6a4193SAlexandrov Stansilav 
4329a6a4193SAlexandrov Stansilav 	ret = elan_init_mute_led(hdev);
4339a6a4193SAlexandrov Stansilav 	if (ret)
4349a6a4193SAlexandrov Stansilav 		goto err;
4359a6a4193SAlexandrov Stansilav 
4369a6a4193SAlexandrov Stansilav 	return 0;
4379a6a4193SAlexandrov Stansilav err:
4389a6a4193SAlexandrov Stansilav 	hid_hw_stop(hdev);
4399a6a4193SAlexandrov Stansilav 	return ret;
4409a6a4193SAlexandrov Stansilav }
4419a6a4193SAlexandrov Stansilav 
4429a6a4193SAlexandrov Stansilav static void elan_remove(struct hid_device *hdev)
4439a6a4193SAlexandrov Stansilav {
4449a6a4193SAlexandrov Stansilav 	hid_hw_stop(hdev);
4459a6a4193SAlexandrov Stansilav }
4469a6a4193SAlexandrov Stansilav 
4479a6a4193SAlexandrov Stansilav static const struct hid_device_id elan_devices[] = {
448*79d11f2aSHans de Goede 	{ HID_USB_DEVICE(USB_VENDOR_ID_ELAN, USB_DEVICE_ID_HP_X2_10_COVER) },
4499a6a4193SAlexandrov Stansilav 	{ }
4509a6a4193SAlexandrov Stansilav };
4519a6a4193SAlexandrov Stansilav MODULE_DEVICE_TABLE(hid, elan_devices);
4529a6a4193SAlexandrov Stansilav 
4539a6a4193SAlexandrov Stansilav static struct hid_driver elan_driver = {
4549a6a4193SAlexandrov Stansilav 	.name = "elan",
4559a6a4193SAlexandrov Stansilav 	.id_table = elan_devices,
4569a6a4193SAlexandrov Stansilav 	.input_mapping = elan_input_mapping,
4579a6a4193SAlexandrov Stansilav 	.input_configured = elan_input_configured,
4589a6a4193SAlexandrov Stansilav 	.raw_event = elan_raw_event,
4599a6a4193SAlexandrov Stansilav 	.probe = elan_probe,
4609a6a4193SAlexandrov Stansilav 	.remove = elan_remove,
4619a6a4193SAlexandrov Stansilav };
4629a6a4193SAlexandrov Stansilav 
4639a6a4193SAlexandrov Stansilav module_hid_driver(elan_driver);
4649a6a4193SAlexandrov Stansilav 
4659a6a4193SAlexandrov Stansilav MODULE_LICENSE("GPL");
4669a6a4193SAlexandrov Stansilav MODULE_AUTHOR("Alexandrov Stanislav");
4679a6a4193SAlexandrov Stansilav MODULE_DESCRIPTION("Driver for HID ELAN Touchpads");
468