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