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