• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 = from_timer(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 	unsigned char data[WACOM_PKGLEN_MAX];
1206 	int i = 1;
1207 	unsigned power_raw, battery_capacity, bat_charging, ps_connected;
1208 
1209 	memcpy(data, wacom->data, len);
1210 
1211 	switch (data[0]) {
1212 	case 0x04:
1213 		wacom_intuos_bt_process_data(wacom, data + i);
1214 		i += 10;
1215 		fallthrough;
1216 	case 0x03:
1217 		wacom_intuos_bt_process_data(wacom, data + i);
1218 		i += 10;
1219 		wacom_intuos_bt_process_data(wacom, data + i);
1220 		i += 10;
1221 		power_raw = data[i];
1222 		bat_charging = (power_raw & 0x08) ? 1 : 0;
1223 		ps_connected = (power_raw & 0x10) ? 1 : 0;
1224 		battery_capacity = batcap_i4[power_raw & 0x07];
1225 		wacom_notify_battery(wacom, WACOM_POWER_SUPPLY_STATUS_AUTO,
1226 				     battery_capacity, bat_charging,
1227 				     battery_capacity || bat_charging,
1228 				     ps_connected);
1229 		break;
1230 	default:
1231 		dev_dbg(wacom->pen_input->dev.parent,
1232 				"Unknown report: %d,%d size:%zu\n",
1233 				data[0], data[1], len);
1234 		return 0;
1235 	}
1236 	return 0;
1237 }
1238 
wacom_wac_finger_count_touches(struct wacom_wac * wacom)1239 static int wacom_wac_finger_count_touches(struct wacom_wac *wacom)
1240 {
1241 	struct input_dev *input = wacom->touch_input;
1242 	unsigned touch_max = wacom->features.touch_max;
1243 	int count = 0;
1244 	int i;
1245 
1246 	if (!touch_max)
1247 		return 0;
1248 
1249 	if (touch_max == 1)
1250 		return test_bit(BTN_TOUCH, input->key) &&
1251 			report_touch_events(wacom);
1252 
1253 	for (i = 0; i < input->mt->num_slots; i++) {
1254 		struct input_mt_slot *ps = &input->mt->slots[i];
1255 		int id = input_mt_get_value(ps, ABS_MT_TRACKING_ID);
1256 		if (id >= 0)
1257 			count++;
1258 	}
1259 
1260 	return count;
1261 }
1262 
wacom_intuos_pro2_bt_pen(struct wacom_wac * wacom)1263 static void wacom_intuos_pro2_bt_pen(struct wacom_wac *wacom)
1264 {
1265 	int pen_frame_len, pen_frames;
1266 
1267 	struct input_dev *pen_input = wacom->pen_input;
1268 	unsigned char *data = wacom->data;
1269 	int number_of_valid_frames = 0;
1270 	ktime_t time_interval = 15000000;
1271 	ktime_t time_packet_received = ktime_get();
1272 	int i;
1273 
1274 	if (wacom->features.type == INTUOSP2_BT ||
1275 	    wacom->features.type == INTUOSP2S_BT) {
1276 		wacom->serial[0] = get_unaligned_le64(&data[99]);
1277 		wacom->id[0]     = get_unaligned_le16(&data[107]);
1278 		pen_frame_len = 14;
1279 		pen_frames = 7;
1280 	} else {
1281 		wacom->serial[0] = get_unaligned_le64(&data[33]);
1282 		wacom->id[0]     = get_unaligned_le16(&data[41]);
1283 		pen_frame_len = 8;
1284 		pen_frames = 4;
1285 	}
1286 
1287 	if (wacom->serial[0] >> 52 == 1) {
1288 		/* Add back in missing bits of ID for non-USI pens */
1289 		wacom->id[0] |= (wacom->serial[0] >> 32) & 0xFFFFF;
1290 	}
1291 
1292 	/* number of valid frames */
1293 	for (i = 0; i < pen_frames; i++) {
1294 		unsigned char *frame = &data[i*pen_frame_len + 1];
1295 		bool valid = frame[0] & 0x80;
1296 
1297 		if (valid)
1298 			number_of_valid_frames++;
1299 	}
1300 
1301 	if (number_of_valid_frames) {
1302 		if (wacom->hid_data.time_delayed)
1303 			time_interval = ktime_get() - wacom->hid_data.time_delayed;
1304 		time_interval = div_u64(time_interval, number_of_valid_frames);
1305 		wacom->hid_data.time_delayed = time_packet_received;
1306 	}
1307 
1308 	for (i = 0; i < number_of_valid_frames; i++) {
1309 		unsigned char *frame = &data[i*pen_frame_len + 1];
1310 		bool valid = frame[0] & 0x80;
1311 		bool prox = frame[0] & 0x40;
1312 		bool range = frame[0] & 0x20;
1313 		bool invert = frame[0] & 0x10;
1314 		int frames_number_reversed = number_of_valid_frames - i - 1;
1315 		ktime_t event_timestamp = time_packet_received - frames_number_reversed * time_interval;
1316 
1317 		if (!valid)
1318 			continue;
1319 
1320 		if (!prox) {
1321 			wacom->shared->stylus_in_proximity = false;
1322 			wacom_exit_report(wacom);
1323 			input_sync(pen_input);
1324 
1325 			wacom->tool[0] = 0;
1326 			wacom->id[0] = 0;
1327 			wacom->serial[0] = 0;
1328 			wacom->hid_data.time_delayed = 0;
1329 			return;
1330 		}
1331 
1332 		if (range) {
1333 			if (!wacom->tool[0]) { /* first in range */
1334 				/* Going into range select tool */
1335 				if (invert)
1336 					wacom->tool[0] = BTN_TOOL_RUBBER;
1337 				else if (wacom->id[0])
1338 					wacom->tool[0] = wacom_intuos_get_tool_type(wacom->id[0]);
1339 				else
1340 					wacom->tool[0] = BTN_TOOL_PEN;
1341 			}
1342 
1343 			input_report_abs(pen_input, ABS_X, get_unaligned_le16(&frame[1]));
1344 			input_report_abs(pen_input, ABS_Y, get_unaligned_le16(&frame[3]));
1345 
1346 			if (wacom->features.type == INTUOSP2_BT ||
1347 			    wacom->features.type == INTUOSP2S_BT) {
1348 				/* Fix rotation alignment: userspace expects zero at left */
1349 				int16_t rotation =
1350 					(int16_t)get_unaligned_le16(&frame[9]);
1351 				rotation += 1800/4;
1352 
1353 				if (rotation > 899)
1354 					rotation -= 1800;
1355 
1356 				input_report_abs(pen_input, ABS_TILT_X,
1357 						 (signed char)frame[7]);
1358 				input_report_abs(pen_input, ABS_TILT_Y,
1359 						 (signed char)frame[8]);
1360 				input_report_abs(pen_input, ABS_Z, rotation);
1361 				input_report_abs(pen_input, ABS_WHEEL,
1362 						 get_unaligned_le16(&frame[11]));
1363 			}
1364 		}
1365 
1366 		if (wacom->tool[0]) {
1367 			input_report_abs(pen_input, ABS_PRESSURE, get_unaligned_le16(&frame[5]));
1368 			if (wacom->features.type == INTUOSP2_BT ||
1369 			    wacom->features.type == INTUOSP2S_BT) {
1370 				input_report_abs(pen_input, ABS_DISTANCE,
1371 						 range ? frame[13] : wacom->features.distance_max);
1372 			} else {
1373 				input_report_abs(pen_input, ABS_DISTANCE,
1374 						 range ? frame[7] : wacom->features.distance_max);
1375 			}
1376 
1377 			input_report_key(pen_input, BTN_TOUCH, frame[0] & 0x09);
1378 			input_report_key(pen_input, BTN_STYLUS, frame[0] & 0x02);
1379 			input_report_key(pen_input, BTN_STYLUS2, frame[0] & 0x04);
1380 
1381 			input_report_key(pen_input, wacom->tool[0], prox);
1382 			input_event(pen_input, EV_MSC, MSC_SERIAL, wacom->serial[0]);
1383 			input_report_abs(pen_input, ABS_MISC,
1384 					 wacom_intuos_id_mangle(wacom->id[0])); /* report tool id */
1385 		}
1386 
1387 		wacom->shared->stylus_in_proximity = prox;
1388 
1389 		/* add timestamp to unpack the frames */
1390 		input_set_timestamp(pen_input, event_timestamp);
1391 
1392 		input_sync(pen_input);
1393 	}
1394 }
1395 
wacom_intuos_pro2_bt_touch(struct wacom_wac * wacom)1396 static void wacom_intuos_pro2_bt_touch(struct wacom_wac *wacom)
1397 {
1398 	const int finger_touch_len = 8;
1399 	const int finger_frames = 4;
1400 	const int finger_frame_len = 43;
1401 
1402 	struct input_dev *touch_input = wacom->touch_input;
1403 	unsigned char *data = wacom->data;
1404 	int num_contacts_left = 5;
1405 	int i, j;
1406 
1407 	for (i = 0; i < finger_frames; i++) {
1408 		unsigned char *frame = &data[i*finger_frame_len + 109];
1409 		int current_num_contacts = frame[0] & 0x7F;
1410 		int contacts_to_send;
1411 
1412 		if (!(frame[0] & 0x80))
1413 			continue;
1414 
1415 		/*
1416 		 * First packet resets the counter since only the first
1417 		 * packet in series will have non-zero current_num_contacts.
1418 		 */
1419 		if (current_num_contacts)
1420 			wacom->num_contacts_left = current_num_contacts;
1421 
1422 		contacts_to_send = min(num_contacts_left, wacom->num_contacts_left);
1423 
1424 		for (j = 0; j < contacts_to_send; j++) {
1425 			unsigned char *touch = &frame[j*finger_touch_len + 1];
1426 			int slot = input_mt_get_slot_by_key(touch_input, touch[0]);
1427 			int x = get_unaligned_le16(&touch[2]);
1428 			int y = get_unaligned_le16(&touch[4]);
1429 			int w = touch[6] * input_abs_get_res(touch_input, ABS_MT_POSITION_X);
1430 			int h = touch[7] * input_abs_get_res(touch_input, ABS_MT_POSITION_Y);
1431 
1432 			if (slot < 0)
1433 				continue;
1434 
1435 			input_mt_slot(touch_input, slot);
1436 			input_mt_report_slot_state(touch_input, MT_TOOL_FINGER, touch[1] & 0x01);
1437 			input_report_abs(touch_input, ABS_MT_POSITION_X, x);
1438 			input_report_abs(touch_input, ABS_MT_POSITION_Y, y);
1439 			input_report_abs(touch_input, ABS_MT_TOUCH_MAJOR, max(w, h));
1440 			input_report_abs(touch_input, ABS_MT_TOUCH_MINOR, min(w, h));
1441 			input_report_abs(touch_input, ABS_MT_ORIENTATION, w > h);
1442 		}
1443 
1444 		input_mt_sync_frame(touch_input);
1445 
1446 		wacom->num_contacts_left -= contacts_to_send;
1447 		if (wacom->num_contacts_left <= 0) {
1448 			wacom->num_contacts_left = 0;
1449 			wacom->shared->touch_down = wacom_wac_finger_count_touches(wacom);
1450 			input_sync(touch_input);
1451 		}
1452 	}
1453 
1454 	if (wacom->num_contacts_left == 0) {
1455 		// Be careful that we don't accidentally call input_sync with
1456 		// only a partial set of fingers of processed
1457 		input_report_switch(touch_input, SW_MUTE_DEVICE, !(data[281] >> 7));
1458 		input_sync(touch_input);
1459 	}
1460 
1461 }
1462 
wacom_intuos_pro2_bt_pad(struct wacom_wac * wacom)1463 static void wacom_intuos_pro2_bt_pad(struct wacom_wac *wacom)
1464 {
1465 	struct input_dev *pad_input = wacom->pad_input;
1466 	unsigned char *data = wacom->data;
1467 	int nbuttons = wacom->features.numbered_buttons;
1468 
1469 	int expresskeys = data[282];
1470 	int center = (data[281] & 0x40) >> 6;
1471 	int ring = data[285] & 0x7F;
1472 	bool ringstatus = data[285] & 0x80;
1473 	bool prox = expresskeys || center || ringstatus;
1474 
1475 	/* Fix touchring data: userspace expects 0 at left and increasing clockwise */
1476 	ring = 71 - ring;
1477 	ring += 3*72/16;
1478 	if (ring > 71)
1479 		ring -= 72;
1480 
1481 	wacom_report_numbered_buttons(pad_input, nbuttons,
1482                                       expresskeys | (center << (nbuttons - 1)));
1483 
1484 	input_report_abs(pad_input, ABS_WHEEL, ringstatus ? ring : 0);
1485 
1486 	input_report_key(pad_input, wacom->tool[1], prox ? 1 : 0);
1487 	input_report_abs(pad_input, ABS_MISC, prox ? PAD_DEVICE_ID : 0);
1488 	input_event(pad_input, EV_MSC, MSC_SERIAL, 0xffffffff);
1489 
1490 	input_sync(pad_input);
1491 }
1492 
wacom_intuos_pro2_bt_battery(struct wacom_wac * wacom)1493 static void wacom_intuos_pro2_bt_battery(struct wacom_wac *wacom)
1494 {
1495 	unsigned char *data = wacom->data;
1496 
1497 	bool chg = data[284] & 0x80;
1498 	int battery_status = data[284] & 0x7F;
1499 
1500 	wacom_notify_battery(wacom, WACOM_POWER_SUPPLY_STATUS_AUTO,
1501 			     battery_status, chg, 1, chg);
1502 }
1503 
wacom_intuos_gen3_bt_pad(struct wacom_wac * wacom)1504 static void wacom_intuos_gen3_bt_pad(struct wacom_wac *wacom)
1505 {
1506 	struct input_dev *pad_input = wacom->pad_input;
1507 	unsigned char *data = wacom->data;
1508 
1509 	int buttons = data[44];
1510 
1511 	wacom_report_numbered_buttons(pad_input, 4, buttons);
1512 
1513 	input_report_key(pad_input, wacom->tool[1], buttons ? 1 : 0);
1514 	input_report_abs(pad_input, ABS_MISC, buttons ? PAD_DEVICE_ID : 0);
1515 	input_event(pad_input, EV_MSC, MSC_SERIAL, 0xffffffff);
1516 
1517 	input_sync(pad_input);
1518 }
1519 
wacom_intuos_gen3_bt_battery(struct wacom_wac * wacom)1520 static void wacom_intuos_gen3_bt_battery(struct wacom_wac *wacom)
1521 {
1522 	unsigned char *data = wacom->data;
1523 
1524 	bool chg = data[45] & 0x80;
1525 	int battery_status = data[45] & 0x7F;
1526 
1527 	wacom_notify_battery(wacom, WACOM_POWER_SUPPLY_STATUS_AUTO,
1528 			     battery_status, chg, 1, chg);
1529 }
1530 
wacom_intuos_pro2_bt_irq(struct wacom_wac * wacom,size_t len)1531 static int wacom_intuos_pro2_bt_irq(struct wacom_wac *wacom, size_t len)
1532 {
1533 	unsigned char *data = wacom->data;
1534 
1535 	if (data[0] != 0x80 && data[0] != 0x81) {
1536 		dev_dbg(wacom->pen_input->dev.parent,
1537 			"%s: received unknown report #%d\n", __func__, data[0]);
1538 		return 0;
1539 	}
1540 
1541 	wacom_intuos_pro2_bt_pen(wacom);
1542 	if (wacom->features.type == INTUOSP2_BT ||
1543 	    wacom->features.type == INTUOSP2S_BT) {
1544 		wacom_intuos_pro2_bt_touch(wacom);
1545 		wacom_intuos_pro2_bt_pad(wacom);
1546 		wacom_intuos_pro2_bt_battery(wacom);
1547 	} else {
1548 		wacom_intuos_gen3_bt_pad(wacom);
1549 		wacom_intuos_gen3_bt_battery(wacom);
1550 	}
1551 	return 0;
1552 }
1553 
wacom_24hdt_irq(struct wacom_wac * wacom)1554 static int wacom_24hdt_irq(struct wacom_wac *wacom)
1555 {
1556 	struct input_dev *input = wacom->touch_input;
1557 	unsigned char *data = wacom->data;
1558 	int i;
1559 	int current_num_contacts = data[61];
1560 	int contacts_to_send = 0;
1561 	int num_contacts_left = 4; /* maximum contacts per packet */
1562 	int byte_per_packet = WACOM_BYTES_PER_24HDT_PACKET;
1563 	int y_offset = 2;
1564 
1565 	if (touch_is_muted(wacom) && !wacom->shared->touch_down)
1566 		return 0;
1567 
1568 	if (wacom->features.type == WACOM_27QHDT) {
1569 		current_num_contacts = data[63];
1570 		num_contacts_left = 10;
1571 		byte_per_packet = WACOM_BYTES_PER_QHDTHID_PACKET;
1572 		y_offset = 0;
1573 	}
1574 
1575 	/*
1576 	 * First packet resets the counter since only the first
1577 	 * packet in series will have non-zero current_num_contacts.
1578 	 */
1579 	if (current_num_contacts)
1580 		wacom->num_contacts_left = current_num_contacts;
1581 
1582 	contacts_to_send = min(num_contacts_left, wacom->num_contacts_left);
1583 
1584 	for (i = 0; i < contacts_to_send; i++) {
1585 		int offset = (byte_per_packet * i) + 1;
1586 		bool touch = (data[offset] & 0x1) && report_touch_events(wacom);
1587 		int slot = input_mt_get_slot_by_key(input, data[offset + 1]);
1588 
1589 		if (slot < 0)
1590 			continue;
1591 		input_mt_slot(input, slot);
1592 		input_mt_report_slot_state(input, MT_TOOL_FINGER, touch);
1593 
1594 		if (touch) {
1595 			int t_x = get_unaligned_le16(&data[offset + 2]);
1596 			int t_y = get_unaligned_le16(&data[offset + 4 + y_offset]);
1597 
1598 			input_report_abs(input, ABS_MT_POSITION_X, t_x);
1599 			input_report_abs(input, ABS_MT_POSITION_Y, t_y);
1600 
1601 			if (wacom->features.type != WACOM_27QHDT) {
1602 				int c_x = get_unaligned_le16(&data[offset + 4]);
1603 				int c_y = get_unaligned_le16(&data[offset + 8]);
1604 				int w = get_unaligned_le16(&data[offset + 10]);
1605 				int h = get_unaligned_le16(&data[offset + 12]);
1606 
1607 				input_report_abs(input, ABS_MT_TOUCH_MAJOR, min(w,h));
1608 				input_report_abs(input, ABS_MT_WIDTH_MAJOR,
1609 						 min(w, h) + int_dist(t_x, t_y, c_x, c_y));
1610 				input_report_abs(input, ABS_MT_WIDTH_MINOR, min(w, h));
1611 				input_report_abs(input, ABS_MT_ORIENTATION, w > h);
1612 			}
1613 		}
1614 	}
1615 	input_mt_sync_frame(input);
1616 
1617 	wacom->num_contacts_left -= contacts_to_send;
1618 	if (wacom->num_contacts_left <= 0) {
1619 		wacom->num_contacts_left = 0;
1620 		wacom->shared->touch_down = wacom_wac_finger_count_touches(wacom);
1621 	}
1622 	return 1;
1623 }
1624 
wacom_mt_touch(struct wacom_wac * wacom)1625 static int wacom_mt_touch(struct wacom_wac *wacom)
1626 {
1627 	struct input_dev *input = wacom->touch_input;
1628 	unsigned char *data = wacom->data;
1629 	int i;
1630 	int current_num_contacts = data[2];
1631 	int contacts_to_send = 0;
1632 	int x_offset = 0;
1633 
1634 	/* MTTPC does not support Height and Width */
1635 	if (wacom->features.type == MTTPC || wacom->features.type == MTTPC_B)
1636 		x_offset = -4;
1637 
1638 	/*
1639 	 * First packet resets the counter since only the first
1640 	 * packet in series will have non-zero current_num_contacts.
1641 	 */
1642 	if (current_num_contacts)
1643 		wacom->num_contacts_left = current_num_contacts;
1644 
1645 	/* There are at most 5 contacts per packet */
1646 	contacts_to_send = min(5, wacom->num_contacts_left);
1647 
1648 	for (i = 0; i < contacts_to_send; i++) {
1649 		int offset = (WACOM_BYTES_PER_MT_PACKET + x_offset) * i + 3;
1650 		bool touch = (data[offset] & 0x1) && report_touch_events(wacom);
1651 		int id = get_unaligned_le16(&data[offset + 1]);
1652 		int slot = input_mt_get_slot_by_key(input, id);
1653 
1654 		if (slot < 0)
1655 			continue;
1656 
1657 		input_mt_slot(input, slot);
1658 		input_mt_report_slot_state(input, MT_TOOL_FINGER, touch);
1659 		if (touch) {
1660 			int x = get_unaligned_le16(&data[offset + x_offset + 7]);
1661 			int y = get_unaligned_le16(&data[offset + x_offset + 9]);
1662 			input_report_abs(input, ABS_MT_POSITION_X, x);
1663 			input_report_abs(input, ABS_MT_POSITION_Y, y);
1664 		}
1665 	}
1666 	input_mt_sync_frame(input);
1667 
1668 	wacom->num_contacts_left -= contacts_to_send;
1669 	if (wacom->num_contacts_left <= 0) {
1670 		wacom->num_contacts_left = 0;
1671 		wacom->shared->touch_down = wacom_wac_finger_count_touches(wacom);
1672 	}
1673 	return 1;
1674 }
1675 
wacom_tpc_mt_touch(struct wacom_wac * wacom)1676 static int wacom_tpc_mt_touch(struct wacom_wac *wacom)
1677 {
1678 	struct input_dev *input = wacom->touch_input;
1679 	unsigned char *data = wacom->data;
1680 	int i;
1681 
1682 	for (i = 0; i < 2; i++) {
1683 		int p = data[1] & (1 << i);
1684 		bool touch = p && report_touch_events(wacom);
1685 
1686 		input_mt_slot(input, i);
1687 		input_mt_report_slot_state(input, MT_TOOL_FINGER, touch);
1688 		if (touch) {
1689 			int x = le16_to_cpup((__le16 *)&data[i * 2 + 2]) & 0x7fff;
1690 			int y = le16_to_cpup((__le16 *)&data[i * 2 + 6]) & 0x7fff;
1691 
1692 			input_report_abs(input, ABS_MT_POSITION_X, x);
1693 			input_report_abs(input, ABS_MT_POSITION_Y, y);
1694 		}
1695 	}
1696 	input_mt_sync_frame(input);
1697 
1698 	/* keep touch state for pen event */
1699 	wacom->shared->touch_down = wacom_wac_finger_count_touches(wacom);
1700 
1701 	return 1;
1702 }
1703 
wacom_tpc_single_touch(struct wacom_wac * wacom,size_t len)1704 static int wacom_tpc_single_touch(struct wacom_wac *wacom, size_t len)
1705 {
1706 	unsigned char *data = wacom->data;
1707 	struct input_dev *input = wacom->touch_input;
1708 	bool prox = report_touch_events(wacom);
1709 	int x = 0, y = 0;
1710 
1711 	if (wacom->features.touch_max > 1 || len > WACOM_PKGLEN_TPC2FG)
1712 		return 0;
1713 
1714 	if (len == WACOM_PKGLEN_TPC1FG) {
1715 		prox = prox && (data[0] & 0x01);
1716 		x = get_unaligned_le16(&data[1]);
1717 		y = get_unaligned_le16(&data[3]);
1718 	} else if (len == WACOM_PKGLEN_TPC1FG_B) {
1719 		prox = prox && (data[2] & 0x01);
1720 		x = get_unaligned_le16(&data[3]);
1721 		y = get_unaligned_le16(&data[5]);
1722 	} else {
1723 		prox = prox && (data[1] & 0x01);
1724 		x = le16_to_cpup((__le16 *)&data[2]);
1725 		y = le16_to_cpup((__le16 *)&data[4]);
1726 	}
1727 
1728 	if (prox) {
1729 		input_report_abs(input, ABS_X, x);
1730 		input_report_abs(input, ABS_Y, y);
1731 	}
1732 	input_report_key(input, BTN_TOUCH, prox);
1733 
1734 	/* keep touch state for pen events */
1735 	wacom->shared->touch_down = prox;
1736 
1737 	return 1;
1738 }
1739 
wacom_tpc_pen(struct wacom_wac * wacom)1740 static int wacom_tpc_pen(struct wacom_wac *wacom)
1741 {
1742 	unsigned char *data = wacom->data;
1743 	struct input_dev *input = wacom->pen_input;
1744 	bool prox = data[1] & 0x20;
1745 
1746 	if (!wacom->shared->stylus_in_proximity) /* first in prox */
1747 		/* Going into proximity select tool */
1748 		wacom->tool[0] = (data[1] & 0x0c) ? BTN_TOOL_RUBBER : BTN_TOOL_PEN;
1749 
1750 	/* keep pen state for touch events */
1751 	wacom->shared->stylus_in_proximity = prox;
1752 
1753 	/* send pen events only when touch is up or forced out
1754 	 * or touch arbitration is off
1755 	 */
1756 	if (!delay_pen_events(wacom)) {
1757 		input_report_key(input, BTN_STYLUS, data[1] & 0x02);
1758 		input_report_key(input, BTN_STYLUS2, data[1] & 0x10);
1759 		input_report_abs(input, ABS_X, le16_to_cpup((__le16 *)&data[2]));
1760 		input_report_abs(input, ABS_Y, le16_to_cpup((__le16 *)&data[4]));
1761 		input_report_abs(input, ABS_PRESSURE, ((data[7] & 0x07) << 8) | data[6]);
1762 		input_report_key(input, BTN_TOUCH, data[1] & 0x05);
1763 		input_report_key(input, wacom->tool[0], prox);
1764 		return 1;
1765 	}
1766 
1767 	return 0;
1768 }
1769 
wacom_tpc_irq(struct wacom_wac * wacom,size_t len)1770 static int wacom_tpc_irq(struct wacom_wac *wacom, size_t len)
1771 {
1772 	unsigned char *data = wacom->data;
1773 
1774 	if (wacom->pen_input) {
1775 		dev_dbg(wacom->pen_input->dev.parent,
1776 			"%s: received report #%d\n", __func__, data[0]);
1777 
1778 		if (len == WACOM_PKGLEN_PENABLED ||
1779 		    data[0] == WACOM_REPORT_PENABLED)
1780 			return wacom_tpc_pen(wacom);
1781 	}
1782 	else if (wacom->touch_input) {
1783 		dev_dbg(wacom->touch_input->dev.parent,
1784 			"%s: received report #%d\n", __func__, data[0]);
1785 
1786 		switch (len) {
1787 		case WACOM_PKGLEN_TPC1FG:
1788 			return wacom_tpc_single_touch(wacom, len);
1789 
1790 		case WACOM_PKGLEN_TPC2FG:
1791 			return wacom_tpc_mt_touch(wacom);
1792 
1793 		default:
1794 			switch (data[0]) {
1795 			case WACOM_REPORT_TPC1FG:
1796 			case WACOM_REPORT_TPCHID:
1797 			case WACOM_REPORT_TPCST:
1798 			case WACOM_REPORT_TPC1FGE:
1799 				return wacom_tpc_single_touch(wacom, len);
1800 
1801 			case WACOM_REPORT_TPCMT:
1802 			case WACOM_REPORT_TPCMT2:
1803 				return wacom_mt_touch(wacom);
1804 
1805 			}
1806 		}
1807 	}
1808 
1809 	return 0;
1810 }
1811 
wacom_offset_rotation(struct input_dev * input,struct hid_usage * usage,int value,int num,int denom)1812 static int wacom_offset_rotation(struct input_dev *input, struct hid_usage *usage,
1813 				 int value, int num, int denom)
1814 {
1815 	struct input_absinfo *abs = &input->absinfo[usage->code];
1816 	int range = (abs->maximum - abs->minimum + 1);
1817 
1818 	value += num*range/denom;
1819 	if (value > abs->maximum)
1820 		value -= range;
1821 	else if (value < abs->minimum)
1822 		value += range;
1823 	return value;
1824 }
1825 
wacom_equivalent_usage(int usage)1826 int wacom_equivalent_usage(int usage)
1827 {
1828 	if ((usage & HID_USAGE_PAGE) == WACOM_HID_UP_WACOMDIGITIZER) {
1829 		int subpage = (usage & 0xFF00) << 8;
1830 		int subusage = (usage & 0xFF);
1831 
1832 		if (subpage == WACOM_HID_SP_PAD ||
1833 		    subpage == WACOM_HID_SP_BUTTON ||
1834 		    subpage == WACOM_HID_SP_DIGITIZER ||
1835 		    subpage == WACOM_HID_SP_DIGITIZERINFO ||
1836 		    usage == WACOM_HID_WD_SENSE ||
1837 		    usage == WACOM_HID_WD_SERIALHI ||
1838 		    usage == WACOM_HID_WD_TOOLTYPE ||
1839 		    usage == WACOM_HID_WD_DISTANCE ||
1840 		    usage == WACOM_HID_WD_TOUCHSTRIP ||
1841 		    usage == WACOM_HID_WD_TOUCHSTRIP2 ||
1842 		    usage == WACOM_HID_WD_TOUCHRING ||
1843 		    usage == WACOM_HID_WD_TOUCHRINGSTATUS ||
1844 		    usage == WACOM_HID_WD_REPORT_VALID ||
1845 		    usage == WACOM_HID_WD_BARRELSWITCH3 ||
1846 		    usage == WACOM_HID_WD_SEQUENCENUMBER) {
1847 			return usage;
1848 		}
1849 
1850 		if (subpage == HID_UP_UNDEFINED)
1851 			subpage = HID_UP_DIGITIZER;
1852 
1853 		return subpage | subusage;
1854 	}
1855 
1856 	if ((usage & HID_USAGE_PAGE) == WACOM_HID_UP_WACOMTOUCH) {
1857 		int subpage = (usage & 0xFF00) << 8;
1858 		int subusage = (usage & 0xFF);
1859 
1860 		if (usage == WACOM_HID_WT_REPORT_VALID)
1861 			return usage;
1862 
1863 		if (subpage == HID_UP_UNDEFINED)
1864 			subpage = WACOM_HID_SP_DIGITIZER;
1865 
1866 		return subpage | subusage;
1867 	}
1868 
1869 	return usage;
1870 }
1871 
wacom_map_usage(struct input_dev * input,struct hid_usage * usage,struct hid_field * field,__u8 type,__u16 code,int fuzz)1872 static void wacom_map_usage(struct input_dev *input, struct hid_usage *usage,
1873 		struct hid_field *field, __u8 type, __u16 code, int fuzz)
1874 {
1875 	struct wacom *wacom = input_get_drvdata(input);
1876 	struct wacom_wac *wacom_wac = &wacom->wacom_wac;
1877 	struct wacom_features *features = &wacom_wac->features;
1878 	int fmin = field->logical_minimum;
1879 	int fmax = field->logical_maximum;
1880 	unsigned int equivalent_usage = wacom_equivalent_usage(usage->hid);
1881 	int resolution_code = code;
1882 	int resolution;
1883 
1884 	if (equivalent_usage == HID_DG_TWIST) {
1885 		resolution_code = ABS_RZ;
1886 	}
1887 
1888 	resolution = hidinput_calc_abs_res(field, resolution_code);
1889 
1890 	if (equivalent_usage == HID_GD_X) {
1891 		fmin += features->offset_left;
1892 		fmax -= features->offset_right;
1893 	}
1894 	if (equivalent_usage == HID_GD_Y) {
1895 		fmin += features->offset_top;
1896 		fmax -= features->offset_bottom;
1897 	}
1898 
1899 	usage->type = type;
1900 	usage->code = code;
1901 
1902 	switch (type) {
1903 	case EV_ABS:
1904 		input_set_abs_params(input, code, fmin, fmax, fuzz, 0);
1905 
1906 		/* older tablet may miss physical usage */
1907 		if ((code == ABS_X || code == ABS_Y) && !resolution) {
1908 			resolution = WACOM_INTUOS_RES;
1909 			hid_warn(input,
1910 				 "Using default resolution for axis type 0x%x code 0x%x\n",
1911 				 type, code);
1912 		}
1913 		input_abs_set_res(input, code, resolution);
1914 		break;
1915 	case EV_REL:
1916 	case EV_KEY:
1917 	case EV_MSC:
1918 	case EV_SW:
1919 		input_set_capability(input, type, code);
1920 		break;
1921 	}
1922 }
1923 
wacom_wac_battery_usage_mapping(struct hid_device * hdev,struct hid_field * field,struct hid_usage * usage)1924 static void wacom_wac_battery_usage_mapping(struct hid_device *hdev,
1925 		struct hid_field *field, struct hid_usage *usage)
1926 {
1927 	return;
1928 }
1929 
wacom_wac_battery_event(struct hid_device * hdev,struct hid_field * field,struct hid_usage * usage,__s32 value)1930 static void wacom_wac_battery_event(struct hid_device *hdev, struct hid_field *field,
1931 		struct hid_usage *usage, __s32 value)
1932 {
1933 	struct wacom *wacom = hid_get_drvdata(hdev);
1934 	struct wacom_wac *wacom_wac = &wacom->wacom_wac;
1935 	unsigned equivalent_usage = wacom_equivalent_usage(usage->hid);
1936 
1937 	switch (equivalent_usage) {
1938 	case HID_DG_BATTERYSTRENGTH:
1939 		if (value == 0) {
1940 			wacom_wac->hid_data.bat_status = POWER_SUPPLY_STATUS_UNKNOWN;
1941 		}
1942 		else {
1943 			value = value * 100 / (field->logical_maximum - field->logical_minimum);
1944 			wacom_wac->hid_data.battery_capacity = value;
1945 			wacom_wac->hid_data.bat_connected = 1;
1946 			wacom_wac->hid_data.bat_status = WACOM_POWER_SUPPLY_STATUS_AUTO;
1947 		}
1948 		wacom_wac->features.quirks |= WACOM_QUIRK_BATTERY;
1949 		break;
1950 	case WACOM_HID_WD_BATTERY_LEVEL:
1951 		value = value * 100 / (field->logical_maximum - field->logical_minimum);
1952 		wacom_wac->hid_data.battery_capacity = value;
1953 		wacom_wac->hid_data.bat_connected = 1;
1954 		wacom_wac->hid_data.bat_status = WACOM_POWER_SUPPLY_STATUS_AUTO;
1955 		wacom_wac->features.quirks |= WACOM_QUIRK_BATTERY;
1956 		break;
1957 	case WACOM_HID_WD_BATTERY_CHARGING:
1958 		wacom_wac->hid_data.bat_charging = value;
1959 		wacom_wac->hid_data.ps_connected = value;
1960 		wacom_wac->hid_data.bat_connected = 1;
1961 		wacom_wac->hid_data.bat_status = WACOM_POWER_SUPPLY_STATUS_AUTO;
1962 		wacom_wac->features.quirks |= WACOM_QUIRK_BATTERY;
1963 		break;
1964 	}
1965 }
1966 
wacom_wac_battery_pre_report(struct hid_device * hdev,struct hid_report * report)1967 static void wacom_wac_battery_pre_report(struct hid_device *hdev,
1968 		struct hid_report *report)
1969 {
1970 	return;
1971 }
1972 
wacom_wac_battery_report(struct hid_device * hdev,struct hid_report * report)1973 static void wacom_wac_battery_report(struct hid_device *hdev,
1974 		struct hid_report *report)
1975 {
1976 	struct wacom *wacom = hid_get_drvdata(hdev);
1977 	struct wacom_wac *wacom_wac = &wacom->wacom_wac;
1978 
1979 	int status = wacom_wac->hid_data.bat_status;
1980 	int capacity = wacom_wac->hid_data.battery_capacity;
1981 	bool charging = wacom_wac->hid_data.bat_charging;
1982 	bool connected = wacom_wac->hid_data.bat_connected;
1983 	bool powered = wacom_wac->hid_data.ps_connected;
1984 
1985 	wacom_notify_battery(wacom_wac, status, capacity, charging,
1986 			     connected, powered);
1987 }
1988 
wacom_wac_pad_usage_mapping(struct hid_device * hdev,struct hid_field * field,struct hid_usage * usage)1989 static void wacom_wac_pad_usage_mapping(struct hid_device *hdev,
1990 		struct hid_field *field, struct hid_usage *usage)
1991 {
1992 	struct wacom *wacom = hid_get_drvdata(hdev);
1993 	struct wacom_wac *wacom_wac = &wacom->wacom_wac;
1994 	struct wacom_features *features = &wacom_wac->features;
1995 	struct input_dev *input = wacom_wac->pad_input;
1996 	unsigned equivalent_usage = wacom_equivalent_usage(usage->hid);
1997 
1998 	switch (equivalent_usage) {
1999 	case WACOM_HID_WD_ACCELEROMETER_X:
2000 		__set_bit(INPUT_PROP_ACCELEROMETER, input->propbit);
2001 		wacom_map_usage(input, usage, field, EV_ABS, ABS_X, 0);
2002 		features->device_type |= WACOM_DEVICETYPE_PAD;
2003 		break;
2004 	case WACOM_HID_WD_ACCELEROMETER_Y:
2005 		__set_bit(INPUT_PROP_ACCELEROMETER, input->propbit);
2006 		wacom_map_usage(input, usage, field, EV_ABS, ABS_Y, 0);
2007 		features->device_type |= WACOM_DEVICETYPE_PAD;
2008 		break;
2009 	case WACOM_HID_WD_ACCELEROMETER_Z:
2010 		__set_bit(INPUT_PROP_ACCELEROMETER, input->propbit);
2011 		wacom_map_usage(input, usage, field, EV_ABS, ABS_Z, 0);
2012 		features->device_type |= WACOM_DEVICETYPE_PAD;
2013 		break;
2014 	case WACOM_HID_WD_BUTTONCENTER:
2015 	case WACOM_HID_WD_BUTTONHOME:
2016 	case WACOM_HID_WD_BUTTONUP:
2017 	case WACOM_HID_WD_BUTTONDOWN:
2018 	case WACOM_HID_WD_BUTTONLEFT:
2019 	case WACOM_HID_WD_BUTTONRIGHT:
2020 		wacom_map_usage(input, usage, field, EV_KEY,
2021 				wacom_numbered_button_to_key(features->numbered_buttons),
2022 				0);
2023 		features->numbered_buttons++;
2024 		features->device_type |= WACOM_DEVICETYPE_PAD;
2025 		break;
2026 	case WACOM_HID_WD_MUTE_DEVICE:
2027 		/* softkey touch switch */
2028 		wacom_wac->is_soft_touch_switch = true;
2029 		fallthrough;
2030 	case WACOM_HID_WD_TOUCHONOFF:
2031 		/*
2032 		 * These two usages, which are used to mute touch events, come
2033 		 * from the pad packet, but are reported on the touch
2034 		 * interface. Because the touch interface may not have
2035 		 * been created yet, we cannot call wacom_map_usage(). In
2036 		 * order to process the usages when we receive them, we set
2037 		 * the usage type and code directly.
2038 		 */
2039 		wacom_wac->has_mute_touch_switch = true;
2040 		usage->type = EV_SW;
2041 		usage->code = SW_MUTE_DEVICE;
2042 		break;
2043 	case WACOM_HID_WD_TOUCHSTRIP:
2044 		wacom_map_usage(input, usage, field, EV_ABS, ABS_RX, 0);
2045 		features->device_type |= WACOM_DEVICETYPE_PAD;
2046 		break;
2047 	case WACOM_HID_WD_TOUCHSTRIP2:
2048 		wacom_map_usage(input, usage, field, EV_ABS, ABS_RY, 0);
2049 		features->device_type |= WACOM_DEVICETYPE_PAD;
2050 		break;
2051 	case WACOM_HID_WD_TOUCHRING:
2052 		if (field->flags & HID_MAIN_ITEM_RELATIVE) {
2053 			wacom_wac->relring_count++;
2054 			if (wacom_wac->relring_count == 1) {
2055 				wacom_map_usage(input, usage, field, EV_REL, REL_WHEEL_HI_RES, 0);
2056 				set_bit(REL_WHEEL, input->relbit);
2057 			}
2058 			else if (wacom_wac->relring_count == 2) {
2059 				wacom_map_usage(input, usage, field, EV_REL, REL_HWHEEL_HI_RES, 0);
2060 				set_bit(REL_HWHEEL, input->relbit);
2061 			}
2062 		} else {
2063 			wacom_wac->absring_count++;
2064 			if (wacom_wac->absring_count == 1)
2065 				wacom_map_usage(input, usage, field, EV_ABS, ABS_WHEEL, 0);
2066 			else if (wacom_wac->absring_count == 2)
2067 				wacom_map_usage(input, usage, field, EV_ABS, ABS_THROTTLE, 0);
2068 		}
2069 		features->device_type |= WACOM_DEVICETYPE_PAD;
2070 		break;
2071 	case WACOM_HID_WD_TOUCHRINGSTATUS:
2072 		/*
2073 		 * Only set up type/code association. Completely mapping
2074 		 * this usage may overwrite the axis resolution and range.
2075 		 */
2076 		usage->type = EV_ABS;
2077 		usage->code = ABS_WHEEL;
2078 		set_bit(EV_ABS, input->evbit);
2079 		features->device_type |= WACOM_DEVICETYPE_PAD;
2080 		break;
2081 	case WACOM_HID_WD_BUTTONCONFIG:
2082 		wacom_map_usage(input, usage, field, EV_KEY, KEY_BUTTONCONFIG, 0);
2083 		features->device_type |= WACOM_DEVICETYPE_PAD;
2084 		break;
2085 	case WACOM_HID_WD_ONSCREEN_KEYBOARD:
2086 		wacom_map_usage(input, usage, field, EV_KEY, KEY_ONSCREEN_KEYBOARD, 0);
2087 		features->device_type |= WACOM_DEVICETYPE_PAD;
2088 		break;
2089 	case WACOM_HID_WD_CONTROLPANEL:
2090 		wacom_map_usage(input, usage, field, EV_KEY, KEY_CONTROLPANEL, 0);
2091 		features->device_type |= WACOM_DEVICETYPE_PAD;
2092 		break;
2093 	case WACOM_HID_WD_MODE_CHANGE:
2094 		/* do not overwrite previous data */
2095 		if (!wacom_wac->has_mode_change) {
2096 			wacom_wac->has_mode_change = true;
2097 			wacom_wac->is_direct_mode = true;
2098 		}
2099 		features->device_type |= WACOM_DEVICETYPE_PAD;
2100 		break;
2101 	}
2102 
2103 	switch (equivalent_usage & 0xfffffff0) {
2104 	case WACOM_HID_WD_EXPRESSKEY00:
2105 		wacom_map_usage(input, usage, field, EV_KEY,
2106 				wacom_numbered_button_to_key(features->numbered_buttons),
2107 				0);
2108 		features->numbered_buttons++;
2109 		features->device_type |= WACOM_DEVICETYPE_PAD;
2110 		break;
2111 	}
2112 }
2113 
wacom_wac_pad_event(struct hid_device * hdev,struct hid_field * field,struct hid_usage * usage,__s32 value)2114 static void wacom_wac_pad_event(struct hid_device *hdev, struct hid_field *field,
2115 		struct hid_usage *usage, __s32 value)
2116 {
2117 	struct wacom *wacom = hid_get_drvdata(hdev);
2118 	struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2119 	struct input_dev *input = wacom_wac->pad_input;
2120 	struct wacom_features *features = &wacom_wac->features;
2121 	unsigned equivalent_usage = wacom_equivalent_usage(usage->hid);
2122 	int i;
2123 	bool do_report = false;
2124 
2125 	/*
2126 	 * Avoid reporting this event and setting inrange_state if this usage
2127 	 * hasn't been mapped.
2128 	 */
2129 	if (!usage->type && equivalent_usage != WACOM_HID_WD_MODE_CHANGE)
2130 		return;
2131 
2132 	if (wacom_equivalent_usage(field->physical) == HID_DG_TABLETFUNCTIONKEY) {
2133 		bool is_abs_touchring = usage->hid == WACOM_HID_WD_TOUCHRING &&
2134 					!(field->flags & HID_MAIN_ITEM_RELATIVE);
2135 
2136 		if (!is_abs_touchring)
2137 			wacom_wac->hid_data.inrange_state |= value;
2138 	}
2139 
2140 	/* Process touch switch state first since it is reported through touch interface,
2141 	 * which is indepentent of pad interface. In the case when there are no other pad
2142 	 * events, the pad interface will not even be created.
2143 	 */
2144 	if ((equivalent_usage == WACOM_HID_WD_MUTE_DEVICE) ||
2145 	   (equivalent_usage == WACOM_HID_WD_TOUCHONOFF)) {
2146 		if (wacom_wac->shared->touch_input) {
2147 			bool *is_touch_on = &wacom_wac->shared->is_touch_on;
2148 
2149 			if (equivalent_usage == WACOM_HID_WD_MUTE_DEVICE && value)
2150 				*is_touch_on = !(*is_touch_on);
2151 			else if (equivalent_usage == WACOM_HID_WD_TOUCHONOFF)
2152 				*is_touch_on = value;
2153 
2154 			input_report_switch(wacom_wac->shared->touch_input,
2155 					    SW_MUTE_DEVICE, !(*is_touch_on));
2156 			input_sync(wacom_wac->shared->touch_input);
2157 		}
2158 		return;
2159 	}
2160 
2161 	if (!input)
2162 		return;
2163 
2164 	switch (equivalent_usage) {
2165 	case WACOM_HID_WD_TOUCHRING:
2166 		/*
2167 		 * Userspace expects touchrings to increase in value with
2168 		 * clockwise gestures and have their zero point at the
2169 		 * tablet's left. HID events "should" be clockwise-
2170 		 * increasing and zero at top, though the MobileStudio
2171 		 * Pro and 2nd-gen Intuos Pro don't do this...
2172 		 */
2173 		if (hdev->vendor == 0x56a &&
2174 		    (hdev->product == 0x34d || hdev->product == 0x34e ||  /* MobileStudio Pro */
2175 		     hdev->product == 0x357 || hdev->product == 0x358 ||  /* Intuos Pro 2 */
2176 		     hdev->product == 0x392 ||				  /* Intuos Pro 2 */
2177 		     hdev->product == 0x398 || hdev->product == 0x399 ||  /* MobileStudio Pro */
2178 		     hdev->product == 0x3AA)) {				  /* MobileStudio Pro */
2179 			value = (field->logical_maximum - value);
2180 
2181 			if (hdev->product == 0x357 || hdev->product == 0x358 ||
2182 			    hdev->product == 0x392)
2183 				value = wacom_offset_rotation(input, usage, value, 3, 16);
2184 			else if (hdev->product == 0x34d || hdev->product == 0x34e ||
2185 				 hdev->product == 0x398 || hdev->product == 0x399 ||
2186 				 hdev->product == 0x3AA)
2187 				value = wacom_offset_rotation(input, usage, value, 1, 2);
2188 		}
2189 		else if (field->flags & HID_MAIN_ITEM_RELATIVE) {
2190 			int hires_value = value * 120 / usage->resolution_multiplier;
2191 			int *ring_value;
2192 			int lowres_code;
2193 
2194 			if (usage->code == REL_WHEEL_HI_RES) {
2195 				/* We must invert the sign for vertical
2196 				 * relative scrolling. Clockwise
2197 				 * rotation produces positive values
2198 				 * from HW, but userspace treats
2199 				 * positive REL_WHEEL as a scroll *up*!
2200 				 */
2201 				hires_value = -hires_value;
2202 				ring_value = &wacom_wac->hid_data.ring_value;
2203 				lowres_code = REL_WHEEL;
2204 			}
2205 			else if (usage->code == REL_HWHEEL_HI_RES) {
2206 				/* No need to invert the sign for
2207 				 * horizontal relative scrolling.
2208 				 * Clockwise rotation produces positive
2209 				 * values from HW and userspace treats
2210 				 * positive REL_HWHEEL as a scroll
2211 				 * right.
2212 				 */
2213 				ring_value = &wacom_wac->hid_data.ring2_value;
2214 				lowres_code = REL_HWHEEL;
2215 			}
2216 			else {
2217 				hid_err(wacom->hdev, "unrecognized relative wheel with code %d\n",
2218 					usage->code);
2219 				break;
2220 			}
2221 
2222 			value = hires_value;
2223 			*ring_value += hires_value;
2224 
2225 			/* Emulate a legacy wheel click for every 120
2226 			 * units of hi-res travel.
2227 			 */
2228 			if (*ring_value >= 120 || *ring_value <= -120) {
2229 				int clicks = *ring_value / 120;
2230 
2231 				input_event(input, usage->type, lowres_code, clicks);
2232 				*ring_value -= clicks * 120;
2233 			}
2234 		}
2235 		else {
2236 			value = wacom_offset_rotation(input, usage, value, 1, 4);
2237 		}
2238 		do_report = true;
2239 		break;
2240 	case WACOM_HID_WD_TOUCHRINGSTATUS:
2241 		if (!value)
2242 			input_event(input, usage->type, usage->code, 0);
2243 		break;
2244 
2245 	case WACOM_HID_WD_MODE_CHANGE:
2246 		if (wacom_wac->is_direct_mode != value) {
2247 			wacom_wac->is_direct_mode = value;
2248 			wacom_schedule_work(&wacom->wacom_wac, WACOM_WORKER_MODE_CHANGE);
2249 		}
2250 		break;
2251 
2252 	case WACOM_HID_WD_BUTTONCENTER:
2253 		for (i = 0; i < wacom->led.count; i++)
2254 			wacom_update_led(wacom, features->numbered_buttons,
2255 					 value, i);
2256 		fallthrough;
2257 	default:
2258 		do_report = true;
2259 		break;
2260 	}
2261 
2262 	if (do_report) {
2263 		input_event(input, usage->type, usage->code, value);
2264 		if (value)
2265 			wacom_wac->hid_data.pad_input_event_flag = true;
2266 	}
2267 }
2268 
wacom_wac_pad_pre_report(struct hid_device * hdev,struct hid_report * report)2269 static void wacom_wac_pad_pre_report(struct hid_device *hdev,
2270 		struct hid_report *report)
2271 {
2272 	struct wacom *wacom = hid_get_drvdata(hdev);
2273 	struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2274 
2275 	wacom_wac->hid_data.inrange_state = 0;
2276 }
2277 
wacom_wac_pad_report(struct hid_device * hdev,struct hid_report * report,struct hid_field * field)2278 static void wacom_wac_pad_report(struct hid_device *hdev,
2279 		struct hid_report *report, struct hid_field *field)
2280 {
2281 	struct wacom *wacom = hid_get_drvdata(hdev);
2282 	struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2283 	struct input_dev *input = wacom_wac->pad_input;
2284 	bool active = wacom_wac->hid_data.inrange_state != 0;
2285 
2286 	/* report prox for expresskey events */
2287 	if (wacom_wac->hid_data.pad_input_event_flag) {
2288 		input_event(input, EV_ABS, ABS_MISC, active ? PAD_DEVICE_ID : 0);
2289 		input_sync(input);
2290 		if (!active)
2291 			wacom_wac->hid_data.pad_input_event_flag = false;
2292 	}
2293 }
2294 
wacom_set_barrel_switch3_usage(struct wacom_wac * wacom_wac)2295 static void wacom_set_barrel_switch3_usage(struct wacom_wac *wacom_wac)
2296 {
2297 	struct input_dev *input = wacom_wac->pen_input;
2298 	struct wacom_features *features = &wacom_wac->features;
2299 
2300 	if (!(features->quirks & WACOM_QUIRK_AESPEN) &&
2301 	    wacom_wac->hid_data.barrelswitch &&
2302 	    wacom_wac->hid_data.barrelswitch2 &&
2303 	    wacom_wac->hid_data.serialhi &&
2304 	    !wacom_wac->hid_data.barrelswitch3) {
2305 		input_set_capability(input, EV_KEY, BTN_STYLUS3);
2306 		features->quirks |= WACOM_QUIRK_PEN_BUTTON3;
2307 	}
2308 }
2309 
wacom_wac_pen_usage_mapping(struct hid_device * hdev,struct hid_field * field,struct hid_usage * usage)2310 static void wacom_wac_pen_usage_mapping(struct hid_device *hdev,
2311 		struct hid_field *field, struct hid_usage *usage)
2312 {
2313 	struct wacom *wacom = hid_get_drvdata(hdev);
2314 	struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2315 	struct wacom_features *features = &wacom_wac->features;
2316 	struct input_dev *input = wacom_wac->pen_input;
2317 	unsigned equivalent_usage = wacom_equivalent_usage(usage->hid);
2318 
2319 	switch (equivalent_usage) {
2320 	case HID_GD_X:
2321 		wacom_map_usage(input, usage, field, EV_ABS, ABS_X, 4);
2322 		break;
2323 	case HID_GD_Y:
2324 		wacom_map_usage(input, usage, field, EV_ABS, ABS_Y, 4);
2325 		break;
2326 	case WACOM_HID_WD_DISTANCE:
2327 	case HID_GD_Z:
2328 		wacom_map_usage(input, usage, field, EV_ABS, ABS_DISTANCE, 0);
2329 		break;
2330 	case HID_DG_TIPPRESSURE:
2331 		wacom_map_usage(input, usage, field, EV_ABS, ABS_PRESSURE, 0);
2332 		break;
2333 	case HID_DG_INRANGE:
2334 		wacom_map_usage(input, usage, field, EV_KEY, BTN_TOOL_PEN, 0);
2335 		break;
2336 	case HID_DG_INVERT:
2337 		wacom_map_usage(input, usage, field, EV_KEY,
2338 				BTN_TOOL_RUBBER, 0);
2339 		break;
2340 	case HID_DG_TILT_X:
2341 		wacom_map_usage(input, usage, field, EV_ABS, ABS_TILT_X, 0);
2342 		break;
2343 	case HID_DG_TILT_Y:
2344 		wacom_map_usage(input, usage, field, EV_ABS, ABS_TILT_Y, 0);
2345 		break;
2346 	case HID_DG_TWIST:
2347 		wacom_map_usage(input, usage, field, EV_ABS, ABS_Z, 0);
2348 		break;
2349 	case HID_DG_ERASER:
2350 		input_set_capability(input, EV_KEY, BTN_TOOL_RUBBER);
2351 		wacom_map_usage(input, usage, field, EV_KEY, BTN_TOUCH, 0);
2352 		break;
2353 	case HID_DG_TIPSWITCH:
2354 		input_set_capability(input, EV_KEY, BTN_TOOL_PEN);
2355 		wacom_map_usage(input, usage, field, EV_KEY, BTN_TOUCH, 0);
2356 		break;
2357 	case HID_DG_BARRELSWITCH:
2358 		wacom_wac->hid_data.barrelswitch = true;
2359 		wacom_set_barrel_switch3_usage(wacom_wac);
2360 		wacom_map_usage(input, usage, field, EV_KEY, BTN_STYLUS, 0);
2361 		break;
2362 	case HID_DG_BARRELSWITCH2:
2363 		wacom_wac->hid_data.barrelswitch2 = true;
2364 		wacom_set_barrel_switch3_usage(wacom_wac);
2365 		wacom_map_usage(input, usage, field, EV_KEY, BTN_STYLUS2, 0);
2366 		break;
2367 	case HID_DG_TOOLSERIALNUMBER:
2368 		features->quirks |= WACOM_QUIRK_TOOLSERIAL;
2369 		wacom_map_usage(input, usage, field, EV_MSC, MSC_SERIAL, 0);
2370 		break;
2371 	case HID_DG_SCANTIME:
2372 		wacom_map_usage(input, usage, field, EV_MSC, MSC_TIMESTAMP, 0);
2373 		break;
2374 	case WACOM_HID_WD_SENSE:
2375 		features->quirks |= WACOM_QUIRK_SENSE;
2376 		wacom_map_usage(input, usage, field, EV_KEY, BTN_TOOL_PEN, 0);
2377 		break;
2378 	case WACOM_HID_WD_SERIALHI:
2379 		wacom_wac->hid_data.serialhi = true;
2380 		wacom_set_barrel_switch3_usage(wacom_wac);
2381 		wacom_map_usage(input, usage, field, EV_ABS, ABS_MISC, 0);
2382 		break;
2383 	case WACOM_HID_WD_FINGERWHEEL:
2384 		input_set_capability(input, EV_KEY, BTN_TOOL_AIRBRUSH);
2385 		wacom_map_usage(input, usage, field, EV_ABS, ABS_WHEEL, 0);
2386 		break;
2387 	case WACOM_HID_WD_BARRELSWITCH3:
2388 		wacom_wac->hid_data.barrelswitch3 = true;
2389 		wacom_map_usage(input, usage, field, EV_KEY, BTN_STYLUS3, 0);
2390 		features->quirks &= ~WACOM_QUIRK_PEN_BUTTON3;
2391 		break;
2392 	case WACOM_HID_WD_SEQUENCENUMBER:
2393 		wacom_wac->hid_data.sequence_number = -1;
2394 		break;
2395 	}
2396 }
2397 
wacom_wac_pen_event(struct hid_device * hdev,struct hid_field * field,struct hid_usage * usage,__s32 value)2398 static void wacom_wac_pen_event(struct hid_device *hdev, struct hid_field *field,
2399 		struct hid_usage *usage, __s32 value)
2400 {
2401 	struct wacom *wacom = hid_get_drvdata(hdev);
2402 	struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2403 	struct wacom_features *features = &wacom_wac->features;
2404 	struct input_dev *input = wacom_wac->pen_input;
2405 	unsigned equivalent_usage = wacom_equivalent_usage(usage->hid);
2406 
2407 	if (wacom_wac->is_invalid_bt_frame)
2408 		return;
2409 
2410 	switch (equivalent_usage) {
2411 	case HID_GD_Z:
2412 		/*
2413 		 * HID_GD_Z "should increase as the control's position is
2414 		 * moved from high to low", while ABS_DISTANCE instead
2415 		 * increases in value as the tool moves from low to high.
2416 		 */
2417 		value = field->logical_maximum - value;
2418 		break;
2419 	case HID_DG_INRANGE:
2420 		mod_timer(&wacom->idleprox_timer, jiffies + msecs_to_jiffies(100));
2421 		wacom_wac->hid_data.inrange_state = value;
2422 		if (!(features->quirks & WACOM_QUIRK_SENSE))
2423 			wacom_wac->hid_data.sense_state = value;
2424 		return;
2425 	case HID_DG_INVERT:
2426 		wacom_wac->hid_data.invert_state = value;
2427 		return;
2428 	case HID_DG_ERASER:
2429 	case HID_DG_TIPSWITCH:
2430 		wacom_wac->hid_data.tipswitch |= value;
2431 		return;
2432 	case HID_DG_BARRELSWITCH:
2433 		wacom_wac->hid_data.barrelswitch = value;
2434 		return;
2435 	case HID_DG_BARRELSWITCH2:
2436 		wacom_wac->hid_data.barrelswitch2 = value;
2437 		return;
2438 	case HID_DG_TOOLSERIALNUMBER:
2439 		if (value) {
2440 			wacom_wac->serial[0] = (wacom_wac->serial[0] & ~0xFFFFFFFFULL);
2441 			wacom_wac->serial[0] |= wacom_s32tou(value, field->report_size);
2442 		}
2443 		return;
2444 	case HID_DG_TWIST:
2445 		/* don't modify the value if the pen doesn't support the feature */
2446 		if (!wacom_is_art_pen(wacom_wac->id[0])) return;
2447 
2448 		/*
2449 		 * Userspace expects pen twist to have its zero point when
2450 		 * the buttons/finger is on the tablet's left. HID values
2451 		 * are zero when buttons are toward the top.
2452 		 */
2453 		value = wacom_offset_rotation(input, usage, value, 1, 4);
2454 		break;
2455 	case WACOM_HID_WD_SENSE:
2456 		wacom_wac->hid_data.sense_state = value;
2457 		return;
2458 	case WACOM_HID_WD_SERIALHI:
2459 		if (value) {
2460 			__u32 raw_value = wacom_s32tou(value, field->report_size);
2461 
2462 			wacom_wac->serial[0] = (wacom_wac->serial[0] & 0xFFFFFFFF);
2463 			wacom_wac->serial[0] |= ((__u64)raw_value) << 32;
2464 			/*
2465 			 * Non-USI EMR devices may contain additional tool type
2466 			 * information here. See WACOM_HID_WD_TOOLTYPE case for
2467 			 * more details.
2468 			 */
2469 			if (value >> 20 == 1) {
2470 				wacom_wac->id[0] |= raw_value & 0xFFFFF;
2471 			}
2472 		}
2473 		return;
2474 	case WACOM_HID_WD_TOOLTYPE:
2475 		/*
2476 		 * Some devices (MobileStudio Pro, and possibly later
2477 		 * devices as well) do not return the complete tool
2478 		 * type in their WACOM_HID_WD_TOOLTYPE usage. Use a
2479 		 * bitwise OR so the complete value can be built
2480 		 * up over time :(
2481 		 */
2482 		wacom_wac->id[0] |= wacom_s32tou(value, field->report_size);
2483 		return;
2484 	case WACOM_HID_WD_OFFSETLEFT:
2485 		if (features->offset_left && value != features->offset_left)
2486 			hid_warn(hdev, "%s: overriding existing left offset "
2487 				 "%d -> %d\n", __func__, value,
2488 				 features->offset_left);
2489 		features->offset_left = value;
2490 		return;
2491 	case WACOM_HID_WD_OFFSETRIGHT:
2492 		if (features->offset_right && value != features->offset_right)
2493 			hid_warn(hdev, "%s: overriding existing right offset "
2494 				 "%d -> %d\n", __func__, value,
2495 				 features->offset_right);
2496 		features->offset_right = value;
2497 		return;
2498 	case WACOM_HID_WD_OFFSETTOP:
2499 		if (features->offset_top && value != features->offset_top)
2500 			hid_warn(hdev, "%s: overriding existing top offset "
2501 				 "%d -> %d\n", __func__, value,
2502 				 features->offset_top);
2503 		features->offset_top = value;
2504 		return;
2505 	case WACOM_HID_WD_OFFSETBOTTOM:
2506 		if (features->offset_bottom && value != features->offset_bottom)
2507 			hid_warn(hdev, "%s: overriding existing bottom offset "
2508 				 "%d -> %d\n", __func__, value,
2509 				 features->offset_bottom);
2510 		features->offset_bottom = value;
2511 		return;
2512 	case WACOM_HID_WD_REPORT_VALID:
2513 		wacom_wac->is_invalid_bt_frame = !value;
2514 		return;
2515 	case WACOM_HID_WD_BARRELSWITCH3:
2516 		wacom_wac->hid_data.barrelswitch3 = value;
2517 		return;
2518 	case WACOM_HID_WD_SEQUENCENUMBER:
2519 		if (wacom_wac->hid_data.sequence_number != value &&
2520 		    wacom_wac->hid_data.sequence_number >= 0) {
2521 			int sequence_size = field->logical_maximum - field->logical_minimum + 1;
2522 			int drop_count = (value - wacom_wac->hid_data.sequence_number) % sequence_size;
2523 			hid_warn(hdev, "Dropped %d packets", drop_count);
2524 		}
2525 		wacom_wac->hid_data.sequence_number = value + 1;
2526 		if (wacom_wac->hid_data.sequence_number > field->logical_maximum)
2527 			wacom_wac->hid_data.sequence_number = field->logical_minimum;
2528 		return;
2529 	}
2530 
2531 	/* send pen events only when touch is up or forced out
2532 	 * or touch arbitration is off
2533 	 */
2534 	if (!usage->type || delay_pen_events(wacom_wac))
2535 		return;
2536 
2537 	/* send pen events only when the pen is in range */
2538 	if (wacom_wac->hid_data.inrange_state)
2539 		input_event(input, usage->type, usage->code, value);
2540 	else if (wacom_wac->shared->stylus_in_proximity && !wacom_wac->hid_data.sense_state)
2541 		input_event(input, usage->type, usage->code, 0);
2542 }
2543 
wacom_wac_pen_pre_report(struct hid_device * hdev,struct hid_report * report)2544 static void wacom_wac_pen_pre_report(struct hid_device *hdev,
2545 		struct hid_report *report)
2546 {
2547 	struct wacom *wacom = hid_get_drvdata(hdev);
2548 	struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2549 
2550 	wacom_wac->is_invalid_bt_frame = false;
2551 	return;
2552 }
2553 
wacom_wac_pen_report(struct hid_device * hdev,struct hid_report * report)2554 static void wacom_wac_pen_report(struct hid_device *hdev,
2555 		struct hid_report *report)
2556 {
2557 	struct wacom *wacom = hid_get_drvdata(hdev);
2558 	struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2559 	struct input_dev *input = wacom_wac->pen_input;
2560 	bool range = wacom_wac->hid_data.inrange_state;
2561 	bool sense = wacom_wac->hid_data.sense_state;
2562 	bool entering_range = !wacom_wac->tool[0] && range;
2563 
2564 	if (wacom_wac->is_invalid_bt_frame)
2565 		return;
2566 
2567 	if (entering_range) { /* first in range */
2568 		/* Going into range select tool */
2569 		if (wacom_wac->hid_data.invert_state)
2570 			wacom_wac->tool[0] = BTN_TOOL_RUBBER;
2571 		else if (wacom_wac->features.quirks & WACOM_QUIRK_AESPEN)
2572 			wacom_wac->tool[0] = BTN_TOOL_PEN;
2573 		else if (wacom_wac->id[0])
2574 			wacom_wac->tool[0] = wacom_intuos_get_tool_type(wacom_wac->id[0]);
2575 		else
2576 			wacom_wac->tool[0] = BTN_TOOL_PEN;
2577 	}
2578 
2579 	/* keep pen state for touch events */
2580 	wacom_wac->shared->stylus_in_proximity = sense;
2581 
2582 	if (!delay_pen_events(wacom_wac) && wacom_wac->tool[0]) {
2583 		int id = wacom_wac->id[0];
2584 		if (wacom_wac->features.quirks & WACOM_QUIRK_PEN_BUTTON3) {
2585 			int sw_state = wacom_wac->hid_data.barrelswitch |
2586 				       (wacom_wac->hid_data.barrelswitch2 << 1);
2587 			wacom_wac->hid_data.barrelswitch = sw_state == 1;
2588 			wacom_wac->hid_data.barrelswitch2 = sw_state == 2;
2589 			wacom_wac->hid_data.barrelswitch3 = sw_state == 3;
2590 		}
2591 		input_report_key(input, BTN_STYLUS, wacom_wac->hid_data.barrelswitch);
2592 		input_report_key(input, BTN_STYLUS2, wacom_wac->hid_data.barrelswitch2);
2593 		input_report_key(input, BTN_STYLUS3, wacom_wac->hid_data.barrelswitch3);
2594 
2595 		/*
2596 		 * Non-USI EMR tools should have their IDs mangled to
2597 		 * match the legacy behavior of wacom_intuos_general
2598 		 */
2599 		if (wacom_wac->serial[0] >> 52 == 1)
2600 			id = wacom_intuos_id_mangle(id);
2601 
2602 		/*
2603 		 * To ensure compatibility with xf86-input-wacom, we should
2604 		 * report the BTN_TOOL_* event prior to the ABS_MISC or
2605 		 * MSC_SERIAL events.
2606 		 */
2607 		input_report_key(input, BTN_TOUCH,
2608 				wacom_wac->hid_data.tipswitch);
2609 		input_report_key(input, wacom_wac->tool[0], sense);
2610 		if (wacom_wac->serial[0]) {
2611 			/*
2612 			 * xf86-input-wacom does not accept a serial number
2613 			 * of '0'. Report the low 32 bits if possible, but
2614 			 * if they are zero, report the upper ones instead.
2615 			 */
2616 			__u32 serial_lo = wacom_wac->serial[0] & 0xFFFFFFFFu;
2617 			__u32 serial_hi = wacom_wac->serial[0] >> 32;
2618 			input_event(input, EV_MSC, MSC_SERIAL, (int)(serial_lo ? serial_lo : serial_hi));
2619 			input_report_abs(input, ABS_MISC, sense ? id : 0);
2620 		}
2621 
2622 		wacom_wac->hid_data.tipswitch = false;
2623 
2624 		input_sync(input);
2625 	}
2626 
2627 	/* Handle AES battery timeout behavior */
2628 	if (wacom_wac->features.quirks & WACOM_QUIRK_AESPEN) {
2629 		if (entering_range)
2630 			cancel_delayed_work(&wacom->aes_battery_work);
2631 		if (!sense)
2632 			schedule_delayed_work(&wacom->aes_battery_work,
2633 					      msecs_to_jiffies(WACOM_AES_BATTERY_TIMEOUT));
2634 	}
2635 
2636 	if (!sense) {
2637 		wacom_wac->tool[0] = 0;
2638 		wacom_wac->id[0] = 0;
2639 		wacom_wac->serial[0] = 0;
2640 	}
2641 }
2642 
wacom_wac_finger_usage_mapping(struct hid_device * hdev,struct hid_field * field,struct hid_usage * usage)2643 static void wacom_wac_finger_usage_mapping(struct hid_device *hdev,
2644 		struct hid_field *field, struct hid_usage *usage)
2645 {
2646 	struct wacom *wacom = hid_get_drvdata(hdev);
2647 	struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2648 	struct input_dev *input = wacom_wac->touch_input;
2649 	unsigned touch_max = wacom_wac->features.touch_max;
2650 	unsigned equivalent_usage = wacom_equivalent_usage(usage->hid);
2651 
2652 	switch (equivalent_usage) {
2653 	case HID_GD_X:
2654 		if (touch_max == 1)
2655 			wacom_map_usage(input, usage, field, EV_ABS, ABS_X, 4);
2656 		else
2657 			wacom_map_usage(input, usage, field, EV_ABS,
2658 					ABS_MT_POSITION_X, 4);
2659 		break;
2660 	case HID_GD_Y:
2661 		if (touch_max == 1)
2662 			wacom_map_usage(input, usage, field, EV_ABS, ABS_Y, 4);
2663 		else
2664 			wacom_map_usage(input, usage, field, EV_ABS,
2665 					ABS_MT_POSITION_Y, 4);
2666 		break;
2667 	case HID_DG_WIDTH:
2668 	case HID_DG_HEIGHT:
2669 		wacom_map_usage(input, usage, field, EV_ABS, ABS_MT_TOUCH_MAJOR, 0);
2670 		wacom_map_usage(input, usage, field, EV_ABS, ABS_MT_TOUCH_MINOR, 0);
2671 		input_set_abs_params(input, ABS_MT_ORIENTATION, 0, 1, 0, 0);
2672 		break;
2673 	case HID_DG_TIPSWITCH:
2674 		wacom_map_usage(input, usage, field, EV_KEY, BTN_TOUCH, 0);
2675 		break;
2676 	case HID_DG_CONTACTCOUNT:
2677 		wacom_wac->hid_data.cc_report = field->report->id;
2678 		wacom_wac->hid_data.cc_index = field->index;
2679 		wacom_wac->hid_data.cc_value_index = usage->usage_index;
2680 		break;
2681 	case HID_DG_CONTACTID:
2682 		if ((field->logical_maximum - field->logical_minimum) < touch_max) {
2683 			/*
2684 			 * The HID descriptor for G11 sensors leaves logical
2685 			 * maximum set to '1' despite it being a multitouch
2686 			 * device. Override to a sensible number.
2687 			 */
2688 			field->logical_maximum = 255;
2689 		}
2690 		break;
2691 	case HID_DG_SCANTIME:
2692 		wacom_map_usage(input, usage, field, EV_MSC, MSC_TIMESTAMP, 0);
2693 		break;
2694 	}
2695 }
2696 
wacom_wac_finger_slot(struct wacom_wac * wacom_wac,struct input_dev * input)2697 static void wacom_wac_finger_slot(struct wacom_wac *wacom_wac,
2698 		struct input_dev *input)
2699 {
2700 	struct hid_data *hid_data = &wacom_wac->hid_data;
2701 	bool mt = wacom_wac->features.touch_max > 1;
2702 	bool touch_down = hid_data->tipswitch && hid_data->confidence;
2703 	bool prox = touch_down && report_touch_events(wacom_wac);
2704 
2705 	if (touch_is_muted(wacom_wac)) {
2706 		if (!wacom_wac->shared->touch_down)
2707 			return;
2708 		prox = false;
2709 	}
2710 
2711 	wacom_wac->hid_data.num_received++;
2712 	if (wacom_wac->hid_data.num_received > wacom_wac->hid_data.num_expected)
2713 		return;
2714 
2715 	if (mt) {
2716 		int slot;
2717 
2718 		slot = input_mt_get_slot_by_key(input, hid_data->id);
2719 		if (slot < 0) {
2720 			return;
2721 		} else {
2722 			struct input_mt_slot *ps = &input->mt->slots[slot];
2723 			int mt_id = input_mt_get_value(ps, ABS_MT_TRACKING_ID);
2724 
2725 			if (!prox && mt_id < 0) {
2726 				// No data to send for this slot; short-circuit
2727 				return;
2728 			}
2729 		}
2730 
2731 		input_mt_slot(input, slot);
2732 		input_mt_report_slot_state(input, MT_TOOL_FINGER, prox);
2733 	}
2734 	else {
2735 		input_report_key(input, BTN_TOUCH, prox);
2736 	}
2737 
2738 	if (prox) {
2739 		input_report_abs(input, mt ? ABS_MT_POSITION_X : ABS_X,
2740 				 hid_data->x);
2741 		input_report_abs(input, mt ? ABS_MT_POSITION_Y : ABS_Y,
2742 				 hid_data->y);
2743 
2744 		if (test_bit(ABS_MT_TOUCH_MAJOR, input->absbit)) {
2745 			input_report_abs(input, ABS_MT_TOUCH_MAJOR, max(hid_data->width, hid_data->height));
2746 			input_report_abs(input, ABS_MT_TOUCH_MINOR, min(hid_data->width, hid_data->height));
2747 			if (hid_data->width != hid_data->height)
2748 				input_report_abs(input, ABS_MT_ORIENTATION, hid_data->width <= hid_data->height ? 0 : 1);
2749 		}
2750 	}
2751 }
2752 
wacom_wac_finger_event(struct hid_device * hdev,struct hid_field * field,struct hid_usage * usage,__s32 value)2753 static void wacom_wac_finger_event(struct hid_device *hdev,
2754 		struct hid_field *field, struct hid_usage *usage, __s32 value)
2755 {
2756 	struct wacom *wacom = hid_get_drvdata(hdev);
2757 	struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2758 	unsigned equivalent_usage = wacom_equivalent_usage(usage->hid);
2759 	struct wacom_features *features = &wacom->wacom_wac.features;
2760 
2761 	if (touch_is_muted(wacom_wac) && !wacom_wac->shared->touch_down)
2762 		return;
2763 
2764 	if (wacom_wac->is_invalid_bt_frame)
2765 		return;
2766 
2767 	switch (equivalent_usage) {
2768 	case HID_DG_CONFIDENCE:
2769 		wacom_wac->hid_data.confidence = value;
2770 		break;
2771 	case HID_GD_X:
2772 		wacom_wac->hid_data.x = value;
2773 		break;
2774 	case HID_GD_Y:
2775 		wacom_wac->hid_data.y = value;
2776 		break;
2777 	case HID_DG_WIDTH:
2778 		wacom_wac->hid_data.width = value;
2779 		break;
2780 	case HID_DG_HEIGHT:
2781 		wacom_wac->hid_data.height = value;
2782 		break;
2783 	case HID_DG_CONTACTID:
2784 		wacom_wac->hid_data.id = value;
2785 		break;
2786 	case HID_DG_TIPSWITCH:
2787 		wacom_wac->hid_data.tipswitch = value;
2788 		break;
2789 	case WACOM_HID_WT_REPORT_VALID:
2790 		wacom_wac->is_invalid_bt_frame = !value;
2791 		return;
2792 	case HID_DG_CONTACTMAX:
2793 		if (!features->touch_max) {
2794 			features->touch_max = value;
2795 		} else {
2796 			hid_warn(hdev, "%s: ignoring attempt to overwrite non-zero touch_max "
2797 				 "%d -> %d\n", __func__, features->touch_max, value);
2798 		}
2799 		return;
2800 	}
2801 
2802 	if (usage->usage_index + 1 == field->report_count) {
2803 		if (equivalent_usage == wacom_wac->hid_data.last_slot_field)
2804 			wacom_wac_finger_slot(wacom_wac, wacom_wac->touch_input);
2805 	}
2806 }
2807 
wacom_wac_finger_pre_report(struct hid_device * hdev,struct hid_report * report)2808 static void wacom_wac_finger_pre_report(struct hid_device *hdev,
2809 		struct hid_report *report)
2810 {
2811 	struct wacom *wacom = hid_get_drvdata(hdev);
2812 	struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2813 	struct hid_data* hid_data = &wacom_wac->hid_data;
2814 	int i;
2815 
2816 	if (touch_is_muted(wacom_wac) && !wacom_wac->shared->touch_down)
2817 		return;
2818 
2819 	wacom_wac->is_invalid_bt_frame = false;
2820 
2821 	hid_data->confidence = true;
2822 
2823 	hid_data->cc_report = 0;
2824 	hid_data->cc_index = -1;
2825 	hid_data->cc_value_index = -1;
2826 
2827 	for (i = 0; i < report->maxfield; i++) {
2828 		struct hid_field *field = report->field[i];
2829 		int j;
2830 
2831 		for (j = 0; j < field->maxusage; j++) {
2832 			struct hid_usage *usage = &field->usage[j];
2833 			unsigned int equivalent_usage =
2834 				wacom_equivalent_usage(usage->hid);
2835 
2836 			switch (equivalent_usage) {
2837 			case HID_GD_X:
2838 			case HID_GD_Y:
2839 			case HID_DG_WIDTH:
2840 			case HID_DG_HEIGHT:
2841 			case HID_DG_CONTACTID:
2842 			case HID_DG_INRANGE:
2843 			case HID_DG_INVERT:
2844 			case HID_DG_TIPSWITCH:
2845 				hid_data->last_slot_field = equivalent_usage;
2846 				break;
2847 			case HID_DG_CONTACTCOUNT:
2848 				hid_data->cc_report = report->id;
2849 				hid_data->cc_index = i;
2850 				hid_data->cc_value_index = j;
2851 				break;
2852 			}
2853 		}
2854 	}
2855 
2856 	if (hid_data->cc_report != 0 &&
2857 	    hid_data->cc_index >= 0) {
2858 		struct hid_field *field = report->field[hid_data->cc_index];
2859 		int value = field->value[hid_data->cc_value_index];
2860 		if (value) {
2861 			hid_data->num_expected = value;
2862 			hid_data->num_received = 0;
2863 		}
2864 	}
2865 	else {
2866 		hid_data->num_expected = wacom_wac->features.touch_max;
2867 		hid_data->num_received = 0;
2868 	}
2869 }
2870 
wacom_wac_finger_report(struct hid_device * hdev,struct hid_report * report)2871 static void wacom_wac_finger_report(struct hid_device *hdev,
2872 		struct hid_report *report)
2873 {
2874 	struct wacom *wacom = hid_get_drvdata(hdev);
2875 	struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2876 	struct input_dev *input = wacom_wac->touch_input;
2877 	unsigned touch_max = wacom_wac->features.touch_max;
2878 
2879 	/* if there was nothing to process, don't send an empty sync */
2880 	if (wacom_wac->hid_data.num_expected == 0)
2881 		return;
2882 
2883 	/* If more packets of data are expected, give us a chance to
2884 	 * process them rather than immediately syncing a partial
2885 	 * update.
2886 	 */
2887 	if (wacom_wac->hid_data.num_received < wacom_wac->hid_data.num_expected)
2888 		return;
2889 
2890 	if (touch_max > 1)
2891 		input_mt_sync_frame(input);
2892 
2893 	input_sync(input);
2894 	wacom_wac->hid_data.num_received = 0;
2895 	wacom_wac->hid_data.num_expected = 0;
2896 
2897 	/* keep touch state for pen event */
2898 	wacom_wac->shared->touch_down = wacom_wac_finger_count_touches(wacom_wac);
2899 }
2900 
wacom_wac_usage_mapping(struct hid_device * hdev,struct hid_field * field,struct hid_usage * usage)2901 void wacom_wac_usage_mapping(struct hid_device *hdev,
2902 		struct hid_field *field, struct hid_usage *usage)
2903 {
2904 	struct wacom *wacom = hid_get_drvdata(hdev);
2905 	struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2906 	struct wacom_features *features = &wacom_wac->features;
2907 
2908 	if (WACOM_DIRECT_DEVICE(field))
2909 		features->device_type |= WACOM_DEVICETYPE_DIRECT;
2910 
2911 	/* usage tests must precede field tests */
2912 	if (WACOM_BATTERY_USAGE(usage))
2913 		wacom_wac_battery_usage_mapping(hdev, field, usage);
2914 	else if (WACOM_PAD_FIELD(field))
2915 		wacom_wac_pad_usage_mapping(hdev, field, usage);
2916 	else if (WACOM_PEN_FIELD(field))
2917 		wacom_wac_pen_usage_mapping(hdev, field, usage);
2918 	else if (WACOM_FINGER_FIELD(field))
2919 		wacom_wac_finger_usage_mapping(hdev, field, usage);
2920 }
2921 
wacom_wac_event(struct hid_device * hdev,struct hid_field * field,struct hid_usage * usage,__s32 value)2922 void wacom_wac_event(struct hid_device *hdev, struct hid_field *field,
2923 		struct hid_usage *usage, __s32 value)
2924 {
2925 	struct wacom *wacom = hid_get_drvdata(hdev);
2926 
2927 	if (wacom->wacom_wac.features.type != HID_GENERIC)
2928 		return;
2929 
2930 	if (value > field->logical_maximum || value < field->logical_minimum)
2931 		return;
2932 
2933 	/* usage tests must precede field tests */
2934 	if (WACOM_BATTERY_USAGE(usage))
2935 		wacom_wac_battery_event(hdev, field, usage, value);
2936 	else if (WACOM_PAD_FIELD(field))
2937 		wacom_wac_pad_event(hdev, field, usage, value);
2938 	else if (WACOM_PEN_FIELD(field) && wacom->wacom_wac.pen_input)
2939 		wacom_wac_pen_event(hdev, field, usage, value);
2940 	else if (WACOM_FINGER_FIELD(field) && wacom->wacom_wac.touch_input)
2941 		wacom_wac_finger_event(hdev, field, usage, value);
2942 }
2943 
wacom_report_events(struct hid_device * hdev,struct hid_report * report,int collection_index,int field_index)2944 static void wacom_report_events(struct hid_device *hdev,
2945 				struct hid_report *report, int collection_index,
2946 				int field_index)
2947 {
2948 	int r;
2949 
2950 	for (r = field_index; r < report->maxfield; r++) {
2951 		struct hid_field *field;
2952 		unsigned count, n;
2953 
2954 		field = report->field[r];
2955 		count = field->report_count;
2956 
2957 		if (!(HID_MAIN_ITEM_VARIABLE & field->flags))
2958 			continue;
2959 
2960 		for (n = 0 ; n < count; n++) {
2961 			if (field->usage[n].collection_index == collection_index)
2962 				wacom_wac_event(hdev, field, &field->usage[n],
2963 						field->value[n]);
2964 			else
2965 				return;
2966 		}
2967 	}
2968 }
2969 
wacom_wac_collection(struct hid_device * hdev,struct hid_report * report,int collection_index,struct hid_field * field,int field_index)2970 static int wacom_wac_collection(struct hid_device *hdev, struct hid_report *report,
2971 			 int collection_index, struct hid_field *field,
2972 			 int field_index)
2973 {
2974 	struct wacom *wacom = hid_get_drvdata(hdev);
2975 
2976 	wacom_report_events(hdev, report, collection_index, field_index);
2977 
2978 	/*
2979 	 * Non-input reports may be sent prior to the device being
2980 	 * completely initialized. Since only their events need
2981 	 * to be processed, exit after 'wacom_report_events' has
2982 	 * been called to prevent potential crashes in the report-
2983 	 * processing functions.
2984 	 */
2985 	if (report->type != HID_INPUT_REPORT)
2986 		return -1;
2987 
2988 	if (WACOM_PAD_FIELD(field))
2989 		return 0;
2990 	else if (WACOM_PEN_FIELD(field) && wacom->wacom_wac.pen_input)
2991 		wacom_wac_pen_report(hdev, report);
2992 	else if (WACOM_FINGER_FIELD(field) && wacom->wacom_wac.touch_input)
2993 		wacom_wac_finger_report(hdev, report);
2994 
2995 	return 0;
2996 }
2997 
wacom_wac_report(struct hid_device * hdev,struct hid_report * report)2998 void wacom_wac_report(struct hid_device *hdev, struct hid_report *report)
2999 {
3000 	struct wacom *wacom = hid_get_drvdata(hdev);
3001 	struct wacom_wac *wacom_wac = &wacom->wacom_wac;
3002 	struct hid_field *field;
3003 	bool pad_in_hid_field = false, pen_in_hid_field = false,
3004 		finger_in_hid_field = false, true_pad = false;
3005 	int r;
3006 	int prev_collection = -1;
3007 
3008 	if (wacom_wac->features.type != HID_GENERIC)
3009 		return;
3010 
3011 	for (r = 0; r < report->maxfield; r++) {
3012 		field = report->field[r];
3013 
3014 		if (WACOM_PAD_FIELD(field))
3015 			pad_in_hid_field = true;
3016 		if (WACOM_PEN_FIELD(field))
3017 			pen_in_hid_field = true;
3018 		if (WACOM_FINGER_FIELD(field))
3019 			finger_in_hid_field = true;
3020 		if (wacom_equivalent_usage(field->physical) == HID_DG_TABLETFUNCTIONKEY)
3021 			true_pad = true;
3022 	}
3023 
3024 	wacom_wac_battery_pre_report(hdev, report);
3025 
3026 	if (pad_in_hid_field && wacom_wac->pad_input)
3027 		wacom_wac_pad_pre_report(hdev, report);
3028 	if (pen_in_hid_field && wacom_wac->pen_input)
3029 		wacom_wac_pen_pre_report(hdev, report);
3030 	if (finger_in_hid_field && wacom_wac->touch_input)
3031 		wacom_wac_finger_pre_report(hdev, report);
3032 
3033 	for (r = 0; r < report->maxfield; r++) {
3034 		field = report->field[r];
3035 
3036 		if (field->usage[0].collection_index != prev_collection) {
3037 			if (wacom_wac_collection(hdev, report,
3038 				field->usage[0].collection_index, field, r) < 0)
3039 				return;
3040 			prev_collection = field->usage[0].collection_index;
3041 		}
3042 	}
3043 
3044 	wacom_wac_battery_report(hdev, report);
3045 
3046 	if (true_pad && wacom_wac->pad_input)
3047 		wacom_wac_pad_report(hdev, report, field);
3048 }
3049 
wacom_bpt_touch(struct wacom_wac * wacom)3050 static int wacom_bpt_touch(struct wacom_wac *wacom)
3051 {
3052 	struct wacom_features *features = &wacom->features;
3053 	struct input_dev *input = wacom->touch_input;
3054 	struct input_dev *pad_input = wacom->pad_input;
3055 	unsigned char *data = wacom->data;
3056 	int i;
3057 
3058 	if (data[0] != 0x02)
3059 	    return 0;
3060 
3061 	for (i = 0; i < 2; i++) {
3062 		int offset = (data[1] & 0x80) ? (8 * i) : (9 * i);
3063 		bool touch = report_touch_events(wacom)
3064 			   && (data[offset + 3] & 0x80);
3065 
3066 		input_mt_slot(input, i);
3067 		input_mt_report_slot_state(input, MT_TOOL_FINGER, touch);
3068 		if (touch) {
3069 			int x = get_unaligned_be16(&data[offset + 3]) & 0x7ff;
3070 			int y = get_unaligned_be16(&data[offset + 5]) & 0x7ff;
3071 			if (features->quirks & WACOM_QUIRK_BBTOUCH_LOWRES) {
3072 				x <<= 5;
3073 				y <<= 5;
3074 			}
3075 			input_report_abs(input, ABS_MT_POSITION_X, x);
3076 			input_report_abs(input, ABS_MT_POSITION_Y, y);
3077 		}
3078 	}
3079 
3080 	input_mt_sync_frame(input);
3081 
3082 	input_report_key(pad_input, BTN_LEFT, (data[1] & 0x08) != 0);
3083 	input_report_key(pad_input, BTN_FORWARD, (data[1] & 0x04) != 0);
3084 	input_report_key(pad_input, BTN_BACK, (data[1] & 0x02) != 0);
3085 	input_report_key(pad_input, BTN_RIGHT, (data[1] & 0x01) != 0);
3086 	wacom->shared->touch_down = wacom_wac_finger_count_touches(wacom);
3087 
3088 	return 1;
3089 }
3090 
wacom_bpt3_touch_msg(struct wacom_wac * wacom,unsigned char * data)3091 static void wacom_bpt3_touch_msg(struct wacom_wac *wacom, unsigned char *data)
3092 {
3093 	struct wacom_features *features = &wacom->features;
3094 	struct input_dev *input = wacom->touch_input;
3095 	bool touch = data[1] & 0x80;
3096 	int slot = input_mt_get_slot_by_key(input, data[0]);
3097 
3098 	if (slot < 0)
3099 		return;
3100 
3101 	touch = touch && report_touch_events(wacom);
3102 
3103 	input_mt_slot(input, slot);
3104 	input_mt_report_slot_state(input, MT_TOOL_FINGER, touch);
3105 
3106 	if (touch) {
3107 		int x = (data[2] << 4) | (data[4] >> 4);
3108 		int y = (data[3] << 4) | (data[4] & 0x0f);
3109 		int width, height;
3110 
3111 		if (features->type >= INTUOSPS && features->type <= INTUOSHT2) {
3112 			width  = data[5] * 100;
3113 			height = data[6] * 100;
3114 		} else {
3115 			/*
3116 			 * "a" is a scaled-down area which we assume is
3117 			 * roughly circular and which can be described as:
3118 			 * a=(pi*r^2)/C.
3119 			 */
3120 			int a = data[5];
3121 			int x_res = input_abs_get_res(input, ABS_MT_POSITION_X);
3122 			int y_res = input_abs_get_res(input, ABS_MT_POSITION_Y);
3123 			width = 2 * int_sqrt(a * WACOM_CONTACT_AREA_SCALE);
3124 			height = width * y_res / x_res;
3125 		}
3126 
3127 		input_report_abs(input, ABS_MT_POSITION_X, x);
3128 		input_report_abs(input, ABS_MT_POSITION_Y, y);
3129 		input_report_abs(input, ABS_MT_TOUCH_MAJOR, width);
3130 		input_report_abs(input, ABS_MT_TOUCH_MINOR, height);
3131 	}
3132 }
3133 
wacom_bpt3_button_msg(struct wacom_wac * wacom,unsigned char * data)3134 static void wacom_bpt3_button_msg(struct wacom_wac *wacom, unsigned char *data)
3135 {
3136 	struct input_dev *input = wacom->pad_input;
3137 	struct wacom_features *features = &wacom->features;
3138 
3139 	if (features->type == INTUOSHT || features->type == INTUOSHT2) {
3140 		input_report_key(input, BTN_LEFT, (data[1] & 0x02) != 0);
3141 		input_report_key(input, BTN_BACK, (data[1] & 0x08) != 0);
3142 	} else {
3143 		input_report_key(input, BTN_BACK, (data[1] & 0x02) != 0);
3144 		input_report_key(input, BTN_LEFT, (data[1] & 0x08) != 0);
3145 	}
3146 	input_report_key(input, BTN_FORWARD, (data[1] & 0x04) != 0);
3147 	input_report_key(input, BTN_RIGHT, (data[1] & 0x01) != 0);
3148 }
3149 
wacom_bpt3_touch(struct wacom_wac * wacom)3150 static int wacom_bpt3_touch(struct wacom_wac *wacom)
3151 {
3152 	unsigned char *data = wacom->data;
3153 	int count = data[1] & 0x07;
3154 	int  touch_changed = 0, i;
3155 
3156 	if (data[0] != 0x02)
3157 	    return 0;
3158 
3159 	/* data has up to 7 fixed sized 8-byte messages starting at data[2] */
3160 	for (i = 0; i < count; i++) {
3161 		int offset = (8 * i) + 2;
3162 		int msg_id = data[offset];
3163 
3164 		if (msg_id >= 2 && msg_id <= 17) {
3165 			wacom_bpt3_touch_msg(wacom, data + offset);
3166 			touch_changed++;
3167 		} else if (msg_id == 128)
3168 			wacom_bpt3_button_msg(wacom, data + offset);
3169 
3170 	}
3171 
3172 	/* only update touch if we actually have a touchpad and touch data changed */
3173 	if (wacom->touch_input && touch_changed) {
3174 		input_mt_sync_frame(wacom->touch_input);
3175 		wacom->shared->touch_down = wacom_wac_finger_count_touches(wacom);
3176 	}
3177 
3178 	return 1;
3179 }
3180 
wacom_bpt_pen(struct wacom_wac * wacom)3181 static int wacom_bpt_pen(struct wacom_wac *wacom)
3182 {
3183 	struct wacom_features *features = &wacom->features;
3184 	struct input_dev *input = wacom->pen_input;
3185 	unsigned char *data = wacom->data;
3186 	int x = 0, y = 0, p = 0, d = 0;
3187 	bool pen = false, btn1 = false, btn2 = false;
3188 	bool range, prox, rdy;
3189 
3190 	if (data[0] != WACOM_REPORT_PENABLED)
3191 	    return 0;
3192 
3193 	range = (data[1] & 0x80) == 0x80;
3194 	prox = (data[1] & 0x40) == 0x40;
3195 	rdy = (data[1] & 0x20) == 0x20;
3196 
3197 	wacom->shared->stylus_in_proximity = range;
3198 	if (delay_pen_events(wacom))
3199 		return 0;
3200 
3201 	if (rdy) {
3202 		p = le16_to_cpup((__le16 *)&data[6]);
3203 		pen = data[1] & 0x01;
3204 		btn1 = data[1] & 0x02;
3205 		btn2 = data[1] & 0x04;
3206 	}
3207 	if (prox) {
3208 		x = le16_to_cpup((__le16 *)&data[2]);
3209 		y = le16_to_cpup((__le16 *)&data[4]);
3210 
3211 		if (data[1] & 0x08) {
3212 			wacom->tool[0] = BTN_TOOL_RUBBER;
3213 			wacom->id[0] = ERASER_DEVICE_ID;
3214 		} else {
3215 			wacom->tool[0] = BTN_TOOL_PEN;
3216 			wacom->id[0] = STYLUS_DEVICE_ID;
3217 		}
3218 		wacom->reporting_data = true;
3219 	}
3220 	if (range) {
3221 		/*
3222 		 * Convert distance from out prox to distance from tablet.
3223 		 * distance will be greater than distance_max once
3224 		 * touching and applying pressure; do not report negative
3225 		 * distance.
3226 		 */
3227 		if (data[8] <= features->distance_max)
3228 			d = features->distance_max - data[8];
3229 	} else {
3230 		wacom->id[0] = 0;
3231 	}
3232 
3233 	if (wacom->reporting_data) {
3234 		input_report_key(input, BTN_TOUCH, pen);
3235 		input_report_key(input, BTN_STYLUS, btn1);
3236 		input_report_key(input, BTN_STYLUS2, btn2);
3237 
3238 		if (prox || !range) {
3239 			input_report_abs(input, ABS_X, x);
3240 			input_report_abs(input, ABS_Y, y);
3241 		}
3242 		input_report_abs(input, ABS_PRESSURE, p);
3243 		input_report_abs(input, ABS_DISTANCE, d);
3244 
3245 		input_report_key(input, wacom->tool[0], range); /* PEN or RUBBER */
3246 		input_report_abs(input, ABS_MISC, wacom->id[0]); /* TOOL ID */
3247 	}
3248 
3249 	if (!range) {
3250 		wacom->reporting_data = false;
3251 	}
3252 
3253 	return 1;
3254 }
3255 
wacom_bpt_irq(struct wacom_wac * wacom,size_t len)3256 static int wacom_bpt_irq(struct wacom_wac *wacom, size_t len)
3257 {
3258 	struct wacom_features *features = &wacom->features;
3259 
3260 	if ((features->type == INTUOSHT2) &&
3261 	    (features->device_type & WACOM_DEVICETYPE_PEN))
3262 		return wacom_intuos_irq(wacom);
3263 	else if (len == WACOM_PKGLEN_BBTOUCH)
3264 		return wacom_bpt_touch(wacom);
3265 	else if (len == WACOM_PKGLEN_BBTOUCH3)
3266 		return wacom_bpt3_touch(wacom);
3267 	else if (len == WACOM_PKGLEN_BBFUN || len == WACOM_PKGLEN_BBPEN)
3268 		return wacom_bpt_pen(wacom);
3269 
3270 	return 0;
3271 }
3272 
wacom_bamboo_pad_pen_event(struct wacom_wac * wacom,unsigned char * data)3273 static void wacom_bamboo_pad_pen_event(struct wacom_wac *wacom,
3274 		unsigned char *data)
3275 {
3276 	unsigned char prefix;
3277 
3278 	/*
3279 	 * We need to reroute the event from the debug interface to the
3280 	 * pen interface.
3281 	 * We need to add the report ID to the actual pen report, so we
3282 	 * temporary overwrite the first byte to prevent having to kzalloc/kfree
3283 	 * and memcpy the report.
3284 	 */
3285 	prefix = data[0];
3286 	data[0] = WACOM_REPORT_BPAD_PEN;
3287 
3288 	/*
3289 	 * actually reroute the event.
3290 	 * No need to check if wacom->shared->pen is valid, hid_input_report()
3291 	 * will check for us.
3292 	 */
3293 	hid_input_report(wacom->shared->pen, HID_INPUT_REPORT, data,
3294 			 WACOM_PKGLEN_PENABLED, 1);
3295 
3296 	data[0] = prefix;
3297 }
3298 
wacom_bamboo_pad_touch_event(struct wacom_wac * wacom,unsigned char * data)3299 static int wacom_bamboo_pad_touch_event(struct wacom_wac *wacom,
3300 		unsigned char *data)
3301 {
3302 	struct input_dev *input = wacom->touch_input;
3303 	unsigned char *finger_data, prefix;
3304 	unsigned id;
3305 	int x, y;
3306 	bool valid;
3307 
3308 	prefix = data[0];
3309 
3310 	for (id = 0; id < wacom->features.touch_max; id++) {
3311 		valid = !!(prefix & BIT(id)) &&
3312 			report_touch_events(wacom);
3313 
3314 		input_mt_slot(input, id);
3315 		input_mt_report_slot_state(input, MT_TOOL_FINGER, valid);
3316 
3317 		if (!valid)
3318 			continue;
3319 
3320 		finger_data = data + 1 + id * 3;
3321 		x = finger_data[0] | ((finger_data[1] & 0x0f) << 8);
3322 		y = (finger_data[2] << 4) | (finger_data[1] >> 4);
3323 
3324 		input_report_abs(input, ABS_MT_POSITION_X, x);
3325 		input_report_abs(input, ABS_MT_POSITION_Y, y);
3326 	}
3327 
3328 	input_mt_sync_frame(input);
3329 
3330 	input_report_key(input, BTN_LEFT, prefix & 0x40);
3331 	input_report_key(input, BTN_RIGHT, prefix & 0x80);
3332 
3333 	/* keep touch state for pen event */
3334 	wacom->shared->touch_down = !!prefix && report_touch_events(wacom);
3335 
3336 	return 1;
3337 }
3338 
wacom_bamboo_pad_irq(struct wacom_wac * wacom,size_t len)3339 static int wacom_bamboo_pad_irq(struct wacom_wac *wacom, size_t len)
3340 {
3341 	unsigned char *data = wacom->data;
3342 
3343 	if (!((len == WACOM_PKGLEN_BPAD_TOUCH) ||
3344 	      (len == WACOM_PKGLEN_BPAD_TOUCH_USB)) ||
3345 	    (data[0] != WACOM_REPORT_BPAD_TOUCH))
3346 		return 0;
3347 
3348 	if (data[1] & 0x01)
3349 		wacom_bamboo_pad_pen_event(wacom, &data[1]);
3350 
3351 	if (data[1] & 0x02)
3352 		return wacom_bamboo_pad_touch_event(wacom, &data[9]);
3353 
3354 	return 0;
3355 }
3356 
wacom_wireless_irq(struct wacom_wac * wacom,size_t len)3357 static int wacom_wireless_irq(struct wacom_wac *wacom, size_t len)
3358 {
3359 	unsigned char *data = wacom->data;
3360 	int connected;
3361 
3362 	if (len != WACOM_PKGLEN_WIRELESS || data[0] != WACOM_REPORT_WL)
3363 		return 0;
3364 
3365 	connected = data[1] & 0x01;
3366 	if (connected) {
3367 		int pid, battery, charging;
3368 
3369 		if ((wacom->shared->type == INTUOSHT ||
3370 		    wacom->shared->type == INTUOSHT2) &&
3371 		    wacom->shared->touch_input &&
3372 		    wacom->shared->touch_max) {
3373 			input_report_switch(wacom->shared->touch_input,
3374 					SW_MUTE_DEVICE, data[5] & 0x40);
3375 			input_sync(wacom->shared->touch_input);
3376 		}
3377 
3378 		pid = get_unaligned_be16(&data[6]);
3379 		battery = (data[5] & 0x3f) * 100 / 31;
3380 		charging = !!(data[5] & 0x80);
3381 		if (wacom->pid != pid) {
3382 			wacom->pid = pid;
3383 			wacom_schedule_work(wacom, WACOM_WORKER_WIRELESS);
3384 		}
3385 
3386 		wacom_notify_battery(wacom, WACOM_POWER_SUPPLY_STATUS_AUTO,
3387 				     battery, charging, 1, 0);
3388 
3389 	} else if (wacom->pid != 0) {
3390 		/* disconnected while previously connected */
3391 		wacom->pid = 0;
3392 		wacom_schedule_work(wacom, WACOM_WORKER_WIRELESS);
3393 		wacom_notify_battery(wacom, POWER_SUPPLY_STATUS_UNKNOWN, 0, 0, 0, 0);
3394 	}
3395 
3396 	return 0;
3397 }
3398 
wacom_status_irq(struct wacom_wac * wacom_wac,size_t len)3399 static int wacom_status_irq(struct wacom_wac *wacom_wac, size_t len)
3400 {
3401 	struct wacom *wacom = container_of(wacom_wac, struct wacom, wacom_wac);
3402 	struct wacom_features *features = &wacom_wac->features;
3403 	unsigned char *data = wacom_wac->data;
3404 
3405 	if (data[0] != WACOM_REPORT_USB)
3406 		return 0;
3407 
3408 	if ((features->type == INTUOSHT ||
3409 	    features->type == INTUOSHT2) &&
3410 	    wacom_wac->shared->touch_input &&
3411 	    features->touch_max) {
3412 		input_report_switch(wacom_wac->shared->touch_input,
3413 				    SW_MUTE_DEVICE, data[8] & 0x40);
3414 		input_sync(wacom_wac->shared->touch_input);
3415 	}
3416 
3417 	if (data[9] & 0x02) { /* wireless module is attached */
3418 		int battery = (data[8] & 0x3f) * 100 / 31;
3419 		bool charging = !!(data[8] & 0x80);
3420 
3421 		features->quirks |= WACOM_QUIRK_BATTERY;
3422 		wacom_notify_battery(wacom_wac, WACOM_POWER_SUPPLY_STATUS_AUTO,
3423 				     battery, charging, battery || charging, 1);
3424 	}
3425 	else if ((features->quirks & WACOM_QUIRK_BATTERY) &&
3426 		 wacom->battery.battery) {
3427 		features->quirks &= ~WACOM_QUIRK_BATTERY;
3428 		wacom_notify_battery(wacom_wac, POWER_SUPPLY_STATUS_UNKNOWN, 0, 0, 0, 0);
3429 	}
3430 	return 0;
3431 }
3432 
wacom_wac_irq(struct wacom_wac * wacom_wac,size_t len)3433 void wacom_wac_irq(struct wacom_wac *wacom_wac, size_t len)
3434 {
3435 	bool sync;
3436 
3437 	switch (wacom_wac->features.type) {
3438 	case PENPARTNER:
3439 		sync = wacom_penpartner_irq(wacom_wac);
3440 		break;
3441 
3442 	case PL:
3443 		sync = wacom_pl_irq(wacom_wac);
3444 		break;
3445 
3446 	case WACOM_G4:
3447 	case GRAPHIRE:
3448 	case GRAPHIRE_BT:
3449 	case WACOM_MO:
3450 		sync = wacom_graphire_irq(wacom_wac);
3451 		break;
3452 
3453 	case PTU:
3454 		sync = wacom_ptu_irq(wacom_wac);
3455 		break;
3456 
3457 	case DTU:
3458 		sync = wacom_dtu_irq(wacom_wac);
3459 		break;
3460 
3461 	case DTUS:
3462 	case DTUSX:
3463 		sync = wacom_dtus_irq(wacom_wac);
3464 		break;
3465 
3466 	case INTUOS:
3467 	case INTUOS3S:
3468 	case INTUOS3:
3469 	case INTUOS3L:
3470 	case INTUOS4S:
3471 	case INTUOS4:
3472 	case INTUOS4L:
3473 	case CINTIQ:
3474 	case WACOM_BEE:
3475 	case WACOM_13HD:
3476 	case WACOM_21UX2:
3477 	case WACOM_22HD:
3478 	case WACOM_24HD:
3479 	case WACOM_27QHD:
3480 	case DTK:
3481 	case CINTIQ_HYBRID:
3482 	case CINTIQ_COMPANION_2:
3483 		sync = wacom_intuos_irq(wacom_wac);
3484 		break;
3485 
3486 	case INTUOS4WL:
3487 		sync = wacom_intuos_bt_irq(wacom_wac, len);
3488 		break;
3489 
3490 	case WACOM_24HDT:
3491 	case WACOM_27QHDT:
3492 		sync = wacom_24hdt_irq(wacom_wac);
3493 		break;
3494 
3495 	case INTUOS5S:
3496 	case INTUOS5:
3497 	case INTUOS5L:
3498 	case INTUOSPS:
3499 	case INTUOSPM:
3500 	case INTUOSPL:
3501 		if (len == WACOM_PKGLEN_BBTOUCH3)
3502 			sync = wacom_bpt3_touch(wacom_wac);
3503 		else if (wacom_wac->data[0] == WACOM_REPORT_USB)
3504 			sync = wacom_status_irq(wacom_wac, len);
3505 		else
3506 			sync = wacom_intuos_irq(wacom_wac);
3507 		break;
3508 
3509 	case INTUOSP2_BT:
3510 	case INTUOSP2S_BT:
3511 	case INTUOSHT3_BT:
3512 		sync = wacom_intuos_pro2_bt_irq(wacom_wac, len);
3513 		break;
3514 
3515 	case TABLETPC:
3516 	case TABLETPCE:
3517 	case TABLETPC2FG:
3518 	case MTSCREEN:
3519 	case MTTPC:
3520 	case MTTPC_B:
3521 		sync = wacom_tpc_irq(wacom_wac, len);
3522 		break;
3523 
3524 	case BAMBOO_PT:
3525 	case BAMBOO_PEN:
3526 	case BAMBOO_TOUCH:
3527 	case INTUOSHT:
3528 	case INTUOSHT2:
3529 		if (wacom_wac->data[0] == WACOM_REPORT_USB)
3530 			sync = wacom_status_irq(wacom_wac, len);
3531 		else
3532 			sync = wacom_bpt_irq(wacom_wac, len);
3533 		break;
3534 
3535 	case BAMBOO_PAD:
3536 		sync = wacom_bamboo_pad_irq(wacom_wac, len);
3537 		break;
3538 
3539 	case WIRELESS:
3540 		sync = wacom_wireless_irq(wacom_wac, len);
3541 		break;
3542 
3543 	case REMOTE:
3544 		sync = false;
3545 		if (wacom_wac->data[0] == WACOM_REPORT_DEVICE_LIST)
3546 			wacom_remote_status_irq(wacom_wac, len);
3547 		else
3548 			sync = wacom_remote_irq(wacom_wac, len);
3549 		break;
3550 
3551 	default:
3552 		sync = false;
3553 		break;
3554 	}
3555 
3556 	if (sync) {
3557 		if (wacom_wac->pen_input)
3558 			input_sync(wacom_wac->pen_input);
3559 		if (wacom_wac->touch_input)
3560 			input_sync(wacom_wac->touch_input);
3561 		if (wacom_wac->pad_input)
3562 			input_sync(wacom_wac->pad_input);
3563 	}
3564 }
3565 
wacom_setup_basic_pro_pen(struct wacom_wac * wacom_wac)3566 static void wacom_setup_basic_pro_pen(struct wacom_wac *wacom_wac)
3567 {
3568 	struct input_dev *input_dev = wacom_wac->pen_input;
3569 
3570 	input_set_capability(input_dev, EV_MSC, MSC_SERIAL);
3571 
3572 	__set_bit(BTN_TOOL_PEN, input_dev->keybit);
3573 	__set_bit(BTN_STYLUS, input_dev->keybit);
3574 	__set_bit(BTN_STYLUS2, input_dev->keybit);
3575 
3576 	input_set_abs_params(input_dev, ABS_DISTANCE,
3577 			     0, wacom_wac->features.distance_max, wacom_wac->features.distance_fuzz, 0);
3578 }
3579 
wacom_setup_cintiq(struct wacom_wac * wacom_wac)3580 static void wacom_setup_cintiq(struct wacom_wac *wacom_wac)
3581 {
3582 	struct input_dev *input_dev = wacom_wac->pen_input;
3583 	struct wacom_features *features = &wacom_wac->features;
3584 
3585 	wacom_setup_basic_pro_pen(wacom_wac);
3586 
3587 	__set_bit(BTN_TOOL_RUBBER, input_dev->keybit);
3588 	__set_bit(BTN_TOOL_BRUSH, input_dev->keybit);
3589 	__set_bit(BTN_TOOL_PENCIL, input_dev->keybit);
3590 	__set_bit(BTN_TOOL_AIRBRUSH, input_dev->keybit);
3591 
3592 	input_set_abs_params(input_dev, ABS_WHEEL, 0, 1023, 0, 0);
3593 	input_set_abs_params(input_dev, ABS_TILT_X, -64, 63, features->tilt_fuzz, 0);
3594 	input_abs_set_res(input_dev, ABS_TILT_X, 57);
3595 	input_set_abs_params(input_dev, ABS_TILT_Y, -64, 63, features->tilt_fuzz, 0);
3596 	input_abs_set_res(input_dev, ABS_TILT_Y, 57);
3597 }
3598 
wacom_setup_intuos(struct wacom_wac * wacom_wac)3599 static void wacom_setup_intuos(struct wacom_wac *wacom_wac)
3600 {
3601 	struct input_dev *input_dev = wacom_wac->pen_input;
3602 
3603 	input_set_capability(input_dev, EV_REL, REL_WHEEL);
3604 
3605 	wacom_setup_cintiq(wacom_wac);
3606 
3607 	__set_bit(BTN_LEFT, input_dev->keybit);
3608 	__set_bit(BTN_RIGHT, input_dev->keybit);
3609 	__set_bit(BTN_MIDDLE, input_dev->keybit);
3610 	__set_bit(BTN_SIDE, input_dev->keybit);
3611 	__set_bit(BTN_EXTRA, input_dev->keybit);
3612 	__set_bit(BTN_TOOL_MOUSE, input_dev->keybit);
3613 	__set_bit(BTN_TOOL_LENS, input_dev->keybit);
3614 
3615 	input_set_abs_params(input_dev, ABS_RZ, -900, 899, 0, 0);
3616 	input_abs_set_res(input_dev, ABS_RZ, 287);
3617 	input_set_abs_params(input_dev, ABS_THROTTLE, -1023, 1023, 0, 0);
3618 }
3619 
wacom_setup_device_quirks(struct wacom * wacom)3620 void wacom_setup_device_quirks(struct wacom *wacom)
3621 {
3622 	struct wacom_wac *wacom_wac = &wacom->wacom_wac;
3623 	struct wacom_features *features = &wacom->wacom_wac.features;
3624 
3625 	/* The pen and pad share the same interface on most devices */
3626 	if (features->type == GRAPHIRE_BT || features->type == WACOM_G4 ||
3627 	    features->type == DTUS ||
3628 	    (features->type >= INTUOS3S && features->type <= WACOM_MO)) {
3629 		if (features->device_type & WACOM_DEVICETYPE_PEN)
3630 			features->device_type |= WACOM_DEVICETYPE_PAD;
3631 	}
3632 
3633 	/* touch device found but size is not defined. use default */
3634 	if (features->device_type & WACOM_DEVICETYPE_TOUCH && !features->x_max) {
3635 		features->x_max = 1023;
3636 		features->y_max = 1023;
3637 	}
3638 
3639 	/*
3640 	 * Intuos5/Pro and Bamboo 3rd gen have no useful data about its
3641 	 * touch interface in its HID descriptor. If this is the touch
3642 	 * interface (PacketSize of WACOM_PKGLEN_BBTOUCH3), override the
3643 	 * tablet values.
3644 	 */
3645 	if ((features->type >= INTUOS5S && features->type <= INTUOSPL) ||
3646 		(features->type >= INTUOSHT && features->type <= BAMBOO_PT)) {
3647 		if (features->pktlen == WACOM_PKGLEN_BBTOUCH3) {
3648 			if (features->touch_max)
3649 				features->device_type |= WACOM_DEVICETYPE_TOUCH;
3650 			if (features->type >= INTUOSHT && features->type <= BAMBOO_PT)
3651 				features->device_type |= WACOM_DEVICETYPE_PAD;
3652 
3653 			if (features->type == INTUOSHT2) {
3654 				features->x_max = features->x_max / 10;
3655 				features->y_max = features->y_max / 10;
3656 			}
3657 			else {
3658 				features->x_max = 4096;
3659 				features->y_max = 4096;
3660 			}
3661 		}
3662 		else if (features->pktlen == WACOM_PKGLEN_BBTOUCH) {
3663 			features->device_type |= WACOM_DEVICETYPE_PAD;
3664 		}
3665 	}
3666 
3667 	/*
3668 	 * Hack for the Bamboo One:
3669 	 * the device presents a PAD/Touch interface as most Bamboos and even
3670 	 * sends ghosts PAD data on it. However, later, we must disable this
3671 	 * ghost interface, and we can not detect it unless we set it here
3672 	 * to WACOM_DEVICETYPE_PAD or WACOM_DEVICETYPE_TOUCH.
3673 	 */
3674 	if (features->type == BAMBOO_PEN &&
3675 	    features->pktlen == WACOM_PKGLEN_BBTOUCH3)
3676 		features->device_type |= WACOM_DEVICETYPE_PAD;
3677 
3678 	/*
3679 	 * Raw Wacom-mode pen and touch events both come from interface
3680 	 * 0, whose HID descriptor has an application usage of 0xFF0D
3681 	 * (i.e., WACOM_HID_WD_DIGITIZER). We route pen packets back
3682 	 * out through the HID_GENERIC device created for interface 1,
3683 	 * so rewrite this one to be of type WACOM_DEVICETYPE_TOUCH.
3684 	 */
3685 	if (features->type == BAMBOO_PAD)
3686 		features->device_type = WACOM_DEVICETYPE_TOUCH;
3687 
3688 	if (features->type == REMOTE)
3689 		features->device_type = WACOM_DEVICETYPE_PAD;
3690 
3691 	if (features->type == INTUOSP2_BT ||
3692 	    features->type == INTUOSP2S_BT) {
3693 		features->device_type |= WACOM_DEVICETYPE_PEN |
3694 					 WACOM_DEVICETYPE_PAD |
3695 					 WACOM_DEVICETYPE_TOUCH;
3696 		features->quirks |= WACOM_QUIRK_BATTERY;
3697 	}
3698 
3699 	if (features->type == INTUOSHT3_BT) {
3700 		features->device_type |= WACOM_DEVICETYPE_PEN |
3701 					 WACOM_DEVICETYPE_PAD;
3702 		features->quirks |= WACOM_QUIRK_BATTERY;
3703 	}
3704 
3705 	switch (features->type) {
3706 	case PL:
3707 	case DTU:
3708 	case DTUS:
3709 	case DTUSX:
3710 	case WACOM_21UX2:
3711 	case WACOM_22HD:
3712 	case DTK:
3713 	case WACOM_24HD:
3714 	case WACOM_27QHD:
3715 	case CINTIQ_HYBRID:
3716 	case CINTIQ_COMPANION_2:
3717 	case CINTIQ:
3718 	case WACOM_BEE:
3719 	case WACOM_13HD:
3720 	case WACOM_24HDT:
3721 	case WACOM_27QHDT:
3722 	case TABLETPC:
3723 	case TABLETPCE:
3724 	case TABLETPC2FG:
3725 	case MTSCREEN:
3726 	case MTTPC:
3727 	case MTTPC_B:
3728 		features->device_type |= WACOM_DEVICETYPE_DIRECT;
3729 		break;
3730 	}
3731 
3732 	if (wacom->hdev->bus == BUS_BLUETOOTH)
3733 		features->quirks |= WACOM_QUIRK_BATTERY;
3734 
3735 	/* quirk for bamboo touch with 2 low res touches */
3736 	if ((features->type == BAMBOO_PT || features->type == BAMBOO_TOUCH) &&
3737 	    features->pktlen == WACOM_PKGLEN_BBTOUCH) {
3738 		features->x_max <<= 5;
3739 		features->y_max <<= 5;
3740 		features->x_fuzz <<= 5;
3741 		features->y_fuzz <<= 5;
3742 		features->quirks |= WACOM_QUIRK_BBTOUCH_LOWRES;
3743 	}
3744 
3745 	if (features->type == WIRELESS) {
3746 		if (features->device_type == WACOM_DEVICETYPE_WL_MONITOR) {
3747 			features->quirks |= WACOM_QUIRK_BATTERY;
3748 		}
3749 	}
3750 
3751 	if (features->type == REMOTE)
3752 		features->device_type |= WACOM_DEVICETYPE_WL_MONITOR;
3753 
3754 	/* HID descriptor for DTK-2451 / DTH-2452 claims to report lots
3755 	 * of things it shouldn't. Lets fix up the damage...
3756 	 */
3757 	if (wacom->hdev->product == 0x382 || wacom->hdev->product == 0x37d) {
3758 		features->quirks &= ~WACOM_QUIRK_TOOLSERIAL;
3759 		__clear_bit(BTN_TOOL_BRUSH, wacom_wac->pen_input->keybit);
3760 		__clear_bit(BTN_TOOL_PENCIL, wacom_wac->pen_input->keybit);
3761 		__clear_bit(BTN_TOOL_AIRBRUSH, wacom_wac->pen_input->keybit);
3762 		__clear_bit(ABS_Z, wacom_wac->pen_input->absbit);
3763 		__clear_bit(ABS_DISTANCE, wacom_wac->pen_input->absbit);
3764 		__clear_bit(ABS_TILT_X, wacom_wac->pen_input->absbit);
3765 		__clear_bit(ABS_TILT_Y, wacom_wac->pen_input->absbit);
3766 		__clear_bit(ABS_WHEEL, wacom_wac->pen_input->absbit);
3767 		__clear_bit(ABS_MISC, wacom_wac->pen_input->absbit);
3768 		__clear_bit(MSC_SERIAL, wacom_wac->pen_input->mscbit);
3769 		__clear_bit(EV_MSC, wacom_wac->pen_input->evbit);
3770 	}
3771 }
3772 
wacom_setup_pen_input_capabilities(struct input_dev * input_dev,struct wacom_wac * wacom_wac)3773 int wacom_setup_pen_input_capabilities(struct input_dev *input_dev,
3774 				   struct wacom_wac *wacom_wac)
3775 {
3776 	struct wacom_features *features = &wacom_wac->features;
3777 
3778 	if (!(features->device_type & WACOM_DEVICETYPE_PEN))
3779 		return -ENODEV;
3780 
3781 	if (features->device_type & WACOM_DEVICETYPE_DIRECT)
3782 		__set_bit(INPUT_PROP_DIRECT, input_dev->propbit);
3783 	else
3784 		__set_bit(INPUT_PROP_POINTER, input_dev->propbit);
3785 
3786 	if (features->type == HID_GENERIC)
3787 		/* setup has already been done */
3788 		return 0;
3789 
3790 	input_dev->evbit[0] |= BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
3791 	__set_bit(BTN_TOUCH, input_dev->keybit);
3792 	__set_bit(ABS_MISC, input_dev->absbit);
3793 
3794 	input_set_abs_params(input_dev, ABS_X, 0 + features->offset_left,
3795 			     features->x_max - features->offset_right,
3796 			     features->x_fuzz, 0);
3797 	input_set_abs_params(input_dev, ABS_Y, 0 + features->offset_top,
3798 			     features->y_max - features->offset_bottom,
3799 			     features->y_fuzz, 0);
3800 	input_set_abs_params(input_dev, ABS_PRESSURE, 0,
3801 		features->pressure_max, features->pressure_fuzz, 0);
3802 
3803 	/* penabled devices have fixed resolution for each model */
3804 	input_abs_set_res(input_dev, ABS_X, features->x_resolution);
3805 	input_abs_set_res(input_dev, ABS_Y, features->y_resolution);
3806 
3807 	switch (features->type) {
3808 	case GRAPHIRE_BT:
3809 		__clear_bit(ABS_MISC, input_dev->absbit);
3810 		fallthrough;
3811 
3812 	case WACOM_MO:
3813 	case WACOM_G4:
3814 		input_set_abs_params(input_dev, ABS_DISTANCE, 0,
3815 					      features->distance_max,
3816 					      features->distance_fuzz, 0);
3817 		fallthrough;
3818 
3819 	case GRAPHIRE:
3820 		input_set_capability(input_dev, EV_REL, REL_WHEEL);
3821 
3822 		__set_bit(BTN_LEFT, input_dev->keybit);
3823 		__set_bit(BTN_RIGHT, input_dev->keybit);
3824 		__set_bit(BTN_MIDDLE, input_dev->keybit);
3825 
3826 		__set_bit(BTN_TOOL_RUBBER, input_dev->keybit);
3827 		__set_bit(BTN_TOOL_PEN, input_dev->keybit);
3828 		__set_bit(BTN_TOOL_MOUSE, input_dev->keybit);
3829 		__set_bit(BTN_STYLUS, input_dev->keybit);
3830 		__set_bit(BTN_STYLUS2, input_dev->keybit);
3831 		break;
3832 
3833 	case WACOM_27QHD:
3834 	case WACOM_24HD:
3835 	case DTK:
3836 	case WACOM_22HD:
3837 	case WACOM_21UX2:
3838 	case WACOM_BEE:
3839 	case CINTIQ:
3840 	case WACOM_13HD:
3841 	case CINTIQ_HYBRID:
3842 	case CINTIQ_COMPANION_2:
3843 		input_set_abs_params(input_dev, ABS_Z, -900, 899, 0, 0);
3844 		input_abs_set_res(input_dev, ABS_Z, 287);
3845 		wacom_setup_cintiq(wacom_wac);
3846 		break;
3847 
3848 	case INTUOS3:
3849 	case INTUOS3L:
3850 	case INTUOS3S:
3851 	case INTUOS4:
3852 	case INTUOS4WL:
3853 	case INTUOS4L:
3854 	case INTUOS4S:
3855 		input_set_abs_params(input_dev, ABS_Z, -900, 899, 0, 0);
3856 		input_abs_set_res(input_dev, ABS_Z, 287);
3857 		fallthrough;
3858 
3859 	case INTUOS:
3860 		wacom_setup_intuos(wacom_wac);
3861 		break;
3862 
3863 	case INTUOS5:
3864 	case INTUOS5L:
3865 	case INTUOSPM:
3866 	case INTUOSPL:
3867 	case INTUOS5S:
3868 	case INTUOSPS:
3869 	case INTUOSP2_BT:
3870 	case INTUOSP2S_BT:
3871 		input_set_abs_params(input_dev, ABS_DISTANCE, 0,
3872 				      features->distance_max,
3873 				      features->distance_fuzz, 0);
3874 
3875 		input_set_abs_params(input_dev, ABS_Z, -900, 899, 0, 0);
3876 		input_abs_set_res(input_dev, ABS_Z, 287);
3877 
3878 		wacom_setup_intuos(wacom_wac);
3879 		break;
3880 
3881 	case WACOM_24HDT:
3882 	case WACOM_27QHDT:
3883 	case MTSCREEN:
3884 	case MTTPC:
3885 	case MTTPC_B:
3886 	case TABLETPC2FG:
3887 	case TABLETPC:
3888 	case TABLETPCE:
3889 		__clear_bit(ABS_MISC, input_dev->absbit);
3890 		fallthrough;
3891 
3892 	case DTUS:
3893 	case DTUSX:
3894 	case PL:
3895 	case DTU:
3896 		__set_bit(BTN_TOOL_PEN, input_dev->keybit);
3897 		__set_bit(BTN_TOOL_RUBBER, input_dev->keybit);
3898 		__set_bit(BTN_STYLUS, input_dev->keybit);
3899 		__set_bit(BTN_STYLUS2, input_dev->keybit);
3900 		break;
3901 
3902 	case PTU:
3903 		__set_bit(BTN_STYLUS2, input_dev->keybit);
3904 		fallthrough;
3905 
3906 	case PENPARTNER:
3907 		__set_bit(BTN_TOOL_PEN, input_dev->keybit);
3908 		__set_bit(BTN_TOOL_RUBBER, input_dev->keybit);
3909 		__set_bit(BTN_STYLUS, input_dev->keybit);
3910 		break;
3911 
3912 	case INTUOSHT:
3913 	case BAMBOO_PT:
3914 	case BAMBOO_PEN:
3915 	case INTUOSHT2:
3916 	case INTUOSHT3_BT:
3917 		if (features->type == INTUOSHT2 ||
3918 		    features->type == INTUOSHT3_BT) {
3919 			wacom_setup_basic_pro_pen(wacom_wac);
3920 		} else {
3921 			__clear_bit(ABS_MISC, input_dev->absbit);
3922 			__set_bit(BTN_TOOL_PEN, input_dev->keybit);
3923 			__set_bit(BTN_TOOL_RUBBER, input_dev->keybit);
3924 			__set_bit(BTN_STYLUS, input_dev->keybit);
3925 			__set_bit(BTN_STYLUS2, input_dev->keybit);
3926 			input_set_abs_params(input_dev, ABS_DISTANCE, 0,
3927 				      features->distance_max,
3928 				      features->distance_fuzz, 0);
3929 		}
3930 		break;
3931 	case BAMBOO_PAD:
3932 		__clear_bit(ABS_MISC, input_dev->absbit);
3933 		break;
3934 	}
3935 	return 0;
3936 }
3937 
wacom_setup_touch_input_capabilities(struct input_dev * input_dev,struct wacom_wac * wacom_wac)3938 int wacom_setup_touch_input_capabilities(struct input_dev *input_dev,
3939 					 struct wacom_wac *wacom_wac)
3940 {
3941 	struct wacom_features *features = &wacom_wac->features;
3942 
3943 	if (!(features->device_type & WACOM_DEVICETYPE_TOUCH))
3944 		return -ENODEV;
3945 
3946 	if (features->device_type & WACOM_DEVICETYPE_DIRECT)
3947 		__set_bit(INPUT_PROP_DIRECT, input_dev->propbit);
3948 	else
3949 		__set_bit(INPUT_PROP_POINTER, input_dev->propbit);
3950 
3951 	if (features->type == HID_GENERIC)
3952 		/* setup has already been done */
3953 		return 0;
3954 
3955 	input_dev->evbit[0] |= BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
3956 	__set_bit(BTN_TOUCH, input_dev->keybit);
3957 
3958 	if (features->touch_max == 1) {
3959 		input_set_abs_params(input_dev, ABS_X, 0,
3960 			features->x_max, features->x_fuzz, 0);
3961 		input_set_abs_params(input_dev, ABS_Y, 0,
3962 			features->y_max, features->y_fuzz, 0);
3963 		input_abs_set_res(input_dev, ABS_X,
3964 				  features->x_resolution);
3965 		input_abs_set_res(input_dev, ABS_Y,
3966 				  features->y_resolution);
3967 	}
3968 	else if (features->touch_max > 1) {
3969 		input_set_abs_params(input_dev, ABS_MT_POSITION_X, 0,
3970 			features->x_max, features->x_fuzz, 0);
3971 		input_set_abs_params(input_dev, ABS_MT_POSITION_Y, 0,
3972 			features->y_max, features->y_fuzz, 0);
3973 		input_abs_set_res(input_dev, ABS_MT_POSITION_X,
3974 				  features->x_resolution);
3975 		input_abs_set_res(input_dev, ABS_MT_POSITION_Y,
3976 				  features->y_resolution);
3977 	}
3978 
3979 	switch (features->type) {
3980 	case INTUOSP2_BT:
3981 	case INTUOSP2S_BT:
3982 		input_dev->evbit[0] |= BIT_MASK(EV_SW);
3983 		__set_bit(SW_MUTE_DEVICE, input_dev->swbit);
3984 
3985 		if (wacom_wac->shared->touch->product == 0x361) {
3986 			input_set_abs_params(input_dev, ABS_MT_POSITION_X,
3987 					     0, 12440, 4, 0);
3988 			input_set_abs_params(input_dev, ABS_MT_POSITION_Y,
3989 					     0, 8640, 4, 0);
3990 		}
3991 		else if (wacom_wac->shared->touch->product == 0x360) {
3992 			input_set_abs_params(input_dev, ABS_MT_POSITION_X,
3993 					     0, 8960, 4, 0);
3994 			input_set_abs_params(input_dev, ABS_MT_POSITION_Y,
3995 					     0, 5920, 4, 0);
3996 		}
3997 		else if (wacom_wac->shared->touch->product == 0x393) {
3998 			input_set_abs_params(input_dev, ABS_MT_POSITION_X,
3999 					     0, 6400, 4, 0);
4000 			input_set_abs_params(input_dev, ABS_MT_POSITION_Y,
4001 					     0, 4000, 4, 0);
4002 		}
4003 		input_abs_set_res(input_dev, ABS_MT_POSITION_X, 40);
4004 		input_abs_set_res(input_dev, ABS_MT_POSITION_Y, 40);
4005 
4006 		fallthrough;
4007 
4008 	case INTUOS5:
4009 	case INTUOS5L:
4010 	case INTUOSPM:
4011 	case INTUOSPL:
4012 	case INTUOS5S:
4013 	case INTUOSPS:
4014 		input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR, 0, features->x_max, 0, 0);
4015 		input_set_abs_params(input_dev, ABS_MT_TOUCH_MINOR, 0, features->y_max, 0, 0);
4016 		input_mt_init_slots(input_dev, features->touch_max, INPUT_MT_POINTER);
4017 		break;
4018 
4019 	case WACOM_24HDT:
4020 		input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR, 0, features->x_max, 0, 0);
4021 		input_set_abs_params(input_dev, ABS_MT_WIDTH_MAJOR, 0, features->x_max, 0, 0);
4022 		input_set_abs_params(input_dev, ABS_MT_WIDTH_MINOR, 0, features->y_max, 0, 0);
4023 		input_set_abs_params(input_dev, ABS_MT_ORIENTATION, 0, 1, 0, 0);
4024 		fallthrough;
4025 
4026 	case WACOM_27QHDT:
4027 		if (wacom_wac->shared->touch->product == 0x32C ||
4028 		    wacom_wac->shared->touch->product == 0xF6) {
4029 			input_dev->evbit[0] |= BIT_MASK(EV_SW);
4030 			__set_bit(SW_MUTE_DEVICE, input_dev->swbit);
4031 			wacom_wac->has_mute_touch_switch = true;
4032 			wacom_wac->is_soft_touch_switch = true;
4033 		}
4034 		fallthrough;
4035 
4036 	case MTSCREEN:
4037 	case MTTPC:
4038 	case MTTPC_B:
4039 	case TABLETPC2FG:
4040 		input_mt_init_slots(input_dev, features->touch_max, INPUT_MT_DIRECT);
4041 		fallthrough;
4042 
4043 	case TABLETPC:
4044 	case TABLETPCE:
4045 		break;
4046 
4047 	case INTUOSHT:
4048 	case INTUOSHT2:
4049 		input_dev->evbit[0] |= BIT_MASK(EV_SW);
4050 		__set_bit(SW_MUTE_DEVICE, input_dev->swbit);
4051 		fallthrough;
4052 
4053 	case BAMBOO_PT:
4054 	case BAMBOO_TOUCH:
4055 		if (features->pktlen == WACOM_PKGLEN_BBTOUCH3) {
4056 			input_set_abs_params(input_dev,
4057 				     ABS_MT_TOUCH_MAJOR,
4058 				     0, features->x_max, 0, 0);
4059 			input_set_abs_params(input_dev,
4060 				     ABS_MT_TOUCH_MINOR,
4061 				     0, features->y_max, 0, 0);
4062 		}
4063 		input_mt_init_slots(input_dev, features->touch_max, INPUT_MT_POINTER);
4064 		break;
4065 
4066 	case BAMBOO_PAD:
4067 		input_mt_init_slots(input_dev, features->touch_max,
4068 				    INPUT_MT_POINTER);
4069 		__set_bit(BTN_LEFT, input_dev->keybit);
4070 		__set_bit(BTN_RIGHT, input_dev->keybit);
4071 		break;
4072 	}
4073 	return 0;
4074 }
4075 
wacom_numbered_button_to_key(int n)4076 static int wacom_numbered_button_to_key(int n)
4077 {
4078 	if (n < 10)
4079 		return BTN_0 + n;
4080 	else if (n < 16)
4081 		return BTN_A + (n-10);
4082 	else if (n < 18)
4083 		return BTN_BASE + (n-16);
4084 	else
4085 		return 0;
4086 }
4087 
wacom_setup_numbered_buttons(struct input_dev * input_dev,int button_count)4088 static void wacom_setup_numbered_buttons(struct input_dev *input_dev,
4089 				int button_count)
4090 {
4091 	int i;
4092 
4093 	for (i = 0; i < button_count; i++) {
4094 		int key = wacom_numbered_button_to_key(i);
4095 
4096 		if (key)
4097 			__set_bit(key, input_dev->keybit);
4098 	}
4099 }
4100 
wacom_24hd_update_leds(struct wacom * wacom,int mask,int group)4101 static void wacom_24hd_update_leds(struct wacom *wacom, int mask, int group)
4102 {
4103 	struct wacom_led *led;
4104 	int i;
4105 	bool updated = false;
4106 
4107 	/*
4108 	 * 24HD has LED group 1 to the left and LED group 0 to the right.
4109 	 * So group 0 matches the second half of the buttons and thus the mask
4110 	 * needs to be shifted.
4111 	 */
4112 	if (group == 0)
4113 		mask >>= 8;
4114 
4115 	for (i = 0; i < 3; i++) {
4116 		led = wacom_led_find(wacom, group, i);
4117 		if (!led) {
4118 			hid_err(wacom->hdev, "can't find LED %d in group %d\n",
4119 				i, group);
4120 			continue;
4121 		}
4122 		if (!updated && mask & BIT(i)) {
4123 			led->held = true;
4124 			led_trigger_event(&led->trigger, LED_FULL);
4125 		} else {
4126 			led->held = false;
4127 		}
4128 	}
4129 }
4130 
wacom_is_led_toggled(struct wacom * wacom,int button_count,int mask,int group)4131 static bool wacom_is_led_toggled(struct wacom *wacom, int button_count,
4132 				 int mask, int group)
4133 {
4134 	int group_button;
4135 
4136 	/*
4137 	 * 21UX2 has LED group 1 to the left and LED group 0
4138 	 * to the right. We need to reverse the group to match this
4139 	 * historical behavior.
4140 	 */
4141 	if (wacom->wacom_wac.features.type == WACOM_21UX2)
4142 		group = 1 - group;
4143 
4144 	group_button = group * (button_count/wacom->led.count);
4145 
4146 	if (wacom->wacom_wac.features.type == INTUOSP2_BT)
4147 		group_button = 8;
4148 
4149 	return mask & (1 << group_button);
4150 }
4151 
wacom_update_led(struct wacom * wacom,int button_count,int mask,int group)4152 static void wacom_update_led(struct wacom *wacom, int button_count, int mask,
4153 			     int group)
4154 {
4155 	struct wacom_led *led, *next_led;
4156 	int cur;
4157 	bool pressed;
4158 
4159 	if (wacom->wacom_wac.features.type == WACOM_24HD)
4160 		return wacom_24hd_update_leds(wacom, mask, group);
4161 
4162 	pressed = wacom_is_led_toggled(wacom, button_count, mask, group);
4163 	cur = wacom->led.groups[group].select;
4164 
4165 	led = wacom_led_find(wacom, group, cur);
4166 	if (!led) {
4167 		hid_err(wacom->hdev, "can't find current LED %d in group %d\n",
4168 			cur, group);
4169 		return;
4170 	}
4171 
4172 	if (!pressed) {
4173 		led->held = false;
4174 		return;
4175 	}
4176 
4177 	if (led->held && pressed)
4178 		return;
4179 
4180 	next_led = wacom_led_next(wacom, led);
4181 	if (!next_led) {
4182 		hid_err(wacom->hdev, "can't find next LED in group %d\n",
4183 			group);
4184 		return;
4185 	}
4186 	if (next_led == led)
4187 		return;
4188 
4189 	next_led->held = true;
4190 	led_trigger_event(&next_led->trigger,
4191 			  wacom_leds_brightness_get(next_led));
4192 }
4193 
wacom_report_numbered_buttons(struct input_dev * input_dev,int button_count,int mask)4194 static void wacom_report_numbered_buttons(struct input_dev *input_dev,
4195 				int button_count, int mask)
4196 {
4197 	struct wacom *wacom = input_get_drvdata(input_dev);
4198 	int i;
4199 
4200 	for (i = 0; i < wacom->led.count; i++)
4201 		wacom_update_led(wacom,  button_count, mask, i);
4202 
4203 	for (i = 0; i < button_count; i++) {
4204 		int key = wacom_numbered_button_to_key(i);
4205 
4206 		if (key)
4207 			input_report_key(input_dev, key, mask & (1 << i));
4208 	}
4209 }
4210 
wacom_setup_pad_input_capabilities(struct input_dev * input_dev,struct wacom_wac * wacom_wac)4211 int wacom_setup_pad_input_capabilities(struct input_dev *input_dev,
4212 				   struct wacom_wac *wacom_wac)
4213 {
4214 	struct wacom_features *features = &wacom_wac->features;
4215 
4216 	if ((features->type == HID_GENERIC) && features->numbered_buttons > 0)
4217 		features->device_type |= WACOM_DEVICETYPE_PAD;
4218 
4219 	if (!(features->device_type & WACOM_DEVICETYPE_PAD))
4220 		return -ENODEV;
4221 
4222 	if (features->type == REMOTE && input_dev == wacom_wac->pad_input)
4223 		return -ENODEV;
4224 
4225 	input_dev->evbit[0] |= BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
4226 
4227 	/* kept for making legacy xf86-input-wacom working with the wheels */
4228 	__set_bit(ABS_MISC, input_dev->absbit);
4229 
4230 	/* kept for making legacy xf86-input-wacom accepting the pad */
4231 	if (!(input_dev->absinfo && (input_dev->absinfo[ABS_X].minimum ||
4232 	      input_dev->absinfo[ABS_X].maximum)))
4233 		input_set_abs_params(input_dev, ABS_X, 0, 1, 0, 0);
4234 	if (!(input_dev->absinfo && (input_dev->absinfo[ABS_Y].minimum ||
4235 	      input_dev->absinfo[ABS_Y].maximum)))
4236 		input_set_abs_params(input_dev, ABS_Y, 0, 1, 0, 0);
4237 
4238 	/* kept for making udev and libwacom accepting the pad */
4239 	__set_bit(BTN_STYLUS, input_dev->keybit);
4240 
4241 	wacom_setup_numbered_buttons(input_dev, features->numbered_buttons);
4242 
4243 	switch (features->type) {
4244 
4245 	case CINTIQ_HYBRID:
4246 	case CINTIQ_COMPANION_2:
4247 	case DTK:
4248 	case DTUS:
4249 	case GRAPHIRE_BT:
4250 		break;
4251 
4252 	case WACOM_MO:
4253 		__set_bit(BTN_BACK, input_dev->keybit);
4254 		__set_bit(BTN_LEFT, input_dev->keybit);
4255 		__set_bit(BTN_FORWARD, input_dev->keybit);
4256 		__set_bit(BTN_RIGHT, input_dev->keybit);
4257 		input_set_abs_params(input_dev, ABS_WHEEL, 0, 71, 0, 0);
4258 		break;
4259 
4260 	case WACOM_G4:
4261 		__set_bit(BTN_BACK, input_dev->keybit);
4262 		__set_bit(BTN_FORWARD, input_dev->keybit);
4263 		input_set_capability(input_dev, EV_REL, REL_WHEEL);
4264 		break;
4265 
4266 	case WACOM_24HD:
4267 		__set_bit(KEY_PROG1, input_dev->keybit);
4268 		__set_bit(KEY_PROG2, input_dev->keybit);
4269 		__set_bit(KEY_PROG3, input_dev->keybit);
4270 
4271 		__set_bit(KEY_ONSCREEN_KEYBOARD, input_dev->keybit);
4272 		__set_bit(KEY_INFO, input_dev->keybit);
4273 
4274 		if (!features->oPid)
4275 			__set_bit(KEY_BUTTONCONFIG, input_dev->keybit);
4276 
4277 		input_set_abs_params(input_dev, ABS_WHEEL, 0, 71, 0, 0);
4278 		input_set_abs_params(input_dev, ABS_THROTTLE, 0, 71, 0, 0);
4279 		break;
4280 
4281 	case WACOM_27QHD:
4282 		__set_bit(KEY_PROG1, input_dev->keybit);
4283 		__set_bit(KEY_PROG2, input_dev->keybit);
4284 		__set_bit(KEY_PROG3, input_dev->keybit);
4285 
4286 		__set_bit(KEY_ONSCREEN_KEYBOARD, input_dev->keybit);
4287 		__set_bit(KEY_BUTTONCONFIG, input_dev->keybit);
4288 
4289 		if (!features->oPid)
4290 			__set_bit(KEY_CONTROLPANEL, input_dev->keybit);
4291 		input_set_abs_params(input_dev, ABS_X, -2048, 2048, 0, 0);
4292 		input_abs_set_res(input_dev, ABS_X, 1024); /* points/g */
4293 		input_set_abs_params(input_dev, ABS_Y, -2048, 2048, 0, 0);
4294 		input_abs_set_res(input_dev, ABS_Y, 1024);
4295 		input_set_abs_params(input_dev, ABS_Z, -2048, 2048, 0, 0);
4296 		input_abs_set_res(input_dev, ABS_Z, 1024);
4297 		__set_bit(INPUT_PROP_ACCELEROMETER, input_dev->propbit);
4298 		break;
4299 
4300 	case WACOM_22HD:
4301 		__set_bit(KEY_PROG1, input_dev->keybit);
4302 		__set_bit(KEY_PROG2, input_dev->keybit);
4303 		__set_bit(KEY_PROG3, input_dev->keybit);
4304 
4305 		__set_bit(KEY_BUTTONCONFIG, input_dev->keybit);
4306 		__set_bit(KEY_INFO, input_dev->keybit);
4307 		fallthrough;
4308 
4309 	case WACOM_21UX2:
4310 	case WACOM_BEE:
4311 	case CINTIQ:
4312 		input_set_abs_params(input_dev, ABS_RX, 0, 4096, 0, 0);
4313 		input_set_abs_params(input_dev, ABS_RY, 0, 4096, 0, 0);
4314 		break;
4315 
4316 	case WACOM_13HD:
4317 		input_set_abs_params(input_dev, ABS_WHEEL, 0, 71, 0, 0);
4318 		break;
4319 
4320 	case INTUOS3:
4321 	case INTUOS3L:
4322 		input_set_abs_params(input_dev, ABS_RY, 0, 4096, 0, 0);
4323 		fallthrough;
4324 
4325 	case INTUOS3S:
4326 		input_set_abs_params(input_dev, ABS_RX, 0, 4096, 0, 0);
4327 		break;
4328 
4329 	case INTUOS5:
4330 	case INTUOS5L:
4331 	case INTUOSPM:
4332 	case INTUOSPL:
4333 	case INTUOS5S:
4334 	case INTUOSPS:
4335 	case INTUOSP2_BT:
4336 	case INTUOSP2S_BT:
4337 		input_set_abs_params(input_dev, ABS_WHEEL, 0, 71, 0, 0);
4338 		break;
4339 
4340 	case INTUOS4WL:
4341 		/*
4342 		 * For Bluetooth devices, the udev rule does not work correctly
4343 		 * for pads unless we add a stylus capability, which forces
4344 		 * ID_INPUT_TABLET to be set.
4345 		 */
4346 		__set_bit(BTN_STYLUS, input_dev->keybit);
4347 		fallthrough;
4348 
4349 	case INTUOS4:
4350 	case INTUOS4L:
4351 	case INTUOS4S:
4352 		input_set_abs_params(input_dev, ABS_WHEEL, 0, 71, 0, 0);
4353 		break;
4354 
4355 	case INTUOSHT:
4356 	case BAMBOO_PT:
4357 	case BAMBOO_TOUCH:
4358 	case INTUOSHT2:
4359 		__clear_bit(ABS_MISC, input_dev->absbit);
4360 
4361 		__set_bit(BTN_LEFT, input_dev->keybit);
4362 		__set_bit(BTN_FORWARD, input_dev->keybit);
4363 		__set_bit(BTN_BACK, input_dev->keybit);
4364 		__set_bit(BTN_RIGHT, input_dev->keybit);
4365 
4366 		break;
4367 
4368 	case REMOTE:
4369 		input_set_capability(input_dev, EV_MSC, MSC_SERIAL);
4370 		input_set_abs_params(input_dev, ABS_WHEEL, 0, 71, 0, 0);
4371 		break;
4372 
4373 	case INTUOSHT3_BT:
4374 	case HID_GENERIC:
4375 		break;
4376 
4377 	default:
4378 		/* no pad supported */
4379 		return -ENODEV;
4380 	}
4381 	return 0;
4382 }
4383 
4384 static const struct wacom_features wacom_features_0x00 =
4385 	{ "Wacom Penpartner", 5040, 3780, 255, 0,
4386 	  PENPARTNER, WACOM_PENPRTN_RES, WACOM_PENPRTN_RES };
4387 static const struct wacom_features wacom_features_0x10 =
4388 	{ "Wacom Graphire", 10206, 7422, 511, 63,
4389 	  GRAPHIRE, WACOM_GRAPHIRE_RES, WACOM_GRAPHIRE_RES };
4390 static const struct wacom_features wacom_features_0x81 =
4391 	{ "Wacom Graphire BT", 16704, 12064, 511, 32,
4392 	  GRAPHIRE_BT, WACOM_GRAPHIRE_RES, WACOM_GRAPHIRE_RES, 2 };
4393 static const struct wacom_features wacom_features_0x11 =
4394 	{ "Wacom Graphire2 4x5", 10206, 7422, 511, 63,
4395 	  GRAPHIRE, WACOM_GRAPHIRE_RES, WACOM_GRAPHIRE_RES };
4396 static const struct wacom_features wacom_features_0x12 =
4397 	{ "Wacom Graphire2 5x7", 13918, 10206, 511, 63,
4398 	  GRAPHIRE, WACOM_GRAPHIRE_RES, WACOM_GRAPHIRE_RES };
4399 static const struct wacom_features wacom_features_0x13 =
4400 	{ "Wacom Graphire3", 10208, 7424, 511, 63,
4401 	  GRAPHIRE, WACOM_GRAPHIRE_RES, WACOM_GRAPHIRE_RES };
4402 static const struct wacom_features wacom_features_0x14 =
4403 	{ "Wacom Graphire3 6x8", 16704, 12064, 511, 63,
4404 	  GRAPHIRE, WACOM_GRAPHIRE_RES, WACOM_GRAPHIRE_RES };
4405 static const struct wacom_features wacom_features_0x15 =
4406 	{ "Wacom Graphire4 4x5", 10208, 7424, 511, 63,
4407 	  WACOM_G4, WACOM_GRAPHIRE_RES, WACOM_GRAPHIRE_RES };
4408 static const struct wacom_features wacom_features_0x16 =
4409 	{ "Wacom Graphire4 6x8", 16704, 12064, 511, 63,
4410 	  WACOM_G4, WACOM_GRAPHIRE_RES, WACOM_GRAPHIRE_RES };
4411 static const struct wacom_features wacom_features_0x17 =
4412 	{ "Wacom BambooFun 4x5", 14760, 9225, 511, 63,
4413 	  WACOM_MO, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4414 static const struct wacom_features wacom_features_0x18 =
4415 	{ "Wacom BambooFun 6x8", 21648, 13530, 511, 63,
4416 	  WACOM_MO, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4417 static const struct wacom_features wacom_features_0x19 =
4418 	{ "Wacom Bamboo1 Medium", 16704, 12064, 511, 63,
4419 	  GRAPHIRE, WACOM_GRAPHIRE_RES, WACOM_GRAPHIRE_RES };
4420 static const struct wacom_features wacom_features_0x60 =
4421 	{ "Wacom Volito", 5104, 3712, 511, 63,
4422 	  GRAPHIRE, WACOM_VOLITO_RES, WACOM_VOLITO_RES };
4423 static const struct wacom_features wacom_features_0x61 =
4424 	{ "Wacom PenStation2", 3250, 2320, 255, 63,
4425 	  GRAPHIRE, WACOM_VOLITO_RES, WACOM_VOLITO_RES };
4426 static const struct wacom_features wacom_features_0x62 =
4427 	{ "Wacom Volito2 4x5", 5104, 3712, 511, 63,
4428 	  GRAPHIRE, WACOM_VOLITO_RES, WACOM_VOLITO_RES };
4429 static const struct wacom_features wacom_features_0x63 =
4430 	{ "Wacom Volito2 2x3", 3248, 2320, 511, 63,
4431 	  GRAPHIRE, WACOM_VOLITO_RES, WACOM_VOLITO_RES };
4432 static const struct wacom_features wacom_features_0x64 =
4433 	{ "Wacom PenPartner2", 3250, 2320, 511, 63,
4434 	  GRAPHIRE, WACOM_VOLITO_RES, WACOM_VOLITO_RES };
4435 static const struct wacom_features wacom_features_0x65 =
4436 	{ "Wacom Bamboo", 14760, 9225, 511, 63,
4437 	  WACOM_MO, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4438 static const struct wacom_features wacom_features_0x69 =
4439 	{ "Wacom Bamboo1", 5104, 3712, 511, 63,
4440 	  GRAPHIRE, WACOM_PENPRTN_RES, WACOM_PENPRTN_RES };
4441 static const struct wacom_features wacom_features_0x6A =
4442 	{ "Wacom Bamboo1 4x6", 14760, 9225, 1023, 63,
4443 	  GRAPHIRE, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4444 static const struct wacom_features wacom_features_0x6B =
4445 	{ "Wacom Bamboo1 5x8", 21648, 13530, 1023, 63,
4446 	  GRAPHIRE, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4447 static const struct wacom_features wacom_features_0x20 =
4448 	{ "Wacom Intuos 4x5", 12700, 10600, 1023, 31,
4449 	  INTUOS, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4450 static const struct wacom_features wacom_features_0x21 =
4451 	{ "Wacom Intuos 6x8", 20320, 16240, 1023, 31,
4452 	  INTUOS, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4453 static const struct wacom_features wacom_features_0x22 =
4454 	{ "Wacom Intuos 9x12", 30480, 24060, 1023, 31,
4455 	  INTUOS, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4456 static const struct wacom_features wacom_features_0x23 =
4457 	{ "Wacom Intuos 12x12", 30480, 31680, 1023, 31,
4458 	  INTUOS, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4459 static const struct wacom_features wacom_features_0x24 =
4460 	{ "Wacom Intuos 12x18", 45720, 31680, 1023, 31,
4461 	  INTUOS, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4462 static const struct wacom_features wacom_features_0x30 =
4463 	{ "Wacom PL400", 5408, 4056, 255, 0,
4464 	  PL, WACOM_PL_RES, WACOM_PL_RES };
4465 static const struct wacom_features wacom_features_0x31 =
4466 	{ "Wacom PL500", 6144, 4608, 255, 0,
4467 	  PL, WACOM_PL_RES, WACOM_PL_RES };
4468 static const struct wacom_features wacom_features_0x32 =
4469 	{ "Wacom PL600", 6126, 4604, 255, 0,
4470 	  PL, WACOM_PL_RES, WACOM_PL_RES };
4471 static const struct wacom_features wacom_features_0x33 =
4472 	{ "Wacom PL600SX", 6260, 5016, 255, 0,
4473 	  PL, WACOM_PL_RES, WACOM_PL_RES };
4474 static const struct wacom_features wacom_features_0x34 =
4475 	{ "Wacom PL550", 6144, 4608, 511, 0,
4476 	  PL, WACOM_PL_RES, WACOM_PL_RES };
4477 static const struct wacom_features wacom_features_0x35 =
4478 	{ "Wacom PL800", 7220, 5780, 511, 0,
4479 	  PL, WACOM_PL_RES, WACOM_PL_RES };
4480 static const struct wacom_features wacom_features_0x37 =
4481 	{ "Wacom PL700", 6758, 5406, 511, 0,
4482 	  PL, WACOM_PL_RES, WACOM_PL_RES };
4483 static const struct wacom_features wacom_features_0x38 =
4484 	{ "Wacom PL510", 6282, 4762, 511, 0,
4485 	  PL, WACOM_PL_RES, WACOM_PL_RES };
4486 static const struct wacom_features wacom_features_0x39 =
4487 	{ "Wacom DTU710", 34080, 27660, 511, 0,
4488 	  PL, WACOM_PL_RES, WACOM_PL_RES };
4489 static const struct wacom_features wacom_features_0xC4 =
4490 	{ "Wacom DTF521", 6282, 4762, 511, 0,
4491 	  PL, WACOM_PL_RES, WACOM_PL_RES };
4492 static const struct wacom_features wacom_features_0xC0 =
4493 	{ "Wacom DTF720", 6858, 5506, 511, 0,
4494 	  PL, WACOM_PL_RES, WACOM_PL_RES };
4495 static const struct wacom_features wacom_features_0xC2 =
4496 	{ "Wacom DTF720a", 6858, 5506, 511, 0,
4497 	  PL, WACOM_PL_RES, WACOM_PL_RES };
4498 static const struct wacom_features wacom_features_0x03 =
4499 	{ "Wacom Cintiq Partner", 20480, 15360, 511, 0,
4500 	  PTU, WACOM_PL_RES, WACOM_PL_RES };
4501 static const struct wacom_features wacom_features_0x41 =
4502 	{ "Wacom Intuos2 4x5", 12700, 10600, 1023, 31,
4503 	  INTUOS, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4504 static const struct wacom_features wacom_features_0x42 =
4505 	{ "Wacom Intuos2 6x8", 20320, 16240, 1023, 31,
4506 	  INTUOS, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4507 static const struct wacom_features wacom_features_0x43 =
4508 	{ "Wacom Intuos2 9x12", 30480, 24060, 1023, 31,
4509 	  INTUOS, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4510 static const struct wacom_features wacom_features_0x44 =
4511 	{ "Wacom Intuos2 12x12", 30480, 31680, 1023, 31,
4512 	  INTUOS, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4513 static const struct wacom_features wacom_features_0x45 =
4514 	{ "Wacom Intuos2 12x18", 45720, 31680, 1023, 31,
4515 	  INTUOS, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4516 static const struct wacom_features wacom_features_0xB0 =
4517 	{ "Wacom Intuos3 4x5", 25400, 20320, 1023, 63,
4518 	  INTUOS3S, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 4 };
4519 static const struct wacom_features wacom_features_0xB1 =
4520 	{ "Wacom Intuos3 6x8", 40640, 30480, 1023, 63,
4521 	  INTUOS3, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 8 };
4522 static const struct wacom_features wacom_features_0xB2 =
4523 	{ "Wacom Intuos3 9x12", 60960, 45720, 1023, 63,
4524 	  INTUOS3, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 8 };
4525 static const struct wacom_features wacom_features_0xB3 =
4526 	{ "Wacom Intuos3 12x12", 60960, 60960, 1023, 63,
4527 	  INTUOS3L, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 8 };
4528 static const struct wacom_features wacom_features_0xB4 =
4529 	{ "Wacom Intuos3 12x19", 97536, 60960, 1023, 63,
4530 	  INTUOS3L, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 8 };
4531 static const struct wacom_features wacom_features_0xB5 =
4532 	{ "Wacom Intuos3 6x11", 54204, 31750, 1023, 63,
4533 	  INTUOS3, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 8 };
4534 static const struct wacom_features wacom_features_0xB7 =
4535 	{ "Wacom Intuos3 4x6", 31496, 19685, 1023, 63,
4536 	  INTUOS3S, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 4 };
4537 static const struct wacom_features wacom_features_0xB8 =
4538 	{ "Wacom Intuos4 4x6", 31496, 19685, 2047, 63,
4539 	  INTUOS4S, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 7 };
4540 static const struct wacom_features wacom_features_0xB9 =
4541 	{ "Wacom Intuos4 6x9", 44704, 27940, 2047, 63,
4542 	  INTUOS4, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 9 };
4543 static const struct wacom_features wacom_features_0xBA =
4544 	{ "Wacom Intuos4 8x13", 65024, 40640, 2047, 63,
4545 	  INTUOS4L, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 9 };
4546 static const struct wacom_features wacom_features_0xBB =
4547 	{ "Wacom Intuos4 12x19", 97536, 60960, 2047, 63,
4548 	  INTUOS4L, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 9 };
4549 static const struct wacom_features wacom_features_0xBC =
4550 	{ "Wacom Intuos4 WL", 40640, 25400, 2047, 63,
4551 	  INTUOS4, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 9 };
4552 static const struct wacom_features wacom_features_0xBD =
4553 	{ "Wacom Intuos4 WL", 40640, 25400, 2047, 63,
4554 	  INTUOS4WL, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 9 };
4555 static const struct wacom_features wacom_features_0x26 =
4556 	{ "Wacom Intuos5 touch S", 31496, 19685, 2047, 63,
4557 	  INTUOS5S, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 7, .touch_max = 16 };
4558 static const struct wacom_features wacom_features_0x27 =
4559 	{ "Wacom Intuos5 touch M", 44704, 27940, 2047, 63,
4560 	  INTUOS5, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 9, .touch_max = 16 };
4561 static const struct wacom_features wacom_features_0x28 =
4562 	{ "Wacom Intuos5 touch L", 65024, 40640, 2047, 63,
4563 	  INTUOS5L, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 9, .touch_max = 16 };
4564 static const struct wacom_features wacom_features_0x29 =
4565 	{ "Wacom Intuos5 S", 31496, 19685, 2047, 63,
4566 	  INTUOS5S, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 7 };
4567 static const struct wacom_features wacom_features_0x2A =
4568 	{ "Wacom Intuos5 M", 44704, 27940, 2047, 63,
4569 	  INTUOS5, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 9 };
4570 static const struct wacom_features wacom_features_0x314 =
4571 	{ "Wacom Intuos Pro S", 31496, 19685, 2047, 63,
4572 	  INTUOSPS, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 7, .touch_max = 16,
4573 	  .check_for_hid_type = true, .hid_type = HID_TYPE_USBNONE };
4574 static const struct wacom_features wacom_features_0x315 =
4575 	{ "Wacom Intuos Pro M", 44704, 27940, 2047, 63,
4576 	  INTUOSPM, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 9, .touch_max = 16,
4577 	  .check_for_hid_type = true, .hid_type = HID_TYPE_USBNONE };
4578 static const struct wacom_features wacom_features_0x317 =
4579 	{ "Wacom Intuos Pro L", 65024, 40640, 2047, 63,
4580 	  INTUOSPL, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 9, .touch_max = 16,
4581 	  .check_for_hid_type = true, .hid_type = HID_TYPE_USBNONE };
4582 static const struct wacom_features wacom_features_0xF4 =
4583 	{ "Wacom Cintiq 24HD", 104480, 65600, 2047, 63,
4584 	  WACOM_24HD, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 16,
4585 	  WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
4586 	  WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET };
4587 static const struct wacom_features wacom_features_0xF8 =
4588 	{ "Wacom Cintiq 24HD touch", 104480, 65600, 2047, 63, /* Pen */
4589 	  WACOM_24HD, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 16,
4590 	  WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
4591 	  WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
4592 	  .oVid = USB_VENDOR_ID_WACOM, .oPid = 0xf6 };
4593 static const struct wacom_features wacom_features_0xF6 =
4594 	{ "Wacom Cintiq 24HD touch", .type = WACOM_24HDT, /* Touch */
4595 	  .oVid = USB_VENDOR_ID_WACOM, .oPid = 0xf8, .touch_max = 10,
4596 	  .check_for_hid_type = true, .hid_type = HID_TYPE_USBNONE };
4597 static const struct wacom_features wacom_features_0x32A =
4598 	{ "Wacom Cintiq 27QHD", 120140, 67920, 2047, 63,
4599 	  WACOM_27QHD, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 0,
4600 	  WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
4601 	  WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET };
4602 static const struct wacom_features wacom_features_0x32B =
4603 	{ "Wacom Cintiq 27QHD touch", 120140, 67920, 2047, 63,
4604 	  WACOM_27QHD, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 0,
4605 	  WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
4606 	  WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
4607 	  .oVid = USB_VENDOR_ID_WACOM, .oPid = 0x32C };
4608 static const struct wacom_features wacom_features_0x32C =
4609 	{ "Wacom Cintiq 27QHD touch", .type = WACOM_27QHDT,
4610 	  .oVid = USB_VENDOR_ID_WACOM, .oPid = 0x32B, .touch_max = 10 };
4611 static const struct wacom_features wacom_features_0x3F =
4612 	{ "Wacom Cintiq 21UX", 87200, 65600, 1023, 63,
4613 	  CINTIQ, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 8 };
4614 static const struct wacom_features wacom_features_0xC5 =
4615 	{ "Wacom Cintiq 20WSX", 86680, 54180, 1023, 63,
4616 	  WACOM_BEE, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 10 };
4617 static const struct wacom_features wacom_features_0xC6 =
4618 	{ "Wacom Cintiq 12WX", 53020, 33440, 1023, 63,
4619 	  WACOM_BEE, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 10 };
4620 static const struct wacom_features wacom_features_0x304 =
4621 	{ "Wacom Cintiq 13HD", 59552, 33848, 1023, 63,
4622 	  WACOM_13HD, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 9,
4623 	  WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
4624 	  WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET };
4625 static const struct wacom_features wacom_features_0x333 =
4626 	{ "Wacom Cintiq 13HD touch", 59552, 33848, 2047, 63,
4627 	  WACOM_13HD, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 9,
4628 	  WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
4629 	  WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
4630 	  .oVid = USB_VENDOR_ID_WACOM, .oPid = 0x335 };
4631 static const struct wacom_features wacom_features_0x335 =
4632 	{ "Wacom Cintiq 13HD touch", .type = WACOM_24HDT, /* Touch */
4633 	  .oVid = USB_VENDOR_ID_WACOM, .oPid = 0x333, .touch_max = 10,
4634 	  .check_for_hid_type = true, .hid_type = HID_TYPE_USBNONE };
4635 static const struct wacom_features wacom_features_0xC7 =
4636 	{ "Wacom DTU1931", 37832, 30305, 511, 0,
4637 	  PL, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4638 static const struct wacom_features wacom_features_0xCE =
4639 	{ "Wacom DTU2231", 47864, 27011, 511, 0,
4640 	  DTU, WACOM_INTUOS_RES, WACOM_INTUOS_RES,
4641 	  .check_for_hid_type = true, .hid_type = HID_TYPE_USBMOUSE };
4642 static const struct wacom_features wacom_features_0xF0 =
4643 	{ "Wacom DTU1631", 34623, 19553, 511, 0,
4644 	  DTU, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4645 static const struct wacom_features wacom_features_0xFB =
4646 	{ "Wacom DTU1031", 22096, 13960, 511, 0,
4647 	  DTUS, WACOM_INTUOS_RES, WACOM_INTUOS_RES, 4,
4648 	  WACOM_DTU_OFFSET, WACOM_DTU_OFFSET,
4649 	  WACOM_DTU_OFFSET, WACOM_DTU_OFFSET };
4650 static const struct wacom_features wacom_features_0x32F =
4651 	{ "Wacom DTU1031X", 22672, 12928, 511, 0,
4652 	  DTUSX, WACOM_INTUOS_RES, WACOM_INTUOS_RES, 0,
4653 	  WACOM_DTU_OFFSET, WACOM_DTU_OFFSET,
4654 	  WACOM_DTU_OFFSET, WACOM_DTU_OFFSET };
4655 static const struct wacom_features wacom_features_0x336 =
4656 	{ "Wacom DTU1141", 23672, 13403, 1023, 0,
4657 	  DTUS, WACOM_INTUOS_RES, WACOM_INTUOS_RES, 4,
4658 	  WACOM_DTU_OFFSET, WACOM_DTU_OFFSET,
4659 	  WACOM_DTU_OFFSET, WACOM_DTU_OFFSET };
4660 static const struct wacom_features wacom_features_0x57 =
4661 	{ "Wacom DTK2241", 95840, 54260, 2047, 63,
4662 	  DTK, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 6,
4663 	  WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
4664 	  WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET };
4665 static const struct wacom_features wacom_features_0x59 = /* Pen */
4666 	{ "Wacom DTH2242", 95840, 54260, 2047, 63,
4667 	  DTK, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 6,
4668 	  WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
4669 	  WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
4670 	  .oVid = USB_VENDOR_ID_WACOM, .oPid = 0x5D };
4671 static const struct wacom_features wacom_features_0x5D = /* Touch */
4672 	{ "Wacom DTH2242",       .type = WACOM_24HDT,
4673 	  .oVid = USB_VENDOR_ID_WACOM, .oPid = 0x59, .touch_max = 10,
4674 	  .check_for_hid_type = true, .hid_type = HID_TYPE_USBNONE };
4675 static const struct wacom_features wacom_features_0xCC =
4676 	{ "Wacom Cintiq 21UX2", 87200, 65600, 2047, 63,
4677 	  WACOM_21UX2, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 18,
4678 	  WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
4679 	  WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET };
4680 static const struct wacom_features wacom_features_0xFA =
4681 	{ "Wacom Cintiq 22HD", 95840, 54260, 2047, 63,
4682 	  WACOM_22HD, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 18,
4683 	  WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
4684 	  WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET };
4685 static const struct wacom_features wacom_features_0x5B =
4686 	{ "Wacom Cintiq 22HDT", 95840, 54260, 2047, 63,
4687 	  WACOM_22HD, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 18,
4688 	  WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
4689 	  WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
4690 	  .oVid = USB_VENDOR_ID_WACOM, .oPid = 0x5e };
4691 static const struct wacom_features wacom_features_0x5E =
4692 	{ "Wacom Cintiq 22HDT", .type = WACOM_24HDT,
4693 	  .oVid = USB_VENDOR_ID_WACOM, .oPid = 0x5b, .touch_max = 10,
4694 	  .check_for_hid_type = true, .hid_type = HID_TYPE_USBNONE };
4695 static const struct wacom_features wacom_features_0x90 =
4696 	{ "Wacom ISDv4 90", 26202, 16325, 255, 0,
4697 	  TABLETPC, WACOM_INTUOS_RES, WACOM_INTUOS_RES }; /* Pen-only */
4698 static const struct wacom_features wacom_features_0x93 =
4699 	{ "Wacom ISDv4 93", 26202, 16325, 255, 0,
4700 	  TABLETPC, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 1 };
4701 static const struct wacom_features wacom_features_0x97 =
4702 	{ "Wacom ISDv4 97", 26202, 16325, 511, 0,
4703 	  TABLETPC, WACOM_INTUOS_RES, WACOM_INTUOS_RES }; /* Pen-only */
4704 static const struct wacom_features wacom_features_0x9A =
4705 	{ "Wacom ISDv4 9A", 26202, 16325, 255, 0,
4706 	  TABLETPC, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 1 };
4707 static const struct wacom_features wacom_features_0x9F =
4708 	{ "Wacom ISDv4 9F", 26202, 16325, 255, 0,
4709 	  TABLETPC, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 1 };
4710 static const struct wacom_features wacom_features_0xE2 =
4711 	{ "Wacom ISDv4 E2", 26202, 16325, 255, 0,
4712 	  TABLETPC2FG, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 2 };
4713 static const struct wacom_features wacom_features_0xE3 =
4714 	{ "Wacom ISDv4 E3", 26202, 16325, 255, 0,
4715 	  TABLETPC2FG, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 2 };
4716 static const struct wacom_features wacom_features_0xE5 =
4717 	{ "Wacom ISDv4 E5", 26202, 16325, 255, 0,
4718 	  MTSCREEN, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4719 static const struct wacom_features wacom_features_0xE6 =
4720 	{ "Wacom ISDv4 E6", 27760, 15694, 255, 0,
4721 	  TABLETPC2FG, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 2 };
4722 static const struct wacom_features wacom_features_0xEC =
4723 	{ "Wacom ISDv4 EC", 25710, 14500, 255, 0,
4724 	  TABLETPC,    WACOM_INTUOS_RES, WACOM_INTUOS_RES }; /* Pen-only */
4725 static const struct wacom_features wacom_features_0xED =
4726 	{ "Wacom ISDv4 ED", 26202, 16325, 255, 0,
4727 	  TABLETPCE, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 1 };
4728 static const struct wacom_features wacom_features_0xEF =
4729 	{ "Wacom ISDv4 EF", 26202, 16325, 255, 0,
4730 	  TABLETPC, WACOM_INTUOS_RES, WACOM_INTUOS_RES }; /* Pen-only */
4731 static const struct wacom_features wacom_features_0x100 =
4732 	{ "Wacom ISDv4 100", 26202, 16325, 255, 0,
4733 	  MTTPC, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4734 static const struct wacom_features wacom_features_0x101 =
4735 	{ "Wacom ISDv4 101", 26202, 16325, 255, 0,
4736 	  MTTPC, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4737 static const struct wacom_features wacom_features_0x10D =
4738 	{ "Wacom ISDv4 10D", 26202, 16325, 255, 0,
4739 	  MTTPC, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4740 static const struct wacom_features wacom_features_0x10E =
4741 	{ "Wacom ISDv4 10E", 27760, 15694, 255, 0,
4742 	  MTTPC, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4743 static const struct wacom_features wacom_features_0x10F =
4744 	{ "Wacom ISDv4 10F", 27760, 15694, 255, 0,
4745 	  MTTPC, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4746 static const struct wacom_features wacom_features_0x116 =
4747 	{ "Wacom ISDv4 116", 26202, 16325, 255, 0,
4748 	  TABLETPCE, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 1 };
4749 static const struct wacom_features wacom_features_0x12C =
4750 	{ "Wacom ISDv4 12C", 27848, 15752, 2047, 0,
4751 	  TABLETPC, WACOM_INTUOS_RES, WACOM_INTUOS_RES }; /* Pen-only */
4752 static const struct wacom_features wacom_features_0x4001 =
4753 	{ "Wacom ISDv4 4001", 26202, 16325, 255, 0,
4754 	  MTTPC, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4755 static const struct wacom_features wacom_features_0x4004 =
4756 	{ "Wacom ISDv4 4004", 11060, 6220, 255, 0,
4757 	  MTTPC_B, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4758 static const struct wacom_features wacom_features_0x5000 =
4759 	{ "Wacom ISDv4 5000", 27848, 15752, 1023, 0,
4760 	  MTTPC_B, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4761 static const struct wacom_features wacom_features_0x5002 =
4762 	{ "Wacom ISDv4 5002", 29576, 16724, 1023, 0,
4763 	  MTTPC_B, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4764 static const struct wacom_features wacom_features_0x47 =
4765 	{ "Wacom Intuos2 6x8", 20320, 16240, 1023, 31,
4766 	  INTUOS, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4767 static const struct wacom_features wacom_features_0x84 =
4768 	{ "Wacom Wireless Receiver", .type = WIRELESS, .touch_max = 16 };
4769 static const struct wacom_features wacom_features_0xD0 =
4770 	{ "Wacom Bamboo 2FG", 14720, 9200, 1023, 31,
4771 	  BAMBOO_TOUCH, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 2 };
4772 static const struct wacom_features wacom_features_0xD1 =
4773 	{ "Wacom Bamboo 2FG 4x5", 14720, 9200, 1023, 31,
4774 	  BAMBOO_PT, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 2 };
4775 static const struct wacom_features wacom_features_0xD2 =
4776 	{ "Wacom Bamboo Craft", 14720, 9200, 1023, 31,
4777 	  BAMBOO_PT, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 2 };
4778 static const struct wacom_features wacom_features_0xD3 =
4779 	{ "Wacom Bamboo 2FG 6x8", 21648, 13700, 1023, 31,
4780 	  BAMBOO_PT, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 2 };
4781 static const struct wacom_features wacom_features_0xD4 =
4782 	{ "Wacom Bamboo Pen", 14720, 9200, 1023, 31,
4783 	  BAMBOO_PEN, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4784 static const struct wacom_features wacom_features_0xD5 =
4785 	{ "Wacom Bamboo Pen 6x8", 21648, 13700, 1023, 31,
4786 	  BAMBOO_PEN, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4787 static const struct wacom_features wacom_features_0xD6 =
4788 	{ "Wacom BambooPT 2FG 4x5", 14720, 9200, 1023, 31,
4789 	  BAMBOO_PT, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 2 };
4790 static const struct wacom_features wacom_features_0xD7 =
4791 	{ "Wacom BambooPT 2FG Small", 14720, 9200, 1023, 31,
4792 	  BAMBOO_PT, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 2 };
4793 static const struct wacom_features wacom_features_0xD8 =
4794 	{ "Wacom Bamboo Comic 2FG", 21648, 13700, 1023, 31,
4795 	  BAMBOO_PT, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 2 };
4796 static const struct wacom_features wacom_features_0xDA =
4797 	{ "Wacom Bamboo 2FG 4x5 SE", 14720, 9200, 1023, 31,
4798 	  BAMBOO_PT, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 2 };
4799 static const struct wacom_features wacom_features_0xDB =
4800 	{ "Wacom Bamboo 2FG 6x8 SE", 21648, 13700, 1023, 31,
4801 	  BAMBOO_PT, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 2 };
4802 static const struct wacom_features wacom_features_0xDD =
4803         { "Wacom Bamboo Connect", 14720, 9200, 1023, 31,
4804           BAMBOO_PT, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4805 static const struct wacom_features wacom_features_0xDE =
4806         { "Wacom Bamboo 16FG 4x5", 14720, 9200, 1023, 31,
4807 	  BAMBOO_PT, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 16 };
4808 static const struct wacom_features wacom_features_0xDF =
4809         { "Wacom Bamboo 16FG 6x8", 21648, 13700, 1023, 31,
4810 	  BAMBOO_PT, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 16 };
4811 static const struct wacom_features wacom_features_0x300 =
4812 	{ "Wacom Bamboo One S", 14720, 9225, 1023, 31,
4813 	  BAMBOO_PEN, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4814 static const struct wacom_features wacom_features_0x301 =
4815 	{ "Wacom Bamboo One M", 21648, 13530, 1023, 31,
4816 	  BAMBOO_PEN, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4817 static const struct wacom_features wacom_features_0x302 =
4818 	{ "Wacom Intuos PT S", 15200, 9500, 1023, 31,
4819 	  INTUOSHT, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 16,
4820 	  .check_for_hid_type = true, .hid_type = HID_TYPE_USBNONE };
4821 static const struct wacom_features wacom_features_0x303 =
4822 	{ "Wacom Intuos PT M", 21600, 13500, 1023, 31,
4823 	  INTUOSHT, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 16,
4824 	  .check_for_hid_type = true, .hid_type = HID_TYPE_USBNONE };
4825 static const struct wacom_features wacom_features_0x30E =
4826 	{ "Wacom Intuos S", 15200, 9500, 1023, 31,
4827 	  INTUOSHT, WACOM_INTUOS_RES, WACOM_INTUOS_RES,
4828 	  .check_for_hid_type = true, .hid_type = HID_TYPE_USBNONE };
4829 static const struct wacom_features wacom_features_0x6004 =
4830 	{ "ISD-V4", 12800, 8000, 255, 0,
4831 	  TABLETPC, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4832 static const struct wacom_features wacom_features_0x307 =
4833 	{ "Wacom ISDv5 307", 59552, 33848, 2047, 63,
4834 	  CINTIQ_HYBRID, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 9,
4835 	  WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
4836 	  WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
4837 	  .oVid = USB_VENDOR_ID_WACOM, .oPid = 0x309 };
4838 static const struct wacom_features wacom_features_0x309 =
4839 	{ "Wacom ISDv5 309", .type = WACOM_24HDT, /* Touch */
4840 	  .oVid = USB_VENDOR_ID_WACOM, .oPid = 0x0307, .touch_max = 10,
4841 	  .check_for_hid_type = true, .hid_type = HID_TYPE_USBNONE };
4842 static const struct wacom_features wacom_features_0x30A =
4843 	{ "Wacom ISDv5 30A", 59552, 33848, 2047, 63,
4844 	  CINTIQ_HYBRID, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 9,
4845 	  WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
4846 	  WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
4847 	  .oVid = USB_VENDOR_ID_WACOM, .oPid = 0x30C };
4848 static const struct wacom_features wacom_features_0x30C =
4849 	{ "Wacom ISDv5 30C", .type = WACOM_24HDT, /* Touch */
4850 	  .oVid = USB_VENDOR_ID_WACOM, .oPid = 0x30A, .touch_max = 10,
4851 	  .check_for_hid_type = true, .hid_type = HID_TYPE_USBNONE };
4852 static const struct wacom_features wacom_features_0x318 =
4853 	{ "Wacom USB Bamboo PAD", 4095, 4095, /* Touch */
4854 	  .type = BAMBOO_PAD, 35, 48, .touch_max = 4 };
4855 static const struct wacom_features wacom_features_0x319 =
4856 	{ "Wacom Wireless Bamboo PAD", 4095, 4095, /* Touch */
4857 	  .type = BAMBOO_PAD, 35, 48, .touch_max = 4 };
4858 static const struct wacom_features wacom_features_0x325 =
4859 	{ "Wacom ISDv5 325", 59552, 33848, 2047, 63,
4860 	  CINTIQ_COMPANION_2, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 11,
4861 	  WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
4862 	  WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
4863 	  .oVid = USB_VENDOR_ID_WACOM, .oPid = 0x326 };
4864 static const struct wacom_features wacom_features_0x326 = /* Touch */
4865 	{ "Wacom ISDv5 326", .type = HID_GENERIC, .oVid = USB_VENDOR_ID_WACOM,
4866 	  .oPid = 0x325 };
4867 static const struct wacom_features wacom_features_0x323 =
4868 	{ "Wacom Intuos P M", 21600, 13500, 1023, 31,
4869 	  INTUOSHT, WACOM_INTUOS_RES, WACOM_INTUOS_RES,
4870 	  .check_for_hid_type = true, .hid_type = HID_TYPE_USBNONE };
4871 static const struct wacom_features wacom_features_0x331 =
4872 	{ "Wacom Express Key Remote", .type = REMOTE,
4873 	  .numbered_buttons = 18, .check_for_hid_type = true,
4874 	  .hid_type = HID_TYPE_USBNONE };
4875 static const struct wacom_features wacom_features_0x33B =
4876 	{ "Wacom Intuos S 2", 15200, 9500, 2047, 63,
4877 	  INTUOSHT2, WACOM_INTUOS_RES, WACOM_INTUOS_RES,
4878 	  .check_for_hid_type = true, .hid_type = HID_TYPE_USBNONE };
4879 static const struct wacom_features wacom_features_0x33C =
4880 	{ "Wacom Intuos PT S 2", 15200, 9500, 2047, 63,
4881 	  INTUOSHT2, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 16,
4882 	  .check_for_hid_type = true, .hid_type = HID_TYPE_USBNONE };
4883 static const struct wacom_features wacom_features_0x33D =
4884 	{ "Wacom Intuos P M 2", 21600, 13500, 2047, 63,
4885 	  INTUOSHT2, WACOM_INTUOS_RES, WACOM_INTUOS_RES,
4886 	  .check_for_hid_type = true, .hid_type = HID_TYPE_USBNONE };
4887 static const struct wacom_features wacom_features_0x33E =
4888 	{ "Wacom Intuos PT M 2", 21600, 13500, 2047, 63,
4889 	  INTUOSHT2, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 16,
4890 	  .check_for_hid_type = true, .hid_type = HID_TYPE_USBNONE };
4891 static const struct wacom_features wacom_features_0x343 =
4892 	{ "Wacom DTK1651", 34816, 19759, 1023, 0,
4893 	  DTUS, WACOM_INTUOS_RES, WACOM_INTUOS_RES, 4,
4894 	  WACOM_DTU_OFFSET, WACOM_DTU_OFFSET,
4895 	  WACOM_DTU_OFFSET, WACOM_DTU_OFFSET };
4896 static const struct wacom_features wacom_features_0x360 =
4897 	{ "Wacom Intuos Pro M", 44800, 29600, 8191, 63,
4898 	  INTUOSP2_BT, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 9, .touch_max = 10 };
4899 static const struct wacom_features wacom_features_0x361 =
4900 	{ "Wacom Intuos Pro L", 62200, 43200, 8191, 63,
4901 	  INTUOSP2_BT, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 9, .touch_max = 10 };
4902 static const struct wacom_features wacom_features_0x377 =
4903 	{ "Wacom Intuos BT S", 15200, 9500, 4095, 63,
4904 	  INTUOSHT3_BT, WACOM_INTUOS_RES, WACOM_INTUOS_RES, 4 };
4905 static const struct wacom_features wacom_features_0x379 =
4906 	{ "Wacom Intuos BT M", 21600, 13500, 4095, 63,
4907 	  INTUOSHT3_BT, WACOM_INTUOS_RES, WACOM_INTUOS_RES, 4 };
4908 static const struct wacom_features wacom_features_0x37A =
4909 	{ "Wacom One by Wacom S", 15200, 9500, 2047, 63,
4910 	  BAMBOO_PEN, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4911 static const struct wacom_features wacom_features_0x37B =
4912 	{ "Wacom One by Wacom M", 21600, 13500, 2047, 63,
4913 	  BAMBOO_PEN, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4914 static const struct wacom_features wacom_features_0x393 =
4915 	{ "Wacom Intuos Pro S", 31920, 19950, 8191, 63,
4916 	  INTUOSP2S_BT, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 7,
4917 	  .touch_max = 10 };
4918 static const struct wacom_features wacom_features_0x3c6 =
4919 	{ "Wacom Intuos BT S", 15200, 9500, 4095, 63,
4920 	  INTUOSHT3_BT, WACOM_INTUOS_RES, WACOM_INTUOS_RES, 4 };
4921 static const struct wacom_features wacom_features_0x3c8 =
4922 	{ "Wacom Intuos BT M", 21600, 13500, 4095, 63,
4923 	  INTUOSHT3_BT, WACOM_INTUOS_RES, WACOM_INTUOS_RES, 4 };
4924 static const struct wacom_features wacom_features_0x3dd =
4925 	{ "Wacom Intuos Pro S", 31920, 19950, 8191, 63,
4926 	  INTUOSP2S_BT, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 7,
4927 	  .touch_max = 10 };
4928 
4929 static const struct wacom_features wacom_features_HID_ANY_ID =
4930 	{ "Wacom HID", .type = HID_GENERIC, .oVid = HID_ANY_ID, .oPid = HID_ANY_ID };
4931 
4932 static const struct wacom_features wacom_features_0x94 =
4933 	{ "Wacom Bootloader", .type = BOOTLOADER };
4934 
4935 #define USB_DEVICE_WACOM(prod)						\
4936 	HID_DEVICE(BUS_USB, HID_GROUP_WACOM, USB_VENDOR_ID_WACOM, prod),\
4937 	.driver_data = (kernel_ulong_t)&wacom_features_##prod
4938 
4939 #define BT_DEVICE_WACOM(prod)						\
4940 	HID_DEVICE(BUS_BLUETOOTH, HID_GROUP_WACOM, USB_VENDOR_ID_WACOM, prod),\
4941 	.driver_data = (kernel_ulong_t)&wacom_features_##prod
4942 
4943 #define I2C_DEVICE_WACOM(prod)						\
4944 	HID_DEVICE(BUS_I2C, HID_GROUP_WACOM, USB_VENDOR_ID_WACOM, prod),\
4945 	.driver_data = (kernel_ulong_t)&wacom_features_##prod
4946 
4947 #define PCI_DEVICE_WACOM(prod)						\
4948 	HID_DEVICE(BUS_PCI, HID_GROUP_WACOM, USB_VENDOR_ID_WACOM, prod),\
4949 	.driver_data = (kernel_ulong_t)&wacom_features_##prod
4950 
4951 #define USB_DEVICE_LENOVO(prod)					\
4952 	HID_USB_DEVICE(USB_VENDOR_ID_LENOVO, prod),			\
4953 	.driver_data = (kernel_ulong_t)&wacom_features_##prod
4954 
4955 const struct hid_device_id wacom_ids[] = {
4956 	{ USB_DEVICE_WACOM(0x00) },
4957 	{ USB_DEVICE_WACOM(0x03) },
4958 	{ USB_DEVICE_WACOM(0x10) },
4959 	{ USB_DEVICE_WACOM(0x11) },
4960 	{ USB_DEVICE_WACOM(0x12) },
4961 	{ USB_DEVICE_WACOM(0x13) },
4962 	{ USB_DEVICE_WACOM(0x14) },
4963 	{ USB_DEVICE_WACOM(0x15) },
4964 	{ USB_DEVICE_WACOM(0x16) },
4965 	{ USB_DEVICE_WACOM(0x17) },
4966 	{ USB_DEVICE_WACOM(0x18) },
4967 	{ USB_DEVICE_WACOM(0x19) },
4968 	{ USB_DEVICE_WACOM(0x20) },
4969 	{ USB_DEVICE_WACOM(0x21) },
4970 	{ USB_DEVICE_WACOM(0x22) },
4971 	{ USB_DEVICE_WACOM(0x23) },
4972 	{ USB_DEVICE_WACOM(0x24) },
4973 	{ USB_DEVICE_WACOM(0x26) },
4974 	{ USB_DEVICE_WACOM(0x27) },
4975 	{ USB_DEVICE_WACOM(0x28) },
4976 	{ USB_DEVICE_WACOM(0x29) },
4977 	{ USB_DEVICE_WACOM(0x2A) },
4978 	{ USB_DEVICE_WACOM(0x30) },
4979 	{ USB_DEVICE_WACOM(0x31) },
4980 	{ USB_DEVICE_WACOM(0x32) },
4981 	{ USB_DEVICE_WACOM(0x33) },
4982 	{ USB_DEVICE_WACOM(0x34) },
4983 	{ USB_DEVICE_WACOM(0x35) },
4984 	{ USB_DEVICE_WACOM(0x37) },
4985 	{ USB_DEVICE_WACOM(0x38) },
4986 	{ USB_DEVICE_WACOM(0x39) },
4987 	{ USB_DEVICE_WACOM(0x3F) },
4988 	{ USB_DEVICE_WACOM(0x41) },
4989 	{ USB_DEVICE_WACOM(0x42) },
4990 	{ USB_DEVICE_WACOM(0x43) },
4991 	{ USB_DEVICE_WACOM(0x44) },
4992 	{ USB_DEVICE_WACOM(0x45) },
4993 	{ USB_DEVICE_WACOM(0x47) },
4994 	{ USB_DEVICE_WACOM(0x57) },
4995 	{ USB_DEVICE_WACOM(0x59) },
4996 	{ USB_DEVICE_WACOM(0x5B) },
4997 	{ USB_DEVICE_WACOM(0x5D) },
4998 	{ USB_DEVICE_WACOM(0x5E) },
4999 	{ USB_DEVICE_WACOM(0x60) },
5000 	{ USB_DEVICE_WACOM(0x61) },
5001 	{ USB_DEVICE_WACOM(0x62) },
5002 	{ USB_DEVICE_WACOM(0x63) },
5003 	{ USB_DEVICE_WACOM(0x64) },
5004 	{ USB_DEVICE_WACOM(0x65) },
5005 	{ USB_DEVICE_WACOM(0x69) },
5006 	{ USB_DEVICE_WACOM(0x6A) },
5007 	{ USB_DEVICE_WACOM(0x6B) },
5008 	{ BT_DEVICE_WACOM(0x81) },
5009 	{ USB_DEVICE_WACOM(0x84) },
5010 	{ USB_DEVICE_WACOM(0x90) },
5011 	{ USB_DEVICE_WACOM(0x93) },
5012 	{ USB_DEVICE_WACOM(0x94) },
5013 	{ USB_DEVICE_WACOM(0x97) },
5014 	{ USB_DEVICE_WACOM(0x9A) },
5015 	{ USB_DEVICE_WACOM(0x9F) },
5016 	{ USB_DEVICE_WACOM(0xB0) },
5017 	{ USB_DEVICE_WACOM(0xB1) },
5018 	{ USB_DEVICE_WACOM(0xB2) },
5019 	{ USB_DEVICE_WACOM(0xB3) },
5020 	{ USB_DEVICE_WACOM(0xB4) },
5021 	{ USB_DEVICE_WACOM(0xB5) },
5022 	{ USB_DEVICE_WACOM(0xB7) },
5023 	{ USB_DEVICE_WACOM(0xB8) },
5024 	{ USB_DEVICE_WACOM(0xB9) },
5025 	{ USB_DEVICE_WACOM(0xBA) },
5026 	{ USB_DEVICE_WACOM(0xBB) },
5027 	{ USB_DEVICE_WACOM(0xBC) },
5028 	{ BT_DEVICE_WACOM(0xBD) },
5029 	{ USB_DEVICE_WACOM(0xC0) },
5030 	{ USB_DEVICE_WACOM(0xC2) },
5031 	{ USB_DEVICE_WACOM(0xC4) },
5032 	{ USB_DEVICE_WACOM(0xC5) },
5033 	{ USB_DEVICE_WACOM(0xC6) },
5034 	{ USB_DEVICE_WACOM(0xC7) },
5035 	{ USB_DEVICE_WACOM(0xCC) },
5036 	{ USB_DEVICE_WACOM(0xCE) },
5037 	{ USB_DEVICE_WACOM(0xD0) },
5038 	{ USB_DEVICE_WACOM(0xD1) },
5039 	{ USB_DEVICE_WACOM(0xD2) },
5040 	{ USB_DEVICE_WACOM(0xD3) },
5041 	{ USB_DEVICE_WACOM(0xD4) },
5042 	{ USB_DEVICE_WACOM(0xD5) },
5043 	{ USB_DEVICE_WACOM(0xD6) },
5044 	{ USB_DEVICE_WACOM(0xD7) },
5045 	{ USB_DEVICE_WACOM(0xD8) },
5046 	{ USB_DEVICE_WACOM(0xDA) },
5047 	{ USB_DEVICE_WACOM(0xDB) },
5048 	{ USB_DEVICE_WACOM(0xDD) },
5049 	{ USB_DEVICE_WACOM(0xDE) },
5050 	{ USB_DEVICE_WACOM(0xDF) },
5051 	{ USB_DEVICE_WACOM(0xE2) },
5052 	{ USB_DEVICE_WACOM(0xE3) },
5053 	{ USB_DEVICE_WACOM(0xE5) },
5054 	{ USB_DEVICE_WACOM(0xE6) },
5055 	{ USB_DEVICE_WACOM(0xEC) },
5056 	{ USB_DEVICE_WACOM(0xED) },
5057 	{ USB_DEVICE_WACOM(0xEF) },
5058 	{ USB_DEVICE_WACOM(0xF0) },
5059 	{ USB_DEVICE_WACOM(0xF4) },
5060 	{ USB_DEVICE_WACOM(0xF6) },
5061 	{ USB_DEVICE_WACOM(0xF8) },
5062 	{ USB_DEVICE_WACOM(0xFA) },
5063 	{ USB_DEVICE_WACOM(0xFB) },
5064 	{ USB_DEVICE_WACOM(0x100) },
5065 	{ USB_DEVICE_WACOM(0x101) },
5066 	{ USB_DEVICE_WACOM(0x10D) },
5067 	{ USB_DEVICE_WACOM(0x10E) },
5068 	{ USB_DEVICE_WACOM(0x10F) },
5069 	{ USB_DEVICE_WACOM(0x116) },
5070 	{ USB_DEVICE_WACOM(0x12C) },
5071 	{ USB_DEVICE_WACOM(0x300) },
5072 	{ USB_DEVICE_WACOM(0x301) },
5073 	{ USB_DEVICE_WACOM(0x302) },
5074 	{ USB_DEVICE_WACOM(0x303) },
5075 	{ USB_DEVICE_WACOM(0x304) },
5076 	{ USB_DEVICE_WACOM(0x307) },
5077 	{ USB_DEVICE_WACOM(0x309) },
5078 	{ USB_DEVICE_WACOM(0x30A) },
5079 	{ USB_DEVICE_WACOM(0x30C) },
5080 	{ USB_DEVICE_WACOM(0x30E) },
5081 	{ USB_DEVICE_WACOM(0x314) },
5082 	{ USB_DEVICE_WACOM(0x315) },
5083 	{ USB_DEVICE_WACOM(0x317) },
5084 	{ USB_DEVICE_WACOM(0x318) },
5085 	{ USB_DEVICE_WACOM(0x319) },
5086 	{ USB_DEVICE_WACOM(0x323) },
5087 	{ USB_DEVICE_WACOM(0x325) },
5088 	{ USB_DEVICE_WACOM(0x326) },
5089 	{ USB_DEVICE_WACOM(0x32A) },
5090 	{ USB_DEVICE_WACOM(0x32B) },
5091 	{ USB_DEVICE_WACOM(0x32C) },
5092 	{ USB_DEVICE_WACOM(0x32F) },
5093 	{ USB_DEVICE_WACOM(0x331) },
5094 	{ USB_DEVICE_WACOM(0x333) },
5095 	{ USB_DEVICE_WACOM(0x335) },
5096 	{ USB_DEVICE_WACOM(0x336) },
5097 	{ USB_DEVICE_WACOM(0x33B) },
5098 	{ USB_DEVICE_WACOM(0x33C) },
5099 	{ USB_DEVICE_WACOM(0x33D) },
5100 	{ USB_DEVICE_WACOM(0x33E) },
5101 	{ USB_DEVICE_WACOM(0x343) },
5102 	{ BT_DEVICE_WACOM(0x360) },
5103 	{ BT_DEVICE_WACOM(0x361) },
5104 	{ BT_DEVICE_WACOM(0x377) },
5105 	{ BT_DEVICE_WACOM(0x379) },
5106 	{ USB_DEVICE_WACOM(0x37A) },
5107 	{ USB_DEVICE_WACOM(0x37B) },
5108 	{ BT_DEVICE_WACOM(0x393) },
5109 	{ BT_DEVICE_WACOM(0x3c6) },
5110 	{ BT_DEVICE_WACOM(0x3c8) },
5111 	{ BT_DEVICE_WACOM(0x3dd) },
5112 	{ USB_DEVICE_WACOM(0x4001) },
5113 	{ USB_DEVICE_WACOM(0x4004) },
5114 	{ USB_DEVICE_WACOM(0x5000) },
5115 	{ USB_DEVICE_WACOM(0x5002) },
5116 	{ USB_DEVICE_LENOVO(0x6004) },
5117 
5118 	{ USB_DEVICE_WACOM(HID_ANY_ID) },
5119 	{ I2C_DEVICE_WACOM(HID_ANY_ID) },
5120 	{ PCI_DEVICE_WACOM(HID_ANY_ID) },
5121 	{ BT_DEVICE_WACOM(HID_ANY_ID) },
5122 	{ }
5123 };
5124 MODULE_DEVICE_TABLE(hid, wacom_ids);
5125