]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - drivers/hid/wacom_wac.c
HID: wacom: Reset expected and received contact counts at the same time
[mirror_ubuntu-jammy-kernel.git] / drivers / hid / wacom_wac.c
CommitLineData
2874c5fd 1// SPDX-License-Identifier: GPL-2.0-or-later
3bea733a 2/*
4104d13f 3 * drivers/input/tablet/wacom_wac.c
3bea733a 4 *
ee54500d 5 * USB Wacom tablet support - Wacom specific code
3bea733a
PC
6 */
7
8/*
3bea733a 9 */
51269fe8 10
3bea733a 11#include "wacom_wac.h"
51269fe8 12#include "wacom.h"
47c78e89 13#include <linux/input/mt.h>
3bea733a 14
e35fb8c1
PC
15/* resolution for penabled devices */
16#define WACOM_PL_RES 20
17#define WACOM_PENPRTN_RES 40
18#define WACOM_VOLITO_RES 50
19#define WACOM_GRAPHIRE_RES 80
20#define WACOM_INTUOS_RES 100
21#define WACOM_INTUOS3_RES 200
22
fa770340
PC
23/* Newer Cintiq and DTU have an offset between tablet and screen areas */
24#define WACOM_DTU_OFFSET 200
25#define WACOM_CINTIQ_OFFSET 400
26
387142bb
BT
27/*
28 * Scale factor relating reported contact size to logical contact area.
4e904954
JG
29 * 2^14/pi is a good approximation on Intuos5 and 3rd-gen Bamboo
30 */
31#define WACOM_CONTACT_AREA_SCALE 2607
32
1924e05e
PC
33static bool touch_arbitration = 1;
34module_param(touch_arbitration, bool, 0644);
35MODULE_PARM_DESC(touch_arbitration, " on (Y) off (N)");
36
c7f0522a
JG
37static void wacom_report_numbered_buttons(struct input_dev *input_dev,
38 int button_count, int mask);
39
5922e613
JG
40static int wacom_numbered_button_to_key(int n);
41
10c55cac
AAS
42static void wacom_update_led(struct wacom *wacom, int button_count, int mask,
43 int group);
387142bb
BT
44/*
45 * Percent of battery capacity for Graphire.
46 * 8th value means AC online and show 100% capacity.
47 */
48static unsigned short batcap_gr[8] = { 1, 15, 25, 35, 50, 70, 100, 100 };
49
81af7e61
BT
50/*
51 * Percent of battery capacity for Intuos4 WL, AC has a separate bit.
52 */
53static unsigned short batcap_i4[8] = { 1, 15, 30, 45, 60, 70, 85, 100 };
54
59d69bc8 55static void __wacom_notify_battery(struct wacom_battery *battery,
16e45989
JG
56 int bat_status, int bat_capacity,
57 bool bat_charging, bool bat_connected,
58 bool ps_connected)
59d69bc8 59{
16e45989
JG
60 bool changed = battery->bat_status != bat_status ||
61 battery->battery_capacity != bat_capacity ||
59d69bc8
BT
62 battery->bat_charging != bat_charging ||
63 battery->bat_connected != bat_connected ||
64 battery->ps_connected != ps_connected;
65
66 if (changed) {
16e45989 67 battery->bat_status = bat_status;
59d69bc8
BT
68 battery->battery_capacity = bat_capacity;
69 battery->bat_charging = bat_charging;
70 battery->bat_connected = bat_connected;
71 battery->ps_connected = ps_connected;
72
73 if (battery->battery)
74 power_supply_changed(battery->battery);
75 }
76}
77
953f2c5f 78static void wacom_notify_battery(struct wacom_wac *wacom_wac,
16e45989
JG
79 int bat_status, int bat_capacity, bool bat_charging,
80 bool bat_connected, bool ps_connected)
953f2c5f
JG
81{
82 struct wacom *wacom = container_of(wacom_wac, struct wacom, wacom_wac);
953f2c5f 83
16e45989
JG
84 __wacom_notify_battery(&wacom->battery, bat_status, bat_capacity,
85 bat_charging, bat_connected, ps_connected);
953f2c5f
JG
86}
87
95dd3b30 88static int wacom_penpartner_irq(struct wacom_wac *wacom)
3bea733a
PC
89{
90 unsigned char *data = wacom->data;
2a6cdbdd 91 struct input_dev *input = wacom->pen_input;
3bea733a
PC
92
93 switch (data[0]) {
73a97f4f
DT
94 case 1:
95 if (data[5] & 0x80) {
96 wacom->tool[0] = (data[5] & 0x20) ? BTN_TOOL_RUBBER : BTN_TOOL_PEN;
97 wacom->id[0] = (data[5] & 0x20) ? ERASER_DEVICE_ID : STYLUS_DEVICE_ID;
8da23fc1
DT
98 input_report_key(input, wacom->tool[0], 1);
99 input_report_abs(input, ABS_MISC, wacom->id[0]); /* report tool id */
252f7769
DT
100 input_report_abs(input, ABS_X, get_unaligned_le16(&data[1]));
101 input_report_abs(input, ABS_Y, get_unaligned_le16(&data[3]));
8da23fc1
DT
102 input_report_abs(input, ABS_PRESSURE, (signed char)data[6] + 127);
103 input_report_key(input, BTN_TOUCH, ((signed char)data[6] > -127));
104 input_report_key(input, BTN_STYLUS, (data[5] & 0x40));
73a97f4f 105 } else {
8da23fc1
DT
106 input_report_key(input, wacom->tool[0], 0);
107 input_report_abs(input, ABS_MISC, 0); /* report tool id */
108 input_report_abs(input, ABS_PRESSURE, -1);
109 input_report_key(input, BTN_TOUCH, 0);
73a97f4f
DT
110 }
111 break;
8da23fc1 112
73a97f4f 113 case 2:
8da23fc1
DT
114 input_report_key(input, BTN_TOOL_PEN, 1);
115 input_report_abs(input, ABS_MISC, STYLUS_DEVICE_ID); /* report tool id */
252f7769
DT
116 input_report_abs(input, ABS_X, get_unaligned_le16(&data[1]));
117 input_report_abs(input, ABS_Y, get_unaligned_le16(&data[3]));
8da23fc1
DT
118 input_report_abs(input, ABS_PRESSURE, (signed char)data[6] + 127);
119 input_report_key(input, BTN_TOUCH, ((signed char)data[6] > -80) && !(data[5] & 0x20));
120 input_report_key(input, BTN_STYLUS, (data[5] & 0x40));
73a97f4f 121 break;
8da23fc1 122
73a97f4f 123 default:
eb71d1bb
DT
124 dev_dbg(input->dev.parent,
125 "%s: received unknown report #%d\n", __func__, data[0]);
73a97f4f 126 return 0;
3bea733a 127 }
8da23fc1 128
3bea733a
PC
129 return 1;
130}
131
95dd3b30 132static int wacom_pl_irq(struct wacom_wac *wacom)
3bea733a 133{
e33da8a5 134 struct wacom_features *features = &wacom->features;
3bea733a 135 unsigned char *data = wacom->data;
2a6cdbdd 136 struct input_dev *input = wacom->pen_input;
e34b9d2f 137 int prox, pressure;
3bea733a 138
cad74700 139 if (data[0] != WACOM_REPORT_PENABLED) {
eb71d1bb
DT
140 dev_dbg(input->dev.parent,
141 "%s: received unknown report #%d\n", __func__, data[0]);
3bea733a
PC
142 return 0;
143 }
144
145 prox = data[1] & 0x40;
146
025bcc15
JG
147 if (!wacom->id[0]) {
148 if ((data[0] & 0x10) || (data[4] & 0x20)) {
149 wacom->tool[0] = BTN_TOOL_RUBBER;
150 wacom->id[0] = ERASER_DEVICE_ID;
3bea733a 151 }
025bcc15
JG
152 else {
153 wacom->tool[0] = BTN_TOOL_PEN;
e34b9d2f 154 wacom->id[0] = STYLUS_DEVICE_ID;
3bea733a 155 }
3bea733a 156 }
8da23fc1 157
025bcc15
JG
158 /* If the eraser is in prox, STYLUS2 is always set. If we
159 * mis-detected the type and notice that STYLUS2 isn't set
160 * then force the eraser out of prox and let the pen in.
161 */
162 if (wacom->tool[0] == BTN_TOOL_RUBBER && !(data[4] & 0x20)) {
163 input_report_key(input, BTN_TOOL_RUBBER, 0);
164 input_report_abs(input, ABS_MISC, 0);
165 input_sync(input);
166 wacom->tool[0] = BTN_TOOL_PEN;
167 wacom->id[0] = STYLUS_DEVICE_ID;
3bea733a
PC
168 }
169
282e4637
JG
170 if (prox) {
171 pressure = (signed char)((data[7] << 1) | ((data[4] >> 2) & 1));
172 if (features->pressure_max > 255)
173 pressure = (pressure << 1) | ((data[4] >> 6) & 1);
174 pressure += (features->pressure_max + 1) / 2;
175
176 input_report_abs(input, ABS_X, data[3] | (data[2] << 7) | ((data[1] & 0x03) << 14));
177 input_report_abs(input, ABS_Y, data[6] | (data[5] << 7) | ((data[4] & 0x03) << 14));
178 input_report_abs(input, ABS_PRESSURE, pressure);
179
180 input_report_key(input, BTN_TOUCH, data[4] & 0x08);
181 input_report_key(input, BTN_STYLUS, data[4] & 0x10);
182 /* Only allow the stylus2 button to be reported for the pen tool. */
183 input_report_key(input, BTN_STYLUS2, (wacom->tool[0] == BTN_TOOL_PEN) && (data[4] & 0x20));
184 }
025bcc15
JG
185
186 if (!prox)
187 wacom->id[0] = 0;
188 input_report_key(input, wacom->tool[0], prox);
189 input_report_abs(input, ABS_MISC, wacom->id[0]);
3bea733a
PC
190 return 1;
191}
192
95dd3b30 193static int wacom_ptu_irq(struct wacom_wac *wacom)
3bea733a
PC
194{
195 unsigned char *data = wacom->data;
2a6cdbdd 196 struct input_dev *input = wacom->pen_input;
3bea733a 197
cad74700 198 if (data[0] != WACOM_REPORT_PENABLED) {
eb71d1bb
DT
199 dev_dbg(input->dev.parent,
200 "%s: received unknown report #%d\n", __func__, data[0]);
3bea733a
PC
201 return 0;
202 }
203
3bea733a 204 if (data[1] & 0x04) {
8da23fc1
DT
205 input_report_key(input, BTN_TOOL_RUBBER, data[1] & 0x20);
206 input_report_key(input, BTN_TOUCH, data[1] & 0x08);
e34b9d2f 207 wacom->id[0] = ERASER_DEVICE_ID;
3bea733a 208 } else {
8da23fc1
DT
209 input_report_key(input, BTN_TOOL_PEN, data[1] & 0x20);
210 input_report_key(input, BTN_TOUCH, data[1] & 0x01);
e34b9d2f 211 wacom->id[0] = STYLUS_DEVICE_ID;
3bea733a 212 }
8da23fc1 213 input_report_abs(input, ABS_MISC, wacom->id[0]); /* report tool id */
252f7769
DT
214 input_report_abs(input, ABS_X, le16_to_cpup((__le16 *)&data[2]));
215 input_report_abs(input, ABS_Y, le16_to_cpup((__le16 *)&data[4]));
216 input_report_abs(input, ABS_PRESSURE, le16_to_cpup((__le16 *)&data[6]));
8da23fc1
DT
217 input_report_key(input, BTN_STYLUS, data[1] & 0x02);
218 input_report_key(input, BTN_STYLUS2, data[1] & 0x10);
3bea733a
PC
219 return 1;
220}
221
c8f2edc5
PC
222static int wacom_dtu_irq(struct wacom_wac *wacom)
223{
74b63417 224 unsigned char *data = wacom->data;
2a6cdbdd 225 struct input_dev *input = wacom->pen_input;
74b63417 226 int prox = data[1] & 0x20;
c8f2edc5 227
eb71d1bb
DT
228 dev_dbg(input->dev.parent,
229 "%s: received report #%d", __func__, data[0]);
c8f2edc5
PC
230
231 if (prox) {
232 /* Going into proximity select tool */
233 wacom->tool[0] = (data[1] & 0x0c) ? BTN_TOOL_RUBBER : BTN_TOOL_PEN;
234 if (wacom->tool[0] == BTN_TOOL_PEN)
235 wacom->id[0] = STYLUS_DEVICE_ID;
236 else
237 wacom->id[0] = ERASER_DEVICE_ID;
238 }
239 input_report_key(input, BTN_STYLUS, data[1] & 0x02);
240 input_report_key(input, BTN_STYLUS2, data[1] & 0x10);
241 input_report_abs(input, ABS_X, le16_to_cpup((__le16 *)&data[2]));
242 input_report_abs(input, ABS_Y, le16_to_cpup((__le16 *)&data[4]));
74b63417 243 input_report_abs(input, ABS_PRESSURE, ((data[7] & 0x01) << 8) | data[6]);
c8f2edc5
PC
244 input_report_key(input, BTN_TOUCH, data[1] & 0x05);
245 if (!prox) /* out-prox */
246 wacom->id[0] = 0;
247 input_report_key(input, wacom->tool[0], prox);
248 input_report_abs(input, ABS_MISC, wacom->id[0]);
249 return 1;
250}
251
497ab1f2
PC
252static int wacom_dtus_irq(struct wacom_wac *wacom)
253{
073b50bc 254 unsigned char *data = wacom->data;
2a6cdbdd 255 struct input_dev *input = wacom->pen_input;
497ab1f2
PC
256 unsigned short prox, pressure = 0;
257
258 if (data[0] != WACOM_REPORT_DTUS && data[0] != WACOM_REPORT_DTUSPAD) {
259 dev_dbg(input->dev.parent,
260 "%s: received unknown report #%d", __func__, data[0]);
261 return 0;
262 } else if (data[0] == WACOM_REPORT_DTUSPAD) {
422b0314 263 input = wacom->pad_input;
497ab1f2
PC
264 input_report_key(input, BTN_0, (data[1] & 0x01));
265 input_report_key(input, BTN_1, (data[1] & 0x02));
266 input_report_key(input, BTN_2, (data[1] & 0x04));
267 input_report_key(input, BTN_3, (data[1] & 0x08));
268 input_report_abs(input, ABS_MISC,
269 data[1] & 0x0f ? PAD_DEVICE_ID : 0);
497ab1f2
PC
270 return 1;
271 } else {
272 prox = data[1] & 0x80;
273 if (prox) {
274 switch ((data[1] >> 3) & 3) {
275 case 1: /* Rubber */
276 wacom->tool[0] = BTN_TOOL_RUBBER;
277 wacom->id[0] = ERASER_DEVICE_ID;
278 break;
279
280 case 2: /* Pen */
281 wacom->tool[0] = BTN_TOOL_PEN;
282 wacom->id[0] = STYLUS_DEVICE_ID;
283 break;
284 }
285 }
286
287 input_report_key(input, BTN_STYLUS, data[1] & 0x20);
288 input_report_key(input, BTN_STYLUS2, data[1] & 0x40);
289 input_report_abs(input, ABS_X, get_unaligned_be16(&data[3]));
290 input_report_abs(input, ABS_Y, get_unaligned_be16(&data[5]));
291 pressure = ((data[1] & 0x03) << 8) | (data[2] & 0xff);
292 input_report_abs(input, ABS_PRESSURE, pressure);
293 input_report_key(input, BTN_TOUCH, pressure > 10);
294
295 if (!prox) /* out-prox */
296 wacom->id[0] = 0;
297 input_report_key(input, wacom->tool[0], prox);
298 input_report_abs(input, ABS_MISC, wacom->id[0]);
497ab1f2
PC
299 return 1;
300 }
301}
302
95dd3b30 303static int wacom_graphire_irq(struct wacom_wac *wacom)
3bea733a 304{
e33da8a5 305 struct wacom_features *features = &wacom->features;
3bea733a 306 unsigned char *data = wacom->data;
2a6cdbdd 307 struct input_dev *input = wacom->pen_input;
3813810c 308 struct input_dev *pad_input = wacom->pad_input;
387142bb 309 int battery_capacity, ps_connected;
252f7769 310 int prox;
3b57ca0f
PC
311 int rw = 0;
312 int retval = 0;
3bea733a 313
387142bb
BT
314 if (features->type == GRAPHIRE_BT) {
315 if (data[0] != WACOM_REPORT_PENABLED_BT) {
316 dev_dbg(input->dev.parent,
317 "%s: received unknown report #%d\n", __func__,
318 data[0]);
319 goto exit;
320 }
321 } else if (data[0] != WACOM_REPORT_PENABLED) {
eb71d1bb
DT
322 dev_dbg(input->dev.parent,
323 "%s: received unknown report #%d\n", __func__, data[0]);
3b57ca0f 324 goto exit;
3bea733a
PC
325 }
326
3b57ca0f
PC
327 prox = data[1] & 0x80;
328 if (prox || wacom->id[0]) {
329 if (prox) {
330 switch ((data[1] >> 5) & 3) {
3bea733a
PC
331
332 case 0: /* Pen */
333 wacom->tool[0] = BTN_TOOL_PEN;
e34b9d2f 334 wacom->id[0] = STYLUS_DEVICE_ID;
3bea733a
PC
335 break;
336
337 case 1: /* Rubber */
338 wacom->tool[0] = BTN_TOOL_RUBBER;
e34b9d2f 339 wacom->id[0] = ERASER_DEVICE_ID;
3bea733a
PC
340 break;
341
342 case 2: /* Mouse with wheel */
8da23fc1 343 input_report_key(input, BTN_MIDDLE, data[1] & 0x04);
df561f66 344 fallthrough;
3bea733a
PC
345
346 case 3: /* Mouse without wheel */
347 wacom->tool[0] = BTN_TOOL_MOUSE;
e34b9d2f 348 wacom->id[0] = CURSOR_DEVICE_ID;
3bea733a 349 break;
3b57ca0f 350 }
3bea733a 351 }
252f7769
DT
352 input_report_abs(input, ABS_X, le16_to_cpup((__le16 *)&data[2]));
353 input_report_abs(input, ABS_Y, le16_to_cpup((__le16 *)&data[4]));
3bea733a 354 if (wacom->tool[0] != BTN_TOOL_MOUSE) {
387142bb
BT
355 if (features->type == GRAPHIRE_BT)
356 input_report_abs(input, ABS_PRESSURE, data[6] |
357 (((__u16) (data[1] & 0x08)) << 5));
358 else
359 input_report_abs(input, ABS_PRESSURE, data[6] |
360 ((data[7] & 0x03) << 8));
8da23fc1
DT
361 input_report_key(input, BTN_TOUCH, data[1] & 0x01);
362 input_report_key(input, BTN_STYLUS, data[1] & 0x02);
363 input_report_key(input, BTN_STYLUS2, data[1] & 0x04);
afb567e3 364 } else {
8da23fc1
DT
365 input_report_key(input, BTN_LEFT, data[1] & 0x01);
366 input_report_key(input, BTN_RIGHT, data[1] & 0x02);
3b57ca0f
PC
367 if (features->type == WACOM_G4 ||
368 features->type == WACOM_MO) {
8da23fc1 369 input_report_abs(input, ABS_DISTANCE, data[6] & 0x3f);
d9f66c1a 370 rw = (data[7] & 0x04) - (data[7] & 0x03);
387142bb
BT
371 } else if (features->type == GRAPHIRE_BT) {
372 /* Compute distance between mouse and tablet */
373 rw = 44 - (data[6] >> 2);
374 rw = clamp_val(rw, 0, 31);
375 input_report_abs(input, ABS_DISTANCE, rw);
376 if (((data[1] >> 5) & 3) == 2) {
377 /* Mouse with wheel */
378 input_report_key(input, BTN_MIDDLE,
379 data[1] & 0x04);
380 rw = (data[6] & 0x01) ? -1 :
381 (data[6] & 0x02) ? 1 : 0;
382 } else {
383 rw = 0;
384 }
3b57ca0f 385 } else {
8da23fc1 386 input_report_abs(input, ABS_DISTANCE, data[7] & 0x3f);
d9f66c1a 387 rw = -(signed char)data[6];
3b57ca0f 388 }
8da23fc1 389 input_report_rel(input, REL_WHEEL, rw);
afb567e3 390 }
3b57ca0f
PC
391
392 if (!prox)
393 wacom->id[0] = 0;
8da23fc1
DT
394 input_report_abs(input, ABS_MISC, wacom->id[0]); /* report tool id */
395 input_report_key(input, wacom->tool[0], prox);
396 input_sync(input); /* sync last event */
80d4e8e9 397 }
3bea733a
PC
398
399 /* send pad data */
e33da8a5 400 switch (features->type) {
73a97f4f 401 case WACOM_G4:
3b57ca0f
PC
402 prox = data[7] & 0xf8;
403 if (prox || wacom->id[1]) {
e34b9d2f 404 wacom->id[1] = PAD_DEVICE_ID;
3813810c
BT
405 input_report_key(pad_input, BTN_BACK, (data[7] & 0x40));
406 input_report_key(pad_input, BTN_FORWARD, (data[7] & 0x80));
3bea733a 407 rw = ((data[7] & 0x18) >> 3) - ((data[7] & 0x20) >> 3);
3813810c 408 input_report_rel(pad_input, REL_WHEEL, rw);
3b57ca0f
PC
409 if (!prox)
410 wacom->id[1] = 0;
3813810c 411 input_report_abs(pad_input, ABS_MISC, wacom->id[1]);
ab687b18 412 retval = 1;
3bea733a 413 }
7ecfbfd3 414 break;
73a97f4f
DT
415
416 case WACOM_MO:
3b57ca0f
PC
417 prox = (data[7] & 0xf8) || data[8];
418 if (prox || wacom->id[1]) {
e34b9d2f 419 wacom->id[1] = PAD_DEVICE_ID;
3813810c
BT
420 input_report_key(pad_input, BTN_BACK, (data[7] & 0x08));
421 input_report_key(pad_input, BTN_LEFT, (data[7] & 0x20));
422 input_report_key(pad_input, BTN_FORWARD, (data[7] & 0x10));
423 input_report_key(pad_input, BTN_RIGHT, (data[7] & 0x40));
424 input_report_abs(pad_input, ABS_WHEEL, (data[8] & 0x7f));
3b57ca0f
PC
425 if (!prox)
426 wacom->id[1] = 0;
3813810c 427 input_report_abs(pad_input, ABS_MISC, wacom->id[1]);
84460014 428 retval = 1;
7ecfbfd3
PC
429 }
430 break;
387142bb
BT
431 case GRAPHIRE_BT:
432 prox = data[7] & 0x03;
433 if (prox || wacom->id[1]) {
434 wacom->id[1] = PAD_DEVICE_ID;
435 input_report_key(pad_input, BTN_0, (data[7] & 0x02));
436 input_report_key(pad_input, BTN_1, (data[7] & 0x01));
3b57ca0f
PC
437 if (!prox)
438 wacom->id[1] = 0;
387142bb 439 input_report_abs(pad_input, ABS_MISC, wacom->id[1]);
84460014 440 retval = 1;
7ecfbfd3
PC
441 }
442 break;
3bea733a 443 }
387142bb
BT
444
445 /* Store current battery capacity and power supply state */
446 if (features->type == GRAPHIRE_BT) {
447 rw = (data[7] >> 2 & 0x07);
448 battery_capacity = batcap_gr[rw];
449 ps_connected = rw == 7;
16e45989
JG
450 wacom_notify_battery(wacom, WACOM_POWER_SUPPLY_STATUS_AUTO,
451 battery_capacity, ps_connected, 1,
452 ps_connected);
3bea733a 453 }
3b57ca0f
PC
454exit:
455 return retval;
3bea733a
PC
456}
457
5fcad167
BT
458static void wacom_intuos_schedule_prox_event(struct wacom_wac *wacom_wac)
459{
460 struct wacom *wacom = container_of(wacom_wac, struct wacom, wacom_wac);
0bbfe28a 461 struct wacom_features *features = &wacom_wac->features;
5fcad167
BT
462 struct hid_report *r;
463 struct hid_report_enum *re;
464
465 re = &(wacom->hdev->report_enum[HID_FEATURE_REPORT]);
0bbfe28a
JG
466 if (features->type == INTUOSHT2)
467 r = re->report_id_hash[WACOM_REPORT_INTUOSHT2_ID];
468 else
469 r = re->report_id_hash[WACOM_REPORT_INTUOS_ID1];
5fcad167
BT
470 if (r) {
471 hid_hw_request(wacom->hdev, r, HID_REQ_GET_REPORT);
472 }
473}
474
fb013a01
JG
475static int wacom_intuos_pad(struct wacom_wac *wacom)
476{
477 struct wacom_features *features = &wacom->features;
478 unsigned char *data = wacom->data;
479 struct input_dev *input = wacom->pad_input;
c7f0522a
JG
480 int i;
481 int buttons = 0, nbuttons = features->numbered_buttons;
482 int keys = 0, nkeys = 0;
483 int ring1 = 0, ring2 = 0;
484 int strip1 = 0, strip2 = 0;
485 bool prox = false;
670e9092
AAS
486 bool wrench = false, keyboard = false, mute_touch = false, menu = false,
487 info = false;
fb013a01
JG
488
489 /* pad packets. Works as a second tool and is always in prox */
490 if (!(data[0] == WACOM_REPORT_INTUOSPAD || data[0] == WACOM_REPORT_INTUOS5PAD ||
491 data[0] == WACOM_REPORT_CINTIQPAD))
492 return 0;
493
494 if (features->type >= INTUOS4S && features->type <= INTUOS4L) {
c7f0522a
JG
495 buttons = (data[3] << 1) | (data[2] & 0x01);
496 ring1 = data[1];
fb013a01 497 } else if (features->type == DTK) {
c7f0522a 498 buttons = data[6];
fb013a01 499 } else if (features->type == WACOM_13HD) {
c7f0522a 500 buttons = (data[4] << 1) | (data[3] & 0x01);
fb013a01 501 } else if (features->type == WACOM_24HD) {
c7f0522a
JG
502 buttons = (data[8] << 8) | data[6];
503 ring1 = data[1];
504 ring2 = data[2];
fb013a01
JG
505
506 /*
507 * Three "buttons" are available on the 24HD which are
508 * physically implemented as a touchstrip. Each button
509 * is approximately 3 bits wide with a 2 bit spacing.
510 * The raw touchstrip bits are stored at:
511 * ((data[3] & 0x1f) << 8) | data[4])
512 */
c7f0522a
JG
513 nkeys = 3;
514 keys = ((data[3] & 0x1C) ? 1<<2 : 0) |
515 ((data[4] & 0xE0) ? 1<<1 : 0) |
516 ((data[4] & 0x07) ? 1<<0 : 0);
670e9092
AAS
517 keyboard = !!(data[4] & 0xE0);
518 info = !!(data[3] & 0x1C);
519
520 if (features->oPid) {
521 mute_touch = !!(data[4] & 0x07);
522 if (mute_touch)
523 wacom->shared->is_touch_on =
524 !wacom->shared->is_touch_on;
525 } else {
526 wrench = !!(data[4] & 0x07);
527 }
fb013a01 528 } else if (features->type == WACOM_27QHD) {
c7f0522a
JG
529 nkeys = 3;
530 keys = data[2] & 0x07;
fb013a01 531
670e9092
AAS
532 wrench = !!(data[2] & 0x01);
533 keyboard = !!(data[2] & 0x02);
534
535 if (features->oPid) {
536 mute_touch = !!(data[2] & 0x04);
537 if (mute_touch)
538 wacom->shared->is_touch_on =
539 !wacom->shared->is_touch_on;
540 } else {
541 menu = !!(data[2] & 0x04);
542 }
fb013a01
JG
543 input_report_abs(input, ABS_X, be16_to_cpup((__be16 *)&data[4]));
544 input_report_abs(input, ABS_Y, be16_to_cpup((__be16 *)&data[6]));
545 input_report_abs(input, ABS_Z, be16_to_cpup((__be16 *)&data[8]));
fb013a01
JG
546 } else if (features->type == CINTIQ_HYBRID) {
547 /*
548 * Do not send hardware buttons under Android. They
549 * are already sent to the system through GPIO (and
550 * have different meaning).
c7f0522a
JG
551 *
552 * d-pad right -> data[4] & 0x10
553 * d-pad up -> data[4] & 0x20
554 * d-pad left -> data[4] & 0x40
555 * d-pad down -> data[4] & 0x80
556 * d-pad center -> data[3] & 0x01
fb013a01 557 */
c7f0522a 558 buttons = (data[4] << 1) | (data[3] & 0x01);
fb013a01 559 } else if (features->type == CINTIQ_COMPANION_2) {
693c3dab
AAS
560 /* d-pad right -> data[2] & 0x10
561 * d-pad up -> data[2] & 0x20
562 * d-pad left -> data[2] & 0x40
563 * d-pad down -> data[2] & 0x80
564 * d-pad center -> data[1] & 0x01
c7f0522a 565 */
0402b6b7 566 buttons = ((data[2] >> 4) << 7) |
693c3dab 567 ((data[1] & 0x04) << 4) |
c7f0522a
JG
568 ((data[2] & 0x0F) << 2) |
569 (data[1] & 0x03);
fb013a01 570 } else if (features->type >= INTUOS5S && features->type <= INTUOSPL) {
fb013a01
JG
571 /*
572 * ExpressKeys on Intuos5/Intuos Pro have a capacitive sensor in
573 * addition to the mechanical switch. Switch data is
574 * stored in data[4], capacitive data in data[5].
c7f0522a
JG
575 *
576 * Touch ring mode switch (data[3]) has no capacitive sensor
fb013a01 577 */
c7f0522a
JG
578 buttons = (data[4] << 1) | (data[3] & 0x01);
579 ring1 = data[2];
fb013a01
JG
580 } else {
581 if (features->type == WACOM_21UX2 || features->type == WACOM_22HD) {
c7f0522a
JG
582 buttons = (data[8] << 10) | ((data[7] & 0x01) << 9) |
583 (data[6] << 1) | (data[5] & 0x01);
fb013a01
JG
584
585 if (features->type == WACOM_22HD) {
c7f0522a
JG
586 nkeys = 3;
587 keys = data[9] & 0x07;
670e9092
AAS
588
589 info = !!(data[9] & 0x01);
590 wrench = !!(data[9] & 0x02);
fb013a01
JG
591 }
592 } else {
ce06760b
PC
593 buttons = ((data[6] & 0x10) << 5) |
594 ((data[5] & 0x10) << 4) |
c7f0522a
JG
595 ((data[6] & 0x0F) << 4) |
596 (data[5] & 0x0F);
fb013a01 597 }
f73d08d0
JG
598 strip1 = ((data[1] & 0x1f) << 8) | data[2];
599 strip2 = ((data[3] & 0x1f) << 8) | data[4];
fb013a01 600 }
c7f0522a 601
073b50bc 602 prox = (buttons & ~(~0U << nbuttons)) | (keys & ~(~0U << nkeys)) |
8f9cfdd3 603 (ring1 & 0x80) | (ring2 & 0x80) | strip1 | strip2;
c7f0522a
JG
604
605 wacom_report_numbered_buttons(input, nbuttons, buttons);
606
607 for (i = 0; i < nkeys; i++)
608 input_report_key(input, KEY_PROG1 + i, keys & (1 << i));
609
670e9092
AAS
610 input_report_key(input, KEY_BUTTONCONFIG, wrench);
611 input_report_key(input, KEY_ONSCREEN_KEYBOARD, keyboard);
612 input_report_key(input, KEY_CONTROLPANEL, menu);
613 input_report_key(input, KEY_INFO, info);
614
615 if (wacom->shared && wacom->shared->touch_input) {
616 input_report_switch(wacom->shared->touch_input,
617 SW_MUTE_DEVICE,
618 !wacom->shared->is_touch_on);
619 input_sync(wacom->shared->touch_input);
620 }
621
c7f0522a 622 input_report_abs(input, ABS_RX, strip1);
03a0dc54 623 input_report_abs(input, ABS_RY, strip2);
c7f0522a 624
aaae03e4
JG
625 input_report_abs(input, ABS_WHEEL, (ring1 & 0x80) ? (ring1 & 0x7f) : 0);
626 input_report_abs(input, ABS_THROTTLE, (ring2 & 0x80) ? (ring2 & 0x7f) : 0);
c7f0522a
JG
627
628 input_report_key(input, wacom->tool[1], prox ? 1 : 0);
629 input_report_abs(input, ABS_MISC, prox ? PAD_DEVICE_ID : 0);
630
631 input_event(input, EV_MSC, MSC_SERIAL, 0xffffffff);
632
fb013a01
JG
633 return 1;
634}
635
82527da3
JG
636static int wacom_intuos_id_mangle(int tool_id)
637{
638 return (tool_id & ~0xFFF) << 4 | (tool_id & 0xFFF);
639}
640
7e129783
BT
641static int wacom_intuos_get_tool_type(int tool_id)
642{
643 int tool_type;
644
645 switch (tool_id) {
646 case 0x812: /* Inking pen */
647 case 0x801: /* Intuos3 Inking pen */
82527da3 648 case 0x12802: /* Intuos4/5 Inking Pen */
7e129783
BT
649 case 0x012:
650 tool_type = BTN_TOOL_PENCIL;
651 break;
652
653 case 0x822: /* Pen */
654 case 0x842:
655 case 0x852:
656 case 0x823: /* Intuos3 Grip Pen */
657 case 0x813: /* Intuos3 Classic Pen */
658 case 0x885: /* Intuos3 Marker Pen */
659 case 0x802: /* Intuos4/5 13HD/24HD General Pen */
660 case 0x804: /* Intuos4/5 13HD/24HD Marker Pen */
661 case 0x8e2: /* IntuosHT2 pen */
662 case 0x022:
82527da3 663 case 0x10804: /* Intuos4/5 13HD/24HD Art Pen */
4e6e7d72 664 case 0x10842: /* MobileStudio Pro Pro Pen slim */
82527da3
JG
665 case 0x14802: /* Intuos4/5 13HD/24HD Classic Pen */
666 case 0x16802: /* Cintiq 13HD Pro Pen */
667 case 0x18802: /* DTH2242 Pen */
668 case 0x10802: /* Intuos4/5 13HD/24HD General Pen */
7e129783
BT
669 tool_type = BTN_TOOL_PEN;
670 break;
671
672 case 0x832: /* Stroke pen */
673 case 0x032:
674 tool_type = BTN_TOOL_BRUSH;
675 break;
676
677 case 0x007: /* Mouse 4D and 2D */
678 case 0x09c:
679 case 0x094:
680 case 0x017: /* Intuos3 2D Mouse */
681 case 0x806: /* Intuos4 Mouse */
682 tool_type = BTN_TOOL_MOUSE;
683 break;
684
685 case 0x096: /* Lens cursor */
686 case 0x097: /* Intuos3 Lens cursor */
687 case 0x006: /* Intuos4 Lens cursor */
688 tool_type = BTN_TOOL_LENS;
689 break;
690
691 case 0x82a: /* Eraser */
9ce9a123 692 case 0x84a:
7e129783
BT
693 case 0x85a:
694 case 0x91a:
695 case 0xd1a:
696 case 0x0fa:
697 case 0x82b: /* Intuos3 Grip Pen Eraser */
698 case 0x81b: /* Intuos3 Classic Pen Eraser */
699 case 0x91b: /* Intuos3 Airbrush Eraser */
700 case 0x80c: /* Intuos4/5 13HD/24HD Marker Pen Eraser */
701 case 0x80a: /* Intuos4/5 13HD/24HD General Pen Eraser */
702 case 0x90a: /* Intuos4/5 13HD/24HD Airbrush Eraser */
82527da3
JG
703 case 0x1480a: /* Intuos4/5 13HD/24HD Classic Pen Eraser */
704 case 0x1090a: /* Intuos4/5 13HD/24HD Airbrush Eraser */
705 case 0x1080c: /* Intuos4/5 13HD/24HD Art Pen Eraser */
4e6e7d72 706 case 0x1084a: /* MobileStudio Pro Pro Pen slim Eraser */
82527da3
JG
707 case 0x1680a: /* Cintiq 13HD Pro Pen Eraser */
708 case 0x1880a: /* DTH2242 Eraser */
709 case 0x1080a: /* Intuos4/5 13HD/24HD General Pen Eraser */
7e129783
BT
710 tool_type = BTN_TOOL_RUBBER;
711 break;
712
713 case 0xd12:
714 case 0x912:
715 case 0x112:
716 case 0x913: /* Intuos3 Airbrush */
717 case 0x902: /* Intuos4/5 13HD/24HD Airbrush */
82527da3 718 case 0x10902: /* Intuos4/5 13HD/24HD Airbrush */
7e129783
BT
719 tool_type = BTN_TOOL_AIRBRUSH;
720 break;
721
722 default: /* Unknown tool */
723 tool_type = BTN_TOOL_PEN;
724 break;
725 }
726 return tool_type;
727}
728
619d3a29
AAS
729static void wacom_exit_report(struct wacom_wac *wacom)
730{
731 struct input_dev *input = wacom->pen_input;
732 struct wacom_features *features = &wacom->features;
733 unsigned char *data = wacom->data;
734 int idx = (features->type == INTUOS) ? (data[1] & 0x01) : 0;
735
736 /*
737 * Reset all states otherwise we lose the initial states
738 * when in-prox next time
739 */
740 input_report_abs(input, ABS_X, 0);
741 input_report_abs(input, ABS_Y, 0);
742 input_report_abs(input, ABS_DISTANCE, 0);
743 input_report_abs(input, ABS_TILT_X, 0);
744 input_report_abs(input, ABS_TILT_Y, 0);
745 if (wacom->tool[idx] >= BTN_TOOL_MOUSE) {
746 input_report_key(input, BTN_LEFT, 0);
747 input_report_key(input, BTN_MIDDLE, 0);
748 input_report_key(input, BTN_RIGHT, 0);
749 input_report_key(input, BTN_SIDE, 0);
750 input_report_key(input, BTN_EXTRA, 0);
751 input_report_abs(input, ABS_THROTTLE, 0);
752 input_report_abs(input, ABS_RZ, 0);
753 } else {
754 input_report_abs(input, ABS_PRESSURE, 0);
755 input_report_key(input, BTN_STYLUS, 0);
756 input_report_key(input, BTN_STYLUS2, 0);
757 input_report_key(input, BTN_TOUCH, 0);
758 input_report_abs(input, ABS_WHEEL, 0);
759 if (features->type >= INTUOS3S)
760 input_report_abs(input, ABS_Z, 0);
761 }
762 input_report_key(input, wacom->tool[idx], 0);
763 input_report_abs(input, ABS_MISC, 0); /* reset tool id */
764 input_event(input, EV_MSC, MSC_SERIAL, wacom->serial[idx]);
765 wacom->id[idx] = 0;
766}
767
95dd3b30 768static int wacom_intuos_inout(struct wacom_wac *wacom)
3bea733a 769{
e33da8a5 770 struct wacom_features *features = &wacom->features;
3bea733a 771 unsigned char *data = wacom->data;
2a6cdbdd 772 struct input_dev *input = wacom->pen_input;
4750f5fe 773 int idx = (features->type == INTUOS) ? (data[1] & 0x01) : 0;
3bea733a 774
4750f5fe
PC
775 if (!(((data[1] & 0xfc) == 0xc0) || /* in prox */
776 ((data[1] & 0xfe) == 0x20) || /* in range */
777 ((data[1] & 0xfe) == 0x80))) /* out prox */
778 return 0;
3bea733a
PC
779
780 /* Enter report */
781 if ((data[1] & 0xfc) == 0xc0) {
782 /* serial number of the tool */
783 wacom->serial[idx] = ((data[3] & 0x0f) << 28) +
784 (data[4] << 20) + (data[5] << 12) +
785 (data[6] << 4) + (data[7] >> 4);
786
493630b2 787 wacom->id[idx] = (data[2] << 4) | (data[3] >> 4) |
82527da3 788 ((data[7] & 0x0f) << 16) | ((data[8] & 0xf0) << 8);
73a97f4f 789
7e129783
BT
790 wacom->tool[idx] = wacom_intuos_get_tool_type(wacom->id[idx]);
791
eff6ca97 792 wacom->shared->stylus_in_proximity = true;
3bea733a
PC
793 return 1;
794 }
795
c1b03f55
PC
796 /* in Range */
797 if ((data[1] & 0xfe) == 0x20) {
526d6e7b
PC
798 if (features->type != INTUOSHT2)
799 wacom->shared->stylus_in_proximity = true;
486b908d 800
c1b03f55
PC
801 /* in Range while exiting */
802 if (wacom->reporting_data) {
803 input_report_key(input, BTN_TOUCH, 0);
804 input_report_abs(input, ABS_PRESSURE, 0);
805 input_report_abs(input, ABS_DISTANCE, wacom->features.distance_max);
806 return 2;
807 }
808 return 1;
4eb1830b
JG
809 }
810
3bea733a
PC
811 /* Exit report */
812 if ((data[1] & 0xfe) == 0x80) {
f3586d2f 813 wacom->shared->stylus_in_proximity = false;
b3bd7ef3 814 wacom->reporting_data = false;
ae584ca4 815
373a5356
PC
816 /* don't report exit if we don't know the ID */
817 if (!wacom->id[idx])
818 return 1;
819
619d3a29 820 wacom_exit_report(wacom);
80d4e8e9 821 return 2;
3bea733a 822 }
373a5356 823
3bea733a
PC
824 return 0;
825}
826
9d339fe4
JG
827static inline bool touch_is_muted(struct wacom_wac *wacom_wac)
828{
829 return wacom_wac->probe_complete &&
830 wacom_wac->shared->has_mute_touch_switch &&
831 !wacom_wac->shared->is_touch_on;
832}
833
1924e05e
PC
834static inline bool report_touch_events(struct wacom_wac *wacom)
835{
836 return (touch_arbitration ? !wacom->shared->stylus_in_proximity : 1);
837}
72b236d6 838
1924e05e
PC
839static inline bool delay_pen_events(struct wacom_wac *wacom)
840{
841 return (wacom->shared->touch_down && touch_arbitration);
72b236d6
AS
842}
843
16e0a6a0 844static int wacom_intuos_general(struct wacom_wac *wacom)
3bea733a 845{
e33da8a5 846 struct wacom_features *features = &wacom->features;
3bea733a 847 unsigned char *data = wacom->data;
2a6cdbdd 848 struct input_dev *input = wacom->pen_input;
16e0a6a0 849 int idx = (features->type == INTUOS) ? (data[1] & 0x01) : 0;
a8a09c85 850 unsigned char type = (data[1] >> 1) & 0x0F;
5f33f430 851 unsigned int x, y, distance, t;
3bea733a 852
16e0a6a0
JG
853 if (data[0] != WACOM_REPORT_PENABLED && data[0] != WACOM_REPORT_CINTIQ &&
854 data[0] != WACOM_REPORT_INTUOS_PEN)
855 return 0;
856
1924e05e 857 if (delay_pen_events(wacom))
c1b03f55
PC
858 return 1;
859
599b0820
PC
860 /* don't report events if we don't know the tool ID */
861 if (!wacom->id[idx]) {
862 /* but reschedule a read of the current tool */
863 wacom_intuos_schedule_prox_event(wacom);
864 return 1;
865 }
866
4750f5fe
PC
867 /*
868 * don't report events for invalid data
869 */
870 /* older I4 styli don't work with new Cintiqs */
82527da3 871 if ((!((wacom->id[idx] >> 16) & 0x01) &&
4750f5fe
PC
872 (features->type == WACOM_21UX2)) ||
873 /* Only large Intuos support Lense Cursor */
874 (wacom->tool[idx] == BTN_TOOL_LENS &&
875 (features->type == INTUOS3 ||
876 features->type == INTUOS3S ||
877 features->type == INTUOS4 ||
878 features->type == INTUOS4S ||
879 features->type == INTUOS5 ||
880 features->type == INTUOS5S ||
881 features->type == INTUOSPM ||
882 features->type == INTUOSPS)) ||
883 /* Cintiq doesn't send data when RDY bit isn't set */
884 (features->type == CINTIQ && !(data[1] & 0x40)))
885 return 1;
886
5f33f430
JG
887 x = (be16_to_cpup((__be16 *)&data[2]) << 1) | ((data[9] >> 1) & 1);
888 y = (be16_to_cpup((__be16 *)&data[4]) << 1) | (data[9] & 1);
889 distance = data[9] >> 2;
890 if (features->type < INTUOS3S) {
891 x >>= 1;
892 y >>= 1;
893 distance >>= 1;
16e0a6a0 894 }
b72fb1dc
JG
895 if (features->type == INTUOSHT2)
896 distance = features->distance_max - distance;
5f33f430
JG
897 input_report_abs(input, ABS_X, x);
898 input_report_abs(input, ABS_Y, y);
899 input_report_abs(input, ABS_DISTANCE, distance);
16e0a6a0 900
a8a09c85
JG
901 switch (type) {
902 case 0x00:
903 case 0x01:
904 case 0x02:
905 case 0x03:
906 /* general pen packet */
5f33f430
JG
907 t = (data[6] << 3) | ((data[7] & 0xC0) >> 5) | (data[1] & 1);
908 if (features->pressure_max < 2047)
909 t >>= 1;
8da23fc1 910 input_report_abs(input, ABS_PRESSURE, t);
eda01dab
PC
911 if (features->type != INTUOSHT2) {
912 input_report_abs(input, ABS_TILT_X,
ec5fc1c1 913 (((data[7] << 1) & 0x7e) | (data[8] >> 7)) - 64);
eda01dab
PC
914 input_report_abs(input, ABS_TILT_Y, (data[8] & 0x7f) - 64);
915 }
8da23fc1
DT
916 input_report_key(input, BTN_STYLUS, data[1] & 2);
917 input_report_key(input, BTN_STYLUS2, data[1] & 4);
918 input_report_key(input, BTN_TOUCH, t > 10);
a8a09c85 919 break;
3bea733a 920
a8a09c85 921 case 0x0a:
a8a09c85 922 /* airbrush second packet */
8da23fc1 923 input_report_abs(input, ABS_WHEEL,
3bea733a 924 (data[6] << 2) | ((data[7] >> 6) & 3));
8da23fc1 925 input_report_abs(input, ABS_TILT_X,
ec5fc1c1
JG
926 (((data[7] << 1) & 0x7e) | (data[8] >> 7)) - 64);
927 input_report_abs(input, ABS_TILT_Y, (data[8] & 0x7f) - 64);
a8a09c85 928 break;
3bea733a 929
a8a09c85 930 case 0x05:
a8a09c85
JG
931 /* Rotation packet */
932 if (features->type >= INTUOS3S) {
933 /* I3 marker pen rotation */
934 t = (data[6] << 3) | ((data[7] >> 5) & 7);
935 t = (data[7] & 0x20) ? ((t > 900) ? ((t-1) / 2 - 1350) :
936 ((t-1) / 2 + 450)) : (450 - t / 2) ;
937 input_report_abs(input, ABS_Z, t);
938 } else {
571f572f 939 /* 4D mouse 2nd packet */
a8a09c85
JG
940 t = (data[6] << 3) | ((data[7] >> 5) & 7);
941 input_report_abs(input, ABS_RZ, (data[7] & 0x20) ?
942 ((t - 1) / 2) : -t / 2);
943 }
944 break;
3bea733a 945
a8a09c85 946 case 0x04:
571f572f
JG
947 /* 4D mouse 1st packet */
948 input_report_key(input, BTN_LEFT, data[8] & 0x01);
949 input_report_key(input, BTN_MIDDLE, data[8] & 0x02);
950 input_report_key(input, BTN_RIGHT, data[8] & 0x04);
951
952 input_report_key(input, BTN_SIDE, data[8] & 0x20);
953 input_report_key(input, BTN_EXTRA, data[8] & 0x10);
954 t = (data[6] << 2) | ((data[7] >> 6) & 3);
955 input_report_abs(input, ABS_THROTTLE, (data[8] & 0x08) ? -t : t);
956 break;
957
a8a09c85 958 case 0x06:
571f572f
JG
959 /* I4 mouse */
960 input_report_key(input, BTN_LEFT, data[6] & 0x01);
961 input_report_key(input, BTN_MIDDLE, data[6] & 0x02);
962 input_report_key(input, BTN_RIGHT, data[6] & 0x04);
963 input_report_rel(input, REL_WHEEL, ((data[7] & 0x80) >> 7)
964 - ((data[7] & 0x40) >> 6));
965 input_report_key(input, BTN_SIDE, data[6] & 0x08);
966 input_report_key(input, BTN_EXTRA, data[6] & 0x10);
3bea733a 967
571f572f
JG
968 input_report_abs(input, ABS_TILT_X,
969 (((data[7] << 1) & 0x7e) | (data[8] >> 7)) - 64);
970 input_report_abs(input, ABS_TILT_Y, (data[8] & 0x7f) - 64);
971 break;
972
973 case 0x08:
974 if (wacom->tool[idx] == BTN_TOOL_MOUSE) {
975 /* 2D mouse packet */
976 input_report_key(input, BTN_LEFT, data[8] & 0x04);
977 input_report_key(input, BTN_MIDDLE, data[8] & 0x08);
978 input_report_key(input, BTN_RIGHT, data[8] & 0x10);
979 input_report_rel(input, REL_WHEEL, (data[8] & 0x01)
980 - ((data[8] & 0x02) >> 1));
981
982 /* I3 2D mouse side buttons */
983 if (features->type >= INTUOS3S && features->type <= INTUOS3L) {
984 input_report_key(input, BTN_SIDE, data[8] & 0x40);
985 input_report_key(input, BTN_EXTRA, data[8] & 0x20);
3bea733a 986 }
a8a09c85 987 }
571f572f 988 else if (wacom->tool[idx] == BTN_TOOL_LENS) {
3bea733a 989 /* Lens cursor packets */
8da23fc1
DT
990 input_report_key(input, BTN_LEFT, data[8] & 0x01);
991 input_report_key(input, BTN_MIDDLE, data[8] & 0x02);
992 input_report_key(input, BTN_RIGHT, data[8] & 0x04);
993 input_report_key(input, BTN_SIDE, data[8] & 0x10);
994 input_report_key(input, BTN_EXTRA, data[8] & 0x08);
3bea733a 995 }
a8a09c85
JG
996 break;
997
571f572f 998 case 0x07:
a8a09c85 999 case 0x09:
571f572f 1000 case 0x0b:
a8a09c85
JG
1001 case 0x0c:
1002 case 0x0d:
1003 case 0x0e:
1004 case 0x0f:
1005 /* unhandled */
1006 break;
3bea733a 1007 }
3bea733a 1008
82527da3
JG
1009 input_report_abs(input, ABS_MISC,
1010 wacom_intuos_id_mangle(wacom->id[idx])); /* report tool id */
8da23fc1
DT
1011 input_report_key(input, wacom->tool[idx], 1);
1012 input_event(input, EV_MSC, MSC_SERIAL, wacom->serial[idx]);
b3bd7ef3 1013 wacom->reporting_data = true;
16e0a6a0 1014 return 2;
3bea733a
PC
1015}
1016
95dd3b30 1017static int wacom_intuos_irq(struct wacom_wac *wacom)
3bea733a
PC
1018{
1019 unsigned char *data = wacom->data;
2a6cdbdd 1020 struct input_dev *input = wacom->pen_input;
16e0a6a0 1021 int result;
3bea733a 1022
eb71d1bb 1023 if (data[0] != WACOM_REPORT_PENABLED &&
06109993
JG
1024 data[0] != WACOM_REPORT_INTUOS_ID1 &&
1025 data[0] != WACOM_REPORT_INTUOS_ID2 &&
eb71d1bb 1026 data[0] != WACOM_REPORT_INTUOSPAD &&
eda01dab 1027 data[0] != WACOM_REPORT_INTUOS_PEN &&
500d4160
PC
1028 data[0] != WACOM_REPORT_CINTIQ &&
1029 data[0] != WACOM_REPORT_CINTIQPAD &&
eb71d1bb
DT
1030 data[0] != WACOM_REPORT_INTUOS5PAD) {
1031 dev_dbg(input->dev.parent,
1032 "%s: received unknown report #%d\n", __func__, data[0]);
3bea733a
PC
1033 return 0;
1034 }
1035
16e0a6a0
JG
1036 /* process pad events */
1037 result = wacom_intuos_pad(wacom);
1038 if (result)
1039 return result;
3bea733a
PC
1040
1041 /* process in/out prox events */
95dd3b30 1042 result = wacom_intuos_inout(wacom);
3bea733a 1043 if (result)
16e0a6a0 1044 return result - 1;
3bea733a
PC
1045
1046 /* process general packets */
16e0a6a0
JG
1047 result = wacom_intuos_general(wacom);
1048 if (result)
1049 return result - 1;
3bea733a 1050
16e0a6a0 1051 return 0;
3bea733a
PC
1052}
1053
149f6f6b
JG
1054static int wacom_remote_irq(struct wacom_wac *wacom_wac, size_t len)
1055{
1056 unsigned char *data = wacom_wac->data;
1057 struct input_dev *input;
1058 struct wacom *wacom = container_of(wacom_wac, struct wacom, wacom_wac);
1059 struct wacom_remote *remote = wacom->remote;
1060 int bat_charging, bat_percent, touch_ring_mode;
1061 __u32 serial;
1062 int i, index = -1;
1063 unsigned long flags;
1064
1065 if (data[0] != WACOM_REPORT_REMOTE) {
1066 hid_dbg(wacom->hdev, "%s: received unknown report #%d",
1067 __func__, data[0]);
1068 return 0;
1069 }
1070
1071 serial = data[3] + (data[4] << 8) + (data[5] << 16);
1072 wacom_wac->id[0] = PAD_DEVICE_ID;
1073
1074 spin_lock_irqsave(&remote->remote_lock, flags);
1075
1076 for (i = 0; i < WACOM_MAX_REMOTES; i++) {
1077 if (remote->remotes[i].serial == serial) {
1078 index = i;
1079 break;
1080 }
1081 }
1082
1083 if (index < 0 || !remote->remotes[index].registered)
1084 goto out;
1085
1086 input = remote->remotes[index].input;
1087
1088 input_report_key(input, BTN_0, (data[9] & 0x01));
1089 input_report_key(input, BTN_1, (data[9] & 0x02));
1090 input_report_key(input, BTN_2, (data[9] & 0x04));
1091 input_report_key(input, BTN_3, (data[9] & 0x08));
1092 input_report_key(input, BTN_4, (data[9] & 0x10));
1093 input_report_key(input, BTN_5, (data[9] & 0x20));
1094 input_report_key(input, BTN_6, (data[9] & 0x40));
1095 input_report_key(input, BTN_7, (data[9] & 0x80));
1096
1097 input_report_key(input, BTN_8, (data[10] & 0x01));
1098 input_report_key(input, BTN_9, (data[10] & 0x02));
1099 input_report_key(input, BTN_A, (data[10] & 0x04));
1100 input_report_key(input, BTN_B, (data[10] & 0x08));
1101 input_report_key(input, BTN_C, (data[10] & 0x10));
1102 input_report_key(input, BTN_X, (data[10] & 0x20));
1103 input_report_key(input, BTN_Y, (data[10] & 0x40));
1104 input_report_key(input, BTN_Z, (data[10] & 0x80));
1105
1106 input_report_key(input, BTN_BASE, (data[11] & 0x01));
1107 input_report_key(input, BTN_BASE2, (data[11] & 0x02));
1108
1109 if (data[12] & 0x80)
fcf887e7 1110 input_report_abs(input, ABS_WHEEL, (data[12] & 0x7f) - 1);
149f6f6b
JG
1111 else
1112 input_report_abs(input, ABS_WHEEL, 0);
1113
1114 bat_percent = data[7] & 0x7f;
1115 bat_charging = !!(data[7] & 0x80);
1116
1117 if (data[9] | data[10] | (data[11] & 0x03) | data[12])
1118 input_report_abs(input, ABS_MISC, PAD_DEVICE_ID);
1119 else
1120 input_report_abs(input, ABS_MISC, 0);
1121
1122 input_event(input, EV_MSC, MSC_SERIAL, serial);
1123
1124 input_sync(input);
1125
1126 /*Which mode select (LED light) is currently on?*/
1127 touch_ring_mode = (data[11] & 0xC0) >> 6;
1128
1129 for (i = 0; i < WACOM_MAX_REMOTES; i++) {
1130 if (remote->remotes[i].serial == serial)
1131 wacom->led.groups[i].select = touch_ring_mode;
1132 }
1133
16e45989
JG
1134 __wacom_notify_battery(&remote->remotes[index].battery,
1135 WACOM_POWER_SUPPLY_STATUS_AUTO, bat_percent,
149f6f6b
JG
1136 bat_charging, 1, bat_charging);
1137
1138out:
1139 spin_unlock_irqrestore(&remote->remote_lock, flags);
1140 return 0;
1141}
1142
1143static void wacom_remote_status_irq(struct wacom_wac *wacom_wac, size_t len)
1144{
1145 struct wacom *wacom = container_of(wacom_wac, struct wacom, wacom_wac);
1146 unsigned char *data = wacom_wac->data;
1147 struct wacom_remote *remote = wacom->remote;
1148 struct wacom_remote_data remote_data;
1149 unsigned long flags;
1150 int i, ret;
1151
1152 if (data[0] != WACOM_REPORT_DEVICE_LIST)
1153 return;
1154
1155 memset(&remote_data, 0, sizeof(struct wacom_remote_data));
1156
1157 for (i = 0; i < WACOM_MAX_REMOTES; i++) {
1158 int j = i * 6;
1159 int serial = (data[j+6] << 16) + (data[j+5] << 8) + data[j+4];
1160 bool connected = data[j+2];
1161
1162 remote_data.remote[i].serial = serial;
1163 remote_data.remote[i].connected = connected;
1164 }
1165
1166 spin_lock_irqsave(&remote->remote_lock, flags);
1167
1168 ret = kfifo_in(&remote->remote_fifo, &remote_data, sizeof(remote_data));
1169 if (ret != sizeof(remote_data)) {
1170 spin_unlock_irqrestore(&remote->remote_lock, flags);
1171 hid_err(wacom->hdev, "Can't queue Remote status event.\n");
1172 return;
1173 }
1174
1175 spin_unlock_irqrestore(&remote->remote_lock, flags);
1176
1177 wacom_schedule_work(wacom_wac, WACOM_WORKER_REMOTE);
1178}
1179
b1e4279e
JG
1180static int int_dist(int x1, int y1, int x2, int y2)
1181{
1182 int x = x2 - x1;
1183 int y = y2 - y1;
1184
1185 return int_sqrt(x*x + y*y);
1186}
1187
81af7e61
BT
1188static void wacom_intuos_bt_process_data(struct wacom_wac *wacom,
1189 unsigned char *data)
1190{
1191 memcpy(wacom->data, data, 10);
1192 wacom_intuos_irq(wacom);
1193
2a6cdbdd 1194 input_sync(wacom->pen_input);
81af7e61
BT
1195 if (wacom->pad_input)
1196 input_sync(wacom->pad_input);
1197}
1198
1199static int wacom_intuos_bt_irq(struct wacom_wac *wacom, size_t len)
1200{
1201 unsigned char data[WACOM_PKGLEN_MAX];
1202 int i = 1;
1203 unsigned power_raw, battery_capacity, bat_charging, ps_connected;
1204
1205 memcpy(data, wacom->data, len);
1206
1207 switch (data[0]) {
1208 case 0x04:
1209 wacom_intuos_bt_process_data(wacom, data + i);
1210 i += 10;
df561f66 1211 fallthrough;
81af7e61
BT
1212 case 0x03:
1213 wacom_intuos_bt_process_data(wacom, data + i);
1214 i += 10;
1215 wacom_intuos_bt_process_data(wacom, data + i);
1216 i += 10;
1217 power_raw = data[i];
1218 bat_charging = (power_raw & 0x08) ? 1 : 0;
1219 ps_connected = (power_raw & 0x10) ? 1 : 0;
1220 battery_capacity = batcap_i4[power_raw & 0x07];
16e45989
JG
1221 wacom_notify_battery(wacom, WACOM_POWER_SUPPLY_STATUS_AUTO,
1222 battery_capacity, bat_charging,
71fa641e 1223 battery_capacity || bat_charging,
953f2c5f 1224 ps_connected);
81af7e61
BT
1225 break;
1226 default:
2a6cdbdd 1227 dev_dbg(wacom->pen_input->dev.parent,
81af7e61
BT
1228 "Unknown report: %d,%d size:%zu\n",
1229 data[0], data[1], len);
1230 return 0;
1231 }
1232 return 0;
1233}
1234
7d059ed0
PC
1235static int wacom_wac_finger_count_touches(struct wacom_wac *wacom)
1236{
2a6cdbdd 1237 struct input_dev *input = wacom->touch_input;
7d059ed0
PC
1238 unsigned touch_max = wacom->features.touch_max;
1239 int count = 0;
1240 int i;
1241
26ba61f8
PC
1242 if (!touch_max)
1243 return 0;
1244
71b5c476
JG
1245 if (touch_max == 1)
1246 return test_bit(BTN_TOUCH, input->key) &&
1924e05e 1247 report_touch_events(wacom);
7d059ed0
PC
1248
1249 for (i = 0; i < input->mt->num_slots; i++) {
1250 struct input_mt_slot *ps = &input->mt->slots[i];
1251 int id = input_mt_get_value(ps, ABS_MT_TRACKING_ID);
1252 if (id >= 0)
1253 count++;
1254 }
1255
1256 return count;
1257}
1258
4922cd26
JG
1259static void wacom_intuos_pro2_bt_pen(struct wacom_wac *wacom)
1260{
87046b6c 1261 int pen_frame_len, pen_frames;
4922cd26
JG
1262
1263 struct input_dev *pen_input = wacom->pen_input;
1264 unsigned char *data = wacom->data;
1265 int i;
1266
912c6aa6
AAS
1267 if (wacom->features.type == INTUOSP2_BT ||
1268 wacom->features.type == INTUOSP2S_BT) {
87046b6c
AAS
1269 wacom->serial[0] = get_unaligned_le64(&data[99]);
1270 wacom->id[0] = get_unaligned_le16(&data[107]);
1271 pen_frame_len = 14;
1272 pen_frames = 7;
1273 } else {
1274 wacom->serial[0] = get_unaligned_le64(&data[33]);
1275 wacom->id[0] = get_unaligned_le16(&data[41]);
1276 pen_frame_len = 8;
1277 pen_frames = 4;
1278 }
1279
4922cd26
JG
1280 if (wacom->serial[0] >> 52 == 1) {
1281 /* Add back in missing bits of ID for non-USI pens */
1282 wacom->id[0] |= (wacom->serial[0] >> 32) & 0xFFFFF;
1283 }
4922cd26
JG
1284
1285 for (i = 0; i < pen_frames; i++) {
1286 unsigned char *frame = &data[i*pen_frame_len + 1];
a48324de
JG
1287 bool valid = frame[0] & 0x80;
1288 bool prox = frame[0] & 0x40;
1289 bool range = frame[0] & 0x20;
2cc08800 1290 bool invert = frame[0] & 0x10;
4922cd26 1291
a48324de 1292 if (!valid)
4922cd26
JG
1293 continue;
1294
619d3a29
AAS
1295 if (!prox) {
1296 wacom->shared->stylus_in_proximity = false;
1297 wacom_exit_report(wacom);
1298 input_sync(pen_input);
2cc08800
JG
1299
1300 wacom->tool[0] = 0;
1301 wacom->id[0] = 0;
1302 wacom->serial[0] = 0;
619d3a29
AAS
1303 return;
1304 }
2cc08800 1305
a48324de 1306 if (range) {
2cc08800
JG
1307 if (!wacom->tool[0]) { /* first in range */
1308 /* Going into range select tool */
1309 if (invert)
1310 wacom->tool[0] = BTN_TOOL_RUBBER;
1311 else if (wacom->id[0])
1312 wacom->tool[0] = wacom_intuos_get_tool_type(wacom->id[0]);
1313 else
1314 wacom->tool[0] = BTN_TOOL_PEN;
1315 }
1316
a48324de
JG
1317 input_report_abs(pen_input, ABS_X, get_unaligned_le16(&frame[1]));
1318 input_report_abs(pen_input, ABS_Y, get_unaligned_le16(&frame[3]));
87046b6c 1319
912c6aa6
AAS
1320 if (wacom->features.type == INTUOSP2_BT ||
1321 wacom->features.type == INTUOSP2S_BT) {
87046b6c
AAS
1322 /* Fix rotation alignment: userspace expects zero at left */
1323 int16_t rotation =
1324 (int16_t)get_unaligned_le16(&frame[9]);
1325 rotation += 1800/4;
1326
1327 if (rotation > 899)
1328 rotation -= 1800;
1329
1330 input_report_abs(pen_input, ABS_TILT_X,
1331 (char)frame[7]);
1332 input_report_abs(pen_input, ABS_TILT_Y,
1333 (char)frame[8]);
1334 input_report_abs(pen_input, ABS_Z, rotation);
1335 input_report_abs(pen_input, ABS_WHEEL,
1336 get_unaligned_le16(&frame[11]));
1337 }
a48324de 1338 }
e92a7be7
JG
1339 if (wacom->tool[0]) {
1340 input_report_abs(pen_input, ABS_PRESSURE, get_unaligned_le16(&frame[5]));
7cdf6e40
AAS
1341 if (wacom->features.type == INTUOSP2_BT ||
1342 wacom->features.type == INTUOSP2S_BT) {
e92a7be7
JG
1343 input_report_abs(pen_input, ABS_DISTANCE,
1344 range ? frame[13] : wacom->features.distance_max);
1345 } else {
1346 input_report_abs(pen_input, ABS_DISTANCE,
1347 range ? frame[7] : wacom->features.distance_max);
1348 }
1349
fe7f8d73 1350 input_report_key(pen_input, BTN_TOUCH, frame[0] & 0x09);
e92a7be7
JG
1351 input_report_key(pen_input, BTN_STYLUS, frame[0] & 0x02);
1352 input_report_key(pen_input, BTN_STYLUS2, frame[0] & 0x04);
4922cd26 1353
e92a7be7
JG
1354 input_report_key(pen_input, wacom->tool[0], prox);
1355 input_event(pen_input, EV_MSC, MSC_SERIAL, wacom->serial[0]);
1356 input_report_abs(pen_input, ABS_MISC,
1357 wacom_intuos_id_mangle(wacom->id[0])); /* report tool id */
1358 }
4922cd26 1359
a48324de 1360 wacom->shared->stylus_in_proximity = prox;
4922cd26
JG
1361
1362 input_sync(pen_input);
1363 }
1364}
1365
1366static void wacom_intuos_pro2_bt_touch(struct wacom_wac *wacom)
1367{
1368 const int finger_touch_len = 8;
1369 const int finger_frames = 4;
1370 const int finger_frame_len = 43;
1371
1372 struct input_dev *touch_input = wacom->touch_input;
1373 unsigned char *data = wacom->data;
1374 int num_contacts_left = 5;
1375 int i, j;
1376
1377 for (i = 0; i < finger_frames; i++) {
1378 unsigned char *frame = &data[i*finger_frame_len + 109];
1379 int current_num_contacts = frame[0] & 0x7F;
1380 int contacts_to_send;
1381
1382 if (!(frame[0] & 0x80))
1383 continue;
1384
1385 /*
1386 * First packet resets the counter since only the first
1387 * packet in series will have non-zero current_num_contacts.
1388 */
1389 if (current_num_contacts)
1390 wacom->num_contacts_left = current_num_contacts;
1391
1392 contacts_to_send = min(num_contacts_left, wacom->num_contacts_left);
1393
1394 for (j = 0; j < contacts_to_send; j++) {
1395 unsigned char *touch = &frame[j*finger_touch_len + 1];
1396 int slot = input_mt_get_slot_by_key(touch_input, touch[0]);
1397 int x = get_unaligned_le16(&touch[2]);
1398 int y = get_unaligned_le16(&touch[4]);
1399 int w = touch[6] * input_abs_get_res(touch_input, ABS_MT_POSITION_X);
1400 int h = touch[7] * input_abs_get_res(touch_input, ABS_MT_POSITION_Y);
1401
1402 if (slot < 0)
1403 continue;
1404
1405 input_mt_slot(touch_input, slot);
1406 input_mt_report_slot_state(touch_input, MT_TOOL_FINGER, touch[1] & 0x01);
1407 input_report_abs(touch_input, ABS_MT_POSITION_X, x);
1408 input_report_abs(touch_input, ABS_MT_POSITION_Y, y);
1409 input_report_abs(touch_input, ABS_MT_TOUCH_MAJOR, max(w, h));
1410 input_report_abs(touch_input, ABS_MT_TOUCH_MINOR, min(w, h));
1411 input_report_abs(touch_input, ABS_MT_ORIENTATION, w > h);
1412 }
1413
1414 input_mt_sync_frame(touch_input);
1415
1416 wacom->num_contacts_left -= contacts_to_send;
1417 if (wacom->num_contacts_left <= 0) {
1418 wacom->num_contacts_left = 0;
1419 wacom->shared->touch_down = wacom_wac_finger_count_touches(wacom);
69dbdfff 1420 input_sync(touch_input);
4922cd26
JG
1421 }
1422 }
1423
69dbdfff
JG
1424 if (wacom->num_contacts_left == 0) {
1425 // Be careful that we don't accidentally call input_sync with
1426 // only a partial set of fingers of processed
1427 input_report_switch(touch_input, SW_MUTE_DEVICE, !(data[281] >> 7));
1428 input_sync(touch_input);
1429 }
1430
4922cd26
JG
1431}
1432
1433static void wacom_intuos_pro2_bt_pad(struct wacom_wac *wacom)
1434{
1435 struct input_dev *pad_input = wacom->pad_input;
1436 unsigned char *data = wacom->data;
dcce8ef8 1437 int nbuttons = wacom->features.numbered_buttons;
4922cd26 1438
dcce8ef8
JG
1439 int expresskeys = data[282];
1440 int center = (data[281] & 0x40) >> 6;
d252f4a1
JG
1441 int ring = data[285] & 0x7F;
1442 bool ringstatus = data[285] & 0x80;
dcce8ef8 1443 bool prox = expresskeys || center || ringstatus;
d252f4a1
JG
1444
1445 /* Fix touchring data: userspace expects 0 at left and increasing clockwise */
1446 ring = 71 - ring;
1447 ring += 3*72/16;
1448 if (ring > 71)
1449 ring -= 72;
4922cd26 1450
dcce8ef8
JG
1451 wacom_report_numbered_buttons(pad_input, nbuttons,
1452 expresskeys | (center << (nbuttons - 1)));
4922cd26 1453
d252f4a1 1454 input_report_abs(pad_input, ABS_WHEEL, ringstatus ? ring : 0);
4922cd26
JG
1455
1456 input_report_key(pad_input, wacom->tool[1], prox ? 1 : 0);
1457 input_report_abs(pad_input, ABS_MISC, prox ? PAD_DEVICE_ID : 0);
1458 input_event(pad_input, EV_MSC, MSC_SERIAL, 0xffffffff);
1459
1460 input_sync(pad_input);
1461}
1462
1463static void wacom_intuos_pro2_bt_battery(struct wacom_wac *wacom)
1464{
1465 unsigned char *data = wacom->data;
1466
1467 bool chg = data[284] & 0x80;
1468 int battery_status = data[284] & 0x7F;
1469
16e45989
JG
1470 wacom_notify_battery(wacom, WACOM_POWER_SUPPLY_STATUS_AUTO,
1471 battery_status, chg, 1, chg);
4922cd26
JG
1472}
1473
87046b6c
AAS
1474static void wacom_intuos_gen3_bt_pad(struct wacom_wac *wacom)
1475{
1476 struct input_dev *pad_input = wacom->pad_input;
1477 unsigned char *data = wacom->data;
1478
1479 int buttons = data[44];
1480
1481 wacom_report_numbered_buttons(pad_input, 4, buttons);
1482
1483 input_report_key(pad_input, wacom->tool[1], buttons ? 1 : 0);
1484 input_report_abs(pad_input, ABS_MISC, buttons ? PAD_DEVICE_ID : 0);
1485 input_event(pad_input, EV_MSC, MSC_SERIAL, 0xffffffff);
1486
1487 input_sync(pad_input);
1488}
1489
1490static void wacom_intuos_gen3_bt_battery(struct wacom_wac *wacom)
1491{
1492 unsigned char *data = wacom->data;
1493
1494 bool chg = data[45] & 0x80;
1495 int battery_status = data[45] & 0x7F;
1496
1497 wacom_notify_battery(wacom, WACOM_POWER_SUPPLY_STATUS_AUTO,
1498 battery_status, chg, 1, chg);
1499}
1500
4922cd26
JG
1501static int wacom_intuos_pro2_bt_irq(struct wacom_wac *wacom, size_t len)
1502{
1503 unsigned char *data = wacom->data;
1504
87046b6c 1505 if (data[0] != 0x80 && data[0] != 0x81) {
4922cd26
JG
1506 dev_dbg(wacom->pen_input->dev.parent,
1507 "%s: received unknown report #%d\n", __func__, data[0]);
1508 return 0;
1509 }
1510
1511 wacom_intuos_pro2_bt_pen(wacom);
912c6aa6
AAS
1512 if (wacom->features.type == INTUOSP2_BT ||
1513 wacom->features.type == INTUOSP2S_BT) {
87046b6c
AAS
1514 wacom_intuos_pro2_bt_touch(wacom);
1515 wacom_intuos_pro2_bt_pad(wacom);
1516 wacom_intuos_pro2_bt_battery(wacom);
1517 } else {
1518 wacom_intuos_gen3_bt_pad(wacom);
1519 wacom_intuos_gen3_bt_battery(wacom);
1520 }
4922cd26
JG
1521 return 0;
1522}
1523
b1e4279e
JG
1524static int wacom_24hdt_irq(struct wacom_wac *wacom)
1525{
2a6cdbdd 1526 struct input_dev *input = wacom->touch_input;
74b63417 1527 unsigned char *data = wacom->data;
b1e4279e 1528 int i;
e0d41fd4 1529 int current_num_contacts = data[61];
b1e4279e 1530 int contacts_to_send = 0;
500d4160
PC
1531 int num_contacts_left = 4; /* maximum contacts per packet */
1532 int byte_per_packet = WACOM_BYTES_PER_24HDT_PACKET;
1533 int y_offset = 2;
1534
9d339fe4
JG
1535 if (touch_is_muted(wacom) && !wacom->shared->touch_down)
1536 return 0;
670e9092 1537
500d4160
PC
1538 if (wacom->features.type == WACOM_27QHDT) {
1539 current_num_contacts = data[63];
1540 num_contacts_left = 10;
1541 byte_per_packet = WACOM_BYTES_PER_QHDTHID_PACKET;
1542 y_offset = 0;
500d4160 1543 }
b1e4279e
JG
1544
1545 /*
1546 * First packet resets the counter since only the first
1547 * packet in series will have non-zero current_num_contacts.
1548 */
7d059ed0 1549 if (current_num_contacts)
b1e4279e
JG
1550 wacom->num_contacts_left = current_num_contacts;
1551
500d4160 1552 contacts_to_send = min(num_contacts_left, wacom->num_contacts_left);
b1e4279e
JG
1553
1554 for (i = 0; i < contacts_to_send; i++) {
500d4160 1555 int offset = (byte_per_packet * i) + 1;
1924e05e 1556 bool touch = (data[offset] & 0x1) && report_touch_events(wacom);
02295e68 1557 int slot = input_mt_get_slot_by_key(input, data[offset + 1]);
b1e4279e
JG
1558
1559 if (slot < 0)
1560 continue;
1561 input_mt_slot(input, slot);
1562 input_mt_report_slot_state(input, MT_TOOL_FINGER, touch);
1563
1564 if (touch) {
edc8e20a 1565 int t_x = get_unaligned_le16(&data[offset + 2]);
500d4160 1566 int t_y = get_unaligned_le16(&data[offset + 4 + y_offset]);
b1e4279e
JG
1567
1568 input_report_abs(input, ABS_MT_POSITION_X, t_x);
1569 input_report_abs(input, ABS_MT_POSITION_Y, t_y);
500d4160
PC
1570
1571 if (wacom->features.type != WACOM_27QHDT) {
1572 int c_x = get_unaligned_le16(&data[offset + 4]);
1573 int c_y = get_unaligned_le16(&data[offset + 8]);
1574 int w = get_unaligned_le16(&data[offset + 10]);
1575 int h = get_unaligned_le16(&data[offset + 12]);
1576
1577 input_report_abs(input, ABS_MT_TOUCH_MAJOR, min(w,h));
1578 input_report_abs(input, ABS_MT_WIDTH_MAJOR,
1579 min(w, h) + int_dist(t_x, t_y, c_x, c_y));
1580 input_report_abs(input, ABS_MT_WIDTH_MINOR, min(w, h));
1581 input_report_abs(input, ABS_MT_ORIENTATION, w > h);
1582 }
b1e4279e 1583 }
b1e4279e 1584 }
9a1c0012 1585 input_mt_sync_frame(input);
b1e4279e
JG
1586
1587 wacom->num_contacts_left -= contacts_to_send;
e0d41fd4 1588 if (wacom->num_contacts_left <= 0) {
b1e4279e 1589 wacom->num_contacts_left = 0;
7d059ed0 1590 wacom->shared->touch_down = wacom_wac_finger_count_touches(wacom);
e0d41fd4 1591 }
b1e4279e
JG
1592 return 1;
1593}
1594
1963518b
PC
1595static int wacom_mt_touch(struct wacom_wac *wacom)
1596{
2a6cdbdd 1597 struct input_dev *input = wacom->touch_input;
74b63417 1598 unsigned char *data = wacom->data;
1963518b
PC
1599 int i;
1600 int current_num_contacts = data[2];
1601 int contacts_to_send = 0;
6afdc289
PC
1602 int x_offset = 0;
1603
1604 /* MTTPC does not support Height and Width */
d51ddb2b 1605 if (wacom->features.type == MTTPC || wacom->features.type == MTTPC_B)
6afdc289 1606 x_offset = -4;
1963518b
PC
1607
1608 /*
1609 * First packet resets the counter since only the first
1610 * packet in series will have non-zero current_num_contacts.
1611 */
7d059ed0 1612 if (current_num_contacts)
1963518b
PC
1613 wacom->num_contacts_left = current_num_contacts;
1614
1615 /* There are at most 5 contacts per packet */
1616 contacts_to_send = min(5, wacom->num_contacts_left);
1617
1618 for (i = 0; i < contacts_to_send; i++) {
6afdc289 1619 int offset = (WACOM_BYTES_PER_MT_PACKET + x_offset) * i + 3;
1924e05e 1620 bool touch = (data[offset] & 0x1) && report_touch_events(wacom);
edc8e20a 1621 int id = get_unaligned_le16(&data[offset + 1]);
02295e68 1622 int slot = input_mt_get_slot_by_key(input, id);
1963518b
PC
1623
1624 if (slot < 0)
1625 continue;
1626
1627 input_mt_slot(input, slot);
1628 input_mt_report_slot_state(input, MT_TOOL_FINGER, touch);
1629 if (touch) {
edc8e20a
JG
1630 int x = get_unaligned_le16(&data[offset + x_offset + 7]);
1631 int y = get_unaligned_le16(&data[offset + x_offset + 9]);
1963518b
PC
1632 input_report_abs(input, ABS_MT_POSITION_X, x);
1633 input_report_abs(input, ABS_MT_POSITION_Y, y);
1634 }
1963518b 1635 }
9a1c0012 1636 input_mt_sync_frame(input);
1963518b
PC
1637
1638 wacom->num_contacts_left -= contacts_to_send;
e0d41fd4 1639 if (wacom->num_contacts_left <= 0) {
1963518b 1640 wacom->num_contacts_left = 0;
7d059ed0 1641 wacom->shared->touch_down = wacom_wac_finger_count_touches(wacom);
e0d41fd4 1642 }
1963518b
PC
1643 return 1;
1644}
1645
84eb5aa6
PC
1646static int wacom_tpc_mt_touch(struct wacom_wac *wacom)
1647{
2a6cdbdd 1648 struct input_dev *input = wacom->touch_input;
84eb5aa6 1649 unsigned char *data = wacom->data;
84eb5aa6
PC
1650 int i;
1651
1652 for (i = 0; i < 2; i++) {
1653 int p = data[1] & (1 << i);
1924e05e 1654 bool touch = p && report_touch_events(wacom);
84eb5aa6
PC
1655
1656 input_mt_slot(input, i);
1657 input_mt_report_slot_state(input, MT_TOOL_FINGER, touch);
1658 if (touch) {
1659 int x = le16_to_cpup((__le16 *)&data[i * 2 + 2]) & 0x7fff;
1660 int y = le16_to_cpup((__le16 *)&data[i * 2 + 6]) & 0x7fff;
1661
1662 input_report_abs(input, ABS_MT_POSITION_X, x);
1663 input_report_abs(input, ABS_MT_POSITION_Y, y);
84eb5aa6
PC
1664 }
1665 }
9a1c0012 1666 input_mt_sync_frame(input);
84eb5aa6
PC
1667
1668 /* keep touch state for pen event */
7d059ed0 1669 wacom->shared->touch_down = wacom_wac_finger_count_touches(wacom);
84eb5aa6 1670
84eb5aa6
PC
1671 return 1;
1672}
1673
a43c7c53 1674static int wacom_tpc_single_touch(struct wacom_wac *wacom, size_t len)
ec67bbed 1675{
74b63417 1676 unsigned char *data = wacom->data;
2a6cdbdd 1677 struct input_dev *input = wacom->touch_input;
1924e05e 1678 bool prox = report_touch_events(wacom);
a43c7c53 1679 int x = 0, y = 0;
ec67bbed 1680
1963518b
PC
1681 if (wacom->features.touch_max > 1 || len > WACOM_PKGLEN_TPC2FG)
1682 return 0;
1683
e0d41fd4
PC
1684 if (len == WACOM_PKGLEN_TPC1FG) {
1685 prox = prox && (data[0] & 0x01);
1686 x = get_unaligned_le16(&data[1]);
1687 y = get_unaligned_le16(&data[3]);
1688 } else if (len == WACOM_PKGLEN_TPC1FG_B) {
1689 prox = prox && (data[2] & 0x01);
1690 x = get_unaligned_le16(&data[3]);
1691 y = get_unaligned_le16(&data[5]);
1692 } else {
1693 prox = prox && (data[1] & 0x01);
1694 x = le16_to_cpup((__le16 *)&data[2]);
1695 y = le16_to_cpup((__le16 *)&data[4]);
1696 }
ec67bbed 1697
a43c7c53
PC
1698 if (prox) {
1699 input_report_abs(input, ABS_X, x);
1700 input_report_abs(input, ABS_Y, y);
1701 }
1702 input_report_key(input, BTN_TOUCH, prox);
73a97f4f 1703
a43c7c53
PC
1704 /* keep touch state for pen events */
1705 wacom->shared->touch_down = prox;
ec67bbed 1706
a43c7c53 1707 return 1;
ec67bbed
PC
1708}
1709
8aa9a9ac 1710static int wacom_tpc_pen(struct wacom_wac *wacom)
545f4e99 1711{
74b63417 1712 unsigned char *data = wacom->data;
2a6cdbdd 1713 struct input_dev *input = wacom->pen_input;
a43c7c53 1714 bool prox = data[1] & 0x20;
8aa9a9ac 1715
a43c7c53 1716 if (!wacom->shared->stylus_in_proximity) /* first in prox */
8aa9a9ac
PC
1717 /* Going into proximity select tool */
1718 wacom->tool[0] = (data[1] & 0x0c) ? BTN_TOOL_RUBBER : BTN_TOOL_PEN;
8aa9a9ac 1719
a43c7c53
PC
1720 /* keep pen state for touch events */
1721 wacom->shared->stylus_in_proximity = prox;
1722
1924e05e
PC
1723 /* send pen events only when touch is up or forced out
1724 * or touch arbitration is off
1725 */
1726 if (!delay_pen_events(wacom)) {
a43c7c53
PC
1727 input_report_key(input, BTN_STYLUS, data[1] & 0x02);
1728 input_report_key(input, BTN_STYLUS2, data[1] & 0x10);
1729 input_report_abs(input, ABS_X, le16_to_cpup((__le16 *)&data[2]));
1730 input_report_abs(input, ABS_Y, le16_to_cpup((__le16 *)&data[4]));
0b335cad 1731 input_report_abs(input, ABS_PRESSURE, ((data[7] & 0x07) << 8) | data[6]);
a43c7c53
PC
1732 input_report_key(input, BTN_TOUCH, data[1] & 0x05);
1733 input_report_key(input, wacom->tool[0], prox);
1734 return 1;
8aa9a9ac 1735 }
8aa9a9ac 1736
a43c7c53 1737 return 0;
8aa9a9ac
PC
1738}
1739
1740static int wacom_tpc_irq(struct wacom_wac *wacom, size_t len)
1741{
74b63417 1742 unsigned char *data = wacom->data;
545f4e99 1743
2ac97f0f 1744 if (wacom->pen_input) {
2a6cdbdd
JG
1745 dev_dbg(wacom->pen_input->dev.parent,
1746 "%s: received report #%d\n", __func__, data[0]);
2ac97f0f
JG
1747
1748 if (len == WACOM_PKGLEN_PENABLED ||
1749 data[0] == WACOM_REPORT_PENABLED)
1750 return wacom_tpc_pen(wacom);
1751 }
1752 else if (wacom->touch_input) {
2a6cdbdd
JG
1753 dev_dbg(wacom->touch_input->dev.parent,
1754 "%s: received report #%d\n", __func__, data[0]);
545f4e99 1755
2ac97f0f
JG
1756 switch (len) {
1757 case WACOM_PKGLEN_TPC1FG:
1758 return wacom_tpc_single_touch(wacom, len);
31175a83 1759
2ac97f0f
JG
1760 case WACOM_PKGLEN_TPC2FG:
1761 return wacom_tpc_mt_touch(wacom);
31175a83 1762
2ac97f0f
JG
1763 default:
1764 switch (data[0]) {
1765 case WACOM_REPORT_TPC1FG:
1766 case WACOM_REPORT_TPCHID:
1767 case WACOM_REPORT_TPCST:
1768 case WACOM_REPORT_TPC1FGE:
1769 return wacom_tpc_single_touch(wacom, len);
d51ddb2b 1770
2ac97f0f
JG
1771 case WACOM_REPORT_TPCMT:
1772 case WACOM_REPORT_TPCMT2:
1773 return wacom_mt_touch(wacom);
1963518b 1774
2ac97f0f 1775 }
31175a83
PC
1776 }
1777 }
4492efff 1778
8aa9a9ac 1779 return 0;
545f4e99
PC
1780}
1781
d252f4a1
JG
1782static int wacom_offset_rotation(struct input_dev *input, struct hid_usage *usage,
1783 int value, int num, int denom)
1784{
1785 struct input_absinfo *abs = &input->absinfo[usage->code];
1786 int range = (abs->maximum - abs->minimum + 1);
1787
1788 value += num*range/denom;
1789 if (value > abs->maximum)
1790 value -= range;
1791 else if (value < abs->minimum)
1792 value += range;
1793 return value;
1794}
1795
ac2423c9 1796int wacom_equivalent_usage(int usage)
c9c09587
JG
1797{
1798 if ((usage & HID_USAGE_PAGE) == WACOM_HID_UP_WACOMDIGITIZER) {
1799 int subpage = (usage & 0xFF00) << 8;
1800 int subusage = (usage & 0xFF);
1801
5922e613
JG
1802 if (subpage == WACOM_HID_SP_PAD ||
1803 subpage == WACOM_HID_SP_BUTTON ||
1804 subpage == WACOM_HID_SP_DIGITIZER ||
b5c921e6 1805 subpage == WACOM_HID_SP_DIGITIZERINFO ||
61ce346a 1806 usage == WACOM_HID_WD_SENSE ||
f85c9dc6
JG
1807 usage == WACOM_HID_WD_SERIALHI ||
1808 usage == WACOM_HID_WD_TOOLTYPE ||
5922e613 1809 usage == WACOM_HID_WD_DISTANCE ||
bf78adcb
JG
1810 usage == WACOM_HID_WD_TOUCHSTRIP ||
1811 usage == WACOM_HID_WD_TOUCHSTRIP2 ||
5922e613 1812 usage == WACOM_HID_WD_TOUCHRING ||
b1f466a9
AAS
1813 usage == WACOM_HID_WD_TOUCHRINGSTATUS ||
1814 usage == WACOM_HID_WD_REPORT_VALID) {
c9c09587
JG
1815 return usage;
1816 }
1817
1818 if (subpage == HID_UP_UNDEFINED)
1819 subpage = HID_UP_DIGITIZER;
1820
1821 return subpage | subusage;
1822 }
1823
ac2423c9
AAS
1824 if ((usage & HID_USAGE_PAGE) == WACOM_HID_UP_WACOMTOUCH) {
1825 int subpage = (usage & 0xFF00) << 8;
1826 int subusage = (usage & 0xFF);
1827
f4e11d59
AAS
1828 if (usage == WACOM_HID_WT_REPORT_VALID)
1829 return usage;
1830
ac2423c9
AAS
1831 if (subpage == HID_UP_UNDEFINED)
1832 subpage = WACOM_HID_SP_DIGITIZER;
1833
1834 return subpage | subusage;
1835 }
1836
c9c09587
JG
1837 return usage;
1838}
1839
2a6cdbdd 1840static void wacom_map_usage(struct input_dev *input, struct hid_usage *usage,
7704ac93
BT
1841 struct hid_field *field, __u8 type, __u16 code, int fuzz)
1842{
345857bb
JG
1843 struct wacom *wacom = input_get_drvdata(input);
1844 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
1845 struct wacom_features *features = &wacom_wac->features;
7704ac93
BT
1846 int fmin = field->logical_minimum;
1847 int fmax = field->logical_maximum;
c9c09587 1848 unsigned int equivalent_usage = wacom_equivalent_usage(usage->hid);
50066a04
JG
1849 int resolution_code = code;
1850
c9c09587 1851 if (equivalent_usage == HID_DG_TWIST) {
50066a04
JG
1852 resolution_code = ABS_RZ;
1853 }
7704ac93 1854
345857bb
JG
1855 if (equivalent_usage == HID_GD_X) {
1856 fmin += features->offset_left;
1857 fmax -= features->offset_right;
1858 }
1859 if (equivalent_usage == HID_GD_Y) {
1860 fmin += features->offset_top;
1861 fmax -= features->offset_bottom;
1862 }
1863
7704ac93
BT
1864 usage->type = type;
1865 usage->code = code;
1866
7704ac93
BT
1867 switch (type) {
1868 case EV_ABS:
1869 input_set_abs_params(input, code, fmin, fmax, fuzz, 0);
1870 input_abs_set_res(input, code,
50066a04 1871 hidinput_calc_abs_res(field, resolution_code));
7704ac93
BT
1872 break;
1873 case EV_KEY:
7704ac93 1874 case EV_MSC:
bf78adcb 1875 case EV_SW:
46fc466e 1876 input_set_capability(input, type, code);
bf78adcb 1877 break;
7704ac93
BT
1878 }
1879}
1880
5ac3d4ae 1881static void wacom_wac_battery_usage_mapping(struct hid_device *hdev,
5922e613
JG
1882 struct hid_field *field, struct hid_usage *usage)
1883{
1884 struct wacom *wacom = hid_get_drvdata(hdev);
1885 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
1886 struct wacom_features *features = &wacom_wac->features;
5922e613
JG
1887 unsigned equivalent_usage = wacom_equivalent_usage(usage->hid);
1888
1889 switch (equivalent_usage) {
5ac3d4ae 1890 case HID_DG_BATTERYSTRENGTH:
93aab7fa
JG
1891 case WACOM_HID_WD_BATTERY_LEVEL:
1892 case WACOM_HID_WD_BATTERY_CHARGING:
1893 features->quirks |= WACOM_QUIRK_BATTERY;
1894 break;
5ac3d4ae
JG
1895 }
1896}
1897
1898static void wacom_wac_battery_event(struct hid_device *hdev, struct hid_field *field,
1899 struct hid_usage *usage, __s32 value)
1900{
1901 struct wacom *wacom = hid_get_drvdata(hdev);
1902 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
1903 unsigned equivalent_usage = wacom_equivalent_usage(usage->hid);
1904
1905 switch (equivalent_usage) {
1906 case HID_DG_BATTERYSTRENGTH:
1907 if (value == 0) {
1908 wacom_wac->hid_data.bat_status = POWER_SUPPLY_STATUS_UNKNOWN;
1909 }
1910 else {
1911 value = value * 100 / (field->logical_maximum - field->logical_minimum);
1912 wacom_wac->hid_data.battery_capacity = value;
1913 wacom_wac->hid_data.bat_connected = 1;
1914 wacom_wac->hid_data.bat_status = WACOM_POWER_SUPPLY_STATUS_AUTO;
1915 }
1916 break;
1917 case WACOM_HID_WD_BATTERY_LEVEL:
1918 value = value * 100 / (field->logical_maximum - field->logical_minimum);
1919 wacom_wac->hid_data.battery_capacity = value;
1920 wacom_wac->hid_data.bat_connected = 1;
1921 wacom_wac->hid_data.bat_status = WACOM_POWER_SUPPLY_STATUS_AUTO;
1922 break;
1923 case WACOM_HID_WD_BATTERY_CHARGING:
1924 wacom_wac->hid_data.bat_charging = value;
1925 wacom_wac->hid_data.ps_connected = value;
1926 wacom_wac->hid_data.bat_connected = 1;
1927 wacom_wac->hid_data.bat_status = WACOM_POWER_SUPPLY_STATUS_AUTO;
1928 break;
1929 }
1930}
1931
1932static void wacom_wac_battery_pre_report(struct hid_device *hdev,
1933 struct hid_report *report)
1934{
1935 return;
1936}
1937
1938static void wacom_wac_battery_report(struct hid_device *hdev,
1939 struct hid_report *report)
1940{
1941 struct wacom *wacom = hid_get_drvdata(hdev);
1942 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
1943 struct wacom_features *features = &wacom_wac->features;
1944
1945 if (features->quirks & WACOM_QUIRK_BATTERY) {
1946 int status = wacom_wac->hid_data.bat_status;
1947 int capacity = wacom_wac->hid_data.battery_capacity;
1948 bool charging = wacom_wac->hid_data.bat_charging;
1949 bool connected = wacom_wac->hid_data.bat_connected;
1950 bool powered = wacom_wac->hid_data.ps_connected;
1951
1952 wacom_notify_battery(wacom_wac, status, capacity, charging,
1953 connected, powered);
1954 }
1955}
1956
1957static void wacom_wac_pad_usage_mapping(struct hid_device *hdev,
1958 struct hid_field *field, struct hid_usage *usage)
1959{
1960 struct wacom *wacom = hid_get_drvdata(hdev);
1961 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
1962 struct wacom_features *features = &wacom_wac->features;
1963 struct input_dev *input = wacom_wac->pad_input;
1964 unsigned equivalent_usage = wacom_equivalent_usage(usage->hid);
1965
1966 switch (equivalent_usage) {
5922e613
JG
1967 case WACOM_HID_WD_ACCELEROMETER_X:
1968 __set_bit(INPUT_PROP_ACCELEROMETER, input->propbit);
1969 wacom_map_usage(input, usage, field, EV_ABS, ABS_X, 0);
f3f24e7b 1970 features->device_type |= WACOM_DEVICETYPE_PAD;
5922e613
JG
1971 break;
1972 case WACOM_HID_WD_ACCELEROMETER_Y:
1973 __set_bit(INPUT_PROP_ACCELEROMETER, input->propbit);
1974 wacom_map_usage(input, usage, field, EV_ABS, ABS_Y, 0);
f3f24e7b 1975 features->device_type |= WACOM_DEVICETYPE_PAD;
5922e613
JG
1976 break;
1977 case WACOM_HID_WD_ACCELEROMETER_Z:
1978 __set_bit(INPUT_PROP_ACCELEROMETER, input->propbit);
1979 wacom_map_usage(input, usage, field, EV_ABS, ABS_Z, 0);
f3f24e7b 1980 features->device_type |= WACOM_DEVICETYPE_PAD;
5922e613 1981 break;
10c55cac 1982 case WACOM_HID_WD_BUTTONCENTER:
5922e613
JG
1983 case WACOM_HID_WD_BUTTONHOME:
1984 case WACOM_HID_WD_BUTTONUP:
1985 case WACOM_HID_WD_BUTTONDOWN:
1986 case WACOM_HID_WD_BUTTONLEFT:
1987 case WACOM_HID_WD_BUTTONRIGHT:
1988 wacom_map_usage(input, usage, field, EV_KEY,
1989 wacom_numbered_button_to_key(features->numbered_buttons),
1990 0);
1991 features->numbered_buttons++;
f3f24e7b 1992 features->device_type |= WACOM_DEVICETYPE_PAD;
5922e613 1993 break;
d793ff81 1994 case WACOM_HID_WD_MUTE_DEVICE:
dc9dc864
PC
1995 /* softkey touch switch */
1996 wacom_wac->is_soft_touch_switch = true;
1997 fallthrough;
1998 case WACOM_HID_WD_TOUCHONOFF:
d2ec58ae 1999 /*
dc9dc864
PC
2000 * These two usages, which are used to mute touch events, come
2001 * from the pad packet, but are reported on the touch
d2ec58ae
AAS
2002 * interface. Because the touch interface may not have
2003 * been created yet, we cannot call wacom_map_usage(). In
dc9dc864 2004 * order to process the usages when we receive them, we set
d2ec58ae
AAS
2005 * the usage type and code directly.
2006 */
2007 wacom_wac->has_mute_touch_switch = true;
2008 usage->type = EV_SW;
2009 usage->code = SW_MUTE_DEVICE;
f3f24e7b 2010 features->device_type |= WACOM_DEVICETYPE_PAD;
bf78adcb
JG
2011 break;
2012 case WACOM_HID_WD_TOUCHSTRIP:
2013 wacom_map_usage(input, usage, field, EV_ABS, ABS_RX, 0);
f3f24e7b 2014 features->device_type |= WACOM_DEVICETYPE_PAD;
bf78adcb
JG
2015 break;
2016 case WACOM_HID_WD_TOUCHSTRIP2:
2017 wacom_map_usage(input, usage, field, EV_ABS, ABS_RY, 0);
f3f24e7b 2018 features->device_type |= WACOM_DEVICETYPE_PAD;
bf78adcb 2019 break;
5922e613
JG
2020 case WACOM_HID_WD_TOUCHRING:
2021 wacom_map_usage(input, usage, field, EV_ABS, ABS_WHEEL, 0);
f3f24e7b 2022 features->device_type |= WACOM_DEVICETYPE_PAD;
5922e613 2023 break;
60a22186 2024 case WACOM_HID_WD_TOUCHRINGSTATUS:
8d411cbf
JG
2025 /*
2026 * Only set up type/code association. Completely mapping
2027 * this usage may overwrite the axis resolution and range.
2028 */
2029 usage->type = EV_ABS;
2030 usage->code = ABS_WHEEL;
2031 set_bit(EV_ABS, input->evbit);
60a22186
AAS
2032 features->device_type |= WACOM_DEVICETYPE_PAD;
2033 break;
4eb220cb
PC
2034 case WACOM_HID_WD_BUTTONCONFIG:
2035 wacom_map_usage(input, usage, field, EV_KEY, KEY_BUTTONCONFIG, 0);
2036 features->device_type |= WACOM_DEVICETYPE_PAD;
2037 break;
2038 case WACOM_HID_WD_ONSCREEN_KEYBOARD:
2039 wacom_map_usage(input, usage, field, EV_KEY, KEY_ONSCREEN_KEYBOARD, 0);
2040 features->device_type |= WACOM_DEVICETYPE_PAD;
2041 break;
2042 case WACOM_HID_WD_CONTROLPANEL:
2043 wacom_map_usage(input, usage, field, EV_KEY, KEY_CONTROLPANEL, 0);
2044 features->device_type |= WACOM_DEVICETYPE_PAD;
2045 break;
4082da80
BT
2046 case WACOM_HID_WD_MODE_CHANGE:
2047 /* do not overwrite previous data */
2048 if (!wacom_wac->has_mode_change) {
2049 wacom_wac->has_mode_change = true;
2050 wacom_wac->is_direct_mode = true;
2051 }
2052 features->device_type |= WACOM_DEVICETYPE_PAD;
2053 break;
5922e613
JG
2054 }
2055
2056 switch (equivalent_usage & 0xfffffff0) {
2057 case WACOM_HID_WD_EXPRESSKEY00:
2058 wacom_map_usage(input, usage, field, EV_KEY,
2059 wacom_numbered_button_to_key(features->numbered_buttons),
2060 0);
2061 features->numbered_buttons++;
f3f24e7b 2062 features->device_type |= WACOM_DEVICETYPE_PAD;
5922e613
JG
2063 break;
2064 }
2065}
2066
c9cfb2ac
PC
2067static void wacom_wac_pad_event(struct hid_device *hdev, struct hid_field *field,
2068 struct hid_usage *usage, __s32 value)
2069{
2070 struct wacom *wacom = hid_get_drvdata(hdev);
2071 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2072 struct input_dev *input = wacom_wac->pad_input;
2073 struct wacom_features *features = &wacom_wac->features;
2074 unsigned equivalent_usage = wacom_equivalent_usage(usage->hid);
10c55cac 2075 int i;
d252f4a1 2076 bool do_report = false;
c9cfb2ac 2077
60a22186
AAS
2078 /*
2079 * Avoid reporting this event and setting inrange_state if this usage
2080 * hasn't been mapped.
2081 */
4082da80 2082 if (!usage->type && equivalent_usage != WACOM_HID_WD_MODE_CHANGE)
60a22186 2083 return;
c9cfb2ac
PC
2084
2085 if (wacom_equivalent_usage(field->physical) == HID_DG_TABLETFUNCTIONKEY) {
60a22186
AAS
2086 if (usage->hid != WACOM_HID_WD_TOUCHRING)
2087 wacom_wac->hid_data.inrange_state |= value;
c9cfb2ac
PC
2088 }
2089
2090 switch (equivalent_usage) {
d252f4a1
JG
2091 case WACOM_HID_WD_TOUCHRING:
2092 /*
2093 * Userspace expects touchrings to increase in value with
2094 * clockwise gestures and have their zero point at the
2095 * tablet's left. HID events "should" be clockwise-
2096 * increasing and zero at top, though the MobileStudio
2097 * Pro and 2nd-gen Intuos Pro don't do this...
2098 */
2099 if (hdev->vendor == 0x56a &&
2100 (hdev->product == 0x34d || hdev->product == 0x34e || /* MobileStudio Pro */
384225c2 2101 hdev->product == 0x357 || hdev->product == 0x358 || /* Intuos Pro 2 */
6e2abc68 2102 hdev->product == 0x392 || /* Intuos Pro 2 */
fe4e940f
JG
2103 hdev->product == 0x398 || hdev->product == 0x399 || /* MobileStudio Pro */
2104 hdev->product == 0x3AA)) { /* MobileStudio Pro */
d252f4a1
JG
2105 value = (field->logical_maximum - value);
2106
6e2abc68
AAS
2107 if (hdev->product == 0x357 || hdev->product == 0x358 ||
2108 hdev->product == 0x392)
d252f4a1 2109 value = wacom_offset_rotation(input, usage, value, 3, 16);
384225c2 2110 else if (hdev->product == 0x34d || hdev->product == 0x34e ||
fe4e940f
JG
2111 hdev->product == 0x398 || hdev->product == 0x399 ||
2112 hdev->product == 0x3AA)
d252f4a1
JG
2113 value = wacom_offset_rotation(input, usage, value, 1, 2);
2114 }
2115 else {
2116 value = wacom_offset_rotation(input, usage, value, 1, 4);
2117 }
2118 do_report = true;
2119 break;
93aab7fa 2120 case WACOM_HID_WD_TOUCHRINGSTATUS:
60a22186
AAS
2121 if (!value)
2122 input_event(input, usage->type, usage->code, 0);
354a3298 2123 break;
93aab7fa 2124
d793ff81 2125 case WACOM_HID_WD_MUTE_DEVICE:
d2ec58ae
AAS
2126 case WACOM_HID_WD_TOUCHONOFF:
2127 if (wacom_wac->shared->touch_input) {
403c0f68
JG
2128 bool *is_touch_on = &wacom_wac->shared->is_touch_on;
2129
2130 if (equivalent_usage == WACOM_HID_WD_MUTE_DEVICE && value)
2131 *is_touch_on = !(*is_touch_on);
2132 else if (equivalent_usage == WACOM_HID_WD_TOUCHONOFF)
2133 *is_touch_on = value;
2134
d2ec58ae 2135 input_report_switch(wacom_wac->shared->touch_input,
403c0f68 2136 SW_MUTE_DEVICE, !(*is_touch_on));
d2ec58ae
AAS
2137 input_sync(wacom_wac->shared->touch_input);
2138 }
354a3298 2139 break;
93aab7fa 2140
4082da80
BT
2141 case WACOM_HID_WD_MODE_CHANGE:
2142 if (wacom_wac->is_direct_mode != value) {
2143 wacom_wac->is_direct_mode = value;
2144 wacom_schedule_work(&wacom->wacom_wac, WACOM_WORKER_MODE_CHANGE);
2145 }
2146 break;
2147
10c55cac
AAS
2148 case WACOM_HID_WD_BUTTONCENTER:
2149 for (i = 0; i < wacom->led.count; i++)
2150 wacom_update_led(wacom, features->numbered_buttons,
2151 value, i);
df561f66 2152 fallthrough;
93aab7fa 2153 default:
d252f4a1
JG
2154 do_report = true;
2155 break;
2156 }
2157
2158 if (do_report) {
5922e613 2159 input_event(input, usage->type, usage->code, value);
ed1fa736
PC
2160 if (value)
2161 wacom_wac->hid_data.pad_input_event_flag = true;
93aab7fa 2162 }
5922e613
JG
2163}
2164
2165static void wacom_wac_pad_pre_report(struct hid_device *hdev,
2166 struct hid_report *report)
2167{
2168 struct wacom *wacom = hid_get_drvdata(hdev);
2169 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2170
2171 wacom_wac->hid_data.inrange_state = 0;
2172}
2173
c9cfb2ac 2174static void wacom_wac_pad_report(struct hid_device *hdev,
f8b6a747 2175 struct hid_report *report, struct hid_field *field)
c9cfb2ac
PC
2176{
2177 struct wacom *wacom = hid_get_drvdata(hdev);
2178 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
c9cfb2ac
PC
2179 struct input_dev *input = wacom_wac->pad_input;
2180 bool active = wacom_wac->hid_data.inrange_state != 0;
2181
2182 /* report prox for expresskey events */
d4b8efeb 2183 if (wacom_wac->hid_data.pad_input_event_flag) {
c9cfb2ac 2184 input_event(input, EV_ABS, ABS_MISC, active ? PAD_DEVICE_ID : 0);
c9cfb2ac 2185 input_sync(input);
ed1fa736
PC
2186 if (!active)
2187 wacom_wac->hid_data.pad_input_event_flag = false;
c9cfb2ac 2188 }
5922e613
JG
2189}
2190
46fc466e
PC
2191static void wacom_set_barrel_switch3_usage(struct wacom_wac *wacom_wac)
2192{
2193 struct input_dev *input = wacom_wac->pen_input;
2194 struct wacom_features *features = &wacom_wac->features;
2195
2196 if (!(features->quirks & WACOM_QUIRK_AESPEN) &&
2197 wacom_wac->hid_data.barrelswitch &&
2198 wacom_wac->hid_data.barrelswitch2 &&
2199 wacom_wac->hid_data.serialhi)
2200 input_set_capability(input, EV_KEY, BTN_STYLUS3);
2201}
2202
7704ac93
BT
2203static void wacom_wac_pen_usage_mapping(struct hid_device *hdev,
2204 struct hid_field *field, struct hid_usage *usage)
2205{
2206 struct wacom *wacom = hid_get_drvdata(hdev);
2a6cdbdd 2207 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
61ce346a 2208 struct wacom_features *features = &wacom_wac->features;
2a6cdbdd 2209 struct input_dev *input = wacom_wac->pen_input;
c9c09587 2210 unsigned equivalent_usage = wacom_equivalent_usage(usage->hid);
7704ac93 2211
c9c09587 2212 switch (equivalent_usage) {
7704ac93 2213 case HID_GD_X:
2a6cdbdd 2214 wacom_map_usage(input, usage, field, EV_ABS, ABS_X, 4);
7704ac93
BT
2215 break;
2216 case HID_GD_Y:
2a6cdbdd 2217 wacom_map_usage(input, usage, field, EV_ABS, ABS_Y, 4);
7704ac93 2218 break;
b5c921e6 2219 case WACOM_HID_WD_DISTANCE:
50066a04
JG
2220 case HID_GD_Z:
2221 wacom_map_usage(input, usage, field, EV_ABS, ABS_DISTANCE, 0);
2222 break;
7704ac93 2223 case HID_DG_TIPPRESSURE:
2a6cdbdd 2224 wacom_map_usage(input, usage, field, EV_ABS, ABS_PRESSURE, 0);
7704ac93
BT
2225 break;
2226 case HID_DG_INRANGE:
2a6cdbdd 2227 wacom_map_usage(input, usage, field, EV_KEY, BTN_TOOL_PEN, 0);
7704ac93
BT
2228 break;
2229 case HID_DG_INVERT:
2a6cdbdd 2230 wacom_map_usage(input, usage, field, EV_KEY,
7704ac93
BT
2231 BTN_TOOL_RUBBER, 0);
2232 break;
50066a04
JG
2233 case HID_DG_TILT_X:
2234 wacom_map_usage(input, usage, field, EV_ABS, ABS_TILT_X, 0);
2235 break;
2236 case HID_DG_TILT_Y:
2237 wacom_map_usage(input, usage, field, EV_ABS, ABS_TILT_Y, 0);
2238 break;
2239 case HID_DG_TWIST:
2240 wacom_map_usage(input, usage, field, EV_ABS, ABS_Z, 0);
2241 break;
7704ac93 2242 case HID_DG_ERASER:
46fc466e
PC
2243 input_set_capability(input, EV_KEY, BTN_TOOL_RUBBER);
2244 wacom_map_usage(input, usage, field, EV_KEY, BTN_TOUCH, 0);
2245 break;
7704ac93 2246 case HID_DG_TIPSWITCH:
46fc466e 2247 input_set_capability(input, EV_KEY, BTN_TOOL_PEN);
2a6cdbdd 2248 wacom_map_usage(input, usage, field, EV_KEY, BTN_TOUCH, 0);
7704ac93
BT
2249 break;
2250 case HID_DG_BARRELSWITCH:
46fc466e
PC
2251 wacom_wac->hid_data.barrelswitch = true;
2252 wacom_set_barrel_switch3_usage(wacom_wac);
2a6cdbdd 2253 wacom_map_usage(input, usage, field, EV_KEY, BTN_STYLUS, 0);
7704ac93
BT
2254 break;
2255 case HID_DG_BARRELSWITCH2:
46fc466e
PC
2256 wacom_wac->hid_data.barrelswitch2 = true;
2257 wacom_set_barrel_switch3_usage(wacom_wac);
2a6cdbdd 2258 wacom_map_usage(input, usage, field, EV_KEY, BTN_STYLUS2, 0);
7704ac93
BT
2259 break;
2260 case HID_DG_TOOLSERIALNUMBER:
83417206 2261 features->quirks |= WACOM_QUIRK_TOOLSERIAL;
2a6cdbdd 2262 wacom_map_usage(input, usage, field, EV_MSC, MSC_SERIAL, 0);
7704ac93 2263 break;
61ce346a
JG
2264 case WACOM_HID_WD_SENSE:
2265 features->quirks |= WACOM_QUIRK_SENSE;
2266 wacom_map_usage(input, usage, field, EV_KEY, BTN_TOOL_PEN, 0);
2267 break;
f85c9dc6 2268 case WACOM_HID_WD_SERIALHI:
46fc466e
PC
2269 wacom_wac->hid_data.serialhi = true;
2270 wacom_set_barrel_switch3_usage(wacom_wac);
f85c9dc6 2271 wacom_map_usage(input, usage, field, EV_ABS, ABS_MISC, 0);
f85c9dc6 2272 break;
929d6d5d 2273 case WACOM_HID_WD_FINGERWHEEL:
46fc466e 2274 input_set_capability(input, EV_KEY, BTN_TOOL_AIRBRUSH);
929d6d5d
JG
2275 wacom_map_usage(input, usage, field, EV_ABS, ABS_WHEEL, 0);
2276 break;
7704ac93
BT
2277 }
2278}
2279
354a3298 2280static void wacom_wac_pen_event(struct hid_device *hdev, struct hid_field *field,
7704ac93
BT
2281 struct hid_usage *usage, __s32 value)
2282{
2283 struct wacom *wacom = hid_get_drvdata(hdev);
2284 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
61ce346a 2285 struct wacom_features *features = &wacom_wac->features;
2a6cdbdd 2286 struct input_dev *input = wacom_wac->pen_input;
c9c09587 2287 unsigned equivalent_usage = wacom_equivalent_usage(usage->hid);
7704ac93 2288
b1f466a9
AAS
2289 if (wacom_wac->is_invalid_bt_frame)
2290 return;
2291
c9c09587 2292 switch (equivalent_usage) {
50066a04
JG
2293 case HID_GD_Z:
2294 /*
2295 * HID_GD_Z "should increase as the control's position is
2296 * moved from high to low", while ABS_DISTANCE instead
2297 * increases in value as the tool moves from low to high.
2298 */
2299 value = field->logical_maximum - value;
2300 break;
7704ac93
BT
2301 case HID_DG_INRANGE:
2302 wacom_wac->hid_data.inrange_state = value;
61ce346a
JG
2303 if (!(features->quirks & WACOM_QUIRK_SENSE))
2304 wacom_wac->hid_data.sense_state = value;
354a3298 2305 return;
7704ac93
BT
2306 case HID_DG_INVERT:
2307 wacom_wac->hid_data.invert_state = value;
354a3298 2308 return;
7704ac93
BT
2309 case HID_DG_ERASER:
2310 case HID_DG_TIPSWITCH:
2311 wacom_wac->hid_data.tipswitch |= value;
354a3298 2312 return;
9e429d56
JG
2313 case HID_DG_BARRELSWITCH:
2314 wacom_wac->hid_data.barrelswitch = value;
2315 return;
2316 case HID_DG_BARRELSWITCH2:
2317 wacom_wac->hid_data.barrelswitch2 = value;
2318 return;
f85c9dc6 2319 case HID_DG_TOOLSERIALNUMBER:
993f0d93
JG
2320 if (value) {
2321 wacom_wac->serial[0] = (wacom_wac->serial[0] & ~0xFFFFFFFFULL);
ff479731 2322 wacom_wac->serial[0] |= wacom_s32tou(value, field->report_size);
993f0d93 2323 }
354a3298 2324 return;
d252f4a1
JG
2325 case HID_DG_TWIST:
2326 /*
2327 * Userspace expects pen twist to have its zero point when
2328 * the buttons/finger is on the tablet's left. HID values
2329 * are zero when buttons are toward the top.
2330 */
2331 value = wacom_offset_rotation(input, usage, value, 1, 4);
2332 break;
61ce346a
JG
2333 case WACOM_HID_WD_SENSE:
2334 wacom_wac->hid_data.sense_state = value;
354a3298 2335 return;
f85c9dc6 2336 case WACOM_HID_WD_SERIALHI:
993f0d93 2337 if (value) {
ff479731
JG
2338 __u32 raw_value = wacom_s32tou(value, field->report_size);
2339
993f0d93 2340 wacom_wac->serial[0] = (wacom_wac->serial[0] & 0xFFFFFFFF);
ff479731 2341 wacom_wac->serial[0] |= ((__u64)raw_value) << 32;
993f0d93
JG
2342 /*
2343 * Non-USI EMR devices may contain additional tool type
2344 * information here. See WACOM_HID_WD_TOOLTYPE case for
2345 * more details.
2346 */
2347 if (value >> 20 == 1) {
ff479731 2348 wacom_wac->id[0] |= raw_value & 0xFFFFF;
993f0d93 2349 }
f85c9dc6 2350 }
354a3298 2351 return;
f85c9dc6
JG
2352 case WACOM_HID_WD_TOOLTYPE:
2353 /*
2354 * Some devices (MobileStudio Pro, and possibly later
2355 * devices as well) do not return the complete tool
2356 * type in their WACOM_HID_WD_TOOLTYPE usage. Use a
2357 * bitwise OR so the complete value can be built
2358 * up over time :(
2359 */
ff479731 2360 wacom_wac->id[0] |= wacom_s32tou(value, field->report_size);
354a3298 2361 return;
345857bb
JG
2362 case WACOM_HID_WD_OFFSETLEFT:
2363 if (features->offset_left && value != features->offset_left)
75a5f3ac 2364 hid_warn(hdev, "%s: overriding existing left offset "
345857bb
JG
2365 "%d -> %d\n", __func__, value,
2366 features->offset_left);
2367 features->offset_left = value;
354a3298 2368 return;
345857bb
JG
2369 case WACOM_HID_WD_OFFSETRIGHT:
2370 if (features->offset_right && value != features->offset_right)
75a5f3ac 2371 hid_warn(hdev, "%s: overriding existing right offset "
345857bb
JG
2372 "%d -> %d\n", __func__, value,
2373 features->offset_right);
2374 features->offset_right = value;
354a3298 2375 return;
345857bb
JG
2376 case WACOM_HID_WD_OFFSETTOP:
2377 if (features->offset_top && value != features->offset_top)
75a5f3ac 2378 hid_warn(hdev, "%s: overriding existing top offset "
345857bb
JG
2379 "%d -> %d\n", __func__, value,
2380 features->offset_top);
2381 features->offset_top = value;
354a3298 2382 return;
345857bb
JG
2383 case WACOM_HID_WD_OFFSETBOTTOM:
2384 if (features->offset_bottom && value != features->offset_bottom)
75a5f3ac 2385 hid_warn(hdev, "%s: overriding existing bottom offset "
345857bb
JG
2386 "%d -> %d\n", __func__, value,
2387 features->offset_bottom);
2388 features->offset_bottom = value;
354a3298 2389 return;
b1f466a9
AAS
2390 case WACOM_HID_WD_REPORT_VALID:
2391 wacom_wac->is_invalid_bt_frame = !value;
2392 return;
7704ac93
BT
2393 }
2394
1924e05e
PC
2395 /* send pen events only when touch is up or forced out
2396 * or touch arbitration is off
2397 */
2398 if (!usage->type || delay_pen_events(wacom_wac))
354a3298 2399 return;
7704ac93 2400
4affc233 2401 /* send pen events only when the pen is in range */
5b40104e
JG
2402 if (wacom_wac->hid_data.inrange_state)
2403 input_event(input, usage->type, usage->code, value);
2404 else if (wacom_wac->shared->stylus_in_proximity && !wacom_wac->hid_data.sense_state)
2405 input_event(input, usage->type, usage->code, 0);
7704ac93
BT
2406}
2407
06324e0c
JG
2408static void wacom_wac_pen_pre_report(struct hid_device *hdev,
2409 struct hid_report *report)
2410{
b1f466a9
AAS
2411 struct wacom *wacom = hid_get_drvdata(hdev);
2412 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2413
2414 wacom_wac->is_invalid_bt_frame = false;
06324e0c
JG
2415 return;
2416}
2417
7704ac93
BT
2418static void wacom_wac_pen_report(struct hid_device *hdev,
2419 struct hid_report *report)
2420{
2421 struct wacom *wacom = hid_get_drvdata(hdev);
2422 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2a6cdbdd 2423 struct input_dev *input = wacom_wac->pen_input;
7690dd18
JG
2424 bool range = wacom_wac->hid_data.inrange_state;
2425 bool sense = wacom_wac->hid_data.sense_state;
7704ac93 2426
b1f466a9
AAS
2427 if (wacom_wac->is_invalid_bt_frame)
2428 return;
2429
7690dd18
JG
2430 if (!wacom_wac->tool[0] && range) { /* first in range */
2431 /* Going into range select tool */
f85c9dc6
JG
2432 if (wacom_wac->hid_data.invert_state)
2433 wacom_wac->tool[0] = BTN_TOOL_RUBBER;
2434 else if (wacom_wac->id[0])
2435 wacom_wac->tool[0] = wacom_intuos_get_tool_type(wacom_wac->id[0]);
2436 else
2437 wacom_wac->tool[0] = BTN_TOOL_PEN;
2438 }
7704ac93
BT
2439
2440 /* keep pen state for touch events */
7690dd18 2441 wacom_wac->shared->stylus_in_proximity = sense;
7704ac93 2442
61ce346a 2443 if (!delay_pen_events(wacom_wac) && wacom_wac->tool[0]) {
f85c9dc6 2444 int id = wacom_wac->id[0];
9e429d56
JG
2445 int sw_state = wacom_wac->hid_data.barrelswitch |
2446 (wacom_wac->hid_data.barrelswitch2 << 1);
2447
2448 input_report_key(input, BTN_STYLUS, sw_state == 1);
2449 input_report_key(input, BTN_STYLUS2, sw_state == 2);
2450 input_report_key(input, BTN_STYLUS3, sw_state == 3);
f85c9dc6
JG
2451
2452 /*
2453 * Non-USI EMR tools should have their IDs mangled to
2454 * match the legacy behavior of wacom_intuos_general
2455 */
2456 if (wacom_wac->serial[0] >> 52 == 1)
2457 id = wacom_intuos_id_mangle(id);
2458
2459 /*
2460 * To ensure compatibility with xf86-input-wacom, we should
2461 * report the BTN_TOOL_* event prior to the ABS_MISC or
2462 * MSC_SERIAL events.
2463 */
7704ac93
BT
2464 input_report_key(input, BTN_TOUCH,
2465 wacom_wac->hid_data.tipswitch);
4affc233 2466 input_report_key(input, wacom_wac->tool[0], sense);
f85c9dc6
JG
2467 if (wacom_wac->serial[0]) {
2468 input_event(input, EV_MSC, MSC_SERIAL, wacom_wac->serial[0]);
4affc233 2469 input_report_abs(input, ABS_MISC, sense ? id : 0);
f85c9dc6 2470 }
7704ac93
BT
2471
2472 wacom_wac->hid_data.tipswitch = false;
2473
2474 input_sync(input);
2475 }
61ce346a 2476
4affc233 2477 if (!sense) {
61ce346a 2478 wacom_wac->tool[0] = 0;
f85c9dc6 2479 wacom_wac->id[0] = 0;
993f0d93 2480 wacom_wac->serial[0] = 0;
f85c9dc6 2481 }
7704ac93
BT
2482}
2483
5ae6e89f
BT
2484static void wacom_wac_finger_usage_mapping(struct hid_device *hdev,
2485 struct hid_field *field, struct hid_usage *usage)
2486{
2487 struct wacom *wacom = hid_get_drvdata(hdev);
2488 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2a6cdbdd 2489 struct input_dev *input = wacom_wac->touch_input;
5ae6e89f 2490 unsigned touch_max = wacom_wac->features.touch_max;
c9c09587 2491 unsigned equivalent_usage = wacom_equivalent_usage(usage->hid);
5ae6e89f 2492
c9c09587 2493 switch (equivalent_usage) {
5ae6e89f
BT
2494 case HID_GD_X:
2495 if (touch_max == 1)
2a6cdbdd 2496 wacom_map_usage(input, usage, field, EV_ABS, ABS_X, 4);
5ae6e89f 2497 else
2a6cdbdd 2498 wacom_map_usage(input, usage, field, EV_ABS,
5ae6e89f
BT
2499 ABS_MT_POSITION_X, 4);
2500 break;
2501 case HID_GD_Y:
2502 if (touch_max == 1)
2a6cdbdd 2503 wacom_map_usage(input, usage, field, EV_ABS, ABS_Y, 4);
5ae6e89f 2504 else
2a6cdbdd 2505 wacom_map_usage(input, usage, field, EV_ABS,
5ae6e89f
BT
2506 ABS_MT_POSITION_Y, 4);
2507 break;
488abb5c
JG
2508 case HID_DG_WIDTH:
2509 case HID_DG_HEIGHT:
488abb5c
JG
2510 wacom_map_usage(input, usage, field, EV_ABS, ABS_MT_TOUCH_MAJOR, 0);
2511 wacom_map_usage(input, usage, field, EV_ABS, ABS_MT_TOUCH_MINOR, 0);
2512 input_set_abs_params(input, ABS_MT_ORIENTATION, 0, 1, 0, 0);
2513 break;
5ae6e89f 2514 case HID_DG_TIPSWITCH:
2a6cdbdd 2515 wacom_map_usage(input, usage, field, EV_KEY, BTN_TOUCH, 0);
5ae6e89f 2516 break;
1b5d514a 2517 case HID_DG_CONTACTCOUNT:
499522c8 2518 wacom_wac->hid_data.cc_report = field->report->id;
1b5d514a
JG
2519 wacom_wac->hid_data.cc_index = field->index;
2520 wacom_wac->hid_data.cc_value_index = usage->usage_index;
2521 break;
6f107fab
JG
2522 case HID_DG_CONTACTID:
2523 if ((field->logical_maximum - field->logical_minimum) < touch_max) {
2524 /*
2525 * The HID descriptor for G11 sensors leaves logical
2526 * maximum set to '1' despite it being a multitouch
2527 * device. Override to a sensible number.
2528 */
2529 field->logical_maximum = 255;
2530 }
2531 break;
5ae6e89f
BT
2532 }
2533}
2534
601a22f3
JG
2535static void wacom_wac_finger_slot(struct wacom_wac *wacom_wac,
2536 struct input_dev *input)
2537{
2538 struct hid_data *hid_data = &wacom_wac->hid_data;
2539 bool mt = wacom_wac->features.touch_max > 1;
2540 bool prox = hid_data->tipswitch &&
1924e05e 2541 report_touch_events(wacom_wac);
601a22f3 2542
9d339fe4 2543 if (touch_is_muted(wacom_wac)) {
d793ff81
PC
2544 if (!wacom_wac->shared->touch_down)
2545 return;
e29c62ff 2546 prox = false;
d793ff81 2547 }
601a22f3 2548
1b5d514a
JG
2549 wacom_wac->hid_data.num_received++;
2550 if (wacom_wac->hid_data.num_received > wacom_wac->hid_data.num_expected)
2551 return;
2552
601a22f3
JG
2553 if (mt) {
2554 int slot;
2555
2556 slot = input_mt_get_slot_by_key(input, hid_data->id);
ccb51c2e 2557 if (slot < 0) {
7cc8524f 2558 return;
ccb51c2e
JG
2559 } else {
2560 struct input_mt_slot *ps = &input->mt->slots[slot];
2561 int mt_id = input_mt_get_value(ps, ABS_MT_TRACKING_ID);
2562
2563 if (!prox && mt_id < 0) {
2564 // No data to send for this slot; short-circuit
2565 return;
2566 }
2567 }
7cc8524f 2568
601a22f3
JG
2569 input_mt_slot(input, slot);
2570 input_mt_report_slot_state(input, MT_TOOL_FINGER, prox);
2571 }
2572 else {
2573 input_report_key(input, BTN_TOUCH, prox);
2574 }
2575
2576 if (prox) {
2577 input_report_abs(input, mt ? ABS_MT_POSITION_X : ABS_X,
2578 hid_data->x);
2579 input_report_abs(input, mt ? ABS_MT_POSITION_Y : ABS_Y,
2580 hid_data->y);
488abb5c
JG
2581
2582 if (test_bit(ABS_MT_TOUCH_MAJOR, input->absbit)) {
2583 input_report_abs(input, ABS_MT_TOUCH_MAJOR, max(hid_data->width, hid_data->height));
2584 input_report_abs(input, ABS_MT_TOUCH_MINOR, min(hid_data->width, hid_data->height));
2585 if (hid_data->width != hid_data->height)
2586 input_report_abs(input, ABS_MT_ORIENTATION, hid_data->width <= hid_data->height ? 0 : 1);
2587 }
601a22f3
JG
2588 }
2589}
2590
354a3298 2591static void wacom_wac_finger_event(struct hid_device *hdev,
5ae6e89f
BT
2592 struct hid_field *field, struct hid_usage *usage, __s32 value)
2593{
2594 struct wacom *wacom = hid_get_drvdata(hdev);
2595 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
c9c09587 2596 unsigned equivalent_usage = wacom_equivalent_usage(usage->hid);
184eccd4 2597 struct wacom_features *features = &wacom->wacom_wac.features;
5ae6e89f 2598
9d339fe4 2599 if (touch_is_muted(wacom_wac) && !wacom_wac->shared->touch_down)
5bed0128
JG
2600 return;
2601
f4e11d59
AAS
2602 if (wacom_wac->is_invalid_bt_frame)
2603 return;
5ae6e89f 2604
c9c09587 2605 switch (equivalent_usage) {
86e16ee7
JG
2606 case HID_DG_CONFIDENCE:
2607 wacom_wac->hid_data.confidence = value;
2608 break;
5ae6e89f
BT
2609 case HID_GD_X:
2610 wacom_wac->hid_data.x = value;
2611 break;
2612 case HID_GD_Y:
2613 wacom_wac->hid_data.y = value;
2614 break;
488abb5c
JG
2615 case HID_DG_WIDTH:
2616 wacom_wac->hid_data.width = value;
2617 break;
2618 case HID_DG_HEIGHT:
2619 wacom_wac->hid_data.height = value;
2620 break;
5ae6e89f
BT
2621 case HID_DG_CONTACTID:
2622 wacom_wac->hid_data.id = value;
2623 break;
2624 case HID_DG_TIPSWITCH:
2625 wacom_wac->hid_data.tipswitch = value;
2626 break;
f4e11d59
AAS
2627 case WACOM_HID_WT_REPORT_VALID:
2628 wacom_wac->is_invalid_bt_frame = !value;
2629 return;
184eccd4 2630 case HID_DG_CONTACTMAX:
88f38846
JG
2631 if (!features->touch_max) {
2632 features->touch_max = value;
2633 } else {
2634 hid_warn(hdev, "%s: ignoring attempt to overwrite non-zero touch_max "
2635 "%d -> %d\n", __func__, features->touch_max, value);
2636 }
184eccd4 2637 return;
5ae6e89f
BT
2638 }
2639
601a22f3 2640 if (usage->usage_index + 1 == field->report_count) {
86e16ee7
JG
2641 if (equivalent_usage == wacom_wac->hid_data.last_slot_field &&
2642 wacom_wac->hid_data.confidence)
2a6cdbdd 2643 wacom_wac_finger_slot(wacom_wac, wacom_wac->touch_input);
601a22f3 2644 }
5ae6e89f
BT
2645}
2646
06324e0c
JG
2647static void wacom_wac_finger_pre_report(struct hid_device *hdev,
2648 struct hid_report *report)
2649{
1b5d514a
JG
2650 struct wacom *wacom = hid_get_drvdata(hdev);
2651 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2652 struct hid_data* hid_data = &wacom_wac->hid_data;
003f50ab 2653 int i;
1b5d514a 2654
9d339fe4 2655 if (touch_is_muted(wacom_wac) && !wacom_wac->shared->touch_down)
5bed0128
JG
2656 return;
2657
f4e11d59 2658 wacom_wac->is_invalid_bt_frame = false;
86e16ee7
JG
2659
2660 hid_data->confidence = true;
f4e11d59 2661
003f50ab
JG
2662 for (i = 0; i < report->maxfield; i++) {
2663 struct hid_field *field = report->field[i];
2664 int j;
2665
2666 for (j = 0; j < field->maxusage; j++) {
2667 struct hid_usage *usage = &field->usage[j];
ac2423c9
AAS
2668 unsigned int equivalent_usage =
2669 wacom_equivalent_usage(usage->hid);
003f50ab 2670
ac2423c9 2671 switch (equivalent_usage) {
003f50ab
JG
2672 case HID_GD_X:
2673 case HID_GD_Y:
2674 case HID_DG_WIDTH:
2675 case HID_DG_HEIGHT:
2676 case HID_DG_CONTACTID:
2677 case HID_DG_INRANGE:
2678 case HID_DG_INVERT:
2679 case HID_DG_TIPSWITCH:
ac2423c9 2680 hid_data->last_slot_field = equivalent_usage;
003f50ab 2681 break;
b43f977d
JG
2682 case HID_DG_CONTACTCOUNT:
2683 hid_data->cc_report = report->id;
2684 hid_data->cc_index = i;
2685 hid_data->cc_value_index = j;
2686 break;
499522c8
JG
2687 }
2688 }
2689 }
b43f977d
JG
2690
2691 if (hid_data->cc_report != 0 &&
2692 hid_data->cc_index >= 0) {
2693 struct hid_field *field = report->field[hid_data->cc_index];
2694 int value = field->value[hid_data->cc_value_index];
3b01e5dc 2695 if (value) {
b43f977d 2696 hid_data->num_expected = value;
3b01e5dc
JG
2697 hid_data->num_received = 0;
2698 }
b43f977d
JG
2699 }
2700 else {
2701 hid_data->num_expected = wacom_wac->features.touch_max;
3b01e5dc 2702 hid_data->num_received = 0;
b43f977d 2703 }
06324e0c
JG
2704}
2705
5ae6e89f
BT
2706static void wacom_wac_finger_report(struct hid_device *hdev,
2707 struct hid_report *report)
2708{
2709 struct wacom *wacom = hid_get_drvdata(hdev);
2710 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2a6cdbdd 2711 struct input_dev *input = wacom_wac->touch_input;
5ae6e89f
BT
2712 unsigned touch_max = wacom_wac->features.touch_max;
2713
ccb51c2e
JG
2714 /* if there was nothing to process, don't send an empty sync */
2715 if (wacom_wac->hid_data.num_expected == 0)
2716 return;
2717
1b5d514a
JG
2718 /* If more packets of data are expected, give us a chance to
2719 * process them rather than immediately syncing a partial
2720 * update.
2721 */
2722 if (wacom_wac->hid_data.num_received < wacom_wac->hid_data.num_expected)
2723 return;
2724
5ae6e89f 2725 if (touch_max > 1)
601a22f3
JG
2726 input_mt_sync_frame(input);
2727
5ae6e89f 2728 input_sync(input);
1b5d514a 2729 wacom_wac->hid_data.num_received = 0;
3b01e5dc 2730 wacom_wac->hid_data.num_expected = 0;
5ae6e89f
BT
2731
2732 /* keep touch state for pen event */
7d059ed0 2733 wacom_wac->shared->touch_down = wacom_wac_finger_count_touches(wacom_wac);
5ae6e89f
BT
2734}
2735
7704ac93
BT
2736void wacom_wac_usage_mapping(struct hid_device *hdev,
2737 struct hid_field *field, struct hid_usage *usage)
2738{
2739 struct wacom *wacom = hid_get_drvdata(hdev);
2740 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
e5bc8eb1 2741 struct wacom_features *features = &wacom_wac->features;
7704ac93 2742
ac2423c9
AAS
2743 if (WACOM_DIRECT_DEVICE(field))
2744 features->device_type |= WACOM_DEVICETYPE_DIRECT;
7704ac93 2745
5ac3d4ae
JG
2746 /* usage tests must precede field tests */
2747 if (WACOM_BATTERY_USAGE(usage))
2748 wacom_wac_battery_usage_mapping(hdev, field, usage);
2749 else if (WACOM_PAD_FIELD(field))
354a3298 2750 wacom_wac_pad_usage_mapping(hdev, field, usage);
5922e613 2751 else if (WACOM_PEN_FIELD(field))
354a3298 2752 wacom_wac_pen_usage_mapping(hdev, field, usage);
5922e613 2753 else if (WACOM_FINGER_FIELD(field))
354a3298 2754 wacom_wac_finger_usage_mapping(hdev, field, usage);
7704ac93
BT
2755}
2756
354a3298 2757void wacom_wac_event(struct hid_device *hdev, struct hid_field *field,
7704ac93
BT
2758 struct hid_usage *usage, __s32 value)
2759{
2760 struct wacom *wacom = hid_get_drvdata(hdev);
2761
2762 if (wacom->wacom_wac.features.type != HID_GENERIC)
354a3298 2763 return;
7704ac93 2764
60a22186
AAS
2765 if (value > field->logical_maximum || value < field->logical_minimum)
2766 return;
2767
5ac3d4ae
JG
2768 /* usage tests must precede field tests */
2769 if (WACOM_BATTERY_USAGE(usage))
2770 wacom_wac_battery_event(hdev, field, usage, value);
2771 else if (WACOM_PAD_FIELD(field) && wacom->wacom_wac.pad_input)
2772 wacom_wac_pad_event(hdev, field, usage, value);
2773 else if (WACOM_PEN_FIELD(field) && wacom->wacom_wac.pen_input)
354a3298 2774 wacom_wac_pen_event(hdev, field, usage, value);
6f46cf9b 2775 else if (WACOM_FINGER_FIELD(field) && wacom->wacom_wac.touch_input)
354a3298 2776 wacom_wac_finger_event(hdev, field, usage, value);
7704ac93
BT
2777}
2778
f8b6a747
AAS
2779static void wacom_report_events(struct hid_device *hdev,
2780 struct hid_report *report, int collection_index,
2781 int field_index)
06324e0c
JG
2782{
2783 int r;
2784
f8b6a747 2785 for (r = field_index; r < report->maxfield; r++) {
06324e0c
JG
2786 struct hid_field *field;
2787 unsigned count, n;
2788
2789 field = report->field[r];
2790 count = field->report_count;
2791
2792 if (!(HID_MAIN_ITEM_VARIABLE & field->flags))
2793 continue;
2794
f8b6a747
AAS
2795 for (n = 0 ; n < count; n++) {
2796 if (field->usage[n].collection_index == collection_index)
2797 wacom_wac_event(hdev, field, &field->usage[n],
2798 field->value[n]);
2799 else
2800 return;
2801 }
06324e0c
JG
2802 }
2803}
2804
7ba8fc09 2805static int wacom_wac_collection(struct hid_device *hdev, struct hid_report *report,
f8b6a747
AAS
2806 int collection_index, struct hid_field *field,
2807 int field_index)
7704ac93
BT
2808{
2809 struct wacom *wacom = hid_get_drvdata(hdev);
06324e0c 2810
f8b6a747 2811 wacom_report_events(hdev, report, collection_index, field_index);
06324e0c 2812
a9ce7856
JG
2813 /*
2814 * Non-input reports may be sent prior to the device being
2815 * completely initialized. Since only their events need
2816 * to be processed, exit after 'wacom_report_events' has
2817 * been called to prevent potential crashes in the report-
2818 * processing functions.
2819 */
2820 if (report->type != HID_INPUT_REPORT)
f8b6a747 2821 return -1;
5ac3d4ae 2822
d9216d75
JG
2823 if (WACOM_PAD_FIELD(field))
2824 return 0;
2825 else if (WACOM_PEN_FIELD(field) && wacom->wacom_wac.pen_input)
c9cfb2ac 2826 wacom_wac_pen_report(hdev, report);
6f46cf9b 2827 else if (WACOM_FINGER_FIELD(field) && wacom->wacom_wac.touch_input)
c9cfb2ac 2828 wacom_wac_finger_report(hdev, report);
f8b6a747
AAS
2829
2830 return 0;
2831}
2832
2833void wacom_wac_report(struct hid_device *hdev, struct hid_report *report)
2834{
2835 struct wacom *wacom = hid_get_drvdata(hdev);
2836 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2837 struct hid_field *field;
2838 bool pad_in_hid_field = false, pen_in_hid_field = false,
d4b8efeb 2839 finger_in_hid_field = false, true_pad = false;
f8b6a747
AAS
2840 int r;
2841 int prev_collection = -1;
2842
2843 if (wacom_wac->features.type != HID_GENERIC)
2844 return;
2845
2846 for (r = 0; r < report->maxfield; r++) {
2847 field = report->field[r];
2848
2849 if (WACOM_PAD_FIELD(field))
2850 pad_in_hid_field = true;
2851 if (WACOM_PEN_FIELD(field))
2852 pen_in_hid_field = true;
2853 if (WACOM_FINGER_FIELD(field))
2854 finger_in_hid_field = true;
d4b8efeb
AAS
2855 if (wacom_equivalent_usage(field->physical) == HID_DG_TABLETFUNCTIONKEY)
2856 true_pad = true;
f8b6a747
AAS
2857 }
2858
2859 wacom_wac_battery_pre_report(hdev, report);
2860
2861 if (pad_in_hid_field && wacom->wacom_wac.pad_input)
2862 wacom_wac_pad_pre_report(hdev, report);
2863 if (pen_in_hid_field && wacom->wacom_wac.pen_input)
2864 wacom_wac_pen_pre_report(hdev, report);
2865 if (finger_in_hid_field && wacom->wacom_wac.touch_input)
2866 wacom_wac_finger_pre_report(hdev, report);
2867
2868 for (r = 0; r < report->maxfield; r++) {
2869 field = report->field[r];
2870
2871 if (field->usage[0].collection_index != prev_collection) {
2872 if (wacom_wac_collection(hdev, report,
2873 field->usage[0].collection_index, field, r) < 0)
2874 return;
2875 prev_collection = field->usage[0].collection_index;
2876 }
2877 }
2878
2879 wacom_wac_battery_report(hdev, report);
d4b8efeb
AAS
2880
2881 if (true_pad && wacom->wacom_wac.pad_input)
2882 wacom_wac_pad_report(hdev, report, field);
7704ac93
BT
2883}
2884
e1d38e49 2885static int wacom_bpt_touch(struct wacom_wac *wacom)
cb734c03 2886{
f4ccbef2 2887 struct wacom_features *features = &wacom->features;
2a6cdbdd 2888 struct input_dev *input = wacom->touch_input;
3116871f 2889 struct input_dev *pad_input = wacom->pad_input;
cb734c03 2890 unsigned char *data = wacom->data;
cb734c03
HR
2891 int i;
2892
5a6c865d
CB
2893 if (data[0] != 0x02)
2894 return 0;
2895
cb734c03 2896 for (i = 0; i < 2; i++) {
8f906860 2897 int offset = (data[1] & 0x80) ? (8 * i) : (9 * i);
1924e05e
PC
2898 bool touch = report_touch_events(wacom)
2899 && (data[offset + 3] & 0x80);
8f906860
CB
2900
2901 input_mt_slot(input, i);
2902 input_mt_report_slot_state(input, MT_TOOL_FINGER, touch);
c5f4dec1 2903 if (touch) {
8f906860
CB
2904 int x = get_unaligned_be16(&data[offset + 3]) & 0x7ff;
2905 int y = get_unaligned_be16(&data[offset + 5]) & 0x7ff;
f4ccbef2
HR
2906 if (features->quirks & WACOM_QUIRK_BBTOUCH_LOWRES) {
2907 x <<= 5;
2908 y <<= 5;
2909 }
cb734c03
HR
2910 input_report_abs(input, ABS_MT_POSITION_X, x);
2911 input_report_abs(input, ABS_MT_POSITION_Y, y);
cb734c03 2912 }
cb734c03
HR
2913 }
2914
9a1c0012 2915 input_mt_sync_frame(input);
cb734c03 2916
3116871f
BT
2917 input_report_key(pad_input, BTN_LEFT, (data[1] & 0x08) != 0);
2918 input_report_key(pad_input, BTN_FORWARD, (data[1] & 0x04) != 0);
2919 input_report_key(pad_input, BTN_BACK, (data[1] & 0x02) != 0);
2920 input_report_key(pad_input, BTN_RIGHT, (data[1] & 0x01) != 0);
7d059ed0 2921 wacom->shared->touch_down = wacom_wac_finger_count_touches(wacom);
cb734c03 2922
3116871f 2923 return 1;
cb734c03
HR
2924}
2925
7d059ed0 2926static void wacom_bpt3_touch_msg(struct wacom_wac *wacom, unsigned char *data)
73149ab8 2927{
9a35c411 2928 struct wacom_features *features = &wacom->features;
2a6cdbdd 2929 struct input_dev *input = wacom->touch_input;
73149ab8 2930 bool touch = data[1] & 0x80;
02295e68
PC
2931 int slot = input_mt_get_slot_by_key(input, data[0]);
2932
2933 if (slot < 0)
7d059ed0 2934 return;
73149ab8 2935
1924e05e 2936 touch = touch && report_touch_events(wacom);
73149ab8 2937
02295e68 2938 input_mt_slot(input, slot);
73149ab8
CB
2939 input_mt_report_slot_state(input, MT_TOOL_FINGER, touch);
2940
2941 if (touch) {
2942 int x = (data[2] << 4) | (data[4] >> 4);
2943 int y = (data[3] << 4) | (data[4] & 0x0f);
9a35c411 2944 int width, height;
4e904954 2945
eda01dab 2946 if (features->type >= INTUOSPS && features->type <= INTUOSHT2) {
0b279da7
JG
2947 width = data[5] * 100;
2948 height = data[6] * 100;
9a35c411
PC
2949 } else {
2950 /*
2951 * "a" is a scaled-down area which we assume is
2952 * roughly circular and which can be described as:
2953 * a=(pi*r^2)/C.
2954 */
2955 int a = data[5];
d7da3a3c
PC
2956 int x_res = input_abs_get_res(input, ABS_MT_POSITION_X);
2957 int y_res = input_abs_get_res(input, ABS_MT_POSITION_Y);
2958 width = 2 * int_sqrt(a * WACOM_CONTACT_AREA_SCALE);
9a35c411
PC
2959 height = width * y_res / x_res;
2960 }
73149ab8
CB
2961
2962 input_report_abs(input, ABS_MT_POSITION_X, x);
2963 input_report_abs(input, ABS_MT_POSITION_Y, y);
4e904954
JG
2964 input_report_abs(input, ABS_MT_TOUCH_MAJOR, width);
2965 input_report_abs(input, ABS_MT_TOUCH_MINOR, height);
73149ab8
CB
2966 }
2967}
2968
2969static void wacom_bpt3_button_msg(struct wacom_wac *wacom, unsigned char *data)
2970{
3116871f 2971 struct input_dev *input = wacom->pad_input;
b5fd2a3e 2972 struct wacom_features *features = &wacom->features;
73149ab8 2973
eda01dab 2974 if (features->type == INTUOSHT || features->type == INTUOSHT2) {
b5fd2a3e
PC
2975 input_report_key(input, BTN_LEFT, (data[1] & 0x02) != 0);
2976 input_report_key(input, BTN_BACK, (data[1] & 0x08) != 0);
2977 } else {
2978 input_report_key(input, BTN_BACK, (data[1] & 0x02) != 0);
2979 input_report_key(input, BTN_LEFT, (data[1] & 0x08) != 0);
2980 }
73149ab8 2981 input_report_key(input, BTN_FORWARD, (data[1] & 0x04) != 0);
73149ab8
CB
2982 input_report_key(input, BTN_RIGHT, (data[1] & 0x01) != 0);
2983}
2984
2985static int wacom_bpt3_touch(struct wacom_wac *wacom)
2986{
73149ab8 2987 unsigned char *data = wacom->data;
19d57d3a 2988 int count = data[1] & 0x07;
eda01dab 2989 int touch_changed = 0, i;
73149ab8 2990
5a6c865d
CB
2991 if (data[0] != 0x02)
2992 return 0;
2993
73149ab8
CB
2994 /* data has up to 7 fixed sized 8-byte messages starting at data[2] */
2995 for (i = 0; i < count; i++) {
2996 int offset = (8 * i) + 2;
2997 int msg_id = data[offset];
2998
eda01dab 2999 if (msg_id >= 2 && msg_id <= 17) {
7d059ed0 3000 wacom_bpt3_touch_msg(wacom, data + offset);
eda01dab
PC
3001 touch_changed++;
3002 } else if (msg_id == 128)
73149ab8
CB
3003 wacom_bpt3_button_msg(wacom, data + offset);
3004
3005 }
2a6cdbdd 3006
eda01dab 3007 /* only update touch if we actually have a touchpad and touch data changed */
84dfbd7f 3008 if (wacom->touch_input && touch_changed) {
2a6cdbdd
JG
3009 input_mt_sync_frame(wacom->touch_input);
3010 wacom->shared->touch_down = wacom_wac_finger_count_touches(wacom);
3011 }
73149ab8 3012
3116871f 3013 return 1;
73149ab8
CB
3014}
3015
2aaacb15
CB
3016static int wacom_bpt_pen(struct wacom_wac *wacom)
3017{
961794a0 3018 struct wacom_features *features = &wacom->features;
2a6cdbdd 3019 struct input_dev *input = wacom->pen_input;
2aaacb15 3020 unsigned char *data = wacom->data;
8947b0cf
JG
3021 int x = 0, y = 0, p = 0, d = 0;
3022 bool pen = false, btn1 = false, btn2 = false;
3023 bool range, prox, rdy;
2aaacb15 3024
4ca4ec71 3025 if (data[0] != WACOM_REPORT_PENABLED)
5a6c865d
CB
3026 return 0;
3027
8947b0cf
JG
3028 range = (data[1] & 0x80) == 0x80;
3029 prox = (data[1] & 0x40) == 0x40;
3030 rdy = (data[1] & 0x20) == 0x20;
3031
3032 wacom->shared->stylus_in_proximity = range;
3033 if (delay_pen_events(wacom))
3034 return 0;
3035
3036 if (rdy) {
3037 p = le16_to_cpup((__le16 *)&data[6]);
3038 pen = data[1] & 0x01;
3039 btn1 = data[1] & 0x02;
3040 btn2 = data[1] & 0x04;
3041 }
3042 if (prox) {
3043 x = le16_to_cpup((__le16 *)&data[2]);
3044 y = le16_to_cpup((__le16 *)&data[4]);
2aaacb15 3045
0149931e
PC
3046 if (data[1] & 0x08) {
3047 wacom->tool[0] = BTN_TOOL_RUBBER;
3048 wacom->id[0] = ERASER_DEVICE_ID;
3049 } else {
3050 wacom->tool[0] = BTN_TOOL_PEN;
3051 wacom->id[0] = STYLUS_DEVICE_ID;
2aaacb15 3052 }
8947b0cf 3053 wacom->reporting_data = true;
0149931e 3054 }
8947b0cf 3055 if (range) {
c18c2cec
CB
3056 /*
3057 * Convert distance from out prox to distance from tablet.
3058 * distance will be greater than distance_max once
3059 * touching and applying pressure; do not report negative
3060 * distance.
3061 */
961794a0
PC
3062 if (data[8] <= features->distance_max)
3063 d = features->distance_max - data[8];
0149931e
PC
3064 } else {
3065 wacom->id[0] = 0;
2aaacb15
CB
3066 }
3067
8947b0cf
JG
3068 if (wacom->reporting_data) {
3069 input_report_key(input, BTN_TOUCH, pen);
3070 input_report_key(input, BTN_STYLUS, btn1);
3071 input_report_key(input, BTN_STYLUS2, btn2);
2aaacb15 3072
8947b0cf
JG
3073 if (prox || !range) {
3074 input_report_abs(input, ABS_X, x);
3075 input_report_abs(input, ABS_Y, y);
3076 }
3077 input_report_abs(input, ABS_PRESSURE, p);
3078 input_report_abs(input, ABS_DISTANCE, d);
2aaacb15 3079
8947b0cf
JG
3080 input_report_key(input, wacom->tool[0], range); /* PEN or RUBBER */
3081 input_report_abs(input, ABS_MISC, wacom->id[0]); /* TOOL ID */
3082 }
3083
3084 if (!range) {
3085 wacom->reporting_data = false;
3086 }
2aaacb15
CB
3087
3088 return 1;
3089}
3090
e1d38e49
CB
3091static int wacom_bpt_irq(struct wacom_wac *wacom, size_t len)
3092{
eda01dab
PC
3093 struct wacom_features *features = &wacom->features;
3094
3095 if ((features->type == INTUOSHT2) &&
eda01dab
PC
3096 (features->device_type & WACOM_DEVICETYPE_PEN))
3097 return wacom_intuos_irq(wacom);
3098 else if (len == WACOM_PKGLEN_BBTOUCH)
e1d38e49 3099 return wacom_bpt_touch(wacom);
73149ab8
CB
3100 else if (len == WACOM_PKGLEN_BBTOUCH3)
3101 return wacom_bpt3_touch(wacom);
3102 else if (len == WACOM_PKGLEN_BBFUN || len == WACOM_PKGLEN_BBPEN)
2aaacb15 3103 return wacom_bpt_pen(wacom);
e1d38e49
CB
3104
3105 return 0;
3106}
3107
8c97a765
BT
3108static void wacom_bamboo_pad_pen_event(struct wacom_wac *wacom,
3109 unsigned char *data)
3110{
3111 unsigned char prefix;
3112
3113 /*
3114 * We need to reroute the event from the debug interface to the
3115 * pen interface.
3116 * We need to add the report ID to the actual pen report, so we
3117 * temporary overwrite the first byte to prevent having to kzalloc/kfree
3118 * and memcpy the report.
3119 */
3120 prefix = data[0];
3121 data[0] = WACOM_REPORT_BPAD_PEN;
3122
3123 /*
3124 * actually reroute the event.
3125 * No need to check if wacom->shared->pen is valid, hid_input_report()
3126 * will check for us.
3127 */
3128 hid_input_report(wacom->shared->pen, HID_INPUT_REPORT, data,
3129 WACOM_PKGLEN_PENABLED, 1);
3130
3131 data[0] = prefix;
3132}
3133
3134static int wacom_bamboo_pad_touch_event(struct wacom_wac *wacom,
3135 unsigned char *data)
3136{
2a6cdbdd 3137 struct input_dev *input = wacom->touch_input;
8c97a765
BT
3138 unsigned char *finger_data, prefix;
3139 unsigned id;
3140 int x, y;
3141 bool valid;
3142
3143 prefix = data[0];
3144
3145 for (id = 0; id < wacom->features.touch_max; id++) {
3146 valid = !!(prefix & BIT(id)) &&
1924e05e 3147 report_touch_events(wacom);
8c97a765
BT
3148
3149 input_mt_slot(input, id);
3150 input_mt_report_slot_state(input, MT_TOOL_FINGER, valid);
3151
3152 if (!valid)
3153 continue;
3154
3155 finger_data = data + 1 + id * 3;
3156 x = finger_data[0] | ((finger_data[1] & 0x0f) << 8);
3157 y = (finger_data[2] << 4) | (finger_data[1] >> 4);
3158
3159 input_report_abs(input, ABS_MT_POSITION_X, x);
3160 input_report_abs(input, ABS_MT_POSITION_Y, y);
3161 }
3162
3163 input_mt_sync_frame(input);
3164
3165 input_report_key(input, BTN_LEFT, prefix & 0x40);
3166 input_report_key(input, BTN_RIGHT, prefix & 0x80);
3167
3168 /* keep touch state for pen event */
1924e05e 3169 wacom->shared->touch_down = !!prefix && report_touch_events(wacom);
8c97a765
BT
3170
3171 return 1;
3172}
3173
3174static int wacom_bamboo_pad_irq(struct wacom_wac *wacom, size_t len)
3175{
3176 unsigned char *data = wacom->data;
3177
3178 if (!((len == WACOM_PKGLEN_BPAD_TOUCH) ||
3179 (len == WACOM_PKGLEN_BPAD_TOUCH_USB)) ||
3180 (data[0] != WACOM_REPORT_BPAD_TOUCH))
3181 return 0;
3182
3183 if (data[1] & 0x01)
3184 wacom_bamboo_pad_pen_event(wacom, &data[1]);
3185
3186 if (data[1] & 0x02)
3187 return wacom_bamboo_pad_touch_event(wacom, &data[9]);
3188
3189 return 0;
3190}
3191
d3825d51
CB
3192static int wacom_wireless_irq(struct wacom_wac *wacom, size_t len)
3193{
16bf288c
CB
3194 unsigned char *data = wacom->data;
3195 int connected;
3196
b5fd2a3e 3197 if (len != WACOM_PKGLEN_WIRELESS || data[0] != WACOM_REPORT_WL)
d3825d51
CB
3198 return 0;
3199
16bf288c
CB
3200 connected = data[1] & 0x01;
3201 if (connected) {
b0882cb7 3202 int pid, battery, charging;
16bf288c 3203
eda01dab
PC
3204 if ((wacom->shared->type == INTUOSHT ||
3205 wacom->shared->type == INTUOSHT2) &&
44b96838
PC
3206 wacom->shared->touch_input &&
3207 wacom->shared->touch_max) {
961794a0
PC
3208 input_report_switch(wacom->shared->touch_input,
3209 SW_MUTE_DEVICE, data[5] & 0x40);
3210 input_sync(wacom->shared->touch_input);
3211 }
3212
16bf288c 3213 pid = get_unaligned_be16(&data[6]);
ac8d1010 3214 battery = (data[5] & 0x3f) * 100 / 31;
b0882cb7 3215 charging = !!(data[5] & 0x80);
16bf288c
CB
3216 if (wacom->pid != pid) {
3217 wacom->pid = pid;
d17d1f17 3218 wacom_schedule_work(wacom, WACOM_WORKER_WIRELESS);
16bf288c 3219 }
ac8d1010 3220
16e45989
JG
3221 wacom_notify_battery(wacom, WACOM_POWER_SUPPLY_STATUS_AUTO,
3222 battery, charging, 1, 0);
953f2c5f 3223
16bf288c
CB
3224 } else if (wacom->pid != 0) {
3225 /* disconnected while previously connected */
3226 wacom->pid = 0;
d17d1f17 3227 wacom_schedule_work(wacom, WACOM_WORKER_WIRELESS);
16e45989 3228 wacom_notify_battery(wacom, POWER_SUPPLY_STATUS_UNKNOWN, 0, 0, 0, 0);
16bf288c
CB
3229 }
3230
d3825d51
CB
3231 return 0;
3232}
3233
4ca4ec71
JG
3234static int wacom_status_irq(struct wacom_wac *wacom_wac, size_t len)
3235{
8f93b0b2 3236 struct wacom *wacom = container_of(wacom_wac, struct wacom, wacom_wac);
4ca4ec71
JG
3237 struct wacom_features *features = &wacom_wac->features;
3238 unsigned char *data = wacom_wac->data;
3239
3240 if (data[0] != WACOM_REPORT_USB)
3241 return 0;
3242
eda01dab
PC
3243 if ((features->type == INTUOSHT ||
3244 features->type == INTUOSHT2) &&
4ca4ec71
JG
3245 wacom_wac->shared->touch_input &&
3246 features->touch_max) {
3247 input_report_switch(wacom_wac->shared->touch_input,
3248 SW_MUTE_DEVICE, data[8] & 0x40);
3249 input_sync(wacom_wac->shared->touch_input);
16bf288c
CB
3250 }
3251
8f93b0b2
JG
3252 if (data[9] & 0x02) { /* wireless module is attached */
3253 int battery = (data[8] & 0x3f) * 100 / 31;
b0882cb7 3254 bool charging = !!(data[8] & 0x80);
8f93b0b2 3255
16e45989
JG
3256 wacom_notify_battery(wacom_wac, WACOM_POWER_SUPPLY_STATUS_AUTO,
3257 battery, charging, battery || charging, 1);
8f93b0b2 3258
59d69bc8 3259 if (!wacom->battery.battery &&
8f93b0b2
JG
3260 !(features->quirks & WACOM_QUIRK_BATTERY)) {
3261 features->quirks |= WACOM_QUIRK_BATTERY;
d17d1f17 3262 wacom_schedule_work(wacom_wac, WACOM_WORKER_BATTERY);
8f93b0b2
JG
3263 }
3264 }
3265 else if ((features->quirks & WACOM_QUIRK_BATTERY) &&
59d69bc8 3266 wacom->battery.battery) {
8f93b0b2 3267 features->quirks &= ~WACOM_QUIRK_BATTERY;
d17d1f17 3268 wacom_schedule_work(wacom_wac, WACOM_WORKER_BATTERY);
16e45989 3269 wacom_notify_battery(wacom_wac, POWER_SUPPLY_STATUS_UNKNOWN, 0, 0, 0, 0);
8f93b0b2 3270 }
d3825d51
CB
3271 return 0;
3272}
3273
95dd3b30 3274void wacom_wac_irq(struct wacom_wac *wacom_wac, size_t len)
3bea733a 3275{
95dd3b30
DT
3276 bool sync;
3277
e33da8a5 3278 switch (wacom_wac->features.type) {
73a97f4f 3279 case PENPARTNER:
95dd3b30
DT
3280 sync = wacom_penpartner_irq(wacom_wac);
3281 break;
73a97f4f
DT
3282
3283 case PL:
95dd3b30
DT
3284 sync = wacom_pl_irq(wacom_wac);
3285 break;
73a97f4f
DT
3286
3287 case WACOM_G4:
3288 case GRAPHIRE:
387142bb 3289 case GRAPHIRE_BT:
73a97f4f 3290 case WACOM_MO:
95dd3b30
DT
3291 sync = wacom_graphire_irq(wacom_wac);
3292 break;
73a97f4f
DT
3293
3294 case PTU:
95dd3b30
DT
3295 sync = wacom_ptu_irq(wacom_wac);
3296 break;
73a97f4f 3297
c8f2edc5
PC
3298 case DTU:
3299 sync = wacom_dtu_irq(wacom_wac);
3300 break;
3301
497ab1f2 3302 case DTUS:
fff00bf8 3303 case DTUSX:
497ab1f2
PC
3304 sync = wacom_dtus_irq(wacom_wac);
3305 break;
3306
73a97f4f
DT
3307 case INTUOS:
3308 case INTUOS3S:
3309 case INTUOS3:
3310 case INTUOS3L:
3311 case INTUOS4S:
3312 case INTUOS4:
3313 case INTUOS4L:
3314 case CINTIQ:
3315 case WACOM_BEE:
56218563 3316 case WACOM_13HD:
3a4b4aaa 3317 case WACOM_21UX2:
d838c644 3318 case WACOM_22HD:
803296b6 3319 case WACOM_24HD:
500d4160 3320 case WACOM_27QHD:
a112e9fd 3321 case DTK:
36d3c510 3322 case CINTIQ_HYBRID:
f7acb55c 3323 case CINTIQ_COMPANION_2:
95dd3b30
DT
3324 sync = wacom_intuos_irq(wacom_wac);
3325 break;
73a97f4f 3326
81af7e61
BT
3327 case INTUOS4WL:
3328 sync = wacom_intuos_bt_irq(wacom_wac, len);
3329 break;
3330
b1e4279e 3331 case WACOM_24HDT:
500d4160 3332 case WACOM_27QHDT:
b1e4279e
JG
3333 sync = wacom_24hdt_irq(wacom_wac);
3334 break;
3335
ae584ca4
JG
3336 case INTUOS5S:
3337 case INTUOS5:
3338 case INTUOS5L:
9a35c411
PC
3339 case INTUOSPS:
3340 case INTUOSPM:
3341 case INTUOSPL:
ae584ca4
JG
3342 if (len == WACOM_PKGLEN_BBTOUCH3)
3343 sync = wacom_bpt3_touch(wacom_wac);
2d13a438
JG
3344 else if (wacom_wac->data[0] == WACOM_REPORT_USB)
3345 sync = wacom_status_irq(wacom_wac, len);
ae584ca4
JG
3346 else
3347 sync = wacom_intuos_irq(wacom_wac);
3348 break;
3349
4922cd26 3350 case INTUOSP2_BT:
912c6aa6 3351 case INTUOSP2S_BT:
87046b6c 3352 case INTUOSHT3_BT:
4922cd26
JG
3353 sync = wacom_intuos_pro2_bt_irq(wacom_wac, len);
3354 break;
3355
73a97f4f 3356 case TABLETPC:
ac173837 3357 case TABLETPCE:
73a97f4f 3358 case TABLETPC2FG:
1963518b 3359 case MTSCREEN:
6afdc289 3360 case MTTPC:
d51ddb2b 3361 case MTTPC_B:
95dd3b30
DT
3362 sync = wacom_tpc_irq(wacom_wac, len);
3363 break;
73a97f4f 3364
cb734c03 3365 case BAMBOO_PT:
3b164a00
PC
3366 case BAMBOO_PEN:
3367 case BAMBOO_TOUCH:
b5fd2a3e 3368 case INTUOSHT:
eda01dab 3369 case INTUOSHT2:
4ca4ec71
JG
3370 if (wacom_wac->data[0] == WACOM_REPORT_USB)
3371 sync = wacom_status_irq(wacom_wac, len);
3372 else
3373 sync = wacom_bpt_irq(wacom_wac, len);
cb734c03
HR
3374 break;
3375
8c97a765
BT
3376 case BAMBOO_PAD:
3377 sync = wacom_bamboo_pad_irq(wacom_wac, len);
cb734c03
HR
3378 break;
3379
d3825d51
CB
3380 case WIRELESS:
3381 sync = wacom_wireless_irq(wacom_wac, len);
3382 break;
3383
72b236d6 3384 case REMOTE:
e6f2813a 3385 sync = false;
72b236d6 3386 if (wacom_wac->data[0] == WACOM_REPORT_DEVICE_LIST)
e6f2813a 3387 wacom_remote_status_irq(wacom_wac, len);
72b236d6
AS
3388 else
3389 sync = wacom_remote_irq(wacom_wac, len);
3390 break;
3391
73a97f4f 3392 default:
95dd3b30
DT
3393 sync = false;
3394 break;
3bea733a 3395 }
95dd3b30 3396
d2d13f18 3397 if (sync) {
2a6cdbdd
JG
3398 if (wacom_wac->pen_input)
3399 input_sync(wacom_wac->pen_input);
3400 if (wacom_wac->touch_input)
3401 input_sync(wacom_wac->touch_input);
d2d13f18
BT
3402 if (wacom_wac->pad_input)
3403 input_sync(wacom_wac->pad_input);
3404 }
3bea733a
PC
3405}
3406
eda01dab 3407static void wacom_setup_basic_pro_pen(struct wacom_wac *wacom_wac)
8da23fc1 3408{
2a6cdbdd 3409 struct input_dev *input_dev = wacom_wac->pen_input;
8da23fc1
DT
3410
3411 input_set_capability(input_dev, EV_MSC, MSC_SERIAL);
8da23fc1 3412
8da23fc1 3413 __set_bit(BTN_TOOL_PEN, input_dev->keybit);
8da23fc1
DT
3414 __set_bit(BTN_STYLUS, input_dev->keybit);
3415 __set_bit(BTN_STYLUS2, input_dev->keybit);
3416
3417 input_set_abs_params(input_dev, ABS_DISTANCE,
bef7e200 3418 0, wacom_wac->features.distance_max, wacom_wac->features.distance_fuzz, 0);
eda01dab
PC
3419}
3420
3421static void wacom_setup_cintiq(struct wacom_wac *wacom_wac)
3422{
3423 struct input_dev *input_dev = wacom_wac->pen_input;
bef7e200 3424 struct wacom_features *features = &wacom_wac->features;
eda01dab
PC
3425
3426 wacom_setup_basic_pro_pen(wacom_wac);
3427
3428 __set_bit(BTN_TOOL_RUBBER, input_dev->keybit);
3429 __set_bit(BTN_TOOL_BRUSH, input_dev->keybit);
3430 __set_bit(BTN_TOOL_PENCIL, input_dev->keybit);
3431 __set_bit(BTN_TOOL_AIRBRUSH, input_dev->keybit);
3432
8da23fc1 3433 input_set_abs_params(input_dev, ABS_WHEEL, 0, 1023, 0, 0);
bef7e200 3434 input_set_abs_params(input_dev, ABS_TILT_X, -64, 63, features->tilt_fuzz, 0);
26fe4124 3435 input_abs_set_res(input_dev, ABS_TILT_X, 57);
bef7e200 3436 input_set_abs_params(input_dev, ABS_TILT_Y, -64, 63, features->tilt_fuzz, 0);
26fe4124 3437 input_abs_set_res(input_dev, ABS_TILT_Y, 57);
6521d0bf
PC
3438}
3439
3440static void wacom_setup_intuos(struct wacom_wac *wacom_wac)
3441{
2a6cdbdd 3442 struct input_dev *input_dev = wacom_wac->pen_input;
6521d0bf
PC
3443
3444 input_set_capability(input_dev, EV_REL, REL_WHEEL);
3445
3446 wacom_setup_cintiq(wacom_wac);
3447
3448 __set_bit(BTN_LEFT, input_dev->keybit);
3449 __set_bit(BTN_RIGHT, input_dev->keybit);
3450 __set_bit(BTN_MIDDLE, input_dev->keybit);
3451 __set_bit(BTN_SIDE, input_dev->keybit);
3452 __set_bit(BTN_EXTRA, input_dev->keybit);
3453 __set_bit(BTN_TOOL_MOUSE, input_dev->keybit);
3454 __set_bit(BTN_TOOL_LENS, input_dev->keybit);
3455
8da23fc1 3456 input_set_abs_params(input_dev, ABS_RZ, -900, 899, 0, 0);
26fe4124 3457 input_abs_set_res(input_dev, ABS_RZ, 287);
8da23fc1
DT
3458 input_set_abs_params(input_dev, ABS_THROTTLE, -1023, 1023, 0, 0);
3459}
3460
42f4f272 3461void wacom_setup_device_quirks(struct wacom *wacom)
bc73dd39 3462{
11db8173 3463 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
42f4f272 3464 struct wacom_features *features = &wacom->wacom_wac.features;
bc73dd39 3465
862cf553
JG
3466 /* The pen and pad share the same interface on most devices */
3467 if (features->type == GRAPHIRE_BT || features->type == WACOM_G4 ||
3b164a00
PC
3468 features->type == DTUS ||
3469 (features->type >= INTUOS3S && features->type <= WACOM_MO)) {
862cf553
JG
3470 if (features->device_type & WACOM_DEVICETYPE_PEN)
3471 features->device_type |= WACOM_DEVICETYPE_PAD;
3472 }
bc73dd39
HR
3473
3474 /* touch device found but size is not defined. use default */
aa86b18c 3475 if (features->device_type & WACOM_DEVICETYPE_TOUCH && !features->x_max) {
bc73dd39
HR
3476 features->x_max = 1023;
3477 features->y_max = 1023;
3478 }
3479
42f4f272
PC
3480 /*
3481 * Intuos5/Pro and Bamboo 3rd gen have no useful data about its
3482 * touch interface in its HID descriptor. If this is the touch
3483 * interface (PacketSize of WACOM_PKGLEN_BBTOUCH3), override the
3484 * tablet values.
3485 */
3b164a00
PC
3486 if ((features->type >= INTUOS5S && features->type <= INTUOSPL) ||
3487 (features->type >= INTUOSHT && features->type <= BAMBOO_PT)) {
42f4f272 3488 if (features->pktlen == WACOM_PKGLEN_BBTOUCH3) {
862cf553
JG
3489 if (features->touch_max)
3490 features->device_type |= WACOM_DEVICETYPE_TOUCH;
a3088abc 3491 if (features->type >= INTUOSHT && features->type <= BAMBOO_PT)
862cf553 3492 features->device_type |= WACOM_DEVICETYPE_PAD;
42f4f272 3493
3b8d5735
JG
3494 if (features->type == INTUOSHT2) {
3495 features->x_max = features->x_max / 10;
3496 features->y_max = features->y_max / 10;
3497 }
3498 else {
3499 features->x_max = 4096;
3500 features->y_max = 4096;
3501 }
42f4f272 3502 }
9633920e
JG
3503 else if (features->pktlen == WACOM_PKGLEN_BBTOUCH) {
3504 features->device_type |= WACOM_DEVICETYPE_PAD;
3505 }
42f4f272
PC
3506 }
3507
580549ef
BT
3508 /*
3509 * Hack for the Bamboo One:
3510 * the device presents a PAD/Touch interface as most Bamboos and even
3511 * sends ghosts PAD data on it. However, later, we must disable this
3512 * ghost interface, and we can not detect it unless we set it here
3513 * to WACOM_DEVICETYPE_PAD or WACOM_DEVICETYPE_TOUCH.
3514 */
3515 if (features->type == BAMBOO_PEN &&
3516 features->pktlen == WACOM_PKGLEN_BBTOUCH3)
3517 features->device_type |= WACOM_DEVICETYPE_PAD;
3518
42f4f272 3519 /*
042628ab
JG
3520 * Raw Wacom-mode pen and touch events both come from interface
3521 * 0, whose HID descriptor has an application usage of 0xFF0D
8de82280 3522 * (i.e., WACOM_HID_WD_DIGITIZER). We route pen packets back
042628ab 3523 * out through the HID_GENERIC device created for interface 1,
70caee0a 3524 * so rewrite this one to be of type WACOM_DEVICETYPE_TOUCH.
42f4f272
PC
3525 */
3526 if (features->type == BAMBOO_PAD)
70caee0a 3527 features->device_type = WACOM_DEVICETYPE_TOUCH;
42f4f272 3528
72b236d6
AS
3529 if (features->type == REMOTE)
3530 features->device_type = WACOM_DEVICETYPE_PAD;
42f4f272 3531
912c6aa6
AAS
3532 if (features->type == INTUOSP2_BT ||
3533 features->type == INTUOSP2S_BT) {
4922cd26
JG
3534 features->device_type |= WACOM_DEVICETYPE_PEN |
3535 WACOM_DEVICETYPE_PAD |
3536 WACOM_DEVICETYPE_TOUCH;
3537 features->quirks |= WACOM_QUIRK_BATTERY;
3538 }
3539
87046b6c
AAS
3540 if (features->type == INTUOSHT3_BT) {
3541 features->device_type |= WACOM_DEVICETYPE_PEN |
3542 WACOM_DEVICETYPE_PAD;
3543 features->quirks |= WACOM_QUIRK_BATTERY;
3544 }
3545
e5bc8eb1
JG
3546 switch (features->type) {
3547 case PL:
3548 case DTU:
3549 case DTUS:
3550 case DTUSX:
3551 case WACOM_21UX2:
3552 case WACOM_22HD:
3553 case DTK:
3554 case WACOM_24HD:
3555 case WACOM_27QHD:
3556 case CINTIQ_HYBRID:
3557 case CINTIQ_COMPANION_2:
3558 case CINTIQ:
3559 case WACOM_BEE:
3560 case WACOM_13HD:
3561 case WACOM_24HDT:
3562 case WACOM_27QHDT:
3563 case TABLETPC:
3564 case TABLETPCE:
3565 case TABLETPC2FG:
3566 case MTSCREEN:
3567 case MTTPC:
3568 case MTTPC_B:
3569 features->device_type |= WACOM_DEVICETYPE_DIRECT;
3570 break;
3571 }
3572
42f4f272
PC
3573 if (wacom->hdev->bus == BUS_BLUETOOTH)
3574 features->quirks |= WACOM_QUIRK_BATTERY;
3575
73149ab8 3576 /* quirk for bamboo touch with 2 low res touches */
be853fd1 3577 if ((features->type == BAMBOO_PT || features->type == BAMBOO_TOUCH) &&
73149ab8 3578 features->pktlen == WACOM_PKGLEN_BBTOUCH) {
f4ccbef2
HR
3579 features->x_max <<= 5;
3580 features->y_max <<= 5;
3581 features->x_fuzz <<= 5;
3582 features->y_fuzz <<= 5;
f4ccbef2 3583 features->quirks |= WACOM_QUIRK_BBTOUCH_LOWRES;
cb734c03 3584 }
d3825d51
CB
3585
3586 if (features->type == WIRELESS) {
ccad85cc 3587 if (features->device_type == WACOM_DEVICETYPE_WL_MONITOR) {
ac8d1010
BT
3588 features->quirks |= WACOM_QUIRK_BATTERY;
3589 }
d3825d51 3590 }
7c35dc3c
BT
3591
3592 if (features->type == REMOTE)
3593 features->device_type |= WACOM_DEVICETYPE_WL_MONITOR;
11db8173
JG
3594
3595 /* HID descriptor for DTK-2451 / DTH-2452 claims to report lots
3596 * of things it shouldn't. Lets fix up the damage...
3597 */
3598 if (wacom->hdev->product == 0x382 || wacom->hdev->product == 0x37d) {
3599 features->quirks &= ~WACOM_QUIRK_TOOLSERIAL;
3600 __clear_bit(BTN_TOOL_BRUSH, wacom_wac->pen_input->keybit);
3601 __clear_bit(BTN_TOOL_PENCIL, wacom_wac->pen_input->keybit);
3602 __clear_bit(BTN_TOOL_AIRBRUSH, wacom_wac->pen_input->keybit);
3603 __clear_bit(ABS_Z, wacom_wac->pen_input->absbit);
3604 __clear_bit(ABS_DISTANCE, wacom_wac->pen_input->absbit);
3605 __clear_bit(ABS_TILT_X, wacom_wac->pen_input->absbit);
3606 __clear_bit(ABS_TILT_Y, wacom_wac->pen_input->absbit);
3607 __clear_bit(ABS_WHEEL, wacom_wac->pen_input->absbit);
3608 __clear_bit(ABS_MISC, wacom_wac->pen_input->absbit);
3609 __clear_bit(MSC_SERIAL, wacom_wac->pen_input->mscbit);
3610 __clear_bit(EV_MSC, wacom_wac->pen_input->evbit);
3611 }
bc73dd39
HR
3612}
3613
2636a3f2 3614int wacom_setup_pen_input_capabilities(struct input_dev *input_dev,
8c0e0a4f
PC
3615 struct wacom_wac *wacom_wac)
3616{
3617 struct wacom_features *features = &wacom_wac->features;
8c0e0a4f 3618
2636a3f2
JG
3619 if (!(features->device_type & WACOM_DEVICETYPE_PEN))
3620 return -ENODEV;
3621
e5bc8eb1
JG
3622 if (features->device_type & WACOM_DEVICETYPE_DIRECT)
3623 __set_bit(INPUT_PROP_DIRECT, input_dev->propbit);
3624 else
3625 __set_bit(INPUT_PROP_POINTER, input_dev->propbit);
3626
46fc466e
PC
3627 if (features->type == HID_GENERIC)
3628 /* setup has already been done */
7704ac93
BT
3629 return 0;
3630
276559d8 3631 input_dev->evbit[0] |= BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
8c0e0a4f 3632 __set_bit(BTN_TOUCH, input_dev->keybit);
8da23fc1
DT
3633 __set_bit(ABS_MISC, input_dev->absbit);
3634
e779ef23
JG
3635 input_set_abs_params(input_dev, ABS_X, 0 + features->offset_left,
3636 features->x_max - features->offset_right,
3637 features->x_fuzz, 0);
3638 input_set_abs_params(input_dev, ABS_Y, 0 + features->offset_top,
3639 features->y_max - features->offset_bottom,
3640 features->y_fuzz, 0);
2636a3f2
JG
3641 input_set_abs_params(input_dev, ABS_PRESSURE, 0,
3642 features->pressure_max, features->pressure_fuzz, 0);
3643
3644 /* penabled devices have fixed resolution for each model */
3645 input_abs_set_res(input_dev, ABS_X, features->x_resolution);
3646 input_abs_set_res(input_dev, ABS_Y, features->y_resolution);
3647
497ab1f2 3648 switch (features->type) {
387142bb
BT
3649 case GRAPHIRE_BT:
3650 __clear_bit(ABS_MISC, input_dev->absbit);
df561f66 3651 fallthrough;
a2f71c6c
PC
3652
3653 case WACOM_MO:
3654 case WACOM_G4:
387142bb
BT
3655 input_set_abs_params(input_dev, ABS_DISTANCE, 0,
3656 features->distance_max,
bef7e200 3657 features->distance_fuzz, 0);
df561f66 3658 fallthrough;
803296b6 3659
a2f71c6c 3660 case GRAPHIRE:
387142bb 3661 input_set_capability(input_dev, EV_REL, REL_WHEEL);
803296b6 3662
387142bb
BT
3663 __set_bit(BTN_LEFT, input_dev->keybit);
3664 __set_bit(BTN_RIGHT, input_dev->keybit);
3665 __set_bit(BTN_MIDDLE, input_dev->keybit);
3666
3667 __set_bit(BTN_TOOL_RUBBER, input_dev->keybit);
3668 __set_bit(BTN_TOOL_PEN, input_dev->keybit);
3669 __set_bit(BTN_TOOL_MOUSE, input_dev->keybit);
3670 __set_bit(BTN_STYLUS, input_dev->keybit);
3671 __set_bit(BTN_STYLUS2, input_dev->keybit);
387142bb 3672 break;
c73a1afb 3673
500d4160 3674 case WACOM_27QHD:
803296b6 3675 case WACOM_24HD:
a112e9fd 3676 case DTK:
d838c644 3677 case WACOM_22HD:
3a4b4aaa 3678 case WACOM_21UX2:
73a97f4f 3679 case WACOM_BEE:
6521d0bf 3680 case CINTIQ:
56218563 3681 case WACOM_13HD:
a2f71c6c 3682 case CINTIQ_HYBRID:
f7acb55c 3683 case CINTIQ_COMPANION_2:
56218563 3684 input_set_abs_params(input_dev, ABS_Z, -900, 899, 0, 0);
26fe4124 3685 input_abs_set_res(input_dev, ABS_Z, 287);
56218563
PC
3686 wacom_setup_cintiq(wacom_wac);
3687 break;
3688
73a97f4f
DT
3689 case INTUOS3:
3690 case INTUOS3L:
73a97f4f 3691 case INTUOS3S:
a2f71c6c
PC
3692 case INTUOS4:
3693 case INTUOS4WL:
3694 case INTUOS4L:
3695 case INTUOS4S:
8da23fc1 3696 input_set_abs_params(input_dev, ABS_Z, -900, 899, 0, 0);
26fe4124 3697 input_abs_set_res(input_dev, ABS_Z, 287);
df561f66 3698 fallthrough;
73a97f4f
DT
3699
3700 case INTUOS:
8da23fc1 3701 wacom_setup_intuos(wacom_wac);
73a97f4f
DT
3702 break;
3703
9fee6195
JG
3704 case INTUOS5:
3705 case INTUOS5L:
9a35c411
PC
3706 case INTUOSPM:
3707 case INTUOSPL:
ae584ca4 3708 case INTUOS5S:
9a35c411 3709 case INTUOSPS:
4922cd26 3710 case INTUOSP2_BT:
912c6aa6 3711 case INTUOSP2S_BT:
2636a3f2
JG
3712 input_set_abs_params(input_dev, ABS_DISTANCE, 0,
3713 features->distance_max,
bef7e200 3714 features->distance_fuzz, 0);
ae584ca4 3715
2636a3f2
JG
3716 input_set_abs_params(input_dev, ABS_Z, -900, 899, 0, 0);
3717 input_abs_set_res(input_dev, ABS_Z, 287);
ae584ca4 3718
2636a3f2 3719 wacom_setup_intuos(wacom_wac);
ae584ca4
JG
3720 break;
3721
b1e4279e 3722 case WACOM_24HDT:
500d4160 3723 case WACOM_27QHDT:
1963518b 3724 case MTSCREEN:
6afdc289 3725 case MTTPC:
d51ddb2b 3726 case MTTPC_B:
6795a524 3727 case TABLETPC2FG:
73a97f4f 3728 case TABLETPC:
ac173837 3729 case TABLETPCE:
a43c7c53 3730 __clear_bit(ABS_MISC, input_dev->absbit);
df561f66 3731 fallthrough;
73a97f4f 3732
497ab1f2 3733 case DTUS:
fff00bf8 3734 case DTUSX:
73a97f4f 3735 case PL:
c8f2edc5 3736 case DTU:
8da23fc1 3737 __set_bit(BTN_TOOL_PEN, input_dev->keybit);
3512069e 3738 __set_bit(BTN_TOOL_RUBBER, input_dev->keybit);
8da23fc1
DT
3739 __set_bit(BTN_STYLUS, input_dev->keybit);
3740 __set_bit(BTN_STYLUS2, input_dev->keybit);
3512069e
JG
3741 break;
3742
3743 case PTU:
8da23fc1 3744 __set_bit(BTN_STYLUS2, input_dev->keybit);
df561f66 3745 fallthrough;
73a97f4f
DT
3746
3747 case PENPARTNER:
1fab84aa 3748 __set_bit(BTN_TOOL_PEN, input_dev->keybit);
8da23fc1 3749 __set_bit(BTN_TOOL_RUBBER, input_dev->keybit);
1fab84aa 3750 __set_bit(BTN_STYLUS, input_dev->keybit);
73a97f4f 3751 break;
cb734c03 3752
b5fd2a3e 3753 case INTUOSHT:
cb734c03 3754 case BAMBOO_PT:
3b164a00 3755 case BAMBOO_PEN:
eda01dab 3756 case INTUOSHT2:
87046b6c
AAS
3757 case INTUOSHT3_BT:
3758 if (features->type == INTUOSHT2 ||
3759 features->type == INTUOSHT3_BT) {
eda01dab
PC
3760 wacom_setup_basic_pro_pen(wacom_wac);
3761 } else {
3762 __clear_bit(ABS_MISC, input_dev->absbit);
3763 __set_bit(BTN_TOOL_PEN, input_dev->keybit);
3764 __set_bit(BTN_TOOL_RUBBER, input_dev->keybit);
3765 __set_bit(BTN_STYLUS, input_dev->keybit);
3766 __set_bit(BTN_STYLUS2, input_dev->keybit);
3767 input_set_abs_params(input_dev, ABS_DISTANCE, 0,
2636a3f2 3768 features->distance_max,
bef7e200 3769 features->distance_fuzz, 0);
eda01dab 3770 }
2636a3f2
JG
3771 break;
3772 case BAMBOO_PAD:
3773 __clear_bit(ABS_MISC, input_dev->absbit);
3774 break;
3775 }
3776 return 0;
3777}
02295e68 3778
2636a3f2
JG
3779int wacom_setup_touch_input_capabilities(struct input_dev *input_dev,
3780 struct wacom_wac *wacom_wac)
3781{
3782 struct wacom_features *features = &wacom_wac->features;
30ebc1ae 3783
2636a3f2
JG
3784 if (!(features->device_type & WACOM_DEVICETYPE_TOUCH))
3785 return -ENODEV;
3786
e5bc8eb1
JG
3787 if (features->device_type & WACOM_DEVICETYPE_DIRECT)
3788 __set_bit(INPUT_PROP_DIRECT, input_dev->propbit);
3789 else
3790 __set_bit(INPUT_PROP_POINTER, input_dev->propbit);
3791
2636a3f2
JG
3792 if (features->type == HID_GENERIC)
3793 /* setup has already been done */
3794 return 0;
3795
276559d8 3796 input_dev->evbit[0] |= BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
2636a3f2
JG
3797 __set_bit(BTN_TOUCH, input_dev->keybit);
3798
3799 if (features->touch_max == 1) {
3800 input_set_abs_params(input_dev, ABS_X, 0,
3801 features->x_max, features->x_fuzz, 0);
3802 input_set_abs_params(input_dev, ABS_Y, 0,
3803 features->y_max, features->y_fuzz, 0);
3804 input_abs_set_res(input_dev, ABS_X,
3805 features->x_resolution);
3806 input_abs_set_res(input_dev, ABS_Y,
3807 features->y_resolution);
3808 }
3809 else if (features->touch_max > 1) {
3810 input_set_abs_params(input_dev, ABS_MT_POSITION_X, 0,
3811 features->x_max, features->x_fuzz, 0);
3812 input_set_abs_params(input_dev, ABS_MT_POSITION_Y, 0,
3813 features->y_max, features->y_fuzz, 0);
3814 input_abs_set_res(input_dev, ABS_MT_POSITION_X,
3815 features->x_resolution);
3816 input_abs_set_res(input_dev, ABS_MT_POSITION_Y,
3817 features->y_resolution);
3818 }
3819
3820 switch (features->type) {
4922cd26 3821 case INTUOSP2_BT:
912c6aa6 3822 case INTUOSP2S_BT:
4922cd26
JG
3823 input_dev->evbit[0] |= BIT_MASK(EV_SW);
3824 __set_bit(SW_MUTE_DEVICE, input_dev->swbit);
3825
3826 if (wacom_wac->shared->touch->product == 0x361) {
3827 input_set_abs_params(input_dev, ABS_MT_POSITION_X,
3828 0, 12440, 4, 0);
3829 input_set_abs_params(input_dev, ABS_MT_POSITION_Y,
3830 0, 8640, 4, 0);
3831 }
3832 else if (wacom_wac->shared->touch->product == 0x360) {
3833 input_set_abs_params(input_dev, ABS_MT_POSITION_X,
3834 0, 8960, 4, 0);
3835 input_set_abs_params(input_dev, ABS_MT_POSITION_Y,
3836 0, 5920, 4, 0);
3837 }
912c6aa6
AAS
3838 else if (wacom_wac->shared->touch->product == 0x393) {
3839 input_set_abs_params(input_dev, ABS_MT_POSITION_X,
3840 0, 6400, 4, 0);
3841 input_set_abs_params(input_dev, ABS_MT_POSITION_Y,
3842 0, 4000, 4, 0);
3843 }
4922cd26 3844 input_abs_set_res(input_dev, ABS_MT_POSITION_X, 40);
68c20cc2 3845 input_abs_set_res(input_dev, ABS_MT_POSITION_Y, 40);
4922cd26 3846
df561f66 3847 fallthrough;
4922cd26 3848
2636a3f2
JG
3849 case INTUOS5:
3850 case INTUOS5L:
3851 case INTUOSPM:
3852 case INTUOSPL:
3853 case INTUOS5S:
3854 case INTUOSPS:
2636a3f2
JG
3855 input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR, 0, features->x_max, 0, 0);
3856 input_set_abs_params(input_dev, ABS_MT_TOUCH_MINOR, 0, features->y_max, 0, 0);
3857 input_mt_init_slots(input_dev, features->touch_max, INPUT_MT_POINTER);
3858 break;
3859
3860 case WACOM_24HDT:
3861 input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR, 0, features->x_max, 0, 0);
3862 input_set_abs_params(input_dev, ABS_MT_WIDTH_MAJOR, 0, features->x_max, 0, 0);
3863 input_set_abs_params(input_dev, ABS_MT_WIDTH_MINOR, 0, features->y_max, 0, 0);
3864 input_set_abs_params(input_dev, ABS_MT_ORIENTATION, 0, 1, 0, 0);
df561f66 3865 fallthrough;
2636a3f2
JG
3866
3867 case WACOM_27QHDT:
670e9092
AAS
3868 if (wacom_wac->shared->touch->product == 0x32C ||
3869 wacom_wac->shared->touch->product == 0xF6) {
3870 input_dev->evbit[0] |= BIT_MASK(EV_SW);
3871 __set_bit(SW_MUTE_DEVICE, input_dev->swbit);
6ca2350e 3872 wacom_wac->has_mute_touch_switch = true;
dc9dc864 3873 wacom_wac->is_soft_touch_switch = true;
670e9092 3874 }
df561f66 3875 fallthrough;
670e9092 3876
2636a3f2
JG
3877 case MTSCREEN:
3878 case MTTPC:
3879 case MTTPC_B:
3880 case TABLETPC2FG:
3881 input_mt_init_slots(input_dev, features->touch_max, INPUT_MT_DIRECT);
df561f66 3882 fallthrough;
2636a3f2
JG
3883
3884 case TABLETPC:
3885 case TABLETPCE:
2636a3f2
JG
3886 break;
3887
3888 case INTUOSHT:
eda01dab 3889 case INTUOSHT2:
2636a3f2
JG
3890 input_dev->evbit[0] |= BIT_MASK(EV_SW);
3891 __set_bit(SW_MUTE_DEVICE, input_dev->swbit);
df561f66 3892 fallthrough;
2636a3f2
JG
3893
3894 case BAMBOO_PT:
3b164a00 3895 case BAMBOO_TOUCH:
2636a3f2
JG
3896 if (features->pktlen == WACOM_PKGLEN_BBTOUCH3) {
3897 input_set_abs_params(input_dev,
3898 ABS_MT_TOUCH_MAJOR,
3899 0, features->x_max, 0, 0);
3900 input_set_abs_params(input_dev,
3901 ABS_MT_TOUCH_MINOR,
3902 0, features->y_max, 0, 0);
cb734c03 3903 }
2636a3f2 3904 input_mt_init_slots(input_dev, features->touch_max, INPUT_MT_POINTER);
cb734c03 3905 break;
2636a3f2 3906
8c97a765 3907 case BAMBOO_PAD:
8c97a765
BT
3908 input_mt_init_slots(input_dev, features->touch_max,
3909 INPUT_MT_POINTER);
3910 __set_bit(BTN_LEFT, input_dev->keybit);
3911 __set_bit(BTN_RIGHT, input_dev->keybit);
3912 break;
3bea733a 3913 }
1963518b 3914 return 0;
3bea733a
PC
3915}
3916
49005b9f
JG
3917static int wacom_numbered_button_to_key(int n)
3918{
3919 if (n < 10)
3920 return BTN_0 + n;
3921 else if (n < 16)
3922 return BTN_A + (n-10);
3923 else if (n < 18)
3924 return BTN_BASE + (n-16);
3925 else
3926 return 0;
3927}
3928
5397df15 3929static void wacom_setup_numbered_buttons(struct input_dev *input_dev,
70ee06c5
AS
3930 int button_count)
3931{
3932 int i;
3933
49005b9f
JG
3934 for (i = 0; i < button_count; i++) {
3935 int key = wacom_numbered_button_to_key(i);
3936
3937 if (key)
3938 __set_bit(key, input_dev->keybit);
3939 }
70ee06c5
AS
3940}
3941
6a06281e
BT
3942static void wacom_24hd_update_leds(struct wacom *wacom, int mask, int group)
3943{
3944 struct wacom_led *led;
3945 int i;
3946 bool updated = false;
3947
3948 /*
3949 * 24HD has LED group 1 to the left and LED group 0 to the right.
3950 * So group 0 matches the second half of the buttons and thus the mask
3951 * needs to be shifted.
3952 */
3953 if (group == 0)
3954 mask >>= 8;
3955
3956 for (i = 0; i < 3; i++) {
3957 led = wacom_led_find(wacom, group, i);
3958 if (!led) {
3959 hid_err(wacom->hdev, "can't find LED %d in group %d\n",
3960 i, group);
3961 continue;
3962 }
3963 if (!updated && mask & BIT(i)) {
3964 led->held = true;
3965 led_trigger_event(&led->trigger, LED_FULL);
3966 } else {
3967 led->held = false;
3968 }
3969 }
3970}
3971
34736aa9
BT
3972static bool wacom_is_led_toggled(struct wacom *wacom, int button_count,
3973 int mask, int group)
3974{
6441fc78 3975 int group_button;
34736aa9 3976
5a0fe8ab 3977 /*
6a06281e 3978 * 21UX2 has LED group 1 to the left and LED group 0
5a0fe8ab
BT
3979 * to the right. We need to reverse the group to match this
3980 * historical behavior.
3981 */
6a06281e 3982 if (wacom->wacom_wac.features.type == WACOM_21UX2)
5a0fe8ab
BT
3983 group = 1 - group;
3984
6441fc78 3985 group_button = group * (button_count/wacom->led.count);
34736aa9 3986
6441fc78
JG
3987 if (wacom->wacom_wac.features.type == INTUOSP2_BT)
3988 group_button = 8;
3989
3990 return mask & (1 << group_button);
34736aa9
BT
3991}
3992
3993static void wacom_update_led(struct wacom *wacom, int button_count, int mask,
3994 int group)
3995{
3996 struct wacom_led *led, *next_led;
3997 int cur;
3998 bool pressed;
3999
6a06281e
BT
4000 if (wacom->wacom_wac.features.type == WACOM_24HD)
4001 return wacom_24hd_update_leds(wacom, mask, group);
4002
34736aa9
BT
4003 pressed = wacom_is_led_toggled(wacom, button_count, mask, group);
4004 cur = wacom->led.groups[group].select;
4005
4006 led = wacom_led_find(wacom, group, cur);
4007 if (!led) {
4008 hid_err(wacom->hdev, "can't find current LED %d in group %d\n",
4009 cur, group);
4010 return;
4011 }
4012
4013 if (!pressed) {
4014 led->held = false;
4015 return;
4016 }
4017
4018 if (led->held && pressed)
4019 return;
4020
4021 next_led = wacom_led_next(wacom, led);
4022 if (!next_led) {
4023 hid_err(wacom->hdev, "can't find next LED in group %d\n",
4024 group);
4025 return;
4026 }
4027 if (next_led == led)
4028 return;
4029
4030 next_led->held = true;
4031 led_trigger_event(&next_led->trigger,
4032 wacom_leds_brightness_get(next_led));
4033}
4034
c7f0522a
JG
4035static void wacom_report_numbered_buttons(struct input_dev *input_dev,
4036 int button_count, int mask)
4037{
34736aa9 4038 struct wacom *wacom = input_get_drvdata(input_dev);
c7f0522a
JG
4039 int i;
4040
34736aa9
BT
4041 for (i = 0; i < wacom->led.count; i++)
4042 wacom_update_led(wacom, button_count, mask, i);
4043
49005b9f
JG
4044 for (i = 0; i < button_count; i++) {
4045 int key = wacom_numbered_button_to_key(i);
4046
4047 if (key)
4048 input_report_key(input_dev, key, mask & (1 << i));
4049 }
c7f0522a
JG
4050}
4051
d2d13f18
BT
4052int wacom_setup_pad_input_capabilities(struct input_dev *input_dev,
4053 struct wacom_wac *wacom_wac)
4054{
4055 struct wacom_features *features = &wacom_wac->features;
4056
e7deb157
PC
4057 if ((features->type == HID_GENERIC) && features->numbered_buttons > 0)
4058 features->device_type |= WACOM_DEVICETYPE_PAD;
4059
862cf553
JG
4060 if (!(features->device_type & WACOM_DEVICETYPE_PAD))
4061 return -ENODEV;
4062
7c35dc3c
BT
4063 if (features->type == REMOTE && input_dev == wacom_wac->pad_input)
4064 return -ENODEV;
4065
d2d13f18
BT
4066 input_dev->evbit[0] |= BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
4067
4068 /* kept for making legacy xf86-input-wacom working with the wheels */
4069 __set_bit(ABS_MISC, input_dev->absbit);
4070
4071 /* kept for making legacy xf86-input-wacom accepting the pad */
5922e613
JG
4072 if (!(input_dev->absinfo && (input_dev->absinfo[ABS_X].minimum ||
4073 input_dev->absinfo[ABS_X].maximum)))
4074 input_set_abs_params(input_dev, ABS_X, 0, 1, 0, 0);
4075 if (!(input_dev->absinfo && (input_dev->absinfo[ABS_Y].minimum ||
4076 input_dev->absinfo[ABS_Y].maximum)))
4077 input_set_abs_params(input_dev, ABS_Y, 0, 1, 0, 0);
d2d13f18 4078
12969e3b
BT
4079 /* kept for making udev and libwacom accepting the pad */
4080 __set_bit(BTN_STYLUS, input_dev->keybit);
4081
70ee06c5
AS
4082 wacom_setup_numbered_buttons(input_dev, features->numbered_buttons);
4083
d2d13f18 4084 switch (features->type) {
70ee06c5
AS
4085
4086 case CINTIQ_HYBRID:
f7acb55c 4087 case CINTIQ_COMPANION_2:
70ee06c5
AS
4088 case DTK:
4089 case DTUS:
387142bb 4090 case GRAPHIRE_BT:
387142bb
BT
4091 break;
4092
3813810c
BT
4093 case WACOM_MO:
4094 __set_bit(BTN_BACK, input_dev->keybit);
4095 __set_bit(BTN_LEFT, input_dev->keybit);
4096 __set_bit(BTN_FORWARD, input_dev->keybit);
4097 __set_bit(BTN_RIGHT, input_dev->keybit);
4098 input_set_abs_params(input_dev, ABS_WHEEL, 0, 71, 0, 0);
4099 break;
4100
4101 case WACOM_G4:
4102 __set_bit(BTN_BACK, input_dev->keybit);
3813810c 4103 __set_bit(BTN_FORWARD, input_dev->keybit);
3813810c
BT
4104 input_set_capability(input_dev, EV_REL, REL_WHEEL);
4105 break;
4106
10059cdc 4107 case WACOM_24HD:
10059cdc
BT
4108 __set_bit(KEY_PROG1, input_dev->keybit);
4109 __set_bit(KEY_PROG2, input_dev->keybit);
4110 __set_bit(KEY_PROG3, input_dev->keybit);
4111
670e9092
AAS
4112 __set_bit(KEY_ONSCREEN_KEYBOARD, input_dev->keybit);
4113 __set_bit(KEY_INFO, input_dev->keybit);
4114
4115 if (!features->oPid)
4116 __set_bit(KEY_BUTTONCONFIG, input_dev->keybit);
4117
10059cdc
BT
4118 input_set_abs_params(input_dev, ABS_WHEEL, 0, 71, 0, 0);
4119 input_set_abs_params(input_dev, ABS_THROTTLE, 0, 71, 0, 0);
4120 break;
4121
500d4160
PC
4122 case WACOM_27QHD:
4123 __set_bit(KEY_PROG1, input_dev->keybit);
4124 __set_bit(KEY_PROG2, input_dev->keybit);
4125 __set_bit(KEY_PROG3, input_dev->keybit);
670e9092
AAS
4126
4127 __set_bit(KEY_ONSCREEN_KEYBOARD, input_dev->keybit);
4128 __set_bit(KEY_BUTTONCONFIG, input_dev->keybit);
4129
4130 if (!features->oPid)
4131 __set_bit(KEY_CONTROLPANEL, input_dev->keybit);
500d4160
PC
4132 input_set_abs_params(input_dev, ABS_X, -2048, 2048, 0, 0);
4133 input_abs_set_res(input_dev, ABS_X, 1024); /* points/g */
4134 input_set_abs_params(input_dev, ABS_Y, -2048, 2048, 0, 0);
4135 input_abs_set_res(input_dev, ABS_Y, 1024);
4136 input_set_abs_params(input_dev, ABS_Z, -2048, 2048, 0, 0);
4137 input_abs_set_res(input_dev, ABS_Z, 1024);
4138 __set_bit(INPUT_PROP_ACCELEROMETER, input_dev->propbit);
4139 break;
4140
10059cdc
BT
4141 case WACOM_22HD:
4142 __set_bit(KEY_PROG1, input_dev->keybit);
4143 __set_bit(KEY_PROG2, input_dev->keybit);
4144 __set_bit(KEY_PROG3, input_dev->keybit);
670e9092
AAS
4145
4146 __set_bit(KEY_BUTTONCONFIG, input_dev->keybit);
4147 __set_bit(KEY_INFO, input_dev->keybit);
df561f66 4148 fallthrough;
10059cdc
BT
4149
4150 case WACOM_21UX2:
10059cdc 4151 case WACOM_BEE:
10059cdc 4152 case CINTIQ:
10059cdc
BT
4153 input_set_abs_params(input_dev, ABS_RX, 0, 4096, 0, 0);
4154 input_set_abs_params(input_dev, ABS_RY, 0, 4096, 0, 0);
4155 break;
4156
4157 case WACOM_13HD:
10059cdc
BT
4158 input_set_abs_params(input_dev, ABS_WHEEL, 0, 71, 0, 0);
4159 break;
4160
4161 case INTUOS3:
4162 case INTUOS3L:
10059cdc 4163 input_set_abs_params(input_dev, ABS_RY, 0, 4096, 0, 0);
df561f66 4164 fallthrough;
10059cdc
BT
4165
4166 case INTUOS3S:
10059cdc
BT
4167 input_set_abs_params(input_dev, ABS_RX, 0, 4096, 0, 0);
4168 break;
36d3c510 4169
10059cdc
BT
4170 case INTUOS5:
4171 case INTUOS5L:
4172 case INTUOSPM:
4173 case INTUOSPL:
10059cdc
BT
4174 case INTUOS5S:
4175 case INTUOSPS:
4922cd26 4176 case INTUOSP2_BT:
912c6aa6 4177 case INTUOSP2S_BT:
10059cdc 4178 input_set_abs_params(input_dev, ABS_WHEEL, 0, 71, 0, 0);
36d3c510 4179 break;
10059cdc 4180
81af7e61
BT
4181 case INTUOS4WL:
4182 /*
4183 * For Bluetooth devices, the udev rule does not work correctly
4184 * for pads unless we add a stylus capability, which forces
4185 * ID_INPUT_TABLET to be set.
4186 */
4187 __set_bit(BTN_STYLUS, input_dev->keybit);
df561f66 4188 fallthrough;
81af7e61 4189
10059cdc
BT
4190 case INTUOS4:
4191 case INTUOS4L:
10059cdc 4192 case INTUOS4S:
10059cdc
BT
4193 input_set_abs_params(input_dev, ABS_WHEEL, 0, 71, 0, 0);
4194 break;
4195
3116871f
BT
4196 case INTUOSHT:
4197 case BAMBOO_PT:
3b164a00 4198 case BAMBOO_TOUCH:
eda01dab 4199 case INTUOSHT2:
3116871f
BT
4200 __clear_bit(ABS_MISC, input_dev->absbit);
4201
4202 __set_bit(BTN_LEFT, input_dev->keybit);
4203 __set_bit(BTN_FORWARD, input_dev->keybit);
4204 __set_bit(BTN_BACK, input_dev->keybit);
4205 __set_bit(BTN_RIGHT, input_dev->keybit);
4206
4207 break;
4208
72b236d6
AS
4209 case REMOTE:
4210 input_set_capability(input_dev, EV_MSC, MSC_SERIAL);
4211 input_set_abs_params(input_dev, ABS_WHEEL, 0, 71, 0, 0);
4212 break;
4213
87046b6c 4214 case INTUOSHT3_BT:
5922e613
JG
4215 case HID_GENERIC:
4216 break;
4217
d2d13f18
BT
4218 default:
4219 /* no pad supported */
b3c8e93f 4220 return -ENODEV;
3bea733a 4221 }
1963518b 4222 return 0;
3bea733a
PC
4223}
4224
e87a344d 4225static const struct wacom_features wacom_features_0x00 =
80befa93
BT
4226 { "Wacom Penpartner", 5040, 3780, 255, 0,
4227 PENPARTNER, WACOM_PENPRTN_RES, WACOM_PENPRTN_RES };
e87a344d 4228static const struct wacom_features wacom_features_0x10 =
80befa93
BT
4229 { "Wacom Graphire", 10206, 7422, 511, 63,
4230 GRAPHIRE, WACOM_GRAPHIRE_RES, WACOM_GRAPHIRE_RES };
387142bb
BT
4231static const struct wacom_features wacom_features_0x81 =
4232 { "Wacom Graphire BT", 16704, 12064, 511, 32,
70ee06c5 4233 GRAPHIRE_BT, WACOM_GRAPHIRE_RES, WACOM_GRAPHIRE_RES, 2 };
e87a344d 4234static const struct wacom_features wacom_features_0x11 =
80befa93
BT
4235 { "Wacom Graphire2 4x5", 10206, 7422, 511, 63,
4236 GRAPHIRE, WACOM_GRAPHIRE_RES, WACOM_GRAPHIRE_RES };
e87a344d 4237static const struct wacom_features wacom_features_0x12 =
80befa93
BT
4238 { "Wacom Graphire2 5x7", 13918, 10206, 511, 63,
4239 GRAPHIRE, WACOM_GRAPHIRE_RES, WACOM_GRAPHIRE_RES };
e87a344d 4240static const struct wacom_features wacom_features_0x13 =
80befa93
BT
4241 { "Wacom Graphire3", 10208, 7424, 511, 63,
4242 GRAPHIRE, WACOM_GRAPHIRE_RES, WACOM_GRAPHIRE_RES };
e87a344d 4243static const struct wacom_features wacom_features_0x14 =
80befa93
BT
4244 { "Wacom Graphire3 6x8", 16704, 12064, 511, 63,
4245 GRAPHIRE, WACOM_GRAPHIRE_RES, WACOM_GRAPHIRE_RES };
e87a344d 4246static const struct wacom_features wacom_features_0x15 =
80befa93
BT
4247 { "Wacom Graphire4 4x5", 10208, 7424, 511, 63,
4248 WACOM_G4, WACOM_GRAPHIRE_RES, WACOM_GRAPHIRE_RES };
e87a344d 4249static const struct wacom_features wacom_features_0x16 =
80befa93
BT
4250 { "Wacom Graphire4 6x8", 16704, 12064, 511, 63,
4251 WACOM_G4, WACOM_GRAPHIRE_RES, WACOM_GRAPHIRE_RES };
e87a344d 4252static const struct wacom_features wacom_features_0x17 =
80befa93
BT
4253 { "Wacom BambooFun 4x5", 14760, 9225, 511, 63,
4254 WACOM_MO, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
e87a344d 4255static const struct wacom_features wacom_features_0x18 =
80befa93
BT
4256 { "Wacom BambooFun 6x8", 21648, 13530, 511, 63,
4257 WACOM_MO, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
e87a344d 4258static const struct wacom_features wacom_features_0x19 =
80befa93
BT
4259 { "Wacom Bamboo1 Medium", 16704, 12064, 511, 63,
4260 GRAPHIRE, WACOM_GRAPHIRE_RES, WACOM_GRAPHIRE_RES };
e87a344d 4261static const struct wacom_features wacom_features_0x60 =
80befa93
BT
4262 { "Wacom Volito", 5104, 3712, 511, 63,
4263 GRAPHIRE, WACOM_VOLITO_RES, WACOM_VOLITO_RES };
e87a344d 4264static const struct wacom_features wacom_features_0x61 =
80befa93
BT
4265 { "Wacom PenStation2", 3250, 2320, 255, 63,
4266 GRAPHIRE, WACOM_VOLITO_RES, WACOM_VOLITO_RES };
e87a344d 4267static const struct wacom_features wacom_features_0x62 =
80befa93
BT
4268 { "Wacom Volito2 4x5", 5104, 3712, 511, 63,
4269 GRAPHIRE, WACOM_VOLITO_RES, WACOM_VOLITO_RES };
e87a344d 4270static const struct wacom_features wacom_features_0x63 =
80befa93
BT
4271 { "Wacom Volito2 2x3", 3248, 2320, 511, 63,
4272 GRAPHIRE, WACOM_VOLITO_RES, WACOM_VOLITO_RES };
e87a344d 4273static const struct wacom_features wacom_features_0x64 =
80befa93
BT
4274 { "Wacom PenPartner2", 3250, 2320, 511, 63,
4275 GRAPHIRE, WACOM_VOLITO_RES, WACOM_VOLITO_RES };
e87a344d 4276static const struct wacom_features wacom_features_0x65 =
80befa93
BT
4277 { "Wacom Bamboo", 14760, 9225, 511, 63,
4278 WACOM_MO, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
e87a344d 4279static const struct wacom_features wacom_features_0x69 =
80befa93
BT
4280 { "Wacom Bamboo1", 5104, 3712, 511, 63,
4281 GRAPHIRE, WACOM_PENPRTN_RES, WACOM_PENPRTN_RES };
11d0cf88 4282static const struct wacom_features wacom_features_0x6A =
80befa93
BT
4283 { "Wacom Bamboo1 4x6", 14760, 9225, 1023, 63,
4284 GRAPHIRE, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
11d0cf88 4285static const struct wacom_features wacom_features_0x6B =
80befa93
BT
4286 { "Wacom Bamboo1 5x8", 21648, 13530, 1023, 63,
4287 GRAPHIRE, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
e87a344d 4288static const struct wacom_features wacom_features_0x20 =
80befa93
BT
4289 { "Wacom Intuos 4x5", 12700, 10600, 1023, 31,
4290 INTUOS, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
e87a344d 4291static const struct wacom_features wacom_features_0x21 =
80befa93
BT
4292 { "Wacom Intuos 6x8", 20320, 16240, 1023, 31,
4293 INTUOS, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
e87a344d 4294static const struct wacom_features wacom_features_0x22 =
80befa93
BT
4295 { "Wacom Intuos 9x12", 30480, 24060, 1023, 31,
4296 INTUOS, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
e87a344d 4297static const struct wacom_features wacom_features_0x23 =
80befa93
BT
4298 { "Wacom Intuos 12x12", 30480, 31680, 1023, 31,
4299 INTUOS, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
e87a344d 4300static const struct wacom_features wacom_features_0x24 =
80befa93
BT
4301 { "Wacom Intuos 12x18", 45720, 31680, 1023, 31,
4302 INTUOS, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
e87a344d 4303static const struct wacom_features wacom_features_0x30 =
80befa93
BT
4304 { "Wacom PL400", 5408, 4056, 255, 0,
4305 PL, WACOM_PL_RES, WACOM_PL_RES };
e87a344d 4306static const struct wacom_features wacom_features_0x31 =
80befa93
BT
4307 { "Wacom PL500", 6144, 4608, 255, 0,
4308 PL, WACOM_PL_RES, WACOM_PL_RES };
e87a344d 4309static const struct wacom_features wacom_features_0x32 =
80befa93
BT
4310 { "Wacom PL600", 6126, 4604, 255, 0,
4311 PL, WACOM_PL_RES, WACOM_PL_RES };
e87a344d 4312static const struct wacom_features wacom_features_0x33 =
80befa93
BT
4313 { "Wacom PL600SX", 6260, 5016, 255, 0,
4314 PL, WACOM_PL_RES, WACOM_PL_RES };
e87a344d 4315static const struct wacom_features wacom_features_0x34 =
80befa93
BT
4316 { "Wacom PL550", 6144, 4608, 511, 0,
4317 PL, WACOM_PL_RES, WACOM_PL_RES };
e87a344d 4318static const struct wacom_features wacom_features_0x35 =
80befa93
BT
4319 { "Wacom PL800", 7220, 5780, 511, 0,
4320 PL, WACOM_PL_RES, WACOM_PL_RES };
e87a344d 4321static const struct wacom_features wacom_features_0x37 =
80befa93
BT
4322 { "Wacom PL700", 6758, 5406, 511, 0,
4323 PL, WACOM_PL_RES, WACOM_PL_RES };
e87a344d 4324static const struct wacom_features wacom_features_0x38 =
80befa93
BT
4325 { "Wacom PL510", 6282, 4762, 511, 0,
4326 PL, WACOM_PL_RES, WACOM_PL_RES };
e87a344d 4327static const struct wacom_features wacom_features_0x39 =
80befa93
BT
4328 { "Wacom DTU710", 34080, 27660, 511, 0,
4329 PL, WACOM_PL_RES, WACOM_PL_RES };
e87a344d 4330static const struct wacom_features wacom_features_0xC4 =
80befa93
BT
4331 { "Wacom DTF521", 6282, 4762, 511, 0,
4332 PL, WACOM_PL_RES, WACOM_PL_RES };
e87a344d 4333static const struct wacom_features wacom_features_0xC0 =
80befa93
BT
4334 { "Wacom DTF720", 6858, 5506, 511, 0,
4335 PL, WACOM_PL_RES, WACOM_PL_RES };
e87a344d 4336static const struct wacom_features wacom_features_0xC2 =
80befa93
BT
4337 { "Wacom DTF720a", 6858, 5506, 511, 0,
4338 PL, WACOM_PL_RES, WACOM_PL_RES };
e87a344d 4339static const struct wacom_features wacom_features_0x03 =
80befa93
BT
4340 { "Wacom Cintiq Partner", 20480, 15360, 511, 0,
4341 PTU, WACOM_PL_RES, WACOM_PL_RES };
e87a344d 4342static const struct wacom_features wacom_features_0x41 =
80befa93
BT
4343 { "Wacom Intuos2 4x5", 12700, 10600, 1023, 31,
4344 INTUOS, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
e87a344d 4345static const struct wacom_features wacom_features_0x42 =
80befa93
BT
4346 { "Wacom Intuos2 6x8", 20320, 16240, 1023, 31,
4347 INTUOS, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
e87a344d 4348static const struct wacom_features wacom_features_0x43 =
80befa93
BT
4349 { "Wacom Intuos2 9x12", 30480, 24060, 1023, 31,
4350 INTUOS, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
e87a344d 4351static const struct wacom_features wacom_features_0x44 =
80befa93
BT
4352 { "Wacom Intuos2 12x12", 30480, 31680, 1023, 31,
4353 INTUOS, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
e87a344d 4354static const struct wacom_features wacom_features_0x45 =
80befa93
BT
4355 { "Wacom Intuos2 12x18", 45720, 31680, 1023, 31,
4356 INTUOS, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
e87a344d 4357static const struct wacom_features wacom_features_0xB0 =
80befa93 4358 { "Wacom Intuos3 4x5", 25400, 20320, 1023, 63,
70ee06c5 4359 INTUOS3S, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 4 };
e87a344d 4360static const struct wacom_features wacom_features_0xB1 =
80befa93 4361 { "Wacom Intuos3 6x8", 40640, 30480, 1023, 63,
70ee06c5 4362 INTUOS3, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 8 };
e87a344d 4363static const struct wacom_features wacom_features_0xB2 =
80befa93 4364 { "Wacom Intuos3 9x12", 60960, 45720, 1023, 63,
70ee06c5 4365 INTUOS3, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 8 };
e87a344d 4366static const struct wacom_features wacom_features_0xB3 =
80befa93 4367 { "Wacom Intuos3 12x12", 60960, 60960, 1023, 63,
70ee06c5 4368 INTUOS3L, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 8 };
e87a344d 4369static const struct wacom_features wacom_features_0xB4 =
80befa93 4370 { "Wacom Intuos3 12x19", 97536, 60960, 1023, 63,
70ee06c5 4371 INTUOS3L, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 8 };
e87a344d 4372static const struct wacom_features wacom_features_0xB5 =
80befa93 4373 { "Wacom Intuos3 6x11", 54204, 31750, 1023, 63,
70ee06c5 4374 INTUOS3, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 8 };
e87a344d 4375static const struct wacom_features wacom_features_0xB7 =
80befa93 4376 { "Wacom Intuos3 4x6", 31496, 19685, 1023, 63,
70ee06c5 4377 INTUOS3S, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 4 };
e87a344d 4378static const struct wacom_features wacom_features_0xB8 =
80befa93 4379 { "Wacom Intuos4 4x6", 31496, 19685, 2047, 63,
70ee06c5 4380 INTUOS4S, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 7 };
e87a344d 4381static const struct wacom_features wacom_features_0xB9 =
80befa93 4382 { "Wacom Intuos4 6x9", 44704, 27940, 2047, 63,
70ee06c5 4383 INTUOS4, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 9 };
e87a344d 4384static const struct wacom_features wacom_features_0xBA =
80befa93 4385 { "Wacom Intuos4 8x13", 65024, 40640, 2047, 63,
70ee06c5 4386 INTUOS4L, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 9 };
e87a344d 4387static const struct wacom_features wacom_features_0xBB =
80befa93 4388 { "Wacom Intuos4 12x19", 97536, 60960, 2047, 63,
70ee06c5 4389 INTUOS4L, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 9 };
3a4b4aaa 4390static const struct wacom_features wacom_features_0xBC =
80befa93 4391 { "Wacom Intuos4 WL", 40640, 25400, 2047, 63,
70ee06c5 4392 INTUOS4, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 9 };
81af7e61
BT
4393static const struct wacom_features wacom_features_0xBD =
4394 { "Wacom Intuos4 WL", 40640, 25400, 2047, 63,
70ee06c5 4395 INTUOS4WL, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 9 };
9fee6195 4396static const struct wacom_features wacom_features_0x26 =
80befa93 4397 { "Wacom Intuos5 touch S", 31496, 19685, 2047, 63,
70ee06c5 4398 INTUOS5S, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 7, .touch_max = 16 };
9fee6195 4399static const struct wacom_features wacom_features_0x27 =
80befa93 4400 { "Wacom Intuos5 touch M", 44704, 27940, 2047, 63,
70ee06c5 4401 INTUOS5, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 9, .touch_max = 16 };
9fee6195 4402static const struct wacom_features wacom_features_0x28 =
80befa93 4403 { "Wacom Intuos5 touch L", 65024, 40640, 2047, 63,
70ee06c5 4404 INTUOS5L, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 9, .touch_max = 16 };
9fee6195 4405static const struct wacom_features wacom_features_0x29 =
80befa93 4406 { "Wacom Intuos5 S", 31496, 19685, 2047, 63,
70ee06c5 4407 INTUOS5S, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 7 };
9fee6195 4408static const struct wacom_features wacom_features_0x2A =
80befa93 4409 { "Wacom Intuos5 M", 44704, 27940, 2047, 63,
70ee06c5 4410 INTUOS5, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 9 };
9a35c411 4411static const struct wacom_features wacom_features_0x314 =
80befa93 4412 { "Wacom Intuos Pro S", 31496, 19685, 2047, 63,
70ee06c5 4413 INTUOSPS, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 7, .touch_max = 16,
29b47391 4414 .check_for_hid_type = true, .hid_type = HID_TYPE_USBNONE };
9a35c411 4415static const struct wacom_features wacom_features_0x315 =
80befa93 4416 { "Wacom Intuos Pro M", 44704, 27940, 2047, 63,
70ee06c5 4417 INTUOSPM, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 9, .touch_max = 16,
29b47391 4418 .check_for_hid_type = true, .hid_type = HID_TYPE_USBNONE };
9a35c411 4419static const struct wacom_features wacom_features_0x317 =
80befa93 4420 { "Wacom Intuos Pro L", 65024, 40640, 2047, 63,
70ee06c5 4421 INTUOSPL, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 9, .touch_max = 16,
29b47391 4422 .check_for_hid_type = true, .hid_type = HID_TYPE_USBNONE };
803296b6 4423static const struct wacom_features wacom_features_0xF4 =
e779ef23 4424 { "Wacom Cintiq 24HD", 104480, 65600, 2047, 63,
70ee06c5 4425 WACOM_24HD, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 16,
e779ef23 4426 WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
fa770340 4427 WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET };
6f4d0382 4428static const struct wacom_features wacom_features_0xF8 =
e779ef23 4429 { "Wacom Cintiq 24HD touch", 104480, 65600, 2047, 63, /* Pen */
70ee06c5 4430 WACOM_24HD, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 16,
fa770340 4431 WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
e779ef23 4432 WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
3bd1f7e2 4433 .oVid = USB_VENDOR_ID_WACOM, .oPid = 0xf6 };
b1e4279e
JG
4434static const struct wacom_features wacom_features_0xF6 =
4435 { "Wacom Cintiq 24HD touch", .type = WACOM_24HDT, /* Touch */
29b47391
BT
4436 .oVid = USB_VENDOR_ID_WACOM, .oPid = 0xf8, .touch_max = 10,
4437 .check_for_hid_type = true, .hid_type = HID_TYPE_USBNONE };
500d4160 4438static const struct wacom_features wacom_features_0x32A =
e779ef23 4439 { "Wacom Cintiq 27QHD", 120140, 67920, 2047, 63,
70ee06c5 4440 WACOM_27QHD, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 0,
e779ef23 4441 WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
a7e6645e 4442 WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET };
500d4160 4443static const struct wacom_features wacom_features_0x32B =
e779ef23 4444 { "Wacom Cintiq 27QHD touch", 120140, 67920, 2047, 63,
70ee06c5 4445 WACOM_27QHD, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 0,
500d4160 4446 WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
e779ef23 4447 WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
500d4160
PC
4448 .oVid = USB_VENDOR_ID_WACOM, .oPid = 0x32C };
4449static const struct wacom_features wacom_features_0x32C =
4450 { "Wacom Cintiq 27QHD touch", .type = WACOM_27QHDT,
4451 .oVid = USB_VENDOR_ID_WACOM, .oPid = 0x32B, .touch_max = 10 };
e87a344d 4452static const struct wacom_features wacom_features_0x3F =
80befa93 4453 { "Wacom Cintiq 21UX", 87200, 65600, 1023, 63,
70ee06c5 4454 CINTIQ, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 8 };
e87a344d 4455static const struct wacom_features wacom_features_0xC5 =
80befa93 4456 { "Wacom Cintiq 20WSX", 86680, 54180, 1023, 63,
70ee06c5 4457 WACOM_BEE, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 10 };
e87a344d 4458static const struct wacom_features wacom_features_0xC6 =
80befa93 4459 { "Wacom Cintiq 12WX", 53020, 33440, 1023, 63,
70ee06c5 4460 WACOM_BEE, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 10 };
56218563 4461static const struct wacom_features wacom_features_0x304 =
e779ef23 4462 { "Wacom Cintiq 13HD", 59552, 33848, 1023, 63,
70ee06c5 4463 WACOM_13HD, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 9,
e779ef23 4464 WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
fa770340 4465 WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET };
b4bf2120 4466static const struct wacom_features wacom_features_0x333 =
e779ef23 4467 { "Wacom Cintiq 13HD touch", 59552, 33848, 2047, 63,
70ee06c5 4468 WACOM_13HD, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 9,
b4bf2120 4469 WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
e779ef23 4470 WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
b4bf2120
PC
4471 .oVid = USB_VENDOR_ID_WACOM, .oPid = 0x335 };
4472static const struct wacom_features wacom_features_0x335 =
4473 { "Wacom Cintiq 13HD touch", .type = WACOM_24HDT, /* Touch */
4474 .oVid = USB_VENDOR_ID_WACOM, .oPid = 0x333, .touch_max = 10,
4475 .check_for_hid_type = true, .hid_type = HID_TYPE_USBNONE };
e87a344d 4476static const struct wacom_features wacom_features_0xC7 =
80befa93
BT
4477 { "Wacom DTU1931", 37832, 30305, 511, 0,
4478 PL, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
c8f2edc5 4479static const struct wacom_features wacom_features_0xCE =
80befa93
BT
4480 { "Wacom DTU2231", 47864, 27011, 511, 0,
4481 DTU, WACOM_INTUOS_RES, WACOM_INTUOS_RES,
29b47391 4482 .check_for_hid_type = true, .hid_type = HID_TYPE_USBMOUSE };
c8f2edc5 4483static const struct wacom_features wacom_features_0xF0 =
80befa93
BT
4484 { "Wacom DTU1631", 34623, 19553, 511, 0,
4485 DTU, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
497ab1f2 4486static const struct wacom_features wacom_features_0xFB =
e779ef23 4487 { "Wacom DTU1031", 22096, 13960, 511, 0,
70ee06c5 4488 DTUS, WACOM_INTUOS_RES, WACOM_INTUOS_RES, 4,
e779ef23 4489 WACOM_DTU_OFFSET, WACOM_DTU_OFFSET,
fa770340 4490 WACOM_DTU_OFFSET, WACOM_DTU_OFFSET };
fff00bf8 4491static const struct wacom_features wacom_features_0x32F =
e779ef23 4492 { "Wacom DTU1031X", 22672, 12928, 511, 0,
70ee06c5 4493 DTUSX, WACOM_INTUOS_RES, WACOM_INTUOS_RES, 0,
e779ef23 4494 WACOM_DTU_OFFSET, WACOM_DTU_OFFSET,
fff00bf8 4495 WACOM_DTU_OFFSET, WACOM_DTU_OFFSET };
007760cf 4496static const struct wacom_features wacom_features_0x336 =
e779ef23 4497 { "Wacom DTU1141", 23672, 13403, 1023, 0,
ff38e829 4498 DTUS, WACOM_INTUOS_RES, WACOM_INTUOS_RES, 4,
e779ef23 4499 WACOM_DTU_OFFSET, WACOM_DTU_OFFSET,
ff38e829 4500 WACOM_DTU_OFFSET, WACOM_DTU_OFFSET };
56218563 4501static const struct wacom_features wacom_features_0x57 =
e779ef23 4502 { "Wacom DTK2241", 95840, 54260, 2047, 63,
70ee06c5 4503 DTK, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 6,
e779ef23 4504 WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
fa770340 4505 WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET };
a112e9fd 4506static const struct wacom_features wacom_features_0x59 = /* Pen */
e779ef23 4507 { "Wacom DTH2242", 95840, 54260, 2047, 63,
70ee06c5 4508 DTK, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 6,
fa770340 4509 WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
e779ef23 4510 WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
a112e9fd
PC
4511 .oVid = USB_VENDOR_ID_WACOM, .oPid = 0x5D };
4512static const struct wacom_features wacom_features_0x5D = /* Touch */
4513 { "Wacom DTH2242", .type = WACOM_24HDT,
29b47391
BT
4514 .oVid = USB_VENDOR_ID_WACOM, .oPid = 0x59, .touch_max = 10,
4515 .check_for_hid_type = true, .hid_type = HID_TYPE_USBNONE };
3a4b4aaa 4516static const struct wacom_features wacom_features_0xCC =
e779ef23 4517 { "Wacom Cintiq 21UX2", 87200, 65600, 2047, 63,
70ee06c5 4518 WACOM_21UX2, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 18,
e779ef23 4519 WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
fa770340 4520 WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET };
d838c644 4521static const struct wacom_features wacom_features_0xFA =
e779ef23 4522 { "Wacom Cintiq 22HD", 95840, 54260, 2047, 63,
70ee06c5 4523 WACOM_22HD, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 18,
e779ef23 4524 WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
fa770340 4525 WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET };
56218563 4526static const struct wacom_features wacom_features_0x5B =
e779ef23 4527 { "Wacom Cintiq 22HDT", 95840, 54260, 2047, 63,
70ee06c5 4528 WACOM_22HD, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 18,
fa770340 4529 WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
e779ef23 4530 WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
3bd1f7e2 4531 .oVid = USB_VENDOR_ID_WACOM, .oPid = 0x5e };
56218563
PC
4532static const struct wacom_features wacom_features_0x5E =
4533 { "Wacom Cintiq 22HDT", .type = WACOM_24HDT,
29b47391
BT
4534 .oVid = USB_VENDOR_ID_WACOM, .oPid = 0x5b, .touch_max = 10,
4535 .check_for_hid_type = true, .hid_type = HID_TYPE_USBNONE };
e87a344d 4536static const struct wacom_features wacom_features_0x90 =
80befa93 4537 { "Wacom ISDv4 90", 26202, 16325, 255, 0,
29b9e148 4538 TABLETPC, WACOM_INTUOS_RES, WACOM_INTUOS_RES }; /* Pen-only */
e87a344d 4539static const struct wacom_features wacom_features_0x93 =
80befa93 4540 { "Wacom ISDv4 93", 26202, 16325, 255, 0,
29b9e148 4541 TABLETPC, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 1 };
11d0cf88 4542static const struct wacom_features wacom_features_0x97 =
80befa93 4543 { "Wacom ISDv4 97", 26202, 16325, 511, 0,
29b9e148 4544 TABLETPC, WACOM_INTUOS_RES, WACOM_INTUOS_RES }; /* Pen-only */
e87a344d 4545static const struct wacom_features wacom_features_0x9A =
80befa93 4546 { "Wacom ISDv4 9A", 26202, 16325, 255, 0,
29b9e148 4547 TABLETPC, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 1 };
e87a344d 4548static const struct wacom_features wacom_features_0x9F =
80befa93 4549 { "Wacom ISDv4 9F", 26202, 16325, 255, 0,
29b9e148 4550 TABLETPC, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 1 };
e87a344d 4551static const struct wacom_features wacom_features_0xE2 =
80befa93
BT
4552 { "Wacom ISDv4 E2", 26202, 16325, 255, 0,
4553 TABLETPC2FG, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 2 };
e87a344d 4554static const struct wacom_features wacom_features_0xE3 =
80befa93
BT
4555 { "Wacom ISDv4 E3", 26202, 16325, 255, 0,
4556 TABLETPC2FG, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 2 };
1963518b 4557static const struct wacom_features wacom_features_0xE5 =
80befa93
BT
4558 { "Wacom ISDv4 E5", 26202, 16325, 255, 0,
4559 MTSCREEN, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
26fcd2a7 4560static const struct wacom_features wacom_features_0xE6 =
80befa93
BT
4561 { "Wacom ISDv4 E6", 27760, 15694, 255, 0,
4562 TABLETPC2FG, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 2 };
0d0e3064 4563static const struct wacom_features wacom_features_0xEC =
80befa93 4564 { "Wacom ISDv4 EC", 25710, 14500, 255, 0,
29b9e148 4565 TABLETPC, WACOM_INTUOS_RES, WACOM_INTUOS_RES }; /* Pen-only */
ac173837 4566static const struct wacom_features wacom_features_0xED =
80befa93 4567 { "Wacom ISDv4 ED", 26202, 16325, 255, 0,
29b9e148 4568 TABLETPCE, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 1 };
ac173837 4569static const struct wacom_features wacom_features_0xEF =
80befa93 4570 { "Wacom ISDv4 EF", 26202, 16325, 255, 0,
29b9e148 4571 TABLETPC, WACOM_INTUOS_RES, WACOM_INTUOS_RES }; /* Pen-only */
6afdc289 4572static const struct wacom_features wacom_features_0x100 =
80befa93
BT
4573 { "Wacom ISDv4 100", 26202, 16325, 255, 0,
4574 MTTPC, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
6afdc289 4575static const struct wacom_features wacom_features_0x101 =
80befa93
BT
4576 { "Wacom ISDv4 101", 26202, 16325, 255, 0,
4577 MTTPC, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
58694837 4578static const struct wacom_features wacom_features_0x10D =
80befa93
BT
4579 { "Wacom ISDv4 10D", 26202, 16325, 255, 0,
4580 MTTPC, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
2d3163f1 4581static const struct wacom_features wacom_features_0x10E =
80befa93
BT
4582 { "Wacom ISDv4 10E", 27760, 15694, 255, 0,
4583 MTTPC, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
9b4f60e5 4584static const struct wacom_features wacom_features_0x10F =
80befa93
BT
4585 { "Wacom ISDv4 10F", 27760, 15694, 255, 0,
4586 MTTPC, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
61616ed0 4587static const struct wacom_features wacom_features_0x116 =
80befa93 4588 { "Wacom ISDv4 116", 26202, 16325, 255, 0,
29b9e148 4589 TABLETPCE, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 1 };
aeaf50d4
JG
4590static const struct wacom_features wacom_features_0x12C =
4591 { "Wacom ISDv4 12C", 27848, 15752, 2047, 0,
29b9e148 4592 TABLETPC, WACOM_INTUOS_RES, WACOM_INTUOS_RES }; /* Pen-only */
edbe265d 4593static const struct wacom_features wacom_features_0x4001 =
80befa93
BT
4594 { "Wacom ISDv4 4001", 26202, 16325, 255, 0,
4595 MTTPC, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
d51ddb2b 4596static const struct wacom_features wacom_features_0x4004 =
80befa93
BT
4597 { "Wacom ISDv4 4004", 11060, 6220, 255, 0,
4598 MTTPC_B, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
d51ddb2b 4599static const struct wacom_features wacom_features_0x5000 =
80befa93
BT
4600 { "Wacom ISDv4 5000", 27848, 15752, 1023, 0,
4601 MTTPC_B, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
d51ddb2b 4602static const struct wacom_features wacom_features_0x5002 =
80befa93
BT
4603 { "Wacom ISDv4 5002", 29576, 16724, 1023, 0,
4604 MTTPC_B, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
e87a344d 4605static const struct wacom_features wacom_features_0x47 =
80befa93
BT
4606 { "Wacom Intuos2 6x8", 20320, 16240, 1023, 31,
4607 INTUOS, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
d3825d51 4608static const struct wacom_features wacom_features_0x84 =
3b164a00 4609 { "Wacom Wireless Receiver", .type = WIRELESS, .touch_max = 16 };
7b824bbd 4610static const struct wacom_features wacom_features_0xD0 =
80befa93 4611 { "Wacom Bamboo 2FG", 14720, 9200, 1023, 31,
3b164a00 4612 BAMBOO_TOUCH, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 2 };
7b824bbd 4613static const struct wacom_features wacom_features_0xD1 =
80befa93
BT
4614 { "Wacom Bamboo 2FG 4x5", 14720, 9200, 1023, 31,
4615 BAMBOO_PT, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 2 };
7b824bbd 4616static const struct wacom_features wacom_features_0xD2 =
80befa93
BT
4617 { "Wacom Bamboo Craft", 14720, 9200, 1023, 31,
4618 BAMBOO_PT, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 2 };
7b824bbd 4619static const struct wacom_features wacom_features_0xD3 =
80befa93
BT
4620 { "Wacom Bamboo 2FG 6x8", 21648, 13700, 1023, 31,
4621 BAMBOO_PT, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 2 };
57a7872f 4622static const struct wacom_features wacom_features_0xD4 =
80befa93 4623 { "Wacom Bamboo Pen", 14720, 9200, 1023, 31,
3b164a00 4624 BAMBOO_PEN, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
18adad1c 4625static const struct wacom_features wacom_features_0xD5 =
80befa93 4626 { "Wacom Bamboo Pen 6x8", 21648, 13700, 1023, 31,
3b164a00 4627 BAMBOO_PEN, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
7b824bbd 4628static const struct wacom_features wacom_features_0xD6 =
80befa93
BT
4629 { "Wacom BambooPT 2FG 4x5", 14720, 9200, 1023, 31,
4630 BAMBOO_PT, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 2 };
7b824bbd 4631static const struct wacom_features wacom_features_0xD7 =
80befa93
BT
4632 { "Wacom BambooPT 2FG Small", 14720, 9200, 1023, 31,
4633 BAMBOO_PT, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 2 };
7b824bbd 4634static const struct wacom_features wacom_features_0xD8 =
80befa93
BT
4635 { "Wacom Bamboo Comic 2FG", 21648, 13700, 1023, 31,
4636 BAMBOO_PT, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 2 };
7b824bbd 4637static const struct wacom_features wacom_features_0xDA =
80befa93
BT
4638 { "Wacom Bamboo 2FG 4x5 SE", 14720, 9200, 1023, 31,
4639 BAMBOO_PT, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 2 };
f41a64ee 4640static const struct wacom_features wacom_features_0xDB =
80befa93
BT
4641 { "Wacom Bamboo 2FG 6x8 SE", 21648, 13700, 1023, 31,
4642 BAMBOO_PT, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 2 };
73149ab8 4643static const struct wacom_features wacom_features_0xDD =
80befa93
BT
4644 { "Wacom Bamboo Connect", 14720, 9200, 1023, 31,
4645 BAMBOO_PT, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
73149ab8 4646static const struct wacom_features wacom_features_0xDE =
80befa93
BT
4647 { "Wacom Bamboo 16FG 4x5", 14720, 9200, 1023, 31,
4648 BAMBOO_PT, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 16 };
73149ab8 4649static const struct wacom_features wacom_features_0xDF =
80befa93
BT
4650 { "Wacom Bamboo 16FG 6x8", 21648, 13700, 1023, 31,
4651 BAMBOO_PT, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 16 };
f41a64ee 4652static const struct wacom_features wacom_features_0x300 =
80befa93 4653 { "Wacom Bamboo One S", 14720, 9225, 1023, 31,
3b164a00 4654 BAMBOO_PEN, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
f41a64ee 4655static const struct wacom_features wacom_features_0x301 =
80befa93 4656 { "Wacom Bamboo One M", 21648, 13530, 1023, 31,
b79cbc55 4657 BAMBOO_PEN, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
b5fd2a3e 4658static const struct wacom_features wacom_features_0x302 =
80befa93
BT
4659 { "Wacom Intuos PT S", 15200, 9500, 1023, 31,
4660 INTUOSHT, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 16,
29b47391 4661 .check_for_hid_type = true, .hid_type = HID_TYPE_USBNONE };
b5fd2a3e 4662static const struct wacom_features wacom_features_0x303 =
80befa93
BT
4663 { "Wacom Intuos PT M", 21600, 13500, 1023, 31,
4664 INTUOSHT, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 16,
29b47391 4665 .check_for_hid_type = true, .hid_type = HID_TYPE_USBNONE };
b5fd2a3e 4666static const struct wacom_features wacom_features_0x30E =
80befa93
BT
4667 { "Wacom Intuos S", 15200, 9500, 1023, 31,
4668 INTUOSHT, WACOM_INTUOS_RES, WACOM_INTUOS_RES,
29b47391 4669 .check_for_hid_type = true, .hid_type = HID_TYPE_USBNONE };
7b4b3068 4670static const struct wacom_features wacom_features_0x6004 =
80befa93
BT
4671 { "ISD-V4", 12800, 8000, 255, 0,
4672 TABLETPC, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
a3e6f654 4673static const struct wacom_features wacom_features_0x307 =
e779ef23 4674 { "Wacom ISDv5 307", 59552, 33848, 2047, 63,
70ee06c5 4675 CINTIQ_HYBRID, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 9,
fa770340 4676 WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
e779ef23 4677 WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
36d3c510 4678 .oVid = USB_VENDOR_ID_WACOM, .oPid = 0x309 };
a3e6f654 4679static const struct wacom_features wacom_features_0x309 =
36d3c510 4680 { "Wacom ISDv5 309", .type = WACOM_24HDT, /* Touch */
29b47391
BT
4681 .oVid = USB_VENDOR_ID_WACOM, .oPid = 0x0307, .touch_max = 10,
4682 .check_for_hid_type = true, .hid_type = HID_TYPE_USBNONE };
89f2ab55 4683static const struct wacom_features wacom_features_0x30A =
e779ef23 4684 { "Wacom ISDv5 30A", 59552, 33848, 2047, 63,
70ee06c5 4685 CINTIQ_HYBRID, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 9,
fa770340 4686 WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
e779ef23 4687 WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
89f2ab55
BT
4688 .oVid = USB_VENDOR_ID_WACOM, .oPid = 0x30C };
4689static const struct wacom_features wacom_features_0x30C =
4690 { "Wacom ISDv5 30C", .type = WACOM_24HDT, /* Touch */
4691 .oVid = USB_VENDOR_ID_WACOM, .oPid = 0x30A, .touch_max = 10,
4692 .check_for_hid_type = true, .hid_type = HID_TYPE_USBNONE };
8c97a765
BT
4693static const struct wacom_features wacom_features_0x318 =
4694 { "Wacom USB Bamboo PAD", 4095, 4095, /* Touch */
4695 .type = BAMBOO_PAD, 35, 48, .touch_max = 4 };
4696static const struct wacom_features wacom_features_0x319 =
4697 { "Wacom Wireless Bamboo PAD", 4095, 4095, /* Touch */
4698 .type = BAMBOO_PAD, 35, 48, .touch_max = 4 };
f7acb55c
JG
4699static const struct wacom_features wacom_features_0x325 =
4700 { "Wacom ISDv5 325", 59552, 33848, 2047, 63,
4701 CINTIQ_COMPANION_2, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 11,
4702 WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
e779ef23 4703 WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
f7acb55c
JG
4704 .oVid = USB_VENDOR_ID_WACOM, .oPid = 0x326 };
4705static const struct wacom_features wacom_features_0x326 = /* Touch */
4706 { "Wacom ISDv5 326", .type = HID_GENERIC, .oVid = USB_VENDOR_ID_WACOM,
4707 .oPid = 0x325 };
fefb391f
PC
4708static const struct wacom_features wacom_features_0x323 =
4709 { "Wacom Intuos P M", 21600, 13500, 1023, 31,
4710 INTUOSHT, WACOM_INTUOS_RES, WACOM_INTUOS_RES,
4711 .check_for_hid_type = true, .hid_type = HID_TYPE_USBNONE };
72b236d6 4712static const struct wacom_features wacom_features_0x331 =
3b164a00
PC
4713 { "Wacom Express Key Remote", .type = REMOTE,
4714 .numbered_buttons = 18, .check_for_hid_type = true,
72b236d6 4715 .hid_type = HID_TYPE_USBNONE };
eda01dab
PC
4716static const struct wacom_features wacom_features_0x33B =
4717 { "Wacom Intuos S 2", 15200, 9500, 2047, 63,
4718 INTUOSHT2, WACOM_INTUOS_RES, WACOM_INTUOS_RES,
4719 .check_for_hid_type = true, .hid_type = HID_TYPE_USBNONE };
4720static const struct wacom_features wacom_features_0x33C =
4721 { "Wacom Intuos PT S 2", 15200, 9500, 2047, 63,
4722 INTUOSHT2, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 16,
4723 .check_for_hid_type = true, .hid_type = HID_TYPE_USBNONE };
4724static const struct wacom_features wacom_features_0x33D =
4725 { "Wacom Intuos P M 2", 21600, 13500, 2047, 63,
4726 INTUOSHT2, WACOM_INTUOS_RES, WACOM_INTUOS_RES,
4727 .check_for_hid_type = true, .hid_type = HID_TYPE_USBNONE };
4728static const struct wacom_features wacom_features_0x33E =
4729 { "Wacom Intuos PT M 2", 21600, 13500, 2047, 63,
4730 INTUOSHT2, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 16,
4731 .check_for_hid_type = true, .hid_type = HID_TYPE_USBNONE };
e1123fe9 4732static const struct wacom_features wacom_features_0x343 =
e779ef23 4733 { "Wacom DTK1651", 34816, 19759, 1023, 0,
e1123fe9 4734 DTUS, WACOM_INTUOS_RES, WACOM_INTUOS_RES, 4,
e779ef23 4735 WACOM_DTU_OFFSET, WACOM_DTU_OFFSET,
e1123fe9 4736 WACOM_DTU_OFFSET, WACOM_DTU_OFFSET };
4922cd26
JG
4737static const struct wacom_features wacom_features_0x360 =
4738 { "Wacom Intuos Pro M", 44800, 29600, 8191, 63,
49cc4c21 4739 INTUOSP2_BT, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 9, .touch_max = 10 };
4922cd26
JG
4740static const struct wacom_features wacom_features_0x361 =
4741 { "Wacom Intuos Pro L", 62200, 43200, 8191, 63,
49cc4c21 4742 INTUOSP2_BT, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 9, .touch_max = 10 };
87046b6c
AAS
4743static const struct wacom_features wacom_features_0x377 =
4744 { "Wacom Intuos BT S", 15200, 9500, 4095, 63,
4745 INTUOSHT3_BT, WACOM_INTUOS_RES, WACOM_INTUOS_RES, 4 };
4746static const struct wacom_features wacom_features_0x379 =
4747 { "Wacom Intuos BT M", 21600, 13500, 4095, 63,
4748 INTUOSHT3_BT, WACOM_INTUOS_RES, WACOM_INTUOS_RES, 4 };
c9472189
JG
4749static const struct wacom_features wacom_features_0x37A =
4750 { "Wacom One by Wacom S", 15200, 9500, 2047, 63,
4751 BAMBOO_PEN, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4752static const struct wacom_features wacom_features_0x37B =
4753 { "Wacom One by Wacom M", 21600, 13500, 2047, 63,
4754 BAMBOO_PEN, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
912c6aa6
AAS
4755static const struct wacom_features wacom_features_0x393 =
4756 { "Wacom Intuos Pro S", 31920, 19950, 8191, 63,
4757 INTUOSP2S_BT, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 7,
4758 .touch_max = 10 };
0c8fbaa5
JD
4759static const struct wacom_features wacom_features_0x3c6 =
4760 { "Wacom Intuos BT S", 15200, 9500, 4095, 63,
4761 INTUOSHT3_BT, WACOM_INTUOS_RES, WACOM_INTUOS_RES, 4 };
4762static const struct wacom_features wacom_features_0x3c8 =
4763 { "Wacom Intuos BT M", 21600, 13500, 4095, 63,
4764 INTUOSHT3_BT, WACOM_INTUOS_RES, WACOM_INTUOS_RES, 4 };
b036f6fb 4765
7704ac93 4766static const struct wacom_features wacom_features_HID_ANY_ID =
41372d5d 4767 { "Wacom HID", .type = HID_GENERIC, .oVid = HID_ANY_ID, .oPid = HID_ANY_ID };
7704ac93 4768
29b47391
BT
4769#define USB_DEVICE_WACOM(prod) \
4770 HID_DEVICE(BUS_USB, HID_GROUP_WACOM, USB_VENDOR_ID_WACOM, prod),\
4771 .driver_data = (kernel_ulong_t)&wacom_features_##prod
b036f6fb 4772
387142bb
BT
4773#define BT_DEVICE_WACOM(prod) \
4774 HID_DEVICE(BUS_BLUETOOTH, HID_GROUP_WACOM, USB_VENDOR_ID_WACOM, prod),\
4775 .driver_data = (kernel_ulong_t)&wacom_features_##prod
1483f551 4776
eef23a84
MW
4777#define I2C_DEVICE_WACOM(prod) \
4778 HID_DEVICE(BUS_I2C, HID_GROUP_WACOM, USB_VENDOR_ID_WACOM, prod),\
4779 .driver_data = (kernel_ulong_t)&wacom_features_##prod
4780
7b4b3068 4781#define USB_DEVICE_LENOVO(prod) \
29b47391
BT
4782 HID_USB_DEVICE(USB_VENDOR_ID_LENOVO, prod), \
4783 .driver_data = (kernel_ulong_t)&wacom_features_##prod
7b4b3068 4784
29b47391 4785const struct hid_device_id wacom_ids[] = {
b036f6fb 4786 { USB_DEVICE_WACOM(0x00) },
a3e6f654 4787 { USB_DEVICE_WACOM(0x03) },
b036f6fb
BB
4788 { USB_DEVICE_WACOM(0x10) },
4789 { USB_DEVICE_WACOM(0x11) },
4790 { USB_DEVICE_WACOM(0x12) },
4791 { USB_DEVICE_WACOM(0x13) },
4792 { USB_DEVICE_WACOM(0x14) },
4793 { USB_DEVICE_WACOM(0x15) },
4794 { USB_DEVICE_WACOM(0x16) },
4795 { USB_DEVICE_WACOM(0x17) },
4796 { USB_DEVICE_WACOM(0x18) },
4797 { USB_DEVICE_WACOM(0x19) },
b036f6fb
BB
4798 { USB_DEVICE_WACOM(0x20) },
4799 { USB_DEVICE_WACOM(0x21) },
4800 { USB_DEVICE_WACOM(0x22) },
4801 { USB_DEVICE_WACOM(0x23) },
4802 { USB_DEVICE_WACOM(0x24) },
a3e6f654
BT
4803 { USB_DEVICE_WACOM(0x26) },
4804 { USB_DEVICE_WACOM(0x27) },
4805 { USB_DEVICE_WACOM(0x28) },
4806 { USB_DEVICE_WACOM(0x29) },
4807 { USB_DEVICE_WACOM(0x2A) },
b036f6fb
BB
4808 { USB_DEVICE_WACOM(0x30) },
4809 { USB_DEVICE_WACOM(0x31) },
4810 { USB_DEVICE_WACOM(0x32) },
4811 { USB_DEVICE_WACOM(0x33) },
4812 { USB_DEVICE_WACOM(0x34) },
4813 { USB_DEVICE_WACOM(0x35) },
4814 { USB_DEVICE_WACOM(0x37) },
4815 { USB_DEVICE_WACOM(0x38) },
4816 { USB_DEVICE_WACOM(0x39) },
a3e6f654 4817 { USB_DEVICE_WACOM(0x3F) },
b036f6fb
BB
4818 { USB_DEVICE_WACOM(0x41) },
4819 { USB_DEVICE_WACOM(0x42) },
4820 { USB_DEVICE_WACOM(0x43) },
4821 { USB_DEVICE_WACOM(0x44) },
4822 { USB_DEVICE_WACOM(0x45) },
a3e6f654 4823 { USB_DEVICE_WACOM(0x47) },
56218563 4824 { USB_DEVICE_WACOM(0x57) },
a112e9fd 4825 { USB_DEVICE_WACOM(0x59) },
56218563 4826 { USB_DEVICE_WACOM(0x5B) },
a3e6f654 4827 { USB_DEVICE_WACOM(0x5D) },
29b47391 4828 { USB_DEVICE_WACOM(0x5E) },
a3e6f654
BT
4829 { USB_DEVICE_WACOM(0x60) },
4830 { USB_DEVICE_WACOM(0x61) },
4831 { USB_DEVICE_WACOM(0x62) },
4832 { USB_DEVICE_WACOM(0x63) },
4833 { USB_DEVICE_WACOM(0x64) },
4834 { USB_DEVICE_WACOM(0x65) },
4835 { USB_DEVICE_WACOM(0x69) },
4836 { USB_DEVICE_WACOM(0x6A) },
4837 { USB_DEVICE_WACOM(0x6B) },
387142bb 4838 { BT_DEVICE_WACOM(0x81) },
a3e6f654
BT
4839 { USB_DEVICE_WACOM(0x84) },
4840 { USB_DEVICE_WACOM(0x90) },
4841 { USB_DEVICE_WACOM(0x93) },
4842 { USB_DEVICE_WACOM(0x97) },
4843 { USB_DEVICE_WACOM(0x9A) },
4844 { USB_DEVICE_WACOM(0x9F) },
b036f6fb
BB
4845 { USB_DEVICE_WACOM(0xB0) },
4846 { USB_DEVICE_WACOM(0xB1) },
4847 { USB_DEVICE_WACOM(0xB2) },
4848 { USB_DEVICE_WACOM(0xB3) },
4849 { USB_DEVICE_WACOM(0xB4) },
4850 { USB_DEVICE_WACOM(0xB5) },
4851 { USB_DEVICE_WACOM(0xB7) },
4852 { USB_DEVICE_WACOM(0xB8) },
4853 { USB_DEVICE_WACOM(0xB9) },
4854 { USB_DEVICE_WACOM(0xBA) },
4855 { USB_DEVICE_WACOM(0xBB) },
3a4b4aaa 4856 { USB_DEVICE_WACOM(0xBC) },
81af7e61 4857 { BT_DEVICE_WACOM(0xBD) },
a3e6f654
BT
4858 { USB_DEVICE_WACOM(0xC0) },
4859 { USB_DEVICE_WACOM(0xC2) },
4860 { USB_DEVICE_WACOM(0xC4) },
b036f6fb
BB
4861 { USB_DEVICE_WACOM(0xC5) },
4862 { USB_DEVICE_WACOM(0xC6) },
4863 { USB_DEVICE_WACOM(0xC7) },
a3e6f654 4864 { USB_DEVICE_WACOM(0xCC) },
29b47391 4865 { USB_DEVICE_WACOM(0xCE) },
cb734c03 4866 { USB_DEVICE_WACOM(0xD0) },
2aaacb15
CB
4867 { USB_DEVICE_WACOM(0xD1) },
4868 { USB_DEVICE_WACOM(0xD2) },
4869 { USB_DEVICE_WACOM(0xD3) },
57a7872f 4870 { USB_DEVICE_WACOM(0xD4) },
18adad1c 4871 { USB_DEVICE_WACOM(0xD5) },
d38acb49
PC
4872 { USB_DEVICE_WACOM(0xD6) },
4873 { USB_DEVICE_WACOM(0xD7) },
a318e6b1
DF
4874 { USB_DEVICE_WACOM(0xD8) },
4875 { USB_DEVICE_WACOM(0xDA) },
47d09235 4876 { USB_DEVICE_WACOM(0xDB) },
73149ab8
CB
4877 { USB_DEVICE_WACOM(0xDD) },
4878 { USB_DEVICE_WACOM(0xDE) },
4879 { USB_DEVICE_WACOM(0xDF) },
b036f6fb
BB
4880 { USB_DEVICE_WACOM(0xE2) },
4881 { USB_DEVICE_WACOM(0xE3) },
1963518b 4882 { USB_DEVICE_WACOM(0xE5) },
26fcd2a7 4883 { USB_DEVICE_WACOM(0xE6) },
0d0e3064 4884 { USB_DEVICE_WACOM(0xEC) },
ac173837
PC
4885 { USB_DEVICE_WACOM(0xED) },
4886 { USB_DEVICE_WACOM(0xEF) },
a3e6f654
BT
4887 { USB_DEVICE_WACOM(0xF0) },
4888 { USB_DEVICE_WACOM(0xF4) },
4889 { USB_DEVICE_WACOM(0xF6) },
4890 { USB_DEVICE_WACOM(0xF8) },
4891 { USB_DEVICE_WACOM(0xFA) },
4892 { USB_DEVICE_WACOM(0xFB) },
6afdc289
PC
4893 { USB_DEVICE_WACOM(0x100) },
4894 { USB_DEVICE_WACOM(0x101) },
58694837 4895 { USB_DEVICE_WACOM(0x10D) },
2d3163f1 4896 { USB_DEVICE_WACOM(0x10E) },
9b4f60e5 4897 { USB_DEVICE_WACOM(0x10F) },
61616ed0 4898 { USB_DEVICE_WACOM(0x116) },
aeaf50d4 4899 { USB_DEVICE_WACOM(0x12C) },
f41a64ee
PC
4900 { USB_DEVICE_WACOM(0x300) },
4901 { USB_DEVICE_WACOM(0x301) },
29b47391
BT
4902 { USB_DEVICE_WACOM(0x302) },
4903 { USB_DEVICE_WACOM(0x303) },
56218563 4904 { USB_DEVICE_WACOM(0x304) },
a3e6f654
BT
4905 { USB_DEVICE_WACOM(0x307) },
4906 { USB_DEVICE_WACOM(0x309) },
89f2ab55
BT
4907 { USB_DEVICE_WACOM(0x30A) },
4908 { USB_DEVICE_WACOM(0x30C) },
a3e6f654 4909 { USB_DEVICE_WACOM(0x30E) },
29b47391
BT
4910 { USB_DEVICE_WACOM(0x314) },
4911 { USB_DEVICE_WACOM(0x315) },
4912 { USB_DEVICE_WACOM(0x317) },
8c97a765
BT
4913 { USB_DEVICE_WACOM(0x318) },
4914 { USB_DEVICE_WACOM(0x319) },
fefb391f 4915 { USB_DEVICE_WACOM(0x323) },
f7acb55c
JG
4916 { USB_DEVICE_WACOM(0x325) },
4917 { USB_DEVICE_WACOM(0x326) },
500d4160
PC
4918 { USB_DEVICE_WACOM(0x32A) },
4919 { USB_DEVICE_WACOM(0x32B) },
4920 { USB_DEVICE_WACOM(0x32C) },
fff00bf8 4921 { USB_DEVICE_WACOM(0x32F) },
72b236d6 4922 { USB_DEVICE_WACOM(0x331) },
b4bf2120
PC
4923 { USB_DEVICE_WACOM(0x333) },
4924 { USB_DEVICE_WACOM(0x335) },
007760cf 4925 { USB_DEVICE_WACOM(0x336) },
eda01dab
PC
4926 { USB_DEVICE_WACOM(0x33B) },
4927 { USB_DEVICE_WACOM(0x33C) },
4928 { USB_DEVICE_WACOM(0x33D) },
4929 { USB_DEVICE_WACOM(0x33E) },
e1123fe9 4930 { USB_DEVICE_WACOM(0x343) },
4922cd26
JG
4931 { BT_DEVICE_WACOM(0x360) },
4932 { BT_DEVICE_WACOM(0x361) },
87046b6c
AAS
4933 { BT_DEVICE_WACOM(0x377) },
4934 { BT_DEVICE_WACOM(0x379) },
c9472189
JG
4935 { USB_DEVICE_WACOM(0x37A) },
4936 { USB_DEVICE_WACOM(0x37B) },
912c6aa6 4937 { BT_DEVICE_WACOM(0x393) },
0c8fbaa5
JD
4938 { BT_DEVICE_WACOM(0x3c6) },
4939 { BT_DEVICE_WACOM(0x3c8) },
edbe265d 4940 { USB_DEVICE_WACOM(0x4001) },
d51ddb2b
JG
4941 { USB_DEVICE_WACOM(0x4004) },
4942 { USB_DEVICE_WACOM(0x5000) },
4943 { USB_DEVICE_WACOM(0x5002) },
00d6f227 4944 { USB_DEVICE_LENOVO(0x6004) },
7704ac93
BT
4945
4946 { USB_DEVICE_WACOM(HID_ANY_ID) },
eef23a84 4947 { I2C_DEVICE_WACOM(HID_ANY_ID) },
b9e06256 4948 { BT_DEVICE_WACOM(HID_ANY_ID) },
3bea733a
PC
4949 { }
4950};
29b47391 4951MODULE_DEVICE_TABLE(hid, wacom_ids);