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