]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - drivers/hid/hid-asus.c
HID: asus: support Republic of Gamers special keys
[mirror_ubuntu-hirsute-kernel.git] / drivers / hid / hid-asus.c
CommitLineData
eeb01a57 1/*
b94f7d5d 2 * HID driver for Asus notebook built-in keyboard.
eeb01a57
YF
3 * Fixes small logical maximum to match usage maximum.
4 *
b94f7d5d
YF
5 * Currently supported devices are:
6 * EeeBook X205TA
7 * VivoBook E200HA
8 *
eeb01a57
YF
9 * Copyright (c) 2016 Yusuke Fujimaki <usk.fujimaki@gmail.com>
10 *
11 * This module based on hid-ortek by
12 * Copyright (c) 2010 Johnathon Harris <jmharris@gmail.com>
13 * Copyright (c) 2011 Jiri Kosina
9ce12d8b
BM
14 *
15 * This module has been updated to add support for Asus i2c touchpad.
16 *
17 * Copyright (c) 2016 Brendan McGrath <redmcg@redmandi.dyndns.org>
18 * Copyright (c) 2016 Victor Vlasenko <victor.vlasenko@sysgears.com>
19 * Copyright (c) 2016 Frederik Wenigwieser <frederik.wenigwieser@gmail.com>
eeb01a57
YF
20 */
21
22/*
23 * This program is free software; you can redistribute it and/or modify it
24 * under the terms of the GNU General Public License as published by the Free
25 * Software Foundation; either version 2 of the License, or (at your option)
26 * any later version.
27 */
28
eeb01a57
YF
29#include <linux/hid.h>
30#include <linux/module.h>
9ce12d8b 31#include <linux/input/mt.h>
eeb01a57
YF
32
33#include "hid-ids.h"
34
9ce12d8b
BM
35MODULE_AUTHOR("Yusuke Fujimaki <usk.fujimaki@gmail.com>");
36MODULE_AUTHOR("Brendan McGrath <redmcg@redmandi.dyndns.org>");
37MODULE_AUTHOR("Victor Vlasenko <victor.vlasenko@sysgears.com>");
38MODULE_AUTHOR("Frederik Wenigwieser <frederik.wenigwieser@gmail.com>");
39MODULE_DESCRIPTION("Asus HID Keyboard and TouchPad");
40
41#define FEATURE_REPORT_ID 0x0d
42#define INPUT_REPORT_ID 0x5d
43
44#define INPUT_REPORT_SIZE 28
45
46#define MAX_CONTACTS 5
47
48#define MAX_X 2794
49#define MAX_Y 1758
50#define MAX_TOUCH_MAJOR 8
51#define MAX_PRESSURE 128
52
53#define CONTACT_DATA_SIZE 5
54
55#define BTN_LEFT_MASK 0x01
56#define CONTACT_TOOL_TYPE_MASK 0x80
57#define CONTACT_X_MSB_MASK 0xf0
58#define CONTACT_Y_MSB_MASK 0x0f
59#define CONTACT_TOUCH_MAJOR_MASK 0x07
60#define CONTACT_PRESSURE_MASK 0x7f
61
62#define QUIRK_FIX_NOTEBOOK_REPORT BIT(0)
63#define QUIRK_NO_INIT_REPORTS BIT(1)
64#define QUIRK_SKIP_INPUT_MAPPING BIT(2)
65#define QUIRK_IS_MULTITOUCH BIT(3)
66
c8b1b3dd
BM
67#define KEYBOARD_QUIRKS (QUIRK_FIX_NOTEBOOK_REPORT | \
68 QUIRK_NO_INIT_REPORTS)
9ce12d8b
BM
69#define TOUCHPAD_QUIRKS (QUIRK_NO_INIT_REPORTS | \
70 QUIRK_SKIP_INPUT_MAPPING | \
71 QUIRK_IS_MULTITOUCH)
72
73#define TRKID_SGN ((TRKID_MAX + 1) >> 1)
74
75struct asus_drvdata {
76 unsigned long quirks;
77 struct input_dev *input;
78};
79
80static void asus_report_contact_down(struct input_dev *input,
81 int toolType, u8 *data)
82{
83 int touch_major, pressure;
84 int x = (data[0] & CONTACT_X_MSB_MASK) << 4 | data[1];
85 int y = MAX_Y - ((data[0] & CONTACT_Y_MSB_MASK) << 8 | data[2]);
86
87 if (toolType == MT_TOOL_PALM) {
88 touch_major = MAX_TOUCH_MAJOR;
89 pressure = MAX_PRESSURE;
90 } else {
91 touch_major = (data[3] >> 4) & CONTACT_TOUCH_MAJOR_MASK;
92 pressure = data[4] & CONTACT_PRESSURE_MASK;
93 }
94
95 input_report_abs(input, ABS_MT_POSITION_X, x);
96 input_report_abs(input, ABS_MT_POSITION_Y, y);
97 input_report_abs(input, ABS_MT_TOUCH_MAJOR, touch_major);
98 input_report_abs(input, ABS_MT_PRESSURE, pressure);
99}
100
101/* Required for Synaptics Palm Detection */
102static void asus_report_tool_width(struct input_dev *input)
103{
104 struct input_mt *mt = input->mt;
105 struct input_mt_slot *oldest;
106 int oldid, count, i;
107
108 oldest = NULL;
109 oldid = mt->trkid;
110 count = 0;
111
112 for (i = 0; i < mt->num_slots; ++i) {
113 struct input_mt_slot *ps = &mt->slots[i];
114 int id = input_mt_get_value(ps, ABS_MT_TRACKING_ID);
115
116 if (id < 0)
117 continue;
118 if ((id - oldid) & TRKID_SGN) {
119 oldest = ps;
120 oldid = id;
121 }
122 count++;
123 }
124
125 if (oldest) {
126 input_report_abs(input, ABS_TOOL_WIDTH,
127 input_mt_get_value(oldest, ABS_MT_TOUCH_MAJOR));
128 }
129}
130
131static void asus_report_input(struct input_dev *input, u8 *data)
132{
133 int i;
134 u8 *contactData = data + 2;
135
136 for (i = 0; i < MAX_CONTACTS; i++) {
137 bool down = !!(data[1] & BIT(i+3));
138 int toolType = contactData[3] & CONTACT_TOOL_TYPE_MASK ?
139 MT_TOOL_PALM : MT_TOOL_FINGER;
140
141 input_mt_slot(input, i);
142 input_mt_report_slot_state(input, toolType, down);
143
144 if (down) {
145 asus_report_contact_down(input, toolType, contactData);
146 contactData += CONTACT_DATA_SIZE;
147 }
148 }
149
150 input_report_key(input, BTN_LEFT, data[1] & BTN_LEFT_MASK);
151 asus_report_tool_width(input);
152
153 input_mt_sync_frame(input);
154 input_sync(input);
155}
156
157static int asus_raw_event(struct hid_device *hdev,
158 struct hid_report *report, u8 *data, int size)
159{
160 struct asus_drvdata *drvdata = hid_get_drvdata(hdev);
161
162 if (drvdata->quirks & QUIRK_IS_MULTITOUCH &&
163 data[0] == INPUT_REPORT_ID &&
164 size == INPUT_REPORT_SIZE) {
165 asus_report_input(drvdata->input, data);
166 return 1;
167 }
168
169 return 0;
170}
171
172static int asus_input_configured(struct hid_device *hdev, struct hid_input *hi)
173{
c8b1b3dd 174 struct input_dev *input = hi->input;
9ce12d8b
BM
175 struct asus_drvdata *drvdata = hid_get_drvdata(hdev);
176
177 if (drvdata->quirks & QUIRK_IS_MULTITOUCH) {
178 int ret;
9ce12d8b
BM
179
180 input_set_abs_params(input, ABS_MT_POSITION_X, 0, MAX_X, 0, 0);
181 input_set_abs_params(input, ABS_MT_POSITION_Y, 0, MAX_Y, 0, 0);
182 input_set_abs_params(input, ABS_TOOL_WIDTH, 0, MAX_TOUCH_MAJOR, 0, 0);
183 input_set_abs_params(input, ABS_MT_TOUCH_MAJOR, 0, MAX_TOUCH_MAJOR, 0, 0);
184 input_set_abs_params(input, ABS_MT_PRESSURE, 0, MAX_PRESSURE, 0, 0);
185
186 __set_bit(BTN_LEFT, input->keybit);
187 __set_bit(INPUT_PROP_BUTTONPAD, input->propbit);
188
189 ret = input_mt_init_slots(input, MAX_CONTACTS, INPUT_MT_POINTER);
190
191 if (ret) {
192 hid_err(hdev, "Asus input mt init slots failed: %d\n", ret);
193 return ret;
194 }
9ce12d8b
BM
195 }
196
c8b1b3dd
BM
197 drvdata->input = input;
198
9ce12d8b
BM
199 return 0;
200}
201
1caccc25
CC
202#define rog_map_key_clear(c) hid_map_usage_clear(hi, usage, bit, \
203 max, EV_KEY, (c))
9ce12d8b
BM
204static int asus_input_mapping(struct hid_device *hdev,
205 struct hid_input *hi, struct hid_field *field,
206 struct hid_usage *usage, unsigned long **bit,
207 int *max)
208{
209 struct asus_drvdata *drvdata = hid_get_drvdata(hdev);
210
211 if (drvdata->quirks & QUIRK_SKIP_INPUT_MAPPING) {
212 /* Don't map anything from the HID report.
213 * We do it all manually in asus_input_configured
214 */
215 return -1;
216 }
217
1caccc25
CC
218 /* ASUS Republic of Gamers laptop keyboard hotkeys */
219 if ((usage->hid & HID_USAGE_PAGE) == 0xff310000) {
220 set_bit(EV_REP, hi->input->evbit);
221 switch (usage->hid & HID_USAGE) {
222 case 0x10: rog_map_key_clear(KEY_BRIGHTNESSDOWN); break;
223 case 0x20: rog_map_key_clear(KEY_BRIGHTNESSUP); break;
224 case 0x35: rog_map_key_clear(KEY_DISPLAY_OFF); break;
225 case 0x6c: rog_map_key_clear(KEY_SLEEP); break;
226 case 0x82: rog_map_key_clear(KEY_CAMERA); break;
227 case 0x88: rog_map_key_clear(KEY_WLAN); break;
228 case 0xb5: rog_map_key_clear(KEY_CALC); break;
229 case 0xc4: rog_map_key_clear(KEY_KBDILLUMUP); break;
230 case 0xc5: rog_map_key_clear(KEY_KBDILLUMDOWN); break;
231
232 /* ASUS touchpad toggle */
233 case 0x6b: rog_map_key_clear(KEY_F21); break;
234
235 /* ROG key */
236 case 0x38: rog_map_key_clear(KEY_PROG1); break;
237
238 /* Fn+C ASUS Splendid */
239 case 0xba: rog_map_key_clear(KEY_PROG2); break;
240
241 /* Fn+Space Power4Gear Hybrid */
242 case 0x5c: rog_map_key_clear(KEY_PROG3); break;
243
244 default:
245 return 0;
246 }
247 return 1;
248 }
249
9ce12d8b
BM
250 return 0;
251}
252
253static int asus_start_multitouch(struct hid_device *hdev)
254{
255 int ret;
256 const unsigned char buf[] = { FEATURE_REPORT_ID, 0x00, 0x03, 0x01, 0x00 };
257 unsigned char *dmabuf = kmemdup(buf, sizeof(buf), GFP_KERNEL);
258
259 if (!dmabuf) {
260 ret = -ENOMEM;
261 hid_err(hdev, "Asus failed to alloc dma buf: %d\n", ret);
262 return ret;
263 }
264
265 ret = hid_hw_raw_request(hdev, dmabuf[0], dmabuf, sizeof(buf),
266 HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
267
268 kfree(dmabuf);
269
270 if (ret != sizeof(buf)) {
271 hid_err(hdev, "Asus failed to start multitouch: %d\n", ret);
272 return ret;
273 }
274
275 return 0;
276}
277
278static int __maybe_unused asus_reset_resume(struct hid_device *hdev)
279{
280 struct asus_drvdata *drvdata = hid_get_drvdata(hdev);
281
282 if (drvdata->quirks & QUIRK_IS_MULTITOUCH)
283 return asus_start_multitouch(hdev);
284
285 return 0;
286}
287
288static int asus_probe(struct hid_device *hdev, const struct hid_device_id *id)
289{
290 int ret;
291 struct asus_drvdata *drvdata;
292
293 drvdata = devm_kzalloc(&hdev->dev, sizeof(*drvdata), GFP_KERNEL);
294 if (drvdata == NULL) {
295 hid_err(hdev, "Can't alloc Asus descriptor\n");
296 return -ENOMEM;
297 }
298
299 hid_set_drvdata(hdev, drvdata);
300
301 drvdata->quirks = id->driver_data;
302
303 if (drvdata->quirks & QUIRK_NO_INIT_REPORTS)
304 hdev->quirks |= HID_QUIRK_NO_INIT_REPORTS;
305
306 ret = hid_parse(hdev);
307 if (ret) {
308 hid_err(hdev, "Asus hid parse failed: %d\n", ret);
309 return ret;
310 }
311
312 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
313 if (ret) {
314 hid_err(hdev, "Asus hw start failed: %d\n", ret);
315 return ret;
316 }
317
318 if (!drvdata->input) {
319 hid_err(hdev, "Asus input not registered\n");
320 ret = -ENOMEM;
321 goto err_stop_hw;
322 }
323
c8b1b3dd
BM
324 if (drvdata->quirks & QUIRK_IS_MULTITOUCH) {
325 drvdata->input->name = "Asus TouchPad";
326 } else {
327 drvdata->input->name = "Asus Keyboard";
328 }
9ce12d8b
BM
329
330 if (drvdata->quirks & QUIRK_IS_MULTITOUCH) {
331 ret = asus_start_multitouch(hdev);
332 if (ret)
333 goto err_stop_hw;
334 }
335
336 return 0;
337err_stop_hw:
338 hid_hw_stop(hdev);
339 return ret;
340}
341
eeb01a57
YF
342static __u8 *asus_report_fixup(struct hid_device *hdev, __u8 *rdesc,
343 unsigned int *rsize)
344{
9ce12d8b
BM
345 struct asus_drvdata *drvdata = hid_get_drvdata(hdev);
346
347 if (drvdata->quirks & QUIRK_FIX_NOTEBOOK_REPORT &&
348 *rsize >= 56 && rdesc[54] == 0x25 && rdesc[55] == 0x65) {
b94f7d5d 349 hid_info(hdev, "Fixing up Asus notebook report descriptor\n");
eeb01a57
YF
350 rdesc[55] = 0xdd;
351 }
352 return rdesc;
353}
354
355static const struct hid_device_id asus_devices[] = {
9ce12d8b 356 { HID_I2C_DEVICE(USB_VENDOR_ID_ASUSTEK,
c8b1b3dd 357 USB_DEVICE_ID_ASUSTEK_NOTEBOOK_KEYBOARD), KEYBOARD_QUIRKS},
9ce12d8b
BM
358 { HID_I2C_DEVICE(USB_VENDOR_ID_ASUSTEK,
359 USB_DEVICE_ID_ASUSTEK_TOUCHPAD), TOUCHPAD_QUIRKS },
1caccc25
CC
360 { HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK,
361 USB_DEVICE_ID_ASUSTEK_ROG_KEYBOARD1) },
362 { HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK,
363 USB_DEVICE_ID_ASUSTEK_ROG_KEYBOARD2) },
eeb01a57
YF
364 { }
365};
366MODULE_DEVICE_TABLE(hid, asus_devices);
367
368static struct hid_driver asus_driver = {
9ce12d8b
BM
369 .name = "asus",
370 .id_table = asus_devices,
371 .report_fixup = asus_report_fixup,
372 .probe = asus_probe,
373 .input_mapping = asus_input_mapping,
374 .input_configured = asus_input_configured,
375#ifdef CONFIG_PM
376 .reset_resume = asus_reset_resume,
377#endif
378 .raw_event = asus_raw_event
eeb01a57
YF
379};
380module_hid_driver(asus_driver);
381
382MODULE_LICENSE("GPL");