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