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