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