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