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