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