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