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