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