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